@replit/river 0.200.0-rc.1 → 0.200.0-rc.3

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.
Files changed (84) hide show
  1. package/README.md +21 -20
  2. package/dist/{chunk-XLXRFSRB.js → chunk-3CCOX55A.js} +76 -42
  3. package/dist/chunk-3CCOX55A.js.map +1 -0
  4. package/dist/chunk-GWYJZFCW.js +653 -0
  5. package/dist/chunk-GWYJZFCW.js.map +1 -0
  6. package/dist/chunk-O4O2E6DJ.js +277 -0
  7. package/dist/chunk-O4O2E6DJ.js.map +1 -0
  8. package/dist/chunk-QXLXDJD5.js +382 -0
  9. package/dist/chunk-QXLXDJD5.js.map +1 -0
  10. package/dist/{chunk-WQVOMUNR.js → chunk-UFMDEG44.js} +24 -18
  11. package/dist/chunk-UFMDEG44.js.map +1 -0
  12. package/dist/{chunk-45HIR2AS.js → chunk-UIYGPURD.js} +90 -532
  13. package/dist/chunk-UIYGPURD.js.map +1 -0
  14. package/dist/{chunk-QMM35C3H.js → chunk-VXYHC666.js} +1 -1
  15. package/dist/chunk-VXYHC666.js.map +1 -0
  16. package/dist/chunk-WSCAA7VY.js +50 -0
  17. package/dist/chunk-WSCAA7VY.js.map +1 -0
  18. package/dist/chunk-ZQVKFLAB.js +399 -0
  19. package/dist/chunk-ZQVKFLAB.js.map +1 -0
  20. package/dist/client-a84783be.d.ts +49 -0
  21. package/dist/{connection-c6521735.d.ts → connection-320fb130.d.ts} +1 -5
  22. package/dist/connection-d0b488e6.d.ts +11 -0
  23. package/dist/context-c9668e95.d.ts +527 -0
  24. package/dist/logging/index.cjs.map +1 -1
  25. package/dist/logging/index.d.cts +1 -1
  26. package/dist/logging/index.d.ts +1 -1
  27. package/dist/logging/index.js +1 -1
  28. package/dist/{index-10ebd26a.d.ts → message-fd349b27.d.ts} +31 -31
  29. package/dist/router/index.cjs +142 -544
  30. package/dist/router/index.cjs.map +1 -1
  31. package/dist/router/index.d.cts +11 -46
  32. package/dist/router/index.d.ts +11 -46
  33. package/dist/router/index.js +2 -4
  34. package/dist/server-dbad597e.d.ts +42 -0
  35. package/dist/{services-34d97070.d.ts → services-690e5553.d.ts} +11 -602
  36. package/dist/transport/impls/uds/client.cjs +1240 -1237
  37. package/dist/transport/impls/uds/client.cjs.map +1 -1
  38. package/dist/transport/impls/uds/client.d.cts +4 -3
  39. package/dist/transport/impls/uds/client.d.ts +4 -3
  40. package/dist/transport/impls/uds/client.js +7 -13
  41. package/dist/transport/impls/uds/client.js.map +1 -1
  42. package/dist/transport/impls/uds/server.cjs +1302 -1168
  43. package/dist/transport/impls/uds/server.cjs.map +1 -1
  44. package/dist/transport/impls/uds/server.d.cts +4 -4
  45. package/dist/transport/impls/uds/server.d.ts +4 -4
  46. package/dist/transport/impls/uds/server.js +6 -6
  47. package/dist/transport/impls/ws/client.cjs +981 -986
  48. package/dist/transport/impls/ws/client.cjs.map +1 -1
  49. package/dist/transport/impls/ws/client.d.cts +6 -5
  50. package/dist/transport/impls/ws/client.d.ts +6 -5
  51. package/dist/transport/impls/ws/client.js +6 -7
  52. package/dist/transport/impls/ws/client.js.map +1 -1
  53. package/dist/transport/impls/ws/server.cjs +1183 -1064
  54. package/dist/transport/impls/ws/server.cjs.map +1 -1
  55. package/dist/transport/impls/ws/server.d.cts +4 -4
  56. package/dist/transport/impls/ws/server.d.ts +4 -4
  57. package/dist/transport/impls/ws/server.js +6 -6
  58. package/dist/transport/index.cjs +1435 -1377
  59. package/dist/transport/index.cjs.map +1 -1
  60. package/dist/transport/index.d.cts +4 -26
  61. package/dist/transport/index.d.ts +4 -26
  62. package/dist/transport/index.js +9 -9
  63. package/dist/util/testHelpers.cjs +782 -333
  64. package/dist/util/testHelpers.cjs.map +1 -1
  65. package/dist/util/testHelpers.d.cts +9 -4
  66. package/dist/util/testHelpers.d.ts +9 -4
  67. package/dist/util/testHelpers.js +45 -23
  68. package/dist/util/testHelpers.js.map +1 -1
  69. package/package.json +1 -1
  70. package/dist/chunk-45HIR2AS.js.map +0 -1
  71. package/dist/chunk-75B5D7MR.js +0 -347
  72. package/dist/chunk-75B5D7MR.js.map +0 -1
  73. package/dist/chunk-AJBZUHSI.js +0 -492
  74. package/dist/chunk-AJBZUHSI.js.map +0 -1
  75. package/dist/chunk-EMIERCU7.js +0 -59
  76. package/dist/chunk-EMIERCU7.js.map +0 -1
  77. package/dist/chunk-LH2VHMQM.js +0 -335
  78. package/dist/chunk-LH2VHMQM.js.map +0 -1
  79. package/dist/chunk-QMM35C3H.js.map +0 -1
  80. package/dist/chunk-W75HU4F6.js +0 -476
  81. package/dist/chunk-W75HU4F6.js.map +0 -1
  82. package/dist/chunk-WQVOMUNR.js.map +0 -1
  83. package/dist/chunk-XLXRFSRB.js.map +0 -1
  84. package/dist/connection-0638316b.d.ts +0 -17
@@ -1 +1 @@
1
- {"version":3,"sources":["../../util/testHelpers.ts","../../router/result.ts","../../router/streams.ts","../../router/procedures.ts","../../tracing/index.ts","../../package.json","../../util/stringify.ts","../../transport/session.ts","../../codec/json.ts","../../transport/options.ts"],"sourcesContent":["import NodeWs, { WebSocketServer } from 'ws';\nimport http from 'node:http';\nimport {\n Err,\n Ok,\n PayloadType,\n Procedure,\n Result,\n ProcedureErrorSchemaType,\n InputReaderErrorSchema,\n OutputReaderErrorSchema,\n ServiceContext,\n ProcedureHandlerContext,\n UNCAUGHT_ERROR_CODE,\n} from '../router';\nimport { Static } from '@sinclair/typebox';\nimport { nanoid } from 'nanoid';\nimport net from 'node:net';\nimport {\n OpaqueTransportMessage,\n PartialTransportMessage,\n} from '../transport/message';\nimport { coerceErrorString } from './stringify';\nimport { Connection, Session, SessionOptions } from '../transport/session';\nimport { Transport } from '../transport/transport';\nimport {\n ReadStream,\n ReadStreamImpl,\n WriteStream,\n WriteStreamImpl,\n} from '../router/streams';\nimport { WsLike } from '../transport/impls/ws/wslike';\nimport { defaultTransportOptions } from '../transport/options';\nimport { BaseErrorSchemaType } from '../router/result';\n\n/**\n * Creates a WebSocket client that connects to a local server at the specified port.\n * This should only be used for testing.\n * @param port - The port number to connect to.\n * @returns A Promise that resolves to a WebSocket instance.\n */\nexport function createLocalWebSocketClient(port: number): WsLike {\n const sock = new NodeWs(`ws://localhost:${port}`);\n sock.binaryType = 'arraybuffer';\n\n return sock;\n}\n\n/**\n * Creates a WebSocket server instance using the provided HTTP server.\n * Only used as helper for testing.\n * @param server - The HTTP server instance to use for the WebSocket server.\n * @returns A Promise that resolves to the created WebSocket server instance.\n */\nexport function createWebSocketServer(server: http.Server) {\n return new WebSocketServer({ server });\n}\n\n/**\n * Starts listening on the given server and returns the automatically allocated port number.\n * This should only be used for testing.\n * @param server - The http server to listen on.\n * @returns A promise that resolves with the allocated port number.\n * @throws An error if a port cannot be allocated.\n */\nexport function onWsServerReady(server: http.Server): Promise<number> {\n return new Promise((resolve, reject) => {\n server.listen(() => {\n const addr = server.address();\n if (typeof addr === 'object' && addr) {\n resolve(addr.port);\n } else {\n reject(new Error(\"couldn't find a port to allocate\"));\n }\n });\n });\n}\n\nexport function onUdsServeReady(\n server: net.Server,\n path: string,\n): Promise<void> {\n return new Promise<void>((resolve) => {\n server.listen(path, resolve);\n });\n}\n\nexport function getIteratorFromStream<T, E extends Static<BaseErrorSchemaType>>(\n readStream: ReadStream<T, E>,\n) {\n return readStream[Symbol.asyncIterator]();\n}\n\n/**\n * Retrieves the next value from an async iterable iterator.\n * @param iter The async iterable iterator.\n * @returns A promise that resolves to the next value from the iterator.\n */\nexport async function iterNext<T>(iter: {\n next(): Promise<\n | {\n done: false;\n value: T;\n }\n | {\n done: true;\n value: undefined;\n }\n >;\n}) {\n return await iter.next().then((res) => res.value as T);\n}\n\nexport function payloadToTransportMessage<Payload>(\n payload: Payload,\n): PartialTransportMessage<Payload> {\n return {\n streamId: 'stream',\n controlFlags: 0,\n payload,\n };\n}\n\nexport function createDummyTransportMessage() {\n return payloadToTransportMessage({\n msg: 'cool',\n test: Math.random(),\n });\n}\n\n/**\n * Waits for a message on the transport.\n * @param {Transport} t - The transport to listen to.\n * @param filter - An optional filter function to apply to the received messages.\n * @returns A promise that resolves with the payload of the first message that passes the filter.\n */\nexport async function waitForMessage(\n t: Transport<Connection>,\n filter?: (msg: OpaqueTransportMessage) => boolean,\n rejectMismatch?: boolean,\n) {\n return new Promise((resolve, reject) => {\n function cleanup() {\n t.removeEventListener('message', onMessage);\n }\n\n function onMessage(msg: OpaqueTransportMessage) {\n if (!filter || filter(msg)) {\n cleanup();\n resolve(msg.payload);\n } else if (rejectMismatch) {\n cleanup();\n reject(new Error('message didnt match the filter'));\n }\n }\n\n t.addEventListener('message', onMessage);\n });\n}\n\nfunction catchProcError(err: unknown) {\n const errorMsg = coerceErrorString(err);\n return Err({ code: UNCAUGHT_ERROR_CODE, message: errorMsg });\n}\n\nexport const testingSessionOptions: SessionOptions = defaultTransportOptions;\n\nexport function dummySession() {\n return new Session<Connection>(\n undefined,\n 'client',\n 'server',\n testingSessionOptions,\n );\n}\n\nfunction dummyCtx<State>(\n state: State,\n session: Session<Connection>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n): ProcedureHandlerContext<State> {\n return {\n ...extendedContext,\n from: session.from,\n state,\n metadata: {},\n abortController: new AbortController(),\n clientAbortSignal: new AbortController().signal,\n onRequestFinished: () => undefined,\n };\n}\n\nexport function asClientRpc<\n State extends object,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'rpc', Init, null, Output, Err>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n) {\n return async (\n msg: Static<Init>,\n ): Promise<\n Result<Static<Output>, Static<Err> | Static<typeof OutputReaderErrorSchema>>\n > => {\n return proc\n .handler(dummyCtx(state, session, extendedContext), msg)\n .catch(catchProcError);\n };\n}\n\nfunction createOutputPipe<\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(): {\n reader: ReadStream<\n Static<Output>,\n Static<Err> | Static<typeof OutputReaderErrorSchema>\n >;\n writer: WriteStream<Result<Static<Output>, Static<Err>>>;\n} {\n const reader = new ReadStreamImpl<\n Static<Output>,\n Static<Err> | Static<typeof OutputReaderErrorSchema>\n >(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n writer.triggerCloseRequest();\n });\n });\n const writer = new WriteStreamImpl<Result<Static<Output>, Static<Err>>>(\n (v) => {\n reader.pushValue(v);\n },\n () => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n reader.triggerClose();\n });\n },\n );\n\n return { reader, writer };\n}\n\nfunction createInputPipe<Input extends PayloadType>(): {\n reader: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>;\n writer: WriteStream<Static<Input>>;\n} {\n const reader = new ReadStreamImpl<\n Static<Input>,\n Static<typeof InputReaderErrorSchema>\n >(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n writer.triggerCloseRequest();\n });\n });\n const writer = new WriteStreamImpl<Static<Input>>(\n (v) => {\n reader.pushValue(Ok(v));\n },\n () => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n reader.triggerClose();\n });\n },\n );\n\n return { reader, writer };\n}\n\nexport function asClientStream<\n State extends object,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'stream', Init, Input, Output, Err>,\n init?: Static<Init>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): [WriteStream<Static<Input>>, ReadStream<Static<Output>, Static<Err>>] {\n const inputPipe = createInputPipe<Input>();\n const outputPipe = createOutputPipe<Output, Err>();\n\n void proc\n .handler(\n dummyCtx(state, session, extendedContext),\n init ?? {},\n inputPipe.reader,\n outputPipe.writer,\n )\n .catch((err: unknown) => outputPipe.writer.write(catchProcError(err)));\n\n return [inputPipe.writer, outputPipe.reader];\n}\n\nexport function asClientSubscription<\n State extends object,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'subscription', Init, null, Output, Err>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): (msg: Static<Init>) => ReadStream<Static<Output>, Static<Err>> {\n const outputPipe = createOutputPipe<Output, Err>();\n\n return (msg: Static<Init>) => {\n void proc\n .handler(\n dummyCtx(state, session, extendedContext),\n msg,\n outputPipe.writer,\n )\n .catch((err: unknown) => outputPipe.writer.write(catchProcError(err)));\n\n return outputPipe.reader;\n };\n}\n\nexport function asClientUpload<\n State extends object,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'upload', Init, Input, Output, Err>,\n init?: Static<Init>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): [\n WriteStream<Static<Input>>,\n () => Promise<Result<Static<Output>, Static<Err>>>,\n] {\n const inputPipe = createInputPipe<Input>();\n const result = proc\n .handler(\n dummyCtx(state, session, extendedContext),\n init ?? {},\n inputPipe.reader,\n )\n .catch(catchProcError);\n\n return [inputPipe.writer, () => result];\n}\n\nexport const getUnixSocketPath = () => {\n // https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections\n return process.platform === 'win32'\n ? `\\\\\\\\?\\\\pipe\\\\${nanoid()}`\n : `/tmp/${nanoid()}.sock`;\n};\n","import {\n Static,\n TLiteral,\n TObject,\n TSchema,\n TString,\n TUnion,\n Type,\n} from '@sinclair/typebox';\nimport { Client } from './client';\nimport { ReadStream } from './streams';\n\ntype TLiteralString = TLiteral<string>;\n\nexport type BaseErrorSchemaType =\n | TObject<{\n code: TLiteralString | TUnion<Array<TLiteralString>>;\n message: TLiteralString | TString;\n }>\n | TObject<{\n code: TLiteralString | TUnion<Array<TLiteralString>>;\n message: TLiteralString | TString;\n extras: TSchema;\n }>;\n\n/**\n * Takes in a specific error schema and returns a result schema the error\n */\nexport const ErrResultSchema = <T extends BaseErrorSchemaType>(t: T) =>\n Type.Object({\n ok: Type.Literal(false),\n payload: t,\n });\n\n/**\n * AnyResultSchema is a schema to validate any result.\n */\nexport const AnyResultSchema = Type.Union([\n Type.Object({\n ok: Type.Literal(false),\n payload: Type.Object({\n code: Type.String(),\n message: Type.String(),\n extras: Type.Optional(Type.Unknown()),\n }),\n }),\n\n Type.Object({\n ok: Type.Literal(true),\n payload: Type.Unknown(),\n }),\n]);\n\nexport interface OkResult<T> {\n ok: true;\n payload: T;\n}\nexport interface ErrResult<Err extends Static<BaseErrorSchemaType>> {\n ok: false;\n payload: Err;\n}\nexport type Result<T, Err extends Static<BaseErrorSchemaType>> =\n | OkResult<T>\n | ErrResult<Err>;\n\nexport function Ok<const T extends Array<unknown>>(p: T): OkResult<T>;\nexport function Ok<const T extends ReadonlyArray<unknown>>(p: T): OkResult<T>;\nexport function Ok<const T>(payload: T): OkResult<T>;\nexport function Ok<const T>(payload: T): OkResult<T> {\n return {\n ok: true,\n payload,\n };\n}\n\nexport function Err<const Err extends Static<BaseErrorSchemaType>>(\n error: Err,\n): ErrResult<Err> {\n return {\n ok: false,\n payload: error,\n };\n}\n\n/**\n * Refine a {@link Result} type to its returned payload.\n */\nexport type ResultUnwrapOk<R> = R extends Result<infer T, infer __E>\n ? T\n : never;\n\n/**\n * Unwrap a {@link Result} type and return the payload if successful,\n * otherwise throws an error.\n * @param result - The result to unwrap.\n * @throws Will throw an error if the result is not ok.\n */\nexport function unwrap<T, Err extends Static<BaseErrorSchemaType>>(\n result: Result<T, Err>,\n): T {\n if (result.ok) {\n return result.payload;\n }\n\n throw new Error(\n `Cannot non-ok result, got: ${result.payload.code} - ${result.payload.message}`,\n );\n}\n\n/**\n * Refine a {@link Result} type to its error payload.\n */\nexport type ResultUnwrapErr<R> = R extends Result<infer __T, infer Err>\n ? Err\n : never;\n\n/**\n * Retrieve the output type for a procedure, represented as a {@link Result}\n * type.\n * Example:\n * ```\n * type Message = Output<typeof client, 'serviceName', 'procedureName'>\n * ```\n */\nexport type Output<\n RiverClient,\n ServiceName extends keyof RiverClient,\n ProcedureName extends keyof RiverClient[ServiceName],\n Procedure = RiverClient[ServiceName][ProcedureName],\n Fn extends (...args: never) => unknown = (...args: never) => unknown,\n> = RiverClient extends Client<infer __ServiceSchemaMap>\n ? Procedure extends object\n ? Procedure extends object & { rpc: infer RpcHandler extends Fn }\n ? Awaited<ReturnType<RpcHandler>>\n : Procedure extends object & { upload: infer UploadHandler extends Fn }\n ? ReturnType<UploadHandler> extends [\n infer __UploadInputMessage,\n (...args: never) => Promise<infer UploadOutputMessage>,\n ]\n ? UploadOutputMessage\n : never\n : Procedure extends object & { stream: infer StreamHandler extends Fn }\n ? ReturnType<StreamHandler> extends [\n infer __StreamInputMessage,\n ReadStream<infer StreamOutputMessage, Static<BaseErrorSchemaType>>,\n ]\n ? StreamOutputMessage\n : never\n : Procedure extends object & {\n subscribe: infer SubscriptionHandler extends Fn;\n }\n ? Awaited<ReturnType<SubscriptionHandler>> extends ReadStream<\n infer SubscriptionOutputMessage,\n Static<BaseErrorSchemaType>\n >\n ? SubscriptionOutputMessage\n : never\n : never\n : never\n : never;\n","import { Static } from '@sinclair/typebox';\nimport { BaseErrorSchemaType, Err, Result } from './result';\n\nexport const StreamDrainedError = {\n code: 'STREAM_DRAINED',\n message: 'Stream was drained',\n} as const;\n\ntype ReadStreamResult<T, E extends Static<BaseErrorSchemaType>> = Result<\n T,\n E | typeof StreamDrainedError\n>;\n\n/**\n * A `ReadStream` represents a stream of data.\n *\n * This stream is not closable by the reader, the reader must wait for\n * the writer to close it.\n *\n * The stream can only be locked (aka consumed) once and will remain\n * locked, trying to lock the stream again will throw an TypeError.\n */\nexport interface ReadStream<T, E extends Static<BaseErrorSchemaType>> {\n /**\n * Stream implements AsyncIterator API and can be consumed via\n * for-await-of loops.\n *\n */\n [Symbol.asyncIterator](): {\n next(): Promise<\n | {\n done: false;\n value: ReadStreamResult<T, E>;\n }\n | {\n done: true;\n value: undefined;\n }\n >;\n };\n /**\n * `asArray` locks the stream and returns a promise that resolves\n * with an array of the stream's content when the stream is closed.\n *\n */\n asArray(): Promise<Array<ReadStreamResult<T, E>>>;\n /**\n * `drain` locks the stream and discards any existing or future data.\n *\n * If there is an existing Promise waiting for the next value,\n * `drain` causes it to resolve with a {@link StreamDrainedError} error.\n */\n drain(): undefined;\n /**\n * `isLocked` returns true if the stream is locked.\n */\n isLocked(): boolean;\n /**\n * `onClose` registers a callback that will be called when the stream\n * is closed. Returns a function that can be used to unregister the\n * listener.\n */\n onClose(cb: () => void): () => void;\n /**\n * `isClosed` returns true if the stream was closed by the writer.\n *\n * Note that the stream can still have queued data and can still be\n * consumed.\n */\n isClosed(): boolean;\n /**\n * `requestClose` sends a request to the writer to close the stream,\n * and resolves the writer closes its end of the stream..\n *\n * The stream can still receive more data after the request is sent. If you\n * no longer wish to use the stream, make sure to call `drain`.\n */\n requestClose(): Promise<undefined>;\n /**\n * `isCloseRequested` checks if the reader has requested to close the stream.\n */\n isCloseRequested(): boolean;\n}\n\n/**\n * A `WriteStream` is a streams that can be written to.\n */\nexport interface WriteStream<T> {\n /**\n * `write` writes a value to the stream. An error is thrown if writing to a closed stream.\n */\n write(value: T): undefined;\n /**\n * `close` signals the closure of the write stream, informing the reader that the stream is complete.\n * Calling close multiple times has no effect.\n */\n close(): undefined;\n /**\n * `isCloseRequested` checks if the reader has requested to close the stream.\n */\n isCloseRequested(): boolean;\n /**\n * `onCloseRequest` registers a callback that will be called when the stream's\n * reader requests a close. Returns a function that can be used to unregister the\n * listener.\n */\n onCloseRequest(cb: () => void): () => void;\n /**\n * `isClosed` returns true if the stream was closed by the writer.\n */\n isClosed(): boolean;\n}\n\n/**\n * Internal implementation of a `ReadStream`.\n * This won't be exposed as an interface to river\n * consumers directly, it has internal river methods\n * to pushed data to the stream and close it.\n */\nexport class ReadStreamImpl<T, E extends Static<BaseErrorSchemaType>>\n implements ReadStream<T, E>\n{\n /**\n * Whether the stream is closed.\n */\n private closed = false;\n /**\n * A list of listeners that will be called when the stream is closed.\n */\n private onCloseListeners: Set<() => void>;\n /**\n * Whether the user has requested to close the stream.\n */\n private closeRequested = false;\n /**\n * Used to signal to the outside world that the user has requested to close the stream.\n */\n private closeRequestCallback: () => void;\n /**\n * Whether the stream is locked.\n */\n private locked = false;\n /**\n * Whether drain was called.\n */\n private drained = false;\n /**\n * This flag allows us to avoid cases where drain was called,\n * but the stream is fully consumed and closed. We don't need\n * to signal that drain was closed.\n */\n private didDrainDisposeValues = false;\n /**\n * A list of values that have been pushed to the stream but not yet emitted to the user.\n */\n private queue: Array<ReadStreamResult<T, E>> = [];\n /**\n * Used by methods in the class to signal to the iterator that it\n * should check for the next value.\n */\n private nextPromise: Promise<void> | null = null;\n /**\n * Resolves nextPromise\n */\n private resolveNextPromise: null | (() => void) = null;\n\n constructor(closeRequestCallback: () => void) {\n this.closeRequestCallback = closeRequestCallback;\n this.onCloseListeners = new Set();\n }\n\n public [Symbol.asyncIterator]() {\n if (this.isLocked()) {\n throw new TypeError('ReadStream is already locked');\n }\n\n // first iteration with drain signals an error, the following one signals end of iteration.\n let didSignalDrain = false;\n this.locked = true;\n\n return {\n next: async () => {\n if (this.drained && didSignalDrain) {\n return {\n done: true,\n value: undefined,\n } as const;\n }\n\n // Wait until we have something in the queue\n while (this.queue.length === 0) {\n if (this.isClosed() && !this.didDrainDisposeValues) {\n return {\n done: true,\n value: undefined,\n } as const;\n }\n\n if (this.drained) {\n didSignalDrain = true;\n\n return {\n done: false,\n value: Err(StreamDrainedError),\n } as const;\n }\n\n if (!this.nextPromise) {\n this.nextPromise = new Promise<void>((resolve) => {\n this.resolveNextPromise = resolve;\n });\n }\n\n await this.nextPromise;\n this.nextPromise = null;\n this.resolveNextPromise = null;\n }\n\n // Unfortunately we have to use non-null assertion here, because T can be undefined\n // we already check for array length above anyway\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = this.queue.shift()!;\n\n return { done: false, value } as const;\n },\n return: async () => {\n this.drain();\n return { done: true, value: undefined } as const;\n },\n };\n }\n\n public async asArray(): Promise<Array<ReadStreamResult<T, E>>> {\n const array: Array<ReadStreamResult<T, E>> = [];\n for await (const value of this) {\n array.push(value);\n }\n\n return array;\n }\n\n public drain(): undefined {\n if (this.drained) {\n return;\n }\n\n this.locked = true;\n this.drained = true;\n this.didDrainDisposeValues = this.queue.length > 0;\n this.queue.length = 0;\n\n this.resolveNextPromise?.();\n }\n\n public isClosed(): boolean {\n return this.closed;\n }\n\n public isLocked(): boolean {\n return this.locked;\n }\n\n public onClose(cb: () => void): () => void {\n if (this.isClosed()) {\n throw new Error('Stream is already closed');\n }\n\n this.onCloseListeners.add(cb);\n\n return () => {\n this.onCloseListeners.delete(cb);\n };\n }\n\n public requestClose(): Promise<undefined> {\n if (this.isClosed()) {\n throw new Error('Cannot request close after stream already closed');\n }\n\n if (!this.closeRequested) {\n this.closeRequested = true;\n this.closeRequestCallback();\n }\n\n return new Promise<undefined>((resolve) => {\n this.onClose(() => {\n resolve(undefined);\n });\n });\n }\n\n public isCloseRequested(): boolean {\n return this.closeRequested;\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Pushes a value to the stream.\n */\n public pushValue(value: Result<T, E>): undefined {\n if (this.drained) {\n return;\n }\n\n if (this.closed) {\n throw new Error('Cannot push to closed stream');\n }\n\n this.queue.push(value);\n this.resolveNextPromise?.();\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Triggers the close of the stream. Make sure to push all remaining\n * values before calling this method.\n */\n public triggerClose(): undefined {\n if (this.isClosed()) {\n throw new Error('Unexpected closing multiple times');\n }\n\n this.closed = true;\n this.resolveNextPromise?.();\n this.onCloseListeners.forEach((cb) => cb());\n this.onCloseListeners.clear();\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n */\n public hasValuesInQueue(): boolean {\n return this.queue.length > 0;\n }\n}\n\n/**\n * Internal implementation of a `WriteStream`.\n * This won't be exposed as an interface to river\n * consumers directly, it has internal river methods\n * to trigger a close request, a way to pass on close\n * signals, and a way to push data to the stream.\n */\nexport class WriteStreamImpl<T> implements WriteStream<T> {\n /**\n * Passed via constructor to pass on write requests\n */\n private writeCb: (value: T) => void;\n /**\n * Passed via constructor to pass on close requests\n */\n private closeCb: () => void;\n /**\n * Whether the stream is closed.\n */\n private closed = false;\n /**\n * Whether the reader has requested to close the stream.\n */\n private closeRequested = false;\n /**\n * A list of listeners that will be called when the stream is closed.\n */\n private onCloseListeners: Set<() => void>;\n\n constructor(writeCb: (value: T) => void, closeCb: () => void) {\n this.writeCb = writeCb;\n this.closeCb = closeCb;\n this.onCloseListeners = new Set();\n }\n\n public write(value: T): undefined {\n if (this.isClosed()) {\n throw new Error('Cannot write to closed stream');\n }\n\n this.writeCb(value);\n }\n\n public close(): undefined {\n if (this.isClosed()) {\n return;\n }\n\n this.closed = true;\n this.closeCb();\n }\n\n public isCloseRequested(): boolean {\n return this.closeRequested;\n }\n\n public onCloseRequest(cb: () => void): () => void {\n if (this.isClosed()) {\n throw new Error('Stream is already closed');\n }\n\n this.onCloseListeners.add(cb);\n\n return () => this.onCloseListeners.delete(cb);\n }\n\n public isClosed(): boolean {\n return this.closed;\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Triggers a close request.\n */\n public triggerCloseRequest(): undefined {\n if (this.isCloseRequested()) {\n throw new Error('Cannot trigger close request multiple times');\n }\n\n if (this.isClosed()) {\n throw new Error('Cannot trigger close request on closed stream');\n }\n\n this.closeRequested = true;\n this.onCloseListeners.forEach((cb) => cb());\n this.onCloseListeners.clear();\n }\n}\n","/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\nimport { Static, TNever, TSchema, TUnion, Type } from '@sinclair/typebox';\nimport { ProcedureHandlerContext } from './context';\nimport { BaseErrorSchemaType, Result } from './result';\nimport { ReadStream, WriteStream } from './streams';\n\n/**\n * Brands a type to prevent it from being directly constructed.\n */\nexport type Branded<T> = T & { readonly __BRAND_DO_NOT_USE: unique symbol };\n\n/**\n * Unbrands a {@link Branded} type.\n */\nexport type Unbranded<T> = T extends Branded<infer U> ? U : never;\n\n/**\n * The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a\n * different type for the very first initialization message. The suffixless types correspond to\n * gRPC's four combinations of stream / non-stream in each direction.\n */\nexport type ValidProcType =\n // Single message in both directions (1:1).\n | 'rpc'\n // Client-stream single message from server (n:1).\n | 'upload'\n // Single message from client, stream from server (1:n).\n | 'subscription'\n // Bidirectional stream (n:n).\n | 'stream';\n\n/**\n * Represents the payload type for {@link Procedure}s.\n */\nexport type PayloadType = TSchema;\n\n/**\n * INTERNAL_RIVER_ERROR_CODE is the code that is used when an internal error occurs,\n * this means that some invariants expected by the river server implementation have\n * been violated. When encountering this error please report this to river maintainers.\n */\nexport const INTERNAL_RIVER_ERROR_CODE = 'INTERNAL_RIVER_ERROR';\n/**\n * UNCAUGHT_ERROR_CODE is the code that is used when an error is thrown\n * inside a procedure handler that's not required.\n */\nexport const UNCAUGHT_ERROR_CODE = 'UNCAUGHT_ERROR' as const;\n/**\n * UNEXPECTED_DISCONNECT_CODE is the code used the stream's session\n * disconnect unexpetedly.\n */\nexport const UNEXPECTED_DISCONNECT_CODE = 'UNEXPECTED_DISCONNECT' as const;\n/**\n * INVALID_REQUEST_CODE is the code used when a client's request is invalid.\n */\nexport const INVALID_REQUEST_CODE = 'INVALID_REQUEST' as const;\n/**\n * ABORT_CODE is the code used when either server or client aborts the stream.\n */\nexport const ABORT_CODE = 'ABORT' as const;\n\n/**\n * OutputReaderErrorSchema is the schema for all the errors that can be\n * emitted in the Output ReadStream on the client.\n */\nexport const OutputReaderErrorSchema = Type.Object({\n code: Type.Union([\n Type.Literal(INTERNAL_RIVER_ERROR_CODE),\n Type.Literal(UNCAUGHT_ERROR_CODE),\n Type.Literal(UNEXPECTED_DISCONNECT_CODE),\n Type.Literal(INVALID_REQUEST_CODE),\n Type.Literal(ABORT_CODE),\n ]),\n message: Type.String(),\n});\n\n/**\n * InputReaderErrorSchema is the schema for all the errors that can be\n * emitted in the Input ReadStream on the server.\n */\nexport const InputReaderErrorSchema = Type.Object({\n code: Type.Union([\n Type.Literal(UNCAUGHT_ERROR_CODE),\n Type.Literal(UNEXPECTED_DISCONNECT_CODE),\n Type.Literal(INVALID_REQUEST_CODE),\n Type.Literal(ABORT_CODE),\n ]),\n message: Type.String(),\n});\n\n/**\n * Represents an acceptable schema to pass to a procedure.\n * Just a type of a schema, not an actual schema.\n */\nexport type ProcedureErrorSchemaType =\n | TUnion<Array<BaseErrorSchemaType>>\n | BaseErrorSchemaType\n | TNever;\n\n/**\n * Procedure for a single message in both directions (1:1).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface RpcProcedure<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'rpc';\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n ): Promise<Result<Static<Output>, Static<Err>>>;\n}\n\n/**\n * Procedure for a client-stream (potentially preceded by an initialization message),\n * single message from server (n:1).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Input - The TypeBox schema of the input object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface UploadProcedure<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'upload';\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>,\n ): Promise<Result<Static<Output>, Static<Err>>>;\n}\n\n/**\n * Procedure for a single message from client, stream from server (1:n).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface SubscriptionProcedure<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'subscription';\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n output: WriteStream<Result<Static<Output>, Static<Err>>>,\n ): Promise<void | undefined>;\n}\n\n/**\n * Procedure for a bidirectional stream (potentially preceded by an initialization message),\n * (n:n).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Input - The TypeBox schema of the input object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface StreamProcedure<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'stream';\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>,\n output: WriteStream<Result<Static<Output>, Static<Err>>>,\n ): Promise<void | undefined>;\n}\n\n/**\n * Defines a Procedure type that can be a:\n * - {@link RpcProcedure} for a single message in both directions (1:1)\n * - {@link UploadProcedure} for a client-stream (potentially preceded by an\n * initialization message)\n * - {@link SubscriptionProcedure} for a single message from client, stream from server (1:n)\n * - {@link StreamProcedure} for a bidirectional stream (potentially preceded by an\n * initialization message)\n *\n * @template State - The TypeBox schema of the state object.\n * @template Ty - The type of the procedure.\n * @template Input - The TypeBox schema of the input object.\n * @template Init - The TypeBox schema of the input initialization object, if any.\n * @template Output - The TypeBox schema of the output object.\n */\nexport type Procedure<\n State,\n Ty extends ValidProcType,\n Init extends PayloadType,\n Input extends PayloadType | null,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> = { type: Ty } & (Input extends PayloadType\n ? Ty extends 'upload'\n ? UploadProcedure<State, Init, Input, Output, Err>\n : Ty extends 'stream'\n ? StreamProcedure<State, Init, Input, Output, Err>\n : never\n : Ty extends 'rpc'\n ? RpcProcedure<State, Init, Output, Err>\n : Ty extends 'subscription'\n ? SubscriptionProcedure<State, Init, Output, Err>\n : never);\n\n/**\n * Represents any {@link Procedure} type.\n *\n * @template State - The context state object. You can provide this to constrain\n * the type of procedures.\n */\nexport type AnyProcedure<State = object> = Procedure<\n State,\n ValidProcType,\n PayloadType,\n PayloadType | null,\n PayloadType,\n ProcedureErrorSchemaType\n>;\n\n/**\n * Represents a map of {@link Procedure}s.\n *\n * @template State - The context state object. You can provide this to constrain\n * the type of procedures.\n */\nexport type ProcedureMap<State = object> = Record<string, AnyProcedure<State>>;\n\n// typescript is funky so with these upcoming procedure constructors, the overloads\n// which handle the `init` case _must_ come first, otherwise the `init` property\n// is not recognized as optional, for some reason\n\n/**\n * Creates an {@link RpcProcedure}.\n */\n// signature: default errors\nfunction rpc<State, Init extends PayloadType, Output extends PayloadType>(def: {\n init: Init;\n output: Output;\n errors?: never;\n description?: string;\n handler: RpcProcedure<State, Init, Output, TNever>['handler'];\n}): Branded<RpcProcedure<State, Init, Output, TNever>>;\n\n// signature: explicit errors\nfunction rpc<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler: RpcProcedure<State, Init, Output, Err>['handler'];\n}): Branded<RpcProcedure<State, Init, Output, Err>>;\n\n// implementation\nfunction rpc({\n init,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: RpcProcedure<\n object,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n ...(description ? { description } : {}),\n type: 'rpc',\n init,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates an {@link UploadProcedure}, optionally with an initialization message.\n */\n// signature: init with default errors\nfunction upload<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors?: never;\n description?: string;\n handler: UploadProcedure<State, Init, Input, Output, TNever>['handler'];\n}): Branded<UploadProcedure<State, Init, Input, Output, TNever>>;\n\n// signature: init with explicit errors\nfunction upload<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler: UploadProcedure<State, Init, Input, Output, Err>['handler'];\n}): Branded<UploadProcedure<State, Init, Input, Output, Err>>;\n\n// implementation\nfunction upload({\n init,\n input,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n input: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: UploadProcedure<\n object,\n PayloadType,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'upload',\n ...(description ? { description } : {}),\n init,\n input,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates a {@link SubscriptionProcedure}.\n */\n// signature: default errors\nfunction subscription<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n output: Output;\n errors?: never;\n description?: string;\n handler: SubscriptionProcedure<State, Init, Output, TNever>['handler'];\n}): Branded<SubscriptionProcedure<State, Init, Output, TNever>>;\n\n// signature: explicit errors\nfunction subscription<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler: SubscriptionProcedure<State, Init, Output, Err>['handler'];\n}): Branded<SubscriptionProcedure<State, Init, Output, Err>>;\n\n// implementation\nfunction subscription({\n init,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: SubscriptionProcedure<\n object,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'subscription',\n ...(description ? { description } : {}),\n init,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates a {@link StreamProcedure}, optionally with an initialization message.\n */\n// signature: with default errors\nfunction stream<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors?: never;\n description?: string;\n handler: StreamProcedure<State, Init, Input, Output, TNever>['handler'];\n}): Branded<StreamProcedure<State, Init, Input, Output, TNever>>;\n\n// signature: explicit errors\nfunction stream<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler: StreamProcedure<State, Init, Input, Output, Err>['handler'];\n}): Branded<StreamProcedure<State, Init, Input, Output, Err>>;\n\n// implementation\nfunction stream({\n init,\n input,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n input: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: StreamProcedure<\n object,\n PayloadType,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'stream',\n ...(description ? { description } : {}),\n init,\n input,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Holds the {@link Procedure} creation functions. Use these to create\n * procedures for services. You aren't allowed to create procedures directly.\n */\nexport const Procedure = {\n rpc,\n upload,\n subscription,\n stream,\n};\n","import {\n Context,\n Span,\n SpanKind,\n context,\n propagation,\n trace,\n} from '@opentelemetry/api';\nimport { version as RIVER_VERSION } from '../package.json';\nimport { ValidProcType } from '../router';\nimport { ClientTransport, Connection, Session } from '../transport';\n\nexport interface PropagationContext {\n traceparent: string;\n tracestate: string;\n}\n\nexport interface TelemetryInfo {\n span: Span;\n ctx: Context;\n}\n\nexport function getPropagationContext(\n ctx: Context,\n): PropagationContext | undefined {\n const tracing = {\n traceparent: '',\n tracestate: '',\n };\n propagation.inject(ctx, tracing);\n return tracing;\n}\n\nexport function createSessionTelemetryInfo(\n session: Session<Connection>,\n propagationCtx?: PropagationContext,\n): TelemetryInfo {\n const parentCtx = propagationCtx\n ? propagation.extract(context.active(), propagationCtx)\n : context.active();\n\n const span = tracer.startSpan(\n `session ${session.id}`,\n {\n attributes: {\n component: 'river',\n 'river.session.id': session.id,\n 'river.session.to': session.to,\n 'river.session.from': session.from,\n },\n },\n parentCtx,\n );\n\n const ctx = trace.setSpan(parentCtx, span);\n\n return { span, ctx };\n}\n\nexport function createConnectionTelemetryInfo(\n connection: Connection,\n info: TelemetryInfo,\n): TelemetryInfo {\n const span = tracer.startSpan(\n `connection ${connection.id}`,\n {\n attributes: {\n component: 'river',\n 'river.connection.id': connection.id,\n },\n links: [{ context: info.span.spanContext() }],\n },\n info.ctx,\n );\n\n const ctx = trace.setSpan(info.ctx, span);\n\n return { span, ctx };\n}\n\nexport function createProcTelemetryInfo(\n transport: ClientTransport<Connection>,\n kind: ValidProcType,\n serviceName: string,\n procedureName: string,\n streamId: string,\n): TelemetryInfo {\n const baseCtx = context.active();\n const span = tracer.startSpan(\n `procedure call ${serviceName}.${procedureName}`,\n {\n attributes: {\n component: 'river',\n 'river.method.kind': kind,\n 'river.method.service': serviceName,\n 'river.method.name': procedureName,\n 'river.streamId': streamId,\n 'span.kind': 'client',\n },\n kind: SpanKind.CLIENT,\n },\n baseCtx,\n );\n\n const ctx = trace.setSpan(baseCtx, span);\n\n transport.log?.info(`invoked ${serviceName}.${procedureName}`, {\n clientId: transport.clientId,\n transportMessage: {\n procedureName,\n serviceName,\n },\n telemetry: {\n traceId: span.spanContext().traceId,\n spanId: span.spanContext().spanId,\n },\n });\n return { span, ctx };\n}\n\nexport function createHandlerSpan<Fn extends (span: Span) => unknown>(\n kind: ValidProcType,\n serviceName: string,\n procedureName: string,\n streamId: string,\n tracing: PropagationContext | undefined,\n fn: Fn,\n): ReturnType<Fn> {\n const ctx = tracing\n ? propagation.extract(context.active(), tracing)\n : context.active();\n\n return tracer.startActiveSpan<Fn>(\n `procedure handler ${serviceName}.${procedureName}`,\n {\n attributes: {\n component: 'river',\n 'river.method.kind': kind,\n 'river.method.service': serviceName,\n 'river.method.name': procedureName,\n 'river.streamId': streamId,\n 'span.kind': 'server',\n },\n kind: SpanKind.SERVER,\n },\n ctx,\n fn,\n );\n}\n\nconst tracer = trace.getTracer('river', RIVER_VERSION);\nexport default tracer;\n","{\n \"name\": \"@replit/river\",\n \"description\": \"It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!\",\n \"version\": \"0.200.0-rc.1\",\n \"type\": \"module\",\n \"exports\": {\n \".\": {\n \"import\": \"./dist/router/index.js\",\n \"require\": \"./dist/router/index.cjs\"\n },\n \"./logging\": {\n \"import\": \"./dist/logging/index.js\",\n \"require\": \"./dist/logging/index.cjs\"\n },\n \"./codec\": {\n \"import\": \"./dist/codec/index.js\",\n \"require\": \"./dist/codec/index.cjs\"\n },\n \"./transport\": {\n \"import\": \"./dist/transport/index.js\",\n \"require\": \"./dist/transport/index.cjs\"\n },\n \"./transport/ws/client\": {\n \"import\": \"./dist/transport/impls/ws/client.js\",\n \"require\": \"./dist/transport/impls/ws/client.cjs\"\n },\n \"./transport/ws/server\": {\n \"import\": \"./dist/transport/impls/ws/server.js\",\n \"require\": \"./dist/transport/impls/ws/server.cjs\"\n },\n \"./transport/uds/client\": {\n \"import\": \"./dist/transport/impls/uds/client.js\",\n \"require\": \"./dist/transport/impls/uds/client.cjs\"\n },\n \"./transport/uds/server\": {\n \"import\": \"./dist/transport/impls/uds/server.js\",\n \"require\": \"./dist/transport/impls/uds/server.cjs\"\n },\n \"./test-util\": {\n \"import\": \"./dist/util/testHelpers.js\",\n \"require\": \"./dist/util/testHelpers.cjs\"\n }\n },\n \"sideEffects\": [\n \"./dist/logging/index.js\"\n ],\n \"files\": [\n \"dist\"\n ],\n \"dependencies\": {\n \"@msgpack/msgpack\": \"^3.0.0-beta2\",\n \"nanoid\": \"^4.0.2\",\n \"ws\": \"^8.17.0\"\n },\n \"peerDependencies\": {\n \"@opentelemetry/api\": \"^1.7.0\",\n \"@sinclair/typebox\": \"~0.32.8\"\n },\n \"devDependencies\": {\n \"@opentelemetry/core\": \"^1.7.0\",\n \"@opentelemetry/sdk-trace-base\": \"^1.24.1\",\n \"@opentelemetry/sdk-trace-web\": \"^1.24.1\",\n \"@types/ws\": \"^8.5.5\",\n \"@typescript-eslint/eslint-plugin\": \"^7.8.0\",\n \"@typescript-eslint/parser\": \"^7.8.0\",\n \"@vitest/ui\": \"^1.3.1\",\n \"eslint\": \"^8.57.0\",\n \"eslint-config-prettier\": \"^9.1.0\",\n \"eslint-plugin-prettier\": \"^5.1.3\",\n \"prettier\": \"^3.0.0\",\n \"tsup\": \"^7.2.0\",\n \"typescript\": \"^5.4.5\",\n \"vitest\": \"^1.3.1\"\n },\n \"scripts\": {\n \"check\": \"tsc --noEmit && npm run format && npm run lint\",\n \"format\": \"npx prettier . --check\",\n \"format:fix\": \"npx prettier . --write\",\n \"lint\": \"eslint .\",\n \"lint:fix\": \"eslint . --fix\",\n \"fix\": \"npm run format:fix && npm run lint:fix\",\n \"build\": \"rm -rf dist && tsup && du -sh dist\",\n \"prepack\": \"npm run build\",\n \"release\": \"npm publish --access public\",\n \"test:ui\": \"echo \\\"remember to go to /__vitest__ in the webview\\\" && vitest --ui --api.host 0.0.0.0 --api.port 3000\",\n \"test\": \"vitest\",\n \"test:single\": \"vitest run --reporter=dot\",\n \"test:flake\": \"./flake.sh\",\n \"bench\": \"vitest bench\"\n },\n \"engines\": {\n \"node\": \">=16\"\n },\n \"keywords\": [\n \"rpc\",\n \"websockets\",\n \"jsonschema\"\n ],\n \"author\": \"Jacky Zhao\",\n \"license\": \"MIT\"\n}\n","export function coerceErrorString(err: unknown): string {\n if (err instanceof Error) {\n return err.message || 'unknown reason';\n }\n\n return `[coerced to error] ${String(err)}`;\n}\n","import { customAlphabet } from 'nanoid';\nimport {\n ControlFlags,\n ControlMessageAckSchema,\n OpaqueTransportMessage,\n PartialTransportMessage,\n TransportClientId,\n TransportMessage,\n} from './message';\nimport { Codec } from '../codec';\nimport { Logger, MessageMetadata } from '../logging/log';\nimport { Static } from '@sinclair/typebox';\nimport {\n PropagationContext,\n TelemetryInfo,\n createSessionTelemetryInfo,\n} from '../tracing';\nimport { SpanStatusCode } from '@opentelemetry/api';\n\nconst nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvxyz', 6);\nexport const unsafeId = () => nanoid();\n\ntype SequenceNumber = number;\n\n/**\n * A connection is the actual raw underlying transport connection.\n * It’s responsible for dispatching to/from the actual connection itself\n * This should be instantiated as soon as the client/server has a connection\n * It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)\n */\nexport abstract class Connection {\n id: string;\n telemetry?: TelemetryInfo;\n constructor() {\n this.id = `conn-${nanoid(12)}`; // for debugging, no collision safety needed\n }\n\n get loggingMetadata(): MessageMetadata {\n const metadata: MessageMetadata = { connId: this.id };\n const spanContext = this.telemetry?.span.spanContext();\n\n if (this.telemetry?.span.isRecording() && spanContext) {\n metadata.telemetry = {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n };\n }\n\n return metadata;\n }\n\n /**\n * Handle adding a callback for when a message is received.\n * @param msg The message that was received.\n */\n abstract addDataListener(cb: (msg: Uint8Array) => void): void;\n abstract removeDataListener(cb: (msg: Uint8Array) => void): void;\n\n /**\n * Handle adding a callback for when the connection is closed.\n * This should also be called if an error happens.\n * @param cb The callback to call when the connection is closed.\n */\n abstract addCloseListener(cb: () => void): void;\n\n /**\n * Handle adding a callback for when an error is received.\n * This should only be used for this.logging errors, all cleanup\n * should be delegated to addCloseListener.\n *\n * The implementer should take care such that the implemented\n * connection will call both the close and error callbacks\n * on an error.\n *\n * @param cb The callback to call when an error is received.\n */\n abstract addErrorListener(cb: (err: Error) => void): void;\n\n /**\n * Sends a message over the connection.\n * @param msg The message to send.\n * @returns true if the message was sent, false otherwise.\n */\n abstract send(msg: Uint8Array): boolean;\n\n /**\n * Closes the connection.\n */\n abstract close(): void;\n}\n\nexport interface SessionOptions {\n /**\n * Frequency at which to send heartbeat acknowledgements\n */\n heartbeatIntervalMs: number;\n /**\n * Number of elapsed heartbeats without a response message before we consider\n * the connection dead.\n */\n heartbeatsUntilDead: number;\n /**\n * Duration to wait between connection disconnect and actual session disconnect\n */\n sessionDisconnectGraceMs: number;\n /**\n * The codec to use for encoding/decoding messages over the wire\n */\n codec: Codec;\n}\n\n/**\n * A session is a higher-level abstraction that operates over the span of potentially multiple transport-level connections\n * - It’s responsible for tracking any metadata for a particular client that might need to be persisted across connections (i.e. the sendBuffer, ack, seq)\n * - This will only be considered disconnected if\n * - the server tells the client that we’ve reconnected but it doesn’t recognize us anymore (server definitely died) or\n * - we hit a grace period after a connection disconnect\n */\nexport class Session<ConnType extends Connection> {\n private codec: Codec;\n private options: SessionOptions;\n readonly telemetry: TelemetryInfo;\n\n /**\n * The buffer of messages that have been sent but not yet acknowledged.\n */\n private sendBuffer: Array<OpaqueTransportMessage> = [];\n\n /**\n * The active connection associated with this session\n */\n connection?: ConnType;\n /**\n * A connection that is currently undergoing handshaking. Used to distinguish between the active\n * connection, but still be able to close it if needed.\n */\n private handshakingConnection?: ConnType;\n readonly from: TransportClientId;\n readonly to: TransportClientId;\n\n /**\n * The unique ID of this session.\n */\n readonly id: string;\n\n /**\n * What the other side advertised as their session ID\n * for this session.\n */\n advertisedSessionId?: string;\n\n /**\n * Number of messages we've sent along this session (excluding handshake and acks)\n */\n private seq: SequenceNumber = 0;\n\n /**\n * Number of unique messages we've received this session (excluding handshake and acks)\n */\n private ack: SequenceNumber = 0;\n\n /**\n * The grace period between when the inner connection is disconnected\n * and when we should consider the entire session disconnected.\n */\n private disconnectionGrace?: ReturnType<typeof setTimeout>;\n\n /**\n * Number of heartbeats we've sent without a response.\n */\n private heartbeatMisses: number;\n\n /**\n * The interval for sending heartbeats.\n */\n private heartbeat: ReturnType<typeof setInterval>;\n private log?: Logger;\n\n constructor(\n conn: ConnType | undefined,\n from: TransportClientId,\n to: TransportClientId,\n options: SessionOptions,\n propagationCtx?: PropagationContext,\n ) {\n this.id = `session-${nanoid(12)}`;\n this.options = options;\n this.from = from;\n this.to = to;\n this.connection = conn;\n this.codec = options.codec;\n\n // setup heartbeat\n this.heartbeatMisses = 0;\n this.heartbeat = setInterval(\n () => this.sendHeartbeat(),\n options.heartbeatIntervalMs,\n );\n this.telemetry = createSessionTelemetryInfo(this, propagationCtx);\n }\n\n bindLogger(log: Logger) {\n this.log = log;\n }\n\n get loggingMetadata(): MessageMetadata {\n const spanContext = this.telemetry.span.spanContext();\n\n return {\n clientId: this.from,\n connectedTo: this.to,\n sessionId: this.id,\n connId: this.connection?.id,\n telemetry: {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n },\n };\n }\n\n /**\n * Sends a message over the session's connection.\n * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.\n *\n * @param msg The partial message to be sent, which will be constructed into a full message.\n * @param addToSendBuff Whether to add the message to the send buffer for retry.\n * @returns The full transport ID of the message that was attempted to be sent.\n */\n send(msg: PartialTransportMessage): string {\n const fullMsg: TransportMessage = this.constructMsg(msg);\n this.log?.debug(`sending msg`, {\n ...this.loggingMetadata,\n transportMessage: fullMsg,\n });\n\n if (this.connection) {\n const ok = this.connection.send(this.codec.toBuffer(fullMsg));\n if (ok) return fullMsg.id;\n this.log?.info(\n `failed to send msg to ${fullMsg.to}, connection is probably dead`,\n {\n ...this.loggingMetadata,\n transportMessage: fullMsg,\n },\n );\n } else {\n this.log?.debug(\n `buffering msg to ${fullMsg.to}, connection not ready yet`,\n { ...this.loggingMetadata, transportMessage: fullMsg },\n );\n }\n\n return fullMsg.id;\n }\n\n sendHeartbeat() {\n const misses = this.heartbeatMisses;\n const missDuration = misses * this.options.heartbeatIntervalMs;\n if (misses > this.options.heartbeatsUntilDead) {\n if (this.connection) {\n this.log?.info(\n `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,\n this.loggingMetadata,\n );\n this.telemetry.span.addEvent('closing connection due to inactivity');\n this.closeStaleConnection();\n }\n return;\n }\n\n this.send({\n streamId: 'heartbeat',\n controlFlags: ControlFlags.AckBit,\n payload: {\n type: 'ACK',\n } satisfies Static<typeof ControlMessageAckSchema>,\n });\n this.heartbeatMisses++;\n }\n\n resetBufferedMessages() {\n this.sendBuffer = [];\n this.seq = 0;\n this.ack = 0;\n }\n\n sendBufferedMessages(conn: ConnType) {\n this.log?.info(`resending ${this.sendBuffer.length} buffered messages`, {\n ...this.loggingMetadata,\n connId: conn.id,\n });\n for (const msg of this.sendBuffer) {\n this.log?.debug(`resending msg`, {\n ...this.loggingMetadata,\n transportMessage: msg,\n connId: conn.id,\n });\n const ok = conn.send(this.codec.toBuffer(msg));\n if (!ok) {\n // this should never happen unless the transport has an\n // incorrect implementation of `createNewOutgoingConnection`\n const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;\n conn.telemetry?.span.setStatus({\n code: SpanStatusCode.ERROR,\n message: errMsg,\n });\n\n this.log?.error(errMsg, {\n ...this.loggingMetadata,\n transportMessage: msg,\n connId: conn.id,\n tags: ['invariant-violation'],\n });\n conn.close();\n return;\n }\n }\n }\n\n updateBookkeeping(ack: number, seq: number) {\n if (seq + 1 < this.ack) {\n this.log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {\n ...this.loggingMetadata,\n tags: ['invariant-violation'],\n });\n return;\n }\n\n this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);\n this.ack = seq + 1;\n }\n\n private closeStaleConnection(conn?: ConnType) {\n if (this.connection === undefined || this.connection === conn) return;\n this.log?.info(\n `closing old inner connection from session to ${this.to}`,\n this.loggingMetadata,\n );\n this.connection.close();\n this.connection = undefined;\n }\n\n replaceWithNewConnection(newConn: ConnType, isTransparentReconnect: boolean) {\n this.closeStaleConnection(newConn);\n this.cancelGrace();\n if (isTransparentReconnect) {\n // only send the buffered messages if this is considered a transparent reconnect. there are\n // cases where the cient reconnects but with a different session id. for those cases we should\n // not send messages from a previous session.\n\n this.sendBufferedMessages(newConn);\n }\n this.connection = newConn;\n\n // we only call replaceWithNewConnection after\n // having successfully completed a handshake so we clear\n // it here\n this.handshakingConnection = undefined;\n }\n\n replaceWithNewHandshakingConnection(newConn: ConnType) {\n this.handshakingConnection = newConn;\n }\n\n beginGrace(cb: () => void) {\n this.log?.info(\n `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,\n this.loggingMetadata,\n );\n\n // Replace any old timeouts to prevent this from firing twice.\n this.cancelGrace();\n this.disconnectionGrace = setTimeout(() => {\n this.log?.info(\n `grace period for ${this.to} elapsed`,\n this.loggingMetadata,\n );\n cb();\n }, this.options.sessionDisconnectGraceMs);\n }\n\n // called on reconnect of the underlying session\n cancelGrace() {\n this.heartbeatMisses = 0;\n clearTimeout(this.disconnectionGrace);\n this.disconnectionGrace = undefined;\n }\n\n /**\n * Used to close the handshaking connection, if set.\n */\n closeHandshakingConnection(expectedHandshakingConn?: ConnType) {\n if (this.handshakingConnection === undefined) return;\n if (\n expectedHandshakingConn !== undefined &&\n this.handshakingConnection === expectedHandshakingConn\n ) {\n // If the handshaking connection is the expected one, don't close it.\n return;\n }\n this.handshakingConnection.close();\n this.handshakingConnection = undefined;\n }\n\n // closed when we want to discard the whole session\n // (i.e. shutdown or session disconnect)\n close() {\n this.closeStaleConnection();\n this.cancelGrace();\n this.resetBufferedMessages();\n clearInterval(this.heartbeat);\n }\n\n get connected() {\n return this.connection !== undefined;\n }\n\n get nextExpectedAck() {\n return this.seq;\n }\n\n get nextExpectedSeq() {\n return this.ack;\n }\n\n /**\n * Check that the peer's next expected seq number matches something that is in our send buffer\n * _or_ matches our actual next seq.\n */\n nextExpectedSeqInRange(nextExpectedSeq: number) {\n for (const msg of this.sendBuffer) {\n if (nextExpectedSeq === msg.seq) {\n return true;\n }\n }\n return nextExpectedSeq === this.seq;\n }\n\n // This is only used in tests to make the session misbehave.\n /* @internal */ advanceAckForTesting(by: number) {\n this.ack += by;\n }\n\n constructMsg<Payload>(\n partialMsg: PartialTransportMessage<Payload>,\n ): TransportMessage<Payload> {\n const msg = {\n ...partialMsg,\n id: unsafeId(),\n to: this.to,\n from: this.from,\n seq: this.seq,\n ack: this.ack,\n };\n\n this.seq++;\n this.sendBuffer.push(msg);\n return msg;\n }\n\n inspectSendBuffer(): ReadonlyArray<OpaqueTransportMessage> {\n return this.sendBuffer;\n }\n}\n","import { Codec } from './types';\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\n// Convert Uint8Array to base64\nfunction uint8ArrayToBase64(uint8Array: Uint8Array) {\n let binary = '';\n uint8Array.forEach((byte) => {\n binary += String.fromCharCode(byte);\n });\n return btoa(binary);\n}\n\n// Convert base64 to Uint8Array\nfunction base64ToUint8Array(base64: string) {\n const binaryString = atob(base64);\n const uint8Array = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n uint8Array[i] = binaryString.charCodeAt(i);\n }\n return uint8Array;\n}\n\ninterface Base64EncodedValue {\n $t: string;\n}\n\n/**\n * Naive JSON codec implementation using JSON.stringify and JSON.parse.\n * @type {Codec}\n */\nexport const NaiveJsonCodec: Codec = {\n toBuffer: (obj: object) => {\n return encoder.encode(\n JSON.stringify(obj, function replacer<\n T extends object,\n >(this: T, key: keyof T) {\n const val = this[key];\n if (val instanceof Uint8Array) {\n return { $t: uint8ArrayToBase64(val) } satisfies Base64EncodedValue;\n } else {\n return val;\n }\n }),\n );\n },\n fromBuffer: (buff: Uint8Array) => {\n try {\n const parsed = JSON.parse(\n decoder.decode(buff),\n function reviver(_key, val: unknown) {\n if ((val as Base64EncodedValue | undefined)?.$t) {\n return base64ToUint8Array((val as Base64EncodedValue).$t);\n } else {\n return val;\n }\n },\n ) as unknown;\n\n if (typeof parsed === 'object') return parsed;\n return null;\n } catch {\n return null;\n }\n },\n};\n","import { NaiveJsonCodec } from '../codec/json';\nimport { ConnectionRetryOptions } from './rateLimit';\nimport { SessionOptions } from './session';\n\nexport type TransportOptions = SessionOptions;\n\nexport type ProvidedTransportOptions = Partial<TransportOptions>;\n\nexport const defaultTransportOptions: TransportOptions = {\n heartbeatIntervalMs: 1_000,\n heartbeatsUntilDead: 2,\n sessionDisconnectGraceMs: 5_000,\n codec: NaiveJsonCodec,\n};\n\nexport type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;\n\nexport type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;\n\nconst defaultConnectionRetryOptions: ConnectionRetryOptions = {\n baseIntervalMs: 250,\n maxJitterMs: 200,\n maxBackoffMs: 32_000,\n attemptBudgetCapacity: 5,\n budgetRestoreIntervalMs: 200,\n};\n\nexport const defaultClientTransportOptions: ClientTransportOptions = {\n ...defaultTransportOptions,\n ...defaultConnectionRetryOptions,\n};\n\nexport type ServerTransportOptions = TransportOptions;\n\nexport type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;\n\nexport const defaultServerTransportOptions: ServerTransportOptions = {\n ...defaultTransportOptions,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAwC;;;ACAxC,qBAQO;AA6BA,IAAM,kBAAkB,oBAAK,MAAM;AAAA,EACxC,oBAAK,OAAO;AAAA,IACV,IAAI,oBAAK,QAAQ,KAAK;AAAA,I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U;AAAA,IACV,WAAW;AAAA,EACb,EACC,MAAM,CAAC,QAAiB,WAAW,OAAO,MAAM,eAAe,GAAG,CAAC,CAAC;AAEvE,SAAO,CAAC,UAAU,QAAQ,WAAW,MAAM;AAC7C;AAEO,SAAS,qBAMd,OACA,MACA,iBACA,UAA+B,aAAa,GACoB;AAChE,QAAM,aAAa,iBAA8B;AAEjD,SAAO,CAAC,QAAsB;AAC5B,SAAK,KACF;AAAA,MACC,SAAS,OAAO,SAAS,eAAe;AAAA,MACxC;AAAA,MACA,WAAW;AAAA,IACb,EACC,MAAM,CAAC,QAAiB,WAAW,OAAO,MAAM,eAAe,GAAG,CAAC,CAAC;AAEvE,WAAO,WAAW;AAAA,EACpB;AACF;AAEO,SAAS,eAOd,OACA,MACA,MACA,iBACA,UAA+B,aAAa,GAI5C;AACA,QAAM,YAAY,gBAAuB;AACzC,QAAM,SAAS,KACZ;AAAA,IACC,SAAS,OAAO,SAAS,eAAe;AAAA,IACxC,QAAQ,CAAC;AAAA,IACT,UAAU;AAAA,EACZ,EACC,MAAM,cAAc;AAEvB,SAAO,CAAC,UAAU,QAAQ,MAAM,MAAM;AACxC;AAEO,IAAM,oBAAoB,MAAM;AAErC,SAAO,QAAQ,aAAa,UACxB,oBAAgB,uBAAO,CAAC,KACxB,YAAQ,uBAAO,CAAC;AACtB;","names":["import_typebox","import_nanoid","import_api","NodeWs"]}
1
+ {"version":3,"sources":["../../util/testHelpers.ts","../../router/result.ts","../../router/streams.ts","../../router/procedures.ts","../../transport/message.ts","../../transport/id.ts","../../tracing/index.ts","../../package.json","../../util/stringify.ts","../../codec/json.ts","../../transport/options.ts","../../transport/sessionStateMachine/common.ts","../../transport/sessionStateMachine/SessionConnecting.ts","../../transport/sessionStateMachine/SessionNoConnection.ts","../../transport/sessionStateMachine/SessionWaitingForHandshake.ts","../../transport/sessionStateMachine/SessionHandshaking.ts","../../transport/sessionStateMachine/SessionConnected.ts","../../transport/sessionStateMachine/transitions.ts"],"sourcesContent":["import NodeWs, { WebSocketServer } from 'ws';\nimport http from 'node:http';\nimport {\n Err,\n Ok,\n PayloadType,\n Procedure,\n Result,\n ProcedureErrorSchemaType,\n InputReaderErrorSchema,\n OutputReaderErrorSchema,\n ServiceContext,\n ProcedureHandlerContext,\n UNCAUGHT_ERROR_CODE,\n} from '../router';\nimport { Static } from '@sinclair/typebox';\nimport { nanoid } from 'nanoid';\nimport net from 'node:net';\nimport {\n OpaqueTransportMessage,\n PartialTransportMessage,\n} from '../transport/message';\nimport { coerceErrorString } from './stringify';\nimport { Transport } from '../transport/transport';\nimport {\n ReadStream,\n ReadStreamImpl,\n WriteStream,\n WriteStreamImpl,\n} from '../router/streams';\nimport { WsLike } from '../transport/impls/ws/wslike';\nimport { defaultTransportOptions } from '../transport/options';\nimport { BaseErrorSchemaType } from '../router/result';\nimport { Connection } from '../transport/connection';\nimport {\n Session,\n SessionOptions,\n SessionState,\n} from '../transport/sessionStateMachine/common';\nimport { SessionStateGraph } from '../transport/sessionStateMachine';\n\n/**\n * Creates a WebSocket client that connects to a local server at the specified port.\n * This should only be used for testing.\n * @param port - The port number to connect to.\n * @returns A Promise that resolves to a WebSocket instance.\n */\nexport function createLocalWebSocketClient(port: number): WsLike {\n const sock = new NodeWs(`ws://localhost:${port}`);\n sock.binaryType = 'arraybuffer';\n\n return sock;\n}\n\n/**\n * Creates a WebSocket server instance using the provided HTTP server.\n * Only used as helper for testing.\n * @param server - The HTTP server instance to use for the WebSocket server.\n * @returns A Promise that resolves to the created WebSocket server instance.\n */\nexport function createWebSocketServer(server: http.Server) {\n return new WebSocketServer({ server });\n}\n\n/**\n * Starts listening on the given server and returns the automatically allocated port number.\n * This should only be used for testing.\n * @param server - The http server to listen on.\n * @returns A promise that resolves with the allocated port number.\n * @throws An error if a port cannot be allocated.\n */\nexport function onWsServerReady(server: http.Server): Promise<number> {\n return new Promise((resolve, reject) => {\n server.listen(() => {\n const addr = server.address();\n if (typeof addr === 'object' && addr) {\n resolve(addr.port);\n } else {\n reject(new Error(\"couldn't find a port to allocate\"));\n }\n });\n });\n}\n\nexport function onUdsServeReady(\n server: net.Server,\n path: string,\n): Promise<void> {\n return new Promise<void>((resolve) => {\n server.listen(path, resolve);\n });\n}\n\nexport function getIteratorFromStream<T, E extends Static<BaseErrorSchemaType>>(\n readStream: ReadStream<T, E>,\n) {\n return readStream[Symbol.asyncIterator]();\n}\n\n/**\n * Retrieves the next value from an async iterable iterator.\n * @param iter The async iterable iterator.\n * @returns A promise that resolves to the next value from the iterator.\n */\nexport async function iterNext<T>(iter: {\n next(): Promise<\n | {\n done: false;\n value: T;\n }\n | {\n done: true;\n value: undefined;\n }\n >;\n}) {\n return await iter.next().then((res) => res.value as T);\n}\n\nexport function payloadToTransportMessage<Payload>(\n payload: Payload,\n): PartialTransportMessage<Payload> {\n return {\n streamId: 'stream',\n controlFlags: 0,\n payload,\n };\n}\n\nexport function createDummyTransportMessage() {\n return payloadToTransportMessage({\n msg: 'cool',\n test: Math.random(),\n });\n}\n\n/**\n * Waits for a message on the transport.\n * @param {Transport} t - The transport to listen to.\n * @param filter - An optional filter function to apply to the received messages.\n * @returns A promise that resolves with the payload of the first message that passes the filter.\n */\nexport async function waitForMessage(\n t: Transport<Connection>,\n filter?: (msg: OpaqueTransportMessage) => boolean,\n rejectMismatch?: boolean,\n) {\n return new Promise((resolve, reject) => {\n function cleanup() {\n t.removeEventListener('message', onMessage);\n }\n\n function onMessage(msg: OpaqueTransportMessage) {\n if (!filter || filter(msg)) {\n cleanup();\n resolve(msg.payload);\n } else if (rejectMismatch) {\n cleanup();\n reject(new Error('message didnt match the filter'));\n }\n }\n\n t.addEventListener('message', onMessage);\n });\n}\n\nfunction catchProcError(err: unknown) {\n const errorMsg = coerceErrorString(err);\n return Err({ code: UNCAUGHT_ERROR_CODE, message: errorMsg });\n}\n\nexport const testingSessionOptions: SessionOptions = defaultTransportOptions;\n\nexport function dummySession() {\n return SessionStateGraph.entrypoints.NoConnection(\n 'client',\n 'server',\n {\n onSessionGracePeriodElapsed: () => {\n /* noop */\n },\n },\n testingSessionOptions,\n );\n}\n\nfunction dummyCtx<State>(\n state: State,\n session: Session<Connection>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n): ProcedureHandlerContext<State> {\n return {\n ...extendedContext,\n state,\n sessionId: session.id,\n from: session.from,\n metadata: {},\n abortController: new AbortController(),\n clientAbortSignal: new AbortController().signal,\n onRequestFinished: () => undefined,\n };\n}\n\nexport function asClientRpc<\n State extends object,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'rpc', Init, null, Output, Err>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n) {\n return async (\n msg: Static<Init>,\n ): Promise<\n Result<Static<Output>, Static<Err> | Static<typeof OutputReaderErrorSchema>>\n > => {\n return proc\n .handler(dummyCtx(state, session, extendedContext), msg)\n .catch(catchProcError);\n };\n}\n\nfunction createOutputPipe<\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(): {\n reader: ReadStream<\n Static<Output>,\n Static<Err> | Static<typeof OutputReaderErrorSchema>\n >;\n writer: WriteStream<Result<Static<Output>, Static<Err>>>;\n} {\n const reader = new ReadStreamImpl<\n Static<Output>,\n Static<Err> | Static<typeof OutputReaderErrorSchema>\n >(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n writer.triggerCloseRequest();\n });\n });\n const writer = new WriteStreamImpl<Result<Static<Output>, Static<Err>>>(\n (v) => {\n reader.pushValue(v);\n },\n );\n writer.onClose(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n reader.triggerClose();\n });\n });\n\n return { reader, writer };\n}\n\nfunction createInputPipe<Input extends PayloadType>(): {\n reader: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>;\n writer: WriteStream<Static<Input>>;\n} {\n const reader = new ReadStreamImpl<\n Static<Input>,\n Static<typeof InputReaderErrorSchema>\n >(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n writer.triggerCloseRequest();\n });\n });\n const writer = new WriteStreamImpl<Static<Input>>((v) => {\n reader.pushValue(Ok(v));\n });\n writer.onClose(() => {\n // Make it async to simulate request going over the wire\n // using promises so that we don't get affected by fake timers.\n void Promise.resolve().then(() => {\n reader.triggerClose();\n });\n });\n\n return { reader, writer };\n}\n\nexport function asClientStream<\n State extends object,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'stream', Init, Input, Output, Err>,\n init?: Static<Init>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): [WriteStream<Static<Input>>, ReadStream<Static<Output>, Static<Err>>] {\n const inputPipe = createInputPipe<Input>();\n const outputPipe = createOutputPipe<Output, Err>();\n\n void proc\n .handler(\n dummyCtx(state, session, extendedContext),\n init ?? {},\n inputPipe.reader,\n outputPipe.writer,\n )\n .catch((err: unknown) => outputPipe.writer.write(catchProcError(err)));\n\n return [inputPipe.writer, outputPipe.reader];\n}\n\nexport function asClientSubscription<\n State extends object,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'subscription', Init, null, Output, Err>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): (msg: Static<Init>) => ReadStream<Static<Output>, Static<Err>> {\n const outputPipe = createOutputPipe<Output, Err>();\n\n return (msg: Static<Init>) => {\n void proc\n .handler(\n dummyCtx(state, session, extendedContext),\n msg,\n outputPipe.writer,\n )\n .catch((err: unknown) => outputPipe.writer.write(catchProcError(err)));\n\n return outputPipe.reader;\n };\n}\n\nexport function asClientUpload<\n State extends object,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(\n state: State,\n proc: Procedure<State, 'upload', Init, Input, Output, Err>,\n init?: Static<Init>,\n extendedContext?: Omit<ServiceContext, 'state'>,\n session: Session<Connection> = dummySession(),\n): [\n WriteStream<Static<Input>>,\n () => Promise<Result<Static<Output>, Static<Err>>>,\n] {\n const inputPipe = createInputPipe<Input>();\n const result = proc\n .handler(\n dummyCtx(state, session, extendedContext),\n init ?? {},\n inputPipe.reader,\n )\n .catch(catchProcError);\n\n return [inputPipe.writer, () => result];\n}\n\nexport const getUnixSocketPath = () => {\n return `/tmp/${nanoid()}.sock`;\n};\n\nexport function getTransportConnections<ConnType extends Connection>(\n transport: Transport<ConnType>,\n): Array<ConnType> {\n const connections = [];\n for (const session of transport.sessions.values()) {\n if (session.state === SessionState.Connected) {\n connections.push(session.conn);\n }\n }\n\n return connections;\n}\n\nexport function numberOfConnections<ConnType extends Connection>(\n transport: Transport<ConnType>,\n): number {\n return getTransportConnections(transport).length;\n}\n\nexport function closeAllConnections<ConnType extends Connection>(\n transport: Transport<ConnType>,\n) {\n for (const conn of getTransportConnections(transport)) {\n conn.close();\n }\n}\n","import {\n Static,\n TLiteral,\n TObject,\n TSchema,\n TString,\n TUnion,\n Type,\n} from '@sinclair/typebox';\nimport { Client } from './client';\nimport { ReadStream } from './streams';\n\ntype TLiteralString = TLiteral<string>;\n\nexport type BaseErrorSchemaType =\n | TObject<{\n code: TLiteralString | TUnion<Array<TLiteralString>>;\n message: TLiteralString | TString;\n }>\n | TObject<{\n code: TLiteralString | TUnion<Array<TLiteralString>>;\n message: TLiteralString | TString;\n extras: TSchema;\n }>;\n\n/**\n * Takes in a specific error schema and returns a result schema the error\n */\nexport const ErrResultSchema = <T extends BaseErrorSchemaType>(t: T) =>\n Type.Object({\n ok: Type.Literal(false),\n payload: t,\n });\n\n/**\n * AnyResultSchema is a schema to validate any result.\n */\nexport const AnyResultSchema = Type.Union([\n Type.Object({\n ok: Type.Literal(false),\n payload: Type.Object({\n code: Type.String(),\n message: Type.String(),\n extras: Type.Optional(Type.Unknown()),\n }),\n }),\n\n Type.Object({\n ok: Type.Literal(true),\n payload: Type.Unknown(),\n }),\n]);\n\nexport interface OkResult<T> {\n ok: true;\n payload: T;\n}\nexport interface ErrResult<Err extends Static<BaseErrorSchemaType>> {\n ok: false;\n payload: Err;\n}\nexport type Result<T, Err extends Static<BaseErrorSchemaType>> =\n | OkResult<T>\n | ErrResult<Err>;\n\nexport function Ok<const T extends Array<unknown>>(p: T): OkResult<T>;\nexport function Ok<const T extends ReadonlyArray<unknown>>(p: T): OkResult<T>;\nexport function Ok<const T>(payload: T): OkResult<T>;\nexport function Ok<const T>(payload: T): OkResult<T> {\n return {\n ok: true,\n payload,\n };\n}\n\nexport function Err<const Err extends Static<BaseErrorSchemaType>>(\n error: Err,\n): ErrResult<Err> {\n return {\n ok: false,\n payload: error,\n };\n}\n\n/**\n * Refine a {@link Result} type to its returned payload.\n */\nexport type ResultUnwrapOk<R> = R extends Result<infer T, infer __E>\n ? T\n : never;\n\n/**\n * Unwrap a {@link Result} type and return the payload if successful,\n * otherwise throws an error.\n * @param result - The result to unwrap.\n * @throws Will throw an error if the result is not ok.\n */\nexport function unwrap<T, Err extends Static<BaseErrorSchemaType>>(\n result: Result<T, Err>,\n): T {\n if (result.ok) {\n return result.payload;\n }\n\n throw new Error(\n `Cannot non-ok result, got: ${result.payload.code} - ${result.payload.message}`,\n );\n}\n\n/**\n * Refine a {@link Result} type to its error payload.\n */\nexport type ResultUnwrapErr<R> = R extends Result<infer __T, infer Err>\n ? Err\n : never;\n\n/**\n * Retrieve the output type for a procedure, represented as a {@link Result}\n * type.\n * Example:\n * ```\n * type Message = Output<typeof client, 'serviceName', 'procedureName'>\n * ```\n */\nexport type Output<\n RiverClient,\n ServiceName extends keyof RiverClient,\n ProcedureName extends keyof RiverClient[ServiceName],\n Procedure = RiverClient[ServiceName][ProcedureName],\n Fn extends (...args: never) => unknown = (...args: never) => unknown,\n> = RiverClient extends Client<infer __ServiceSchemaMap>\n ? Procedure extends object\n ? Procedure extends object & { rpc: infer RpcHandler extends Fn }\n ? Awaited<ReturnType<RpcHandler>>\n : Procedure extends object & { upload: infer UploadHandler extends Fn }\n ? ReturnType<UploadHandler> extends [\n infer __UploadInputMessage,\n (...args: never) => Promise<infer UploadOutputMessage>,\n ]\n ? UploadOutputMessage\n : never\n : Procedure extends object & { stream: infer StreamHandler extends Fn }\n ? ReturnType<StreamHandler> extends [\n infer __StreamInputMessage,\n ReadStream<infer StreamOutputMessage, Static<BaseErrorSchemaType>>,\n ]\n ? StreamOutputMessage\n : never\n : Procedure extends object & {\n subscribe: infer SubscriptionHandler extends Fn;\n }\n ? Awaited<ReturnType<SubscriptionHandler>> extends ReadStream<\n infer SubscriptionOutputMessage,\n Static<BaseErrorSchemaType>\n >\n ? SubscriptionOutputMessage\n : never\n : never\n : never\n : never;\n","import { Static } from '@sinclair/typebox';\nimport { BaseErrorSchemaType, Err, Result } from './result';\n\nexport const StreamDrainedError = {\n code: 'STREAM_DRAINED',\n message: 'Stream was drained',\n} as const;\n\ntype ReadStreamResult<T, E extends Static<BaseErrorSchemaType>> = Result<\n T,\n E | typeof StreamDrainedError\n>;\n\n/**\n * A `ReadStream` represents a stream of data.\n *\n * This stream is not closable by the reader, the reader must wait for\n * the writer to close it.\n *\n * The stream can only be locked (aka consumed) once and will remain\n * locked, trying to lock the stream again will throw an TypeError.\n */\nexport interface ReadStream<T, E extends Static<BaseErrorSchemaType>> {\n /**\n * Stream implements AsyncIterator API and can be consumed via\n * for-await-of loops.\n *\n */\n [Symbol.asyncIterator](): {\n next(): Promise<\n | {\n done: false;\n value: ReadStreamResult<T, E>;\n }\n | {\n done: true;\n value: undefined;\n }\n >;\n };\n /**\n * `asArray` locks the stream and returns a promise that resolves\n * with an array of the stream's content when the stream is closed.\n *\n */\n asArray(): Promise<Array<ReadStreamResult<T, E>>>;\n /**\n * `drain` locks the stream and discards any existing or future data.\n *\n * If there is an existing Promise waiting for the next value,\n * `drain` causes it to resolve with a {@link StreamDrainedError} error.\n */\n drain(): undefined;\n /**\n * `isLocked` returns true if the stream is locked.\n */\n isLocked(): boolean;\n /**\n * `onClose` registers a callback that will be called when the stream\n * is closed. Returns a function that can be used to unregister the\n * listener.\n */\n onClose(cb: () => void): () => void;\n /**\n * `isClosed` returns true if the stream was closed by the writer.\n *\n * Note that the stream can still have queued data and can still be\n * consumed.\n */\n isClosed(): boolean;\n /**\n * `requestClose` sends a request to the writer to close the stream,\n * and resolves the writer closes its end of the stream..\n *\n * The stream can still receive more data after the request is sent. If you\n * no longer wish to use the stream, make sure to call `drain`.\n */\n requestClose(): Promise<undefined>;\n /**\n * `isCloseRequested` checks if the reader has requested to close the stream.\n */\n isCloseRequested(): boolean;\n}\n\n/**\n * A `WriteStream` is a streams that can be written to.\n */\nexport interface WriteStream<T> {\n /**\n * `write` writes a value to the stream. An error is thrown if writing to a closed stream.\n */\n write(value: T): undefined;\n /**\n * `close` signals the closure of the write stream, informing the reader that the stream is complete.\n * Calling close multiple times has no effect.\n */\n close(): undefined;\n /**\n * `isCloseRequested` checks if the reader has requested to close the stream.\n */\n isCloseRequested(): boolean;\n /**\n * `onCloseRequest` registers a callback that will be called when the stream's\n * reader requests a close. Returns a function that can be used to unregister the\n * listener.\n */\n onCloseRequest(cb: () => void): () => void;\n /**\n * `isClosed` returns true if the stream was closed by the writer.\n */\n isClosed(): boolean;\n /**\n * `onClose` registers a callback that will be called when the stream\n * is closed. Returns a function that can be used to unregister the\n * listener.\n */\n onClose(cb: () => void): () => void;\n}\n\n/**\n * Internal implementation of a `ReadStream`.\n * This won't be exposed as an interface to river\n * consumers directly, it has internal river methods\n * to pushed data to the stream and close it.\n */\nexport class ReadStreamImpl<T, E extends Static<BaseErrorSchemaType>>\n implements ReadStream<T, E>\n{\n /**\n * Whether the stream is closed.\n */\n private closed = false;\n /**\n * A list of listeners that will be called when the stream is closed.\n */\n private onCloseListeners: Set<() => void>;\n /**\n * Whether the user has requested to close the stream.\n */\n private closeRequested = false;\n /**\n * Used to signal to the outside world that the user has requested to close the stream.\n */\n private closeRequestCallback: () => void;\n /**\n * Whether the stream is locked.\n */\n private locked = false;\n /**\n * Whether drain was called.\n */\n private drained = false;\n /**\n * This flag allows us to avoid cases where drain was called,\n * but the stream is fully consumed and closed. We don't need\n * to signal that drain was closed.\n */\n private didDrainDisposeValues = false;\n /**\n * A list of values that have been pushed to the stream but not yet emitted to the user.\n */\n private queue: Array<ReadStreamResult<T, E>> = [];\n /**\n * Used by methods in the class to signal to the iterator that it\n * should check for the next value.\n */\n private nextPromise: Promise<void> | null = null;\n /**\n * Resolves nextPromise\n */\n private resolveNextPromise: null | (() => void) = null;\n\n constructor(closeRequestCallback: () => void) {\n this.closeRequestCallback = closeRequestCallback;\n this.onCloseListeners = new Set();\n }\n\n public [Symbol.asyncIterator]() {\n if (this.isLocked()) {\n throw new TypeError('ReadStream is already locked');\n }\n\n // first iteration with drain signals an error, the following one signals end of iteration.\n let didSignalDrain = false;\n this.locked = true;\n\n return {\n next: async () => {\n if (this.drained && didSignalDrain) {\n return {\n done: true,\n value: undefined,\n } as const;\n }\n\n // Wait until we have something in the queue\n while (this.queue.length === 0) {\n if (this.isClosed() && !this.didDrainDisposeValues) {\n return {\n done: true,\n value: undefined,\n } as const;\n }\n\n if (this.drained) {\n didSignalDrain = true;\n\n return {\n done: false,\n value: Err(StreamDrainedError),\n } as const;\n }\n\n if (!this.nextPromise) {\n this.nextPromise = new Promise<void>((resolve) => {\n this.resolveNextPromise = resolve;\n });\n }\n\n await this.nextPromise;\n this.nextPromise = null;\n this.resolveNextPromise = null;\n }\n\n // Unfortunately we have to use non-null assertion here, because T can be undefined\n // we already check for array length above anyway\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = this.queue.shift()!;\n\n return { done: false, value } as const;\n },\n return: async () => {\n this.drain();\n return { done: true, value: undefined } as const;\n },\n };\n }\n\n public async asArray(): Promise<Array<ReadStreamResult<T, E>>> {\n const array: Array<ReadStreamResult<T, E>> = [];\n for await (const value of this) {\n array.push(value);\n }\n\n return array;\n }\n\n public drain(): undefined {\n if (this.drained) {\n return;\n }\n\n this.locked = true;\n this.drained = true;\n this.didDrainDisposeValues = this.queue.length > 0;\n this.queue.length = 0;\n\n this.resolveNextPromise?.();\n }\n\n public isClosed(): boolean {\n return this.closed;\n }\n\n public isLocked(): boolean {\n return this.locked;\n }\n\n public onClose(cb: () => void): () => void {\n if (this.isClosed()) {\n throw new Error('Stream is already closed');\n }\n\n this.onCloseListeners.add(cb);\n\n return () => {\n this.onCloseListeners.delete(cb);\n };\n }\n\n public requestClose(): Promise<undefined> {\n if (this.isClosed()) {\n throw new Error('Cannot request close after stream already closed');\n }\n\n if (!this.closeRequested) {\n this.closeRequested = true;\n this.closeRequestCallback();\n }\n\n return new Promise<undefined>((resolve) => {\n this.onClose(() => {\n resolve(undefined);\n });\n });\n }\n\n public isCloseRequested(): boolean {\n return this.closeRequested;\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Pushes a value to the stream.\n */\n public pushValue(value: Result<T, E>): undefined {\n if (this.drained) {\n return;\n }\n\n if (this.closed) {\n throw new Error('Cannot push to closed stream');\n }\n\n this.queue.push(value);\n this.resolveNextPromise?.();\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Triggers the close of the stream. Make sure to push all remaining\n * values before calling this method.\n */\n public triggerClose(): undefined {\n if (this.isClosed()) {\n throw new Error('Unexpected closing multiple times');\n }\n\n this.closed = true;\n this.resolveNextPromise?.();\n this.onCloseListeners.forEach((cb) => cb());\n this.onCloseListeners.clear();\n\n // TODO maybe log a warn if after a certain amount of time\n // the queue was not fully consumed\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n */\n public hasValuesInQueue(): boolean {\n return this.queue.length > 0;\n }\n}\n\n/**\n * Internal implementation of a `WriteStream`.\n * This won't be exposed as an interface to river\n * consumers directly, it has internal river methods\n * to trigger a close request, a way to pass on close\n * signals, and a way to push data to the stream.\n */\nexport class WriteStreamImpl<T> implements WriteStream<T> {\n /**\n * Passed via constructor to pass on write requests\n */\n private writeCb: (value: T) => void;\n /**\n * Whether the stream is closed.\n */\n private closed = false;\n /**\n * A list of listeners that will be called when the stream is closed.\n */\n private onCloseListeners: Set<() => void>;\n /**\n * Whether the reader has requested to close the stream.\n */\n private closeRequested = false;\n /**\n * A list of listeners that will be called when a close request is triggered.\n */\n private onCloseRequestListeners: Set<() => void>;\n\n constructor(writeCb: (value: T) => void) {\n this.writeCb = writeCb;\n this.onCloseListeners = new Set();\n this.onCloseRequestListeners = new Set();\n }\n\n public write(value: T): undefined {\n if (this.isClosed()) {\n throw new Error('Cannot write to closed stream');\n }\n\n this.writeCb(value);\n }\n\n public isClosed(): boolean {\n return this.closed;\n }\n\n onClose(cb: () => void): () => void {\n if (this.isClosed()) {\n cb();\n\n return () => undefined;\n }\n\n this.onCloseListeners.add(cb);\n\n return () => this.onCloseListeners.delete(cb);\n }\n\n public close(): undefined {\n if (this.isClosed()) {\n return;\n }\n\n this.closed = true;\n this.onCloseListeners.forEach((cb) => cb());\n\n // cleanup\n this.onCloseListeners.clear();\n this.onCloseRequestListeners.clear();\n this.writeCb = () => undefined;\n }\n\n public isCloseRequested(): boolean {\n return this.closeRequested;\n }\n\n public onCloseRequest(cb: () => void): () => void {\n if (this.isClosed()) {\n throw new Error('Stream is already closed');\n }\n\n if (this.isCloseRequested()) {\n cb();\n\n return () => undefined;\n }\n\n this.onCloseRequestListeners.add(cb);\n\n return () => this.onCloseRequestListeners.delete(cb);\n }\n\n /**\n * @internal meant for use within river, not exposed as a public API\n *\n * Triggers a close request.\n */\n public triggerCloseRequest(): undefined {\n if (this.isCloseRequested()) {\n throw new Error('Cannot trigger close request multiple times');\n }\n\n if (this.isClosed()) {\n throw new Error('Cannot trigger close request on closed stream');\n }\n\n this.closeRequested = true;\n this.onCloseRequestListeners.forEach((cb) => cb());\n this.onCloseRequestListeners.clear();\n }\n}\n","/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\nimport { Static, TNever, TSchema, TUnion, Type } from '@sinclair/typebox';\nimport { ProcedureHandlerContext } from './context';\nimport { BaseErrorSchemaType, Result } from './result';\nimport { ReadStream, WriteStream } from './streams';\n\n/**\n * Brands a type to prevent it from being directly constructed.\n */\nexport type Branded<T> = T & { readonly __BRAND_DO_NOT_USE: unique symbol };\n\n/**\n * Unbrands a {@link Branded} type.\n */\nexport type Unbranded<T> = T extends Branded<infer U> ? U : never;\n\n/**\n * The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a\n * different type for the very first initialization message. The suffixless types correspond to\n * gRPC's four combinations of stream / non-stream in each direction.\n */\nexport type ValidProcType =\n // Single message in both directions (1:1).\n | 'rpc'\n // Client-stream single message from server (n:1).\n | 'upload'\n // Single message from client, stream from server (1:n).\n | 'subscription'\n // Bidirectional stream (n:n).\n | 'stream';\n\n/**\n * Represents the payload type for {@link Procedure}s.\n */\nexport type PayloadType = TSchema;\n\n/**\n * INTERNAL_RIVER_ERROR_CODE is the code that is used when an internal error occurs,\n * this means that some invariants expected by the river server implementation have\n * been violated. When encountering this error please report this to river maintainers.\n */\nexport const INTERNAL_RIVER_ERROR_CODE = 'INTERNAL_RIVER_ERROR';\n/**\n * UNCAUGHT_ERROR_CODE is the code that is used when an error is thrown\n * inside a procedure handler that's not required.\n */\nexport const UNCAUGHT_ERROR_CODE = 'UNCAUGHT_ERROR' as const;\n/**\n * UNEXPECTED_DISCONNECT_CODE is the code used the stream's session\n * disconnect unexpetedly.\n */\nexport const UNEXPECTED_DISCONNECT_CODE = 'UNEXPECTED_DISCONNECT' as const;\n/**\n * INVALID_REQUEST_CODE is the code used when a client's request is invalid.\n */\nexport const INVALID_REQUEST_CODE = 'INVALID_REQUEST' as const;\n/**\n * ABORT_CODE is the code used when either server or client aborts the stream.\n */\nexport const ABORT_CODE = 'ABORT' as const;\n\n/**\n * OutputReaderErrorSchema is the schema for all the errors that can be\n * emitted in the Output ReadStream on the client.\n */\nexport const OutputReaderErrorSchema = Type.Object({\n code: Type.Union([\n Type.Literal(INTERNAL_RIVER_ERROR_CODE),\n Type.Literal(UNCAUGHT_ERROR_CODE),\n Type.Literal(UNEXPECTED_DISCONNECT_CODE),\n Type.Literal(INVALID_REQUEST_CODE),\n Type.Literal(ABORT_CODE),\n ]),\n message: Type.String(),\n});\n\n/**\n * InputReaderErrorSchema is the schema for all the errors that can be\n * emitted in the Input ReadStream on the server.\n */\nexport const InputReaderErrorSchema = Type.Object({\n code: Type.Union([\n Type.Literal(UNCAUGHT_ERROR_CODE),\n Type.Literal(UNEXPECTED_DISCONNECT_CODE),\n Type.Literal(INVALID_REQUEST_CODE),\n Type.Literal(ABORT_CODE),\n ]),\n message: Type.String(),\n});\n\n/**\n * Represents an acceptable schema to pass to a procedure.\n * Just a type of a schema, not an actual schema.\n */\nexport type ProcedureErrorSchemaType =\n | TUnion<Array<BaseErrorSchemaType>>\n | BaseErrorSchemaType\n | TNever;\n\n/**\n * Procedure for a single message in both directions (1:1).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface RpcProcedure<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'rpc';\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n ): Promise<Result<Static<Output>, Static<Err>>>;\n}\n\n/**\n * Procedure for a client-stream (potentially preceded by an initialization message),\n * single message from server (n:1).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Input - The TypeBox schema of the input object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface UploadProcedure<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'upload';\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>,\n ): Promise<Result<Static<Output>, Static<Err>>>;\n}\n\n/**\n * Procedure for a single message from client, stream from server (1:n).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface SubscriptionProcedure<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'subscription';\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n output: WriteStream<Result<Static<Output>, Static<Err>>>,\n ): Promise<void | undefined>;\n}\n\n/**\n * Procedure for a bidirectional stream (potentially preceded by an initialization message),\n * (n:n).\n *\n * @template State - The context state object.\n * @template Init - The TypeBox schema of the initialization object.\n * @template Input - The TypeBox schema of the input object.\n * @template Output - The TypeBox schema of the output object.\n * @template Err - The TypeBox schema of the error object.\n */\nexport interface StreamProcedure<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> {\n type: 'stream';\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler(\n context: ProcedureHandlerContext<State>,\n init: Static<Init>,\n input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>,\n output: WriteStream<Result<Static<Output>, Static<Err>>>,\n ): Promise<void | undefined>;\n}\n\n/**\n * Defines a Procedure type that can be a:\n * - {@link RpcProcedure} for a single message in both directions (1:1)\n * - {@link UploadProcedure} for a client-stream (potentially preceded by an\n * initialization message)\n * - {@link SubscriptionProcedure} for a single message from client, stream from server (1:n)\n * - {@link StreamProcedure} for a bidirectional stream (potentially preceded by an\n * initialization message)\n *\n * @template State - The TypeBox schema of the state object.\n * @template Ty - The type of the procedure.\n * @template Input - The TypeBox schema of the input object.\n * @template Init - The TypeBox schema of the input initialization object, if any.\n * @template Output - The TypeBox schema of the output object.\n */\nexport type Procedure<\n State,\n Ty extends ValidProcType,\n Init extends PayloadType,\n Input extends PayloadType | null,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n> = { type: Ty } & (Input extends PayloadType\n ? Ty extends 'upload'\n ? UploadProcedure<State, Init, Input, Output, Err>\n : Ty extends 'stream'\n ? StreamProcedure<State, Init, Input, Output, Err>\n : never\n : Ty extends 'rpc'\n ? RpcProcedure<State, Init, Output, Err>\n : Ty extends 'subscription'\n ? SubscriptionProcedure<State, Init, Output, Err>\n : never);\n\n/**\n * Represents any {@link Procedure} type.\n *\n * @template State - The context state object. You can provide this to constrain\n * the type of procedures.\n */\nexport type AnyProcedure<State = object> = Procedure<\n State,\n ValidProcType,\n PayloadType,\n PayloadType | null,\n PayloadType,\n ProcedureErrorSchemaType\n>;\n\n/**\n * Represents a map of {@link Procedure}s.\n *\n * @template State - The context state object. You can provide this to constrain\n * the type of procedures.\n */\nexport type ProcedureMap<State = object> = Record<string, AnyProcedure<State>>;\n\n// typescript is funky so with these upcoming procedure constructors, the overloads\n// which handle the `init` case _must_ come first, otherwise the `init` property\n// is not recognized as optional, for some reason\n\n/**\n * Creates an {@link RpcProcedure}.\n */\n// signature: default errors\nfunction rpc<State, Init extends PayloadType, Output extends PayloadType>(def: {\n init: Init;\n output: Output;\n errors?: never;\n description?: string;\n handler: RpcProcedure<State, Init, Output, TNever>['handler'];\n}): Branded<RpcProcedure<State, Init, Output, TNever>>;\n\n// signature: explicit errors\nfunction rpc<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler: RpcProcedure<State, Init, Output, Err>['handler'];\n}): Branded<RpcProcedure<State, Init, Output, Err>>;\n\n// implementation\nfunction rpc({\n init,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: RpcProcedure<\n object,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n ...(description ? { description } : {}),\n type: 'rpc',\n init,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates an {@link UploadProcedure}, optionally with an initialization message.\n */\n// signature: init with default errors\nfunction upload<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors?: never;\n description?: string;\n handler: UploadProcedure<State, Init, Input, Output, TNever>['handler'];\n}): Branded<UploadProcedure<State, Init, Input, Output, TNever>>;\n\n// signature: init with explicit errors\nfunction upload<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler: UploadProcedure<State, Init, Input, Output, Err>['handler'];\n}): Branded<UploadProcedure<State, Init, Input, Output, Err>>;\n\n// implementation\nfunction upload({\n init,\n input,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n input: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: UploadProcedure<\n object,\n PayloadType,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'upload',\n ...(description ? { description } : {}),\n init,\n input,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates a {@link SubscriptionProcedure}.\n */\n// signature: default errors\nfunction subscription<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n output: Output;\n errors?: never;\n description?: string;\n handler: SubscriptionProcedure<State, Init, Output, TNever>['handler'];\n}): Branded<SubscriptionProcedure<State, Init, Output, TNever>>;\n\n// signature: explicit errors\nfunction subscription<\n State,\n Init extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n output: Output;\n errors: Err;\n description?: string;\n handler: SubscriptionProcedure<State, Init, Output, Err>['handler'];\n}): Branded<SubscriptionProcedure<State, Init, Output, Err>>;\n\n// implementation\nfunction subscription({\n init,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: SubscriptionProcedure<\n object,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'subscription',\n ...(description ? { description } : {}),\n init,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Creates a {@link StreamProcedure}, optionally with an initialization message.\n */\n// signature: with default errors\nfunction stream<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors?: never;\n description?: string;\n handler: StreamProcedure<State, Init, Input, Output, TNever>['handler'];\n}): Branded<StreamProcedure<State, Init, Input, Output, TNever>>;\n\n// signature: explicit errors\nfunction stream<\n State,\n Init extends PayloadType,\n Input extends PayloadType,\n Output extends PayloadType,\n Err extends ProcedureErrorSchemaType,\n>(def: {\n init: Init;\n input: Input;\n output: Output;\n errors: Err;\n description?: string;\n handler: StreamProcedure<State, Init, Input, Output, Err>['handler'];\n}): Branded<StreamProcedure<State, Init, Input, Output, Err>>;\n\n// implementation\nfunction stream({\n init,\n input,\n output,\n errors = Type.Never(),\n description,\n handler,\n}: {\n init: PayloadType;\n input: PayloadType;\n output: PayloadType;\n errors?: ProcedureErrorSchemaType;\n description?: string;\n handler: StreamProcedure<\n object,\n PayloadType,\n PayloadType,\n PayloadType,\n ProcedureErrorSchemaType\n >['handler'];\n}) {\n return {\n type: 'stream',\n ...(description ? { description } : {}),\n init,\n input,\n output,\n errors,\n handler,\n };\n}\n\n/**\n * Holds the {@link Procedure} creation functions. Use these to create\n * procedures for services. You aren't allowed to create procedures directly.\n */\nexport const Procedure = {\n rpc,\n upload,\n subscription,\n stream,\n};\n","import { Type, TSchema, Static } from '@sinclair/typebox';\nimport { PropagationContext } from '../tracing';\nimport { generateId } from './id';\nimport {\n ErrResult,\n InputReaderErrorSchema,\n OutputReaderErrorSchema,\n} from '../router';\n\n/**\n * Control flags for transport messages.\n */\nexport const enum ControlFlags {\n /**\n * Used in heartbeat messages.\n */\n AckBit = 0b00001,\n /**\n * Used in stream open requests.\n */\n StreamOpenBit = 0b00010,\n /**\n * Used when writer closes the stream.\n */\n StreamClosedBit = 0b01000,\n /**\n * Used when readers no longer wish to receive messages.\n */\n StreamCloseRequestBit = 0b10000,\n /**\n * Used when a stream is aborted due to cancellation or errors\n */\n StreamAbortBit = 0b00100,\n}\n\n/**\n * Generic Typebox schema for a transport message.\n * @template T The type of the payload.\n * @param {T} t The payload schema.\n * @returns The transport message schema.\n */\nexport const TransportMessageSchema = <T extends TSchema>(t: T) =>\n Type.Object({\n id: Type.String(),\n from: Type.String(),\n to: Type.String(),\n seq: Type.Integer(),\n ack: Type.Integer(),\n serviceName: Type.Optional(Type.String()),\n procedureName: Type.Optional(Type.String()),\n streamId: Type.String(),\n controlFlags: Type.Integer(),\n tracing: Type.Optional(\n Type.Object({\n traceparent: Type.String(),\n tracestate: Type.String(),\n }),\n ),\n payload: t,\n });\n\n/**\n * Defines the schema for a transport acknowledgement message. This is never constructed manually\n * and is only used internally by the library for tracking inflight messages.\n * @returns The transport message schema.\n */\nexport const ControlMessageAckSchema = Type.Object({\n type: Type.Literal('ACK'),\n});\n\n/**\n * Defines the schema for a transport close message. This is never constructed manually and is only\n * used internally by the library for closing and cleaning up streams.\n */\nexport const ControlMessageCloseSchema = Type.Object({\n type: Type.Literal('CLOSE'),\n});\n\nexport const PROTOCOL_VERSION = 'v1.1';\nexport const ControlMessageHandshakeRequestSchema = Type.Object({\n type: Type.Literal('HANDSHAKE_REQ'),\n protocolVersion: Type.String(),\n sessionId: Type.String(),\n /**\n * Specifies what the server's expected session state (from the pov of the client). This can be\n * used by the server to know whether this is a new or a reestablished connection, and whether it\n * is compatible with what it already has.\n */\n expectedSessionState: Type.Object({\n // what the client expects the server to send next\n nextExpectedSeq: Type.Integer(),\n // TODO: remove optional once we know all servers\n // are nextSentSeq here\n // what the server expects the client to send next\n nextSentSeq: Type.Optional(Type.Integer()),\n }),\n\n metadata: Type.Optional(Type.Unknown()),\n});\n\nexport const HandshakeErrorRetriableResponseCodes = Type.Union([\n Type.Literal('SESSION_STATE_MISMATCH'),\n]);\n\nexport const HandshakeErrorFatalResponseCodes = Type.Union([\n Type.Literal('MALFORMED_HANDSHAKE_META'),\n Type.Literal('MALFORMED_HANDSHAKE'),\n Type.Literal('PROTOCOL_VERSION_MISMATCH'),\n Type.Literal('REJECTED_BY_CUSTOM_HANDLER'),\n]);\n\nexport const HandshakeErrorResponseCodes = Type.Union([\n HandshakeErrorRetriableResponseCodes,\n HandshakeErrorFatalResponseCodes,\n]);\n\nexport const ControlMessageHandshakeResponseSchema = Type.Object({\n type: Type.Literal('HANDSHAKE_RESP'),\n status: Type.Union([\n Type.Object({\n ok: Type.Literal(true),\n sessionId: Type.String(),\n }),\n Type.Object({\n ok: Type.Literal(false),\n reason: Type.String(),\n // TODO: remove optional once we know all servers\n // are sending code here\n code: Type.Optional(HandshakeErrorResponseCodes),\n }),\n ]),\n});\n\nexport const ControlMessagePayloadSchema = Type.Union([\n ControlMessageCloseSchema,\n ControlMessageAckSchema,\n ControlMessageHandshakeRequestSchema,\n ControlMessageHandshakeResponseSchema,\n]);\n\n/**\n * Defines the schema for an opaque transport message that is agnostic to any\n * procedure/service.\n * @returns The transport message schema.\n */\nexport const OpaqueTransportMessageSchema = TransportMessageSchema(\n Type.Unknown(),\n);\n\n/**\n * Represents a transport message. This is the same type as {@link TransportMessageSchema} but\n * we can't statically infer generics from generic Typebox schemas so we have to define it again here.\n *\n * TypeScript can't enforce types when a bitmask is involved, so these are the semantics of\n * `controlFlags`:\n * * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value\n * (suggestion: use `nanoid`).\n * * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.\n * * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,\n * `payload` should be discarded (usually contains a control message).\n * * If `controlFlags & AckBit == AckBit`, the message is an explicit acknowledgement message and doesn't\n * contain any payload that is relevant to the application so should not be delivered.\n * @template Payload The type of the payload.\n */\nexport interface TransportMessage<Payload = unknown> {\n id: string;\n from: TransportClientId;\n to: TransportClientId;\n seq: number;\n ack: number;\n serviceName?: string;\n procedureName?: string;\n streamId: string;\n controlFlags: number;\n tracing?: PropagationContext;\n payload: Payload;\n}\n\nexport type PartialTransportMessage<Payload = unknown> = Omit<\n TransportMessage<Payload>,\n 'id' | 'from' | 'to' | 'seq' | 'ack'\n>;\n\nexport function handshakeRequestMessage({\n from,\n to,\n sessionId,\n expectedSessionState,\n metadata,\n tracing,\n}: {\n from: TransportClientId;\n to: TransportClientId;\n sessionId: string;\n expectedSessionState: Static<\n typeof ControlMessageHandshakeRequestSchema\n >['expectedSessionState'];\n metadata?: unknown;\n tracing?: PropagationContext;\n}): TransportMessage<Static<typeof ControlMessageHandshakeRequestSchema>> {\n return {\n id: generateId(),\n from,\n to,\n seq: 0,\n ack: 0,\n streamId: generateId(),\n controlFlags: 0,\n tracing,\n payload: {\n type: 'HANDSHAKE_REQ',\n protocolVersion: PROTOCOL_VERSION,\n sessionId,\n expectedSessionState,\n metadata,\n } satisfies Static<typeof ControlMessageHandshakeRequestSchema>,\n };\n}\n\n/**\n * This is a reason that can be given during the handshake to indicate that the peer has the wrong\n * session state.\n */\nexport const SESSION_STATE_MISMATCH = 'session state mismatch';\n\nexport function handshakeResponseMessage({\n from,\n to,\n status,\n}: {\n from: TransportClientId;\n to: TransportClientId;\n status: Static<typeof ControlMessageHandshakeResponseSchema>['status'];\n}): TransportMessage<Static<typeof ControlMessageHandshakeResponseSchema>> {\n return {\n id: generateId(),\n from,\n to,\n seq: 0,\n ack: 0,\n streamId: generateId(),\n controlFlags: 0,\n payload: {\n type: 'HANDSHAKE_RESP',\n status,\n } satisfies Static<typeof ControlMessageHandshakeResponseSchema>,\n };\n}\n\nexport function closeStreamMessage(streamId: string): PartialTransportMessage {\n return {\n streamId,\n controlFlags: ControlFlags.StreamClosedBit,\n payload: {\n type: 'CLOSE' as const,\n } satisfies Static<typeof ControlMessagePayloadSchema>,\n };\n}\n\nexport function requestCloseStreamMessage(\n streamId: string,\n): PartialTransportMessage {\n return {\n streamId,\n controlFlags: ControlFlags.StreamCloseRequestBit,\n payload: {\n type: 'CLOSE' as const,\n } satisfies Static<typeof ControlMessagePayloadSchema>,\n };\n}\n\nexport function abortMessage(\n streamId: string,\n payload: ErrResult<\n Static<typeof OutputReaderErrorSchema | typeof InputReaderErrorSchema>\n >,\n) {\n return {\n streamId,\n controlFlags: ControlFlags.StreamAbortBit,\n payload,\n };\n}\n\n/**\n * A type alias for a transport message with an opaque payload.\n * @template T - The type of the opaque payload.\n */\nexport type OpaqueTransportMessage = TransportMessage;\nexport type TransportClientId = string;\n\n/**\n * Checks if the given control flag (usually found in msg.controlFlag) is an ack message.\n * @param controlFlag - The control flag to check.\n * @returns True if the control flag contains the AckBit, false otherwise.\n */\nexport function isAck(controlFlag: number): boolean {\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */\n return (controlFlag & ControlFlags.AckBit) === ControlFlags.AckBit;\n}\n\n/**\n * Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.\n * @param controlFlag - The control flag to check.\n * @returns True if the control flag contains the StreamOpenBit, false otherwise.\n */\nexport function isStreamOpen(controlFlag: number): boolean {\n return (\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */\n (controlFlag & ControlFlags.StreamOpenBit) === ControlFlags.StreamOpenBit\n );\n}\n\n/**\n * Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.\n * @param controlFlag - The control flag to check.\n * @returns True if the control flag contains the StreamCloseBit, false otherwise.\n */\nexport function isStreamClose(controlFlag: number): boolean {\n return (\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */\n (controlFlag & ControlFlags.StreamClosedBit) ===\n ControlFlags.StreamClosedBit\n );\n}\n\n/**\n * Checks if the given control flag (usually found in msg.controlFlag) is a stream close request message.\n * @param controlFlag - The control flag to check.\n * @returns True if the control flag contains the StreamCloseBit, false otherwise.\n */\nexport function isStreamCloseRequest(controlFlag: number): boolean {\n return (\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */\n (controlFlag & ControlFlags.StreamCloseRequestBit) ===\n ControlFlags.StreamCloseRequestBit\n );\n}\n\n/**\n * Checks if the given control flag (usually found in msg.controlFlag) is an abort message.\n * @param controlFlag - The control flag to check.\n * @returns True if the control flag contains the AbortBit, false otherwise\n */\nexport function isStreamAbort(controlFlag: number): boolean {\n return (\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */\n (controlFlag & ControlFlags.StreamAbortBit) === ControlFlags.StreamAbortBit\n );\n}\n","import { customAlphabet } from 'nanoid';\n\nconst alphabet = customAlphabet(\n '1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ',\n);\nexport const generateId = () => alphabet(12);\n","import {\n Context,\n Span,\n SpanKind,\n context,\n propagation,\n trace,\n} from '@opentelemetry/api';\nimport { version as RIVER_VERSION } from '../package.json';\nimport { ValidProcType } from '../router';\nimport { ClientTransport, Connection } from '../transport';\n\nexport interface PropagationContext {\n traceparent: string;\n tracestate: string;\n}\n\nexport interface TelemetryInfo {\n span: Span;\n ctx: Context;\n}\n\nexport function getPropagationContext(\n ctx: Context,\n): PropagationContext | undefined {\n const tracing = {\n traceparent: '',\n tracestate: '',\n };\n propagation.inject(ctx, tracing);\n return tracing;\n}\n\nexport function createSessionTelemetryInfo(\n sessionId: string,\n to: string,\n from: string,\n propagationCtx?: PropagationContext,\n): TelemetryInfo {\n const parentCtx = propagationCtx\n ? propagation.extract(context.active(), propagationCtx)\n : context.active();\n\n const span = tracer.startSpan(\n `session ${sessionId}`,\n {\n attributes: {\n component: 'river',\n 'river.session.id': sessionId,\n 'river.session.to': to,\n 'river.session.from': from,\n },\n },\n parentCtx,\n );\n\n const ctx = trace.setSpan(parentCtx, span);\n\n return { span, ctx };\n}\n\nexport function createConnectionTelemetryInfo(\n connection: Connection,\n info: TelemetryInfo,\n): TelemetryInfo {\n const span = tracer.startSpan(\n `connection ${connection.id}`,\n {\n attributes: {\n component: 'river',\n 'river.connection.id': connection.id,\n },\n links: [{ context: info.span.spanContext() }],\n },\n info.ctx,\n );\n\n const ctx = trace.setSpan(info.ctx, span);\n\n return { span, ctx };\n}\n\nexport function createProcTelemetryInfo(\n transport: ClientTransport<Connection>,\n kind: ValidProcType,\n serviceName: string,\n procedureName: string,\n streamId: string,\n): TelemetryInfo {\n const baseCtx = context.active();\n const span = tracer.startSpan(\n `procedure call ${serviceName}.${procedureName}`,\n {\n attributes: {\n component: 'river',\n 'river.method.kind': kind,\n 'river.method.service': serviceName,\n 'river.method.name': procedureName,\n 'river.streamId': streamId,\n 'span.kind': 'client',\n },\n kind: SpanKind.CLIENT,\n },\n baseCtx,\n );\n\n const ctx = trace.setSpan(baseCtx, span);\n\n transport.log?.info(`invoked ${serviceName}.${procedureName}`, {\n clientId: transport.clientId,\n transportMessage: {\n procedureName,\n serviceName,\n },\n telemetry: {\n traceId: span.spanContext().traceId,\n spanId: span.spanContext().spanId,\n },\n });\n return { span, ctx };\n}\n\nexport function createHandlerSpan<Fn extends (span: Span) => unknown>(\n kind: ValidProcType,\n serviceName: string,\n procedureName: string,\n streamId: string,\n tracing: PropagationContext | undefined,\n fn: Fn,\n): ReturnType<Fn> {\n const ctx = tracing\n ? propagation.extract(context.active(), tracing)\n : context.active();\n\n return tracer.startActiveSpan<Fn>(\n `procedure handler ${serviceName}.${procedureName}`,\n {\n attributes: {\n component: 'river',\n 'river.method.kind': kind,\n 'river.method.service': serviceName,\n 'river.method.name': procedureName,\n 'river.streamId': streamId,\n 'span.kind': 'server',\n },\n kind: SpanKind.SERVER,\n },\n ctx,\n fn,\n );\n}\n\nconst tracer = trace.getTracer('river', RIVER_VERSION);\nexport default tracer;\n","{\n \"name\": \"@replit/river\",\n \"description\": \"It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!\",\n \"version\": \"0.200.0-rc.3\",\n \"type\": \"module\",\n \"exports\": {\n \".\": {\n \"import\": \"./dist/router/index.js\",\n \"require\": \"./dist/router/index.cjs\"\n },\n \"./logging\": {\n \"import\": \"./dist/logging/index.js\",\n \"require\": \"./dist/logging/index.cjs\"\n },\n \"./codec\": {\n \"import\": \"./dist/codec/index.js\",\n \"require\": \"./dist/codec/index.cjs\"\n },\n \"./transport\": {\n \"import\": \"./dist/transport/index.js\",\n \"require\": \"./dist/transport/index.cjs\"\n },\n \"./transport/ws/client\": {\n \"import\": \"./dist/transport/impls/ws/client.js\",\n \"require\": \"./dist/transport/impls/ws/client.cjs\"\n },\n \"./transport/ws/server\": {\n \"import\": \"./dist/transport/impls/ws/server.js\",\n \"require\": \"./dist/transport/impls/ws/server.cjs\"\n },\n \"./transport/uds/client\": {\n \"import\": \"./dist/transport/impls/uds/client.js\",\n \"require\": \"./dist/transport/impls/uds/client.cjs\"\n },\n \"./transport/uds/server\": {\n \"import\": \"./dist/transport/impls/uds/server.js\",\n \"require\": \"./dist/transport/impls/uds/server.cjs\"\n },\n \"./test-util\": {\n \"import\": \"./dist/util/testHelpers.js\",\n \"require\": \"./dist/util/testHelpers.cjs\"\n }\n },\n \"sideEffects\": [\n \"./dist/logging/index.js\"\n ],\n \"files\": [\n \"dist\"\n ],\n \"dependencies\": {\n \"@msgpack/msgpack\": \"^3.0.0-beta2\",\n \"nanoid\": \"^4.0.2\",\n \"ws\": \"^8.17.0\"\n },\n \"peerDependencies\": {\n \"@opentelemetry/api\": \"^1.7.0\",\n \"@sinclair/typebox\": \"~0.32.8\"\n },\n \"devDependencies\": {\n \"@opentelemetry/core\": \"^1.7.0\",\n \"@opentelemetry/sdk-trace-base\": \"^1.24.1\",\n \"@opentelemetry/sdk-trace-web\": \"^1.24.1\",\n \"@types/ws\": \"^8.5.5\",\n \"@typescript-eslint/eslint-plugin\": \"^7.8.0\",\n \"@typescript-eslint/parser\": \"^7.8.0\",\n \"@vitest/ui\": \"^1.3.1\",\n \"eslint\": \"^8.57.0\",\n \"eslint-config-prettier\": \"^9.1.0\",\n \"eslint-plugin-prettier\": \"^5.1.3\",\n \"prettier\": \"^3.0.0\",\n \"tsup\": \"^7.2.0\",\n \"typescript\": \"^5.4.5\",\n \"vitest\": \"^1.3.1\"\n },\n \"scripts\": {\n \"check\": \"tsc --noEmit && npm run format && npm run lint\",\n \"format\": \"npx prettier . --check\",\n \"format:fix\": \"npx prettier . --write\",\n \"lint\": \"eslint .\",\n \"lint:fix\": \"eslint . --fix\",\n \"fix\": \"npm run format:fix && npm run lint:fix\",\n \"build\": \"rm -rf dist && tsup && du -sh dist\",\n \"prepack\": \"npm run build\",\n \"release\": \"npm publish --access public\",\n \"test:ui\": \"echo \\\"remember to go to /__vitest__ in the webview\\\" && vitest --ui --api.host 0.0.0.0 --api.port 3000\",\n \"test\": \"vitest\",\n \"test:single\": \"vitest run --reporter=dot\",\n \"test:flake\": \"./flake.sh\",\n \"bench\": \"vitest bench\"\n },\n \"engines\": {\n \"node\": \">=16\"\n },\n \"keywords\": [\n \"rpc\",\n \"websockets\",\n \"jsonschema\"\n ],\n \"author\": \"Jacky Zhao\",\n \"license\": \"MIT\"\n}\n","export function coerceErrorString(err: unknown): string {\n if (err instanceof Error) {\n return err.message || 'unknown reason';\n }\n\n return `[coerced to error] ${String(err)}`;\n}\n","import { Codec } from './types';\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\n// Convert Uint8Array to base64\nfunction uint8ArrayToBase64(uint8Array: Uint8Array) {\n let binary = '';\n uint8Array.forEach((byte) => {\n binary += String.fromCharCode(byte);\n });\n return btoa(binary);\n}\n\n// Convert base64 to Uint8Array\nfunction base64ToUint8Array(base64: string) {\n const binaryString = atob(base64);\n const uint8Array = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n uint8Array[i] = binaryString.charCodeAt(i);\n }\n return uint8Array;\n}\n\ninterface Base64EncodedValue {\n $t: string;\n}\n\n/**\n * Naive JSON codec implementation using JSON.stringify and JSON.parse.\n * @type {Codec}\n */\nexport const NaiveJsonCodec: Codec = {\n toBuffer: (obj: object) => {\n return encoder.encode(\n JSON.stringify(obj, function replacer<\n T extends object,\n >(this: T, key: keyof T) {\n const val = this[key];\n if (val instanceof Uint8Array) {\n return { $t: uint8ArrayToBase64(val) } satisfies Base64EncodedValue;\n } else {\n return val;\n }\n }),\n );\n },\n fromBuffer: (buff: Uint8Array) => {\n try {\n const parsed = JSON.parse(\n decoder.decode(buff),\n function reviver(_key, val: unknown) {\n if ((val as Base64EncodedValue | undefined)?.$t) {\n return base64ToUint8Array((val as Base64EncodedValue).$t);\n } else {\n return val;\n }\n },\n ) as unknown;\n\n if (typeof parsed === 'object') return parsed;\n return null;\n } catch {\n return null;\n }\n },\n};\n","import { NaiveJsonCodec } from '../codec/json';\nimport { ConnectionRetryOptions } from './rateLimit';\nimport { SessionOptions } from './sessionStateMachine/common';\n\nexport type TransportOptions = SessionOptions;\n\nexport type ProvidedTransportOptions = Partial<TransportOptions>;\n\nexport const defaultTransportOptions: TransportOptions = {\n heartbeatIntervalMs: 1_000,\n heartbeatsUntilDead: 2,\n sessionDisconnectGraceMs: 5_000,\n connectionTimeoutMs: 2_000,\n handshakeTimeoutMs: 1_000,\n codec: NaiveJsonCodec,\n};\n\nexport type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;\n\nexport type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;\n\nconst defaultConnectionRetryOptions: ConnectionRetryOptions = {\n baseIntervalMs: 250,\n maxJitterMs: 200,\n maxBackoffMs: 32_000,\n attemptBudgetCapacity: 5,\n budgetRestoreIntervalMs: 200,\n};\n\nexport const defaultClientTransportOptions: ClientTransportOptions = {\n ...defaultTransportOptions,\n ...defaultConnectionRetryOptions,\n};\n\nexport type ServerTransportOptions = TransportOptions;\n\nexport type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;\n\nexport const defaultServerTransportOptions: ServerTransportOptions = {\n ...defaultTransportOptions,\n};\n","import { Logger, MessageMetadata } from '../../logging';\nimport { TelemetryInfo } from '../../tracing';\nimport {\n OpaqueTransportMessage,\n OpaqueTransportMessageSchema,\n PartialTransportMessage,\n TransportClientId,\n TransportMessage,\n} from '../message';\nimport { Value } from '@sinclair/typebox/value';\nimport { SessionNoConnection } from './SessionNoConnection';\nimport { SessionConnecting } from './SessionConnecting';\nimport { SessionHandshaking } from './SessionHandshaking';\nimport { SessionConnected } from './SessionConnected';\nimport { Codec } from '../../codec';\nimport { Connection } from '../connection';\nimport { generateId } from '../id';\n\nexport const enum SessionState {\n NoConnection = 'NoConnection',\n Connecting = 'Connecting',\n Handshaking = 'Handshaking',\n Connected = 'Connected',\n WaitingForHandshake = 'WaitingForHandshake',\n}\n\nexport type Session<ConnType extends Connection> =\n | SessionNoConnection\n | SessionConnecting<ConnType>\n | SessionHandshaking<ConnType>\n | SessionConnected<ConnType>;\n\nexport const ERR_CONSUMED = `session state has been consumed and is no longer valid`;\n\nabstract class StateMachineState {\n abstract readonly state: SessionState;\n\n /*\n * Whether this state has been consumed\n * and we've moved on to another state\n */\n _isConsumed: boolean;\n\n // called when we're transitioning to another state\n // note that this is internal and should not be called directly\n // by consumers, the proxy will call this when the state is consumed\n // and we're transitioning to another state\n abstract _handleStateExit(): void;\n\n // called when we exit the state machine entirely\n // note that this is internal and should not be called directly\n // by consumers, the proxy will call this when .close is closed\n abstract _handleClose(): void;\n\n close(): void {\n this._handleClose();\n }\n\n constructor() {\n this._isConsumed = false;\n\n // proxy helps us prevent access to properties after the state has been consumed\n // e.g. if we hold a reference to a state and try to access it after it's been consumed\n // we intercept the access and throw an error to help catch bugs\n return new Proxy(this, {\n get(target, prop) {\n // always allow access to _isConsumed, id, and state\n if (prop === '_isConsumed' || prop === 'id' || prop === 'state') {\n return Reflect.get(target, prop);\n }\n\n // modify _handleStateExit\n if (prop === '_handleStateExit') {\n return () => {\n target._isConsumed = true;\n target._handleStateExit();\n };\n }\n\n // modify _handleClose\n if (prop === '_handleClose') {\n return () => {\n target._handleStateExit();\n target._handleClose();\n };\n }\n\n if (target._isConsumed) {\n throw new Error(\n `${ERR_CONSUMED}: getting ${prop.toString()} on consumed state`,\n );\n }\n\n return Reflect.get(target, prop);\n },\n set(target, prop, value) {\n if (target._isConsumed) {\n throw new Error(\n `${ERR_CONSUMED}: setting ${prop.toString()} on consumed state`,\n );\n }\n\n return Reflect.set(target, prop, value);\n },\n });\n }\n}\n\nexport interface SessionOptions {\n /**\n * Frequency at which to send heartbeat acknowledgements\n */\n heartbeatIntervalMs: number;\n /**\n * Number of elapsed heartbeats without a response message before we consider\n * the connection dead.\n */\n heartbeatsUntilDead: number;\n /**\n * Duration to wait between connection disconnect and actual session disconnect\n */\n sessionDisconnectGraceMs: number;\n /**\n * Connection timeout in milliseconds\n */\n connectionTimeoutMs: number;\n /**\n * Handshake timeout in milliseconds\n */\n handshakeTimeoutMs: number;\n /**\n * The codec to use for encoding/decoding messages over the wire\n */\n codec: Codec;\n}\n\n// all session states have a from and options\nexport abstract class CommonSession extends StateMachineState {\n readonly from: TransportClientId;\n readonly options: SessionOptions;\n\n log?: Logger;\n abstract get loggingMetadata(): MessageMetadata;\n\n constructor(\n from: TransportClientId,\n options: SessionOptions,\n log: Logger | undefined,\n ) {\n super();\n this.from = from;\n this.options = options;\n this.log = log;\n }\n\n parseMsg(msg: Uint8Array): OpaqueTransportMessage | null {\n const parsedMsg = this.options.codec.fromBuffer(msg);\n\n if (parsedMsg === null) {\n const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));\n this.log?.error(\n `received malformed msg: ${decodedBuffer}`,\n this.loggingMetadata,\n );\n return null;\n }\n\n if (!Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {\n this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {\n ...this.loggingMetadata,\n validationErrors: [\n ...Value.Errors(OpaqueTransportMessageSchema, parsedMsg),\n ],\n });\n\n return null;\n }\n\n return parsedMsg;\n }\n}\n\nexport type InheritedProperties = Pick<\n IdentifiedSession,\n 'id' | 'from' | 'to' | 'seq' | 'ack' | 'sendBuffer' | 'telemetry' | 'options'\n>;\n\nexport type SessionId = string;\n\n// all sessions where we know the other side's client id\nexport abstract class IdentifiedSession extends CommonSession {\n readonly id: SessionId;\n readonly telemetry: TelemetryInfo;\n readonly to: TransportClientId;\n\n /**\n * Index of the message we will send next (excluding handshake)\n */\n seq: number;\n\n /**\n * Number of unique messages we've received this session (excluding handshake)\n */\n ack: number;\n sendBuffer: Array<OpaqueTransportMessage>;\n\n constructor(\n id: SessionId,\n from: TransportClientId,\n to: TransportClientId,\n seq: number,\n ack: number,\n sendBuffer: Array<OpaqueTransportMessage>,\n telemetry: TelemetryInfo,\n options: SessionOptions,\n log: Logger | undefined,\n ) {\n super(from, options, log);\n this.id = id;\n this.to = to;\n this.seq = seq;\n this.ack = ack;\n this.sendBuffer = sendBuffer;\n this.telemetry = telemetry;\n this.log = log;\n }\n\n get loggingMetadata(): MessageMetadata {\n const spanContext = this.telemetry.span.spanContext();\n\n return {\n clientId: this.from,\n connectedTo: this.to,\n sessionId: this.id,\n telemetry: {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n },\n };\n }\n\n constructMsg<Payload>(\n partialMsg: PartialTransportMessage<Payload>,\n ): TransportMessage<Payload> {\n const msg = {\n ...partialMsg,\n id: generateId(),\n to: this.to,\n from: this.from,\n seq: this.seq,\n ack: this.ack,\n };\n\n this.seq++;\n return msg;\n }\n\n nextSeq(): number {\n return this.sendBuffer.length > 0 ? this.sendBuffer[0].seq : this.seq;\n }\n\n send(msg: PartialTransportMessage): string {\n const constructedMsg = this.constructMsg(msg);\n this.sendBuffer.push(constructedMsg);\n return constructedMsg.id;\n }\n\n _handleStateExit(): void {\n // noop\n }\n\n _handleClose(): void {\n // zero out the buffer\n this.sendBuffer.length = 0;\n this.telemetry.span.end();\n }\n}\n","import { Connection } from '../connection';\nimport { IdentifiedSession, SessionState } from './common';\n\nexport interface SessionConnectingListeners {\n onConnectionEstablished: (conn: Connection) => void;\n onConnectionFailed: (err: unknown) => void;\n\n // timeout related\n onConnectionTimeout: () => void;\n}\n\n/*\n * A session that is connecting but we don't have access to the raw connection\n * yet.\n *\n * Valid transitions:\n * - Connecting -> NoConnection (timeout)\n * - Connecting -> Handshaking (on connection established)\n */\nexport class SessionConnecting<\n ConnType extends Connection,\n> extends IdentifiedSession {\n readonly state = SessionState.Connecting as const;\n connPromise: Promise<ConnType>;\n listeners: SessionConnectingListeners;\n\n connectionTimeout?: ReturnType<typeof setTimeout>;\n\n constructor(\n connPromise: Promise<ConnType>,\n listeners: SessionConnectingListeners,\n ...args: ConstructorParameters<typeof IdentifiedSession>\n ) {\n super(...args);\n this.connPromise = connPromise;\n this.listeners = listeners;\n\n this.connectionTimeout = setTimeout(() => {\n listeners.onConnectionTimeout();\n }, this.options.connectionTimeoutMs);\n\n connPromise.then(\n (conn) => {\n if (this._isConsumed) return;\n listeners.onConnectionEstablished(conn);\n },\n (err) => {\n if (this._isConsumed) return;\n listeners.onConnectionFailed(err);\n },\n );\n }\n\n // close a pending connection if it resolves, ignore errors if the promise\n // ends up rejected anyways\n bestEffortClose() {\n void this.connPromise\n .then((conn) => conn.close())\n .catch(() => {\n // ignore errors\n });\n }\n\n _handleStateExit(): void {\n super._handleStateExit();\n clearTimeout(this.connectionTimeout);\n this.connectionTimeout = undefined;\n }\n\n _handleClose(): void {\n // close the pending connection if it resolves\n this.bestEffortClose();\n super._handleClose();\n }\n}\n","import { IdentifiedSession, SessionState } from './common';\n\nexport interface SessionNoConnectionListeners {\n // timeout related\n onSessionGracePeriodElapsed: () => void;\n}\n\n/*\n * A session that is not connected and cannot send or receive messages.\n *\n * Valid transitions:\n * - NoConnection -> Connecting (on connect)\n */\nexport class SessionNoConnection extends IdentifiedSession {\n readonly state = SessionState.NoConnection as const;\n listeners: SessionNoConnectionListeners;\n\n gracePeriodTimeout?: ReturnType<typeof setTimeout>;\n\n constructor(\n listeners: SessionNoConnectionListeners,\n ...args: ConstructorParameters<typeof IdentifiedSession>\n ) {\n super(...args);\n this.listeners = listeners;\n\n this.gracePeriodTimeout = setTimeout(() => {\n this.listeners.onSessionGracePeriodElapsed();\n }, this.options.sessionDisconnectGraceMs);\n }\n\n _handleClose(): void {\n super._handleClose();\n }\n\n _handleStateExit(): void {\n super._handleStateExit();\n\n if (this.gracePeriodTimeout) {\n clearTimeout(this.gracePeriodTimeout);\n this.gracePeriodTimeout = undefined;\n }\n }\n}\n","import { MessageMetadata } from '../../logging';\nimport { Connection } from '../connection';\nimport { TransportMessage } from '../message';\nimport { SessionHandshakingListeners } from './SessionHandshaking';\nimport { CommonSession, SessionState } from './common';\n\n/*\n * Server-side session that has a connection but is waiting for the client to identify itself.\n *\n * Valid transitions:\n * - WaitingForHandshake -> NoConnection (on close)\n * - WaitingForHandshake -> Connected (on handshake)\n */\nexport class SessionWaitingForHandshake<\n ConnType extends Connection,\n> extends CommonSession {\n readonly state = SessionState.WaitingForHandshake as const;\n conn: ConnType;\n listeners: SessionHandshakingListeners;\n\n handshakeTimeout?: ReturnType<typeof setTimeout>;\n\n constructor(\n conn: ConnType,\n listeners: SessionHandshakingListeners,\n ...args: ConstructorParameters<typeof CommonSession>\n ) {\n super(...args);\n this.conn = conn;\n this.listeners = listeners;\n\n this.handshakeTimeout = setTimeout(() => {\n listeners.onHandshakeTimeout();\n }, this.options.handshakeTimeoutMs);\n\n this.conn.addDataListener(this.onHandshakeData);\n this.conn.addErrorListener(listeners.onConnectionErrored);\n this.conn.addCloseListener(listeners.onConnectionClosed);\n }\n\n onHandshakeData = (msg: Uint8Array) => {\n const parsedMsg = this.parseMsg(msg);\n if (parsedMsg === null) {\n this.listeners.onInvalidHandshake('could not parse message');\n return;\n }\n\n // after this fires, the listener is responsible for transitioning the session\n // and thus removing the handshake timeout\n this.listeners.onHandshake(parsedMsg);\n };\n\n get loggingMetadata(): MessageMetadata {\n return {\n clientId: this.from,\n connId: this.conn.id,\n };\n }\n\n sendHandshake(msg: TransportMessage): boolean {\n return this.conn.send(this.options.codec.toBuffer(msg));\n }\n\n _handleStateExit(): void {\n this.conn.removeDataListener(this.onHandshakeData);\n this.conn.removeErrorListener(this.listeners.onConnectionErrored);\n this.conn.removeCloseListener(this.listeners.onConnectionClosed);\n clearTimeout(this.handshakeTimeout);\n this.handshakeTimeout = undefined;\n }\n\n _handleClose(): void {\n this.conn.close();\n }\n}\n","import { Connection } from '../connection';\nimport { OpaqueTransportMessage, TransportMessage } from '../message';\nimport { IdentifiedSession, SessionState } from './common';\n\nexport interface SessionHandshakingListeners {\n onConnectionErrored: (err: unknown) => void;\n onConnectionClosed: () => void;\n onHandshake: (msg: OpaqueTransportMessage) => void;\n onInvalidHandshake: (reason: string) => void;\n\n // timeout related\n onHandshakeTimeout: () => void;\n}\n\n/*\n * A session that is handshaking and waiting for the other side to identify itself.\n *\n * Valid transitions:\n * - Handshaking -> NoConnection (on close)\n * - Handshaking -> Connected (on handshake)\n */\nexport class SessionHandshaking<\n ConnType extends Connection,\n> extends IdentifiedSession {\n readonly state = SessionState.Handshaking as const;\n conn: ConnType;\n listeners: SessionHandshakingListeners;\n\n handshakeTimeout: ReturnType<typeof setTimeout>;\n\n constructor(\n conn: ConnType,\n listeners: SessionHandshakingListeners,\n ...args: ConstructorParameters<typeof IdentifiedSession>\n ) {\n super(...args);\n this.conn = conn;\n this.listeners = listeners;\n\n this.handshakeTimeout = setTimeout(() => {\n listeners.onHandshakeTimeout();\n }, this.options.handshakeTimeoutMs);\n\n this.conn.addDataListener(this.onHandshakeData);\n this.conn.addErrorListener(listeners.onConnectionErrored);\n this.conn.addCloseListener(listeners.onConnectionClosed);\n }\n\n onHandshakeData = (msg: Uint8Array) => {\n const parsedMsg = this.parseMsg(msg);\n if (parsedMsg === null) {\n this.listeners.onInvalidHandshake('could not parse message');\n return;\n }\n\n this.listeners.onHandshake(parsedMsg);\n };\n\n sendHandshake(msg: TransportMessage): boolean {\n return this.conn.send(this.options.codec.toBuffer(msg));\n }\n\n _handleStateExit(): void {\n super._handleStateExit();\n this.conn.removeDataListener(this.onHandshakeData);\n this.conn.removeErrorListener(this.listeners.onConnectionErrored);\n this.conn.removeCloseListener(this.listeners.onConnectionClosed);\n clearTimeout(this.handshakeTimeout);\n }\n\n _handleClose(): void {\n super._handleClose();\n this.conn.close();\n }\n}\n","import { Static } from '@sinclair/typebox';\nimport {\n ControlFlags,\n ControlMessageAckSchema,\n OpaqueTransportMessage,\n PartialTransportMessage,\n isAck,\n} from '../message';\nimport { IdentifiedSession, SessionState } from './common';\nimport { Connection } from '../connection';\nimport { SpanStatusCode } from '@opentelemetry/api';\n\nexport interface SessionConnectedListeners {\n onConnectionErrored: (err: unknown) => void;\n onConnectionClosed: () => void;\n onMessage: (msg: OpaqueTransportMessage) => void;\n onInvalidMessage: (reason: string) => void;\n}\n\n/*\n * A session that is connected and can send and receive messages.\n *\n * Valid transitions:\n * - Connected -> NoConnection (on close)\n */\nexport class SessionConnected<\n ConnType extends Connection,\n> extends IdentifiedSession {\n readonly state = SessionState.Connected as const;\n conn: ConnType;\n listeners: SessionConnectedListeners;\n\n heartbeatHandle?: ReturnType<typeof setInterval> | undefined;\n heartbeatMisses = 0;\n\n get isActivelyHeartbeating() {\n return this.heartbeatHandle !== undefined;\n }\n\n updateBookkeeping(ack: number, seq: number) {\n this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);\n this.ack = seq + 1;\n this.heartbeatMisses = 0;\n }\n\n send(msg: PartialTransportMessage): string {\n const constructedMsg = this.constructMsg(msg);\n this.sendBuffer.push(constructedMsg);\n this.conn.send(this.options.codec.toBuffer(constructedMsg));\n return constructedMsg.id;\n }\n\n constructor(\n conn: ConnType,\n listeners: SessionConnectedListeners,\n ...args: ConstructorParameters<typeof IdentifiedSession>\n ) {\n super(...args);\n this.conn = conn;\n this.listeners = listeners;\n\n this.conn.addDataListener(this.onMessageData);\n this.conn.addCloseListener(listeners.onConnectionClosed);\n this.conn.addErrorListener(listeners.onConnectionErrored);\n\n // send any buffered messages\n if (this.sendBuffer.length > 0) {\n this.log?.debug(\n `sending ${this.sendBuffer.length} buffered messages`,\n this.loggingMetadata,\n );\n }\n\n for (const msg of this.sendBuffer) {\n conn.send(this.options.codec.toBuffer(msg));\n }\n\n // dont explicity clear the buffer, we'll just filter out old messages\n // when we receive an ack\n }\n\n startActiveHeartbeat() {\n this.heartbeatHandle = setInterval(() => {\n const misses = this.heartbeatMisses;\n const missDuration = misses * this.options.heartbeatIntervalMs;\n if (misses >= this.options.heartbeatsUntilDead) {\n this.log?.info(\n `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,\n this.loggingMetadata,\n );\n this.telemetry.span.addEvent('closing connection due to inactivity');\n this.conn.close();\n clearInterval(this.heartbeatHandle);\n this.heartbeatHandle = undefined;\n return;\n }\n\n this.sendHeartbeat();\n this.heartbeatMisses++;\n }, this.options.heartbeatIntervalMs);\n }\n\n private sendHeartbeat() {\n this.send({\n streamId: 'heartbeat',\n controlFlags: ControlFlags.AckBit,\n payload: {\n type: 'ACK',\n } satisfies Static<typeof ControlMessageAckSchema>,\n });\n }\n\n onMessageData = (msg: Uint8Array) => {\n const parsedMsg = this.parseMsg(msg);\n if (parsedMsg === null) return;\n\n // check message ordering here\n if (parsedMsg.seq !== this.ack) {\n if (parsedMsg.seq < this.ack) {\n this.log?.debug(\n `received duplicate msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack}), discarding`,\n {\n ...this.loggingMetadata,\n transportMessage: parsedMsg,\n },\n );\n } else {\n const reason = `received out-of-order msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack})`;\n this.log?.error(reason, {\n ...this.loggingMetadata,\n transportMessage: parsedMsg,\n tags: ['invariant-violation'],\n });\n this.telemetry.span.setStatus({\n code: SpanStatusCode.ERROR,\n message: reason,\n });\n\n this.listeners.onInvalidMessage(reason);\n }\n\n return;\n }\n\n // message is ok to update bookkeeping with\n this.log?.debug(`received msg`, {\n ...this.loggingMetadata,\n transportMessage: parsedMsg,\n });\n\n this.updateBookkeeping(parsedMsg.ack, parsedMsg.seq);\n\n // dispatch directly if its not an explicit ack\n if (!isAck(parsedMsg.controlFlags)) {\n this.listeners.onMessage(parsedMsg);\n return;\n }\n\n // discard acks (unless we aren't heartbeating in which case just respond)\n this.log?.debug(`discarding msg (ack bit set)`, {\n ...this.loggingMetadata,\n transportMessage: parsedMsg,\n });\n\n // if we are not actively heartbeating, we are in passive\n // heartbeat mode and should send a response to the ack\n if (!this.isActivelyHeartbeating) {\n this.sendHeartbeat();\n }\n };\n\n _handleStateExit(): void {\n super._handleStateExit();\n this.conn.removeDataListener(this.onMessageData);\n this.conn.removeCloseListener(this.listeners.onConnectionClosed);\n this.conn.removeErrorListener(this.listeners.onConnectionErrored);\n clearInterval(this.heartbeatHandle);\n this.heartbeatHandle = undefined;\n }\n\n _handleClose(): void {\n super._handleClose();\n this.conn.close();\n }\n}\n","import { OpaqueTransportMessage, TransportClientId } from '..';\nimport {\n SessionConnecting,\n SessionConnectingListeners,\n} from './SessionConnecting';\nimport {\n SessionNoConnection,\n SessionNoConnectionListeners,\n} from './SessionNoConnection';\nimport { IdentifiedSession, SessionOptions } from './common';\nimport { PropagationContext, createSessionTelemetryInfo } from '../../tracing';\nimport { SessionWaitingForHandshake } from './SessionWaitingForHandshake';\nimport {\n SessionHandshaking,\n SessionHandshakingListeners,\n} from './SessionHandshaking';\nimport {\n SessionConnected,\n SessionConnectedListeners,\n} from './SessionConnected';\nimport { generateId } from '../id';\nimport { Connection } from '../connection';\nimport { Logger } from '../../logging';\n\nfunction inheritSharedSession(\n session: IdentifiedSession,\n): ConstructorParameters<typeof IdentifiedSession> {\n return [\n session.id,\n session.from,\n session.to,\n session.seq,\n session.ack,\n session.sendBuffer,\n session.telemetry,\n session.options,\n session.log,\n ];\n}\n\n/*\n * Session state machine:\n * 1. SessionNoConnection is the client entrypoint as\n * we know who the other side is already, we just need to connect\n * 5. SessionWaitingForHandshake is the server entrypoint\n * as we have a connection but don't know who the other side is yet\n *\n * 1. SessionNoConnection ◄──┐\n * │ reconnect / connect attempt │\n * ▼ │\n * 2. SessionConnecting │\n * │ connect success ──────────────┤ connect failure\n * ▼ │\n * 3. SessionHandshaking │\n * │ handshake success ┌──────┤ connection drop\n * 5. WaitingForHandshake │ handshake failure ─────┤ │\n * │ handshake success ▼ │ │ connection drop\n * ├───────────────────────► 4. SessionConnected │ │ heartbeat misses\n * │ │ invalid message ───────┼──────┘\n * │ ▼ │\n * └───────────────────────► x. Destroy Session ◄─────┘\n * handshake failure\n */\nexport const SessionStateGraph = {\n entrypoints: {\n NoConnection(\n to: TransportClientId,\n from: TransportClientId,\n listeners: SessionNoConnectionListeners,\n options: SessionOptions,\n log?: Logger,\n ) {\n const id = `session-${generateId()}`;\n const telemetry = createSessionTelemetryInfo(id, to, from);\n const sendBuffer: Array<OpaqueTransportMessage> = [];\n\n const session = new SessionNoConnection(\n listeners,\n id,\n from,\n to,\n 0,\n 0,\n sendBuffer,\n telemetry,\n options,\n log,\n );\n\n session.log?.info(`session ${session.id} created in NoConnection state`, {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n });\n\n return session;\n },\n WaitingForHandshake<ConnType extends Connection>(\n from: TransportClientId,\n conn: ConnType,\n listeners: SessionHandshakingListeners,\n options: SessionOptions,\n log?: Logger,\n ): SessionWaitingForHandshake<ConnType> {\n const session = new SessionWaitingForHandshake(\n conn,\n listeners,\n from,\n options,\n log,\n );\n\n session.log?.info(`session created in WaitingForHandshake state`, {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n });\n\n return session;\n },\n },\n // All of the transitions 'move'/'consume' the old session and return a new one.\n // After a session is transitioned, any usage of the old session will throw.\n transition: {\n // happy path transitions\n NoConnectionToConnecting<ConnType extends Connection>(\n oldSession: SessionNoConnection,\n connPromise: Promise<ConnType>,\n listeners: SessionConnectingListeners,\n ): SessionConnecting<ConnType> {\n const carriedState = inheritSharedSession(oldSession);\n oldSession._handleStateExit();\n\n const session = new SessionConnecting(\n connPromise,\n listeners,\n ...carriedState,\n );\n session.log?.info(\n `session ${session.id} transition from NoConnection to Connecting`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n return session;\n },\n ConnectingToHandshaking<ConnType extends Connection>(\n oldSession: SessionConnecting<ConnType>,\n conn: ConnType,\n listeners: SessionHandshakingListeners,\n ): SessionHandshaking<ConnType> {\n const carriedState = inheritSharedSession(oldSession);\n oldSession._handleStateExit();\n\n const session = new SessionHandshaking(conn, listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from Connecting to Handshaking`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n HandshakingToConnected<ConnType extends Connection>(\n oldSession: SessionHandshaking<ConnType>,\n listeners: SessionConnectedListeners,\n ): SessionConnected<ConnType> {\n const carriedState = inheritSharedSession(oldSession);\n const conn = oldSession.conn;\n oldSession._handleStateExit();\n\n const session = new SessionConnected(conn, listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from Handshaking to Connected`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n WaitingForHandshakeToConnected<ConnType extends Connection>(\n pendingSession: SessionWaitingForHandshake<ConnType>,\n oldSession: SessionNoConnection | undefined,\n sessionId: string,\n to: TransportClientId,\n propagationCtx: PropagationContext | undefined,\n listeners: SessionConnectedListeners,\n ): SessionConnected<ConnType> {\n const conn = pendingSession.conn;\n const { from, options } = pendingSession;\n const carriedState: ConstructorParameters<typeof IdentifiedSession> =\n oldSession\n ? // old session exists, inherit state\n inheritSharedSession(oldSession)\n : // old session does not exist, create new state\n [\n sessionId,\n from,\n to,\n 0,\n 0,\n [],\n createSessionTelemetryInfo(sessionId, to, from, propagationCtx),\n options,\n pendingSession.log,\n ];\n\n pendingSession._handleStateExit();\n oldSession?._handleStateExit();\n\n const session = new SessionConnected(conn, listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from WaitingForHandshake to Connected`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n // disconnect paths\n ConnectingToNoConnection<ConnType extends Connection>(\n oldSession: SessionConnecting<ConnType>,\n listeners: SessionNoConnectionListeners,\n ): SessionNoConnection {\n const carriedState = inheritSharedSession(oldSession);\n oldSession.bestEffortClose();\n oldSession._handleStateExit();\n\n const session = new SessionNoConnection(listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from Connecting to NoConnection`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n HandshakingToNoConnection<ConnType extends Connection>(\n oldSession: SessionHandshaking<ConnType>,\n listeners: SessionNoConnectionListeners,\n ): SessionNoConnection {\n const carriedState = inheritSharedSession(oldSession);\n oldSession.conn.close();\n oldSession._handleStateExit();\n\n const session = new SessionNoConnection(listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from Handshaking to NoConnection`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n ConnectedToNoConnection<ConnType extends Connection>(\n oldSession: SessionConnected<ConnType>,\n listeners: SessionNoConnectionListeners,\n ): SessionNoConnection {\n const carriedState = inheritSharedSession(oldSession);\n oldSession.conn.close();\n oldSession._handleStateExit();\n\n const session = new SessionNoConnection(listeners, ...carriedState);\n session.log?.info(\n `session ${session.id} transition from Connected to NoConnection`,\n {\n ...session.loggingMetadata,\n tags: ['state-transition'],\n },\n );\n\n return session;\n },\n },\n} as const;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;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,wBACd,WACiB;AACjB,QAAM,cAAc,CAAC;AACrB,aAAW,WAAW,UAAU,SAAS,OAAO,GAAG;AACjD,QAAI,QAAQ,uCAAkC;AAC5C,kBAAY,KAAK,QAAQ,IAAI;AAAA,IAC/B;AAAA,EACF;AAEA,SAAO;AACT;AAEO,SAAS,oBACd,WACQ;AACR,SAAO,wBAAwB,SAAS,EAAE;AAC5C;AAEO,SAAS,oBACd,WACA;AACA,aAAW,QAAQ,wBAAwB,SAAS,GAAG;AACrD,SAAK,MAAM;AAAA,EACb;AACF;","names":["import_typebox","import_typebox","import_nanoid","import_api","NodeWs"]}
@@ -1,6 +1,7 @@
1
+ import { T as Transport, C as Connection, m as SessionOptions, e as SessionNoConnection, n as ServiceContext, S as Session } from '../context-c9668e95.js';
2
+ import { P as PartialTransportMessage, b as OpaqueTransportMessage } from '../message-fd349b27.js';
1
3
  import { Static } from '@sinclair/typebox';
2
- import { B as BaseErrorSchemaType, R as ReadStream, O as OkResult, E as ErrResult, T as Transport, C as Connection, S as SessionOptions, a as Session, P as PayloadType, b as ProcedureErrorSchemaType, c as Procedure, d as ServiceContext, e as Result, f as OutputReaderErrorSchema, W as WriteStream } from '../services-34d97070.js';
3
- import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-10ebd26a.js';
4
+ import { B as BaseErrorSchemaType, R as ReadStream, O as OkResult, E as ErrResult, P as PayloadType, a as ProcedureErrorSchemaType, b as Procedure, c as Result, d as OutputReaderErrorSchema, W as WriteStream } from '../services-690e5553.js';
4
5
  import NodeWs from 'ws';
5
6
  import http from 'node:http';
6
7
  import net from 'node:net';
@@ -8,6 +9,7 @@ import { W as WsLike } from '../wslike-e0b32dd5.js';
8
9
  import '../types-3e5768ec.js';
9
10
  import '@sinclair/typebox/value';
10
11
  import '@opentelemetry/api';
12
+ import '../client-a84783be.js';
11
13
 
12
14
  /**
13
15
  * Creates a WebSocket client that connects to a local server at the specified port.
@@ -71,7 +73,7 @@ declare function createDummyTransportMessage(): PartialTransportMessage<{
71
73
  */
72
74
  declare function waitForMessage(t: Transport<Connection>, filter?: (msg: OpaqueTransportMessage) => boolean, rejectMismatch?: boolean): Promise<unknown>;
73
75
  declare const testingSessionOptions: SessionOptions;
74
- declare function dummySession(): Session<Connection>;
76
+ declare function dummySession(): SessionNoConnection;
75
77
  declare function asClientRpc<State extends object, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'rpc', Init, null, Output, Err>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): (msg: Static<Init>) => Promise<Result<Static<Output>, Static<Err> | Static<typeof OutputReaderErrorSchema>>>;
76
78
  declare function asClientStream<State extends object, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'stream', Init, Input, Output, Err>, init?: Static<Init>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): [WriteStream<Static<Input>>, ReadStream<Static<Output>, Static<Err>>];
77
79
  declare function asClientSubscription<State extends object, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'subscription', Init, null, Output, Err>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): (msg: Static<Init>) => ReadStream<Static<Output>, Static<Err>>;
@@ -80,5 +82,8 @@ declare function asClientUpload<State extends object, Init extends PayloadType,
80
82
  () => Promise<Result<Static<Output>, Static<Err>>>
81
83
  ];
82
84
  declare const getUnixSocketPath: () => string;
85
+ declare function getTransportConnections<ConnType extends Connection>(transport: Transport<ConnType>): Array<ConnType>;
86
+ declare function numberOfConnections<ConnType extends Connection>(transport: Transport<ConnType>): number;
87
+ declare function closeAllConnections<ConnType extends Connection>(transport: Transport<ConnType>): void;
83
88
 
84
- export { asClientRpc, asClientStream, asClientSubscription, asClientUpload, createDummyTransportMessage, createLocalWebSocketClient, createWebSocketServer, dummySession, getIteratorFromStream, getUnixSocketPath, iterNext, onUdsServeReady, onWsServerReady, payloadToTransportMessage, testingSessionOptions, waitForMessage };
89
+ export { asClientRpc, asClientStream, asClientSubscription, asClientUpload, closeAllConnections, createDummyTransportMessage, createLocalWebSocketClient, createWebSocketServer, dummySession, getIteratorFromStream, getTransportConnections, getUnixSocketPath, iterNext, numberOfConnections, onUdsServeReady, onWsServerReady, payloadToTransportMessage, testingSessionOptions, waitForMessage };
@@ -1,6 +1,7 @@
1
+ import { T as Transport, C as Connection, m as SessionOptions, e as SessionNoConnection, n as ServiceContext, S as Session } from '../context-c9668e95.js';
2
+ import { P as PartialTransportMessage, b as OpaqueTransportMessage } from '../message-fd349b27.js';
1
3
  import { Static } from '@sinclair/typebox';
2
- import { B as BaseErrorSchemaType, R as ReadStream, O as OkResult, E as ErrResult, T as Transport, C as Connection, S as SessionOptions, a as Session, P as PayloadType, b as ProcedureErrorSchemaType, c as Procedure, d as ServiceContext, e as Result, f as OutputReaderErrorSchema, W as WriteStream } from '../services-34d97070.js';
3
- import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-10ebd26a.js';
4
+ import { B as BaseErrorSchemaType, R as ReadStream, O as OkResult, E as ErrResult, P as PayloadType, a as ProcedureErrorSchemaType, b as Procedure, c as Result, d as OutputReaderErrorSchema, W as WriteStream } from '../services-690e5553.js';
4
5
  import NodeWs from 'ws';
5
6
  import http from 'node:http';
6
7
  import net from 'node:net';
@@ -8,6 +9,7 @@ import { W as WsLike } from '../wslike-e0b32dd5.js';
8
9
  import '../types-3e5768ec.js';
9
10
  import '@sinclair/typebox/value';
10
11
  import '@opentelemetry/api';
12
+ import '../client-a84783be.js';
11
13
 
12
14
  /**
13
15
  * Creates a WebSocket client that connects to a local server at the specified port.
@@ -71,7 +73,7 @@ declare function createDummyTransportMessage(): PartialTransportMessage<{
71
73
  */
72
74
  declare function waitForMessage(t: Transport<Connection>, filter?: (msg: OpaqueTransportMessage) => boolean, rejectMismatch?: boolean): Promise<unknown>;
73
75
  declare const testingSessionOptions: SessionOptions;
74
- declare function dummySession(): Session<Connection>;
76
+ declare function dummySession(): SessionNoConnection;
75
77
  declare function asClientRpc<State extends object, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'rpc', Init, null, Output, Err>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): (msg: Static<Init>) => Promise<Result<Static<Output>, Static<Err> | Static<typeof OutputReaderErrorSchema>>>;
76
78
  declare function asClientStream<State extends object, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'stream', Init, Input, Output, Err>, init?: Static<Init>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): [WriteStream<Static<Input>>, ReadStream<Static<Output>, Static<Err>>];
77
79
  declare function asClientSubscription<State extends object, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(state: State, proc: Procedure<State, 'subscription', Init, null, Output, Err>, extendedContext?: Omit<ServiceContext, 'state'>, session?: Session<Connection>): (msg: Static<Init>) => ReadStream<Static<Output>, Static<Err>>;
@@ -80,5 +82,8 @@ declare function asClientUpload<State extends object, Init extends PayloadType,
80
82
  () => Promise<Result<Static<Output>, Static<Err>>>
81
83
  ];
82
84
  declare const getUnixSocketPath: () => string;
85
+ declare function getTransportConnections<ConnType extends Connection>(transport: Transport<ConnType>): Array<ConnType>;
86
+ declare function numberOfConnections<ConnType extends Connection>(transport: Transport<ConnType>): number;
87
+ declare function closeAllConnections<ConnType extends Connection>(transport: Transport<ConnType>): void;
83
88
 
84
- export { asClientRpc, asClientStream, asClientSubscription, asClientUpload, createDummyTransportMessage, createLocalWebSocketClient, createWebSocketServer, dummySession, getIteratorFromStream, getUnixSocketPath, iterNext, onUdsServeReady, onWsServerReady, payloadToTransportMessage, testingSessionOptions, waitForMessage };
89
+ export { asClientRpc, asClientStream, asClientSubscription, asClientUpload, closeAllConnections, createDummyTransportMessage, createLocalWebSocketClient, createWebSocketServer, dummySession, getIteratorFromStream, getTransportConnections, getUnixSocketPath, iterNext, numberOfConnections, onUdsServeReady, onWsServerReady, payloadToTransportMessage, testingSessionOptions, waitForMessage };