@buoy-gg/network 7.0.4 → 7.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/lib/commonjs/index.js +85 -0
- package/lib/commonjs/network/components/NetworkEventDetailView.js +201 -40
- package/lib/commonjs/network/components/NetworkEventItemCompact.js +52 -5
- package/lib/commonjs/network/components/NetworkHeaderLive.js +132 -0
- package/lib/commonjs/network/components/NetworkListScreen.js +134 -56
- package/lib/commonjs/network/components/NetworkModal.js +154 -36
- package/lib/commonjs/network/components/NetworkSavedScreen.js +245 -0
- package/lib/commonjs/network/components/NetworkUpgradeBanner.js +76 -0
- package/lib/commonjs/network/hooks/useLiveNetworkEvent.js +62 -0
- package/lib/commonjs/network/hooks/useNetworkSaved.js +38 -0
- package/lib/commonjs/network/index.js +84 -0
- package/lib/commonjs/network/sync/networkSyncAdapter.js +85 -7
- package/lib/commonjs/network/utils/generateCurl.js +71 -0
- package/lib/commonjs/network/utils/networkEventStore.js +10 -1
- package/lib/commonjs/network/utils/networkListener.js +54 -1
- package/lib/commonjs/network/utils/networkSavedStore.js +575 -0
- package/lib/commonjs/network/utils/withPinnedFirst.js +32 -0
- package/lib/module/index.js +17 -1
- package/lib/module/network/components/NetworkEventDetailView.js +204 -41
- package/lib/module/network/components/NetworkEventItemCompact.js +53 -6
- package/lib/module/network/components/NetworkHeaderLive.js +132 -2
- package/lib/module/network/components/NetworkListScreen.js +136 -58
- package/lib/module/network/components/NetworkModal.js +156 -39
- package/lib/module/network/components/NetworkSavedScreen.js +241 -0
- package/lib/module/network/components/NetworkUpgradeBanner.js +72 -0
- package/lib/module/network/hooks/useLiveNetworkEvent.js +58 -0
- package/lib/module/network/hooks/useNetworkSaved.js +34 -0
- package/lib/module/network/index.js +7 -1
- package/lib/module/network/sync/networkSyncAdapter.js +85 -7
- package/lib/module/network/utils/generateCurl.js +67 -0
- package/lib/module/network/utils/networkEventStore.js +10 -1
- package/lib/module/network/utils/networkListener.js +54 -1
- package/lib/module/network/utils/networkSavedStore.js +569 -0
- package/lib/module/network/utils/withPinnedFirst.js +28 -0
- package/lib/typescript/index.d.ts +13 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkEventDetailView.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkEventItemCompact.d.ts +12 -0
- package/lib/typescript/network/components/NetworkEventItemCompact.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkHeaderLive.d.ts +28 -0
- package/lib/typescript/network/components/NetworkHeaderLive.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkListScreen.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkModal.d.ts +34 -0
- package/lib/typescript/network/components/NetworkModal.d.ts.map +1 -1
- package/lib/typescript/network/components/NetworkSavedScreen.d.ts +29 -0
- package/lib/typescript/network/components/NetworkSavedScreen.d.ts.map +1 -0
- package/lib/typescript/network/components/NetworkUpgradeBanner.d.ts +17 -0
- package/lib/typescript/network/components/NetworkUpgradeBanner.d.ts.map +1 -0
- package/lib/typescript/network/hooks/useLiveNetworkEvent.d.ts +19 -0
- package/lib/typescript/network/hooks/useLiveNetworkEvent.d.ts.map +1 -0
- package/lib/typescript/network/hooks/useNetworkSaved.d.ts +21 -0
- package/lib/typescript/network/hooks/useNetworkSaved.d.ts.map +1 -0
- package/lib/typescript/network/index.d.ts +8 -1
- package/lib/typescript/network/index.d.ts.map +1 -1
- package/lib/typescript/network/sync/networkSyncAdapter.d.ts +52 -3
- package/lib/typescript/network/sync/networkSyncAdapter.d.ts.map +1 -1
- package/lib/typescript/network/types/index.d.ts +43 -0
- package/lib/typescript/network/types/index.d.ts.map +1 -1
- package/lib/typescript/network/utils/generateCurl.d.ts +3 -0
- package/lib/typescript/network/utils/generateCurl.d.ts.map +1 -0
- package/lib/typescript/network/utils/networkEventStore.d.ts.map +1 -1
- package/lib/typescript/network/utils/networkListener.d.ts +4 -0
- package/lib/typescript/network/utils/networkListener.d.ts.map +1 -1
- package/lib/typescript/network/utils/networkSavedStore.d.ts +186 -0
- package/lib/typescript/network/utils/networkSavedStore.d.ts.map +1 -0
- package/lib/typescript/network/utils/withPinnedFirst.d.ts +24 -0
- package/lib/typescript/network/utils/withPinnedFirst.d.ts.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SESSION_ID = exports.MAX_SAVED = exports.MAX_PINNED = exports.FREE_TIER_SAVED_LIMIT = void 0;
|
|
7
|
+
exports.flagsForEventId = flagsForEventId;
|
|
8
|
+
exports.networkSavedStore = void 0;
|
|
9
|
+
var _sharedUi = require("@buoy-gg/shared-ui");
|
|
10
|
+
/**
|
|
11
|
+
* Pinned + saved network requests.
|
|
12
|
+
*
|
|
13
|
+
* The live `networkEventStore` is volatile by design: it caps at 500 events
|
|
14
|
+
* and `clearEvents()` wipes it. So "pin this failure so I don't lose it"
|
|
15
|
+
* cannot be an ID pointing into that store — it has to be a SNAPSHOT that
|
|
16
|
+
* outlives the stream. This store owns those snapshots and persists them.
|
|
17
|
+
*
|
|
18
|
+
* Two independent flags per record:
|
|
19
|
+
* - `pinned` — hoisted into the PINNED section at the top of the list,
|
|
20
|
+
* unaffected by search/filters.
|
|
21
|
+
* - `saved` — kept in the Saved (favorites) screen, out of the live stream.
|
|
22
|
+
* A record exists while either flag is true and is dropped when both go false,
|
|
23
|
+
* so all four states fall out without special cases.
|
|
24
|
+
*
|
|
25
|
+
* IDENTITY WARNING: `networkListener` mints request IDs from a per-runtime
|
|
26
|
+
* counter (`fetch_1`, `xhr_2`), so after an app reload `fetch_1` is a
|
|
27
|
+
* DIFFERENT request. Records are therefore keyed on a generated `key`, carry
|
|
28
|
+
* the `sessionId` that captured them, and only trust `liveId` while that
|
|
29
|
+
* session is the current one. Snapshots restored from an earlier session get
|
|
30
|
+
* their event `id` rewritten to `saved:<key>` so a restored pin can never
|
|
31
|
+
* resolve to (or dedupe against) an unrelated live request.
|
|
32
|
+
*
|
|
33
|
+
* Shape/persistence follows `ignoredPatternsStore` in @buoy-gg/shared-ui:
|
|
34
|
+
* lazy hydrate on first subscribe, a `dirty` flag so a slow read can't clobber
|
|
35
|
+
* a mutation, and commit = set + emit + persist.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/** Identifies THIS runtime. Records from other sessions can't trust `liveId`. */
|
|
39
|
+
const SESSION_ID = exports.SESSION_ID = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
40
|
+
|
|
41
|
+
/** Pins render un-virtualized in the list header, so the count stays bounded. */
|
|
42
|
+
const MAX_PINNED = exports.MAX_PINNED = 25;
|
|
43
|
+
/** Upper bound on the persisted favorites list (oldest unpinned evicted). */
|
|
44
|
+
const MAX_SAVED = exports.MAX_SAVED = 50;
|
|
45
|
+
/** Free tier keeps a handful of favorites; Pro is unlimited (up to MAX_SAVED). */
|
|
46
|
+
const FREE_TIER_SAVED_LIMIT = exports.FREE_TIER_SAVED_LIMIT = 5;
|
|
47
|
+
/** Per-body cap on a snapshot. */
|
|
48
|
+
const BODY_LIMIT = 16 * 1024;
|
|
49
|
+
/**
|
|
50
|
+
* Total body bytes kept across ALL records. `persistentStorage` falls back to
|
|
51
|
+
* AsyncStorage on RN CLI (no expo-file-system), which has real per-key size
|
|
52
|
+
* limits, so the blob is budgeted rather than left to grow. Bodies are dropped
|
|
53
|
+
* oldest-first, pins last.
|
|
54
|
+
*/
|
|
55
|
+
const TOTAL_BODY_BUDGET = 512 * 1024;
|
|
56
|
+
|
|
57
|
+
/** Why a toggle didn't take effect, so the UI can explain itself. */
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Is this actually a network event we can store and re-render later?
|
|
61
|
+
*
|
|
62
|
+
* This store WRITES TO DISK, which turns "accept whatever you're handed" into a
|
|
63
|
+
* permanent failure: a bad value becomes a record, the record is persisted, and
|
|
64
|
+
* every subsequent render of the pinned strip throws on it — a crash loop that
|
|
65
|
+
* survives restarts. Found exactly that way, by an automation harness invoking
|
|
66
|
+
* a row's `onLongPress` with a gesture object instead of the row's event
|
|
67
|
+
* (`TypeError: Cannot read property 'replace' of undefined` in the row, from
|
|
68
|
+
* `event.url.replace(...)`).
|
|
69
|
+
*
|
|
70
|
+
* `id` and `url` are the two fields the row and detail views dereference
|
|
71
|
+
* without guarding, so they are the bar for admission — both here, where
|
|
72
|
+
* records are created, and in `parseRecords`, so anything already on disk is
|
|
73
|
+
* dropped at hydration instead of crashing forever.
|
|
74
|
+
*/
|
|
75
|
+
function isStorableEvent(value) {
|
|
76
|
+
if (!value || typeof value !== "object") return false;
|
|
77
|
+
const event = value;
|
|
78
|
+
return typeof event.id === "string" && typeof event.url === "string";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Handlers the desktop dashboard installs to forward mutations to the device.
|
|
83
|
+
*
|
|
84
|
+
* The dashboard must not keep its own copy of the device's pins: the device is
|
|
85
|
+
* the only place the snapshots can stay in sync with the traffic that produced
|
|
86
|
+
* them (and the only place a >16KB body still exists). So in remote mode this
|
|
87
|
+
* store becomes a MIRROR — same seam `networkEventStore.disableCapture()` +
|
|
88
|
+
* `replaceEvents()` uses for the event list.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* How long a local optimistic write suppresses incoming mirrors.
|
|
93
|
+
*
|
|
94
|
+
* A toggle is applied locally AND forwarded; the device echoes the new state
|
|
95
|
+
* back on its next snapshot. Without this window the in-flight snapshot — the
|
|
96
|
+
* one already on the wire when the user tapped — would land first and visibly
|
|
97
|
+
* revert the toggle for a frame.
|
|
98
|
+
*/
|
|
99
|
+
const REMOTE_ECHO_GRACE_MS = 800;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Read an event's flags out of a state snapshot.
|
|
103
|
+
*
|
|
104
|
+
* Two lookups because an event ID means two different things: a live request
|
|
105
|
+
* matches by `liveId` (the fast Set path), while a snapshot restored from an
|
|
106
|
+
* earlier session carries the namespaced `saved:<key>` id and has to be found
|
|
107
|
+
* among the records.
|
|
108
|
+
*/
|
|
109
|
+
function flagsForEventId(state, eventId) {
|
|
110
|
+
if (!eventId) return {
|
|
111
|
+
pinned: false,
|
|
112
|
+
saved: false
|
|
113
|
+
};
|
|
114
|
+
const pinned = state.pinnedLiveIds.has(eventId);
|
|
115
|
+
const saved = state.savedLiveIds.has(eventId);
|
|
116
|
+
if (pinned || saved) return {
|
|
117
|
+
pinned,
|
|
118
|
+
saved
|
|
119
|
+
};
|
|
120
|
+
const record = state.records.find(entry => entry.event.id === eventId);
|
|
121
|
+
return record ? {
|
|
122
|
+
pinned: record.pinned,
|
|
123
|
+
saved: record.saved
|
|
124
|
+
} : {
|
|
125
|
+
pinned: false,
|
|
126
|
+
saved: false
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const EMPTY_STATE = {
|
|
130
|
+
records: [],
|
|
131
|
+
pinnedEvents: [],
|
|
132
|
+
savedRecords: [],
|
|
133
|
+
pinnedLiveIds: new Set(),
|
|
134
|
+
savedLiveIds: new Set()
|
|
135
|
+
};
|
|
136
|
+
function truncateBody(data) {
|
|
137
|
+
if (data === undefined || data === null) return {
|
|
138
|
+
value: data,
|
|
139
|
+
truncated: false
|
|
140
|
+
};
|
|
141
|
+
if (typeof data === "string") {
|
|
142
|
+
if (data.length <= BODY_LIMIT) return {
|
|
143
|
+
value: data,
|
|
144
|
+
truncated: false
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
value: `${data.slice(0, BODY_LIMIT)}\n\n… truncated by Buoy (body exceeded ${BODY_LIMIT} bytes)`,
|
|
148
|
+
truncated: true
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const serialized = JSON.stringify(data);
|
|
153
|
+
if (serialized === undefined) return {
|
|
154
|
+
value: undefined,
|
|
155
|
+
truncated: false
|
|
156
|
+
};
|
|
157
|
+
if (serialized.length <= BODY_LIMIT) return {
|
|
158
|
+
value: data,
|
|
159
|
+
truncated: false
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
value: `${serialized.slice(0, BODY_LIMIT)}\n\n… truncated by Buoy (body exceeded ${BODY_LIMIT} bytes)`,
|
|
163
|
+
truncated: true
|
|
164
|
+
};
|
|
165
|
+
} catch {
|
|
166
|
+
// Circular or otherwise unserializable — it can't be persisted anyway.
|
|
167
|
+
return {
|
|
168
|
+
value: "[unserializable body]",
|
|
169
|
+
truncated: true
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Freeze an event into a persistable snapshot with capped bodies. */
|
|
175
|
+
function snapshot(event) {
|
|
176
|
+
const request = truncateBody(event.requestData);
|
|
177
|
+
const response = truncateBody(event.responseData);
|
|
178
|
+
if (!request.truncated && !response.truncated) {
|
|
179
|
+
return {
|
|
180
|
+
event: {
|
|
181
|
+
...event
|
|
182
|
+
},
|
|
183
|
+
bodyTruncated: false
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
event: {
|
|
188
|
+
...event,
|
|
189
|
+
requestData: request.value,
|
|
190
|
+
responseData: response.value
|
|
191
|
+
},
|
|
192
|
+
bodyTruncated: true
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Bytes a snapshot's bodies actually occupy. Sizes are clamped to BODY_LIMIT
|
|
198
|
+
* because `snapshot()` already truncated anything larger, and a stripped body
|
|
199
|
+
* counts as zero — otherwise the budget would never converge.
|
|
200
|
+
*/
|
|
201
|
+
function bodyBytes(event) {
|
|
202
|
+
let total = 0;
|
|
203
|
+
if (event.requestData != null) {
|
|
204
|
+
total += Math.min(event.requestSize ?? 0, BODY_LIMIT);
|
|
205
|
+
}
|
|
206
|
+
if (event.responseData != null) {
|
|
207
|
+
total += Math.min(event.responseSize ?? 0, BODY_LIMIT);
|
|
208
|
+
}
|
|
209
|
+
return total;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Enforce TOTAL_BODY_BUDGET by stripping bodies from the least important
|
|
214
|
+
* records first (oldest, unpinned). The record itself — URL, status, headers,
|
|
215
|
+
* timings, the error — always survives; only the payload goes.
|
|
216
|
+
*/
|
|
217
|
+
function applyBodyBudget(records) {
|
|
218
|
+
let total = 0;
|
|
219
|
+
for (const record of records) total += bodyBytes(record.event);
|
|
220
|
+
if (total <= TOTAL_BODY_BUDGET) return records;
|
|
221
|
+
|
|
222
|
+
// Keep bodies for pins first, then newest saves.
|
|
223
|
+
const priority = [...records].sort((a, b) => {
|
|
224
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
|
|
225
|
+
return b.savedAt - a.savedAt;
|
|
226
|
+
});
|
|
227
|
+
const stripped = new Set();
|
|
228
|
+
let kept = 0;
|
|
229
|
+
for (const record of priority) {
|
|
230
|
+
const size = bodyBytes(record.event);
|
|
231
|
+
if (kept + size <= TOTAL_BODY_BUDGET) {
|
|
232
|
+
kept += size;
|
|
233
|
+
} else {
|
|
234
|
+
stripped.add(record.key);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (stripped.size === 0) return records;
|
|
238
|
+
return records.map(record => stripped.has(record.key) ? {
|
|
239
|
+
...record,
|
|
240
|
+
event: {
|
|
241
|
+
...record.event,
|
|
242
|
+
requestData: undefined,
|
|
243
|
+
responseData: undefined
|
|
244
|
+
},
|
|
245
|
+
bodyTruncated: true
|
|
246
|
+
} : record);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Tolerate junk/older shapes in storage rather than throwing away the list. */
|
|
250
|
+
function parseRecords(raw) {
|
|
251
|
+
if (!Array.isArray(raw)) return [];
|
|
252
|
+
const parsed = [];
|
|
253
|
+
for (const entry of raw) {
|
|
254
|
+
if (!entry || typeof entry !== "object") continue;
|
|
255
|
+
const record = entry;
|
|
256
|
+
if (typeof record.key !== "string") continue;
|
|
257
|
+
// Not just "is an object" — a record whose event can't be rendered would
|
|
258
|
+
// crash the list on every launch, and storage is forever. Drop it instead.
|
|
259
|
+
if (!isStorableEvent(record.event)) continue;
|
|
260
|
+
if (!record.pinned && !record.saved) continue;
|
|
261
|
+
parsed.push({
|
|
262
|
+
key: record.key,
|
|
263
|
+
// A restored snapshot's `event.id` is a stale per-runtime counter, so it
|
|
264
|
+
// is rewritten to a namespaced id that can never collide with a live one.
|
|
265
|
+
event: {
|
|
266
|
+
...record.event,
|
|
267
|
+
id: `saved:${record.key}`
|
|
268
|
+
},
|
|
269
|
+
pinned: !!record.pinned,
|
|
270
|
+
saved: !!record.saved,
|
|
271
|
+
savedAt: typeof record.savedAt === "number" ? record.savedAt : 0,
|
|
272
|
+
sessionId: typeof record.sessionId === "string" ? record.sessionId : "unknown",
|
|
273
|
+
liveId: typeof record.liveId === "string" ? record.liveId : "",
|
|
274
|
+
bodyTruncated: !!record.bodyTruncated
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return parsed;
|
|
278
|
+
}
|
|
279
|
+
class NetworkSavedStore {
|
|
280
|
+
records = [];
|
|
281
|
+
listeners = new Set();
|
|
282
|
+
loaded = false;
|
|
283
|
+
loadPromise = null;
|
|
284
|
+
/** Set once a mutation happens so a slow initial load can't clobber it. */
|
|
285
|
+
dirty = false;
|
|
286
|
+
keyCounter = 0;
|
|
287
|
+
/**
|
|
288
|
+
* Live IDs tracked in THIS session. `syncFromLive` runs on every captured
|
|
289
|
+
* response, so the hot path has to be a Set membership test.
|
|
290
|
+
*/
|
|
291
|
+
trackedLiveIds = new Set();
|
|
292
|
+
/** Derived view, rebuilt on commit so consumers get stable references. */
|
|
293
|
+
state = EMPTY_STATE;
|
|
294
|
+
/** Set on the desktop dashboard — see NetworkSavedRemote. */
|
|
295
|
+
remote = null;
|
|
296
|
+
remoteEchoUntil = 0;
|
|
297
|
+
getState() {
|
|
298
|
+
return this.state;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ===========================================================================
|
|
302
|
+
// REMOTE MIRROR (desktop dashboard)
|
|
303
|
+
// ===========================================================================
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Turn this store into a mirror of a connected device's records. Mutations
|
|
307
|
+
* are applied locally for immediate feedback AND forwarded to the device,
|
|
308
|
+
* which is the source of truth; nothing is persisted to this machine.
|
|
309
|
+
*/
|
|
310
|
+
enableRemoteMode(remote) {
|
|
311
|
+
this.remote = remote;
|
|
312
|
+
}
|
|
313
|
+
disableRemoteMode() {
|
|
314
|
+
this.remote = null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Feed the device's records in (mirror mode only). `force` bypasses the
|
|
319
|
+
* echo guard — used when tearing the mirror down, where there is no later
|
|
320
|
+
* snapshot to converge on.
|
|
321
|
+
*/
|
|
322
|
+
replaceRecords(records, force = false) {
|
|
323
|
+
if (!this.remote) return;
|
|
324
|
+
// A local toggle is still in flight — the snapshot on the wire predates it
|
|
325
|
+
// and would flicker the button back. Drop this one; the next carries the
|
|
326
|
+
// device's real answer.
|
|
327
|
+
if (!force && Date.now() < this.remoteEchoUntil) return;
|
|
328
|
+
this.records = records;
|
|
329
|
+
this.rebuild();
|
|
330
|
+
this.emit();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Whether a record's `liveId` can be trusted against the current event
|
|
335
|
+
* stream. Locally that means "captured in this runtime"; mirrored, the ids
|
|
336
|
+
* belong to the device whose events we are also mirroring, so they line up
|
|
337
|
+
* by construction.
|
|
338
|
+
*/
|
|
339
|
+
sessionMatches(record) {
|
|
340
|
+
return this.remote !== null || record.sessionId === SESSION_ID;
|
|
341
|
+
}
|
|
342
|
+
getRecords() {
|
|
343
|
+
return this.records;
|
|
344
|
+
}
|
|
345
|
+
subscribe(listener) {
|
|
346
|
+
this.listeners.add(listener);
|
|
347
|
+
void this.ensureLoaded();
|
|
348
|
+
return () => {
|
|
349
|
+
this.listeners.delete(listener);
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Resolve an event the live store no longer has — a pin whose request was
|
|
355
|
+
* cleared or aged out, or a snapshot restored from an earlier session.
|
|
356
|
+
*/
|
|
357
|
+
getEventById(id) {
|
|
358
|
+
for (const record of this.records) {
|
|
359
|
+
if (record.event.id === id) return record.event;
|
|
360
|
+
}
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
isPinned(liveId) {
|
|
364
|
+
return this.state.pinnedLiveIds.has(liveId);
|
|
365
|
+
}
|
|
366
|
+
isSaved(liveId) {
|
|
367
|
+
return this.state.savedLiveIds.has(liveId);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Keep a tracked snapshot current while its request is still in flight —
|
|
372
|
+
* pinning a pending request would otherwise freeze it as "Pending" forever.
|
|
373
|
+
* Called from `networkEventStore` for every response/error, so the no-op
|
|
374
|
+
* path (nothing pinned) is a single empty-Set check.
|
|
375
|
+
*/
|
|
376
|
+
syncFromLive(event) {
|
|
377
|
+
if (this.trackedLiveIds.size === 0) return;
|
|
378
|
+
if (!this.trackedLiveIds.has(event.id)) return;
|
|
379
|
+
const index = this.records.findIndex(record => this.sessionMatches(record) && record.liveId === event.id);
|
|
380
|
+
if (index === -1) return;
|
|
381
|
+
const next = snapshot(event);
|
|
382
|
+
this.commit(this.records.map((record, i) => i === index ? {
|
|
383
|
+
...record,
|
|
384
|
+
event: next.event,
|
|
385
|
+
bodyTruncated: next.bodyTruncated
|
|
386
|
+
} : record));
|
|
387
|
+
}
|
|
388
|
+
togglePin(event) {
|
|
389
|
+
return this.setFlag(event, "pinned");
|
|
390
|
+
}
|
|
391
|
+
toggleSave(event) {
|
|
392
|
+
return this.setFlag(event, "saved");
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Drop a record outright, whatever its flags. */
|
|
396
|
+
remove(key) {
|
|
397
|
+
if (!this.records.some(record => record.key === key)) return;
|
|
398
|
+
this.remote?.removeRecord(key);
|
|
399
|
+
this.commit(this.records.filter(record => record.key !== key));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Empty the favorites list; pinned-only records stay pinned. */
|
|
403
|
+
clearSaved() {
|
|
404
|
+
if (!this.records.some(record => record.saved)) return;
|
|
405
|
+
this.remote?.clearSaved();
|
|
406
|
+
this.commit(this.records.filter(record => record.pinned).map(record => ({
|
|
407
|
+
...record,
|
|
408
|
+
saved: false
|
|
409
|
+
})));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Unpin everything; saved-only records stay saved. */
|
|
413
|
+
clearPinned() {
|
|
414
|
+
if (!this.records.some(record => record.pinned)) return;
|
|
415
|
+
this.remote?.clearPinned();
|
|
416
|
+
this.commit(this.records.filter(record => record.saved).map(record => ({
|
|
417
|
+
...record,
|
|
418
|
+
pinned: false
|
|
419
|
+
})));
|
|
420
|
+
}
|
|
421
|
+
setFlag(event, flag) {
|
|
422
|
+
// Gate every write on a real event — see isStorableEvent. Rejecting here is
|
|
423
|
+
// what keeps a bad caller from turning into persisted, crash-looping state.
|
|
424
|
+
if (!isStorableEvent(event)) return {
|
|
425
|
+
ok: false,
|
|
426
|
+
reason: "invalid-event"
|
|
427
|
+
};
|
|
428
|
+
const existing = this.findRecordFor(event);
|
|
429
|
+
|
|
430
|
+
// Mirror mode: tell the device first, then apply the same change locally so
|
|
431
|
+
// the button responds now instead of a snapshot later.
|
|
432
|
+
if (this.remote) {
|
|
433
|
+
const next = existing ? !existing[flag] : true;
|
|
434
|
+
if (flag === "pinned") this.remote.setPinned(event.id, next);else this.remote.setSaved(event.id, next);
|
|
435
|
+
this.remoteEchoUntil = Date.now() + REMOTE_ECHO_GRACE_MS;
|
|
436
|
+
}
|
|
437
|
+
if (existing) {
|
|
438
|
+
const next = !existing[flag];
|
|
439
|
+
if (next && !this.hasRoomFor(flag, existing.key)) {
|
|
440
|
+
return {
|
|
441
|
+
ok: false,
|
|
442
|
+
reason: flag === "pinned" ? "pin-cap" : "saved-cap"
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
const updated = {
|
|
446
|
+
...existing,
|
|
447
|
+
[flag]: next
|
|
448
|
+
};
|
|
449
|
+
this.commit(updated.pinned || updated.saved ? this.records.map(record => record.key === existing.key ? updated : record) : this.records.filter(record => record.key !== existing.key));
|
|
450
|
+
return {
|
|
451
|
+
ok: true,
|
|
452
|
+
active: next
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (!this.hasRoomFor(flag, null)) {
|
|
456
|
+
return {
|
|
457
|
+
ok: false,
|
|
458
|
+
reason: flag === "pinned" ? "pin-cap" : "saved-cap"
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
const savedAt = Date.now();
|
|
462
|
+
const frozen = snapshot(event);
|
|
463
|
+
const record = {
|
|
464
|
+
key: `${SESSION_ID}_${++this.keyCounter}`,
|
|
465
|
+
event: frozen.event,
|
|
466
|
+
pinned: flag === "pinned",
|
|
467
|
+
saved: flag === "saved",
|
|
468
|
+
savedAt,
|
|
469
|
+
sessionId: SESSION_ID,
|
|
470
|
+
liveId: event.id,
|
|
471
|
+
bodyTruncated: frozen.bodyTruncated
|
|
472
|
+
};
|
|
473
|
+
this.commit(this.evictIfNeeded([record, ...this.records]));
|
|
474
|
+
return {
|
|
475
|
+
ok: true,
|
|
476
|
+
active: true
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Match an event to its record: by live ID within this session, or by the
|
|
482
|
+
* namespaced snapshot ID when the row being toggled IS a restored snapshot.
|
|
483
|
+
*/
|
|
484
|
+
findRecordFor(event) {
|
|
485
|
+
return this.records.find(record => record.event.id === event.id || this.sessionMatches(record) && record.liveId === event.id);
|
|
486
|
+
}
|
|
487
|
+
hasRoomFor(flag, exceptKey) {
|
|
488
|
+
const limit = flag === "pinned" ? MAX_PINNED : MAX_SAVED;
|
|
489
|
+
const count = this.records.filter(record => record[flag] && record.key !== exceptKey).length;
|
|
490
|
+
return count < limit;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Trim the oldest UNPINNED saves once the list outgrows MAX_SAVED. */
|
|
494
|
+
evictIfNeeded(records) {
|
|
495
|
+
const saved = records.filter(record => record.saved);
|
|
496
|
+
if (saved.length <= MAX_SAVED) return records;
|
|
497
|
+
const evictable = saved.filter(record => !record.pinned).sort((a, b) => a.savedAt - b.savedAt).slice(0, saved.length - MAX_SAVED);
|
|
498
|
+
if (evictable.length === 0) return records;
|
|
499
|
+
const evicted = new Set(evictable.map(record => record.key));
|
|
500
|
+
return records.filter(record => !evicted.has(record.key));
|
|
501
|
+
}
|
|
502
|
+
ensureLoaded() {
|
|
503
|
+
// Mirror mode reads the device's records, never this machine's storage.
|
|
504
|
+
if (this.remote) return Promise.resolve();
|
|
505
|
+
if (this.loaded) return Promise.resolve();
|
|
506
|
+
if (this.loadPromise) return this.loadPromise;
|
|
507
|
+
this.loadPromise = (async () => {
|
|
508
|
+
try {
|
|
509
|
+
const stored = await _sharedUi.persistentStorage.getItem(_sharedUi.devToolsStorageKeys.network.saved());
|
|
510
|
+
// A pin may have raced this read — never overwrite user intent.
|
|
511
|
+
if (stored && !this.dirty) {
|
|
512
|
+
this.records = parseRecords(JSON.parse(stored));
|
|
513
|
+
this.rebuild();
|
|
514
|
+
}
|
|
515
|
+
} catch {
|
|
516
|
+
// Corrupt or unreadable — start empty rather than crashing the tool.
|
|
517
|
+
} finally {
|
|
518
|
+
this.loaded = true;
|
|
519
|
+
this.emit();
|
|
520
|
+
}
|
|
521
|
+
})();
|
|
522
|
+
return this.loadPromise;
|
|
523
|
+
}
|
|
524
|
+
persist() {
|
|
525
|
+
// The dashboard must not write the device's pins into its own storage —
|
|
526
|
+
// they'd outlive the connection and shadow the next device.
|
|
527
|
+
if (this.remote) return;
|
|
528
|
+
_sharedUi.persistentStorage.setItem(_sharedUi.devToolsStorageKeys.network.saved(), JSON.stringify(this.records)).catch(() => {
|
|
529
|
+
// Silently fail — records remain in memory for this session.
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
commit(next) {
|
|
533
|
+
this.dirty = true;
|
|
534
|
+
// The budget is applied at COMMIT, not at persist, so what the UI shows is
|
|
535
|
+
// exactly what survives a reload — no silently-vanishing bodies.
|
|
536
|
+
this.records = applyBodyBudget(next);
|
|
537
|
+
this.rebuild();
|
|
538
|
+
this.emit();
|
|
539
|
+
this.persist();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Rebuild the derived view once per mutation so renders get stable refs. */
|
|
543
|
+
rebuild() {
|
|
544
|
+
const pinnedRecords = [];
|
|
545
|
+
const savedRecords = [];
|
|
546
|
+
const pinnedLiveIds = new Set();
|
|
547
|
+
const savedLiveIds = new Set();
|
|
548
|
+
const tracked = new Set();
|
|
549
|
+
for (const record of this.records) {
|
|
550
|
+
if (record.pinned) pinnedRecords.push(record);
|
|
551
|
+
if (record.saved) savedRecords.push(record);
|
|
552
|
+
if (!this.sessionMatches(record) || !record.liveId) continue;
|
|
553
|
+
tracked.add(record.liveId);
|
|
554
|
+
if (record.pinned) pinnedLiveIds.add(record.liveId);
|
|
555
|
+
if (record.saved) savedLiveIds.add(record.liveId);
|
|
556
|
+
}
|
|
557
|
+
this.trackedLiveIds = tracked;
|
|
558
|
+
this.state = {
|
|
559
|
+
records: this.records,
|
|
560
|
+
pinnedEvents: pinnedRecords.map(record => record.event),
|
|
561
|
+
savedRecords,
|
|
562
|
+
pinnedLiveIds,
|
|
563
|
+
savedLiveIds
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
emit() {
|
|
567
|
+
this.listeners.forEach(listener => listener());
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Singleton — one instance per JS runtime, shared by the mobile modal, the
|
|
573
|
+
* desktop inline port and the sync adapter.
|
|
574
|
+
*/
|
|
575
|
+
const networkSavedStore = exports.networkSavedStore = new NetworkSavedStore();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.splitPinned = splitPinned;
|
|
7
|
+
/**
|
|
8
|
+
* Split a filtered event list into its PINNED section and the rest.
|
|
9
|
+
*
|
|
10
|
+
* Lives as a pure function because there are TWO list implementations — the
|
|
11
|
+
* mobile modal's `NetworkListScreen` and the desktop dashboard's hand-mirrored
|
|
12
|
+
* `NetworkInline` — and pin ordering silently diverging between them is
|
|
13
|
+
* exactly the drift `buoy.tools.json` exists to prevent.
|
|
14
|
+
*
|
|
15
|
+
* Pins are deliberately NOT filtered: they come straight from the saved store,
|
|
16
|
+
* so a pinned request stays visible no matter what the search box or the
|
|
17
|
+
* status chips say. The only thing the filtered list contributes is the
|
|
18
|
+
* dedupe — a pinned request that is still live must not render twice.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const EMPTY = [];
|
|
22
|
+
function splitPinned(filtered, pinnedEvents, /** Live IDs pinned in THIS session — from `networkSavedStore.getState()`. */
|
|
23
|
+
pinnedLiveIds) {
|
|
24
|
+
if (pinnedEvents.length === 0) return {
|
|
25
|
+
pinned: EMPTY,
|
|
26
|
+
rest: filtered
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
pinned: pinnedEvents,
|
|
30
|
+
rest: pinnedLiveIds.size === 0 ? filtered : filtered.filter(event => !pinnedLiveIds.has(event.id))
|
|
31
|
+
};
|
|
32
|
+
}
|
package/lib/module/index.js
CHANGED
|
@@ -18,9 +18,13 @@ export { networkToolPreset, createNetworkTool } from "./preset";
|
|
|
18
18
|
// =============================================================================
|
|
19
19
|
// COMPONENTS (For custom UI implementations)
|
|
20
20
|
// =============================================================================
|
|
21
|
-
|
|
21
|
+
// `NetworkToolView` is the same tool rendered inline (header bar + body)
|
|
22
|
+
// instead of inside a JsModal — for hosts that show tools in a panel.
|
|
23
|
+
export { NetworkModal, NetworkToolView } from "./network/components/NetworkModal";
|
|
22
24
|
export { NetworkEventDetailView } from "./network/components/NetworkEventDetailView";
|
|
23
25
|
export { NetworkEventItemCompact } from "./network/components/NetworkEventItemCompact";
|
|
26
|
+
export { NetworkSavedScreen } from "./network/components/NetworkSavedScreen";
|
|
27
|
+
export { NetworkSavedButton, NetworkDetailPinActions } from "./network/components/NetworkHeaderLive";
|
|
24
28
|
|
|
25
29
|
// =============================================================================
|
|
26
30
|
// HOOKS (For consuming network data in custom components)
|
|
@@ -30,7 +34,13 @@ export { useNetworkEvents } from "./network/hooks/useNetworkEvents";
|
|
|
30
34
|
// list and the header counters subscribe independently so a host UI never
|
|
31
35
|
// re-renders wholesale per network event.
|
|
32
36
|
export { useNetworkEventList } from "./network/hooks/useNetworkEventList";
|
|
37
|
+
export { useLiveNetworkEvent } from "./network/hooks/useLiveNetworkEvent";
|
|
33
38
|
export { useNetworkBadgeCounts } from "./network/hooks/useNetworkBadgeCounts";
|
|
39
|
+
// Pinned/saved requests. `splitPinned` is the shared pin-ordering util — the
|
|
40
|
+
// desktop dashboard's hand-mirrored list calls it too, so the two list
|
|
41
|
+
// implementations can't disagree about where a pin goes.
|
|
42
|
+
export { useNetworkSaved } from "./network/hooks/useNetworkSaved";
|
|
43
|
+
export { splitPinned } from "./network/utils/withPinnedFirst";
|
|
34
44
|
export { TickProvider, useTickEveryMinute } from "./network/hooks/useTickEveryMinute";
|
|
35
45
|
|
|
36
46
|
// =============================================================================
|
|
@@ -63,5 +73,11 @@ export { NetworkBodyResolverProvider } from "./network/sync/networkBodyResolver"
|
|
|
63
73
|
// =============================================================================
|
|
64
74
|
/** @internal */
|
|
65
75
|
export { networkEventStore } from "./network/utils/networkEventStore";
|
|
76
|
+
/**
|
|
77
|
+
* @internal — the desktop dashboard puts this in remote-mirror mode so pins
|
|
78
|
+
* made there are forwarded to the device instead of stored locally.
|
|
79
|
+
*/
|
|
80
|
+
export { networkSavedStore, flagsForEventId, FREE_TIER_SAVED_LIMIT, MAX_PINNED, MAX_SAVED } from "./network/utils/networkSavedStore";
|
|
81
|
+
export { generateCurl } from "./network/utils/generateCurl";
|
|
66
82
|
/** @internal */
|
|
67
83
|
export { networkListener, addNetworkListener, removeAllNetworkListeners } from "./network/utils/networkListener";
|