@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.
- package/dist/client/events.d.ts +17 -0
- package/dist/client/events.js +26 -30
- package/dist/client/format.d.ts +21 -0
- package/dist/client/format.js +2 -3
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +1 -2
- package/dist/client/protocol.d.ts +149 -0
- package/dist/client/protocol.js +342 -343
- package/dist/client/stream.d.ts +28 -0
- package/dist/client/stream.js +88 -80
- package/dist/client/types.d.ts +8 -0
- package/dist/client/types.js +0 -2
- package/dist/common/binary.d.ts +19 -0
- package/dist/common/binary.js +17 -17
- package/dist/common/blob.d.ts +22 -0
- package/dist/common/blob.js +44 -40
- package/dist/common/enums.d.ts +41 -0
- package/dist/common/enums.js +45 -47
- package/dist/common/index.d.ts +4 -0
- package/dist/common/index.js +0 -2
- package/dist/common/types.d.ts +34 -0
- package/dist/common/types.js +0 -2
- package/dist/server/api.d.ts +34 -0
- package/dist/server/api.js +7 -9
- package/dist/server/connection.d.ts +25 -0
- package/dist/server/connection.js +18 -20
- package/dist/server/constants.d.ts +4 -0
- package/dist/server/constants.js +2 -4
- package/dist/server/format.d.ts +40 -0
- package/dist/server/format.js +56 -43
- package/dist/server/index.d.ts +11 -0
- package/dist/server/index.js +0 -2
- package/dist/server/injectables.d.ts +14 -0
- package/dist/server/injectables.js +18 -20
- package/dist/server/protocol.d.ts +118 -0
- package/dist/server/protocol.js +365 -384
- package/dist/server/registry.d.ts +3 -0
- package/dist/server/registry.js +3 -4
- package/dist/server/stream.d.ts +12 -0
- package/dist/server/stream.js +28 -26
- package/dist/server/transport.d.ts +23 -0
- package/dist/server/transport.js +1 -7
- package/dist/server/types.d.ts +13 -0
- package/dist/server/types.js +0 -2
- package/dist/server/utils.d.ts +15 -0
- package/dist/server/utils.js +16 -13
- package/package.json +17 -14
- package/src/server/api.ts +2 -1
- package/src/server/protocol.ts +1 -1
- package/dist/client/events.js.map +0 -1
- package/dist/client/format.js.map +0 -1
- package/dist/client/index.js.map +0 -1
- package/dist/client/protocol.js.map +0 -1
- package/dist/client/stream.js.map +0 -1
- package/dist/client/types.js.map +0 -1
- package/dist/common/binary.js.map +0 -1
- package/dist/common/blob.js.map +0 -1
- package/dist/common/enums.js.map +0 -1
- package/dist/common/index.js.map +0 -1
- package/dist/common/types.js.map +0 -1
- package/dist/server/api.js.map +0 -1
- package/dist/server/connection.js.map +0 -1
- package/dist/server/constants.js.map +0 -1
- package/dist/server/format.js.map +0 -1
- package/dist/server/index.js.map +0 -1
- package/dist/server/injectables.js.map +0 -1
- package/dist/server/protocol.js.map +0 -1
- package/dist/server/registry.js.map +0 -1
- package/dist/server/stream.js.map +0 -1
- package/dist/server/transport.js.map +0 -1
- package/dist/server/types.js.map +0 -1
- package/dist/server/utils.js.map +0 -1
package/dist/server/protocol.js
CHANGED
|
@@ -1,399 +1,380 @@
|
|
|
1
|
-
import { defer, isAbortError, throwError } from
|
|
2
|
-
import { Hook, Scope } from
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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
|