@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
@@ -1,399 +1,380 @@
1
- import { defer, isAbortError, throwError } from "@nmtjs/common";
2
- import { Hook, Scope } from "@nmtjs/core";
1
+ import { defer, isAbortError, throwError } from '@nmtjs/common';
2
+ import { Hook, Scope, } from '@nmtjs/core';
3
3
  import { concat, decodeNumber, encodeNumber } from "../common/binary.js";
4
4
  import { ErrorCode, ServerMessageType } from "../common/enums.js";
5
- import { isIterableResult } from "./api.js";
6
- import { Connection, ConnectionContext } from "./connection.js";
5
+ import { isIterableResult, } from "./api.js";
6
+ import { Connection, ConnectionContext, } from "./connection.js";
7
7
  import { ProtocolInjectables } from "./injectables.js";
8
8
  import { ProtocolClientStream, ProtocolServerStream } from "./stream.js";
9
9
  import { getFormat } from "./utils.js";
10
10
  export class ProtocolError extends Error {
11
- code;
12
- data;
13
- constructor(code, message, data) {
14
- super(message);
15
- this.code = code;
16
- this.data = data;
17
- }
18
- get message() {
19
- return `${this.code} ${super.message}`;
20
- }
21
- toString() {
22
- return `${this.code} ${this.message}`;
23
- }
24
- toJSON() {
25
- return {
26
- code: this.code,
27
- message: this.message,
28
- data: this.data
29
- };
30
- }
11
+ code;
12
+ data;
13
+ constructor(code, message, data) {
14
+ super(message);
15
+ this.code = code;
16
+ this.data = data;
17
+ }
18
+ get message() {
19
+ return `${this.code} ${super.message}`;
20
+ }
21
+ toString() {
22
+ return `${this.code} ${this.message}`;
23
+ }
24
+ toJSON() {
25
+ return {
26
+ code: this.code,
27
+ message: this.message,
28
+ data: this.data,
29
+ };
30
+ }
31
31
  }
32
32
  export class ProtocolConnections {
33
- #collection = new Map();
34
- constructor(application) {
35
- this.application = application;
36
- }
37
- get(connectionId) {
38
- const connection = this.#collection.get(connectionId);
39
- if (!connection) throwError("Connection not found");
40
- return connection;
41
- }
42
- async add(transport, options, params) {
43
- const connection = new Connection(options);
44
- const format = getFormat(this.application.format, params);
45
- const container = this.application.container.fork(Scope.Connection);
46
- const context = new ConnectionContext(container, format);
47
- container.provide(ProtocolInjectables.connection, connection);
48
- try {
49
- await this.initialize(connection);
50
- this.#collection.set(connection.id, {
51
- connection,
52
- context,
53
- transport
54
- });
55
- return {
56
- connection,
57
- context
58
- };
59
- } catch (error) {
60
- container.dispose().catch((error) => {
61
- this.application.logger.error({
62
- error,
63
- connection
64
- }, "Error during disposing connection");
65
- });
66
- throw error;
67
- }
68
- }
69
- async remove(connectionId) {
70
- const { connection, context } = this.get(connectionId);
71
- this.application.registry.hooks.call(Hook.OnDisconnect, { concurrent: true }, connection);
72
- this.#collection.delete(connectionId);
73
- const { rpcs, serverStreams, clientStreams, rpcStreams, container } = context;
74
- for (const call of rpcs.values()) {
75
- call.abort(new Error("Connection closed"));
76
- }
77
- for (const stream of clientStreams.values()) {
78
- stream.destroy(new Error("Connection closed"));
79
- }
80
- for (const stream of serverStreams.values()) {
81
- stream.destroy(new Error("Connection closed"));
82
- }
83
- for (const stream of rpcStreams.values()) {
84
- stream.abort(new Error("Connection closed"));
85
- }
86
- try {
87
- await container.dispose();
88
- } catch (error) {
89
- this.application.logger.error({
90
- error,
91
- connection
92
- }, "Error during closing connection");
93
- }
94
- }
95
- async initialize(connection) {
96
- await this.application.registry.hooks.call(Hook.OnConnect, { concurrent: false }, connection);
97
- }
33
+ application;
34
+ #collection = new Map();
35
+ constructor(application) {
36
+ this.application = application;
37
+ }
38
+ get(connectionId) {
39
+ const connection = this.#collection.get(connectionId);
40
+ if (!connection)
41
+ throwError('Connection not found');
42
+ return connection;
43
+ }
44
+ async add(transport, options, params) {
45
+ const connection = new Connection(options);
46
+ const format = getFormat(this.application.format, params);
47
+ const container = this.application.container.fork(Scope.Connection);
48
+ const context = new ConnectionContext(container, format);
49
+ container.provide(ProtocolInjectables.connection, connection);
50
+ try {
51
+ await this.initialize(connection);
52
+ this.#collection.set(connection.id, { connection, context, transport });
53
+ return { connection, context };
54
+ }
55
+ catch (error) {
56
+ container.dispose().catch((error) => {
57
+ this.application.logger.error({ error, connection }, 'Error during disposing connection');
58
+ });
59
+ throw error;
60
+ }
61
+ }
62
+ async remove(connectionId) {
63
+ const { connection, context } = this.get(connectionId);
64
+ this.application.registry.hooks.call(Hook.OnDisconnect, { concurrent: true }, connection);
65
+ this.#collection.delete(connectionId);
66
+ const { rpcs, serverStreams, clientStreams, rpcStreams, container } = context;
67
+ for (const call of rpcs.values()) {
68
+ call.abort(new Error('Connection closed'));
69
+ }
70
+ for (const stream of clientStreams.values()) {
71
+ stream.destroy(new Error('Connection closed'));
72
+ }
73
+ for (const stream of serverStreams.values()) {
74
+ stream.destroy(new Error('Connection closed'));
75
+ }
76
+ for (const stream of rpcStreams.values()) {
77
+ stream.abort(new Error('Connection closed'));
78
+ }
79
+ try {
80
+ await container.dispose();
81
+ }
82
+ catch (error) {
83
+ this.application.logger.error({ error, connection }, 'Error during closing connection');
84
+ }
85
+ }
86
+ async initialize(connection) {
87
+ await this.application.registry.hooks.call(Hook.OnConnect, { concurrent: false }, connection);
88
+ }
98
89
  }
99
90
  export class ProtocolClientStreams {
100
- constructor(connections) {
101
- this.connections = connections;
102
- }
103
- get(connectionId, streamId) {
104
- const { context } = this.connections.get(connectionId);
105
- const { clientStreams } = context;
106
- const stream = clientStreams.get(streamId) ?? throwError("Stream not found");
107
- return stream;
108
- }
109
- remove(connectionId, streamId) {
110
- const { context } = this.connections.get(connectionId);
111
- const { clientStreams } = context;
112
- clientStreams.get(streamId) || throwError("Stream not found");
113
- clientStreams.delete(streamId);
114
- }
115
- add(connectionId, streamId, metadata, read) {
116
- const { context } = this.connections.get(connectionId);
117
- const { clientStreams } = context;
118
- const stream = new ProtocolClientStream(streamId, metadata, { read });
119
- clientStreams.set(streamId, stream);
120
- return stream;
121
- }
122
- push(connectionId, streamId, chunk) {
123
- const stream = this.get(connectionId, streamId);
124
- stream.write(Buffer.from(chunk));
125
- }
126
- end(connectionId, streamId) {
127
- const stream = this.get(connectionId, streamId);
128
- stream.end(null);
129
- this.remove(connectionId, streamId);
130
- }
131
- abort(connectionId, streamId, error = new Error("Aborted")) {
132
- const stream = this.get(connectionId, streamId);
133
- stream.destroy(error);
134
- this.remove(connectionId, streamId);
135
- }
91
+ connections;
92
+ constructor(connections) {
93
+ this.connections = connections;
94
+ }
95
+ get(connectionId, streamId) {
96
+ const { context } = this.connections.get(connectionId);
97
+ const { clientStreams } = context;
98
+ const stream = clientStreams.get(streamId) ?? throwError('Stream not found');
99
+ return stream;
100
+ }
101
+ remove(connectionId, streamId) {
102
+ const { context } = this.connections.get(connectionId);
103
+ const { clientStreams } = context;
104
+ clientStreams.get(streamId) || throwError('Stream not found');
105
+ clientStreams.delete(streamId);
106
+ }
107
+ add(connectionId, streamId, metadata, read) {
108
+ const { context } = this.connections.get(connectionId);
109
+ const { clientStreams } = context;
110
+ const stream = new ProtocolClientStream(streamId, metadata, { read });
111
+ clientStreams.set(streamId, stream);
112
+ return stream;
113
+ }
114
+ push(connectionId, streamId, chunk) {
115
+ const stream = this.get(connectionId, streamId);
116
+ stream.write(Buffer.from(chunk));
117
+ }
118
+ end(connectionId, streamId) {
119
+ const stream = this.get(connectionId, streamId);
120
+ stream.end(null);
121
+ this.remove(connectionId, streamId);
122
+ }
123
+ abort(connectionId, streamId, error = new Error('Aborted')) {
124
+ const stream = this.get(connectionId, streamId);
125
+ stream.destroy(error);
126
+ this.remove(connectionId, streamId);
127
+ }
136
128
  }
137
129
  export class ProtocolServerStreams {
138
- constructor(connections) {
139
- this.connections = connections;
140
- }
141
- get(connectionId, streamId) {
142
- const { context } = this.connections.get(connectionId);
143
- const { serverStreams } = context;
144
- const stream = serverStreams.get(streamId) ?? throwError("Stream not found");
145
- return stream;
146
- }
147
- add(connectionId, streamId, blob) {
148
- const { context } = this.connections.get(connectionId);
149
- const { serverStreams } = context;
150
- const stream = new ProtocolServerStream(streamId, blob);
151
- serverStreams.set(streamId, stream);
152
- return stream;
153
- }
154
- remove(connectionId, streamId) {
155
- const { context } = this.connections.get(connectionId);
156
- const { serverStreams } = context;
157
- serverStreams.has(streamId) || throwError("Stream not found");
158
- serverStreams.delete(streamId);
159
- }
160
- pull(connectionId, streamId) {
161
- const stream = this.get(connectionId, streamId);
162
- stream.resume();
163
- }
164
- abort(connectionId, streamId, error = new Error("Aborted")) {
165
- const stream = this.get(connectionId, streamId);
166
- stream.destroy(error);
167
- this.remove(connectionId, streamId);
168
- }
130
+ connections;
131
+ constructor(connections) {
132
+ this.connections = connections;
133
+ }
134
+ get(connectionId, streamId) {
135
+ const { context } = this.connections.get(connectionId);
136
+ const { serverStreams } = context;
137
+ const stream = serverStreams.get(streamId) ?? throwError('Stream not found');
138
+ return stream;
139
+ }
140
+ add(connectionId, streamId, blob) {
141
+ const { context } = this.connections.get(connectionId);
142
+ const { serverStreams } = context;
143
+ const stream = new ProtocolServerStream(streamId, blob);
144
+ serverStreams.set(streamId, stream);
145
+ return stream;
146
+ }
147
+ remove(connectionId, streamId) {
148
+ const { context } = this.connections.get(connectionId);
149
+ const { serverStreams } = context;
150
+ serverStreams.has(streamId) || throwError('Stream not found');
151
+ serverStreams.delete(streamId);
152
+ }
153
+ pull(connectionId, streamId) {
154
+ const stream = this.get(connectionId, streamId);
155
+ stream.resume();
156
+ }
157
+ abort(connectionId, streamId, error = new Error('Aborted')) {
158
+ const stream = this.get(connectionId, streamId);
159
+ stream.destroy(error);
160
+ this.remove(connectionId, streamId);
161
+ }
169
162
  }
170
163
  export class Protocol {
171
- #connections;
172
- #clientStreams;
173
- #serverStreams;
174
- constructor(application) {
175
- this.application = application;
176
- this.#connections = new ProtocolConnections(this.application);
177
- this.#clientStreams = new ProtocolClientStreams(this.#connections);
178
- this.#serverStreams = new ProtocolServerStreams(this.#connections);
179
- }
180
- async call(options) {
181
- const { container, connection } = options;
182
- try {
183
- return await this.application.api.call(options);
184
- } catch (error) {
185
- if (error instanceof ProtocolError === false) {
186
- this.application.logger.error({
187
- error,
188
- connection
189
- }, "Error during RPC call");
190
- throw new ProtocolError(ErrorCode.InternalServerError, "Internal server error");
191
- }
192
- throw error;
193
- } finally {
194
- container.dispose().catch((error) => {
195
- this.application.logger.error({
196
- error,
197
- connection
198
- }, "Error during disposing connection's container");
199
- });
200
- }
201
- }
202
- async rpc(connectionId, rpc, params = {}) {
203
- const { connection, context, transport } = this.#connections.get(connectionId);
204
- const { rpcs, format } = context;
205
- const { callId, namespace, procedure, payload } = rpc;
206
- const abortController = new AbortController();
207
- const signal = params.signal ? AbortSignal.any([params.signal, abortController.signal]) : abortController.signal;
208
- rpcs.set(callId, abortController);
209
- const callIdEncoded = encodeNumber(callId, "Uint32");
210
- const container = context.container.fork(Scope.Call);
211
- if (params.provides) {
212
- for (const [key, value] of params.provides) {
213
- container.provide(key, value);
214
- }
215
- }
216
- try {
217
- const response = await this.call({
218
- connection,
219
- container,
220
- namespace,
221
- payload,
222
- procedure,
223
- signal,
224
- metadata: params.metadata
225
- });
226
- const responseEncoded = format.encoder.encodeRPC({
227
- callId,
228
- result: response.output
229
- }, {
230
- addStream: (blob) => {
231
- const streamId = context.streamId++;
232
- const stream = this.#serverStreams.add(connectionId, streamId, blob);
233
- stream.on("data", (chunk) => {
234
- stream.pause();
235
- const buf = Buffer.from(chunk);
236
- transport.send(connection, ServerMessageType.ServerStreamPush, concat(encodeNumber(streamId, "Uint32"), buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)), {
237
- callId,
238
- streamId
239
- });
240
- });
241
- stream.on("error", (err) => {
242
- transport.send(connection, ServerMessageType.ServerStreamAbort, encodeNumber(streamId, "Uint32"), {
243
- callId,
244
- streamId
245
- });
246
- });
247
- stream.on("end", () => {
248
- transport.send(connection, ServerMessageType.ServerStreamEnd, encodeNumber(streamId, "Uint32"), {
249
- callId,
250
- streamId
251
- });
252
- });
253
- return stream;
254
- },
255
- getStream: (id) => {
256
- return this.#serverStreams.get(connectionId, id);
257
- }
258
- });
259
- if (isIterableResult(response)) {
260
- transport.send(connection, ServerMessageType.RpcStreamResponse, responseEncoded, { callId });
261
- try {
262
- const controller = new AbortController();
263
- context.rpcStreams.set(callId, controller);
264
- const iterable = typeof response.iterable === "function" ? response.iterable(controller.signal) : response.iterable;
265
- try {
266
- for await (const chunk of iterable) {
267
- controller.signal.throwIfAborted();
268
- const chunkEncoded = format.encoder.encode(chunk);
269
- transport.send(connection, ServerMessageType.RpcStreamChunk, concat(callIdEncoded, chunkEncoded), { callId });
270
- }
271
- transport.send(connection, ServerMessageType.RpcStreamEnd, callIdEncoded, { callId });
272
- } catch (error) {
273
- if (!isAbortError(error)) {
274
- throw error;
275
- }
276
- }
277
- } catch (error) {
278
- this.application.logger.error(error);
279
- transport.send(connection, ServerMessageType.RpcStreamAbort, callIdEncoded, { callId });
280
- } finally {
281
- context.rpcStreams.delete(callId);
282
- response.onFinish && defer(response.onFinish);
283
- }
284
- } else {
285
- transport.send(connection, ServerMessageType.RpcResponse, responseEncoded, { callId });
286
- }
287
- } catch (error) {
288
- const payload = format.encoder.encodeRPC({
289
- callId,
290
- error
291
- }, {
292
- addStream(blob) {
293
- throwError("Cannot handle stream for error response");
294
- },
295
- getStream(id) {
296
- throwError("Cannot handle stream for error response");
297
- }
298
- });
299
- transport.send(connection, ServerMessageType.RpcResponse, payload, {
300
- error,
301
- callId
302
- });
303
- } finally {
304
- rpcs.delete(callId);
305
- container.dispose().catch((error) => {
306
- this.application.logger.error({
307
- error,
308
- connection
309
- }, "Error during disposing connection");
310
- });
311
- }
312
- }
313
- async rpcRaw(connectionId, buffer, params = {}) {
314
- const { connection, context, transport } = this.#connections.get(connectionId);
315
- const { format } = context;
316
- const rpc = format.decoder.decodeRPC(buffer, {
317
- addStream: (streamId, callId, metadata) => {
318
- return this.#clientStreams.add(connectionId, streamId, metadata, (size) => {
319
- transport.send(connection, ServerMessageType.ClientStreamPull, concat(encodeNumber(streamId, "Uint32"), encodeNumber(size, "Uint32")), {
320
- callId,
321
- streamId
322
- });
323
- });
324
- },
325
- getStream: (id) => {
326
- return this.#clientStreams.get(connectionId, id);
327
- }
328
- });
329
- return await this.rpc(connectionId, rpc, params);
330
- }
331
- rpcAbort(connectionId, callId) {
332
- const { context } = this.#connections.get(connectionId);
333
- const call = context.rpcs.get(callId) ?? throwError("Call not found");
334
- call.abort();
335
- }
336
- rpcAbortRaw(connectionId, buffer) {
337
- const callId = decodeNumber(buffer, "Uint32");
338
- return this.rpcAbort(connectionId, callId);
339
- }
340
- rpcStreamAbort(connectionId, callId) {
341
- const { context } = this.#connections.get(connectionId);
342
- const ab = context.rpcStreams.get(callId) ?? throwError("Call stream not found");
343
- ab.abort();
344
- }
345
- rpcStreamAbortRaw(connectionId, buffer) {
346
- const callId = decodeNumber(buffer, "Uint32");
347
- return this.rpcStreamAbort(connectionId, callId);
348
- }
349
- notify(connectionId, event, payload) {
350
- throw Error("Unimplemented");
351
- }
352
- addConnection(transport, options, params) {
353
- return this.#connections.add(transport, options, params);
354
- }
355
- removeConnection(connectionId) {
356
- return this.#connections.remove(connectionId);
357
- }
358
- getConnection(connectionId) {
359
- return this.#connections.get(connectionId);
360
- }
361
- initializeConnection(connection) {
362
- return this.#connections.initialize(connection);
363
- }
364
- getClientStream(connectionId, streamId) {
365
- return this.#clientStreams.get(connectionId, streamId);
366
- }
367
- addClientStream(connectionId, streamId, metadata, read) {
368
- return this.#clientStreams.add(connectionId, streamId, metadata, read);
369
- }
370
- removeClientStream(connectionId, streamId) {
371
- return this.#clientStreams.remove(connectionId, streamId);
372
- }
373
- pushClientStream(connectionId, streamId, chunk) {
374
- return this.#clientStreams.push(connectionId, streamId, chunk);
375
- }
376
- endClientStream(connectionId, streamId) {
377
- return this.#clientStreams.end(connectionId, streamId);
378
- }
379
- abortClientStream(connectionId, streamId, error) {
380
- return this.#clientStreams.abort(connectionId, streamId, error);
381
- }
382
- getServerStream(connectionId, streamId) {
383
- return this.#serverStreams.get(connectionId, streamId);
384
- }
385
- addServerStream(connectionId, streamId, blob) {
386
- return this.#serverStreams.add(connectionId, streamId, blob);
387
- }
388
- removeServerStream(connectionId, streamId) {
389
- return this.#serverStreams.remove(connectionId, streamId);
390
- }
391
- pullServerStream(connectionId, streamId) {
392
- return this.#serverStreams.pull(connectionId, streamId);
393
- }
394
- abortServerStream(connectionId, streamId, error) {
395
- return this.#serverStreams.abort(connectionId, streamId, error);
396
- }
164
+ application;
165
+ #connections;
166
+ #clientStreams;
167
+ #serverStreams;
168
+ constructor(application) {
169
+ this.application = application;
170
+ this.#connections = new ProtocolConnections(this.application);
171
+ this.#clientStreams = new ProtocolClientStreams(this.#connections);
172
+ this.#serverStreams = new ProtocolServerStreams(this.#connections);
173
+ }
174
+ async call(options) {
175
+ const { container, connection } = options;
176
+ try {
177
+ return await this.application.api.call(options);
178
+ }
179
+ catch (error) {
180
+ if (error instanceof ProtocolError === false) {
181
+ this.application.logger.error({ error, connection }, 'Error during RPC call');
182
+ throw new ProtocolError(ErrorCode.InternalServerError, 'Internal server error');
183
+ }
184
+ throw error;
185
+ }
186
+ finally {
187
+ container.dispose().catch((error) => {
188
+ this.application.logger.error({ error, connection }, "Error during disposing connection's container");
189
+ });
190
+ }
191
+ }
192
+ async rpc(connectionId, rpc, params = {}) {
193
+ const { connection, context, transport } = this.#connections.get(connectionId);
194
+ const { rpcs, format } = context;
195
+ const { callId, namespace, procedure, payload } = rpc;
196
+ const abortController = new AbortController();
197
+ const signal = params.signal
198
+ ? AbortSignal.any([params.signal, abortController.signal])
199
+ : abortController.signal;
200
+ rpcs.set(callId, abortController);
201
+ const callIdEncoded = encodeNumber(callId, 'Uint32');
202
+ const container = context.container.fork(Scope.Call);
203
+ if (params.provides) {
204
+ for (const [key, value] of params.provides) {
205
+ container.provide(key, value);
206
+ }
207
+ }
208
+ try {
209
+ const response = await this.call({
210
+ connection,
211
+ container,
212
+ namespace,
213
+ payload,
214
+ procedure,
215
+ signal,
216
+ metadata: params.metadata,
217
+ });
218
+ const responseEncoded = format.encoder.encodeRPC({
219
+ callId,
220
+ result: response.output,
221
+ }, {
222
+ addStream: (blob) => {
223
+ const streamId = context.streamId++;
224
+ const stream = this.#serverStreams.add(connectionId, streamId, blob);
225
+ stream.on('data', (chunk) => {
226
+ stream.pause();
227
+ const buf = Buffer.from(chunk);
228
+ transport.send(connection, ServerMessageType.ServerStreamPush, concat(encodeNumber(streamId, 'Uint32'), buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)), { callId, streamId });
229
+ });
230
+ stream.on('error', (err) => {
231
+ transport.send(connection, ServerMessageType.ServerStreamAbort, encodeNumber(streamId, 'Uint32'), { callId, streamId });
232
+ });
233
+ stream.on('end', () => {
234
+ transport.send(connection, ServerMessageType.ServerStreamEnd, encodeNumber(streamId, 'Uint32'), { callId, streamId });
235
+ });
236
+ return stream;
237
+ },
238
+ getStream: (id) => {
239
+ return this.#serverStreams.get(connectionId, id);
240
+ },
241
+ });
242
+ if (isIterableResult(response)) {
243
+ transport.send(connection, ServerMessageType.RpcStreamResponse, responseEncoded, { callId });
244
+ try {
245
+ const controller = new AbortController();
246
+ context.rpcStreams.set(callId, controller);
247
+ const iterable = typeof response.iterable === 'function'
248
+ ? await response.iterable(controller.signal)
249
+ : response.iterable;
250
+ try {
251
+ for await (const chunk of iterable) {
252
+ controller.signal.throwIfAborted();
253
+ const chunkEncoded = format.encoder.encode(chunk);
254
+ transport.send(connection, ServerMessageType.RpcStreamChunk, concat(callIdEncoded, chunkEncoded), { callId });
255
+ }
256
+ transport.send(connection, ServerMessageType.RpcStreamEnd, callIdEncoded, { callId });
257
+ }
258
+ catch (error) {
259
+ // do not re-throw AbortError errors, they are expected
260
+ if (!isAbortError(error)) {
261
+ throw error;
262
+ }
263
+ }
264
+ }
265
+ catch (error) {
266
+ this.application.logger.error(error);
267
+ transport.send(connection, ServerMessageType.RpcStreamAbort, callIdEncoded, { callId });
268
+ }
269
+ finally {
270
+ context.rpcStreams.delete(callId);
271
+ response.onFinish && defer(response.onFinish);
272
+ }
273
+ }
274
+ else {
275
+ transport.send(connection, ServerMessageType.RpcResponse, responseEncoded, { callId });
276
+ }
277
+ }
278
+ catch (error) {
279
+ const payload = format.encoder.encodeRPC({ callId, error }, {
280
+ addStream(blob) {
281
+ throwError('Cannot handle stream for error response');
282
+ },
283
+ getStream(id) {
284
+ throwError('Cannot handle stream for error response');
285
+ },
286
+ });
287
+ transport.send(connection, ServerMessageType.RpcResponse, payload, {
288
+ error,
289
+ callId,
290
+ });
291
+ }
292
+ finally {
293
+ rpcs.delete(callId);
294
+ container.dispose().catch((error) => {
295
+ this.application.logger.error({ error, connection }, 'Error during disposing connection');
296
+ });
297
+ }
298
+ }
299
+ async rpcRaw(connectionId, buffer, params = {}) {
300
+ const { connection, context, transport } = this.#connections.get(connectionId);
301
+ const { format } = context;
302
+ const rpc = format.decoder.decodeRPC(buffer, {
303
+ addStream: (streamId, callId, metadata) => {
304
+ return this.#clientStreams.add(connectionId, streamId, metadata, (size) => {
305
+ transport.send(connection, ServerMessageType.ClientStreamPull, concat(encodeNumber(streamId, 'Uint32'), encodeNumber(size, 'Uint32')), { callId, streamId });
306
+ });
307
+ },
308
+ getStream: (id) => {
309
+ return this.#clientStreams.get(connectionId, id);
310
+ },
311
+ });
312
+ return await this.rpc(connectionId, rpc, params);
313
+ }
314
+ rpcAbort(connectionId, callId) {
315
+ const { context } = this.#connections.get(connectionId);
316
+ const call = context.rpcs.get(callId) ?? throwError('Call not found');
317
+ call.abort();
318
+ }
319
+ rpcAbortRaw(connectionId, buffer) {
320
+ const callId = decodeNumber(buffer, 'Uint32');
321
+ return this.rpcAbort(connectionId, callId);
322
+ }
323
+ rpcStreamAbort(connectionId, callId) {
324
+ const { context } = this.#connections.get(connectionId);
325
+ const ab = context.rpcStreams.get(callId) ?? throwError('Call stream not found');
326
+ ab.abort();
327
+ }
328
+ rpcStreamAbortRaw(connectionId, buffer) {
329
+ const callId = decodeNumber(buffer, 'Uint32');
330
+ return this.rpcStreamAbort(connectionId, callId);
331
+ }
332
+ notify(connectionId, event, payload) {
333
+ throw Error('Unimplemented');
334
+ }
335
+ addConnection(transport, options, params) {
336
+ return this.#connections.add(transport, options, params);
337
+ }
338
+ removeConnection(connectionId) {
339
+ return this.#connections.remove(connectionId);
340
+ }
341
+ getConnection(connectionId) {
342
+ return this.#connections.get(connectionId);
343
+ }
344
+ initializeConnection(connection) {
345
+ return this.#connections.initialize(connection);
346
+ }
347
+ getClientStream(connectionId, streamId) {
348
+ return this.#clientStreams.get(connectionId, streamId);
349
+ }
350
+ addClientStream(connectionId, streamId, metadata, read) {
351
+ return this.#clientStreams.add(connectionId, streamId, metadata, read);
352
+ }
353
+ removeClientStream(connectionId, streamId) {
354
+ return this.#clientStreams.remove(connectionId, streamId);
355
+ }
356
+ pushClientStream(connectionId, streamId, chunk) {
357
+ return this.#clientStreams.push(connectionId, streamId, chunk);
358
+ }
359
+ endClientStream(connectionId, streamId) {
360
+ return this.#clientStreams.end(connectionId, streamId);
361
+ }
362
+ abortClientStream(connectionId, streamId, error) {
363
+ return this.#clientStreams.abort(connectionId, streamId, error);
364
+ }
365
+ getServerStream(connectionId, streamId) {
366
+ return this.#serverStreams.get(connectionId, streamId);
367
+ }
368
+ addServerStream(connectionId, streamId, blob) {
369
+ return this.#serverStreams.add(connectionId, streamId, blob);
370
+ }
371
+ removeServerStream(connectionId, streamId) {
372
+ return this.#serverStreams.remove(connectionId, streamId);
373
+ }
374
+ pullServerStream(connectionId, streamId) {
375
+ return this.#serverStreams.pull(connectionId, streamId);
376
+ }
377
+ abortServerStream(connectionId, streamId, error) {
378
+ return this.#serverStreams.abort(connectionId, streamId, error);
379
+ }
397
380
  }
398
-
399
- //# sourceMappingURL=protocol.js.map