@nmtjs/protocol 0.14.5 → 0.15.0-beta.2
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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/client/index.js +3 -1
- package/dist/client/protocol.js +6 -336
- package/dist/client/stream.js +137 -46
- package/dist/client/versions/v1.js +127 -0
- package/dist/common/binary.js +21 -9
- package/dist/common/blob.js +54 -30
- package/dist/common/constants.js +1 -0
- package/dist/common/enums.js +24 -11
- package/dist/common/index.js +2 -0
- package/dist/common/utils.js +6 -0
- package/dist/server/format.js +10 -8
- package/dist/server/index.js +3 -6
- package/dist/server/protocol.js +4 -354
- package/dist/server/stream.js +10 -1
- package/dist/server/utils.js +4 -4
- package/dist/server/versions/v1.js +118 -0
- package/package.json +17 -22
- package/dist/client/events.d.ts +0 -15
- package/dist/client/events.js +0 -28
- package/dist/client/format.d.ts +0 -21
- package/dist/client/index.d.ts +0 -4
- package/dist/client/protocol.d.ts +0 -150
- package/dist/client/stream.d.ts +0 -31
- package/dist/common/binary.d.ts +0 -19
- package/dist/common/blob.d.ts +0 -22
- package/dist/common/enums.d.ts +0 -41
- package/dist/common/index.d.ts +0 -4
- package/dist/common/types.d.ts +0 -33
- package/dist/server/api.d.ts +0 -33
- package/dist/server/api.js +0 -7
- package/dist/server/connection.d.ts +0 -25
- package/dist/server/connection.js +0 -22
- package/dist/server/constants.d.ts +0 -4
- package/dist/server/constants.js +0 -2
- package/dist/server/format.d.ts +0 -40
- package/dist/server/index.d.ts +0 -11
- package/dist/server/injectables.d.ts +0 -14
- package/dist/server/injectables.js +0 -22
- package/dist/server/protocol.d.ts +0 -128
- package/dist/server/registry.d.ts +0 -3
- package/dist/server/registry.js +0 -3
- package/dist/server/stream.d.ts +0 -13
- package/dist/server/transport.d.ts +0 -23
- package/dist/server/transport.js +0 -3
- package/dist/server/types.d.ts +0 -13
- package/dist/server/utils.d.ts +0 -15
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from "./events.js";
|
|
2
1
|
export * from "./format.js";
|
|
3
2
|
export * from "./protocol.js";
|
|
4
3
|
export * from "./stream.js";
|
|
4
|
+
import { ProtocolVersion } from "../common/enums.js";
|
|
5
|
+
import { ProtocolVersion1 } from "./versions/v1.js";
|
|
6
|
+
export const versions = { [ProtocolVersion.v1]: new ProtocolVersion1() };
|
package/dist/client/protocol.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { concat } from "../common/binary.js";
|
|
2
|
+
export class ProtocolVersionInterface {
|
|
3
|
+
encode(...chunks) {
|
|
4
|
+
return concat(...chunks);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
6
7
|
export class ProtocolError extends Error {
|
|
7
8
|
code;
|
|
8
9
|
data;
|
|
@@ -21,334 +22,3 @@ export class ProtocolError extends Error {
|
|
|
21
22
|
return { code: this.code, message: this.message, data: this.data };
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
export class ProtocolClientStreams {
|
|
25
|
-
#collection = new Map();
|
|
26
|
-
get(streamId) {
|
|
27
|
-
const stream = this.#collection.get(streamId);
|
|
28
|
-
if (!stream)
|
|
29
|
-
throw new Error('Stream not found');
|
|
30
|
-
return stream;
|
|
31
|
-
}
|
|
32
|
-
add(source, streamId, metadata) {
|
|
33
|
-
const stream = new ProtocolClientBlobStream(source, streamId, metadata);
|
|
34
|
-
this.#collection.set(streamId, stream);
|
|
35
|
-
return stream;
|
|
36
|
-
}
|
|
37
|
-
remove(streamId) {
|
|
38
|
-
this.#collection.delete(streamId);
|
|
39
|
-
}
|
|
40
|
-
abort(streamId, error) {
|
|
41
|
-
const stream = this.get(streamId);
|
|
42
|
-
stream.abort(error);
|
|
43
|
-
this.remove(streamId);
|
|
44
|
-
}
|
|
45
|
-
pull(streamId, size) {
|
|
46
|
-
const stream = this.get(streamId);
|
|
47
|
-
return stream.read(size);
|
|
48
|
-
}
|
|
49
|
-
end(streamId) {
|
|
50
|
-
this.get(streamId).end();
|
|
51
|
-
this.remove(streamId);
|
|
52
|
-
}
|
|
53
|
-
clear(error) {
|
|
54
|
-
if (error) {
|
|
55
|
-
for (const stream of this.#collection.values()) {
|
|
56
|
-
stream.abort(error);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
this.#collection.clear();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export class ProtocolServerStreams {
|
|
63
|
-
#collection = new Map();
|
|
64
|
-
has(streamId) {
|
|
65
|
-
return this.#collection.has(streamId);
|
|
66
|
-
}
|
|
67
|
-
get(streamId) {
|
|
68
|
-
const stream = this.#collection.get(streamId);
|
|
69
|
-
if (!stream)
|
|
70
|
-
throw new Error('Stream not found');
|
|
71
|
-
return stream;
|
|
72
|
-
}
|
|
73
|
-
add(streamId, stream) {
|
|
74
|
-
this.#collection.set(streamId, stream);
|
|
75
|
-
return stream;
|
|
76
|
-
}
|
|
77
|
-
remove(streamId) {
|
|
78
|
-
this.#collection.delete(streamId);
|
|
79
|
-
}
|
|
80
|
-
abort(streamId) {
|
|
81
|
-
if (this.has(streamId)) {
|
|
82
|
-
const stream = this.get(streamId);
|
|
83
|
-
stream.abort();
|
|
84
|
-
this.remove(streamId);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
async push(streamId, chunk) {
|
|
88
|
-
const stream = this.get(streamId);
|
|
89
|
-
return await stream.push(chunk);
|
|
90
|
-
}
|
|
91
|
-
end(streamId) {
|
|
92
|
-
const stream = this.get(streamId);
|
|
93
|
-
stream.end();
|
|
94
|
-
this.remove(streamId);
|
|
95
|
-
}
|
|
96
|
-
clear(error) {
|
|
97
|
-
if (error) {
|
|
98
|
-
for (const stream of this.#collection.values()) {
|
|
99
|
-
stream.abort(error);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
this.#collection.clear();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
export var ProtocolTransportStatus;
|
|
106
|
-
(function (ProtocolTransportStatus) {
|
|
107
|
-
ProtocolTransportStatus["CONNECTED"] = "CONNECTED";
|
|
108
|
-
ProtocolTransportStatus["DISCONNECTED"] = "DISCONNECTED";
|
|
109
|
-
ProtocolTransportStatus["CONNECTING"] = "CONNECTING";
|
|
110
|
-
})(ProtocolTransportStatus || (ProtocolTransportStatus = {}));
|
|
111
|
-
export class ProtocolTransport extends EventEmitter {
|
|
112
|
-
options;
|
|
113
|
-
status = ProtocolTransportStatus.DISCONNECTED;
|
|
114
|
-
constructor(options) {
|
|
115
|
-
super();
|
|
116
|
-
this.options = options;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
export class ProtocolBaseTransformer {
|
|
120
|
-
encodeRPC(_procedure, payload) {
|
|
121
|
-
return payload;
|
|
122
|
-
}
|
|
123
|
-
decodeRPC(_procedure, payload) {
|
|
124
|
-
return payload;
|
|
125
|
-
}
|
|
126
|
-
decodeRPCChunk(_procedure, payload) {
|
|
127
|
-
return payload;
|
|
128
|
-
}
|
|
129
|
-
decodeEvent(_event, payload) {
|
|
130
|
-
return payload;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
export class BaseProtocol extends EventEmitter {
|
|
134
|
-
format;
|
|
135
|
-
clientStreams = new ProtocolClientStreams();
|
|
136
|
-
serverStreams = new ProtocolServerStreams();
|
|
137
|
-
rpcStreams = new ProtocolServerStreams();
|
|
138
|
-
calls = new Map();
|
|
139
|
-
callId = 0;
|
|
140
|
-
streamId = 0;
|
|
141
|
-
constructor(format) {
|
|
142
|
-
super();
|
|
143
|
-
this.format = format;
|
|
144
|
-
}
|
|
145
|
-
get contentType() {
|
|
146
|
-
return this.format.contentType;
|
|
147
|
-
}
|
|
148
|
-
handleCallResponse(callId, call, response, transformer) {
|
|
149
|
-
if (response.error) {
|
|
150
|
-
call.reject(new ProtocolError(response.error.code, response.error.message, response.error.data));
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
try {
|
|
154
|
-
const transformed = transformer.decodeRPC(call.procedure, response.result);
|
|
155
|
-
if (response.stream)
|
|
156
|
-
call.resolve({ result: transformed, stream: response.stream });
|
|
157
|
-
else
|
|
158
|
-
call.resolve(transformed);
|
|
159
|
-
}
|
|
160
|
-
catch (error) {
|
|
161
|
-
call.reject(new ProtocolError(ErrorCode.ClientRequestError, 'Unable to decode response', error));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
this.calls.delete(callId);
|
|
165
|
-
}
|
|
166
|
-
handleRpcResponse({ callId, error, result, streams }, transformer, stream) {
|
|
167
|
-
const call = this.calls.get(callId);
|
|
168
|
-
if (!call)
|
|
169
|
-
throw new Error('Call not found');
|
|
170
|
-
for (const key in streams) {
|
|
171
|
-
const stream = streams[key];
|
|
172
|
-
this.serverStreams.add(stream.id, stream);
|
|
173
|
-
}
|
|
174
|
-
this.handleCallResponse(callId, call, error ? { error } : { result, stream }, transformer);
|
|
175
|
-
return call;
|
|
176
|
-
}
|
|
177
|
-
handleRpcStreamResponse(response, stream, transformer) {
|
|
178
|
-
const call = this.handleRpcResponse(response, transformer, stream);
|
|
179
|
-
this.rpcStreams.add(response.callId, stream);
|
|
180
|
-
return call;
|
|
181
|
-
}
|
|
182
|
-
createCall(procedure, options) {
|
|
183
|
-
const timeoutSignal = AbortSignal.timeout(options.timeout);
|
|
184
|
-
const signal = options.signal
|
|
185
|
-
? AbortSignal.any([options.signal, timeoutSignal])
|
|
186
|
-
: timeoutSignal;
|
|
187
|
-
const call = Object.assign(createPromise(), { procedure, signal });
|
|
188
|
-
timeoutSignal.addEventListener('abort', () => {
|
|
189
|
-
const error = new ProtocolError(ErrorCode.RequestTimeout, 'Request timeout');
|
|
190
|
-
call.reject(error);
|
|
191
|
-
}, { once: true });
|
|
192
|
-
return call;
|
|
193
|
-
}
|
|
194
|
-
createRpc(procedure, payload, options, transformer) {
|
|
195
|
-
const callId = ++this.callId;
|
|
196
|
-
const call = this.createCall(procedure, options);
|
|
197
|
-
const { buffer, streams } = this.format.encodeRPC({ callId, procedure, payload: transformer.encodeRPC(procedure, payload) }, {
|
|
198
|
-
addStream: (blob) => {
|
|
199
|
-
const streamId = ++this.streamId;
|
|
200
|
-
return this.clientStreams.add(blob.source, streamId, blob.metadata);
|
|
201
|
-
},
|
|
202
|
-
getStream: (id) => {
|
|
203
|
-
const stream = this.clientStreams.get(id);
|
|
204
|
-
return stream;
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
this.calls.set(callId, call);
|
|
208
|
-
return { callId, call, streams, buffer };
|
|
209
|
-
}
|
|
210
|
-
pushRpcStream(callId, chunk) {
|
|
211
|
-
this.rpcStreams.push(callId, chunk);
|
|
212
|
-
}
|
|
213
|
-
endRpcStream(callId) {
|
|
214
|
-
this.rpcStreams.end(callId);
|
|
215
|
-
}
|
|
216
|
-
abortRpcStream(callId) {
|
|
217
|
-
this.rpcStreams.abort(callId);
|
|
218
|
-
}
|
|
219
|
-
removeClientStream(streamId) {
|
|
220
|
-
this.clientStreams.remove(streamId);
|
|
221
|
-
}
|
|
222
|
-
pullClientStream(streamId, size) {
|
|
223
|
-
return this.clientStreams.pull(streamId, size);
|
|
224
|
-
}
|
|
225
|
-
endClientStream(streamId) {
|
|
226
|
-
this.clientStreams.end(streamId);
|
|
227
|
-
}
|
|
228
|
-
abortClientStream(streamId, error) {
|
|
229
|
-
this.clientStreams.abort(streamId, error);
|
|
230
|
-
}
|
|
231
|
-
addServerStream(stream) {
|
|
232
|
-
this.serverStreams.add(stream.id, stream);
|
|
233
|
-
}
|
|
234
|
-
removeServerStream(streamId) {
|
|
235
|
-
this.serverStreams.remove(streamId);
|
|
236
|
-
}
|
|
237
|
-
pushServerStream(streamId, chunk) {
|
|
238
|
-
return this.serverStreams.push(streamId, chunk);
|
|
239
|
-
}
|
|
240
|
-
endServerStream(streamId) {
|
|
241
|
-
this.serverStreams.end(streamId);
|
|
242
|
-
}
|
|
243
|
-
abortServerStream(streamId, _error) {
|
|
244
|
-
this.serverStreams.abort(streamId);
|
|
245
|
-
}
|
|
246
|
-
emitEvent(event, payload, transformer) {
|
|
247
|
-
const transformed = transformer.decodeEvent(event, payload);
|
|
248
|
-
this.emit(event,
|
|
249
|
-
//@ts-expect-error
|
|
250
|
-
transformed);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
export class Protocol extends BaseProtocol {
|
|
254
|
-
handleServerMessage(buffer, transport, transformer) {
|
|
255
|
-
const type = decodeNumber(buffer, 'Uint8');
|
|
256
|
-
const messageBuffer = buffer.slice(Uint8Array.BYTES_PER_ELEMENT);
|
|
257
|
-
if (type in ServerMessageType) {
|
|
258
|
-
const messageType = type;
|
|
259
|
-
if (typeof ServerMessageType[messageType] !== 'undefined') {
|
|
260
|
-
this[messageType](messageBuffer, transport, transformer);
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
throw new Error(`Unknown message type: ${messageType}`);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
[ServerMessageType.Event](buffer, _transport, transformer) {
|
|
268
|
-
const [event, payload] = this.format.decode(buffer);
|
|
269
|
-
this.emitEvent(event, payload, transformer);
|
|
270
|
-
}
|
|
271
|
-
[ServerMessageType.RpcResponse](buffer, transport, transformer) {
|
|
272
|
-
const response = this.format.decodeRPC(buffer, {
|
|
273
|
-
addStream: (id, callId, metadata) => {
|
|
274
|
-
return new ProtocolServerBlobStream(id, metadata, {
|
|
275
|
-
start: () => {
|
|
276
|
-
transport.send(ClientMessageType.ServerStreamPull, encodeNumber(id, 'Uint32'), { callId, streamId: id });
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
},
|
|
280
|
-
getStream: (id) => {
|
|
281
|
-
return this.serverStreams.get(id);
|
|
282
|
-
},
|
|
283
|
-
});
|
|
284
|
-
this.handleRpcResponse(response, transformer);
|
|
285
|
-
}
|
|
286
|
-
[ServerMessageType.RpcStreamResponse](buffer, transport, transformer) {
|
|
287
|
-
const response = this.format.decodeRPC(buffer, {
|
|
288
|
-
addStream: (id, callId, metadata) => {
|
|
289
|
-
return new ProtocolServerBlobStream(id, metadata, {
|
|
290
|
-
start: () => {
|
|
291
|
-
transport.send(ClientMessageType.ServerStreamPull, encodeNumber(id, 'Uint32'), { callId, streamId: id });
|
|
292
|
-
},
|
|
293
|
-
});
|
|
294
|
-
},
|
|
295
|
-
getStream: (id) => {
|
|
296
|
-
return this.serverStreams.get(id);
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
const stream = new ProtocolServerStream({
|
|
300
|
-
transform: (chunk, controller) => {
|
|
301
|
-
const transformed = transformer.decodeRPCChunk(call.procedure, chunk);
|
|
302
|
-
controller.enqueue(transformed);
|
|
303
|
-
},
|
|
304
|
-
});
|
|
305
|
-
const call = this.handleRpcStreamResponse(response, stream, transformer);
|
|
306
|
-
}
|
|
307
|
-
[ServerMessageType.RpcStreamChunk](buffer, _transport, _transformer) {
|
|
308
|
-
const callId = decodeNumber(buffer, 'Uint32');
|
|
309
|
-
const chunk = buffer.slice(Uint32Array.BYTES_PER_ELEMENT);
|
|
310
|
-
const payload = this.format.decode(chunk);
|
|
311
|
-
this.pushRpcStream(callId, payload);
|
|
312
|
-
}
|
|
313
|
-
[ServerMessageType.RpcStreamEnd](buffer, _transport, _transformer) {
|
|
314
|
-
const callId = decodeNumber(buffer, 'Uint32');
|
|
315
|
-
this.endRpcStream(callId);
|
|
316
|
-
}
|
|
317
|
-
[ServerMessageType.RpcStreamAbort](buffer, _transport, _transformer) {
|
|
318
|
-
const callId = decodeNumber(buffer, 'Uint32');
|
|
319
|
-
this.abortRpcStream(callId);
|
|
320
|
-
}
|
|
321
|
-
[ServerMessageType.ServerStreamPush](buffer, transport, _transformer) {
|
|
322
|
-
const streamId = decodeNumber(buffer, 'Uint32');
|
|
323
|
-
const chunk = buffer.slice(Uint32Array.BYTES_PER_ELEMENT);
|
|
324
|
-
this.pushServerStream(streamId, chunk);
|
|
325
|
-
transport.send(ClientMessageType.ServerStreamPull, encodeNumber(streamId, 'Uint32'), { streamId });
|
|
326
|
-
}
|
|
327
|
-
[ServerMessageType.ServerStreamEnd](buffer, _transport, _transformer) {
|
|
328
|
-
const streamId = decodeNumber(buffer, 'Uint32');
|
|
329
|
-
this.endServerStream(streamId);
|
|
330
|
-
}
|
|
331
|
-
[ServerMessageType.ServerStreamAbort](buffer, _transport, _transformer) {
|
|
332
|
-
const streamId = decodeNumber(buffer, 'Uint32');
|
|
333
|
-
this.abortServerStream(streamId);
|
|
334
|
-
}
|
|
335
|
-
[ServerMessageType.ClientStreamPull](buffer, transport, _transformer) {
|
|
336
|
-
const streamId = decodeNumber(buffer, 'Uint32');
|
|
337
|
-
const size = decodeNumber(buffer, 'Uint32', Uint32Array.BYTES_PER_ELEMENT);
|
|
338
|
-
this.pullClientStream(streamId, size).then((chunk) => {
|
|
339
|
-
if (chunk) {
|
|
340
|
-
transport.send(ClientMessageType.ClientStreamPush, concat(encodeNumber(streamId, 'Uint32'), chunk), { streamId });
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
transport.send(ClientMessageType.ClientStreamEnd, encodeNumber(streamId, 'Uint32'), { streamId });
|
|
344
|
-
this.endClientStream(streamId);
|
|
345
|
-
}
|
|
346
|
-
}, () => {
|
|
347
|
-
transport.send(ClientMessageType.ClientStreamAbort, encodeNumber(streamId, 'Uint32'), { streamId });
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
[ServerMessageType.ClientStreamAbort](buffer, _transport, _transformer) {
|
|
351
|
-
const streamId = decodeNumber(buffer, 'Uint32');
|
|
352
|
-
this.abortClientStream(streamId);
|
|
353
|
-
}
|
|
354
|
-
}
|
package/dist/client/stream.js
CHANGED
|
@@ -1,38 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { concat, encodeText } from "../common/binary.js";
|
|
3
|
-
import {
|
|
4
|
-
export class ProtocolClientBlobStream extends
|
|
1
|
+
import { DuplexStream } from '@nmtjs/common';
|
|
2
|
+
import { concat, decodeText, encodeText } from "../common/binary.js";
|
|
3
|
+
import { kBlobKey } from "../common/constants.js";
|
|
4
|
+
export class ProtocolClientBlobStream extends DuplexStream {
|
|
5
5
|
source;
|
|
6
6
|
id;
|
|
7
7
|
metadata;
|
|
8
|
-
[
|
|
8
|
+
[kBlobKey] = true;
|
|
9
9
|
#queue;
|
|
10
10
|
#reader;
|
|
11
|
+
#sourceReader = null;
|
|
11
12
|
constructor(source, id, metadata) {
|
|
13
|
+
let sourceReader = null;
|
|
12
14
|
super({
|
|
13
15
|
start: () => {
|
|
14
|
-
|
|
16
|
+
sourceReader = source.getReader();
|
|
15
17
|
},
|
|
16
|
-
|
|
18
|
+
pull: async (controller) => {
|
|
19
|
+
const { done, value } = await sourceReader.read();
|
|
20
|
+
if (done) {
|
|
21
|
+
controller.close();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const chunk = value;
|
|
25
|
+
controller.enqueue(chunk instanceof Uint8Array
|
|
26
|
+
? chunk
|
|
27
|
+
: new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
28
|
+
},
|
|
29
|
+
transform: (chunk) => {
|
|
17
30
|
if (chunk instanceof ArrayBuffer) {
|
|
18
|
-
|
|
31
|
+
return new Uint8Array(chunk);
|
|
19
32
|
}
|
|
20
33
|
else if (chunk instanceof Uint8Array) {
|
|
21
|
-
|
|
34
|
+
return chunk;
|
|
22
35
|
}
|
|
23
36
|
else if (typeof chunk === 'string') {
|
|
24
|
-
|
|
37
|
+
return encodeText(chunk);
|
|
25
38
|
}
|
|
26
39
|
else {
|
|
27
40
|
throw new Error('Invalid chunk data type. Expected ArrayBuffer, Uint8Array, or string.');
|
|
28
41
|
}
|
|
29
42
|
},
|
|
43
|
+
cancel: (reason) => {
|
|
44
|
+
// Use reader.cancel() if reader exists (stream is locked), otherwise source.cancel()
|
|
45
|
+
if (sourceReader) {
|
|
46
|
+
sourceReader.cancel(reason);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
source.cancel(reason);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
30
52
|
});
|
|
31
53
|
this.source = source;
|
|
32
54
|
this.id = id;
|
|
33
55
|
this.metadata = metadata;
|
|
34
|
-
this.#queue = new
|
|
56
|
+
this.#queue = new Uint8Array(0);
|
|
35
57
|
this.#reader = this.readable.getReader();
|
|
58
|
+
this.#sourceReader = sourceReader;
|
|
59
|
+
}
|
|
60
|
+
async abort(reason = 'Stream aborted') {
|
|
61
|
+
await this.#reader.cancel(reason);
|
|
62
|
+
this.#reader.releaseLock();
|
|
63
|
+
this.#sourceReader?.releaseLock();
|
|
64
|
+
}
|
|
65
|
+
async end() {
|
|
66
|
+
// Release the reader lock when the stream is finished
|
|
67
|
+
this.#reader.releaseLock();
|
|
68
|
+
this.#sourceReader?.releaseLock();
|
|
36
69
|
}
|
|
37
70
|
async read(size) {
|
|
38
71
|
while (this.#queue.byteLength < size) {
|
|
@@ -40,7 +73,7 @@ export class ProtocolClientBlobStream extends TransformStream {
|
|
|
40
73
|
if (done) {
|
|
41
74
|
if (this.#queue.byteLength > 0) {
|
|
42
75
|
const chunk = this.#queue;
|
|
43
|
-
this.#queue = new
|
|
76
|
+
this.#queue = new Uint8Array(0);
|
|
44
77
|
return chunk;
|
|
45
78
|
}
|
|
46
79
|
return null;
|
|
@@ -48,51 +81,109 @@ export class ProtocolClientBlobStream extends TransformStream {
|
|
|
48
81
|
const buffer = value;
|
|
49
82
|
this.#queue = concat(this.#queue, buffer);
|
|
50
83
|
}
|
|
51
|
-
const chunk = this.#queue.
|
|
52
|
-
this.#queue = this.#queue.
|
|
84
|
+
const chunk = this.#queue.subarray(0, size);
|
|
85
|
+
this.#queue = this.#queue.subarray(size);
|
|
53
86
|
return chunk;
|
|
54
87
|
}
|
|
55
|
-
abort(error = new Error('Stream aborted')) {
|
|
56
|
-
this.#reader.cancel(error);
|
|
57
|
-
this.source.cancel(error);
|
|
58
|
-
}
|
|
59
|
-
end() {
|
|
60
|
-
return this.source.cancel('Stream ended');
|
|
61
|
-
}
|
|
62
88
|
}
|
|
63
|
-
export class
|
|
64
|
-
#writer;
|
|
65
|
-
constructor(options) {
|
|
66
|
-
super(options);
|
|
67
|
-
this.#writer = this.writable.getWriter();
|
|
68
|
-
}
|
|
89
|
+
export class ProtocolServerStreamInterface extends DuplexStream {
|
|
69
90
|
async *[Symbol.asyncIterator]() {
|
|
70
91
|
const reader = this.readable.getReader();
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
92
|
+
try {
|
|
93
|
+
while (true) {
|
|
94
|
+
const { done, value } = await reader.read();
|
|
95
|
+
if (!done)
|
|
96
|
+
yield value;
|
|
97
|
+
else
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
reader.releaseLock();
|
|
77
103
|
}
|
|
78
104
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
105
|
+
}
|
|
106
|
+
export class ProtocolServerStream extends ProtocolServerStreamInterface {
|
|
107
|
+
}
|
|
108
|
+
export class ProtocolServerRPCStream extends ProtocolServerStream {
|
|
109
|
+
createAsyncIterable(onDone) {
|
|
110
|
+
return {
|
|
111
|
+
[Symbol.asyncIterator]: () => {
|
|
112
|
+
const iterator = this[Symbol.asyncIterator]();
|
|
113
|
+
return {
|
|
114
|
+
async next() {
|
|
115
|
+
const result = await iterator.next();
|
|
116
|
+
if (result.done)
|
|
117
|
+
onDone();
|
|
118
|
+
return result;
|
|
119
|
+
},
|
|
120
|
+
async return(value) {
|
|
121
|
+
onDone();
|
|
122
|
+
return iterator.return?.(value) ?? { done: true, value };
|
|
123
|
+
},
|
|
124
|
+
async throw(error) {
|
|
125
|
+
onDone();
|
|
126
|
+
return iterator.throw?.(error) ?? Promise.reject(error);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
};
|
|
87
131
|
}
|
|
88
132
|
}
|
|
89
|
-
export class ProtocolServerBlobStream extends
|
|
90
|
-
id;
|
|
133
|
+
export class ProtocolServerBlobStream extends ProtocolServerStreamInterface {
|
|
91
134
|
metadata;
|
|
92
|
-
[
|
|
93
|
-
constructor(
|
|
135
|
+
[kBlobKey] = true;
|
|
136
|
+
constructor(metadata, options) {
|
|
94
137
|
super(options);
|
|
95
|
-
this.id = id;
|
|
96
138
|
this.metadata = metadata;
|
|
97
139
|
}
|
|
140
|
+
get size() {
|
|
141
|
+
return this.metadata.size || Number.NaN;
|
|
142
|
+
}
|
|
143
|
+
get type() {
|
|
144
|
+
return this.metadata.type || 'application/octet-stream';
|
|
145
|
+
}
|
|
146
|
+
async text() {
|
|
147
|
+
const chunks = [];
|
|
148
|
+
for await (const chunk of this)
|
|
149
|
+
chunks.push(chunk);
|
|
150
|
+
return decodeText(concat(...chunks));
|
|
151
|
+
}
|
|
152
|
+
async bytes() {
|
|
153
|
+
const chunks = [];
|
|
154
|
+
for await (const chunk of this)
|
|
155
|
+
chunks.push(chunk);
|
|
156
|
+
return concat(...chunks);
|
|
157
|
+
}
|
|
158
|
+
async arrayBuffer() {
|
|
159
|
+
const bytes = await this.bytes();
|
|
160
|
+
return bytes.buffer;
|
|
161
|
+
}
|
|
162
|
+
async json() {
|
|
163
|
+
const text = await this.text();
|
|
164
|
+
return JSON.parse(text);
|
|
165
|
+
}
|
|
166
|
+
stream() {
|
|
167
|
+
const transform = new TransformStream({
|
|
168
|
+
transform: (chunk, controller) => {
|
|
169
|
+
controller.enqueue(chunk instanceof Uint8Array
|
|
170
|
+
? chunk
|
|
171
|
+
: new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
this.readable.pipeThrough(transform);
|
|
175
|
+
return transform.readable;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Throws an error
|
|
179
|
+
*/
|
|
180
|
+
async formData() {
|
|
181
|
+
throw new Error('Method not implemented.');
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Throws an error
|
|
185
|
+
*/
|
|
186
|
+
slice() {
|
|
187
|
+
throw new Error('Unable to slice');
|
|
188
|
+
}
|
|
98
189
|
}
|