@nmtjs/protocol 0.12.4 → 0.12.6

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 (72) hide show
  1. package/dist/client/events.d.ts +17 -0
  2. package/dist/client/events.js +26 -30
  3. package/dist/client/format.d.ts +21 -0
  4. package/dist/client/format.js +2 -3
  5. package/dist/client/index.d.ts +5 -0
  6. package/dist/client/index.js +1 -2
  7. package/dist/client/protocol.d.ts +149 -0
  8. package/dist/client/protocol.js +342 -343
  9. package/dist/client/stream.d.ts +28 -0
  10. package/dist/client/stream.js +88 -80
  11. package/dist/client/types.d.ts +8 -0
  12. package/dist/client/types.js +0 -2
  13. package/dist/common/binary.d.ts +19 -0
  14. package/dist/common/binary.js +17 -17
  15. package/dist/common/blob.d.ts +22 -0
  16. package/dist/common/blob.js +44 -40
  17. package/dist/common/enums.d.ts +41 -0
  18. package/dist/common/enums.js +45 -47
  19. package/dist/common/index.d.ts +4 -0
  20. package/dist/common/index.js +0 -2
  21. package/dist/common/types.d.ts +34 -0
  22. package/dist/common/types.js +0 -2
  23. package/dist/server/api.d.ts +34 -0
  24. package/dist/server/api.js +7 -9
  25. package/dist/server/connection.d.ts +25 -0
  26. package/dist/server/connection.js +18 -20
  27. package/dist/server/constants.d.ts +4 -0
  28. package/dist/server/constants.js +2 -4
  29. package/dist/server/format.d.ts +40 -0
  30. package/dist/server/format.js +56 -43
  31. package/dist/server/index.d.ts +11 -0
  32. package/dist/server/index.js +0 -2
  33. package/dist/server/injectables.d.ts +14 -0
  34. package/dist/server/injectables.js +18 -20
  35. package/dist/server/protocol.d.ts +118 -0
  36. package/dist/server/protocol.js +365 -384
  37. package/dist/server/registry.d.ts +3 -0
  38. package/dist/server/registry.js +3 -4
  39. package/dist/server/stream.d.ts +12 -0
  40. package/dist/server/stream.js +28 -26
  41. package/dist/server/transport.d.ts +23 -0
  42. package/dist/server/transport.js +1 -7
  43. package/dist/server/types.d.ts +13 -0
  44. package/dist/server/types.js +0 -2
  45. package/dist/server/utils.d.ts +15 -0
  46. package/dist/server/utils.js +16 -13
  47. package/package.json +17 -14
  48. package/src/server/api.ts +2 -1
  49. package/src/server/protocol.ts +1 -1
  50. package/dist/client/events.js.map +0 -1
  51. package/dist/client/format.js.map +0 -1
  52. package/dist/client/index.js.map +0 -1
  53. package/dist/client/protocol.js.map +0 -1
  54. package/dist/client/stream.js.map +0 -1
  55. package/dist/client/types.js.map +0 -1
  56. package/dist/common/binary.js.map +0 -1
  57. package/dist/common/blob.js.map +0 -1
  58. package/dist/common/enums.js.map +0 -1
  59. package/dist/common/index.js.map +0 -1
  60. package/dist/common/types.js.map +0 -1
  61. package/dist/server/api.js.map +0 -1
  62. package/dist/server/connection.js.map +0 -1
  63. package/dist/server/constants.js.map +0 -1
  64. package/dist/server/format.js.map +0 -1
  65. package/dist/server/index.js.map +0 -1
  66. package/dist/server/injectables.js.map +0 -1
  67. package/dist/server/protocol.js.map +0 -1
  68. package/dist/server/registry.js.map +0 -1
  69. package/dist/server/stream.js.map +0 -1
  70. package/dist/server/transport.js.map +0 -1
  71. package/dist/server/types.js.map +0 -1
  72. package/dist/server/utils.js.map +0 -1
@@ -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
+ }
@@ -1,24 +1,22 @@
1
- import { randomUUID } from "node:crypto";
1
+ import { randomUUID } from 'node:crypto';
2
2
  export class Connection {
3
- id;
4
- data;
5
- constructor(options) {
6
- this.id = options.id ?? randomUUID();
7
- this.data = options.data;
8
- }
3
+ id;
4
+ data;
5
+ constructor(options) {
6
+ this.id = options.id ?? randomUUID();
7
+ this.data = options.data;
8
+ }
9
9
  }
10
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
- }
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
22
  }
23
-
24
- //# sourceMappingURL=connection.js.map
@@ -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;
@@ -1,4 +1,2 @@
1
- export const kTransportPlugin = Symbol.for("neemata:TransportPluginKey");
2
- export const kIterableResponse = Symbol.for("neemata:IterableResponseKey");
3
-
4
- //# sourceMappingURL=constants.js.map
1
+ export const kTransportPlugin = Symbol.for('neemata:TransportPluginKey');
2
+ export const kIterableResponse = Symbol.for('neemata:IterableResponseKey');
@@ -0,0 +1,40 @@
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
+ export interface BaseServerDecoder {
6
+ accept: Pattern[];
7
+ decode(buffer: ArrayBuffer): any;
8
+ decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext<ProtocolClientStream>): ProtocolRPC;
9
+ }
10
+ export interface BaseServerEncoder {
11
+ contentType: string;
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;
23
+ }
24
+ export declare abstract class BaseServerFormat implements BaseServerDecoder, BaseServerEncoder {
25
+ abstract accept: Pattern[];
26
+ abstract contentType: string;
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;
31
+ }
32
+ export declare const parseContentTypes: (types: string) => string[];
33
+ export declare class Format {
34
+ decoders: Map<Pattern, BaseServerDecoder>;
35
+ encoders: Map<Pattern, BaseServerEncoder>;
36
+ constructor(formats: BaseServerFormat[]);
37
+ supportsDecoder(contentType: string, throwIfUnsupported?: boolean): BaseServerDecoder | null;
38
+ supportsEncoder(contentType: string, throwIfUnsupported?: boolean): BaseServerEncoder | null;
39
+ private supports;
40
+ }
@@ -1,47 +1,60 @@
1
- import { match } from "@nmtjs/core";
2
- export class BaseServerFormat {}
1
+ import { match } from '@nmtjs/core';
2
+ export class BaseServerFormat {
3
+ }
3
4
  export const parseContentTypes = (types) => {
4
- if (types === "*/*") return ["*/*"];
5
- return types.split(",").map((t) => {
6
- const [type, ...rest] = t.split(";");
7
- const params = new Map(rest.map((p) => p.trim().split("=").slice(0, 2).map((p) => p.trim())));
8
- return {
9
- type,
10
- q: params.has("q") ? Number.parseFloat(params.get("q")) : 1
11
- };
12
- }).sort((a, b) => {
13
- if (a.type === "*/*") return 1;
14
- if (b.type === "*/*") return -1;
15
- return b.q - a.q ? -1 : 1;
16
- }).map((t) => t.type);
5
+ if (types === '*/*')
6
+ return ['*/*'];
7
+ return types
8
+ .split(',')
9
+ .map((t) => {
10
+ const [type, ...rest] = t.split(';');
11
+ const params = new Map(rest.map((p) => p
12
+ .trim()
13
+ .split('=')
14
+ .slice(0, 2)
15
+ .map((p) => p.trim())));
16
+ return {
17
+ type,
18
+ q: params.has('q') ? Number.parseFloat(params.get('q')) : 1,
19
+ };
20
+ })
21
+ .sort((a, b) => {
22
+ if (a.type === '*/*')
23
+ return 1;
24
+ if (b.type === '*/*')
25
+ return -1;
26
+ return b.q - a.q ? -1 : 1;
27
+ })
28
+ .map((t) => t.type);
17
29
  };
18
30
  export class Format {
19
- decoders = new Map();
20
- encoders = new Map();
21
- constructor(formats) {
22
- for (const format of formats) {
23
- this.encoders.set(format.contentType, format);
24
- for (const acceptType of format.accept) {
25
- this.decoders.set(acceptType, format);
26
- }
27
- }
28
- }
29
- supportsDecoder(contentType, throwIfUnsupported = false) {
30
- return this.supports(this.decoders, contentType, throwIfUnsupported);
31
- }
32
- supportsEncoder(contentType, throwIfUnsupported = false) {
33
- return this.supports(this.encoders, contentType, throwIfUnsupported);
34
- }
35
- supports(formats, contentType, throwIfUnsupported = false) {
36
- const types = parseContentTypes(contentType);
37
- for (const type of types) {
38
- for (const [pattern, format] of formats) {
39
- if (type === "*/*" || match(type, pattern)) return format;
40
- }
41
- }
42
- if (throwIfUnsupported) throw new Error(`No supported format found: ${contentType}`);
43
- return null;
44
- }
31
+ decoders = new Map();
32
+ encoders = new Map();
33
+ constructor(formats) {
34
+ for (const format of formats) {
35
+ this.encoders.set(format.contentType, format);
36
+ for (const acceptType of format.accept) {
37
+ this.decoders.set(acceptType, format);
38
+ }
39
+ }
40
+ }
41
+ supportsDecoder(contentType, throwIfUnsupported = false) {
42
+ return this.supports(this.decoders, contentType, throwIfUnsupported);
43
+ }
44
+ supportsEncoder(contentType, throwIfUnsupported = false) {
45
+ return this.supports(this.encoders, contentType, throwIfUnsupported);
46
+ }
47
+ supports(formats, contentType, throwIfUnsupported = false) {
48
+ // TODO: Use node:utils.MIMEType (not implemented yet in Deno and Bun yet)
49
+ const types = parseContentTypes(contentType);
50
+ for (const type of types) {
51
+ for (const [pattern, format] of formats) {
52
+ if (type === '*/*' || match(type, pattern))
53
+ return format;
54
+ }
55
+ }
56
+ if (throwIfUnsupported)
57
+ throw new Error(`No supported format found: ${contentType}`);
58
+ return null;
59
+ }
45
60
  }
46
-
47
- //# sourceMappingURL=format.js.map
@@ -0,0 +1,11 @@
1
+ export * from './api.ts';
2
+ export * from './connection.ts';
3
+ export * from './constants.ts';
4
+ export * from './format.ts';
5
+ export * from './injectables.ts';
6
+ export * from './protocol.ts';
7
+ export * from './registry.ts';
8
+ export * from './stream.ts';
9
+ export * from './transport.ts';
10
+ export * from './types.ts';
11
+ export * from './utils.ts';
@@ -9,5 +9,3 @@ export * from "./stream.js";
9
9
  export * from "./transport.js";
10
10
  export * from "./types.js";
11
11
  export * from "./utils.js";
12
-
13
- //# sourceMappingURL=index.js.map
@@ -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
+ };
@@ -1,24 +1,22 @@
1
- import { createFactoryInjectable, createLazyInjectable, Scope } from "@nmtjs/core";
2
- const connection = createLazyInjectable(Scope.Connection, "RPC connection");
1
+ import { createFactoryInjectable, createLazyInjectable, Scope, } from '@nmtjs/core';
2
+ const connection = createLazyInjectable(Scope.Connection, 'RPC connection');
3
3
  const connectionData = createLazyInjectable(Scope.Connection, "RPC connection's data");
4
- const connectionAbortSignal = createLazyInjectable(Scope.Connection, "Connection abort signal");
5
- const rpcClientAbortSignal = createLazyInjectable(Scope.Call, "RPC client abort signal");
6
- const rpcTimeoutSignal = createLazyInjectable(Scope.Call, "RPC timeout signal");
4
+ const connectionAbortSignal = createLazyInjectable(Scope.Connection, 'Connection abort signal');
5
+ const rpcClientAbortSignal = createLazyInjectable(Scope.Call, 'RPC client abort signal');
6
+ const rpcTimeoutSignal = createLazyInjectable(Scope.Call, 'RPC timeout signal');
7
7
  const rpcAbortSignal = createFactoryInjectable({
8
- dependencies: {
9
- rpcTimeoutSignal,
10
- rpcClientAbortSignal,
11
- connectionAbortSignal
12
- },
13
- factory: (ctx) => AbortSignal.any(Object.values(ctx))
14
- }, "Any RPC abort signal");
8
+ dependencies: {
9
+ rpcTimeoutSignal,
10
+ rpcClientAbortSignal,
11
+ connectionAbortSignal,
12
+ },
13
+ factory: (ctx) => AbortSignal.any(Object.values(ctx)),
14
+ }, 'Any RPC abort signal');
15
15
  export const ProtocolInjectables = {
16
- connection,
17
- connectionData,
18
- connectionAbortSignal,
19
- rpcClientAbortSignal,
20
- rpcTimeoutSignal,
21
- rpcAbortSignal
16
+ connection,
17
+ connectionData,
18
+ connectionAbortSignal,
19
+ rpcClientAbortSignal,
20
+ rpcTimeoutSignal,
21
+ rpcAbortSignal,
22
22
  };
23
-
24
- //# sourceMappingURL=injectables.js.map
@@ -0,0 +1,118 @@
1
+ import { type Callback } from '@nmtjs/common';
2
+ import { type AnyInjectable, type Container, type Logger } from '@nmtjs/core';
3
+ import type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts';
4
+ import type { ProtocolRPC } from '../common/types.ts';
5
+ import { type ProtocolApi, type ProtocolApiCallOptions } from './api.ts';
6
+ import { Connection, ConnectionContext, type ConnectionOptions } from './connection.ts';
7
+ import type { Format } from './format.ts';
8
+ import type { ProtocolRegistry } from './registry.ts';
9
+ import { ProtocolClientStream, ProtocolServerStream } from './stream.ts';
10
+ import type { Transport } from './transport.ts';
11
+ import { type ResolveFormatParams } from './utils.ts';
12
+ export declare class ProtocolError extends Error {
13
+ code: string;
14
+ data?: any;
15
+ constructor(code: string, message?: string, data?: any);
16
+ get message(): string;
17
+ toString(): string;
18
+ toJSON(): {
19
+ code: string;
20
+ message: string;
21
+ data: any;
22
+ };
23
+ }
24
+ export type ProtocolConnectionTransport = {
25
+ send: Transport<any>['send'];
26
+ };
27
+ export declare class ProtocolConnections {
28
+ #private;
29
+ private readonly application;
30
+ constructor(application: {
31
+ logger: Logger;
32
+ registry: ProtocolRegistry;
33
+ format: Format;
34
+ container: Container;
35
+ });
36
+ get(connectionId: string): {
37
+ connection: Connection;
38
+ context: ConnectionContext;
39
+ transport: ProtocolConnectionTransport;
40
+ };
41
+ add<T>(transport: ProtocolConnectionTransport, options: ConnectionOptions<T>, params: ResolveFormatParams): Promise<{
42
+ connection: Connection<T>;
43
+ context: ConnectionContext;
44
+ }>;
45
+ remove(connectionId: string): Promise<void>;
46
+ initialize(connection: Connection): Promise<void>;
47
+ }
48
+ export declare class ProtocolClientStreams {
49
+ private readonly connections;
50
+ constructor(connections: ProtocolConnections);
51
+ get(connectionId: string, streamId: number): ProtocolClientStream;
52
+ remove(connectionId: string, streamId: number): void;
53
+ add(connectionId: string, streamId: number, metadata: ProtocolBlobMetadata, read: Callback): ProtocolClientStream;
54
+ push(connectionId: string, streamId: number, chunk: ArrayBuffer): void;
55
+ end(connectionId: string, streamId: number): void;
56
+ abort(connectionId: string, streamId: number, error?: Error): void;
57
+ }
58
+ export declare class ProtocolServerStreams {
59
+ private readonly connections;
60
+ constructor(connections: ProtocolConnections);
61
+ get(connectionId: string, streamId: number): ProtocolServerStream;
62
+ add(connectionId: string, streamId: number, blob: ProtocolBlob): ProtocolServerStream;
63
+ remove(connectionId: string, streamId: number): void;
64
+ pull(connectionId: string, streamId: number): void;
65
+ abort(connectionId: string, streamId: number, error?: Error): void;
66
+ }
67
+ export type ProtocolRPCOptions = {
68
+ signal?: AbortSignal;
69
+ provides?: [AnyInjectable, any][];
70
+ metadata?: ProtocolApiCallOptions['metadata'];
71
+ };
72
+ export declare class Protocol {
73
+ #private;
74
+ protected readonly application: {
75
+ logger: Logger;
76
+ format: Format;
77
+ container: Container;
78
+ registry: ProtocolRegistry;
79
+ api: ProtocolApi;
80
+ };
81
+ constructor(application: {
82
+ logger: Logger;
83
+ format: Format;
84
+ container: Container;
85
+ registry: ProtocolRegistry;
86
+ api: ProtocolApi;
87
+ });
88
+ call(options: ProtocolApiCallOptions): Promise<import("./api.ts").ProtocolApiCallResult>;
89
+ rpc(connectionId: string, rpc: ProtocolRPC, params?: ProtocolRPCOptions): Promise<void>;
90
+ rpcRaw(connectionId: string, buffer: ArrayBuffer, params?: ProtocolRPCOptions): Promise<void>;
91
+ rpcAbort(connectionId: string, callId: number): void;
92
+ rpcAbortRaw(connectionId: string, buffer: ArrayBuffer): void;
93
+ rpcStreamAbort(connectionId: string, callId: number): void;
94
+ rpcStreamAbortRaw(connectionId: string, buffer: ArrayBuffer): void;
95
+ notify(connectionId: string, event: any, payload: any): void;
96
+ addConnection(transport: ProtocolConnectionTransport, options: ConnectionOptions, params: ResolveFormatParams): Promise<{
97
+ connection: Connection<unknown>;
98
+ context: ConnectionContext;
99
+ }>;
100
+ removeConnection(connectionId: string): Promise<void>;
101
+ getConnection(connectionId: string): {
102
+ connection: Connection;
103
+ context: ConnectionContext;
104
+ transport: ProtocolConnectionTransport;
105
+ };
106
+ initializeConnection(connection: Connection): Promise<void>;
107
+ getClientStream(connectionId: string, streamId: number): ProtocolClientStream;
108
+ addClientStream(connectionId: string, streamId: number, metadata: ProtocolBlobMetadata, read: Callback): ProtocolClientStream;
109
+ removeClientStream(connectionId: string, streamId: number): void;
110
+ pushClientStream(connectionId: string, streamId: number, chunk: ArrayBuffer): void;
111
+ endClientStream(connectionId: string, streamId: number): void;
112
+ abortClientStream(connectionId: string, streamId: number, error?: Error): void;
113
+ getServerStream(connectionId: string, streamId: number): ProtocolServerStream;
114
+ addServerStream(connectionId: string, streamId: number, blob: ProtocolBlob): ProtocolServerStream;
115
+ removeServerStream(connectionId: string, streamId: number): void;
116
+ pullServerStream(connectionId: string, streamId: number): void;
117
+ abortServerStream(connectionId: string, streamId: number, error?: Error): void;
118
+ }