@capybara-devtools/core 0.5.0 → 0.6.0
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/client.d.ts.map +1 -1
- package/dist/client.js +20 -2
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +40 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +35 -0
- package/dist/init.js.map +1 -0
- package/dist/plugins/console.d.ts.map +1 -1
- package/dist/plugins/console.js +105 -9
- package/dist/plugins/console.js.map +1 -1
- package/dist/plugins/network/body.d.ts +9 -0
- package/dist/plugins/network/body.d.ts.map +1 -0
- package/dist/plugins/network/body.js +114 -0
- package/dist/plugins/network/body.js.map +1 -0
- package/dist/plugins/network/failure.d.ts +16 -0
- package/dist/plugins/network/failure.d.ts.map +1 -0
- package/dist/plugins/network/failure.js +73 -0
- package/dist/plugins/network/failure.js.map +1 -0
- package/dist/plugins/network/fetch.d.ts +12 -0
- package/dist/plugins/network/fetch.d.ts.map +1 -0
- package/dist/plugins/network/fetch.js +159 -0
- package/dist/plugins/network/fetch.js.map +1 -0
- package/dist/plugins/network/settings.d.ts +24 -0
- package/dist/plugins/network/settings.d.ts.map +1 -0
- package/dist/plugins/network/settings.js +26 -0
- package/dist/plugins/network/settings.js.map +1 -0
- package/dist/plugins/network/xhr.d.ts +4 -0
- package/dist/plugins/network/xhr.d.ts.map +1 -0
- package/dist/plugins/network/xhr.js +214 -0
- package/dist/plugins/network/xhr.js.map +1 -0
- package/dist/plugins/network.d.ts +2 -15
- package/dist/plugins/network.d.ts.map +1 -1
- package/dist/plugins/network.js +5 -518
- package/dist/plugins/network.js.map +1 -1
- package/dist/plugins/state/context-names.d.ts +13 -0
- package/dist/plugins/state/context-names.d.ts.map +1 -0
- package/dist/plugins/state/context-names.js +280 -0
- package/dist/plugins/state/context-names.js.map +1 -0
- package/dist/plugins/state/context.d.ts +0 -89
- package/dist/plugins/state/context.d.ts.map +1 -1
- package/dist/plugins/state/context.js +3 -474
- package/dist/plugins/state/context.js.map +1 -1
- package/dist/plugins/state/engine.d.ts +5 -0
- package/dist/plugins/state/engine.d.ts.map +1 -1
- package/dist/plugins/state/engine.js +12 -2
- package/dist/plugins/state/engine.js.map +1 -1
- package/dist/plugins/state/fibers.d.ts +75 -0
- package/dist/plugins/state/fibers.d.ts.map +1 -0
- package/dist/plugins/state/fibers.js +92 -0
- package/dist/plugins/state/fibers.js.map +1 -0
- package/dist/plugins/state/index.d.ts +5 -0
- package/dist/plugins/state/index.d.ts.map +1 -1
- package/dist/plugins/state/index.js.map +1 -1
- package/dist/plugins/state/mobx.d.ts +22 -0
- package/dist/plugins/state/mobx.d.ts.map +1 -0
- package/dist/plugins/state/mobx.js +69 -0
- package/dist/plugins/state/mobx.js.map +1 -0
- package/dist/plugins/state/react-hook.d.ts +19 -0
- package/dist/plugins/state/react-hook.d.ts.map +1 -0
- package/dist/plugins/state/react-hook.js +100 -0
- package/dist/plugins/state/react-hook.js.map +1 -0
- package/dist/plugins/state/redux.d.ts +2 -0
- package/dist/plugins/state/redux.d.ts.map +1 -1
- package/dist/plugins/state/redux.js +2 -2
- package/dist/plugins/state/redux.js.map +1 -1
- package/dist/plugins/state/xstate.d.ts +26 -0
- package/dist/plugins/state/xstate.d.ts.map +1 -0
- package/dist/plugins/state/xstate.js +85 -0
- package/dist/plugins/state/xstate.js.map +1 -0
- package/dist/plugins/storage.d.ts +1 -1
- package/dist/plugins/storage.d.ts.map +1 -1
- package/dist/plugins/storage.js +5 -7
- package/dist/plugins/storage.js.map +1 -1
- package/dist/serialize.d.ts.map +1 -1
- package/dist/serialize.js +1 -0
- package/dist/serialize.js.map +1 -1
- package/package.json +7 -5
- package/src/client.ts +19 -2
- package/src/index.ts +8 -2
- package/src/init.ts +66 -0
- package/src/plugins/console.ts +103 -10
- package/src/plugins/network/body.ts +114 -0
- package/src/plugins/network/failure.ts +79 -0
- package/src/plugins/network/fetch.ts +169 -0
- package/src/plugins/network/settings.ts +48 -0
- package/src/plugins/network/xhr.ts +223 -0
- package/src/plugins/network.ts +8 -572
- package/src/plugins/state/context-names.ts +265 -0
- package/src/plugins/state/context.ts +3 -532
- package/src/plugins/state/engine.ts +12 -2
- package/src/plugins/state/fibers.ts +160 -0
- package/src/plugins/state/index.ts +2 -0
- package/src/plugins/state/mobx.ts +80 -0
- package/src/plugins/state/react-hook.ts +98 -0
- package/src/plugins/state/redux.ts +2 -2
- package/src/plugins/state/xstate.ts +94 -0
- package/src/plugins/storage.ts +13 -8
- package/src/serialize.ts +2 -1
|
@@ -2,270 +2,9 @@ import type { StateStoreInfo } from '@capybara-devtools/protocol';
|
|
|
2
2
|
import type { PluginContext } from '../../client.js';
|
|
3
3
|
import type { StateSink, StoreDriver } from './index.js';
|
|
4
4
|
import { register, unregister } from './registry.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// and 19, which were checked. Nothing is ever written to a fiber: React prevents extensions to them in development.
|
|
9
|
-
|
|
10
|
-
/** A React fiber, as far as Capybara reads one. */
|
|
11
|
-
export interface Fiber {
|
|
12
|
-
tag: number;
|
|
13
|
-
key?: string | null;
|
|
14
|
-
type: any;
|
|
15
|
-
/** memo() keeps the wrapper here and the function in `type`. */
|
|
16
|
-
elementType?: any;
|
|
17
|
-
return: Fiber | null;
|
|
18
|
-
child: Fiber | null;
|
|
19
|
-
sibling: Fiber | null;
|
|
20
|
-
alternate: Fiber | null;
|
|
21
|
-
memoizedProps: any;
|
|
22
|
-
memoizedState?: any;
|
|
23
|
-
/** Children this fiber removed in the commit that made it. */
|
|
24
|
-
deletions?: Fiber[] | null;
|
|
25
|
-
_debugOwner?: unknown;
|
|
26
|
-
/** React 19: where the element was created. */
|
|
27
|
-
_debugStack?: unknown;
|
|
28
|
-
/** React 18 with the JSX source transform. */
|
|
29
|
-
_debugSource?: unknown;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** What React hands `onCommitFiberRoot`; `current` is the HostRoot fiber of the tree just committed. */
|
|
33
|
-
export interface FiberRoot {
|
|
34
|
-
current: Fiber;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Where a walk cut short by its budget goes on from. */
|
|
38
|
-
export interface WalkCursor {
|
|
39
|
-
top: Fiber;
|
|
40
|
-
node: Fiber;
|
|
41
|
-
/** Ancestors of `node` below `top`, outermost first. */
|
|
42
|
-
stack: Fiber[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface ProviderHit {
|
|
46
|
-
fiber: Fiber;
|
|
47
|
-
/** The context object, the same for every provider of one context. */
|
|
48
|
-
context: unknown;
|
|
49
|
-
value: unknown;
|
|
50
|
-
/** Neither this fiber nor its alternate was in `seen` before. */
|
|
51
|
-
first: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface WalkProvidersOptions {
|
|
55
|
-
/**
|
|
56
|
-
* Right after a commit: go only into subtrees whose children changed in it, and report known providers whose
|
|
57
|
-
* value changed too. Without it, every fiber is visited and only providers missing from `seen` are reported.
|
|
58
|
-
*/
|
|
59
|
-
changed?: boolean;
|
|
60
|
-
/** Return after about this many milliseconds, with `next` to go on from. Default: no limit. */
|
|
61
|
-
budget?: number;
|
|
62
|
-
/** Return once this many providers are reported, with `next` to go on from. Default: no limit. */
|
|
63
|
-
limit?: number;
|
|
64
|
-
from?: WalkCursor | null;
|
|
65
|
-
/** Called on every fiber visited, e.g. to look for stores in hooks. */
|
|
66
|
-
visit?: (fiber: Fiber) => void;
|
|
67
|
-
/** Clock for `budget`. Default `performance.now`, else `Date.now`. */
|
|
68
|
-
now?: () => number;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface WalkProvidersResult {
|
|
72
|
-
providers: ProviderHit[];
|
|
73
|
-
visited: number;
|
|
74
|
-
/** With `changed`: a fiber visited removed children in the commit, so some provider may be gone. */
|
|
75
|
-
removed: boolean;
|
|
76
|
-
/** Set when the budget ran out first. */
|
|
77
|
-
next: WalkCursor | null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Finds context providers (tag 10) under `root`, a FiberRoot or a fiber. `seen` maps each provider fiber, and its
|
|
82
|
-
* alternate, to the last value reported, and is updated as providers are reported.
|
|
83
|
-
*
|
|
84
|
-
* Self-contained, like the engine: the Metro bridge runs it from its source inside apps without the SDK. Iterative
|
|
85
|
-
* with a stack of its own rather than `return` pointers, which may lead to the other copy of a parent.
|
|
86
|
-
*/
|
|
87
|
-
export function walkProviders(root: object, seen: WeakMap<object, unknown>, options: WalkProvidersOptions = {}): WalkProvidersResult {
|
|
88
|
-
const PROVIDER = 10;
|
|
89
|
-
const changed = options.changed === true;
|
|
90
|
-
const visit = typeof options.visit === 'function' ? options.visit : undefined;
|
|
91
|
-
const clock =
|
|
92
|
-
typeof options.now === 'function'
|
|
93
|
-
? options.now
|
|
94
|
-
: typeof performance === 'object' && performance !== null && typeof performance.now === 'function'
|
|
95
|
-
? () => performance.now()
|
|
96
|
-
: () => Date.now();
|
|
97
|
-
const budget = typeof options.budget === 'number' && options.budget >= 0 ? options.budget : Infinity;
|
|
98
|
-
const deadline = budget === Infinity ? Infinity : clock() + budget;
|
|
99
|
-
const limit = typeof options.limit === 'number' && options.limit > 0 ? options.limit : Infinity;
|
|
100
|
-
const from = options.from;
|
|
101
|
-
const top: Fiber | null = from ? from.top : 'current' in root ? (root as FiberRoot).current : (root as Fiber);
|
|
102
|
-
const stack: Fiber[] = from ? from.stack.slice() : [];
|
|
103
|
-
const providers: ProviderHit[] = [];
|
|
104
|
-
let visited = 0;
|
|
105
|
-
let removed = false;
|
|
106
|
-
let node: Fiber | null = from ? from.node : top;
|
|
107
|
-
|
|
108
|
-
while (node != null) {
|
|
109
|
-
visited++;
|
|
110
|
-
if (node.tag === PROVIDER) {
|
|
111
|
-
const type = node.type;
|
|
112
|
-
// React 19 renders the context itself; React 18 renders `context.Provider`, which points back to it.
|
|
113
|
-
const context = type != null && typeof type === 'object' && type._context != null ? type._context : type;
|
|
114
|
-
const props = node.memoizedProps;
|
|
115
|
-
const value = props != null && typeof props === 'object' ? props.value : undefined;
|
|
116
|
-
const alternate = node.alternate;
|
|
117
|
-
const own = seen.has(node);
|
|
118
|
-
if (!own && (alternate == null || !seen.has(alternate))) {
|
|
119
|
-
providers.push({ fiber: node, context, value, first: true });
|
|
120
|
-
seen.set(node, value);
|
|
121
|
-
if (alternate != null) seen.set(alternate, value);
|
|
122
|
-
} else if (changed) {
|
|
123
|
-
const last = own ? seen.get(node) : seen.get(alternate as Fiber);
|
|
124
|
-
if (!(last === value || (last !== last && value !== value))) {
|
|
125
|
-
providers.push({ fiber: node, context, value, first: false });
|
|
126
|
-
seen.set(node, value);
|
|
127
|
-
if (alternate != null) seen.set(alternate, value);
|
|
128
|
-
} else if (!own) {
|
|
129
|
-
seen.set(node, value);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (changed && !removed && node.deletions != null && node.deletions.length > 0) removed = true;
|
|
134
|
-
if (visit) visit(node);
|
|
135
|
-
|
|
136
|
-
let child: Fiber | null = node.child;
|
|
137
|
-
// A subtree that bailed out keeps its child fiber, so nothing under it changed in this commit.
|
|
138
|
-
if (changed && child != null && node.alternate != null && node.alternate.child === child) child = null;
|
|
139
|
-
if (child != null) {
|
|
140
|
-
stack.push(node);
|
|
141
|
-
node = child;
|
|
142
|
-
} else {
|
|
143
|
-
let next: Fiber | null = null;
|
|
144
|
-
while (node != null && node !== top) {
|
|
145
|
-
if (node.sibling != null) {
|
|
146
|
-
next = node.sibling;
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
node = stack.length > 0 ? stack.pop()! : null;
|
|
150
|
-
}
|
|
151
|
-
node = next;
|
|
152
|
-
}
|
|
153
|
-
if (
|
|
154
|
-
node != null &&
|
|
155
|
-
top != null &&
|
|
156
|
-
(providers.length >= limit || (deadline !== Infinity && (visited & 127) === 0 && clock() >= deadline))
|
|
157
|
-
) {
|
|
158
|
-
return { providers, visited, removed, next: { top, node, stack } };
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return { providers, visited, removed, next: null };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// ---------------------------------------------------------------------------
|
|
165
|
-
// The global hook
|
|
166
|
-
// ---------------------------------------------------------------------------
|
|
167
|
-
|
|
168
|
-
const HOOK = '__REACT_DEVTOOLS_GLOBAL_HOOK__';
|
|
169
|
-
const HOST_ROOT = 3;
|
|
170
|
-
|
|
171
|
-
/** React DevTools' global hook, as far as Capybara uses it. */
|
|
172
|
-
export interface ReactHook {
|
|
173
|
-
isDisabled?: unknown;
|
|
174
|
-
renderers?: unknown;
|
|
175
|
-
getFiberRoots?: (rendererID: unknown) => Iterable<unknown>;
|
|
176
|
-
onCommitFiberRoot?: unknown;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Has `onCommit` called after each commit React reports to the React DevTools global hook. An existing hook keeps
|
|
181
|
-
* its object, which React holds on to: only its `onCommitFiberRoot` property is wrapped, the original called
|
|
182
|
-
* first. Without a hook, installs a minimal one, which only works when this runs before react-dom loads.
|
|
183
|
-
* Returns the undo, which leaves a wrapper someone else wrapped since in place, passing calls on.
|
|
184
|
-
*/
|
|
185
|
-
export function installReactHook(onCommit: (root: FiberRoot) => void): { hook: ReactHook | undefined; undo: () => void } {
|
|
186
|
-
const none = { hook: undefined, undo: () => {} };
|
|
187
|
-
if (typeof window === 'undefined') return none;
|
|
188
|
-
try {
|
|
189
|
-
// Where React looks the hook up; the same object as `window` in browsers and React Native.
|
|
190
|
-
const scope = globalThis as Record<string, unknown>;
|
|
191
|
-
let hook = scope[HOOK] as ReactHook | null | undefined;
|
|
192
|
-
if (hook === undefined) {
|
|
193
|
-
hook = minimalHook();
|
|
194
|
-
Object.defineProperty(scope, HOOK, { value: hook, configurable: true, writable: true, enumerable: false });
|
|
195
|
-
if (scope[HOOK] !== hook) return none;
|
|
196
|
-
}
|
|
197
|
-
// `isDisabled` opts out of DevTools; React then never calls the hook, so there is nothing to wrap.
|
|
198
|
-
if (hook === null || typeof hook !== 'object' || hook.isDisabled) return none;
|
|
199
|
-
const target = hook;
|
|
200
|
-
const original = target.onCommitFiberRoot;
|
|
201
|
-
let active = true;
|
|
202
|
-
const wrapper = function (this: unknown, ...args: unknown[]) {
|
|
203
|
-
try {
|
|
204
|
-
if (typeof original === 'function') return original.apply(this, args);
|
|
205
|
-
} finally {
|
|
206
|
-
if (active) {
|
|
207
|
-
try {
|
|
208
|
-
onCommit(args[1] as FiberRoot);
|
|
209
|
-
} catch {
|
|
210
|
-
// never throw into React's commit
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
target.onCommitFiberRoot = wrapper;
|
|
216
|
-
if (target.onCommitFiberRoot !== wrapper) return none;
|
|
217
|
-
return {
|
|
218
|
-
hook: target,
|
|
219
|
-
undo: () => {
|
|
220
|
-
active = false;
|
|
221
|
-
try {
|
|
222
|
-
if (target.onCommitFiberRoot === wrapper) target.onCommitFiberRoot = original;
|
|
223
|
-
} catch {
|
|
224
|
-
// a hook frozen since keeps the wrapper, which now only passes calls on
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
};
|
|
228
|
-
} catch {
|
|
229
|
-
return none;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* What React needs of the hook, plus what React Refresh and DevTools read when they load later: `renderers`
|
|
235
|
-
* filled by `inject`, and `getFiberRoots`. No `checkDCE`, so React still suggests installing DevTools.
|
|
236
|
-
*/
|
|
237
|
-
function minimalHook(): ReactHook {
|
|
238
|
-
const renderers = new Map<number, unknown>();
|
|
239
|
-
const roots = new Map<number, Set<FiberRoot>>();
|
|
240
|
-
let nextId = 0;
|
|
241
|
-
const getFiberRoots = (id: number) => {
|
|
242
|
-
let mounted = roots.get(id);
|
|
243
|
-
if (!mounted) roots.set(id, (mounted = new Set()));
|
|
244
|
-
return mounted;
|
|
245
|
-
};
|
|
246
|
-
return {
|
|
247
|
-
renderers,
|
|
248
|
-
supportsFiber: true,
|
|
249
|
-
inject: (renderer: unknown) => {
|
|
250
|
-
const id = ++nextId;
|
|
251
|
-
renderers.set(id, renderer);
|
|
252
|
-
return id;
|
|
253
|
-
},
|
|
254
|
-
getFiberRoots,
|
|
255
|
-
// Keeps mounted roots the way React DevTools does, so a client started later finds them.
|
|
256
|
-
onCommitFiberRoot: (id: number, root: FiberRoot) => {
|
|
257
|
-
const state = root?.current?.memoizedState as { element?: unknown } | null | undefined;
|
|
258
|
-
if (state == null || state.element == null) getFiberRoots(id).delete(root);
|
|
259
|
-
else getFiberRoots(id).add(root);
|
|
260
|
-
},
|
|
261
|
-
onCommitFiberUnmount: () => {},
|
|
262
|
-
onPostCommitFiberRoot: () => {},
|
|
263
|
-
} as ReactHook;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// ---------------------------------------------------------------------------
|
|
267
|
-
// Stores
|
|
268
|
-
// ---------------------------------------------------------------------------
|
|
5
|
+
import { contextName, ownerName, vendorOf } from './context-names.js';
|
|
6
|
+
import { HOST_ROOT, walkProviders, type Fiber, type FiberRoot, type ProviderHit, type WalkCursor } from './fibers.js';
|
|
7
|
+
import { installReactHook } from './react-hook.js';
|
|
269
8
|
|
|
270
9
|
/** Per discovery step, so a large tree never blocks a frame: walking time, then providers to set up. */
|
|
271
10
|
const CHUNK_MS = 5;
|
|
@@ -649,271 +388,3 @@ function mounted(fiber: Fiber): boolean {
|
|
|
649
388
|
};
|
|
650
389
|
return reaches(fiber) || reaches(fiber.alternate);
|
|
651
390
|
}
|
|
652
|
-
|
|
653
|
-
// ---------------------------------------------------------------------------
|
|
654
|
-
// Names
|
|
655
|
-
// ---------------------------------------------------------------------------
|
|
656
|
-
|
|
657
|
-
const COMPONENT_TAGS = new Set([0, 1, 11, 14, 15]);
|
|
658
|
-
|
|
659
|
-
function contextName(context: object): string | undefined {
|
|
660
|
-
try {
|
|
661
|
-
const name = (context as { displayName?: unknown }).displayName;
|
|
662
|
-
return typeof name === 'string' && name !== '' ? name : undefined;
|
|
663
|
-
} catch {
|
|
664
|
-
return undefined;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* The component that rendered the provider: its owner, which development builds keep, else the closest one above.
|
|
670
|
-
* `owned` keeps to the owner, for an element created outside any render (e.g. passed to `root.render()`) has none.
|
|
671
|
-
*/
|
|
672
|
-
function ownerName(fiber: Fiber, owned = false): string | undefined {
|
|
673
|
-
try {
|
|
674
|
-
for (let owner = fiber._debugOwner as Fiber | { name?: unknown } | null | undefined, hops = 0; owner != null && hops < 20; hops++) {
|
|
675
|
-
// A Server Component owner is plain info with a name.
|
|
676
|
-
if (!('tag' in owner)) return typeof owner.name === 'string' && owner.name !== '' ? owner.name : undefined;
|
|
677
|
-
const name = componentName(owner);
|
|
678
|
-
if (name !== undefined) return name;
|
|
679
|
-
owner = owner._debugOwner as Fiber | null | undefined;
|
|
680
|
-
}
|
|
681
|
-
if (owned) return undefined;
|
|
682
|
-
for (let node = fiber.return, hops = 0; node != null && hops < 50; node = node.return, hops++) {
|
|
683
|
-
if (!COMPONENT_TAGS.has(node.tag)) continue;
|
|
684
|
-
const name = componentName(node);
|
|
685
|
-
if (name !== undefined) return name;
|
|
686
|
-
}
|
|
687
|
-
} catch {
|
|
688
|
-
// a getter that threw: numbered instead
|
|
689
|
-
}
|
|
690
|
-
return undefined;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function componentName(fiber: Fiber): string | undefined {
|
|
694
|
-
if (!COMPONENT_TAGS.has(fiber.tag)) return undefined;
|
|
695
|
-
// memo() and forwardRef() wrap the function, sometimes one inside the other.
|
|
696
|
-
let type = fiber.elementType ?? fiber.type;
|
|
697
|
-
for (let depth = 0; type != null && depth < 3; depth++) {
|
|
698
|
-
const name: unknown = type.displayName ?? (typeof type === 'function' ? type.name : undefined);
|
|
699
|
-
// Bundlers rename clashing functions with a suffix, e.g. Vite's pre-bundling turns SWRConfig into SWRConfig$1.
|
|
700
|
-
if (typeof name === 'string' && name !== '') return name.replace(/\$\d+$/, '') || name;
|
|
701
|
-
if (typeof type !== 'object') return undefined;
|
|
702
|
-
type = type.render ?? type.type;
|
|
703
|
-
}
|
|
704
|
-
return undefined;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
// ---------------------------------------------------------------------------
|
|
708
|
-
// Library contexts
|
|
709
|
-
// ---------------------------------------------------------------------------
|
|
710
|
-
|
|
711
|
-
/** Contexts that libraries name, by display name. */
|
|
712
|
-
const LIBRARY_CONTEXTS = new Map(Object.entries({
|
|
713
|
-
ReactRedux: 'react-redux',
|
|
714
|
-
TextAncestorContext: 'React Native',
|
|
715
|
-
RootTagContext: 'React Native',
|
|
716
|
-
ScrollViewContext: 'React Native',
|
|
717
|
-
VirtualizedListContext: 'React Native',
|
|
718
|
-
ImageAnalyticsTagContext: 'React Native',
|
|
719
|
-
SafeAreaInsetsContext: 'react-native-safe-area-context',
|
|
720
|
-
SafeAreaFrameContext: 'react-native-safe-area-context',
|
|
721
|
-
LinkingContext: 'React Navigation',
|
|
722
|
-
LocaleDirContext: 'React Navigation',
|
|
723
|
-
UnhandledLinkingContext: 'React Navigation',
|
|
724
|
-
HeaderBackContext: 'React Navigation',
|
|
725
|
-
HeaderHeightContext: 'React Navigation',
|
|
726
|
-
HeaderShownContext: 'React Navigation',
|
|
727
|
-
NavigatorContext: 'Expo Router',
|
|
728
|
-
RouteNode: 'Expo Router',
|
|
729
|
-
}));
|
|
730
|
-
|
|
731
|
-
/** Where React Navigation keeps the contexts it names, so that two copies of it share them. */
|
|
732
|
-
const REACT_NAVIGATION_CONTEXTS = '__react_navigation__elements_contexts';
|
|
733
|
-
|
|
734
|
-
/** Library components that render providers, by the names their code gives them. Names an app might use are left out. */
|
|
735
|
-
const LIBRARY_COMPONENTS = new Map(Object.entries({
|
|
736
|
-
QueryClientProvider: 'TanStack Query',
|
|
737
|
-
QueryErrorResetBoundary: 'TanStack Query',
|
|
738
|
-
PersistQueryClientProvider: 'TanStack Query',
|
|
739
|
-
HydrationBoundary: 'TanStack Query',
|
|
740
|
-
SWRConfig: 'SWR',
|
|
741
|
-
BaseNavigationContainer: 'React Navigation',
|
|
742
|
-
NavigationContainerInner: 'React Navigation',
|
|
743
|
-
NavigationIndependentTree: 'React Navigation',
|
|
744
|
-
NavigationProvider: 'React Navigation',
|
|
745
|
-
NavigationStateListenerProvider: 'React Navigation',
|
|
746
|
-
PreventRemoveProvider: 'React Navigation',
|
|
747
|
-
EnsureSingleNavigator: 'React Navigation',
|
|
748
|
-
SceneView: 'React Navigation',
|
|
749
|
-
ServerContainer: 'React Navigation',
|
|
750
|
-
FrameSizeProvider: 'React Navigation',
|
|
751
|
-
AnimatedHeaderHeightProvider: 'React Navigation',
|
|
752
|
-
BottomTabView: 'React Navigation',
|
|
753
|
-
CardContainerInner: 'React Navigation',
|
|
754
|
-
DrawerViewBase: 'React Navigation',
|
|
755
|
-
NativeStackView: 'React Navigation',
|
|
756
|
-
MaterialTopTabView: 'React Navigation',
|
|
757
|
-
NavigationContent: 'React Navigation',
|
|
758
|
-
PanGestureHandler: 'React Navigation',
|
|
759
|
-
ExpoRoot: 'Expo Router',
|
|
760
|
-
ContextNavigator: 'Expo Router',
|
|
761
|
-
InnerRoot: 'Expo Router',
|
|
762
|
-
RootModalProvider: 'Expo Router',
|
|
763
|
-
LinkPreviewContextProvider: 'Expo Router',
|
|
764
|
-
NativeTabsNavigator: 'Expo Router',
|
|
765
|
-
SlotNavigator: 'Expo Router',
|
|
766
|
-
SplitViewNavigator: 'Expo Router',
|
|
767
|
-
ExperimentalStackNavigator: 'Expo Router',
|
|
768
|
-
ZoomTransitionSourceContextProvider: 'Expo Router',
|
|
769
|
-
ZoomTransitionTargetContextProvider: 'Expo Router',
|
|
770
|
-
// Expo Router 58, which carries its own copy of React Navigation.
|
|
771
|
-
RoutingQueueProvider: 'Expo Router',
|
|
772
|
-
RemovalEventEmitterRegistryProvider: 'Expo Router',
|
|
773
|
-
RoutesWithRemovalPreventedProvider: 'Expo Router',
|
|
774
|
-
PreventRemovalProvider: 'Expo Router',
|
|
775
|
-
GuardContextProvider: 'Expo Router',
|
|
776
|
-
NavigationActivityProvider: 'Expo Router',
|
|
777
|
-
StandardRouterNavigator: 'Expo Router',
|
|
778
|
-
NativeTabsNavigatorWrapper: 'Expo Router',
|
|
779
|
-
NativeTabsContent: 'Expo Router',
|
|
780
|
-
NativeStackNavigatorContent: 'Expo Router',
|
|
781
|
-
ExperimentalStackNavigatorContent: 'Expo Router',
|
|
782
|
-
HeaderToolbarHostBase: 'Expo Router',
|
|
783
|
-
StackToolbarHeader: 'Expo Router',
|
|
784
|
-
StackToolbarBottom: 'Expo Router',
|
|
785
|
-
NativeToolbarMenu: 'Expo Router',
|
|
786
|
-
LinkWithPreview: 'Expo Router',
|
|
787
|
-
HrefPreview: 'Expo Router',
|
|
788
|
-
PreviewForRootHrefState: 'Expo Router',
|
|
789
|
-
InnerRouter: 'Expo Router',
|
|
790
|
-
FocusedHead: 'Expo Router',
|
|
791
|
-
HeadShim: 'Expo Router',
|
|
792
|
-
WebSafeAreaProvider: 'Expo Router',
|
|
793
|
-
ServerRoot: 'Expo Router',
|
|
794
|
-
ServerRouter: 'Expo Router',
|
|
795
|
-
ServerDocument: 'Expo Router',
|
|
796
|
-
SafeAreaProvider: 'react-native-safe-area-context',
|
|
797
|
-
ScreenStack: 'react-native-screens',
|
|
798
|
-
ScreenStackItem: 'react-native-screens',
|
|
799
|
-
InnerScreen: 'react-native-screens',
|
|
800
|
-
GestureDetectorProvider: 'react-native-screens',
|
|
801
|
-
ReanimatedScreenProvider: 'react-native-screens',
|
|
802
|
-
ReanimatedNativeStackScreen: 'react-native-screens',
|
|
803
|
-
GestureHandlerRootView: 'react-native-gesture-handler',
|
|
804
|
-
InterceptingGestureDetector: 'react-native-gesture-handler',
|
|
805
|
-
ScrollViewResponderProvider: 'react-native-gesture-handler',
|
|
806
|
-
SkipEntering: 'react-native-reanimated',
|
|
807
|
-
AppContainer: 'React Native',
|
|
808
|
-
}));
|
|
809
|
-
|
|
810
|
-
/** Expo Router names each route's component after the route, e.g. `Route(index)`. */
|
|
811
|
-
const EXPO_ROUTE = /^Route\(.*\)$/;
|
|
812
|
-
|
|
813
|
-
/**
|
|
814
|
-
* Library components whose names an app might use too, recognized by the component that rendered them: Expo
|
|
815
|
-
* Router's `Content` comes from ContextNavigator, its `Route` from a route's own component.
|
|
816
|
-
*/
|
|
817
|
-
const LIBRARY_COMPONENTS_BY_OWNER = new Map<string, (owner: string) => boolean>([
|
|
818
|
-
['Content', (owner) => owner === 'ContextNavigator'],
|
|
819
|
-
['Route', (owner) => EXPO_ROUTE.test(owner)],
|
|
820
|
-
]);
|
|
821
|
-
|
|
822
|
-
const NAVIGATION_COLORS = ['primary', 'background', 'card', 'text', 'border', 'notification'];
|
|
823
|
-
|
|
824
|
-
/** A React Navigation theme, which its ThemeContext holds; an app's ThemeContext usually holds something else. */
|
|
825
|
-
function isNavigationTheme(value: Record<string, unknown>): boolean {
|
|
826
|
-
const colors = value.colors as Record<string, unknown> | null | undefined;
|
|
827
|
-
return typeof value.dark === 'boolean' && colors != null && NAVIGATION_COLORS.every((key) => typeof colors[key] === 'string');
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Expo Router's layouts (Stack, Tabs, Drawer…) carry `Screen` and `Protected` as statics, and some are anonymous
|
|
832
|
-
* arrow functions, so the name of the app's layout that renders them would be taken for theirs.
|
|
833
|
-
*/
|
|
834
|
-
function ownedByExpoRouterLayout(fiber: Fiber): boolean {
|
|
835
|
-
const owner = fiber._debugOwner as Fiber | null | undefined;
|
|
836
|
-
if (owner == null || !('tag' in owner)) return false;
|
|
837
|
-
for (let type = owner.elementType ?? owner.type, depth = 0; type != null && depth < 3; depth++) {
|
|
838
|
-
if (typeof type.Screen === 'function' && typeof type.Protected === 'function') return true;
|
|
839
|
-
if (typeof type !== 'object') return false;
|
|
840
|
-
type = type.render ?? type.type;
|
|
841
|
-
}
|
|
842
|
-
return false;
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
/** The name of the component that rendered the owner: one step past `ownerName(fiber, true)`. */
|
|
846
|
-
function ownerOfOwnerName(fiber: Fiber): string | undefined {
|
|
847
|
-
for (let owner = fiber._debugOwner as Fiber | null | undefined, hops = 0; owner != null && 'tag' in owner && hops < 20; hops++) {
|
|
848
|
-
if (componentName(owner) !== undefined) return ownerName(owner, true);
|
|
849
|
-
owner = owner._debugOwner as Fiber | null | undefined;
|
|
850
|
-
}
|
|
851
|
-
return undefined;
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
/**
|
|
855
|
-
* The library a context comes from, or undefined for the app's own. Runs once per store. Only the owner counts, not
|
|
856
|
-
* a component above: an app's provider passed to `root.render()` inside a library's has no owner.
|
|
857
|
-
*/
|
|
858
|
-
function vendorOf(fiber: Fiber, context: object, value: unknown): string | undefined {
|
|
859
|
-
try {
|
|
860
|
-
const redux = (globalThis as Record<symbol, unknown>)[Symbol.for('react-redux-context')];
|
|
861
|
-
if (redux instanceof Map) for (const known of redux.values()) if (known === context) return 'react-redux';
|
|
862
|
-
const navigation = (globalThis as Record<string, unknown>)[REACT_NAVIGATION_CONTEXTS];
|
|
863
|
-
if (navigation instanceof Map) for (const known of navigation.values()) if (known === context) return 'React Navigation';
|
|
864
|
-
// Bundlers number names they had to rename, e.g. `SWRConfig$1`.
|
|
865
|
-
const plain = (text: string | undefined) => text?.replace(/(?:\$|_)\d+$/, '');
|
|
866
|
-
const name = plain(contextName(context));
|
|
867
|
-
if (value !== null && typeof value === 'object') {
|
|
868
|
-
const v = value as Record<string, unknown>;
|
|
869
|
-
if (typeof v.getQueryCache === 'function' && typeof v.getMutationCache === 'function') return 'TanStack Query';
|
|
870
|
-
const cache = v.cache as Record<string, unknown> | null | undefined;
|
|
871
|
-
if (typeof v.mutate === 'function' && cache != null && typeof cache.get === 'function' && typeof cache.set === 'function') {
|
|
872
|
-
return 'SWR';
|
|
873
|
-
}
|
|
874
|
-
if (name === 'ThemeContext' && isNavigationTheme(v)) return 'React Navigation';
|
|
875
|
-
}
|
|
876
|
-
if (ownedByExpoRouterLayout(fiber)) return 'Expo Router';
|
|
877
|
-
const owner = plain(ownerName(fiber, true));
|
|
878
|
-
if (owner === undefined) return LIBRARY_CONTEXTS.get(name ?? '') ?? sourcePackage(fiber);
|
|
879
|
-
const byOwner = LIBRARY_COMPONENTS_BY_OWNER.get(owner);
|
|
880
|
-
const byName =
|
|
881
|
-
LIBRARY_CONTEXTS.get(name ?? '') ??
|
|
882
|
-
LIBRARY_COMPONENTS.get(owner) ??
|
|
883
|
-
(EXPO_ROUTE.test(owner) || (byOwner !== undefined && byOwner(plain(ownerOfOwnerName(fiber)) ?? '')) ? 'Expo Router' : undefined);
|
|
884
|
-
return byName ?? sourcePackage(fiber);
|
|
885
|
-
} catch {
|
|
886
|
-
return undefined;
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
/**
|
|
891
|
-
* The package whose code created the provider element, from where React says it was created. Web only: React
|
|
892
|
-
* Native's stacks name the bundle rather than files.
|
|
893
|
-
*/
|
|
894
|
-
function sourcePackage(fiber: Fiber): string | undefined {
|
|
895
|
-
let path: string | undefined;
|
|
896
|
-
const source = fiber._debugSource as { fileName?: unknown } | null | undefined;
|
|
897
|
-
if (typeof source?.fileName === 'string') {
|
|
898
|
-
path = source.fileName;
|
|
899
|
-
} else {
|
|
900
|
-
// React Native's frames name the bundle, never a file: not worth formatting the stack for.
|
|
901
|
-
if (typeof navigator === 'object' && navigator?.product === 'ReactNative') return undefined;
|
|
902
|
-
const stack = (fiber._debugStack as { stack?: unknown } | null | undefined)?.stack;
|
|
903
|
-
if (typeof stack !== 'string' || stack.includes('UnknownOwner')) return undefined;
|
|
904
|
-
// The first frame is React's jsxDEV or createElement; the next is the code that created the element.
|
|
905
|
-
const frame = stack.split('\n').filter((line) => /:\d+:\d+\)?\s*$/.test(line))[1];
|
|
906
|
-
path = frame && /((?:[a-z][\w+.-]*:\/\/|\/)[^\s()]+?)(?::\d+){1,2}\)?\s*$/i.exec(frame)?.[1]?.replace(/[?#].*$/, '');
|
|
907
|
-
}
|
|
908
|
-
if (path === undefined) return undefined;
|
|
909
|
-
// Turbopack names chunks after the path they hold, with underscores for slashes.
|
|
910
|
-
if (/\/node_modules_[^/]*$/.test(path)) return 'a dependency';
|
|
911
|
-
const at = path.lastIndexOf('node_modules/');
|
|
912
|
-
if (at < 0) return undefined;
|
|
913
|
-
const rest = path.slice(at + 'node_modules/'.length);
|
|
914
|
-
// Vite serves dependencies as `.vite/deps/<name>.js`, with `@scope_name` for scoped ones.
|
|
915
|
-
const vite = /^\.vite\/deps\/([^/]+?)\.js$/.exec(rest)?.[1];
|
|
916
|
-
if (vite !== undefined) return vite.startsWith('chunk-') ? 'a dependency' : vite.replace(/^(@[^_]+)_/, '$1/');
|
|
917
|
-
const parts = rest.split('/');
|
|
918
|
-
return (parts[0]?.startsWith('@') ? `${parts[0]}/${parts[1]}` : parts[0]) || 'a dependency';
|
|
919
|
-
}
|
|
@@ -30,6 +30,8 @@ export interface StateEngineOptions {
|
|
|
30
30
|
/** Runs before a state is diffed or read; only what it returns leaves the app. */
|
|
31
31
|
sanitizeState?(state: unknown, store: StateStoreInfo): unknown;
|
|
32
32
|
sanitizeAction?(action: unknown, store: StateStoreInfo): unknown;
|
|
33
|
+
/** Runs before the data of an entry is read; only what it returns leaves the app. */
|
|
34
|
+
sanitizeEntry?(data: unknown, entry: { store: StateStoreInfo; key: string }): unknown;
|
|
33
35
|
/** Leave out keys that hold functions, such as Zustand actions. Default true. */
|
|
34
36
|
hideFunctions?: boolean;
|
|
35
37
|
}
|
|
@@ -213,7 +215,7 @@ export function createStateEngine(
|
|
|
213
215
|
const ENTRY_LIMIT = 1_200;
|
|
214
216
|
/** Summaries per `state.entries` event; after a reset the `entries` command lists the rest. */
|
|
215
217
|
const ENTRIES_PER_EVENT = 200;
|
|
216
|
-
/**
|
|
218
|
+
/** MAX_BODY_SIZE, the cap of network bodies, written out since the Metro bridge runs this function on its own. */
|
|
217
219
|
const MAX_READ = 128 * 1024;
|
|
218
220
|
const PREVIEW: SerializeOptions = { maxDepth: 3, maxKeys: 20, maxItems: 20, maxString: 200, maxNodes: 200 };
|
|
219
221
|
// Tried in order until a read fits in MAX_READ.
|
|
@@ -323,6 +325,14 @@ export function createStateEngine(
|
|
|
323
325
|
return `[sanitizeState threw: ${message(error)}]`;
|
|
324
326
|
}
|
|
325
327
|
};
|
|
328
|
+
const cleanEntry = (t: Tracked, key: string, data: unknown): unknown => {
|
|
329
|
+
if (!options.sanitizeEntry) return data;
|
|
330
|
+
try {
|
|
331
|
+
return options.sanitizeEntry(data, { store: t.info, key });
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return `[sanitizeEntry threw: ${message(error)}]`;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
326
336
|
const actionType = (action: unknown): string => {
|
|
327
337
|
if (typeof action === 'string') return action;
|
|
328
338
|
try {
|
|
@@ -635,7 +645,7 @@ export function createStateEngine(
|
|
|
635
645
|
if (typeof driver.keys !== 'function') throw new Error(`${t.info.label} has no entries`);
|
|
636
646
|
const summary = summarize(driver, String(key));
|
|
637
647
|
if (!summary) return null;
|
|
638
|
-
const data = typeof driver.data === 'function' ? driver.data(String(key)) : undefined;
|
|
648
|
+
const data = cleanEntry(t, String(key), typeof driver.data === 'function' ? driver.data(String(key)) : undefined);
|
|
639
649
|
const { value, truncated } = fit(at(data, path));
|
|
640
650
|
return { summary, data: value, truncated };
|
|
641
651
|
};
|