Skip to content

Commit

Permalink
Merge branch 'main' into otaviom/temp-custom-resource-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jul 18, 2022
2 parents 22564af + 7c4cd96 commit 0c69e63
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions packages/aws-cdk/lib/notices.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ClientRequest } from 'http';
import * as https from 'https';
import * as path from 'path';
import * as fs from 'fs-extra';
Expand Down Expand Up @@ -108,9 +109,18 @@ export class WebsiteNoticeDataSource implements NoticeDataSource {
fetch(): Promise<Notice[]> {
const timeout = 3000;
return new Promise((resolve, reject) => {
let req: ClientRequest | undefined;

let timer = setTimeout(() => {
if (req) {
req.destroy(new Error('Request timed out'));
}
}, timeout);

timer.unref();

try {
const req = https.get('https://cli.cdk.dev-tools.aws.dev/notices.json',
{ timeout },
req = https.get('https://cli.cdk.dev-tools.aws.dev/notices.json',
res => {
if (res.statusCode === 200) {
res.setEncoding('utf8');
Expand Down Expand Up @@ -138,20 +148,6 @@ export class WebsiteNoticeDataSource implements NoticeDataSource {
}
});
req.on('error', reject);
req.on('timeout', () => {
// The 'timeout' event doesn't stop anything by itself, it just
// notifies that it has been long time since we saw bytes.
// In our case, we want to give up.
req.destroy(new Error('Request timed out'));
});

// It's not like I don't *trust* the 'timeout' event... but I don't trust it.
// Add a backup timer that will destroy the request after all.
// (This is at least necessary to make the tests pass, but that's probably because of 'nock'.
// It's not clear whether users will hit this).
setTimeout(() => {
req.destroy(new Error('Request timed out. You should never see this message; if you do, please let us know at https://github.com/aws/aws-cdk/issues'));
}, timeout + 200);
} catch (e) {
reject(new Error(`HTTPS 'get' call threw an error: ${e.message}`));
}
Expand Down

0 comments on commit 0c69e63

Please sign in to comment.