-
Notifications
You must be signed in to change notification settings - Fork 59
feat(toolkit-lib): network detector #926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 28 commits
7ee3f4d
7718108
91d3441
51ffbf6
d7dcdc6
f69b420
f7cd018
d0d4e93
ec0768f
60f2c12
c342cc2
671b1ee
995765b
5365dfb
d037ec8
2cbbc6b
22f49d4
4e65441
c05df0e
0d975e9
e647834
34683ea
12e07e2
0fc2d90
ae56f62
0818fb2
7f2d4ea
c33ec6c
b2822d2
0dd91d8
ae20ea5
2adf47e
d1355f1
1dc3a75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,6 @@ export * from './api/cloud-assembly'; | |
| export * from './api/io'; | ||
| export * from './api/tags'; | ||
| export * from './api/plugin'; | ||
|
|
||
| // Utilities | ||
| export { NetworkDetector } from './util/network-detector'; | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import * as https from 'node:https'; | ||
| import type { RequestOptions } from 'node:https'; | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import { cdkCacheDir } from './'; | ||
|
|
||
| interface CachedConnectivity { | ||
| expiration: number; | ||
| hasConnectivity: boolean; | ||
| } | ||
|
|
||
| const TIME_TO_LIVE_SUCCESS = 60 * 60 * 1000; // 1 hour | ||
| const CACHE_FILE_PATH = path.join(cdkCacheDir(), 'connection.json'); | ||
|
|
||
| /** | ||
| * Detects internet connectivity by making a lightweight request to the notices endpoint | ||
| */ | ||
| export class NetworkDetector { | ||
| /** | ||
| * Check if internet connectivity is available | ||
| */ | ||
| public static async hasConnectivity(agent?: https.Agent, ioHelper?: any): Promise<boolean> { | ||
| const cachedData = await this.load(); | ||
| const expiration = cachedData.expiration ?? 0; | ||
| await ioHelper?.notify({ | ||
| message: `hasconnectivity, ${JSON.stringify(cachedData)}`, | ||
| time: new Date(Date.now()), | ||
| level: 'info', | ||
| data: undefined, | ||
| }); | ||
|
|
||
| if (Date.now() > expiration) { | ||
| try { | ||
| const connected = await this.ping(agent); | ||
| const updatedData = { | ||
| expiration: Date.now() + TIME_TO_LIVE_SUCCESS, | ||
| hasConnectivity: connected, | ||
| }; | ||
| await this.save(updatedData); | ||
| return connected; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } else { | ||
| return cachedData.hasConnectivity; | ||
| } | ||
| } | ||
|
|
||
| // private static readonly TIMEOUT_MS = 500; | ||
| private static readonly URL = 'https://cli.cdk.dev-tools.aws.dev/notices.json'; | ||
|
|
||
| private static async load(): Promise<CachedConnectivity> { | ||
| const defaultValue = { | ||
| expiration: 0, | ||
| hasConnectivity: false, | ||
| }; | ||
|
|
||
| try { | ||
| return fs.existsSync(CACHE_FILE_PATH) | ||
| ? await fs.readJSON(CACHE_FILE_PATH) as CachedConnectivity | ||
| : defaultValue; | ||
| } catch { | ||
| return defaultValue; | ||
| } | ||
| } | ||
|
|
||
| private static async save(cached: CachedConnectivity): Promise<void> { | ||
| try { | ||
| await fs.ensureFile(CACHE_FILE_PATH); | ||
| await fs.writeJSON(CACHE_FILE_PATH, cached); | ||
| } catch { | ||
| // Silently ignore cache save errors | ||
| } | ||
| } | ||
|
|
||
| private static ping(agent?: https.Agent): Promise<boolean> { | ||
| const options: RequestOptions = { | ||
| // method: 'HEAD', | ||
| agent: agent, | ||
| // timeout: this.TIMEOUT_MS, | ||
| }; | ||
|
|
||
| return new Promise((resolve) => { | ||
| const req = https.request( | ||
| NetworkDetector.URL, | ||
| options, | ||
| (res) => { | ||
| resolve(res.statusCode !== undefined && res.statusCode < 500); | ||
| }, | ||
| ); | ||
| req.on('error', () => resolve(false)); | ||
| req.on('timeout', () => { | ||
| req.destroy(); | ||
| resolve(false); | ||
| }); | ||
|
|
||
| req.end(); | ||
| }); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.