@standardserver/peer 0.0.22 → 0.0.24
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/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +55 -51
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -257,7 +257,7 @@ declare class EventStreamTransmitter {
|
|
|
257
257
|
private readonly iterator;
|
|
258
258
|
private readonly messageId;
|
|
259
259
|
private readonly send;
|
|
260
|
-
private
|
|
260
|
+
private isDone;
|
|
261
261
|
constructor(iterator: AsyncIterator<unknown>, messageId: string, send: (message: PeerEventStreamMessage) => Promise<void>);
|
|
262
262
|
cancel(): Promise<void>;
|
|
263
263
|
transmit(): Promise<void>;
|
|
@@ -285,6 +285,7 @@ declare class ServerPeer {
|
|
|
285
285
|
message(message: ClientPeerSendMessage, handleRequest: (request: StandardLazyRequest) => Promise<StandardResponse>): Promise<void>;
|
|
286
286
|
close(reason?: unknown): Promise<void>;
|
|
287
287
|
private closeById;
|
|
288
|
+
private cancelById;
|
|
288
289
|
}
|
|
289
290
|
|
|
290
291
|
declare function isPeerMessage(maybe: unknown): maybe is PeerMessage;
|
package/dist/index.d.ts
CHANGED
|
@@ -257,7 +257,7 @@ declare class EventStreamTransmitter {
|
|
|
257
257
|
private readonly iterator;
|
|
258
258
|
private readonly messageId;
|
|
259
259
|
private readonly send;
|
|
260
|
-
private
|
|
260
|
+
private isDone;
|
|
261
261
|
constructor(iterator: AsyncIterator<unknown>, messageId: string, send: (message: PeerEventStreamMessage) => Promise<void>);
|
|
262
262
|
cancel(): Promise<void>;
|
|
263
263
|
transmit(): Promise<void>;
|
|
@@ -285,6 +285,7 @@ declare class ServerPeer {
|
|
|
285
285
|
message(message: ClientPeerSendMessage, handleRequest: (request: StandardLazyRequest) => Promise<StandardResponse>): Promise<void>;
|
|
286
286
|
close(reason?: unknown): Promise<void>;
|
|
287
287
|
private closeById;
|
|
288
|
+
private cancelById;
|
|
288
289
|
}
|
|
289
290
|
|
|
290
291
|
declare function isPeerMessage(maybe: unknown): maybe is PeerMessage;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, omit,
|
|
1
|
+
import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, omit, AbortError, stringifyJSON } from '@standardserver/shared';
|
|
2
2
|
import { withEventMeta, ErrorEvent, unwrapEvent, generateContentDisposition, flattenStandardHeader, getFilenameFromContentDisposition, isStandardRequest, isStandardResponse } from '@standardserver/core';
|
|
3
3
|
|
|
4
4
|
function toEventIterator(queue, cleanup) {
|
|
@@ -36,10 +36,10 @@ class EventStreamTransmitter {
|
|
|
36
36
|
this.messageId = messageId;
|
|
37
37
|
this.send = send;
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
isDone = false;
|
|
40
40
|
async cancel() {
|
|
41
|
-
if (!this.
|
|
42
|
-
this.
|
|
41
|
+
if (!this.isDone) {
|
|
42
|
+
this.isDone = true;
|
|
43
43
|
await this.iterator.return?.();
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -47,43 +47,41 @@ class EventStreamTransmitter {
|
|
|
47
47
|
while (true) {
|
|
48
48
|
try {
|
|
49
49
|
const item = await this.iterator.next();
|
|
50
|
-
if (this.
|
|
50
|
+
if (this.isDone) {
|
|
51
51
|
return;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
if (item.done) {
|
|
54
|
+
this.isDone = true;
|
|
55
|
+
}
|
|
54
56
|
try {
|
|
57
|
+
const [data, meta] = unwrapEvent(item.value);
|
|
55
58
|
await this.send({
|
|
56
59
|
kind: "event-stream",
|
|
57
60
|
id: this.messageId,
|
|
58
61
|
json: { ...meta, event: item.done ? "close" : "message", data }
|
|
59
62
|
});
|
|
60
63
|
} catch (error) {
|
|
61
|
-
|
|
62
|
-
await this.cancel();
|
|
63
|
-
}
|
|
64
|
+
await this.cancel();
|
|
64
65
|
throw error;
|
|
65
66
|
}
|
|
66
|
-
if (
|
|
67
|
-
this.isCompleted = true;
|
|
67
|
+
if (this.isDone) {
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
70
|
} catch (error) {
|
|
71
|
-
if (this.isCompleted) {
|
|
72
|
-
throw error;
|
|
73
|
-
}
|
|
74
71
|
if (error instanceof ErrorEvent) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
72
|
+
if (!this.isDone) {
|
|
73
|
+
this.isDone = true;
|
|
74
|
+
const [resolvedError, meta] = unwrapEvent(error);
|
|
75
|
+
await this.send({
|
|
76
|
+
kind: "event-stream",
|
|
77
|
+
id: this.messageId,
|
|
78
|
+
json: { ...meta, event: "error", data: resolvedError.data }
|
|
79
|
+
});
|
|
80
|
+
}
|
|
82
81
|
return;
|
|
83
|
-
} else {
|
|
84
|
-
this.isCompleted = true;
|
|
85
|
-
throw error;
|
|
86
82
|
}
|
|
83
|
+
this.isDone = true;
|
|
84
|
+
throw error;
|
|
87
85
|
}
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -117,40 +115,40 @@ class OctetStreamTransmitter {
|
|
|
117
115
|
this.send = send;
|
|
118
116
|
this.reader = stream.getReader();
|
|
119
117
|
}
|
|
120
|
-
|
|
118
|
+
isDone = false;
|
|
121
119
|
reader;
|
|
122
120
|
async cancel() {
|
|
123
|
-
if (!this.
|
|
124
|
-
this.
|
|
121
|
+
if (!this.isDone) {
|
|
122
|
+
this.isDone = true;
|
|
125
123
|
await this.reader.cancel();
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
126
|
async transmit() {
|
|
129
127
|
while (true) {
|
|
130
128
|
try {
|
|
131
|
-
const
|
|
132
|
-
if (this.
|
|
129
|
+
const item = await this.reader.read();
|
|
130
|
+
if (this.isDone) {
|
|
133
131
|
return;
|
|
134
132
|
}
|
|
133
|
+
if (item.done) {
|
|
134
|
+
this.isDone = true;
|
|
135
|
+
}
|
|
135
136
|
try {
|
|
136
137
|
await this.send({
|
|
137
|
-
json: { close: done },
|
|
138
|
-
binary: value,
|
|
138
|
+
json: { close: item.done },
|
|
139
|
+
binary: item.value,
|
|
139
140
|
kind: "octet-stream",
|
|
140
141
|
id: this.messageId
|
|
141
142
|
});
|
|
142
143
|
} catch (err) {
|
|
143
|
-
|
|
144
|
-
await this.cancel();
|
|
145
|
-
}
|
|
144
|
+
await this.cancel();
|
|
146
145
|
throw err;
|
|
147
146
|
}
|
|
148
|
-
if (
|
|
149
|
-
this.isCompleted = true;
|
|
147
|
+
if (this.isDone) {
|
|
150
148
|
return;
|
|
151
149
|
}
|
|
152
150
|
} catch (error) {
|
|
153
|
-
this.
|
|
151
|
+
this.isDone = true;
|
|
154
152
|
throw error;
|
|
155
153
|
}
|
|
156
154
|
}
|
|
@@ -279,8 +277,6 @@ class ClientPeer {
|
|
|
279
277
|
void transmitter.transmit().catch(async (error) => {
|
|
280
278
|
if (state.eventStreamTransmitter) {
|
|
281
279
|
await this.abortById(id, error);
|
|
282
|
-
} else {
|
|
283
|
-
emitUnhandledRejection(error);
|
|
284
280
|
}
|
|
285
281
|
});
|
|
286
282
|
} else if (request.body instanceof ReadableStream) {
|
|
@@ -289,10 +285,7 @@ class ClientPeer {
|
|
|
289
285
|
void transmitter.transmit().catch(async (error) => {
|
|
290
286
|
if (state.octetStreamTransmitter) {
|
|
291
287
|
await this.abortById(id, error);
|
|
292
|
-
} else {
|
|
293
|
-
emitUnhandledRejection(error);
|
|
294
288
|
}
|
|
295
|
-
/* v8 ignore stop -- @preserve */
|
|
296
289
|
});
|
|
297
290
|
}
|
|
298
291
|
} catch (reason) {
|
|
@@ -619,23 +612,25 @@ class ServerPeer {
|
|
|
619
612
|
} else {
|
|
620
613
|
const transmitter = new EventStreamTransmitter(response.body, message.id, this.send);
|
|
621
614
|
state.eventStreamTransmitter = transmitter;
|
|
622
|
-
await transmitter.transmit()
|
|
615
|
+
await transmitter.transmit().catch(async (reason) => {
|
|
616
|
+
if (state.eventStreamTransmitter) {
|
|
617
|
+
await this.cancelById(message.id, reason);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
623
620
|
}
|
|
624
621
|
} else if (response.body instanceof ReadableStream) {
|
|
625
622
|
const transmitter = new OctetStreamTransmitter(response.body, message.id, this.send);
|
|
626
623
|
state.octetStreamTransmitter = transmitter;
|
|
627
|
-
await transmitter.transmit()
|
|
624
|
+
await transmitter.transmit().catch(async (reason) => {
|
|
625
|
+
if (state.octetStreamTransmitter) {
|
|
626
|
+
await this.cancelById(message.id, reason);
|
|
627
|
+
}
|
|
628
|
+
});
|
|
628
629
|
}
|
|
629
630
|
state.controller = void 0;
|
|
630
631
|
await this.closeById(id);
|
|
631
632
|
} catch (reason) {
|
|
632
|
-
await
|
|
633
|
-
/**
|
|
634
|
-
* Do not need to send cancel message if request was closed or aborted
|
|
635
|
-
*/
|
|
636
|
-
this.requests.has(message.id) ? this.send({ id: message.id, kind: "cancel" }) : void 0,
|
|
637
|
-
this.closeById(id, reason)
|
|
638
|
-
]);
|
|
633
|
+
await this.cancelById(message.id, reason);
|
|
639
634
|
throw reason;
|
|
640
635
|
}
|
|
641
636
|
}
|
|
@@ -666,6 +661,15 @@ class ServerPeer {
|
|
|
666
661
|
state.octetStreamTransmitter = void 0;
|
|
667
662
|
await Promise.all(promises);
|
|
668
663
|
}
|
|
664
|
+
async cancelById(id, reason) {
|
|
665
|
+
await Promise.all([
|
|
666
|
+
/**
|
|
667
|
+
* Do not need to send cancel message if request was closed or aborted
|
|
668
|
+
*/
|
|
669
|
+
this.requests.has(id) ? this.send({ id, kind: "cancel" }) : void 0,
|
|
670
|
+
this.closeById(id, reason)
|
|
671
|
+
]);
|
|
672
|
+
}
|
|
669
673
|
}
|
|
670
674
|
|
|
671
675
|
export { ClientPeer, EventStreamTransmitter, HibernationEventIterator, ServerPeer, decodePeerMessage, encodePeerMessage, isClientPeerSendMessage, isPeerCancelMessage, isPeerEventStreamMessage, isPeerMessage, isPeerOctetStreamMessage, isPeerRequestMessage, isPeerResponseMessage, isPeerStreamCancelMessage, isServerPeerSendMessage, toEventIterator };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardserver/peer",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.24",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://standardserver.dev",
|
|
7
7
|
"repository": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@standardserver/core": "0.0.
|
|
26
|
-
"@standardserver/shared": "0.0.
|
|
25
|
+
"@standardserver/core": "0.0.24",
|
|
26
|
+
"@standardserver/shared": "0.0.24"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "unbuild",
|