@@ -40,34 +40,50 @@ try {
40
40
*/
41
41
42
42
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 )
56
66
}
57
- }
58
- const result = await sdk . postSettings ( orgs . map ( org => {
59
67
return {
60
- organization : org . id
68
+ orgs,
69
+ settings : result . data
61
70
}
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
69
83
}
70
84
} )
85
+ // mark apiKeySettingsInit as handled
86
+ apiKeySettingsInit . catch ( ( ) => { } )
71
87
72
88
/**
73
89
*
@@ -78,42 +94,43 @@ async function findSocketYML () {
78
94
const fs = require ( 'fs/promises' )
79
95
while ( dir !== prevDir ) {
80
96
const ymlPath = path . join ( dir , 'socket.yml' )
97
+ const yml = fs . readFile ( ymlPath , 'utf-8' )
81
98
// mark as handled
82
- const yml = fs . readFile ( ymlPath , 'utf-8' ) . catch ( ( ) => { } )
99
+ yml . catch ( ( ) => { } )
83
100
const yamlPath = path . join ( dir , 'socket.yaml' )
101
+ const yaml = fs . readFile ( yamlPath , 'utf-8' )
84
102
// 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 ) {
95
109
if ( isErrnoException ( e ) ) {
96
110
if ( e . code !== 'ENOENT' && e . code !== 'EISDIR' ) {
97
111
throw e
98
112
}
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 ) ) {
100
124
throw new Error ( 'Found file but was unable to parse ' + ymlPath )
101
125
}
102
126
}
103
127
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 )
110
131
}
111
132
} catch ( e ) {
112
- if ( isErrnoException ( e ) ) {
113
- if ( e . code !== 'ENOENT' && e . code !== 'EISDIR' ) {
114
- throw e
115
- }
116
- } else {
133
+ if ( checkFileFoundError ( e ) ) {
117
134
throw new Error ( 'Found file but was unable to parse ' + yamlPath )
118
135
}
119
136
}
@@ -124,11 +141,12 @@ async function findSocketYML () {
124
141
}
125
142
126
143
/**
127
- * @type {Promise<ReturnType<import('../utils/issue-rules.cjs')['createIssueUXLookup']>> }
144
+ * @type {Promise<ReturnType<import('../utils/issue-rules.cjs')['createIssueUXLookup']> | undefined > }
128
145
*/
129
- const uxLookupPromise = settingsPromise . then ( async ( { getSetting } ) => {
146
+ const uxLookupInit = settingsPromise . then ( async ( { getSetting } ) => {
130
147
const enforcedOrgs = getSetting ( 'enforcedOrgs' ) ?? [ ]
131
- const { orgs, settings } = await apiKeySettingsPromise
148
+ const remoteSettings = await apiKeySettingsInit
149
+ const { orgs, settings } = remoteSettings
132
150
133
151
// remove any organizations not being enforced
134
152
for ( const [ i , org ] of orgs . entries ( ) ) {
@@ -152,6 +170,8 @@ const uxLookupPromise = settingsPromise.then(async ({ getSetting }) => {
152
170
}
153
171
return createIssueUXLookup ( settings )
154
172
} )
173
+ // mark uxLookupInit as handled
174
+ uxLookupInit . catch ( ( ) => { } )
155
175
156
176
// shadow `npm` and `npx` to mitigate subshells
157
177
require ( './link.cjs' ) ( fs . realpathSync ( path . join ( __dirname , 'bin' ) ) , 'npm' )
@@ -506,7 +526,7 @@ async function packagesHaveRiskyIssues (safeArb, _registry, pkgs, ora = null, _i
506
526
const pkgDatas = [ ]
507
527
try {
508
528
// TODO: determine org based on cwd, pass in
509
- const uxLookup = await uxLookupPromise
529
+ const uxLookup = await uxLookupInit ( )
510
530
511
531
for await ( const pkgData of batchScan ( pkgs . map ( pkg => pkg . pkgid ) ) ) {
512
532
/**
0 commit comments