@sgedda/mockifyer-fetch 1.8.106 → 1.8.108
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.
- package/README.md +181 -0
- package/dist/clients/fetch-client.d.ts +7 -2
- package/dist/clients/fetch-client.js +47 -7
- package/dist/core-proxy.browser.d.ts +19 -0
- package/dist/core-proxy.browser.js +57 -0
- package/dist/index.browser.d.ts +53 -0
- package/dist/index.browser.js +111 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +41 -1564
- package/dist/mockifyer-class.d.ts +128 -0
- package/dist/mockifyer-class.js +1417 -0
- package/dist/node-capabilities.browser.d.ts +6 -0
- package/dist/node-capabilities.browser.js +110 -0
- package/dist/node-capabilities.d.ts +51 -0
- package/dist/node-capabilities.js +6 -0
- package/dist/node-capabilities.node.d.ts +6 -0
- package/dist/node-capabilities.node.js +31 -0
- package/dist/react-native.js +2 -1
- package/dist/utils/fetch-response-body.d.ts +18 -1
- package/dist/utils/fetch-response-body.js +18 -4
- package/package.json +13 -2
- package/src/clients/fetch-client.ts +62 -10
- package/src/core-proxy.browser.ts +62 -0
- package/src/index.browser.ts +171 -0
- package/src/index.ts +48 -1917
- package/src/mockifyer-class.ts +1830 -0
- package/src/node-capabilities.browser.ts +123 -0
- package/src/node-capabilities.node.ts +40 -0
- package/src/node-capabilities.ts +60 -0
- package/src/react-native.ts +2 -1
- package/src/utils/fetch-response-body.ts +38 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Browser implementation of NodeCapabilities.
|
|
4
|
+
* Uses Web Crypto API and provides no-ops for fs-based features.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.nodeCapabilities = void 0;
|
|
8
|
+
// Pure-JS SHA-256 implementation (from crypto-digest.ts)
|
|
9
|
+
function pureSha256Hex(input) {
|
|
10
|
+
const utf8 = unescape(encodeURIComponent(input));
|
|
11
|
+
const bytes = [];
|
|
12
|
+
for (let i = 0; i < utf8.length; i += 1) {
|
|
13
|
+
bytes.push(utf8.charCodeAt(i));
|
|
14
|
+
}
|
|
15
|
+
const K = [
|
|
16
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
17
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
18
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
19
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
20
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
21
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
22
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
23
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
24
|
+
];
|
|
25
|
+
const rotr = (n, x) => (x >>> n) | (x << (32 - n));
|
|
26
|
+
const ch = (x, y, z) => (x & y) ^ (~x & z);
|
|
27
|
+
const maj = (x, y, z) => (x & y) ^ (x & z) ^ (y & z);
|
|
28
|
+
const sigma0 = (x) => rotr(2, x) ^ rotr(13, x) ^ rotr(22, x);
|
|
29
|
+
const sigma1 = (x) => rotr(6, x) ^ rotr(11, x) ^ rotr(25, x);
|
|
30
|
+
const gamma0 = (x) => rotr(7, x) ^ rotr(18, x) ^ (x >>> 3);
|
|
31
|
+
const gamma1 = (x) => rotr(17, x) ^ rotr(19, x) ^ (x >>> 10);
|
|
32
|
+
// Padding
|
|
33
|
+
const len = bytes.length;
|
|
34
|
+
const bitLen = len * 8;
|
|
35
|
+
bytes.push(0x80);
|
|
36
|
+
while ((bytes.length % 64) !== 56) {
|
|
37
|
+
bytes.push(0);
|
|
38
|
+
}
|
|
39
|
+
for (let i = 7; i >= 0; i -= 1) {
|
|
40
|
+
bytes.push((bitLen >>> (i * 8)) & 0xff);
|
|
41
|
+
}
|
|
42
|
+
// Initial hash
|
|
43
|
+
let H = [
|
|
44
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
45
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
46
|
+
];
|
|
47
|
+
// Process blocks
|
|
48
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
49
|
+
const W = [];
|
|
50
|
+
for (let t = 0; t < 16; t += 1) {
|
|
51
|
+
W[t] =
|
|
52
|
+
(bytes[offset + t * 4] << 24) |
|
|
53
|
+
(bytes[offset + t * 4 + 1] << 16) |
|
|
54
|
+
(bytes[offset + t * 4 + 2] << 8) |
|
|
55
|
+
bytes[offset + t * 4 + 3];
|
|
56
|
+
}
|
|
57
|
+
for (let t = 16; t < 64; t += 1) {
|
|
58
|
+
W[t] = (gamma1(W[t - 2]) + W[t - 7] + gamma0(W[t - 15]) + W[t - 16]) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
61
|
+
for (let t = 0; t < 64; t += 1) {
|
|
62
|
+
const T1 = (h + sigma1(e) + ch(e, f, g) + K[t] + W[t]) >>> 0;
|
|
63
|
+
const T2 = (sigma0(a) + maj(a, b, c)) >>> 0;
|
|
64
|
+
h = g;
|
|
65
|
+
g = f;
|
|
66
|
+
f = e;
|
|
67
|
+
e = (d + T1) >>> 0;
|
|
68
|
+
d = c;
|
|
69
|
+
c = b;
|
|
70
|
+
b = a;
|
|
71
|
+
a = (T1 + T2) >>> 0;
|
|
72
|
+
}
|
|
73
|
+
H = [
|
|
74
|
+
(H[0] + a) >>> 0,
|
|
75
|
+
(H[1] + b) >>> 0,
|
|
76
|
+
(H[2] + c) >>> 0,
|
|
77
|
+
(H[3] + d) >>> 0,
|
|
78
|
+
(H[4] + e) >>> 0,
|
|
79
|
+
(H[5] + f) >>> 0,
|
|
80
|
+
(H[6] + g) >>> 0,
|
|
81
|
+
(H[7] + h) >>> 0,
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
return H.map((n) => n.toString(16).padStart(8, '0')).join('');
|
|
85
|
+
}
|
|
86
|
+
const browserCapabilities = {
|
|
87
|
+
digest(input) {
|
|
88
|
+
// Use pure-JS implementation (Web Crypto is async, we need sync)
|
|
89
|
+
return pureSha256Hex(input);
|
|
90
|
+
},
|
|
91
|
+
getCurrentScenario(_mockDataPath, _clientId) {
|
|
92
|
+
// Browser: use env var only, or default
|
|
93
|
+
return (typeof process !== 'undefined' && process.env?.MOCKIFYER_SCENARIO) || 'default';
|
|
94
|
+
},
|
|
95
|
+
getScenarioFolderPath(mockDataPath, _scenario) {
|
|
96
|
+
// Browser: return mock path as-is (no filesystem resolution)
|
|
97
|
+
return mockDataPath;
|
|
98
|
+
},
|
|
99
|
+
ensureScenarioFolder(_mockDataPath, _scenario) {
|
|
100
|
+
// Browser: no-op (no filesystem)
|
|
101
|
+
},
|
|
102
|
+
// Optional features not supported in browser
|
|
103
|
+
mirrorProxyRecording: undefined,
|
|
104
|
+
spillBodyToDisk: undefined,
|
|
105
|
+
readSpilledBody: undefined,
|
|
106
|
+
applyRequestCorrelation: undefined,
|
|
107
|
+
generateAtlasHtml: undefined,
|
|
108
|
+
};
|
|
109
|
+
// Export as nodeCapabilities for consistency (package.json maps .node to .browser)
|
|
110
|
+
exports.nodeCapabilities = browserCapabilities;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform-specific capabilities for Mockifyer.
|
|
3
|
+
* Node entry provides full implementations; browser/RN provide limited or no-op versions.
|
|
4
|
+
*/
|
|
5
|
+
export interface NodeCapabilities {
|
|
6
|
+
/**
|
|
7
|
+
* SHA-256 digest for cache keys and request hashing.
|
|
8
|
+
* Node: crypto.createHash. Browser: Web Crypto or pure-JS fallback.
|
|
9
|
+
*/
|
|
10
|
+
digest(input: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Get current scenario name from filesystem or env.
|
|
13
|
+
* Node: reads from disk/env. Browser: returns default or env-only.
|
|
14
|
+
*/
|
|
15
|
+
getCurrentScenario(mockDataPath: string, clientId?: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Get filesystem path for scenario folder.
|
|
18
|
+
* Node: resolves paths. Browser: returns mock path (no fs operations).
|
|
19
|
+
*/
|
|
20
|
+
getScenarioFolderPath(mockDataPath: string, scenario: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Ensure scenario directory exists.
|
|
23
|
+
* Node: creates directory. Browser: no-op.
|
|
24
|
+
*/
|
|
25
|
+
ensureScenarioFolder(mockDataPath: string, scenario: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Mirror proxy recording to client filesystem.
|
|
28
|
+
* Node: writes to disk. Browser: no-op or error.
|
|
29
|
+
*/
|
|
30
|
+
mirrorProxyRecording?(mockData: any, scenario: string, hash: string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Spill large response bodies to disk.
|
|
33
|
+
* Node: writes file. Browser: keeps in memory or errors.
|
|
34
|
+
*/
|
|
35
|
+
spillBodyToDisk?(body: unknown, spillPath: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Read spilled body from disk.
|
|
38
|
+
* Node: reads file. Browser: returns undefined.
|
|
39
|
+
*/
|
|
40
|
+
readSpilledBody?(spillPath: string): unknown;
|
|
41
|
+
/**
|
|
42
|
+
* HTTP request correlation (for distributed tracing).
|
|
43
|
+
* Node: uses http/https modules. Browser: header-only or no-op.
|
|
44
|
+
*/
|
|
45
|
+
applyRequestCorrelation?(request: any, context: any): void;
|
|
46
|
+
/**
|
|
47
|
+
* Generate Atlas HTML documentation.
|
|
48
|
+
* Node: full HTML generation. Browser: not supported.
|
|
49
|
+
*/
|
|
50
|
+
generateAtlasHtml?(data: any): string | undefined;
|
|
51
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Node.js implementation of NodeCapabilities.
|
|
4
|
+
* Imports real utilities from mockifyer-core that use fs/path/crypto.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.nodeCapabilities = void 0;
|
|
8
|
+
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
9
|
+
// Import crypto digest directly from source (works in both Jest and production)
|
|
10
|
+
const cryptoDigest = require('@sgedda/mockifyer-core/utils/crypto-digest');
|
|
11
|
+
const sha256Hex = cryptoDigest.sha256Hex;
|
|
12
|
+
exports.nodeCapabilities = {
|
|
13
|
+
digest(input) {
|
|
14
|
+
return sha256Hex(input);
|
|
15
|
+
},
|
|
16
|
+
getCurrentScenario(mockDataPath, clientId) {
|
|
17
|
+
return (0, mockifyer_core_1.getCurrentScenario)(mockDataPath, clientId);
|
|
18
|
+
},
|
|
19
|
+
getScenarioFolderPath(mockDataPath, scenario) {
|
|
20
|
+
return (0, mockifyer_core_1.getScenarioFolderPath)(mockDataPath, scenario);
|
|
21
|
+
},
|
|
22
|
+
ensureScenarioFolder(mockDataPath, scenario) {
|
|
23
|
+
(0, mockifyer_core_1.ensureScenarioFolder)(mockDataPath, scenario);
|
|
24
|
+
},
|
|
25
|
+
// Optional capabilities - lazily loaded when needed
|
|
26
|
+
mirrorProxyRecording: undefined, // Loaded on demand
|
|
27
|
+
spillBodyToDisk: undefined,
|
|
28
|
+
readSpilledBody: undefined,
|
|
29
|
+
applyRequestCorrelation: undefined,
|
|
30
|
+
generateAtlasHtml: undefined,
|
|
31
|
+
};
|
package/dist/react-native.js
CHANGED
|
@@ -121,7 +121,8 @@ async function loadBundledMockData(bundledDataPath) {
|
|
|
121
121
|
async function setupMockifyerForReactNative(options) {
|
|
122
122
|
const { isDev, mockDataPath = 'mock-data', bundledDataPath = './assets/mock-data', recordMode = false, startDisabled = false, persistRuntimeEnabled, config: userConfig = {}, proxyBaseUrl, proxyScenario, proxyRecordOnMiss, proxyRecordResponses, skipDashboardRedisHealthCheck, useLaunchArgumentsClientId = false, launchArgumentClientIdKey = mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY, runtimeMode: runtimeModeOption, } = options;
|
|
123
123
|
const proxyShouldRecordOnMiss = proxyRecordOnMiss ?? recordMode;
|
|
124
|
-
const
|
|
124
|
+
const recordResponsesConfig = proxyRecordResponses ?? userConfig.proxy?.recordResponses;
|
|
125
|
+
const proxyShouldRecordResponses = (0, mockifyer_core_2.resolveRecordResponses)(typeof recordResponsesConfig === 'boolean' ? recordResponsesConfig : false);
|
|
125
126
|
const launchClientIdKey = launchArgumentClientIdKey ?? mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY;
|
|
126
127
|
const clientIdFromLaunchArgs = (0, mockifyer_core_2.tryGetClientIdFromLaunchArguments)(launchClientIdKey);
|
|
127
128
|
const shouldApplyLaunchClientId = useLaunchArgumentsClientId || Boolean(clientIdFromLaunchArgs);
|
|
@@ -1,9 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch "null body status" codes — Response body must be null.
|
|
3
|
+
* Passing '' / JSON (including JSON.stringify(null) → "null") throws in undici:
|
|
4
|
+
* `TypeError: Response constructor: Invalid response status code 204`
|
|
5
|
+
*
|
|
6
|
+
* @see https://fetch.spec.whatwg.org/#null-body-status
|
|
7
|
+
*/
|
|
8
|
+
import type { StreamReplayMode, StoredResponseStream } from '@sgedda/mockifyer-core';
|
|
1
9
|
/**
|
|
2
10
|
* Whether `status` forbids a Response body under the Fetch spec.
|
|
3
11
|
*/
|
|
4
12
|
export declare function isFetchNullBodyStatus(status: number): boolean;
|
|
13
|
+
export interface BodyInitForFetchResponseOptions {
|
|
14
|
+
/** Recorded stream timeline — when set (and ReadableStream is supported), rebuild a paced stream. */
|
|
15
|
+
stream?: StoredResponseStream | null;
|
|
16
|
+
/** Replay pacing; defaults to `accelerated`. */
|
|
17
|
+
streamReplay?: StreamReplayMode | null;
|
|
18
|
+
}
|
|
5
19
|
/**
|
|
6
20
|
* Body argument for `new Response(body, { status })`.
|
|
7
21
|
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
22
|
+
* When `data` is already a ReadableStream, it is returned as-is (live passthrough).
|
|
23
|
+
* When `options.stream` is set and the runtime supports ReadableStream bodies,
|
|
24
|
+
* returns a paced replay stream; otherwise falls back to the flat concatenated body.
|
|
8
25
|
*/
|
|
9
|
-
export declare function bodyInitForFetchResponse(status: number, data: unknown): BodyInit | null;
|
|
26
|
+
export declare function bodyInitForFetchResponse(status: number, data: unknown, options?: BodyInitForFetchResponseOptions): BodyInit | null;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isFetchNullBodyStatus = isFetchNullBodyStatus;
|
|
4
|
-
exports.bodyInitForFetchResponse = bodyInitForFetchResponse;
|
|
5
2
|
/**
|
|
6
3
|
* Fetch "null body status" codes — Response body must be null.
|
|
7
4
|
* Passing '' / JSON (including JSON.stringify(null) → "null") throws in undici:
|
|
@@ -9,6 +6,10 @@ exports.bodyInitForFetchResponse = bodyInitForFetchResponse;
|
|
|
9
6
|
*
|
|
10
7
|
* @see https://fetch.spec.whatwg.org/#null-body-status
|
|
11
8
|
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.isFetchNullBodyStatus = isFetchNullBodyStatus;
|
|
11
|
+
exports.bodyInitForFetchResponse = bodyInitForFetchResponse;
|
|
12
|
+
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
12
13
|
const FETCH_NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
|
|
13
14
|
/**
|
|
14
15
|
* Whether `status` forbids a Response body under the Fetch spec.
|
|
@@ -19,11 +20,24 @@ function isFetchNullBodyStatus(status) {
|
|
|
19
20
|
/**
|
|
20
21
|
* Body argument for `new Response(body, { status })`.
|
|
21
22
|
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
23
|
+
* When `data` is already a ReadableStream, it is returned as-is (live passthrough).
|
|
24
|
+
* When `options.stream` is set and the runtime supports ReadableStream bodies,
|
|
25
|
+
* returns a paced replay stream; otherwise falls back to the flat concatenated body.
|
|
22
26
|
*/
|
|
23
|
-
function bodyInitForFetchResponse(status, data) {
|
|
27
|
+
function bodyInitForFetchResponse(status, data, options) {
|
|
24
28
|
if (isFetchNullBodyStatus(status)) {
|
|
25
29
|
return null;
|
|
26
30
|
}
|
|
31
|
+
if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
const storedStream = options?.stream;
|
|
35
|
+
if (storedStream &&
|
|
36
|
+
Array.isArray(storedStream.chunks) &&
|
|
37
|
+
storedStream.chunks.length > 0 &&
|
|
38
|
+
(0, mockifyer_core_1.supportsReadableStreamBody)()) {
|
|
39
|
+
return (0, mockifyer_core_1.createReplayReadableStream)(storedStream, (0, mockifyer_core_1.resolveStreamReplayMode)(options?.streamReplay));
|
|
40
|
+
}
|
|
27
41
|
if (typeof data === 'string') {
|
|
28
42
|
return data;
|
|
29
43
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.108",
|
|
4
4
|
"description": "Mockifyer for Fetch - API mocking and logging utility for storing and replaying API responses",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"react-native": "dist/react-native.js",
|
|
8
|
+
"browser": {
|
|
9
|
+
"./dist/index.js": "./dist/index.browser.js",
|
|
10
|
+
"./dist/core-proxy.js": "./dist/core-proxy.browser.js",
|
|
11
|
+
"./dist/node-capabilities.js": "./dist/node-capabilities.browser.js",
|
|
12
|
+
"./dist/node-capabilities.node.js": "./dist/node-capabilities.browser.js",
|
|
13
|
+
"./src/index.ts": "./src/index.browser.ts",
|
|
14
|
+
"./src/node-capabilities.ts": "./src/node-capabilities.browser.ts",
|
|
15
|
+
"./src/node-capabilities.node.ts": "./src/node-capabilities.browser.ts",
|
|
16
|
+
"./src/core-proxy.ts": "./src/core-proxy.browser.ts"
|
|
17
|
+
},
|
|
8
18
|
"exports": {
|
|
9
19
|
".": {
|
|
20
|
+
"browser": "./dist/index.browser.js",
|
|
10
21
|
"react-native": "./dist/react-native.js",
|
|
11
22
|
"types": "./dist/index.d.ts",
|
|
12
23
|
"default": "./dist/index.js"
|
|
@@ -49,7 +60,7 @@
|
|
|
49
60
|
},
|
|
50
61
|
"license": "MIT",
|
|
51
62
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.10.
|
|
63
|
+
"@sgedda/mockifyer-core": "^1.10.199"
|
|
53
64
|
},
|
|
54
65
|
"devDependencies": {
|
|
55
66
|
"@types/node": "^20.11.24",
|
|
@@ -9,8 +9,12 @@ import {
|
|
|
9
9
|
MOCKIFYER_CLIENT_ID_HEADER,
|
|
10
10
|
MOCKIFYER_DEVICE_ID_HEADER,
|
|
11
11
|
resolveMockifyerTraceFromHeaders,
|
|
12
|
+
detectStreamKind,
|
|
13
|
+
teeResponseBodyForRecording,
|
|
14
|
+
captureReadableStreamChunks,
|
|
15
|
+
supportsReadableStreamBody,
|
|
16
|
+
type StreamRecordCaps,
|
|
12
17
|
} from '@sgedda/mockifyer-core';
|
|
13
|
-
import { performDashboardProxyRequest } from '../core-proxy';
|
|
14
18
|
|
|
15
19
|
function headerContentType(headers: Headers): string | undefined {
|
|
16
20
|
const contentType = headers.get('content-type');
|
|
@@ -74,11 +78,13 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
74
78
|
private proxyScenario?: string;
|
|
75
79
|
/** When set, included as `record` on `/api/proxy`. When unset, dashboard per-scenario default applies. */
|
|
76
80
|
private proxyRecordOnMiss?: boolean;
|
|
77
|
-
private proxyRecordResponses: boolean
|
|
81
|
+
private proxyRecordResponses: boolean | Array<string | RegExp>;
|
|
78
82
|
private getClientId?: () => string | undefined;
|
|
79
83
|
private getStrictLaneScenario?: () => boolean;
|
|
80
84
|
private getUpstreamTlsInsecure?: () => boolean;
|
|
81
85
|
private getExplicitProxyScenarioContext?: () => boolean;
|
|
86
|
+
private getShouldRecordStreams?: () => boolean;
|
|
87
|
+
private getStreamRecordCaps?: () => StreamRecordCaps;
|
|
82
88
|
private clientIdSnapshot?: string;
|
|
83
89
|
private deviceId?: string;
|
|
84
90
|
|
|
@@ -89,7 +95,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
89
95
|
baseUrl: string;
|
|
90
96
|
scenario?: string;
|
|
91
97
|
recordOnMiss?: boolean;
|
|
92
|
-
recordResponses?: boolean
|
|
98
|
+
recordResponses?: boolean | Array<string | RegExp>;
|
|
93
99
|
strictLaneScenario?: boolean;
|
|
94
100
|
mirrorRecordedMocksToClient?: boolean;
|
|
95
101
|
};
|
|
@@ -99,6 +105,9 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
99
105
|
getUpstreamTlsInsecure?: () => boolean;
|
|
100
106
|
/** When false, skip `/api/proxy` and call the real URL (devtools-friendly passthrough). */
|
|
101
107
|
getExplicitProxyScenarioContext?: () => boolean;
|
|
108
|
+
/** When true, tee streaming bodies and capture chunk timelines while recording. */
|
|
109
|
+
getShouldRecordStreams?: () => boolean;
|
|
110
|
+
getStreamRecordCaps?: () => StreamRecordCaps;
|
|
102
111
|
deviceId?: string;
|
|
103
112
|
}) {
|
|
104
113
|
super();
|
|
@@ -107,11 +116,14 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
107
116
|
this.proxyBaseUrl = config?.proxy?.baseUrl;
|
|
108
117
|
this.proxyScenario = config?.proxy?.scenario;
|
|
109
118
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss;
|
|
119
|
+
// Pass recordResponses through (boolean or array) - envelope handles URL matching
|
|
110
120
|
this.proxyRecordResponses = config?.proxy?.recordResponses ?? false;
|
|
111
121
|
this.getClientId = config?.getClientId;
|
|
112
122
|
this.getStrictLaneScenario = config?.getStrictLaneScenario;
|
|
113
123
|
this.getUpstreamTlsInsecure = config?.getUpstreamTlsInsecure;
|
|
114
124
|
this.getExplicitProxyScenarioContext = config?.getExplicitProxyScenarioContext;
|
|
125
|
+
this.getShouldRecordStreams = config?.getShouldRecordStreams;
|
|
126
|
+
this.getStreamRecordCaps = config?.getStreamRecordCaps;
|
|
115
127
|
this.clientIdSnapshot = config?.clientId;
|
|
116
128
|
this.deviceId = config?.deviceId;
|
|
117
129
|
}
|
|
@@ -194,6 +206,9 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
194
206
|
headers.forEach((value, key) => {
|
|
195
207
|
headerRecord[key] = value;
|
|
196
208
|
});
|
|
209
|
+
|
|
210
|
+
// Dashboard proxy now works in both Node and browser (browser uses fetch, Node uses http)
|
|
211
|
+
const { performDashboardProxyRequest } = require('../core-proxy');
|
|
197
212
|
return performDashboardProxyRequest({
|
|
198
213
|
proxyBaseUrl: this.proxyBaseUrl,
|
|
199
214
|
url,
|
|
@@ -228,7 +243,51 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
228
243
|
}
|
|
229
244
|
|
|
230
245
|
const response = await fetchFn(url, requestConfig);
|
|
246
|
+
|
|
247
|
+
const responseHeaders: Record<string, string> = {};
|
|
248
|
+
response.headers.forEach((value: string, key: string) => {
|
|
249
|
+
responseHeaders[key] = value;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const mockifyerTrace = resolveMockifyerTraceFromHeaders(responseHeaders);
|
|
231
253
|
const contentType = response.headers.get('content-type') || '';
|
|
254
|
+
const streamKind = detectStreamKind(response.headers);
|
|
255
|
+
const canStream = supportsReadableStreamBody() && streamKind != null && response.body != null;
|
|
256
|
+
const shouldRecordStreams = this.getShouldRecordStreams?.() === true;
|
|
257
|
+
|
|
258
|
+
// Streaming path: pass the live body through without buffering so the caller
|
|
259
|
+
// is not delayed. When recording, tee and capture chunks in the background.
|
|
260
|
+
if (canStream) {
|
|
261
|
+
const caps = this.getStreamRecordCaps?.() ?? {};
|
|
262
|
+
if (shouldRecordStreams) {
|
|
263
|
+
const teed = teeResponseBodyForRecording(response.body);
|
|
264
|
+
if (teed) {
|
|
265
|
+
const recording = captureReadableStreamChunks(teed.record, streamKind, caps);
|
|
266
|
+
return {
|
|
267
|
+
data: teed.live as any,
|
|
268
|
+
status: response.status,
|
|
269
|
+
statusText: response.statusText,
|
|
270
|
+
headers: responseHeaders,
|
|
271
|
+
config,
|
|
272
|
+
mockifyerStreamRecording: recording,
|
|
273
|
+
...(mockifyerTrace ? { mockifyerTrace } : {}),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
// Tee unavailable — fall through to buffered text() so recording still works.
|
|
277
|
+
} else {
|
|
278
|
+
// Not recording: avoid buffering the live stream for the caller.
|
|
279
|
+
return {
|
|
280
|
+
data: response.body as any,
|
|
281
|
+
status: response.status,
|
|
282
|
+
statusText: response.statusText,
|
|
283
|
+
headers: responseHeaders,
|
|
284
|
+
config,
|
|
285
|
+
...(mockifyerTrace ? { mockifyerTrace } : {}),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Flat path (legacy / React Native without ReadableStream / non-stream responses)
|
|
232
291
|
const rawText = await response.text();
|
|
233
292
|
let data: any = rawText;
|
|
234
293
|
if (contentType.includes('application/json')) {
|
|
@@ -239,13 +298,6 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
239
298
|
}
|
|
240
299
|
}
|
|
241
300
|
|
|
242
|
-
const responseHeaders: Record<string, string> = {};
|
|
243
|
-
response.headers.forEach((value: string, key: string) => {
|
|
244
|
-
responseHeaders[key] = value;
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
const mockifyerTrace = resolveMockifyerTraceFromHeaders(responseHeaders);
|
|
248
|
-
|
|
249
301
|
return {
|
|
250
302
|
data,
|
|
251
303
|
status: response.status,
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser implementation for core-proxy - exports browser-compatible dashboard proxy via fetch
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function applyProxyRecordOnMissEnv(config: any): any {
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function mirrorProxyRecordingToClient(): void {
|
|
10
|
+
// Browser: no-op
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Export browser implementation of dashboard proxy (uses fetch instead of Node http)
|
|
14
|
+
export { performDashboardProxyRequest } from '@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request';
|
|
15
|
+
|
|
16
|
+
export function initMockifyerForDashboardProxy(): Promise<never> {
|
|
17
|
+
return Promise.reject(new Error(
|
|
18
|
+
'[Mockifyer Browser] Use setupMockifyer from browser entry instead of initMockifyerForDashboardProxy'
|
|
19
|
+
));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function initMockifyerForLocalFilesystem(): never {
|
|
23
|
+
throw new Error('[Mockifyer Browser] Use setupMockifyer from browser entry');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function initMockifyerForDashboardProxyClients(): Promise<never> {
|
|
27
|
+
return Promise.reject(new Error(
|
|
28
|
+
'[Mockifyer Browser] Use setupMockifyer from browser entry instead of initMockifyerForDashboardProxyClients'
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function initMockifyerForLocalFilesystemClients(): never {
|
|
33
|
+
throw new Error('[Mockifyer Browser] Use setupMockifyer from browser entry');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function resolveClientInitFlags(): any {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function needsSiblingClientSetup(): boolean {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function syncDualMockifyerControls(): void {
|
|
45
|
+
// Browser: no-op
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function pickPrimaryDualMockifyerInstance(_transport: string, _flags: any, result: any): any {
|
|
49
|
+
return result.fetch || result.axios;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function loadAxiosSetupMockifyer(): null {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function loadFetchSetupMockifyer(): null {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type InitMockifyerForDashboardProxyOptions = any;
|
|
61
|
+
export type InitMockifyerForLocalFilesystemOptions = any;
|
|
62
|
+
export type DualMockifyerControlSurface = any;
|