@naturalcycles/dev-lib 19.12.2 → 19.13.1
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 {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
|
|
1
2
|
import { red } from '@naturalcycles/nodejs-lib/colors';
|
|
2
3
|
import createMitm from 'mitm';
|
|
3
4
|
const LOCAL_HOSTS = ['localhost', '127.0.0.1'];
|
|
@@ -6,7 +7,7 @@ let mitm;
|
|
|
6
7
|
/**
|
|
7
8
|
* Based on: https://github.com/palmerj3/jest-offline/blob/master/index.js
|
|
8
9
|
*/
|
|
9
|
-
export function testOffline() {
|
|
10
|
+
export function testOffline(opt) {
|
|
10
11
|
if (mitm)
|
|
11
12
|
return; // already applied
|
|
12
13
|
if (detectLeaks) {
|
|
@@ -14,11 +15,14 @@ export function testOffline() {
|
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
17
|
mitm = createMitm();
|
|
17
|
-
mitm.on('connect', (socket,
|
|
18
|
-
const { host } =
|
|
18
|
+
mitm.on('connect', (socket, socketOptions) => {
|
|
19
|
+
const { host } = socketOptions;
|
|
19
20
|
if (!LOCAL_HOSTS.includes(host)) {
|
|
20
21
|
process.stderr.write(red(`Network request forbidden by testOffline: ${host}\n`));
|
|
21
|
-
|
|
22
|
+
opt?.onForbiddenRequest?.(host);
|
|
23
|
+
throw new AppError(`Network request forbidden by testOffline: ${host}`, {
|
|
24
|
+
backendResponseStatusCode: 410,
|
|
25
|
+
});
|
|
22
26
|
}
|
|
23
27
|
socket.bypass();
|
|
24
28
|
});
|