@sgedda/mockifyer-fetch 1.8.92 → 1.8.97

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 CHANGED
@@ -104,6 +104,7 @@ class MockifyerClass {
104
104
  durationMs: eventPartial.durationMs,
105
105
  clientId: this.config.clientId ?? eventPartial.clientId ?? null,
106
106
  errorMessage: eventPartial.errorMessage,
107
+ requestHeaders,
107
108
  requestBody,
108
109
  responseBody,
109
110
  });
@@ -630,10 +631,7 @@ class MockifyerClass {
630
631
  config.__mockifyer_bypass = true;
631
632
  return config;
632
633
  }
633
- if (!(0, mockifyer_core_1.shouldApplyMockifyer)(this.activationMode, config.headers, {
634
- useProxyLane: { proxyBaseUrl: this.config.proxy?.baseUrl, resolvedClientId: this.config.clientId },
635
- })) {
636
- await this.applyOutboundCorrelation(config);
634
+ if (!(0, mockifyer_core_1.shouldApplyMockifyer)(this.activationMode, config.headers)) {
637
635
  config.__mockifyer_bypass = true;
638
636
  return config;
639
637
  }
@@ -1133,6 +1131,7 @@ class MockifyerClass {
1133
1131
  : this.currentSessionId,
1134
1132
  requestId: hopIds.requestId,
1135
1133
  parentRequestId: hopIds.parentRequestId,
1134
+ parentRequestIds: hopIds.parentRequestIds,
1136
1135
  ...((0, mockifyer_core_1.newRecordingUsesAlwaysUseRealApi)() ? { alwaysUseRealApi: true } : {}),
1137
1136
  };
1138
1137
  (0, mockifyer_core_1.applyRecordingPassthroughFlag)(mockData, saveDecision.alwaysUseRealApi);
@@ -1185,16 +1184,7 @@ class MockifyerClass {
1185
1184
  }
1186
1185
  async persistMatchedMockAfterLiveCapture(cachedMock, capturedResponse, durationMs, liveCorrelation) {
1187
1186
  const updated = (0, mockifyer_core_1.buildMockDataAfterLiveCapture)(cachedMock.mockData, capturedResponse, durationMs);
1188
- const hopIds = (0, mockifyer_core_1.resolvePersistedHopIds)(cachedMock.mockData, liveCorrelation);
1189
- if (hopIds.requestId) {
1190
- updated.requestId = hopIds.requestId;
1191
- }
1192
- if (hopIds.parentRequestId) {
1193
- updated.parentRequestId = hopIds.parentRequestId;
1194
- }
1195
- else {
1196
- delete updated.parentRequestId;
1197
- }
1187
+ (0, mockifyer_core_1.applyPersistedHopIds)(updated, (0, mockifyer_core_1.resolvePersistedHopIds)(cachedMock.mockData, liveCorrelation));
1198
1188
  if (this.databaseProvider) {
1199
1189
  await this.databaseProvider.save(updated);
1200
1190
  return;
@@ -1,15 +1,16 @@
1
1
  /**
2
2
  * Metro terminal keys for Mockifyer:
3
- * - `t` (default): start/stop Atlas capture (start opens the live stream page;
3
+ * - `t` (default): start/stop Atlas capture (start opens the live stream page
4
+ * and signals the app to call `enableMockifyer()` for `runtimeMode: 'manual'`;
4
5
  * stop ends capture — generate HTML from the live page “Render docs” button)
5
- * - `m` (default): open the Mockifyer dashboard in the browser
6
+ * - dashboard key is off unless `dashboardKey` is set (for example `"m"`)
6
7
  *
7
- * Opening `mockifyer-atlas` (SSE `/mockifyer-network-events/stream`) or the live
8
- * page auto-starts capture when idle — then press `t` once to stop, or use
9
- * Render docs on the live page without stopping.
8
+ * Capture starts only when `t` is pressed. Opening the live page or connecting
9
+ * the SSE stream does not start it. Press `t` again to stop, or use Render docs
10
+ * on the live page without stopping.
10
11
  *
11
12
  * `a` is reserved by Metro for Android (`i` iOS, `r` reload, `d` Dev Menu, `j` DevTools).
12
- * Disable Atlas with `atlasKey: false`, dashboard with `dashboardKey: false`.
13
+ * Disable Atlas with `atlasKey: false`.
13
14
  */
14
15
  import * as readline from "readline";
15
16
  /** `t` — `a` launches Android in the Metro / RN CLI. */
@@ -26,8 +27,8 @@ export interface AttachMetroAtlasKeyHandlerOptions {
26
27
  */
27
28
  atlasKey?: AtlasKeyOption;
28
29
  /**
29
- * Key that opens the dashboard in the browser (default `"m"`).
30
- * Pass `false` to disable.
30
+ * Key that opens the dashboard in the browser.
31
+ * Off unless set (for example `"m"`). Pass `false` to keep it off.
31
32
  */
32
33
  dashboardKey?: AtlasKeyOption;
33
34
  /**
@@ -35,7 +36,7 @@ export interface AttachMetroAtlasKeyHandlerOptions {
35
36
  * Mount prefix from `MOCKIFYER_DASHBOARD_BASE` is appended when set.
36
37
  */
37
38
  dashboardUrl?: string;
38
- /** Called on session start. `reason` is `key` (Metro `t`) or `stream` (live page connected). */
39
+ /** Called when capture starts. Only the Atlas key (`t`) starts a session. */
39
40
  onSessionStart?: (reason: MetroAtlasSessionStartReason) => void;
40
41
  /** Called on session stop — end capture (HTML is rendered from the live page). */
41
42
  onSessionStop?: () => void | Promise<void>;
@@ -67,7 +68,8 @@ export declare function resolveKeyOption(value: AtlasKeyOption | undefined, defa
67
68
  */
68
69
  export declare function resolveAtlasKeyOption(atlasKey?: AtlasKeyOption): string | null;
69
70
  /**
70
- * Normalize `dashboardKey` option: default `"m"`, `false` → disabled.
71
+ * Normalize `dashboardKey`. Omitted or `false` → disabled.
72
+ * An explicit letter (for example `"m"`) opens the dashboard.
71
73
  */
72
74
  export declare function resolveDashboardKeyOption(dashboardKey?: AtlasKeyOption): string | null;
73
75
  /**
@@ -108,11 +110,12 @@ export declare function stopMetroAtlasSession(): boolean;
108
110
  /** Open the dashboard URL when the dashboard key is enabled. */
109
111
  export declare function openMetroDashboard(): boolean;
110
112
  /**
111
- * First SSE client for `/mockifyer-network-events/stream` auto-starts capture.
113
+ * SSE client connected to `/mockifyer-network-events/stream`.
114
+ * Does not start capture — that happens only when the Atlas key (`t`) is pressed.
112
115
  */
113
116
  export declare function notifyMetroAtlasStreamClientConnected(): void;
114
117
  /**
115
- * SSE client disconnected — capture stays open until Metro `a` (or explicit stop).
118
+ * SSE client disconnected — capture stays open until the Atlas key is pressed again.
116
119
  */
117
120
  export declare function notifyMetroAtlasStreamClientDisconnected(): void;
118
121
  /**
@@ -53,16 +53,17 @@ exports.detachMetroAtlasKeyHandler = detachMetroAtlasKeyHandler;
53
53
  exports.attachMetroAtlasKeyHandler = attachMetroAtlasKeyHandler;
54
54
  /**
55
55
  * Metro terminal keys for Mockifyer:
56
- * - `t` (default): start/stop Atlas capture (start opens the live stream page;
56
+ * - `t` (default): start/stop Atlas capture (start opens the live stream page
57
+ * and signals the app to call `enableMockifyer()` for `runtimeMode: 'manual'`;
57
58
  * stop ends capture — generate HTML from the live page “Render docs” button)
58
- * - `m` (default): open the Mockifyer dashboard in the browser
59
+ * - dashboard key is off unless `dashboardKey` is set (for example `"m"`)
59
60
  *
60
- * Opening `mockifyer-atlas` (SSE `/mockifyer-network-events/stream`) or the live
61
- * page auto-starts capture when idle — then press `t` once to stop, or use
62
- * Render docs on the live page without stopping.
61
+ * Capture starts only when `t` is pressed. Opening the live page or connecting
62
+ * the SSE stream does not start it. Press `t` again to stop, or use Render docs
63
+ * on the live page without stopping.
63
64
  *
64
65
  * `a` is reserved by Metro for Android (`i` iOS, `r` reload, `d` Dev Menu, `j` DevTools).
65
- * Disable Atlas with `atlasKey: false`, dashboard with `dashboardKey: false`.
66
+ * Disable Atlas with `atlasKey: false`.
66
67
  */
67
68
  const readline = __importStar(require("readline"));
68
69
  const child_process_1 = require("child_process");
@@ -112,9 +113,12 @@ function resolveAtlasKeyOption(atlasKey) {
112
113
  return resolveKeyOption(atlasKey, exports.DEFAULT_METRO_ATLAS_KEY);
113
114
  }
114
115
  /**
115
- * Normalize `dashboardKey` option: default `"m"`, `false` → disabled.
116
+ * Normalize `dashboardKey`. Omitted or `false` → disabled.
117
+ * An explicit letter (for example `"m"`) opens the dashboard.
116
118
  */
117
119
  function resolveDashboardKeyOption(dashboardKey) {
120
+ if (dashboardKey === undefined || dashboardKey === false)
121
+ return null;
118
122
  return resolveKeyOption(dashboardKey, exports.DEFAULT_METRO_DASHBOARD_KEY);
119
123
  }
120
124
  /**
@@ -224,10 +228,10 @@ function startMetroAtlasSession(reason = "key") {
224
228
  return false;
225
229
  }
226
230
  if (reason === "stream") {
227
- console.log(`[Mockifyer] Atlas capture started (stream connected) — press ${key} to stop, or Render docs on the live page.`);
231
+ console.log(`[Mockifyer] Atlas capture started (stream connected) — Mockifyer activated (turns off again on stop if it was off). Press ${key} to stop, or Render docs on the live page.`);
228
232
  }
229
233
  else {
230
- console.log(`[Mockifyer] Atlas capture started — press ${key} again to stop, or Render docs on the live page.`);
234
+ console.log(`[Mockifyer] Atlas capture started — Mockifyer activated (turns off again on stop if it was off). Press ${key} again to stop, or Render docs on the live page.`);
231
235
  openMetroAtlasLivePage();
232
236
  }
233
237
  return true;
@@ -262,7 +266,7 @@ function stopMetroAtlasSession() {
262
266
  const startedAt = sessionStartedAt ?? Date.now();
263
267
  const duration = formatSessionDuration(startedAt);
264
268
  sessionPhase = "rendering";
265
- console.log(`[Mockifyer] Atlas capture stopped (${duration}).`);
269
+ console.log(`[Mockifyer] Atlas capture stopped (${duration}) — Mockifyer turns off again if it was off before press t.`);
266
270
  Promise.resolve()
267
271
  .then(() => onSessionStop())
268
272
  .catch((err) => {
@@ -293,16 +297,14 @@ function openMetroDashboard() {
293
297
  return true;
294
298
  }
295
299
  /**
296
- * First SSE client for `/mockifyer-network-events/stream` auto-starts capture.
300
+ * SSE client connected to `/mockifyer-network-events/stream`.
301
+ * Does not start capture — that happens only when the Atlas key (`t`) is pressed.
297
302
  */
298
303
  function notifyMetroAtlasStreamClientConnected() {
299
304
  streamSubscriberCount += 1;
300
- if (streamSubscriberCount === 1) {
301
- startMetroAtlasSession("stream");
302
- }
303
305
  }
304
306
  /**
305
- * SSE client disconnected — capture stays open until Metro `a` (or explicit stop).
307
+ * SSE client disconnected — capture stays open until the Atlas key is pressed again.
306
308
  */
307
309
  function notifyMetroAtlasStreamClientDisconnected() {
308
310
  streamSubscriberCount = Math.max(0, streamSubscriberCount - 1);
@@ -452,10 +454,7 @@ function attachMetroAtlasKeyHandler(options) {
452
454
  attached = true;
453
455
  const parts = [];
454
456
  if (atlasKey) {
455
- parts.push(`Press ${atlasKey} to start/stop Atlas capture (start opens live stream; Render docs on the page writes HTML)`);
456
- }
457
- if (effectiveDashboardKey && dashboardUrl) {
458
- parts.push(`Press ${effectiveDashboardKey} to open dashboard (${dashboardUrl})`);
457
+ parts.push(`Press ${atlasKey} to start/stop Atlas capture (activates Mockifyer if off, restores off on stop; start opens live stream; Render docs on the page writes HTML)`);
459
458
  }
460
459
  if (parts.length > 0) {
461
460
  console.log(`[Mockifyer] info ${parts.join(". ")}.`);
@@ -109,7 +109,7 @@ export interface ConfigureMetroOptions {
109
109
  * projectRoot: __dirname,
110
110
  * mockDataPath: './mock-data',
111
111
  * // atlasKey: 't' (default) — Metro terminal: start/stop Atlas (start opens live stream; stop → HTML). `a` is Android.
112
- * // dashboardKey: 'm' (default) — open dashboard in browser
112
+ * // dashboardKey: 'm' — optional; off unless set. Opens the dashboard in the browser
113
113
  * // dashboardUrl: 'http://localhost:3002' — or MOCKIFYER_DASHBOARD_URL
114
114
  * },
115
115
  * });
@@ -128,7 +128,7 @@ function resolveMockifyerCoreReactNativeEntry(moduleName, platform) {
128
128
  * projectRoot: __dirname,
129
129
  * mockDataPath: './mock-data',
130
130
  * // atlasKey: 't' (default) — Metro terminal: start/stop Atlas (start opens live stream; stop → HTML). `a` is Android.
131
- * // dashboardKey: 'm' (default) — open dashboard in browser
131
+ * // dashboardKey: 'm' — optional; off unless set. Opens the dashboard in the browser
132
132
  * // dashboardUrl: 'http://localhost:3002' — or MOCKIFYER_DASHBOARD_URL
133
133
  * },
134
134
  * });
@@ -21,9 +21,9 @@
21
21
  * 17. POST /mockifyer-network-events/clear — clear ring buffer
22
22
  * 18. GET /mockifyer-atlas-live — live hop stream web page (SSE + expand/collapse)
23
23
  * 19. GET /mockifyer-atlas-trace?id= — re-call a hop with X-Mockifyer-Include-Trace (`&format=html` opens a result tab)
24
- * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session active flag
25
- * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream; stop ends capture; Render docs on the live page writes HTML; stream auto-starts; `atlasKey: false` to disable). `a` is reserved for Android.
26
- * 22. Metro terminal key `m` — open Mockifyer dashboard in the browser (`dashboardKey: false` to disable)
24
+ * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session (`active` plus `activateMockifyer` for the app)
25
+ * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream and signals enableMockifyer; stop ends capture; Render docs on the live page writes HTML; capture does not start until `t`; `atlasKey: false` to disable). `a` is reserved for Android.
26
+ * 22. Optional Metro `dashboardKey` — open the dashboard in the browser (off unless set)
27
27
  * 23. On hop ingest / Atlas render — pull nested hops from dashboard `/api/network-events/trace` (remote BFF → dashboard → Atlas)
28
28
  *
29
29
  * The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
@@ -39,14 +39,15 @@ export interface MetroSyncMiddlewareOptions {
39
39
  mockDataPath?: string;
40
40
  /**
41
41
  * Metro terminal key that starts/stops Atlas capture (default `"t"`).
42
- * Start clears the hop buffer; stop ends capture. Write HTML via live page
43
- * “Render docs” (`POST /mockifyer-network-events/render`) or the CLI.
42
+ * Start clears the hop buffer and signals the app to enable Mockifyer; stop ends
43
+ * capture. Write HTML via live page “Render docs” (`POST /mockifyer-network-events/render`)
44
+ * or the CLI.
44
45
  * Pass `false` to disable. `a` is reserved by Metro for Android.
45
46
  */
46
47
  atlasKey?: AtlasKeyOption;
47
48
  /**
48
- * Metro terminal key that opens the dashboard in the browser (default `"m"`).
49
- * Pass `false` to disable.
49
+ * Metro terminal key that opens the dashboard in the browser.
50
+ * Off unless set (for example `"m"`).
50
51
  */
51
52
  dashboardKey?: AtlasKeyOption;
52
53
  /**
@@ -22,9 +22,9 @@
22
22
  * 17. POST /mockifyer-network-events/clear — clear ring buffer
23
23
  * 18. GET /mockifyer-atlas-live — live hop stream web page (SSE + expand/collapse)
24
24
  * 19. GET /mockifyer-atlas-trace?id= — re-call a hop with X-Mockifyer-Include-Trace (`&format=html` opens a result tab)
25
- * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session active flag
26
- * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream; stop ends capture; Render docs on the live page writes HTML; stream auto-starts; `atlasKey: false` to disable). `a` is reserved for Android.
27
- * 22. Metro terminal key `m` — open Mockifyer dashboard in the browser (`dashboardKey: false` to disable)
25
+ * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session (`active` plus `activateMockifyer` for the app)
26
+ * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream and signals enableMockifyer; stop ends capture; Render docs on the live page writes HTML; capture does not start until `t`; `atlasKey: false` to disable). `a` is reserved for Android.
27
+ * 22. Optional Metro `dashboardKey` — open the dashboard in the browser (off unless set)
28
28
  * 23. On hop ingest / Atlas render — pull nested hops from dashboard `/api/network-events/trace` (remote BFF → dashboard → Atlas)
29
29
  *
30
30
  * The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
@@ -1259,15 +1259,21 @@ function createMockSyncMiddleware(options) {
1259
1259
  res.end(JSON.stringify(result));
1260
1260
  return;
1261
1261
  }
1262
- // Atlas `t` capture session flag — device / live page poll this for session UI.
1262
+ // Atlas `t` capture session — device polls this for include-trace and enableMockifyer().
1263
1263
  if ((url === mockifyer_core_2.ATLAS_CAPTURE_SESSION_PATH ||
1264
1264
  url === `${mockifyer_core_2.ATLAS_CAPTURE_SESSION_PATH}/`) &&
1265
1265
  req.method === "GET") {
1266
+ const phase = (0, metro_atlas_key_handlers_1.getMetroAtlasSessionPhase)();
1267
+ const capturing = phase === "capturing";
1266
1268
  res.setHeader("Content-Type", "application/json");
1267
1269
  res.setHeader("Cache-Control", "no-store");
1268
1270
  res.end(JSON.stringify({
1269
1271
  success: true,
1270
1272
  active: (0, mockifyer_core_2.isMetroAtlasCaptureSessionActive)(),
1273
+ phase,
1274
+ capturing,
1275
+ /** App should call enableMockifyer() while capturing (runtimeMode: manual). */
1276
+ activateMockifyer: capturing,
1271
1277
  }));
1272
1278
  return;
1273
1279
  }
@@ -1481,8 +1487,7 @@ function createMockSyncMiddleware(options) {
1481
1487
  const qIndex = fullUrl.indexOf("?");
1482
1488
  const params = new URLSearchParams(qIndex >= 0 ? fullUrl.slice(qIndex + 1) : "");
1483
1489
  const backlog = params.get("backlog") !== "0";
1484
- // First SSE client (e.g. mockifyer-atlas) auto-starts Metro Atlas capture.
1485
- // Must run before reading the buffer — start clears hops for a clean session.
1490
+ // Track the SSE client. Capture starts only when Metro `t` is pressed.
1486
1491
  (0, metro_atlas_key_handlers_1.notifyMetroAtlasStreamClientConnected)();
1487
1492
  const buffer = (0, mockifyer_core_2.getMetroNetworkEventBuffer)();
1488
1493
  res.writeHead(200, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sgedda/mockifyer-fetch",
3
- "version": "1.8.92",
3
+ "version": "1.8.97",
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.171"
52
+ "@sgedda/mockifyer-core": "^1.10.177"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "^20.11.24",
package/src/index.ts CHANGED
@@ -83,6 +83,7 @@ import {
83
83
  applyOutboundRequestCorrelation,
84
84
  adoptStoredOutboundRequestId,
85
85
  resolvePersistedHopIds,
86
+ applyPersistedHopIds,
86
87
  attachMockifyerRequestIdToError,
87
88
  resolveMockifyerRequestIdForError,
88
89
  configureFlightRecorder,
@@ -234,6 +235,7 @@ class MockifyerClass {
234
235
  durationMs: eventPartial.durationMs,
235
236
  clientId: this.config.clientId ?? eventPartial.clientId ?? null,
236
237
  errorMessage: eventPartial.errorMessage,
238
+ requestHeaders,
237
239
  requestBody,
238
240
  responseBody,
239
241
  });
@@ -857,12 +859,7 @@ class MockifyerClass {
857
859
  return config;
858
860
  }
859
861
 
860
- if (
861
- !shouldApplyMockifyer(this.activationMode, config.headers, {
862
- useProxyLane: { proxyBaseUrl: this.config.proxy?.baseUrl, resolvedClientId: this.config.clientId },
863
- })
864
- ) {
865
- await this.applyOutboundCorrelation(config);
862
+ if (!shouldApplyMockifyer(this.activationMode, config.headers)) {
866
863
  (config as any).__mockifyer_bypass = true;
867
864
  return config;
868
865
  }
@@ -1491,6 +1488,7 @@ class MockifyerClass {
1491
1488
  : this.currentSessionId,
1492
1489
  requestId: hopIds.requestId,
1493
1490
  parentRequestId: hopIds.parentRequestId,
1491
+ parentRequestIds: hopIds.parentRequestIds,
1494
1492
  ...(newRecordingUsesAlwaysUseRealApi() ? { alwaysUseRealApi: true as const } : {}),
1495
1493
  };
1496
1494
 
@@ -1555,15 +1553,7 @@ class MockifyerClass {
1555
1553
  liveCorrelation?: RequestCorrelationContext
1556
1554
  ): Promise<void> {
1557
1555
  const updated = buildMockDataAfterLiveCapture(cachedMock.mockData, capturedResponse, durationMs);
1558
- const hopIds = resolvePersistedHopIds(cachedMock.mockData, liveCorrelation);
1559
- if (hopIds.requestId) {
1560
- updated.requestId = hopIds.requestId;
1561
- }
1562
- if (hopIds.parentRequestId) {
1563
- updated.parentRequestId = hopIds.parentRequestId;
1564
- } else {
1565
- delete updated.parentRequestId;
1566
- }
1556
+ applyPersistedHopIds(updated, resolvePersistedHopIds(cachedMock.mockData, liveCorrelation));
1567
1557
 
1568
1558
  if (this.databaseProvider) {
1569
1559
  await this.databaseProvider.save(updated);
@@ -1,15 +1,16 @@
1
1
  /**
2
2
  * Metro terminal keys for Mockifyer:
3
- * - `t` (default): start/stop Atlas capture (start opens the live stream page;
3
+ * - `t` (default): start/stop Atlas capture (start opens the live stream page
4
+ * and signals the app to call `enableMockifyer()` for `runtimeMode: 'manual'`;
4
5
  * stop ends capture — generate HTML from the live page “Render docs” button)
5
- * - `m` (default): open the Mockifyer dashboard in the browser
6
+ * - dashboard key is off unless `dashboardKey` is set (for example `"m"`)
6
7
  *
7
- * Opening `mockifyer-atlas` (SSE `/mockifyer-network-events/stream`) or the live
8
- * page auto-starts capture when idle — then press `t` once to stop, or use
9
- * Render docs on the live page without stopping.
8
+ * Capture starts only when `t` is pressed. Opening the live page or connecting
9
+ * the SSE stream does not start it. Press `t` again to stop, or use Render docs
10
+ * on the live page without stopping.
10
11
  *
11
12
  * `a` is reserved by Metro for Android (`i` iOS, `r` reload, `d` Dev Menu, `j` DevTools).
12
- * Disable Atlas with `atlasKey: false`, dashboard with `dashboardKey: false`.
13
+ * Disable Atlas with `atlasKey: false`.
13
14
  */
14
15
  import * as readline from "readline";
15
16
  import { spawn } from "child_process";
@@ -52,8 +53,8 @@ export interface AttachMetroAtlasKeyHandlerOptions {
52
53
  */
53
54
  atlasKey?: AtlasKeyOption;
54
55
  /**
55
- * Key that opens the dashboard in the browser (default `"m"`).
56
- * Pass `false` to disable.
56
+ * Key that opens the dashboard in the browser.
57
+ * Off unless set (for example `"m"`). Pass `false` to keep it off.
57
58
  */
58
59
  dashboardKey?: AtlasKeyOption;
59
60
  /**
@@ -61,7 +62,7 @@ export interface AttachMetroAtlasKeyHandlerOptions {
61
62
  * Mount prefix from `MOCKIFYER_DASHBOARD_BASE` is appended when set.
62
63
  */
63
64
  dashboardUrl?: string;
64
- /** Called on session start. `reason` is `key` (Metro `t`) or `stream` (live page connected). */
65
+ /** Called when capture starts. Only the Atlas key (`t`) starts a session. */
65
66
  onSessionStart?: (reason: MetroAtlasSessionStartReason) => void;
66
67
  /** Called on session stop — end capture (HTML is rendered from the live page). */
67
68
  onSessionStop?: () => void | Promise<void>;
@@ -131,11 +132,13 @@ export function resolveAtlasKeyOption(
131
132
  }
132
133
 
133
134
  /**
134
- * Normalize `dashboardKey` option: default `"m"`, `false` → disabled.
135
+ * Normalize `dashboardKey`. Omitted or `false` → disabled.
136
+ * An explicit letter (for example `"m"`) opens the dashboard.
135
137
  */
136
138
  export function resolveDashboardKeyOption(
137
139
  dashboardKey?: AtlasKeyOption,
138
140
  ): string | null {
141
+ if (dashboardKey === undefined || dashboardKey === false) return null;
139
142
  return resolveKeyOption(dashboardKey, DEFAULT_METRO_DASHBOARD_KEY);
140
143
  }
141
144
 
@@ -261,11 +264,11 @@ export function startMetroAtlasSession(
261
264
 
262
265
  if (reason === "stream") {
263
266
  console.log(
264
- `[Mockifyer] Atlas capture started (stream connected) — press ${key} to stop, or Render docs on the live page.`,
267
+ `[Mockifyer] Atlas capture started (stream connected) — Mockifyer activated (turns off again on stop if it was off). Press ${key} to stop, or Render docs on the live page.`,
265
268
  );
266
269
  } else {
267
270
  console.log(
268
- `[Mockifyer] Atlas capture started — press ${key} again to stop, or Render docs on the live page.`,
271
+ `[Mockifyer] Atlas capture started — Mockifyer activated (turns off again on stop if it was off). Press ${key} again to stop, or Render docs on the live page.`,
269
272
  );
270
273
  openMetroAtlasLivePage();
271
274
  }
@@ -301,7 +304,9 @@ export function stopMetroAtlasSession(): boolean {
301
304
  const startedAt = sessionStartedAt ?? Date.now();
302
305
  const duration = formatSessionDuration(startedAt);
303
306
  sessionPhase = "rendering";
304
- console.log(`[Mockifyer] Atlas capture stopped (${duration}).`);
307
+ console.log(
308
+ `[Mockifyer] Atlas capture stopped (${duration}) — Mockifyer turns off again if it was off before press t.`,
309
+ );
305
310
  Promise.resolve()
306
311
  .then(() => onSessionStop())
307
312
  .catch((err: unknown) => {
@@ -333,17 +338,15 @@ export function openMetroDashboard(): boolean {
333
338
  }
334
339
 
335
340
  /**
336
- * First SSE client for `/mockifyer-network-events/stream` auto-starts capture.
341
+ * SSE client connected to `/mockifyer-network-events/stream`.
342
+ * Does not start capture — that happens only when the Atlas key (`t`) is pressed.
337
343
  */
338
344
  export function notifyMetroAtlasStreamClientConnected(): void {
339
345
  streamSubscriberCount += 1;
340
- if (streamSubscriberCount === 1) {
341
- startMetroAtlasSession("stream");
342
- }
343
346
  }
344
347
 
345
348
  /**
346
- * SSE client disconnected — capture stays open until Metro `a` (or explicit stop).
349
+ * SSE client disconnected — capture stays open until the Atlas key is pressed again.
347
350
  */
348
351
  export function notifyMetroAtlasStreamClientDisconnected(): void {
349
352
  streamSubscriberCount = Math.max(0, streamSubscriberCount - 1);
@@ -526,12 +529,9 @@ export function attachMetroAtlasKeyHandler(
526
529
  const parts: string[] = [];
527
530
  if (atlasKey) {
528
531
  parts.push(
529
- `Press ${atlasKey} to start/stop Atlas capture (start opens live stream; Render docs on the page writes HTML)`,
532
+ `Press ${atlasKey} to start/stop Atlas capture (activates Mockifyer if off, restores off on stop; start opens live stream; Render docs on the page writes HTML)`,
530
533
  );
531
534
  }
532
- if (effectiveDashboardKey && dashboardUrl) {
533
- parts.push(`Press ${effectiveDashboardKey} to open dashboard (${dashboardUrl})`);
534
- }
535
535
  if (parts.length > 0) {
536
536
  console.log(`[Mockifyer] info ${parts.join(". ")}.`);
537
537
  }
@@ -172,7 +172,7 @@ export interface ConfigureMetroOptions {
172
172
  * projectRoot: __dirname,
173
173
  * mockDataPath: './mock-data',
174
174
  * // atlasKey: 't' (default) — Metro terminal: start/stop Atlas (start opens live stream; stop → HTML). `a` is Android.
175
- * // dashboardKey: 'm' (default) — open dashboard in browser
175
+ * // dashboardKey: 'm' — optional; off unless set. Opens the dashboard in the browser
176
176
  * // dashboardUrl: 'http://localhost:3002' — or MOCKIFYER_DASHBOARD_URL
177
177
  * },
178
178
  * });
@@ -21,9 +21,9 @@
21
21
  * 17. POST /mockifyer-network-events/clear — clear ring buffer
22
22
  * 18. GET /mockifyer-atlas-live — live hop stream web page (SSE + expand/collapse)
23
23
  * 19. GET /mockifyer-atlas-trace?id= — re-call a hop with X-Mockifyer-Include-Trace (`&format=html` opens a result tab)
24
- * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session active flag
25
- * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream; stop ends capture; Render docs on the live page writes HTML; stream auto-starts; `atlasKey: false` to disable). `a` is reserved for Android.
26
- * 22. Metro terminal key `m` — open Mockifyer dashboard in the browser (`dashboardKey: false` to disable)
24
+ * 20. GET /mockifyer-atlas-capture — Atlas `t` capture session (`active` plus `activateMockifyer` for the app)
25
+ * 21. Metro terminal key `t` — start/stop Atlas capture (start opens live stream and signals enableMockifyer; stop ends capture; Render docs on the live page writes HTML; capture does not start until `t`; `atlasKey: false` to disable). `a` is reserved for Android.
26
+ * 22. Optional Metro `dashboardKey` — open the dashboard in the browser (off unless set)
27
27
  * 23. On hop ingest / Atlas render — pull nested hops from dashboard `/api/network-events/trace` (remote BFF → dashboard → Atlas)
28
28
  *
29
29
  * The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
@@ -83,6 +83,7 @@ import {
83
83
  } from "@sgedda/mockifyer-core";
84
84
  import {
85
85
  attachMetroAtlasKeyHandler,
86
+ getMetroAtlasSessionPhase,
86
87
  notifyMetroAtlasStreamClientConnected,
87
88
  notifyMetroAtlasStreamClientDisconnected,
88
89
  resolveMetroDashboardUrl,
@@ -96,14 +97,15 @@ export interface MetroSyncMiddlewareOptions {
96
97
  mockDataPath?: string;
97
98
  /**
98
99
  * Metro terminal key that starts/stops Atlas capture (default `"t"`).
99
- * Start clears the hop buffer; stop ends capture. Write HTML via live page
100
- * “Render docs” (`POST /mockifyer-network-events/render`) or the CLI.
100
+ * Start clears the hop buffer and signals the app to enable Mockifyer; stop ends
101
+ * capture. Write HTML via live page “Render docs” (`POST /mockifyer-network-events/render`)
102
+ * or the CLI.
101
103
  * Pass `false` to disable. `a` is reserved by Metro for Android.
102
104
  */
103
105
  atlasKey?: AtlasKeyOption;
104
106
  /**
105
- * Metro terminal key that opens the dashboard in the browser (default `"m"`).
106
- * Pass `false` to disable.
107
+ * Metro terminal key that opens the dashboard in the browser.
108
+ * Off unless set (for example `"m"`).
107
109
  */
108
110
  dashboardKey?: AtlasKeyOption;
109
111
  /**
@@ -1664,18 +1666,24 @@ export function createMockSyncMiddleware(options?: MetroSyncMiddlewareOptions) {
1664
1666
  return;
1665
1667
  }
1666
1668
 
1667
- // Atlas `t` capture session flag — device / live page poll this for session UI.
1669
+ // Atlas `t` capture session — device polls this for include-trace and enableMockifyer().
1668
1670
  if (
1669
1671
  (url === ATLAS_CAPTURE_SESSION_PATH ||
1670
1672
  url === `${ATLAS_CAPTURE_SESSION_PATH}/`) &&
1671
1673
  req.method === "GET"
1672
1674
  ) {
1675
+ const phase = getMetroAtlasSessionPhase();
1676
+ const capturing = phase === "capturing";
1673
1677
  res.setHeader("Content-Type", "application/json");
1674
1678
  res.setHeader("Cache-Control", "no-store");
1675
1679
  res.end(
1676
1680
  JSON.stringify({
1677
1681
  success: true,
1678
1682
  active: isMetroAtlasCaptureSessionActive(),
1683
+ phase,
1684
+ capturing,
1685
+ /** App should call enableMockifyer() while capturing (runtimeMode: manual). */
1686
+ activateMockifyer: capturing,
1679
1687
  }),
1680
1688
  );
1681
1689
  return;
@@ -1933,8 +1941,7 @@ export function createMockSyncMiddleware(options?: MetroSyncMiddlewareOptions) {
1933
1941
  qIndex >= 0 ? fullUrl.slice(qIndex + 1) : "",
1934
1942
  );
1935
1943
  const backlog = params.get("backlog") !== "0";
1936
- // First SSE client (e.g. mockifyer-atlas) auto-starts Metro Atlas capture.
1937
- // Must run before reading the buffer — start clears hops for a clean session.
1944
+ // Track the SSE client. Capture starts only when Metro `t` is pressed.
1938
1945
  notifyMetroAtlasStreamClientConnected();
1939
1946
  const buffer = getMetroNetworkEventBuffer();
1940
1947
  res.writeHead(200, {