@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,609 @@
|
|
|
1
|
+
import type { StateChangePayload, StateStoreInfo } from '@capybara-devtools/protocol';
|
|
2
|
+
import type { StateSink, StoreDriver } from './index.js';
|
|
3
|
+
import { register, unregister } from './registry.js';
|
|
4
|
+
|
|
5
|
+
/** What Capybara reads of a Redux store. Redux 4 and 5 stores and Redux Toolkit's fit. */
|
|
6
|
+
export interface ReduxStoreLike {
|
|
7
|
+
getState(): unknown;
|
|
8
|
+
dispatch(action: any, ...rest: any[]): any;
|
|
9
|
+
subscribe(listener: () => void): () => void;
|
|
10
|
+
replaceReducer?(nextReducer: any): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ReduxStoreOptions {
|
|
14
|
+
/** Shown in the State tab. Defaults to Redux Toolkit's `devTools.name`, then "Redux store #n". */
|
|
15
|
+
name?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A Redux store enhancer, typed loosely so the SDK needs no Redux types. */
|
|
19
|
+
export type ReduxStoreEnhancer = <StoreCreator>(next: StoreCreator) => StoreCreator;
|
|
20
|
+
|
|
21
|
+
type AnyFunction = (...args: any[]) => any;
|
|
22
|
+
type Via = StateStoreInfo['via'];
|
|
23
|
+
|
|
24
|
+
/** What `connect()` returns. zustand's devtools() and other libraries report their state through it. */
|
|
25
|
+
interface Connection {
|
|
26
|
+
init(state: unknown, liftedData?: unknown): unknown;
|
|
27
|
+
send(action: unknown, state: unknown): unknown;
|
|
28
|
+
subscribe(listener: unknown): () => void;
|
|
29
|
+
unsubscribe(): unknown;
|
|
30
|
+
error(message: unknown): unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The Redux DevTools extension's global, as far as the shim passes calls on to it. */
|
|
34
|
+
interface Extension {
|
|
35
|
+
(config?: unknown): (next: AnyFunction) => AnyFunction;
|
|
36
|
+
connect?(options: object): Partial<Connection> | null | undefined;
|
|
37
|
+
send?(action: unknown, state: unknown, options?: unknown, instanceId?: unknown): unknown;
|
|
38
|
+
open?(...args: unknown[]): unknown;
|
|
39
|
+
notifyErrors?(...args: unknown[]): unknown;
|
|
40
|
+
listen?(...args: unknown[]): unknown;
|
|
41
|
+
disconnect?(...args: unknown[]): unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The sanitizers and filters an app gave the extension, applied the same way before anything leaves the app. */
|
|
45
|
+
interface Filters {
|
|
46
|
+
state(raw: unknown): unknown;
|
|
47
|
+
action(action: unknown): unknown;
|
|
48
|
+
/** A change to keep off the timeline; it shows with the next one. */
|
|
49
|
+
hidden(action: unknown, state: unknown): boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface Watch {
|
|
53
|
+
driver: StoreDriver;
|
|
54
|
+
/** The store's `getState`, which stands for the store in the registry and in `watched`. */
|
|
55
|
+
key: object;
|
|
56
|
+
id: string | undefined;
|
|
57
|
+
sink: StateSink | null;
|
|
58
|
+
/** Actions being dispatched, innermost last. A change while it is empty did not come through dispatch. */
|
|
59
|
+
stack: unknown[];
|
|
60
|
+
filters: Filters | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const EXTENSION = '__REDUX_DEVTOOLS_EXTENSION__';
|
|
64
|
+
const COMPOSE = '__REDUX_DEVTOOLS_EXTENSION_COMPOSE__';
|
|
65
|
+
/** Marks the shim's globals. From `Symbol.for`, so a second copy of the SDK sees the first one's shim. */
|
|
66
|
+
const SHIM = Symbol.for('capybara.reduxShim');
|
|
67
|
+
/** Holds the name given to an enhancer from capybaraReduxEnhancer(). */
|
|
68
|
+
const EXPLICIT = Symbol.for('capybara.reduxEnhancer');
|
|
69
|
+
/** Stores made from the extension's own `send`, one per name. */
|
|
70
|
+
const MAX_SENT = 20;
|
|
71
|
+
/** On the stack while `replaceReducer` runs: its `@@redux/REPLACE` goes around the wrapped dispatch. */
|
|
72
|
+
const REPLACING = {};
|
|
73
|
+
const LABELS = { redux: 'Redux store', zustand: 'zustand' } as const;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Stores being watched, by `getState`. Redux Toolkit and the enhancers further out pass that function on as it
|
|
77
|
+
* is, so it is the same key from inside the enhancer chain and for the store the app holds.
|
|
78
|
+
*/
|
|
79
|
+
const watched = new WeakMap<object, Watch>();
|
|
80
|
+
const labelNumbers = new Map<string, number>();
|
|
81
|
+
const labelCounts = new Map<string, number>();
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Stands in for the Redux DevTools extension, or wraps it when installed, so stores created from now on show
|
|
85
|
+
* in the State tab: Redux Toolkit's configureStore(), `createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__())`
|
|
86
|
+
* and zustand's devtools(). Returns the undo, which puts back each global only while it is still the shim's.
|
|
87
|
+
*/
|
|
88
|
+
export function installReduxShim(): () => void {
|
|
89
|
+
if (typeof window === 'undefined') return () => {};
|
|
90
|
+
const scope = window as unknown as Record<string, unknown>;
|
|
91
|
+
try {
|
|
92
|
+
const real = scope[EXTENSION];
|
|
93
|
+
const realCompose = scope[COMPOSE];
|
|
94
|
+
// Installed already: hot reload, init() again, or a second copy of the SDK.
|
|
95
|
+
if (isShim(real) || isShim(realCompose)) return () => {};
|
|
96
|
+
const shim = createShim(
|
|
97
|
+
typeof real === 'function' ? (real as Extension) : undefined,
|
|
98
|
+
typeof realCompose === 'function' ? (realCompose as AnyFunction) : undefined,
|
|
99
|
+
);
|
|
100
|
+
const undo: Array<() => void> = [];
|
|
101
|
+
const restore = () => {
|
|
102
|
+
for (const step of undo) step();
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
undo.push(put(scope, EXTENSION, shim.extension));
|
|
106
|
+
undo.push(put(scope, COMPOSE, shim.compose));
|
|
107
|
+
} catch {
|
|
108
|
+
restore();
|
|
109
|
+
return () => {};
|
|
110
|
+
}
|
|
111
|
+
return restore;
|
|
112
|
+
} catch {
|
|
113
|
+
return () => {};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Whether the shim is the extension now, so devtools() stores created meanwhile report through it. */
|
|
118
|
+
export function reduxShimActive(): boolean {
|
|
119
|
+
try {
|
|
120
|
+
return typeof window !== 'undefined' && isShim((window as unknown as Record<string, unknown>)[EXTENSION]);
|
|
121
|
+
} catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A store enhancer that shows the store in the State tab, for stores the shim cannot see: created before
|
|
128
|
+
* `init()`, by a Redux Toolkit that loaded before it, or with `devTools: false`. A store the shim sees too is
|
|
129
|
+
* shown once, under this enhancer's name when both are in Redux Toolkit's `enhancers`.
|
|
130
|
+
*/
|
|
131
|
+
export function capybaraReduxEnhancer(options: ReduxStoreOptions = {}): ReduxStoreEnhancer {
|
|
132
|
+
const name = nameOf(options);
|
|
133
|
+
const enhancer = watching(name, 'enhancer');
|
|
134
|
+
Object.defineProperty(enhancer, EXPLICIT, { value: { name } });
|
|
135
|
+
return enhancer as unknown as ReduxStoreEnhancer;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Shows a store that exists already. Best effort: the actions middleware dispatches, e.g. from thunks, show as
|
|
140
|
+
* "unknown action"; capybaraReduxEnhancer() names them. Returns a function that undoes this call.
|
|
141
|
+
*/
|
|
142
|
+
export function connectReduxStore(store: ReduxStoreLike, options: ReduxStoreOptions = {}): () => void {
|
|
143
|
+
if (typeof window === 'undefined' || !isStore(store)) return () => {};
|
|
144
|
+
try {
|
|
145
|
+
const known = watched.get(store.getState);
|
|
146
|
+
if (known) {
|
|
147
|
+
// The registry may have dropped it since, e.g. for a newer instance under the same label.
|
|
148
|
+
enroll(known);
|
|
149
|
+
return () => {};
|
|
150
|
+
}
|
|
151
|
+
const watch = createWatch(store, nameOf(options) ?? defaultLabel('redux', store.getState()), 'connect');
|
|
152
|
+
const unsubscribe = store.subscribe(onChange(watch, store));
|
|
153
|
+
const undo = [
|
|
154
|
+
patch(store, 'dispatch', (original) => dispatching(watch, store, original)),
|
|
155
|
+
patch(store, 'replaceReducer', (original) => dispatching(watch, store, original, REPLACING)),
|
|
156
|
+
];
|
|
157
|
+
watched.set(watch.key, watch);
|
|
158
|
+
enroll(watch);
|
|
159
|
+
let undone = false;
|
|
160
|
+
return () => {
|
|
161
|
+
if (undone) return;
|
|
162
|
+
undone = true;
|
|
163
|
+
watched.delete(watch.key);
|
|
164
|
+
try {
|
|
165
|
+
if (watch.id !== undefined) unregister(watch.id);
|
|
166
|
+
unsubscribe();
|
|
167
|
+
for (const step of undo) step();
|
|
168
|
+
} catch {
|
|
169
|
+
// the store may be torn down already
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
} catch {
|
|
173
|
+
return () => {};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* "Redux store #2". Numbered by the keys of the state rather than in creation order, so a store that a hot
|
|
179
|
+
* reload creates again keeps its label, which is how the UI recognizes it.
|
|
180
|
+
*/
|
|
181
|
+
export function defaultLabel(library: keyof typeof LABELS, state: unknown): string {
|
|
182
|
+
const prefix = LABELS[library];
|
|
183
|
+
let shape: string = typeof state;
|
|
184
|
+
try {
|
|
185
|
+
if (Array.isArray(state)) shape = 'array';
|
|
186
|
+
else if (state !== null && typeof state === 'object') {
|
|
187
|
+
const keys = Object.keys(state);
|
|
188
|
+
shape = keys.length > 50 ? 'object' : `{${keys.sort().join(',')}}`;
|
|
189
|
+
}
|
|
190
|
+
} catch {
|
|
191
|
+
// a proxy that will not list its keys
|
|
192
|
+
}
|
|
193
|
+
const key = `${prefix}\n${shape}`;
|
|
194
|
+
let n = labelNumbers.get(key);
|
|
195
|
+
if (n === undefined) {
|
|
196
|
+
n = (labelCounts.get(prefix) ?? 0) + 1;
|
|
197
|
+
labelCounts.set(prefix, n);
|
|
198
|
+
if (labelNumbers.size < 1_000) labelNumbers.set(key, n);
|
|
199
|
+
}
|
|
200
|
+
return `${prefix} #${n}`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function createShim(real: Extension | undefined, realCompose: AnyFunction | undefined) {
|
|
204
|
+
const sent = new Map<string, Connection>();
|
|
205
|
+
// Right above the extension's own enhancer, whose store keeps lifted actions and states.
|
|
206
|
+
const enhancerFor = (config: unknown, name: string | undefined) => {
|
|
207
|
+
const theirs = real?.(config);
|
|
208
|
+
const ours = watching(name, 'shim', config);
|
|
209
|
+
return (next: AnyFunction) => ours(theirs ? theirs(next) : next);
|
|
210
|
+
};
|
|
211
|
+
// Redux Toolkit passes `trace: true` in development; the shim never builds those stacks.
|
|
212
|
+
const extension: Extension = Object.assign((config?: unknown) => enhancerFor(config, nameOf(config)), {
|
|
213
|
+
connect(options: object) {
|
|
214
|
+
// The extension writes an instanceId and a default name into the options it is given.
|
|
215
|
+
const theirs = real?.connect?.(copy(options));
|
|
216
|
+
const ours = connection(copy(options), 'zustand');
|
|
217
|
+
return theirs !== null && typeof theirs === 'object' ? tee(theirs, ours) : ours;
|
|
218
|
+
},
|
|
219
|
+
send(action: unknown, state: unknown, options?: unknown, instanceId?: unknown) {
|
|
220
|
+
try {
|
|
221
|
+
const name = nameOf(options);
|
|
222
|
+
const key = name ?? (instanceId === undefined || instanceId === null ? '' : `#${String(instanceId)}`);
|
|
223
|
+
let target = sent.get(key);
|
|
224
|
+
if (!target) {
|
|
225
|
+
if (sent.size >= MAX_SENT) sent.delete(sent.keys().next().value!);
|
|
226
|
+
target = connection(copy(options), 'redux');
|
|
227
|
+
sent.set(key, target);
|
|
228
|
+
}
|
|
229
|
+
target.send(action, state);
|
|
230
|
+
} catch {
|
|
231
|
+
// never throw into the app
|
|
232
|
+
}
|
|
233
|
+
return real?.send?.(action, state, options, instanceId);
|
|
234
|
+
},
|
|
235
|
+
open: (...args: unknown[]) => real?.open?.(...args),
|
|
236
|
+
notifyErrors: (...args: unknown[]) => real?.notifyErrors?.(...args),
|
|
237
|
+
listen: (...args: unknown[]) => real?.listen?.(...args),
|
|
238
|
+
disconnect: (...args: unknown[]) => real?.disconnect?.(...args),
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const withCapybara = (config: unknown, funcs: unknown[]): unknown => {
|
|
242
|
+
let name = nameOf(config);
|
|
243
|
+
// A capybaraReduxEnhancer() in the list would watch the same store from further out; the shim's takes its name.
|
|
244
|
+
const rest = funcs.filter((fn) => {
|
|
245
|
+
const explicit = typeof fn === 'function' ? (fn as unknown as Record<symbol, { name?: string }>)[EXPLICIT] : undefined;
|
|
246
|
+
if (!explicit) return true;
|
|
247
|
+
name = explicit.name ?? name;
|
|
248
|
+
return false;
|
|
249
|
+
});
|
|
250
|
+
// Last in the list, so applied first: under applyMiddleware, it sees the actions thunks dispatch.
|
|
251
|
+
if (realCompose) return (realCompose(config) as AnyFunction)(...rest, watching(name, 'shim', config));
|
|
252
|
+
return [...rest, enhancerFor(config, name)].reduce(
|
|
253
|
+
(outer, inner) =>
|
|
254
|
+
(...args: unknown[]) =>
|
|
255
|
+
(outer as AnyFunction)((inner as AnyFunction)(...args)),
|
|
256
|
+
);
|
|
257
|
+
};
|
|
258
|
+
const compose = (...args: unknown[]): unknown => {
|
|
259
|
+
if (args.length === 0) return extension();
|
|
260
|
+
if (args.length === 1 && typeof args[0] === 'object') {
|
|
261
|
+
const config = args[0];
|
|
262
|
+
return (...funcs: unknown[]) => withCapybara(config, funcs);
|
|
263
|
+
}
|
|
264
|
+
return withCapybara({}, args);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
for (const fn of [extension, compose]) Object.defineProperty(fn, SHIM, { value: true });
|
|
268
|
+
return { extension, compose };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Reports go to both, ours first: it never throws, so the extension's errors reach the app as before. Messages
|
|
273
|
+
* such as time travel come only from the extension.
|
|
274
|
+
*/
|
|
275
|
+
function tee(theirs: Partial<Connection>, ours: Connection): Connection {
|
|
276
|
+
return {
|
|
277
|
+
init(...args: [unknown, unknown?]) {
|
|
278
|
+
ours.init(args[0]);
|
|
279
|
+
return theirs.init?.(...args);
|
|
280
|
+
},
|
|
281
|
+
send(action, state) {
|
|
282
|
+
ours.send(action, state);
|
|
283
|
+
return theirs.send?.(action, state);
|
|
284
|
+
},
|
|
285
|
+
subscribe(listener) {
|
|
286
|
+
const off = theirs.subscribe?.(listener);
|
|
287
|
+
return () => {
|
|
288
|
+
if (typeof off === 'function') off();
|
|
289
|
+
};
|
|
290
|
+
},
|
|
291
|
+
unsubscribe() {
|
|
292
|
+
return theirs.unsubscribe?.();
|
|
293
|
+
},
|
|
294
|
+
error(message) {
|
|
295
|
+
ours.error(message);
|
|
296
|
+
return theirs.error?.(message);
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A store per `connect()`. The library sends its whole state with each action, so the driver keeps only the
|
|
303
|
+
* last one. `send` may come before `init`, e.g. from persist hydrating synchronously.
|
|
304
|
+
*/
|
|
305
|
+
function connection(options: Record<string, unknown>, library: 'redux' | 'zustand'): Connection {
|
|
306
|
+
const name = nameOf(options);
|
|
307
|
+
const filters = filtersOf(options);
|
|
308
|
+
let state: unknown;
|
|
309
|
+
let id: string | undefined;
|
|
310
|
+
let sink: StateSink | null = null;
|
|
311
|
+
let registered = false;
|
|
312
|
+
const driver: StoreDriver = {
|
|
313
|
+
info: { library, label: name ?? '', via: 'shim', actions: true },
|
|
314
|
+
getState: () => (filters ? filters.state(state) : state),
|
|
315
|
+
start(next, storeId) {
|
|
316
|
+
sink = next;
|
|
317
|
+
id = storeId;
|
|
318
|
+
return () => {
|
|
319
|
+
if (sink === next) sink = null;
|
|
320
|
+
};
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
const report = (next: unknown, action: unknown, cause?: StateChangePayload['cause']) => {
|
|
324
|
+
if (!registered) {
|
|
325
|
+
registered = true;
|
|
326
|
+
// `init` is the state to start from; a `send` before it changed a state never seen.
|
|
327
|
+
if (cause === 'init') state = next;
|
|
328
|
+
if (name === undefined) driver.info.label = defaultLabel(library, next);
|
|
329
|
+
id = register(driver, self) ?? id;
|
|
330
|
+
}
|
|
331
|
+
state = next;
|
|
332
|
+
if (sink === null || id === undefined || (action !== undefined && filters?.hidden(action, next))) return;
|
|
333
|
+
sink.record(id, driver.getState!(), action !== undefined && filters ? filters.action(action) : action, cause);
|
|
334
|
+
};
|
|
335
|
+
// v1 never messages the app, so `subscribe` keeps no listener; zustand calls it without checking it exists.
|
|
336
|
+
const self: Connection = {
|
|
337
|
+
init(next) {
|
|
338
|
+
try {
|
|
339
|
+
report(next, undefined, 'init');
|
|
340
|
+
} catch {
|
|
341
|
+
// never throw into the app's store
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
send(action, next) {
|
|
345
|
+
try {
|
|
346
|
+
if (!action) {
|
|
347
|
+
// Without an action, `next` is the extension's lifted state: zustand sends one after an import.
|
|
348
|
+
const computed = (next as { computedStates?: unknown } | null)?.computedStates;
|
|
349
|
+
const last: unknown = Array.isArray(computed) ? computed[computed.length - 1] : undefined;
|
|
350
|
+
if (last !== null && typeof last === 'object' && 'state' in last) report(last.state, undefined, 'external');
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
report(next, amendAction(action));
|
|
354
|
+
} catch {
|
|
355
|
+
// never throw into the app's store
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
subscribe: () => () => {},
|
|
359
|
+
unsubscribe: () => {},
|
|
360
|
+
error(message) {
|
|
361
|
+
try {
|
|
362
|
+
if (registered) report(state, { type: '@@capybara/error', message });
|
|
363
|
+
} catch {
|
|
364
|
+
// never throw into the app's store
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
};
|
|
368
|
+
return self;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** As the extension reads an action: a string is its type, a missing type is "update", PERFORM_ACTION unwraps. */
|
|
372
|
+
function amendAction(action: unknown): unknown {
|
|
373
|
+
if (typeof action === 'string') return { type: action };
|
|
374
|
+
if (action === null || typeof action !== 'object') return { type: 'update' };
|
|
375
|
+
const { type, action: inner } = action as { type?: unknown; action?: unknown };
|
|
376
|
+
if (!type) return { ...action, type: 'update' };
|
|
377
|
+
if (type === 'PERFORM_ACTION' && inner !== null && typeof inner === 'object') return inner;
|
|
378
|
+
return action;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** `config` holds the extension's options, whose sanitizers and filters apply to each store the enhancer makes. */
|
|
382
|
+
function watching(name: string | undefined, via: Via, config?: unknown) {
|
|
383
|
+
return (next: AnyFunction) =>
|
|
384
|
+
(...args: unknown[]) =>
|
|
385
|
+
watchCreated(next(...args), name, via, config);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Watches a store as an enhancer creates it, and hands on a copy whose dispatch keeps the action stack. */
|
|
389
|
+
function watchCreated(store: unknown, name: string | undefined, via: Via, config?: unknown): unknown {
|
|
390
|
+
if (typeof window === 'undefined' || !isStore(store)) return store;
|
|
391
|
+
try {
|
|
392
|
+
// An enhancer further in watches it already, from where it sees more.
|
|
393
|
+
if (watched.has(store.getState)) return store;
|
|
394
|
+
const watch = createWatch(store, name ?? defaultLabel('redux', store.getState()), via, filtersOf(config));
|
|
395
|
+
// Subscribed before anything outside can be, so each change is recorded before another listener dispatches.
|
|
396
|
+
store.subscribe(onChange(watch, store));
|
|
397
|
+
const created: ReduxStoreLike = { ...store, dispatch: dispatching(watch, store, store.dispatch) };
|
|
398
|
+
if (typeof store.replaceReducer === 'function') {
|
|
399
|
+
created.replaceReducer = dispatching(watch, store, store.replaceReducer, REPLACING);
|
|
400
|
+
}
|
|
401
|
+
watched.set(watch.key, watch);
|
|
402
|
+
const id = enroll(watch);
|
|
403
|
+
if (id !== undefined && watch.sink !== null) watch.sink.record(id, watch.driver.getState!(), undefined, 'init');
|
|
404
|
+
return created;
|
|
405
|
+
} catch {
|
|
406
|
+
return store;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function createWatch(store: ReduxStoreLike, label: string, via: Via, filters?: Filters): Watch {
|
|
411
|
+
const watch: Watch = {
|
|
412
|
+
key: store.getState,
|
|
413
|
+
id: undefined,
|
|
414
|
+
sink: null,
|
|
415
|
+
stack: [],
|
|
416
|
+
filters,
|
|
417
|
+
driver: {
|
|
418
|
+
info: { library: 'redux', label, via, actions: true },
|
|
419
|
+
getState: () => (filters ? filters.state(store.getState()) : store.getState()),
|
|
420
|
+
start(sink, id) {
|
|
421
|
+
watch.sink = sink;
|
|
422
|
+
watch.id = id;
|
|
423
|
+
return () => {
|
|
424
|
+
if (watch.sink === sink) watch.sink = null;
|
|
425
|
+
};
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
};
|
|
429
|
+
return watch;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Registers the store, or gets its id when it is registered already. */
|
|
433
|
+
function enroll(watch: Watch): string | undefined {
|
|
434
|
+
const id = register(watch.driver, watch.key);
|
|
435
|
+
if (id !== undefined) watch.id = id;
|
|
436
|
+
return id;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function onChange(watch: Watch, store: ReduxStoreLike): () => void {
|
|
440
|
+
return () => {
|
|
441
|
+
const { sink, id, stack, filters } = watch;
|
|
442
|
+
if (sink === null || id === undefined) return;
|
|
443
|
+
try {
|
|
444
|
+
let action = stack.length > 0 ? stack[stack.length - 1] : undefined;
|
|
445
|
+
let cause: StateChangePayload['cause'] | undefined;
|
|
446
|
+
if (action === REPLACING) {
|
|
447
|
+
action = { type: '@@redux/REPLACE' };
|
|
448
|
+
cause = 'external';
|
|
449
|
+
} else if (action === undefined) {
|
|
450
|
+
// connectReduxStore's patch is outside applyMiddleware, which dispatches to the store underneath.
|
|
451
|
+
if (watch.driver.info.via === 'connect') action = { type: 'unknown action' };
|
|
452
|
+
// e.g. the extension travelling in time
|
|
453
|
+
else cause = 'external';
|
|
454
|
+
}
|
|
455
|
+
const state = store.getState();
|
|
456
|
+
if (!filters) sink.record(id, state, action, cause);
|
|
457
|
+
else if (action === undefined) sink.record(id, filters.state(state), undefined, cause);
|
|
458
|
+
else if (!filters.hidden(action, state)) sink.record(id, filters.state(state), filters.action(action), cause);
|
|
459
|
+
} catch {
|
|
460
|
+
// never throw into the app's store
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* The extension's options that keep data back: `stateSanitizer`, `actionSanitizer`, `actionsDenylist`,
|
|
467
|
+
* `actionsAllowlist` and `predicate`, read as the extension reads them. Undefined when there are none.
|
|
468
|
+
*/
|
|
469
|
+
function filtersOf(options: unknown): Filters | undefined {
|
|
470
|
+
const o = copy(options);
|
|
471
|
+
const stateSanitizer = typeof o.stateSanitizer === 'function' ? (o.stateSanitizer as AnyFunction) : undefined;
|
|
472
|
+
const actionSanitizer = typeof o.actionSanitizer === 'function' ? (o.actionSanitizer as AnyFunction) : undefined;
|
|
473
|
+
const predicate = typeof o.predicate === 'function' ? (o.predicate as AnyFunction) : undefined;
|
|
474
|
+
const deny = pattern(o.actionsDenylist ?? o.actionsBlacklist);
|
|
475
|
+
const allow = pattern(o.actionsAllowlist ?? o.actionsWhitelist);
|
|
476
|
+
if (!stateSanitizer && !actionSanitizer && !predicate && !deny && !allow) return undefined;
|
|
477
|
+
let count = 0;
|
|
478
|
+
let raw: unknown = {};
|
|
479
|
+
let clean: unknown;
|
|
480
|
+
return {
|
|
481
|
+
// Kept by reference, so a store that has not changed reads as the same state rather than a pending one.
|
|
482
|
+
state(next) {
|
|
483
|
+
if (!stateSanitizer) return next;
|
|
484
|
+
if (next !== raw) {
|
|
485
|
+
raw = next;
|
|
486
|
+
try {
|
|
487
|
+
clean = stateSanitizer(next, count);
|
|
488
|
+
} catch (error) {
|
|
489
|
+
// Never fall back to the state the sanitizer was meant to filter.
|
|
490
|
+
clean = `[stateSanitizer threw: ${error instanceof Error ? error.message : String(error)}]`;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return clean;
|
|
494
|
+
},
|
|
495
|
+
action(action) {
|
|
496
|
+
if (!actionSanitizer) return action;
|
|
497
|
+
try {
|
|
498
|
+
return actionSanitizer(action, count++);
|
|
499
|
+
} catch {
|
|
500
|
+
return { type: typeOf(action) };
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
hidden(action, state) {
|
|
504
|
+
const type = typeOf(action);
|
|
505
|
+
try {
|
|
506
|
+
return (allow !== undefined && !allow.test(type)) || (deny !== undefined && deny.test(type)) || (predicate !== undefined && !predicate(state, action));
|
|
507
|
+
} catch {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** A list of action types as the extension joins it: a string or strings, as one regular expression. */
|
|
515
|
+
function pattern(list: unknown): RegExp | undefined {
|
|
516
|
+
try {
|
|
517
|
+
if (typeof list === 'string' && list !== '') return new RegExp(list);
|
|
518
|
+
if (Array.isArray(list) && list.length > 0) return new RegExp(list.join('|'));
|
|
519
|
+
} catch {
|
|
520
|
+
// an invalid pattern filters nothing
|
|
521
|
+
}
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function typeOf(action: unknown): string {
|
|
526
|
+
if (typeof action === 'string') return action;
|
|
527
|
+
try {
|
|
528
|
+
return String((action as { type?: unknown } | null)?.type ?? '');
|
|
529
|
+
} catch {
|
|
530
|
+
return '';
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** `original`, with the action on the stack while it runs. While no client watches, it only reads one flag. */
|
|
535
|
+
function dispatching(watch: Watch, target: object, original: AnyFunction, marker?: object): AnyFunction {
|
|
536
|
+
return function (this: unknown, action: unknown) {
|
|
537
|
+
// Thunks and other non-actions stay off the stack: only the actions they dispatch change the state.
|
|
538
|
+
if (watch.sink === null || (marker === undefined && (action === null || typeof action !== 'object'))) {
|
|
539
|
+
return original.apply(target, arguments as unknown as unknown[]);
|
|
540
|
+
}
|
|
541
|
+
watch.stack.push(marker ?? action);
|
|
542
|
+
try {
|
|
543
|
+
return original.apply(target, arguments as unknown as unknown[]);
|
|
544
|
+
} finally {
|
|
545
|
+
watch.stack.pop();
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Replaces a method of the app's store. The undo leaves alone a method someone replaced since. */
|
|
551
|
+
function patch(store: ReduxStoreLike, name: 'dispatch' | 'replaceReducer', make: (original: AnyFunction) => AnyFunction): () => void {
|
|
552
|
+
const target = store as unknown as Record<string, unknown>;
|
|
553
|
+
const original = target[name];
|
|
554
|
+
if (typeof original !== 'function') return () => {};
|
|
555
|
+
const wrapper = make(original as AnyFunction);
|
|
556
|
+
try {
|
|
557
|
+
target[name] = wrapper;
|
|
558
|
+
} catch {
|
|
559
|
+
// A frozen store keeps its method, and its changes show as unknown actions.
|
|
560
|
+
return () => {};
|
|
561
|
+
}
|
|
562
|
+
return () => {
|
|
563
|
+
if (target[name] === wrapper) target[name] = original;
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** Sets a global; the undo puts back what was there, only while the global is still `value`. */
|
|
568
|
+
function put(scope: Record<string, unknown>, key: string, value: unknown): () => void {
|
|
569
|
+
const had = Object.prototype.hasOwnProperty.call(scope, key);
|
|
570
|
+
const previous = scope[key];
|
|
571
|
+
scope[key] = value;
|
|
572
|
+
return () => {
|
|
573
|
+
try {
|
|
574
|
+
if (scope[key] !== value) return;
|
|
575
|
+
if (had) scope[key] = previous;
|
|
576
|
+
else delete scope[key];
|
|
577
|
+
} catch {
|
|
578
|
+
// a global made read-only since stays as it is
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function isShim(value: unknown): boolean {
|
|
584
|
+
return typeof value === 'function' && (value as unknown as Record<symbol, unknown>)[SHIM] === true;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function isStore(value: unknown): value is ReduxStoreLike {
|
|
588
|
+
const store = value as Partial<ReduxStoreLike> | null;
|
|
589
|
+
return (
|
|
590
|
+
store !== null &&
|
|
591
|
+
(typeof store === 'object' || typeof store === 'function') &&
|
|
592
|
+
typeof store.getState === 'function' &&
|
|
593
|
+
typeof store.dispatch === 'function' &&
|
|
594
|
+
typeof store.subscribe === 'function'
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function nameOf(options: unknown): string | undefined {
|
|
599
|
+
try {
|
|
600
|
+
const name = (options as { name?: unknown } | null | undefined)?.name;
|
|
601
|
+
return typeof name === 'string' && name !== '' ? name : undefined;
|
|
602
|
+
} catch {
|
|
603
|
+
return undefined;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function copy(options: unknown): Record<string, unknown> {
|
|
608
|
+
return options !== null && typeof options === 'object' ? { ...(options as Record<string, unknown>) } : {};
|
|
609
|
+
}
|