@terrascale/sdk 0.0.3 → 0.0.5

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 (66) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +206 -67
  3. package/package.json +10 -4
  4. package/sdk-public-v1-route-manifest.json +85 -0
  5. package/sdk-v1-route-manifest.json +896 -15
  6. package/src/http.js +3 -48
  7. package/src/index.js +80 -1
  8. package/src/local/environment.js +0 -1
  9. package/src/ts-auth.js +1 -5
  10. package/src/v1/backups.js +111 -0
  11. package/src/v1/catalog.js +240 -0
  12. package/src/v1/changes.js +225 -0
  13. package/src/v1/database-schema.js +45 -0
  14. package/src/v1/decode.js +6 -5
  15. package/src/v1/errors.js +23 -3
  16. package/src/v1/export.js +68 -0
  17. package/src/v1/ids.js +5 -2
  18. package/src/v1/imports.js +72 -0
  19. package/src/v1/indexes.js +76 -0
  20. package/src/v1/json.js +4 -3
  21. package/src/v1/management-decode.js +24 -0
  22. package/src/v1/management-routes.js +72 -0
  23. package/src/v1/management-validation.js +25 -0
  24. package/src/v1/routes.js +32 -7
  25. package/src/v1/saved-queries.js +58 -0
  26. package/src/v1/shape-crypto.js +125 -0
  27. package/src/v1/shape-manifest.js +31 -0
  28. package/src/v1/shape-reader.js +214 -0
  29. package/src/v1/shape-subscription.js +208 -0
  30. package/src/v1/shapes.js +11 -5
  31. package/src/v1/sql.js +98 -0
  32. package/src/v1/system.js +80 -0
  33. package/src/v1/terrabase-v1.js +231 -30
  34. package/src/v1/transport.js +117 -33
  35. package/src/v1/usage.js +101 -0
  36. package/types/http.d.ts +2 -8
  37. package/types/index.d.ts +73 -0
  38. package/types/local/environment.d.ts +1 -1
  39. package/types/ts-auth.d.ts +0 -2
  40. package/types/v1/backups.d.ts +128 -0
  41. package/types/v1/catalog.d.ts +104 -0
  42. package/types/v1/changes.d.ts +53 -0
  43. package/types/v1/database-schema.d.ts +82 -0
  44. package/types/v1/decode.d.ts +20 -4
  45. package/types/v1/errors.d.ts +16 -1
  46. package/types/v1/export.d.ts +26 -0
  47. package/types/v1/ids.d.ts +2 -2
  48. package/types/v1/imports.d.ts +132 -0
  49. package/types/v1/indexes.d.ts +75 -0
  50. package/types/v1/management-decode.d.ts +15 -0
  51. package/types/v1/management-routes.d.ts +3 -0
  52. package/types/v1/management-validation.d.ts +8 -0
  53. package/types/v1/routes.d.ts +5 -2
  54. package/types/v1/saved-queries.d.ts +33 -0
  55. package/types/v1/shape-crypto.d.ts +39 -0
  56. package/types/v1/shape-manifest.d.ts +28 -0
  57. package/types/v1/shape-reader.d.ts +45 -0
  58. package/types/v1/shape-subscription.d.ts +35 -0
  59. package/types/v1/shapes.d.ts +16 -2
  60. package/types/v1/sql.d.ts +47 -0
  61. package/types/v1/system.d.ts +85 -0
  62. package/types/v1/terrabase-v1.d.ts +140 -6
  63. package/types/v1/transport.d.ts +33 -10
  64. package/types/v1/usage.d.ts +171 -0
  65. package/src/ts-auth-discovery.js +0 -207
  66. package/types/ts-auth-discovery.d.ts +0 -19
@@ -0,0 +1,214 @@
1
+ // The proposed free-read v1 protocol is separate from the historical shape() contract.
2
+ import { requireId } from "../ids.js";
3
+ import { malformed, object, string } from "./decode.js";
4
+ import { decodeShapePage, encodeShapeRequest, requireShapeHandle, requireShapeOffset } from "./shapes.js";
5
+ import { decryptShapeObject } from "./shape-crypto.js";
6
+ import { parseExactJson } from "./json.js";
7
+ import { TerraBaseV1Error } from "./errors.js";
8
+ /** @import { V1Transport } from "./transport.js" */
9
+ /** @import { V1ShapeRequest, V1ShapePage } from "./shapes.js" */
10
+ /** @typedef {{handle: string; offset: string; epoch: string; keyId: string}} V1SealedShapePosition */
11
+ /** @typedef {{ latestEpoch: bigint; keys: Map<string, { epoch: bigint; key: Uint8Array }> }} ShapeKeys */
12
+ /** @type {WeakMap<V1Transport, Map<string, ShapeKeys>>} Only immutable credentials retain keys across decrypts. */
13
+ const shapeKeys = new WeakMap();
14
+ /** @param {Readonly<Record<string, string>>} params */
15
+ const keyScope = params => JSON.stringify([params.database, params.collection]);
16
+ /**
17
+ * A refetch invalidates both the materialized state and any epoch keys for that collection.
18
+ * @param {V1Transport} transport @param {Readonly<Record<string, string>>} params
19
+ * @param {"readFreeShape" | "getShapeKey"} operation @param {import("./transport.js").V1CallOptions} options
20
+ */
21
+ async function shapeCall(transport, params, operation, options) {
22
+ try { return await transport.call(operation, params, options); }
23
+ catch (error) {
24
+ if (error instanceof TerraBaseV1Error && error.status === 409 && error.code === "must_refetch")
25
+ shapeKeys.get(transport)?.delete(keyScope(params));
26
+ throw error;
27
+ }
28
+ }
29
+ /** @param {V1Transport} transport @param {Readonly<Record<string, string>>} params @returns {ShapeKeys | undefined} */
30
+ function currentKeys(transport, params) {
31
+ // Callback tokens can change users; authorize every decrypt without retaining their keys.
32
+ if (transport.hasTokenProvider) return undefined;
33
+ let scopes = shapeKeys.get(transport);
34
+ if (scopes === undefined) { scopes = new Map(); shapeKeys.set(transport, scopes); }
35
+ const scope = keyScope(params);
36
+ let cache = scopes.get(scope);
37
+ if (cache === undefined) { cache = { latestEpoch: 0n, keys: new Map() }; scopes.set(scope, cache); }
38
+ // Expiry is lazy; a higher served epoch also protects against a regressed client clock.
39
+ const epoch = BigInt(Math.floor(Date.now() / 600_000));
40
+ if (epoch > cache.latestEpoch) cache.latestEpoch = epoch;
41
+ for (const [id, entry] of cache.keys) if (entry.epoch < cache.latestEpoch) cache.keys.delete(id);
42
+ return cache;
43
+ }
44
+ /** @param {unknown} value @returns {string} */
45
+ function decimal(value) {
46
+ const text = requireShapeOffset(value);
47
+ if (text === "-1" || text.startsWith("snap.")) malformed("shape decimal");
48
+ return text;
49
+ }
50
+ /** @param {unknown} value @returns {string} */
51
+ function resumeToken(value) {
52
+ // Resume envelopes are opaque: validate header safety, never decode or normalize them.
53
+ if (typeof value !== "string" || !/^[A-Za-z0-9_-]+$/u.test(value)) malformed("shape resume cursor");
54
+ return value;
55
+ }
56
+ /** @param {string} offset @returns {string} */
57
+ function snapshotKey(offset) {
58
+ const encoded = offset.split(".")[2] ?? "";
59
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
60
+ Uint8Array.from(atob(encoded.replace(/-/gu, "+").replace(/_/gu, "/")), c => c.charCodeAt(0)),
61
+ );
62
+ }
63
+ /** @param {import("./json.js").JsonValue | undefined} value @param {Headers} headers @returns {V1ShapePage} */
64
+ export function decodeFreeShapePage(value, headers) {
65
+ const page = decodeShapePage(value, headers);
66
+ const raw = object(value, "free shape page");
67
+ const covered = decimal(raw.covered_offset);
68
+ const values = decimal(raw.values_at);
69
+ resumeToken(raw.cursor_resume);
70
+ if (page.offset === "-1" || page.nextOffset === "-1" || BigInt(values) < BigInt(covered)) malformed("shape coverage");
71
+ if (page.offset.startsWith("snap.")) {
72
+ const head = page.offset.split(".")[1];
73
+ if (covered !== head || values !== head || page.upToDate || page.changes.length !== 1 || page.changes[0]?.offset !== head ||
74
+ page.changes[0]?.ops.some(op => op.op !== "upsert") ||
75
+ (page.nextOffset.startsWith("snap.") ? page.nextOffset.split(".")[1] !== head || page.nextOffset === page.offset : page.nextOffset !== head))
76
+ malformed("shape snapshot coverage");
77
+ if (page.nextOffset.startsWith("snap.") && (page.nextOffset.endsWith(".0") ||
78
+ (!page.offset.endsWith(".0") && snapshotKey(page.nextOffset) <= snapshotKey(page.offset)))) malformed("shape snapshot progression");
79
+ } else {
80
+ if (page.nextOffset.startsWith("snap.") || page.nextOffset !== covered || BigInt(covered) < BigInt(page.offset) ||
81
+ (!page.upToDate && covered === page.offset)) malformed("shape log coverage");
82
+ let previous = BigInt(page.offset);
83
+ for (const change of page.changes) {
84
+ const offset = BigInt(decimal(change.offset));
85
+ if (offset <= previous || offset > BigInt(covered)) malformed("shape commit order");
86
+ previous = offset;
87
+ }
88
+ }
89
+ for (const change of page.changes) {
90
+ let previousId;
91
+ for (const op of change.ops) {
92
+ if (previousId !== undefined && op.id <= previousId) malformed("shape document order");
93
+ previousId = op.id;
94
+ }
95
+ }
96
+ return page;
97
+ }
98
+ /**
99
+ * Follows exactly one authorized same-route 307; credentials never follow an arbitrary Location.
100
+ * @param {V1Transport} transport @param {Readonly<Record<string,string>>} params
101
+ * @param {V1ShapeRequest} request @param {AbortSignal | undefined} signal
102
+ * @param {string | undefined} [ifNoneMatch] A complete entity-tag list or *; 304 throws not_modified with response headers.
103
+ * @returns {Promise<V1ShapePage>}
104
+ */
105
+ export async function readShapePage(transport, params, request, signal, ifNoneMatch) {
106
+ const isResume = (request.offset ?? "-1") === "-1" && request.cursor !== undefined;
107
+ if (isResume && (request.handle !== undefined || request.live !== undefined)) throw new TypeError("Shape resume requires offset=-1 without handle or live.");
108
+ const query = encodeShapeRequest(isResume ? { ...request, cursor: undefined } : request);
109
+ // Canonical filter size is checked by the producer; check our exact wire spelling here.
110
+ if (new TextEncoder().encode(query.get("where") ?? "").byteLength > 8192) throw new TypeError("Shape filters are limited to 8 KiB of UTF-8.");
111
+ if (query.get("offset") === "-1" && query.has("handle")) throw new TypeError("Shape bootstrap must omit handle.");
112
+ /** @type {Record<string,string>} */
113
+ const headers = {};
114
+ if (ifNoneMatch !== undefined) {
115
+ if (typeof ifNoneMatch !== "string" || !/^[ \t]*(?:\*|(?:W\/)?"[\x21\x23-\x7e\x80-\xff]*"(?:[ \t]*,[ \t]*(?:W\/)?"[\x21\x23-\x7e\x80-\xff]*")*)[ \t]*$/u.test(ifNoneMatch))
116
+ throw new TypeError("ifNoneMatch must be * or a list of quoted entity tags.");
117
+ headers["If-None-Match"] = ifNoneMatch;
118
+ }
119
+ const source = new URL(await transport.url("readFreeShape", params, signal));
120
+ if (isResume) {
121
+ query.set("cursor", resumeToken(request.cursor));
122
+ // The producer reserves 64 bytes for request-line framing in its 8 KiB ceiling.
123
+ if (new TextEncoder().encode(`${source.pathname}?${query}`).length > 8192 - 64) {
124
+ headers["TB-Shape-Resume"] = resumeToken(request.cursor);
125
+ query.delete("cursor");
126
+ }
127
+ }
128
+ let response = await shapeCall(transport, params, "readFreeShape", { query, headers, signal });
129
+ if (response.status === 307) {
130
+ if (query.get("offset") !== "-1") malformed("unexpected shape redirect");
131
+ const location = response.headers.get("location");
132
+ if (location === null) malformed("shape redirect Location");
133
+ const target = new URL(location, source);
134
+ if (target.origin !== source.origin || target.pathname !== source.pathname || target.username !== "" || target.password !== "" || target.hash !== "") malformed("shape redirect target");
135
+ for (const key of target.searchParams.keys()) {
136
+ if (!["where", "handle", "offset"].includes(key) || target.searchParams.getAll(key).length !== 1) malformed("shape redirect query");
137
+ }
138
+ const handle = requireShapeHandle(target.searchParams.get("handle"));
139
+ const offset = requireShapeOffset(target.searchParams.get("offset"));
140
+ if (offset === "-1" || (!isResume && !/^snap\.[0-9]+\.0$/u.test(offset)) || (isResume && offset.startsWith("snap."))) malformed("shape redirect offset");
141
+ if (target.searchParams.has("where") !== query.has("where")) malformed("shape redirect filter");
142
+ // Keep the caller's validated filter: the server's canonical spelling must not broaden it.
143
+ query.delete("cursor");
144
+ delete headers["TB-Shape-Resume"];
145
+ query.set("handle", handle);
146
+ query.set("offset", offset);
147
+ response = await shapeCall(transport, params, "readFreeShape", { query, headers, signal });
148
+ if (response.status !== 200) malformed("repeated shape redirect");
149
+ } else if (query.get("offset") === "-1") malformed("shape bootstrap requires redirect");
150
+ const page = decodeFreeShapePage(response.value, response.headers);
151
+ if (page.handle !== query.get("handle") || page.offset !== query.get("offset")) malformed("shape response position");
152
+ const inputCursor = query.get("cursor");
153
+ if (request.live === true && (page.cursor === undefined || (inputCursor !== null && BigInt(page.cursor) <= BigInt(inputCursor)))) malformed("shape live cursor advance");
154
+ return page;
155
+ }
156
+ /**
157
+ * Reuse keys only for static credentials; callback tokens authorize every decrypt with no-store.
158
+ * @param {V1Transport} transport @param {Readonly<Record<string,string>>} params
159
+ * @param {Uint8Array} bytes @param {V1SealedShapePosition} position @param {AbortSignal | undefined} signal
160
+ * @returns {Promise<V1ShapePage>}
161
+ */
162
+ export async function decryptSealedShapePage(transport, params, bytes, position, signal) {
163
+ const handle = requireShapeHandle(position.handle);
164
+ const offset = requireShapeOffset(position.offset);
165
+ if (offset === "-1") throw new TypeError("Sealed shapes require a snapshot or log offset.");
166
+ if (typeof position.epoch !== "string" || !/^(?:0|[1-9][0-9]*)$/u.test(position.epoch) || BigInt(position.epoch) > (1n << 64n) - 1n) throw new TypeError("Shape epoch must be a canonical uint64.");
167
+ const epoch = position.epoch;
168
+ const keyId = requireId(position.keyId, "Shape key ID");
169
+ signal?.throwIfAborted();
170
+ const cache = currentKeys(transport, params);
171
+ const cacheId = JSON.stringify([handle, epoch, keyId]);
172
+ let raw = cache?.keys.get(cacheId)?.key;
173
+ if (raw === undefined) {
174
+ const query = new URLSearchParams({ handle, epoch, key_id: keyId });
175
+ const response = await shapeCall(transport, params, "getShapeKey", { query, cache: "no-store", signal });
176
+ const key = object(response.value, "shape epoch key");
177
+ if (key.handle !== handle || key.epoch !== epoch || key.key_id !== keyId) malformed("shape epoch key binding");
178
+ const encoded = string(key.key, "shape epoch key");
179
+ if (!/^[A-Za-z0-9_-]{43}$/u.test(encoded)) malformed("shape epoch key encoding");
180
+ raw = Uint8Array.from(atob(encoded.replace(/-/gu, "+").replace(/_/gu, "/")), c => c.charCodeAt(0));
181
+ if (btoa(String.fromCharCode(...raw)).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "") !== encoded) malformed("shape epoch key encoding");
182
+ const servedEpoch = BigInt(epoch);
183
+ const active = currentKeys(transport, params);
184
+ if (cache !== undefined && active === cache && servedEpoch >= cache.latestEpoch) {
185
+ cache.latestEpoch = servedEpoch;
186
+ for (const [id, entry] of cache.keys) if (entry.epoch < servedEpoch) cache.keys.delete(id);
187
+ cache.keys.set(cacheId, { epoch: servedEpoch, key: raw });
188
+ }
189
+ }
190
+ const plaintext = await decryptShapeObject(bytes, { handle, offset, epoch, keyId, key: raw });
191
+ signal?.throwIfAborted();
192
+ const page = decodeFreeShapePage(parseExactJson(new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(plaintext)), new Headers());
193
+ if (page.handle !== handle || page.offset !== offset || page.upToDate) malformed("sealed shape position");
194
+ return page;
195
+ }
196
+
197
+ /** @typedef {{readonly handle: string; readonly epoch: string; readonly keyId: string}} V1ShapeKeyRequest */
198
+ /** @typedef {V1ShapeKeyRequest & {readonly key: Uint8Array}} V1ShapeKey */
199
+ /** @param {V1Transport} transport @param {Readonly<Record<string, string>>} params @param {V1ShapeKeyRequest} request @param {AbortSignal | undefined} signal @returns {Promise<V1ShapeKey>} */
200
+ export async function readShapeKey(transport, params, request, signal) {
201
+ const handle = requireShapeHandle(request.handle);
202
+ const epoch = decimal(request.epoch);
203
+ const keyId = requireId(request.keyId);
204
+ const response = await shapeCall(transport, params, "getShapeKey", {
205
+ query: new URLSearchParams({ handle, epoch, key_id: keyId }), cache: "no-store", signal,
206
+ });
207
+ const key = object(response.value, "shape epoch key");
208
+ if (key.handle !== handle || key.epoch !== epoch || key.key_id !== keyId) malformed("shape epoch key binding");
209
+ const encoded = string(key.key, "shape key");
210
+ if (!/^[A-Za-z0-9_-]{43}$/u.test(encoded)) malformed("shape key encoding");
211
+ const bytes = Uint8Array.from(atob(encoded.replace(/-/gu, "+").replace(/_/gu, "/")), c => c.charCodeAt(0));
212
+ if (btoa(String.fromCharCode(...bytes)).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "") !== encoded) malformed("shape key encoding");
213
+ return Object.freeze({ handle, epoch, keyId, key: bytes });
214
+ }
@@ -0,0 +1,208 @@
1
+ // Client state for one collection shape: bootstrap, snapshot pages, log chunks, then the live tail.
2
+ import { TerraBaseV1Error } from "./errors.js";
3
+ import { requireFilter } from "./filter.js";
4
+ import { parseExactJson, stringifyExactJson } from "./json.js";
5
+ import { readShapePage } from "./shape-reader.js";
6
+ /**
7
+ * @import { JsonObject } from "./json.js"
8
+ * @import { V1Filter } from "./filter.js"
9
+ * @import { V1Transport } from "./transport.js"
10
+ */
11
+ /**
12
+ * @typedef {object} V1ShapeSubscribeOptions
13
+ * @property {AbortSignal | undefined} [signal] Closes the subscription when aborted; cancellation settles `done`.
14
+ * @property {((subscription: V1ShapeSubscription) => void) | undefined} [onUpdate] Runs after validated responses and freshness changes; throwing rejects `done` unless already closed or aborted.
15
+ */
16
+ /** @typedef {{ readonly version: string; readonly document: JsonObject }} V1ShapeDocument */
17
+ const retryBaseMilliseconds = 250;
18
+ const retryCapMilliseconds = 30_000;
19
+ // The eighth attempt reaches the delay ceiling; retries continue until success or cancellation.
20
+ const maximumBackoffAttempt = 8;
21
+ /** @param {import("./errors.js").TerraBaseV1Error} error @returns {boolean} */
22
+ function isRetryable(error) {
23
+ return (error.status === 429 && error.code === "shape_busy") ||
24
+ (error.status === 503 && (error.code === "unavailable" || error.code === "shape_too_large"));
25
+ }
26
+ /** @param {number} attempt @param {number | undefined} retryAfter @returns {number} */
27
+ function retryDelay(attempt, retryAfter) {
28
+ return Math.max(Math.min(retryCapMilliseconds, retryBaseMilliseconds * 2 ** (attempt - 1)), (retryAfter ?? 0) * 1000);
29
+ }
30
+ /** @param {unknown} value @param {readonly string[]} allowed @param {string} what */
31
+ function requireMembers(value, allowed, what) {
32
+ if (typeof value !== "object" || value === null) throw new TypeError(`${what} is an object.`);
33
+ for (const key of Object.keys(value)) if (!allowed.includes(key)) throw new TypeError(`Unknown ${what} member ${key}.`);
34
+ }
35
+ /** @template T @param {T} value @returns {T} */
36
+ function deepFreeze(value) {
37
+ if (typeof value === "object" && value !== null && !Object.isFrozen(value)) {
38
+ Object.freeze(value);
39
+ for (const item of Object.values(value)) deepFreeze(item);
40
+ }
41
+ return value;
42
+ }
43
+ // setTimeout overflows above 2^31-1 ms and fires at once, so a long Retry-After is waited in chunks.
44
+ const maximumTimerMilliseconds = 2 ** 31 - 1;
45
+ /** @param {number} milliseconds @param {AbortSignal} signal @returns {Promise<void>} */
46
+ function sleep(milliseconds, signal) {
47
+ return new Promise((resolve, reject) => {
48
+ signal.throwIfAborted();
49
+ let remaining = milliseconds;
50
+ /** @type {ReturnType<typeof setTimeout> | undefined} */
51
+ let timer;
52
+ const onAbort = () => { clearTimeout(timer); reject(signal.reason); };
53
+ const wait = () => {
54
+ const chunk = Math.min(remaining, maximumTimerMilliseconds);
55
+ timer = setTimeout(() => {
56
+ remaining -= chunk;
57
+ if (remaining > 0) wait();
58
+ else { signal.removeEventListener("abort", onAbort); resolve(); }
59
+ }, chunk);
60
+ };
61
+ signal.addEventListener("abort", onAbort, { once: true });
62
+ wait();
63
+ });
64
+ }
65
+ export class V1ShapeSubscription {
66
+ /** @type {Map<string, V1ShapeDocument>} */
67
+ #documents = new Map();
68
+ /** @type {string | undefined} */
69
+ #handle;
70
+ #offset = "-1";
71
+ /** @type {string | undefined} */
72
+ #cursor;
73
+ /** @type {string | undefined} Usable only from a snapshot-complete or log position. */
74
+ #resume;
75
+ #upToDate = false;
76
+ // Set after a changed-handle 409 while the saved resume is still usable; cleared by any commit or reset.
77
+ #resumeAttempt = false;
78
+ #controller = new AbortController();
79
+ #transport;
80
+ #params;
81
+ /** @type {V1Filter | undefined} */
82
+ #where;
83
+ #onUpdate;
84
+ #external;
85
+ #onExternalAbort = () => this.close();
86
+ #done;
87
+ /**
88
+ * @param {V1Transport} transport
89
+ * @param {Readonly<Record<string, string>>} params
90
+ * @param {{ where?: V1Filter | undefined }} [shape]
91
+ * @param {V1ShapeSubscribeOptions} [options]
92
+ */
93
+ constructor(transport, params, shape = {}, options = {}) {
94
+ requireMembers(shape, ["where"], "shape");
95
+ requireMembers(options, ["signal", "onUpdate"], "shape subscribe option");
96
+ const { where } = shape;
97
+ const { signal, onUpdate } = options;
98
+ if (signal !== undefined && !(signal instanceof AbortSignal)) throw new TypeError("signal is an AbortSignal.");
99
+ if (onUpdate !== undefined && typeof onUpdate !== "function") throw new TypeError("onUpdate is a function.");
100
+ this.#transport = transport;
101
+ this.#params = Object.freeze({ ...params });
102
+ // Exact copy: a structuredClone would lose the source text of large or fractional JSON numbers.
103
+ this.#where = where === undefined ? undefined : requireFilter(parseExactJson(stringifyExactJson(requireFilter(where), "shape filter")));
104
+ this.#onUpdate = onUpdate;
105
+ this.#external = signal;
106
+ if (signal?.aborted === true) this.close();
107
+ else signal?.addEventListener("abort", this.#onExternalAbort);
108
+ this.#done = this.#run();
109
+ // Callback-only consumers must not trigger a process-level unhandled rejection.
110
+ void this.#done.catch(() => {});
111
+ }
112
+ /** A copy per read: callers cannot change the documents this subscription holds. */
113
+ get state() { return new Map(this.#documents); }
114
+ get upToDate() { return this.#upToDate; }
115
+ /** Settles when the run ends: resolves on close, rejects on a terminal failure. */
116
+ get done() { return this.#done; }
117
+ close() { this.#controller.abort(); }
118
+ /** @returns {Promise<void>} */
119
+ async #run() {
120
+ const signal = this.#controller.signal;
121
+ let retries = 0;
122
+ try {
123
+ for (;;) {
124
+ signal.throwIfAborted();
125
+ const request = this.#request();
126
+ let page;
127
+ try {
128
+ page = await readShapePage(this.#transport, this.#params, request, signal);
129
+ } catch (error) {
130
+ if (!(error instanceof TerraBaseV1Error)) throw error;
131
+ if (isRetryable(error)) {
132
+ retries = Math.min(retries + 1, maximumBackoffAttempt);
133
+ await sleep(retryDelay(retries, error.retryAfter), signal);
134
+ continue;
135
+ }
136
+ if (error.status !== 409 || error.code !== "must_refetch") throw error;
137
+ this.#recover(request, error);
138
+ continue;
139
+ }
140
+ retries = 0;
141
+ this.#commit(page);
142
+ this.#onUpdate?.(this);
143
+ }
144
+ } catch (error) {
145
+ if (signal.aborted) return;
146
+ throw error;
147
+ } finally {
148
+ // A finished run must not keep the caller's signal (and this subscription) alive.
149
+ this.#external?.removeEventListener("abort", this.#onExternalAbort);
150
+ }
151
+ }
152
+ /** @returns {import("./shapes.js").V1ShapeRequest} */
153
+ #request() {
154
+ if (this.#resumeAttempt) return { where: this.#where, offset: "-1", cursor: this.#resume };
155
+ if (this.#handle === undefined) return { where: this.#where, offset: "-1" };
156
+ // Only a fresh head is a live tail; snapshot positions are never live.
157
+ if (this.#upToDate && !this.#offset.startsWith("snap.")) return { where: this.#where, offset: this.#offset, handle: this.#handle, live: true, cursor: this.#cursor };
158
+ return { where: this.#where, offset: this.#offset, handle: this.#handle };
159
+ }
160
+ /**
161
+ * @param {import("./shapes.js").V1ShapeRequest} request
162
+ * @param {TerraBaseV1Error} error
163
+ */
164
+ #recover(request, error) {
165
+ if (this.#resumeAttempt) return this.#reset();
166
+ if (request.handle === undefined) throw error;
167
+ // §7.6 reads the lineage from the response header. A body that names another handle is ambiguous: fail closed.
168
+ const handle = error.headers.get("tb-shape-handle");
169
+ const changed = handle !== null && handle !== this.#handle && (error.handle === undefined || error.handle === handle);
170
+ if (changed && this.#resume !== undefined) {
171
+ // One attempt on the new lineage; its own 409 falls through to #reset above.
172
+ this.#resumeAttempt = true;
173
+ if (this.#upToDate) {
174
+ this.#upToDate = false;
175
+ this.#onUpdate?.(this);
176
+ }
177
+ return;
178
+ }
179
+ this.#reset();
180
+ }
181
+ #reset() {
182
+ this.#documents = new Map();
183
+ this.#handle = undefined;
184
+ this.#offset = "-1";
185
+ this.#cursor = undefined;
186
+ this.#resume = undefined;
187
+ this.#upToDate = false;
188
+ this.#resumeAttempt = false;
189
+ this.#onUpdate?.(this);
190
+ }
191
+ /** @param {import("./shapes.js").V1ShapePage} page */
192
+ #commit(page) {
193
+ // Repeats are applied, never skipped: a repeated event can carry a newer materialized value.
194
+ const documents = new Map(this.#documents);
195
+ for (const change of page.changes)
196
+ for (const op of change.ops) {
197
+ if (op.op === "delete") documents.delete(op.id);
198
+ else documents.set(op.id, deepFreeze({ version: op.version, document: op.document }));
199
+ }
200
+ this.#documents = documents;
201
+ this.#handle = page.handle;
202
+ this.#offset = page.nextOffset;
203
+ this.#cursor = page.cursor;
204
+ this.#resume = page.nextOffset.startsWith("snap.") ? undefined : page.cursorResume;
205
+ this.#upToDate = page.upToDate;
206
+ this.#resumeAttempt = false;
207
+ }
208
+ }
package/src/v1/shapes.js CHANGED
@@ -16,7 +16,7 @@ import { stringifyExactJson } from "./json.js";
16
16
  * @property {boolean | undefined} [live] True only for log positions; waits up to 20 seconds.
17
17
  * @property {string | undefined} [cursor] Previous live response's TB-Shape-Cursor.
18
18
  */
19
- /** @typedef {{ readonly op: "upsert"; readonly id: import("../ids.js").EntityId; readonly version: string; readonly document: JsonObject } | { readonly op: "delete"; readonly id: import("../ids.js").EntityId }} V1ShapeOp */
19
+ /** @typedef {{ readonly op: "upsert"; readonly id: string; readonly version: string; readonly document: JsonObject } | { readonly op: "delete"; readonly id: string }} V1ShapeOp */
20
20
  /** @typedef {{ readonly offset: string; readonly ops: readonly V1ShapeOp[] }} V1ShapeChange */
21
21
  /**
22
22
  * One snapshot page or log chunk. Only a fresh open tail/live response can report upToDate.
@@ -29,6 +29,9 @@ import { stringifyExactJson } from "./json.js";
29
29
  * @property {readonly V1ShapeChange[]} changes Apply commits and operations in their received order.
30
30
  * @property {string | undefined} cursor Echo this as cursor on the next live request.
31
31
  * @property {string | undefined} etag
32
+ * @property {string | undefined} [coveredOffset] Free-read protocol coverage (feed sequence, not write offset).
33
+ * @property {string | undefined} [valuesAt] Free-read materialization position.
34
+ * @property {string | undefined} [cursorResume] Opaque free-read resume token.
32
35
  */
33
36
  const maximumOffset = (1n << 63n) - 1n;
34
37
  const handlePattern = /^h1-[0-9a-f]{16}$/u;
@@ -59,7 +62,7 @@ export function requireShapeOffset(value) {
59
62
  throw new TypeError("A shape offset is -1, a signed-64-bit decimal log position, or snap.<head>.<canonical-base64url-id>.");
60
63
  }
61
64
  /** @param {unknown} value @returns {string} */
62
- function requireHandle(value) {
65
+ export function requireShapeHandle(value) {
63
66
  if (typeof value !== "string" || !handlePattern.test(value)) throw new TypeError("A shape handle is h1- followed by 16 lowercase hex characters.");
64
67
  return value;
65
68
  }
@@ -72,7 +75,7 @@ export function encodeShapeRequest(request) {
72
75
  if (request.where !== undefined) query.set("where", stringifyExactJson(requireFilter(request.where), "shape filter"));
73
76
  const offset = requireShapeOffset(request.offset ?? "-1");
74
77
  query.set("offset", offset);
75
- if (request.handle !== undefined) query.set("handle", requireHandle(request.handle));
78
+ if (request.handle !== undefined) query.set("handle", requireShapeHandle(request.handle));
76
79
  else if (offset !== "-1") throw new TypeError("A shape handle is required after bootstrap.");
77
80
  if (request.live !== undefined && typeof request.live !== "boolean") throw new TypeError("live is a boolean.");
78
81
  if (request.live === true) {
@@ -94,10 +97,13 @@ export function decodeShapePage(value, headers) {
94
97
  const cursor = headers.get("tb-shape-cursor") ?? undefined;
95
98
  if (cursor !== undefined && !isLogOffset(cursor)) malformed("shape cursor");
96
99
  return Object.freeze({
97
- shapeId, handle: requireHandle(raw.handle),
100
+ shapeId, handle: requireShapeHandle(raw.handle),
98
101
  offset: requireShapeOffset(raw.offset), nextOffset: requireShapeOffset(raw.next_offset),
99
102
  upToDate: boolean(raw.up_to_date, "shape up_to_date"), cursor,
100
103
  etag: headers.get("etag") ?? undefined,
104
+ ...(raw.covered_offset === undefined ? {} : { coveredOffset: requireShapeOffset(raw.covered_offset) }),
105
+ ...(raw.values_at === undefined ? {} : { valuesAt: requireShapeOffset(raw.values_at) }),
106
+ ...(raw.cursor_resume === undefined ? {} : { cursorResume: string(raw.cursor_resume, "shape resume cursor") }),
101
107
  changes: Object.freeze(array(raw.changes, "shape changes").map(value => {
102
108
  const change = object(value, "shape change");
103
109
  return Object.freeze({ offset: u64(change.offset, "shape change offset"), ops: Object.freeze(array(change.ops, "shape operations").map(value => {
@@ -130,7 +136,7 @@ export async function readShape(transport, params, request, signal) {
130
136
  const target = new URL(location, source);
131
137
  if (target.origin !== source.origin || target.pathname !== source.pathname || target.username !== "" || target.password !== "" || target.hash !== "")
132
138
  malformed("shape redirect target");
133
- const handle = requireHandle(target.searchParams.get("handle"));
139
+ const handle = requireShapeHandle(target.searchParams.get("handle"));
134
140
  const offset = requireShapeOffset(target.searchParams.get("offset"));
135
141
  if (!offset.startsWith("snap.")) malformed("shape bootstrap offset");
136
142
  query.set("handle", handle);
package/src/v1/sql.js ADDED
@@ -0,0 +1,98 @@
1
+ import { array, boolean, malformed, object, string } from "./decode.js";
2
+ import { requireDocumentId, requireName } from "./ids.js";
3
+ /**
4
+ * @import { JsonObject, JsonValue } from "./json.js"
5
+ * @import { V1RequestOptions } from "./terrabase-v1.js"
6
+ * @import { V1Transport } from "./transport.js"
7
+ */
8
+
9
+ /** @typedef {{ readonly id: string; readonly value: JsonObject }} V1SqlRow */
10
+ /**
11
+ * SQL returns one bounded result, without a pagination cursor.
12
+ * @typedef {{ readonly matched: number; readonly truncated: boolean; readonly rows: readonly V1SqlRow[] }} V1SqlResult
13
+ */
14
+ /** @typedef {{ readonly field: string; readonly reason: string }} V1SqlIndexSuggestion */
15
+ /**
16
+ * @typedef {{
17
+ * readonly ok: boolean;
18
+ * readonly error: string | null;
19
+ * readonly collection: string | null;
20
+ * readonly hasLimit: boolean;
21
+ * readonly plan: readonly string[];
22
+ * readonly indexes: readonly string[];
23
+ * readonly suggestions: readonly V1SqlIndexSuggestion[];
24
+ * }} V1SqlCheck
25
+ */
26
+
27
+ /** @param {string} sql @param {V1RequestOptions} options @returns {string} */
28
+ function requestBody(sql, options) {
29
+ if (typeof sql !== "string" || sql.length > 16_384)
30
+ throw new TypeError("SQL must be a string of at most 16384 UTF-16 code units.");
31
+ if (options === null || typeof options !== "object" || Array.isArray(options))
32
+ throw new TypeError("SQL request options must be an object.");
33
+ for (const member of Object.keys(options))
34
+ if (member !== "signal") throw new TypeError(`Unknown SQL request option ${member}.`);
35
+ // SQL syntax, including empty statements, stays producer-owned: check returns ok:false.
36
+ return JSON.stringify({ sql });
37
+ }
38
+
39
+ /** @param {JsonValue | undefined} value @returns {V1SqlResult} */
40
+ function decodeResult(value) {
41
+ const raw = object(value, "SQL result");
42
+ const rows = array(raw.rows, "SQL rows").map(entry => {
43
+ const row = object(entry, "SQL row");
44
+ return Object.freeze({ id: requireDocumentId(row.id), value: object(row.value, "SQL row value") });
45
+ });
46
+ const matched = raw.matched;
47
+ if (typeof matched !== "number" || !Number.isInteger(matched) || matched < 0 || matched > 1000 || matched !== rows.length)
48
+ malformed("SQL matched count");
49
+ if (new Set(rows.map(row => row.id)).size !== rows.length) malformed("SQL row identities");
50
+ return Object.freeze({ matched, truncated: boolean(raw.truncated, "SQL truncation flag"), rows: Object.freeze(rows) });
51
+ }
52
+
53
+ /** @param {JsonValue | undefined} value @param {"checkSql" | "explainSql"} operation @returns {V1SqlCheck} */
54
+ function decodeCheck(value, operation) {
55
+ const raw = object(value, "SQL check");
56
+ const ok = boolean(raw.ok, "SQL check flag");
57
+ const error = raw.error === null ? null : string(raw.error, "SQL check error");
58
+ const collection = raw.collection === null ? null : requireName("collection", raw.collection);
59
+ const hasLimit = boolean(raw.hasLimit, "SQL limit flag");
60
+ const plan = array(raw.plan, "SQL plan").map(step => string(step, "SQL plan step"));
61
+ const indexes = array(raw.indexes, "SQL indexes").map(index => string(index, "SQL index"));
62
+ const suggestions = array(raw.suggestions, "SQL index suggestions").map(entry => {
63
+ const suggestion = object(entry, "SQL index suggestion");
64
+ return Object.freeze({ field: string(suggestion.field, "SQL index field"), reason: string(suggestion.reason, "SQL index reason") });
65
+ });
66
+ if (ok ? error !== null || collection === null : error === null || error === "" || collection !== null || hasLimit || plan.length > 0 || indexes.length > 0 || suggestions.length > 0)
67
+ malformed("SQL check outcome");
68
+ if (operation === "explainSql" && !ok) malformed("SQL explain outcome");
69
+ return Object.freeze({ ok, error, collection, hasLimit, plan: Object.freeze(plan), indexes: Object.freeze(indexes), suggestions: Object.freeze(suggestions) });
70
+ }
71
+
72
+ /**
73
+ * Runs the producer's read-only SQL subset; it never converts a legacy SQL builder.
74
+ * @param {V1Transport} transport
75
+ * @param {string} database
76
+ * @param {string} sql
77
+ * @param {V1RequestOptions} [options]
78
+ * @returns {Promise<V1SqlResult>}
79
+ */
80
+ export async function runSql(transport, database, sql, options = {}) {
81
+ const body = requestBody(sql, options);
82
+ return decodeResult((await transport.call("runSql", { database: requireName("database", database) }, { body, signal: options.signal })).value);
83
+ }
84
+
85
+ /**
86
+ * Check reports invalid SQL in a successful body; explain reports a producer HTTP error.
87
+ * @param {V1Transport} transport
88
+ * @param {string} database
89
+ * @param {string} sql
90
+ * @param {V1RequestOptions} [options]
91
+ * @param {"checkSql" | "explainSql"} [operation]
92
+ * @returns {Promise<V1SqlCheck>}
93
+ */
94
+ export async function checkSql(transport, database, sql, options = {}, operation = "checkSql") {
95
+ if (operation !== "checkSql" && operation !== "explainSql") throw new TypeError("SQL checks use checkSql or explainSql.");
96
+ const body = requestBody(sql, options);
97
+ return decodeCheck((await transport.call(operation, { database: requireName("database", database) }, { body, signal: options.signal })).value, operation);
98
+ }