@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,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser implementation of NodeCapabilities.
|
|
3
|
+
* Uses Web Crypto API and provides no-ops for fs-based features.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NodeCapabilities } from './node-capabilities';
|
|
7
|
+
|
|
8
|
+
// Pure-JS SHA-256 implementation (from crypto-digest.ts)
|
|
9
|
+
function pureSha256Hex(input: string): string {
|
|
10
|
+
const utf8 = unescape(encodeURIComponent(input));
|
|
11
|
+
const bytes: number[] = [];
|
|
12
|
+
for (let i = 0; i < utf8.length; i += 1) {
|
|
13
|
+
bytes.push(utf8.charCodeAt(i));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const K = [
|
|
17
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
18
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
19
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
20
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
21
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
22
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
23
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
24
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const rotr = (n: number, x: number): number => (x >>> n) | (x << (32 - n));
|
|
28
|
+
const ch = (x: number, y: number, z: number): number => (x & y) ^ (~x & z);
|
|
29
|
+
const maj = (x: number, y: number, z: number): number => (x & y) ^ (x & z) ^ (y & z);
|
|
30
|
+
const sigma0 = (x: number): number => rotr(2, x) ^ rotr(13, x) ^ rotr(22, x);
|
|
31
|
+
const sigma1 = (x: number): number => rotr(6, x) ^ rotr(11, x) ^ rotr(25, x);
|
|
32
|
+
const gamma0 = (x: number): number => rotr(7, x) ^ rotr(18, x) ^ (x >>> 3);
|
|
33
|
+
const gamma1 = (x: number): number => rotr(17, x) ^ rotr(19, x) ^ (x >>> 10);
|
|
34
|
+
|
|
35
|
+
// Padding
|
|
36
|
+
const len = bytes.length;
|
|
37
|
+
const bitLen = len * 8;
|
|
38
|
+
bytes.push(0x80);
|
|
39
|
+
while ((bytes.length % 64) !== 56) {
|
|
40
|
+
bytes.push(0);
|
|
41
|
+
}
|
|
42
|
+
for (let i = 7; i >= 0; i -= 1) {
|
|
43
|
+
bytes.push((bitLen >>> (i * 8)) & 0xff);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Initial hash
|
|
47
|
+
let H = [
|
|
48
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
49
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
// Process blocks
|
|
53
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
54
|
+
const W: number[] = [];
|
|
55
|
+
for (let t = 0; t < 16; t += 1) {
|
|
56
|
+
W[t] =
|
|
57
|
+
(bytes[offset + t * 4] << 24) |
|
|
58
|
+
(bytes[offset + t * 4 + 1] << 16) |
|
|
59
|
+
(bytes[offset + t * 4 + 2] << 8) |
|
|
60
|
+
bytes[offset + t * 4 + 3];
|
|
61
|
+
}
|
|
62
|
+
for (let t = 16; t < 64; t += 1) {
|
|
63
|
+
W[t] = (gamma1(W[t - 2]) + W[t - 7] + gamma0(W[t - 15]) + W[t - 16]) >>> 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
67
|
+
for (let t = 0; t < 64; t += 1) {
|
|
68
|
+
const T1 = (h + sigma1(e) + ch(e, f, g) + K[t] + W[t]) >>> 0;
|
|
69
|
+
const T2 = (sigma0(a) + maj(a, b, c)) >>> 0;
|
|
70
|
+
h = g;
|
|
71
|
+
g = f;
|
|
72
|
+
f = e;
|
|
73
|
+
e = (d + T1) >>> 0;
|
|
74
|
+
d = c;
|
|
75
|
+
c = b;
|
|
76
|
+
b = a;
|
|
77
|
+
a = (T1 + T2) >>> 0;
|
|
78
|
+
}
|
|
79
|
+
H = [
|
|
80
|
+
(H[0] + a) >>> 0,
|
|
81
|
+
(H[1] + b) >>> 0,
|
|
82
|
+
(H[2] + c) >>> 0,
|
|
83
|
+
(H[3] + d) >>> 0,
|
|
84
|
+
(H[4] + e) >>> 0,
|
|
85
|
+
(H[5] + f) >>> 0,
|
|
86
|
+
(H[6] + g) >>> 0,
|
|
87
|
+
(H[7] + h) >>> 0,
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return H.map((n) => n.toString(16).padStart(8, '0')).join('');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const browserCapabilities: NodeCapabilities = {
|
|
95
|
+
digest(input: string): string {
|
|
96
|
+
// Use pure-JS implementation (Web Crypto is async, we need sync)
|
|
97
|
+
return pureSha256Hex(input);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
getCurrentScenario(_mockDataPath: string, _clientId?: string): string {
|
|
101
|
+
// Browser: use env var only, or default
|
|
102
|
+
return (typeof process !== 'undefined' && process.env?.MOCKIFYER_SCENARIO) || 'default';
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
getScenarioFolderPath(mockDataPath: string, _scenario: string): string {
|
|
106
|
+
// Browser: return mock path as-is (no filesystem resolution)
|
|
107
|
+
return mockDataPath;
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
ensureScenarioFolder(_mockDataPath: string, _scenario: string): void {
|
|
111
|
+
// Browser: no-op (no filesystem)
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
// Optional features not supported in browser
|
|
115
|
+
mirrorProxyRecording: undefined,
|
|
116
|
+
spillBodyToDisk: undefined,
|
|
117
|
+
readSpilledBody: undefined,
|
|
118
|
+
applyRequestCorrelation: undefined,
|
|
119
|
+
generateAtlasHtml: undefined,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Export as nodeCapabilities for consistency (package.json maps .node to .browser)
|
|
123
|
+
export const nodeCapabilities = browserCapabilities;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js implementation of NodeCapabilities.
|
|
3
|
+
* Imports real utilities from mockifyer-core that use fs/path/crypto.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NodeCapabilities } from './node-capabilities';
|
|
7
|
+
import {
|
|
8
|
+
getCurrentScenario,
|
|
9
|
+
getScenarioFolderPath,
|
|
10
|
+
ensureScenarioFolder,
|
|
11
|
+
} from '@sgedda/mockifyer-core';
|
|
12
|
+
|
|
13
|
+
// Import crypto digest directly from source (works in both Jest and production)
|
|
14
|
+
const cryptoDigest = require('@sgedda/mockifyer-core/utils/crypto-digest');
|
|
15
|
+
const sha256Hex = cryptoDigest.sha256Hex;
|
|
16
|
+
|
|
17
|
+
export const nodeCapabilities: NodeCapabilities = {
|
|
18
|
+
digest(input: string): string {
|
|
19
|
+
return sha256Hex(input);
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
getCurrentScenario(mockDataPath: string, clientId?: string): string {
|
|
23
|
+
return getCurrentScenario(mockDataPath, clientId);
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
getScenarioFolderPath(mockDataPath: string, scenario: string): string {
|
|
27
|
+
return getScenarioFolderPath(mockDataPath, scenario);
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
ensureScenarioFolder(mockDataPath: string, scenario: string): void {
|
|
31
|
+
ensureScenarioFolder(mockDataPath, scenario);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
// Optional capabilities - lazily loaded when needed
|
|
35
|
+
mirrorProxyRecording: undefined, // Loaded on demand
|
|
36
|
+
spillBodyToDisk: undefined,
|
|
37
|
+
readSpilledBody: undefined,
|
|
38
|
+
applyRequestCorrelation: undefined,
|
|
39
|
+
generateAtlasHtml: undefined,
|
|
40
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform-specific capabilities for Mockifyer.
|
|
3
|
+
* Node entry provides full implementations; browser/RN provide limited or no-op versions.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface NodeCapabilities {
|
|
7
|
+
/**
|
|
8
|
+
* SHA-256 digest for cache keys and request hashing.
|
|
9
|
+
* Node: crypto.createHash. Browser: Web Crypto or pure-JS fallback.
|
|
10
|
+
*/
|
|
11
|
+
digest(input: string): string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get current scenario name from filesystem or env.
|
|
15
|
+
* Node: reads from disk/env. Browser: returns default or env-only.
|
|
16
|
+
*/
|
|
17
|
+
getCurrentScenario(mockDataPath: string, clientId?: string): string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get filesystem path for scenario folder.
|
|
21
|
+
* Node: resolves paths. Browser: returns mock path (no fs operations).
|
|
22
|
+
*/
|
|
23
|
+
getScenarioFolderPath(mockDataPath: string, scenario: string): string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Ensure scenario directory exists.
|
|
27
|
+
* Node: creates directory. Browser: no-op.
|
|
28
|
+
*/
|
|
29
|
+
ensureScenarioFolder(mockDataPath: string, scenario: string): void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Mirror proxy recording to client filesystem.
|
|
33
|
+
* Node: writes to disk. Browser: no-op or error.
|
|
34
|
+
*/
|
|
35
|
+
mirrorProxyRecording?(mockData: any, scenario: string, hash: string): Promise<void>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Spill large response bodies to disk.
|
|
39
|
+
* Node: writes file. Browser: keeps in memory or errors.
|
|
40
|
+
*/
|
|
41
|
+
spillBodyToDisk?(body: unknown, spillPath: string): void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Read spilled body from disk.
|
|
45
|
+
* Node: reads file. Browser: returns undefined.
|
|
46
|
+
*/
|
|
47
|
+
readSpilledBody?(spillPath: string): unknown;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* HTTP request correlation (for distributed tracing).
|
|
51
|
+
* Node: uses http/https modules. Browser: header-only or no-op.
|
|
52
|
+
*/
|
|
53
|
+
applyRequestCorrelation?(request: any, context: any): void;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generate Atlas HTML documentation.
|
|
57
|
+
* Node: full HTML generation. Browser: not supported.
|
|
58
|
+
*/
|
|
59
|
+
generateAtlasHtml?(data: any): string | undefined;
|
|
60
|
+
}
|
package/src/react-native.ts
CHANGED
|
@@ -259,8 +259,9 @@ export async function setupMockifyerForReactNative(
|
|
|
259
259
|
} = options;
|
|
260
260
|
|
|
261
261
|
const proxyShouldRecordOnMiss = proxyRecordOnMiss ?? recordMode;
|
|
262
|
+
const recordResponsesConfig = proxyRecordResponses ?? userConfig.proxy?.recordResponses;
|
|
262
263
|
const proxyShouldRecordResponses = resolveRecordResponses(
|
|
263
|
-
|
|
264
|
+
typeof recordResponsesConfig === 'boolean' ? recordResponsesConfig : false
|
|
264
265
|
);
|
|
265
266
|
|
|
266
267
|
const launchClientIdKey =
|
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @see https://fetch.spec.whatwg.org/#null-body-status
|
|
7
7
|
*/
|
|
8
|
+
|
|
9
|
+
import type { StreamReplayMode, StoredResponseStream } from '@sgedda/mockifyer-core';
|
|
10
|
+
import {
|
|
11
|
+
createReplayReadableStream,
|
|
12
|
+
resolveStreamReplayMode,
|
|
13
|
+
supportsReadableStreamBody,
|
|
14
|
+
} from '@sgedda/mockifyer-core';
|
|
15
|
+
|
|
8
16
|
const FETCH_NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
|
|
9
17
|
|
|
10
18
|
/**
|
|
@@ -14,14 +22,43 @@ export function isFetchNullBodyStatus(status: number): boolean {
|
|
|
14
22
|
return FETCH_NULL_BODY_STATUSES.has(status);
|
|
15
23
|
}
|
|
16
24
|
|
|
25
|
+
export interface BodyInitForFetchResponseOptions {
|
|
26
|
+
/** Recorded stream timeline — when set (and ReadableStream is supported), rebuild a paced stream. */
|
|
27
|
+
stream?: StoredResponseStream | null;
|
|
28
|
+
/** Replay pacing; defaults to `accelerated`. */
|
|
29
|
+
streamReplay?: StreamReplayMode | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
/**
|
|
18
33
|
* Body argument for `new Response(body, { status })`.
|
|
19
34
|
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
35
|
+
* When `data` is already a ReadableStream, it is returned as-is (live passthrough).
|
|
36
|
+
* When `options.stream` is set and the runtime supports ReadableStream bodies,
|
|
37
|
+
* returns a paced replay stream; otherwise falls back to the flat concatenated body.
|
|
20
38
|
*/
|
|
21
|
-
export function bodyInitForFetchResponse(
|
|
39
|
+
export function bodyInitForFetchResponse(
|
|
40
|
+
status: number,
|
|
41
|
+
data: unknown,
|
|
42
|
+
options?: BodyInitForFetchResponseOptions
|
|
43
|
+
): BodyInit | null {
|
|
22
44
|
if (isFetchNullBodyStatus(status)) {
|
|
23
45
|
return null;
|
|
24
46
|
}
|
|
47
|
+
if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
|
|
48
|
+
return data as BodyInit;
|
|
49
|
+
}
|
|
50
|
+
const storedStream = options?.stream;
|
|
51
|
+
if (
|
|
52
|
+
storedStream &&
|
|
53
|
+
Array.isArray(storedStream.chunks) &&
|
|
54
|
+
storedStream.chunks.length > 0 &&
|
|
55
|
+
supportsReadableStreamBody()
|
|
56
|
+
) {
|
|
57
|
+
return createReplayReadableStream(
|
|
58
|
+
storedStream,
|
|
59
|
+
resolveStreamReplayMode(options?.streamReplay)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
25
62
|
if (typeof data === 'string') {
|
|
26
63
|
return data;
|
|
27
64
|
}
|