20 Commits
Author SHA1 Message Date
Harvmaster 3f25eab193 Fix package-lock 2026-09-14 10:11:50 +00:00
Harvmaster 2d970d3123 Many fixes 2026-09-14 08:00:08 +00:00
Harvmaster 93b012592b Merge branch '6-add-websocket-transport' into 7-add-resources-route 2026-09-02 09:59:37 +00:00
Harvmaster 0a7e9bc903 Merge branch '5-add-http-and-sse' into 6-add-websocket-transport 2026-09-02 09:59:28 +00:00
Harvmaster 714b0bee0c Merge branch '4-add-broadcaster' into 5-add-http-and-sse 2026-09-02 09:58:51 +00:00
Harvmaster cbefa8729a Merge branch '3.5-add-auth' into 4-add-broadcaster 2026-09-02 09:58:44 +00:00
Harvmaster 50eed7c4d6 Add auth controls and headers 2026-09-02 09:57:20 +00:00
Harvmaster 0cf140ff17 Merge branch 'add-base-and-storage' into 3-add-routing 2026-08-31 12:37:36 +00:00
Harvmaster a71494f79d Add signature and authed_requests to database 2026-08-31 12:34:54 +00:00
Harvmaster 6febaf327a Merge branch '6-add-websocket-transport' into 7-add-resources-route 2026-08-03 03:41:09 +00:00
Harvmaster c9845d0f28 Formatting 2026-08-03 03:40:57 +00:00
Harvmaster 97d4422b8f Merge branch '5-add-http-and-sse' into 6-add-websocket-transport 2026-08-03 03:38:14 +00:00
Harvmaster 8be4467721 Formatting 2026-08-03 03:37:22 +00:00
Harvmaster 9c0746bb24 Merge branch '4-add-broadcaster' into 5-add-http-and-sse 2026-08-03 03:36:02 +00:00
Harvmaster 161b56c756 Formatting 2026-08-03 03:35:09 +00:00
Harvmaster 1cee27e78b Merge branch '3-add-routing' into 4-add-broadcaster 2026-08-03 03:33:28 +00:00
Harvmaster a36e267280 Formatting 2026-08-03 03:32:11 +00:00
Harvmaster 1c1c1b6a07 Merge branch '3-add-routing' into 4-add-broadcaster 2026-08-03 03:30:02 +00:00
Harvmaster 1ba075b5fa Merge branch 'add-base-and-storage' into 3-add-routing 2026-08-03 03:29:37 +00:00
Harvmaster d4b3b72e75 Formatting 2026-08-03 03:29:30 +00:00
32 changed files with 1678 additions and 1658 deletions
+266 -705
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -17,7 +17,7 @@
"scripts": { "scripts": {
"analyze": "tsdown --clean --no-fixed-extension --sourcemap source/index.ts && esbuild-analyzer dist/", "analyze": "tsdown --clean --no-fixed-extension --sourcemap source/index.ts && esbuild-analyzer dist/",
"build": "tsdown --clean --sourcemap source/index.ts", "build": "tsdown --clean --sourcemap source/index.ts",
"dev": "tsx watch source/app.ts", "dev": "tsx watch source/index.ts",
"docs": "typedoc --hideGenerator --categorizeByGroup", "docs": "typedoc --hideGenerator --categorizeByGroup",
"format": "prettier --write . && eslint --fix", "format": "prettier --write . && eslint --fix",
"spellcheck": "cspell 'source/**' 'test/**' 'playground/**'", "spellcheck": "cspell 'source/**' 'test/**' 'playground/**'",
@@ -63,7 +63,7 @@
"@types/debug": "^4.1.13", "@types/debug": "^4.1.13",
"@types/node": "^25.9.3", "@types/node": "^25.9.3",
"@types/ws": "^8.18.1", "@types/ws": "^8.18.1",
"@vitest/coverage-v8": "^4.1.9", "@vitest/coverage-v8": "^5.0.0",
"@viz-kit/esbuild-analyzer": "^1.0.0", "@viz-kit/esbuild-analyzer": "^1.0.0",
"@xo-cash/eslint-config": "1.0.2", "@xo-cash/eslint-config": "1.0.2",
"cspell": "^10.0.1", "cspell": "^10.0.1",
@@ -74,6 +74,6 @@
"typedoc-plugin-coverage": "^4.0.2", "typedoc-plugin-coverage": "^4.0.2",
"typescript": "^6.0.3", "typescript": "^6.0.3",
"typescript-eslint": "^8.65.0", "typescript-eslint": "^8.65.0",
"vitest": "^4.1.9" "vitest": "^5.0.0"
} }
} }
+122
View File
@@ -0,0 +1,122 @@
import type { Database } from '../services/storage/database.ts';
import { hexToBin, instantiateSecp256k1, type Secp256k1, sha256 } from '@bitauth/libauth';
import { UnauthorizedError } from '../errors/unauthorized-error.ts';
export type AuthSecp256k1RequiredDeps = {
database: Database;
};
export type AuthSecp256k1OptionalDeps = {
secp256k1: Secp256k1;
};
export type AuthSecp256k1Deps = AuthSecp256k1RequiredDeps & Partial<AuthSecp256k1OptionalDeps>;
export type AuthSecp256k1Options = {
timestampWindowMs: number;
};
export class AuthSecp256k1 {
/**
* Create a new instance of AuthSecp256k1
* @returns A new instance of AuthSecp256k1
*/
static async create(inputDeps: AuthSecp256k1Deps, options: AuthSecp256k1Options): Promise<AuthSecp256k1> {
const deps = {
secp256k1: inputDeps.secp256k1 || await instantiateSecp256k1(),
...inputDeps,
};
return new AuthSecp256k1(deps, options);
}
/**
* TODO: Consider adding a Record<string, Mutex> where each key is the signature to guarantee signatures arent being processed concurrently.
*/
readonly #deps: Required<AuthSecp256k1Deps>;
readonly #options: AuthSecp256k1Options;
/**
* @param deps - The dependencies to use
* @param options - The options to use
*/
private constructor(deps: Required<AuthSecp256k1Deps>, options: AuthSecp256k1Options) {
this.#deps = deps;
this.#options = options;
}
/**
* Verify a signature
* @param publicKeyHex - The public key to verify the signature against
* @param signatureHex - The signature to verify
* @param payload - The payload to verify the signature against
* @returns Whether the signature is valid
*/
async verifySignature(publicKeyHex: string, signatureHex: string, payload: string): Promise<boolean> {
// Convert the public key and signature to binary
const publicKey = hexToBin(publicKeyHex);
const signature = hexToBin(signatureHex);
// Convert the payload to bytes and compute the sha256 hash
const payloadBytes = new TextEncoder().encode(payload);
const messageHash = sha256.hash(payloadBytes);
// Verify the signature
const verified = this.#deps.secp256k1.verifySignatureDERLowS(signature, publicKey, messageHash);
// If the signature is not valid, throw an unauthorized error
if (!verified) {
throw new UnauthorizedError('Invalid signature');
}
// Return the verified signature
return verified;
}
/**
* Verify a signature is unique and add it to the database if it is
*
* @param signature - The signature to verify
* @returns Whether the signature is unique
*/
async verifyUniqueRequest(signature: string): Promise<boolean> {
// If the signature is not provided, throw an unauthorized error
if (!signature) {
throw new UnauthorizedError('Signature is required');
}
// Check if the signature has been used before
const request = await this.#deps.database.db.selectFrom('authed_requests').selectAll()
.where('signature', '=', signature)
.executeTakeFirst();
// If the signature has been used before, throw an unauthorized error
if (request) {
throw new UnauthorizedError('Request already used');
}
// Add the signature to the requests table
await this.#deps.database.db.insertInto('authed_requests').values({ signature })
.execute();
// Return true if the signature is valid
return true;
}
/**
* Assert that a timestamp is within a allowed window
* @param timestamp - The timestamp to check
* @param windowMs - The window in milliseconds
*
* @throws An {@link UnauthorizedError} if the timestamp is outside the allowed window
*/
assertTimestampFreshness(timestamp: number, windowMs = this.#options.timestampWindowMs): void {
// Subtract the timestamp from the current time to get the age in milliseconds
const age = Math.abs(Date.now() - timestamp);
// If the timestamp is outside the allowed window, throw an unauthorized error
if (age > windowMs) {
throw new UnauthorizedError('Timestamp outside allowed window');
}
}
}
+25 -3
View File
@@ -1,5 +1,6 @@
import { Config } from './services/config.ts'; import { Config } from './services/config.ts';
import { Database, MigrationService } from './services/storage/index.ts'; import { Database, MigrationService } from './services/storage/index.ts';
import { AuthSecp256k1 } from './auth/auth.ts';
import { Broadcaster } from './services/broadcaster.ts'; import { Broadcaster } from './services/broadcaster.ts';
import { ApplicationRouter } from './services/router.ts'; import { ApplicationRouter } from './services/router.ts';
import { HttpTransportRouter } from './services/transport/http-transport.ts'; import { HttpTransportRouter } from './services/transport/http-transport.ts';
@@ -23,24 +24,34 @@ export class App {
const migrations = new MigrationService(database, debug); const migrations = new MigrationService(database, debug);
await migrations.migrateToLatest(); await migrations.migrateToLatest();
// Create an Auth instance that can be passed in for signature validation
const auth = await AuthSecp256k1.create({ database }, { timestampWindowMs: config.auth.timestampWindowMs });
// Domain services are shared across all transports and route modules. // Domain services are shared across all transports and route modules.
const broadcaster = new Broadcaster(debug); const broadcaster = new Broadcaster(debug);
const routes = [ const routes = [
// DataRoute owns resource read/write/subscribe logic and maps resource // DataRoute owns resource read/write/subscribe logic and maps resource
// ids to broadcaster topics. timestampWindowMs controls write replay protection. // ids to broadcaster topics. timestampWindowMs controls write replay protection.
new DataRoute(database, broadcaster, config.auth.timestampWindowMs), new DataRoute({
auth: auth,
database: database,
broadcaster: broadcaster,
}, {
timestampWindowMs: config.auth.timestampWindowMs,
}),
]; ];
// Route loading is an explicit startup phase, not first-request work. // Route loading is an explicit startup phase, not first-request work.
// ApplicationRouter.create validates every path and rejects duplicates // ApplicationRouter.create validates every path and rejects duplicates
// before any client can connect. // before any client can connect.
const router = await ApplicationRouter.create(routes); const router = await ApplicationRouter.create({ auth }, routes);
// Both transports share one ApplicationRouter. Routes use the shared // Both transports share one ApplicationRouter. Routes use the shared
// Broadcaster directly, while HTTP and WebSocket remain protocol adapters. // Broadcaster directly, while HTTP and WebSocket remain protocol adapters.
const http = new HttpTransportRouter(router, debug); const http = new HttpTransportRouter(router, debug);
const ws = new WsTransportRouter(router, debug, config.server.maxRequestBodyBytes); const ws = new WsTransportRouter(router, debug, config.server.maxRequestBodyBytes);
const host = new ServerHost(config, debug, [http, ws]); const host = new ServerHost(config, debug, [ http, ws ]);
return new App(host, database); return new App(host, database);
} }
@@ -53,6 +64,7 @@ export class App {
) {} ) {}
async start(): Promise<void> { async start(): Promise<void> {
await this.database.start();
await this.host.start(); await this.host.start();
} }
@@ -67,6 +79,16 @@ export class App {
})(); })();
await this.stopPromise; await this.stopPromise;
} }
startUniqueRequestCleanup(cleanupIntervalMs: number, timestampWindowMs: number): void {
// Every 10 seconds, we will cleanup the requests table
setInterval(async () => {
await this.database.db
.deleteFrom('authed_requests')
.where('timestamp', '<', Date.now() - timestampWindowMs)
.execute();
}, cleanupIntervalMs);
}
} }
const app = await App.create(); const app = await App.create();
+29 -52
View File
@@ -1,5 +1,3 @@
import { createHash } from 'node:crypto';
import { hexToBin, instantiateSecp256k1, type Secp256k1 } from '@bitauth/libauth';
import { toExtendedJson } from '@xo-cash/utils'; import { toExtendedJson } from '@xo-cash/utils';
import { z } from 'zod'; import { z } from 'zod';
@@ -9,6 +7,7 @@ import type { BaseBroadcaster } from '../services/broadcaster.ts';
import { ApplicationError, UnauthorizedError } from '../errors/index.ts'; import { ApplicationError, UnauthorizedError } from '../errors/index.ts';
import type { Database } from '../services/storage/database.ts'; import type { Database } from '../services/storage/database.ts';
import type { RouteDefinition, RouteModule, RouteStream } from './types.ts'; import type { RouteDefinition, RouteModule, RouteStream } from './types.ts';
import { AuthSecp256k1 } from '../auth/auth.ts';
/** /**
* Schema to validate a single write resource. * Schema to validate a single write resource.
@@ -42,11 +41,21 @@ const resourceIdsSchema = z.object({
resourceId: z resourceId: z
.array(z.string().min(1)) .array(z.string().min(1))
.min(1, 'At least one resourceId is required') .min(1, 'At least one resourceId is required')
.transform((ids) => [...new Set(ids)]), .transform((ids) => [ ...new Set(ids) ]),
}); });
type WriteResource = z.infer<typeof writeResource>; type WriteResource = z.infer<typeof writeResource>;
export type DataRouteDeps = {
auth: AuthSecp256k1;
database: Database;
broadcaster: BaseBroadcaster;
}
export type DataRouteOptions = {
timestampWindowMs: number;
}
/** /**
* Resource data domain routes. * Resource data domain routes.
* *
@@ -56,16 +65,9 @@ type WriteResource = z.infer<typeof writeResource>;
* RouteStream API. * RouteStream API.
*/ */
export class DataRoute implements RouteModule { export class DataRoute implements RouteModule {
/**
* Promise to instantiate the secp256k1 library
* This is a bit annoying, but keeping a single instance alive makes more sense than instantiating it for each verification.
*/
private readonly secp256k1Promise: Promise<Secp256k1> = instantiateSecp256k1();
constructor( constructor(
private readonly database: Database, private readonly deps: DataRouteDeps,
private readonly broadcaster: BaseBroadcaster, private readonly options: DataRouteOptions,
private readonly timestampWindowMs: number,
) {} ) {}
/** Declare exact routes; each handler owns its stream behavior. */ /** Declare exact routes; each handler owns its stream behavior. */
@@ -95,7 +97,7 @@ export class DataRoute implements RouteModule {
const resourceIds = this.getResourceIds(stream); const resourceIds = this.getResourceIds(stream);
// Remove duplicates. // Remove duplicates.
const uniqueIds = [...new Set(resourceIds)]; const uniqueIds = [ ...new Set(resourceIds) ];
// If there are no resource ids, return an empty array. // If there are no resource ids, return an empty array.
if (uniqueIds.length === 0) { if (uniqueIds.length === 0) {
@@ -105,9 +107,9 @@ export class DataRoute implements RouteModule {
} }
// Read the data from the database. // Read the data from the database.
const rows = await this.database.db const rows = await this.deps.database.db
.selectFrom('resource_data') .selectFrom('resource_data')
.select(['resource_id', 'public_key', 'blob', 'timestamp']) .select([ 'resource_id', 'public_key', 'blob', 'timestamp' ])
.where('resource_id', 'in', uniqueIds) .where('resource_id', 'in', uniqueIds)
.orderBy('timestamp', 'asc') .orderBy('timestamp', 'asc')
.execute(); .execute();
@@ -140,26 +142,27 @@ export class DataRoute implements RouteModule {
const rows = resources.map((resource) => ({ const rows = resources.map((resource) => ({
resourceId: resource.id, resourceId: resource.id,
publicKey: resource.publicKey, publicKey: resource.publicKey,
signature: resource.signature,
blob: Buffer.from(resource.value), blob: Buffer.from(resource.value),
})); }));
// Upsert every row atomically; conflicts update blob and timestamp only. // Upsert every row atomically; conflicts update blob and timestamp only.
await this.database.db.transaction().execute(async (trx) => { await this.deps.database.db.transaction().execute(async (trx) => {
for (const { resourceId, publicKey, blob } of rows) { for (const { resourceId, publicKey, blob, signature } of rows) {
await trx await trx
.insertInto('resource_data') .insertInto('resource_data')
.values({ .values({
resource_id: resourceId, resource_id: resourceId,
public_key: publicKey, public_key: publicKey,
signature: signature,
blob, blob,
timestamp, timestamp,
}) })
.onConflict((oc) => .onConflict((oc) =>
oc.columns(['resource_id', 'public_key']).doUpdateSet({ oc.columns([ 'resource_id', 'public_key' ]).doUpdateSet({
blob, blob,
timestamp, timestamp,
}), }))
)
.execute(); .execute();
} }
}); });
@@ -177,7 +180,7 @@ export class DataRoute implements RouteModule {
// Notify subscribers on each changed resource. Topic names are scoped per // Notify subscribers on each changed resource. Topic names are scoped per
// resource id so clients only receive events for resources they joined. // resource id so clients only receive events for resources they joined.
for (const { resourceId, instance } of written) { for (const { resourceId, instance } of written) {
await this.broadcaster.publish(DataRoute.resourceTopic(resourceId), { await this.deps.broadcaster.publish(DataRoute.resourceTopic(resourceId), {
type: 'instance-changed', type: 'instance-changed',
data: { resourceId, ...instance }, data: { resourceId, ...instance },
}); });
@@ -200,7 +203,7 @@ export class DataRoute implements RouteModule {
// Subscribe registers the topics synchronously, then keeps this route // Subscribe registers the topics synchronously, then keeps this route
// active until those topics are removed or the connection closes. // active until those topics are removed or the connection closes.
await this.broadcaster.subscribe(stream, topics); await this.deps.broadcaster.subscribe(stream, topics);
} }
/** /**
@@ -218,7 +221,7 @@ export class DataRoute implements RouteModule {
} }
// Unsubscribe from the resource topics. // Unsubscribe from the resource topics.
await this.broadcaster.unsubscribe( await this.deps.broadcaster.unsubscribe(
stream, stream,
resourceIds.map((resourceId) => DataRoute.resourceTopic(resourceId)), resourceIds.map((resourceId) => DataRoute.resourceTopic(resourceId)),
); );
@@ -242,7 +245,7 @@ export class DataRoute implements RouteModule {
private async verifyWriteResource(resource: WriteResource): Promise<void> { private async verifyWriteResource(resource: WriteResource): Promise<void> {
this.assertFreshTimestamp(resource.timestamp); this.assertFreshTimestamp(resource.timestamp);
if (!(await this.verifySignature(resource.publicKey, resource.signature, DataRoute.canonicalWritePayload(resource)))) { if (!(await this.deps.auth.verifySignature(resource.publicKey, resource.signature, DataRoute.canonicalWritePayload(resource)))) {
throw new UnauthorizedError('Invalid resource signature'); throw new UnauthorizedError('Invalid resource signature');
} }
} }
@@ -254,37 +257,11 @@ export class DataRoute implements RouteModule {
*/ */
private assertFreshTimestamp(timestamp: number): void { private assertFreshTimestamp(timestamp: number): void {
const age = Math.abs(Date.now() - timestamp); const age = Math.abs(Date.now() - timestamp);
if (age > this.timestampWindowMs) { if (age > this.options.timestampWindowMs) {
throw new UnauthorizedError('Timestamp outside allowed window'); throw new UnauthorizedError('Timestamp outside allowed window');
} }
} }
/**
* Verify a secp256k1 signature.
*
* @param publicKeyHex - The public key to verify the signature against.
* @param signatureHex - The signature to verify.
* @param payload - The payload to verify the signature against.
* @returns Whether the signature is valid.
*/
private async verifySignature(publicKeyHex: string, signatureHex: string, payload: string): Promise<boolean> {
const secp256k1 = await this.secp256k1Promise;
try {
// Convert the public key and signature to binary.
const publicKey = hexToBin(publicKeyHex);
const signature = hexToBin(signatureHex);
// Create a SHA-256 hash of the payload.
const messageHash = createHash('sha256').update(payload).digest();
// Low-S normalization rejects malleable signature encodings.
return secp256k1.verifySignatureDERLowS(signature, publicKey, messageHash);
} catch {
return false;
}
}
/** /**
* Build the transport-independent payload authenticated by each signature. * Build the transport-independent payload authenticated by each signature.
* *
@@ -292,7 +269,7 @@ export class DataRoute implements RouteModule {
* whether the client sent them over HTTP or WebSocket. * whether the client sent them over HTTP or WebSocket.
*/ */
private static canonicalWritePayload(resource: WriteResource): string { private static canonicalWritePayload(resource: WriteResource): string {
return `${resource.timestamp}${resource.id}${toExtendedJson(resource.value)}`; return `${resource.timestamp}:${resource.id}:${toExtendedJson(resource.value)}`;
} }
/** Broadcaster topic for a single resource's instance-changed events. */ /** Broadcaster topic for a single resource's instance-changed events. */
+10
View File
@@ -1,6 +1,10 @@
import type { BaseStream } from '../services/stream/base-stream.js'; import type { BaseStream } from '../services/stream/base-stream.js';
/** Canonical request headers exposed to transport-neutral route handlers. */
export type RequestHeaders = Readonly<Record<string, string>>;
export type RouteSendOptions = { export type RouteSendOptions = {
/** Defaults to `response`; any other value sends an application event. */ /** Defaults to `response`; any other value sends an application event. */
type?: string; type?: string;
@@ -15,10 +19,15 @@ export type RouteSendOptions = {
* connection lifetime are shared with other requests on the same connection. * connection lifetime are shared with other requests on the same connection.
*/ */
export interface RouteStream { export interface RouteStream {
/** Connection shared by every request on the same transport session. */ /** Connection shared by every request on the same transport session. */
readonly connection: BaseStream; readonly connection: BaseStream;
/** Canonical route path selected for this request. */
readonly path: string;
readonly body: unknown; readonly body: unknown;
readonly headers: RequestHeaders;
readonly streaming: boolean; readonly streaming: boolean;
readonly bidirectional: boolean; readonly bidirectional: boolean;
@@ -29,6 +38,7 @@ export type RouteHandler = (stream: RouteStream) => void | Promise<void>;
/** An exact application route with no transport-specific metadata. */ /** An exact application route with no transport-specific metadata. */
export type RouteDefinition = { export type RouteDefinition = {
/** Exact route name. Parameter and wildcard syntax are not supported. */ /** Exact route name. Parameter and wildcard syntax are not supported. */
url: string; url: string;
handler: RouteHandler; handler: RouteHandler;
+2
View File
@@ -6,6 +6,7 @@ import { ApplicationError } from '../errors/index.ts';
/** Request-scoped view from which the broadcaster obtains a stable connection. */ /** Request-scoped view from which the broadcaster obtains a stable connection. */
export interface BroadcastStream { export interface BroadcastStream {
/** Connection identity shared by every request on the same transport session. */ /** Connection identity shared by every request on the same transport session. */
readonly connection: BaseStream; readonly connection: BaseStream;
@@ -15,6 +16,7 @@ export interface BroadcastStream {
/** One pending subscribe call and the topics whose removal will resolve it. */ /** One pending subscribe call and the topics whose removal will resolve it. */
interface SubscriptionWaiter { interface SubscriptionWaiter {
/** Only topics newly introduced by this particular subscribe call. */ /** Only topics newly introduced by this particular subscribe call. */
readonly remainingTopics: Set<string>; readonly remainingTopics: Set<string>;
+7 -1
View File
@@ -32,7 +32,7 @@ const configSchema = z.object({
.object({ .object({
origin: z.string().default('*'), origin: z.string().default('*'),
methods: z.array(z.string()).default([ 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS' ]), methods: z.array(z.string()).default([ 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS' ]),
allowedHeaders: z.array(z.string()).default([ 'Content-Type', 'cache-control', 'X-Timestamp', 'X-PublicKey', 'X-Signature' ]), allowedHeaders: z.array(z.string()).default([ 'Content-Type', 'cache-control', 'X-Timestamp', 'X-Public-Key', 'X-Signature' ]),
}) })
.partial() .partial()
.prefault({}), .prefault({}),
@@ -48,6 +48,11 @@ const configSchema = z.object({
.int() .int()
.positive() .positive()
.default(5 * 60 * 1000), .default(5 * 60 * 1000),
uniqueRequestCleanupIntervalMs: z.coerce
.number()
.int()
.positive()
.default(10 * 60 * 1000),
}) })
.prefault({}), .prefault({}),
}); });
@@ -83,6 +88,7 @@ export class Config {
}, },
auth: { auth: {
timestampWindowMs: process.env.AUTH_TIMESTAMP_WINDOW_MS ? Number(process.env.AUTH_TIMESTAMP_WINDOW_MS) : undefined, timestampWindowMs: process.env.AUTH_TIMESTAMP_WINDOW_MS ? Number(process.env.AUTH_TIMESTAMP_WINDOW_MS) : undefined,
uniqueRequestCleanupIntervalMs: process.env.AUTH_UNIQUE_REQUEST_CLEANUP_INTERVAL_MS ? Number(process.env.AUTH_UNIQUE_REQUEST_CLEANUP_INTERVAL_MS) : undefined,
}, },
}); });
} }
+13 -2
View File
@@ -1,7 +1,10 @@
import type { RouteSendOptions, RouteStream } from '../routes/types.ts'; import type { RequestHeaders, RouteSendOptions, RouteStream } from '../routes/types.ts';
import type { BaseStream } from './stream/base-stream.ts'; import type { BaseStream } from './stream/base-stream.ts';
import { HTTP_STATUS_CODE_NO_CONTENT, HTTP_STATUS_CODE_SUCCESS } from '../constants.ts'; import { HTTP_STATUS_CODE_NO_CONTENT, HTTP_STATUS_CODE_SUCCESS } from '../constants.ts';
/** Shared immutable value used when a request supplies no headers. */
const EMPTY_REQUEST_HEADERS: RequestHeaders = Object.freeze({});
/** /**
* Binds one application request to a connection-level stream. * Binds one application request to a connection-level stream.
* *
@@ -10,16 +13,24 @@ import { HTTP_STATUS_CODE_NO_CONTENT, HTTP_STATUS_CODE_SUCCESS } from '../consta
* connection-level services such as the broadcaster. * connection-level services such as the broadcaster.
*/ */
export class ApplicationRouteStream implements RouteStream { export class ApplicationRouteStream implements RouteStream {
readonly headers: RequestHeaders;
/** /**
* @param connection - Shared transport stream backing this request. * @param connection - Shared transport stream backing this request.
* @param body - Transport-decoded application payload for the route handler. * @param body - Transport-decoded application payload for the route handler.
* @param path - Canonical application route selected for this request.
* @param requestId - Optional correlation ID for multiplexed transports. * @param requestId - Optional correlation ID for multiplexed transports.
* @param headers - Transport-normalized request headers for this dispatch.
*/ */
constructor( constructor(
readonly connection: BaseStream, readonly connection: BaseStream,
readonly body: unknown, readonly body: unknown,
readonly path: string,
private readonly requestId?: string, private readonly requestId?: string,
) {} headers: RequestHeaders = EMPTY_REQUEST_HEADERS,
) {
this.headers = headers === EMPTY_REQUEST_HEADERS ? headers : Object.freeze({ ...headers });
}
/** Whether the underlying connection can deliver server-pushed events. */ /** Whether the underlying connection can deliver server-pushed events. */
get streaming(): boolean { get streaming(): boolean {
+56 -6
View File
@@ -1,24 +1,53 @@
import type { RouteDefinition, RouteModule } from '../routes/types.ts'; import { z } from 'zod';
import { ApplicationError } from '../errors/index.ts'; import { toExtendedJson } from '@xo-cash/utils';
import type { RequestHeaders, RouteDefinition, RouteModule } from '../routes/types.ts';
import { ApplicationError, UnauthorizedError } from '../errors/index.ts';
import { ApplicationRouteStream } from './route-stream.ts'; import { ApplicationRouteStream } from './route-stream.ts';
import type { BaseStream } from './stream/base-stream.ts'; import type { BaseStream } from './stream/base-stream.ts';
import type { AuthSecp256k1 } from '../auth/auth.ts';
/** Canonical request produced by every transport adapter. */ /** Canonical request produced by every transport adapter. */
export type ApplicationRequest = { export type ApplicationRequest = {
/** Exact application route name. */ /** Exact application route name. */
path: string; path: string;
/** Transport-decoded application payload. */ /** Transport-decoded application payload. */
body?: unknown; body?: unknown;
/** Transport-normalized request headers, keyed by lowercase name. */
headers?: RequestHeaders;
/** Optional correlation ID supplied by a multiplexed transport. */ /** Optional correlation ID supplied by a multiplexed transport. */
requestId?: string; requestId?: string;
}; };
export type ApplicationRouterDependencies = {
/** Authentication service. */
auth: AuthSecp256k1;
};
const accountSchema = z
.object({
'x-public-key': z.string(),
'x-signature': z.string(),
'x-timestamp': z.coerce.number(),
})
.transform((data) => ({
publicKey: data['x-public-key'],
signature: data['x-signature'],
timestamp: data['x-timestamp'],
}));
/** Exact-match application routing shared by every wire transport. */ /** Exact-match application routing shared by every wire transport. */
export class ApplicationRouter { export class ApplicationRouter {
/** @param routes - Validated route table keyed by exact path. */ /** @param routes - Validated route table keyed by exact path. */
private constructor(private readonly routes: ReadonlyMap<string, RouteDefinition>) {} private constructor(
private readonly deps: ApplicationRouterDependencies,
private readonly routes: ReadonlyMap<string, RouteDefinition>,
) {}
/** /**
* Load and validate the complete route table before accepting traffic. * Load and validate the complete route table before accepting traffic.
@@ -26,7 +55,7 @@ export class ApplicationRouter {
* @param routeModules - Route modules whose handlers will be registered. * @param routeModules - Route modules whose handlers will be registered.
* @returns A ready-to-dispatch router instance. * @returns A ready-to-dispatch router instance.
*/ */
static async create(routeModules: RouteModule[]): Promise<ApplicationRouter> { static async create(deps: ApplicationRouterDependencies, routeModules: RouteModule[]): Promise<ApplicationRouter> {
const routes = new Map<string, RouteDefinition>(); const routes = new Map<string, RouteDefinition>();
// Collect routes from every module and reject duplicates at startup. // Collect routes from every module and reject duplicates at startup.
@@ -41,7 +70,7 @@ export class ApplicationRouter {
} }
} }
return new ApplicationRouter(routes); return new ApplicationRouter(deps, routes);
} }
/** /**
@@ -51,12 +80,33 @@ export class ApplicationRouter {
* @param connection - Shared connection stream for this transport session. * @param connection - Shared connection stream for this transport session.
*/ */
async dispatch(request: ApplicationRequest, connection: BaseStream): Promise<void> { async dispatch(request: ApplicationRequest, connection: BaseStream): Promise<void> {
// Authenticate the headers on the request.
const { publicKey, signature, timestamp } = accountSchema.parse(request.headers);
// Make sure the request signature is valid and hasnt been used before
await this.deps.auth.verifyUniqueRequest(signature);
// Ensure that the headers are present.
if (!publicKey || !signature || !timestamp) {
throw new UnauthorizedError('Missing authentication headers');
}
// Compile the signature payload as `Path:Timestamp:Body`
const signaturePayload = `${timestamp}:${request.path}:${toExtendedJson(request.body)}`;
// Verify the signature of the request.
const verified = await this.deps.auth.verifySignature(publicKey, signature, signaturePayload);
if (!verified) {
throw new UnauthorizedError('Invalid signature');
}
// Get the route from the routes map.
const route = this.routes.get(request.path); const route = this.routes.get(request.path);
if (!route) { if (!route) {
throw new ApplicationError(404, `No route found for ${request.path}`); throw new ApplicationError(404, `No route found for ${request.path}`);
} }
const stream = new ApplicationRouteStream(connection, request.body, request.requestId); const stream = new ApplicationRouteStream(connection, request.body, request.path, request.requestId, request.headers);
await route.handler(stream); await route.handler(stream);
} }
+4 -4
View File
@@ -40,8 +40,8 @@ export class ServerHost {
const corsMiddleware = cors({ const corsMiddleware = cors({
origin: corsConfig.origin ?? '*', origin: corsConfig.origin ?? '*',
allowMethods: corsConfig.methods ?? ['POST', 'OPTIONS'], allowMethods: corsConfig.methods ?? [ 'POST', 'OPTIONS' ],
allowHeaders: corsConfig.allowedHeaders ?? ['Content-Type', 'Accept'], allowHeaders: corsConfig.allowedHeaders ?? [ 'Content-Type', 'Accept' ],
}); });
this.app.use('*', corsMiddleware); this.app.use('*', corsMiddleware);
@@ -77,7 +77,7 @@ export class ServerHost {
throw new Error('ServerHost supports only one WebSocket upgrade server'); throw new Error('ServerHost supports only one WebSocket upgrade server');
} }
const [upgradeTransport] = upgradeTransports; const [ upgradeTransport ] = upgradeTransports;
this.server = serve({ this.server = serve({
fetch: this.app.fetch, fetch: this.app.fetch,
@@ -124,7 +124,7 @@ export class ServerHost {
const closeTransports = this.transports.map((transport) => Promise.resolve(transport.stop?.())); const closeTransports = this.transports.map((transport) => Promise.resolve(transport.stop?.()));
// Create a promise that resolves when the server and transports are closed // Create a promise that resolves when the server and transports are closed
this.stopPromise = Promise.all([closeServer, ...closeTransports]).then(() => { this.stopPromise = Promise.all([ closeServer, ...closeTransports ]).then(() => {
this.stopPromise = undefined; this.stopPromise = undefined;
}); });
+11 -6
View File
@@ -6,6 +6,7 @@ import type { Logger } from '../../utils/logger.ts';
/** Options required to open a SQLite database connection. */ /** Options required to open a SQLite database connection. */
export type DatabaseOptions = { export type DatabaseOptions = {
/** Filesystem path to the SQLite database file. */ /** Filesystem path to the SQLite database file. */
path: string; path: string;
@@ -39,9 +40,6 @@ export class Database {
this.kysely = new Kysely<DatabaseTables>({ this.kysely = new Kysely<DatabaseTables>({
dialect: this.dialect, dialect: this.dialect,
}); });
// Configure the SQLite pragmas.
this.configurePragmas();
} }
/** /**
@@ -53,6 +51,13 @@ export class Database {
return this.kysely; return this.kysely;
} }
async start(): Promise<void> {
this.debug('starting database connection');
// Configure the SQLite pragmas.
await this.configurePragmas();
}
/** /**
* Destroys the database connection. * Destroys the database connection.
*/ */
@@ -66,10 +71,10 @@ export class Database {
* *
* WAL improves write concurrency; foreign keys enforce referential integrity. * WAL improves write concurrency; foreign keys enforce referential integrity.
*/ */
private configurePragmas(): void { private async configurePragmas(): Promise<void> {
this.debug('configuring SQLite pragmas'); this.debug('configuring SQLite pragmas');
this.kysely.executeQuery(CompiledQuery.raw('PRAGMA journal_mode = WAL')); await this.kysely.executeQuery(CompiledQuery.raw('PRAGMA journal_mode = WAL'));
this.kysely.executeQuery(CompiledQuery.raw('PRAGMA foreign_keys = ON')); await this.kysely.executeQuery(CompiledQuery.raw('PRAGMA foreign_keys = ON'));
} }
} }
@@ -23,7 +23,18 @@ export const up = async (db: Kysely<DatabaseTables>): Promise<void> => {
.addColumn('public_key', 'text', (col) => col.notNull()) .addColumn('public_key', 'text', (col) => col.notNull())
.addColumn('blob', 'blob', (col) => col.notNull()) .addColumn('blob', 'blob', (col) => col.notNull())
.addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime)) .addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime))
.addPrimaryKeyConstraint('pk_resource_data', ['resource_id', 'public_key']) .addColumn('signature', 'text', (col) => col.notNull())
.addPrimaryKeyConstraint('pk_resource_data', [ 'resource_id', 'public_key' ])
.execute();
// Table for authed requests
// We will store the signature and the timestamp of the request, and we will clear out rows that are older than our msTimeout for our auth
await db.schema
.createTable('authed_requests')
.ifNotExists()
.addColumn('signature', 'text', (col) => col.notNull())
.addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime))
.addPrimaryKeyConstraint('pk_authed_requests', [ 'signature' ])
.execute(); .execute();
}; };
@@ -33,5 +44,9 @@ export const up = async (db: Kysely<DatabaseTables>): Promise<void> => {
* @param db - Kysely database to apply the rollback against. * @param db - Kysely database to apply the rollback against.
*/ */
export const down = async (db: Kysely<DatabaseTables>): Promise<void> => { export const down = async (db: Kysely<DatabaseTables>): Promise<void> => {
await db.schema.dropTable('resource_data').ifExists().execute(); await db.schema.dropTable('resource_data').ifExists()
.execute();
await db.schema.dropTable('authed_requests').ifExists()
.execute();
}; };
+14
View File
@@ -10,6 +10,7 @@ export type BlobColumn = ColumnType<Buffer, Buffer | Uint8Array, Buffer>;
* One row per (resource_id, public_key). Each publicKey owns a slot within a shared resource. * One row per (resource_id, public_key). Each publicKey owns a slot within a shared resource.
*/ */
export interface ResourceDataTable { export interface ResourceDataTable {
/** Shared resource identifier grouping related instances. */ /** Shared resource identifier grouping related instances. */
resource_id: string; resource_id: string;
@@ -21,9 +22,22 @@ export interface ResourceDataTable {
/** Millisecond timestamp of the last write. */ /** Millisecond timestamp of the last write. */
timestamp: Timestamp; timestamp: Timestamp;
/** Signature of the write. */
signature: string;
}
export interface AuthedRequestsTable {
/** Signature of the request. */
signature: string;
/** Millisecond timestamp of the request. */
timestamp: Timestamp;
} }
/** Complete Kysely schema mapping for the sync server database. */ /** Complete Kysely schema mapping for the sync server database. */
export interface DatabaseTables { export interface DatabaseTables {
resource_data: ResourceDataTable; resource_data: ResourceDataTable;
authed_requests: AuthedRequestsTable;
} }
+2
View File
@@ -1,5 +1,6 @@
/** A normal request/response result before transport encoding. */ /** A normal request/response result before transport encoding. */
export type StreamResponse = { export type StreamResponse = {
/** Optional correlation ID for multiplexed transports. */ /** Optional correlation ID for multiplexed transports. */
id?: string; id?: string;
@@ -15,6 +16,7 @@ export type StreamResponse = {
/** An application event before a transport applies its wire encoding. */ /** An application event before a transport applies its wire encoding. */
export type StreamEvent = { export type StreamEvent = {
/** Optional event or correlation ID. */ /** Optional event or correlation ID. */
id?: string; id?: string;
@@ -0,0 +1,59 @@
import { Hono } from "hono";
import { RequestHeaders } from "../../routes/types";
import { ApplicationError } from "../../errors";
import { HTTP_STATUS_CODE_BAD_REQUEST } from "../../constants";
/** RFC 9110 field-name token grammar. */
const HEADER_NAME_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
/** Hono variables populated by transport-boundary middleware. */
export type AppEnv = {
Variables: {
/** Decoded Extended JSON request body, when present. */
parsedBody?: unknown;
/** Raw JSON text preserved for signature verification. */
rawJsonBody?: string;
};
};
export abstract class BaseTransport {
/** Attach wire endpoints and middleware to the shared Hono application. */
abstract register(app: Hono<AppEnv>): void
/** Close transport-owned long-lived connections during server shutdown. */
abstract stop(): Promise<void>
/**
* Validate and normalize request headers at a transport boundary.
*
* Lowercase names give HTTP and WebSocket routes identical lookup semantics.
* Case-insensitive duplicates are rejected instead of selecting an ambiguous
* authentication value. Header values may not contain line breaks.
*/
public static normalizeRequestHeaders(headers: Readonly<Record<string, string>>): RequestHeaders {
const normalizedEntries: Array<[string, string]> = [];
const names = new Set<string>();
for (const [ name, value ] of Object.entries(headers)) {
if (!HEADER_NAME_PATTERN.test(name)) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid request header name');
}
if (value.includes('\r') || value.includes('\n')) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid request header value');
}
const normalizedName = name.toLowerCase();
if (names.has(normalizedName)) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Duplicate request header name');
}
names.add(normalizedName);
normalizedEntries.push([ normalizedName, value ]);
}
return Object.freeze(Object.fromEntries(normalizedEntries));
};
}
+5 -2
View File
@@ -5,12 +5,12 @@ import { toExtendedJson, fromExtendedJson } from '@xo-cash/utils';
import type { Logger } from '../../utils/logger.ts'; import type { Logger } from '../../utils/logger.ts';
import type { ApplicationRequest, ApplicationRouter } from '../router.ts'; import type { ApplicationRequest, ApplicationRouter } from '../router.ts';
import type { StreamResponse } from '../stream/base-stream.ts'; import type { StreamResponse } from '../stream/base-stream.ts';
import type { AppEnv, TransportRouter } from './transport-router.ts';
import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; import { ApplicationError, normalizePublicError } from '../../errors/index.ts';
import { HTTP_STATUS_CODE_BAD_REQUEST, HTTP_STATUS_CODE_NO_CONTENT } from '../../constants.ts'; import { HTTP_STATUS_CODE_BAD_REQUEST, HTTP_STATUS_CODE_NO_CONTENT } from '../../constants.ts';
import { HonoSSEStream } from '../stream/hono-sse-stream.ts'; import { HonoSSEStream } from '../stream/hono-sse-stream.ts';
import { HttpRequestStream } from '../stream/http-request-stream.ts'; import { HttpRequestStream } from '../stream/http-request-stream.ts';
import { type AppEnv, BaseTransport } from './base-transport.ts';
/** Hono context key where decoded Extended JSON bodies are stored. */ /** Hono context key where decoded Extended JSON bodies are stored. */
const PARSED_BODY_KEY = 'parsedBody'; const PARSED_BODY_KEY = 'parsedBody';
@@ -21,7 +21,7 @@ const PARSED_BODY_KEY = 'parsedBody';
* Normal HTTP and SSE both enter the same application router with different * Normal HTTP and SSE both enter the same application router with different
* connection-stream capabilities. * connection-stream capabilities.
*/ */
export class HttpTransportRouter implements TransportRouter { export class HttpTransportRouter extends BaseTransport {
private readonly debug: Logger; private readonly debug: Logger;
/** SSE connections retained until their final subscription or peer closes. */ /** SSE connections retained until their final subscription or peer closes. */
@@ -35,6 +35,8 @@ export class HttpTransportRouter implements TransportRouter {
private readonly router: ApplicationRouter, private readonly router: ApplicationRouter,
debug: Logger, debug: Logger,
) { ) {
super();
this.debug = debug.extend('http-transport'); this.debug = debug.extend('http-transport');
} }
@@ -111,6 +113,7 @@ export class HttpTransportRouter implements TransportRouter {
return { return {
path: context.req.path, path: context.req.path,
...(body === undefined ? {} : { body }), ...(body === undefined ? {} : { body }),
headers: context.req.header(),
}; };
} }
@@ -0,0 +1,38 @@
import type { RequestHeaders } from '../../routes/types.ts';
import { ApplicationError } from '../../errors/index.ts';
import { HTTP_STATUS_CODE_BAD_REQUEST } from '../../constants.ts';
/** RFC 9110 field-name token grammar. */
const HEADER_NAME_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
/**
* Validate and normalize request headers at a transport boundary.
*
* Lowercase names give HTTP and WebSocket routes identical lookup semantics.
* Case-insensitive duplicates are rejected instead of selecting an ambiguous
* authentication value. Header values may not contain line breaks.
*/
export const normalizeRequestHeaders = (headers: Readonly<Record<string, string>>): RequestHeaders => {
const normalizedEntries: Array<[string, string]> = [];
const names = new Set<string>();
for (const [ name, value ] of Object.entries(headers)) {
if (!HEADER_NAME_PATTERN.test(name)) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid request header name');
}
if (value.includes('\r') || value.includes('\n')) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid request header value');
}
const normalizedName = name.toLowerCase();
if (names.has(normalizedName)) {
throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Duplicate request header name');
}
names.add(normalizedName);
normalizedEntries.push([ normalizedName, value ]);
}
return Object.freeze(Object.fromEntries(normalizedEntries));
};
@@ -4,6 +4,7 @@ import type { Hono } from 'hono';
/** Hono variables populated by transport-boundary middleware. */ /** Hono variables populated by transport-boundary middleware. */
export type AppEnv = { export type AppEnv = {
Variables: { Variables: {
/** Decoded Extended JSON request body, when present. */ /** Decoded Extended JSON request body, when present. */
parsedBody?: unknown; parsedBody?: unknown;
@@ -19,6 +20,7 @@ export type AppEnv = {
* not application routing. Implementations remain unaware of route modules. * not application routing. Implementations remain unaware of route modules.
*/ */
export interface TransportRouter { export interface TransportRouter {
/** /**
* Attach wire endpoints and middleware to the shared Hono application. * Attach wire endpoints and middleware to the shared Hono application.
* *
@@ -32,6 +34,7 @@ export interface TransportRouter {
/** A transport which also supplies the WebSocket server used during upgrade. */ /** A transport which also supplies the WebSocket server used during upgrade. */
export interface UpgradeTransportRouter extends TransportRouter { export interface UpgradeTransportRouter extends TransportRouter {
/** WebSocket server instance passed to the Node HTTP listener. */ /** WebSocket server instance passed to the Node HTTP listener. */
readonly websocketServer: WebSocketServerLike; readonly websocketServer: WebSocketServerLike;
} }
+8 -11
View File
@@ -8,11 +8,11 @@ import { z } from 'zod';
import type { Logger } from '../../utils/logger.ts'; import type { Logger } from '../../utils/logger.ts';
import type { ApplicationRequest, ApplicationRouter } from '../router.ts'; import type { ApplicationRequest, ApplicationRouter } from '../router.ts';
import type { AppEnv, UpgradeTransportRouter } from './transport-router.ts';
import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; import { ApplicationError, normalizePublicError } from '../../errors/index.ts';
import { HTTP_STATUS_CODE_BAD_REQUEST } from '../../constants.ts'; import { HTTP_STATUS_CODE_BAD_REQUEST } from '../../constants.ts';
import { WSStream } from '../stream/ws-stream.ts'; import { WSStream } from '../stream/ws-stream.ts';
import { type AppEnv, BaseTransport } from './base-transport.ts';
/** Default WebSocket upgrade path for application messages. */ /** Default WebSocket upgrade path for application messages. */
const WS_ROUTE = '/ws'; const WS_ROUTE = '/ws';
@@ -20,7 +20,8 @@ const WS_ROUTE = '/ws';
// Strict validation prevents legacy or protocol-specific fields reaching routes. // Strict validation prevents legacy or protocol-specific fields reaching routes.
const wsRequestSchema = z const wsRequestSchema = z
.object({ .object({
id: z.string().min(1).optional(), id: z.string().min(1)
.optional(),
path: z.string().min(1), path: z.string().min(1),
body: z.unknown().optional(), body: z.unknown().optional(),
}) })
@@ -32,7 +33,7 @@ const wsRequestSchema = z
* One WSStream is shared by every message on a socket, allowing subscribe and * One WSStream is shared by every message on a socket, allowing subscribe and
* unsubscribe requests to operate on the same broadcaster registration. * unsubscribe requests to operate on the same broadcaster registration.
*/ */
export class WsTransportRouter implements UpgradeTransportRouter { export class WsTransportRouter extends BaseTransport {
/** /**
* Native Node WebSocket server used by Hono's Node adapter. * Native Node WebSocket server used by Hono's Node adapter.
* *
@@ -41,9 +42,6 @@ export class WsTransportRouter implements UpgradeTransportRouter {
*/ */
private readonly wsServer: WebSocketServer; private readonly wsServer: WebSocketServer;
/** Upgrade server wired into the Node HTTP listener by ServerHost. */
readonly websocketServer: WebSocketServerLike;
private readonly debug: Logger; private readonly debug: Logger;
/** /**
@@ -58,6 +56,8 @@ export class WsTransportRouter implements UpgradeTransportRouter {
private readonly maxRequestBodyBytes: number, private readonly maxRequestBodyBytes: number,
private readonly url: string = WS_ROUTE, private readonly url: string = WS_ROUTE,
) { ) {
super();
this.debug = debug.extend('ws-transport'); this.debug = debug.extend('ws-transport');
// Hono's Node WebSocket helper delegates frame handling to `ws`; unlike // Hono's Node WebSocket helper delegates frame handling to `ws`; unlike
@@ -67,7 +67,6 @@ export class WsTransportRouter implements UpgradeTransportRouter {
noServer: true, noServer: true,
maxPayload: this.maxRequestBodyBytes, maxPayload: this.maxRequestBodyBytes,
}); });
this.websocketServer = this.wsServer as unknown as WebSocketServerLike;
} }
/** /**
@@ -178,13 +177,11 @@ export class WsTransportRouter implements UpgradeTransportRouter {
* @param error - Failure to normalize into the public error shape. * @param error - Failure to normalize into the public error shape.
*/ */
private sendError(ws: WSContext, requestId: string | undefined, error: unknown): void { private sendError(ws: WSContext, requestId: string | undefined, error: unknown): void {
ws.send( ws.send(toExtendedJson({
toExtendedJson({
...(requestId === undefined ? {} : { id: requestId }), ...(requestId === undefined ? {} : { id: requestId }),
type: 'error', type: 'error',
...normalizePublicError(error), ...normalizePublicError(error),
}), }));
);
} }
/** /**
+68
View File
@@ -0,0 +1,68 @@
import { ApplicationRouter } from "../../source/services/router";
import { BaseStream } from "../../source/services/stream/base-stream";
export type ControlledRequestDeps = {
router: ApplicationRouter;
connection: BaseStream;
}
export type ControlledRequestOptions = {
path: string;
requestId: string;
body?: unknown;
headers?: Record<string, string>;
includeAuthHeaders?: boolean;
timeout?: number;
}
export type ControlledRequestParams = ControlledRequestOptions & ControlledRequestDeps;
export type ControlledRequest = {
request: Promise<void>;
started: Promise<void>;
release: () => void;
}
export const createControlledRequest = ({ router, connection, ...requestOptions }: ControlledRequestParams): ControlledRequest => {
// Create promises with resolvers to signal the request has started and released
const { promise: started, resolve: signalStarted } = Promise.withResolvers<void>();
const { promise: released, resolve: release } = Promise.withResolvers<void>();
// The request options (sets the includeAuthHeaders to true by default)
const options = {
includeAuthHeaders: true,
...requestOptions
}
// The test auth headers
const authHeaders = {
'x-public-key': 'public-key',
'x-signature': 'signature',
'x-timestamp': '1000',
}
// If the request should include the test auth headers (default is true)
if (options.includeAuthHeaders) {
options.headers = {
...options.headers,
...authHeaders,
};
}
// Add the signalStarted and released promises to the request body
options.body = {
...(options.body ?? {}),
signalStarted,
released,
}
// Dispatch the request
const request = router.dispatch(options, connection);
// Return the request, started, and release promises
return {
request,
started,
release,
};
};
+29
View File
@@ -0,0 +1,29 @@
import { vi } from "vitest";
import type { AuthSecp256k1 } from "../../source/auth/auth";
import type { RouteDefinition, RouteModule } from "../../source/routes/types";
/**
* Convert an array of route definitions to a route module by returning an object with a getRoutes method that returns the routes.
* @param routes - The route definitions to convert.
* @returns A route module.
*/
export const toRoutes = (routes: RouteDefinition[]): RouteModule => {
return {
getRoutes: async () => routes,
};
};
export const mockAuth = {
verifySignature: vi.fn().mockResolvedValue(true),
verifyUniqueRequest: vi.fn().mockResolvedValue(true),
assertTimestampFreshness: vi.fn().mockResolvedValue(true),
} as unknown as AuthSecp256k1;
/**
* Create a mock of the AuthSecp256k1 service
* @returns A mock of the AuthSecp256k1 service
*/
export const createMockAuth = (): AuthSecp256k1 => {
return mockAuth;
};
+3 -5
View File
@@ -1,7 +1,4 @@
import { import { BaseStream, type StreamMessage } from '../../source/services/stream/base-stream.ts';
BaseStream,
type StreamMessage,
} from "../../source/services/stream/base-stream.js";
/** Minimal observable connection used by application and broadcaster tests. */ /** Minimal observable connection used by application and broadcaster tests. */
export class TestConnection extends BaseStream { export class TestConnection extends BaseStream {
@@ -18,7 +15,7 @@ export class TestConnection extends BaseStream {
async send(message: StreamMessage): Promise<void> { async send(message: StreamMessage): Promise<void> {
if (this.closed) { if (this.closed) {
throw new Error("connection is closed"); throw new Error('connection is closed');
} }
this.messages.push(message); this.messages.push(message);
@@ -37,6 +34,7 @@ export class TestConnection extends BaseStream {
onClose(callback: () => void): void { onClose(callback: () => void): void {
if (this.closed) { if (this.closed) {
callback(); callback();
return; return;
} }
+64 -54
View File
@@ -1,24 +1,26 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import { DataRoute } from "../../source/routes/resources.js"; import { DataRoute } from '../../source/routes/resources.ts';
import { UnauthorizedError } from "../../source/errors/index.js"; import { UnauthorizedError } from '../../source/errors/index.ts';
import { type BaseBroadcaster } from "../../source/services/broadcaster.js"; import { type BaseBroadcaster } from '../../source/services/broadcaster.ts';
import { ApplicationRouteStream } from "../../source/services/route-stream.js"; import { ApplicationRouteStream } from '../../source/services/route-stream.ts';
import { Database } from "../../source/services/storage/database.js"; import type { Database } from '../../source/services/storage/database.ts';
import { TestConnection } from "../helpers/test-connection.js"; import { TestConnection } from '../helpers/test-connection.ts';
import { HTTP_STATUS_CODE_NOT_ACCEPTED } from "../../source/constants.js"; import { HTTP_STATUS_CODE_NOT_ACCEPTED } from '../../source/constants.ts';
import { createMockAuth } from '../helpers/misc.ts';
import { AuthSecp256k1 } from '../../source/auth/auth.ts';
function createBroadcasterStub() { const createBroadcasterStub = (): BaseBroadcaster => {
return { return {
subscribe: vi.fn(), subscribe: vi.fn(),
unsubscribe: vi.fn().mockResolvedValue(undefined), unsubscribe: vi.fn().mockResolvedValue(undefined),
publish: vi.fn(), publish: vi.fn(),
sendEvent: vi.fn(), sendEvent: vi.fn(),
} as unknown as BaseBroadcaster; } as unknown as BaseBroadcaster;
} };
describe("DataRoute subscriptions", () => { describe('DataRoute subscriptions', (): void => {
it("subscribes to future resource changes until removal", async () => { it('subscribes to future resource changes until removal', async (): Promise<void> => {
let resolveRemoved: () => void = () => undefined; let resolveRemoved: () => void = () => undefined;
const removed = new Promise<void>((resolve) => { const removed = new Promise<void>((resolve) => {
resolveRemoved = resolve; resolveRemoved = resolve;
@@ -32,30 +34,28 @@ describe("DataRoute subscriptions", () => {
vi.mocked(broadcaster.subscribe).mockReturnValue(removed); vi.mocked(broadcaster.subscribe).mockReturnValue(removed);
const connection = new TestConnection(true, false); const connection = new TestConnection(true, false);
const stream = new ApplicationRouteStream(connection, { const stream = new ApplicationRouteStream(connection, {
resourceId: ["a", "b"], resourceId: [ 'a', 'b' ],
}, 'subscribe-1');
const route = new DataRoute({
auth: await AuthSecp256k1.create({ database: storage }, { timestampWindowMs: 0 }),
database: storage,
broadcaster: broadcaster,
}, {
timestampWindowMs: 0,
}); });
const route = new DataRoute(storage, broadcaster, 0);
const execution = route.subscribeData(stream); const execution = route.subscribeData(stream);
expect(broadcaster.subscribe).toHaveBeenCalledWith( expect(broadcaster.subscribe).toHaveBeenCalledWith(stream, [ 'resource:a', 'resource:b' ]);
stream,
["resource:a", "resource:b"],
);
expect(storage.db.transaction).not.toHaveBeenCalled(); expect(storage.db.transaction).not.toHaveBeenCalled();
expect(connection.messages).toEqual([]); expect(connection.messages).toEqual([]);
await expect( await expect(Promise.race([ execution.then(() => 'settled'), Promise.resolve('pending') ])).resolves.toBe('pending');
Promise.race([
execution.then(() => "settled"),
Promise.resolve("pending"),
]),
).resolves.toBe("pending");
resolveRemoved(); resolveRemoved();
await execution; await execution;
}); });
it("unsubscribes a bidirectional connection and acknowledges the request", async () => { it('unsubscribes a bidirectional connection and acknowledges the request', async (): Promise<void> => {
const storage = { const storage = {
db: { db: {
transaction: vi.fn(), transaction: vi.fn(),
@@ -63,58 +63,70 @@ describe("DataRoute subscriptions", () => {
} as unknown as Database; } as unknown as Database;
const broadcaster = createBroadcasterStub(); const broadcaster = createBroadcasterStub();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const stream = new ApplicationRouteStream( const stream = new ApplicationRouteStream(connection, { resourceId: [ 'a' ] }, '/unsubscribe', 'unsubscribe-1');
connection, const route = new DataRoute({
{ resourceId: ["a"] }, auth: await AuthSecp256k1.create({ database: storage }, { timestampWindowMs: 0 }),
"unsubscribe-1", database: storage,
); broadcaster: broadcaster,
const route = new DataRoute(storage, broadcaster, 0); }, {
timestampWindowMs: 0,
});
await route.unsubscribeData(stream); await route.unsubscribeData(stream);
expect(broadcaster.unsubscribe).toHaveBeenCalledWith(stream, [ expect(broadcaster.unsubscribe).toHaveBeenCalledWith(stream, [ 'resource:a' ]);
"resource:a",
]);
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
{ {
id: "unsubscribe-1", id: 'unsubscribe-1',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: {}, body: {},
}, },
]); ]);
}); });
it("rejects selective unsubscribe on a one-way connection", async () => { it('rejects selective unsubscribe on a one-way connection', async (): Promise<void> => {
const storage = { const storage = {
db: { db: {
transaction: vi.fn(), transaction: vi.fn(),
}, },
} as unknown as Database; } as unknown as Database;
const broadcaster = createBroadcasterStub(); const broadcaster = createBroadcasterStub();
const stream = new ApplicationRouteStream(new TestConnection(true, false), { const connection = new TestConnection(true, false);
resourceId: ["a"], const stream = new ApplicationRouteStream(connection, {
resourceId: [ 'a' ],
}, '/unsubscribe', 'unsubscribe-1');
const route = new DataRoute({
auth: await AuthSecp256k1.create({ database: storage }, { timestampWindowMs: 0 }),
database: storage,
broadcaster: broadcaster,
}, {
timestampWindowMs: 0,
}); });
const route = new DataRoute(storage, broadcaster, 0);
await expect(route.unsubscribeData(stream)).rejects.toMatchObject({ statusCode: HTTP_STATUS_CODE_NOT_ACCEPTED }); await expect(route.unsubscribeData(stream)).rejects.toMatchObject({ statusCode: HTTP_STATUS_CODE_NOT_ACCEPTED });
expect(broadcaster.unsubscribe).not.toHaveBeenCalled(); expect(broadcaster.unsubscribe).not.toHaveBeenCalled();
}); });
}); });
describe("DataRoute resource write auth", () => { describe('DataRoute resource write auth', (): void => {
it("rejects an invalid resource signature before writing the batch", async () => { it('rejects an invalid resource signature before writing the batch', async (): Promise<void> => {
const storage = { const storage = {
db: { db: {
transaction: vi.fn(), transaction: vi.fn(),
}, },
} as unknown as Database; } as unknown as Database;
const broadcaster = createBroadcasterStub() const broadcaster = createBroadcasterStub();
const route = new DataRoute(storage, broadcaster, 0); const route = new DataRoute({
auth: await AuthSecp256k1.create({ database: storage }, { timestampWindowMs: 0 }),
database: storage,
broadcaster: broadcaster,
}, {
timestampWindowMs: 0,
});
await expect( await expect(route.writeData({
route.writeData({
connection: new TestConnection(true, true), connection: new TestConnection(true, true),
streaming: true, streaming: true,
bidirectional: true, bidirectional: true,
@@ -122,19 +134,17 @@ describe("DataRoute resource write auth", () => {
body: { body: {
resources: [ resources: [
{ {
id: "resource-a", id: 'resource-a',
publicKey: "not-a-public-key", publicKey: 'not-a-public-key',
timestamp: Date.now(), timestamp: Date.now(),
signature: "not-a-signature", signature: 'not-a-signature',
value: new Uint8Array([1, 2, 3]), value: new Uint8Array([ 1, 2, 3 ]),
}, },
], ],
}, },
} as unknown as ApplicationRouteStream), } as unknown as ApplicationRouteStream)).rejects.toBeInstanceOf(UnauthorizedError);
).rejects.toBeInstanceOf(UnauthorizedError);
expect(storage.db.transaction).not.toHaveBeenCalled() expect(storage.db.transaction).not.toHaveBeenCalled();
expect(broadcaster.publish).not.toHaveBeenCalled(); expect(broadcaster.publish).not.toHaveBeenCalled();
}); });
}); });
+60 -79
View File
@@ -1,133 +1,119 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import { ApplicationError } from "../../source/errors/index.js"; import { Broadcaster } from '../../source/services/broadcaster.ts';
import { Broadcaster } from "../../source/services/broadcaster.js"; import { ApplicationRouteStream } from '../../source/services/route-stream.ts';
import { ApplicationRouteStream } from "../../source/services/route-stream.js"; import { Logger } from '../../source/utils/logger.ts';
import { Logger } from "../../source/utils/logger.js"; import { TestConnection } from '../helpers/test-connection.ts';
import { TestConnection } from "../helpers/test-connection.js";
function createBroadcaster(): Broadcaster { const createBroadcaster = (): Broadcaster => {
return new Broadcaster(new Logger("broadcaster-test")); return new Broadcaster(new Logger('broadcaster-test'));
} };
function routeStream(connection: TestConnection): ApplicationRouteStream { const routeStream = (connection: TestConnection): ApplicationRouteStream => {
return new ApplicationRouteStream(connection, undefined); return new ApplicationRouteStream(connection, undefined, '/items', 'items-1');
} };
async function expectPending(promise: Promise<void>): Promise<void> { const expectPending = async (promise: Promise<void>): Promise<void> => {
const settled = vi.fn(); const settled = vi.fn();
void promise.then(settled); void promise.then(settled);
await Promise.resolve(); await Promise.resolve();
expect(settled).not.toHaveBeenCalled(); expect(settled).not.toHaveBeenCalled();
} };
describe("Broadcaster subscriptions", () => { describe('Broadcaster subscriptions', () => {
it("delivers events and resolves after a later request removes the topic", async () => { it('delivers events and resolves after a later request removes the topic', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const subscribed = broadcaster.subscribe(routeStream(connection), [ const subscribed = broadcaster.subscribe(routeStream(connection), [ 'items' ]);
"items",
]);
await expectPending(subscribed); await expectPending(subscribed);
await broadcaster.publish("items", { await broadcaster.publish('items', {
type: "item-changed", type: 'item-changed',
data: { id: "a" }, data: { id: 'a' },
}); });
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
expect.objectContaining({ expect.objectContaining({
type: "item-changed", type: 'item-changed',
data: { id: "a" }, data: { id: 'a' },
}), }),
]); ]);
// A different request-scoped facade still resolves the connection's // A different request-scoped facade still resolves the connection's
// original subscription. // original subscription.
await broadcaster.unsubscribe(routeStream(connection), ["items"]); await broadcaster.unsubscribe(routeStream(connection), [ 'items' ]);
await expect(subscribed).resolves.toBeUndefined(); await expect(subscribed).resolves.toBeUndefined();
}); });
it("resolves fully duplicate subscriptions immediately", async () => { it('resolves fully duplicate subscriptions immediately', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const first = broadcaster.subscribe(routeStream(connection), [ const first = broadcaster.subscribe(routeStream(connection), [ 'items', 'items' ]);
"items", const duplicate = broadcaster.subscribe(routeStream(connection), [ 'items' ]);
"items",
]);
const duplicate = broadcaster.subscribe(routeStream(connection), ["items"]);
await expect(duplicate).resolves.toBeUndefined(); await expect(duplicate).resolves.toBeUndefined();
await expectPending(first); await expectPending(first);
expect(connection.closeCallbacks).toHaveLength(1); expect(connection.closeCallbacks).toHaveLength(1);
await broadcaster.unsubscribe(routeStream(connection), ["items"]); await broadcaster.unsubscribe(routeStream(connection), [ 'items' ]);
await first; await first;
}); });
it("waits only for topics newly added by a partially overlapping call", async () => { it('waits only for topics newly added by a partially overlapping call', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const first = broadcaster.subscribe(routeStream(connection), ["a"]); const first = broadcaster.subscribe(routeStream(connection), [ 'a' ]);
const second = broadcaster.subscribe(routeStream(connection), ["a", "b"]); const second = broadcaster.subscribe(routeStream(connection), [ 'a', 'b' ]);
await broadcaster.unsubscribe(routeStream(connection), ["a"]); await broadcaster.unsubscribe(routeStream(connection), [ 'a' ]);
await expect(first).resolves.toBeUndefined(); await expect(first).resolves.toBeUndefined();
await expectPending(second); await expectPending(second);
await broadcaster.unsubscribe(routeStream(connection), ["b"]); await broadcaster.unsubscribe(routeStream(connection), [ 'b' ]);
await expect(second).resolves.toBeUndefined(); await expect(second).resolves.toBeUndefined();
}); });
it("resolves every pending subscription and removes topics on close", async () => { it('resolves every pending subscription and removes topics on close', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, false); const connection = new TestConnection(true, false);
const first = broadcaster.subscribe(routeStream(connection), ["a"]); const first = broadcaster.subscribe(routeStream(connection), [ 'a' ]);
const second = broadcaster.subscribe(routeStream(connection), ["b"]); const second = broadcaster.subscribe(routeStream(connection), [ 'b' ]);
connection.close(); connection.close();
await Promise.all([first, second]); await Promise.all([ first, second ]);
await broadcaster.publish("a", { type: "changed", data: null }); await broadcaster.publish('a', { type: 'changed', data: null });
await broadcaster.publish("b", { type: "changed", data: null }); await broadcaster.publish('b', { type: 'changed', data: null });
expect(connection.messages).toEqual([]); expect(connection.messages).toEqual([]);
}); });
it("immediately resolves registration against an already-closed connection", async () => { it('immediately resolves registration against an already-closed connection', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, false); const connection = new TestConnection(true, false);
connection.close(); connection.close();
await expect( await expect(broadcaster.subscribe(routeStream(connection), [ 'items' ])).resolves.toBeUndefined();
broadcaster.subscribe(routeStream(connection), ["items"]),
).resolves.toBeUndefined();
}); });
it("rejects subscriptions on a non-streaming connection", () => { it('rejects subscriptions on a non-streaming connection', (): void => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(false, false); const connection = new TestConnection(false, false);
expect(() => expect(() => broadcaster.subscribe(routeStream(connection), [ 'items' ])).toThrowError(expect.objectContaining({ statusCode: 406 }));
broadcaster.subscribe(routeStream(connection), ["items"]),
).toThrowError(
expect.objectContaining({ statusCode: 406 }),
);
}); });
it("treats an empty subscription and repeated unsubscribe as no-ops", async () => { it('treats an empty subscription and repeated unsubscribe as no-ops', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
await expect( await expect(broadcaster.subscribe(routeStream(connection), [])).resolves.toBeUndefined();
broadcaster.subscribe(routeStream(connection), []), await broadcaster.unsubscribe(routeStream(connection), [ 'missing' ]);
).resolves.toBeUndefined();
await broadcaster.unsubscribe(routeStream(connection), ["missing"]);
await broadcaster.unsubscribe(routeStream(connection)); await broadcaster.unsubscribe(routeStream(connection));
expect(connection.closeCallbacks).toHaveLength(0); expect(connection.closeCallbacks).toHaveLength(0);
}); });
it("fans out concurrently to independent connections", async () => { it('fans out concurrently to independent connections', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const first = new TestConnection(true, false); const first = new TestConnection(true, false);
const second = new TestConnection(true, false); const second = new TestConnection(true, false);
@@ -141,23 +127,20 @@ describe("Broadcaster subscriptions", () => {
markSecondSent = resolve; markSecondSent = resolve;
}); });
first.send = async (message) => { first.send = async (message): Promise<void> => {
await firstReleased; await firstReleased;
await originalFirstSend(message); await originalFirstSend(message);
}; };
second.send = async (message) => {
second.send = async (message): Promise<void> => {
await TestConnection.prototype.send.call(second, message); await TestConnection.prototype.send.call(second, message);
markSecondSent(); markSecondSent();
}; };
const firstSubscription = broadcaster.subscribe(routeStream(first), [ const firstSubscription = broadcaster.subscribe(routeStream(first), [ 'items' ]);
"items", const secondSubscription = broadcaster.subscribe(routeStream(second), [ 'items' ]);
]); const publication = broadcaster.publish('items', {
const secondSubscription = broadcaster.subscribe(routeStream(second), [ type: 'item-changed',
"items",
]);
const publication = broadcaster.publish("items", {
type: "item-changed",
data: {}, data: {},
}); });
@@ -170,19 +153,17 @@ describe("Broadcaster subscriptions", () => {
first.close(); first.close();
second.close(); second.close();
await Promise.all([firstSubscription, secondSubscription]); await Promise.all([ firstSubscription, secondSubscription ]);
}); });
it("closes and removes a connection whose event delivery fails", async () => { it('closes and removes a connection whose event delivery fails', async (): Promise<void> => {
const broadcaster = createBroadcaster(); const broadcaster = createBroadcaster();
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
connection.send = vi.fn().mockRejectedValue(new Error("socket failed")); connection.send = vi.fn().mockRejectedValue(new Error('socket failed'));
const subscribed = broadcaster.subscribe(routeStream(connection), [ const subscribed = broadcaster.subscribe(routeStream(connection), [ 'items' ]);
"items",
]);
await broadcaster.publish("items", { await broadcaster.publish('items', {
type: "item-changed", type: 'item-changed',
data: {}, data: {},
}); });
await subscribed; await subscribed;
@@ -190,8 +171,8 @@ describe("Broadcaster subscriptions", () => {
expect(connection.closed).toBe(true); expect(connection.closed).toBe(true);
expect(connection.send).toHaveBeenCalledOnce(); expect(connection.send).toHaveBeenCalledOnce();
await broadcaster.publish("items", { await broadcaster.publish('items', {
type: "item-changed", type: 'item-changed',
data: {}, data: {},
}); });
expect(connection.send).toHaveBeenCalledOnce(); expect(connection.send).toHaveBeenCalledOnce();
+1 -1
View File
@@ -18,7 +18,7 @@ const testConfigDefaultsTo1MiBRequestBodyLimit = (): void => {
expect(config.server.host).toBe('0.0.0.0'); expect(config.server.host).toBe('0.0.0.0');
expect(config.server.cors.origin).toBe('*'); expect(config.server.cors.origin).toBe('*');
expect(config.server.cors.methods).toEqual([ 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS' ]); expect(config.server.cors.methods).toEqual([ 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS' ]);
expect(config.server.cors.allowedHeaders).toEqual([ 'Content-Type', 'cache-control', 'X-Timestamp', 'X-PublicKey', 'X-Signature' ]); expect(config.server.cors.allowedHeaders).toEqual([ 'Content-Type', 'cache-control', 'X-Timestamp', 'X-Public-Key', 'X-Signature' ]);
expect(config.auth.timestampWindowMs).toBe(300000); expect(config.auth.timestampWindowMs).toBe(300000);
}; };
+95 -80
View File
@@ -1,48 +1,41 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from 'vitest';
import type { RouteDefinition, RouteModule } from "../../source/routes/types.js"; // Source
import { ApplicationError } from "../../source/errors/index.js"; import { ApplicationRouter } from '../../source/services/router.ts';
import { ApplicationRouter } from "../../source/services/router.js";
import { TestConnection } from "../helpers/test-connection.js";
function moduleWith(routes: RouteDefinition[]): RouteModule { // Helpers
return { import { createMockAuth, toRoutes } from '../helpers/misc.ts';
async getRoutes() { import { TestConnection } from '../helpers/test-connection.ts';
return routes; import { createControlledRequest } from '../helpers/controlled-request.ts';
},
};
}
describe("ApplicationRouter initialization", () => { /**
it("rejects duplicate exact paths during startup", async () => { * A mock of the AuthSecp256k1 service
const route = { url: "/echo", handler: () => undefined }; */
const auth = createMockAuth();
await expect( describe('ApplicationRouter initialization', (): void => {
ApplicationRouter.create([moduleWith([route]), moduleWith([route])]), it('rejects duplicate exact paths during startup', async (): Promise<void> => {
).rejects.toThrow("Duplicate application route: /echo"); const route = { url: '/echo', handler: (): void => undefined };
await expect(ApplicationRouter.create({ auth }, [ toRoutes([ route ]), toRoutes([ route ]) ])).rejects.toThrow('Duplicate application route: /echo');
}); });
it.each(["echo", "/", "/items/:id", "/items?active=true", "/items#active"])( it.each([ 'echo', '/', '/items/:id', '/items?active=true', '/items#active' ])('rejects the invalid route path %s', async (url): Promise<void> => {
"rejects the invalid route path %s", await expect(ApplicationRouter.create({ auth }, [ toRoutes([{ url, handler: (): void => undefined }]) ])).rejects.toThrow('Invalid application route');
async (url) => { });
await expect(
ApplicationRouter.create([
moduleWith([{ url, handler: () => undefined }]),
]),
).rejects.toThrow("Invalid application route");
},
);
}); });
describe("ApplicationRouter dispatch", () => { describe('ApplicationRouter dispatch', (): void => {
it("binds the connection, body, and request ID to one route stream", async () => { it('binds the connection, body, and request ID to one route stream', async (): Promise<void> => {
const connection = new TestConnection(false, false); const connection = new TestConnection(false, false);
const router = await ApplicationRouter.create([ const router = await ApplicationRouter.create({ auth }, [
moduleWith([ toRoutes([
{ {
url: "/echo", url: '/echo',
handler: async (stream) => { handler: async (stream): Promise<void> => {
expect(stream.connection).toBe(connection); expect(stream.connection).toBe(connection);
expect(stream.path).toBe('/echo');
expect(stream.headers).toEqual({ 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' });
await stream.send(stream.body); await stream.send(stream.body);
}, },
}, },
@@ -50,85 +43,107 @@ describe("ApplicationRouter dispatch", () => {
]); ]);
await router.dispatch( await router.dispatch(
{ path: "/echo", body: { value: 1 }, requestId: "request-1" }, {
path: '/echo',
body: { value: 1 },
requestId: '1',
headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' },
},
connection, connection,
); );
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
{ {
id: "request-1", id: '1',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { value: 1 }, body: { value: 1 },
}, },
]); ]);
await expect( await expect(router.dispatch(
router.dispatch({ path: "/echo/other", body: {} }, connection), { path: '/echo/other', body: {}, headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
).rejects.toMatchObject({ statusCode: 404 }); connection,
)).rejects.toMatchObject({ statusCode: 404 });
}); });
it("preserves correlation when concurrent requests finish out of order", async () => { it('preserves correlation when concurrent requests finish out of order', async (): Promise<void> => {
const completions = new Map<string, () => void>(); const router = await ApplicationRouter.create({ auth }, [
const router = await ApplicationRouter.create([ toRoutes([
moduleWith([
{ {
url: "/delayed", url: '/delayed',
handler: async (stream) => { handler: async (stream): Promise<void> => {
const key = (stream.body as { key: string }).key; const { key, signalStarted, released } = stream.body as {
await new Promise<void>((resolve) => completions.set(key, resolve)); key: string;
signalStarted: () => void;
released: Promise<void>;
};
signalStarted();
await released;
await stream.send({ key }); await stream.send({ key });
}, },
}, },
]), ]),
]); ]);
const connection = new TestConnection(true, true);
const first = router.dispatch( const connection = new TestConnection(false, false);
{ path: "/delayed", body: { key: "A" }, requestId: "A" },
connection,
);
const second = router.dispatch(
{ path: "/delayed", body: { key: "B" }, requestId: "B" },
connection,
);
completions.get("B")?.(); const first = createControlledRequest({ router, connection, path: '/delayed', requestId: 'A', body: { key: 'A' } });
await second; const second = createControlledRequest({ router, connection, path: '/delayed', requestId: 'B', body: { key: 'B' } });
completions.get("A")?.();
await first; expect(connection.messages).toEqual([]);
await Promise.all([ first.started, second.started ]);
second.release();
await second.request;
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
{ {
id: "B", id: 'B',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { key: "B" }, body: { key: 'B' },
},
{
id: "A",
type: "response",
statusCode: 200,
body: { key: "A" },
}, },
]); ]);
});
it("propagates route failures without infrastructure-specific cleanup", async () => { first.release();
const error = new Error("route failed"); await first.request;
const router = await ApplicationRouter.create([
moduleWith([ expect(connection.messages).toEqual([
{ {
url: "/failure", id: 'B',
handler: () => { type: 'response',
statusCode: 200,
body: { key: 'B' },
},
{
id: 'A',
type: 'response',
statusCode: 200,
body: { key: 'A' },
},
]);
}, 1000);
it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
const error = new Error('route failed');
const router = await ApplicationRouter.create({ auth }, [
toRoutes([
{
url: '/failure',
handler: (): void => {
throw error; throw error;
}, },
}, },
]), ]),
]); ]);
await expect( await expect(router.dispatch(
router.dispatch({ path: "/failure" }, new TestConnection(false, false)), { path: '/failure', headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
).rejects.toBe(error); new TestConnection(false, false),
)).rejects.toBe(error);
}); });
}); });
+16 -20
View File
@@ -1,34 +1,32 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import { HonoSSEStream } from "../../../source/services/stream/hono-sse-stream.js"; import { HonoSSEStream } from '../../../source/services/stream/hono-sse-stream.ts';
import { HttpRequestStream } from "../../../source/services/stream/http-request-stream.js"; import { HttpRequestStream } from '../../../source/services/stream/http-request-stream.ts';
import { WSStream } from "../../../source/services/stream/ws-stream.js"; import { WSStream } from '../../../source/services/stream/ws-stream.ts';
describe("stream lifecycle observers", () => { describe('stream lifecycle observers', (): void => {
it("buffers exactly one normal HTTP response", async () => { it('buffers exactly one normal HTTP response', async (): Promise<void> => {
const stream = new HttpRequestStream(); const stream = new HttpRequestStream();
await stream.send({ await stream.send({
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { ok: true }, body: { ok: true },
}); });
expect(stream.getResponse()).toEqual({ expect(stream.getResponse()).toEqual({
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { ok: true }, body: { ok: true },
}); });
await expect( await expect(stream.send({
stream.send({ type: 'response',
type: "response",
statusCode: 200, statusCode: 200,
body: { second: true }, body: { second: true },
}), })).rejects.toThrow('only send one response');
).rejects.toThrow("only send one response");
}); });
it("notifies WebSocket observers registered after remote closure", () => { it('notifies WebSocket observers registered after remote closure', (): void => {
const stream = new WSStream({ const stream = new WSStream({
send: vi.fn(), send: vi.fn(),
close: vi.fn(), close: vi.fn(),
@@ -42,19 +40,17 @@ describe("stream lifecycle observers", () => {
expect(onClose).toHaveBeenCalledOnce(); expect(onClose).toHaveBeenCalledOnce();
}); });
it("notifies SSE observers registered after local closure", () => { it('notifies SSE observers registered after local closure', async (): Promise<void> => {
const streamApi = { const streamApi = {
writeSSE: vi.fn(), writeSSE: vi.fn(),
close: vi.fn(), close: vi.fn(),
}; };
const stream = new HonoSSEStream( const stream = new HonoSSEStream(streamApi as unknown as ConstructorParameters<typeof HonoSSEStream>[0]);
streamApi as unknown as ConstructorParameters<typeof HonoSSEStream>[0],
);
const onClose = vi.fn(); const onClose = vi.fn();
stream.close(); await stream.close();
stream.onClose(onClose); stream.onClose(onClose);
stream.close(); await stream.close();
expect(streamApi.close).toHaveBeenCalledOnce(); expect(streamApi.close).toHaveBeenCalledOnce();
expect(onClose).toHaveBeenCalledOnce(); expect(onClose).toHaveBeenCalledOnce();
+111 -96
View File
@@ -1,27 +1,44 @@
import { Hono } from "hono"; import { Hono } from 'hono';
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import type { RouteDefinition } from "../../../source/routes/types.js"; import type { RouteDefinition } from '../../../source/routes/types.ts';
import { ApplicationError } from "../../../source/errors/index.js"; import { ApplicationError } from '../../../source/errors/index.ts';
import { ApplicationRouter } from "../../../source/services/router.js"; import { ApplicationRouter } from '../../../source/services/router.ts';
import { Broadcaster } from "../../../source/services/broadcaster.js"; import { Broadcaster } from '../../../source/services/broadcaster.ts';
import { HttpTransportRouter } from "../../../source/services/transport/http-transport.js"; import { HttpTransportRouter } from '../../../source/services/transport/http-transport.ts';
import type { AppEnv } from "../../../source/services/transport/transport-router.js"; import type { AppEnv } from '../../../source/services/transport/transport-router.ts';
import { fromExtendedJson, toExtendedJson } from "@xo-cash/utils"; import { fromExtendedJson, toExtendedJson } from '@xo-cash/utils';
import { Logger } from "../../../source/utils/logger.js"; import { Logger } from '../../../source/utils/logger.ts';
import { ServerHost } from "../../../source/services/server-host.js"; import { ServerHost } from '../../../source/services/server-host.ts';
import { AuthSecp256k1 } from '../../../source/auth/auth.ts';
async function createApp( /**
* A mock of the AuthSecp256k1 service
*/
const auth = {
verifySignature: vi.fn().mockResolvedValue(true),
verifyUniqueRequest: vi.fn().mockResolvedValue(true),
assertTimestampFreshness: vi.fn().mockResolvedValue(true),
} as unknown as AuthSecp256k1;
const mockAuthHeaders = {
'x-public-key': 'public-key',
'x-signature': 'signature',
'x-timestamp': '1000',
};
const createApp = async (
routes: RouteDefinition[] | ((broadcaster: Broadcaster) => RouteDefinition[]), routes: RouteDefinition[] | ((broadcaster: Broadcaster) => RouteDefinition[]),
maxRequestBodyBytes = 1024 * 1024, maxRequestBodyBytes = 1024 * 1024,
): Promise<Hono<AppEnv>> { ): Promise<Hono<AppEnv>> => {
const debug = new Logger("http-transport-test"); const debug = new Logger('http-transport-test');
const broadcaster = new Broadcaster(debug); const broadcaster = new Broadcaster(debug);
const resolvedRoutes = const resolvedRoutes = typeof routes === 'function' ? routes(broadcaster) : routes;
typeof routes === "function" ? routes(broadcaster) : routes; const router = await ApplicationRouter.create({
const router = await ApplicationRouter.create([ auth,
}, [
{ {
async getRoutes() { async getRoutes(): Promise<RouteDefinition[]> {
return resolvedRoutes; return resolvedRoutes;
}, },
}, },
@@ -30,128 +47,129 @@ async function createApp(
const app = new Hono<AppEnv>(); const app = new Hono<AppEnv>();
app.onError(HttpTransportRouter.createErrorHandler(debug)); app.onError(HttpTransportRouter.createErrorHandler(debug));
app.use("*", ServerHost.limitBodySizeMiddleware(maxRequestBodyBytes, debug)); app.use('*', ServerHost.limitBodySizeMiddleware(maxRequestBodyBytes, debug));
app.use("*", HttpTransportRouter.createExtJsonMiddleware(debug)); app.use('*', HttpTransportRouter.createExtJsonMiddleware(debug));
transport.register(app); transport.register(app);
return app;
}
describe("HttpTransportRouter", () => { return app;
it("runs normal HTTP through a non-streaming route stream", async () => { };
describe('HttpTransportRouter', (): void => {
it('runs normal HTTP through a non-streaming route stream', async (): Promise<void> => {
const app = await createApp([ const app = await createApp([
{ {
url: "/echo", url: '/echowtf',
handler: async (stream) => stream.send(stream.body), handler: async (stream): Promise<void> => stream.send(stream.body),
}, },
]); ]);
const value = new Uint8Array([1, 2, 3]); const value = new Uint8Array([ 1, 2, 3 ]);
const response = await app.request("/echo", { const request = new Request('http://localhost/echowtf', {
method: "POST", method: 'POST',
headers: { "content-type": "application/json" }, headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: toExtendedJson({ value }), body: toExtendedJson({ value }),
}); });
const response = await app.request(request);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(fromExtendedJson(await response.text())).toEqual({ value }); expect(fromExtendedJson(await response.text())).toEqual({ value });
}); });
it("returns 204 when a normal HTTP route sends nothing", async () => { it('returns 204 when a normal HTTP route sends nothing', async (): Promise<void> => {
const app = await createApp([ const app = await createApp([
{ {
url: "/nothing", url: '/nothing',
handler: () => undefined, handler: (): void => undefined,
}, },
]); ]);
const response = await app.request("/nothing", { method: "POST" }); const response = await app.request('/nothing', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(response.status).toBe(204); expect(response.status).toBe(204);
expect(await response.text()).toBe(""); expect(await response.text()).toBe('');
}); });
it("returns normalized errors for non-streaming requests", async () => { it('returns normalized errors for non-streaming requests', async (): Promise<void> => {
const app = await createApp([]); const app = await createApp([]);
const missing = await app.request("/missing", { method: "POST" }); const missing = await app.request('/missing', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(missing.status).toBe(404); expect(missing.status).toBe(404);
expect(await missing.json()).toEqual({ expect(await missing.json()).toEqual({
statusCode: 404, statusCode: 404,
error: "No route found for /missing", error: 'No route found for /missing',
}); });
const invalid = await app.request("/missing", { const invalid = await app.request('/missing', {
method: "POST", method: 'POST',
headers: { "content-type": "application/json" }, headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: "{", body: '{',
}); });
expect(invalid.status).toBe(400); expect(invalid.status).toBe(400);
expect(await invalid.json()).toEqual({ expect(await invalid.json()).toEqual({
statusCode: 400, statusCode: 400,
error: "Invalid JSON in request body", error: 'Invalid JSON in request body',
}); });
}); });
it("rejects subscribe when normal HTTP has no streaming capability", async () => { it('rejects subscribe when normal HTTP has no streaming capability', async (): Promise<void> => {
const app = await createApp((broadcaster) => [ const app = await createApp((broadcaster) => [
{ {
url: "/items/subscribe", url: '/items/subscribe',
handler: async (stream) => { handler: async (stream): Promise<void> => {
await broadcaster.subscribe(stream, ["items"]); await broadcaster.subscribe(stream, [ 'items' ]);
}, },
}, },
]); ]);
const response = await app.request("/items/subscribe", { method: "POST" }); const response = await app.request('/items/subscribe', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(response.status).toBe(406); expect(response.status).toBe(406);
expect(await response.json()).toMatchObject({ statusCode: 406 }); expect(await response.json()).toMatchObject({ statusCode: 406 });
}); });
it("sends SSE route errors as events and closes only that stream", async () => { it('sends SSE route errors as events and closes only that stream', async (): Promise<void> => {
const app = await createApp([ const app = await createApp([
{ {
url: "/items/subscribe", url: '/items/subscribe',
handler: () => { handler: (): void => {
throw new Error("private storage failure"); throw new Error('private storage failure');
}, },
}, },
]); ]);
const response = await app.request("/items/subscribe", { const response = await app.request('/items/subscribe', {
method: "POST", method: 'POST',
headers: { accept: "text/event-stream" }, headers: { accept: 'text/event-stream', ...mockAuthHeaders },
}); });
const events = await response.text(); const events = await response.text();
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(events).toContain("event: error"); expect(events).toContain('event: error');
expect(events).toContain( expect(events).toContain('data: {"statusCode":500,"error":"Internal Server Error"}');
'data: {"statusCode":500,"error":"Internal Server Error"}', expect(events).not.toContain('private storage failure');
);
expect(events).not.toContain("private storage failure");
}); });
it("sends a normal route as one SSE response event and then closes", async () => { it('sends a normal route as one SSE response event and then closes', async (): Promise<void> => {
const app = await createApp([ const app = await createApp([
{ {
url: "/echo", url: '/echo',
handler: (stream) => stream.send({ ok: true }), handler: (stream): Promise<void> => stream.send({ ok: true }),
}, },
]); ]);
const response = await app.request("/echo", { const response = await app.request('/echo', {
method: "POST", method: 'POST',
headers: { accept: "text/event-stream" }, headers: { accept: 'text/event-stream', ...mockAuthHeaders },
}); });
const events = await response.text(); const events = await response.text();
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(events).toContain("event: response"); expect(events).toContain('event: response');
expect(events).toContain('data: {"ok":true}'); expect(events).toContain('data: {"ok":true}');
}); });
it("keeps SSE open until the route's subscription promise resolves", async () => { it("keeps SSE open until the route's subscription promise resolves", async (): Promise<void> => {
let removeSubscription: () => Promise<void> = async () => undefined; let removeSubscription: () => Promise<void> = async () => undefined;
let markSubscribed: () => void = () => undefined; let markSubscribed: () => void = () => undefined;
const subscribed = new Promise<void>((resolve) => { const subscribed = new Promise<void>((resolve) => {
@@ -159,10 +177,10 @@ describe("HttpTransportRouter", () => {
}); });
const app = await createApp((broadcaster) => [ const app = await createApp((broadcaster) => [
{ {
url: "/items/subscribe", url: '/items/subscribe',
handler: async (stream) => { handler: async (stream): Promise<void> => {
const topics = ["items"]; const topics = [ 'items' ];
removeSubscription = () => broadcaster.unsubscribe(stream, topics); removeSubscription = (): Promise<void> => broadcaster.unsubscribe(stream, topics);
const removed = broadcaster.subscribe(stream, topics); const removed = broadcaster.subscribe(stream, topics);
markSubscribed(); markSubscribed();
@@ -171,9 +189,9 @@ describe("HttpTransportRouter", () => {
}, },
]); ]);
const response = await app.request("/items/subscribe", { const response = await app.request('/items/subscribe', {
method: "POST", method: 'POST',
headers: { accept: "text/event-stream" }, headers: { accept: 'text/event-stream', ...mockAuthHeaders },
}); });
const body = response.text(); const body = response.text();
const completed = vi.fn(); const completed = vi.fn();
@@ -185,58 +203,55 @@ describe("HttpTransportRouter", () => {
await removeSubscription(); await removeSubscription();
expect(await body).toBe(""); expect(await body).toBe('');
expect(completed).toHaveBeenCalledOnce(); expect(completed).toHaveBeenCalledOnce();
}); });
it("rejects unsubscribe over non-bidirectional SSE", async () => { it('rejects unsubscribe over non-bidirectional SSE', async (): Promise<void> => {
const app = await createApp((broadcaster) => [ const app = await createApp((broadcaster) => [
{ {
url: "/items/unsubscribe", url: '/items/unsubscribe',
handler: async (stream) => { handler: async (stream): Promise<void> => {
if (!stream.bidirectional) { if (!stream.bidirectional) {
throw new ApplicationError( throw new ApplicationError(400, 'This route requires an existing bidirectional stream');
400,
"This route requires an existing bidirectional stream",
);
} }
await broadcaster.unsubscribe(stream, ["items"]); await broadcaster.unsubscribe(stream, [ 'items' ]);
}, },
}, },
]); ]);
const response = await app.request("/items/unsubscribe", { const response = await app.request('/items/unsubscribe', {
method: "POST", method: 'POST',
headers: { accept: "text/event-stream" }, headers: { accept: 'text/event-stream', ...mockAuthHeaders },
}); });
const events = await response.text(); const events = await response.text();
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(events).toContain("event: error"); expect(events).toContain('event: error');
expect(events).toContain('"statusCode":400'); expect(events).toContain('"statusCode":400');
}); });
it("rejects HTTP bodies larger than the configured byte limit", async () => { it('rejects HTTP bodies larger than the configured byte limit', async (): Promise<void> => {
const app = await createApp( const app = await createApp(
[ [
{ {
url: "/echo", url: '/echo',
handler: async (stream) => stream.send(stream.body), handler: async (stream): Promise<void> => stream.send(stream.body),
}, },
], ],
32, 32,
); );
const response = await app.request("/echo", { const response = await app.request('/echo', {
method: "POST", method: 'POST',
headers: { "content-type": "application/json" }, headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: JSON.stringify({ value: "x".repeat(64) }), body: JSON.stringify({ value: 'x'.repeat(64) }),
}); });
expect(response.status).toBe(413); expect(response.status).toBe(413);
expect(await response.json()).toEqual({ expect(await response.json()).toEqual({
statusCode: 413, statusCode: 413,
error: "Request body exceeds the 32 byte limit", error: 'Request body exceeds the 32 byte limit',
}); });
}); });
}); });
+50 -40
View File
@@ -1,58 +1,68 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import { z } from "zod"; import { z } from 'zod';
import { WebSocketServer } from "ws";
import { ApplicationRouter } from "../../../source/services/router.js"; import { ApplicationRouter } from '../../../source/services/router.ts';
import { import { WsTransportRouter } from '../../../source/services/transport/ws-transport.ts';
WsTransportRouter, import { Logger } from '../../../source/utils/logger.ts';
} from "../../../source/services/transport/ws-transport.js"; import { toExtendedJson } from '@xo-cash/utils';
import { Logger } from "../../../source/utils/logger.js"; import { AuthSecp256k1 } from '../../../source/auth/auth.ts';
import { toExtendedJson } from "@xo-cash/utils"; import { toRoutes } from '../../helpers/misc.ts';
describe("WebSocket request decoding", () => { /**
it("decodes the minimal route-agnostic envelope and Extended JSON body", async () => { * A mock of the AuthSecp256k1 service
await expect( */
WsTransportRouter.decodeWebSocketRequest( const auth = {
toExtendedJson({ verifySignature: vi.fn().mockResolvedValue(true),
id: "request-1", verifyUniqueRequest: vi.fn().mockResolvedValue(true),
path: "/data/write", assertTimestampFreshness: vi.fn().mockResolvedValue(true),
body: { value: new Uint8Array([1, 2, 3]) }, } as unknown as AuthSecp256k1;
}),
), describe('WebSocket request decoding', (): void => {
).resolves.toEqual({ it('decodes the minimal route-agnostic envelope and Extended JSON body', async (): Promise<void> => {
requestId: "request-1", await expect(WsTransportRouter.decodeWebSocketRequest(toExtendedJson({
path: "/data/write", id: 'request-1',
body: { value: new Uint8Array([1, 2, 3]) }, path: '/data/write',
body: { value: new Uint8Array([ 1, 2, 3 ]) },
}))).resolves.toEqual({
requestId: 'request-1',
path: '/data/write',
body: { value: new Uint8Array([ 1, 2, 3 ]) },
}); });
}); });
it.each([ it.each([ '{}', '{"path":42}', '{"path":"/data/get","id":1}', '{"path":"/data/get","method":"POST"}' ])(
"{}", 'rejects an invalid envelope: %s',
'{"path":42}', async (payload) => {
'{"path":"/data/get","id":1}', await expect(WsTransportRouter.decodeWebSocketRequest(payload)).rejects.toBeInstanceOf(z.ZodError);
'{"path":"/data/get","method":"POST"}', },
])("rejects an invalid envelope: %s", async (payload) => {
await expect(WsTransportRouter.decodeWebSocketRequest(payload)).rejects.toBeInstanceOf(
z.ZodError,
); );
});
it("rejects malformed JSON", async () => { it('rejects malformed JSON', async (): Promise<void> => {
await expect(WsTransportRouter.decodeWebSocketRequest("{")).rejects.toMatchObject({ await expect(WsTransportRouter.decodeWebSocketRequest('{')).rejects.toMatchObject({
statusCode: 400, statusCode: 400,
message: "Invalid JSON in WebSocket message", message: 'Invalid JSON in WebSocket message',
}); });
}); });
}); });
describe("WsTransportRouter payload limits", () => { describe('WsTransportRouter payload limits', (): void => {
it("configures Hono's ws server with the requested maxPayload", async () => { it("configures Hono's ws server with the requested maxPayload", async () => {
const debug = new Logger("ws-transport-test"); const debug = new Logger('ws-transport-test');
const router = await ApplicationRouter.create([]); const router = await ApplicationRouter.create({
auth,
}, [
toRoutes([
{
url: '/data/write',
handler: async (stream): Promise<void> => {
await stream.send({});
},
},
]),
]);
const transport = new WsTransportRouter(router, debug, 1024); const transport = new WsTransportRouter(router, debug, 1024);
const wsServer = transport.websocketServer as unknown as WebSocketServer;
expect(wsServer.options.maxPayload).toBe(1024); expect(transport['wsServer'].options.maxPayload).toBe(1024);
await transport.stop(); await transport.stop();
}); });
}); });
+47 -46
View File
@@ -1,41 +1,44 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from 'vitest';
import type { RouteDefinition, RouteModule } from "../source/routes/types.js"; import { ApplicationRouter } from '../source/services/router.ts';
import { ApplicationRouter } from "../source/services/router.js"; import { Broadcaster } from '../source/services/broadcaster.ts';
import { Broadcaster } from "../source/services/broadcaster.js"; import { Logger } from '../source/utils/logger.ts';
import { Logger } from "../source/utils/logger.js"; import { TestConnection } from './helpers/test-connection.ts';
import { TestConnection } from "./helpers/test-connection.js";
function moduleWith(routes: RouteDefinition[]): RouteModule { import { createControlledRequest } from './helpers/controlled-request.ts';
return { import { createMockAuth, toRoutes } from './helpers/misc.ts';
async getRoutes() {
return routes; /**
* A mock of the AuthSecp256k1 service
*/
const auth = createMockAuth();
describe('long-lived subscription dispatch', (): void => {
it('allows duplicate subscribe and unsubscribe requests while the original request waits', async (): Promise<void> => {
const broadcaster = new Broadcaster(new Logger('subscription-flow-test'));
const router = await ApplicationRouter.create({
auth,
}, },
[
toRoutes([
{
url: '/items/subscribe',
handler: async (stream): Promise<void> => {
const { signalStarted, released } = stream.body as {
signalStarted: () => void;
released: Promise<void>;
}; };
}
async function expectPending(promise: Promise<void>): Promise<void> { signalStarted();
const settled = vi.fn();
void promise.then(settled);
await Promise.resolve();
expect(settled).not.toHaveBeenCalled();
}
describe("long-lived subscription dispatch", () => { await broadcaster.subscribe(stream, [ 'items' ]);
it("allows duplicate subscribe and unsubscribe requests while the original request waits", async () => { await released;
const broadcaster = new Broadcaster(new Logger("subscription-flow-test"));
const router = await ApplicationRouter.create([
moduleWith([
{
url: "/items/subscribe",
handler: async (stream) => {
await broadcaster.subscribe(stream, ["items"]);
}, },
}, },
{ {
url: "/items/unsubscribe", url: '/items/unsubscribe',
handler: async (stream) => { handler: async (stream): Promise<void> => {
await broadcaster.unsubscribe(stream, ["items"]); await broadcaster.unsubscribe(stream, [ 'items' ]);
await stream.send({}); await stream.send({});
}, },
}, },
@@ -43,31 +46,29 @@ describe("long-lived subscription dispatch", () => {
]); ]);
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const original = router.dispatch( const original = createControlledRequest({ router, connection, path: '/items/subscribe', requestId: 'subscribe-1' });
{ path: "/items/subscribe", requestId: "subscribe-1" },
connection,
);
await vi.waitFor(() => expect(connection.closeCallbacks).toHaveLength(1)); await vi.waitFor(() => expect(connection.closeCallbacks).toHaveLength(1));
await expectPending(original); await original.started;
// This request uses a different ApplicationRouteStream over the same // This request uses a different ApplicationRouteStream over the same
// connection. Since the topic already exists, its dispatch completes. // connection. Since the topic already exists, its dispatch completes.
await router.dispatch( const second = createControlledRequest({ router, connection, path: '/items/subscribe', requestId: 'subscribe-2' });
{ path: "/items/subscribe", requestId: "subscribe-2" }, second.release();
connection, await second.request;
);
await expectPending(original);
await router.dispatch( // Unsubscribe the original request stream
{ path: "/items/unsubscribe", requestId: "unsubscribe-1" }, const third = createControlledRequest({ router, connection, path: '/items/unsubscribe', requestId: 'unsubscribe-1' });
connection, third.release();
); await third.request;
await original;
// Release the original request stream
original.release();
await original.request;
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
{ {
id: "unsubscribe-1", id: 'unsubscribe-1',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: {}, body: {},
}, },
+3 -3
View File
@@ -2,8 +2,8 @@
"compilerOptions": { "compilerOptions": {
"rootDir": "./source", "rootDir": "./source",
"outDir": "./dist", "outDir": "./dist",
"module": "es2022", "module": "esnext",
"target": "es2022", "target": "esnext",
"skipLibCheck": true, "skipLibCheck": true,
"strict": true, "strict": true,
"moduleResolution": "bundler", "moduleResolution": "bundler",
@@ -15,5 +15,5 @@
"declarationMap": true, "declarationMap": true,
"types": ["node"] "types": ["node"]
}, },
"exclude": ["node_modules/**/*", "dist/**/*", "test"] "exclude": ["node_modules/**/*", "dist/**/*", "test/**/*"]
} }