@nmtjs/protocol 0.10.2 → 0.10.4
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.
- package/dist/client/stream.js +10 -5
- package/dist/client/stream.js.map +1 -1
- package/dist/common/blob.js +1 -1
- package/dist/common/blob.js.map +1 -1
- package/dist/server/api.js.map +1 -1
- package/dist/server/connection.js.map +1 -1
- package/dist/server/protocol.js +0 -2
- package/dist/server/protocol.js.map +1 -1
- package/dist/server/transport.js.map +1 -1
- package/dist/server/types.js.map +1 -1
- package/package.json +7 -7
- package/src/client/stream.ts +13 -5
- package/src/common/blob.ts +1 -1
- package/src/server/api.ts +1 -7
- package/src/server/connection.ts +0 -2
- package/src/server/protocol.ts +0 -2
- package/src/server/transport.ts +0 -1
- package/src/server/types.ts +0 -1
package/dist/client/stream.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defer
|
|
1
|
+
import { defer } from "@nmtjs/common";
|
|
2
2
|
import { concat, encodeText } from "../common/binary.js";
|
|
3
3
|
export class ProtocolClientBlobStream extends TransformStream {
|
|
4
4
|
#queue;
|
|
@@ -53,6 +53,11 @@ export class ProtocolClientBlobStream extends TransformStream {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
export class ProtocolServerStream extends TransformStream {
|
|
56
|
+
#writer;
|
|
57
|
+
constructor(options) {
|
|
58
|
+
super(options);
|
|
59
|
+
this.#writer = this.writable.getWriter();
|
|
60
|
+
}
|
|
56
61
|
async *[Symbol.asyncIterator]() {
|
|
57
62
|
const reader = this.readable.getReader();
|
|
58
63
|
while (true) {
|
|
@@ -62,13 +67,13 @@ export class ProtocolServerStream extends TransformStream {
|
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
async push(chunk) {
|
|
65
|
-
await this.
|
|
70
|
+
await this.#writer.write(chunk);
|
|
66
71
|
}
|
|
67
72
|
async end() {
|
|
68
|
-
await this.
|
|
73
|
+
await this.#writer.close();
|
|
69
74
|
}
|
|
70
|
-
abort(error = new Error("Stream aborted")) {
|
|
71
|
-
this.
|
|
75
|
+
async abort(error = new Error("Stream aborted")) {
|
|
76
|
+
await this.#writer.abort(error);
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
export class ProtocolServerBlobStream extends ProtocolServerStream {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,
|
|
1
|
+
{"mappings":"AAAA,SAAS,aAAa,eAAe;AACrC,SAAS,QAAQ,kBAAkB,qBAAqB;AAGxD,OAAO,MAAM,iCAAiC,gBAG5C;CACA;CACA;CAEA,YACWA,QACAC,IACAC,UACT;AACA,QAAM;GACJ,OAAO,MAAM;AACX,UAAM,MAAM,OAAO,YAAY,KAAK,CAAC;GACtC;GACD,WAAW,CAAC,OAAO,eAAe;AAChC,QAAI,iBAAiB,aAAa;AAChC,gBAAW,QAAQ,MAAM;IAC1B,WAAU,iBAAiB,YAAY;AACtC,gBAAW,QAAQ,MAAM,OAAiC;IAC3D,kBAAiB,UAAU,UAAU;AACpC,gBAAW,QAAQ,WAAW,MAAM,CAAC;IACtC,OAAM;AACL,WAAM,IAAI,MACR;IAEH;GACF;EACF,EAAC;OArBO;OACA;OACA;AAqBT,OAAKC,SAAS,IAAI,YAAY;AAC9B,OAAKC,UAAU,KAAK,SAAS,WAAW;CACzC;CAED,MAAM,KAAKC,MAAc;AACvB,SAAO,KAAKF,OAAO,aAAa,MAAM;GACpC,MAAM,EAAE,MAAM,OAAO,GAAG,MAAM,KAAKC,QAAQ,MAAM;AACjD,OAAI,MAAM;AACR,QAAI,KAAKD,OAAO,aAAa,GAAG;KAC9B,MAAM,QAAQ,KAAKA;AACnB,UAAKA,SAAS,IAAI,YAAY;AAC9B,YAAO;IACR;AACD,WAAO;GACR;GACD,MAAM,SAAS;AACf,QAAKA,SAAS,OAAO,KAAKA,QAAQ,OAAO;EAC1C;EACD,MAAM,QAAQ,KAAKA,OAAO,MAAM,GAAG,KAAK;AACxC,OAAKA,SAAS,KAAKA,OAAO,MAAM,KAAK;AACrC,SAAO;CACR;CAED,MAAM,QAAQ,IAAI,MAAM,mBAAmB;AACzC,OAAKC,QAAQ,OAAO,MAAM;AAC1B,OAAK,OAAO,OAAO,MAAM;CAC1B;CAED,MAAM;AACJ,SAAO,KAAK,OAAO,OAAO,eAAe;CAC1C;AACF;AAOD,OAAO,MAAM,6BACH,gBAEV;CACE;CAEA,YAAYE,SAA+B;AACzC,QAAM,QAAQ;AAEd,OAAKC,UAAU,KAAK,SAAS,WAAW;CACzC;CAED,QAAQ,OAAO,iBAAiB;EAC9B,MAAM,SAAS,KAAK,SAAS,WAAW;AACxC,SAAO,MAAM;GACX,MAAM,EAAE,MAAM,OAAO,GAAG,MAAM,OAAO,MAAM;AAC3C,QAAK,KAAM,OAAM;OACZ;EACN;CACF;CAED,MAAM,KAAKC,OAAU;AACnB,QAAM,KAAKD,QAAQ,MAAM,MAAM;CAChC;CAED,MAAM,MAAM;AACV,QAAM,KAAKA,QAAQ,OAAO;CAC3B;CAED,MAAM,MAAM,QAAQ,IAAI,MAAM,mBAAmB;AAC/C,QAAM,KAAKA,QAAQ,MAAM,MAAM;CAChC;AACF;AAED,OAAO,MAAM,iCAAiC,qBAAkC;CAC9E,YACWN,IACAC,UACTO,SACA;AACA,QAAM,QAAQ;OAJL;OACA;CAIV;AACF","names":["source: ReadableStream","id: number","metadata: ProtocolBlobMetadata","#queue","#reader","size: number","options?: Transformer<any, T>","#writer","chunk: T","options?: Transformer<any, ArrayBuffer>"],"sources":["../../src/client/stream.ts"],"sourcesContent":["import { defer } from '@nmtjs/common'\nimport { concat, encodeText } from '../common/binary.ts'\nimport type { ProtocolBlobMetadata } from '../common/blob.ts'\n\nexport class ProtocolClientBlobStream extends TransformStream<\n any,\n ArrayBuffer\n> {\n #queue: ArrayBuffer\n #reader: ReadableStreamDefaultReader\n\n constructor(\n readonly source: ReadableStream,\n readonly id: number,\n readonly metadata: ProtocolBlobMetadata,\n ) {\n super({\n start: () => {\n defer(() => source.pipeThrough(this))\n },\n transform: (chunk, controller) => {\n if (chunk instanceof ArrayBuffer) {\n controller.enqueue(chunk)\n } else if (chunk instanceof Uint8Array) {\n controller.enqueue(chunk.buffer as unknown as ArrayBuffer)\n } else if (typeof chunk === 'string') {\n controller.enqueue(encodeText(chunk))\n } else {\n throw new Error(\n 'Invalid chunk data type. Expected ArrayBuffer, Uint8Array, or string.',\n )\n }\n },\n })\n\n this.#queue = new ArrayBuffer(0)\n this.#reader = this.readable.getReader()\n }\n\n async read(size: number) {\n while (this.#queue.byteLength < size) {\n const { done, value } = await this.#reader.read()\n if (done) {\n if (this.#queue.byteLength > 0) {\n const chunk = this.#queue\n this.#queue = new ArrayBuffer(0)\n return chunk\n }\n return null\n }\n const buffer = value as ArrayBuffer\n this.#queue = concat(this.#queue, buffer)\n }\n const chunk = this.#queue.slice(0, size)\n this.#queue = this.#queue.slice(size)\n return chunk\n }\n\n abort(error = new Error('Stream aborted')) {\n this.#reader.cancel(error)\n this.source.cancel(error)\n }\n\n end() {\n return this.source.cancel('Stream ended')\n }\n}\n\nexport interface ProtocolServerStreamInterface<T = any> {\n [Symbol.asyncIterator](): AsyncGenerator<T>\n abort(error?: Error): void\n}\n\nexport class ProtocolServerStream<T = any>\n extends TransformStream<any, T>\n implements ProtocolServerStreamInterface<T>\n{\n #writer: WritableStreamDefaultWriter\n\n constructor(options?: Transformer<any, T>) {\n super(options)\n\n this.#writer = this.writable.getWriter()\n }\n\n async *[Symbol.asyncIterator]() {\n const reader = this.readable.getReader()\n while (true) {\n const { done, value } = await reader.read()\n if (!done) yield value\n else break\n }\n }\n\n async push(chunk: T) {\n await this.#writer.write(chunk)\n }\n\n async end() {\n await this.#writer.close()\n }\n\n async abort(error = new Error('Stream aborted')) {\n await this.#writer.abort(error)\n }\n}\n\nexport class ProtocolServerBlobStream extends ProtocolServerStream<ArrayBuffer> {\n constructor(\n readonly id: number,\n readonly metadata: ProtocolBlobMetadata,\n options?: Transformer<any, ArrayBuffer>,\n ) {\n super(options)\n }\n}\n"],"version":3,"file":"stream.js"}
|
package/dist/common/blob.js
CHANGED
|
@@ -13,7 +13,7 @@ export class ProtocolBlob {
|
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
static from(source, metadata = {}) {
|
|
16
|
-
let _source
|
|
16
|
+
let _source;
|
|
17
17
|
if (source instanceof globalThis.ReadableStream) {
|
|
18
18
|
_source = source;
|
|
19
19
|
} else if ("File" in globalThis && source instanceof globalThis.File) {
|
package/dist/common/blob.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,OAAO,MAAMA,UAAyB,OAAO,IAAI,kBAAkB;AAcnE,OAAO,MAAM,aAA8C;CACzD,CAAU,WAAW;CAErB,AAAgB;CAChB,AAAgB;CAEhB,YACEC,QACAC,MACA,OAAO,4BACPC,UACA;AACA,aAAW,SAAS,eAAe,QAAQ,EACzC,OAAM,IAAI,MAAM;AAElB,OAAK,SAAS;AACd,OAAK,WAAW;GACd;GACA;GACA;EACD;CACF;CAED,OAAO,KACLF,QACAG,WAII,CAAE,GACN;EACA,IAAIC
|
|
1
|
+
{"mappings":"AAAA,OAAO,MAAMA,UAAyB,OAAO,IAAI,kBAAkB;AAcnE,OAAO,MAAM,aAA8C;CACzD,CAAU,WAAW;CAErB,AAAgB;CAChB,AAAgB;CAEhB,YACEC,QACAC,MACA,OAAO,4BACPC,UACA;AACA,aAAW,SAAS,eAAe,QAAQ,EACzC,OAAM,IAAI,MAAM;AAElB,OAAK,SAAS;AACd,OAAK,WAAW;GACd;GACA;GACA;EACD;CACF;CAED,OAAO,KACLF,QACAG,WAII,CAAE,GACN;EACA,IAAIC;AAEJ,MAAI,kBAAkB,WAAW,gBAAgB;AAC/C,aAAU;EACX,WAAU,UAAU,cAAc,kBAAkB,WAAW,MAAM;AACpE,aAAU,OAAO,QAAQ;AACzB,YAAS,OAAO,OAAO;AACvB,YAAS,WAAW,OAAO;EAC5B,WAAU,kBAAkB,WAAW,MAAM;AAC5C,aAAU,OAAO,QAAQ;AACzB,YAAS,OAAO,OAAO;EACxB,kBAAiB,WAAW,UAAU;GACrC,MAAM,OAAO,IAAI,KAAK,CAAC,MAAO;AAC9B,aAAU,KAAK,QAAQ;AACvB,YAAS,OAAO,KAAK;AACrB,YAAS,OAAO,SAAS,QAAQ;EAClC,WAAU,kBAAkB,WAAW,aAAa;GACnD,MAAM,OAAO,IAAI,KAAK,CAAC,MAAO;AAC9B,aAAU,KAAK,QAAQ;AACvB,YAAS,OAAO,KAAK;EACtB,OAAM;AACL,aAAU;EACX;AAED,SAAO,IAAI,aACT,SACA,SAAS,MACT,SAAS,MACT,SAAS;CAEZ;AACF","names":["BlobKey: unique symbol","source: any","size?: number","filename?: string","metadata: {\n size?: number\n type?: string\n filename?: string\n }","_source: any"],"sources":["../../src/common/blob.ts"],"sourcesContent":["export const BlobKey: unique symbol = Symbol.for('neemata:BlobKey')\nexport type BlobKey = typeof BlobKey\n\nexport type ProtocolBlobMetadata = {\n type: string\n size?: number\n filename?: string\n}\n\nexport interface ProtocolBlobInterface {\n readonly metadata: ProtocolBlobMetadata\n readonly [BlobKey]: true\n}\n\nexport class ProtocolBlob implements ProtocolBlobInterface {\n readonly [BlobKey] = true\n\n public readonly metadata: ProtocolBlobMetadata\n public readonly source: any\n\n constructor(\n source: any,\n size?: number,\n type = 'application/octet-stream',\n filename?: string,\n ) {\n if (typeof size !== 'undefined' && size <= 0)\n throw new Error('Blob size is invalid')\n\n this.source = source\n this.metadata = {\n size,\n type,\n filename,\n }\n }\n\n static from(\n source: any,\n metadata: {\n size?: number\n type?: string\n filename?: string\n } = {},\n ) {\n let _source: any\n\n if (source instanceof globalThis.ReadableStream) {\n _source = source\n } else if ('File' in globalThis && source instanceof globalThis.File) {\n _source = source.stream()\n metadata.size = source.size\n metadata.filename = source.name\n } else if (source instanceof globalThis.Blob) {\n _source = source.stream()\n metadata.size = source.size\n } else if (typeof source === 'string') {\n const blob = new Blob([source])\n _source = blob.stream()\n metadata.size = blob.size\n metadata.type = metadata.type || 'text/plain'\n } else if (source instanceof globalThis.ArrayBuffer) {\n const blob = new Blob([source])\n _source = blob.stream()\n metadata.size = blob.size\n } else {\n _source = source\n }\n\n return new ProtocolBlob(\n _source,\n metadata.size,\n metadata.type,\n metadata.filename,\n )\n }\n}\n"],"version":3,"file":"blob.js"}
|
package/dist/server/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":"AAoCA,OAAO,MAAM,mBAAmB,CAC9BA,WAC4C,cAAc;AAE5D,OAAO,MAAM,uBAAuB,CAClCA,WACgD,kBAAkB","names":["result: ProtocolApiCallResult"],"sources":["../../src/server/api.ts"],"sourcesContent":["import type { Container, Hook, MetadataStore } from '@nmtjs/core'\nimport type { Connection } from './connection.ts'\n\nexport type ProtocolApiCallOptions = {\n connection: Connection\n namespace: string\n procedure: string\n container: Container\n payload: any\n signal: AbortSignal\n metadata?: (metadata: MetadataStore) => void\n}\n\nexport type ProtocolAnyIterable<T> =\n | (() => AsyncGenerator<T>)\n | AsyncIterable<T>\n\nexport interface ProtocolApiCallBaseResult {\n output: unknown\n}\nexport interface ProtocolApiCallSubscriptionResult\n extends ProtocolApiCallBaseResult {\n subscription: never\n}\n\nexport interface ProtocolApiCallIterableResult\n extends ProtocolApiCallBaseResult {\n iterable: ProtocolAnyIterable<unknown>\n onFinish?: () => void\n}\n\nexport type ProtocolApiCallResult =\n | ProtocolApiCallBaseResult\n | ProtocolApiCallSubscriptionResult\n | ProtocolApiCallIterableResult\n\nexport const isIterableResult = (\n result: ProtocolApiCallResult,\n): result is ProtocolApiCallIterableResult => 'iterable' in result\n\nexport const isSubscriptionResult = (\n result: ProtocolApiCallResult,\n): result is ProtocolApiCallSubscriptionResult => 'subscription' in result\n\nexport interface ProtocolApi {\n call(options: ProtocolApiCallOptions): Promise<ProtocolApiCallResult>\n}\n\ndeclare module '@nmtjs/core' {\n export interface HookType {\n [Hook.OnConnect]: (connection: Connection) => any\n [Hook.OnDisconnect]: (connection: Connection) => any\n }\n}\n"],"version":3,"file":"api.js"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,kBAAkB,aAAa;
|
|
1
|
+
{"mappings":"AAAA,SAAS,kBAAkB,aAAa;AAUxC,OAAO,MAAM,WAA2B;CACtC,AAAS;CACT,AAAS;CAET,YAAYA,SAAkC;AAC5C,OAAK,KAAK,QAAQ,MAAM,YAAY;AACpC,OAAK,OAAO,QAAQ;CACrB;AACF;AAED,OAAO,MAAM,kBAAkB;CAC7B,WAAW;CACX,QAAQ,IAAI;CACZ,gBAAgB,IAAI;CACpB,gBAAgB,IAAI;CACpB,aAAa,IAAI;CACjB;CACA;CAKA,YACEC,WACAC,QACA;AACA,OAAK,YAAY;AACjB,OAAK,SAAS;CACf;AACF","names":["options: ConnectionOptions<Data>","container: ConnectionContext['container']","format: ConnectionContext['format']"],"sources":["../../src/server/connection.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto'\nimport type { Container } from '@nmtjs/core'\nimport type { BaseServerDecoder, BaseServerEncoder } from './format.ts'\nimport type { ProtocolClientStream, ProtocolServerStream } from './stream.ts'\n\nexport type ConnectionOptions<Data = unknown> = {\n id?: string\n data: Data\n}\n\nexport class Connection<Data = unknown> {\n readonly id: string\n readonly data: Data\n\n constructor(options: ConnectionOptions<Data>) {\n this.id = options.id ?? randomUUID()\n this.data = options.data\n }\n}\n\nexport class ConnectionContext {\n streamId = 1\n calls = new Map<number, AbortController>()\n clientStreams = new Map<number, ProtocolClientStream>()\n serverStreams = new Map<number, ProtocolServerStream>()\n rpcStreams = new Map<number, AbortController>()\n container: Container\n format: {\n encoder: BaseServerEncoder\n decoder: BaseServerDecoder\n }\n\n constructor(\n container: ConnectionContext['container'],\n format: ConnectionContext['format'],\n ) {\n this.container = container\n this.format = format\n }\n}\n"],"version":3,"file":"connection.js"}
|
package/dist/server/protocol.js
CHANGED
|
@@ -158,7 +158,6 @@ export class ProtocolServerStreams {
|
|
|
158
158
|
serverStreams.delete(streamId);
|
|
159
159
|
}
|
|
160
160
|
pull(connectionId, streamId) {
|
|
161
|
-
console.log("Pulling stream", streamId);
|
|
162
161
|
const stream = this.get(connectionId, streamId);
|
|
163
162
|
stream.resume();
|
|
164
163
|
}
|
|
@@ -246,7 +245,6 @@ export class Protocol {
|
|
|
246
245
|
});
|
|
247
246
|
});
|
|
248
247
|
stream.on("end", () => {
|
|
249
|
-
console.log("Stream ended");
|
|
250
248
|
transport.send(connection, ServerMessageType.ServerStreamEnd, encodeNumber(streamId, "Uint32"), {
|
|
251
249
|
callId,
|
|
252
250
|
streamId
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAwB,OAAO,kBAAkB,eAAe;AAChE,SAGE,MAEA,aACK,aAAa;AACpB,SAAS,QAAQ,cAAc,oBAAoB,qBAAqB;AAExE,SAAS,WAAW,yBAAyB,oBAAoB;AAEjE,SACE,wBAGK,UAAU;AACjB,SACE,YACA,yBAEK,iBAAiB;AAExB,SAAS,2BAA2B,kBAAkB;AAEtD,SAAS,sBAAsB,4BAA4B,aAAa;AAExE,SAAS,iBAA2C,YAAY;AAEhE,OAAO,MAAM,sBAAsB,MAAM;CACvC;CACA;CAEA,YAAYA,MAAcC,SAAkBC,MAAY;AACtD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,OAAO;CACb;CAED,IAAI,UAAU;AACZ,UAAQ,EAAE,KAAK,KAAK,GAAG,MAAM,QAAQ;CACtC;CAED,WAAW;AACT,UAAQ,EAAE,KAAK,KAAK,GAAG,KAAK,QAAQ;CACrC;CAED,SAAS;AACP,SAAO;GACL,MAAM,KAAK;GACX,SAAS,KAAK;GACd,MAAM,KAAK;EACZ;CACF;AACF;AAMD,OAAO,MAAM,oBAAoB;CAC/B,AAASC,cAAc,IAAI;CAS3B,YACmBC,aAMjB;OANiB;CAMf;CAEJ,IAAIC,cAAsB;EACxB,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e,KAAK,cAAc,UAAU,MAAM;CAC/D;CAED,gBAAgBb,cAAsBM,UAAkB;AACtD,SAAO,KAAKO,eAAe,IAAI,cAAc,SAAS;CACvD;CAED,kBAAkBb,cAAsBM,UAAkBe,OAAe;AACvE,SAAO,KAAKR,eAAe,MAAM,cAAc,UAAU,MAAM;CAChE;CAED,gBAAgBb,cAAsBM,UAAkB;AACtD,SAAO,KAAKQ,eAAe,IAAI,cAAc,SAAS;CACvD;CAED,gBAAgBd,cAAsBM,UAAkBI,MAAoB;AAC1E,SAAO,KAAKI,eAAe,IAAI,cAAc,UAAU,KAAK;CAC7D;CAED,mBAAmBd,cAAsBM,UAAkB;AACzD,SAAO,KAAKQ,eAAe,OAAO,cAAc,SAAS;CAC1D;CAED,iBAAiBd,cAAsBM,UAAkB;AACvD,SAAO,KAAKQ,eAAe,KAAK,cAAc,SAAS;CACxD;CAED,kBAAkBd,cAAsBM,UAAkBe,OAAe;AACvE,SAAO,KAAKP,eAAe,MAAM,cAAc,UAAU,MAAM;CAChE;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: ProtocolConnectionTransport","options: ConnectionOptions<T>","params: ResolveFormatParams","connection: Connection","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 }","#connections","#clientStreams","#serverStreams","options: ProtocolApiCallOptions","rpc: ProtocolRPC","params: ProtocolRPCOptions","buffer: ArrayBuffer","callId: number","options: ConnectionOptions","error?: Error"],"sources":["../../src/server/protocol.ts"],"sourcesContent":["import { type Callback, defer, throwError } from '@nmtjs/common'\nimport {\n type AnyInjectable,\n type Container,\n Hook,\n type Logger,\n Scope,\n} 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 {\n isIterableResult,\n type ProtocolApi,\n type ProtocolApiCallOptions,\n} from './api.ts'\nimport {\n Connection,\n ConnectionContext,\n type ConnectionOptions,\n} from './connection.ts'\nimport type { Format } from './format.ts'\nimport { ProtocolInjectables } from './injectables.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 type ProtocolConnectionTransport = {\n send: Transport<any>['send']\n}\n\nexport class ProtocolConnections {\n readonly #collection = new Map<\n string,\n {\n connection: Connection\n context: ConnectionContext\n transport: ProtocolConnectionTransport\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: ProtocolConnectionTransport,\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 container.provide(ProtocolInjectables.connection, connection)\n try {\n await this.initialize(connection)\n this.#collection.set(connection.id, { connection, context, transport })\n return { connection, context }\n } catch (error) {\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n 'Error during disposing connection',\n )\n })\n throw error\n }\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 async initialize(connection: Connection) {\n await this.application.registry.hooks.call(\n Hook.OnConnect,\n { concurrent: false },\n connection,\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.write(Buffer.from(chunk))\n }\n\n end(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.end(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 console.log('Pulling stream', streamId)\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 type ProtocolRPCOptions = {\n signal?: AbortSignal\n provides?: [AnyInjectable, any][]\n metadata?: ProtocolApiCallOptions['metadata']\n}\n\nexport class Protocol {\n #connections: ProtocolConnections\n #clientStreams: ProtocolClientStreams\n #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 call(options: ProtocolApiCallOptions) {\n const { container, connection } = options\n try {\n return await this.application.api.call(options)\n } catch (error) {\n if (error instanceof ProtocolError === false) {\n this.application.logger.error(\n { error, connection },\n 'Error during RPC call',\n )\n throw new ProtocolError(\n ErrorCode.InternalServerError,\n 'Internal server error',\n )\n }\n throw error\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 rpc(\n connectionId: string,\n rpc: ProtocolRPC,\n params: ProtocolRPCOptions = {},\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 calls.set(callId, abortController)\n\n const callIdEncoded = encodeNumber(callId, 'Uint32')\n const container = context.container.fork(Scope.Call)\n\n if (params.provides) {\n for (const [key, value] of params.provides) {\n container.provide(key, value)\n }\n }\n\n try {\n const response = await this.call({\n connection,\n container,\n namespace,\n payload,\n procedure,\n signal,\n metadata: params.metadata,\n })\n\n const responseEncoded = format.encoder.encodeRPC(\n {\n callId,\n result: response.output,\n },\n {\n addStream: (blob) => {\n const streamId = context.streamId++\n const stream = this.#serverStreams.add(connectionId, streamId, 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(streamId, 'Uint32'),\n (buf.buffer as ArrayBuffer).slice(\n buf.byteOffset,\n buf.byteOffset + buf.byteLength,\n ),\n ),\n { callId, streamId },\n )\n })\n stream.on('error', (err) => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamAbort,\n encodeNumber(streamId, 'Uint32'),\n { callId, streamId },\n )\n })\n stream.on('end', () => {\n console.log('Stream ended')\n transport.send(\n connection,\n ServerMessageType.ServerStreamEnd,\n encodeNumber(streamId, 'Uint32'),\n { callId, streamId },\n )\n })\n return stream\n },\n getStream: (id) => {\n return this.#serverStreams.get(connectionId, id)\n },\n },\n )\n\n if ('subscription' in response) {\n throwError('Unimplemented')\n } else if (isIterableResult(response)) {\n transport.send(\n connection,\n ServerMessageType.RpcStreamResponse,\n responseEncoded,\n { callId },\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 ab.signal.throwIfAborted()\n const chunkEncoded = format.encoder.encode(chunk)\n transport.send(\n connection,\n ServerMessageType.RpcStreamChunk,\n concat(callIdEncoded, chunkEncoded),\n { callId },\n )\n }\n transport.send(\n connection,\n ServerMessageType.RpcStreamEnd,\n callIdEncoded,\n { callId },\n )\n } catch (error) {\n this.application.logger.error(error)\n transport.send(\n connection,\n ServerMessageType.RpcStreamAbort,\n callIdEncoded,\n { callId },\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 { callId },\n )\n }\n } catch (error) {\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\n transport.send(connection, ServerMessageType.RpcResponse, payload, {\n error,\n callId,\n })\n } finally {\n calls.delete(callId)\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n 'Error during disposing connection',\n )\n })\n }\n }\n\n async rpcRaw(\n connectionId: string,\n buffer: ArrayBuffer,\n params: ProtocolRPCOptions = {},\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: (streamId, callId, metadata) => {\n return this.#clientStreams.add(\n connectionId,\n streamId,\n metadata,\n (size) => {\n transport.send(\n connection,\n ServerMessageType.ClientStreamPull,\n concat(\n encodeNumber(streamId, 'Uint32'),\n encodeNumber(size, 'Uint32'),\n ),\n { callId, streamId },\n )\n },\n )\n },\n getStream: (id) => {\n return this.#clientStreams.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 addConnection(\n transport: ProtocolConnectionTransport,\n options: ConnectionOptions,\n params: ResolveFormatParams,\n ) {\n return this.#connections.add(transport, options, params)\n }\n\n removeConnection(connectionId: string) {\n return this.#connections.remove(connectionId)\n }\n\n getConnection(connectionId: string) {\n return this.#connections.get(connectionId)\n }\n\n initializeConnection(connection: Connection) {\n return this.#connections.initialize(connection)\n }\n\n getClientStream(\n connectionId: string,\n streamId: number,\n ): ProtocolClientStream {\n return this.#clientStreams.get(connectionId, streamId)\n }\n\n addClientStream(\n connectionId: string,\n streamId: number,\n metadata: ProtocolBlobMetadata,\n read: Callback,\n ) {\n return this.#clientStreams.add(connectionId, streamId, metadata, read)\n }\n\n removeClientStream(connectionId: string, streamId: number) {\n return this.#clientStreams.remove(connectionId, streamId)\n }\n\n pushClientStream(connectionId: string, streamId: number, chunk: ArrayBuffer) {\n return this.#clientStreams.push(connectionId, streamId, chunk)\n }\n\n endClientStream(connectionId: string, streamId: number) {\n return this.#clientStreams.end(connectionId, streamId)\n }\n\n abortClientStream(connectionId: string, streamId: number, error?: Error) {\n return this.#clientStreams.abort(connectionId, streamId, error)\n }\n\n getServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.get(connectionId, streamId)\n }\n\n addServerStream(connectionId: string, streamId: number, blob: ProtocolBlob) {\n return this.#serverStreams.add(connectionId, streamId, blob)\n }\n\n removeServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.remove(connectionId, streamId)\n }\n\n pullServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.pull(connectionId, streamId)\n }\n\n abortServerStream(connectionId: string, streamId: number, error?: Error) {\n return this.#serverStreams.abort(connectionId, streamId, error)\n }\n}\n"],"version":3,"file":"protocol.js"}
|
|
1
|
+
{"mappings":"AAAA,SAAwB,OAAO,kBAAkB,eAAe;AAChE,SAGE,MAEA,aACK,aAAa;AACpB,SAAS,QAAQ,cAAc,oBAAoB,qBAAqB;AAExE,SAAS,WAAW,yBAAyB,oBAAoB;AAEjE,SACE,wBAGK,UAAU;AACjB,SACE,YACA,yBAEK,iBAAiB;AAExB,SAAS,2BAA2B,kBAAkB;AAEtD,SAAS,sBAAsB,4BAA4B,aAAa;AAExE,SAAS,iBAA2C,YAAY;AAEhE,OAAO,MAAM,sBAAsB,MAAM;CACvC;CACA;CAEA,YAAYA,MAAcC,SAAkBC,MAAY;AACtD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,OAAO;CACb;CAED,IAAI,UAAU;AACZ,UAAQ,EAAE,KAAK,KAAK,GAAG,MAAM,QAAQ;CACtC;CAED,WAAW;AACT,UAAQ,EAAE,KAAK,KAAK,GAAG,KAAK,QAAQ;CACrC;CAED,SAAS;AACP,SAAO;GACL,MAAM,KAAK;GACX,SAAS,KAAK;GACd,MAAM,KAAK;EACZ;CACF;AACF;AAMD,OAAO,MAAM,oBAAoB;CAC/B,AAASC,cAAc,IAAI;CAS3B,YACmBC,aAMjB;OANiB;CAMf;CAEJ,IAAIC,cAAsB;EACxB,MAAM,aAAa,KAAKF,YAAY,IAAI,aAAa;AACrD,OAAK,WAAY,YAAW,uBAAuB;AACnD,SAAO;CACR;CAED,MAAM,IACJG,WACAC,SACAC,QACA;EACA,MAAM,aAAa,IAAI,WAAW;EAClC,MAAM,SAAS,UAAU,KAAK,YAAY,QAAQ,OAAO;EACzD,MAAM,YAAY,KAAK,YAAY,UAAU,KAAK,MAAM,WAAW;EACnE,MAAM,UAAU,IAAI,kBAAkB,WAAW;AACjD,YAAU,QAAQ,oBAA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CAC7D;CAED,mBAAmBd,cAAsBM,UAAkB;AACzD,SAAO,KAAKQ,eAAe,OAAO,cAAc,SAAS;CAC1D;CAED,iBAAiBd,cAAsBM,UAAkB;AACvD,SAAO,KAAKQ,eAAe,KAAK,cAAc,SAAS;CACxD;CAED,kBAAkBd,cAAsBM,UAAkBe,OAAe;AACvE,SAAO,KAAKP,eAAe,MAAM,cAAc,UAAU,MAAM;CAChE;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: ProtocolConnectionTransport","options: ConnectionOptions<T>","params: ResolveFormatParams","connection: Connection","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 }","#connections","#clientStreams","#serverStreams","options: ProtocolApiCallOptions","rpc: ProtocolRPC","params: ProtocolRPCOptions","buffer: ArrayBuffer","callId: number","options: ConnectionOptions","error?: Error"],"sources":["../../src/server/protocol.ts"],"sourcesContent":["import { type Callback, defer, throwError } from '@nmtjs/common'\nimport {\n type AnyInjectable,\n type Container,\n Hook,\n type Logger,\n Scope,\n} 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 {\n isIterableResult,\n type ProtocolApi,\n type ProtocolApiCallOptions,\n} from './api.ts'\nimport {\n Connection,\n ConnectionContext,\n type ConnectionOptions,\n} from './connection.ts'\nimport type { Format } from './format.ts'\nimport { ProtocolInjectables } from './injectables.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 type ProtocolConnectionTransport = {\n send: Transport<any>['send']\n}\n\nexport class ProtocolConnections {\n readonly #collection = new Map<\n string,\n {\n connection: Connection\n context: ConnectionContext\n transport: ProtocolConnectionTransport\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: ProtocolConnectionTransport,\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 container.provide(ProtocolInjectables.connection, connection)\n try {\n await this.initialize(connection)\n this.#collection.set(connection.id, { connection, context, transport })\n return { connection, context }\n } catch (error) {\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n 'Error during disposing connection',\n )\n })\n throw error\n }\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 async initialize(connection: Connection) {\n await this.application.registry.hooks.call(\n Hook.OnConnect,\n { concurrent: false },\n connection,\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.write(Buffer.from(chunk))\n }\n\n end(connectionId: string, streamId: number) {\n const stream = this.get(connectionId, streamId)\n stream.end(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 type ProtocolRPCOptions = {\n signal?: AbortSignal\n provides?: [AnyInjectable, any][]\n metadata?: ProtocolApiCallOptions['metadata']\n}\n\nexport class Protocol {\n #connections: ProtocolConnections\n #clientStreams: ProtocolClientStreams\n #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 call(options: ProtocolApiCallOptions) {\n const { container, connection } = options\n try {\n return await this.application.api.call(options)\n } catch (error) {\n if (error instanceof ProtocolError === false) {\n this.application.logger.error(\n { error, connection },\n 'Error during RPC call',\n )\n throw new ProtocolError(\n ErrorCode.InternalServerError,\n 'Internal server error',\n )\n }\n throw error\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 rpc(\n connectionId: string,\n rpc: ProtocolRPC,\n params: ProtocolRPCOptions = {},\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 calls.set(callId, abortController)\n\n const callIdEncoded = encodeNumber(callId, 'Uint32')\n const container = context.container.fork(Scope.Call)\n\n if (params.provides) {\n for (const [key, value] of params.provides) {\n container.provide(key, value)\n }\n }\n\n try {\n const response = await this.call({\n connection,\n container,\n namespace,\n payload,\n procedure,\n signal,\n metadata: params.metadata,\n })\n\n const responseEncoded = format.encoder.encodeRPC(\n {\n callId,\n result: response.output,\n },\n {\n addStream: (blob) => {\n const streamId = context.streamId++\n const stream = this.#serverStreams.add(connectionId, streamId, 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(streamId, 'Uint32'),\n (buf.buffer as ArrayBuffer).slice(\n buf.byteOffset,\n buf.byteOffset + buf.byteLength,\n ),\n ),\n { callId, streamId },\n )\n })\n stream.on('error', (err) => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamAbort,\n encodeNumber(streamId, 'Uint32'),\n { callId, streamId },\n )\n })\n stream.on('end', () => {\n transport.send(\n connection,\n ServerMessageType.ServerStreamEnd,\n encodeNumber(streamId, 'Uint32'),\n { callId, streamId },\n )\n })\n return stream\n },\n getStream: (id) => {\n return this.#serverStreams.get(connectionId, id)\n },\n },\n )\n\n if ('subscription' in response) {\n throwError('Unimplemented')\n } else if (isIterableResult(response)) {\n transport.send(\n connection,\n ServerMessageType.RpcStreamResponse,\n responseEncoded,\n { callId },\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 ab.signal.throwIfAborted()\n const chunkEncoded = format.encoder.encode(chunk)\n transport.send(\n connection,\n ServerMessageType.RpcStreamChunk,\n concat(callIdEncoded, chunkEncoded),\n { callId },\n )\n }\n transport.send(\n connection,\n ServerMessageType.RpcStreamEnd,\n callIdEncoded,\n { callId },\n )\n } catch (error) {\n this.application.logger.error(error)\n transport.send(\n connection,\n ServerMessageType.RpcStreamAbort,\n callIdEncoded,\n { callId },\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 { callId },\n )\n }\n } catch (error) {\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\n transport.send(connection, ServerMessageType.RpcResponse, payload, {\n error,\n callId,\n })\n } finally {\n calls.delete(callId)\n container.dispose().catch((error) => {\n this.application.logger.error(\n { error, connection },\n 'Error during disposing connection',\n )\n })\n }\n }\n\n async rpcRaw(\n connectionId: string,\n buffer: ArrayBuffer,\n params: ProtocolRPCOptions = {},\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: (streamId, callId, metadata) => {\n return this.#clientStreams.add(\n connectionId,\n streamId,\n metadata,\n (size) => {\n transport.send(\n connection,\n ServerMessageType.ClientStreamPull,\n concat(\n encodeNumber(streamId, 'Uint32'),\n encodeNumber(size, 'Uint32'),\n ),\n { callId, streamId },\n )\n },\n )\n },\n getStream: (id) => {\n return this.#clientStreams.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 addConnection(\n transport: ProtocolConnectionTransport,\n options: ConnectionOptions,\n params: ResolveFormatParams,\n ) {\n return this.#connections.add(transport, options, params)\n }\n\n removeConnection(connectionId: string) {\n return this.#connections.remove(connectionId)\n }\n\n getConnection(connectionId: string) {\n return this.#connections.get(connectionId)\n }\n\n initializeConnection(connection: Connection) {\n return this.#connections.initialize(connection)\n }\n\n getClientStream(\n connectionId: string,\n streamId: number,\n ): ProtocolClientStream {\n return this.#clientStreams.get(connectionId, streamId)\n }\n\n addClientStream(\n connectionId: string,\n streamId: number,\n metadata: ProtocolBlobMetadata,\n read: Callback,\n ) {\n return this.#clientStreams.add(connectionId, streamId, metadata, read)\n }\n\n removeClientStream(connectionId: string, streamId: number) {\n return this.#clientStreams.remove(connectionId, streamId)\n }\n\n pushClientStream(connectionId: string, streamId: number, chunk: ArrayBuffer) {\n return this.#clientStreams.push(connectionId, streamId, chunk)\n }\n\n endClientStream(connectionId: string, streamId: number) {\n return this.#clientStreams.end(connectionId, streamId)\n }\n\n abortClientStream(connectionId: string, streamId: number, error?: Error) {\n return this.#clientStreams.abort(connectionId, streamId, error)\n }\n\n getServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.get(connectionId, streamId)\n }\n\n addServerStream(connectionId: string, streamId: number, blob: ProtocolBlob) {\n return this.#serverStreams.add(connectionId, streamId, blob)\n }\n\n removeServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.remove(connectionId, streamId)\n }\n\n pullServerStream(connectionId: string, streamId: number) {\n return this.#serverStreams.pull(connectionId, streamId)\n }\n\n abortServerStream(connectionId: string, streamId: number, error?: Error) {\n return this.#serverStreams.abort(connectionId, streamId, error)\n }\n}\n"],"version":3,"file":"protocol.js"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":"AAGA,SAAS,wBAAwB,gBAAgB;AA4BjD,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 { 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 { Format } from './format.ts'\nimport type { Protocol } from './protocol.ts'\nimport type { ProtocolRegistry } from './registry.ts'\nimport type { ProtocolSendMetadata } from './types.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 metadata: ProtocolSendMetadata,\n ) => any\n}\n\nexport interface TransportPluginContext extends PluginContext {\n protocol: Protocol\n registry: ProtocolRegistry\n format: Format\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,"file":"transport.js"}
|
package/dist/server/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"","names":[],"sources":["../../src/server/types.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"mappings":"","names":[],"sources":["../../src/server/types.ts"],"sourcesContent":["import type { ProtocolBlob, ProtocolBlobInterface } from '../common/blob.ts'\nimport type { ProtocolClientStream } from './stream.ts'\n\nexport type InputType<T> = T extends ProtocolBlobInterface\n ? ProtocolClientStream\n : T extends object\n ? { [K in keyof T]: InputType<T[K]> }\n : T\n\nexport type OutputType<T> = T extends ProtocolBlobInterface\n ? ProtocolBlob\n : T extends object\n ? { [K in keyof T]: OutputType<T[K]> }\n : T\n\nexport type ProtocolSendMetadata = {\n streamId?: number\n callId?: number\n error?: any\n}\n"],"version":3,"file":"types.js"}
|
package/package.json
CHANGED
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@nmtjs/common": "0.10.
|
|
20
|
-
"@nmtjs/
|
|
21
|
-
"@nmtjs/
|
|
19
|
+
"@nmtjs/common": "0.10.4",
|
|
20
|
+
"@nmtjs/core": "0.10.4",
|
|
21
|
+
"@nmtjs/type": "0.10.4"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@nmtjs/common": "0.10.
|
|
25
|
-
"@nmtjs/core": "0.10.
|
|
26
|
-
"@nmtjs/type": "0.10.
|
|
24
|
+
"@nmtjs/common": "0.10.4",
|
|
25
|
+
"@nmtjs/core": "0.10.4",
|
|
26
|
+
"@nmtjs/type": "0.10.4"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"src",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"LICENSE.md",
|
|
32
32
|
"README.md"
|
|
33
33
|
],
|
|
34
|
-
"version": "0.10.
|
|
34
|
+
"version": "0.10.4",
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "neemata-build --root=./src './**/*.ts'",
|
|
37
37
|
"type-check": "tsc --noEmit"
|
package/src/client/stream.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defer } from '@nmtjs/common'
|
|
2
2
|
import { concat, encodeText } from '../common/binary.ts'
|
|
3
3
|
import type { ProtocolBlobMetadata } from '../common/blob.ts'
|
|
4
4
|
|
|
@@ -75,6 +75,14 @@ export class ProtocolServerStream<T = any>
|
|
|
75
75
|
extends TransformStream<any, T>
|
|
76
76
|
implements ProtocolServerStreamInterface<T>
|
|
77
77
|
{
|
|
78
|
+
#writer: WritableStreamDefaultWriter
|
|
79
|
+
|
|
80
|
+
constructor(options?: Transformer<any, T>) {
|
|
81
|
+
super(options)
|
|
82
|
+
|
|
83
|
+
this.#writer = this.writable.getWriter()
|
|
84
|
+
}
|
|
85
|
+
|
|
78
86
|
async *[Symbol.asyncIterator]() {
|
|
79
87
|
const reader = this.readable.getReader()
|
|
80
88
|
while (true) {
|
|
@@ -85,15 +93,15 @@ export class ProtocolServerStream<T = any>
|
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
async push(chunk: T) {
|
|
88
|
-
await this.
|
|
96
|
+
await this.#writer.write(chunk)
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
async end() {
|
|
92
|
-
await this.
|
|
100
|
+
await this.#writer.close()
|
|
93
101
|
}
|
|
94
102
|
|
|
95
|
-
abort(error = new Error('Stream aborted')) {
|
|
96
|
-
this.
|
|
103
|
+
async abort(error = new Error('Stream aborted')) {
|
|
104
|
+
await this.#writer.abort(error)
|
|
97
105
|
}
|
|
98
106
|
}
|
|
99
107
|
|
package/src/common/blob.ts
CHANGED
package/src/server/api.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Container,
|
|
3
|
-
Metadata,
|
|
4
|
-
MetadataKey,
|
|
5
|
-
MetadataStore,
|
|
6
|
-
} from '@nmtjs/core'
|
|
7
|
-
import type { Hook } from '@nmtjs/core'
|
|
1
|
+
import type { Container, Hook, MetadataStore } from '@nmtjs/core'
|
|
8
2
|
import type { Connection } from './connection.ts'
|
|
9
3
|
|
|
10
4
|
export type ProtocolApiCallOptions = {
|
package/src/server/connection.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto'
|
|
2
2
|
import type { Container } from '@nmtjs/core'
|
|
3
|
-
import type { InteractivePromise } from '../../../common/src/index.ts'
|
|
4
|
-
import type { ProtocolApiCallResult } from './api.ts'
|
|
5
3
|
import type { BaseServerDecoder, BaseServerEncoder } from './format.ts'
|
|
6
4
|
import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts'
|
|
7
5
|
|
package/src/server/protocol.ts
CHANGED
|
@@ -231,7 +231,6 @@ export class ProtocolServerStreams {
|
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
pull(connectionId: string, streamId: number) {
|
|
234
|
-
console.log('Pulling stream', streamId)
|
|
235
234
|
const stream = this.get(connectionId, streamId)
|
|
236
235
|
stream.resume()
|
|
237
236
|
}
|
|
@@ -364,7 +363,6 @@ export class Protocol {
|
|
|
364
363
|
)
|
|
365
364
|
})
|
|
366
365
|
stream.on('end', () => {
|
|
367
|
-
console.log('Stream ended')
|
|
368
366
|
transport.send(
|
|
369
367
|
connection,
|
|
370
368
|
ServerMessageType.ServerStreamEnd,
|
package/src/server/transport.ts
CHANGED
package/src/server/types.ts
CHANGED