@nmtjs/protocol 0.15.0-beta.35 → 0.15.0-beta.37

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 (93) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +1 -1
  3. package/dist/client/events.d.ts +15 -0
  4. package/dist/client/events.js +28 -0
  5. package/dist/client/format.d.ts +13 -18
  6. package/dist/client/format.js +0 -1
  7. package/dist/client/index.d.ts +1 -4
  8. package/dist/client/index.js +4 -7
  9. package/dist/client/protocol.d.ts +138 -102
  10. package/dist/client/protocol.js +336 -7
  11. package/dist/client/stream.d.ts +19 -37
  12. package/dist/client/stream.js +46 -138
  13. package/dist/common/binary.d.ts +6 -5
  14. package/dist/common/binary.js +9 -22
  15. package/dist/common/blob.d.ts +8 -15
  16. package/dist/common/blob.js +30 -55
  17. package/dist/common/enums.d.ts +7 -17
  18. package/dist/common/enums.js +14 -34
  19. package/dist/common/index.d.ts +0 -2
  20. package/dist/common/index.js +4 -7
  21. package/dist/common/types.d.ts +24 -5
  22. package/dist/common/types.js +0 -1
  23. package/dist/server/api.d.ts +33 -0
  24. package/dist/server/api.js +7 -0
  25. package/dist/server/connection.d.ts +25 -0
  26. package/dist/server/connection.js +22 -0
  27. package/dist/server/constants.d.ts +4 -0
  28. package/dist/server/constants.js +2 -0
  29. package/dist/server/format.d.ts +23 -17
  30. package/dist/server/format.js +8 -13
  31. package/dist/server/index.d.ts +6 -4
  32. package/dist/server/index.js +11 -9
  33. package/dist/server/injectables.d.ts +14 -0
  34. package/dist/server/injectables.js +22 -0
  35. package/dist/server/protocol.d.ts +114 -88
  36. package/dist/server/protocol.js +354 -5
  37. package/dist/server/registry.d.ts +3 -0
  38. package/dist/server/registry.js +3 -0
  39. package/dist/server/stream.d.ts +0 -5
  40. package/dist/server/stream.js +2 -31
  41. package/dist/server/transport.d.ts +23 -0
  42. package/dist/server/transport.js +3 -0
  43. package/dist/server/types.d.ts +8 -29
  44. package/dist/server/types.js +0 -1
  45. package/dist/server/utils.d.ts +6 -3
  46. package/dist/server/utils.js +4 -5
  47. package/package.json +24 -17
  48. package/dist/client/format.js.map +0 -1
  49. package/dist/client/index.js.map +0 -1
  50. package/dist/client/protocol.js.map +0 -1
  51. package/dist/client/stream.js.map +0 -1
  52. package/dist/client/versions/v1.d.ts +0 -108
  53. package/dist/client/versions/v1.js +0 -128
  54. package/dist/client/versions/v1.js.map +0 -1
  55. package/dist/common/binary.js.map +0 -1
  56. package/dist/common/blob.js.map +0 -1
  57. package/dist/common/constants.d.ts +0 -2
  58. package/dist/common/constants.js +0 -2
  59. package/dist/common/constants.js.map +0 -1
  60. package/dist/common/enums.js.map +0 -1
  61. package/dist/common/index.js.map +0 -1
  62. package/dist/common/types.js.map +0 -1
  63. package/dist/common/utils.d.ts +0 -2
  64. package/dist/common/utils.js +0 -7
  65. package/dist/common/utils.js.map +0 -1
  66. package/dist/server/format.js.map +0 -1
  67. package/dist/server/index.js.map +0 -1
  68. package/dist/server/protocol.js.map +0 -1
  69. package/dist/server/stream.js.map +0 -1
  70. package/dist/server/types.js.map +0 -1
  71. package/dist/server/utils.js.map +0 -1
  72. package/dist/server/versions/v1.d.ts +0 -77
  73. package/dist/server/versions/v1.js +0 -119
  74. package/dist/server/versions/v1.js.map +0 -1
  75. package/src/client/format.ts +0 -49
  76. package/src/client/index.ts +0 -8
  77. package/src/client/protocol.ts +0 -107
  78. package/src/client/stream.ts +0 -222
  79. package/src/client/versions/v1.ts +0 -205
  80. package/src/common/binary.ts +0 -70
  81. package/src/common/blob.ts +0 -94
  82. package/src/common/constants.ts +0 -2
  83. package/src/common/enums.ts +0 -62
  84. package/src/common/index.ts +0 -6
  85. package/src/common/types.ts +0 -18
  86. package/src/common/utils.ts +0 -12
  87. package/src/server/format.ts +0 -117
  88. package/src/server/index.ts +0 -10
  89. package/src/server/protocol.ts +0 -97
  90. package/src/server/stream.ts +0 -72
  91. package/src/server/types.ts +0 -42
  92. package/src/server/utils.ts +0 -22
  93. package/src/server/versions/v1.ts +0 -198
@@ -2,39 +2,26 @@
2
2
  /// <reference lib="dom" />
3
3
  const utf8decoder = new TextDecoder();
4
4
  const utf8encoder = new TextEncoder();
5
- export const encodeNumber = (value, type, littleEndian = true) => {
5
+ export const encodeNumber = (value, type, littleEndian = false) => {
6
6
  const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT;
7
7
  const ab = new ArrayBuffer(bytesNeeded);
8
8
  const dv = new DataView(ab);
9
9
  dv[`set${type}`](0, value, littleEndian);
10
10
  return ab;
11
11
  };
12
- export const decodeNumber = (buffer, type, offset = 0, littleEndian = true) => {
13
- const ab = buffer instanceof ArrayBuffer ? buffer : buffer.buffer;
14
- const baseOffset = buffer instanceof ArrayBuffer ? 0 : buffer.byteOffset;
15
- const view = new DataView(ab);
16
- return view[`get${type}`](baseOffset + offset, littleEndian);
12
+ export const decodeNumber = (buffer, type, offset = 0, littleEndian = false) => {
13
+ const view = new DataView(buffer);
14
+ return view[`get${type}`](offset, littleEndian);
17
15
  };
18
- export const encodeText = (text) => utf8encoder.encode(text);
16
+ export const encodeText = (text) => new Uint8Array(utf8encoder.encode(text)).buffer;
19
17
  export const decodeText = (buffer) => utf8decoder.decode(buffer);
20
18
  export const concat = (...buffers) => {
21
- let totalLength = 0;
22
- for (const buffer of buffers)
23
- totalLength += buffer.byteLength;
19
+ const totalLength = buffers.reduce((acc, buffer) => acc + buffer.byteLength, 0);
24
20
  const view = new Uint8Array(totalLength);
25
21
  let offset = 0;
26
22
  for (const buffer of buffers) {
27
- const chunk = buffer instanceof ArrayBuffer
28
- ? new Uint8Array(buffer)
29
- : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
30
- view.set(chunk, offset);
31
- offset += chunk.byteLength;
23
+ view.set(new Uint8Array(buffer), offset);
24
+ offset += buffer.byteLength;
32
25
  }
33
- return view;
26
+ return view.buffer;
34
27
  };
35
- export const UTF8Transform = () => new TransformStream({
36
- transform(chunk, controller) {
37
- controller.enqueue(encodeText(chunk));
38
- },
39
- });
40
- //# sourceMappingURL=binary.js.map
@@ -1,4 +1,5 @@
1
- import { kBlobKey } from './constants.ts';
1
+ export declare const BlobKey: unique symbol;
2
+ export type BlobKey = typeof BlobKey;
2
3
  export type ProtocolBlobMetadata = {
3
4
  type: string;
4
5
  size?: number | undefined;
@@ -6,24 +7,16 @@ export type ProtocolBlobMetadata = {
6
7
  };
7
8
  export interface ProtocolBlobInterface {
8
9
  readonly metadata: ProtocolBlobMetadata;
9
- readonly [kBlobKey]: any;
10
+ readonly [BlobKey]: true;
10
11
  }
11
12
  export declare class ProtocolBlob implements ProtocolBlobInterface {
12
- [kBlobKey]: true;
13
- readonly source: any;
13
+ readonly [BlobKey] = true;
14
14
  readonly metadata: ProtocolBlobMetadata;
15
- readonly encode?: (metadata: ProtocolBlobMetadata) => unknown;
16
- readonly toJSON?: () => unknown;
17
- constructor({ source, encode, size, type, filename }: {
18
- source: any;
19
- encode?: () => unknown;
20
- size?: number;
21
- type?: string;
22
- filename?: string;
23
- });
24
- static from(_source: any, _metadata?: {
15
+ readonly source: any;
16
+ constructor(source: any, size?: number, type?: string, filename?: string);
17
+ static from(source: any, metadata?: {
25
18
  size?: number;
26
19
  type?: string;
27
20
  filename?: string;
28
- }, _encode?: (metadata: ProtocolBlobMetadata) => unknown): ProtocolBlob;
21
+ }): ProtocolBlob;
29
22
  }
@@ -1,67 +1,42 @@
1
- import { kBlobKey } from './constants.js';
1
+ export const BlobKey = Symbol.for('neemata:BlobKey');
2
2
  export class ProtocolBlob {
3
- [kBlobKey] = true;
4
- source;
3
+ [BlobKey] = true;
5
4
  metadata;
6
- encode;
7
- toJSON;
8
- constructor({ source, encode, size, type = 'application/octet-stream', filename, }) {
5
+ source;
6
+ constructor(source, size, type = 'application/octet-stream', filename) {
9
7
  if (typeof size !== 'undefined' && size <= 0)
10
8
  throw new Error('Blob size is invalid');
11
- this.encode = encode;
12
9
  this.source = source;
13
10
  this.metadata = { size, type, filename };
14
- if (encode) {
15
- Object.defineProperty(this, 'toJSON', {
16
- configurable: false,
17
- enumerable: false,
18
- writable: false,
19
- value: encode,
20
- });
21
- }
22
11
  }
23
- static from(_source, _metadata = {}, _encode) {
24
- let source;
25
- const metadata = { type: 'application/octet-stream', ..._metadata };
26
- if (_source instanceof globalThis.ReadableStream) {
27
- source = _source;
28
- }
29
- else if ('File' in globalThis && _source instanceof globalThis.File) {
30
- source = _source.stream();
31
- metadata.size ??= _source.size;
32
- metadata.filename ??= _source.name;
33
- }
34
- else if (_source instanceof globalThis.Blob) {
35
- source = _source.stream();
36
- metadata.size ??= _source.size;
37
- metadata.type ??= _source.type;
38
- }
39
- else if (typeof _source === 'string') {
40
- const blob = new Blob([_source]);
41
- source = blob.stream();
42
- metadata.size ??= blob.size;
43
- metadata.type ??= 'text/plain';
44
- }
45
- else if (globalThis.ArrayBuffer.isView(_source)) {
46
- const blob = new Blob([_source]);
47
- source = blob.stream();
48
- metadata.size ??= blob.size;
49
- }
50
- else if (_source instanceof globalThis.ArrayBuffer) {
51
- const blob = new Blob([_source]);
52
- source = blob.stream();
53
- metadata.size ??= blob.size;
12
+ static from(source, metadata = {}) {
13
+ let _source;
14
+ if (source instanceof globalThis.ReadableStream) {
15
+ _source = source;
16
+ }
17
+ else if ('File' in globalThis && source instanceof globalThis.File) {
18
+ _source = source.stream();
19
+ metadata.size = source.size;
20
+ metadata.filename = source.name;
21
+ }
22
+ else if (source instanceof globalThis.Blob) {
23
+ _source = source.stream();
24
+ metadata.size = source.size;
25
+ }
26
+ else if (typeof source === 'string') {
27
+ const blob = new Blob([source]);
28
+ _source = blob.stream();
29
+ metadata.size = blob.size;
30
+ metadata.type = metadata.type || 'text/plain';
31
+ }
32
+ else if (source instanceof globalThis.ArrayBuffer) {
33
+ const blob = new Blob([source]);
34
+ _source = blob.stream();
35
+ metadata.size = blob.size;
54
36
  }
55
37
  else {
56
- source = _source;
38
+ _source = source;
57
39
  }
58
- return new ProtocolBlob({
59
- source,
60
- encode: _encode?.bind(null, metadata),
61
- size: metadata.size,
62
- type: metadata.type,
63
- filename: metadata.filename,
64
- });
40
+ return new ProtocolBlob(_source, metadata.size, metadata.type, metadata.filename);
65
41
  }
66
42
  }
67
- //# sourceMappingURL=blob.js.map
@@ -1,17 +1,15 @@
1
- export declare enum ProtocolVersion {
2
- v1 = 1
3
- }
4
1
  export declare enum ClientMessageType {
5
2
  Rpc = 10,
6
3
  RpcAbort = 11,
7
- RpcPull = 12,
4
+ RpcStreamAbort = 12,
8
5
  ClientStreamPush = 20,
9
6
  ClientStreamEnd = 21,
10
7
  ClientStreamAbort = 22,
11
- ServerStreamAbort = 33,
12
- ServerStreamPull = 34
8
+ ServerStreamAbort = 23,
9
+ ServerStreamPull = 24
13
10
  }
14
11
  export declare enum ServerMessageType {
12
+ Event = 1,
15
13
  RpcResponse = 10,
16
14
  RpcStreamResponse = 11,
17
15
  RpcStreamChunk = 12,
@@ -20,10 +18,10 @@ export declare enum ServerMessageType {
20
18
  ServerStreamPush = 20,
21
19
  ServerStreamEnd = 21,
22
20
  ServerStreamAbort = 22,
23
- ClientStreamAbort = 33,
24
- ClientStreamPull = 34
21
+ ClientStreamAbort = 23,
22
+ ClientStreamPull = 24
25
23
  }
26
- export declare enum ConnectionType {
24
+ export declare enum TransportType {
27
25
  Bidirectional = "Bidirectional",
28
26
  Unidirectional = "Unidirectional"
29
27
  }
@@ -41,11 +39,3 @@ export declare enum ErrorCode {
41
39
  ClientRequestError = "ClientRequestError",
42
40
  ConnectionError = "ConnectionError"
43
41
  }
44
- export declare enum MessageByteLength {
45
- MessageType = 1,
46
- MessageError = 1,
47
- ProcedureLength = 2,
48
- CallId = 4,
49
- StreamId = 4,
50
- ChunkSize = 4
51
- }
@@ -1,24 +1,17 @@
1
- export { ProtocolVersion };
2
- var ProtocolVersion;
3
- (function (ProtocolVersion) {
4
- ProtocolVersion[ProtocolVersion["v1"] = 1] = "v1";
5
- })(ProtocolVersion || (ProtocolVersion = {}));
6
- export { ClientMessageType };
7
- var ClientMessageType;
1
+ export var ClientMessageType;
8
2
  (function (ClientMessageType) {
9
3
  ClientMessageType[ClientMessageType["Rpc"] = 10] = "Rpc";
10
4
  ClientMessageType[ClientMessageType["RpcAbort"] = 11] = "RpcAbort";
11
- ClientMessageType[ClientMessageType["RpcPull"] = 12] = "RpcPull";
5
+ ClientMessageType[ClientMessageType["RpcStreamAbort"] = 12] = "RpcStreamAbort";
12
6
  ClientMessageType[ClientMessageType["ClientStreamPush"] = 20] = "ClientStreamPush";
13
7
  ClientMessageType[ClientMessageType["ClientStreamEnd"] = 21] = "ClientStreamEnd";
14
8
  ClientMessageType[ClientMessageType["ClientStreamAbort"] = 22] = "ClientStreamAbort";
15
- ClientMessageType[ClientMessageType["ServerStreamAbort"] = 33] = "ServerStreamAbort";
16
- ClientMessageType[ClientMessageType["ServerStreamPull"] = 34] = "ServerStreamPull";
9
+ ClientMessageType[ClientMessageType["ServerStreamAbort"] = 23] = "ServerStreamAbort";
10
+ ClientMessageType[ClientMessageType["ServerStreamPull"] = 24] = "ServerStreamPull";
17
11
  })(ClientMessageType || (ClientMessageType = {}));
18
- export { ServerMessageType };
19
- var ServerMessageType;
12
+ export var ServerMessageType;
20
13
  (function (ServerMessageType) {
21
- // Event = 1,
14
+ ServerMessageType[ServerMessageType["Event"] = 1] = "Event";
22
15
  ServerMessageType[ServerMessageType["RpcResponse"] = 10] = "RpcResponse";
23
16
  ServerMessageType[ServerMessageType["RpcStreamResponse"] = 11] = "RpcStreamResponse";
24
17
  ServerMessageType[ServerMessageType["RpcStreamChunk"] = 12] = "RpcStreamChunk";
@@ -27,17 +20,15 @@ var ServerMessageType;
27
20
  ServerMessageType[ServerMessageType["ServerStreamPush"] = 20] = "ServerStreamPush";
28
21
  ServerMessageType[ServerMessageType["ServerStreamEnd"] = 21] = "ServerStreamEnd";
29
22
  ServerMessageType[ServerMessageType["ServerStreamAbort"] = 22] = "ServerStreamAbort";
30
- ServerMessageType[ServerMessageType["ClientStreamAbort"] = 33] = "ClientStreamAbort";
31
- ServerMessageType[ServerMessageType["ClientStreamPull"] = 34] = "ClientStreamPull";
23
+ ServerMessageType[ServerMessageType["ClientStreamAbort"] = 23] = "ClientStreamAbort";
24
+ ServerMessageType[ServerMessageType["ClientStreamPull"] = 24] = "ClientStreamPull";
32
25
  })(ServerMessageType || (ServerMessageType = {}));
33
- export { ConnectionType };
34
- var ConnectionType;
35
- (function (ConnectionType) {
36
- ConnectionType["Bidirectional"] = "Bidirectional";
37
- ConnectionType["Unidirectional"] = "Unidirectional";
38
- })(ConnectionType || (ConnectionType = {}));
39
- export { ErrorCode };
40
- var ErrorCode;
26
+ export var TransportType;
27
+ (function (TransportType) {
28
+ TransportType["Bidirectional"] = "Bidirectional";
29
+ TransportType["Unidirectional"] = "Unidirectional";
30
+ })(TransportType || (TransportType = {}));
31
+ export var ErrorCode;
41
32
  (function (ErrorCode) {
42
33
  ErrorCode["ValidationError"] = "ValidationError";
43
34
  ErrorCode["BadRequest"] = "BadRequest";
@@ -52,14 +43,3 @@ var ErrorCode;
52
43
  ErrorCode["ClientRequestError"] = "ClientRequestError";
53
44
  ErrorCode["ConnectionError"] = "ConnectionError";
54
45
  })(ErrorCode || (ErrorCode = {}));
55
- export { MessageByteLength };
56
- var MessageByteLength;
57
- (function (MessageByteLength) {
58
- MessageByteLength[MessageByteLength["MessageType"] = 1] = "MessageType";
59
- MessageByteLength[MessageByteLength["MessageError"] = 1] = "MessageError";
60
- MessageByteLength[MessageByteLength["ProcedureLength"] = 2] = "ProcedureLength";
61
- MessageByteLength[MessageByteLength["CallId"] = 4] = "CallId";
62
- MessageByteLength[MessageByteLength["StreamId"] = 4] = "StreamId";
63
- MessageByteLength[MessageByteLength["ChunkSize"] = 4] = "ChunkSize";
64
- })(MessageByteLength || (MessageByteLength = {}));
65
- //# sourceMappingURL=enums.js.map
@@ -1,6 +1,4 @@
1
1
  export * from './binary.ts';
2
2
  export * from './blob.ts';
3
- export * from './constants.ts';
4
3
  export * from './enums.ts';
5
4
  export * from './types.ts';
6
- export * from './utils.ts';
@@ -1,7 +1,4 @@
1
- export * from './binary.js';
2
- export * from './blob.js';
3
- export * from './constants.js';
4
- export * from './enums.js';
5
- export * from './types.js';
6
- export * from './utils.js';
7
- //# sourceMappingURL=index.js.map
1
+ export * from "./binary.js";
2
+ export * from "./blob.js";
3
+ export * from "./enums.js";
4
+ export * from "./types.js";
@@ -1,14 +1,33 @@
1
- import type { ProtocolBlobMetadata } from './blob.ts';
1
+ import type { OneOf } from '@nmtjs/common';
2
+ import type { ProtocolBlob, ProtocolBlobMetadata } from './blob.ts';
2
3
  type Stream = any;
3
4
  export interface BaseProtocolError {
4
5
  code: string;
5
6
  message: string;
6
7
  data?: any;
7
8
  }
8
- export type ProtocolRPCPayload = unknown;
9
- export type ProtocolRPCResponse = unknown;
10
- export type EncodeRPCStreams = Record<number, ProtocolBlobMetadata>;
9
+ export type ProtocolRPC = {
10
+ callId: number;
11
+ procedure: string;
12
+ payload: any;
13
+ };
14
+ export type ProtocolRPCResponse<T = Stream> = OneOf<[
15
+ {
16
+ callId: number;
17
+ error: BaseProtocolError;
18
+ },
19
+ {
20
+ callId: number;
21
+ result: any;
22
+ streams: Record<number, T>;
23
+ }
24
+ ]>;
25
+ export interface EncodeRPCContext<T = Stream> {
26
+ getStream: (id: number) => T;
27
+ addStream: (blob: ProtocolBlob) => T;
28
+ }
11
29
  export interface DecodeRPCContext<T = Stream> {
12
- addStream: (id: number, metadata: ProtocolBlobMetadata) => T;
30
+ getStream: (id: number, callId: number) => T;
31
+ addStream: (id: number, callId: number, metadata: ProtocolBlobMetadata) => T;
13
32
  }
14
33
  export {};
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=types.js.map
@@ -0,0 +1,33 @@
1
+ import type { Async } from '@nmtjs/common';
2
+ import type { Container, Hook, MetadataStore } from '@nmtjs/core';
3
+ import type { Connection } from './connection.ts';
4
+ import { kIterableResponse } from './constants.ts';
5
+ export type ProtocolApiCallOptions = {
6
+ connection: Connection;
7
+ procedure: string;
8
+ container: Container;
9
+ payload: any;
10
+ signal: AbortSignal;
11
+ validateMetadata?: (metadata: MetadataStore) => void;
12
+ };
13
+ export type ProtocolAnyIterable<T> = ((signal: AbortSignal) => Async<AsyncIterable<T>>) | AsyncIterable<T>;
14
+ export interface ProtocolApiCallBaseResult<T = unknown> {
15
+ output: T;
16
+ }
17
+ export interface ProtocolApiCallIterableResult<Y = unknown, O = unknown> extends ProtocolApiCallBaseResult<O> {
18
+ [kIterableResponse]: true;
19
+ iterable: ProtocolAnyIterable<Y>;
20
+ onFinish?: () => void;
21
+ }
22
+ export type ProtocolApiCallResult = ProtocolApiCallBaseResult | ProtocolApiCallIterableResult;
23
+ export interface ProtocolApi {
24
+ call(options: ProtocolApiCallOptions): Promise<ProtocolApiCallResult>;
25
+ }
26
+ export declare function isIterableResult(value: ProtocolApiCallResult): value is ProtocolApiCallIterableResult;
27
+ export declare function createStreamResponse<Y, O>(iterable: ProtocolAnyIterable<Y>, output?: O, onFinish?: () => void): ProtocolApiCallIterableResult<Y, O>;
28
+ declare module '@nmtjs/core' {
29
+ interface HookType {
30
+ [Hook.OnConnect]: (connection: Connection) => any;
31
+ [Hook.OnDisconnect]: (connection: Connection) => any;
32
+ }
33
+ }
@@ -0,0 +1,7 @@
1
+ import { kIterableResponse } from "./constants.js";
2
+ export function isIterableResult(value) {
3
+ return value && value[kIterableResponse] === true;
4
+ }
5
+ export function createStreamResponse(iterable, output = undefined, onFinish) {
6
+ return { [kIterableResponse]: true, iterable, output, onFinish };
7
+ }
@@ -0,0 +1,25 @@
1
+ import type { Container } from '@nmtjs/core';
2
+ import type { BaseServerDecoder, BaseServerEncoder } from './format.ts';
3
+ import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts';
4
+ export type ConnectionOptions<Data = unknown> = {
5
+ id?: string;
6
+ data: Data;
7
+ };
8
+ export declare class Connection<Data = unknown> {
9
+ readonly id: string;
10
+ readonly data: Data;
11
+ constructor(options: ConnectionOptions<Data>);
12
+ }
13
+ export declare class ConnectionContext {
14
+ streamId: number;
15
+ rpcs: Map<number, AbortController>;
16
+ clientStreams: Map<number, ProtocolClientStream>;
17
+ serverStreams: Map<number, ProtocolServerStream>;
18
+ rpcStreams: Map<number, AbortController>;
19
+ container: Container;
20
+ format: {
21
+ encoder: BaseServerEncoder;
22
+ decoder: BaseServerDecoder;
23
+ };
24
+ constructor(container: ConnectionContext['container'], format: ConnectionContext['format']);
25
+ }
@@ -0,0 +1,22 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ export class Connection {
3
+ id;
4
+ data;
5
+ constructor(options) {
6
+ this.id = options.id ?? randomUUID();
7
+ this.data = options.data;
8
+ }
9
+ }
10
+ export class ConnectionContext {
11
+ streamId = 1;
12
+ rpcs = new Map();
13
+ clientStreams = new Map();
14
+ serverStreams = new Map();
15
+ rpcStreams = new Map();
16
+ container;
17
+ format;
18
+ constructor(container, format) {
19
+ this.container = container;
20
+ this.format = format;
21
+ }
22
+ }
@@ -0,0 +1,4 @@
1
+ export declare const kTransportPlugin: unique symbol;
2
+ export type kTransportPlugin = typeof kTransportPlugin;
3
+ export declare const kIterableResponse: unique symbol;
4
+ export type kIterableResponse = typeof kIterableResponse;
@@ -0,0 +1,2 @@
1
+ export const kTransportPlugin = Symbol.for('neemata:TransportPluginKey');
2
+ export const kIterableResponse = Symbol.for('neemata:IterableResponseKey');
@@ -1,33 +1,39 @@
1
- import type { Pattern } from '@nmtjs/common';
2
- import type { ProtocolBlobMetadata } from '../common/blob.ts';
3
- import type { DecodeRPCContext, EncodeRPCStreams, ProtocolRPCPayload } from '../common/types.ts';
4
- import type { ProtocolClientStream } from './stream.ts';
1
+ import type { OneOf } from '@nmtjs/common';
2
+ import type { Pattern } from '@nmtjs/core';
3
+ import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPC, ProtocolRPCResponse } from '../common/types.ts';
4
+ import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts';
5
5
  export interface BaseServerDecoder {
6
6
  accept: Pattern[];
7
- decode(buffer: ArrayBufferView): unknown;
8
- decodeRPC(buffer: ArrayBufferView, context: DecodeRPCContext<() => ProtocolClientStream>): ProtocolRPCPayload;
7
+ decode(buffer: ArrayBuffer): any;
8
+ decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext<ProtocolClientStream>): ProtocolRPC;
9
9
  }
10
10
  export interface BaseServerEncoder {
11
11
  contentType: string;
12
- encode(data: unknown): ArrayBufferView;
13
- encodeRPC(data: unknown, streams: EncodeRPCStreams): ArrayBufferView;
14
- encodeBlob(streamId: number, metadata: ProtocolBlobMetadata): unknown;
12
+ encode(data: any): ArrayBuffer;
13
+ encodeRPC(rpc: OneOf<[
14
+ {
15
+ callId: number;
16
+ error: any;
17
+ },
18
+ {
19
+ callId: number;
20
+ result: any;
21
+ }
22
+ ]>, context: EncodeRPCContext<ProtocolServerStream>): ArrayBuffer;
15
23
  }
16
24
  export declare abstract class BaseServerFormat implements BaseServerDecoder, BaseServerEncoder {
17
25
  abstract accept: Pattern[];
18
26
  abstract contentType: string;
19
- abstract encode(data: unknown): ArrayBufferView;
20
- abstract encodeRPC(data: unknown, streams: EncodeRPCStreams): ArrayBufferView;
21
- abstract encodeBlob(streamId: number, metadata: ProtocolBlobMetadata): unknown;
22
- abstract decode(buffer: ArrayBufferView): any;
23
- abstract decodeRPC(buffer: ArrayBufferView, context: DecodeRPCContext<() => ProtocolClientStream>): ProtocolRPCPayload;
27
+ abstract encode(data: any): ArrayBuffer;
28
+ abstract encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext<ProtocolServerStream>): ArrayBuffer;
29
+ abstract decode(buffer: ArrayBuffer): any;
30
+ abstract decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext<ProtocolClientStream>): ProtocolRPC;
24
31
  }
25
32
  export declare const parseContentTypes: (types: string) => string[];
26
- export declare class ProtocolFormats {
33
+ export declare class ProtocolFormat {
27
34
  decoders: Map<Pattern, BaseServerDecoder>;
28
35
  encoders: Map<Pattern, BaseServerEncoder>;
29
- default: BaseServerFormat;
30
- constructor(formats: [BaseServerFormat, ...BaseServerFormat[]]);
36
+ constructor(formats: BaseServerFormat[]);
31
37
  supportsDecoder(contentType: string, throwIfUnsupported?: boolean): BaseServerDecoder | null;
32
38
  supportsEncoder(contentType: string, throwIfUnsupported?: boolean): BaseServerEncoder | null;
33
39
  private supports;
@@ -1,22 +1,20 @@
1
- import { match } from '@nmtjs/common';
1
+ import { match } from '@nmtjs/core';
2
2
  export class BaseServerFormat {
3
3
  }
4
4
  export const parseContentTypes = (types) => {
5
- const normalized = types.trim();
6
- if (normalized === '*/*')
5
+ if (types === '*/*')
7
6
  return ['*/*'];
8
- return normalized
7
+ return types
9
8
  .split(',')
10
- .map((t) => t.trim())
11
9
  .map((t) => {
12
- const [rawType, ...rest] = t.split(';');
10
+ const [type, ...rest] = t.split(';');
13
11
  const params = new Map(rest.map((p) => p
14
12
  .trim()
15
13
  .split('=')
16
14
  .slice(0, 2)
17
- .map((part) => part.trim())));
15
+ .map((p) => p.trim())));
18
16
  return {
19
- type: rawType.trim(),
17
+ type,
20
18
  q: params.has('q') ? Number.parseFloat(params.get('q')) : 1,
21
19
  };
22
20
  })
@@ -25,16 +23,14 @@ export const parseContentTypes = (types) => {
25
23
  return 1;
26
24
  if (b.type === '*/*')
27
25
  return -1;
28
- return b.q - a.q;
26
+ return b.q - a.q ? -1 : 1;
29
27
  })
30
28
  .map((t) => t.type);
31
29
  };
32
- export class ProtocolFormats {
30
+ export class ProtocolFormat {
33
31
  decoders = new Map();
34
32
  encoders = new Map();
35
- default;
36
33
  constructor(formats) {
37
- this.default = formats[0];
38
34
  for (const format of formats) {
39
35
  this.encoders.set(format.contentType, format);
40
36
  for (const acceptType of format.accept) {
@@ -62,4 +58,3 @@ export class ProtocolFormats {
62
58
  return null;
63
59
  }
64
60
  }
65
- //# sourceMappingURL=format.js.map
@@ -1,9 +1,11 @@
1
+ export * from './api.ts';
2
+ export * from './connection.ts';
3
+ export * from './constants.ts';
1
4
  export * from './format.ts';
5
+ export * from './injectables.ts';
2
6
  export * from './protocol.ts';
7
+ export * from './registry.ts';
3
8
  export * from './stream.ts';
9
+ export * from './transport.ts';
4
10
  export * from './types.ts';
5
11
  export * from './utils.ts';
6
- import { ProtocolVersion1 } from './versions/v1.ts';
7
- export declare const versions: {
8
- 1: ProtocolVersion1;
9
- };
@@ -1,9 +1,11 @@
1
- export * from './format.js';
2
- export * from './protocol.js';
3
- export * from './stream.js';
4
- export * from './types.js';
5
- export * from './utils.js';
6
- import { ProtocolVersion } from '../common/enums.js';
7
- import { ProtocolVersion1 } from './versions/v1.js';
8
- export const versions = { [ProtocolVersion.v1]: new ProtocolVersion1() };
9
- //# sourceMappingURL=index.js.map
1
+ export * from "./api.js";
2
+ export * from "./connection.js";
3
+ export * from "./constants.js";
4
+ export * from "./format.js";
5
+ export * from "./injectables.js";
6
+ export * from "./protocol.js";
7
+ export * from "./registry.js";
8
+ export * from "./stream.js";
9
+ export * from "./transport.js";
10
+ export * from "./types.js";
11
+ export * from "./utils.js";
@@ -0,0 +1,14 @@
1
+ import { Scope } from '@nmtjs/core';
2
+ import type { Connection } from './connection.ts';
3
+ export declare const ProtocolInjectables: {
4
+ readonly connection: import("@nmtjs/core").LazyInjectable<Connection<unknown>, Scope.Connection>;
5
+ readonly connectionData: import("@nmtjs/core").LazyInjectable<any, Scope.Connection>;
6
+ readonly connectionAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Connection>;
7
+ readonly rpcClientAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
8
+ readonly rpcTimeoutSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
9
+ readonly rpcAbortSignal: import("@nmtjs/core").FactoryInjectable<AbortSignal, {
10
+ rpcTimeoutSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
11
+ rpcClientAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
12
+ connectionAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Connection>;
13
+ }, Scope.Global, AbortSignal>;
14
+ };