generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathweb-data-source.ts
More file actions
135 lines (121 loc) · 4.28 KB
/
web-data-source.ts
File metadata and controls
135 lines (121 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type { ClientRequest } from 'node:http';
import type { RequestOptions } from 'node:https';
import * as https from 'node:https';
import type { Notice, NoticeDataSource } from './types';
import { ToolkitError } from '../../toolkit/toolkit-error';
import { formatErrorMessage, humanHttpStatusError, humanNetworkError } from '../../util';
import { NetworkDetector } from '../../util/network-detector';
import type { IoHelper } from '../io/private';
/**
* A data source that fetches notices from the CDK notices data source
*/
export class WebsiteNoticeDataSourceProps {
/**
* The URL to load notices from.
*
* Note this must be a valid JSON document in the CDK notices data schema.
*
* @see https://github.com/cdklabs/aws-cdk-notices
*
* @default - Official CDK notices
*/
readonly url?: string | URL;
/**
* The agent responsible for making the network requests.
*
* Use this so set up a proxy connection.
*
* @default - Uses the shared global node agent
*/
readonly agent?: https.Agent;
/**
* Whether or not we want to skip the check for if we have already determined we are in
* a network-less environment. Forces WebsiteNoticeDataSource to make a network call.
*
* @default false
*/
readonly skipNetworkCache?: boolean;
}
export class WebsiteNoticeDataSource implements NoticeDataSource {
/**
* The URL notices are loaded from.
*/
public readonly url: any;
private readonly agent?: https.Agent;
private readonly skipNetworkCache?: boolean;
constructor(private readonly ioHelper: IoHelper, props: WebsiteNoticeDataSourceProps = {}) {
this.agent = props.agent;
this.url = props.url ?? 'https://cli.cdk.dev-tools.aws.dev/notices.json';
this.skipNetworkCache = props.skipNetworkCache;
}
async fetch(): Promise<Notice[]> {
if (!this.skipNetworkCache) {
await this.ioHelper.notify({
message: `website data source fetch starting, ${this.agent !== undefined}}`,
time: new Date(Date.now()),
level: 'info',
data: undefined,
});
// Check connectivity before attempting network request
const hasConnectivity = await NetworkDetector.hasConnectivity(this.agent);
if (!hasConnectivity) {
throw new ToolkitError('No internet connectivity detected');
}
}
// We are observing lots of timeouts when running in a massively parallel
// integration test environment, so wait for a longer timeout there.
//
// In production, have a short timeout to not hold up the user experience.
const timeout = process.env.TESTING_CDK ? 30_000 : 3_000;
const options: RequestOptions = {
agent: this.agent,
};
const notices = await new Promise<Notice[]>((resolve, reject) => {
let req: ClientRequest | undefined;
let timer = setTimeout(() => {
if (req) {
req.destroy(new ToolkitError('Request timed out'));
}
}, timeout);
timer.unref();
try {
req = https.get(
this.url,
options,
res => {
if (res.statusCode === 200) {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const data = JSON.parse(rawData).notices as Notice[];
if (!data) {
throw new ToolkitError("'notices' key is missing from received data");
}
resolve(data ?? []);
} catch (e: any) {
reject(ToolkitError.withCause(`Parse error: ${formatErrorMessage(e)}`, e));
}
});
res.on('error', e => {
reject(ToolkitError.withCause(formatErrorMessage(e), e));
});
} else {
reject(new ToolkitError(`${humanHttpStatusError(res.statusCode!)} (Status code: ${res.statusCode})`));
}
},
);
req.on('error', e => {
reject(ToolkitError.withCause(humanNetworkError(e), e));
});
} catch (e: any) {
reject(ToolkitError.withCause(formatErrorMessage(e), e));
}
});
await this.ioHelper.defaults.debug('Notices refreshed');
return notices;
}
}