Skip to content
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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ jobs:
# The generated fixtures, against the built runtime as a consumer sees
# it, under the strictest flags and erasable syntax only.
- run: npm run typecheck:generated
# What `/** @internal */` marks in that output is plumbing a consumer's
# `stripInternal` may drop from its declarations: their declarations,
# stripped, must still type-check on their own.
- run: npm run typecheck:stripped
# The wire conformance vectors are vendored from
# ComlineProject/specification at the commit in SPEC_REV; this fails if
# the copy was edited.
Expand Down
430 changes: 392 additions & 38 deletions codegen/src/generator.rs

Large diffs are not rendered by default.

251 changes: 205 additions & 46 deletions codegen/tests/generate.rs

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions codegen/tests/nested_namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ fn a_nested_protocols_path_is_spelled_with_colons() {
fn the_display_path_changes_no_number_or_hash() {
let req = request(&nested_protocol());
let files = generated(&req, "nested");
let connection = file(&files, "connection.ts");
// The package's digest is defined once, in the module its files share,
// and the package module re-exports it.
let wire = file(&files, "wire.ts");
let hash = format!("CONNECTION_IR_HASH = {:#018x}n;", connections::connection_ir_hash(&req.schemas));
assert!(connection.contains(&hash), "{connection}");
assert!(wire.contains(&hash), "{wire}");
let connection = file(&files, "connection.ts");
assert!(connection.contains("export { CONNECTION_IR_HASH, Format } from \"./wire.js\";"), "{connection}");
let admin = file(&files, "chat/admin.ts");
assert!(admin.contains("checkCall(this.peer, AdminSender.PROTOCOL, 0, "), "{admin}");
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"build": "tsc -p tsconfig.json",
"typecheck": "tsc -p tsconfig.json --noEmit",
"test": "tsc -p tsconfig.json && node --test dist/test/*.test.js dist/test/prop/*.test.js",
"typecheck:generated": "tsc -p tsconfig.generated.json --erasableSyntaxOnly"
"typecheck:generated": "tsc -p tsconfig.generated.json --erasableSyntaxOnly",
"typecheck:stripped": "node -e \"require('node:fs').rmSync('dist/stripped', { recursive: true, force: true })\" && tsc -p tsconfig.generated.json --declaration --emitDeclarationOnly --noEmit false --stripInternal --outDir dist/stripped && tsc -p tsconfig.stripped.json"
},
"devDependencies": {
"@types/node": "^20.14.0",
Expand Down
6 changes: 3 additions & 3 deletions runtime/test/generated-bounds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Exercises generated bounds (core#98) against the runtime. `generated/bounds.ts`
// Exercises generated bounds (core#98) against the runtime. `generated/bounds/`
// is written by `comline-codegen-typescript`'s `generated_bounds_match_the_
// runtime_test_fixture` test; this proves it type-checks and that every
// bound it declares is checked, both ways, in both formats.
Expand All @@ -11,14 +11,14 @@ import {
type Customer,
type Directory,
type Folder,
CONNECTION_IR_HASH,
CountryCode,
CustomerLayout,
DirectoryDispatcher,
DirectorySender,
FolderLayout,
Name,
} from "./generated/bounds.js";
} from "./generated/bounds/account.js";
import { CONNECTION_IR_HASH } from "./generated/bounds/wire.js";

const bound = (measure: Measure) => (e: unknown) =>
e instanceof RuntimeError && e.is("boundExceeded") && e.measure === measure;
Expand Down
6 changes: 3 additions & 3 deletions runtime/test/generated-connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Exercises real generator output on a `Connection`. `generated/link.ts` is
// Exercises real generator output on a `Connection`. `generated/link/` is
// written by `comline-codegen-typescript`'s `generated_link_matches_the_
// runtime_test_fixture` test; this proves it type-checks and runs.

Expand All @@ -13,10 +13,10 @@ import {
type EventsMessageParams,
ChatDispatcher,
ChatSender,
CONNECTION_IR_HASH,
EventsDispatcher,
EventsSender,
} from "./generated/link.js";
} from "./generated/link/link.js";
import { CONNECTION_IR_HASH } from "./generated/link/wire.js";

class Sent {
constructor(public count: bigint) {}
Expand Down
6 changes: 3 additions & 3 deletions runtime/test/generated-jsonrpc-connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Exercises real generator output on a JSON-RPC `Connection`. `generated/link_jsonrpc.ts` is
// Exercises real generator output on a JSON-RPC `Connection`. `generated/link_jsonrpc/` is
// written by `comline-codegen-typescript`'s `generated_jsonrpc_link_matches_the_
// runtime_test_fixture` test; this proves it type-checks and runs.

Expand All @@ -13,10 +13,10 @@ import {
type EventsMessageParams,
ChatDispatcher,
ChatSender,
CONNECTION_IR_HASH,
EventsDispatcher,
EventsSender,
} from "./generated/link_jsonrpc.js";
} from "./generated/link_jsonrpc/link.js";
import { CONNECTION_IR_HASH } from "./generated/link_jsonrpc/wire.js";

class Sent {
constructor(public count: bigint) {}
Expand Down
4 changes: 4 additions & 0 deletions runtime/test/generated/acme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ export interface ChatSendParams {
text: string;
}

/** @internal */
export const ChatSendParamsLayout: Layout = { struct: [["text", "string"]] };

export interface ChatHistoryParams {
limit: number;
}

/** @internal */
export const ChatHistoryParamsLayout: Layout = { struct: [["limit", "u32"]] };

export interface ChatNoteParams {
text: string;
}

/** @internal */
export const ChatNoteParamsLayout: Layout = { struct: [["text", "string"]] };

export interface Chat {
Expand All @@ -74,6 +77,7 @@ export interface Chat {
note(params: ChatNoteParams): Promise<void>;
}

/** @internal */
export const CHAT_CALLS = ["send", "history", "wipe", "note"] as const;

export class ChatDispatcher implements Dispatch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
resolveKind,
checkChars,
checkCharsExact,
PackedCodec,
type Codec,
type Connection,
type Dispatch,
Expand All @@ -20,14 +19,6 @@ import {
* two ends of a connection must agree on it. */
export const IR_HASH = 0x17305ca8c4eea701n;

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0x17305ca8c4eea701n;

/** The package's wire format (`wire.format`): what this file's helpers
* encode with, and what the peer has to speak too. */
export const Format: Codec = new PackedCodec();

/** A string of exactly 2 characters: make one with `CountryCode.from`. */
export type CountryCode = string & { readonly __brand: "CountryCode" };

Expand Down Expand Up @@ -76,21 +67,25 @@ export interface DirectoryFindParams {
query: string;
}

/** @internal */
export const DirectoryFindParamsLayout: Layout = { struct: [["query", { chars: { max: 8 } }]] };

export interface DirectoryTreeParams {
root: Folder;
}

/** @internal */
export const DirectoryTreeParamsLayout: Layout = { struct: [["root", () => FolderLayout]] };

export interface Directory {
find(peer: Peer, params: DirectoryFindParams): Promise<Customer[]>;
tree(peer: Peer, params: DirectoryTreeParams): Promise<number>;
}

/** @internal */
export const DIRECTORY_CALLS = ["find", "tree"] as const;

/** @internal */
export class DirectoryDispatcher implements Dispatch {
/** This protocol's number on a connection. */
static readonly PROTOCOL = 0;
Expand Down Expand Up @@ -136,7 +131,7 @@ export class DirectoryDispatcher implements Dispatch {
}
}

/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.accept(transport, Format, CONNECTION_IR_HASH)`, or let the package's `serve` / `connect` do it. */
/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.accept(transport, Format, CONNECTION_IR_HASH)` (`Format` and `CONNECTION_IR_HASH` are in `./wire.js`), or let the package's `serve` / `connect` do it. */
provideOn(conn: Connection): void {
conn.provide(DirectoryDispatcher.PROTOCOL, this);
}
Expand Down
62 changes: 62 additions & 0 deletions runtime/test/generated/bounds/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Generated by Comline
// For TypeScript 5.0 or later; verified up to 5.9.

/**
* Where to start:
*
* - Implement what you provide: `Directory` (in `./account.js`).
* - Serve with `serve(transport, { directory })`; `accept(transport, { directory })` takes the same and hands back the connection.
* - Dial with `connect(transport)`.
* - Call the other end with `new DirectorySender(conn).find(…)`, on the connection `connect` returns or the peer a handler is given.
*
* What is marked internal is wire plumbing: exported for hand-written use, and not what
* a user calls.
*/

import {
Connection,
type Transport,
} from "@comline/runtime";
import * as ns_account from "./account.js";
import { CONNECTION_IR_HASH, Format } from "./wire.js";

/** The package's wire format and the digest of every schema in it, which every file of the package shares. */
export { CONNECTION_IR_HASH, Format } from "./wire.js";

/** What the accepting side provides: one field per protocol, so leaving one out doesn't type-check. */
export interface Services {
directory: ns_account.Directory;
}

/**
* Provide every protocol in `services` on `conn`: the peer's calls to them come here.
* @internal
*/
export function provideServices(conn: Connection, services: Services): void {
new ns_account.DirectoryDispatcher(services.directory).provideOn(conn);
}

/**
* Accept a connection over `transport`: run the handshake, then provide
* every protocol in `services` on it. The connection comes back serving,
* so the server can call the client through it.
*/
export async function accept(transport: Transport, services: Services): Promise<Connection> {
const conn = await Connection.accept(transport, Format, CONNECTION_IR_HASH);
provideServices(conn, services);
return conn;
}

/** {@link accept}, then serve every call until the peer leaves. */
export async function serve(transport: Transport, services: Services): Promise<void> {
await (await accept(transport, services)).closed;
}

/**
* Dial over `transport`: run the handshake. Call the server's protocols
* through their senders.
*/
export async function connect(transport: Transport): Promise<Connection> {
const conn = await Connection.connect(transport, Format, CONNECTION_IR_HASH);
return conn;
}
14 changes: 14 additions & 0 deletions runtime/test/generated/bounds/wire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by Comline
// For TypeScript 5.0 or later; verified up to 5.9.

import {
PackedCodec,
type Codec,
} from "@comline/runtime";

/** The package's wire format (`wire.format`). */
export const Format: Codec = new PackedCodec();

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0x17305ca8c4eea701n;
4 changes: 4 additions & 0 deletions runtime/test/generated/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ export interface ChatSendParams {
text: string;
}

/** @internal */
export const ChatSendParamsLayout: Layout = { struct: [["text", "string"]] };

export interface ChatHistoryParams {
limit: number;
}

/** @internal */
export const ChatHistoryParamsLayout: Layout = { struct: [["limit", "u32"]] };

export interface ChatNoteParams {
text: string;
}

/** @internal */
export const ChatNoteParamsLayout: Layout = { struct: [["text", "string"]] };

export interface Chat {
Expand All @@ -74,6 +77,7 @@ export interface Chat {
note(params: ChatNoteParams): Promise<void>;
}

/** @internal */
export const CHAT_CALLS = ["send", "history", "wipe", "note"] as const;

export class ChatDispatcher implements Dispatch {
Expand Down
10 changes: 6 additions & 4 deletions runtime/test/generated/clash/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import type { Greeting } from "./localdeps/greeting.js";
* two ends of a connection must agree on it. */
export const IR_HASH = 0xa14195a41e817a01n;

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0xe9ca523b62a2c19fn;

export interface Message {
greeting: Greeting;
}
Expand All @@ -35,14 +31,17 @@ export interface ChatGreetParams {
n: number;
}

/** @internal */
export const ChatGreetParamsLayout: Layout = { struct: [["n", "u32"]] };

export interface Chat {
greet(peer: Peer, params: ChatGreetParams): Promise<number>;
}

/** @internal */
export const CHAT_CALLS = ["greet"] as const;

/** @internal */
export class ChatDispatcher implements Dispatch {
/** This protocol's number on a connection. */
static readonly PROTOCOL = 0;
Expand Down Expand Up @@ -109,14 +108,17 @@ export interface EventsNotifyParams {
n: number;
}

/** @internal */
export const EventsNotifyParamsLayout: Layout = { struct: [["n", "u32"]] };

export interface Events {
notify(peer: Peer, params: EventsNotifyParams): Promise<number>;
}

/** @internal */
export const EVENTS_CALLS = ["notify"] as const;

/** @internal */
export class EventsDispatcher implements Dispatch {
/** This protocol's number on a connection. */
static readonly PROTOCOL = 1;
Expand Down
Loading
Loading