@naturalcycles/dev-lib 19.12.1 → 19.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Based on: https://github.com/palmerj3/jest-offline/blob/master/index.js
|
|
3
3
|
*/
|
|
4
|
-
export declare function testOffline(): void;
|
|
4
|
+
export declare function testOffline(opt?: TestOfflineOptions): void;
|
|
5
5
|
/**
|
|
6
6
|
* Undo/reset the testOffline() function by allowing network calls again.
|
|
7
7
|
*/
|
|
8
8
|
export declare function testOnline(): void;
|
|
9
|
+
interface TestOfflineOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Called when a forbidden network request is detected.
|
|
12
|
+
*/
|
|
13
|
+
onForbiddenRequest?: (host: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -6,17 +6,19 @@ let mitm;
|
|
|
6
6
|
/**
|
|
7
7
|
* Based on: https://github.com/palmerj3/jest-offline/blob/master/index.js
|
|
8
8
|
*/
|
|
9
|
-
export function testOffline() {
|
|
9
|
+
export function testOffline(opt) {
|
|
10
|
+
if (mitm)
|
|
11
|
+
return; // already applied
|
|
10
12
|
if (detectLeaks) {
|
|
11
13
|
console.log('NOT applying testOffline() when --detectLeaks is on');
|
|
12
14
|
return;
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
mitm
|
|
16
|
-
|
|
17
|
-
const { host } = opts;
|
|
16
|
+
mitm = createMitm();
|
|
17
|
+
mitm.on('connect', (socket, socketOptions) => {
|
|
18
|
+
const { host } = socketOptions;
|
|
18
19
|
if (!LOCAL_HOSTS.includes(host)) {
|
|
19
20
|
process.stderr.write(red(`Network request forbidden by testOffline: ${host}\n`));
|
|
21
|
+
opt?.onForbiddenRequest?.(host);
|
|
20
22
|
throw new Error(`Network request forbidden by testOffline: ${host}`);
|
|
21
23
|
}
|
|
22
24
|
socket.bypass();
|
|
@@ -27,4 +29,5 @@ export function testOffline() {
|
|
|
27
29
|
*/
|
|
28
30
|
export function testOnline() {
|
|
29
31
|
mitm?.disable();
|
|
32
|
+
mitm = undefined;
|
|
30
33
|
}
|