Skip to content

Commit a20f8e4

Browse files
committed
reorder reading env allowing one off commands to work when logged in
1 parent 6ce9d34 commit a20f8e4

File tree

4 files changed

+77
-55
lines changed

4 files changed

+77
-55
lines changed

lib/shadow/npm-injection.cjs

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,50 @@ try {
4040
*/
4141

4242
const pubTokenPromise = sdkPromise.then(({ getDefaultKey, FREE_API_KEY }) => getDefaultKey() || FREE_API_KEY)
43-
const apiKeySettingsPromise = sdkPromise.then(async ({ setupSdk }) => {
44-
const sdk = await setupSdk(await pubTokenPromise)
45-
const orgResult = await sdk.getOrganizations()
46-
if (!orgResult.success) {
47-
throw new Error('Failed to fetch Socket organization info: ' + orgResult.error.message)
48-
}
49-
/**
50-
* @type {(Exclude<typeof orgResult.data.organizations[string], undefined>)[]}
51-
*/
52-
const orgs = []
53-
for (const org of Object.values(orgResult.data.organizations)) {
54-
if (org) {
55-
orgs.push(org)
43+
const apiKeySettingsInit = sdkPromise.then(async ({ setupSdk }) => {
44+
try {
45+
const sdk = await setupSdk(await pubTokenPromise)
46+
const orgResult = await sdk.getOrganizations()
47+
if (!orgResult.success) {
48+
throw new Error('Failed to fetch Socket organization info: ' + orgResult.error.message)
49+
}
50+
/**
51+
* @type {(Exclude<typeof orgResult.data.organizations[string], undefined>)[]}
52+
*/
53+
const orgs = []
54+
for (const org of Object.values(orgResult.data.organizations)) {
55+
if (org) {
56+
orgs.push(org)
57+
}
58+
}
59+
const result = await sdk.postSettings(orgs.map(org => {
60+
return {
61+
organization: org.id
62+
}
63+
}))
64+
if (!result.success) {
65+
throw new Error('Failed to fetch API key settings: ' + result.error.message)
5666
}
57-
}
58-
const result = await sdk.postSettings(orgs.map(org => {
5967
return {
60-
organization: org.id
68+
orgs,
69+
settings: result.data
6170
}
62-
}))
63-
if (!result.success) {
64-
throw new Error('Failed to fetch API key settings: ' + result.error.message)
65-
}
66-
return {
67-
orgs,
68-
settings: result.data
71+
} catch (e) {
72+
if (e && typeof e === 'object' && 'cause' in e) {
73+
const cause = e.cause
74+
if (isErrnoException(cause)) {
75+
if (cause.code === 'ENOTFOUND' || cause.code === 'ECONNREFUSED') {
76+
throw new Error('Unable to connect to socket.dev, ensure internet connectivity before retrying', {
77+
cause: e
78+
})
79+
}
80+
}
81+
}
82+
throw e
6983
}
7084
})
85+
// mark apiKeySettingsInit as handled
86+
apiKeySettingsInit.catch(() => {})
7187

7288
/**
7389
*
@@ -78,42 +94,43 @@ async function findSocketYML () {
7894
const fs = require('fs/promises')
7995
while (dir !== prevDir) {
8096
const ymlPath = path.join(dir, 'socket.yml')
97+
const yml = fs.readFile(ymlPath, 'utf-8')
8198
// mark as handled
82-
const yml = fs.readFile(ymlPath, 'utf-8').catch(() => {})
99+
yml.catch(() => {})
83100
const yamlPath = path.join(dir, 'socket.yaml')
101+
const yaml = fs.readFile(yamlPath, 'utf-8')
84102
// mark as handled
85-
const yaml = fs.readFile(yamlPath, 'utf-8').catch(() => {})
86-
try {
87-
const txt = await yml
88-
if (txt != null) {
89-
return {
90-
path: ymlPath,
91-
parsed: config.parseSocketConfig(txt)
92-
}
93-
}
94-
} catch (e) {
103+
yaml.catch(() => {})
104+
/**
105+
* @param {unknown} e
106+
* @returns {boolean}
107+
*/
108+
function checkFileFoundError (e) {
95109
if (isErrnoException(e)) {
96110
if (e.code !== 'ENOENT' && e.code !== 'EISDIR') {
97111
throw e
98112
}
99-
} else {
113+
return false
114+
}
115+
return true
116+
}
117+
try {
118+
return {
119+
path: ymlPath,
120+
parsed: config.parseSocketConfig(await yml)
121+
}
122+
} catch (e) {
123+
if (checkFileFoundError(e)) {
100124
throw new Error('Found file but was unable to parse ' + ymlPath)
101125
}
102126
}
103127
try {
104-
const txt = await yaml
105-
if (txt != null) {
106-
return {
107-
path: yamlPath,
108-
parsed: config.parseSocketConfig(txt)
109-
}
128+
return {
129+
path: ymlPath,
130+
parsed: config.parseSocketConfig(await yaml)
110131
}
111132
} catch (e) {
112-
if (isErrnoException(e)) {
113-
if (e.code !== 'ENOENT' && e.code !== 'EISDIR') {
114-
throw e
115-
}
116-
} else {
133+
if (checkFileFoundError(e)) {
117134
throw new Error('Found file but was unable to parse ' + yamlPath)
118135
}
119136
}
@@ -124,11 +141,12 @@ async function findSocketYML () {
124141
}
125142

126143
/**
127-
* @type {Promise<ReturnType<import('../utils/issue-rules.cjs')['createIssueUXLookup']>>}
144+
* @type {Promise<ReturnType<import('../utils/issue-rules.cjs')['createIssueUXLookup']> | undefined>}
128145
*/
129-
const uxLookupPromise = settingsPromise.then(async ({ getSetting }) => {
146+
const uxLookupInit = settingsPromise.then(async ({ getSetting }) => {
130147
const enforcedOrgs = getSetting('enforcedOrgs') ?? []
131-
const { orgs, settings } = await apiKeySettingsPromise
148+
const remoteSettings = await apiKeySettingsInit
149+
const { orgs, settings } = remoteSettings
132150

133151
// remove any organizations not being enforced
134152
for (const [i, org] of orgs.entries()) {
@@ -152,6 +170,8 @@ const uxLookupPromise = settingsPromise.then(async ({ getSetting }) => {
152170
}
153171
return createIssueUXLookup(settings)
154172
})
173+
// mark uxLookupInit as handled
174+
uxLookupInit.catch(() => {})
155175

156176
// shadow `npm` and `npx` to mitigate subshells
157177
require('./link.cjs')(fs.realpathSync(path.join(__dirname, 'bin')), 'npm')
@@ -506,7 +526,7 @@ async function packagesHaveRiskyIssues (safeArb, _registry, pkgs, ora = null, _i
506526
const pkgDatas = []
507527
try {
508528
// TODO: determine org based on cwd, pass in
509-
const uxLookup = await uxLookupPromise
529+
const uxLookup = await uxLookupInit()
510530

511531
for await (const pkgData of batchScan(pkgs.map(pkg => pkg.pkgid))) {
512532
/**

lib/shadow/tty-server.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const path = require('path')
22
const { PassThrough } = require('stream')
3-
const { isErrnoException } = require('../utils/type-helpers.cjs')
3+
44
const ipc_version = require('../../package.json').version
5+
const { isErrnoException } = require('../utils/type-helpers.cjs')
56

67
/**
78
* @typedef {import('stream').Readable} Readable

lib/utils/sdk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let defaultKey
2020

2121
/** @returns {string | undefined} */
2222
export function getDefaultKey () {
23-
defaultKey = getSetting('apiKey') || process.env['SOCKET_SECURITY_API_KEY'] || defaultKey
23+
defaultKey = process.env['SOCKET_SECURITY_API_KEY'] || getSetting('apiKey') || defaultKey
2424
return defaultKey
2525
}
2626

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
"test/**/*"
1010
],
1111
"exclude": [
12-
"lib/shadow/**"
12+
"alib/shadow/**"
1313
],
1414
"compilerOptions": {
1515
"allowJs": true,
1616
"checkJs": true,
1717
"noEmit": true,
1818
"resolveJsonModule": true,
19-
"module": "es2022",
19+
"module": "NodeNext",
2020
"moduleResolution": "node",
21-
"target": "ESNext",
21+
"lib": ["ES2022"],
22+
"target": "es2022",
2223
"types": ["node"],
2324

2425
/* New checks being tried out */

0 commit comments

Comments
 (0)