Skip to content

Commit

Permalink
Add schema versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Wundero committed Feb 19, 2025
1 parent 60e8eff commit 829a121
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
19 changes: 18 additions & 1 deletion apps/deployment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import type {
RouteResponseSchema,
ServerRoute,
} from "@sinkr/validators";
import { ServerRequestSchema } from "@sinkr/validators";
import { ServerRequestSchema, SINKR_SCHEMA_HEADER } from "@sinkr/validators";

import { getCoordinatorInstance, getPeers, handleSource, ws } from "./server";
import { getDB, init } from "./utils";

export const MAX_CONNECTIONS_PER_OBJECT = 500;

export const SUPPORTED_SCHEMA_VERSION = 0;

const coordinatorCtx = new AsyncLocalStorage<ObjectCoordinator>();

export function getCoordinator() {
Expand Down Expand Up @@ -76,6 +78,14 @@ export class ObjectCoordinator extends DurableObject<Env> {
}

async fetch(request: Request) {
const schemaVersion = parseInt(
request.headers.get(SINKR_SCHEMA_HEADER) ?? "-1",
);
if (schemaVersion !== SUPPORTED_SCHEMA_VERSION) {
return new Response("Invalid protocol version", {
status: 400,
});
}
return coordinatorCtx.run(this, async () => {
if (request.headers.get("Upgrade") === "websocket") {
const reqUrl = new URL(request.url);
Expand Down Expand Up @@ -234,6 +244,13 @@ export class SocketHandler extends DurableObject<Env> {
async fetch(request: Request) {
console.log("On durable object fetch");

const schemaVersion = parseInt(
request.headers.get(SINKR_SCHEMA_HEADER) ?? "-1",
);
if (schemaVersion !== SUPPORTED_SCHEMA_VERSION) {
return new Response("Invalid protocol version", { status: 400 });
}

if (request.headers.get("Upgrade") === "websocket") {
const res = await ws.handleDurableUpgrade(this, request);
this.ctx.waitUntil(this.updateCoordinator());
Expand Down
2 changes: 1 addition & 1 deletion packages/validators/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/validators",
"version": "0.6.0",
"version": "0.6.1",
"license": "MIT",
"exports": "./mod.ts"
}
2 changes: 1 addition & 1 deletion packages/validators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/validators",
"version": "0.6.0",
"version": "0.6.1",
"type": "module",
"main": "src/index.ts",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions packages/validators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from "./client";
export * from "./message";
export * from "./server";

export const SINKR_SCHEMA_VERSION = 0;
export const SINKR_SCHEMA_HEADER = "X-Sinkr-Proto-Version";

0 comments on commit 829a121

Please sign in to comment.