diff --git a/src/index.ts b/src/index.ts index fc096a6..585d793 100644 --- a/src/index.ts +++ b/src/index.ts @@ -201,41 +201,14 @@ async function fastFailMetadataRequest( // 2. we can't just check the IP, which is tarpitted and slow to respond // on a user's local machine. // - // Additional logic has been added to make sure that we don't create an - // unhandled rejection in scenarios where a failure happens sometime - // after a success. + // Returns first resolved promise or if all promises get rejected we return an AggregateError. // // Note, however, if a failure happens prior to a success, a rejection should // occur, this is for folks running locally. // - let responded = false; - const r1: Promise = request(options) - .then(res => { - responded = true; - return res; - }) - .catch(err => { - if (responded) { - return r2; - } else { - responded = true; - throw err; - } - }); - const r2: Promise = request(secondaryOptions) - .then(res => { - responded = true; - return res; - }) - .catch(err => { - if (responded) { - return r1; - } else { - responded = true; - throw err; - } - }); - return Promise.race([r1, r2]); + const r1: Promise = request(options); + const r2: Promise = request(secondaryOptions); + return Promise.any([r1, r2]); } /** diff --git a/test/index.test.ts b/test/index.test.ts index 128a2dc..7ee9a4f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -461,7 +461,7 @@ describe('unit test', () => { err = _err; }; - const secondary = secondaryHostRequest(500); + const secondary = secondaryHostRequest(500, 'ENOTFOUND'); const primary = nock(HOST) .get(`${PATH}/${TYPE}`) .replyWithError({code: 'ENOTFOUND'}); @@ -470,7 +470,7 @@ describe('unit test', () => { primary.done(); console.info = info; delete process.env.DEBUG_AUTH; - assert.strictEqual(/failed, reason/.test(err!.message), true); + assert.strictEqual(/All promises were rejected/.test(err!.message), true); }); [ @@ -481,8 +481,8 @@ describe('unit test', () => { 'ENOTFOUND', 'ECONNREFUSED', ].forEach(errorCode => { - it(`should fail fast on isAvailable if ${errorCode} is returned`, async () => { - const secondary = secondaryHostRequest(500); + it(`should fail on isAvailable if ${errorCode} is returned for primary and secondary requests`, async () => { + const secondary = secondaryHostRequest(500, errorCode); const primary = nock(HOST) .get(`${PATH}/${TYPE}`) .replyWithError({code: errorCode}); @@ -493,13 +493,13 @@ describe('unit test', () => { }); }); - it('should fail fast on isAvailable if 404 status code is returned', async () => { + it('should return first successful response', async () => { const secondary = secondaryHostRequest(500); const primary = nock(HOST).get(`${PATH}/${TYPE}`).reply(404); const isGCE = await gcp.isAvailable(); await secondary; primary.done(); - assert.strictEqual(false, isGCE); + assert.strictEqual(true, isGCE); }); it('should fail fast with GCE_METADATA_HOST 404 on isAvailable', async () => { @@ -654,8 +654,8 @@ describe('unit test', () => { it('resets cache when resetIsAvailableCache() is called', async () => { // we will attempt to hit the secondary and primary server twice, // mock accordingly. - const secondary = secondaryHostRequest(250); - const secondary2 = secondaryHostRequest(500); + const secondary = secondaryHostRequest(250, 'ENOENT'); + const secondary2 = secondaryHostRequest(500, 'ENOENT'); const primary = nock(HOST) .get(`${PATH}/${TYPE}`) .reply(200, {}, HEADERS) diff --git a/tsconfig.json b/tsconfig.json index cc54f42..1b80878 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "./node_modules/gts/tsconfig-google.json", "compilerOptions": { - "lib": ["es2018", "dom"], + "lib": ["es2021", "dom"], "rootDir": ".", "outDir": "build" },