@sgedda/mockifyer-fetch 1.8.43 → 1.8.46
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/dist/index.js +49 -25
- package/dist/metro-sync-middleware.d.ts +5 -4
- package/dist/metro-sync-middleware.js +56 -8
- package/package.json +2 -2
- package/src/index.ts +62 -27
- package/src/metro-sync-middleware.ts +71 -8
package/dist/index.js
CHANGED
|
@@ -91,6 +91,16 @@ class MockifyerClass {
|
|
|
91
91
|
const parentRequestId = config.__mockifyer_parentRequestId;
|
|
92
92
|
return parentRequestId ? { requestId, parentRequestId } : { requestId };
|
|
93
93
|
}
|
|
94
|
+
/** Attach dashboard lookup id onto thrown fetch/client errors. */
|
|
95
|
+
throwWithMockifyerRequestId(error) {
|
|
96
|
+
const err = error;
|
|
97
|
+
const hopRequestId = err?.config?.__mockifyer_requestId;
|
|
98
|
+
const requestId = (0, mockifyer_core_1.resolveMockifyerRequestIdForError)({
|
|
99
|
+
hopRequestId,
|
|
100
|
+
responseHeaders: err?.response?.headers,
|
|
101
|
+
});
|
|
102
|
+
throw (0, mockifyer_core_1.attachMockifyerRequestIdToError)(error, requestId);
|
|
103
|
+
}
|
|
94
104
|
/**
|
|
95
105
|
* Serve stored mock body with optional `$pool` resolution from the local fixture pool.
|
|
96
106
|
* Always passes `loadPoolResponse` — including React Native where Node `fs`/`path` are
|
|
@@ -245,6 +255,7 @@ class MockifyerClass {
|
|
|
245
255
|
this.config.clientId = (0, mockifyer_core_1.resolveClientId)(this.config);
|
|
246
256
|
}
|
|
247
257
|
this.activationMode = (0, mockifyer_core_1.resolveActivationMode)(this.config);
|
|
258
|
+
this.domainPathRules = new mockifyer_core_1.DomainPathRulesSession({ config: this.config });
|
|
248
259
|
if (this.config.proxy?.baseUrl && this.config.proxy.mirrorRecordedMocksToClient === undefined) {
|
|
249
260
|
const raw = typeof process !== 'undefined'
|
|
250
261
|
? String(process.env?.MOCKIFYER_PROXY_MIRROR_TO_CLIENT || '').trim().toLowerCase()
|
|
@@ -325,16 +336,12 @@ class MockifyerClass {
|
|
|
325
336
|
async findBestMatchingMock(request, options) {
|
|
326
337
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
327
338
|
const requestUrl = request?.url || '';
|
|
328
|
-
if (
|
|
329
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
330
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
339
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestUrl)) {
|
|
331
340
|
return undefined;
|
|
332
341
|
}
|
|
333
342
|
const requestKey = this.generateRequestKey(request);
|
|
334
343
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
335
|
-
if (
|
|
336
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
337
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
344
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestKey)) {
|
|
338
345
|
return undefined;
|
|
339
346
|
}
|
|
340
347
|
// Use database provider if available, otherwise fallback to filesystem
|
|
@@ -350,16 +357,12 @@ class MockifyerClass {
|
|
|
350
357
|
}
|
|
351
358
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
352
359
|
const requestUrl = request?.url || '';
|
|
353
|
-
if (
|
|
354
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
355
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
360
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestUrl)) {
|
|
356
361
|
return undefined;
|
|
357
362
|
}
|
|
358
363
|
const requestKey = this.generateRequestKey(request);
|
|
359
364
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
360
|
-
if (
|
|
361
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
362
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
365
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestKey)) {
|
|
363
366
|
return undefined;
|
|
364
367
|
}
|
|
365
368
|
// Try exact match first
|
|
@@ -511,7 +514,7 @@ class MockifyerClass {
|
|
|
511
514
|
// CRITICAL: Completely bypass Mockifyer interception for sync endpoints
|
|
512
515
|
// This prevents any Mockifyer processing (mocking, saving, etc.) for these endpoints
|
|
513
516
|
const url = config.url || '';
|
|
514
|
-
if (
|
|
517
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url)) {
|
|
515
518
|
// Mark this request to completely skip Mockifyer processing
|
|
516
519
|
config.__mockifyer_skip_save = true;
|
|
517
520
|
config.__mockifyer_bypass = true;
|
|
@@ -558,6 +561,14 @@ class MockifyerClass {
|
|
|
558
561
|
if (this.usesDashboardProxy()) {
|
|
559
562
|
return config;
|
|
560
563
|
}
|
|
564
|
+
this.domainPathRules.discover(request.url, this.config.baseUrl);
|
|
565
|
+
const trafficGate = this.domainPathRules.getTrafficGate(request.url, this.config.baseUrl);
|
|
566
|
+
config.__mockifyer_domain_path_may_record = trafficGate.mayRecord;
|
|
567
|
+
if (!trafficGate.mayReplay) {
|
|
568
|
+
mockifyer_core_2.logger.debug(`[Mockifyer-Fetch] Domain path rules: replay denied for ${request.method} ${request.url}` +
|
|
569
|
+
(trafficGate.matchedDomainPath ? ` (rule: ${trafficGate.matchedDomainPath})` : ' (allowlist unmatched)'));
|
|
570
|
+
return config;
|
|
571
|
+
}
|
|
561
572
|
const cachedMock = await this.findBestMatchingMock(request);
|
|
562
573
|
// Return cached mock if found
|
|
563
574
|
if (cachedMock) {
|
|
@@ -649,7 +660,7 @@ class MockifyerClass {
|
|
|
649
660
|
// Check multiple ways to get the URL in case response.config is undefined
|
|
650
661
|
const url = response.config?.url || response.request?.responseURL || response.url || response.config?.url || '';
|
|
651
662
|
// CRITICAL: Check URL FIRST before any other processing
|
|
652
|
-
if (
|
|
663
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url)) {
|
|
653
664
|
// Mark as bypassed to prevent any further processing
|
|
654
665
|
response.config.__mockifyer_skip_save = true;
|
|
655
666
|
response.config.__mockifyer_bypass = true;
|
|
@@ -695,9 +706,7 @@ class MockifyerClass {
|
|
|
695
706
|
return response;
|
|
696
707
|
}
|
|
697
708
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
698
|
-
if (
|
|
699
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
700
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
709
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(responseDataStr)) {
|
|
701
710
|
return response;
|
|
702
711
|
}
|
|
703
712
|
}
|
|
@@ -781,7 +790,7 @@ class MockifyerClass {
|
|
|
781
790
|
responseBody: error.response?.data,
|
|
782
791
|
}, this.readRequestCorrelation(error.config));
|
|
783
792
|
}
|
|
784
|
-
|
|
793
|
+
this.throwWithMockifyerRequestId(error);
|
|
785
794
|
});
|
|
786
795
|
}
|
|
787
796
|
/**
|
|
@@ -875,6 +884,17 @@ class MockifyerClass {
|
|
|
875
884
|
(0, mockifyer_core_1.shouldExcludeRecording)(url, recordingExclusions, this.config.baseUrl)) {
|
|
876
885
|
return;
|
|
877
886
|
}
|
|
887
|
+
const mayRecordFlag = response.config?.__mockifyer_domain_path_may_record;
|
|
888
|
+
if (mayRecordFlag === false) {
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
if (mayRecordFlag !== true) {
|
|
892
|
+
this.domainPathRules.discover(url, this.config.baseUrl);
|
|
893
|
+
const gate = this.domainPathRules.getTrafficGate(url, this.config.baseUrl);
|
|
894
|
+
if (!gate.mayRecord) {
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
878
898
|
// CRITICAL: Check response data for sync endpoint references and Metro rejection messages
|
|
879
899
|
// This is a defense-in-depth measure
|
|
880
900
|
try {
|
|
@@ -895,9 +915,7 @@ class MockifyerClass {
|
|
|
895
915
|
return;
|
|
896
916
|
}
|
|
897
917
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
898
|
-
if (
|
|
899
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
900
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
918
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(responseDataStr)) {
|
|
901
919
|
return;
|
|
902
920
|
}
|
|
903
921
|
}
|
|
@@ -1222,6 +1240,10 @@ class MockifyerClass {
|
|
|
1222
1240
|
this.loadMockData();
|
|
1223
1241
|
}
|
|
1224
1242
|
}
|
|
1243
|
+
// RN Hybrid: pull domain-path-rules.json via Metro before traffic can discover
|
|
1244
|
+
// allowlist defaults that would block already-enabled paths.
|
|
1245
|
+
this.domainPathRules.invalidateCache();
|
|
1246
|
+
await this.domainPathRules.hydrate();
|
|
1225
1247
|
}
|
|
1226
1248
|
clearStaleCacheEntries() {
|
|
1227
1249
|
return 0; // No cache in fetch implementation
|
|
@@ -1294,9 +1316,7 @@ function setupMockifyer(config) {
|
|
|
1294
1316
|
const headers = init?.headers || {};
|
|
1295
1317
|
const body = init?.body;
|
|
1296
1318
|
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1297
|
-
if (
|
|
1298
|
-
url.includes('/mockifyer-clear') ||
|
|
1299
|
-
url.includes('/mockifyer-sync') ||
|
|
1319
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url) ||
|
|
1300
1320
|
url.includes('api.resend.com') ||
|
|
1301
1321
|
(0, mockifyer_core_1.isMockifyerDashboardProxyApiUrl)(url)) {
|
|
1302
1322
|
return await originalFetchForPatched(input, init);
|
|
@@ -1376,7 +1396,11 @@ function setupMockifyer(config) {
|
|
|
1376
1396
|
headers: responseHeaders
|
|
1377
1397
|
});
|
|
1378
1398
|
}
|
|
1379
|
-
throw error
|
|
1399
|
+
throw (0, mockifyer_core_1.attachMockifyerRequestIdToError)(error, (0, mockifyer_core_1.resolveMockifyerRequestIdForError)({
|
|
1400
|
+
hopRequestId: error?.config
|
|
1401
|
+
?.__mockifyer_requestId,
|
|
1402
|
+
responseHeaders: error?.response?.headers,
|
|
1403
|
+
}));
|
|
1380
1404
|
}
|
|
1381
1405
|
};
|
|
1382
1406
|
mockifyer_core_2.logger.info('[Mockifyer] ✅ Patched global fetch (useGlobalFetch: true)');
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Provides sync mechanisms:
|
|
5
5
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
6
|
-
* 2. GET /mockifyer-
|
|
7
|
-
* 3. GET /mockifyer-sync-to-device
|
|
8
|
-
* 4. GET /mockifyer-
|
|
9
|
-
* 5. GET /mockifyer-
|
|
6
|
+
* 2. GET/POST /mockifyer-domain-path-rules - Load or merge discovered domain-path allowlist keys into scenario file
|
|
7
|
+
* 3. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
8
|
+
* 4. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
9
|
+
* 5. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
10
|
+
* 6. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
10
11
|
*
|
|
11
12
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
12
13
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Provides sync mechanisms:
|
|
6
6
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
7
|
-
* 2. GET /mockifyer-
|
|
8
|
-
* 3. GET /mockifyer-sync-to-device
|
|
9
|
-
* 4. GET /mockifyer-
|
|
10
|
-
* 5. GET /mockifyer-
|
|
7
|
+
* 2. GET/POST /mockifyer-domain-path-rules - Load or merge discovered domain-path allowlist keys into scenario file
|
|
8
|
+
* 3. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
9
|
+
* 4. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
10
|
+
* 5. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
11
|
+
* 6. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
11
12
|
*
|
|
12
13
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
13
14
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -256,13 +257,13 @@ function saveMockToProjectFolder(mockData, projectRoot, mockDataPath, testConfig
|
|
|
256
257
|
try {
|
|
257
258
|
// CRITICAL: Never save Mockifyer sync endpoint requests to prevent infinite loops
|
|
258
259
|
const url = mockData?.request?.url || '';
|
|
259
|
-
if (
|
|
260
|
+
if ((0, mockifyer_core_2.containsMockifyerSyncEndpointMarker)(url)) {
|
|
260
261
|
console.warn(`[MockSync] ⚠️ Rejecting save - Mockifyer sync endpoint detected: ${url}`);
|
|
261
262
|
return { success: false, error: 'Cannot save Mockifyer sync endpoint requests' };
|
|
262
263
|
}
|
|
263
264
|
// Also check if the mockData string contains nested sync requests
|
|
264
265
|
const mockDataStr = JSON.stringify(mockData);
|
|
265
|
-
if (
|
|
266
|
+
if ((0, mockifyer_core_2.containsMockifyerSyncEndpointMarker)(mockDataStr)) {
|
|
266
267
|
mockifyer_core_1.logger.warn(`[MockSync] ⚠️ Rejecting save - Mock data contains nested Mockifyer sync requests`);
|
|
267
268
|
return { success: false, error: 'Mock data contains nested Mockifyer sync requests' };
|
|
268
269
|
}
|
|
@@ -299,12 +300,12 @@ function saveMockToProjectFolder(mockData, projectRoot, mockDataPath, testConfig
|
|
|
299
300
|
function saveProxyMirrorMockToProject(mockData, projectRoot, mockDataPath, scenarioName, relativePath, testConfig) {
|
|
300
301
|
try {
|
|
301
302
|
const url = mockData?.request?.url || '';
|
|
302
|
-
if (
|
|
303
|
+
if ((0, mockifyer_core_2.containsMockifyerSyncEndpointMarker)(url)) {
|
|
303
304
|
console.warn(`[MockSync] ⚠️ Rejecting save - Mockifyer sync endpoint detected: ${url}`);
|
|
304
305
|
return { success: false, error: 'Cannot save Mockifyer sync endpoint requests' };
|
|
305
306
|
}
|
|
306
307
|
const mockDataStr = JSON.stringify(mockData);
|
|
307
|
-
if (
|
|
308
|
+
if ((0, mockifyer_core_2.containsMockifyerSyncEndpointMarker)(mockDataStr)) {
|
|
308
309
|
return { success: false, error: 'Mock data contains nested Mockifyer sync requests' };
|
|
309
310
|
}
|
|
310
311
|
const id = scenarioName.trim();
|
|
@@ -625,6 +626,53 @@ function createMockSyncMiddleware(options) {
|
|
|
625
626
|
res.end(JSON.stringify(result));
|
|
626
627
|
return;
|
|
627
628
|
}
|
|
629
|
+
// Handle GET endpoint for domain-path rules (RN Hybrid hydrate at startup)
|
|
630
|
+
if (url === '/mockifyer-domain-path-rules' && req.method === 'GET') {
|
|
631
|
+
const fullUrl = req.url || '';
|
|
632
|
+
const qIndex = fullUrl.indexOf('?');
|
|
633
|
+
const query = qIndex >= 0 ? fullUrl.slice(qIndex + 1) : '';
|
|
634
|
+
const params = new URLSearchParams(query);
|
|
635
|
+
const scenarioParam = params.get('scenario');
|
|
636
|
+
const scenarioName = scenarioParam && scenarioParam.trim() !== ''
|
|
637
|
+
? scenarioParam.trim()
|
|
638
|
+
: getCurrentScenario(mockDataPath);
|
|
639
|
+
const rules = (0, mockifyer_core_2.readDomainPathRulesFile)(mockDataPath, scenarioName);
|
|
640
|
+
res.setHeader('Content-Type', 'application/json');
|
|
641
|
+
res.end(JSON.stringify({ success: true, scenario: scenarioName, rules }));
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
// Handle POST endpoint for domain-path rules discovery merge (Hybrid / RN)
|
|
645
|
+
if (url === '/mockifyer-domain-path-rules' && req.method === 'POST') {
|
|
646
|
+
let body = '';
|
|
647
|
+
req.on('data', (chunk) => {
|
|
648
|
+
body += chunk.toString();
|
|
649
|
+
});
|
|
650
|
+
req.on('end', () => {
|
|
651
|
+
try {
|
|
652
|
+
const parsed = JSON.parse(body);
|
|
653
|
+
const scenarioName = typeof parsed.scenario === 'string' && parsed.scenario.trim() !== ''
|
|
654
|
+
? parsed.scenario.trim()
|
|
655
|
+
: getCurrentScenario(mockDataPath);
|
|
656
|
+
const upserts = (0, mockifyer_core_2.parseDomainPathRules)(parsed.upserts ?? parsed.rules ?? {});
|
|
657
|
+
const existing = (0, mockifyer_core_2.readDomainPathRulesFile)(mockDataPath, scenarioName);
|
|
658
|
+
const { rules, changed } = (0, mockifyer_core_2.mergeDomainPathRuleUpserts)(existing, upserts);
|
|
659
|
+
if (changed) {
|
|
660
|
+
(0, mockifyer_core_2.writeDomainPathRulesFile)(mockDataPath, scenarioName, rules);
|
|
661
|
+
}
|
|
662
|
+
res.setHeader('Content-Type', 'application/json');
|
|
663
|
+
res.end(JSON.stringify({ success: true, changed, scenario: scenarioName, rules }));
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
res.statusCode = 400;
|
|
667
|
+
res.setHeader('Content-Type', 'application/json');
|
|
668
|
+
res.end(JSON.stringify({
|
|
669
|
+
success: false,
|
|
670
|
+
error: `Invalid JSON: ${error.message}`,
|
|
671
|
+
}));
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
628
676
|
// Handle POST endpoint for direct save (HybridProvider)
|
|
629
677
|
if (url === '/mockifyer-save' && req.method === 'POST') {
|
|
630
678
|
let body = '';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.46",
|
|
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",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.10.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.10.31"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
package/src/index.ts
CHANGED
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
isUsableNodeLikePoolFs,
|
|
43
43
|
type PoolResponseItem,
|
|
44
44
|
shouldBypassMockifyerForUrl,
|
|
45
|
+
containsMockifyerSyncEndpointMarker,
|
|
45
46
|
isMockifyerDashboardProxyApiUrl,
|
|
46
47
|
resolveRecordingExclusions,
|
|
47
48
|
shouldExcludeRecording,
|
|
@@ -75,7 +76,10 @@ import {
|
|
|
75
76
|
getInlineTraceEnvelopeBusinessBody,
|
|
76
77
|
resolveRecordResponses,
|
|
77
78
|
applyOutboundRequestCorrelation,
|
|
79
|
+
attachMockifyerRequestIdToError,
|
|
80
|
+
resolveMockifyerRequestIdForError,
|
|
78
81
|
type RequestCorrelationContext,
|
|
82
|
+
DomainPathRulesSession,
|
|
79
83
|
installNodeInboundRequestCorrelationCapture,
|
|
80
84
|
} from '@sgedda/mockifyer-core';
|
|
81
85
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
@@ -115,6 +119,7 @@ class MockifyerClass {
|
|
|
115
119
|
* unavailable (React Native / Metro stubs). Seeded via {@link warmPoolResponseCache}.
|
|
116
120
|
*/
|
|
117
121
|
private readonly poolResponseCache = new Map<string, PoolResponseItem>();
|
|
122
|
+
private readonly domainPathRules: DomainPathRulesSession;
|
|
118
123
|
|
|
119
124
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
120
125
|
private logNetworkEvent(
|
|
@@ -184,6 +189,17 @@ class MockifyerClass {
|
|
|
184
189
|
return parentRequestId ? { requestId, parentRequestId } : { requestId };
|
|
185
190
|
}
|
|
186
191
|
|
|
192
|
+
/** Attach dashboard lookup id onto thrown fetch/client errors. */
|
|
193
|
+
private throwWithMockifyerRequestId(error: unknown): never {
|
|
194
|
+
const err = error as { config?: unknown; response?: { headers?: unknown } };
|
|
195
|
+
const hopRequestId = (err?.config as { __mockifyer_requestId?: string } | undefined)?.__mockifyer_requestId;
|
|
196
|
+
const requestId = resolveMockifyerRequestIdForError({
|
|
197
|
+
hopRequestId,
|
|
198
|
+
responseHeaders: err?.response?.headers,
|
|
199
|
+
});
|
|
200
|
+
throw attachMockifyerRequestIdToError(error, requestId);
|
|
201
|
+
}
|
|
202
|
+
|
|
187
203
|
/**
|
|
188
204
|
* Serve stored mock body with optional `$pool` resolution from the local fixture pool.
|
|
189
205
|
* Always passes `loadPoolResponse` — including React Native where Node `fs`/`path` are
|
|
@@ -353,6 +369,7 @@ class MockifyerClass {
|
|
|
353
369
|
this.config.clientId = resolveClientId(this.config);
|
|
354
370
|
}
|
|
355
371
|
this.activationMode = resolveActivationMode(this.config);
|
|
372
|
+
this.domainPathRules = new DomainPathRulesSession({ config: this.config });
|
|
356
373
|
|
|
357
374
|
if (this.config.proxy?.baseUrl && this.config.proxy.mirrorRecordedMocksToClient === undefined) {
|
|
358
375
|
const raw =
|
|
@@ -448,18 +465,14 @@ class MockifyerClass {
|
|
|
448
465
|
): Promise<CachedMockData | undefined> {
|
|
449
466
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
450
467
|
const requestUrl = request?.url || '';
|
|
451
|
-
if (requestUrl
|
|
452
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
453
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
468
|
+
if (containsMockifyerSyncEndpointMarker(requestUrl)) {
|
|
454
469
|
return undefined;
|
|
455
470
|
}
|
|
456
471
|
|
|
457
472
|
const requestKey = this.generateRequestKey(request);
|
|
458
473
|
|
|
459
474
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
460
|
-
if (requestKey
|
|
461
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
462
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
475
|
+
if (containsMockifyerSyncEndpointMarker(requestKey)) {
|
|
463
476
|
return undefined;
|
|
464
477
|
}
|
|
465
478
|
|
|
@@ -481,18 +494,14 @@ class MockifyerClass {
|
|
|
481
494
|
|
|
482
495
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
483
496
|
const requestUrl = request?.url || '';
|
|
484
|
-
if (requestUrl
|
|
485
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
486
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
497
|
+
if (containsMockifyerSyncEndpointMarker(requestUrl)) {
|
|
487
498
|
return undefined;
|
|
488
499
|
}
|
|
489
500
|
|
|
490
501
|
const requestKey = this.generateRequestKey(request);
|
|
491
502
|
|
|
492
503
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
493
|
-
if (requestKey
|
|
494
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
495
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
504
|
+
if (containsMockifyerSyncEndpointMarker(requestKey)) {
|
|
496
505
|
return undefined;
|
|
497
506
|
}
|
|
498
507
|
|
|
@@ -672,7 +681,7 @@ class MockifyerClass {
|
|
|
672
681
|
// CRITICAL: Completely bypass Mockifyer interception for sync endpoints
|
|
673
682
|
// This prevents any Mockifyer processing (mocking, saving, etc.) for these endpoints
|
|
674
683
|
const url = config.url || '';
|
|
675
|
-
if (
|
|
684
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
676
685
|
// Mark this request to completely skip Mockifyer processing
|
|
677
686
|
(config as any).__mockifyer_skip_save = true;
|
|
678
687
|
(config as any).__mockifyer_bypass = true;
|
|
@@ -732,6 +741,17 @@ class MockifyerClass {
|
|
|
732
741
|
if (this.usesDashboardProxy()) {
|
|
733
742
|
return config;
|
|
734
743
|
}
|
|
744
|
+
|
|
745
|
+
this.domainPathRules.discover(request.url, this.config.baseUrl);
|
|
746
|
+
const trafficGate = this.domainPathRules.getTrafficGate(request.url, this.config.baseUrl);
|
|
747
|
+
(config as any).__mockifyer_domain_path_may_record = trafficGate.mayRecord;
|
|
748
|
+
if (!trafficGate.mayReplay) {
|
|
749
|
+
logger.debug(
|
|
750
|
+
`[Mockifyer-Fetch] Domain path rules: replay denied for ${request.method} ${request.url}` +
|
|
751
|
+
(trafficGate.matchedDomainPath ? ` (rule: ${trafficGate.matchedDomainPath})` : ' (allowlist unmatched)')
|
|
752
|
+
);
|
|
753
|
+
return config;
|
|
754
|
+
}
|
|
735
755
|
|
|
736
756
|
const cachedMock = await this.findBestMatchingMock(request);
|
|
737
757
|
|
|
@@ -847,7 +867,7 @@ class MockifyerClass {
|
|
|
847
867
|
const url = response.config?.url || response.request?.responseURL || response.url || (response as any).config?.url || '';
|
|
848
868
|
|
|
849
869
|
// CRITICAL: Check URL FIRST before any other processing
|
|
850
|
-
if (
|
|
870
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
851
871
|
// Mark as bypassed to prevent any further processing
|
|
852
872
|
(response.config as any).__mockifyer_skip_save = true;
|
|
853
873
|
(response.config as any).__mockifyer_bypass = true;
|
|
@@ -901,10 +921,7 @@ class MockifyerClass {
|
|
|
901
921
|
}
|
|
902
922
|
|
|
903
923
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
904
|
-
if (responseDataStr
|
|
905
|
-
responseDataStr.includes('/mockifyer-save') ||
|
|
906
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
907
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
924
|
+
if (containsMockifyerSyncEndpointMarker(responseDataStr)) {
|
|
908
925
|
return response;
|
|
909
926
|
}
|
|
910
927
|
} catch (e) {
|
|
@@ -1013,7 +1030,7 @@ class MockifyerClass {
|
|
|
1013
1030
|
this.readRequestCorrelation(error.config)
|
|
1014
1031
|
);
|
|
1015
1032
|
}
|
|
1016
|
-
|
|
1033
|
+
this.throwWithMockifyerRequestId(error);
|
|
1017
1034
|
}
|
|
1018
1035
|
);
|
|
1019
1036
|
}
|
|
@@ -1121,6 +1138,18 @@ class MockifyerClass {
|
|
|
1121
1138
|
) {
|
|
1122
1139
|
return;
|
|
1123
1140
|
}
|
|
1141
|
+
|
|
1142
|
+
const mayRecordFlag = (response.config as any)?.__mockifyer_domain_path_may_record;
|
|
1143
|
+
if (mayRecordFlag === false) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
if (mayRecordFlag !== true) {
|
|
1147
|
+
this.domainPathRules.discover(url, this.config.baseUrl);
|
|
1148
|
+
const gate = this.domainPathRules.getTrafficGate(url, this.config.baseUrl);
|
|
1149
|
+
if (!gate.mayRecord) {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1124
1153
|
|
|
1125
1154
|
// CRITICAL: Check response data for sync endpoint references and Metro rejection messages
|
|
1126
1155
|
// This is a defense-in-depth measure
|
|
@@ -1142,10 +1171,7 @@ class MockifyerClass {
|
|
|
1142
1171
|
}
|
|
1143
1172
|
|
|
1144
1173
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
1145
|
-
if (responseDataStr
|
|
1146
|
-
responseDataStr.includes('/mockifyer-save') ||
|
|
1147
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
1148
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
1174
|
+
if (containsMockifyerSyncEndpointMarker(responseDataStr)) {
|
|
1149
1175
|
return;
|
|
1150
1176
|
}
|
|
1151
1177
|
} catch (e) {
|
|
@@ -1535,6 +1561,10 @@ class MockifyerClass {
|
|
|
1535
1561
|
(this as any).loadMockData();
|
|
1536
1562
|
}
|
|
1537
1563
|
}
|
|
1564
|
+
// RN Hybrid: pull domain-path-rules.json via Metro before traffic can discover
|
|
1565
|
+
// allowlist defaults that would block already-enabled paths.
|
|
1566
|
+
this.domainPathRules.invalidateCache();
|
|
1567
|
+
await this.domainPathRules.hydrate();
|
|
1538
1568
|
}
|
|
1539
1569
|
|
|
1540
1570
|
clearStaleCacheEntries(): number {
|
|
@@ -1626,9 +1656,7 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1626
1656
|
|
|
1627
1657
|
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1628
1658
|
if (
|
|
1629
|
-
url
|
|
1630
|
-
url.includes('/mockifyer-clear') ||
|
|
1631
|
-
url.includes('/mockifyer-sync') ||
|
|
1659
|
+
containsMockifyerSyncEndpointMarker(url) ||
|
|
1632
1660
|
url.includes('api.resend.com') ||
|
|
1633
1661
|
isMockifyerDashboardProxyApiUrl(url)
|
|
1634
1662
|
) {
|
|
@@ -1715,7 +1743,14 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1715
1743
|
headers: responseHeaders
|
|
1716
1744
|
});
|
|
1717
1745
|
}
|
|
1718
|
-
throw
|
|
1746
|
+
throw attachMockifyerRequestIdToError(
|
|
1747
|
+
error,
|
|
1748
|
+
resolveMockifyerRequestIdForError({
|
|
1749
|
+
hopRequestId: (error as { config?: { __mockifyer_requestId?: string } })?.config
|
|
1750
|
+
?.__mockifyer_requestId,
|
|
1751
|
+
responseHeaders: (error as { response?: { headers?: unknown } })?.response?.headers,
|
|
1752
|
+
})
|
|
1753
|
+
);
|
|
1719
1754
|
}
|
|
1720
1755
|
} as typeof fetch;
|
|
1721
1756
|
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Provides sync mechanisms:
|
|
5
5
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
6
|
-
* 2. GET /mockifyer-
|
|
7
|
-
* 3. GET /mockifyer-sync-to-device
|
|
8
|
-
* 4. GET /mockifyer-
|
|
9
|
-
* 5. GET /mockifyer-
|
|
6
|
+
* 2. GET/POST /mockifyer-domain-path-rules - Load or merge discovered domain-path allowlist keys into scenario file
|
|
7
|
+
* 3. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
8
|
+
* 4. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
9
|
+
* 5. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
10
|
+
* 6. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
10
11
|
*
|
|
11
12
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
12
13
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -23,6 +24,12 @@ import {
|
|
|
23
24
|
POOL_ID_PATTERN,
|
|
24
25
|
loadPoolResponseItem,
|
|
25
26
|
type PoolResponseItem,
|
|
27
|
+
mergeDomainPathRuleUpserts,
|
|
28
|
+
parseDomainPathRules,
|
|
29
|
+
readDomainPathRulesFile,
|
|
30
|
+
writeDomainPathRulesFile,
|
|
31
|
+
type DomainPathRulesMap,
|
|
32
|
+
containsMockifyerSyncEndpointMarker,
|
|
26
33
|
} from '@sgedda/mockifyer-core';
|
|
27
34
|
|
|
28
35
|
export interface MetroSyncMiddlewareOptions {
|
|
@@ -275,14 +282,14 @@ function saveMockToProjectFolder(
|
|
|
275
282
|
try {
|
|
276
283
|
// CRITICAL: Never save Mockifyer sync endpoint requests to prevent infinite loops
|
|
277
284
|
const url = mockData?.request?.url || '';
|
|
278
|
-
if (
|
|
285
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
279
286
|
console.warn(`[MockSync] ⚠️ Rejecting save - Mockifyer sync endpoint detected: ${url}`);
|
|
280
287
|
return { success: false, error: 'Cannot save Mockifyer sync endpoint requests' };
|
|
281
288
|
}
|
|
282
289
|
|
|
283
290
|
// Also check if the mockData string contains nested sync requests
|
|
284
291
|
const mockDataStr = JSON.stringify(mockData);
|
|
285
|
-
if (
|
|
292
|
+
if (containsMockifyerSyncEndpointMarker(mockDataStr)) {
|
|
286
293
|
logger.warn(`[MockSync] ⚠️ Rejecting save - Mock data contains nested Mockifyer sync requests`);
|
|
287
294
|
return { success: false, error: 'Mock data contains nested Mockifyer sync requests' };
|
|
288
295
|
}
|
|
@@ -332,12 +339,12 @@ function saveProxyMirrorMockToProject(
|
|
|
332
339
|
): { success: boolean; filename?: string; scenario?: string; error?: string } {
|
|
333
340
|
try {
|
|
334
341
|
const url = mockData?.request?.url || '';
|
|
335
|
-
if (
|
|
342
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
336
343
|
console.warn(`[MockSync] ⚠️ Rejecting save - Mockifyer sync endpoint detected: ${url}`);
|
|
337
344
|
return { success: false, error: 'Cannot save Mockifyer sync endpoint requests' };
|
|
338
345
|
}
|
|
339
346
|
const mockDataStr = JSON.stringify(mockData);
|
|
340
|
-
if (
|
|
347
|
+
if (containsMockifyerSyncEndpointMarker(mockDataStr)) {
|
|
341
348
|
return { success: false, error: 'Mock data contains nested Mockifyer sync requests' };
|
|
342
349
|
}
|
|
343
350
|
const id = scenarioName.trim();
|
|
@@ -718,6 +725,62 @@ export function createMockSyncMiddleware(options?: MetroSyncMiddlewareOptions) {
|
|
|
718
725
|
return;
|
|
719
726
|
}
|
|
720
727
|
|
|
728
|
+
// Handle GET endpoint for domain-path rules (RN Hybrid hydrate at startup)
|
|
729
|
+
if (url === '/mockifyer-domain-path-rules' && req.method === 'GET') {
|
|
730
|
+
const fullUrl = req.url || '';
|
|
731
|
+
const qIndex = fullUrl.indexOf('?');
|
|
732
|
+
const query = qIndex >= 0 ? fullUrl.slice(qIndex + 1) : '';
|
|
733
|
+
const params = new URLSearchParams(query);
|
|
734
|
+
const scenarioParam = params.get('scenario');
|
|
735
|
+
const scenarioName =
|
|
736
|
+
scenarioParam && scenarioParam.trim() !== ''
|
|
737
|
+
? scenarioParam.trim()
|
|
738
|
+
: getCurrentScenario(mockDataPath);
|
|
739
|
+
const rules = readDomainPathRulesFile(mockDataPath, scenarioName);
|
|
740
|
+
res.setHeader('Content-Type', 'application/json');
|
|
741
|
+
res.end(JSON.stringify({ success: true, scenario: scenarioName, rules }));
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// Handle POST endpoint for domain-path rules discovery merge (Hybrid / RN)
|
|
746
|
+
if (url === '/mockifyer-domain-path-rules' && req.method === 'POST') {
|
|
747
|
+
let body = '';
|
|
748
|
+
req.on('data', (chunk: Buffer) => {
|
|
749
|
+
body += chunk.toString();
|
|
750
|
+
});
|
|
751
|
+
req.on('end', () => {
|
|
752
|
+
try {
|
|
753
|
+
const parsed = JSON.parse(body) as {
|
|
754
|
+
scenario?: string;
|
|
755
|
+
upserts?: DomainPathRulesMap;
|
|
756
|
+
rules?: DomainPathRulesMap;
|
|
757
|
+
};
|
|
758
|
+
const scenarioName =
|
|
759
|
+
typeof parsed.scenario === 'string' && parsed.scenario.trim() !== ''
|
|
760
|
+
? parsed.scenario.trim()
|
|
761
|
+
: getCurrentScenario(mockDataPath);
|
|
762
|
+
const upserts = parseDomainPathRules(parsed.upserts ?? parsed.rules ?? {});
|
|
763
|
+
const existing = readDomainPathRulesFile(mockDataPath, scenarioName);
|
|
764
|
+
const { rules, changed } = mergeDomainPathRuleUpserts(existing, upserts);
|
|
765
|
+
if (changed) {
|
|
766
|
+
writeDomainPathRulesFile(mockDataPath, scenarioName, rules);
|
|
767
|
+
}
|
|
768
|
+
res.setHeader('Content-Type', 'application/json');
|
|
769
|
+
res.end(JSON.stringify({ success: true, changed, scenario: scenarioName, rules }));
|
|
770
|
+
} catch (error) {
|
|
771
|
+
res.statusCode = 400;
|
|
772
|
+
res.setHeader('Content-Type', 'application/json');
|
|
773
|
+
res.end(
|
|
774
|
+
JSON.stringify({
|
|
775
|
+
success: false,
|
|
776
|
+
error: `Invalid JSON: ${(error as Error).message}`,
|
|
777
|
+
})
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
|
|
721
784
|
// Handle POST endpoint for direct save (HybridProvider)
|
|
722
785
|
if (url === '/mockifyer-save' && req.method === 'POST') {
|
|
723
786
|
let body = '';
|