@rocicorp/zero 0.25.1 → 0.26.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- const version = "0.25.1";
1
+ const version = "0.26.0-canary.0";
2
2
  const packageJson = {
3
3
  version
4
4
  };
@@ -1,4 +1,5 @@
1
1
  import type { LogContext } from '@rocicorp/logger';
2
+ import { Subscribable } from '../../../shared/src/subscribable.ts';
2
3
  import type { ConnectionManager } from './connection-manager.ts';
3
4
  /**
4
5
  * The current connection state of the Zero instance. One of the following states:
@@ -87,10 +88,9 @@ export declare class ConnectionImpl implements Connection {
87
88
  auth: string | null | undefined;
88
89
  }): Promise<void>;
89
90
  }
90
- export declare class ConnectionSource implements Source<ConnectionState> {
91
+ export declare class ConnectionSource extends Subscribable<ConnectionState> implements Source<ConnectionState> {
91
92
  #private;
92
93
  constructor(connectionManager: ConnectionManager);
93
94
  get current(): ConnectionState;
94
- subscribe: (listener: (s: ConnectionState) => void) => (() => void);
95
95
  }
96
96
  //# sourceMappingURL=connection.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAIjD,OAAO,KAAK,EACV,iBAAiB,EAElB,MAAM,yBAAyB,CAAC;AAGjC;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,eAAe,GACvB;IACE,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;CAChB,GACD;IACE,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GACD;IACE,IAAI,EAAE,WAAW,CAAC;CACnB,GACD;IACE,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EACF;QACE,IAAI,EAAE,QAAQ,CAAC;QACf,MAAM,EAAE,G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;AAED,qBAAa,gBAAiB,YAAW,MAAM,CAAC,eAAe,CAAC;;gBAIlD,iBAAiB,EAAE,iBAAiB;IAKhD,IAAI,OAAO,IAAI,eAAe,CAE7B;IAED,SAAS,GAAI,UAAU,CAAC,CAAC,EAAE,eAAe,KAAK,IAAI,KAAG,CAAC,MAAM,IAAI,CAAC,CAI7D;CA0DN"}
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAC,YAAY,EAAC,MAAM,qCAAqC,CAAC;AAGjE,OAAO,KAAK,EACV,iBAAiB,EAElB,MAAM,yBAAyB,CAAC;AAGjC;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,eAAe,GACvB;IACE,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;CAChB,GACD;IACE,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GACD;IACE,IAAI,EAAE,WAAW,CAAC;CACnB,GACD;IACE,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EACF;QACE,IAAI,EAAE,QAAQ,CAAC;QACf,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;QAClB,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,GACD;QACE,IAAI,EAAE,OAAO,CAAC;QACd,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;QAClB,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,GACD;QACE,IAAI,EAAE,YAAY,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACP,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEN,MAAM,WAAW,MAAM,CAAC,CAAC;IACvB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IAEpB;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;IAExC;;;;;;;;;;OAUG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE;QAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;KAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClE;AAED,qBAAa,cAAe,YAAW,UAAU;;gBAO7C,iBAAiB,EAAE,iBAAiB,EACpC,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,KAAK,IAAI;IAQpD,IAAI,KAAK,IAAI,MAAM,CAAC,eAAe,CAAC,CAEnC;IAEK,OAAO,CAAC,IAAI,CAAC,EAAE;QAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;KAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAsCvE;AAED,qBAAa,gBACX,SAAQ,YAAY,CAAC,eAAe,CACpC,YAAW,MAAM,CAAC,eAAe,CAAC;;gBAItB,iBAAiB,EAAE,iBAAiB;IAchD,IAAI,OAAO,IAAI,eAAe,CAE7B;CA0DF"}
@@ -1,4 +1,5 @@
1
1
  import { unreachable } from "../../../shared/src/asserts.js";
2
+ import { Subscribable } from "../../../shared/src/subscribable.js";
2
3
  import { PushFailed, TransformFailed } from "../../../zero-protocol/src/error-kind-enum.js";
3
4
  import { NoSocketOrigin } from "./client-error-kind-enum.js";
4
5
  import { Disconnected, NeedsAuth, Error, Connecting, Connected, Closed } from "./connection-status-enum.js";
@@ -42,20 +43,19 @@ class ConnectionImpl {
42
43
  await nextStatePromise;
43
44
  }
44
45
  }
45
- class ConnectionSource {
46
+ class ConnectionSource extends Subscribable {
46
47
  #state;
47
- #connectionManager;
48
48
  constructor(connectionManager) {
49
- this.#connectionManager = connectionManager;
49
+ super();
50
50
  this.#state = this.#mapConnectionManagerState(connectionManager.state);
51
+ connectionManager.subscribe((state) => {
52
+ this.#state = this.#mapConnectionManagerState(state);
53
+ this.notify(this.#state);
54
+ });
51
55
  }
52
56
  get current() {
53
57
  return this.#state;
54
58
  }
55
- subscribe = (listener) => this.#connectionManager.subscribe((state) => {
56
- this.#state = this.#mapConnectionManagerState(state);
57
- listener(this.#state);
58
- });
59
59
  #mapConnectionManagerState(state) {
60
60
  switch (state.name) {
61
61
  case Closed:
@@ -1 +1 @@
1
- {"version":3,"file":"connection.js","sources":["../../../../../zero-client/src/client/connection.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {unreachable} from '../../../shared/src/asserts.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport {ClientErrorKind} from './client-error-kind.ts';\nimport type {\n ConnectionManager,\n ConnectionManagerState,\n} from './connection-manager.ts';\nimport {ConnectionStatus} from './connection-status.ts';\n\n/**\n * The current connection state of the Zero instance. One of the following states:\n *\n * - `connecting`: The client is actively trying to connect every 5 seconds.\n * - `disconnected`: The client is now in an \"offline\" state. It will continue\n * to try to connect every 5 seconds.\n * - `connected`: The client has opened a successful connection to the server.\n * - `needs-auth`: Authentication is invalid or expired. No connection retries will be made\n * until the host application calls `connect()`.\n * - `error`: A fatal error occurred. No connection retries will be made until the host\n * application calls `connect()` again.\n * - `closed`: The client was shut down (for example via `zero.close()`). This is\n * a terminal state, and a new Zero instance must be created to reconnect.\n */\nexport type ConnectionState =\n | {\n name: 'disconnected';\n reason: string;\n }\n | {\n name: 'connecting';\n reason?: string;\n }\n | {\n name: 'connected';\n }\n | {\n name: 'needs-auth';\n reason:\n | {\n type: 'mutate';\n status: 401 | 403;\n body?: string;\n }\n | {\n type: 'query';\n status: 401 | 403;\n body?: string;\n }\n | {\n type: 'zero-cache';\n reason: string;\n };\n }\n | {\n name: 'error';\n reason: string;\n }\n | {\n name: 'closed';\n reason: string;\n };\n\nexport interface Source<T> {\n /**\n * The current state value.\n */\n readonly current: T;\n\n /**\n * Subscribe to state changes.\n *\n * @param listener - Called when the state changes with the new state value.\n * @returns A function to unsubscribe from state changes.\n */\n subscribe(listener: (state: T) => void): () => void;\n}\n\n/**\n * Connection API for managing Zero's connection lifecycle.\n */\nexport interface Connection {\n /**\n * The current connection state as a subscribable value.\n */\n readonly state: Source<ConnectionState>;\n\n /**\n * Resumes the connection from a terminal state.\n *\n * If called when not in a terminal state, this method does nothing.\n *\n * @param opts - Optional connection options\n * @param opts.auth - Token to use for authentication. If provided, this overrides\n * the stored auth credential for this connection attempt.\n * If `null` or `undefined`, the stored auth credential is cleared.\n * @returns A promise that resolves once the connection state has transitioned to connecting.\n */\n connect(opts?: {auth: string | null | undefined}): Promise<void>;\n}\n\nexport class ConnectionImpl implements Connection {\n readonly #connectionManager: ConnectionManager;\n readonly #lc: LogContext;\n readonly #source: ConnectionSource;\n readonly #setAuth: (auth: string | null | undefined) => void;\n\n constructor(\n connectionManager: ConnectionManager,\n lc: LogContext,\n setAuth: (auth: string | null | undefined) => void,\n ) {\n this.#connectionManager = connectionManager;\n this.#lc = lc;\n this.#source = new ConnectionSource(connectionManager);\n this.#setAuth = setAuth;\n }\n\n get state(): Source<ConnectionState> {\n return this.#source;\n }\n\n async connect(opts?: {auth: string | null | undefined}): Promise<void> {\n const lc = this.#lc.withContext('connect');\n\n if (opts && 'auth' in opts) {\n lc.debug?.('Updating auth credential from connect()');\n this.#setAuth(opts.auth);\n }\n\n // if the connection is disconnected due to a missing cacheURL, we don't allow a reconnect\n if (\n this.#connectionManager.state.name === ConnectionStatus.Disconnected &&\n this.#connectionManager.state.reason.kind ===\n ClientErrorKind.NoSocketOrigin\n ) {\n lc.error?.(\n 'connect() called but the connection is disconnected due to a missing cacheURL. No reconnect will be attempted.',\n );\n return;\n }\n\n // only allow connect() to be called from a terminal state\n if (!this.#connectionManager.isInTerminalState()) {\n lc.debug?.(\n 'connect() called but not in a terminal state. Current state:',\n this.#connectionManager.state.name,\n );\n return;\n }\n\n lc.info?.(\n `Resuming connection from state: ${this.#connectionManager.state.name}`,\n );\n\n // Transition to connecting, which will trigger the state change resolver\n // and unblock the run loop. Wait for the next state change (connected, disconnected, etc.)\n const {nextStatePromise} = this.#connectionManager.connecting();\n await nextStatePromise;\n }\n}\n\nexport class ConnectionSource implements Source<ConnectionState> {\n #state: ConnectionState;\n readonly #connectionManager: ConnectionManager;\n\n constructor(connectionManager: ConnectionManager) {\n this.#connectionManager = connectionManager;\n this.#state = this.#mapConnectionManagerState(connectionManager.state);\n }\n\n get current(): ConnectionState {\n return this.#state;\n }\n\n subscribe = (listener: (s: ConnectionState) => void): (() => void) =>\n this.#connectionManager.subscribe(state => {\n this.#state = this.#mapConnectionManagerState(state);\n listener(this.#state);\n });\n\n #mapConnectionManagerState(state: ConnectionManagerState): ConnectionState {\n switch (state.name) {\n case ConnectionStatus.Closed:\n return {\n name: 'closed',\n reason: state.reason.message,\n };\n case ConnectionStatus.Connected:\n return {\n name: 'connected',\n };\n case ConnectionStatus.Connecting:\n return {\n name: 'connecting',\n ...(state.reason?.message ? {reason: state.reason.message} : {}),\n };\n case ConnectionStatus.Disconnected:\n return {\n name: 'disconnected',\n reason: state.reason.message,\n };\n case ConnectionStatus.Error:\n return {\n name: 'error',\n reason: state.reason.message,\n };\n case ConnectionStatus.NeedsAuth:\n return {\n name: 'needs-auth',\n reason:\n state.reason.errorBody.kind === ErrorKind.PushFailed\n ? {\n type: 'mutate',\n status: state.reason.errorBody.status,\n ...(state.reason.errorBody.bodyPreview\n ? {body: state.reason.errorBody.bodyPreview}\n : {}),\n }\n : state.reason.errorBody.kind === ErrorKind.TransformFailed\n ? {\n type: 'query',\n status: state.reason.errorBody.status,\n ...(state.reason.errorBody.bodyPreview\n ? {body: state.reason.errorBody.bodyPreview}\n : {}),\n }\n : {\n type: 'zero-cache',\n reason: state.reason.message,\n },\n };\n\n default:\n unreachable(state);\n }\n }\n}\n"],"names":["ConnectionStatus.Disconnected","ClientErrorKind.NoSocketOrigin","ConnectionStatus.Closed","ConnectionStatus.Connected","ConnectionStatus.Connecting","ConnectionStatus.Error","ConnectionStatus.NeedsAuth","ErrorKind.PushFailed","ErrorKind.TransformFailed"],"mappings":";;;;AAqGO,MAAM,eAAqC;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YACE,mBACA,IACA,SACA;AACA,SAAK,qBAAqB;AAC1B,SAAK,MAAM;AACX,SAAK,UAAU,IAAI,iBAAiB,iBAAiB;AACrD,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,IAAI,QAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,QAAQ,MAAyD;AACrE,UAAM,KAAK,KAAK,IAAI,YAAY,SAAS;AAEzC,QAAI,QAAQ,UAAU,MAAM;AAC1B,SAAG,QAAQ,yCAAyC;AACpD,WAAK,SAAS,KAAK,IAAI;AAAA,IACzB;AAGA,QACE,KAAK,mBAAmB,MAAM,SAASA,gBACvC,KAAK,mBAAmB,MAAM,OAAO,SACnCC,gBACF;AACA,SAAG;AAAA,QACD;AAAA,MAAA;AAEF;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,mBAAmB,qBAAqB;AAChD,SAAG;AAAA,QACD;AAAA,QACA,KAAK,mBAAmB,MAAM;AAAA,MAAA;AAEhC;AAAA,IACF;AAEA,OAAG;AAAA,MACD,mCAAmC,KAAK,mBAAmB,MAAM,IAAI;AAAA,IAAA;AAKvE,UAAM,EAAC,iBAAA,IAAoB,KAAK,mBAAmB,WAAA;AACnD,UAAM;AAAA,EACR;AACF;AAEO,MAAM,iBAAoD;AAAA,EAC/D;AAAA,EACS;AAAA,EAET,YAAY,mBAAsC;A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AEP;AAAA,YACE,MAAM;AAAA,YACN,QAAQ,MAAM,OAAO;AAAA,UAAA;AAAA,QACvB;AAAA,MAGZ;AACE,oBAAiB;AAAA,IAAA;AAAA,EAEvB;AACF;"}
1
+ {"version":3,"file":"connection.js","sources":["../../../../../zero-client/src/client/connection.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {unreachable} from '../../../shared/src/asserts.ts';\nimport {Subscribable} from '../../../shared/src/subscribable.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport {ClientErrorKind} from './client-error-kind.ts';\nimport type {\n ConnectionManager,\n ConnectionManagerState,\n} from './connection-manager.ts';\nimport {ConnectionStatus} from './connection-status.ts';\n\n/**\n * The current connection state of the Zero instance. One of the following states:\n *\n * - `connecting`: The client is actively trying to connect every 5 seconds.\n * - `disconnected`: The client is now in an \"offline\" state. It will continue\n * to try to connect every 5 seconds.\n * - `connected`: The client has opened a successful connection to the server.\n * - `needs-auth`: Authentication is invalid or expired. No connection retries will be made\n * until the host application calls `connect()`.\n * - `error`: A fatal error occurred. No connection retries will be made until the host\n * application calls `connect()` again.\n * - `closed`: The client was shut down (for example via `zero.close()`). This is\n * a terminal state, and a new Zero instance must be created to reconnect.\n */\nexport type ConnectionState =\n | {\n name: 'disconnected';\n reason: string;\n }\n | {\n name: 'connecting';\n reason?: string;\n }\n | {\n name: 'connected';\n }\n | {\n name: 'needs-auth';\n reason:\n | {\n type: 'mutate';\n status: 401 | 403;\n body?: string;\n }\n | {\n type: 'query';\n status: 401 | 403;\n body?: string;\n }\n | {\n type: 'zero-cache';\n reason: string;\n };\n }\n | {\n name: 'error';\n reason: string;\n }\n | {\n name: 'closed';\n reason: string;\n };\n\nexport interface Source<T> {\n /**\n * The current state value.\n */\n readonly current: T;\n\n /**\n * Subscribe to state changes.\n *\n * @param listener - Called when the state changes with the new state value.\n * @returns A function to unsubscribe from state changes.\n */\n subscribe(listener: (state: T) => void): () => void;\n}\n\n/**\n * Connection API for managing Zero's connection lifecycle.\n */\nexport interface Connection {\n /**\n * The current connection state as a subscribable value.\n */\n readonly state: Source<ConnectionState>;\n\n /**\n * Resumes the connection from a terminal state.\n *\n * If called when not in a terminal state, this method does nothing.\n *\n * @param opts - Optional connection options\n * @param opts.auth - Token to use for authentication. If provided, this overrides\n * the stored auth credential for this connection attempt.\n * If `null` or `undefined`, the stored auth credential is cleared.\n * @returns A promise that resolves once the connection state has transitioned to connecting.\n */\n connect(opts?: {auth: string | null | undefined}): Promise<void>;\n}\n\nexport class ConnectionImpl implements Connection {\n readonly #connectionManager: ConnectionManager;\n readonly #lc: LogContext;\n readonly #source: ConnectionSource;\n readonly #setAuth: (auth: string | null | undefined) => void;\n\n constructor(\n connectionManager: ConnectionManager,\n lc: LogContext,\n setAuth: (auth: string | null | undefined) => void,\n ) {\n this.#connectionManager = connectionManager;\n this.#lc = lc;\n this.#source = new ConnectionSource(connectionManager);\n this.#setAuth = setAuth;\n }\n\n get state(): Source<ConnectionState> {\n return this.#source;\n }\n\n async connect(opts?: {auth: string | null | undefined}): Promise<void> {\n const lc = this.#lc.withContext('connect');\n\n if (opts && 'auth' in opts) {\n lc.debug?.('Updating auth credential from connect()');\n this.#setAuth(opts.auth);\n }\n\n // if the connection is disconnected due to a missing cacheURL, we don't allow a reconnect\n if (\n this.#connectionManager.state.name === ConnectionStatus.Disconnected &&\n this.#connectionManager.state.reason.kind ===\n ClientErrorKind.NoSocketOrigin\n ) {\n lc.error?.(\n 'connect() called but the connection is disconnected due to a missing cacheURL. No reconnect will be attempted.',\n );\n return;\n }\n\n // only allow connect() to be called from a terminal state\n if (!this.#connectionManager.isInTerminalState()) {\n lc.debug?.(\n 'connect() called but not in a terminal state. Current state:',\n this.#connectionManager.state.name,\n );\n return;\n }\n\n lc.info?.(\n `Resuming connection from state: ${this.#connectionManager.state.name}`,\n );\n\n // Transition to connecting, which will trigger the state change resolver\n // and unblock the run loop. Wait for the next state change (connected, disconnected, etc.)\n const {nextStatePromise} = this.#connectionManager.connecting();\n await nextStatePromise;\n }\n}\n\nexport class ConnectionSource\n extends Subscribable<ConnectionState>\n implements Source<ConnectionState>\n{\n #state: ConnectionState;\n\n constructor(connectionManager: ConnectionManager) {\n super();\n this.#state = this.#mapConnectionManagerState(connectionManager.state);\n\n // Subscribe to ConnectionManager immediately to keep #state in sync.\n // This ensures `current` always returns the correct state, even if\n // external code hasn't subscribed yet (fixes race condition where\n // connection completes before React subscribes).\n connectionManager.subscribe(state => {\n this.#state = this.#mapConnectionManagerState(state);\n this.notify(this.#state);\n });\n }\n\n get current(): ConnectionState {\n return this.#state;\n }\n\n #mapConnectionManagerState(state: ConnectionManagerState): ConnectionState {\n switch (state.name) {\n case ConnectionStatus.Closed:\n return {\n name: 'closed',\n reason: state.reason.message,\n };\n case ConnectionStatus.Connected:\n return {\n name: 'connected',\n };\n case ConnectionStatus.Connecting:\n return {\n name: 'connecting',\n ...(state.reason?.message ? {reason: state.reason.message} : {}),\n };\n case ConnectionStatus.Disconnected:\n return {\n name: 'disconnected',\n reason: state.reason.message,\n };\n case ConnectionStatus.Error:\n return {\n name: 'error',\n reason: state.reason.message,\n };\n case ConnectionStatus.NeedsAuth:\n return {\n name: 'needs-auth',\n reason:\n state.reason.errorBody.kind === ErrorKind.PushFailed\n ? {\n type: 'mutate',\n status: state.reason.errorBody.status,\n ...(state.reason.errorBody.bodyPreview\n ? {body: state.reason.errorBody.bodyPreview}\n : {}),\n }\n : state.reason.errorBody.kind === ErrorKind.TransformFailed\n ? {\n type: 'query',\n status: state.reason.errorBody.status,\n ...(state.reason.errorBody.bodyPreview\n ? {body: state.reason.errorBody.bodyPreview}\n : {}),\n }\n : {\n type: 'zero-cache',\n reason: state.reason.message,\n },\n };\n\n default:\n unreachable(state);\n }\n }\n}\n"],"names":["ConnectionStatus.Disconnected","ClientErrorKind.NoSocketOrigin","ConnectionStatus.Closed","ConnectionStatus.Connected","ConnectionStatus.Connecting","ConnectionStatus.Error","ConnectionStatus.NeedsAuth","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YAAQ,MAAM,MAAA;AAAA,MACZ,KAAKC;AACH,eAAO;AAAA,UACL,MAAM;AAAA,UACN,QAAQ,MAAM,OAAO;AAAA,QAAA;AAAA,MAEzB,KAAKC;AACH,eAAO;AAAA,UACL,MAAM;AAAA,QAAA;AAAA,MAEV,KAAKC;AACH,eAAO;AAAA,UACL,MAAM;AAAA,UACN,GAAI,MAAM,QAAQ,UAAU,EAAC,QAAQ,MAAM,OAAO,YAAW,CAAA;AAAA,QAAC;AAAA,MAElE,KAAKJ;AACH,eAAO;AAAA,UACL,MAAM;AAAA,UACN,QAAQ,MAAM,OAAO;AAAA,QAAA;AAAA,MAEzB,KAAKK;AACH,eAAO;AAAA,UACL,MAAM;AAAA,UACN,QAAQ,MAAM,OAAO;AAAA,QAAA;AAAA,MAEzB,KAAKC;AACH,eAAO;AAAA,UACL,MAAM;AAAA,UACN,QACE,MAAM,OAAO,UAAU,SAASC,aAC5B;AAAA,YACE,MAAM;AAAA,YACN,QAAQ,MAAM,OAAO,UAAU;AAAA,YAC/B,GAAI,MAAM,OAAO,UAAU,cACvB,EAAC,MAAM,MAAM,OAAO,UAAU,gBAC9B,CAAA;AAAA,UAAC,IAEP,MAAM,OAAO,UAAU,SAASC,kBAC9B;AAAA,YACE,MAAM;AAAA,YACN,QAAQ,MAAM,OAAO,UAAU;AAAA,YAC/B,GAAI,MAAM,OAAO,UAAU,cACvB,EAAC,MAAM,MAAM,OAAO,UAAU,gBAC9B,CAAA;AAAA,UAAC,IAEP;AAAA,YACE,MAAM;AAAA,YACN,QAAQ,MAAM,OAAO;AAAA,UAAA;AAAA,QACvB;AAAA,MAGZ;AACE,oBAAiB;AAAA,IAAA;AAAA,EAEvB;AACF;"}
@@ -1,4 +1,4 @@
1
- const version = "0.25.1";
1
+ const version = "0.26.0-canary.0";
2
2
  export {
3
3
  version
4
4
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocicorp/zero",
3
- "version": "0.25.1",
3
+ "version": "0.26.0-canary.0",
4
4
  "description": "Zero is a web framework for serverless web development.",
5
5
  "author": "Rocicorp, Inc.",
6
6
  "repository": {