@sgedda/mockifyer-fetch 1.8.44 → 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 +33 -23
- 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 +40 -25
- package/src/metro-sync-middleware.ts +71 -8
package/dist/index.js
CHANGED
|
@@ -255,6 +255,7 @@ class MockifyerClass {
|
|
|
255
255
|
this.config.clientId = (0, mockifyer_core_1.resolveClientId)(this.config);
|
|
256
256
|
}
|
|
257
257
|
this.activationMode = (0, mockifyer_core_1.resolveActivationMode)(this.config);
|
|
258
|
+
this.domainPathRules = new mockifyer_core_1.DomainPathRulesSession({ config: this.config });
|
|
258
259
|
if (this.config.proxy?.baseUrl && this.config.proxy.mirrorRecordedMocksToClient === undefined) {
|
|
259
260
|
const raw = typeof process !== 'undefined'
|
|
260
261
|
? String(process.env?.MOCKIFYER_PROXY_MIRROR_TO_CLIENT || '').trim().toLowerCase()
|
|
@@ -335,16 +336,12 @@ class MockifyerClass {
|
|
|
335
336
|
async findBestMatchingMock(request, options) {
|
|
336
337
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
337
338
|
const requestUrl = request?.url || '';
|
|
338
|
-
if (
|
|
339
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
340
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
339
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestUrl)) {
|
|
341
340
|
return undefined;
|
|
342
341
|
}
|
|
343
342
|
const requestKey = this.generateRequestKey(request);
|
|
344
343
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
345
|
-
if (
|
|
346
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
347
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
344
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestKey)) {
|
|
348
345
|
return undefined;
|
|
349
346
|
}
|
|
350
347
|
// Use database provider if available, otherwise fallback to filesystem
|
|
@@ -360,16 +357,12 @@ class MockifyerClass {
|
|
|
360
357
|
}
|
|
361
358
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
362
359
|
const requestUrl = request?.url || '';
|
|
363
|
-
if (
|
|
364
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
365
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
360
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestUrl)) {
|
|
366
361
|
return undefined;
|
|
367
362
|
}
|
|
368
363
|
const requestKey = this.generateRequestKey(request);
|
|
369
364
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
370
|
-
if (
|
|
371
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
372
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
365
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(requestKey)) {
|
|
373
366
|
return undefined;
|
|
374
367
|
}
|
|
375
368
|
// Try exact match first
|
|
@@ -521,7 +514,7 @@ class MockifyerClass {
|
|
|
521
514
|
// CRITICAL: Completely bypass Mockifyer interception for sync endpoints
|
|
522
515
|
// This prevents any Mockifyer processing (mocking, saving, etc.) for these endpoints
|
|
523
516
|
const url = config.url || '';
|
|
524
|
-
if (
|
|
517
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url)) {
|
|
525
518
|
// Mark this request to completely skip Mockifyer processing
|
|
526
519
|
config.__mockifyer_skip_save = true;
|
|
527
520
|
config.__mockifyer_bypass = true;
|
|
@@ -568,6 +561,14 @@ class MockifyerClass {
|
|
|
568
561
|
if (this.usesDashboardProxy()) {
|
|
569
562
|
return config;
|
|
570
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
|
+
}
|
|
571
572
|
const cachedMock = await this.findBestMatchingMock(request);
|
|
572
573
|
// Return cached mock if found
|
|
573
574
|
if (cachedMock) {
|
|
@@ -659,7 +660,7 @@ class MockifyerClass {
|
|
|
659
660
|
// Check multiple ways to get the URL in case response.config is undefined
|
|
660
661
|
const url = response.config?.url || response.request?.responseURL || response.url || response.config?.url || '';
|
|
661
662
|
// CRITICAL: Check URL FIRST before any other processing
|
|
662
|
-
if (
|
|
663
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url)) {
|
|
663
664
|
// Mark as bypassed to prevent any further processing
|
|
664
665
|
response.config.__mockifyer_skip_save = true;
|
|
665
666
|
response.config.__mockifyer_bypass = true;
|
|
@@ -705,9 +706,7 @@ class MockifyerClass {
|
|
|
705
706
|
return response;
|
|
706
707
|
}
|
|
707
708
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
708
|
-
if (
|
|
709
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
710
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
709
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(responseDataStr)) {
|
|
711
710
|
return response;
|
|
712
711
|
}
|
|
713
712
|
}
|
|
@@ -885,6 +884,17 @@ class MockifyerClass {
|
|
|
885
884
|
(0, mockifyer_core_1.shouldExcludeRecording)(url, recordingExclusions, this.config.baseUrl)) {
|
|
886
885
|
return;
|
|
887
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
|
+
}
|
|
888
898
|
// CRITICAL: Check response data for sync endpoint references and Metro rejection messages
|
|
889
899
|
// This is a defense-in-depth measure
|
|
890
900
|
try {
|
|
@@ -905,9 +915,7 @@ class MockifyerClass {
|
|
|
905
915
|
return;
|
|
906
916
|
}
|
|
907
917
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
908
|
-
if (
|
|
909
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
910
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
918
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(responseDataStr)) {
|
|
911
919
|
return;
|
|
912
920
|
}
|
|
913
921
|
}
|
|
@@ -1232,6 +1240,10 @@ class MockifyerClass {
|
|
|
1232
1240
|
this.loadMockData();
|
|
1233
1241
|
}
|
|
1234
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();
|
|
1235
1247
|
}
|
|
1236
1248
|
clearStaleCacheEntries() {
|
|
1237
1249
|
return 0; // No cache in fetch implementation
|
|
@@ -1304,9 +1316,7 @@ function setupMockifyer(config) {
|
|
|
1304
1316
|
const headers = init?.headers || {};
|
|
1305
1317
|
const body = init?.body;
|
|
1306
1318
|
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1307
|
-
if (
|
|
1308
|
-
url.includes('/mockifyer-clear') ||
|
|
1309
|
-
url.includes('/mockifyer-sync') ||
|
|
1319
|
+
if ((0, mockifyer_core_1.containsMockifyerSyncEndpointMarker)(url) ||
|
|
1310
1320
|
url.includes('api.resend.com') ||
|
|
1311
1321
|
(0, mockifyer_core_1.isMockifyerDashboardProxyApiUrl)(url)) {
|
|
1312
1322
|
return await originalFetchForPatched(input, init);
|
|
@@ -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,
|
|
@@ -78,6 +79,7 @@ import {
|
|
|
78
79
|
attachMockifyerRequestIdToError,
|
|
79
80
|
resolveMockifyerRequestIdForError,
|
|
80
81
|
type RequestCorrelationContext,
|
|
82
|
+
DomainPathRulesSession,
|
|
81
83
|
installNodeInboundRequestCorrelationCapture,
|
|
82
84
|
} from '@sgedda/mockifyer-core';
|
|
83
85
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
@@ -117,6 +119,7 @@ class MockifyerClass {
|
|
|
117
119
|
* unavailable (React Native / Metro stubs). Seeded via {@link warmPoolResponseCache}.
|
|
118
120
|
*/
|
|
119
121
|
private readonly poolResponseCache = new Map<string, PoolResponseItem>();
|
|
122
|
+
private readonly domainPathRules: DomainPathRulesSession;
|
|
120
123
|
|
|
121
124
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
122
125
|
private logNetworkEvent(
|
|
@@ -366,6 +369,7 @@ class MockifyerClass {
|
|
|
366
369
|
this.config.clientId = resolveClientId(this.config);
|
|
367
370
|
}
|
|
368
371
|
this.activationMode = resolveActivationMode(this.config);
|
|
372
|
+
this.domainPathRules = new DomainPathRulesSession({ config: this.config });
|
|
369
373
|
|
|
370
374
|
if (this.config.proxy?.baseUrl && this.config.proxy.mirrorRecordedMocksToClient === undefined) {
|
|
371
375
|
const raw =
|
|
@@ -461,18 +465,14 @@ class MockifyerClass {
|
|
|
461
465
|
): Promise<CachedMockData | undefined> {
|
|
462
466
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
463
467
|
const requestUrl = request?.url || '';
|
|
464
|
-
if (requestUrl
|
|
465
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
466
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
468
|
+
if (containsMockifyerSyncEndpointMarker(requestUrl)) {
|
|
467
469
|
return undefined;
|
|
468
470
|
}
|
|
469
471
|
|
|
470
472
|
const requestKey = this.generateRequestKey(request);
|
|
471
473
|
|
|
472
474
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
473
|
-
if (requestKey
|
|
474
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
475
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
475
|
+
if (containsMockifyerSyncEndpointMarker(requestKey)) {
|
|
476
476
|
return undefined;
|
|
477
477
|
}
|
|
478
478
|
|
|
@@ -494,18 +494,14 @@ class MockifyerClass {
|
|
|
494
494
|
|
|
495
495
|
// CRITICAL: Never try to match sync endpoint requests - they should never be mocked
|
|
496
496
|
const requestUrl = request?.url || '';
|
|
497
|
-
if (requestUrl
|
|
498
|
-
requestUrl.includes('/mockifyer-clear') ||
|
|
499
|
-
requestUrl.includes('/mockifyer-sync')) {
|
|
497
|
+
if (containsMockifyerSyncEndpointMarker(requestUrl)) {
|
|
500
498
|
return undefined;
|
|
501
499
|
}
|
|
502
500
|
|
|
503
501
|
const requestKey = this.generateRequestKey(request);
|
|
504
502
|
|
|
505
503
|
// CRITICAL: Also check requestKey for sync endpoint URLs (defense in depth)
|
|
506
|
-
if (requestKey
|
|
507
|
-
requestKey.includes('/mockifyer-clear') ||
|
|
508
|
-
requestKey.includes('/mockifyer-sync')) {
|
|
504
|
+
if (containsMockifyerSyncEndpointMarker(requestKey)) {
|
|
509
505
|
return undefined;
|
|
510
506
|
}
|
|
511
507
|
|
|
@@ -685,7 +681,7 @@ class MockifyerClass {
|
|
|
685
681
|
// CRITICAL: Completely bypass Mockifyer interception for sync endpoints
|
|
686
682
|
// This prevents any Mockifyer processing (mocking, saving, etc.) for these endpoints
|
|
687
683
|
const url = config.url || '';
|
|
688
|
-
if (
|
|
684
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
689
685
|
// Mark this request to completely skip Mockifyer processing
|
|
690
686
|
(config as any).__mockifyer_skip_save = true;
|
|
691
687
|
(config as any).__mockifyer_bypass = true;
|
|
@@ -745,6 +741,17 @@ class MockifyerClass {
|
|
|
745
741
|
if (this.usesDashboardProxy()) {
|
|
746
742
|
return config;
|
|
747
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
|
+
}
|
|
748
755
|
|
|
749
756
|
const cachedMock = await this.findBestMatchingMock(request);
|
|
750
757
|
|
|
@@ -860,7 +867,7 @@ class MockifyerClass {
|
|
|
860
867
|
const url = response.config?.url || response.request?.responseURL || response.url || (response as any).config?.url || '';
|
|
861
868
|
|
|
862
869
|
// CRITICAL: Check URL FIRST before any other processing
|
|
863
|
-
if (
|
|
870
|
+
if (containsMockifyerSyncEndpointMarker(url)) {
|
|
864
871
|
// Mark as bypassed to prevent any further processing
|
|
865
872
|
(response.config as any).__mockifyer_skip_save = true;
|
|
866
873
|
(response.config as any).__mockifyer_bypass = true;
|
|
@@ -914,10 +921,7 @@ class MockifyerClass {
|
|
|
914
921
|
}
|
|
915
922
|
|
|
916
923
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
917
|
-
if (responseDataStr
|
|
918
|
-
responseDataStr.includes('/mockifyer-save') ||
|
|
919
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
920
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
924
|
+
if (containsMockifyerSyncEndpointMarker(responseDataStr)) {
|
|
921
925
|
return response;
|
|
922
926
|
}
|
|
923
927
|
} catch (e) {
|
|
@@ -1134,6 +1138,18 @@ class MockifyerClass {
|
|
|
1134
1138
|
) {
|
|
1135
1139
|
return;
|
|
1136
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
|
+
}
|
|
1137
1153
|
|
|
1138
1154
|
// CRITICAL: Check response data for sync endpoint references and Metro rejection messages
|
|
1139
1155
|
// This is a defense-in-depth measure
|
|
@@ -1155,10 +1171,7 @@ class MockifyerClass {
|
|
|
1155
1171
|
}
|
|
1156
1172
|
|
|
1157
1173
|
// CRITICAL: Check for sync endpoint URLs in response data
|
|
1158
|
-
if (responseDataStr
|
|
1159
|
-
responseDataStr.includes('/mockifyer-save') ||
|
|
1160
|
-
responseDataStr.includes('/mockifyer-clear') ||
|
|
1161
|
-
responseDataStr.includes('/mockifyer-sync'))) {
|
|
1174
|
+
if (containsMockifyerSyncEndpointMarker(responseDataStr)) {
|
|
1162
1175
|
return;
|
|
1163
1176
|
}
|
|
1164
1177
|
} catch (e) {
|
|
@@ -1548,6 +1561,10 @@ class MockifyerClass {
|
|
|
1548
1561
|
(this as any).loadMockData();
|
|
1549
1562
|
}
|
|
1550
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();
|
|
1551
1568
|
}
|
|
1552
1569
|
|
|
1553
1570
|
clearStaleCacheEntries(): number {
|
|
@@ -1639,9 +1656,7 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1639
1656
|
|
|
1640
1657
|
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1641
1658
|
if (
|
|
1642
|
-
url
|
|
1643
|
-
url.includes('/mockifyer-clear') ||
|
|
1644
|
-
url.includes('/mockifyer-sync') ||
|
|
1659
|
+
containsMockifyerSyncEndpointMarker(url) ||
|
|
1645
1660
|
url.includes('api.resend.com') ||
|
|
1646
1661
|
isMockifyerDashboardProxyApiUrl(url)
|
|
1647
1662
|
) {
|
|
@@ -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 = '';
|