Skip to content
Closed
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
16 changes: 16 additions & 0 deletions pkgs/id/web/src/shell-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ const isInteger = (value: unknown): value is number => typeof value === "number"
export function parseShellContract(value: unknown): ShellContract {
if (!isObject(value)) throw new Error("shell contract must be an object");

if (typeof value.max_frame_bytes !== "number" || !Number.isFinite(value.max_frame_bytes) || value.max_frame_bytes < 0) {
throw new Error("max_frame_bytes must be a non-negative finite number");
}

if (!Array.isArray(value.routing_fields) || value.routing_fields.some((item) => typeof item !== "string")) {
throw new Error("routing_fields must be an array of strings");
}

if (!isObject(value.projection)) {
throw new Error("projection must be an object");
}

if (!Array.isArray(value.projection.redacted_keys) || value.projection.redacted_keys.some((item) => typeof item !== "string")) {
throw new Error("projection.redacted_keys must be an array of strings");
}

for (const side of ["client_frames", "server_frames"] as const) {
const frames = value[side];
if (!isObject(frames)) throw new Error(`${side} must be an object`);
Expand Down
30 changes: 30 additions & 0 deletions pkgs/id/web/src/world-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ function worldPageMarkup(): string {
describe("shell contract validator", () => {
const cap = "cap";

it("validates top-level fields when loading a contract", () => {
const missingMaxBytes = structuredClone(contract);
delete (missingMaxBytes as any).max_frame_bytes;
expect(() => parseShellContract(missingMaxBytes)).toThrow("max_frame_bytes must be a non-negative finite number");

const negativeMaxBytes = structuredClone(contract);
(negativeMaxBytes as any).max_frame_bytes = -1;
expect(() => parseShellContract(negativeMaxBytes)).toThrow("max_frame_bytes must be a non-negative finite number");

const missingRoutingFields = structuredClone(contract);
delete (missingRoutingFields as any).routing_fields;
expect(() => parseShellContract(missingRoutingFields)).toThrow("routing_fields must be an array of strings");

const stringRoutingFields = structuredClone(contract);
(stringRoutingFields as any).routing_fields = "world";
expect(() => parseShellContract(stringRoutingFields)).toThrow("routing_fields must be an array of strings");

const missingProjection = structuredClone(contract);
delete (missingProjection as any).projection;
expect(() => parseShellContract(missingProjection)).toThrow("projection must be an object");

const missingRedactedKeys = structuredClone(contract);
delete (missingRedactedKeys as any).projection.redacted_keys;
expect(() => parseShellContract(missingRedactedKeys)).toThrow("projection.redacted_keys must be an array of strings");

const stringRedactedKeys = structuredClone(contract);
(stringRedactedKeys as any).projection.redacted_keys = "seed";
expect(() => parseShellContract(stringRedactedKeys)).toThrow("projection.redacted_keys must be an array of strings");
});

it("validates every client and server field spec when loading a contract", () => {
expect(parseShellContract(contractDocument)).toEqual(contract);

Expand Down
Loading