Formatting

This commit is contained in:
2026-08-03 03:32:11 +00:00
parent 1ba075b5fa
commit a36e267280
6 changed files with 130 additions and 152 deletions
-3
View File
@@ -12,6 +12,3 @@ export const HTTP_STATUS_CODE_CREATED = 201;
* No content response status code. * No content response status code.
*/ */
export const HTTP_STATUS_CODE_NO_CONTENT = 204; export const HTTP_STATUS_CODE_NO_CONTENT = 204;
+3
View File
@@ -1,6 +1,7 @@
import type { BaseStream } from '../services/stream/base-stream.js'; import type { BaseStream } from '../services/stream/base-stream.js';
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,6 +16,7 @@ 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;
@@ -29,6 +31,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;
+1
View File
@@ -5,6 +5,7 @@ import type { BaseStream } from './stream/base-stream.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;
+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;
+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;
} }
+40 -63
View File
@@ -1,47 +1,37 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from 'vitest';
import type { RouteDefinition, RouteModule } from "../../source/routes/types.js"; import type { RouteDefinition, RouteModule } from '../../source/routes/types.ts';
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.ts';
import { TestConnection } from "../helpers/test-connection.js";
function moduleWith(routes: RouteDefinition[]): RouteModule { const moduleWith = (routes: RouteDefinition[]): RouteModule => {
return { return {
async getRoutes() { async getRoutes(): Promise<RouteDefinition[]> {
return routes; return routes;
}, },
}; };
} };
describe("ApplicationRouter initialization", () => { describe('ApplicationRouter initialization', (): void => {
it("rejects duplicate exact paths during startup", async () => { it('rejects duplicate exact paths during startup', async (): Promise<void> => {
const route = { url: "/echo", handler: () => undefined }; const route = { url: '/echo', handler: (): void => undefined };
await expect( await expect(ApplicationRouter.create([ moduleWith([ route ]), moduleWith([ route ]) ])).rejects.toThrow('Duplicate application route: /echo');
ApplicationRouter.create([moduleWith([route]), moduleWith([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([ moduleWith([{ 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([
moduleWith([ moduleWith([
{ {
url: "/echo", url: '/echo',
handler: async (stream) => { handler: async (stream): Promise<void> => {
expect(stream.connection).toBe(connection); expect(stream.connection).toBe(connection);
await stream.send(stream.body); await stream.send(stream.body);
}, },
@@ -49,32 +39,27 @@ describe("ApplicationRouter dispatch", () => {
]), ]),
]); ]);
await router.dispatch( await router.dispatch({ path: '/echo', body: { value: 1 }, requestId: 'request-1' }, connection);
{ path: "/echo", body: { value: 1 }, requestId: "request-1" },
connection,
);
expect(connection.messages).toEqual([ expect(connection.messages).toEqual([
{ {
id: "request-1", id: 'request-1',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { value: 1 }, body: { value: 1 },
}, },
]); ]);
await expect( await expect(router.dispatch({ path: '/echo/other', body: {} }, connection)).rejects.toMatchObject({ statusCode: 404 });
router.dispatch({ path: "/echo/other", body: {} }, 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 completions = new Map<string, () => void>();
const router = await ApplicationRouter.create([ const router = await ApplicationRouter.create([
moduleWith([ moduleWith([
{ {
url: "/delayed", url: '/delayed',
handler: async (stream) => { handler: async (stream): Promise<void> => {
const key = (stream.body as { key: string }).key; const key = (stream.body as { key: string }).key;
await new Promise<void>((resolve) => completions.set(key, resolve)); await new Promise<void>((resolve) => completions.set(key, resolve));
await stream.send({ key }); await stream.send({ key });
@@ -84,51 +69,43 @@ describe("ApplicationRouter dispatch", () => {
]); ]);
const connection = new TestConnection(true, true); const connection = new TestConnection(true, true);
const first = router.dispatch( const first = router.dispatch({ path: '/delayed', body: { key: 'A' }, requestId: 'A' }, connection);
{ path: "/delayed", body: { key: "A" }, requestId: "A" }, const second = router.dispatch({ path: '/delayed', body: { key: 'B' }, requestId: 'B' }, connection);
connection,
);
const second = router.dispatch(
{ path: "/delayed", body: { key: "B" }, requestId: "B" },
connection,
);
completions.get("B")?.(); completions.get('B')?.();
await second; await second;
completions.get("A")?.(); completions.get('A')?.();
await first; await first;
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", id: 'A',
type: "response", type: 'response',
statusCode: 200, statusCode: 200,
body: { key: "A" }, body: { key: 'A' },
}, },
]); ]);
}); });
it("propagates route failures without infrastructure-specific cleanup", async () => { it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
const error = new Error("route failed"); const error = new Error('route failed');
const router = await ApplicationRouter.create([ const router = await ApplicationRouter.create([
moduleWith([ moduleWith([
{ {
url: "/failure", url: '/failure',
handler: () => { handler: (): void => {
throw error; throw error;
}, },
}, },
]), ]),
]); ]);
await expect( await expect(router.dispatch({ path: '/failure' }, new TestConnection(false, false))).rejects.toBe(error);
router.dispatch({ path: "/failure" }, new TestConnection(false, false)),
).rejects.toBe(error);
}); });
}); });