Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zero-cache): improve the error message for unsupported protocol versions #3462

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/zero-cache/src/workers/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
type PongMessage,
upstreamSchema,
} from '../../../zero-protocol/src/mod.js';
import {PROTOCOL_VERSION} from '../../../zero-protocol/src/protocol-version.js';
import {
MIN_SERVER_SUPPORTED_PROTOCOL_VERSION,
PROTOCOL_VERSION,
} from '../../../zero-protocol/src/protocol-version.js';
import type {ConnectParams} from '../services/dispatcher/connect-params.js';
import type {Mutagen} from '../services/mutagen/mutagen.js';
import type {
Expand Down Expand Up @@ -102,14 +105,16 @@ export class Connection {
*/
init() {
if (
this.#protocolVersion !== PROTOCOL_VERSION &&
this.#protocolVersion !== PROTOCOL_VERSION - 1
this.#protocolVersion > PROTOCOL_VERSION ||
this.#protocolVersion < MIN_SERVER_SUPPORTED_PROTOCOL_VERSION
) {
this.#closeWithError({
kind: ErrorKind.VersionNotSupported,
message: `server supports v${
PROTOCOL_VERSION - 1
} and v${PROTOCOL_VERSION} protocols`,
message: `server is at sync protocol v${PROTOCOL_VERSION} and does not support v${
this.#protocolVersion
}. The ${
this.#protocolVersion > PROTOCOL_VERSION ? 'server' : 'client'
} must be updated to a newer release.`,
});
} else {
const connectedMessage: ConnectedMessage = [
Expand Down
25 changes: 17 additions & 8 deletions packages/zero-protocol/src/protocol-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {assert} from '../../shared/src/asserts.js';

/**
* The current `PROTOCOL_VERSION` of the code.
*
* The `PROTOCOL_VERSION` encompasses both the wire-protocol of the `/sync/...`
* connection between the browser and `zero-cache`, as well as the format of
* the `AST` objects stored in both components (i.e. IDB and CVR).
Expand All @@ -7,13 +11,18 @@
* accompanied by an increment of the `PROTOCOL_VERSION` and a new major
* release. The server (`zero-cache`) must be deployed before clients start
* running the new code.
*
* The contract for backwards compatibility is that a `zero-cache` supports
* its current `PROTOCOL_VERSION` and the previous one (i.e.
* `PROTOCOL_VERSION - 1`, which is necessary to support old clients when
* the server is rolled out). This corresponds to supporting clients running
* the current release and the previous (major) release. Any client connections
* from earlier protocol versions are closed with a `VersionNotSupported`
* error.
*/
export const PROTOCOL_VERSION = 3;

/**
* The minimum protocol version supported by the server. The contract for
* backwards compatibility is that a `zero-cache` supports the current
* `PROTOCOL_VERSION` and at least the previous one (i.e. `PROTOCOL_VERSION - 1`)
* if not earlier ones as well. This corresponds to supporting clients running
* the current release and the previous (major) release. Any client connections
* from protocol versions before `MIN_SERVER_SUPPORTED_PROTOCOL_VERSION` are
* closed with a `VersionNotSupported` error.
*/
export const MIN_SERVER_SUPPORTED_PROTOCOL_VERSION = 2;

assert(MIN_SERVER_SUPPORTED_PROTOCOL_VERSION < PROTOCOL_VERSION);
Loading