@pgxsinkit/client 0.5.0 → 0.5.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.
@@ -10,7 +10,7 @@
10
10
  // lazy-group guard worker-side and Drizzle's own result mapping (relational/nested included) runs on the tab.
11
11
  // `ensureSynced` (async lazy-activation) is a plain RPC to the shared engine, and `isSynced` — a SYNCHRONOUS
12
12
  // peek, so never an RPC — is answered from the started-state snapshot the worker computes on its own client
13
- // and pushes (ADR-0059). The only members that stay unsupported are the genuinely tab-local ones — `pglite`
13
+ // and pushes (ADR-0059). The only members that stay unsupported are the genuinely tab-local ones — `pgwasm`
14
14
  // (no local store) and `dropReadCache` (an engine-wide cache rebuild) — each throwing a clear error saying why.
15
15
 
16
16
  import type {
@@ -803,7 +803,7 @@ export interface AttachSyncClientOptions<TRegistry extends SyncTableRegistry> {
803
803
  * token after an app auth-state change, ADR-0032 decision 3). One-shot Drizzle reads
804
804
  * (`query`/`queryRow`/`queryRaw`/`queryRawRow`) and `ensureSynced` ARE proxied to the worker, and `isSynced`
805
805
  * is answered from the worker-pushed started-state snapshot (ADR-0059); the members that throw are the
806
- * structurally unproxiable ones — `pglite`, `dropReadCache`, and `drizzle.transaction()`.
806
+ * structurally unproxiable ones — `pgwasm`, `dropReadCache`, and `drizzle.transaction()`.
807
807
  */
808
808
  export type AttachedSyncClient<TRegistry extends SyncTableRegistry> = SyncClient<TRegistry> & {
809
809
  notifyAuthChanged: () => void;
@@ -2237,7 +2237,7 @@ export async function attachSyncClient<const TRegistry extends SyncTableRegistry
2237
2237
  const noBridgeTransaction = (): never => {
2238
2238
  throw new Error(
2239
2239
  "[pgxsinkit] client.drizzle.transaction() is not available on a worker-attached client: a read " +
2240
- "transaction needs a local PGlite the tab does not have. Use one-shot reads (client.query) instead.",
2240
+ "transaction needs a local store the tab does not have. Use one-shot reads (client.query) instead.",
2241
2241
  );
2242
2242
  };
2243
2243
  // The ONE encoder of the `guardedQuery` positional wire tuple ({@link GuardedQueryWireArgs}): both tab-side
@@ -2254,7 +2254,7 @@ export async function attachSyncClient<const TRegistry extends SyncTableRegistry
2254
2254
  const options = rowMode ? { rowMode } : {};
2255
2255
  return use ? [sql, params, options, use] : [sql, params, options];
2256
2256
  };
2257
- // A `PgwasmClient`-shaped bridge executor Drizzle runs reads against (ADR-0032 decision 4). Drizzle's pglite
2257
+ // A `PgwasmClient`-shaped bridge executor Drizzle runs reads against (ADR-0032 decision 4). Drizzle's pgwasm
2258
2258
  // driver calls only `client.query` for reads and `client.transaction` for `.transaction()`. `query` routes
2259
2259
  // to the worker's `guardedQuery` RPC (stripping drizzle's non-serializable `parsers` to just `rowMode`; the
2260
2260
  // worker re-applies drizzle's identity parsers) and returns the full `Results` so Drizzle's mapping runs on
@@ -2411,7 +2411,7 @@ export async function attachSyncClient<const TRegistry extends SyncTableRegistry
2411
2411
  retryFailed: (table) => rpc<void>("retryFailed", [table]),
2412
2412
  recoverSending: (table) => rpc<void>("recoverSending", [table]),
2413
2413
  readMutationDetails: (table) => rpc<MutationDetail[]>("readMutationDetails", [table]),
2414
- // Inspection reads run in the worker (ADR-0032 S2) — the same surface as the in-process client. `pglite`
2414
+ // Inspection reads run in the worker (ADR-0032 S2) — the same surface as the in-process client. `pgwasm`
2415
2415
  // itself stays blocked (below); these route the raw statement over the RPC round-trip instead.
2416
2416
  // `options.blob` (ADR-0061) makes this the single-statement COPY bulk load too: the bytes ride the SAME
2417
2417
  // RPC and are TRANSFERRED (detached tab-side), never cloned.
@@ -2555,7 +2555,7 @@ export async function attachSyncClient<const TRegistry extends SyncTableRegistry
2555
2555
  liveQueryDiagnostics: () => rpc<LiveQueryDiagnostics[]>("liveQueryDiagnostics", []),
2556
2556
  // Store backup (ADR-0035): the worker runs the live export on its owned client (its lifecycle slot
2557
2557
  // serialises it against every tab). The dump crosses back as a transferred `ArrayBuffer`; rebuild the
2558
- // `File` tab-side from the wire form — NOT in the notSupported set (unlike the local `pglite` reads).
2558
+ // `File` tab-side from the wire form — NOT in the notSupported set (unlike the local `pgwasm` reads).
2559
2559
  exportStore: async (exportOptions?: StoreExportOptions): Promise<StoreExportResult> => {
2560
2560
  const wire = await rpc<ExportArtefactWire>("exportStore", [exportOptions]);
2561
2561
  return {
@@ -2566,7 +2566,7 @@ export async function attachSyncClient<const TRegistry extends SyncTableRegistry
2566
2566
  },
2567
2567
  // Diagnostic dump (ADR-0035): the worker runs the throwaway-clone dump on its owned client (same
2568
2568
  // lifecycle slot as every tab). The SQL crosses back as the same transferred `ArrayBuffer`; rebuild the
2569
- // `File` tab-side. NOT in the notSupported set (unlike the local `pglite` reads).
2569
+ // `File` tab-side. NOT in the notSupported set (unlike the local `pgwasm` reads).
2570
2570
  exportDiagnostics: async (exportOptions?: DiagnosticExportOptions): Promise<DiagnosticExportResult> => {
2571
2571
  const wire = await rpc<ExportArtefactWire>("exportDiagnostics", [exportOptions]);
2572
2572
  return {
@@ -353,7 +353,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
353
353
  engineStorage?.durability ?? getSyncRegistryStorage(options.registry)?.durability ?? "relaxed";
354
354
  // The declared build (ADR-0063), read at mint time for the same reason as durability.
355
355
  const currentBuild = () => engineStorage?.build ?? getSyncRegistryStorage(options.registry)?.build ?? "c";
356
- const builtInCreatePglite: StoreEngineFactory =
356
+ const builtInCreateStore: StoreEngineFactory =
357
357
  options.createStore ??
358
358
  ((storePath: string, backendOverride?: "memory") =>
359
359
  createPgwasmClient(storePath, {
@@ -381,7 +381,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
381
381
  const declaredEngine = engineStorage?.engine?.module;
382
382
  let store: PgwasmClient;
383
383
  if (declaredEngine === undefined) {
384
- store = await builtInCreatePglite(storePath, backendOverride);
384
+ store = await builtInCreateStore(storePath, backendOverride);
385
385
  } else {
386
386
  const factory = await resolveStoreEngine(declaredEngine);
387
387
  syncDebug("worker store engine declared", { storePath, module: declaredEngine });
@@ -406,7 +406,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
406
406
  const liveSubs = new Map<string, LiveSub>();
407
407
  // The live-query lifecycle (pgwasm registration, diff listener, and the ADR-0040 decision-1 awaited-teardown
408
408
  // set) lives in a single manager owned per engine. Created lazily on first subscribe because it needs the
409
- // booted client's `live` namespace (`active.pglite.live`); the worker never restarts its engine in place
409
+ // booted client's `live` namespace (`active.pgwasm.live`); the worker never restarts its engine in place
410
410
  // (ADR-0040 decision 7), so one manager serves the host's whole lifetime. `close()` disposes it.
411
411
  let liveManager: LiveQueryManager | null = null;
412
412
  const ensureLiveManager = (active: SyncClient<TRegistry>): LiveQueryManager =>
@@ -447,7 +447,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
447
447
  // as `BootReport.provision` (with `phases.pgwasmCreateMs = null`) instead of timing a create it never ran.
448
448
  let provisioned: {
449
449
  storePath: string;
450
- pglite: Promise<PgwasmClient>;
450
+ pgwasm: Promise<PgwasmClient>;
451
451
  stamp: Promise<{ initdbMs: number; provisionReadyAt: number }>;
452
452
  } | null = null;
453
453
  // A provision request performs a bounded authority read before it may mint. An attach that arrives during
@@ -734,7 +734,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
734
734
  // Never adopt when restoring (restore boots a brand-new store from the backup, not the provisioned one).
735
735
  const adoptingProvisioned = restoreFrom == null && provisioned != null && provisioned.storePath === storePath;
736
736
  const precreatedPgwasm = adoptingProvisioned
737
- ? provisioned!.pglite
737
+ ? provisioned!.pgwasm
738
738
  : restoreFrom
739
739
  ? undefined
740
740
  : options.precreatedPgwasm;
@@ -856,7 +856,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
856
856
  const dispatchRpc = async (active: SyncClient<TRegistry>, op: RpcOp, args: unknown[]): Promise<unknown> => {
857
857
  // Guarded-read parser re-application (ADR-0032 decision 4). A guarded read compiled by the tab's
858
858
  // Drizzle-over-bridge arrives as the {@link GuardedQueryWireArgs} tuple `[sql, params?, { rowMode? }, use?]`:
859
- // the `parsers` map drizzle's pglite session normally passes could not cross the bridge (it is FUNCTIONS,
859
+ // the `parsers` map the pgwasm drizzle session normally passes could not cross the bridge (it is FUNCTIONS,
860
860
  // not clonable), so the tab stripped it and we re-apply the identical map here before executing. Without it
861
861
  // pgwasm's default parsers would turn the identity-parsed OIDs (temporal OIDs + `numeric[]`) into `Date`s
862
862
  // and numbers, whereas the in-process drizzle session sees them as raw STRINGS — so a guarded read would
@@ -937,7 +937,7 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
937
937
  case "diagnostics":
938
938
  return active.diagnostics(args[0] as TableKey | undefined);
939
939
  case "rawQuery":
940
- // Inspection read (ADR-0032 S2): run straight against the worker's own store — `active.pglite`
940
+ // Inspection read (ADR-0032 S2): run straight against the worker's own store — `active.pgwasm`
941
941
  // exists worker-side (it's the in-process client). `Results` rows/fields structured-clone across
942
942
  // the wire (Dates survive postMessage), so the tab receives the same shape it would in-process.
943
943
  // The options arg is the clonable RawQueryOptions subset (rowMode, plus the ADR-0061 COPY `blob`).
@@ -1283,17 +1283,17 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
1283
1283
  }
1284
1284
 
1285
1285
  const provisionStartedAt = nowStamp();
1286
- const pglite = Promise.resolve().then(() => createStore(storePath, backendOverride));
1287
- const stamp = pglite.then(() => {
1286
+ const pgwasm = Promise.resolve().then(() => createStore(storePath, backendOverride));
1287
+ const stamp = pgwasm.then(() => {
1288
1288
  const readyAt = nowStamp();
1289
1289
  return { initdbMs: readyAt - provisionStartedAt, provisionReadyAt: readyAt };
1290
1290
  });
1291
- provisioned = { storePath, pglite, stamp };
1291
+ provisioned = { storePath, pgwasm, stamp };
1292
1292
  // Guard unobserved rejections when no attach ever adopts the spare (the page navigated away).
1293
- void pglite.catch(() => undefined);
1293
+ void pgwasm.catch(() => undefined);
1294
1294
  void stamp.catch(() => undefined);
1295
1295
  syncDebug("worker store provisioned", { storePath });
1296
- await pglite;
1296
+ await pgwasm;
1297
1297
  });
1298
1298
  provisionAttempt = attempt;
1299
1299
  const attemptStartedAt = nowStamp();
@@ -1,7 +1,7 @@
1
- // A defensive guard against PGlite upstream bug https://github.com/electric-sql/pglite/issues/1055 on the
2
- // live-query path. WHY this exists:
1
+ // A defensive guard against upstream bug https://github.com/electric-sql/pglite/issues/1055 on the
2
+ // live-query path, which the `live` extension carried until the pgwasm switch (ADR-0062). WHY this exists:
3
3
  //
4
- // PGlite 0.5.4's `live.query` / `live.incrementalQuery` inline bound params into a `CREATE TEMP VIEW`
4
+ // The inherited `live.query` / `live.incrementalQuery` inline bound params into a `CREATE TEMP VIEW`
5
5
  // (via `formatQuery`) WHENEVER `params.length > 0`. That inlining rewrites every `$N` placeholder with
6
6
  // `sql.replace(/\$([0-9]+)/g, (_, n) => "%" + n + "L")` — emitting `%NL`, which is Postgres `format()`'s
7
7
  // "pad to width N, consume the NEXT argument sequentially" directive, NOT the positional `%N$L` it means to
@@ -9,17 +9,18 @@
9
9
  // - out-of-order placeholders (`… $2 … $1 …`) silently bind params in TEXTUAL order → wrong rows;
10
10
  // - a repeated placeholder (`$1 … $1`) throws `too few arguments for format()`;
11
11
  // - a skipped placeholder silently mis-binds.
12
- // The ONLY input shape under which pgwasm's broken inlining is still correct: the placeholders, read in
12
+ // The ONLY input shape under which that broken inlining is still correct: the placeholders, read in
13
13
  // textual order, are exactly `$1, $2, …, $n` (n = params.length) — each appearing exactly once, strictly
14
14
  // ascending. Drizzle-compiled SQL always satisfies this (its serializer numbers params in the same
15
15
  // left-to-right pass that emits them); hand-written raw SQL may not.
16
16
  //
17
17
  // So this guard fails LOUDLY at the client boundary for any other shape, turning a silent wrong-rows /
18
- // cryptic-`format()` failure into an actionable error naming the upstream bug. It can be DELETED once
19
- // transcrobes/pgxsinkit pin a pgwasm release whose `formatQuery` emits the positional `%N$L`.
18
+ // cryptic-`format()` failure into an actionable error naming the upstream bug. pgwasm's own `formatQuery`
19
+ // emits the positional `%N$L` (tests/unit/pgwasm-live-format-query.test.ts), so the guard no longer
20
+ // protects pgwasm itself; whether to delete it is an open decision.
20
21
 
21
22
  /**
22
- * Throw if `sql` + `params` would hit pgwasm bug #1055's broken param inlining (see the module header).
23
+ * Throw if `sql` + `params` would hit bug #1055's broken param inlining (see the module header).
23
24
  *
24
25
  * `params.length === 0` returns immediately: pgwasm skips `formatQuery` entirely in that case, so any `$N`
25
26
  * tokens are Postgres's own concern, not this guard's. Otherwise the SQL is scanned with the SAME regex
@@ -46,7 +47,7 @@ export function assertLiveQueryParamsSafe(sql: string, params: readonly unknown[
46
47
 
47
48
  const found = numbers.length === 0 ? "(none)" : numbers.map((n) => `$${n}`).join(", ");
48
49
  throw new Error(
49
- `[pgxsinkit] live query hits PGlite bug #1055 ` +
50
+ `[pgxsinkit] live query hits upstream bug #1055 ` +
50
51
  `(https://github.com/electric-sql/pglite/issues/1055): its live-query param inlining binds $N ` +
51
52
  `placeholders SEQUENTIALLY (in textual order), not positionally, so anything but a strictly ` +
52
53
  `ascending $1..$n each used once silently mis-binds or throws. Found placeholders [${found}] for ` +