@atproto/lex-server 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,9 +6,83 @@ const lex_data_1 = require("@atproto/lex-data");
6
6
  const lex_json_1 = require("@atproto/lex-json");
7
7
  const lex_schema_1 = require("@atproto/lex-schema");
8
8
  const drain_websocket_js_1 = require("./lib/drain-websocket.js");
9
+ /**
10
+ * XRPC router for handling AT Protocol Lexicon methods.
11
+ *
12
+ * The router handles HTTP routing, parameter parsing, input validation,
13
+ * authentication, and response serialization for XRPC methods. It supports
14
+ * queries (GET), procedures (POST), and subscriptions (WebSocket).
15
+ *
16
+ * @example Setting up a basic XRPC server
17
+ * ```typescript
18
+ * import { LexRouter } from '@atproto/lex-server'
19
+ * import { serve, upgradeWebSocket } from '@atproto/lex-server/nodejs'
20
+ * import { getProfile, createPost, subscribeRepos } from './lexicons'
21
+ *
22
+ * const router = new LexRouter({ upgradeWebSocket })
23
+ *
24
+ * // Register a query handler (GET request)
25
+ * router.add(getProfile, async (ctx) => {
26
+ * const profile = await db.getProfile(ctx.params.actor)
27
+ * return { body: profile }
28
+ * })
29
+ *
30
+ * // Register a procedure handler with authentication (POST request)
31
+ * router.add(createPost, {
32
+ * handler: async (ctx) => {
33
+ * const post = await db.createPost(ctx.credentials.did, ctx.input.body)
34
+ * return { body: { uri: post.uri, cid: post.cid } }
35
+ * },
36
+ * auth: async ({ request }) => {
37
+ * return verifyAccessToken(request)
38
+ * }
39
+ * })
40
+ *
41
+ * // Register a subscription handler (WebSocket)
42
+ * router.add(subscribeRepos, async function* (ctx) {
43
+ * for await (const event of eventStream.since(ctx.params.cursor)) {
44
+ * if (ctx.signal.aborted) break
45
+ * yield event
46
+ * }
47
+ * })
48
+ *
49
+ * // Start the server
50
+ * const server = await serve(router, { port: 3000 })
51
+ * console.log('XRPC server listening on port 3000')
52
+ * ```
53
+ *
54
+ * @example Using with service authentication
55
+ * ```typescript
56
+ * import { LexRouter, serviceAuth } from '@atproto/lex-server'
57
+ *
58
+ * const router = new LexRouter()
59
+ *
60
+ * const auth = serviceAuth({
61
+ * audience: 'did:web:api.example.com',
62
+ * unique: async (nonce) => {
63
+ * // Check and record nonce uniqueness
64
+ * return await nonceStore.checkAndAdd(nonce)
65
+ * }
66
+ * })
67
+ *
68
+ * router.add(protectedMethod, {
69
+ * handler: async (ctx) => {
70
+ * // ctx.credentials contains { did, didDocument, jwt }
71
+ * return { body: { callerDid: ctx.credentials.did } }
72
+ * },
73
+ * auth
74
+ * })
75
+ * ```
76
+ */
9
77
  class LexRouter {
10
78
  options;
79
+ /** Map of NSID strings to their fetch handlers. */
11
80
  handlers = new Map();
81
+ /**
82
+ * Creates a new XRPC router.
83
+ *
84
+ * @param options - Router configuration options
85
+ */
12
86
  constructor(options = {}) {
13
87
  this.options = options;
14
88
  }
@@ -195,6 +269,35 @@ class LexRouter {
195
269
  }
196
270
  return Response.json({ error: 'InternalError', message: 'An internal error occurred' }, { status: 500 });
197
271
  }
272
+ /**
273
+ * The main fetch handler for processing XRPC requests.
274
+ *
275
+ * Routes incoming requests to the appropriate method handler based on the
276
+ * NSID in the URL path. Returns appropriate error responses for invalid
277
+ * paths or unimplemented methods.
278
+ *
279
+ * This handler can be used directly with HTTP servers that support the
280
+ * fetch API pattern, or converted to a Node.js request listener using
281
+ * `toRequestListener()`.
282
+ *
283
+ * @param request - The incoming HTTP request
284
+ * @param connection - Optional connection metadata
285
+ * @returns A promise resolving to the HTTP response
286
+ *
287
+ * @example
288
+ * ```typescript
289
+ * // Use with Deno
290
+ * Deno.serve(router.fetch)
291
+ *
292
+ * // Use with Bun
293
+ * Bun.serve({ fetch: router.fetch })
294
+ *
295
+ * // Use with Node.js
296
+ * import { toRequestListener } from '@atproto/lex-server/nodejs'
297
+ * const listener = toRequestListener(router.fetch)
298
+ * http.createServer(listener).listen(3000)
299
+ * ```
300
+ */
198
301
  fetch = async (request, connection) => {
199
302
  const nsid = extractMethodN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AAa,CAAC,MAAmB,EAAE,KAAc;IACxD,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,MAAM,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IAC1D,OAAO,CACL,KAAK,KAAK,MAAM,CAAC,MAAM;QACvB,CAAC,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,MAAM,CAAC,MAAM,CAAC,CAC1D,CAAA;AACH,CAAC","sourcesContent":["import { encode } from '@atproto/lex-cbor'\nimport { LexError, LexValue, isPlainObject, ui8Concat } from '@atproto/lex-data'\nimport { lexParse, lexToJson } from '@atproto/lex-json'\nimport {\n InferMethodInput,\n InferMethodMessage,\n InferMethodOutput,\n InferMethodOutputBody,\n InferMethodOutputEncoding,\n InferMethodParams,\n Main,\n NsidString,\n Procedure,\n Query,\n Subscription,\n getMain,\n isNsidString,\n} from '@atproto/lex-schema'\nimport { drainWebsocket } from './lib/drain-websocket.js'\n\ntype Awaitable<T> = T | Promise<T>\nexport type LexMethod = Query | Procedure | Subscription\n\nexport type NetAddr = {\n hostname: string\n port: number\n transport: 'tcp' | 'udp'\n}\n\nexport type UnixAddr = {\n path: string\n transport: 'unix' | 'unixpacket'\n}\n\nexport type Addr = NetAddr | UnixAddr | undefined\n\nexport type ConnectionInfo<A extends Addr = Addr> = {\n remoteAddr: A\n completed: Promise<void>\n}\n\nexport type FetchHandler = (\n request: Request,\n connection?: ConnectionInfo,\n) => Promise<Response>\n\nexport type LexRouterHandlerContext<Method extends LexMethod, Credentials> = {\n credentials: Credentials\n input: InferMethodInput<Method, Body>\n params: InferMethodParams<Method>\n request: Request\n signal: AbortSignal\n connection?: ConnectionInfo\n}\n\ntype AsOptionalPayloadOptions<T> = T extends undefined | void\n ? { encoding?: undefined; body?: undefined }\n : T\n\nexport type LexRouterHandlerOutput<Method extends Query | Procedure> =\n | Response\n | ({\n headers?: HeadersInit\n } & (InferMethodOutputEncoding<Method> extends 'application/json'\n ? {\n // Allow omitting body when output is JSON\n encoding?: 'application/json'\n body: InferMethodOutputBody<Method>\n }\n : AsOptionalPayloadOptions<InferMethodOutput<Method, BodyInit>>))\n\nexport type LexRouterMethodHandler<\n Method extends Query | Procedure = Query | Procedure,\n Credentials = unknown,\n> = (\n ctx: LexRouterHandlerContext<Method, Credentials>,\n) => Awaitable<LexRouterHandlerOutput<Method>>\n\nexport type LexRouterMethodConfig<\n Method extends Query | Procedure = Query | Procedure,\n Credentials = unknown,\n> = {\n handler: LexRouterMethodHandler<Method, Credentials>\n auth: LexRouterAuth<Credentials, Method>\n}\n\nexport type LexRouterSubscriptionHandler<\n Method extends Subscription = Subscription,\n Credentials = unknown,\n> = (\n ctx: LexRouterHandlerContext<Method, Credentials>,\n) => AsyncIterable<InferMethodMessage<Method>>\n\nexport type LexRouterSubscriptionConfig<\n Method extends Subscription = Subscription,\n Credentials = unknown,\n> = {\n handler: LexRouterSubscriptionHandler<Method, Credentials>\n auth: LexRouterAuth<Credentials, Method>\n}\n\nexport type LexRouterAuthContext<Method extends LexMethod = LexMethod> = {\n method: Method\n params: InferMethodParams<Method>\n request: Request\n connection?: ConnectionInfo\n}\n\nexport type LexRouterAuth<\n Credentials = unknown,\n Method extends LexMethod = LexMethod,\n> = (ctx: LexRouterAuthContext<Method>) => Credentials | Promise<Credentials>\n\nexport type LexErrorHandlerContext = {\n error: unknown\n request: Request\n method: LexMethod\n}\n\nexport type UpgradeWebSocket = (request: Request) => {\n socket: WebSocket\n response: Response\n}\n\nexport type LexRouterOptions = {\n upgradeWebSocket?: UpgradeWebSocket\n onHandlerError?: (ctx: LexErrorHandlerContext) => void | Promise<void>\n highWaterMark?: number\n lowWaterMark?: number\n}\n\nexport class LexRouter {\n private handlers: Map<NsidString, FetchHandler> = new Map()\n\n constructor(readonly options: LexRouterOptions = {}) {}\n\n add<M extends Subscription>(\n ns: Main<M>,\n handler: LexRouterSubscriptionHandler<M, void>,\n ): this\n add<M extends Subscription, Credentials>(\n ns: Main<M>,\n config: LexRouterSubscriptionConfig<M, Credentials>,\n ): this\n add<M extends Query | Procedure>(\n ns: Main<M>,\n handler: LexRouterMethodHandler<M, void>,\n ): this\n add<M extends Query | Procedure, Credentials>(\n ns: Main<M>,\n config: LexRouterMethodConfig<M, Credentials>,\n ): this\n add<M extends LexMethod, Credentials = unknown>(\n ns: Main<M>,\n config: M extends Subscription\n ?\n | LexRouterSubscriptionHandler<M, Credentials>\n | LexRouterSubscriptionConfig<M, Credentials>\n : M extends Query | Procedure\n ?\n | LexRouterMethodHandler<M, Credentials>\n | LexRouterMethodConfig<M, Credentials>\n : never,\n ): this\n add<M extends LexMethod>(\n ns: Main<M>,\n config:\n | LexRouterSubscriptionHandler<any, any>\n | LexRouterSubscriptionConfig<any, any>\n | LexRouterMethodHandler<any, any>\n | LexRouterMethodConfig<any, any>,\n ) {\n const method = getMain(ns)\n if (this.handlers.has(method.nsid)) {\n throw new TypeError(`Method ${method.nsid} already registered`)\n }\n const methodConfig =\n typeof config === 'function'\n ? { handler: config, auth: undefined }\n : config\n\n const fetch: FetchHandler =\n method.type === 'subscription'\n ? this.buildSubscriptionHandler(\n method,\n methodConfig.handler as LexRouterSubscriptionHandler<any, any>,\n methodConfig.auth,\n )\n : this.buildMethodHandler(\n method,\n methodConfig.handler as LexRouterMethodHandler<any, any>,\n methodConfig.auth,\n )\n\n this.handlers.set(method.nsid, fetch)\n\n return this\n }\n\n private buildMethodHandler<Method extends Query | Procedure, Credentials>(\n method: Method,\n methodHandler: LexRouterMethodHandler<Method, Credentials>,\n auth?: LexRouterAuth<Credentials, Method>,\n ): FetchHandler {\n const getInput = (\n method.type === 'procedure'\n ? getProcedureInput.bind(method)\n : getQueryInput.bind(method)\n ) as (request: Request) => Promise<InferMethodInput<Method, Body>>\n\n return async (request, connection) => {\n // @NOTE CORS requests should be handled by a middleware before reaching\n // this point.\n if (\n (method.type === 'procedure' && request.method !== 'POST') ||\n (method.type === 'query' &&\n request.method !== 'GET' &&\n request.method !== 'HEAD')\n ) {\n return Response.json(\n { error: 'InvalidRequest', message: 'Method not allowed' },\n { status: 405 },\n )\n }\n\n try {\n const url = new URL(request.url)\n const params = method.parameters.fromURLSearchParams(url.searchParams)\n\n const credentials = auth\n ? await auth({ method, params, request, connection })\n : (undefined as Credentials)\n\n const input = await getInput(request)\n\n const output = await methodHandler({\n credentials,\n params,\n input,\n request,\n connection,\n signal: request.signal,\n })\n\n if (output instanceof Response) {\n return output\n }\n\n // @TODO add validation of output based on method.output.schema?\n\n if (output.body === undefined && output.encoding === undefined) {\n return new Response(null, { status: 200, headers: output.headers })\n }\n\n if (method.output?.encoding === 'application/json') {\n return Response.json(lexToJson(output.body as LexValue), {\n status: 200,\n headers: output.headers,\n })\n }\n\n const headers = new Headers(output.headers)\n headers.set('content-type', output.encoding!)\n return new Response(output.body as BodyInit | null | undefined, {\n status: 200,\n headers,\n })\n } catch (error) {\n return this.handleError(request, method, error)\n }\n }\n }\n\n private buildSubscriptionHandler<Method extends Subscription, Credentials>(\n method: Method,\n methodHandler: LexRouterSubscriptionHandler<Method, Credentials>,\n auth?: LexRouterAuth<Credentials, Method>,\n ): FetchHandler {\n const {\n onHandlerError,\n upgradeWebSocket = (globalThis as any).Deno?.upgradeWebSocket as\n | UpgradeWebSocket\n | undefined,\n } = this.options\n if (!upgradeWebSocket) {\n throw new TypeError(\n 'WebSocket upgrade not supported in this environment. Please provide an upgradeWebSocket option when creating the LexRouter.',\n )\n }\n\n return async (request, connection) => {\n if (request.method !== 'GET') {\n return Response.json(\n { error: 'InvalidRequest', message: 'Method not allowed' },\n { status: 405 },\n )\n }\n\n if (\n request.headers.get('connection')?.toLowerCase() !== 'upgrade' ||\n request.headers.get('upgrade')?.toLowerCase() !== 'websocket'\n ) {\n return Response.json(\n {\n error: 'InvalidRequest',\n message: 'XRPC subscriptions are only available over WebSocket',\n },\n {\n status: 426,\n headers: {\n Connection: 'Upgrade',\n Upgrade: 'websocket',\n },\n },\n )\n }\n\n if (request.signal.aborted) {\n return Response.json(\n { error: 'RequestAborted', message: 'The request was aborted' },\n { status: 499 },\n )\n }\n\n try {\n const { response, socket } = upgradeWebSocket(request)\n\n // @NOTE We are using a distinct signal than request.signal because that\n // signal may get aborted before the WebSocket is closed (this is the\n // case with Deno).\n const abortController = new AbortController()\n const { signal } = abortController\n const abort = () => abortController.abort()\n\n const onMessage = (event: unknown) => {\n const error = new LexError(\n 'InvalidRequest',\n 'XRPC subscriptions do not accept messages',\n { cause: event },\n )\n socket.send(encodeErrorFrame(error))\n socket.close(1008, error.error)\n }\n\n const onOpen = async () => {\n try {\n const url = new URL(request.url)\n const params = method.parameters.fromURLSearchParams(\n url.searchParams,\n )\n\n const credentials: Credentials = auth\n ? await auth({ method, params, request, connection })\n : (undefined as Credentials)\n\n signal.throwIfAborted()\n\n const iterable = methodHandler({\n credentials,\n params,\n input: undefined as InferMethodInput<Method, Body>,\n request,\n connection,\n signal,\n })\n\n const iterator = iterable[Symbol.asyncIterator]()\n\n signal.addEventListener('abort', async () => {\n // @NOTE will cause the process to crash if this throws\n await iterator.return?.()\n })\n\n while (!signal.aborted && socket.readyState === 1) {\n const result = await iterator.next()\n if (result.done) break\n\n // @TODO add validation of output based on method.output.schema?\n\n const data = encodeMessageFrame(method, result.value)\n\n socket.send(data)\n\n // Apply backpressure by waiting for the buffered data to drain\n // before generating the next message\n await drainWebsocket(socket, signal, this.options)\n }\n\n if (socket.readyState === 1) {\n socket.close(1000)\n }\n } catch (error) {\n // If the socket is still open, send an error frame before closing\n if (socket.readyState === 1) {\n const lexError =\n error instanceof LexError\n ? error\n : new LexError('InternalError', 'An internal error occurred')\n\n socket.send(encodeErrorFrame(lexError))\n\n socket.close(\n // https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1\n error instanceof LexError ? 1008 : 1011,\n lexError.error,\n )\n }\n\n // Only report unexpected processing errors\n if (onHandlerError && !isAbortReason(request.signal, error)) {\n await onHandlerError({ error, request, method })\n }\n } finally {\n abortController.abort()\n }\n }\n\n socket.addEventListener('error', abort)\n socket.addEventListener('close', abort)\n socket.addEventListener('open', onOpen)\n socket.addEventListener('message', onMessage)\n\n return response\n } catch (error) {\n return this.handleError(request, method, error)\n }\n }\n }\n\n private async handleError(\n request: Request,\n method: LexMethod,\n error: unknown,\n ) {\n // Only report unexpected processing errors\n const { onHandlerError } = this.options\n if (onHandlerError && !isAbortReason(request.signal, error)) {\n await onHandlerError({ error, request, method })\n }\n\n if (error instanceof LexError) {\n return error.toResponse()\n }\n\n return Response.json(\n { error: 'InternalError', message: 'An internal error occurred' },\n { status: 500 },\n )\n }\n\n fetch: FetchHandler = async (\n request: Request,\n connection?: ConnectionInfo,\n ): Promise<Response> => {\n const nsid = extractMethodNsid(request)\n\n const fetch = nsid\n ? (this.handlers as Map<unknown, FetchHandler>).get(nsid)\n : undefined\n if (fetch) return fetch(request, connection)\n\n if (!nsid || !isNsidString(nsid)) {\n return Response.json(\n {\n error: 'InvalidRequest',\n message: 'Invalid XRPC method path',\n },\n { status: 404 },\n )\n }\n\n return Response.json(\n {\n error: 'MethodNotImplemented',\n message: `XRPC method \"${nsid}\" not implemented on this server`,\n },\n { status: 501 },\n )\n }\n}\n\nfunction extractMethodNsid(request: Request): string | null {\n const { pathname } = new URL(request.url)\n if (!pathname.startsWith('/xrpc/')) return null\n if (pathname.includes('/', 6)) return null\n // We don't really need to validate the NSID here, the existence of the route\n // (which is looked up based on an NSID) is sufficient.\n return pathname.slice(6)\n}\n\nasync function getProcedureInput<M extends Procedure>(\n this: M,\n request: Request,\n): Promise<InferMethodInput<M, Body>> {\n const encodingRaw = request.headers\n .get('content-type')\n ?.split(';')[0]\n .trim()\n .toLowerCase()\n\n const encoding =\n encodingRaw ||\n // If the caller did not provide a content-type, but the method\n // expects an input, assume binary\n (request.body != null && this.input.encoding != null\n ? 'application/octet-stream'\n : undefined)\n\n if (!this.input.matchesEncoding(encoding)) {\n throw new LexError('InvalidRequest', `Invalid content-type: ${encoding}`)\n }\n\n if (this.input.encoding === 'application/json') {\n // @TODO limit size?\n const data = lexParse(await request.text())\n const body = this.input.schema ? this.input.schema.parse(data) : data\n return { encoding, body } as InferMethodInput<M, Body>\n } else if (this.input.encoding) {\n const body: Body = request\n return { encoding, body } as InferMethodInput<M, Body>\n } else {\n return undefined as InferMethodInput<M, Body>\n }\n}\n\nasync function getQueryInput<M extends Query>(\n this: M,\n request: Request,\n): Promise<InferMethodInput<M, Body>> {\n if (\n request.body ||\n request.headers.has('content-type') ||\n request.headers.has('content-length')\n ) {\n throw new LexError('InvalidRequest', 'GET requests must not have a body')\n }\n\n return undefined as InferMethodInput<M, Body>\n}\n\n// Pre-encoded frame header for error frames\nconst ERROR_FRAME_HEADER = /*#__PURE__*/ encode({ op: -1 })\n\nfunction encodeErrorFrame(error: LexError): Uint8Array {\n return ui8Concat([ERROR_FRAME_HEADER, encode(error.toJSON())])\n}\n\n// Pre-encoded frame header for message frames with unknown type\nconst UNKNOWN_MESSAGE_FRAME_HEADER = /*#__PURE__*/ encode({ op: 1 })\n\nfunction encodeMessageFrame(method: Subscription, value: LexValue): Uint8Array {\n if (isPlainObject(value) && typeof value.$type === 'string') {\n const { $type, ...rest } = value\n return ui8Concat([\n encode({\n op: 1,\n t:\n // If $type starts with `nsid#`, strip the NSID prefix\n $type.charCodeAt(0) !== 0x23 && // '#'\n $type.charCodeAt(method.nsid.length) === 0x23 && // '#'\n $type.startsWith(method.nsid)\n ? $type.slice(method.nsid.length)\n : $type,\n }),\n encode(rest),\n ])\n }\n\n return ui8Concat([UNKNOWN_MESSAGE_FRAME_HEADER, encode(value)])\n}\n\nfunction isAbortReason(signal: AbortSignal, error: unknown): boolean {\n if (!signal.aborted || signal.reason == null) return false\n return (\n error === signal.reason ||\n (error instanceof Error && error.cause === signal.reason)\n )\n}\n"]}
1
+ {"version":3,"file":"lex-server.js","sourceRoot":"","sources":["../src/lex-server.ts"],"names":[],"mappings":";;;AAAA,gDAA0C;AAC1C,gDAAgF;AAChF,gDAAuD;AACvD,oDAc4B;AAC5B,iEAAyD;AA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E,KAAc;IACxD,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,MAAM,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IAC1D,OAAO,CACL,KAAK,KAAK,MAAM,CAAC,MAAM;QACvB,CAAC,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,MAAM,CAAC,MAAM,CAAC,CAC1D,CAAA;AACH,CAAC","sourcesContent":["import { encode } from '@atproto/lex-cbor'\nimport { LexError, LexValue, isPlainObject, ui8Concat } from '@atproto/lex-data'\nimport { lexParse, lexToJson } from '@atproto/lex-json'\nimport {\n InferMethodInput,\n InferMethodMessage,\n InferMethodOutput,\n InferMethodOutputBody,\n InferMethodOutputEncoding,\n InferMethodParams,\n Main,\n NsidString,\n Procedure,\n Query,\n Subscription,\n getMain,\n isNsidString,\n} from '@atproto/lex-schema'\nimport { drainWebsocket } from './lib/drain-websocket.js'\n\ntype Awaitable<T> = T | Promise<T>\n\n/**\n * Union type representing the supported Lexicon method types.\n *\n * - `Query`: Read-only methods invoked via HTTP GET\n * - `Procedure`: Methods that may modify state, invoked via HTTP POST\n * - `Subscription`: Real-time streaming methods over WebSocket\n */\nexport type LexMethod = Query | Procedure | Subscription\n\n/**\n * Network address for TCP or UDP connections.\n *\n * @example\n * ```typescript\n * const addr: NetAddr = {\n * hostname: '127.0.0.1',\n * port: 3000,\n * transport: 'tcp'\n * }\n * ```\n */\nexport type NetAddr = {\n /** The hostname or IP address of the connection. */\n hostname: string\n /** The port number of the connection. */\n port: number\n /** The transport protocol used. */\n transport: 'tcp' | 'udp'\n}\n\n/**\n * Unix domain socket address.\n *\n * @example\n * ```typescript\n * const addr: UnixAddr = {\n * path: '/var/run/app.sock',\n * transport: 'unix'\n * }\n * ```\n */\nexport type UnixAddr = {\n /** The filesystem path to the Unix socket. */\n path: string\n /** The transport protocol used. */\n transport: 'unix' | 'unixpacket'\n}\n\n/**\n * Union type for all supported address types.\n *\n * Can be a network address ({@link NetAddr}), Unix socket address ({@link UnixAddr}),\n * or `undefined` when the address is not available.\n */\nexport type Addr = NetAddr | UnixAddr | undefined\n\n/**\n * Metadata about the client connection for an incoming request.\n *\n * @typeParam A - The address type, defaults to {@link Addr}\n *\n * @example\n * ```typescript\n * const info: ConnectionInfo<NetAddr> = {\n * remoteAddr: { hostname: '192.168.1.1', port: 54321, transport: 'tcp' },\n * completed: new Promise((resolve) => socket.on('close', resolve))\n * }\n * ```\n */\nexport type ConnectionInfo<A extends Addr = Addr> = {\n /** The remote address of the client, if available. */\n remoteAddr: A\n /** Promise that resolves when the connection is fully closed. */\n completed: Promise<void>\n}\n\n/**\n * Function signature for handling HTTP requests in the XRPC router.\n *\n * This is the standard fetch-style handler that processes incoming requests\n * and returns responses. It is used both internally by the router and can\n * be used to integrate with other HTTP frameworks.\n *\n * @param request - The incoming HTTP request\n * @param connection - Optional connection metadata including remote address\n * @returns A promise resolving to the HTTP response\n *\n * @example\n * ```typescript\n * const handler: FetchHandler = async (request, connection) => {\n * console.log('Request from:', connection?.remoteAddr)\n * return new Response('Hello, World!')\n * }\n * ```\n */\nexport type FetchHandler = (\n request: Request,\n connection?: ConnectionInfo,\n) => Promise<Response>\n\n/**\n * Context object passed to XRPC method handlers.\n *\n * Contains all the information needed to process a request, including\n * parsed parameters, authentication credentials, and the raw request object.\n *\n * @typeParam Method - The Lexicon method type (Query, Procedure, or Subscription)\n * @typeParam Credentials - The type of authentication credentials, determined by the auth handler\n *\n * @example\n * ```typescript\n * const handler: LexRouterMethodHandler<MyMethod, UserCredentials> = async (ctx) => {\n * const { credentials, params, input, signal } = ctx\n * // credentials.userId is available if auth handler returns UserCredentials\n * // params contains validated query parameters\n * // input contains the request body (for procedures)\n * // signal can be used to abort long-running operations\n * return { body: { result: 'success' } }\n * }\n * ```\n */\nexport type LexRouterHandlerContext<Method extends LexMethod, Credentials> = {\n /** Authentication credentials returned by the auth handler. */\n credentials: Credentials\n /** Parsed and validated request input (body for procedures, undefined for queries). */\n input: InferMethodInput<Method, Body>\n /** Parsed and validated URL query parameters. */\n params: InferMethodParams<Method>\n /** The original HTTP request object. */\n request: Request\n /** Abort signal that is triggered when the request is cancelled. */\n signal: AbortSignal\n /** Connection metadata including remote address. */\n connection?: ConnectionInfo\n}\n\ntype AsOptionalPayloadOptions<T> = T extends undefined | void\n ? { encoding?: undefined; body?: undefined }\n : T\n\n/**\n * Return type for XRPC method handlers (queries and procedures).\n *\n * Handlers can return either:\n * - A raw {@link Response} object for full control over the HTTP response\n * - An object with `body`, optional `encoding`, and optional `headers`\n *\n * For JSON methods, the body is automatically serialized. For other encodings,\n * the body must be a valid {@link BodyInit} type.\n *\n * @typeParam Method - The Lexicon method type (Query or Procedure)\n *\n * @example\n * ```typescript\n * // Return JSON body (most common)\n * return { body: { users: [...] } }\n *\n * // Return with custom headers\n * return {\n * body: { data: 'value' },\n * headers: { 'Cache-Control': 'max-age=3600' }\n * }\n *\n * // Return raw Response for full control\n * return new Response(binaryData, {\n * headers: { 'Content-Type': 'application/octet-stream' }\n * })\n * ```\n */\nexport type LexRouterHandlerOutput<Method extends Query | Procedure> =\n | Response\n | ({\n headers?: HeadersInit\n } & (InferMethodOutputEncoding<Method> extends 'application/json'\n ? {\n // Allow omitting body when output is JSON\n encoding?: 'application/json'\n body: InferMethodOutputBody<Method>\n }\n : AsOptionalPayloadOptions<InferMethodOutput<Method, BodyInit>>))\n\n/**\n * Handler function for XRPC query and procedure methods.\n *\n * Receives a context object with request details and credentials,\n * and returns either a Response or a structured output object.\n *\n * @typeParam Method - The Lexicon method type (Query or Procedure)\n * @typeParam Credentials - The type of authentication credentials\n *\n * @example\n * ```typescript\n * const getProfile: LexRouterMethodHandler<GetProfileMethod, UserCredentials> = async (ctx) => {\n * const profile = await db.getProfile(ctx.params.actor)\n * return { body: profile }\n * }\n * ```\n */\nexport type LexRouterMethodHandler<\n Method extends Query | Procedure = Query | Procedure,\n Credentials = unknown,\n> = (\n ctx: LexRouterHandlerContext<Method, Credentials>,\n) => Awaitable<LexRouterHandlerOutput<Method>>\n\n/**\n * Configuration object for registering an XRPC method with authentication.\n *\n * Used when you need to specify both a handler and an auth function.\n *\n * @typeParam Method - The Lexicon method type (Query or Procedure)\n * @typeParam Credentials - The type of authentication credentials\n *\n * @example\n * ```typescript\n * const config: LexRouterMethodConfig<GetProfileMethod, UserCredentials> = {\n * handler: async (ctx) => {\n * return { body: await getProfile(ctx.params.actor) }\n * },\n * auth: async ({ request }) => {\n * return verifyToken(request.headers.get('authorization'))\n * }\n * }\n * ```\n */\nexport type LexRouterMethodConfig<\n Method extends Query | Procedure = Query | Procedure,\n Credentials = unknown,\n> = {\n /** The handler function that processes the request. */\n handler: LexRouterMethodHandler<Method, Credentials>\n /** Authentication function that validates credentials before the handler runs. */\n auth: LexRouterAuth<Credentials, Method>\n}\n\n/**\n * Handler function for XRPC subscription methods (WebSocket streams).\n *\n * Returns an async iterable that yields messages to be sent over the WebSocket.\n * The connection remains open until the iterable completes or an error occurs.\n *\n * @typeParam Method - The Lexicon subscription method type\n * @typeParam Credentials - The type of authentication credentials\n *\n * @example\n * ```typescript\n * const subscribeRepos: LexRouterSubscriptionHandler<SubscribeReposMethod> = async function* (ctx) {\n * const cursor = ctx.params.cursor ?? 0\n * for await (const event of eventStream.since(cursor)) {\n * if (ctx.signal.aborted) break\n * yield { $type: 'com.atproto.sync.subscribeRepos#commit', ...event }\n * }\n * }\n * ```\n */\nexport type LexRouterSubscriptionHandler<\n Method extends Subscription = Subscription,\n Credentials = unknown,\n> = (\n ctx: LexRouterHandlerContext<Method, Credentials>,\n) => AsyncIterable<InferMethodMessage<Method>>\n\n/**\n * Configuration object for registering an XRPC subscription with authentication.\n *\n * Used when you need to specify both a handler and an auth function for subscriptions.\n *\n * @typeParam Method - The Lexicon subscription method type\n * @typeParam Credentials - The type of authentication credentials\n *\n * @example\n * ```typescript\n * const config: LexRouterSubscriptionConfig<SubscribeReposMethod, ServiceCredentials> = {\n * handler: async function* (ctx) {\n * for await (const event of eventStream) {\n * yield event\n * }\n * },\n * auth: async ({ request }) => {\n * return verifyServiceAuth(request)\n * }\n * }\n * ```\n */\nexport type LexRouterSubscriptionConfig<\n Method extends Subscription = Subscription,\n Credentials = unknown,\n> = {\n /** The handler function that yields subscription messages. */\n handler: LexRouterSubscriptionHandler<Method, Credentials>\n /** Authentication function that validates credentials before the handler runs. */\n auth: LexRouterAuth<Credentials, Method>\n}\n\n/**\n * Context object passed to authentication handlers.\n *\n * Contains the information needed to authenticate a request before\n * the main handler is invoked.\n *\n * @typeParam Method - The Lexicon method type\n *\n * @example\n * ```typescript\n * const authHandler: LexRouterAuth<UserCredentials> = async (ctx) => {\n * const token = ctx.request.headers.get('authorization')\n * if (!token) throw new LexError('AuthenticationRequired', 'Missing token')\n * return { userId: await verifyToken(token) }\n * }\n * ```\n */\nexport type LexRouterAuthContext<Method extends LexMethod = LexMethod> = {\n /** The Lexicon method definition being called. */\n method: Method\n /** Parsed and validated URL query parameters. */\n params: InferMethodParams<Method>\n /** The original HTTP request object. */\n request: Request\n /** Connection metadata including remote address. */\n connection?: ConnectionInfo\n}\n\n/**\n * Authentication handler function for XRPC methods.\n *\n * Called before the main handler to validate authentication credentials.\n * Should return the validated credentials or throw an error if authentication fails.\n *\n * @typeParam Credentials - The type of credentials to return on success\n * @typeParam Method - The Lexicon method type\n *\n * @example\n * ```typescript\n * // Simple token-based auth\n * const tokenAuth: LexRouterAuth<{ userId: string }> = async ({ request }) => {\n * const token = request.headers.get('authorization')?.replace('Bearer ', '')\n * if (!token) throw new LexError('AuthenticationRequired', 'Token required')\n * const userId = await verifyToken(token)\n * return { userId }\n * }\n *\n * // Using with serviceAuth for AT Protocol service authentication\n * import { serviceAuth } from '@atproto/lex-server'\n * const auth = serviceAuth({ audience: 'did:web:example.com', unique: checkNonce })\n * ```\n */\nexport type LexRouterAuth<\n Credentials = unknown,\n Method extends LexMethod = LexMethod,\n> = (ctx: LexRouterAuthContext<Method>) => Credentials | Promise<Credentials>\n\n/**\n * Context object passed to error handler callbacks.\n *\n * Used for logging and monitoring errors that occur during request handling.\n */\nexport type LexErrorHandlerContext = {\n /** The error that was thrown during handling. */\n error: unknown\n /** The original HTTP request that triggered the error. */\n request: Request\n /** The Lexicon method that was being executed. */\n method: LexMethod\n}\n\n/**\n * Function that upgrades an HTTP request to a WebSocket connection.\n *\n * This is platform-specific: Deno provides this natively, while Node.js\n * requires the `upgradeWebSocket` function from this package.\n *\n * @param request - The HTTP request to upgrade\n * @returns An object containing the WebSocket and the upgrade response\n *\n * @example\n * ```typescript\n * // In Node.js, use the provided upgradeWebSocket function\n * import { upgradeWebSocket } from '@atproto/lex-server/nodejs'\n *\n * const router = new LexRouter({ upgradeWebSocket })\n * ```\n */\nexport type UpgradeWebSocket = (request: Request) => {\n /** The WebSocket instance for bidirectional communication. */\n socket: WebSocket\n /** The HTTP response to return (101 Switching Protocols). */\n response: Response\n}\n\n/**\n * Configuration options for the {@link LexRouter}.\n *\n * @example\n * ```typescript\n * const options: LexRouterOptions = {\n * upgradeWebSocket,\n * onHandlerError: async ({ error, request, method }) => {\n * console.error(`Error in ${method.nsid}:`, error)\n * await reportToSentry(error)\n * },\n * highWaterMark: 64 * 1024, // 64KB\n * lowWaterMark: 16 * 1024 // 16KB\n * }\n * ```\n */\nexport type LexRouterOptions = {\n /**\n * Function to upgrade HTTP requests to WebSocket connections.\n * Required for subscription methods. Defaults to Deno's built-in\n * upgradeWebSocket if available.\n */\n upgradeWebSocket?: UpgradeWebSocket\n /**\n * Callback invoked when an error occurs during request handling.\n * Useful for logging and error reporting. Not called for client-induced\n * errors (e.g., request abortion).\n */\n onHandlerError?: (ctx: LexErrorHandlerContext) => void | Promise<void>\n /**\n * High water mark for WebSocket backpressure (in bytes).\n * When buffered data exceeds this, the handler will wait before sending more.\n */\n highWaterMark?: number\n /**\n * Low water mark for WebSocket backpressure (in bytes).\n * The handler resumes sending when buffered data drops below this.\n */\n lowWaterMark?: number\n}\n\n/**\n * XRPC router for handling AT Protocol Lexicon methods.\n *\n * The router handles HTTP routing, parameter parsing, input validation,\n * authentication, and response serialization for XRPC methods. It supports\n * queries (GET), procedures (POST), and subscriptions (WebSocket).\n *\n * @example Setting up a basic XRPC server\n * ```typescript\n * import { LexRouter } from '@atproto/lex-server'\n * import { serve, upgradeWebSocket } from '@atproto/lex-server/nodejs'\n * import { getProfile, createPost, subscribeRepos } from './lexicons'\n *\n * const router = new LexRouter({ upgradeWebSocket })\n *\n * // Register a query handler (GET request)\n * router.add(getProfile, async (ctx) => {\n * const profile = await db.getProfile(ctx.params.actor)\n * return { body: profile }\n * })\n *\n * // Register a procedure handler with authentication (POST request)\n * router.add(createPost, {\n * handler: async (ctx) => {\n * const post = await db.createPost(ctx.credentials.did, ctx.input.body)\n * return { body: { uri: post.uri, cid: post.cid } }\n * },\n * auth: async ({ request }) => {\n * return verifyAccessToken(request)\n * }\n * })\n *\n * // Register a subscription handler (WebSocket)\n * router.add(subscribeRepos, async function* (ctx) {\n * for await (const event of eventStream.since(ctx.params.cursor)) {\n * if (ctx.signal.aborted) break\n * yield event\n * }\n * })\n *\n * // Start the server\n * const server = await serve(router, { port: 3000 })\n * console.log('XRPC server listening on port 3000')\n * ```\n *\n * @example Using with service authentication\n * ```typescript\n * import { LexRouter, serviceAuth } from '@atproto/lex-server'\n *\n * const router = new LexRouter()\n *\n * const auth = serviceAuth({\n * audience: 'did:web:api.example.com',\n * unique: async (nonce) => {\n * // Check and record nonce uniqueness\n * return await nonceStore.checkAndAdd(nonce)\n * }\n * })\n *\n * router.add(protectedMethod, {\n * handler: async (ctx) => {\n * // ctx.credentials contains { did, didDocument, jwt }\n * return { body: { callerDid: ctx.credentials.did } }\n * },\n * auth\n * })\n * ```\n */\nexport class LexRouter {\n /** Map of NSID strings to their fetch handlers. */\n readonly handlers: Map<NsidString, FetchHandler> = new Map()\n\n /**\n * Creates a new XRPC router.\n *\n * @param options - Router configuration options\n */\n constructor(readonly options: LexRouterOptions = {}) {}\n\n /**\n * Registers a subscription handler without authentication.\n *\n * @param ns - The Lexicon namespace definition for the subscription\n * @param handler - Async generator function that yields subscription messages\n * @returns This router instance for chaining\n */\n add<M extends Subscription>(\n ns: Main<M>,\n handler: LexRouterSubscriptionHandler<M, void>,\n ): this\n /**\n * Registers a subscription handler with authentication.\n *\n * @param ns - The Lexicon namespace definition for the subscription\n * @param config - Configuration object with handler and auth function\n * @returns This router instance for chaining\n */\n add<M extends Subscription, Credentials>(\n ns: Main<M>,\n config: LexRouterSubscriptionConfig<M, Credentials>,\n ): this\n /**\n * Registers a query or procedure handler without authentication.\n *\n * @param ns - The Lexicon namespace definition for the method\n * @param handler - Handler function that processes requests\n * @returns This router instance for chaining\n */\n add<M extends Query | Procedure>(\n ns: Main<M>,\n handler: LexRouterMethodHandler<M, void>,\n ): this\n /**\n * Registers a query or procedure handler with authentication.\n *\n * @param ns - The Lexicon namespace definition for the method\n * @param config - Configuration object with handler and auth function\n * @returns This router instance for chaining\n */\n add<M extends Query | Procedure, Credentials>(\n ns: Main<M>,\n config: LexRouterMethodConfig<M, Credentials>,\n ): this\n /**\n * Registers a Lexicon method handler.\n *\n * This is the unified overload that accepts any method type with optional authentication.\n *\n * @param ns - The Lexicon namespace definition\n * @param config - Handler function or configuration object\n * @returns This router instance for chaining\n *\n * @throws {TypeError} If a method with the same NSID is already registered\n *\n * @example\n * ```typescript\n * // Register without auth (credentials will be void)\n * router.add(myQuery, async (ctx) => {\n * return { body: { data: 'value' } }\n * })\n *\n * // Register with auth\n * router.add(myProcedure, {\n * handler: async (ctx) => {\n * console.log('Caller:', ctx.credentials.userId)\n * return { body: { success: true } }\n * },\n * auth: async ({ request }) => ({ userId: await verifyToken(request) })\n * })\n * ```\n */\n add<M extends LexMethod, Credentials = unknown>(\n ns: Main<M>,\n config: M extends Subscription\n ?\n | LexRouterSubscriptionHandler<M, Credentials>\n | LexRouterSubscriptionConfig<M, Credentials>\n : M extends Query | Procedure\n ?\n | LexRouterMethodHandler<M, Credentials>\n | LexRouterMethodConfig<M, Credentials>\n : never,\n ): this\n add<M extends LexMethod>(\n ns: Main<M>,\n config:\n | LexRouterSubscriptionHandler<any, any>\n | LexRouterSubscriptionConfig<any, any>\n | LexRouterMethodHandler<any, any>\n | LexRouterMethodConfig<any, any>,\n ) {\n const method = getMain(ns)\n if (this.handlers.has(method.nsid)) {\n throw new TypeError(`Method ${method.nsid} already registered`)\n }\n const methodConfig =\n typeof config === 'function'\n ? { handler: config, auth: undefined }\n : config\n\n const fetch: FetchHandler =\n method.type === 'subscription'\n ? this.buildSubscriptionHandler(\n method,\n methodConfig.handler as LexRouterSubscriptionHandler<any, any>,\n methodConfig.auth,\n )\n : this.buildMethodHandler(\n method,\n methodConfig.handler as LexRouterMethodHandler<any, any>,\n methodConfig.auth,\n )\n\n this.handlers.set(method.nsid, fetch)\n\n return this\n }\n\n private buildMethodHandler<Method extends Query | Procedure, Credentials>(\n method: Method,\n methodHandler: LexRouterMethodHandler<Method, Credentials>,\n auth?: LexRouterAuth<Credentials, Method>,\n ): FetchHandler {\n const getInput = (\n method.type === 'procedure'\n ? getProcedureInput.bind(method)\n : getQueryInput.bind(method)\n ) as (request: Request) => Promise<InferMethodInput<Method, Body>>\n\n return async (request, connection) => {\n // @NOTE CORS requests should be handled by a middleware before reaching\n // this point.\n if (\n (method.type === 'procedure' && request.method !== 'POST') ||\n (method.type === 'query' &&\n request.method !== 'GET' &&\n request.method !== 'HEAD')\n ) {\n return Response.json(\n { error: 'InvalidRequest', message: 'Method not allowed' },\n { status: 405 },\n )\n }\n\n try {\n const url = new URL(request.url)\n const params = method.parameters.fromURLSearchParams(url.searchParams)\n\n const credentials = auth\n ? await auth({ method, params, request, connection })\n : (undefined as Credentials)\n\n const input = await getInput(request)\n\n const output = await methodHandler({\n credentials,\n params,\n input,\n request,\n connection,\n signal: request.signal,\n })\n\n if (output instanceof Response) {\n return output\n }\n\n // @TODO add validation of output based on method.output.schema?\n\n if (output.body === undefined && output.encoding === undefined) {\n return new Response(null, { status: 200, headers: output.headers })\n }\n\n if (method.output?.encoding === 'application/json') {\n return Response.json(lexToJson(output.body as LexValue), {\n status: 200,\n headers: output.headers,\n })\n }\n\n const headers = new Headers(output.headers)\n headers.set('content-type', output.encoding!)\n return new Response(output.body as BodyInit | null | undefined, {\n status: 200,\n headers,\n })\n } catch (error) {\n return this.handleError(request, method, error)\n }\n }\n }\n\n private buildSubscriptionHandler<Method extends Subscription, Credentials>(\n method: Method,\n methodHandler: LexRouterSubscriptionHandler<Method, Credentials>,\n auth?: LexRouterAuth<Credentials, Method>,\n ): FetchHandler {\n const {\n onHandlerError,\n upgradeWebSocket = (globalThis as any).Deno?.upgradeWebSocket as\n | UpgradeWebSocket\n | undefined,\n } = this.options\n if (!upgradeWebSocket) {\n throw new TypeError(\n 'WebSocket upgrade not supported in this environment. Please provide an upgradeWebSocket option when creating the LexRouter.',\n )\n }\n\n return async (request, connection) => {\n if (request.method !== 'GET') {\n return Response.json(\n { error: 'InvalidRequest', message: 'Method not allowed' },\n { status: 405 },\n )\n }\n\n if (\n request.headers.get('connection')?.toLowerCase() !== 'upgrade' ||\n request.headers.get('upgrade')?.toLowerCase() !== 'websocket'\n ) {\n return Response.json(\n {\n error: 'InvalidRequest',\n message: 'XRPC subscriptions are only available over WebSocket',\n },\n {\n status: 426,\n headers: {\n Connection: 'Upgrade',\n Upgrade: 'websocket',\n },\n },\n )\n }\n\n if (request.signal.aborted) {\n return Response.json(\n { error: 'RequestAborted', message: 'The request was aborted' },\n { status: 499 },\n )\n }\n\n try {\n const { response, socket } = upgradeWebSocket(request)\n\n // @NOTE We are using a distinct signal than request.signal because that\n // signal may get aborted before the WebSocket is closed (this is the\n // case with Deno).\n const abortController = new AbortController()\n const { signal } = abortController\n const abort = () => abortController.abort()\n\n const onMessage = (event: unknown) => {\n const error = new LexError(\n 'InvalidRequest',\n 'XRPC subscriptions do not accept messages',\n { cause: event },\n )\n socket.send(encodeErrorFrame(error))\n socket.close(1008, error.error)\n }\n\n const onOpen = async () => {\n try {\n const url = new URL(request.url)\n const params = method.parameters.fromURLSearchParams(\n url.searchParams,\n )\n\n const credentials: Credentials = auth\n ? await auth({ method, params, request, connection })\n : (undefined as Credentials)\n\n signal.throwIfAborted()\n\n const iterable = methodHandler({\n credentials,\n params,\n input: undefined as InferMethodInput<Method, Body>,\n request,\n connection,\n signal,\n })\n\n const iterator = iterable[Symbol.asyncIterator]()\n\n signal.addEventListener('abort', async () => {\n // @NOTE will cause the process to crash if this throws\n await iterator.return?.()\n })\n\n while (!signal.aborted && socket.readyState === 1) {\n const result = await iterator.next()\n if (result.done) break\n\n // @TODO add validation of output based on method.output.schema?\n\n const data = encodeMessageFrame(method, result.value)\n\n socket.send(data)\n\n // Apply backpressure by waiting for the buffered data to drain\n // before generating the next message\n await drainWebsocket(socket, signal, this.options)\n }\n\n if (socket.readyState === 1) {\n socket.close(1000)\n }\n } catch (error) {\n // If the socket is still open, send an error frame before closing\n if (socket.readyState === 1) {\n const lexError =\n error instanceof LexError\n ? error\n : new LexError('InternalError', 'An internal error occurred')\n\n socket.send(encodeErrorFrame(lexError))\n\n socket.close(\n // https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1\n error instanceof LexError ? 1008 : 1011,\n lexError.error,\n )\n }\n\n // Only report unexpected processing errors\n if (onHandlerError && !isAbortReason(request.signal, error)) {\n await onHandlerError({ error, request, method })\n }\n } finally {\n abortController.abort()\n }\n }\n\n socket.addEventListener('error', abort)\n socket.addEventListener('close', abort)\n socket.addEventListener('open', onOpen)\n socket.addEventListener('message', onMessage)\n\n return response\n } catch (error) {\n return this.handleError(request, method, error)\n }\n }\n }\n\n private async handleError(\n request: Request,\n method: LexMethod,\n error: unknown,\n ) {\n // Only report unexpected processing errors\n const { onHandlerError } = this.options\n if (onHandlerError && !isAbortReason(request.signal, error)) {\n await onHandlerError({ error, request, method })\n }\n\n if (error instanceof LexError) {\n return error.toResponse()\n }\n\n return Response.json(\n { error: 'InternalError', message: 'An internal error occurred' },\n { status: 500 },\n )\n }\n\n /**\n * The main fetch handler for processing XRPC requests.\n *\n * Routes incoming requests to the appropriate method handler based on the\n * NSID in the URL path. Returns appropriate error responses for invalid\n * paths or unimplemented methods.\n *\n * This handler can be used directly with HTTP servers that support the\n * fetch API pattern, or converted to a Node.js request listener using\n * `toRequestListener()`.\n *\n * @param request - The incoming HTTP request\n * @param connection - Optional connection metadata\n * @returns A promise resolving to the HTTP response\n *\n * @example\n * ```typescript\n * // Use with Deno\n * Deno.serve(router.fetch)\n *\n * // Use with Bun\n * Bun.serve({ fetch: router.fetch })\n *\n * // Use with Node.js\n * import { toRequestListener } from '@atproto/lex-server/nodejs'\n * const listener = toRequestListener(router.fetch)\n * http.createServer(listener).listen(3000)\n * ```\n */\n fetch: FetchHandler = async (\n request: Request,\n connection?: ConnectionInfo,\n ): Promise<Response> => {\n const nsid = extractMethodNsid(request)\n\n const fetch = nsid\n ? (this.handlers as Map<unknown, FetchHandler>).get(nsid)\n : undefined\n if (fetch) return fetch(request, connection)\n\n if (!nsid || !isNsidString(nsid)) {\n return Response.json(\n {\n error: 'InvalidRequest',\n message: 'Invalid XRPC method path',\n },\n { status: 404 },\n )\n }\n\n return Response.json(\n {\n error: 'MethodNotImplemented',\n message: `XRPC method \"${nsid}\" not implemented on this server`,\n },\n { status: 501 },\n )\n }\n}\n\nfunction extractMethodNsid(request: Request): string | null {\n const { pathname } = new URL(request.url)\n if (!pathname.startsWith('/xrpc/')) return null\n if (pathname.includes('/', 6)) return null\n // We don't really need to validate the NSID here, the existence of the route\n // (which is looked up based on an NSID) is sufficient.\n return pathname.slice(6)\n}\n\nasync function getProcedureInput<M extends Procedure>(\n this: M,\n request: Request,\n): Promise<InferMethodInput<M, Body>> {\n const encodingRaw = request.headers\n .get('content-type')\n ?.split(';')[0]\n .trim()\n .toLowerCase()\n\n const encoding =\n encodingRaw ||\n // If the caller did not provide a content-type, but the method\n // expects an input, assume binary\n (request.body != null && this.input.encoding != null\n ? 'application/octet-stream'\n : undefined)\n\n if (!this.input.matchesEncoding(encoding)) {\n throw new LexError('InvalidRequest', `Invalid content-type: ${encoding}`)\n }\n\n if (this.input.encoding === 'application/json') {\n // @TODO limit size?\n const data = lexParse(await request.text())\n const body = this.input.schema ? this.input.schema.parse(data) : data\n return { encoding, body } as InferMethodInput<M, Body>\n } else if (this.input.encoding) {\n const body: Body = request\n return { encoding, body } as InferMethodInput<M, Body>\n } else {\n return undefined as InferMethodInput<M, Body>\n }\n}\n\nasync function getQueryInput<M extends Query>(\n this: M,\n request: Request,\n): Promise<InferMethodInput<M, Body>> {\n if (\n request.body ||\n request.headers.has('content-type') ||\n request.headers.has('content-length')\n ) {\n throw new LexError('InvalidRequest', 'GET requests must not have a body')\n }\n\n return undefined as InferMethodInput<M, Body>\n}\n\n// Pre-encoded frame header for error frames\nconst ERROR_FRAME_HEADER = /*#__PURE__*/ encode({ op: -1 })\n\nfunction encodeErrorFrame(error: LexError): Uint8Array {\n return ui8Concat([ERROR_FRAME_HEADER, encode(error.toJSON())])\n}\n\n// Pre-encoded frame header for message frames with unknown type\nconst UNKNOWN_MESSAGE_FRAME_HEADER = /*#__PURE__*/ encode({ op: 1 })\n\nfunction encodeMessageFrame(method: Subscription, value: LexValue): Uint8Array {\n if (isPlainObject(value) && typeof value.$type === 'string') {\n const { $type, ...rest } = value\n return ui8Concat([\n encode({\n op: 1,\n t:\n // If $type starts with `nsid#`, strip the NSID prefix\n $type.charCodeAt(0) !== 0x23 && // '#'\n $type.charCodeAt(method.nsid.length) === 0x23 && // '#'\n $type.startsWith(method.nsid)\n ? $type.slice(method.nsid.length)\n : $type,\n }),\n encode(rest),\n ])\n }\n\n return ui8Concat([UNKNOWN_MESSAGE_FRAME_HEADER, encode(value)])\n}\n\nfunction isAbortReason(signal: AbortSignal, error: unknown): boolean {\n if (!signal.aborted || signal.reason == null) return false\n return (\n error === signal.reason ||\n (error instanceof Error && error.cause === signal.reason)\n )\n}\n"]}
@@ -1,7 +1,73 @@
1
+ /**
2
+ * Type representing the value of a WWW-Authenticate HTTP header.
3
+ *
4
+ * Supports multiple authentication schemes, each with optional parameters.
5
+ * Parameters can be provided as a token68 string (for schemes like Bearer)
6
+ * or as key-value pairs.
7
+ *
8
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7235#section-4.1 | RFC 7235 Section 4.1}
9
+ *
10
+ * @example Bearer scheme with parameters
11
+ * ```typescript
12
+ * const auth: WWWAuthenticate = {
13
+ * Bearer: {
14
+ * realm: 'api.example.com',
15
+ * error: 'InvalidToken',
16
+ * error_description: 'The token has expired'
17
+ * }
18
+ * }
19
+ * // Formats to: Bearer realm="api.example.com", error="InvalidToken", error_description="The token has expired"
20
+ * ```
21
+ *
22
+ * @example Multiple schemes
23
+ * ```typescript
24
+ * const auth: WWWAuthenticate = {
25
+ * Bearer: { realm: 'api' },
26
+ * Basic: { realm: 'api' }
27
+ * }
28
+ * // Formats to: Bearer realm="api", Basic realm="api"
29
+ * ```
30
+ *
31
+ * @example Token68 value (no parameters)
32
+ * ```typescript
33
+ * const auth: WWWAuthenticate = {
34
+ * Bearer: 'base64encodedvalue=='
35
+ * }
36
+ * // Formats to: Bearer base64encodedvalue==
37
+ * ```
38
+ */
1
39
  export type WWWAuthenticate = {
2
40
  [authScheme in string]?: string | {
3
41
  [authParam in string]?: string;
4
42
  };
5
43
  };
44
+ /**
45
+ * Formats a WWWAuthenticate object into an HTTP header string.
46
+ *
47
+ * Converts the structured authentication scheme and parameter data into
48
+ * the proper WWW-Authenticate header format per RFC 7235.
49
+ *
50
+ * @param wwwAuthenticate - The authentication schemes and parameters
51
+ * @returns Formatted header string ready for use in HTTP responses
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const header = formatWWWAuthenticateHeader({
56
+ * Bearer: {
57
+ * realm: 'api.example.com',
58
+ * error: 'MissingToken'
59
+ * }
60
+ * })
61
+ * // Returns: 'Bearer realm="api.example.com", error="MissingToken"'
62
+ * ```
63
+ *
64
+ * @example Empty or undefined values
65
+ * ```typescript
66
+ * const header = formatWWWAuthenticateHeader({
67
+ * Bearer: { realm: 'api', error: undefined }
68
+ * })
69
+ * // Returns: 'Bearer realm="api"' (undefined values are omitted)
70
+ * ```
71
+ */
6
72
  export declare function formatWWWAuthenticateHeader(wwwAuthenticate: WWWAuthenticate): string;
7
73
  //# sourceMappingURL=www-authenticate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"www-authenticate.d.ts","sourceRoot":"","sources":["../../src/lib/www-authenticate.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;KAC3B,UAAU,IAAI,MAAM,CAAC,CAAC,EACnB,MAAM,GACN;SAAG,SAAS,IAAI,MAAM,CAAC,CAAC,EAAE,MAAM;KAAE;CACvC,CAAA;AAED,wBAAgB,2BAA2B,CACzC,eAAe,EAAE,eAAe,GAC/B,MAAM,CAiBR"}
1
+ {"version":3,"file":"www-authenticate.d.ts","sourceRoot":"","sources":["../../src/lib/www-authenticate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,MAAM,eAAe,GAAG;KAC3B,UAAU,IAAI,MAAM,CAAC,CAAC,EACnB,MAAM,GACN;SAAG,SAAS,IAAI,MAAM,CAAC,CAAC,EAAE,MAAM;KAAE;CACvC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,2BAA2B,CACzC,eAAe,EAAE,eAAe,GAC/B,MAAM,CAiBR"}
@@ -1,6 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formatWWWAuthenticateHeader = formatWWWAuthenticateHeader;
4
+ /**
5
+ * Formats a WWWAuthenticate object into an HTTP header string.
6
+ *
7
+ * Converts the structured authentication scheme and parameter data into
8
+ * the proper WWW-Authenticate header format per RFC 7235.
9
+ *
10
+ * @param wwwAuthenticate - The authentication schemes and parameters
11
+ * @returns Formatted header string ready for use in HTTP responses
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const header = formatWWWAuthenticateHeader({
16
+ * Bearer: {
17
+ * realm: 'api.example.com',
18
+ * error: 'MissingToken'
19
+ * }
20
+ * })
21
+ * // Returns: 'Bearer realm="api.example.com", error="MissingToken"'
22
+ * ```
23
+ *
24
+ * @example Empty or undefined values
25
+ * ```typescript
26
+ * const header = formatWWWAuthenticateHeader({
27
+ * Bearer: { realm: 'api', error: undefined }
28
+ * })
29
+ * // Returns: 'Bearer realm="api"' (undefined values are omitted)
30
+ * ```
31
+ */
4
32
  function formatWWWAuthenticateHeader(wwwAuthenticate) {
5
33
  return Object.entries(wwwAuthenticate)
6
34
  .map(([authScheme, authParams]) => {
@@ -1 +1 @@
1
- {"version":3,"file":"www-authenticate.js","sourceRoot":"","sources":["../../src/lib/www-authenticate.ts"],"names":[],"mappings":";;AAMA,kEAmBC;AAnBD,SAAgB,2BAA2B,CACzC,eAAgC;IAEhC,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;SACnC,GAAG,CAAC,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,EAAE;QAChC,IAAI,UAAU,KAAK,SAAS;YAAE,OAAO,IAAI,CAAA;QACzC,MAAM,SAAS,GACb,OAAO,UAAU,KAAK,QAAQ;YAC5B,CAAC,CAAC,CAAC,UAAU,CAAC;YACd,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC;iBACvB,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,CAAC;iBACjC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QAC/D,MAAM,aAAa,GAAG,SAAS,EAAE,MAAM;YACrC,CAAC,CAAC,GAAG,UAAU,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACzC,CAAC,CAAC,UAAU,CAAA;QACd,OAAO,aAAa,CAAA;IACtB,CAAC,CAAC;SACD,MAAM,CAAC,OAAO,CAAC;SACf,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC","sourcesContent":["export type WWWAuthenticate = {\n [authScheme in string]?:\n | string // token68\n | { [authParam in string]?: string }\n}\n\nexport function formatWWWAuthenticateHeader(\n wwwAuthenticate: WWWAuthenticate,\n): string {\n return Object.entries(wwwAuthenticate)\n .map(([authScheme, authParams]) => {\n if (authParams === undefined) return null\n const paramsEnc =\n typeof authParams === 'string'\n ? [authParams]\n : Object.entries(authParams)\n .filter(([_, val]) => val != null)\n .map(([name, val]) => `${name}=${JSON.stringify(val)}`)\n const authChallenge = paramsEnc?.length\n ? `${authScheme} ${paramsEnc.join(', ')}`\n : authScheme\n return authChallenge\n })\n .filter(Boolean)\n .join(', ')\n}\n"]}
1
+ {"version":3,"file":"www-authenticate.js","sourceRoot":"","sources":["../../src/lib/www-authenticate.ts"],"names":[],"mappings":";;AAwEA,kEAmBC;AA/CD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,SAAgB,2BAA2B,CACzC,eAAgC;IAEhC,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;SACnC,GAAG,CAAC,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,EAAE;QAChC,IAAI,UAAU,KAAK,SAAS;YAAE,OAAO,IAAI,CAAA;QACzC,MAAM,SAAS,GACb,OAAO,UAAU,KAAK,QAAQ;YAC5B,CAAC,CAAC,CAAC,UAAU,CAAC;YACd,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC;iBACvB,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,CAAC;iBACjC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QAC/D,MAAM,aAAa,GAAG,SAAS,EAAE,MAAM;YACrC,CAAC,CAAC,GAAG,UAAU,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACzC,CAAC,CAAC,UAAU,CAAA;QACd,OAAO,aAAa,CAAA;IACtB,CAAC,CAAC;SACD,MAAM,CAAC,OAAO,CAAC;SACf,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC","sourcesContent":["/**\n * Type representing the value of a WWW-Authenticate HTTP header.\n *\n * Supports multiple authentication schemes, each with optional parameters.\n * Parameters can be provided as a token68 string (for schemes like Bearer)\n * or as key-value pairs.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc7235#section-4.1 | RFC 7235 Section 4.1}\n *\n * @example Bearer scheme with parameters\n * ```typescript\n * const auth: WWWAuthenticate = {\n * Bearer: {\n * realm: 'api.example.com',\n * error: 'InvalidToken',\n * error_description: 'The token has expired'\n * }\n * }\n * // Formats to: Bearer realm=\"api.example.com\", error=\"InvalidToken\", error_description=\"The token has expired\"\n * ```\n *\n * @example Multiple schemes\n * ```typescript\n * const auth: WWWAuthenticate = {\n * Bearer: { realm: 'api' },\n * Basic: { realm: 'api' }\n * }\n * // Formats to: Bearer realm=\"api\", Basic realm=\"api\"\n * ```\n *\n * @example Token68 value (no parameters)\n * ```typescript\n * const auth: WWWAuthenticate = {\n * Bearer: 'base64encodedvalue=='\n * }\n * // Formats to: Bearer base64encodedvalue==\n * ```\n */\nexport type WWWAuthenticate = {\n [authScheme in string]?:\n | string // token68\n | { [authParam in string]?: string }\n}\n\n/**\n * Formats a WWWAuthenticate object into an HTTP header string.\n *\n * Converts the structured authentication scheme and parameter data into\n * the proper WWW-Authenticate header format per RFC 7235.\n *\n * @param wwwAuthenticate - The authentication schemes and parameters\n * @returns Formatted header string ready for use in HTTP responses\n *\n * @example\n * ```typescript\n * const header = formatWWWAuthenticateHeader({\n * Bearer: {\n * realm: 'api.example.com',\n * error: 'MissingToken'\n * }\n * })\n * // Returns: 'Bearer realm=\"api.example.com\", error=\"MissingToken\"'\n * ```\n *\n * @example Empty or undefined values\n * ```typescript\n * const header = formatWWWAuthenticateHeader({\n * Bearer: { realm: 'api', error: undefined }\n * })\n * // Returns: 'Bearer realm=\"api\"' (undefined values are omitted)\n * ```\n */\nexport function formatWWWAuthenticateHeader(\n wwwAuthenticate: WWWAuthenticate,\n): string {\n return Object.entries(wwwAuthenticate)\n .map(([authScheme, authParams]) => {\n if (authParams === undefined) return null\n const paramsEnc =\n typeof authParams === 'string'\n ? [authParams]\n : Object.entries(authParams)\n .filter(([_, val]) => val != null)\n .map(([name, val]) => `${name}=${JSON.stringify(val)}`)\n const authChallenge = paramsEnc?.length\n ? `${authScheme} ${paramsEnc.join(', ')}`\n : authScheme\n return authChallenge\n })\n .filter(Boolean)\n .join(', ')\n}\n"]}