@capybara-devtools/core 0.2.1 → 0.3.1
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 -2
- package/dist/client.d.ts +3 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +9 -4
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/state/context.d.ts +102 -0
- package/dist/plugins/state/context.d.ts.map +1 -0
- package/dist/plugins/state/context.js +765 -0
- package/dist/plugins/state/context.js.map +1 -0
- package/dist/plugins/state/engine.d.ts +71 -0
- package/dist/plugins/state/engine.d.ts.map +1 -0
- package/dist/plugins/state/engine.js +710 -0
- package/dist/plugins/state/engine.js.map +1 -0
- package/dist/plugins/state/index.d.ts +59 -0
- package/dist/plugins/state/index.d.ts.map +1 -0
- package/dist/plugins/state/index.js +52 -0
- package/dist/plugins/state/index.js.map +1 -0
- package/dist/plugins/state/redux.d.ts +43 -0
- package/dist/plugins/state/redux.d.ts.map +1 -0
- package/dist/plugins/state/redux.js +580 -0
- package/dist/plugins/state/redux.js.map +1 -0
- package/dist/plugins/state/registry.d.ts +22 -0
- package/dist/plugins/state/registry.d.ts.map +1 -0
- package/dist/plugins/state/registry.js +109 -0
- package/dist/plugins/state/registry.js.map +1 -0
- package/dist/plugins/state/swr.d.ts +49 -0
- package/dist/plugins/state/swr.d.ts.map +1 -0
- package/dist/plugins/state/swr.js +359 -0
- package/dist/plugins/state/swr.js.map +1 -0
- package/dist/plugins/state/tanstack.d.ts +31 -0
- package/dist/plugins/state/tanstack.d.ts.map +1 -0
- package/dist/plugins/state/tanstack.js +461 -0
- package/dist/plugins/state/tanstack.js.map +1 -0
- package/dist/plugins/state/zustand.d.ts +21 -0
- package/dist/plugins/state/zustand.d.ts.map +1 -0
- package/dist/plugins/state/zustand.js +70 -0
- package/dist/plugins/state/zustand.js.map +1 -0
- package/package.json +11 -2
- package/src/client.ts +12 -6
- package/src/index.ts +19 -0
- package/src/plugins/state/context.ts +831 -0
- package/src/plugins/state/engine.ts +739 -0
- package/src/plugins/state/index.ts +112 -0
- package/src/plugins/state/redux.ts +609 -0
- package/src/plugins/state/registry.ts +126 -0
- package/src/plugins/state/swr.ts +375 -0
- package/src/plugins/state/tanstack.ts +488 -0
- package/src/plugins/state/zustand.ts +82 -0
|
@@ -0,0 +1,831 @@
|
|
|
1
|
+
import type { StateStoreInfo } from '@capybara-devtools/protocol';
|
|
2
|
+
import type { PluginContext } from '../../client.js';
|
|
3
|
+
import type { StateSink, StoreDriver } from './index.js';
|
|
4
|
+
import { register, unregister } from './registry.js';
|
|
5
|
+
|
|
6
|
+
// React Context is read from React's own fibers, through the global hook React DevTools uses. Fibers are React
|
|
7
|
+
// internals; what is read here (tags, `child`, `sibling`, `alternate`, `memoizedProps`) is the same in React 18
|
|
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
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
|
|
270
|
+
/** Per discovery step, so a large tree never blocks a frame: walking time, then providers to set up. */
|
|
271
|
+
const CHUNK_MS = 5;
|
|
272
|
+
const CHUNK_PROVIDERS = 250;
|
|
273
|
+
/** Unmounted providers are looked for at most this often, after a commit that removed something. */
|
|
274
|
+
const SWEEP_MS = 2_000;
|
|
275
|
+
/** Instance counts are sent once they settle, since a list can mount many providers one commit after another. */
|
|
276
|
+
const ANNOUNCE_MS = 300;
|
|
277
|
+
/** Context stores never take most of the registry's 100 places from the app's other stores. */
|
|
278
|
+
const MAX_APP_CONTEXTS = 40;
|
|
279
|
+
const MAX_LIBRARY_CONTEXTS = 20;
|
|
280
|
+
|
|
281
|
+
/** Up to this many providers under one name, the store's state holds each one's value; past it, the latest one. */
|
|
282
|
+
const MAX_KEYED = 20;
|
|
283
|
+
|
|
284
|
+
interface Instance {
|
|
285
|
+
group: Group;
|
|
286
|
+
/** Either copy of the provider's fiber. */
|
|
287
|
+
fiber: Fiber;
|
|
288
|
+
value: unknown;
|
|
289
|
+
/** Its key in the state of a store with several providers: the React key of the provider or of a component above it, else `#n`. */
|
|
290
|
+
key: string;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Providers of one context under one name: one store. */
|
|
294
|
+
interface Group {
|
|
295
|
+
context: object;
|
|
296
|
+
name: string;
|
|
297
|
+
label: string;
|
|
298
|
+
vendor: string | undefined;
|
|
299
|
+
instances: Set<Instance>;
|
|
300
|
+
/** Instances that took each key so far, so keys stay unique and are never reused. */
|
|
301
|
+
keys: Map<string, number>;
|
|
302
|
+
/** The value that changed last: the state of a store with more than `MAX_KEYED` providers. */
|
|
303
|
+
latest: unknown;
|
|
304
|
+
/** As last recorded, or as the engine read it when it started watching. */
|
|
305
|
+
state: unknown;
|
|
306
|
+
driver: StoreDriver;
|
|
307
|
+
/** From the registry; undefined while over the cap. */
|
|
308
|
+
id: string | undefined;
|
|
309
|
+
sink: StateSink | null;
|
|
310
|
+
storeId: string | undefined;
|
|
311
|
+
live: boolean;
|
|
312
|
+
/** Found by the walk under way, and registered once it ends, with the instances it found. */
|
|
313
|
+
fresh: boolean;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
interface RootState {
|
|
317
|
+
/** Set while the first walk of the tree is still going. */
|
|
318
|
+
cursor: WalkCursor | null;
|
|
319
|
+
commits: number;
|
|
320
|
+
ignored: boolean;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export interface ContextTracker {
|
|
324
|
+
/** The store's info as it is now; the engine keeps the one it was first given. */
|
|
325
|
+
info(store: StateStoreInfo): StateStoreInfo;
|
|
326
|
+
stop(): void;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Shows the React Context providers the app mounts as stores, one per context and name. The state is the provider's
|
|
331
|
+
* value, or with several providers each one's value by key. Read only, and only from development builds of React.
|
|
332
|
+
*/
|
|
333
|
+
export function trackContexts(ctx: Pick<PluginContext, 'emit' | 'console'>): ContextTracker {
|
|
334
|
+
const seen = new WeakMap<object, unknown>();
|
|
335
|
+
const instances = new WeakMap<object, Instance>();
|
|
336
|
+
const groups = new Map<object, Map<string, Group>>();
|
|
337
|
+
const labels = new Map<string, Group>();
|
|
338
|
+
const byId = new Map<string, Group>();
|
|
339
|
+
const roots = new WeakMap<object, RootState>();
|
|
340
|
+
const numbers = new WeakMap<object, number>();
|
|
341
|
+
const counts = { app: 0, library: 0 };
|
|
342
|
+
const announcing = new Set<Group>();
|
|
343
|
+
const fresh: Group[] = [];
|
|
344
|
+
/** Stores whose state may have changed, recorded once the walk under way ends. */
|
|
345
|
+
const dirty = new Set<Group>();
|
|
346
|
+
/** Roots whose first walk is still going; stores found meanwhile wait for it, to be announced once. */
|
|
347
|
+
let discovering = 0;
|
|
348
|
+
let numbered = 0;
|
|
349
|
+
let stopped = false;
|
|
350
|
+
let lastSweep = 0;
|
|
351
|
+
let sweepTimer: ReturnType<typeof setTimeout> | null = null;
|
|
352
|
+
let announceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
353
|
+
|
|
354
|
+
const all = (): Group[] => [...groups.values()].flatMap((byName) => [...byName.values()]);
|
|
355
|
+
const later = (run: () => void, ms: number) =>
|
|
356
|
+
setTimeout(() => {
|
|
357
|
+
if (stopped) return;
|
|
358
|
+
try {
|
|
359
|
+
run();
|
|
360
|
+
} catch (error) {
|
|
361
|
+
fail(error);
|
|
362
|
+
}
|
|
363
|
+
}, ms);
|
|
364
|
+
|
|
365
|
+
const announce = (group: Group, extra?: Partial<StateStoreInfo>) => {
|
|
366
|
+
if (group.sink === null || group.storeId === undefined) return;
|
|
367
|
+
ctx.emit('state.store', { store: { ...group.driver.info, ...extra, id: group.storeId } });
|
|
368
|
+
};
|
|
369
|
+
/** Whether the count changed. */
|
|
370
|
+
const count = (group: Group): boolean => {
|
|
371
|
+
const size = group.instances.size;
|
|
372
|
+
const info = group.driver.info;
|
|
373
|
+
if ((info.instances ?? 1) === size) return false;
|
|
374
|
+
if (size > 1) info.instances = size;
|
|
375
|
+
else delete info.instances;
|
|
376
|
+
return true;
|
|
377
|
+
};
|
|
378
|
+
const recount = (group: Group) => {
|
|
379
|
+
if (!count(group) || group.fresh) return;
|
|
380
|
+
announcing.add(group);
|
|
381
|
+
announceTimer ??= later(() => {
|
|
382
|
+
announceTimer = null;
|
|
383
|
+
for (const pending of announcing) if (pending.live) announce(pending);
|
|
384
|
+
announcing.clear();
|
|
385
|
+
}, ANNOUNCE_MS);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* One provider's value, or each one's by key: one value standing for several would make each diff compare one
|
|
390
|
+
* provider with another. Very many, such as React Native's one per `<Text>` that holds elements, keep the latest value.
|
|
391
|
+
*/
|
|
392
|
+
const stateOf = (group: Group): unknown => {
|
|
393
|
+
const size = group.instances.size;
|
|
394
|
+
if (size === 1) return group.instances.values().next().value!.value;
|
|
395
|
+
if (size > MAX_KEYED) return group.latest;
|
|
396
|
+
const state: Record<string, unknown> = {};
|
|
397
|
+
for (const instance of group.instances) state[instance.key] = instance.value;
|
|
398
|
+
return state;
|
|
399
|
+
};
|
|
400
|
+
/** Records the stores that changed, once each: a list that renders again changes all its providers in one commit. */
|
|
401
|
+
const flush = () => {
|
|
402
|
+
for (const group of dirty) {
|
|
403
|
+
if (!group.live || group.fresh) continue;
|
|
404
|
+
const next = stateOf(group);
|
|
405
|
+
if (next === group.state) continue;
|
|
406
|
+
group.state = next;
|
|
407
|
+
if (group.sink !== null && group.storeId !== undefined) group.sink.record(group.storeId, next, undefined, 'set');
|
|
408
|
+
}
|
|
409
|
+
dirty.clear();
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const enroll = (group: Group) => {
|
|
413
|
+
const kind = group.vendor === undefined ? 'app' : 'library';
|
|
414
|
+
if (counts[kind] >= (kind === 'app' ? MAX_APP_CONTEXTS : MAX_LIBRARY_CONTEXTS)) return;
|
|
415
|
+
group.id = register(group.driver);
|
|
416
|
+
if (group.id === undefined) return;
|
|
417
|
+
counts[kind]++;
|
|
418
|
+
byId.set(group.id, group);
|
|
419
|
+
};
|
|
420
|
+
const settle = () => {
|
|
421
|
+
flush();
|
|
422
|
+
if (discovering > 0) return;
|
|
423
|
+
for (const group of fresh.splice(0)) {
|
|
424
|
+
group.fresh = false;
|
|
425
|
+
if (!group.live) continue;
|
|
426
|
+
count(group);
|
|
427
|
+
group.state = stateOf(group);
|
|
428
|
+
enroll(group);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
const dispose = (group: Group) => {
|
|
432
|
+
group.live = false;
|
|
433
|
+
const byName = groups.get(group.context);
|
|
434
|
+
byName?.delete(group.name);
|
|
435
|
+
if (byName?.size === 0) groups.delete(group.context);
|
|
436
|
+
if (labels.get(group.label) === group) labels.delete(group.label);
|
|
437
|
+
announcing.delete(group);
|
|
438
|
+
dirty.delete(group);
|
|
439
|
+
if (group.id === undefined) return;
|
|
440
|
+
counts[group.vendor === undefined ? 'app' : 'library']--;
|
|
441
|
+
byId.delete(group.id);
|
|
442
|
+
unregister(group.id);
|
|
443
|
+
};
|
|
444
|
+
const prune = (group: Group) => {
|
|
445
|
+
let lost = false;
|
|
446
|
+
for (const instance of [...group.instances]) {
|
|
447
|
+
if (mounted(instance.fiber)) continue;
|
|
448
|
+
group.instances.delete(instance);
|
|
449
|
+
lost = true;
|
|
450
|
+
}
|
|
451
|
+
if (group.instances.size === 0) dispose(group);
|
|
452
|
+
else if (lost) {
|
|
453
|
+
recount(group);
|
|
454
|
+
dirty.add(group);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
const sweep = () => {
|
|
458
|
+
sweepTimer = null;
|
|
459
|
+
lastSweep = Date.now();
|
|
460
|
+
for (const group of all()) prune(group);
|
|
461
|
+
flush();
|
|
462
|
+
};
|
|
463
|
+
const sweepSoon = () => {
|
|
464
|
+
if (sweepTimer !== null) return;
|
|
465
|
+
sweepTimer = later(sweep, Math.max(0, lastSweep + SWEEP_MS - Date.now()));
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
/** One label per context, so the registry never takes two contexts for hot-reloaded copies of one store. */
|
|
469
|
+
const labelFor = (name: string, context: object): string => {
|
|
470
|
+
for (let n = 1; ; n++) {
|
|
471
|
+
const label = n === 1 ? name : `${name} #${n}`;
|
|
472
|
+
const holder = labels.get(label);
|
|
473
|
+
if (holder?.live) prune(holder);
|
|
474
|
+
if (!holder?.live || holder.context === context) return label;
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
const nameFor = (fiber: Fiber, context: object): string => {
|
|
478
|
+
const name = contextName(context) ?? ownerName(fiber);
|
|
479
|
+
if (name !== undefined) return name;
|
|
480
|
+
let n = numbers.get(context);
|
|
481
|
+
if (n === undefined) numbers.set(context, (n = ++numbered));
|
|
482
|
+
return `Context #${n}`;
|
|
483
|
+
};
|
|
484
|
+
const keyFor = (fiber: Fiber, group: Group): string => {
|
|
485
|
+
let base: string | undefined;
|
|
486
|
+
// A list keys the provider itself or the component that renders it.
|
|
487
|
+
for (let node: Fiber | null = fiber, hops = 0; node != null && hops < 6 && base === undefined; node = node.return, hops++) {
|
|
488
|
+
if (node.key != null) base = String(node.key);
|
|
489
|
+
}
|
|
490
|
+
const n = (group.keys.get(base ?? '#') ?? 0) + 1;
|
|
491
|
+
group.keys.set(base ?? '#', n);
|
|
492
|
+
if (base === undefined) return `#${n}`;
|
|
493
|
+
return n === 1 ? base : `${base} #${n}`;
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const add = (fiber: Fiber, context: object, value: unknown) => {
|
|
497
|
+
const name = nameFor(fiber, context);
|
|
498
|
+
let byName = groups.get(context);
|
|
499
|
+
let group = byName?.get(name);
|
|
500
|
+
if (!group) {
|
|
501
|
+
const vendor = vendorOf(fiber, context, value);
|
|
502
|
+
const label = labelFor(name, context);
|
|
503
|
+
const created: Group = {
|
|
504
|
+
context,
|
|
505
|
+
name,
|
|
506
|
+
label,
|
|
507
|
+
vendor,
|
|
508
|
+
instances: new Set(),
|
|
509
|
+
keys: new Map(),
|
|
510
|
+
latest: value,
|
|
511
|
+
state: undefined,
|
|
512
|
+
id: undefined,
|
|
513
|
+
sink: null,
|
|
514
|
+
storeId: undefined,
|
|
515
|
+
live: true,
|
|
516
|
+
fresh: true,
|
|
517
|
+
driver: {
|
|
518
|
+
info: { library: 'context', label, via: 'fiber', actions: false, ...(vendor !== undefined && { vendor }) },
|
|
519
|
+
getState: () => created.state,
|
|
520
|
+
start(sink, id) {
|
|
521
|
+
created.sink = sink;
|
|
522
|
+
created.storeId = id;
|
|
523
|
+
return () => {
|
|
524
|
+
if (created.sink === sink) created.sink = null;
|
|
525
|
+
};
|
|
526
|
+
},
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
group = created;
|
|
530
|
+
if (!byName) groups.set(context, (byName = new Map()));
|
|
531
|
+
byName.set(name, group);
|
|
532
|
+
labels.set(label, group);
|
|
533
|
+
fresh.push(group);
|
|
534
|
+
}
|
|
535
|
+
const instance: Instance = { group, fiber, value, key: keyFor(fiber, group) };
|
|
536
|
+
instances.set(fiber, instance);
|
|
537
|
+
if (fiber.alternate) instances.set(fiber.alternate, instance);
|
|
538
|
+
group.instances.add(instance);
|
|
539
|
+
dirty.add(group);
|
|
540
|
+
recount(group);
|
|
541
|
+
};
|
|
542
|
+
const handle = (hit: ProviderHit) => {
|
|
543
|
+
const fiber = hit.fiber;
|
|
544
|
+
let instance = instances.get(fiber);
|
|
545
|
+
if (!instance && fiber.alternate) {
|
|
546
|
+
instance = instances.get(fiber.alternate);
|
|
547
|
+
if (instance) instances.set(fiber, instance);
|
|
548
|
+
}
|
|
549
|
+
if (instance?.group.live) {
|
|
550
|
+
instance.fiber = fiber;
|
|
551
|
+
const { value } = hit;
|
|
552
|
+
if (instance.value === value || (instance.value !== instance.value && value !== value)) return;
|
|
553
|
+
instance.value = value;
|
|
554
|
+
instance.group.latest = value;
|
|
555
|
+
dirty.add(instance.group);
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (hit.context !== null && typeof hit.context === 'object') add(fiber, hit.context, hit.value);
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const step = (root: FiberRoot, state: RootState) => {
|
|
562
|
+
const result = walkProviders(root, seen, { budget: CHUNK_MS, limit: CHUNK_PROVIDERS, from: state.cursor });
|
|
563
|
+
for (const hit of result.providers) handle(hit);
|
|
564
|
+
state.cursor = result.next;
|
|
565
|
+
if (!state.cursor) discovering--;
|
|
566
|
+
settle();
|
|
567
|
+
if (state.cursor) {
|
|
568
|
+
later(() => step(root, state), 0);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
// Commits during the walk can unmount parts of the tree it had yet to reach.
|
|
572
|
+
if (state.commits > 0) sweepSoon();
|
|
573
|
+
};
|
|
574
|
+
const discover = (root: FiberRoot) => {
|
|
575
|
+
const current = root?.current;
|
|
576
|
+
// `_debugOwner` exists only in development builds.
|
|
577
|
+
const ignored = current == null || current.tag !== HOST_ROOT || !('_debugOwner' in current);
|
|
578
|
+
const state: RootState = { cursor: null, commits: 0, ignored };
|
|
579
|
+
roots.set(root, state);
|
|
580
|
+
if (ignored) return;
|
|
581
|
+
discovering++;
|
|
582
|
+
step(root, state);
|
|
583
|
+
};
|
|
584
|
+
const onCommit = (root: FiberRoot) => {
|
|
585
|
+
if (stopped || root === null || typeof root !== 'object') return;
|
|
586
|
+
try {
|
|
587
|
+
const state = roots.get(root);
|
|
588
|
+
if (!state) return discover(root);
|
|
589
|
+
if (state.ignored) return;
|
|
590
|
+
if (state.cursor) state.commits++;
|
|
591
|
+
const result = walkProviders(root, seen, { changed: true });
|
|
592
|
+
for (const hit of result.providers) handle(hit);
|
|
593
|
+
settle();
|
|
594
|
+
if (result.removed) sweepSoon();
|
|
595
|
+
} catch (error) {
|
|
596
|
+
fail(error);
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
const halt = () => {
|
|
601
|
+
stopped = true;
|
|
602
|
+
installed.undo();
|
|
603
|
+
if (sweepTimer !== null) clearTimeout(sweepTimer);
|
|
604
|
+
if (announceTimer !== null) clearTimeout(announceTimer);
|
|
605
|
+
};
|
|
606
|
+
// React internals changed shape, or a bug: stop for good rather than fail on every commit. The stores stay, with a note.
|
|
607
|
+
const fail = (error: unknown) => {
|
|
608
|
+
if (stopped) return;
|
|
609
|
+
const note = `Stopped watching React Context: ${error instanceof Error ? error.message : String(error)}`;
|
|
610
|
+
for (const group of all()) announce(group, { note });
|
|
611
|
+
ctx.console.warn(`[capybara] ${note}`);
|
|
612
|
+
halt();
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
const installed = installReactHook(onCommit);
|
|
616
|
+
// A hook from React DevTools (always there in React Native) knows the trees mounted already.
|
|
617
|
+
const hook = installed.hook;
|
|
618
|
+
if (hook && hook.renderers instanceof Map && hook.renderers.size > 0 && typeof hook.getFiberRoots === 'function') {
|
|
619
|
+
later(() => {
|
|
620
|
+
for (const id of [...(hook.renderers as Map<unknown, unknown>).keys()]) {
|
|
621
|
+
for (const root of [...hook.getFiberRoots!(id)]) {
|
|
622
|
+
if (root !== null && typeof root === 'object' && !roots.has(root)) discover(root as FiberRoot);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}, 0);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
return {
|
|
629
|
+
info(store) {
|
|
630
|
+
const group = byId.get(store.id);
|
|
631
|
+
return group?.live ? { ...group.driver.info, id: store.id } : store;
|
|
632
|
+
},
|
|
633
|
+
stop() {
|
|
634
|
+
if (!stopped) halt();
|
|
635
|
+
for (const group of all()) dispose(group);
|
|
636
|
+
},
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** Whether the fiber, or its other copy, still hangs from a root: React cuts removed subtrees loose. */
|
|
641
|
+
function mounted(fiber: Fiber): boolean {
|
|
642
|
+
const reaches = (start: Fiber | null) => {
|
|
643
|
+
let node = start;
|
|
644
|
+
for (let hops = 0; node != null && hops < 100_000; hops++) {
|
|
645
|
+
if (node.tag === HOST_ROOT) return true;
|
|
646
|
+
node = node.return;
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
};
|
|
650
|
+
return reaches(fiber) || reaches(fiber.alternate);
|
|
651
|
+
}
|
|
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
|
+
NavigatorContext: 'Expo Router',
|
|
725
|
+
RouteNode: 'Expo Router',
|
|
726
|
+
}));
|
|
727
|
+
|
|
728
|
+
/** Library components that render providers, by the names their code gives them. Names an app might use are left out. */
|
|
729
|
+
const LIBRARY_COMPONENTS = new Map(Object.entries({
|
|
730
|
+
QueryClientProvider: 'TanStack Query',
|
|
731
|
+
QueryErrorResetBoundary: 'TanStack Query',
|
|
732
|
+
PersistQueryClientProvider: 'TanStack Query',
|
|
733
|
+
HydrationBoundary: 'TanStack Query',
|
|
734
|
+
SWRConfig: 'SWR',
|
|
735
|
+
BaseNavigationContainer: 'React Navigation',
|
|
736
|
+
NavigationContainerInner: 'React Navigation',
|
|
737
|
+
NavigationIndependentTree: 'React Navigation',
|
|
738
|
+
NavigationProvider: 'React Navigation',
|
|
739
|
+
NavigationStateListenerProvider: 'React Navigation',
|
|
740
|
+
PreventRemoveProvider: 'React Navigation',
|
|
741
|
+
EnsureSingleNavigator: 'React Navigation',
|
|
742
|
+
SceneView: 'React Navigation',
|
|
743
|
+
ServerContainer: 'React Navigation',
|
|
744
|
+
FrameSizeProvider: 'React Navigation',
|
|
745
|
+
AnimatedHeaderHeightProvider: 'React Navigation',
|
|
746
|
+
BottomTabView: 'React Navigation',
|
|
747
|
+
CardContainerInner: 'React Navigation',
|
|
748
|
+
DrawerViewBase: 'React Navigation',
|
|
749
|
+
NativeStackView: 'React Navigation',
|
|
750
|
+
MaterialTopTabView: 'React Navigation',
|
|
751
|
+
ExpoRoot: 'Expo Router',
|
|
752
|
+
ContextNavigator: 'Expo Router',
|
|
753
|
+
InnerRoot: 'Expo Router',
|
|
754
|
+
RootModalProvider: 'Expo Router',
|
|
755
|
+
LinkPreviewContextProvider: 'Expo Router',
|
|
756
|
+
NativeTabsNavigator: 'Expo Router',
|
|
757
|
+
SlotNavigator: 'Expo Router',
|
|
758
|
+
SplitViewNavigator: 'Expo Router',
|
|
759
|
+
ExperimentalStackNavigator: 'Expo Router',
|
|
760
|
+
ZoomTransitionSourceContextProvider: 'Expo Router',
|
|
761
|
+
ZoomTransitionTargetContextProvider: 'Expo Router',
|
|
762
|
+
SafeAreaProvider: 'react-native-safe-area-context',
|
|
763
|
+
ScreenStack: 'react-native-screens',
|
|
764
|
+
ScreenStackItem: 'react-native-screens',
|
|
765
|
+
InnerScreen: 'react-native-screens',
|
|
766
|
+
GestureDetectorProvider: 'react-native-screens',
|
|
767
|
+
ReanimatedScreenProvider: 'react-native-screens',
|
|
768
|
+
ReanimatedNativeStackScreen: 'react-native-screens',
|
|
769
|
+
GestureHandlerRootView: 'react-native-gesture-handler',
|
|
770
|
+
InterceptingGestureDetector: 'react-native-gesture-handler',
|
|
771
|
+
ScrollViewResponderProvider: 'react-native-gesture-handler',
|
|
772
|
+
SkipEntering: 'react-native-reanimated',
|
|
773
|
+
AppContainer: 'React Native',
|
|
774
|
+
}));
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* The library a context comes from, or undefined for the app's own. Runs once per store. Only the owner counts, not
|
|
778
|
+
* a component above: an app's provider passed to `root.render()` inside a library's has no owner.
|
|
779
|
+
*/
|
|
780
|
+
function vendorOf(fiber: Fiber, context: object, value: unknown): string | undefined {
|
|
781
|
+
try {
|
|
782
|
+
const redux = (globalThis as Record<symbol, unknown>)[Symbol.for('react-redux-context')];
|
|
783
|
+
if (redux instanceof Map) for (const known of redux.values()) if (known === context) return 'react-redux';
|
|
784
|
+
if (value !== null && typeof value === 'object') {
|
|
785
|
+
const v = value as Record<string, unknown>;
|
|
786
|
+
if (typeof v.getQueryCache === 'function' && typeof v.getMutationCache === 'function') return 'TanStack Query';
|
|
787
|
+
const cache = v.cache as Record<string, unknown> | null | undefined;
|
|
788
|
+
if (typeof v.mutate === 'function' && cache != null && typeof cache.get === 'function' && typeof cache.set === 'function') {
|
|
789
|
+
return 'SWR';
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
// Bundlers number names they had to rename, e.g. `SWRConfig$1`.
|
|
793
|
+
const plain = (text: string | undefined) => text?.replace(/(?:\$|_)\d+$/, '');
|
|
794
|
+
const byName =
|
|
795
|
+
LIBRARY_CONTEXTS.get(plain(contextName(context)) ?? '') ?? LIBRARY_COMPONENTS.get(plain(ownerName(fiber, true)) ?? '');
|
|
796
|
+
return byName ?? sourcePackage(fiber);
|
|
797
|
+
} catch {
|
|
798
|
+
return undefined;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* The package whose code created the provider element, from where React says it was created. Web only: React
|
|
804
|
+
* Native's stacks name the bundle rather than files.
|
|
805
|
+
*/
|
|
806
|
+
function sourcePackage(fiber: Fiber): string | undefined {
|
|
807
|
+
let path: string | undefined;
|
|
808
|
+
const source = fiber._debugSource as { fileName?: unknown } | null | undefined;
|
|
809
|
+
if (typeof source?.fileName === 'string') {
|
|
810
|
+
path = source.fileName;
|
|
811
|
+
} else {
|
|
812
|
+
// React Native's frames name the bundle, never a file: not worth formatting the stack for.
|
|
813
|
+
if (typeof navigator === 'object' && navigator?.product === 'ReactNative') return undefined;
|
|
814
|
+
const stack = (fiber._debugStack as { stack?: unknown } | null | undefined)?.stack;
|
|
815
|
+
if (typeof stack !== 'string' || stack.includes('UnknownOwner')) return undefined;
|
|
816
|
+
// The first frame is React's jsxDEV or createElement; the next is the code that created the element.
|
|
817
|
+
const frame = stack.split('\n').filter((line) => /:\d+:\d+\)?\s*$/.test(line))[1];
|
|
818
|
+
path = frame && /((?:[a-z][\w+.-]*:\/\/|\/)[^\s()]+?)(?::\d+){1,2}\)?\s*$/i.exec(frame)?.[1]?.replace(/[?#].*$/, '');
|
|
819
|
+
}
|
|
820
|
+
if (path === undefined) return undefined;
|
|
821
|
+
// Turbopack names chunks after the path they hold, with underscores for slashes.
|
|
822
|
+
if (/\/node_modules_[^/]*$/.test(path)) return 'a dependency';
|
|
823
|
+
const at = path.lastIndexOf('node_modules/');
|
|
824
|
+
if (at < 0) return undefined;
|
|
825
|
+
const rest = path.slice(at + 'node_modules/'.length);
|
|
826
|
+
// Vite serves dependencies as `.vite/deps/<name>.js`, with `@scope_name` for scoped ones.
|
|
827
|
+
const vite = /^\.vite\/deps\/([^/]+?)\.js$/.exec(rest)?.[1];
|
|
828
|
+
if (vite !== undefined) return vite.startsWith('chunk-') ? 'a dependency' : vite.replace(/^(@[^_]+)_/, '$1/');
|
|
829
|
+
const parts = rest.split('/');
|
|
830
|
+
return (parts[0]?.startsWith('@') ? `${parts[0]}/${parts[1]}` : parts[0]) || 'a dependency';
|
|
831
|
+
}
|