@@ -40,34 +40,50 @@ try {
4040 */
4141
4242const 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
157177require ( './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 /**
0 commit comments