@syncular/client 0.63.0 → 0.65.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/README.md +1 -0
- package/dist/client.js +2 -4
- package/dist/dev-profiler.d.ts +127 -0
- package/dist/dev-profiler.js +338 -0
- package/dist/devtools.d.ts +5 -0
- package/dist/devtools.js +13 -10
- package/dist/encryption.d.ts +3 -2
- package/dist/encryption.js +4 -3
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +35 -0
- package/dist/multi-tab.js +2 -15
- package/dist/worker-core.d.ts +4 -1
- package/dist/worker-core.js +3 -20
- package/package.json +11 -3
- package/src/client.ts +2 -3
- package/src/dev-profiler.ts +525 -0
- package/src/devtools.ts +17 -14
- package/src/encryption.ts +9 -3
- package/src/errors.ts +40 -0
- package/src/multi-tab.ts +2 -17
- package/src/worker-core.ts +8 -23
package/dist/worker-core.d.ts
CHANGED
|
@@ -31,7 +31,10 @@ export interface SyncWorkerOverrides {
|
|
|
31
31
|
* deadlines and the client's §8.5 heartbeat watchdog.
|
|
32
32
|
*/
|
|
33
33
|
readonly scheduleBackground?: (callback: () => void, delayMs: number) => () => void;
|
|
34
|
-
|
|
34
|
+
/** `http` reads the current host auth set, as the default transport does. */
|
|
35
|
+
readonly createTransport?: (config: WorkerInitConfig, http: {
|
|
36
|
+
readonly headers: () => Readonly<Record<string, string>>;
|
|
37
|
+
}) => SyncTransport;
|
|
35
38
|
readonly createSegments?: (config: WorkerInitConfig) => SegmentDownloader | undefined;
|
|
36
39
|
/**
|
|
37
40
|
* Custom realtime connector, e.g. one that mints a fresh ticket per
|
package/dist/worker-core.js
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
1
|
import { SyncClient } from './client.js';
|
|
2
2
|
import { withClientDiagnosticsHost, } from './diagnostics.js';
|
|
3
3
|
import { encryptionConfigFromKeyring } from './encryption.js';
|
|
4
|
-
import {
|
|
4
|
+
import { ClientSyncError, invalidRequest, STORAGE_BUSY_CODE, toErrorShape, } from './errors.js';
|
|
5
5
|
import { httpBlobTransport, httpSegmentDownloader, httpSyncTransport, webSocketRealtimeConnector, } from './http.js';
|
|
6
6
|
import { openPersistentWasmDatabase } from './wasm-database.js';
|
|
7
7
|
import { LOCAL_AUTHORITY_UNCONFIGURED_CODE, WORKER_FAILED_CODE, WORKER_METHOD_UNAVAILABLE_CODE, } from './worker-protocol.js';
|
|
8
|
-
function toErrorShape(caught) {
|
|
9
|
-
const error = classifySqliteFailure(caught).error;
|
|
10
|
-
if (error instanceof ClientSyncError) {
|
|
11
|
-
return {
|
|
12
|
-
code: error.code,
|
|
13
|
-
message: error.message,
|
|
14
|
-
retryable: error.retryable,
|
|
15
|
-
...(error.details !== undefined ? { details: error.details } : {}),
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
code: WORKER_FAILED_CODE,
|
|
20
|
-
message: error instanceof Error ? error.message : String(error),
|
|
21
|
-
retryable: false,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
8
|
/**
|
|
25
9
|
* ArrayBuffers safe to transfer out of a query result: whole-buffer
|
|
26
10
|
* views only (a view into a larger/pooled buffer is cloned instead —
|
|
@@ -236,7 +220,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
236
220
|
}
|
|
237
221
|
}
|
|
238
222
|
const remote = overrides.createTransport !== undefined
|
|
239
|
-
? overrides.createTransport(config)
|
|
223
|
+
? overrides.createTransport(config, http)
|
|
240
224
|
: httpSyncTransport(config.endpoints.syncUrl, http);
|
|
241
225
|
const remoteSegments = overrides.createSegments !== undefined
|
|
242
226
|
? overrides.createSegments(config)
|
|
@@ -501,8 +485,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
501
485
|
}
|
|
502
486
|
async function defaultOpenDatabase(config) {
|
|
503
487
|
if (config.database.mode !== 'persistent') {
|
|
504
|
-
throw new ClientSyncError(WORKER_FAILED_CODE, "database mode 'custom' requires
|
|
505
|
-
'worker bootstrap (startSyncWorker({ openDatabase }))');
|
|
488
|
+
throw new ClientSyncError(WORKER_FAILED_CODE, "database mode 'custom' requires startSyncWorker({ openDatabase })");
|
|
506
489
|
}
|
|
507
490
|
return openPersistentWasmDatabase(config.database.name, {
|
|
508
491
|
...(config.database.directory !== undefined
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.65.0",
|
|
4
4
|
"description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -97,6 +97,14 @@
|
|
|
97
97
|
"types": "./dist/native-bridge.d.ts",
|
|
98
98
|
"default": "./dist/native-bridge.js"
|
|
99
99
|
}
|
|
100
|
+
},
|
|
101
|
+
"./dev": {
|
|
102
|
+
"bun": "./src/dev-profiler.ts",
|
|
103
|
+
"browser": "./dist/dev-profiler.js",
|
|
104
|
+
"import": {
|
|
105
|
+
"types": "./dist/dev-profiler.d.ts",
|
|
106
|
+
"default": "./dist/dev-profiler.js"
|
|
107
|
+
}
|
|
100
108
|
}
|
|
101
109
|
},
|
|
102
110
|
"files": [
|
|
@@ -113,11 +121,11 @@
|
|
|
113
121
|
},
|
|
114
122
|
"dependencies": {
|
|
115
123
|
"@sqlite.org/sqlite-wasm": "^3.53.0-build1",
|
|
116
|
-
"@syncular/core": "0.
|
|
124
|
+
"@syncular/core": "0.65.0"
|
|
117
125
|
},
|
|
118
126
|
"devDependencies": {
|
|
119
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
120
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.65.0",
|
|
121
129
|
"playwright": "1.63.0",
|
|
122
130
|
"vite": "8.3.4"
|
|
123
131
|
}
|
package/src/client.ts
CHANGED
|
@@ -117,6 +117,7 @@ import {
|
|
|
117
117
|
ClientSyncError,
|
|
118
118
|
invalidRequest,
|
|
119
119
|
classifySqliteFailure,
|
|
120
|
+
isStableCode,
|
|
120
121
|
REALTIME_LOST_CODE,
|
|
121
122
|
RealtimeUnavailableError,
|
|
122
123
|
transportFailed,
|
|
@@ -4305,9 +4306,7 @@ export class SyncClient {
|
|
|
4305
4306
|
}
|
|
4306
4307
|
|
|
4307
4308
|
#diagnosticCode(code: string): string {
|
|
4308
|
-
return code
|
|
4309
|
-
? code
|
|
4310
|
-
: 'client.unknown_failure';
|
|
4309
|
+
return isStableCode(code) ? code : 'client.unknown_failure';
|
|
4311
4310
|
}
|
|
4312
4311
|
|
|
4313
4312
|
async #runSync(signal: AbortSignal | undefined): Promise<SyncSummary> {
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@syncular/client/dev`: a development profiler. It wraps the database,
|
|
3
|
+
* the sync transport, the realtime connector and the client (or a worker or
|
|
4
|
+
* native handle) an app already constructs, and reports per live query, per
|
|
5
|
+
* statement, per round, per unit and per write burst. It keeps SQL text in
|
|
6
|
+
* memory, so `createSyncProfiler` refuses a build without a development or
|
|
7
|
+
* test signal. No production entry imports this module.
|
|
8
|
+
*/
|
|
9
|
+
import type { MutationInput, QueryReadSpec, QuerySnapshot } from './client';
|
|
10
|
+
import type { ClientDatabase, SqlValue } from './database';
|
|
11
|
+
import { developmentBuild } from './devtools';
|
|
12
|
+
import type { ClientDiagnosticsListener } from './diagnostics';
|
|
13
|
+
import { ClientSyncError } from './errors';
|
|
14
|
+
import type { ClientChangeListener } from './invalidation';
|
|
15
|
+
import type { SyncProgressListener } from './progress';
|
|
16
|
+
import { canonicalValue } from './reactive-store';
|
|
17
|
+
import type { RealtimeConnector, SyncTransport } from './transport';
|
|
18
|
+
|
|
19
|
+
/** The surface `profiler.client()` wraps: a `SyncClient`, a worker handle or a native handle. */
|
|
20
|
+
export interface ProfiledClient {
|
|
21
|
+
querySnapshot(spec: QueryReadSpec): QuerySnapshot | Promise<QuerySnapshot>;
|
|
22
|
+
mutate(mutations: readonly MutationInput[]): unknown;
|
|
23
|
+
onChange(listener: ClientChangeListener): () => void;
|
|
24
|
+
onProgress(listener: SyncProgressListener): () => void;
|
|
25
|
+
onDiagnostics(listener: ClientDiagnosticsListener): () => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SyncProfilerOptions {
|
|
29
|
+
/** Milliseconds clock; tests pass a virtual one. Default `performance.now`. */
|
|
30
|
+
readonly now?: () => number;
|
|
31
|
+
/** The server's `maxSubscriptionsPerRequest`. Default 500. */
|
|
32
|
+
readonly unitLimit?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Single-operation commits that start within this many milliseconds of the
|
|
35
|
+
* previous one join its write burst. Default 16.
|
|
36
|
+
*/
|
|
37
|
+
readonly burstWindowMs?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Shared between a worker's profiler and the page's: the worker posts its
|
|
40
|
+
* statements and rounds, the page merges them into its report. Pass
|
|
41
|
+
* `new BroadcastChannel(name)` with the same name on both sides.
|
|
42
|
+
*/
|
|
43
|
+
readonly channel?: Pick<BroadcastChannel, 'postMessage' | 'onmessage'>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface QueryProfile {
|
|
47
|
+
/** The owner id of a live query, or the SQL of an unowned read. */
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly sql: string;
|
|
50
|
+
readonly tables: readonly string[];
|
|
51
|
+
readonly runs: number;
|
|
52
|
+
/** Runs whose rows equal the previous run's rows. */
|
|
53
|
+
readonly identicalRuns: number;
|
|
54
|
+
readonly totalMs: number;
|
|
55
|
+
readonly maxMs: number;
|
|
56
|
+
/** Rows of the last run. */
|
|
57
|
+
readonly rows: number;
|
|
58
|
+
/** Runs per changed table, plus `mount` (first run) and `refresh` (no table changed). */
|
|
59
|
+
readonly triggers: Readonly<Record<string, number>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface StatementProfile {
|
|
63
|
+
readonly sql: string;
|
|
64
|
+
readonly runs: number;
|
|
65
|
+
readonly totalMs: number;
|
|
66
|
+
readonly maxMs: number;
|
|
67
|
+
readonly rows: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface RoundProfile {
|
|
71
|
+
readonly via: 'http' | 'socket';
|
|
72
|
+
readonly requestBytes: number;
|
|
73
|
+
readonly responseBytes: number;
|
|
74
|
+
/** From sending the request to the last response byte. */
|
|
75
|
+
readonly ms: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface UnitProfile {
|
|
79
|
+
readonly unitId: string;
|
|
80
|
+
readonly table: string | undefined;
|
|
81
|
+
/** Segment bytes downloaded or received inline. */
|
|
82
|
+
readonly bytes: number;
|
|
83
|
+
readonly rows: number;
|
|
84
|
+
/** From the unit's first progress event to its last. */
|
|
85
|
+
readonly ms: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ProfileReport {
|
|
89
|
+
readonly queries: readonly QueryProfile[];
|
|
90
|
+
readonly statements: readonly StatementProfile[];
|
|
91
|
+
/** The last 256 rounds. */
|
|
92
|
+
readonly rounds: readonly RoundProfile[];
|
|
93
|
+
readonly deltas: { readonly count: number; readonly bytes: number };
|
|
94
|
+
readonly units: {
|
|
95
|
+
/** `activeUnits` of the last diagnostics snapshot: the units the next pull sends. */
|
|
96
|
+
readonly active: number;
|
|
97
|
+
readonly peak: number;
|
|
98
|
+
readonly limit: number;
|
|
99
|
+
readonly bootstraps: readonly UnitProfile[];
|
|
100
|
+
};
|
|
101
|
+
readonly writes: {
|
|
102
|
+
readonly mutateCalls: number;
|
|
103
|
+
readonly operations: number;
|
|
104
|
+
/** Runs of more than one single-operation commit (`burstWindowMs`). */
|
|
105
|
+
readonly bursts: readonly {
|
|
106
|
+
readonly commits: number;
|
|
107
|
+
readonly tables: readonly string[];
|
|
108
|
+
}[];
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface SyncProfiler {
|
|
113
|
+
database<D extends ClientDatabase>(database: D): D;
|
|
114
|
+
transport(transport: SyncTransport): SyncTransport;
|
|
115
|
+
realtime(connector: RealtimeConnector): RealtimeConnector;
|
|
116
|
+
client<C extends ProfiledClient>(client: C): C;
|
|
117
|
+
report(): ProfileReport;
|
|
118
|
+
reset(): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface ProfileBudgets {
|
|
122
|
+
readonly maxActiveUnits?: number;
|
|
123
|
+
/** Request plus response bytes of one round. */
|
|
124
|
+
readonly maxRoundBytes?: number;
|
|
125
|
+
readonly maxRunsPerQuery?: number;
|
|
126
|
+
/** Identical runs summed over all queries. */
|
|
127
|
+
readonly maxIdenticalRuns?: number;
|
|
128
|
+
/** Commits of the largest write burst. */
|
|
129
|
+
readonly maxWriteBurst?: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
|
|
133
|
+
type UnitState = Mutable<UnitProfile> & {
|
|
134
|
+
startedAt: number;
|
|
135
|
+
segment: string | undefined;
|
|
136
|
+
segmentBytes: number;
|
|
137
|
+
segmentRows: number;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** A proxy that replaces `overrides` and binds every other method to `target`. */
|
|
141
|
+
function wrap<T extends object>(target: T, overrides: Partial<T>): T {
|
|
142
|
+
return new Proxy(target, {
|
|
143
|
+
get(object, key) {
|
|
144
|
+
if (Object.hasOwn(overrides, key)) return overrides[key as keyof T];
|
|
145
|
+
const value: unknown = Reflect.get(object, key, object);
|
|
146
|
+
return typeof value === 'function' ? value.bind(object) : value;
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function createSyncProfiler(
|
|
152
|
+
options: SyncProfilerOptions = {},
|
|
153
|
+
): SyncProfiler {
|
|
154
|
+
if (!developmentBuild())
|
|
155
|
+
throw new ClientSyncError(
|
|
156
|
+
'client.profiler_in_production',
|
|
157
|
+
'createSyncProfiler requires a development or test build',
|
|
158
|
+
);
|
|
159
|
+
const now = options.now ?? (() => performance.now());
|
|
160
|
+
const burstWindowMs = options.burstWindowMs ?? 16;
|
|
161
|
+
const channel = options.channel;
|
|
162
|
+
const source = Math.random().toString(36).slice(2);
|
|
163
|
+
let queries = new Map<string, Mutable<QueryProfile>>();
|
|
164
|
+
// Per query: the canonical rows of its last run and the change sequence it saw.
|
|
165
|
+
let queryState = new Map<string, { rows: string; seq: number }>();
|
|
166
|
+
let statements = new Map<string, Mutable<StatementProfile>>();
|
|
167
|
+
let rounds: Mutable<RoundProfile>[] = [];
|
|
168
|
+
let deltas = { count: 0, bytes: 0 };
|
|
169
|
+
let units = { active: 0, peak: 0 };
|
|
170
|
+
let bootstraps = new Map<string, UnitState>();
|
|
171
|
+
let writes = {
|
|
172
|
+
mutateCalls: 0,
|
|
173
|
+
operations: 0,
|
|
174
|
+
bursts: [] as { commits: number; tables: string[] }[],
|
|
175
|
+
};
|
|
176
|
+
let burst: { commits: number; tables: string[]; at: number } | undefined;
|
|
177
|
+
let changeSeq = 0;
|
|
178
|
+
const changedAt = new Map<string, number>();
|
|
179
|
+
const remote = new Map<
|
|
180
|
+
string,
|
|
181
|
+
Pick<ProfileReport, 'statements' | 'rounds'>
|
|
182
|
+
>();
|
|
183
|
+
let posting = false;
|
|
184
|
+
const clear = () => {
|
|
185
|
+
queries = new Map();
|
|
186
|
+
queryState = new Map();
|
|
187
|
+
statements = new Map();
|
|
188
|
+
rounds = [];
|
|
189
|
+
deltas = { count: 0, bytes: 0 };
|
|
190
|
+
units = { active: units.active, peak: units.active };
|
|
191
|
+
bootstraps = new Map();
|
|
192
|
+
writes = { mutateCalls: 0, operations: 0, bursts: [] };
|
|
193
|
+
burst = undefined;
|
|
194
|
+
remote.clear();
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const post = () => {
|
|
198
|
+
if (channel === undefined || posting) return;
|
|
199
|
+
posting = true;
|
|
200
|
+
queueMicrotask(() => {
|
|
201
|
+
posting = false;
|
|
202
|
+
channel.postMessage({
|
|
203
|
+
syncularProfile: source,
|
|
204
|
+
statements: [...statements.values()],
|
|
205
|
+
rounds,
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
if (channel !== undefined)
|
|
210
|
+
channel.onmessage = (event: MessageEvent) => {
|
|
211
|
+
const data = event.data as {
|
|
212
|
+
syncularProfile?: unknown;
|
|
213
|
+
syncularProfileReset?: unknown;
|
|
214
|
+
} & Pick<ProfileReport, 'statements' | 'rounds'>;
|
|
215
|
+
if (data?.syncularProfileReset === true) clear();
|
|
216
|
+
else if (typeof data?.syncularProfile === 'string')
|
|
217
|
+
remote.set(data.syncularProfile, {
|
|
218
|
+
statements: data.statements,
|
|
219
|
+
rounds: data.rounds,
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const round = (entry: Mutable<RoundProfile>) => {
|
|
224
|
+
rounds.push(entry);
|
|
225
|
+
if (rounds.length > 256) rounds.shift();
|
|
226
|
+
post();
|
|
227
|
+
return entry;
|
|
228
|
+
};
|
|
229
|
+
const statement =
|
|
230
|
+
<R>(run: (sql: string, params?: readonly SqlValue[]) => R) =>
|
|
231
|
+
(sql: string, params?: readonly SqlValue[]): R => {
|
|
232
|
+
const startedAt = now();
|
|
233
|
+
const result = run(sql, params);
|
|
234
|
+
const ms = now() - startedAt;
|
|
235
|
+
const entry =
|
|
236
|
+
statements.get(sql) ??
|
|
237
|
+
statements
|
|
238
|
+
.set(sql, { sql, runs: 0, totalMs: 0, maxMs: 0, rows: 0 })
|
|
239
|
+
.get(sql)!;
|
|
240
|
+
entry.runs += 1;
|
|
241
|
+
entry.totalMs += ms;
|
|
242
|
+
entry.maxMs = Math.max(entry.maxMs, ms);
|
|
243
|
+
if (Array.isArray(result)) entry.rows += result.length;
|
|
244
|
+
post();
|
|
245
|
+
return result;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
database: (database) =>
|
|
250
|
+
wrap(database, {
|
|
251
|
+
exec: statement((sql, params) => database.exec(sql, params)),
|
|
252
|
+
query: statement((sql, params) => database.query(sql, params)),
|
|
253
|
+
queryReadOnly: statement((sql, params) =>
|
|
254
|
+
database.queryReadOnly(sql, params),
|
|
255
|
+
),
|
|
256
|
+
} as Partial<typeof database>),
|
|
257
|
+
|
|
258
|
+
transport: (transport) => async (request, signal) => {
|
|
259
|
+
const startedAt = now();
|
|
260
|
+
const response = await transport(request, signal);
|
|
261
|
+
round({
|
|
262
|
+
via: 'http',
|
|
263
|
+
requestBytes: request.length,
|
|
264
|
+
responseBytes: response.length,
|
|
265
|
+
ms: now() - startedAt,
|
|
266
|
+
});
|
|
267
|
+
return response;
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
realtime: (connector) => (handlers, clientId) => {
|
|
271
|
+
let open: (Mutable<RoundProfile> & { startedAt: number }) | undefined;
|
|
272
|
+
const socket = connector(
|
|
273
|
+
{
|
|
274
|
+
...handlers,
|
|
275
|
+
onBinary: (bytes) => {
|
|
276
|
+
// §8.7: tag 0x00 is a delta, tag 0x01 a chunk of the open round.
|
|
277
|
+
if (bytes[0] === 0) {
|
|
278
|
+
deltas.count += 1;
|
|
279
|
+
deltas.bytes += bytes.length - 1;
|
|
280
|
+
} else if (open !== undefined) {
|
|
281
|
+
open.responseBytes += bytes.length - 1;
|
|
282
|
+
open.ms = now() - open.startedAt;
|
|
283
|
+
post();
|
|
284
|
+
}
|
|
285
|
+
handlers.onBinary(bytes);
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
clientId,
|
|
289
|
+
);
|
|
290
|
+
const profiled = (opened: Awaited<typeof socket>) =>
|
|
291
|
+
wrap(opened, {
|
|
292
|
+
sendBytes: (bytes: Uint8Array) => {
|
|
293
|
+
if (bytes[0] === 1) {
|
|
294
|
+
const startedAt = now();
|
|
295
|
+
open = Object.assign(
|
|
296
|
+
round({
|
|
297
|
+
via: 'socket',
|
|
298
|
+
requestBytes: bytes.length - 1,
|
|
299
|
+
responseBytes: 0,
|
|
300
|
+
ms: 0,
|
|
301
|
+
}),
|
|
302
|
+
{ startedAt },
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
opened.sendBytes(bytes);
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
return socket instanceof Promise
|
|
309
|
+
? socket.then(profiled)
|
|
310
|
+
: profiled(socket);
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
client: (client) => {
|
|
314
|
+
client.onChange((batch) => {
|
|
315
|
+
changeSeq += 1;
|
|
316
|
+
for (const change of batch.tables)
|
|
317
|
+
changedAt.set(change.table, changeSeq);
|
|
318
|
+
});
|
|
319
|
+
client.onDiagnostics((snapshot) => {
|
|
320
|
+
units.active = snapshot.activeUnits;
|
|
321
|
+
units.peak = Math.max(units.peak, snapshot.activeUnits);
|
|
322
|
+
});
|
|
323
|
+
client.onProgress((progress) => {
|
|
324
|
+
if (progress.unitId === undefined || progress.phase === 'request')
|
|
325
|
+
return;
|
|
326
|
+
let unit = bootstraps.get(progress.unitId);
|
|
327
|
+
if (unit === undefined) {
|
|
328
|
+
unit = {
|
|
329
|
+
unitId: progress.unitId,
|
|
330
|
+
table: progress.table,
|
|
331
|
+
bytes: 0,
|
|
332
|
+
rows: 0,
|
|
333
|
+
ms: 0,
|
|
334
|
+
startedAt: now(),
|
|
335
|
+
segment: undefined,
|
|
336
|
+
segmentBytes: 0,
|
|
337
|
+
segmentRows: 0,
|
|
338
|
+
};
|
|
339
|
+
bootstraps.set(progress.unitId, unit);
|
|
340
|
+
}
|
|
341
|
+
// A progress event repeats its segment's running totals; a new
|
|
342
|
+
// segment, or totals that went down, starts the next one.
|
|
343
|
+
if (
|
|
344
|
+
progress.segmentId !== unit.segment ||
|
|
345
|
+
progress.bytesReceived < unit.segmentBytes ||
|
|
346
|
+
progress.rowsProcessed < unit.segmentRows
|
|
347
|
+
) {
|
|
348
|
+
unit.bytes += unit.segmentBytes;
|
|
349
|
+
unit.rows += unit.segmentRows;
|
|
350
|
+
}
|
|
351
|
+
unit.segment = progress.segmentId;
|
|
352
|
+
unit.segmentBytes = progress.bytesReceived;
|
|
353
|
+
unit.segmentRows = progress.rowsProcessed;
|
|
354
|
+
unit.ms = now() - unit.startedAt;
|
|
355
|
+
});
|
|
356
|
+
const record = (
|
|
357
|
+
spec: QueryReadSpec,
|
|
358
|
+
startedAt: number,
|
|
359
|
+
seq: number,
|
|
360
|
+
snapshot: QuerySnapshot,
|
|
361
|
+
) => {
|
|
362
|
+
const id = spec.owner?.id ?? spec.sql;
|
|
363
|
+
const ms = now() - startedAt;
|
|
364
|
+
const query =
|
|
365
|
+
queries.get(id) ??
|
|
366
|
+
queries
|
|
367
|
+
.set(id, {
|
|
368
|
+
id,
|
|
369
|
+
sql: spec.sql,
|
|
370
|
+
tables: spec.owner?.tables ?? [],
|
|
371
|
+
runs: 0,
|
|
372
|
+
identicalRuns: 0,
|
|
373
|
+
totalMs: 0,
|
|
374
|
+
maxMs: 0,
|
|
375
|
+
rows: 0,
|
|
376
|
+
triggers: {},
|
|
377
|
+
})
|
|
378
|
+
.get(id)!;
|
|
379
|
+
const previous = queryState.get(id);
|
|
380
|
+
const rows = canonicalValue(snapshot.rows);
|
|
381
|
+
const changed = query.tables.filter(
|
|
382
|
+
(table) => (changedAt.get(table) ?? 0) > (previous?.seq ?? 0),
|
|
383
|
+
);
|
|
384
|
+
const triggers = query.triggers as Record<string, number>;
|
|
385
|
+
for (const trigger of previous === undefined
|
|
386
|
+
? ['mount']
|
|
387
|
+
: changed.length > 0
|
|
388
|
+
? changed
|
|
389
|
+
: ['refresh'])
|
|
390
|
+
triggers[trigger] = (triggers[trigger] ?? 0) + 1;
|
|
391
|
+
if (previous?.rows === rows) query.identicalRuns += 1;
|
|
392
|
+
queryState.set(id, { rows, seq });
|
|
393
|
+
query.runs += 1;
|
|
394
|
+
query.totalMs += ms;
|
|
395
|
+
query.maxMs = Math.max(query.maxMs, ms);
|
|
396
|
+
query.rows = snapshot.rows.length;
|
|
397
|
+
return snapshot;
|
|
398
|
+
};
|
|
399
|
+
return wrap(client, {
|
|
400
|
+
querySnapshot: (spec: QueryReadSpec) => {
|
|
401
|
+
const startedAt = now();
|
|
402
|
+
const seq = changeSeq;
|
|
403
|
+
const snapshot = client.querySnapshot(spec);
|
|
404
|
+
return snapshot instanceof Promise
|
|
405
|
+
? snapshot.then((value) => record(spec, startedAt, seq, value))
|
|
406
|
+
: record(spec, startedAt, seq, snapshot);
|
|
407
|
+
},
|
|
408
|
+
mutate: (mutations: readonly MutationInput[]) => {
|
|
409
|
+
writes.mutateCalls += 1;
|
|
410
|
+
writes.operations += mutations.length;
|
|
411
|
+
const at = now();
|
|
412
|
+
if (mutations.length !== 1) burst = undefined;
|
|
413
|
+
else if (burst !== undefined && at - burst.at <= burstWindowMs) {
|
|
414
|
+
burst.at = at;
|
|
415
|
+
burst.commits += 1;
|
|
416
|
+
if (!burst.tables.includes(mutations[0]!.table))
|
|
417
|
+
burst.tables.push(mutations[0]!.table);
|
|
418
|
+
if (burst.commits === 2) writes.bursts.push(burst);
|
|
419
|
+
} else burst = { commits: 1, tables: [mutations[0]!.table], at };
|
|
420
|
+
return client.mutate(mutations);
|
|
421
|
+
},
|
|
422
|
+
} as Partial<typeof client>);
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
report: () => {
|
|
426
|
+
const merged = new Map<string, Mutable<StatementProfile>>();
|
|
427
|
+
for (const list of [
|
|
428
|
+
[...statements.values()],
|
|
429
|
+
...[...remote.values()].map((entry) => entry.statements),
|
|
430
|
+
])
|
|
431
|
+
for (const entry of list) {
|
|
432
|
+
const into = merged.get(entry.sql);
|
|
433
|
+
if (into === undefined) merged.set(entry.sql, { ...entry });
|
|
434
|
+
else {
|
|
435
|
+
into.runs += entry.runs;
|
|
436
|
+
into.totalMs += entry.totalMs;
|
|
437
|
+
into.maxMs = Math.max(into.maxMs, entry.maxMs);
|
|
438
|
+
into.rows += entry.rows;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
queries: [...queries.values()].map((query) => ({
|
|
443
|
+
...query,
|
|
444
|
+
triggers: { ...query.triggers },
|
|
445
|
+
})),
|
|
446
|
+
statements: [...merged.values()],
|
|
447
|
+
rounds: [
|
|
448
|
+
...[...remote.values()].flatMap((entry) => entry.rounds),
|
|
449
|
+
...rounds,
|
|
450
|
+
].map((entry) => ({
|
|
451
|
+
via: entry.via,
|
|
452
|
+
requestBytes: entry.requestBytes,
|
|
453
|
+
responseBytes: entry.responseBytes,
|
|
454
|
+
ms: entry.ms,
|
|
455
|
+
})),
|
|
456
|
+
deltas: { ...deltas },
|
|
457
|
+
units: {
|
|
458
|
+
...units,
|
|
459
|
+
limit: options.unitLimit ?? 500,
|
|
460
|
+
bootstraps: [...bootstraps.values()].map((unit) => ({
|
|
461
|
+
unitId: unit.unitId,
|
|
462
|
+
table: unit.table,
|
|
463
|
+
bytes: unit.bytes + unit.segmentBytes,
|
|
464
|
+
rows: unit.rows + unit.segmentRows,
|
|
465
|
+
ms: unit.ms,
|
|
466
|
+
})),
|
|
467
|
+
},
|
|
468
|
+
writes: {
|
|
469
|
+
...writes,
|
|
470
|
+
bursts: writes.bursts.map((entry) => ({
|
|
471
|
+
commits: entry.commits,
|
|
472
|
+
tables: [...entry.tables],
|
|
473
|
+
})),
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
},
|
|
477
|
+
|
|
478
|
+
reset: () => {
|
|
479
|
+
clear();
|
|
480
|
+
channel?.postMessage({ syncularProfileReset: true });
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Throws `client.profile_budget_exceeded` listing every budget the report
|
|
487
|
+
* exceeds, with `budget`, `limit` and `measured` per entry in `details.exceeded`.
|
|
488
|
+
*/
|
|
489
|
+
export function assertBudgets(
|
|
490
|
+
report: ProfileReport,
|
|
491
|
+
budgets: ProfileBudgets,
|
|
492
|
+
): void {
|
|
493
|
+
const measured: Record<keyof ProfileBudgets, number> = {
|
|
494
|
+
maxActiveUnits: report.units.peak,
|
|
495
|
+
maxRoundBytes: Math.max(
|
|
496
|
+
0,
|
|
497
|
+
...report.rounds.map((entry) => entry.requestBytes + entry.responseBytes),
|
|
498
|
+
),
|
|
499
|
+
maxRunsPerQuery: Math.max(0, ...report.queries.map((query) => query.runs)),
|
|
500
|
+
maxIdenticalRuns: report.queries.reduce(
|
|
501
|
+
(sum, query) => sum + query.identicalRuns,
|
|
502
|
+
0,
|
|
503
|
+
),
|
|
504
|
+
maxWriteBurst: Math.max(
|
|
505
|
+
0,
|
|
506
|
+
...report.writes.bursts.map((entry) => entry.commits),
|
|
507
|
+
),
|
|
508
|
+
};
|
|
509
|
+
const exceeded = Object.entries(budgets)
|
|
510
|
+
.filter(
|
|
511
|
+
([budget, limit]) => measured[budget as keyof ProfileBudgets] > limit,
|
|
512
|
+
)
|
|
513
|
+
.map(([budget, limit]) => ({
|
|
514
|
+
budget,
|
|
515
|
+
limit,
|
|
516
|
+
measured: measured[budget as keyof ProfileBudgets],
|
|
517
|
+
}));
|
|
518
|
+
if (exceeded.length > 0)
|
|
519
|
+
throw new ClientSyncError(
|
|
520
|
+
'client.profile_budget_exceeded',
|
|
521
|
+
'the profile report exceeds its budgets',
|
|
522
|
+
false,
|
|
523
|
+
{ exceeded },
|
|
524
|
+
);
|
|
525
|
+
}
|
package/src/devtools.ts
CHANGED
|
@@ -56,23 +56,26 @@ interface DevtoolsRegistry {
|
|
|
56
56
|
|
|
57
57
|
const KEY = '__SYNCULAR__';
|
|
58
58
|
|
|
59
|
+
/**
|
|
60
|
+
* @internal True when the build carries a development or test signal and no
|
|
61
|
+
* production one. The registry and `@syncular/client/dev` read it.
|
|
62
|
+
*/
|
|
63
|
+
export function developmentBuild(): boolean {
|
|
64
|
+
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production')
|
|
65
|
+
return false;
|
|
66
|
+
return (
|
|
67
|
+
(typeof import.meta.env !== 'undefined' &&
|
|
68
|
+
(import.meta.env.DEV as boolean | string | undefined) === true) ||
|
|
69
|
+
(typeof process !== 'undefined' &&
|
|
70
|
+
(process.env.NODE_ENV === 'development' ||
|
|
71
|
+
process.env.NODE_ENV === 'test'))
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
59
75
|
/** The page global to install on, or undefined when gated off. */
|
|
60
76
|
function registryHost(): Record<string, unknown> | undefined {
|
|
61
77
|
const g = globalThis as Record<string, unknown> & { window?: unknown };
|
|
62
|
-
|
|
63
|
-
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production')
|
|
64
|
-
return undefined;
|
|
65
|
-
if (
|
|
66
|
-
!(
|
|
67
|
-
(typeof import.meta.env !== 'undefined' &&
|
|
68
|
-
(import.meta.env.DEV as boolean | string | undefined) === true) ||
|
|
69
|
-
(typeof process !== 'undefined' &&
|
|
70
|
-
(process.env.NODE_ENV === 'development' ||
|
|
71
|
-
process.env.NODE_ENV === 'test'))
|
|
72
|
-
)
|
|
73
|
-
)
|
|
74
|
-
return undefined;
|
|
75
|
-
return g;
|
|
78
|
+
return g.window !== undefined && developmentBuild() ? g : undefined;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
function registryOn(host: Record<string, unknown>): DevtoolsRegistry {
|