@query-farm/vgi 0.51.0 → 0.52.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.
Files changed (70) hide show
  1. package/README.md +45 -3
  2. package/dist/client-entry.js +833 -775
  3. package/dist/client-entry.js.map +10 -10
  4. package/dist/functions/storage-blobs.d.ts +173 -0
  5. package/dist/functions/storage-cf-do.d.ts +12 -0
  6. package/dist/functions/storage.d.ts +22 -0
  7. package/dist/functions/table.d.ts +6 -1
  8. package/dist/generated/vgi-client.d.ts +7 -1
  9. package/dist/generated/vgi-protocol-schemas.d.ts +10 -9
  10. package/dist/generated/vgi-protocol-types.d.ts +244 -2
  11. package/dist/generated/vgi-service.d.ts +1 -1
  12. package/dist/hosted.d.ts +117 -0
  13. package/dist/http/blob-vendor.d.ts +33 -0
  14. package/dist/http/fetch.d.ts +53 -2
  15. package/dist/http/hosted-auth.d.ts +45 -0
  16. package/dist/index.core.d.ts +9 -2
  17. package/dist/index.js +5238 -3619
  18. package/dist/index.js.map +44 -28
  19. package/dist/protocol/arguments-ref.d.ts +107 -0
  20. package/dist/protocol/dispatch.d.ts +3 -2
  21. package/dist/protocol/handlers/function.d.ts +21 -0
  22. package/dist/protocol/serialize.d.ts +1 -1
  23. package/dist/protocol/serializers/bind.d.ts +27 -1
  24. package/dist/protocol/serializers/cardinality.d.ts +1 -1
  25. package/dist/protocol/serializers/splits.d.ts +7 -0
  26. package/dist/protocol/state-serializer.d.ts +8 -7
  27. package/dist/protocol/stream-init.d.ts +17 -0
  28. package/dist/protocol/types.d.ts +24 -0
  29. package/dist/secrets/lookups.d.ts +31 -0
  30. package/dist/serve-entry.d.ts +25 -4
  31. package/dist/serve-entry.js +2940 -1219
  32. package/dist/serve-entry.js.map +44 -29
  33. package/dist/worker-cf-entry.d.ts +5 -0
  34. package/dist/worker-cf-entry.js +2928 -1223
  35. package/dist/worker-cf-entry.js.map +47 -31
  36. package/package.json +5 -4
  37. package/src/functions/storage-blobs.ts +654 -0
  38. package/src/functions/storage-cf-do.ts +24 -0
  39. package/src/functions/storage.ts +54 -5
  40. package/src/functions/table-buffering.ts +14 -4
  41. package/src/functions/table.ts +8 -4
  42. package/src/generated/vgi-client.ts +9 -2
  43. package/src/generated/vgi-protocol-schemas.ts +115 -107
  44. package/src/generated/vgi-protocol-types.ts +640 -3
  45. package/src/generated/vgi-service.ts +2 -2
  46. package/src/hosted.ts +254 -0
  47. package/src/http/blob-vendor.ts +80 -0
  48. package/src/http/fetch.ts +170 -8
  49. package/src/http/hosted-auth.ts +326 -0
  50. package/src/index.core.ts +64 -1
  51. package/src/protocol/arguments-ref.ts +260 -0
  52. package/src/protocol/dispatch.ts +3 -2
  53. package/src/protocol/handlers/function.ts +109 -60
  54. package/src/protocol/handlers/table-buffering.ts +5 -0
  55. package/src/protocol/serialize.ts +3 -0
  56. package/src/protocol/serializers/bind.ts +96 -69
  57. package/src/protocol/serializers/cardinality.ts +4 -7
  58. package/src/protocol/serializers/init.ts +5 -11
  59. package/src/protocol/serializers/splits.ts +31 -4
  60. package/src/protocol/state-serializer.ts +27 -39
  61. package/src/protocol/stream-init.ts +87 -0
  62. package/src/protocol/types.ts +25 -0
  63. package/src/secrets/lookups.ts +53 -0
  64. package/src/serve-entry.ts +105 -8
  65. package/src/worker-cf-entry.ts +33 -0
  66. package/src/worker.ts +25 -0
  67. package/dist/protocol/carried-init-request.d.ts +0 -28
  68. package/dist/util/zstd-sync.d.ts +0 -7
  69. package/src/protocol/carried-init-request.ts +0 -139
  70. package/src/util/zstd-sync.ts +0 -50
@@ -1,28 +0,0 @@
1
- import type { InitRequest } from "./types.js";
2
- /** Pack an init request's IPC bytes for the cursor: `u8 codec | payload`. */
3
- export declare function packInitRequest(ipc: Uint8Array): Uint8Array;
4
- /** What a continuation needs from the carried init request. */
5
- export interface CarriedInitRequest {
6
- /**
7
- * The parsed request -- a fresh top-level object with a fresh `bind_call`
8
- * each call, so the per-turn assignments the dispatcher makes (split
9
- * payloads) never reach the shared parse. The nested values (Arrow
10
- * schemas and batches, arguments) are shared and must be treated as
11
- * read-only, as they always were.
12
- */
13
- request: InitRequest;
14
- /** The raw bind-call IPC bytes, which the split-token fingerprint hashes. */
15
- bindCallIpc: Uint8Array;
16
- }
17
- /**
18
- * Parse the init request a cursor carries (as packed by
19
- * {@link packInitRequest}), from the process memo when this stream's turns have
20
- * been here before.
21
- */
22
- export declare function parseCarriedInitRequest(packed: Uint8Array): CarriedInitRequest;
23
- /** Cumulative parse / memo-hit counts for this process (observability and tests). */
24
- export declare function carriedInitRequestStats(): {
25
- parses: number;
26
- hits: number;
27
- entries: number;
28
- };
@@ -1,7 +0,0 @@
1
- /** A synchronous zstd codec. */
2
- export interface ZstdSync {
3
- compress(data: Uint8Array): Uint8Array;
4
- decompress(data: Uint8Array): Uint8Array;
5
- }
6
- /** This runtime's synchronous zstd codec, or `null` when it has none. */
7
- export declare function zstdSync(): ZstdSync | null;
@@ -1,139 +0,0 @@
1
- // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
- // The init request an HTTP stream carries in its cursor: packed once, parsed
3
- // once per process.
4
- //
5
- // An HTTP stream has no memory between turns, so its cursor carries the whole
6
- // init request (~9 KB of Arrow IPC for a DuckDB scan: the bind call with its
7
- // arguments, settings and sealed attach, the output schema, the filter
8
- // snapshot) and every turn used to re-parse it: the IPC read, the scalar-dict
9
- // walk and the nested bind-call / schema / filter reads cost ~0.9 ms, the
10
- // largest VGI-side share of the ~4 ms a 1000-row turn took. The cursor is also
11
- // re-sealed and base64-encoded on every turn, so its size is paid twice per
12
- // turn in AEAD and base64 work, and twice again on the wire.
13
- //
14
- // - `packInitRequest` zstd-compresses the request once, at /init (~9 KB ->
15
- // ~2.6 KB), behind a one-byte codec tag. A runtime without synchronous zstd
16
- // stores it raw.
17
- // - `parseCarriedInitRequest` keeps a small per-process LRU from the packed
18
- // bytes to their parse. It is a pure memo of a pure function: the key is the
19
- // exact bytes (hash, then a full comparison), so a hit returns precisely
20
- // what parsing those bytes would -- and those bytes came out of a cursor the
21
- // worker sealed. Nothing principal-dependent is cached: the split tokens and
22
- // the sealed attach are still opened, under the caller, on every turn. A
23
- // miss (cold process, eviction, another replica's stream) parses as before,
24
- // so correctness never depends on a hit.
25
-
26
- import { deserializeBatch } from "../arrow/index.js";
27
- import { batchToScalarDict } from "../util/arrow/index.js";
28
- import { zstdSync } from "../util/zstd-sync.js";
29
- import { deserializeInitRequest } from "./serialize.js";
30
- import type { InitRequest } from "./types.js";
31
-
32
- const RAW = 0;
33
- const ZSTD = 1;
34
-
35
- /** Pack an init request's IPC bytes for the cursor: `u8 codec | payload`. */
36
- export function packInitRequest(ipc: Uint8Array): Uint8Array {
37
- const codec = zstdSync();
38
- const compressed = codec ? codec.compress(ipc) : null;
39
- const useZstd = compressed !== null && compressed.byteLength < ipc.byteLength;
40
- const payload = useZstd ? compressed! : ipc;
41
- const packed = new Uint8Array(payload.byteLength + 1);
42
- packed[0] = useZstd ? ZSTD : RAW;
43
- packed.set(payload, 1);
44
- return packed;
45
- }
46
-
47
- function unpackInitRequest(packed: Uint8Array): Uint8Array {
48
- if (packed.byteLength === 0) throw new Error("state token carries no init request");
49
- switch (packed[0]) {
50
- case RAW:
51
- return packed.slice(1);
52
- case ZSTD: {
53
- const codec = zstdSync();
54
- if (!codec) {
55
- throw new Error(
56
- "state token carries a zstd-compressed init request, and this runtime has no synchronous zstd " +
57
- "(it was minted by a worker running under Bun or Node >= 22.15 with the same signing key)",
58
- );
59
- }
60
- return codec.decompress(packed.subarray(1));
61
- }
62
- default:
63
- throw new Error(`state token carries an init request with unknown codec ${packed[0]}`);
64
- }
65
- }
66
-
67
- /** What a continuation needs from the carried init request. */
68
- export interface CarriedInitRequest {
69
- /**
70
- * The parsed request -- a fresh top-level object with a fresh `bind_call`
71
- * each call, so the per-turn assignments the dispatcher makes (split
72
- * payloads) never reach the shared parse. The nested values (Arrow
73
- * schemas and batches, arguments) are shared and must be treated as
74
- * read-only, as they always were.
75
- */
76
- request: InitRequest;
77
- /** The raw bind-call IPC bytes, which the split-token fingerprint hashes. */
78
- bindCallIpc: Uint8Array;
79
- }
80
-
81
- interface Parsed {
82
- packed: Uint8Array;
83
- request: InitRequest;
84
- bindCallIpc: Uint8Array;
85
- }
86
-
87
- /** Streams whose parse a process keeps. Past this, the least recently used is dropped. */
88
- const MAX_ENTRIES = 128;
89
- const parsed = new Map<number, Parsed>();
90
- const stats = { parses: 0, hits: 0 };
91
-
92
- /** FNV-1a over the bytes, folded with the length. */
93
- function hashBytes(bytes: Uint8Array): number {
94
- let h = 0x811c9dc5 ^ bytes.byteLength;
95
- for (let i = 0; i < bytes.byteLength; i++) {
96
- h ^= bytes[i]!;
97
- h = Math.imul(h, 0x01000193);
98
- }
99
- return h >>> 0;
100
- }
101
-
102
- function sameBytes(a: Uint8Array, b: Uint8Array): boolean {
103
- if (a.byteLength !== b.byteLength) return false;
104
- for (let i = 0; i < a.byteLength; i++) if (a[i] !== b[i]) return false;
105
- return true;
106
- }
107
-
108
- /**
109
- * Parse the init request a cursor carries (as packed by
110
- * {@link packInitRequest}), from the process memo when this stream's turns have
111
- * been here before.
112
- */
113
- export function parseCarriedInitRequest(packed: Uint8Array): CarriedInitRequest {
114
- const key = hashBytes(packed);
115
- let entry = parsed.get(key);
116
- if (entry && sameBytes(entry.packed, packed)) {
117
- // Refresh its recency.
118
- parsed.delete(key);
119
- parsed.set(key, entry);
120
- stats.hits++;
121
- } else {
122
- const dict = batchToScalarDict(deserializeBatch(unpackInitRequest(packed)));
123
- stats.parses++;
124
- entry = { packed: packed.slice(), request: deserializeInitRequest(dict), bindCallIpc: dict.bind_call };
125
- // A hash collision simply replaces the other stream's entry.
126
- parsed.delete(key);
127
- if (parsed.size >= MAX_ENTRIES) parsed.delete(parsed.keys().next().value!);
128
- parsed.set(key, entry);
129
- }
130
- return {
131
- request: { ...entry.request, bind_call: { ...entry.request.bind_call } },
132
- bindCallIpc: entry.bindCallIpc,
133
- };
134
- }
135
-
136
- /** Cumulative parse / memo-hit counts for this process (observability and tests). */
137
- export function carriedInitRequestStats(): { parses: number; hits: number; entries: number } {
138
- return { ...stats, entries: parsed.size };
139
- }
@@ -1,50 +0,0 @@
1
- // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
- // Synchronous zstd, where the runtime has it.
3
- //
4
- // The state serializer is synchronous (vgi-rpc's StateSerializer contract), so
5
- // it cannot use the async helpers vgi-rpc exposes. Bun has
6
- // `Bun.zstdCompressSync`; Node >= 22.15 has `zlib.zstdCompressSync`, reached
7
- // from ESM through `process.getBuiltinModule`. Anything else (workerd, a
8
- // browser) gets `null` and callers fall back to storing bytes uncompressed.
9
-
10
- /** A synchronous zstd codec. */
11
- export interface ZstdSync {
12
- compress(data: Uint8Array): Uint8Array;
13
- decompress(data: Uint8Array): Uint8Array;
14
- }
15
-
16
- /** Level 3 is zstd's default: ~3.4x on an init request, ~50 us for 9 KB. */
17
- const LEVEL = 3;
18
-
19
- function resolveZstdSync(): ZstdSync | null {
20
- const bun = (globalThis as any).Bun;
21
- if (typeof bun?.zstdCompressSync === "function" && typeof bun?.zstdDecompressSync === "function") {
22
- return {
23
- compress: (data) => new Uint8Array(bun.zstdCompressSync(data, { level: LEVEL })),
24
- // Copied into a fresh buffer: Arrow readers want byteOffset 0.
25
- decompress: (data) => new Uint8Array(bun.zstdDecompressSync(data)),
26
- };
27
- }
28
- let zlib: any = null;
29
- try {
30
- zlib = (globalThis as any).process?.getBuiltinModule?.("node:zlib") ?? null;
31
- } catch {
32
- zlib = null;
33
- }
34
- if (typeof zlib?.zstdCompressSync === "function" && typeof zlib?.zstdDecompressSync === "function") {
35
- const params = { [zlib.constants.ZSTD_c_compressionLevel]: LEVEL };
36
- return {
37
- compress: (data) => new Uint8Array(zlib.zstdCompressSync(data, { params })),
38
- decompress: (data) => new Uint8Array(zlib.zstdDecompressSync(data)),
39
- };
40
- }
41
- return null;
42
- }
43
-
44
- let resolved: ZstdSync | null | undefined;
45
-
46
- /** This runtime's synchronous zstd codec, or `null` when it has none. */
47
- export function zstdSync(): ZstdSync | null {
48
- if (resolved === undefined) resolved = resolveZstdSync();
49
- return resolved;
50
- }