@electric-sql/client 0.3.1 → 0.3.2

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 CHANGED
@@ -46,7 +46,7 @@ The client exports a `ShapeStream` class for getting updates to shapes on a row-
46
46
  ### `ShapeStream`
47
47
 
48
48
  ```tsx
49
- import { ShapeStream } from 'electric-sql'
49
+ import { ShapeStream } from '@electric-sql/client'
50
50
 
51
51
  // Passes subscribers rows as they're inserted, updated, or deleted
52
52
  const stream = new ShapeStream({
@@ -61,7 +61,7 @@ stream.subscribe(messages => {
61
61
  ### `Shape`
62
62
 
63
63
  ```tsx
64
- import { ShapeStream, Shape } from 'electric-sql'
64
+ import { ShapeStream, Shape } from '@electric-sql/client'
65
65
 
66
66
  const stream = new ShapeStream({
67
67
  url: `${BASE_URL}/v1/shape/foo`,
@@ -68,7 +68,9 @@ __export(src_exports, {
68
68
  BackoffDefaults: () => BackoffDefaults,
69
69
  FetchError: () => FetchError,
70
70
  Shape: () => Shape,
71
- ShapeStream: () => ShapeStream
71
+ ShapeStream: () => ShapeStream,
72
+ isChangeMessage: () => isChangeMessage,
73
+ isControlMessage: () => isControlMessage
72
74
  });
73
75
  module.exports = __toCommonJS(src_exports);
74
76
 
@@ -170,6 +172,14 @@ function makeNullableParser(parser, notNullable) {
170
172
  return parser;
171
173
  }
172
174
 
175
+ // src/helpers.ts
176
+ function isChangeMessage(message) {
177
+ return `key` in message;
178
+ }
179
+ function isControlMessage(message) {
180
+ return !isChangeMessage(message);
181
+ }
182
+
173
183
  // src/client.ts
174
184
  var BackoffDefaults = {
175
185
  initialDelay: 100,
@@ -244,7 +254,7 @@ var ShapeStream = class {
244
254
  }
245
255
  start() {
246
256
  return __async(this, null, function* () {
247
- var _a, _b;
257
+ var _a;
248
258
  this.isUpToDate = false;
249
259
  const { url, where, signal } = this.options;
250
260
  while (!(signal == null ? void 0 : signal.aborted) && !this.isUpToDate || this.options.subscribe) {
@@ -293,7 +303,7 @@ var ShapeStream = class {
293
303
  const batch = this.messageParser.parse(messages, this.schema);
294
304
  if (batch.length > 0) {
295
305
  const lastMessage = batch[batch.length - 1];
296
- if (((_b = lastMessage.headers) == null ? void 0 : _b[`control`]) === `up-to-date` && !this.isUpToDate) {
306
+ if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date` && !this.isUpToDate) {
297
307
  this.isUpToDate = true;
298
308
  this.notifyUpToDateSubscribers();
299
309
  }
@@ -454,8 +464,7 @@ var Shape = class {
454
464
  let isUpToDate = false;
455
465
  let newlyUpToDate = false;
456
466
  messages.forEach((message) => {
457
- var _a, _b;
458
- if (`key` in message) {
467
+ if (isChangeMessage(message)) {
459
468
  dataMayHaveChanged = [`insert`, `update`, `delete`].includes(
460
469
  message.headers.operation
461
470
  );
@@ -471,18 +480,22 @@ var Shape = class {
471
480
  break;
472
481
  }
473
482
  }
474
- if (((_a = message.headers) == null ? void 0 : _a[`control`]) === `up-to-date`) {
475
- isUpToDate = true;
476
- if (!this.hasNotifiedSubscribersUpToDate) {
477
- newlyUpToDate = true;
483
+ if (isControlMessage(message)) {
484
+ switch (message.headers.control) {
485
+ case `up-to-date`:
486
+ isUpToDate = true;
487
+ if (!this.hasNotifiedSubscribersUpToDate) {
488
+ newlyUpToDate = true;
489
+ }
490
+ break;
491
+ case `must-refetch`:
492
+ this.data.clear();
493
+ this.error = false;
494
+ isUpToDate = false;
495
+ newlyUpToDate = false;
496
+ break;
478
497
  }
479
498
  }
480
- if (((_b = message.headers) == null ? void 0 : _b[`control`]) === `must-refetch`) {
481
- this.data.clear();
482
- this.error = false;
483
- isUpToDate = false;
484
- newlyUpToDate = false;
485
- }
486
499
  });
487
500
  if (newlyUpToDate || isUpToDate && dataMayHaveChanged) {
488
501
  this.hasNotifiedSubscribersUpToDate = true;
@@ -492,6 +505,7 @@ var Shape = class {
492
505
  handleError(e) {
493
506
  if (e instanceof FetchError) {
494
507
  this.error = e;
508
+ this.notify();
495
509
  }
496
510
  }
497
511
  notify() {
@@ -505,6 +519,8 @@ var Shape = class {
505
519
  BackoffDefaults,
506
520
  FetchError,
507
521
  Shape,
508
- ShapeStream
522
+ ShapeStream,
523
+ isChangeMessage,
524
+ isControlMessage
509
525
  });
510
526
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/parser.ts","../../src/client.ts"],"sourcesContent":["export * from './client'\nexport * from './types'\n","import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ArgumentsType } from 'vitest'\nimport { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: ArgumentsType<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[])\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 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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (`key` in 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 (message.headers?.[`control`] === `up-to-date`) {\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n }\n\n if (message.headers?.[`control`] === `must-refetch`) {\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\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 }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback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,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,YAAI,aAAQ,YAAR,mBAAkB,gBAAe,cAAc;AACjD,qBAAa;AACb,YAAI,CAAC,KAAK,gCAAgC;AACxC,0BAAgB;AAAA,QAClB;AAAA,MACF;AAEA,YAAI,aAAQ,YAAR,mBAAkB,gBAAe,gBAAgB;AACnD,aAAK,KAAK,MAAM;AAChB,aAAK,QAAQ;AACb,qBAAa;AACb,wBAAgB;AAAA,MAClB;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;AAAA,IACf;AAAA,EACF;AAAA,EAEQ,SAAe;AACrB,SAAK,YAAY,QAAQ,CAAC,aAAa;AACrC,eAAS,KAAK,SAAS;AAAA,IACzB,CAAC;AAAA,EACH;AACF;","names":["key"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/parser.ts","../../src/helpers.ts","../../src/client.ts"],"sourcesContent":["export * from './client'\nexport * from './types'\nexport * from './helpers'\n","import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ChangeMessage, ControlMessage, Message, Value } 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 Value = { [key: string]: Value }>(\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 Value = { [key: string]: Value }>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(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[])\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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,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;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,cACd,OACA,QACO;AACP,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,iBA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AAC,aAAa;AACrC,eAAS,KAAK,SAAS;AAAA,IACzB,CAAC;AAAA,EACH;AACF;","names":["key"]}
@@ -1,2 +1,2 @@
1
- var C=Object.defineProperty;var v=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable;var D=(a,e,s)=>e in a?C(a,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):a[e]=s,p=(a,e)=>{for(var s in e||(e={}))E.call(e,s)&&D(a,s,e[s]);if(v)for(var s of v(e))P.call(e,s)&&D(a,s,e[s]);return a};var O=(a,e)=>{var s={};for(var t in a)E.call(a,t)&&e.indexOf(t)<0&&(s[t]=a[t]);if(a!=null&&v)for(var t of v(a))e.indexOf(t)<0&&P.call(a,t)&&(s[t]=a[t]);return s};var d=(a,e,s)=>new Promise((t,o)=>{var i=n=>{try{c(s.next(n))}catch(h){o(h)}},r=n=>{try{c(s.throw(n))}catch(h){o(h)}},c=n=>n.done?t(n.value):Promise.resolve(n.value).then(i,r);c((s=s.apply(a,e)).next())});var S=a=>Number(a),j=a=>a==="true"||a==="t",R=a=>BigInt(a),k=a=>JSON.parse(a),x={int2:S,int4:S,int8:R,bool:j,float4:S,float8:S,json:k,jsonb:k};function A(a,e){let s=0,t=null,o="",i=!1,r=0,c;function n(h){let l=[];for(;s<h.length;s++){if(t=h[s],i)t==="\\"?o+=h[++s]:t==='"'?(l.push(e?e(o):o),o="",i=h[s+1]==='"',r=s+2):o+=t;else if(t==='"')i=!0;else if(t==="{")r=++s,l.push(n(h));else if(t==="}"){i=!1,r<s&&l.push(e?e(h.slice(r,s)):h.slice(r,s)),r=s+1;break}else t===","&&c!=="}"&&c!=='"'&&(l.push(e?e(h.slice(r,s)):h.slice(r,s)),r=s+1);c=t}return r<s&&l.push(e?e(h.slice(r,s+1)):h.slice(r,s+1)),l}return n(a)[0]}var y=class{constructor(e){this.parser=p(p({},x),e)}parse(e,s){return JSON.parse(e,(t,o)=>{if(t==="value"&&typeof o=="object"){let i=o;Object.keys(i).forEach(r=>{i[r]=this.parseRow(r,i[r],s)})}return o})}parseRow(e,s,t){var m;let o=t[e];if(!o)return s;let b=o,{type:i,dims:r}=b,c=O(b,["type","dims"]),n=w=>w,h=(m=this.parser[i])!=null?m:n,l=B(h,o.not_null);return r&&r>0?A(s,l):l(s,c)}};function B(a,e){return e!=null&&e?a:t=>t===null||t==="NULL"?null:a(t)}var N={initialDelay:100,maxDelay:1e4,multiplier:1.3},T=class{constructor(e){this.messageQueue=[];this.isProcessing=!1;this.callback=e}process(e){this.messageQueue.push(e),this.isProcessing||this.processQueue()}processQueue(){return d(this,null,function*(){for(this.isProcessing=!0;this.messageQueue.length>0;){let e=this.messageQueue.shift();yield this.callback(e)}this.isProcessing=!1})}},f=class a extends Error{constructor(s,t,o,i,r,c){super(c||`HTTP Error ${s} at ${r}: ${t!=null?t:JSON.stringify(o)}`);this.url=r;this.name="FetchError",this.status=s,this.text=t,this.json=o,this.headers=i}static fromResponse(s,t){return d(this,null,function*(){let o=s.status,i=Object.fromEntries([...s.headers.entries()]),r,c,n=s.headers.get("content-type");return n&&n.includes("application/json")?c=yield s.json():r=yield s.text(),new a(o,r,c,i,t)})}},I=class{constructor(e){this.subscribers=new Map;this.upToDateSubscribers=new Map;this.isUpToDate=!1;var s,t,o;this.validateOptions(e),this.options=p({subscribe:!0},e),this.lastOffset=(s=this.options.offset)!=null?s:"-1",this.shapeId=this.options.shapeId,this.messageParser=new y(e.parser),this.backoffOptions=(t=e.backoffOptions)!=null?t:N,this.fetchClient=(o=e.fetchClient)!=null?o:(...i)=>fetch(...i),this.start()}start(){return d(this,null,function*(){var o,i;this.isUpToDate=!1;let{url:e,where:s,signal:t}=this.options;for(;!(t!=null&&t.aborted)&&!this.isUpToDate||this.options.subscribe;){let r=new URL(e);s&&r.searchParams.set("where",s),r.searchParams.set("offset",this.lastOffset),this.isUpToDate&&r.searchParams.set("live","true"),this.shapeId&&r.searchParams.set("shape_id",this.shapeId);let c;try{let u=yield this.fetchWithBackoff(r);if(u)c=u;else break}catch(u){if(!(u instanceof f))throw u;if(u.status==409){let M=u.headers["x-electric-shape-id"];this.reset(M),this.publish(u.json);continue}else if(u.status>=400&&u.status<500)throw this.sendErrorToUpToDateSubscribers(u),this.sendErrorToSubscribers(u),u}let{headers:n,status:h}=c,l=n.get("X-Electric-Shape-Id");l&&(this.shapeId=l);let b=n.get("X-Electric-Chunk-Last-Offset");b&&(this.lastOffset=b);let m=()=>{let u=n.get("X-Electric-Schema");return u?JSON.parse(u):{}};this.schema=(o=this.schema)!=null?o:m();let w=h===204?"[]":yield c.text(),g=this.messageParser.parse(w,this.schema);g.length>0&&(((i=g[g.length-1].headers)==null?void 0:i.control)==="up-to-date"&&!this.isUpToDate&&(this.isUpToDate=!0,this.notifyUpToDateSubscribers()),this.publish(g))}})}subscribe(e,s){let t=Math.random(),o=new T(e);return this.subscribers.set(t,[o,s]),()=>{this.subscribers.delete(t)}}unsubscribeAll(){this.subscribers.clear()}publish(e){this.subscribers.forEach(([s,t])=>{s.process(e)})}sendErrorToSubscribers(e){this.subscribers.forEach(([s,t])=>{t==null||t(e)})}subscribeOnceToUpToDate(e,s){let t=Math.random();return this.upToDateSubscribers.set(t,[e,s]),()=>{this.upToDateSubscribers.delete(t)}}unsubscribeAllUpToDateSubscribers(){this.upToDateSubscribers.clear()}notifyUpToDateSubscribers(){this.upToDateSubscribers.forEach(([e])=>{e()})}sendErrorToUpToDateSubscribers(e){this.upToDateSubscribers.forEach(([s,t])=>t(e))}reset(e){this.lastOffset="-1",this.shapeId=e,this.isUpToDate=!1,this.schema=void 0}validateOptions(e){if(!e.url)throw new Error("Invalid shape option. It must provide the url");if(e.signal&&!(e.signal instanceof AbortSignal))throw new Error("Invalid signal option. It must be an instance of AbortSignal.");if(e.offset!==void 0&&e.offset!=="-1"&&!e.shapeId)throw new Error("shapeId is required if this isn't an initial fetch (i.e. offset > -1)")}fetchWithBackoff(e){return d(this,null,function*(){let{initialDelay:s,maxDelay:t,multiplier:o}=this.backoffOptions,i=this.options.signal,r=s,c=0;for(;;)try{let n=yield this.fetchClient(e.toString(),{signal:i});if(n.ok)return n;throw yield f.fromResponse(n,e.toString())}catch(n){if(i!=null&&i.aborted)return;if(n instanceof f&&n.status>=400&&n.status<500)throw n;yield new Promise(h=>setTimeout(h,r)),r=Math.min(r*o,t),c++,console.log(`Retry attempt #${c} after ${r}ms`)}})}},U=class{constructor(e){this.data=new Map;this.subscribers=new Map;this.error=!1;this.hasNotifiedSubscribersUpToDate=!1;this.stream=e,this.stream.subscribe(this.process.bind(this),this.handleError.bind(this));let s=this.stream.subscribeOnceToUpToDate(()=>{s()},t=>{throw this.handleError(t),t})}get isUpToDate(){return this.stream.isUpToDate}get value(){return new Promise(e=>{if(this.stream.isUpToDate)e(this.valueSync);else{let s=this.stream.subscribeOnceToUpToDate(()=>{s(),e(this.valueSync)},t=>{throw t})}})}get valueSync(){return this.data}subscribe(e){let s=Math.random();return this.subscribers.set(s,e),()=>{this.subscribers.delete(s)}}unsubscribeAll(){this.subscribers.clear()}get numSubscribers(){return this.subscribers.size}process(e){let s=!1,t=!1,o=!1;e.forEach(i=>{var r,c;if("key"in i)switch(s=["insert","update","delete"].includes(i.headers.operation),i.headers.operation){case"insert":this.data.set(i.key,i.value);break;case"update":this.data.set(i.key,p(p({},this.data.get(i.key)),i.value));break;case"delete":this.data.delete(i.key);break}((r=i.headers)==null?void 0:r.control)==="up-to-date"&&(t=!0,this.hasNotifiedSubscribersUpToDate||(o=!0)),((c=i.headers)==null?void 0:c.control)==="must-refetch"&&(this.data.clear(),this.error=!1,t=!1,o=!1)}),(o||t&&s)&&(this.hasNotifiedSubscribersUpToDate=!0,this.notify())}handleError(e){e instanceof f&&(this.error=e)}notify(){this.subscribers.forEach(e=>{e(this.valueSync)})}};export{N as BackoffDefaults,f as FetchError,U as Shape,I as ShapeStream};
1
+ var x=Object.defineProperty;var v=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var P=(r,e,s)=>e in r?x(r,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[e]=s,f=(r,e)=>{for(var s in e||(e={}))E.call(e,s)&&P(r,s,e[s]);if(v)for(var s of v(e))k.call(e,s)&&P(r,s,e[s]);return r};var M=(r,e)=>{var s={};for(var t in r)E.call(r,t)&&e.indexOf(t)<0&&(s[t]=r[t]);if(r!=null&&v)for(var t of v(r))e.indexOf(t)<0&&k.call(r,t)&&(s[t]=r[t]);return s};var d=(r,e,s)=>new Promise((t,a)=>{var i=n=>{try{h(s.next(n))}catch(c){a(c)}},o=n=>{try{h(s.throw(n))}catch(c){a(c)}},h=n=>n.done?t(n.value):Promise.resolve(n.value).then(i,o);h((s=s.apply(r,e)).next())});var S=r=>Number(r),j=r=>r==="true"||r==="t",V=r=>BigInt(r),O=r=>JSON.parse(r),R={int2:S,int4:S,int8:V,bool:j,float4:S,float8:S,json:O,jsonb:O};function B(r,e){let s=0,t=null,a="",i=!1,o=0,h;function n(c){let l=[];for(;s<c.length;s++){if(t=c[s],i)t==="\\"?a+=c[++s]:t==='"'?(l.push(e?e(a):a),a="",i=c[s+1]==='"',o=s+2):a+=t;else if(t==='"')i=!0;else if(t==="{")o=++s,l.push(n(c));else if(t==="}"){i=!1,o<s&&l.push(e?e(c.slice(o,s)):c.slice(o,s)),o=s+1;break}else t===","&&h!=="}"&&h!=='"'&&(l.push(e?e(c.slice(o,s)):c.slice(o,s)),o=s+1);h=t}return o<s&&l.push(e?e(c.slice(o,s+1)):c.slice(o,s+1)),l}return n(r)[0]}var y=class{constructor(e){this.parser=f(f({},R),e)}parse(e,s){return JSON.parse(e,(t,a)=>{if(t==="value"&&typeof a=="object"){let i=a;Object.keys(i).forEach(o=>{i[o]=this.parseRow(o,i[o],s)})}return a})}parseRow(e,s,t){var m;let a=t[e];if(!a)return s;let g=a,{type:i,dims:o}=g,h=M(g,["type","dims"]),n=p=>p,c=(m=this.parser[i])!=null?m:n,l=N(c,a.not_null);return o&&o>0?B(s,l):l(s,h)}};function N(r,e){return e!=null&&e?r:t=>t===null||t==="NULL"?null:r(t)}function T(r){return"key"in r}function w(r){return!T(r)}var A={initialDelay:100,maxDelay:1e4,multiplier:1.3},D=class{constructor(e){this.messageQueue=[];this.isProcessing=!1;this.callback=e}process(e){this.messageQueue.push(e),this.isProcessing||this.processQueue()}processQueue(){return d(this,null,function*(){for(this.isProcessing=!0;this.messageQueue.length>0;){let e=this.messageQueue.shift();yield this.callback(e)}this.isProcessing=!1})}},b=class r extends Error{constructor(s,t,a,i,o,h){super(h||`HTTP Error ${s} at ${o}: ${t!=null?t:JSON.stringify(a)}`);this.url=o;this.name="FetchError",this.status=s,this.text=t,this.json=a,this.headers=i}static fromResponse(s,t){return d(this,null,function*(){let a=s.status,i=Object.fromEntries([...s.headers.entries()]),o,h,n=s.headers.get("content-type");return n&&n.includes("application/json")?h=yield s.json():o=yield s.text(),new r(a,o,h,i,t)})}},I=class{constructor(e){this.subscribers=new Map;this.upToDateSubscribers=new Map;this.isUpToDate=!1;var s,t,a;this.validateOptions(e),this.options=f({subscribe:!0},e),this.lastOffset=(s=this.options.offset)!=null?s:"-1",this.shapeId=this.options.shapeId,this.messageParser=new y(e.parser),this.backoffOptions=(t=e.backoffOptions)!=null?t:A,this.fetchClient=(a=e.fetchClient)!=null?a:(...i)=>fetch(...i),this.start()}start(){return d(this,null,function*(){var a;this.isUpToDate=!1;let{url:e,where:s,signal:t}=this.options;for(;!(t!=null&&t.aborted)&&!this.isUpToDate||this.options.subscribe;){let i=new URL(e);s&&i.searchParams.set("where",s),i.searchParams.set("offset",this.lastOffset),this.isUpToDate&&i.searchParams.set("live","true"),this.shapeId&&i.searchParams.set("shape_id",this.shapeId);let o;try{let u=yield this.fetchWithBackoff(i);if(u)o=u;else break}catch(u){if(!(u instanceof b))throw u;if(u.status==409){let C=u.headers["x-electric-shape-id"];this.reset(C),this.publish(u.json);continue}else if(u.status>=400&&u.status<500)throw this.sendErrorToUpToDateSubscribers(u),this.sendErrorToSubscribers(u),u}let{headers:h,status:n}=o,c=h.get("X-Electric-Shape-Id");c&&(this.shapeId=c);let l=h.get("X-Electric-Chunk-Last-Offset");l&&(this.lastOffset=l);let g=()=>{let u=h.get("X-Electric-Schema");return u?JSON.parse(u):{}};this.schema=(a=this.schema)!=null?a:g();let m=n===204?"[]":yield o.text(),p=this.messageParser.parse(m,this.schema);if(p.length>0){let u=p[p.length-1];w(u)&&u.headers.control==="up-to-date"&&!this.isUpToDate&&(this.isUpToDate=!0,this.notifyUpToDateSubscribers()),this.publish(p)}}})}subscribe(e,s){let t=Math.random(),a=new D(e);return this.subscribers.set(t,[a,s]),()=>{this.subscribers.delete(t)}}unsubscribeAll(){this.subscribers.clear()}publish(e){this.subscribers.forEach(([s,t])=>{s.process(e)})}sendErrorToSubscribers(e){this.subscribers.forEach(([s,t])=>{t==null||t(e)})}subscribeOnceToUpToDate(e,s){let t=Math.random();return this.upToDateSubscribers.set(t,[e,s]),()=>{this.upToDateSubscribers.delete(t)}}unsubscribeAllUpToDateSubscribers(){this.upToDateSubscribers.clear()}notifyUpToDateSubscribers(){this.upToDateSubscribers.forEach(([e])=>{e()})}sendErrorToUpToDateSubscribers(e){this.upToDateSubscribers.forEach(([s,t])=>t(e))}reset(e){this.lastOffset="-1",this.shapeId=e,this.isUpToDate=!1,this.schema=void 0}validateOptions(e){if(!e.url)throw new Error("Invalid shape option. It must provide the url");if(e.signal&&!(e.signal instanceof AbortSignal))throw new Error("Invalid signal option. It must be an instance of AbortSignal.");if(e.offset!==void 0&&e.offset!=="-1"&&!e.shapeId)throw new Error("shapeId is required if this isn't an initial fetch (i.e. offset > -1)")}fetchWithBackoff(e){return d(this,null,function*(){let{initialDelay:s,maxDelay:t,multiplier:a}=this.backoffOptions,i=this.options.signal,o=s,h=0;for(;;)try{let n=yield this.fetchClient(e.toString(),{signal:i});if(n.ok)return n;throw yield b.fromResponse(n,e.toString())}catch(n){if(i!=null&&i.aborted)return;if(n instanceof b&&n.status>=400&&n.status<500)throw n;yield new Promise(c=>setTimeout(c,o)),o=Math.min(o*a,t),h++,console.log(`Retry attempt #${h} after ${o}ms`)}})}},U=class{constructor(e){this.data=new Map;this.subscribers=new Map;this.error=!1;this.hasNotifiedSubscribersUpToDate=!1;this.stream=e,this.stream.subscribe(this.process.bind(this),this.handleError.bind(this));let s=this.stream.subscribeOnceToUpToDate(()=>{s()},t=>{throw this.handleError(t),t})}get isUpToDate(){return this.stream.isUpToDate}get value(){return new Promise(e=>{if(this.stream.isUpToDate)e(this.valueSync);else{let s=this.stream.subscribeOnceToUpToDate(()=>{s(),e(this.valueSync)},t=>{throw t})}})}get valueSync(){return this.data}subscribe(e){let s=Math.random();return this.subscribers.set(s,e),()=>{this.subscribers.delete(s)}}unsubscribeAll(){this.subscribers.clear()}get numSubscribers(){return this.subscribers.size}process(e){let s=!1,t=!1,a=!1;e.forEach(i=>{if(T(i))switch(s=["insert","update","delete"].includes(i.headers.operation),i.headers.operation){case"insert":this.data.set(i.key,i.value);break;case"update":this.data.set(i.key,f(f({},this.data.get(i.key)),i.value));break;case"delete":this.data.delete(i.key);break}if(w(i))switch(i.headers.control){case"up-to-date":t=!0,this.hasNotifiedSubscribersUpToDate||(a=!0);break;case"must-refetch":this.data.clear(),this.error=!1,t=!1,a=!1;break}}),(a||t&&s)&&(this.hasNotifiedSubscribersUpToDate=!0,this.notify())}handleError(e){e instanceof b&&(this.error=e,this.notify())}notify(){this.subscribers.forEach(e=>{e(this.valueSync)})}};export{A as BackoffDefaults,b as FetchError,U as Shape,I as ShapeStream,T as isChangeMessage,w as isControlMessage};
2
2
  //# sourceMappingURL=index.browser.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/parser.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ArgumentsType } from 'vitest'\nimport { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: ArgumentsType<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[])\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 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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (`key` in 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 (message.headers?.[`control`] === `up-to-date`) {\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n }\n\n if (message.headers?.[`control`] === `must-refetch`) {\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\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 }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback(this.valueSync)\n })\n }\n}\n"],"mappings":"wsBAQA,IAAMA,EAAeC,GAAkB,OAAOA,CAAK,EAC7CC,EAAaD,GAAkBA,IAAU,QAAUA,IAAU,IAC7DE,EAAeF,GAAkB,OAAOA,CAAK,EAC7CG,EAAaH,GAAkB,KAAK,MAAMA,CAAK,EAExCI,EAAwB,CACnC,KAAML,EACN,KAAMA,EACN,KAAMG,EACN,KAAMD,EACN,OAAQF,EACR,OAAQA,EACR,KAAMI,EACN,MAAOA,CACT,EAGO,SAASE,EACdL,EACAM,EACO,CACP,IAAIC,EAAI,EACJC,EAAO,KACPC,EAAM,GACNC,EAAS,GACTC,EAAO,EACPC,EAEJ,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Q,GAAG,EAC5B,KACJ,GAGEK,EAAAL,EAAQ,UAAR,YAAAK,EAAkB,WAAe,eACnCmC,EAAa,GACR,KAAK,iCACRC,EAAgB,OAIhBnC,EAAAN,EAAQ,UAAR,YAAAM,EAAkB,WAAe,iBACnC,KAAK,KAAK,MAAM,EAChB,KAAK,MAAQ,GACbkC,EAAa,GACbC,EAAgB,GAEpB,CAAC,GAIGA,GAAkBD,GAAcD,KAClC,KAAK,+BAAiC,GACtC,KAAK,OAAO,EAEhB,CAEQ,YAAY,EAAgB,CAC9B,aAAa9C,IACf,KAAK,MAAQ,EAEjB,CAEQ,QAAe,CACrB,KAAK,YAAY,QAASH,GAAa,CACrCA,EAAS,KAAK,SAAS,CACzB,CAAC,CACH,CACF","names":["parseNumber","value","parseBool","parseBigInt","parseJson","defaultParser","pgArrayParser","parser","i","char","str","quoted","last","p","loop","x","xs","MessageParser","__spreadValues","messages","schema","key","row","_b","columnInfo","_a","typ","dimensions","additionalInfo","__objRest","identityParser","v","typParser","makeNullableParser","notNullable","BackoffDefaults","MessageProcessor","callback","messages","__async","FetchError","_FetchError","status","text","json","headers","url","message","response","contentType","ShapeStream","options","_a","_b","_c","__spreadValues","MessageParser","args","where","signal","fetchUrl","maybeResponse","e","newShapeId","shapeId","lastOffset","getSchema","schemaHeader","batch","onError","subscriptionId","subscriber","_","error","errorFn","errorCallback","initialDelay","maxDelay","multiplier","delay","attempt","result","resolve","Shape","stream","unsubscribe","dataMayHaveChanged","isUpToDate","newlyUpToDate"]}
1
+ {"version":3,"sources":["../src/parser.ts","../src/helpers.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ChangeMessage, ControlMessage, Message, Value } 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 Value = { [key: string]: Value }>(\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 Value = { [key: string]: Value }>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(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[])\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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): 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":"wsBAQA,IAAMA,EAAeC,GAAkB,OAAOA,CAAK,EAC7CC,EAAaD,GAAkBA,IAAU,QAAUA,IAAU,IAC7DE,EAAeF,GAAkB,OAAOA,CAAK,EAC7CG,EAAaH,GAAkB,KAAK,MAAMA,CAAK,EAExCI,EAAwB,CACnC,KAAML,EACN,KAAMA,EACN,KAAMG,EACN,KAAMD,EACN,OAAQF,EACR,OAAQA,EACR,KAAMI,EACN,MAAOA,CACT,EAGO,SAASE,EACdL,EACAM,EACO,CACP,IAAIC,EAAI,EACJC,EAAO,KACPC,EAAM,GACNC,EAAS,GACTC,EAAO,EACPC,EAEJ,SAASC,EAAKC,EAAoB,CAChC,IAAMC,EAAK,CAAC,EACZ,KAAOR,EAAIO,EAAE,OAAQP,IAAK,CAExB,GADAC,EAAOM,EAAEP,CAAC,EACNG,EACEF,IAAS,KACXC,GAAOK,EAAE,EAAEP,CAAC,EACHC,IAAS,KAClBO,EAAG,KAAKT,EAASA,EAAOG,CAAG,EAAIA,CAAG,EAClCA,EAAM,GACNC,EAASI,EAAEP,EAAI,CAAC,IAAM,IACtBI,EAAOJ,EAAI,GAEXE,GAAOD,UAEAA,IAAS,IAClBE,EAAS,WACAF,IAAS,IAClBG,EAAO,EAAEJ,EACTQ,EAAG,KAAKF,EAAKC,CAAC,CAAC,UACNN,IAAS,IAAK,CACvBE,EAAS,GACTC,EAAOJ,GACLQ,EAAG,KAAKT,EAASA,EAAOQ,EAAE,MAAMH,EAAMJ,CAAC,CAA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CAEQ,QAAe,CACrB,KAAK,YAAY,QAASH,GAAa,CACrCA,EAAS,KAAK,SAAS,CACzB,CAAC,CACH,CACF","names":["parseNumber","value","parseBool","parseBigInt","parseJson","defaultParser","pgArrayParser","parser","i","char","str","quoted","last","p","loop","x","xs","MessageParser","__spreadValues","messages","schema","key","row","_b","columnInfo","_a","typ","dimensions","additionalInfo","__objRest","identityParser","v","typParser","makeNullableParser","notNullable","isChangeMessage","message","isControlMessage","BackoffDefaults","MessageProcessor","callback","messages","__async","FetchError","_FetchError","status","text","json","headers","url","message","response","contentType","ShapeStream","options","_a","_b","_c","__spreadValues","MessageParser","args","where","signal","fetchUrl","maybeResponse","e","newShapeId","shapeId","lastOffset","getSchema","schemaHeader","batch","lastMessage","isControlMessage","onError","subscriptionId","subscriber","_","error","errorFn","errorCallback","initialDelay","maxDelay","multiplier","delay","attempt","result","resolve","Shape","stream","unsubscribe","dataMayHaveChanged","isUpToDate","newlyUpToDate","isChangeMessage"]}
package/dist/index.d.ts CHANGED
@@ -3,10 +3,12 @@ type Value = string | number | boolean | bigint | null | Value[] | {
3
3
  };
4
4
  type Offset = `-1` | `${number}_${number}`;
5
5
  interface Header {
6
- [key: string]: Value;
6
+ [key: Exclude<string, `operation` | `control`>]: Value;
7
7
  }
8
8
  type ControlMessage = {
9
- headers: Header;
9
+ headers: Header & {
10
+ control: `up-to-date` | `must-refetch`;
11
+ };
10
12
  };
11
13
  type ChangeMessage<T> = {
12
14
  key: string;
@@ -241,4 +243,45 @@ declare class Shape {
241
243
  private notify;
242
244
  }
243
245
 
244
- export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type Message, type NumericColumn, type Offset, type RegularColumn, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn };
246
+ /**
247
+ * Type guard for checking {@link Message} is {@link ChangeMessage}.
248
+ *
249
+ * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)
250
+ * for information on how to use type guards.
251
+ *
252
+ * @param message - the message to check
253
+ * @returns true if the message is a {@link ChangeMessage}
254
+ *
255
+ * @example
256
+ * ```ts
257
+ * if (isChangeMessage(message)) {
258
+ * const msgChng: ChangeMessage = message // Ok
259
+ * const msgCtrl: ControlMessage = message // Err, type mismatch
260
+ * }
261
+ * ```
262
+ */
263
+ declare function isChangeMessage<T extends Value = {
264
+ [key: string]: Value;
265
+ }>(message: Message<T>): message is ChangeMessage<T>;
266
+ /**
267
+ * Type guard for checking {@link Message} is {@link ControlMessage}.
268
+ *
269
+ * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)
270
+ * for information on how to use type guards.
271
+ *
272
+ * @param message - the message to check
273
+ * @returns true if the message is a {@link ControlMessage}
274
+ *
275
+ * * @example
276
+ * ```ts
277
+ * if (isControlMessage(message)) {
278
+ * const msgChng: ChangeMessage = message // Err, type mismatch
279
+ * const msgCtrl: ControlMessage = message // Ok
280
+ * }
281
+ * ```
282
+ */
283
+ declare function isControlMessage<T extends Value = {
284
+ [key: string]: Value;
285
+ }>(message: Message<T>): message is ControlMessage;
286
+
287
+ export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type Message, type NumericColumn, type Offset, type RegularColumn, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage };
@@ -125,6 +125,14 @@ function makeNullableParser(parser, notNullable) {
125
125
  return parser;
126
126
  }
127
127
 
128
+ // src/helpers.ts
129
+ function isChangeMessage(message) {
130
+ return `key` in message;
131
+ }
132
+ function isControlMessage(message) {
133
+ return !isChangeMessage(message);
134
+ }
135
+
128
136
  // src/client.ts
129
137
  var BackoffDefaults = {
130
138
  initialDelay: 100,
@@ -194,7 +202,7 @@ var ShapeStream = class {
194
202
  this.start();
195
203
  }
196
204
  async start() {
197
- var _a, _b;
205
+ var _a;
198
206
  this.isUpToDate = false;
199
207
  const { url, where, signal } = this.options;
200
208
  while (!(signal == null ? void 0 : signal.aborted) && !this.isUpToDate || this.options.subscribe) {
@@ -243,7 +251,7 @@ var ShapeStream = class {
243
251
  const batch = this.messageParser.parse(messages, this.schema);
244
252
  if (batch.length > 0) {
245
253
  const lastMessage = batch[batch.length - 1];
246
- if (((_b = lastMessage.headers) == null ? void 0 : _b[`control`]) === `up-to-date` && !this.isUpToDate) {
254
+ if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date` && !this.isUpToDate) {
247
255
  this.isUpToDate = true;
248
256
  this.notifyUpToDateSubscribers();
249
257
  }
@@ -401,8 +409,7 @@ var Shape = class {
401
409
  let isUpToDate = false;
402
410
  let newlyUpToDate = false;
403
411
  messages.forEach((message) => {
404
- var _a, _b;
405
- if (`key` in message) {
412
+ if (isChangeMessage(message)) {
406
413
  dataMayHaveChanged = [`insert`, `update`, `delete`].includes(
407
414
  message.headers.operation
408
415
  );
@@ -418,18 +425,22 @@ var Shape = class {
418
425
  break;
419
426
  }
420
427
  }
421
- if (((_a = message.headers) == null ? void 0 : _a[`control`]) === `up-to-date`) {
422
- isUpToDate = true;
423
- if (!this.hasNotifiedSubscribersUpToDate) {
424
- newlyUpToDate = true;
428
+ if (isControlMessage(message)) {
429
+ switch (message.headers.control) {
430
+ case `up-to-date`:
431
+ isUpToDate = true;
432
+ if (!this.hasNotifiedSubscribersUpToDate) {
433
+ newlyUpToDate = true;
434
+ }
435
+ break;
436
+ case `must-refetch`:
437
+ this.data.clear();
438
+ this.error = false;
439
+ isUpToDate = false;
440
+ newlyUpToDate = false;
441
+ break;
425
442
  }
426
443
  }
427
- if (((_b = message.headers) == null ? void 0 : _b[`control`]) === `must-refetch`) {
428
- this.data.clear();
429
- this.error = false;
430
- isUpToDate = false;
431
- newlyUpToDate = false;
432
- }
433
444
  });
434
445
  if (newlyUpToDate || isUpToDate && dataMayHaveChanged) {
435
446
  this.hasNotifiedSubscribersUpToDate = true;
@@ -439,6 +450,7 @@ var Shape = class {
439
450
  handleError(e) {
440
451
  if (e instanceof FetchError) {
441
452
  this.error = e;
453
+ this.notify();
442
454
  }
443
455
  }
444
456
  notify() {
@@ -451,6 +463,8 @@ export {
451
463
  BackoffDefaults,
452
464
  FetchError,
453
465
  Shape,
454
- ShapeStream
466
+ ShapeStream,
467
+ isChangeMessage,
468
+ isControlMessage
455
469
  };
456
470
  //# sourceMappingURL=index.legacy-esm.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/parser.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ArgumentsType } from 'vitest'\nimport { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: ArgumentsType<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[])\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 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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (`key` in 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 (message.headers?.[`control`] === `up-to-date`) {\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n }\n\n if (message.headers?.[`control`] === `must-refetch`) {\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\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 }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback(this.valueSync)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,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;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,cACd,OACA,QACO;AACP,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,KA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ACpB,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,YAAI,aAAQ,YAAR,mBAAkB,gBAAe,cAAc;AACjD,qBAAa;AACb,YAAI,CAAC,KAAK,gCAAgC;AACxC,0BAAgB;AAAA,QAClB;AAAA,MACF;AAEA,YAAI,aAAQ,YAAR,mBAAkB,gBAAe,gBAAgB;AACnD,aAAK,KAAK,MAAM;AAChB,aAAK,QAAQ;AACb,qBAAa;AACb,wBAAgB;AAAA,MAClB;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;AAAA,IACf;AAAA,EACF;AAAA,EAEQ,SAAe;AACrB,SAAK,YAAY,QAAQ,CAAC,aAAa;AACrC,eAAS,KAAK,SAAS;AAAA,IACzB,CAAC;AAAA,EACH;AACF;","names":["key"]}
1
+ {"version":3,"sources":["../src/parser.ts","../src/helpers.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ChangeMessage, ControlMessage, Message, Value } 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 Value = { [key: string]: Value }>(\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 Value = { [key: string]: Value }>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(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[])\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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,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;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,cACd,OACA,QACO;AACP,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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mjs CHANGED
@@ -145,6 +145,14 @@ function makeNullableParser(parser, notNullable) {
145
145
  return parser;
146
146
  }
147
147
 
148
+ // src/helpers.ts
149
+ function isChangeMessage(message) {
150
+ return `key` in message;
151
+ }
152
+ function isControlMessage(message) {
153
+ return !isChangeMessage(message);
154
+ }
155
+
148
156
  // src/client.ts
149
157
  var BackoffDefaults = {
150
158
  initialDelay: 100,
@@ -219,7 +227,7 @@ var ShapeStream = class {
219
227
  }
220
228
  start() {
221
229
  return __async(this, null, function* () {
222
- var _a, _b;
230
+ var _a;
223
231
  this.isUpToDate = false;
224
232
  const { url, where, signal } = this.options;
225
233
  while (!(signal == null ? void 0 : signal.aborted) && !this.isUpToDate || this.options.subscribe) {
@@ -268,7 +276,7 @@ var ShapeStream = class {
268
276
  const batch = this.messageParser.parse(messages, this.schema);
269
277
  if (batch.length > 0) {
270
278
  const lastMessage = batch[batch.length - 1];
271
- if (((_b = lastMessage.headers) == null ? void 0 : _b[`control`]) === `up-to-date` && !this.isUpToDate) {
279
+ if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date` && !this.isUpToDate) {
272
280
  this.isUpToDate = true;
273
281
  this.notifyUpToDateSubscribers();
274
282
  }
@@ -429,8 +437,7 @@ var Shape = class {
429
437
  let isUpToDate = false;
430
438
  let newlyUpToDate = false;
431
439
  messages.forEach((message) => {
432
- var _a, _b;
433
- if (`key` in message) {
440
+ if (isChangeMessage(message)) {
434
441
  dataMayHaveChanged = [`insert`, `update`, `delete`].includes(
435
442
  message.headers.operation
436
443
  );
@@ -446,18 +453,22 @@ var Shape = class {
446
453
  break;
447
454
  }
448
455
  }
449
- if (((_a = message.headers) == null ? void 0 : _a[`control`]) === `up-to-date`) {
450
- isUpToDate = true;
451
- if (!this.hasNotifiedSubscribersUpToDate) {
452
- newlyUpToDate = true;
456
+ if (isControlMessage(message)) {
457
+ switch (message.headers.control) {
458
+ case `up-to-date`:
459
+ isUpToDate = true;
460
+ if (!this.hasNotifiedSubscribersUpToDate) {
461
+ newlyUpToDate = true;
462
+ }
463
+ break;
464
+ case `must-refetch`:
465
+ this.data.clear();
466
+ this.error = false;
467
+ isUpToDate = false;
468
+ newlyUpToDate = false;
469
+ break;
453
470
  }
454
471
  }
455
- if (((_b = message.headers) == null ? void 0 : _b[`control`]) === `must-refetch`) {
456
- this.data.clear();
457
- this.error = false;
458
- isUpToDate = false;
459
- newlyUpToDate = false;
460
- }
461
472
  });
462
473
  if (newlyUpToDate || isUpToDate && dataMayHaveChanged) {
463
474
  this.hasNotifiedSubscribersUpToDate = true;
@@ -467,6 +478,7 @@ var Shape = class {
467
478
  handleError(e) {
468
479
  if (e instanceof FetchError) {
469
480
  this.error = e;
481
+ this.notify();
470
482
  }
471
483
  }
472
484
  notify() {
@@ -479,6 +491,8 @@ export {
479
491
  BackoffDefaults,
480
492
  FetchError,
481
493
  Shape,
482
- ShapeStream
494
+ ShapeStream,
495
+ isChangeMessage,
496
+ isControlMessage
483
497
  };
484
498
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/parser.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ArgumentsType } from 'vitest'\nimport { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(options.parser)\n\n this.backoffOptions = options.backoffOptions ?? BackoffDefaults\n this.fetchClient =\n options.fetchClient ??\n ((...args: ArgumentsType<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[])\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 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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): void {\n let dataMayHaveChanged = false\n let isUpToDate = false\n let newlyUpToDate = false\n\n messages.forEach((message) => {\n if (`key` in 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 (message.headers?.[`control`] === `up-to-date`) {\n isUpToDate = true\n if (!this.hasNotifiedSubscribersUpToDate) {\n newlyUpToDate = true\n }\n }\n\n if (message.headers?.[`control`] === `must-refetch`) {\n this.data.clear()\n this.error = false\n isUpToDate = false\n newlyUpToDate = false\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 }\n }\n\n private notify(): void {\n this.subscribers.forEach((callback) => {\n callback(this.valueSync)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,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;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,cACd,OACA,QACO;AACP,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,GA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ources":["../src/parser.ts","../src/helpers.ts","../src/client.ts"],"sourcesContent":["import { ColumnInfo, Message, Schema, Value } from './types'\n\nexport type ParseFunction = (\n value: string,\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)\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(\n value: string,\n parser?: (s: string) => Value\n): 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 {\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[] {\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 string, schema)\n })\n }\n return value\n }) as Message[]\n }\n\n // Parses the message values using the provided parser based on the schema information\n private parseRow(key: string, value: string, 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 identityParser = (v: string) => v\n const typParser = this.parser[typ] ?? identityParser\n const parser = makeNullableParser(typParser, columnInfo.not_null)\n\n if (dimensions && dimensions > 0) {\n // It's an array\n return pgArrayParser(value, parser)\n }\n\n return parser(value, additionalInfo)\n }\n}\n\nfunction makeNullableParser(\n parser: ParseFunction,\n notNullable?: boolean\n): ParseFunction {\n const isNullable = !(notNullable ?? false)\n if (isNullable) {\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: string) =>\n value === null || value === `NULL` ? null : parser(value)\n }\n return parser\n}\n","import { ChangeMessage, ControlMessage, Message, Value } 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 Value = { [key: string]: Value }>(\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 Value = { [key: string]: Value }>(\n message: Message<T>\n): message is ControlMessage {\n return !isChangeMessage(message)\n}\n","import { Message, Value, Offset, Schema } from './types'\nimport { MessageParser, Parser } from './parser'\nimport { isChangeMessage, isControlMessage } from './helpers'\n\nexport type ShapeData = Map<string, { [key: string]: Value }>\nexport type ShapeChangedCallback = (value: ShapeData) => 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 {\n private messageQueue: Message[][] = []\n private isProcessing = false\n private callback: (messages: Message[]) => void | Promise<void>\n\n constructor(callback: (messages: Message[]) => void | Promise<void>) {\n this.callback = callback\n }\n\n process(messages: Message[]) {\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\n *\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 * 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 */\nexport class ShapeStream {\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, ((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\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(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[])\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[]) => 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[]) {\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 {Shape}\n *\n * const shapeStream = new ShapeStream(url: 'http://localhost:3000/v1/shape/foo'})\n * const shape = new Shape(shapeStream)\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 {\n private stream: ShapeStream\n\n private data: ShapeData = new Map()\n private subscribers = new Map<number, ShapeChangedCallback>()\n public error: FetchError | false = false\n private hasNotifiedSubscribersUpToDate: boolean = false\n\n constructor(stream: ShapeStream) {\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> {\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): () => 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[]): 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,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;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,cACd,OACA,QACO;AACP,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,EA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B;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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@electric-sql/client",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Postgres everywhere - your data, in sync, wherever you need it.",
5
5
  "type": "module",
6
6
  "main": "dist/cjs/index.cjs",
package/src/client.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { ArgumentsType } from 'vitest'
2
1
  import { Message, Value, Offset, Schema } from './types'
3
2
  import { MessageParser, Parser } from './parser'
3
+ import { isChangeMessage, isControlMessage } from './helpers'
4
4
 
5
5
  export type ShapeData = Map<string, { [key: string]: Value }>
6
6
  export type ShapeChangedCallback = (value: ShapeData) => void
@@ -196,7 +196,7 @@ export class ShapeStream {
196
196
  this.backoffOptions = options.backoffOptions ?? BackoffDefaults
197
197
  this.fetchClient =
198
198
  options.fetchClient ??
199
- ((...args: ArgumentsType<typeof fetch>) => fetch(...args))
199
+ ((...args: Parameters<typeof fetch>) => fetch(...args))
200
200
 
201
201
  this.start()
202
202
  }
@@ -270,7 +270,8 @@ export class ShapeStream {
270
270
  if (batch.length > 0) {
271
271
  const lastMessage = batch[batch.length - 1]
272
272
  if (
273
- lastMessage.headers?.[`control`] === `up-to-date` &&
273
+ isControlMessage(lastMessage) &&
274
+ lastMessage.headers.control === `up-to-date` &&
274
275
  !this.isUpToDate
275
276
  ) {
276
277
  this.isUpToDate = true
@@ -514,7 +515,7 @@ export class Shape {
514
515
  let newlyUpToDate = false
515
516
 
516
517
  messages.forEach((message) => {
517
- if (`key` in message) {
518
+ if (isChangeMessage(message)) {
518
519
  dataMayHaveChanged = [`insert`, `update`, `delete`].includes(
519
520
  message.headers.operation
520
521
  )
@@ -535,19 +536,22 @@ export class Shape {
535
536
  }
536
537
  }
537
538
 
538
- if (message.headers?.[`control`] === `up-to-date`) {
539
- isUpToDate = true
540
- if (!this.hasNotifiedSubscribersUpToDate) {
541
- newlyUpToDate = true
539
+ if (isControlMessage(message)) {
540
+ switch (message.headers.control) {
541
+ case `up-to-date`:
542
+ isUpToDate = true
543
+ if (!this.hasNotifiedSubscribersUpToDate) {
544
+ newlyUpToDate = true
545
+ }
546
+ break
547
+ case `must-refetch`:
548
+ this.data.clear()
549
+ this.error = false
550
+ isUpToDate = false
551
+ newlyUpToDate = false
552
+ break
542
553
  }
543
554
  }
544
-
545
- if (message.headers?.[`control`] === `must-refetch`) {
546
- this.data.clear()
547
- this.error = false
548
- isUpToDate = false
549
- newlyUpToDate = false
550
- }
551
555
  })
552
556
 
553
557
  // Always notify subscribers when the Shape first is up to date.
@@ -561,6 +565,7 @@ export class Shape {
561
565
  private handleError(e: Error): void {
562
566
  if (e instanceof FetchError) {
563
567
  this.error = e
568
+ this.notify()
564
569
  }
565
570
  }
566
571
 
package/src/helpers.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { ChangeMessage, ControlMessage, Message, Value } from './types'
2
+
3
+ /**
4
+ * Type guard for checking {@link Message} is {@link ChangeMessage}.
5
+ *
6
+ * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)
7
+ * for information on how to use type guards.
8
+ *
9
+ * @param message - the message to check
10
+ * @returns true if the message is a {@link ChangeMessage}
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * if (isChangeMessage(message)) {
15
+ * const msgChng: ChangeMessage = message // Ok
16
+ * const msgCtrl: ControlMessage = message // Err, type mismatch
17
+ * }
18
+ * ```
19
+ */
20
+ export function isChangeMessage<T extends Value = { [key: string]: Value }>(
21
+ message: Message<T>
22
+ ): message is ChangeMessage<T> {
23
+ return `key` in message
24
+ }
25
+
26
+ /**
27
+ * Type guard for checking {@link Message} is {@link ControlMessage}.
28
+ *
29
+ * See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)
30
+ * for information on how to use type guards.
31
+ *
32
+ * @param message - the message to check
33
+ * @returns true if the message is a {@link ControlMessage}
34
+ *
35
+ * * @example
36
+ * ```ts
37
+ * if (isControlMessage(message)) {
38
+ * const msgChng: ChangeMessage = message // Err, type mismatch
39
+ * const msgCtrl: ControlMessage = message // Ok
40
+ * }
41
+ * ```
42
+ */
43
+ export function isControlMessage<T extends Value = { [key: string]: Value }>(
44
+ message: Message<T>
45
+ ): message is ControlMessage {
46
+ return !isChangeMessage(message)
47
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './client'
2
2
  export * from './types'
3
+ export * from './helpers'
package/src/types.ts CHANGED
@@ -10,11 +10,11 @@ export type Value =
10
10
  export type Offset = `-1` | `${number}_${number}`
11
11
 
12
12
  interface Header {
13
- [key: string]: Value
13
+ [key: Exclude<string, `operation` | `control`>]: Value
14
14
  }
15
15
 
16
16
  export type ControlMessage = {
17
- headers: Header
17
+ headers: Header & { control: `up-to-date` | `must-refetch` }
18
18
  }
19
19
 
20
20
  export type ChangeMessage<T> = {