@electric-sql/client 0.3.4 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/cjs/index.cjs +87 -54
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.browser.mjs +1 -1
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.legacy-esm.js +87 -54
- package/dist/index.legacy-esm.js.map +1 -1
- package/dist/index.mjs +87 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client.ts +104 -65
- package/src/parser.ts +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -177,7 +177,9 @@ declare class ShapeStream<T extends Row = Row> {
|
|
|
177
177
|
private upToDateSubscribers;
|
|
178
178
|
private lastOffset;
|
|
179
179
|
private messageParser;
|
|
180
|
+
private lastSyncedAt?;
|
|
180
181
|
isUpToDate: boolean;
|
|
182
|
+
private connected;
|
|
181
183
|
shapeId?: string;
|
|
182
184
|
constructor(options: ShapeStreamOptions);
|
|
183
185
|
start(): Promise<void>;
|
|
@@ -187,6 +189,9 @@ declare class ShapeStream<T extends Row = Row> {
|
|
|
187
189
|
private sendErrorToSubscribers;
|
|
188
190
|
subscribeOnceToUpToDate(callback: () => void | Promise<void>, error: (err: FetchError | Error) => void): () => void;
|
|
189
191
|
unsubscribeAllUpToDateSubscribers(): void;
|
|
192
|
+
/** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */
|
|
193
|
+
lastSynced(): number;
|
|
194
|
+
isConnected(): boolean;
|
|
190
195
|
private notifyUpToDateSubscribers;
|
|
191
196
|
private sendErrorToUpToDateSubscribers;
|
|
192
197
|
/**
|
|
@@ -235,7 +240,8 @@ declare class Shape<T extends Row = Row> {
|
|
|
235
240
|
error: FetchError | false;
|
|
236
241
|
private hasNotifiedSubscribersUpToDate;
|
|
237
242
|
constructor(stream: ShapeStream<T>);
|
|
238
|
-
|
|
243
|
+
lastSynced(): number;
|
|
244
|
+
isConnected(): boolean;
|
|
239
245
|
get value(): Promise<ShapeData<T>>;
|
|
240
246
|
get valueSync(): ShapeData<T>;
|
|
241
247
|
subscribe(callback: ShapeChangedCallback<T>): () => void;
|
package/dist/index.legacy-esm.js
CHANGED
|
@@ -92,7 +92,7 @@ var MessageParser = class {
|
|
|
92
92
|
}
|
|
93
93
|
parse(messages, schema) {
|
|
94
94
|
return JSON.parse(messages, (key, value) => {
|
|
95
|
-
if (key === `value` && typeof value === `object`) {
|
|
95
|
+
if (key === `value` && typeof value === `object` && value !== null) {
|
|
96
96
|
const row = value;
|
|
97
97
|
Object.keys(row).forEach((key2) => {
|
|
98
98
|
row[key2] = this.parseRow(key2, row[key2], schema);
|
|
@@ -204,7 +204,9 @@ var ShapeStream = class {
|
|
|
204
204
|
constructor(options) {
|
|
205
205
|
this.subscribers = /* @__PURE__ */ new Map();
|
|
206
206
|
this.upToDateSubscribers = /* @__PURE__ */ new Map();
|
|
207
|
+
// unix time
|
|
207
208
|
this.isUpToDate = false;
|
|
209
|
+
this.connected = false;
|
|
208
210
|
var _a, _b, _c;
|
|
209
211
|
this.validateOptions(options);
|
|
210
212
|
this.options = __spreadValues({ subscribe: true }, options);
|
|
@@ -219,58 +221,72 @@ var ShapeStream = class {
|
|
|
219
221
|
var _a;
|
|
220
222
|
this.isUpToDate = false;
|
|
221
223
|
const { url, where, signal } = this.options;
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (this.shapeId) {
|
|
230
|
-
fetchUrl.searchParams.set(`shape_id`, this.shapeId);
|
|
231
|
-
}
|
|
232
|
-
let response;
|
|
233
|
-
try {
|
|
234
|
-
const maybeResponse = await this.fetchWithBackoff(fetchUrl);
|
|
235
|
-
if (maybeResponse) response = maybeResponse;
|
|
236
|
-
else break;
|
|
237
|
-
} catch (e) {
|
|
238
|
-
if (!(e instanceof FetchError)) throw e;
|
|
239
|
-
if (e.status == 409) {
|
|
240
|
-
const newShapeId = e.headers[`x-electric-shape-id`];
|
|
241
|
-
this.reset(newShapeId);
|
|
242
|
-
this.publish(e.json);
|
|
243
|
-
continue;
|
|
244
|
-
} else if (e.status >= 400 && e.status < 500) {
|
|
245
|
-
this.sendErrorToUpToDateSubscribers(e);
|
|
246
|
-
this.sendErrorToSubscribers(e);
|
|
247
|
-
throw e;
|
|
224
|
+
try {
|
|
225
|
+
while (!(signal == null ? void 0 : signal.aborted) && !this.isUpToDate || this.options.subscribe) {
|
|
226
|
+
const fetchUrl = new URL(url);
|
|
227
|
+
if (where) fetchUrl.searchParams.set(`where`, where);
|
|
228
|
+
fetchUrl.searchParams.set(`offset`, this.lastOffset);
|
|
229
|
+
if (this.isUpToDate) {
|
|
230
|
+
fetchUrl.searchParams.set(`live`, `true`);
|
|
248
231
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
232
|
+
if (this.shapeId) {
|
|
233
|
+
fetchUrl.searchParams.set(`shape_id`, this.shapeId);
|
|
234
|
+
}
|
|
235
|
+
let response;
|
|
236
|
+
try {
|
|
237
|
+
const maybeResponse = await this.fetchWithBackoff(fetchUrl);
|
|
238
|
+
if (maybeResponse) response = maybeResponse;
|
|
239
|
+
else break;
|
|
240
|
+
} catch (e) {
|
|
241
|
+
if (!(e instanceof FetchError)) throw e;
|
|
242
|
+
if (e.status == 400) {
|
|
243
|
+
this.reset();
|
|
244
|
+
this.publish(e.json);
|
|
245
|
+
continue;
|
|
246
|
+
} else if (e.status == 409) {
|
|
247
|
+
const newShapeId = e.headers[`x-electric-shape-id`];
|
|
248
|
+
this.reset(newShapeId);
|
|
249
|
+
this.publish(e.json);
|
|
250
|
+
continue;
|
|
251
|
+
} else if (e.status >= 400 && e.status < 500) {
|
|
252
|
+
this.sendErrorToUpToDateSubscribers(e);
|
|
253
|
+
this.sendErrorToSubscribers(e);
|
|
254
|
+
throw e;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const { headers, status } = response;
|
|
258
|
+
const shapeId = headers.get(`X-Electric-Shape-Id`);
|
|
259
|
+
if (shapeId) {
|
|
260
|
+
this.shapeId = shapeId;
|
|
261
|
+
}
|
|
262
|
+
const lastOffset = headers.get(`X-Electric-Chunk-Last-Offset`);
|
|
263
|
+
if (lastOffset) {
|
|
264
|
+
this.lastOffset = lastOffset;
|
|
265
|
+
}
|
|
266
|
+
const getSchema = () => {
|
|
267
|
+
const schemaHeader = headers.get(`X-Electric-Schema`);
|
|
268
|
+
return schemaHeader ? JSON.parse(schemaHeader) : {};
|
|
269
|
+
};
|
|
270
|
+
this.schema = (_a = this.schema) != null ? _a : getSchema();
|
|
271
|
+
const messages = status === 204 ? `[]` : await response.text();
|
|
272
|
+
if (status === 204) {
|
|
273
|
+
this.lastSyncedAt = Date.now();
|
|
274
|
+
}
|
|
275
|
+
const batch = this.messageParser.parse(messages, this.schema);
|
|
276
|
+
if (batch.length > 0) {
|
|
277
|
+
const lastMessage = batch[batch.length - 1];
|
|
278
|
+
if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date`) {
|
|
279
|
+
this.lastSyncedAt = Date.now();
|
|
280
|
+
if (!this.isUpToDate) {
|
|
281
|
+
this.isUpToDate = true;
|
|
282
|
+
this.notifyUpToDateSubscribers();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
this.publish(batch);
|
|
271
286
|
}
|
|
272
|
-
this.publish(batch);
|
|
273
287
|
}
|
|
288
|
+
} finally {
|
|
289
|
+
this.connected = false;
|
|
274
290
|
}
|
|
275
291
|
}
|
|
276
292
|
subscribe(callback, onError) {
|
|
@@ -304,6 +320,14 @@ var ShapeStream = class {
|
|
|
304
320
|
unsubscribeAllUpToDateSubscribers() {
|
|
305
321
|
this.upToDateSubscribers.clear();
|
|
306
322
|
}
|
|
323
|
+
/** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */
|
|
324
|
+
lastSynced() {
|
|
325
|
+
if (this.lastSyncedAt === void 0) return Infinity;
|
|
326
|
+
return Date.now() - this.lastSyncedAt;
|
|
327
|
+
}
|
|
328
|
+
isConnected() {
|
|
329
|
+
return this.connected;
|
|
330
|
+
}
|
|
307
331
|
notifyUpToDateSubscribers() {
|
|
308
332
|
this.upToDateSubscribers.forEach(([callback]) => {
|
|
309
333
|
callback();
|
|
@@ -322,6 +346,7 @@ var ShapeStream = class {
|
|
|
322
346
|
this.lastOffset = `-1`;
|
|
323
347
|
this.shapeId = shapeId;
|
|
324
348
|
this.isUpToDate = false;
|
|
349
|
+
this.connected = false;
|
|
325
350
|
this.schema = void 0;
|
|
326
351
|
}
|
|
327
352
|
validateOptions(options) {
|
|
@@ -347,9 +372,14 @@ var ShapeStream = class {
|
|
|
347
372
|
while (true) {
|
|
348
373
|
try {
|
|
349
374
|
const result = await this.fetchClient(url.toString(), { signal });
|
|
350
|
-
if (result.ok)
|
|
351
|
-
|
|
375
|
+
if (result.ok) {
|
|
376
|
+
if (this.options.subscribe) {
|
|
377
|
+
this.connected = true;
|
|
378
|
+
}
|
|
379
|
+
return result;
|
|
380
|
+
} else throw await FetchError.fromResponse(result, url.toString());
|
|
352
381
|
} catch (e) {
|
|
382
|
+
this.connected = false;
|
|
353
383
|
if (signal == null ? void 0 : signal.aborted) {
|
|
354
384
|
return void 0;
|
|
355
385
|
} else if (e instanceof FetchError && e.status >= 400 && e.status < 500) {
|
|
@@ -382,8 +412,11 @@ var Shape = class {
|
|
|
382
412
|
}
|
|
383
413
|
);
|
|
384
414
|
}
|
|
385
|
-
|
|
386
|
-
return this.stream.
|
|
415
|
+
lastSynced() {
|
|
416
|
+
return this.stream.lastSynced();
|
|
417
|
+
}
|
|
418
|
+
isConnected() {
|
|
419
|
+
return this.stream.isConnected();
|
|
387
420
|
}
|
|
388
421
|
get value() {
|
|
389
422
|
return new Promise((resolve) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/parser.ts","../src/helpers.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Row, Schema, Value } from './types'\n\ntype NullToken = null | `NULL`\ntype Token = Exclude<string, NullToken>\ntype NullableToken = Token | NullToken\nexport type ParseFunction = (\n value: Token,\n additionalInfo?: Omit<ColumnInfo, `type` | `dims`>\n) => Value\ntype NullableParseFunction = (\n value: NullableToken,\n additionalInfo?: Omit<ColumnInfo, `type` | `dims`>\n) => Value\nexport type Parser = { [key: string]: ParseFunction }\n\nconst parseNumber = (value: string) => Number(value)\nconst parseBool = (value: string) => value === `true` || value === `t`\nconst parseBigInt = (value: string) => BigInt(value)\nconst parseJson = (value: string) => JSON.parse(value)\nconst identityParser: ParseFunction = (v: string) => v\n\nexport const defaultParser: Parser = {\n int2: parseNumber,\n int4: parseNumber,\n int8: parseBigInt,\n bool: parseBool,\n float4: parseNumber,\n float8: parseNumber,\n json: parseJson,\n jsonb: parseJson,\n}\n\n// Taken from: https://github.com/electric-sql/pglite/blob/main/packages/pglite/src/types.ts#L233-L279\nexport function pgArrayParser(value: Token, parser?: ParseFunction): Value {\n let i = 0\n let char = null\n let str = ``\n let quoted = false\n let last = 0\n let p: string | undefined = undefined\n\n function loop(x: string): Value[] {\n const xs = []\n for (; i < x.length; i++) {\n char = x[i]\n if (quoted) {\n if (char === `\\\\`) {\n str += x[++i]\n } else if (char === `\"`) {\n xs.push(parser ? parser(str) : str)\n str = ``\n quoted = x[i + 1] === `\"`\n last = i + 2\n } else {\n str += char\n }\n } else if (char === `\"`) {\n quoted = true\n } else if (char === `{`) {\n last = ++i\n xs.push(loop(x))\n } else if (char === `}`) {\n quoted = false\n last < i &&\n xs.push(parser ? parser(x.slice(last, i)) : x.slice(last, i))\n last = i + 1\n break\n } else if (char === `,` && p !== `}` && p !== `\"`) {\n xs.push(parser ? parser(x.slice(last, i)) : x.slice(last, i))\n last = i + 1\n }\n p = char\n }\n last < i &&\n xs.push(parser ? parser(x.slice(last, i + 1)) : x.slice(last, i + 1))\n return xs\n }\n\n return loop(value)[0]\n}\n\nexport class MessageParser<T extends Row> {\n private parser: Parser\n constructor(parser?: Parser) {\n // Merge the provided parser with the default parser\n // to use the provided parser whenever defined\n // and otherwise fall back to the default parser\n this.parser = { ...defaultParser, ...parser }\n }\n\n parse(messages: string, schema: Schema): Message<T>[] {\n return JSON.parse(messages, (key, value) => {\n // typeof value === `object` is needed because\n // there could be a column named `value`\n // and the value associated to that column will be a string\n if (key === `value` && typeof value === `object`) {\n // Parse the row values\n const row = value as Record<string, Value>\n Object.keys(row).forEach((key) => {\n row[key] = this.parseRow(key, row[key] as NullableToken, schema)\n })\n }\n return value\n }) as Message<T>[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: NullableToken, schema: Schema): Value {\n const columnInfo = schema[key]\n if (!columnInfo) {\n // We don't have information about the value\n // so we just return it\n return value\n }\n\n // Copy the object but don't include `dimensions` and `type`\n const { type: typ, dims: dimensions, ...additionalInfo } = columnInfo\n\n // Pick the right parser for the type\n // and support parsing null values if needed\n // if no parser is provided for the given type, just return the value as is\n const typeParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typeParser, columnInfo, key)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n const nullablePgArrayParser = makeNullableParser(\n (value, _) => pgArrayParser(value, parser),\n columnInfo,\n key\n )\n return nullablePgArrayParser(value)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n columnInfo: ColumnInfo,\n columnName?: string\n): NullableParseFunction {\n const isNullable = !(columnInfo.not_null ?? false)\n // The sync service contains `null` value for a column whose value is NULL\n // but if the column value is an array that contains a NULL value\n // then it will be included in the array string as `NULL`, e.g.: `\"{1,NULL,3}\"`\n return (value: NullableToken) => {\n if (isPgNull(value)) {\n if (!isNullable) {\n throw new Error(`Column ${columnName ?? `unknown`} is not nullable`)\n }\n return null\n }\n return parser(value, columnInfo)\n }\n}\n\nfunction isPgNull(value: NullableToken): value is NullToken {\n return value === null || value === `NULL`\n}\n","import { ChangeMessage, ControlMessage, Message, Row } from './types'\n\n/**\n * Type guard for checking {@link Message} is {@link ChangeMessage}.\n *\n * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)\n * for information on how to use type guards.\n *\n * @param message - the message to check\n * @returns true if the message is a {@link ChangeMessage}\n *\n * @example\n * ```ts\n * if (isChangeMessage(message)) {\n * const msgChng: ChangeMessage = message // Ok\n * const msgCtrl: ControlMessage = message // Err, type mismatch\n * }\n * ```\n */\nexport function isChangeMessage<T extends Row = Row>(\n message: Message<T>\n): message is ChangeMessage<T> {\n return `key` in message\n}\n\n/**\n * Type guard for checking {@link Message} is {@link ControlMessage}.\n *\n * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)\n * for information on how to use type guards.\n *\n * @param message - the message to check\n * @returns true if the message is a {@link ControlMessage}\n *\n * * @example\n * ```ts\n * if (isControlMessage(message)) {\n * const msgChng: ChangeMessage = message // Err, type mismatch\n * const msgCtrl: ControlMessage = message // Ok\n * }\n * ```\n */\nexport function isControlMessage<T extends Row = Row>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Offset, Schema, Row } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData<T extends Row = Row> = Map<string, T>\nexport type ShapeChangedCallback<T extends Row = Row> = (\n value: ShapeData<T>\n) => void\n\nexport interface BackoffOptions {\n initialDelay: number\n maxDelay: number\n multiplier: number\n}\n\nexport const BackoffDefaults = {\n initialDelay: 100,\n maxDelay: 10_000,\n multiplier: 1.3,\n}\n\n/**\n * Options for constructing a ShapeStream.\n */\nexport interface ShapeStreamOptions {\n /**\n * The full URL to where the Shape is hosted. This can either be the Electric server\n * directly or a proxy. E.g. for a local Electric instance, you might set `http://localhost:3000/v1/shape/foo`\n */\n url: string\n /**\n * where clauses for the shape.\n */\n where?: string\n /**\n * The \"offset\" on the shape log. This is typically not set as the ShapeStream\n * will handle this automatically. A common scenario where you might pass an offset\n * is if you're maintaining a local cache of the log. If you've gone offline\n * and are re-starting a ShapeStream to catch-up to the latest state of the Shape,\n * you'd pass in the last offset and shapeId you'd seen from the Electric server\n * so it knows at what point in the shape to catch you up from.\n */\n offset?: Offset\n /**\n * Similar to `offset`, this isn't typically used unless you're maintaining\n * a cache of the shape log.\n */\n shapeId?: string\n backoffOptions?: BackoffOptions\n /**\n * Automatically fetch updates to the Shape. If you just want to sync the current\n * shape and stop, pass false.\n */\n subscribe?: boolean\n signal?: AbortSignal\n fetchClient?: typeof fetch\n parser?: Parser\n}\n\n/**\n * Receives batches of `messages`, puts them on a queue and processes\n * them asynchronously by passing to a registered callback function.\n *\n * @constructor\n * @param {(messages: Message[]) => void} callback function\n */\nclass MessageProcessor<T extends Row = Row> {\n private messageQueue: Message<T>[][] = []\n private isProcessing = false\n private callback: (messages: Message<T>[]) => void | Promise<void>\n\n constructor(callback: (messages: Message<T>[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message<T>[]) {\n this.messageQueue.push(messages)\n\n if (!this.isProcessing) {\n this.processQueue()\n }\n }\n\n private async processQueue() {\n this.isProcessing = true\n\n while (this.messageQueue.length > 0) {\n const messages = this.messageQueue.shift()!\n\n await this.callback(messages)\n }\n\n this.isProcessing = false\n }\n}\n\nexport class FetchError extends Error {\n status: number\n text?: string\n json?: object\n headers: Record<string, string>\n\n constructor(\n status: number,\n text: string | undefined,\n json: object | undefined,\n headers: Record<string, string>,\n public url: string,\n message?: string\n ) {\n super(\n message ||\n `HTTP Error ${status} at ${url}: ${text ?? JSON.stringify(json)}`\n )\n this.name = `FetchError`\n this.status = status\n this.text = text\n this.json = json\n this.headers = headers\n }\n\n static async fromResponse(\n response: Response,\n url: string\n ): Promise<FetchError> {\n const status = response.status\n const headers = Object.fromEntries([...response.headers.entries()])\n let text: string | undefined = undefined\n let json: object | undefined = undefined\n\n const contentType = response.headers.get(`content-type`)\n if (contentType && contentType.includes(`application/json`)) {\n json = (await response.json()) as object\n } else {\n text = await response.text()\n }\n\n return new FetchError(status, text, json, headers, url)\n }\n}\n\n/**\n * Reads updates to a shape from Electric using HTTP requests and long polling. Notifies subscribers\n * when new messages come in. Doesn't maintain any history of the\n * log but does keep track of the offset position and is the best way\n * to consume the HTTP `GET /v1/shape` api.\n *\n * @constructor\n * @param {ShapeStreamOptions} options - configure the shape stream\n * @example\n * Register a callback function to subscribe to the messages.\n * ```\n * const stream = new ShapeStream(options)\n * stream.subscribe(messages => {\n * // messages is 1 or more row updates\n * })\n * ```\n *\n * To abort the stream, abort the `signal`\n * passed in via the `ShapeStreamOptions`.\n * ```\n * const aborter = new AbortController()\n * const issueStream = new ShapeStream({\n * url: `${BASE_URL}/${table}`\n * subscribe: true,\n * signal: aborter.signal,\n * })\n * // Later...\n * aborter.abort()\n * ```\n */\nexport class ShapeStream<T extends Row = Row> {\n private options: ShapeStreamOptions\n private backoffOptions: BackoffOptions\n private fetchClient: typeof fetch\n private schema?: Schema\n\n private subscribers = new Map<\n number,\n [MessageProcessor<T>, ((error: Error) => void) | undefined]\n >()\n private upToDateSubscribers = new Map<\n number,\n [() => void, (error: FetchError | Error) => void]\n >()\n\n private lastOffset: Offset\n private messageParser: MessageParser<T>\n public isUpToDate: boolean = false\n\n shapeId?: string\n\n constructor(options: ShapeStreamOptions) {\n this.validateOptions(options)\n this.options = { subscribe: true, ...options }\n this.lastOffset = this.options.offset ?? `-1`\n this.shapeId = this.options.shapeId\n this.messageParser = new MessageParser<T>(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: Parameters<typeof fetch>) => fetch(...args))\n\n this.start()\n }\n\n async start() {\n this.isUpToDate = false\n\n const { url, where, signal } = this.options\n\n while ((!signal?.aborted && !this.isUpToDate) || this.options.subscribe) {\n const fetchUrl = new URL(url)\n if (where) fetchUrl.searchParams.set(`where`, where)\n fetchUrl.searchParams.set(`offset`, this.lastOffset)\n\n if (this.isUpToDate) {\n fetchUrl.searchParams.set(`live`, `true`)\n }\n\n if (this.shapeId) {\n // This should probably be a header for better cache breaking?\n fetchUrl.searchParams.set(`shape_id`, this.shapeId!)\n }\n\n let response!: Response\n\n try {\n const maybeResponse = await this.fetchWithBackoff(fetchUrl)\n if (maybeResponse) response = maybeResponse\n else break\n } catch (e) {\n if (!(e instanceof FetchError)) throw e // should never happen\n if (e.status == 409) {\n // Upon receiving a 409, we should start from scratch\n // with the newly provided shape ID\n const newShapeId = e.headers[`x-electric-shape-id`]\n this.reset(newShapeId)\n this.publish(e.json as Message<T>[])\n continue\n } else if (e.status >= 400 && e.status < 500) {\n // Notify subscribers\n this.sendErrorToUpToDateSubscribers(e)\n this.sendErrorToSubscribers(e)\n\n // 400 errors are not actionable without additional user input, so we're throwing them.\n throw e\n }\n }\n\n const { headers, status } = response\n const shapeId = headers.get(`X-Electric-Shape-Id`)\n if (shapeId) {\n this.shapeId = shapeId\n }\n\n const lastOffset = headers.get(`X-Electric-Chunk-Last-Offset`)\n if (lastOffset) {\n this.lastOffset = lastOffset as Offset\n }\n\n const getSchema = (): Schema => {\n const schemaHeader = headers.get(`X-Electric-Schema`)\n return schemaHeader ? JSON.parse(schemaHeader) : {}\n }\n this.schema = this.schema ?? getSchema()\n\n const messages = status === 204 ? `[]` : await response.text()\n\n const batch = this.messageParser.parse(messages, this.schema)\n\n // Update isUpToDate\n if (batch.length > 0) {\n const lastMessage = batch[batch.length - 1]\n if (\n isControlMessage(lastMessage) &&\n lastMessage.headers.control === `up-to-date` &&\n !this.isUpToDate\n ) {\n this.isUpToDate = true\n this.notifyUpToDateSubscribers()\n }\n\n this.publish(batch)\n }\n }\n }\n\n subscribe(\n callback: (messages: Message<T>[]) => void | Promise<void>,\n onError?: (error: FetchError | Error) => void\n ) {\n const subscriptionId = Math.random()\n const subscriber = new MessageProcessor(callback)\n\n this.subscribers.set(subscriptionId, [subscriber, onError])\n\n return () => {\n this.subscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAll(): void {\n this.subscribers.clear()\n }\n\n private publish(messages: Message<T>[]) {\n this.subscribers.forEach(([subscriber, _]) => {\n subscriber.process(messages)\n })\n }\n\n private sendErrorToSubscribers(error: Error) {\n this.subscribers.forEach(([_, errorFn]) => {\n errorFn?.(error)\n })\n }\n\n subscribeOnceToUpToDate(\n callback: () => void | Promise<void>,\n error: (err: FetchError | Error) => void\n ) {\n const subscriptionId = Math.random()\n\n this.upToDateSubscribers.set(subscriptionId, [callback, error])\n\n return () => {\n this.upToDateSubscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAllUpToDateSubscribers(): void {\n this.upToDateSubscribers.clear()\n }\n\n private notifyUpToDateSubscribers() {\n this.upToDateSubscribers.forEach(([callback]) => {\n callback()\n })\n }\n\n private sendErrorToUpToDateSubscribers(error: FetchError | Error) {\n // eslint-disable-next-line\n this.upToDateSubscribers.forEach(([_, errorCallback]) =>\n errorCallback(error)\n )\n }\n\n /**\n * Resets the state of the stream, optionally with a provided\n * shape ID\n */\n private reset(shapeId?: string) {\n this.lastOffset = `-1`\n this.shapeId = shapeId\n this.isUpToDate = false\n this.schema = undefined\n }\n\n private validateOptions(options: ShapeStreamOptions): void {\n if (!options.url) {\n throw new Error(`Invalid shape option. It must provide the url`)\n }\n if (options.signal && !(options.signal instanceof AbortSignal)) {\n throw new Error(\n `Invalid signal option. It must be an instance of AbortSignal.`\n )\n }\n\n if (\n options.offset !== undefined &&\n options.offset !== `-1` &&\n !options.shapeId\n ) {\n throw new Error(\n `shapeId is required if this isn't an initial fetch (i.e. offset > -1)`\n )\n }\n }\n\n private async fetchWithBackoff(url: URL) {\n const { initialDelay, maxDelay, multiplier } = this.backoffOptions\n const signal = this.options.signal\n\n let delay = initialDelay\n let attempt = 0\n\n // eslint-disable-next-line no-constant-condition -- we're retrying with a lag until we get a non-500 response or the abort signal is triggered\n while (true) {\n try {\n const result = await this.fetchClient(url.toString(), { signal })\n if (result.ok) return result\n else throw await FetchError.fromResponse(result, url.toString())\n } catch (e) {\n if (signal?.aborted) {\n return undefined\n } else if (\n e instanceof FetchError &&\n e.status >= 400 &&\n e.status < 500\n ) {\n // Any client errors cannot be backed off on, leave it to the caller to handle.\n throw e\n } else {\n // Exponentially backoff on errors.\n // Wait for the current delay duration\n await new Promise((resolve) => setTimeout(resolve, delay))\n\n // Increase the delay for the next attempt\n delay = Math.min(delay * multiplier, maxDelay)\n\n attempt++\n console.log(`Retry attempt #${attempt} after ${delay}ms`)\n }\n }\n }\n }\n}\n\n/**\n * A Shape is an object that subscribes to a shape log,\n * keeps a materialised shape `.value` in memory and\n * notifies subscribers when the value has changed.\n *\n * It can be used without a framework and as a primitive\n * to simplify developing framework hooks.\n *\n * @constructor\n * @param {ShapeStream<T extends Row>} - the underlying shape stream\n * @example\n * ```\n * const shapeStream = new ShapeStream<{ foo: number }>(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\n * ```\n *\n * `value` returns a promise that resolves the Shape data once the Shape has been\n * fully loaded (and when resuming from being offline):\n *\n * const value = await shape.value\n *\n * `valueSync` returns the current data synchronously:\n *\n * const value = shape.valueSync\n *\n * Subscribe to updates. Called whenever the shape updates in Postgres.\n *\n * shape.subscribe(shapeData => {\n * console.log(shapeData)\n * })\n */\nexport class Shape<T extends Row = Row> {\n private stream: ShapeStream<T>\n\n private data: ShapeData<T> = new Map()\n private subscribers = new Map<number, ShapeChangedCallback<T>>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream<T>) {\n this.stream = stream\n this.stream.subscribe(this.process.bind(this), this.handleError.bind(this))\n const unsubscribe = this.stream.subscribeOnceToUpToDate(\n () => {\n unsubscribe()\n },\n (e) => {\n this.handleError(e)\n throw e\n }\n )\n }\n\n get isUpToDate(): boolean {\n return this.stream.isUpToDate\n }\n\n get value(): Promise<ShapeData<T>> {\n return new Promise((resolve) => {\n if (this.stream.isUpToDate) {\n resolve(this.valueSync)\n } else {\n const unsubscribe = this.stream.subscribeOnceToUpToDate(\n () => {\n unsubscribe()\n resolve(this.valueSync)\n },\n (e) => {\n throw e\n }\n )\n }\n })\n }\n\n get valueSync() {\n return this.data\n }\n\n subscribe(callback: ShapeChangedCallback<T>): () => void {\n const subscriptionId = Math.random()\n\n this.subscribers.set(subscriptionId, callback)\n\n return () => {\n this.subscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAll(): void {\n this.subscribers.clear()\n }\n\n get numSubscribers() {\n return this.subscribers.size\n }\n\n private process(messages: Message<T>[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (isChangeMessage(message)) {\n dataMayHaveChanged = [`insert`, `update`, `delete`].includes(\n message.headers.operation\n )\n\n switch (message.headers.operation) {\n case `insert`:\n this.data.set(message.key, message.value)\n break\n case `update`:\n this.data.set(message.key, {\n ...this.data.get(message.key)!,\n ...message.value,\n })\n break\n case `delete`:\n this.data.delete(message.key)\n break\n }\n }\n\n if (isControlMessage(message)) {\n switch (message.headers.control) {\n case `up-to-date`:\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n break\n case `must-refetch`:\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\n break\n }\n }\n })\n\n // Always notify subscribers when the Shape first is up to date.\n // FIXME this would be cleaner with a simple state machine.\n if (newlyUpToDate || (isUpToDate && dataMayHaveChanged)) {\n this.hasNotifiedSubscribersUpToDate = true\n this.notify()\n }\n }\n\n private handleError(e: Error): void {\n if (e instanceof FetchError) {\n this.error = e\n this.notify()\n }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback(this.valueSync)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,IAAM,cAAc,CAAC,UAAkB,OAAO,KAAK;AACnD,IAAM,YAAY,CAAC,UAAkB,UAAU,UAAU,UAAU;AACnE,IAAM,cAAc,CAAC,UAAkB,OAAO,KAAK;AACnD,IAAM,YAAY,CAAC,UAAkB,KAAK,MAAM,KAAK;AACrD,IAAM,iBAAgC,CAAC,MAAc;AAE9C,IAAM,gBAAwB;AAAA,EACnC,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AACT;AAGO,SAAS,cAAc,OAAc,QAA+B;AACzE,MAAI,IAAI;AACR,MAAI,OAAO;AACX,MAAI,MAAM;AACV,MAAI,SAAS;AACb,MAAI,OAAO;AACX,MAAI,IAAwB;AAE5B,WAAS,KAAK,GAAoB;AAChC,UAAM,KAAK,CAAC;AACZ,WAAO,IAAI,EAAE,QAAQ,KAAK;AACxB,aAAO,EAAE,CAAC;AACV,UAAI,QAAQ;AACV,YAAI,SAAS,MAAM;AACjB,iBAAO,EAAE,EAAE,CAAC;AAAA,QACd,WAAW,SAAS,KAAK;AACvB,aAAG,KAAK,SAAS,OAAO,GAAG,IAAI,GAAG;AAClC,gBAAM;AACN,mBAAS,EAAE,IAAI,CAAC,MAAM;AACtB,iBAAO,IAAI;AAAA,QACb,OAAO;AACL,iBAAO;AAAA,QACT;AAAA,MACF,WAAW,SAAS,KAAK;AACvB,iBAAS;AAAA,MACX,WAAW,SAAS,KAAK;AACvB,eAAO,EAAE;AACT,WAAG,KAAK,KAAK,CAAC,CAAC;AAAA,MACjB,WAAW,SAAS,KAAK;AACvB,iBAAS;AACT,eAAO,KACL,GAAG,KAAK,SAAS,OAAO,EAAE,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,CAAC;AAAA,EACH;AACF;","names":["key","value"]}
|
|
1
|
+
{"version":3,"sources":["../src/parser.ts","../src/helpers.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Row, Schema, Value } from './types'\n\ntype NullToken = null | `NULL`\ntype Token = Exclude<string, NullToken>\ntype NullableToken = Token | NullToken\nexport type ParseFunction = (\n value: Token,\n additionalInfo?: Omit<ColumnInfo, `type` | `dims`>\n) => Value\ntype NullableParseFunction = (\n value: NullableToken,\n additionalInfo?: Omit<ColumnInfo, `type` | `dims`>\n) => Value\nexport type Parser = { [key: string]: ParseFunction }\n\nconst parseNumber = (value: string) => Number(value)\nconst parseBool = (value: string) => value === `true` || value === `t`\nconst parseBigInt = (value: string) => BigInt(value)\nconst parseJson = (value: string) => JSON.parse(value)\nconst identityParser: ParseFunction = (v: string) => v\n\nexport const defaultParser: Parser = {\n int2: parseNumber,\n int4: parseNumber,\n int8: parseBigInt,\n bool: parseBool,\n float4: parseNumber,\n float8: parseNumber,\n json: parseJson,\n jsonb: parseJson,\n}\n\n// Taken from: https://github.com/electric-sql/pglite/blob/main/packages/pglite/src/types.ts#L233-L279\nexport function pgArrayParser(value: Token, parser?: ParseFunction): Value {\n let i = 0\n let char = null\n let str = ``\n let quoted = false\n let last = 0\n let p: string | undefined = undefined\n\n function loop(x: string): Value[] {\n const xs = []\n for (; i < x.length; i++) {\n char = x[i]\n if (quoted) {\n if (char === `\\\\`) {\n str += x[++i]\n } else if (char === `\"`) {\n xs.push(parser ? parser(str) : str)\n str = ``\n quoted = x[i + 1] === `\"`\n last = i + 2\n } else {\n str += char\n }\n } else if (char === `\"`) {\n quoted = true\n } else if (char === `{`) {\n last = ++i\n xs.push(loop(x))\n } else if (char === `}`) {\n quoted = false\n last < i &&\n xs.push(parser ? parser(x.slice(last, i)) : x.slice(last, i))\n last = i + 1\n break\n } else if (char === `,` && p !== `}` && p !== `\"`) {\n xs.push(parser ? parser(x.slice(last, i)) : x.slice(last, i))\n last = i + 1\n }\n p = char\n }\n last < i &&\n xs.push(parser ? parser(x.slice(last, i + 1)) : x.slice(last, i + 1))\n return xs\n }\n\n return loop(value)[0]\n}\n\nexport class MessageParser<T extends Row> {\n private parser: Parser\n constructor(parser?: Parser) {\n // Merge the provided parser with the default parser\n // to use the provided parser whenever defined\n // and otherwise fall back to the default parser\n this.parser = { ...defaultParser, ...parser }\n }\n\n parse(messages: string, schema: Schema): Message<T>[] {\n return JSON.parse(messages, (key, value) => {\n // typeof value === `object` && value !== null\n // is needed because there could be a column named `value`\n // and the value associated to that column will be a string or null.\n // But `typeof null === 'object'` so we need to make an explicit check.\n if (key === `value` && typeof value === `object` && value !== null) {\n // Parse the row values\n const row = value as Record<string, Value>\n Object.keys(row).forEach((key) => {\n row[key] = this.parseRow(key, row[key] as NullableToken, schema)\n })\n }\n return value\n }) as Message<T>[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: NullableToken, schema: Schema): Value {\n const columnInfo = schema[key]\n if (!columnInfo) {\n // We don't have information about the value\n // so we just return it\n return value\n }\n\n // Copy the object but don't include `dimensions` and `type`\n const { type: typ, dims: dimensions, ...additionalInfo } = columnInfo\n\n // Pick the right parser for the type\n // and support parsing null values if needed\n // if no parser is provided for the given type, just return the value as is\n const typeParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typeParser, columnInfo, key)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n const nullablePgArrayParser = makeNullableParser(\n (value, _) => pgArrayParser(value, parser),\n columnInfo,\n key\n )\n return nullablePgArrayParser(value)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n columnInfo: ColumnInfo,\n columnName?: string\n): NullableParseFunction {\n const isNullable = !(columnInfo.not_null ?? false)\n // The sync service contains `null` value for a column whose value is NULL\n // but if the column value is an array that contains a NULL value\n // then it will be included in the array string as `NULL`, e.g.: `\"{1,NULL,3}\"`\n return (value: NullableToken) => {\n if (isPgNull(value)) {\n if (!isNullable) {\n throw new Error(`Column ${columnName ?? `unknown`} is not nullable`)\n }\n return null\n }\n return parser(value, columnInfo)\n }\n}\n\nfunction isPgNull(value: NullableToken): value is NullToken {\n return value === null || value === `NULL`\n}\n","import { ChangeMessage, ControlMessage, Message, Row } from './types'\n\n/**\n * Type guard for checking {@link Message} is {@link ChangeMessage}.\n *\n * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)\n * for information on how to use type guards.\n *\n * @param message - the message to check\n * @returns true if the message is a {@link ChangeMessage}\n *\n * @example\n * ```ts\n * if (isChangeMessage(message)) {\n * const msgChng: ChangeMessage = message // Ok\n * const msgCtrl: ControlMessage = message // Err, type mismatch\n * }\n * ```\n */\nexport function isChangeMessage<T extends Row = Row>(\n message: Message<T>\n): message is ChangeMessage<T> {\n return `key` in message\n}\n\n/**\n * Type guard for checking {@link Message} is {@link ControlMessage}.\n *\n * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)\n * for information on how to use type guards.\n *\n * @param message - the message to check\n * @returns true if the message is a {@link ControlMessage}\n *\n * * @example\n * ```ts\n * if (isControlMessage(message)) {\n * const msgChng: ChangeMessage = message // Err, type mismatch\n * const msgCtrl: ControlMessage = message // Ok\n * }\n * ```\n */\nexport function isControlMessage<T extends Row = Row>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Offset, Schema, Row } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData<T extends Row = Row> = Map<string, T>\nexport type ShapeChangedCallback<T extends Row = Row> = (\n value: ShapeData<T>\n) => void\n\nexport interface BackoffOptions {\n initialDelay: number\n maxDelay: number\n multiplier: number\n}\n\nexport const BackoffDefaults = {\n initialDelay: 100,\n maxDelay: 10_000,\n multiplier: 1.3,\n}\n\n/**\n * Options for constructing a ShapeStream.\n */\nexport interface ShapeStreamOptions {\n /**\n * The full URL to where the Shape is hosted. This can either be the Electric server\n * directly or a proxy. E.g. for a local Electric instance, you might set `http://localhost:3000/v1/shape/foo`\n */\n url: string\n /**\n * where clauses for the shape.\n */\n where?: string\n /**\n * The \"offset\" on the shape log. This is typically not set as the ShapeStream\n * will handle this automatically. A common scenario where you might pass an offset\n * is if you're maintaining a local cache of the log. If you've gone offline\n * and are re-starting a ShapeStream to catch-up to the latest state of the Shape,\n * you'd pass in the last offset and shapeId you'd seen from the Electric server\n * so it knows at what point in the shape to catch you up from.\n */\n offset?: Offset\n /**\n * Similar to `offset`, this isn't typically used unless you're maintaining\n * a cache of the shape log.\n */\n shapeId?: string\n backoffOptions?: BackoffOptions\n /**\n * Automatically fetch updates to the Shape. If you just want to sync the current\n * shape and stop, pass false.\n */\n subscribe?: boolean\n signal?: AbortSignal\n fetchClient?: typeof fetch\n parser?: Parser\n}\n\n/**\n * Receives batches of `messages`, puts them on a queue and processes\n * them asynchronously by passing to a registered callback function.\n *\n * @constructor\n * @param {(messages: Message[]) => void} callback function\n */\nclass MessageProcessor<T extends Row = Row> {\n private messageQueue: Message<T>[][] = []\n private isProcessing = false\n private callback: (messages: Message<T>[]) => void | Promise<void>\n\n constructor(callback: (messages: Message<T>[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message<T>[]) {\n this.messageQueue.push(messages)\n\n if (!this.isProcessing) {\n this.processQueue()\n }\n }\n\n private async processQueue() {\n this.isProcessing = true\n\n while (this.messageQueue.length > 0) {\n const messages = this.messageQueue.shift()!\n\n await this.callback(messages)\n }\n\n this.isProcessing = false\n }\n}\n\nexport class FetchError extends Error {\n status: number\n text?: string\n json?: object\n headers: Record<string, string>\n\n constructor(\n status: number,\n text: string | undefined,\n json: object | undefined,\n headers: Record<string, string>,\n public url: string,\n message?: string\n ) {\n super(\n message ||\n `HTTP Error ${status} at ${url}: ${text ?? JSON.stringify(json)}`\n )\n this.name = `FetchError`\n this.status = status\n this.text = text\n this.json = json\n this.headers = headers\n }\n\n static async fromResponse(\n response: Response,\n url: string\n ): Promise<FetchError> {\n const status = response.status\n const headers = Object.fromEntries([...response.headers.entries()])\n let text: string | undefined = undefined\n let json: object | undefined = undefined\n\n const contentType = response.headers.get(`content-type`)\n if (contentType && contentType.includes(`application/json`)) {\n json = (await response.json()) as object\n } else {\n text = await response.text()\n }\n\n return new FetchError(status, text, json, headers, url)\n }\n}\n\n/**\n * Reads updates to a shape from Electric using HTTP requests and long polling. Notifies subscribers\n * when new messages come in. Doesn't maintain any history of the\n * log but does keep track of the offset position and is the best way\n * to consume the HTTP `GET /v1/shape` api.\n *\n * @constructor\n * @param {ShapeStreamOptions} options - configure the shape stream\n * @example\n * Register a callback function to subscribe to the messages.\n * ```\n * const stream = new ShapeStream(options)\n * stream.subscribe(messages => {\n * // messages is 1 or more row updates\n * })\n * ```\n *\n * To abort the stream, abort the `signal`\n * passed in via the `ShapeStreamOptions`.\n * ```\n * const aborter = new AbortController()\n * const issueStream = new ShapeStream({\n * url: `${BASE_URL}/${table}`\n * subscribe: true,\n * signal: aborter.signal,\n * })\n * // Later...\n * aborter.abort()\n * ```\n */\nexport class ShapeStream<T extends Row = Row> {\n private options: ShapeStreamOptions\n private backoffOptions: BackoffOptions\n private fetchClient: typeof fetch\n private schema?: Schema\n\n private subscribers = new Map<\n number,\n [MessageProcessor<T>, ((error: Error) => void) | undefined]\n >()\n private upToDateSubscribers = new Map<\n number,\n [() => void, (error: FetchError | Error) => void]\n >()\n\n private lastOffset: Offset\n private messageParser: MessageParser<T>\n private lastSyncedAt?: number // unix time\n public isUpToDate: boolean = false\n private connected: boolean = false\n\n shapeId?: string\n\n constructor(options: ShapeStreamOptions) {\n this.validateOptions(options)\n this.options = { subscribe: true, ...options }\n this.lastOffset = this.options.offset ?? `-1`\n this.shapeId = this.options.shapeId\n this.messageParser = new MessageParser<T>(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: Parameters<typeof fetch>) => fetch(...args))\n\n this.start()\n }\n\n async start() {\n this.isUpToDate = false\n\n const { url, where, signal } = this.options\n\n try {\n while ((!signal?.aborted && !this.isUpToDate) || this.options.subscribe) {\n const fetchUrl = new URL(url)\n if (where) fetchUrl.searchParams.set(`where`, where)\n fetchUrl.searchParams.set(`offset`, this.lastOffset)\n\n if (this.isUpToDate) {\n fetchUrl.searchParams.set(`live`, `true`)\n }\n\n if (this.shapeId) {\n // This should probably be a header for better cache breaking?\n fetchUrl.searchParams.set(`shape_id`, this.shapeId!)\n }\n\n let response!: Response\n\n try {\n const maybeResponse = await this.fetchWithBackoff(fetchUrl)\n if (maybeResponse) response = maybeResponse\n else break\n } catch (e) {\n if (!(e instanceof FetchError)) throw e // should never happen\n if (e.status == 400) {\n // The request is invalid, most likely because the shape has been deleted.\n // We should start from scratch, this will force the shape to be recreated.\n this.reset()\n this.publish(e.json as Message<T>[])\n continue\n } else if (e.status == 409) {\n // Upon receiving a 409, we should start from scratch\n // with the newly provided shape ID\n const newShapeId = e.headers[`x-electric-shape-id`]\n this.reset(newShapeId)\n this.publish(e.json as Message<T>[])\n continue\n } else if (e.status >= 400 && e.status < 500) {\n // Notify subscribers\n this.sendErrorToUpToDateSubscribers(e)\n this.sendErrorToSubscribers(e)\n\n // 400 errors are not actionable without additional user input, so we're throwing them.\n throw e\n }\n }\n\n const { headers, status } = response\n const shapeId = headers.get(`X-Electric-Shape-Id`)\n if (shapeId) {\n this.shapeId = shapeId\n }\n\n const lastOffset = headers.get(`X-Electric-Chunk-Last-Offset`)\n if (lastOffset) {\n this.lastOffset = lastOffset as Offset\n }\n\n const getSchema = (): Schema => {\n const schemaHeader = headers.get(`X-Electric-Schema`)\n return schemaHeader ? JSON.parse(schemaHeader) : {}\n }\n this.schema = this.schema ?? getSchema()\n\n const messages = status === 204 ? `[]` : await response.text()\n\n if (status === 204) {\n // There's no content so we are live and up to date\n this.lastSyncedAt = Date.now()\n }\n\n const batch = this.messageParser.parse(messages, this.schema)\n\n // Update isUpToDate\n if (batch.length > 0) {\n const lastMessage = batch[batch.length - 1]\n if (\n isControlMessage(lastMessage) &&\n lastMessage.headers.control === `up-to-date`\n ) {\n this.lastSyncedAt = Date.now()\n if (!this.isUpToDate) {\n this.isUpToDate = true\n this.notifyUpToDateSubscribers()\n }\n }\n\n this.publish(batch)\n }\n }\n } finally {\n this.connected = false\n }\n }\n\n subscribe(\n callback: (messages: Message<T>[]) => void | Promise<void>,\n onError?: (error: FetchError | Error) => void\n ) {\n const subscriptionId = Math.random()\n const subscriber = new MessageProcessor(callback)\n\n this.subscribers.set(subscriptionId, [subscriber, onError])\n\n return () => {\n this.subscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAll(): void {\n this.subscribers.clear()\n }\n\n private publish(messages: Message<T>[]) {\n this.subscribers.forEach(([subscriber, _]) => {\n subscriber.process(messages)\n })\n }\n\n private sendErrorToSubscribers(error: Error) {\n this.subscribers.forEach(([_, errorFn]) => {\n errorFn?.(error)\n })\n }\n\n subscribeOnceToUpToDate(\n callback: () => void | Promise<void>,\n error: (err: FetchError | Error) => void\n ) {\n const subscriptionId = Math.random()\n\n this.upToDateSubscribers.set(subscriptionId, [callback, error])\n\n return () => {\n this.upToDateSubscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAllUpToDateSubscribers(): void {\n this.upToDateSubscribers.clear()\n }\n\n /** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */\n lastSynced(): number {\n if (this.lastSyncedAt === undefined) return Infinity\n return Date.now() - this.lastSyncedAt\n }\n\n isConnected(): boolean {\n return this.connected\n }\n\n private notifyUpToDateSubscribers() {\n this.upToDateSubscribers.forEach(([callback]) => {\n callback()\n })\n }\n\n private sendErrorToUpToDateSubscribers(error: FetchError | Error) {\n // eslint-disable-next-line\n this.upToDateSubscribers.forEach(([_, errorCallback]) =>\n errorCallback(error)\n )\n }\n\n /**\n * Resets the state of the stream, optionally with a provided\n * shape ID\n */\n private reset(shapeId?: string) {\n this.lastOffset = `-1`\n this.shapeId = shapeId\n this.isUpToDate = false\n this.connected = false\n this.schema = undefined\n }\n\n private validateOptions(options: ShapeStreamOptions): void {\n if (!options.url) {\n throw new Error(`Invalid shape option. It must provide the url`)\n }\n if (options.signal && !(options.signal instanceof AbortSignal)) {\n throw new Error(\n `Invalid signal option. It must be an instance of AbortSignal.`\n )\n }\n\n if (\n options.offset !== undefined &&\n options.offset !== `-1` &&\n !options.shapeId\n ) {\n throw new Error(\n `shapeId is required if this isn't an initial fetch (i.e. offset > -1)`\n )\n }\n }\n\n private async fetchWithBackoff(url: URL) {\n const { initialDelay, maxDelay, multiplier } = this.backoffOptions\n const signal = this.options.signal\n\n let delay = initialDelay\n let attempt = 0\n\n // eslint-disable-next-line no-constant-condition -- we're retrying with a lag until we get a non-500 response or the abort signal is triggered\n while (true) {\n try {\n const result = await this.fetchClient(url.toString(), { signal })\n if (result.ok) {\n if (this.options.subscribe) {\n this.connected = true\n }\n return result\n } else throw await FetchError.fromResponse(result, url.toString())\n } catch (e) {\n this.connected = false\n if (signal?.aborted) {\n return undefined\n } else if (\n e instanceof FetchError &&\n e.status >= 400 &&\n e.status < 500\n ) {\n // Any client errors cannot be backed off on, leave it to the caller to handle.\n throw e\n } else {\n // Exponentially backoff on errors.\n // Wait for the current delay duration\n await new Promise((resolve) => setTimeout(resolve, delay))\n\n // Increase the delay for the next attempt\n delay = Math.min(delay * multiplier, maxDelay)\n\n attempt++\n console.log(`Retry attempt #${attempt} after ${delay}ms`)\n }\n }\n }\n }\n}\n\n/**\n * A Shape is an object that subscribes to a shape log,\n * keeps a materialised shape `.value` in memory and\n * notifies subscribers when the value has changed.\n *\n * It can be used without a framework and as a primitive\n * to simplify developing framework hooks.\n *\n * @constructor\n * @param {ShapeStream<T extends Row>} - the underlying shape stream\n * @example\n * ```\n * const shapeStream = new ShapeStream<{ foo: number }>(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\n * ```\n *\n * `value` returns a promise that resolves the Shape data once the Shape has been\n * fully loaded (and when resuming from being offline):\n *\n * const value = await shape.value\n *\n * `valueSync` returns the current data synchronously:\n *\n * const value = shape.valueSync\n *\n * Subscribe to updates. Called whenever the shape updates in Postgres.\n *\n * shape.subscribe(shapeData => {\n * console.log(shapeData)\n * })\n */\nexport class Shape<T extends Row = Row> {\n private stream: ShapeStream<T>\n\n private data: ShapeData<T> = new Map()\n private subscribers = new Map<number, ShapeChangedCallback<T>>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream<T>) {\n this.stream = stream\n this.stream.subscribe(this.process.bind(this), this.handleError.bind(this))\n const unsubscribe = this.stream.subscribeOnceToUpToDate(\n () => {\n unsubscribe()\n },\n (e) => {\n this.handleError(e)\n throw e\n }\n )\n }\n\n lastSynced(): number {\n return this.stream.lastSynced()\n }\n\n isConnected(): boolean {\n return this.stream.isConnected()\n }\n\n get value(): Promise<ShapeData<T>> {\n return new Promise((resolve) => {\n if (this.stream.isUpToDate) {\n resolve(this.valueSync)\n } else {\n const unsubscribe = this.stream.subscribeOnceToUpToDate(\n () => {\n unsubscribe()\n resolve(this.valueSync)\n },\n (e) => {\n throw e\n }\n )\n }\n })\n }\n\n get valueSync() {\n return this.data\n }\n\n subscribe(callback: ShapeChangedCallback<T>): () => void {\n const subscriptionId = Math.random()\n\n this.subscribers.set(subscriptionId, callback)\n\n return () => {\n this.subscribers.delete(subscriptionId)\n }\n }\n\n unsubscribeAll(): void {\n this.subscribers.clear()\n }\n\n get numSubscribers() {\n return this.subscribers.size\n }\n\n private process(messages: Message<T>[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (isChangeMessage(message)) {\n dataMayHaveChanged = [`insert`, `update`, `delete`].includes(\n message.headers.operation\n )\n\n switch (message.headers.operation) {\n case `insert`:\n this.data.set(message.key, message.value)\n break\n case `update`:\n this.data.set(message.key, {\n ...this.data.get(message.key)!,\n ...message.value,\n })\n break\n case `delete`:\n this.data.delete(message.key)\n break\n }\n }\n\n if (isControlMessage(message)) {\n switch (message.headers.control) {\n case `up-to-date`:\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n break\n case `must-refetch`:\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\n break\n }\n }\n })\n\n // Always notify subscribers when the Shape first is up to date.\n // FIXME this would be cleaner with a simple state machine.\n if (newlyUpToDate || (isUpToDate && dataMayHaveChanged)) {\n this.hasNotifiedSubscribersUpToDate = true\n this.notify()\n }\n }\n\n private handleError(e: Error): void {\n if (e instanceof FetchError) {\n this.error = e\n this.notify()\n }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback(this.valueSync)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,IAAM,cAAc,CAAC,UAAkB,OAAO,KAAK;AACnD,IAAM,YAAY,CAAC,UAAkB,UAAU,UAAU,UAAU;AACnE,IAAM,cAAc,CAAC,UAAkB,OAAO,KAAK;AACnD,IAAM,YAAY,CAAC,UAAkB,KAAK,MAAM,KAAK;AACrD,IAAM,iBAAgC,CAAC,MAAc;AAE9C,IAAM,gBAAwB;AAAA,EACnC,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AACT;AAGO,SAAS,cAAc,OAAc,QAA+B;AACzE,MAAI,IAAI;AACR,MAAI,OAAO;AACX,MAAI,MAAM;AACV,MAAI,SAAS;AACb,MAAI,OAAO;AACX,MAAI,IAAwB;AAE5B,WAAS,KAAK,GAAoB;AAChC,UAAM,KAAK,CAAC;AACZ,WAAO,IAAI,EAAE,QAAQ,KAAK;AACxB,aAAO,EAAE,CAAC;AACV,UAAI,QAAQ;AACV,YAAI,SAAS,MAAM;AACjB,iBAAO,EAAE,EAAE,CAAC;AAAA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;AAEpB,aAAS,QAAQ,CAAC,YAAY;AAC5B,UAAI,gBAAgB,OAAO,GAAG;AAC5B,6BAAqB,CAAC,UAAU,UAAU,QAAQ,EAAE;AAAA,UAClD,QAAQ,QAAQ;AAAA,QAClB;AAEA,gBAAQ,QAAQ,QAAQ,WAAW;AAAA,UACjC,KAAK;AACH,iBAAK,KAAK,IAAI,QAAQ,KAAK,QAAQ,KAAK;AACxC;AAAA,UACF,KAAK;AACH,iBAAK,KAAK,IAAI,QAAQ,KAAK,kCACtB,KAAK,KAAK,IAAI,QAAQ,GAAG,IACzB,QAAQ,MACZ;AACD;AAAA,UACF,KAAK;AACH,iBAAK,KAAK,OAAO,QAAQ,GAAG;AAC5B;AAAA,QACJ;AAAA,MACF;AAEA,UAAI,iBAAiB,OAAO,GAAG;AAC7B,gBAAQ,QAAQ,QAAQ,SAAS;AAAA,UAC/B,KAAK;AACH,yBAAa;AACb,gBAAI,CAAC,KAAK,gCAAgC;AACxC,8BAAgB;AAAA,YAClB;AACA;AAAA,UACF,KAAK;AACH,iBAAK,KAAK,MAAM;AAChB,iBAAK,QAAQ;AACb,yBAAa;AACb,4BAAgB;AAChB;AAAA,QACJ;AAAA,MACF;AAAA,IACF,CAAC;AAID,QAAI,iBAAkB,cAAc,oBAAqB;AACvD,WAAK,iCAAiC;AACtC,WAAK,OAAO;AAAA,IACd;AAAA,EACF;AAAA,EAEQ,YAAY,GAAgB;AAClC,QAAI,aAAa,YAAY;AAC3B,WAAK,QAAQ;AACb,WAAK,OAAO;AAAA,IACd;AAAA,EACF;AAAA,EAEQ,SAAe;AACrB,SAAK,YAAY,QAAQ,CAAC,aAAa;AACrC,eAAS,KAAK,SAAS;AAAA,IACzB,CAAC;AAAA,EACH;AACF;","names":["key","value"]}
|
package/dist/index.mjs
CHANGED
|
@@ -112,7 +112,7 @@ var MessageParser = class {
|
|
|
112
112
|
}
|
|
113
113
|
parse(messages, schema) {
|
|
114
114
|
return JSON.parse(messages, (key, value) => {
|
|
115
|
-
if (key === `value` && typeof value === `object`) {
|
|
115
|
+
if (key === `value` && typeof value === `object` && value !== null) {
|
|
116
116
|
const row = value;
|
|
117
117
|
Object.keys(row).forEach((key2) => {
|
|
118
118
|
row[key2] = this.parseRow(key2, row[key2], schema);
|
|
@@ -228,7 +228,9 @@ var ShapeStream = class {
|
|
|
228
228
|
constructor(options) {
|
|
229
229
|
this.subscribers = /* @__PURE__ */ new Map();
|
|
230
230
|
this.upToDateSubscribers = /* @__PURE__ */ new Map();
|
|
231
|
+
// unix time
|
|
231
232
|
this.isUpToDate = false;
|
|
233
|
+
this.connected = false;
|
|
232
234
|
var _a, _b, _c;
|
|
233
235
|
this.validateOptions(options);
|
|
234
236
|
this.options = __spreadValues({ subscribe: true }, options);
|
|
@@ -244,58 +246,72 @@ var ShapeStream = class {
|
|
|
244
246
|
var _a;
|
|
245
247
|
this.isUpToDate = false;
|
|
246
248
|
const { url, where, signal } = this.options;
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (this.shapeId) {
|
|
255
|
-
fetchUrl.searchParams.set(`shape_id`, this.shapeId);
|
|
256
|
-
}
|
|
257
|
-
let response;
|
|
258
|
-
try {
|
|
259
|
-
const maybeResponse = yield this.fetchWithBackoff(fetchUrl);
|
|
260
|
-
if (maybeResponse) response = maybeResponse;
|
|
261
|
-
else break;
|
|
262
|
-
} catch (e) {
|
|
263
|
-
if (!(e instanceof FetchError)) throw e;
|
|
264
|
-
if (e.status == 409) {
|
|
265
|
-
const newShapeId = e.headers[`x-electric-shape-id`];
|
|
266
|
-
this.reset(newShapeId);
|
|
267
|
-
this.publish(e.json);
|
|
268
|
-
continue;
|
|
269
|
-
} else if (e.status >= 400 && e.status < 500) {
|
|
270
|
-
this.sendErrorToUpToDateSubscribers(e);
|
|
271
|
-
this.sendErrorToSubscribers(e);
|
|
272
|
-
throw e;
|
|
249
|
+
try {
|
|
250
|
+
while (!(signal == null ? void 0 : signal.aborted) && !this.isUpToDate || this.options.subscribe) {
|
|
251
|
+
const fetchUrl = new URL(url);
|
|
252
|
+
if (where) fetchUrl.searchParams.set(`where`, where);
|
|
253
|
+
fetchUrl.searchParams.set(`offset`, this.lastOffset);
|
|
254
|
+
if (this.isUpToDate) {
|
|
255
|
+
fetchUrl.searchParams.set(`live`, `true`);
|
|
273
256
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
257
|
+
if (this.shapeId) {
|
|
258
|
+
fetchUrl.searchParams.set(`shape_id`, this.shapeId);
|
|
259
|
+
}
|
|
260
|
+
let response;
|
|
261
|
+
try {
|
|
262
|
+
const maybeResponse = yield this.fetchWithBackoff(fetchUrl);
|
|
263
|
+
if (maybeResponse) response = maybeResponse;
|
|
264
|
+
else break;
|
|
265
|
+
} catch (e) {
|
|
266
|
+
if (!(e instanceof FetchError)) throw e;
|
|
267
|
+
if (e.status == 400) {
|
|
268
|
+
this.reset();
|
|
269
|
+
this.publish(e.json);
|
|
270
|
+
continue;
|
|
271
|
+
} else if (e.status == 409) {
|
|
272
|
+
const newShapeId = e.headers[`x-electric-shape-id`];
|
|
273
|
+
this.reset(newShapeId);
|
|
274
|
+
this.publish(e.json);
|
|
275
|
+
continue;
|
|
276
|
+
} else if (e.status >= 400 && e.status < 500) {
|
|
277
|
+
this.sendErrorToUpToDateSubscribers(e);
|
|
278
|
+
this.sendErrorToSubscribers(e);
|
|
279
|
+
throw e;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const { headers, status } = response;
|
|
283
|
+
const shapeId = headers.get(`X-Electric-Shape-Id`);
|
|
284
|
+
if (shapeId) {
|
|
285
|
+
this.shapeId = shapeId;
|
|
286
|
+
}
|
|
287
|
+
const lastOffset = headers.get(`X-Electric-Chunk-Last-Offset`);
|
|
288
|
+
if (lastOffset) {
|
|
289
|
+
this.lastOffset = lastOffset;
|
|
290
|
+
}
|
|
291
|
+
const getSchema = () => {
|
|
292
|
+
const schemaHeader = headers.get(`X-Electric-Schema`);
|
|
293
|
+
return schemaHeader ? JSON.parse(schemaHeader) : {};
|
|
294
|
+
};
|
|
295
|
+
this.schema = (_a = this.schema) != null ? _a : getSchema();
|
|
296
|
+
const messages = status === 204 ? `[]` : yield response.text();
|
|
297
|
+
if (status === 204) {
|
|
298
|
+
this.lastSyncedAt = Date.now();
|
|
299
|
+
}
|
|
300
|
+
const batch = this.messageParser.parse(messages, this.schema);
|
|
301
|
+
if (batch.length > 0) {
|
|
302
|
+
const lastMessage = batch[batch.length - 1];
|
|
303
|
+
if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date`) {
|
|
304
|
+
this.lastSyncedAt = Date.now();
|
|
305
|
+
if (!this.isUpToDate) {
|
|
306
|
+
this.isUpToDate = true;
|
|
307
|
+
this.notifyUpToDateSubscribers();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
this.publish(batch);
|
|
296
311
|
}
|
|
297
|
-
this.publish(batch);
|
|
298
312
|
}
|
|
313
|
+
} finally {
|
|
314
|
+
this.connected = false;
|
|
299
315
|
}
|
|
300
316
|
});
|
|
301
317
|
}
|
|
@@ -330,6 +346,14 @@ var ShapeStream = class {
|
|
|
330
346
|
unsubscribeAllUpToDateSubscribers() {
|
|
331
347
|
this.upToDateSubscribers.clear();
|
|
332
348
|
}
|
|
349
|
+
/** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */
|
|
350
|
+
lastSynced() {
|
|
351
|
+
if (this.lastSyncedAt === void 0) return Infinity;
|
|
352
|
+
return Date.now() - this.lastSyncedAt;
|
|
353
|
+
}
|
|
354
|
+
isConnected() {
|
|
355
|
+
return this.connected;
|
|
356
|
+
}
|
|
333
357
|
notifyUpToDateSubscribers() {
|
|
334
358
|
this.upToDateSubscribers.forEach(([callback]) => {
|
|
335
359
|
callback();
|
|
@@ -348,6 +372,7 @@ var ShapeStream = class {
|
|
|
348
372
|
this.lastOffset = `-1`;
|
|
349
373
|
this.shapeId = shapeId;
|
|
350
374
|
this.isUpToDate = false;
|
|
375
|
+
this.connected = false;
|
|
351
376
|
this.schema = void 0;
|
|
352
377
|
}
|
|
353
378
|
validateOptions(options) {
|
|
@@ -374,9 +399,14 @@ var ShapeStream = class {
|
|
|
374
399
|
while (true) {
|
|
375
400
|
try {
|
|
376
401
|
const result = yield this.fetchClient(url.toString(), { signal });
|
|
377
|
-
if (result.ok)
|
|
378
|
-
|
|
402
|
+
if (result.ok) {
|
|
403
|
+
if (this.options.subscribe) {
|
|
404
|
+
this.connected = true;
|
|
405
|
+
}
|
|
406
|
+
return result;
|
|
407
|
+
} else throw yield FetchError.fromResponse(result, url.toString());
|
|
379
408
|
} catch (e) {
|
|
409
|
+
this.connected = false;
|
|
380
410
|
if (signal == null ? void 0 : signal.aborted) {
|
|
381
411
|
return void 0;
|
|
382
412
|
} else if (e instanceof FetchError && e.status >= 400 && e.status < 500) {
|
|
@@ -410,8 +440,11 @@ var Shape = class {
|
|
|
410
440
|
}
|
|
411
441
|
);
|
|
412
442
|
}
|
|
413
|
-
|
|
414
|
-
return this.stream.
|
|
443
|
+
lastSynced() {
|
|
444
|
+
return this.stream.lastSynced();
|
|
445
|
+
}
|
|
446
|
+
isConnected() {
|
|
447
|
+
return this.stream.isConnected();
|
|
415
448
|
}
|
|
416
449
|
get value() {
|
|
417
450
|
return new Promise((resolve) => {
|