Skip to content

Commit ef51d7a

Browse files
committed
Fix blessed screen errors on exit
1 parent a1f66b5 commit ef51d7a

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/commands/analytics/display-analytics.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ ${mdTableStringNumber('Name', 'Counts', data['top_five_alert_types'])}
208208

209209
function displayAnalyticsScreen(data: FormattedData): void {
210210
const ScreenWidget = require('blessed/lib/widgets/screen')
211-
const screen: Widgets.Screen = new ScreenWidget({})
211+
// Lazily access constants.blessedOptions.
212+
const screen: Widgets.Screen = new ScreenWidget({
213+
...constants.blessedOptions
214+
})
212215
const contrib = require('blessed-contrib')
213216
const grid = new contrib.grid({ rows: 5, cols: 4, screen })
214217

src/commands/threat-feed/output-threat-feed.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { logger } from '@socketsecurity/registry/lib/logger'
22

3-
import { ThreadFeedResponse, ThreatResult } from './types'
3+
import constants from '../../constants'
44

5+
import type { ThreadFeedResponse, ThreatResult } from './types'
56
import type { Widgets } from 'blessed'
67

78
export async function outputThreatFeed(
@@ -27,7 +28,10 @@ export async function outputThreatFeed(
2728

2829
// Note: this temporarily takes over the terminal (just like `man` does).
2930
const ScreenWidget = require('blessed/lib/widgets/screen')
30-
const screen: Widgets.Screen = new ScreenWidget()
31+
// Lazily access constants.blessedOptions.
32+
const screen: Widgets.Screen = new ScreenWidget({
33+
...constants.blessedOptions
34+
})
3135
// Register these keys first so you can always exit, even when it gets stuck
3236
// If we don't do this and the code crashes, the user must hard-kill the
3337
// node process just to exit it. That's very bad UX.

src/constants.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type ENV = Remap<
5656
SOCKET_SECURITY_API_PROXY: string
5757
SOCKET_SECURITY_API_TOKEN: string
5858
SOCKET_SECURITY_GITHUB_PAT: string
59+
TERM: string
5960
XDG_DATA_HOME: string
6061
}>
6162
>
@@ -128,6 +129,7 @@ type Constants = Remap<
128129
readonly SOCKET_SECURITY_API_PROXY: 'SOCKET_SECURITY_API_PROXY'
129130
readonly SOCKET_SECURITY_API_TOKEN: 'SOCKET_SECURITY_API_TOKEN'
130131
readonly SOCKET_SECURITY_GITHUB_PAT: 'SOCKET_SECURITY_GITHUB_PAT'
132+
readonly TERM: 'TERM'
131133
readonly VLT: 'vlt'
132134
readonly WITH_SENTRY: 'with-sentry'
133135
readonly XDG_DATA_HOME: 'XDG_DATA_HOME'
@@ -136,6 +138,11 @@ type Constants = Remap<
136138
readonly YARN_CLASSIC: 'yarn/classic'
137139
readonly YARN_LOCK: 'yarn.lock'
138140
readonly bashRcPath: string
141+
readonly blessedOptions: {
142+
smartCSR: boolean
143+
term: string
144+
useBCE: boolean
145+
}
139146
readonly distCliPath: string
140147
readonly distInstrumentWithSentryPath: string
141148
readonly distPath: string
@@ -209,6 +216,7 @@ const SOCKET_SECURITY_API_BASE_URL = 'SOCKET_SECURITY_API_BASE_URL'
209216
const SOCKET_SECURITY_API_PROXY = 'SOCKET_SECURITY_API_PROXY'
210217
const SOCKET_SECURITY_API_TOKEN = 'SOCKET_SECURITY_API_TOKEN'
211218
const SOCKET_SECURITY_GITHUB_PAT = 'SOCKET_SECURITY_GITHUB_PAT'
219+
const TERM = 'TERM'
212220
const VLT = 'vlt'
213221
const WITH_SENTRY = 'with-sentry'
214222
const XDG_DATA_HOME = 'XDG_DATA_HOME'
@@ -287,6 +295,8 @@ const LAZY_ENV = () => {
287295
// access token with read/write permissions set for "Contents" and "Pull Request".
288296
// https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
289297
SOCKET_SECURITY_GITHUB_PAT: envAsString(env['SOCKET_SECURITY_GITHUB_PAT']),
298+
// Specifies the type of terminal or terminal emulator being used by the process.
299+
TERM: envAsString(env['TERM']),
290300
// The location of the base directory on Linux and MacOS used to store
291301
// user-specific data files, defaulting to $HOME/.local/share if not set or empty.
292302
XDG_DATA_HOME: envAsString(env['XDG_DATA_HOME'])
@@ -297,6 +307,14 @@ const lazyBashRcPath = () =>
297307
// Lazily access constants.homePath.
298308
path.join(constants.homePath, '.bashrc')
299309

310+
const lazyBlessedOptions = () =>
311+
Object.freeze({
312+
smartCSR: true,
313+
// Lazily access constants.WIN32.
314+
term: constants.WIN32 ? 'windows-ansi' : 'xterm',
315+
useBCE: true
316+
})
317+
300318
const lazyDistCliPath = () =>
301319
// Lazily access constants.distPath.
302320
path.join(constants.distPath, 'cli.js')
@@ -451,6 +469,7 @@ const constants = createConstantsObject(
451469
SOCKET_SECURITY_API_PROXY,
452470
SOCKET_SECURITY_API_TOKEN,
453471
SOCKET_SECURITY_GITHUB_PAT,
472+
TERM,
454473
VLT,
455474
WITH_SENTRY,
456475
XDG_DATA_HOME,
@@ -459,6 +478,7 @@ const constants = createConstantsObject(
459478
YARN_CLASSIC,
460479
YARN_LOCK,
461480
bashRcPath: undefined,
481+
blessedOptions: undefined,
462482
distCliPath: undefined,
463483
distInstrumentWithSentryPath: undefined,
464484
distPath: undefined,
@@ -479,6 +499,7 @@ const constants = createConstantsObject(
479499
DIST_TYPE: LAZY_DIST_TYPE,
480500
ENV: LAZY_ENV,
481501
bashRcPath: lazyBashRcPath,
502+
blessedOptions: lazyBlessedOptions,
482503
distCliPath: lazyDistCliPath,
483504
distInstrumentWithSentryPath: lazyDistInstrumentWithSentryPath,
484505
distPath: lazyDistPath,

0 commit comments

Comments
 (0)