@nmtjs/protocol 0.6.4 → 0.7.0

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 (62) hide show
  1. package/dist/client/events.js +29 -26
  2. package/dist/client/events.js.map +1 -1
  3. package/dist/client/format.js +1 -2
  4. package/dist/client/format.js.map +1 -1
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/client/protocol.js +331 -308
  7. package/dist/client/protocol.js.map +1 -1
  8. package/dist/client/stream.js +79 -92
  9. package/dist/client/stream.js.map +1 -1
  10. package/dist/common/binary.js +20 -20
  11. package/dist/common/binary.js.map +1 -1
  12. package/dist/common/blob.js +36 -40
  13. package/dist/common/blob.js.map +1 -1
  14. package/dist/common/enums.js +44 -44
  15. package/dist/common/enums.js.map +1 -1
  16. package/dist/common/index.js.map +1 -1
  17. package/dist/common/types.js +1 -1
  18. package/dist/common/types.js.map +1 -1
  19. package/dist/server/api.js +1 -1
  20. package/dist/server/api.js.map +1 -1
  21. package/dist/server/connection.js +18 -18
  22. package/dist/server/connection.js.map +1 -1
  23. package/dist/server/constants.js +1 -1
  24. package/dist/server/constants.js.map +1 -1
  25. package/dist/server/format.js +42 -45
  26. package/dist/server/format.js.map +1 -1
  27. package/dist/server/index.js.map +1 -1
  28. package/dist/server/injectables.js +18 -18
  29. package/dist/server/injectables.js.map +1 -1
  30. package/dist/server/protocol.js +282 -296
  31. package/dist/server/protocol.js.map +1 -1
  32. package/dist/server/registry.js +2 -19
  33. package/dist/server/registry.js.map +1 -1
  34. package/dist/server/stream.js +24 -26
  35. package/dist/server/stream.js.map +1 -1
  36. package/dist/server/transport.js +6 -6
  37. package/dist/server/transport.js.map +1 -1
  38. package/dist/server/utils.js +9 -9
  39. package/dist/server/utils.js.map +1 -1
  40. package/package.json +12 -16
  41. package/{lib → src}/client/protocol.ts +46 -5
  42. package/{lib → src}/client/stream.ts +18 -26
  43. package/{lib → src}/server/connection.ts +1 -1
  44. package/{lib → src}/server/protocol.ts +19 -9
  45. package/src/server/registry.ts +3 -0
  46. package/lib/server/registry.ts +0 -24
  47. /package/{lib → src}/client/events.ts +0 -0
  48. /package/{lib → src}/client/format.ts +0 -0
  49. /package/{lib → src}/client/index.ts +0 -0
  50. /package/{lib → src}/common/binary.ts +0 -0
  51. /package/{lib → src}/common/blob.ts +0 -0
  52. /package/{lib → src}/common/enums.ts +0 -0
  53. /package/{lib → src}/common/index.ts +0 -0
  54. /package/{lib → src}/common/types.ts +0 -0
  55. /package/{lib → src}/server/api.ts +0 -0
  56. /package/{lib → src}/server/constants.ts +0 -0
  57. /package/{lib → src}/server/format.ts +0 -0
  58. /package/{lib → src}/server/index.ts +0 -0
  59. /package/{lib → src}/server/injectables.ts +0 -0
  60. /package/{lib → src}/server/stream.ts +0 -0
  61. /package/{lib → src}/server/transport.ts +0 -0
  62. /package/{lib → src}/server/utils.ts +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server/protocol.ts"],"sourcesContent":["import { type Callback, createPromise, defer, throwError } from '@nmtjs/common'\nimport { type Container, Hook, type Logger, Scope } from '@nmtjs/core'\nimport { concat, decodeNumber, encodeNumber } from '../common/binary.ts'\nimport type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'\nimport { ErrorCode, ServerMessageType } from '../common/enums.ts'\nimport type { ProtocolRPC } from '../common/types.ts'\nimport type { ProtocolApi, ProtocolApiCallResult } from './api.ts'\nimport {\n Connection,\n ConnectionContext,\n type ConnectionOptions,\n} from './connection.ts'\nimport type { Format } from './format.ts'\nimport type { ProtocolRegistry } from './registry.ts'\nimport { ProtocolClientStream, ProtocolServerStream } from './stream.ts'\nimport type { Transport } from './transport.ts'\nimport { getFormat, type ResolveFormatParams } from './utils.ts'\n\nexport class ProtocolError extends Error {\n code: string\n data?: any\n\n constructor(code: string, message?: string, data?: any) {\n super(message)\n this.code = code\n this.data = data\n }\n\n get message() {\n return `${this.code} ${super.message}`\n }\n\n toString() {\n return `${this.code} ${this.message}`\n }\n\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n data: this.data,\n }\n }\n}\n\nexport class ProtocolConnections {\n readonly #collection = new Map<\n string,\n {\n connection: Connection\n context: ConnectionContext\n transport: Transport\n }\n >()\n\n constructor(\n private readonly application: {\n logger: Logger\n registry: ProtocolRegistry\n format: Format\n container: Container\n },\n ) {}\n\n get(connectionId: string) {\n const connection = this.#collection.get(connectionId)\n if (!connection) throwError('Connection not found')\n return connection\n }\n\n async add<T>(\n transport: Transport<any>,\n options: ConnectionOptions<T>,\n params: ResolveFormatParams,\n ) {\n const connection = new Connection(options)\n const format = getFormat(this.application.format, params)\n const container = this.application.container.fork(Scope.Connection)\n const context = new ConnectionContext(container, format)\n\n this.#collection.set(connection.id, { connection, context, transport })\n\n await this.application.registry.hooks.call(\n Hook.OnConnect,\n { concurrent: false },\n connection,\n )\n\n return { connection, context }\n }\n\n async remove(connectionId: string) {\n const { connection, context } = this.get(connectionId)\n\n this.application.registry.hooks.call(\n Hook.OnDisconnect,\n { concurrent: true },\n connection,\n )\n\n this.#collection.delete(connectionId)\n\n const { calls, serverStreams, clientStreams, rpcStreams, container } =\n context\n\n for (const call of calls.values()) {\n call.reject(new Error('Connection closed'))\n }\n\n for (const stream of clientStreams.values()) {\n stream.destroy(new Error('Connection closed'))\n }\n\n for (const stream of serverStreams.values()) {\n stream.destroy(new Error('Connection closed'))\n }\n\n for (const stream of rpcStreams.values()) {\n stream.abort(new Error('Connection closed'))\n }\n\n try {\n await container.dispose()\n } catch (error) {\n this.application.logger.error(\n { error, connection },\n 'Error during closing connection',\n )\n }\n }\n}\n\nexport class ProtocolClientStreams {\n constructor(private readonly connections: ProtocolConnections) {}\n\n get(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n const stream = clientStreams.get(streamId) ?? throwError('Stream not found')\n return stream\n }\n\n remove(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n clientStreams.get(streamId) || throwError('Stream not found')\n clientStreams.delete(streamId)\n }\n\n add(\n connectionId: string,\n streamId: number,\n metadata: ProtocolBlobMetadata,\n read: Callback,\n ) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n const stream = new ProtocolClientStream(streamId, metadata, { read })\n clientStreams.set(streamId, stream)\n return stream\n }\n\n push(connectionId: string, streamId: number, chunk: ArrayBuffer) {\n const stream = this.get(connectionId, streamId)\n stream.push(Buffer.from(chunk))\n }\n\n end(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.push(null)\n this.remove(connectionId, streamId)\n }\n\n abort(connectionId: string, streamId: number, error = new Error('Aborted')) {\n const stream = this.get(connectionId, streamId)\n stream.destroy(error)\n this.remove(connectionId, streamId)\n }\n}\n\nexport class ProtocolServerStreams {\n constructor(private readonly connections: ProtocolConnections) {}\n\n get(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n const stream = serverStreams.get(streamId) ?? throwError('Stream not found')\n return stream\n }\n\n add(connectionId: string, streamId: number, blob: ProtocolBlob) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n const stream = new ProtocolServerStream(streamId, blob)\n serverStreams.set(streamId, stream)\n return stream\n }\n\n remove(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n serverStreams.has(streamId) || throwError('Stream not found')\n serverStreams.delete(streamId)\n }\n\n pull(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.resume()\n }\n\n abort(connectionId: string, streamId: number, error = new Error('Aborted')) {\n const stream = this.get(connectionId, streamId)\n stream.destroy(error)\n this.remove(connectionId, streamId)\n }\n}\n\nexport class Protocol {\n readonly connections: ProtocolConnections\n readonly clientStreams: ProtocolClientStreams\n readonly serverStreams: ProtocolServerStreams\n\n constructor(\n protected readonly application: {\n logger: Logger\n format: Format\n container: Container\n registry: ProtocolRegistry\n api: ProtocolApi\n },\n ) {\n this.connections = new ProtocolConnections(this.application)\n this.clientStreams = new ProtocolClientStreams(this.connections)\n this.serverStreams = new ProtocolServerStreams(this.connections)\n }\n\n async rpc(\n connectionId: string,\n rpc: ProtocolRPC,\n params: { signal?: AbortSignal } = {},\n ) {\n const { connection, context, transport } =\n this.connections.get(connectionId)\n const { calls, format } = context\n const { callId, namespace, procedure, payload } = rpc\n const abortController = new AbortController()\n const signal = params.signal\n ? AbortSignal.any([params.signal, abortController.signal])\n : abortController.signal\n\n const call = Object.assign(createPromise<ProtocolApiCallResult>(), {\n abort: () => abortController.abort(),\n })\n\n calls.set(callId, call)\n call.promise.finally(() => calls.delete(callId))\n\n const callIdEncoded = encodeNumber(callId, 'Uint32')\n const container = context.container.fork(Scope.Call)\n\n try {\n const response = await this.application.api.call({\n connection,\n container,\n namespace,\n payload,\n procedure,\n signal,\n })\n\n const responseEncoded = format.encoder.encodeRPC(\n {\n callId,\n payload: response.output,\n },\n {\n addStream: (blob) => {\n const id = context.streamId++\n const stream = this.serverStreams.add(connectionId, id, blob)\n stream.on('data', (chunk) => {\n stream.pause()\n transport.send(\n connection,\n ServerMessageType.ServerStreamPush,\n concat(\n encodeNumber(id, 'Uint32'),\n Buffer.from(chunk).buffer as ArrayBuffer,\n ),\n )\n })\n stream.on('error', (err) => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamAbort,\n encodeNumber(id, 'Uint32'),\n )\n })\n stream.on('end', () => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamEnd,\n encodeNumber(id, 'Uint32'),\n )\n })\n return stream\n },\n getStream: (id) => {\n return this.clientStreams.get(connectionId, id)\n },\n },\n )\n\n if ('subscription' in response) {\n throwError('Unimplemented')\n } else if ('iterable' in response) {\n transport.send(\n connection,\n ServerMessageType.RpcStreamResponse,\n responseEncoded,\n )\n try {\n const ab = new AbortController()\n context.rpcStreams.set(callId, ab)\n const iterable =\n typeof response.iterable === 'function'\n ? response.iterable()\n : response.iterable\n for await (const chunk of iterable) {\n if (ab.signal.aborted) break\n const chunkEncoded = format.encoder.encode(chunk)\n transport.send(\n connection,\n ServerMessageType.RpcStreamChunk,\n concat(callIdEncoded, chunkEncoded),\n )\n }\n } catch (error) {\n this.application.logger.error(error)\n transport.send(\n connection,\n ServerMessageType.RpcStreamAbort,\n callIdEncoded,\n )\n } finally {\n context.rpcStreams.delete(callId)\n response.onFinish && defer(response.onFinish)\n }\n } else {\n transport.send(\n connection,\n ServerMessageType.RpcResponse,\n responseEncoded,\n )\n }\n } catch (error) {\n if (error instanceof ProtocolError === false) {\n this.application.logger.error(\n { error, connection },\n 'Error during RPC call',\n )\n\n // biome-ignore lint/suspicious/noCatchAssign:\n error = new ProtocolError(\n ErrorCode.InternalServerError,\n 'Internal server error',\n )\n }\n\n const payload = format.encoder.encodeRPC(\n { callId, error },\n {\n addStream(blob) {\n throwError('Cannot handle stream for error response')\n },\n getStream(id) {\n throwError('Cannot handle stream for error response')\n },\n },\n )\n transport.send(connection, ServerMessageType.RpcResponse, payload)\n } finally {\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n \"Error during disposing connection's container\",\n )\n })\n }\n }\n\n async rpcRaw(\n connectionId: string,\n buffer: ArrayBuffer,\n params: { signal?: AbortSignal } = {},\n ) {\n const { connection, context, transport } =\n this.connections.get(connectionId)\n\n const { format } = context\n\n const rpc = format.decoder.decodeRPC(buffer, {\n addStream: (id, metadata) => {\n return this.clientStreams.add(connectionId, id, metadata, (size) => {\n transport.send(\n connection,\n ServerMessageType.ClientStreamPull,\n concat(encodeNumber(id, 'Uint32'), encodeNumber(size, 'Uint32')),\n )\n })\n },\n getStream: (id) => {\n return this.serverStreams.get(connectionId, id)\n },\n })\n\n return await this.rpc(connectionId, rpc, params)\n }\n\n rpcAbort(connectionId: string, callId: number) {\n const { context } = this.connections.get(connectionId)\n const call = context.calls.get(callId) ?? throwError('Call not found')\n call.abort()\n }\n\n rpcAbortRaw(connectionId: string, buffer: ArrayBuffer) {\n const callId = decodeNumber(buffer, 'Uint32')\n return this.rpcAbort(connectionId, callId)\n }\n\n rpcStreamAbort(connectionId: string, callId: number) {\n const { context } = this.connections.get(connectionId)\n const ab =\n context.rpcStreams.get(callId) ?? throwError('Call stream not found')\n ab.abort()\n }\n\n rpcStreamAbortRaw(connectionId: string, buffer: ArrayBuffer) {\n const callId = decodeNumber(buffer, 'Uint32')\n return this.rpcStreamAbort(connectionId, callId)\n }\n\n notify(connectionId: string, event, payload) {\n throw Error('Unimplemented')\n }\n}\n"],"names":["createPromise","defer","throwError","Hook","Scope","concat","decodeNumber","encodeNumber","ErrorCode","ServerMessageType","Connection","ConnectionContext","ProtocolClientStream","ProtocolServerStream","getFormat","ProtocolError","Error","code","data","constructor","message","toString","toJSON","ProtocolConnections","application","Map","get","connectionId","connection","add","transport","options","params","format","container","fork","context","set","id","registry","hooks","call","OnConnect","concurrent","remove","OnDisconnect","delete","calls","serverStreams","clientStreams","rpcStreams","values","reject","stream","destroy","abort","dispose","error","logger","ProtocolClientStreams","connections","streamId","metadata","read","push","chunk","Buffer","from","end","ProtocolServerStreams","blob","has","pull","resume","Protocol","rpc","callId","namespace","procedure","payload","abortController","AbortController","signal","AbortSignal","any","Object","assign","promise","finally","callIdEncoded","Call","response","api","responseEncoded","encoder","encodeRPC","output","addStream","on","pause","send","ServerStreamPush","buffer","err","ServerStreamAbort","ServerStreamEnd","getStream","RpcStreamResponse","ab","iterable","aborted","chunkEncoded","encode","RpcStreamChunk","RpcStreamAbort","onFinish","RpcResponse","InternalServerError","catch","rpcRaw","decoder","decodeRPC","size","ClientStreamPull","rpcAbort","rpcAbortRaw","rpcStreamAbort","rpcStreamAbortRaw","notify","event"],"mappings":"AAAA,SAAwBA,aAAa,EAAEC,KAAK,EAAEC,UAAU,QAAQ,gBAAe;AAC/E,SAAyBC,IAAI,EAAeC,KAAK,QAAQ,cAAa;AACtE,SAASC,MAAM,EAAEC,YAAY,EAAEC,YAAY,QAAQ,sBAAqB;AAExE,SAASC,SAAS,EAAEC,iBAAiB,QAAQ,qBAAo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W,CAAC,OAAO;AACjB,WAAO,KAAK,cAAc,IAAI,cAAc,GAAG;GAChD;EACF,EAAC;AAEF,SAAO,MAAM,KAAK,IAAI,cAAc,KAAK,OAAO;CACjD;CAED,SAASZ,cAAsBc,QAAgB;EAC7C,MAAM,EAAE,SAAS,GAAG,KAAK,YAAY,IAAI,aAAa;EACtD,MAAM,OAAO,QAAQ,MAAM,IAAI,OAAO,IAAI,WAAW,iBAAiB;AACtE,OAAK,OAAO;CACb;CAED,YAAYd,cAAsBa,QAAqB;EACrD,MAAM,SAAS,aAAa,QAAQ,SAAS;AAC7C,SAAO,KAAK,SAAS,cAAc,OAAO;CAC3C;CAED,eAAeb,cAAsBc,QAAgB;EACnD,MAAM,EAAE,SAAS,GAAG,KAAK,YAAY,IAAI,aAAa;EACtD,MAAM,KACJ,QAAQ,WAAW,IAAI,OAAO,IAAI,WAAW,wBAAwB;AACvE,KAAG,OAAO;CACX;CAED,kBAAkBd,cAAsBa,QAAqB;EAC3D,MAAM,SAAS,aAAa,QAAQ,SAAS;AAC7C,SAAO,KAAK,eAAe,cAAc,OAAO;CACjD;CAED,OAAOb,cAAsB,OAAO,SAAS;AAC3C,QAAM,MAAM,gBAAgB;CAC7B;AACF","names":["code: string","message?: string","data?: any","#collection","application: {\n logger: Logger\n registry: ProtocolRegistry\n format: Format\n container: Container\n }","connectionId: string","transport: Transport<any>","options: ConnectionOptions<T>","params: ResolveFormatParams","connections: ProtocolConnections","streamId: number","metadata: ProtocolBlobMetadata","read: Callback","chunk: ArrayBuffer","blob: ProtocolBlob","application: {\n logger: Logger\n format: Format\n container: Container\n registry: ProtocolRegistry\n api: ProtocolApi\n }","rpc: ProtocolRPC","params: { signal?: AbortSignal }","buffer: ArrayBuffer","callId: number"],"sources":["src/server/protocol.ts"],"sourcesContent":["import { type Callback, createPromise, defer, throwError } from '@nmtjs/common'\nimport { type Container, Hook, type Logger, Scope } from '@nmtjs/core'\nimport { concat, decodeNumber, encodeNumber } from '../common/binary.ts'\nimport type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'\nimport { ErrorCode, ServerMessageType } from '../common/enums.ts'\nimport type { ProtocolRPC } from '../common/types.ts'\nimport type { ProtocolApi, ProtocolApiCallResult } from './api.ts'\nimport {\n Connection,\n ConnectionContext,\n type ConnectionOptions,\n} from './connection.ts'\nimport type { Format } from './format.ts'\nimport type { ProtocolRegistry } from './registry.ts'\nimport { ProtocolClientStream, ProtocolServerStream } from './stream.ts'\nimport type { Transport } from './transport.ts'\nimport { getFormat, type ResolveFormatParams } from './utils.ts'\n\nexport class ProtocolError extends Error {\n code: string\n data?: any\n\n constructor(code: string, message?: string, data?: any) {\n super(message)\n this.code = code\n this.data = data\n }\n\n get message() {\n return `${this.code} ${super.message}`\n }\n\n toString() {\n return `${this.code} ${this.message}`\n }\n\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n data: this.data,\n }\n }\n}\n\nexport class ProtocolConnections {\n readonly #collection = new Map<\n string,\n {\n connection: Connection\n context: ConnectionContext\n transport: Transport\n }\n >()\n\n constructor(\n private readonly application: {\n logger: Logger\n registry: ProtocolRegistry\n format: Format\n container: Container\n },\n ) {}\n\n get(connectionId: string) {\n const connection = this.#collection.get(connectionId)\n if (!connection) throwError('Connection not found')\n return connection\n }\n\n async add<T>(\n transport: Transport<any>,\n options: ConnectionOptions<T>,\n params: ResolveFormatParams,\n ) {\n const connection = new Connection(options)\n const format = getFormat(this.application.format, params)\n const container = this.application.container.fork(Scope.Connection)\n const context = new ConnectionContext(container, format)\n\n this.#collection.set(connection.id, { connection, context, transport })\n\n await this.application.registry.hooks.call(\n Hook.OnConnect,\n { concurrent: false },\n connection,\n )\n\n return { connection, context }\n }\n\n async remove(connectionId: string) {\n const { connection, context } = this.get(connectionId)\n\n this.application.registry.hooks.call(\n Hook.OnDisconnect,\n { concurrent: true },\n connection,\n )\n\n this.#collection.delete(connectionId)\n\n const { calls, serverStreams, clientStreams, rpcStreams, container } =\n context\n\n for (const call of calls.values()) {\n call.abort(new Error('Connection closed'))\n }\n\n for (const stream of clientStreams.values()) {\n stream.destroy(new Error('Connection closed'))\n }\n\n for (const stream of serverStreams.values()) {\n stream.destroy(new Error('Connection closed'))\n }\n\n for (const stream of rpcStreams.values()) {\n stream.abort(new Error('Connection closed'))\n }\n\n try {\n await container.dispose()\n } catch (error) {\n this.application.logger.error(\n { error, connection },\n 'Error during closing connection',\n )\n }\n }\n}\n\nexport class ProtocolClientStreams {\n constructor(private readonly connections: ProtocolConnections) {}\n\n get(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n const stream = clientStreams.get(streamId) ?? throwError('Stream not found')\n return stream\n }\n\n remove(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n clientStreams.get(streamId) || throwError('Stream not found')\n clientStreams.delete(streamId)\n }\n\n add(\n connectionId: string,\n streamId: number,\n metadata: ProtocolBlobMetadata,\n read: Callback,\n ) {\n const { context } = this.connections.get(connectionId)\n const { clientStreams } = context\n const stream = new ProtocolClientStream(streamId, metadata, { read })\n clientStreams.set(streamId, stream)\n return stream\n }\n\n push(connectionId: string, streamId: number, chunk: ArrayBuffer) {\n const stream = this.get(connectionId, streamId)\n stream.push(Buffer.from(chunk))\n }\n\n end(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.push(null)\n this.remove(connectionId, streamId)\n }\n\n abort(connectionId: string, streamId: number, error = new Error('Aborted')) {\n const stream = this.get(connectionId, streamId)\n stream.destroy(error)\n this.remove(connectionId, streamId)\n }\n}\n\nexport class ProtocolServerStreams {\n constructor(private readonly connections: ProtocolConnections) {}\n\n get(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n const stream = serverStreams.get(streamId) ?? throwError('Stream not found')\n return stream\n }\n\n add(connectionId: string, streamId: number, blob: ProtocolBlob) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n const stream = new ProtocolServerStream(streamId, blob)\n serverStreams.set(streamId, stream)\n return stream\n }\n\n remove(connectionId: string, streamId: number) {\n const { context } = this.connections.get(connectionId)\n const { serverStreams } = context\n serverStreams.has(streamId) || throwError('Stream not found')\n serverStreams.delete(streamId)\n }\n\n pull(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.resume()\n }\n\n abort(connectionId: string, streamId: number, error = new Error('Aborted')) {\n const stream = this.get(connectionId, streamId)\n stream.destroy(error)\n this.remove(connectionId, streamId)\n }\n}\n\nexport class Protocol {\n readonly connections: ProtocolConnections\n readonly clientStreams: ProtocolClientStreams\n readonly serverStreams: ProtocolServerStreams\n\n constructor(\n protected readonly application: {\n logger: Logger\n format: Format\n container: Container\n registry: ProtocolRegistry\n api: ProtocolApi\n },\n ) {\n this.connections = new ProtocolConnections(this.application)\n this.clientStreams = new ProtocolClientStreams(this.connections)\n this.serverStreams = new ProtocolServerStreams(this.connections)\n }\n\n async rpc(\n connectionId: string,\n rpc: ProtocolRPC,\n params: { signal?: AbortSignal } = {},\n ) {\n const { connection, context, transport } =\n this.connections.get(connectionId)\n const { calls, format } = context\n const { callId, namespace, procedure, payload } = rpc\n const abortController = new AbortController()\n const signal = params.signal\n ? AbortSignal.any([params.signal, abortController.signal])\n : abortController.signal\n\n const call = Object.assign(createPromise<ProtocolApiCallResult>(), {\n abort: () => abortController.abort(),\n })\n\n calls.set(callId, call)\n call.promise.finally(() => calls.delete(callId))\n\n const callIdEncoded = encodeNumber(callId, 'Uint32')\n const container = context.container.fork(Scope.Call)\n\n try {\n const response = await this.application.api.call({\n connection,\n container,\n namespace,\n payload,\n procedure,\n signal,\n })\n\n const responseEncoded = format.encoder.encodeRPC(\n {\n callId,\n payload: response.output,\n },\n {\n addStream: (blob) => {\n const id = context.streamId++\n const stream = this.serverStreams.add(connectionId, id, blob)\n stream.on('data', (chunk) => {\n stream.pause()\n const buf = Buffer.from(chunk)\n transport.send(\n connection,\n ServerMessageType.ServerStreamPush,\n concat(\n encodeNumber(id, 'Uint32'),\n (buf.buffer as ArrayBuffer).slice(\n buf.byteOffset,\n buf.byteOffset + buf.byteLength,\n ),\n ),\n )\n })\n stream.on('error', (err) => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamAbort,\n encodeNumber(id, 'Uint32'),\n )\n })\n stream.on('end', () => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamEnd,\n encodeNumber(id, 'Uint32'),\n )\n })\n return stream\n },\n getStream: (id) => {\n return this.clientStreams.get(connectionId, id)\n },\n },\n )\n\n if ('subscription' in response) {\n throwError('Unimplemented')\n } else if ('iterable' in response) {\n transport.send(\n connection,\n ServerMessageType.RpcStreamResponse,\n responseEncoded,\n )\n try {\n const ab = new AbortController()\n context.rpcStreams.set(callId, ab)\n const iterable =\n typeof response.iterable === 'function'\n ? response.iterable()\n : response.iterable\n for await (const chunk of iterable) {\n if (ab.signal.aborted) break\n const chunkEncoded = format.encoder.encode(chunk)\n transport.send(\n connection,\n ServerMessageType.RpcStreamChunk,\n concat(callIdEncoded, chunkEncoded),\n )\n }\n } catch (error) {\n this.application.logger.error(error)\n transport.send(\n connection,\n ServerMessageType.RpcStreamAbort,\n callIdEncoded,\n )\n } finally {\n context.rpcStreams.delete(callId)\n response.onFinish && defer(response.onFinish)\n }\n } else {\n transport.send(\n connection,\n ServerMessageType.RpcResponse,\n responseEncoded,\n )\n }\n } catch (error) {\n if (error instanceof ProtocolError === false) {\n this.application.logger.error(\n { error, connection },\n 'Error during RPC call',\n )\n\n // biome-ignore lint/suspicious/noCatchAssign:\n error = new ProtocolError(\n ErrorCode.InternalServerError,\n 'Internal server error',\n )\n }\n\n const payload = format.encoder.encodeRPC(\n { callId, error },\n {\n addStream(blob) {\n throwError('Cannot handle stream for error response')\n },\n getStream(id) {\n throwError('Cannot handle stream for error response')\n },\n },\n )\n transport.send(connection, ServerMessageType.RpcResponse, payload)\n } finally {\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n \"Error during disposing connection's container\",\n )\n })\n }\n }\n\n async rpcRaw(\n connectionId: string,\n buffer: ArrayBuffer,\n params: { signal?: AbortSignal } = {},\n ) {\n const { connection, context, transport } =\n this.connections.get(connectionId)\n\n const { format } = context\n\n const rpc = format.decoder.decodeRPC(buffer, {\n addStream: (id, metadata) => {\n const stream = this.clientStreams.add(\n connectionId,\n id,\n metadata,\n (size) => {\n transport.send(\n connection,\n ServerMessageType.ClientStreamPull,\n concat(encodeNumber(id, 'Uint32'), encodeNumber(size, 'Uint32')),\n )\n },\n )\n return stream\n },\n getStream: (id) => {\n return this.serverStreams.get(connectionId, id)\n },\n })\n\n return await this.rpc(connectionId, rpc, params)\n }\n\n rpcAbort(connectionId: string, callId: number) {\n const { context } = this.connections.get(connectionId)\n const call = context.calls.get(callId) ?? throwError('Call not found')\n call.abort()\n }\n\n rpcAbortRaw(connectionId: string, buffer: ArrayBuffer) {\n const callId = decodeNumber(buffer, 'Uint32')\n return this.rpcAbort(connectionId, callId)\n }\n\n rpcStreamAbort(connectionId: string, callId: number) {\n const { context } = this.connections.get(connectionId)\n const ab =\n context.rpcStreams.get(callId) ?? throwError('Call stream not found')\n ab.abort()\n }\n\n rpcStreamAbortRaw(connectionId: string, buffer: ArrayBuffer) {\n const callId = decodeNumber(buffer, 'Uint32')\n return this.rpcStreamAbort(connectionId, callId)\n }\n\n notify(connectionId: string, event, payload) {\n throw Error('Unimplemented')\n }\n}\n"],"version":3}
@@ -1,19 +1,2 @@
1
- import { Registry } from '@nmtjs/core';
2
- import { compile } from '@nmtjs/type/compiler';
3
- export class ProtocolRegistry extends Registry {
4
- types = new Set();
5
- compiled = new Map();
6
- registerType(type) {
7
- this.types.add(type);
8
- }
9
- compile() {
10
- for (const type of this.types){
11
- this.compiled.set(type, compile(type));
12
- }
13
- }
14
- clear() {
15
- super.clear();
16
- this.types.clear();
17
- this.compiled.clear();
18
- }
19
- }
1
+ import { Registry } from "@nmtjs/core";
2
+ export class ProtocolRegistry extends Registry {}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server/registry.ts"],"sourcesContent":["import { Registry } from '@nmtjs/core'\nimport type { BaseType, BaseTypeAny } from '@nmtjs/type'\nimport { type Compiled, compile } from '@nmtjs/type/compiler'\n\nexport class ProtocolRegistry extends Registry {\n readonly types = new Set<BaseType>()\n readonly compiled = new Map<any, Compiled>()\n\n registerType(type: BaseTypeAny) {\n this.types.add(type)\n }\n\n compile() {\n for (const type of this.types) {\n this.compiled.set(type, compile(type))\n }\n }\n\n clear() {\n super.clear()\n this.types.clear()\n this.compiled.clear()\n }\n}\n"],"names":["Registry","compile","ProtocolRegistry","types","Set","compiled","Map","registerType","type","add","set","clear"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,cAAa;AAEtC,SAAwBC,OAAO,QAAQ,uBAAsB;AAE7D,OAAO,MAAMC,yBAAyBF;IAC3BG,QAAQ,IAAIC,MAAe;IAC3BC,WAAW,IAAIC,MAAoB;IAE5CC,aAAaC,IAAiB,EAAE;QAC9B,IAAI,CAACL,KAAK,CAACM,GAAG,CAACD;IACjB;IAEAP,UAAU;QACR,KAAK,MAAMO,QAAQ,IAAI,CAACL,KAAK,CAAE;YAC7B,IAAI,CAACE,QAAQ,CAACK,GAAG,CAACF,MAAMP,QAAQO;QAClC;IACF;IAEAG,QAAQ;QACN,KAAK,CAACA;QACN,IAAI,CAACR,KAAK,CAACQ,KAAK;QAChB,IAAI,CAACN,QAAQ,CAACM,KAAK;IACrB;AACF"}
1
+ {"mappings":"AAAA,SAAS,gBAAgB,aAAa;AAEtC,OAAO,MAAM,yBAAyB,SAAS,CAAE","names":[],"sources":["src/server/registry.ts"],"sourcesContent":["import { Registry } from '@nmtjs/core'\n\nexport class ProtocolRegistry extends Registry {}\n"],"version":3}
@@ -1,30 +1,28 @@
1
- import { PassThrough, Readable } from 'node:stream';
2
- import { ReadableStream } from 'node:stream/web';
1
+ import { PassThrough, Readable } from "node:stream";
2
+ import { ReadableStream } from "node:stream/web";
3
3
  export class ProtocolClientStream extends Readable {
4
- id;
5
- metadata;
6
- constructor(id, metadata, options){
7
- super(options);
8
- this.id = id;
9
- this.metadata = metadata;
10
- }
4
+ constructor(id, metadata, options) {
5
+ super(options);
6
+ this.id = id;
7
+ this.metadata = metadata;
8
+ }
11
9
  }
12
10
  export class ProtocolServerStream extends PassThrough {
13
- id;
14
- metadata;
15
- constructor(id, blob){
16
- let readable;
17
- if (blob.source instanceof Readable) {
18
- readable = blob.source;
19
- } else if (blob.source instanceof ReadableStream) {
20
- readable = Readable.fromWeb(blob.source);
21
- } else {
22
- throw new Error('Invalid source type');
23
- }
24
- super();
25
- this.pause();
26
- readable.pipe(this);
27
- this.id = id;
28
- this.metadata = blob.metadata;
29
- }
11
+ id;
12
+ metadata;
13
+ constructor(id, blob) {
14
+ let readable;
15
+ if (blob.source instanceof Readable) {
16
+ readable = blob.source;
17
+ } else if (blob.source instanceof ReadableStream) {
18
+ readable = Readable.fromWeb(blob.source);
19
+ } else {
20
+ throw new Error("Invalid source type");
21
+ }
22
+ super();
23
+ this.pause();
24
+ readable.pipe(this);
25
+ this.id = id;
26
+ this.metadata = blob.metadata;
27
+ }
30
28
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server/stream.ts"],"sourcesContent":["import {\n PassThrough,\n Readable,\n type ReadableOptions,\n type TransformOptions,\n} from 'node:stream'\nimport { ReadableStream } from 'node:stream/web'\nimport type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'\n\nexport class ProtocolClientStream extends Readable {\n constructor(\n public readonly id: number,\n public readonly metadata: ProtocolBlobMetadata,\n options?: ReadableOptions,\n ) {\n super(options)\n }\n}\n\nexport class ProtocolServerStream extends PassThrough {\n public readonly id: number\n public readonly metadata: ProtocolBlobMetadata\n\n constructor(id: number, blob: ProtocolBlob) {\n let readable: Readable\n\n if (blob.source instanceof Readable) {\n readable = blob.source\n } else if (blob.source instanceof ReadableStream) {\n readable = Readable.fromWeb(blob.source as ReadableStream)\n } else {\n throw new Error('Invalid source type')\n }\n\n super()\n\n this.pause()\n readable.pipe(this)\n\n this.id = id\n this.metadata = blob.metadata\n }\n}\n"],"names":["PassThrough","Readable","ReadableStream","ProtocolClientStream","constructor","id","metadata","options","ProtocolServerStream","blob","readable","source","fromWeb","Error","pause","pipe"],"mappings":"AAAA,SACEA,WAAW,EACXC,QAAQ,QAGH,cAAa;AACpB,SAASC,cAAc,QAAQ,kBAAiB;AAGhD,OAAO,MAAMC,6BAA6BF;;;IACxCG,YACE,AAAgBC,EAAU,EAC1B,AAAgBC,QAA8B,EAC9CC,OAAyB,CACzB;QACA,KAAK,CAACA;aAJUF,KAAAA;aACAC,WAAAA;IAIlB;AACF;AAEA,OAAO,MAAME,6BAA6BR;IACxBK,GAAU;IACVC,SAA8B;IAE9CF,YAAYC,EAAU,EAAEI,IAAkB,CAAE;QAC1C,IAAIC;QAEJ,IAAID,KAAKE,MAAM,YAAYV,UAAU;YACnCS,WAAWD,KAAKE,MAAM;QACxB,OAAO,IAAIF,KAAKE,MAAM,YAAYT,gBAAgB;YAChDQ,WAAWT,SAASW,OAAO,CAACH,KAAKE,MAAM;QACzC,OAAO;YACL,MAAM,IAAIE,MAAM;QAClB;QAEA,KAAK;QAEL,IAAI,CAACC,KAAK;QACVJ,SAASK,IAAI,CAAC,IAAI;QAElB,IAAI,CAACV,EAAE,GAAGA;QACV,IAAI,CAACC,QAAQ,GAAGG,KAAKH,QAAQ;IAC/B;AACF"}
1
+ {"mappings":"AAAA,SACE,aACA,gBAGK,aAAa;AACpB,SAAS,sBAAsB,iBAAiB;AAGhD,OAAO,MAAM,6BAA6B,SAAS;CACjD,YACkBA,IACAC,UAChBC,SACA;AACA,QAAM,QAAQ;OAJE;OACA;CAIjB;AACF;AAED,OAAO,MAAM,6BAA6B,YAAY;CACpD,AAAgB;CAChB,AAAgB;CAEhB,YAAYF,IAAYG,MAAoB;EAC1C,IAAIC;AAEJ,MAAI,KAAK,kBAAkB,UAAU;AACnC,cAAW,KAAK;EACjB,WAAU,KAAK,kBAAkB,gBAAgB;AAChD,cAAW,SAAS,QAAQ,KAAK,OAAyB;EAC3D,OAAM;AACL,SAAM,IAAI,MAAM;EACjB;AAED,SAAO;AAEP,OAAK,OAAO;AACZ,WAAS,KAAK,KAAK;AAEnB,OAAK,KAAK;AACV,OAAK,WAAW,KAAK;CACtB;AACF","names":["id: number","metadata: ProtocolBlobMetadata","options?: ReadableOptions","blob: ProtocolBlob","readable: Readable"],"sources":["src/server/stream.ts"],"sourcesContent":["import {\n PassThrough,\n Readable,\n type ReadableOptions,\n type TransformOptions,\n} from 'node:stream'\nimport { ReadableStream } from 'node:stream/web'\nimport type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'\n\nexport class ProtocolClientStream extends Readable {\n constructor(\n public readonly id: number,\n public readonly metadata: ProtocolBlobMetadata,\n options?: ReadableOptions,\n ) {\n super(options)\n }\n}\n\nexport class ProtocolServerStream extends PassThrough {\n public readonly id: number\n public readonly metadata: ProtocolBlobMetadata\n\n constructor(id: number, blob: ProtocolBlob) {\n let readable: Readable\n\n if (blob.source instanceof Readable) {\n readable = blob.source\n } else if (blob.source instanceof ReadableStream) {\n readable = Readable.fromWeb(blob.source as ReadableStream)\n } else {\n throw new Error('Invalid source type')\n }\n\n super()\n\n this.pause()\n readable.pipe(this)\n\n this.id = id\n this.metadata = blob.metadata\n }\n}\n"],"version":3}
@@ -1,7 +1,7 @@
1
1
  import { kTransportPlugin } from "./constants.js";
2
- export const createTransport = (name, init)=>({
3
- name,
4
- init,
5
- [kTransportPlugin]: true
6
- });
7
- export const isTransportPlugin = (plugin)=>kTransportPlugin in plugin;
2
+ export const createTransport = (name, init) => ({
3
+ name,
4
+ init,
5
+ [kTransportPlugin]: true
6
+ });
7
+ export const isTransportPlugin = (plugin) => kTransportPlugin in plugin;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server/transport.ts"],"sourcesContent":["import type { Async } from '@nmtjs/common'\nimport type { BasePlugin, PluginContext } from '@nmtjs/core'\nimport type { ServerMessageType } from '../common/enums.ts'\nimport type { Connection } from './connection.ts'\nimport { kTransportPlugin } from './constants.ts'\nimport type { Protocol } from './protocol.ts'\nimport type { ProtocolRegistry } from './registry.ts'\n\nexport interface Transport<T = unknown> {\n start: () => Promise<void>\n stop: () => Promise<void>\n send: (\n connection: Connection<T>,\n messageType: ServerMessageType,\n buffer: ArrayBuffer,\n ) => Async<void>\n}\n\nexport interface TransportPluginContext extends PluginContext {\n protocol: Protocol\n registry: ProtocolRegistry\n}\n\nexport interface TransportPlugin<Type = unknown, Options = unknown>\n extends BasePlugin<Transport<Type>, Options, TransportPluginContext> {\n [kTransportPlugin]: any\n}\n\nexport const createTransport = <Type = unknown, Options = unknown>(\n name: string,\n init: TransportPlugin<Type, Options>['init'],\n): TransportPlugin<Type, Options> => ({ name, init, [kTransportPlugin]: true })\n\nexport const isTransportPlugin = (\n plugin: BasePlugin<any, any, any>,\n): plugin is TransportPlugin => kTransportPlugin in plugin\n"],"names":["kTransportPlugin","createTransport","name","init","isTransportPlugin","plugin"],"mappings":"AAIA,SAASA,gBAAgB,QAAQ,iBAAgB;AAwBjD,OAAO,MAAMC,kBAAkB,CAC7BC,MACAC,OACoC,CAAA;QAAED;QAAMC;QAAM,CAACH,iBAAiB,EAAE;IAAK,CAAA,EAAE;AAE/E,OAAO,MAAMI,oBAAoB,CAC/BC,SAC8BL,oBAAoBK,OAAM"}
1
+ {"mappings":"AAIA,SAAS,wBAAwB,gBAAgB;AAwBjD,OAAO,MAAM,kBAAkB,CAC7BA,MACAC,UACoC;CAAE;CAAM;EAAO,mBAAmB;AAAM;AAE9E,OAAO,MAAM,oBAAoB,CAC/BC,WAC8B,oBAAoB","names":["name: string","init: TransportPlugin<Type, Options>['init']","plugin: BasePlugin<any, any, any>"],"sources":["src/server/transport.ts"],"sourcesContent":["import type { Async } from '@nmtjs/common'\nimport type { BasePlugin, PluginContext } from '@nmtjs/core'\nimport type { ServerMessageType } from '../common/enums.ts'\nimport type { Connection } from './connection.ts'\nimport { kTransportPlugin } from './constants.ts'\nimport type { Protocol } from './protocol.ts'\nimport type { ProtocolRegistry } from './registry.ts'\n\nexport interface Transport<T = unknown> {\n start: () => Promise<void>\n stop: () => Promise<void>\n send: (\n connection: Connection<T>,\n messageType: ServerMessageType,\n buffer: ArrayBuffer,\n ) => Async<void>\n}\n\nexport interface TransportPluginContext extends PluginContext {\n protocol: Protocol\n registry: ProtocolRegistry\n}\n\nexport interface TransportPlugin<Type = unknown, Options = unknown>\n extends BasePlugin<Transport<Type>, Options, TransportPluginContext> {\n [kTransportPlugin]: any\n}\n\nexport const createTransport = <Type = unknown, Options = unknown>(\n name: string,\n init: TransportPlugin<Type, Options>['init'],\n): TransportPlugin<Type, Options> => ({ name, init, [kTransportPlugin]: true })\n\nexport const isTransportPlugin = (\n plugin: BasePlugin<any, any, any>,\n): plugin is TransportPlugin => kTransportPlugin in plugin\n"],"version":3}
@@ -1,10 +1,10 @@
1
- export const getFormat = (format, { acceptType, contentType })=>{
2
- const encoder = contentType ? format.supportsEncoder(contentType) : undefined;
3
- if (!encoder) throw new Error('Unsupported content-type');
4
- const decoder = acceptType ? format.supportsDecoder(acceptType) : undefined;
5
- if (!decoder) throw new Error('Unsupported accept');
6
- return {
7
- encoder,
8
- decoder
9
- };
1
+ export const getFormat = (format, { acceptType, contentType }) => {
2
+ const encoder = contentType ? format.supportsEncoder(contentType) : undefined;
3
+ if (!encoder) throw new Error("Unsupported content-type");
4
+ const decoder = acceptType ? format.supportsDecoder(acceptType) : undefined;
5
+ if (!decoder) throw new Error("Unsupported accept");
6
+ return {
7
+ encoder,
8
+ decoder
9
+ };
10
10
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server/utils.ts"],"sourcesContent":["import type { Format } from './format.ts'\n\nexport type ResolveFormatParams = {\n contentType?: string | null\n acceptType?: string | null\n}\n\nexport const getFormat = (\n format: Format,\n { acceptType, contentType }: ResolveFormatParams,\n) => {\n const encoder = contentType ? format.supportsEncoder(contentType) : undefined\n if (!encoder) throw new Error('Unsupported content-type')\n\n const decoder = acceptType ? format.supportsDecoder(acceptType) : undefined\n if (!decoder) throw new Error('Unsupported accept')\n\n return {\n encoder,\n decoder,\n }\n}\n"],"names":["getFormat","format","acceptType","contentType","encoder","supportsEncoder","undefined","Error","decoder","supportsDecoder"],"mappings":"AAOA,OAAO,MAAMA,YAAY,CACvBC,QACA,EAAEC,UAAU,EAAEC,WAAW,EAAuB;IAEhD,MAAMC,UAAUD,cAAcF,OAAOI,eAAe,CAACF,eAAeG;IACpE,IAAI,CAACF,SAAS,MAAM,IAAIG,MAAM;IAE9B,MAAMC,UAAUN,aAAaD,OAAOQ,eAAe,CAACP,cAAcI;IAClE,IAAI,CAACE,SAAS,MAAM,IAAID,MAAM;IAE9B,OAAO;QACLH;QACAI;IACF;AACF,EAAC"}
1
+ {"mappings":"AAOA,OAAO,MAAM,YAAY,CACvBA,QACA,EAAE,YAAY,aAAkC,KAC7C;CACH,MAAM,UAAU,cAAc,OAAO,gBAAgB,YAAY,GAAG;AACpE,MAAK,QAAS,OAAM,IAAI,MAAM;CAE9B,MAAM,UAAU,aAAa,OAAO,gBAAgB,WAAW,GAAG;AAClE,MAAK,QAAS,OAAM,IAAI,MAAM;AAE9B,QAAO;EACL;EACA;CACD;AACF","names":["format: Format"],"sources":["src/server/utils.ts"],"sourcesContent":["import type { Format } from './format.ts'\n\nexport type ResolveFormatParams = {\n contentType?: string | null\n acceptType?: string | null\n}\n\nexport const getFormat = (\n format: Format,\n { acceptType, contentType }: ResolveFormatParams,\n) => {\n const encoder = contentType ? format.supportsEncoder(contentType) : undefined\n if (!encoder) throw new Error('Unsupported content-type')\n\n const decoder = acceptType ? format.supportsDecoder(acceptType) : undefined\n if (!decoder) throw new Error('Unsupported accept')\n\n return {\n encoder,\n decoder,\n }\n}\n"],"version":3}
package/package.json CHANGED
@@ -3,36 +3,32 @@
3
3
  "type": "module",
4
4
  "exports": {
5
5
  "./common": {
6
- "bun": "./lib/common/index.ts",
7
- "default": "./dist/common/index.js",
8
- "types": "./lib/common/index.ts"
6
+ "types": "./src/common/index.ts",
7
+ "import": "./dist/common/index.js"
9
8
  },
10
9
  "./server": {
11
- "bun": "./lib/server/index.ts",
12
- "default": "./dist/server/index.js",
13
- "types": "./lib/server/index.ts"
10
+ "types": "./src/server/index.ts",
11
+ "import": "./dist/server/index.js"
14
12
  },
15
13
  "./client": {
16
- "bun": "./lib/client/index.ts",
17
- "default": "./dist/client/index.js",
18
- "types": "./lib/client/index.ts"
14
+ "types": "./src/client/index.ts",
15
+ "import": "./dist/client/index.js"
19
16
  }
20
17
  },
21
18
  "dependencies": {
22
- "@nmtjs/common": "0.6.4",
23
- "@nmtjs/core": "0.6.4",
24
- "@nmtjs/type": "0.6.4"
19
+ "@nmtjs/common": "0.7.0",
20
+ "@nmtjs/type": "0.7.0",
21
+ "@nmtjs/core": "0.7.0"
25
22
  },
26
23
  "files": [
27
- "index.ts",
28
- "lib",
24
+ "src",
29
25
  "dist",
30
26
  "LICENSE.md",
31
27
  "README.md"
32
28
  ],
33
- "version": "0.6.4",
29
+ "version": "0.7.0",
34
30
  "scripts": {
35
- "build": "neemata-build --root=./lib './**/*.ts'",
31
+ "build": "neemata-build --root=./src './**/*.ts'",
36
32
  "type-check": "tsc --noEmit"
37
33
  }
38
34
  }
@@ -81,10 +81,18 @@ export class ProtocolClientStreams {
81
81
  }
82
82
 
83
83
  end(streamId: number) {
84
- const stream = this.get(streamId)
85
- stream.end()
84
+ this.get(streamId).end()
86
85
  this.remove(streamId)
87
86
  }
87
+
88
+ clear(error?: Error) {
89
+ if (error) {
90
+ for (const stream of this.#collection.values()) {
91
+ stream.abort(error)
92
+ }
93
+ }
94
+ this.#collection.clear()
95
+ }
88
96
  }
89
97
 
90
98
  export class ProtocolServerStreams {
@@ -127,12 +135,26 @@ export class ProtocolServerStreams {
127
135
  stream.end()
128
136
  this.remove(streamId)
129
137
  }
138
+
139
+ clear(error?: Error) {
140
+ if (error) {
141
+ for (const stream of this.#collection.values()) {
142
+ stream.abort(error)
143
+ }
144
+ }
145
+ this.#collection.clear()
146
+ }
130
147
  }
131
148
 
132
149
  export interface ProtocolTransport
133
- extends EventEmitter<{
134
- [K in `${ServerMessageType}`]: [ArrayBuffer]
135
- }> {
150
+ extends EventEmitter<
151
+ {
152
+ [K in `${ServerMessageType}`]: [ArrayBuffer]
153
+ } & {
154
+ connected: []
155
+ disconnected: []
156
+ }
157
+ > {
136
158
  connect(
137
159
  auth: any,
138
160
  contentType: BaseClientFormat['contentType'],
@@ -354,6 +376,10 @@ export abstract class ProtocolBaseClient<
354
376
  }
355
377
  },
356
378
  )
379
+
380
+ this.#transport.on('disconnected', () => {
381
+ this.#clear()
382
+ })
357
383
  }
358
384
 
359
385
  async connect(auth: any) {
@@ -361,6 +387,7 @@ export abstract class ProtocolBaseClient<
361
387
  }
362
388
 
363
389
  async disconnect() {
390
+ this.#clear()
364
391
  return await this.#transport.disconnect()
365
392
  }
366
393
 
@@ -373,6 +400,20 @@ export abstract class ProtocolBaseClient<
373
400
  return await this.#transport.send(messageType, buffer)
374
401
  }
375
402
 
403
+ async #clear() {
404
+ const error = new ProtocolError(
405
+ ErrorCode.ConnectionError,
406
+ 'Connection closed',
407
+ )
408
+ for (const call of this.#calls.values()) call.reject(error)
409
+ this.#calls.clear()
410
+ this.#serverStreams.clear(error)
411
+ this.#clientStreams.clear(error)
412
+ this.#rpcStreams.clear(error)
413
+ this.#callId = 0
414
+ this.#streamId = 0
415
+ }
416
+
376
417
  protected async _call(
377
418
  namespace: string,
378
419
  procedure: string,
@@ -1,5 +1,5 @@
1
1
  import { type Callback, defer } from '@nmtjs/common'
2
- import { encodeText } from '../common/binary.ts'
2
+ import { concat, encodeText } from '../common/binary.ts'
3
3
  import type { ProtocolBlobMetadata } from '../common/blob.ts'
4
4
 
5
5
  export class ProtocolClientBlobStream extends TransformStream<
@@ -38,31 +38,31 @@ export class ProtocolClientBlobStream extends TransformStream<
38
38
  }
39
39
 
40
40
  async read(size: number) {
41
- if (this.#queue.byteLength >= size) {
42
- const chunk = this.#queue.slice(0, size)
43
- const remaining = this.#queue.slice(size)
44
- this.#queue = remaining
45
- return chunk
46
- } else {
41
+ while (this.#queue.byteLength < size) {
47
42
  const { done, value } = await this.#reader.read()
48
- if (!done) {
49
- const buffer = value as ArrayBuffer
50
- const chunk = buffer.slice(0, size)
51
- const remaining = buffer.slice(size)
52
- this.#queue = remaining
53
- return chunk
43
+ if (done) {
44
+ if (this.#queue.byteLength > 0) {
45
+ const chunk = this.#queue
46
+ this.#queue = new ArrayBuffer(0)
47
+ return chunk
48
+ }
49
+ return null
54
50
  }
55
- return null
51
+ const buffer = value as ArrayBuffer
52
+ this.#queue = concat(this.#queue, buffer)
56
53
  }
54
+ const chunk = this.#queue.slice(0, size)
55
+ this.#queue = this.#queue.slice(size)
56
+ return chunk
57
57
  }
58
58
 
59
59
  abort(error = new Error('Stream aborted')) {
60
60
  this.#reader.cancel(error)
61
+ this.source.cancel(error)
61
62
  }
62
63
 
63
- async end() {
64
- if (!this.writable.locked) await this.writable.close()
65
- else await this.writable.abort(new Error('Stream closed'))
64
+ end() {
65
+ return this.source.cancel('Stream ended')
66
66
  }
67
67
  }
68
68
 
@@ -91,7 +91,7 @@ export class ProtocolServerStream<T = any>
91
91
  }
92
92
  }
93
93
 
94
- async push(chunk: any) {
94
+ async push(chunk: T) {
95
95
  await this.#writer.write(chunk)
96
96
  }
97
97
 
@@ -112,12 +112,4 @@ export class ProtocolServerBlobStream extends ProtocolServerStream<ArrayBuffer>
112
112
  ) {
113
113
  super(start)
114
114
  }
115
-
116
- push(chunk: ArrayBuffer) {
117
- return super.push(chunk)
118
- }
119
-
120
- end() {
121
- return super.end()
122
- }
123
115
  }
@@ -1,6 +1,6 @@
1
1
  import { randomUUID } from 'node:crypto'
2
- import type { InteractivePromise } from '@nmtjs/common'
3
2
  import type { Container } from '@nmtjs/core'
3
+ import type { InteractivePromise } from '../../../common/src/index.ts'
4
4
  import type { ProtocolApiCallResult } from './api.ts'
5
5
  import type { BaseServerDecoder, BaseServerEncoder } from './format.ts'
6
6
  import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts'
@@ -104,7 +104,7 @@ export class ProtocolConnections {
104
104
  context
105
105
 
106
106
  for (const call of calls.values()) {
107
- call.reject(new Error('Connection closed'))
107
+ call.abort(new Error('Connection closed'))
108
108
  }
109
109
 
110
110
  for (const stream of clientStreams.values()) {
@@ -279,12 +279,16 @@ export class Protocol {
279
279
  const stream = this.serverStreams.add(connectionId, id, blob)
280
280
  stream.on('data', (chunk) => {
281
281
  stream.pause()
282
+ const buf = Buffer.from(chunk)
282
283
  transport.send(
283
284
  connection,
284
285
  ServerMessageType.ServerStreamPush,
285
286
  concat(
286
287
  encodeNumber(id, 'Uint32'),
287
- Buffer.from(chunk).buffer as ArrayBuffer,
288
+ (buf.buffer as ArrayBuffer).slice(
289
+ buf.byteOffset,
290
+ buf.byteOffset + buf.byteLength,
291
+ ),
288
292
  ),
289
293
  )
290
294
  })
@@ -400,13 +404,19 @@ export class Protocol {
400
404
 
401
405
  const rpc = format.decoder.decodeRPC(buffer, {
402
406
  addStream: (id, metadata) => {
403
- return this.clientStreams.add(connectionId, id, metadata, (size) => {
404
- transport.send(
405
- connection,
406
- ServerMessageType.ClientStreamPull,
407
- concat(encodeNumber(id, 'Uint32'), encodeNumber(size, 'Uint32')),
408
- )
409
- })
407
+ const stream = this.clientStreams.add(
408
+ connectionId,
409
+ id,
410
+ metadata,
411
+ (size) => {
412
+ transport.send(
413
+ connection,
414
+ ServerMessageType.ClientStreamPull,
415
+ concat(encodeNumber(id, 'Uint32'), encodeNumber(size, 'Uint32')),
416
+ )
417
+ },
418
+ )
419
+ return stream
410
420
  },
411
421
  getStream: (id) => {
412
422
  return this.serverStreams.get(connectionId, id)
@@ -0,0 +1,3 @@
1
+ import { Registry } from '@nmtjs/core'
2
+
3
+ export class ProtocolRegistry extends Registry {}
@@ -1,24 +0,0 @@
1
- import { Registry } from '@nmtjs/core'
2
- import type { BaseType, BaseTypeAny } from '@nmtjs/type'
3
- import { type Compiled, compile } from '@nmtjs/type/compiler'
4
-
5
- export class ProtocolRegistry extends Registry {
6
- readonly types = new Set<BaseType>()
7
- readonly compiled = new Map<any, Compiled>()
8
-
9
- registerType(type: BaseTypeAny) {
10
- this.types.add(type)
11
- }
12
-
13
- compile() {
14
- for (const type of this.types) {
15
- this.compiled.set(type, compile(type))
16
- }
17
- }
18
-
19
- clear() {
20
- super.clear()
21
- this.types.clear()
22
- this.compiled.clear()
23
- }
24
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes