@minip2p/core 0.7.3 → 0.7.5
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/README.md +3 -1
- package/dist/backend.d.ts +22 -5
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +2 -0
- package/dist/backend.js.map +1 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -1
- package/dist/event-drain.d.ts +17 -0
- package/dist/event-drain.d.ts.map +1 -0
- package/dist/event-drain.js +75 -0
- package/dist/event-drain.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/native-ids.d.ts +31 -0
- package/dist/native-ids.d.ts.map +1 -0
- package/dist/native-ids.js +62 -0
- package/dist/native-ids.js.map +1 -0
- package/dist/sdk.d.ts +65 -10
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +442 -48
- package/dist/sdk.js.map +1 -1
- package/dist/types.d.ts +33 -22
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +24 -22
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/backend.ts +22 -5
- package/src/errors.ts +21 -0
- package/src/event-drain.ts +87 -0
- package/src/index.ts +1 -0
- package/src/native-ids.ts +66 -0
- package/src/sdk.ts +512 -80
- package/src/types.ts +32 -22
package/dist/sdk.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
/* oxlint-disable default-case, func-style, max-classes-per-file, no-await-in-loop, no-empty-function, no-loop-func, no-use-before-define, promise/avoid-new, promise/prefer-await-to-callbacks, promise/prefer-await-to-then, typescript/no-invalid-void-type, unicorn/consistent-function-scoping, unicorn/no-new-array, unicorn/no-useless-spread, unicorn/no-useless-undefined -- Promise adapters, sequential async iterators, void event payloads, snapshot fan-out, and bounded queues are deliberate SDK internals. */
|
|
2
|
-
import { AbortError, ClosedError, ConnectCancelledError, ConnectFailedError, ConnectResultLostError, ConnectResultUnavailableError, DriverFailedError, EventQueueOverflowError, OpenStreamError, PeerDisconnectedError, StreamClosedError, TimeoutError, } from "./errors.js";
|
|
2
|
+
import { AbortError, ClosedError, ConnectCancelledError, ConnectFailedError, ConnectResultLostError, ConnectResultUnavailableError, DriverFailedError, EventQueueOverflowError, OpenStreamError, PeerDisconnectedError, StreamClosedError, TimeoutError, WriteBufferFullError, } from "./errors.js";
|
|
3
3
|
import { P2pEvent_Tags, PathKind_Tags } from "./types.js";
|
|
4
4
|
const DEFAULT_TIMEOUT_MS = 65_000;
|
|
5
5
|
const EVENT_QUEUE_CAP = 4096;
|
|
6
6
|
const EVENT_FLUSH_BATCH = 256;
|
|
7
7
|
const CONNECT_TERMINAL_CAP = 1024;
|
|
8
|
-
|
|
9
|
-
const
|
|
8
|
+
/** Default per-stream write high-water mark: one Yamux stream send cap. */
|
|
9
|
+
const DEFAULT_WRITE_HIGH_WATER_MARK = 256 * 1024;
|
|
10
|
+
/** Native events routed to one {@link Stream} after it became ready. */
|
|
11
|
+
const STREAM_EVENT_TAGS = [
|
|
12
|
+
P2pEvent_Tags.StreamData,
|
|
13
|
+
P2pEvent_Tags.StreamRemoteWriteClosed,
|
|
14
|
+
P2pEvent_Tags.StreamWriteAccepted,
|
|
15
|
+
P2pEvent_Tags.StreamWriteStopped,
|
|
16
|
+
P2pEvent_Tags.StreamClosed,
|
|
17
|
+
];
|
|
18
|
+
const isStreamEvent = (event) => STREAM_EVENT_TAGS.includes(event.tag);
|
|
10
19
|
class BoundedQueue {
|
|
11
20
|
#items;
|
|
12
21
|
#head = 0;
|
|
@@ -45,6 +54,174 @@ class BoundedQueue {
|
|
|
45
54
|
this.#size = 0;
|
|
46
55
|
}
|
|
47
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Native events that are never dropped, because stream and connection state
|
|
59
|
+
* depends on each one (ADR 0012), plus the driver failure that ends them.
|
|
60
|
+
*/
|
|
61
|
+
const LOSSLESS_TAGS = new Set([
|
|
62
|
+
P2pEvent_Tags.StreamReady,
|
|
63
|
+
...STREAM_EVENT_TAGS,
|
|
64
|
+
P2pEvent_Tags.ConnectionClosed,
|
|
65
|
+
P2pEvent_Tags.ConnectionReplaced,
|
|
66
|
+
P2pEvent_Tags.DriverFailed,
|
|
67
|
+
]);
|
|
68
|
+
/** Returns `next` appended to `last` when both are data for one stream. */
|
|
69
|
+
const coalesceData = (last, next) => {
|
|
70
|
+
if (last.source !== "native" ||
|
|
71
|
+
next.source !== "native" ||
|
|
72
|
+
last.event.tag !== P2pEvent_Tags.StreamData ||
|
|
73
|
+
next.event.tag !== P2pEvent_Tags.StreamData) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const { inner } = last.event;
|
|
77
|
+
const { inner: added } = next.event;
|
|
78
|
+
if (inner.peerId !== added.peerId ||
|
|
79
|
+
inner.connId !== added.connId ||
|
|
80
|
+
inner.streamId !== added.streamId) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const data = new Uint8Array(inner.data.byteLength + added.data.byteLength);
|
|
84
|
+
data.set(new Uint8Array(inner.data));
|
|
85
|
+
data.set(new Uint8Array(added.data), inner.data.byteLength);
|
|
86
|
+
const event = {
|
|
87
|
+
inner: { ...inner, data: data.buffer },
|
|
88
|
+
tag: P2pEvent_Tags.StreamData,
|
|
89
|
+
};
|
|
90
|
+
return { event, source: "native" };
|
|
91
|
+
};
|
|
92
|
+
/** The stream a native per-stream event belongs to. */
|
|
93
|
+
const streamEventKey = (item) => {
|
|
94
|
+
if (item.source !== "native" ||
|
|
95
|
+
(item.event.tag !== P2pEvent_Tags.StreamReady && !isStreamEvent(item.event))) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const { peerId, connId, streamId } = item.event.inner;
|
|
99
|
+
return streamKey(peerId, connId, streamId);
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The endpoint's shared event queue, in delivery order.
|
|
103
|
+
*
|
|
104
|
+
* Stream events and connection-end events (plus `DriverFailed`) never drop,
|
|
105
|
+
* and adjacent data for one stream coalesces into one event, so native
|
|
106
|
+
* receive budgets bound them; they do not count against the capacity. Past `capacity` other items, the oldest
|
|
107
|
+
* gossipsub message is dropped first, then the oldest other item.
|
|
108
|
+
*
|
|
109
|
+
* A peer-opened stream that closes without data before its `StreamReady`
|
|
110
|
+
* is dispatched leaves the queue whole, so a peer opening and closing
|
|
111
|
+
* streams cannot grow it; its events still reach `release`.
|
|
112
|
+
*/
|
|
113
|
+
class EventQueue {
|
|
114
|
+
#items = new Map();
|
|
115
|
+
/** Droppable messages, then other droppable items, oldest first. */
|
|
116
|
+
#messages = new Set();
|
|
117
|
+
#others = new Set();
|
|
118
|
+
/**
|
|
119
|
+
* Peer-opened streams whose `StreamReady` is still queued, with their
|
|
120
|
+
* queued item ids; `undefined` once one carried data.
|
|
121
|
+
*/
|
|
122
|
+
#unseen = new Map();
|
|
123
|
+
#capacity;
|
|
124
|
+
#release;
|
|
125
|
+
#next = 0;
|
|
126
|
+
#newest;
|
|
127
|
+
constructor(capacity, release) {
|
|
128
|
+
this.#capacity = capacity;
|
|
129
|
+
this.#release = release;
|
|
130
|
+
}
|
|
131
|
+
get length() {
|
|
132
|
+
return this.#items.size;
|
|
133
|
+
}
|
|
134
|
+
/** Appends `item`, returning the item dropped to make room, if any. */
|
|
135
|
+
push(item) {
|
|
136
|
+
const newest = this.#newest === undefined ? undefined : this.#items.get(this.#newest);
|
|
137
|
+
const merged = newest === undefined ? undefined : coalesceData(newest, item);
|
|
138
|
+
if (merged !== undefined && this.#newest !== undefined) {
|
|
139
|
+
// A data event retires no adapter IDs, so the absorbed one needs no
|
|
140
|
+
// `eventHandled`.
|
|
141
|
+
this.#items.set(this.#newest, merged);
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const id = this.#next;
|
|
145
|
+
this.#next += 1;
|
|
146
|
+
if (this.#elideUnseen(item, id)) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
this.#items.set(id, item);
|
|
150
|
+
this.#newest = id;
|
|
151
|
+
if (item.source === "native" && item.event.tag === P2pEvent_Tags.Message) {
|
|
152
|
+
this.#messages.add(id);
|
|
153
|
+
}
|
|
154
|
+
else if (item.source !== "native" || !LOSSLESS_TAGS.has(item.event.tag)) {
|
|
155
|
+
this.#others.add(id);
|
|
156
|
+
}
|
|
157
|
+
if (this.#messages.size + this.#others.size <= this.#capacity) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
const [victim] = this.#messages.size > 0 ? this.#messages : this.#others;
|
|
161
|
+
return victim === undefined ? undefined : this.#remove(victim);
|
|
162
|
+
}
|
|
163
|
+
shift() {
|
|
164
|
+
const [oldest] = this.#items.keys();
|
|
165
|
+
if (oldest === undefined) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const item = this.#remove(oldest);
|
|
169
|
+
if (item?.source === "native" &&
|
|
170
|
+
item.event.tag === P2pEvent_Tags.StreamReady) {
|
|
171
|
+
this.#unseen.delete(streamEventKey(item) ?? "");
|
|
172
|
+
}
|
|
173
|
+
return item;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Records `item` (as `id`) against an unseen peer stream. Returns whether
|
|
177
|
+
* it closed one that carried no data, which leaves the queue whole.
|
|
178
|
+
*/
|
|
179
|
+
#elideUnseen(item, id) {
|
|
180
|
+
const key = streamEventKey(item);
|
|
181
|
+
if (key === undefined || item.source !== "native") {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const { event } = item;
|
|
185
|
+
if (event.tag === P2pEvent_Tags.StreamReady) {
|
|
186
|
+
if (!event.inner.initiatedLocally) {
|
|
187
|
+
this.#unseen.set(key, [id]);
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (!this.#unseen.has(key)) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const ids = this.#unseen.get(key);
|
|
195
|
+
if (event.tag === P2pEvent_Tags.StreamData) {
|
|
196
|
+
this.#unseen.set(key, undefined);
|
|
197
|
+
}
|
|
198
|
+
else if (ids !== undefined && event.tag === P2pEvent_Tags.StreamClosed) {
|
|
199
|
+
this.#unseen.delete(key);
|
|
200
|
+
for (const queued of ids) {
|
|
201
|
+
const removed = this.#remove(queued);
|
|
202
|
+
if (removed !== undefined) {
|
|
203
|
+
this.#release(removed);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
this.#release(item);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
ids?.push(id);
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
#remove(id) {
|
|
215
|
+
const item = this.#items.get(id);
|
|
216
|
+
this.#items.delete(id);
|
|
217
|
+
this.#messages.delete(id);
|
|
218
|
+
this.#others.delete(id);
|
|
219
|
+
if (this.#newest === id) {
|
|
220
|
+
this.#newest = undefined;
|
|
221
|
+
}
|
|
222
|
+
return item;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
48
225
|
/** A negotiated custom-protocol stream with exclusive pull or flowing reads. */
|
|
49
226
|
export class Stream {
|
|
50
227
|
/** Remote peer that owns the other end of this stream. */
|
|
@@ -59,6 +236,7 @@ export class Stream {
|
|
|
59
236
|
initiatedLocally;
|
|
60
237
|
#backend;
|
|
61
238
|
#onTerminal;
|
|
239
|
+
#consume;
|
|
62
240
|
#listeners = new Map();
|
|
63
241
|
#fifo = [];
|
|
64
242
|
#reads = [];
|
|
@@ -66,16 +244,40 @@ export class Stream {
|
|
|
66
244
|
#mode;
|
|
67
245
|
#receiveState = { kind: "receiving" };
|
|
68
246
|
#closed = false;
|
|
247
|
+
/** Writes not yet accepted by native, oldest (possibly in flight) first. */
|
|
248
|
+
#writes = [];
|
|
249
|
+
#writeBytes = 0;
|
|
250
|
+
/** Native holds the head write's tail until `StreamWriteAccepted`. */
|
|
251
|
+
#writeInFlight = false;
|
|
252
|
+
/** Why later writes fail: `closeWrite`, a remote stop, or a terminal. */
|
|
253
|
+
#writeEnded;
|
|
254
|
+
#closeWriteQueued = false;
|
|
255
|
+
/**
|
|
256
|
+
* Most bytes the stream buffers before {@link write} rejects with
|
|
257
|
+
* {@link WriteBufferFullError}, counting the write native is still taking.
|
|
258
|
+
* A write into an empty buffer is always admitted, whatever its size.
|
|
259
|
+
*/
|
|
260
|
+
writeHighWaterMark = DEFAULT_WRITE_HIGH_WATER_MARK;
|
|
69
261
|
constructor(backend, meta, onTerminal) {
|
|
70
262
|
this.#backend = backend;
|
|
71
263
|
this.#onTerminal = onTerminal;
|
|
264
|
+
this.#consume = streamConsumer(backend, meta);
|
|
72
265
|
this.peerId = meta.peerId;
|
|
73
266
|
this.protocolId = meta.protocolId;
|
|
74
267
|
this.streamId = meta.streamId;
|
|
75
268
|
this.connId = meta.connId;
|
|
76
269
|
this.initiatedLocally = meta.initiatedLocally;
|
|
77
270
|
}
|
|
78
|
-
/**
|
|
271
|
+
/**
|
|
272
|
+
* Subscribes to stream lifecycle or data events.
|
|
273
|
+
*
|
|
274
|
+
* A chunk counts as consumed once every `data` handler has returned, and
|
|
275
|
+
* only consumed bytes let the sender continue: a stream delivers at most
|
|
276
|
+
* one receive window that has not been consumed. An `async` handler is
|
|
277
|
+
* consumed when it returns its Promise, not when that Promise settles; to
|
|
278
|
+
* hold the sender back while asynchronous work runs, use {@link read}
|
|
279
|
+
* instead.
|
|
280
|
+
*/
|
|
79
281
|
on(type, handler) {
|
|
80
282
|
if (type === "data") {
|
|
81
283
|
if (this.#mode === "pull") {
|
|
@@ -104,6 +306,11 @@ export class Stream {
|
|
|
104
306
|
/**
|
|
105
307
|
* Reads the next buffered chunk, or `undefined` after the remote write side
|
|
106
308
|
* closes. Pull reads and flowing `data` handlers are mutually exclusive.
|
|
309
|
+
*
|
|
310
|
+
* Returning a chunk consumes it, which lets the sender continue: the
|
|
311
|
+
* stream delivers at most one receive window that has not been read, so a
|
|
312
|
+
* reader that stops calling `read()` stalls its sender rather than
|
|
313
|
+
* buffering without bound.
|
|
107
314
|
*/
|
|
108
315
|
read() {
|
|
109
316
|
if (this.#mode === "flowing") {
|
|
@@ -113,48 +320,102 @@ export class Stream {
|
|
|
113
320
|
if (this.#receiveState.kind === "failed") {
|
|
114
321
|
return Promise.reject(this.#receiveState.error);
|
|
115
322
|
}
|
|
116
|
-
const buffered = this.#
|
|
117
|
-
if (buffered !== undefined) {
|
|
323
|
+
const buffered = this.#take();
|
|
324
|
+
if (buffered !== undefined || this.#receiveState.kind === "eof") {
|
|
118
325
|
return Promise.resolve(buffered);
|
|
119
326
|
}
|
|
120
|
-
if (this.#receiveState.kind === "eof") {
|
|
121
|
-
return Promise.resolve(this.#shift());
|
|
122
|
-
}
|
|
123
327
|
return new Promise((resolve, reject) => {
|
|
124
328
|
this.#reads.push({ reject, resolve });
|
|
125
329
|
});
|
|
126
330
|
}
|
|
127
|
-
/**
|
|
331
|
+
/**
|
|
332
|
+
* Writes UTF-8 text or bytes to the stream, resolving once native has
|
|
333
|
+
* accepted every byte. Writes are delivered in call order.
|
|
334
|
+
*
|
|
335
|
+
* Rejects with {@link WriteBufferFullError} when the stream already
|
|
336
|
+
* buffers {@link writeHighWaterMark} bytes (a first write is always
|
|
337
|
+
* admitted, so a payload larger than the mark can still be sent), and with
|
|
338
|
+
* a closed error after `closeWrite()`, a remote stop, or the stream
|
|
339
|
+
* closing.
|
|
340
|
+
*
|
|
341
|
+
* A write queued behind a pending one keeps a reference to `data`, and
|
|
342
|
+
* native reads it only when the write reaches the head of the queue: leave
|
|
343
|
+
* the buffer untouched until the Promise settles.
|
|
344
|
+
*/
|
|
128
345
|
write(data) {
|
|
129
346
|
if (this.#closed) {
|
|
130
|
-
|
|
347
|
+
return Promise.reject(new ClosedError());
|
|
348
|
+
}
|
|
349
|
+
if (this.#writeEnded !== undefined) {
|
|
350
|
+
return Promise.reject(this.#writeEnded);
|
|
131
351
|
}
|
|
132
|
-
|
|
352
|
+
const bytes = toUint8Array(data);
|
|
353
|
+
if (this.#writeBytes > 0 &&
|
|
354
|
+
this.#writeBytes + bytes.byteLength > this.writeHighWaterMark) {
|
|
355
|
+
return Promise.reject(new WriteBufferFullError(this.#writeBytes, this.writeHighWaterMark));
|
|
356
|
+
}
|
|
357
|
+
return new Promise((resolve, reject) => {
|
|
358
|
+
this.#writes.push({ data: bytes, reject, resolve });
|
|
359
|
+
this.#writeBytes += bytes.byteLength;
|
|
360
|
+
this.#pumpWrites();
|
|
361
|
+
});
|
|
133
362
|
}
|
|
134
|
-
/**
|
|
363
|
+
/**
|
|
364
|
+
* Half-closes the local write side once every earlier write has been
|
|
365
|
+
* accepted, keeping reads available. Later writes reject.
|
|
366
|
+
*
|
|
367
|
+
* With no write pending the close goes to native now and throws if native
|
|
368
|
+
* refuses it. Behind pending writes it is queued, and a close native then
|
|
369
|
+
* refuses is reported by the stream's terminal event instead.
|
|
370
|
+
*/
|
|
135
371
|
closeWrite() {
|
|
136
|
-
if (
|
|
372
|
+
if (this.#closed || this.#writeEnded !== undefined) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
this.#writeEnded = new StreamClosedError("The stream write side closed");
|
|
376
|
+
if (this.#writes.length === 0) {
|
|
137
377
|
this.#backend.closeStreamWrite(this.peerId, this.connId, this.streamId);
|
|
378
|
+
return;
|
|
138
379
|
}
|
|
380
|
+
this.#closeWriteQueued = true;
|
|
139
381
|
}
|
|
140
|
-
/**
|
|
382
|
+
/**
|
|
383
|
+
* Abruptly resets the stream and emits `closed`. On a stream that already
|
|
384
|
+
* closed, discards unread bytes instead.
|
|
385
|
+
*/
|
|
141
386
|
reset() {
|
|
142
|
-
if (
|
|
143
|
-
this.#
|
|
144
|
-
|
|
387
|
+
if (this.#closed) {
|
|
388
|
+
this.#discardUnread();
|
|
389
|
+
return;
|
|
145
390
|
}
|
|
391
|
+
this.#backend.resetStream(this.peerId, this.connId, this.streamId);
|
|
392
|
+
this.terminal();
|
|
146
393
|
}
|
|
147
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* Relinquishes the stream and requests a reset while it remains active.
|
|
396
|
+
* On a stream that already closed, discards unread bytes instead.
|
|
397
|
+
*/
|
|
148
398
|
abandon() {
|
|
149
|
-
if (
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
// A native close can overtake its queued terminal event.
|
|
155
|
-
}
|
|
156
|
-
this.terminal();
|
|
399
|
+
if (this.#closed) {
|
|
400
|
+
this.#discardUnread();
|
|
401
|
+
return;
|
|
157
402
|
}
|
|
403
|
+
try {
|
|
404
|
+
this.#backend.abandonStream(this.peerId, this.connId, this.streamId);
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
// A native close can overtake its queued terminal event.
|
|
408
|
+
}
|
|
409
|
+
this.terminal();
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* A cleanly closed stream keeps unread bytes for later reads, and they
|
|
413
|
+
* hold its native stream slot until consumed; discarding them releases it.
|
|
414
|
+
*/
|
|
415
|
+
#discardUnread() {
|
|
416
|
+
this.#consumed(this.#fifoBytes);
|
|
417
|
+
this.#fifo.length = 0;
|
|
418
|
+
this.#fifoBytes = 0;
|
|
158
419
|
}
|
|
159
420
|
[Symbol.dispose]() {
|
|
160
421
|
this.abandon();
|
|
@@ -182,23 +443,44 @@ export class Stream {
|
|
|
182
443
|
const read = this.#reads.shift();
|
|
183
444
|
if (read !== undefined) {
|
|
184
445
|
read.resolve(chunk);
|
|
446
|
+
this.#consumed(chunk.byteLength);
|
|
185
447
|
return;
|
|
186
448
|
}
|
|
187
449
|
if (this.#mode === "flowing") {
|
|
188
|
-
this.#
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
if (this.#fifo.length >= STREAM_CHUNK_CAP ||
|
|
192
|
-
this.#fifoBytes + chunk.byteLength > STREAM_BYTE_CAP) {
|
|
193
|
-
this.#emit("dataOverflow", {
|
|
194
|
-
droppedBytes: chunk.byteLength,
|
|
195
|
-
droppedChunks: 1,
|
|
196
|
-
});
|
|
450
|
+
this.#emitData(chunk);
|
|
197
451
|
return;
|
|
198
452
|
}
|
|
453
|
+
// Unbounded here, because native delivers at most one receive window
|
|
454
|
+
// that has not been consumed.
|
|
199
455
|
this.#fifo.push(chunk);
|
|
200
456
|
this.#fifoBytes += chunk.byteLength;
|
|
201
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Native accepted the whole in-flight write.
|
|
460
|
+
* @internal
|
|
461
|
+
*/
|
|
462
|
+
writeAccepted() {
|
|
463
|
+
if (!this.#writeInFlight) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
this.#writeInFlight = false;
|
|
467
|
+
this.#settleHead();
|
|
468
|
+
this.#pumpWrites();
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* The remote asked this side to stop writing.
|
|
472
|
+
* @internal
|
|
473
|
+
*/
|
|
474
|
+
writeStopped(errorCode) {
|
|
475
|
+
if (this.#closed) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const error = new StreamClosedError(`The remote stopped reading this stream (code ${errorCode})`);
|
|
479
|
+
this.#writeEnded ??= error;
|
|
480
|
+
this.#closeWriteQueued = false;
|
|
481
|
+
this.#failWrites(error);
|
|
482
|
+
this.#emit("writeStopped", { errorCode });
|
|
483
|
+
}
|
|
202
484
|
/** @internal */
|
|
203
485
|
remoteWriteClosed() {
|
|
204
486
|
if (this.#closed || this.#receiveState.kind !== "receiving") {
|
|
@@ -218,17 +500,17 @@ export class Stream {
|
|
|
218
500
|
return;
|
|
219
501
|
}
|
|
220
502
|
this.#closed = true;
|
|
503
|
+
this.#failWrites(error);
|
|
221
504
|
// StreamClosed carries no cause. Once the remote FIN establishes EOF,
|
|
222
505
|
// later full closure cannot reinterpret that receive-side outcome.
|
|
223
506
|
const hasRemoteEof = options.preserveRemoteEof === true && this.#receiveState.kind === "eof";
|
|
224
507
|
if (!hasRemoteEof) {
|
|
225
508
|
this.#receiveState = { error, kind: "failed" };
|
|
226
|
-
this.#
|
|
227
|
-
this.#fifoBytes = 0;
|
|
509
|
+
this.#discardUnread();
|
|
228
510
|
}
|
|
229
511
|
for (const read of this.#reads.splice(0)) {
|
|
230
512
|
if (hasRemoteEof) {
|
|
231
|
-
read.resolve(this.#
|
|
513
|
+
read.resolve(this.#take());
|
|
232
514
|
}
|
|
233
515
|
else {
|
|
234
516
|
read.reject(error);
|
|
@@ -238,21 +520,98 @@ export class Stream {
|
|
|
238
520
|
this.#listeners.clear();
|
|
239
521
|
this.#onTerminal();
|
|
240
522
|
}
|
|
523
|
+
/**
|
|
524
|
+
* Hands queued writes to native one at a time. A write native accepts only
|
|
525
|
+
* in part stays at the head until `StreamWriteAccepted`, which keeps
|
|
526
|
+
* writes, and the queued close, in order.
|
|
527
|
+
*/
|
|
528
|
+
#pumpWrites() {
|
|
529
|
+
while (!this.#writeInFlight && !this.#closed) {
|
|
530
|
+
const [head] = this.#writes;
|
|
531
|
+
if (head === undefined) {
|
|
532
|
+
if (this.#closeWriteQueued) {
|
|
533
|
+
this.#closeWriteQueued = false;
|
|
534
|
+
try {
|
|
535
|
+
this.#backend.closeStreamWrite(this.peerId, this.connId, this.streamId);
|
|
536
|
+
}
|
|
537
|
+
catch {
|
|
538
|
+
// A deferred close has no caller left to throw to; the stream
|
|
539
|
+
// already ended, and its terminal event reports that.
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
let accepted;
|
|
545
|
+
try {
|
|
546
|
+
accepted = this.#backend.sendStream(this.peerId, this.connId, this.streamId, head.data);
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
this.#failWrites(error);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (!accepted) {
|
|
553
|
+
this.#writeInFlight = true;
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
this.#settleHead();
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
#settleHead() {
|
|
560
|
+
const head = this.#writes.shift();
|
|
561
|
+
if (head !== undefined) {
|
|
562
|
+
this.#writeBytes -= head.data.byteLength;
|
|
563
|
+
head.resolve();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
#failWrites(error) {
|
|
567
|
+
this.#writeInFlight = false;
|
|
568
|
+
this.#writeBytes = 0;
|
|
569
|
+
for (const write of this.#writes.splice(0)) {
|
|
570
|
+
write.reject(error);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Hands buffered chunks to `data` handlers. After a clean close the
|
|
575
|
+
* buffer still holds the bytes that arrived before EOF (a failed stream
|
|
576
|
+
* has already discarded them), so a late handler drains it too.
|
|
577
|
+
*/
|
|
241
578
|
#flushFlowing() {
|
|
242
|
-
while (this.#fifo.length > 0
|
|
243
|
-
const chunk = this.#shift();
|
|
579
|
+
while (this.#fifo.length > 0) {
|
|
580
|
+
const chunk = this.#fifo.shift();
|
|
244
581
|
if (chunk !== undefined) {
|
|
245
|
-
this.#
|
|
582
|
+
this.#fifoBytes -= chunk.byteLength;
|
|
583
|
+
this.#emitData(chunk);
|
|
246
584
|
}
|
|
247
585
|
}
|
|
248
586
|
}
|
|
249
|
-
|
|
587
|
+
/** Hands `chunk` to every `data` handler; it is consumed once they return. */
|
|
588
|
+
#emitData(chunk) {
|
|
589
|
+
// Counted first: a handler may transfer the buffer, detaching it.
|
|
590
|
+
const bytes = chunk.byteLength;
|
|
591
|
+
this.#emit("data", chunk);
|
|
592
|
+
this.#consumed(bytes);
|
|
593
|
+
}
|
|
594
|
+
/** Takes the oldest buffered chunk for a reader, consuming it. */
|
|
595
|
+
#take() {
|
|
250
596
|
const chunk = this.#fifo.shift();
|
|
251
597
|
if (chunk !== undefined) {
|
|
252
598
|
this.#fifoBytes -= chunk.byteLength;
|
|
599
|
+
this.#consumed(chunk.byteLength);
|
|
253
600
|
}
|
|
254
601
|
return chunk;
|
|
255
602
|
}
|
|
603
|
+
/** Acknowledges consumed bytes to native, letting the sender continue. */
|
|
604
|
+
#consumed(bytes) {
|
|
605
|
+
if (bytes === 0) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
this.#consume(bytes);
|
|
610
|
+
}
|
|
611
|
+
catch {
|
|
612
|
+
// The stream or its connection is already gone natively.
|
|
613
|
+
}
|
|
614
|
+
}
|
|
256
615
|
#emit(type, payload) {
|
|
257
616
|
for (const handler of [...(this.#listeners.get(type) ?? [])]) {
|
|
258
617
|
try {
|
|
@@ -264,6 +623,27 @@ export class Stream {
|
|
|
264
623
|
}
|
|
265
624
|
}
|
|
266
625
|
}
|
|
626
|
+
/**
|
|
627
|
+
* The backend's acknowledgement function for a stream. A connection whose
|
|
628
|
+
* end the adapter already saw has nothing left to settle.
|
|
629
|
+
*/
|
|
630
|
+
const streamConsumer = (backend, meta) => {
|
|
631
|
+
try {
|
|
632
|
+
return backend.streamConsumer(meta.connId, meta.streamId);
|
|
633
|
+
}
|
|
634
|
+
catch {
|
|
635
|
+
return () => { };
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
/** Acknowledges data that arrived for a stream the SDK no longer tracks. */
|
|
639
|
+
const consumeUntracked = (backend, inner) => {
|
|
640
|
+
try {
|
|
641
|
+
backend.streamConsumer(inner.connId, inner.streamId)(inner.data.byteLength);
|
|
642
|
+
}
|
|
643
|
+
catch {
|
|
644
|
+
// Settled already, or its connection is gone: nothing to release.
|
|
645
|
+
}
|
|
646
|
+
};
|
|
267
647
|
/**
|
|
268
648
|
* Promise-first, platform-neutral owner for one native minip2p endpoint.
|
|
269
649
|
*
|
|
@@ -276,7 +656,9 @@ export class Minip2pBase {
|
|
|
276
656
|
#catchAll = new Set();
|
|
277
657
|
#closeHandlers = new Set();
|
|
278
658
|
#waiters = new Set();
|
|
279
|
-
#queue = new
|
|
659
|
+
#queue = new EventQueue(EVENT_QUEUE_CAP, (item) => {
|
|
660
|
+
this.#releaseQueueItem(item);
|
|
661
|
+
});
|
|
280
662
|
#connects = new Map();
|
|
281
663
|
#terminalConnects = new Set();
|
|
282
664
|
#streams = new Map();
|
|
@@ -923,9 +1305,7 @@ export class Minip2pBase {
|
|
|
923
1305
|
this.#streamReady(event.inner);
|
|
924
1306
|
return;
|
|
925
1307
|
}
|
|
926
|
-
if (event
|
|
927
|
-
event.tag === P2pEvent_Tags.StreamRemoteWriteClosed ||
|
|
928
|
-
event.tag === P2pEvent_Tags.StreamClosed) {
|
|
1308
|
+
if (isStreamEvent(event)) {
|
|
929
1309
|
this.#streamEvent(event);
|
|
930
1310
|
return;
|
|
931
1311
|
}
|
|
@@ -1067,7 +1447,21 @@ export class Minip2pBase {
|
|
|
1067
1447
|
});
|
|
1068
1448
|
}
|
|
1069
1449
|
else if (event.tag === P2pEvent_Tags.StreamData) {
|
|
1070
|
-
stream
|
|
1450
|
+
if (stream === undefined) {
|
|
1451
|
+
// Nothing will read it, so it is consumed now: an abandon that
|
|
1452
|
+
// native refused (the stream had already closed there) leaves the
|
|
1453
|
+
// bytes holding the native stream slot otherwise.
|
|
1454
|
+
consumeUntracked(this.#backend, event.inner);
|
|
1455
|
+
}
|
|
1456
|
+
else {
|
|
1457
|
+
stream.receive(event.inner.data);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
else if (event.tag === P2pEvent_Tags.StreamWriteAccepted) {
|
|
1461
|
+
stream?.writeAccepted();
|
|
1462
|
+
}
|
|
1463
|
+
else if (event.tag === P2pEvent_Tags.StreamWriteStopped) {
|
|
1464
|
+
stream?.writeStopped(event.inner.errorCode);
|
|
1071
1465
|
}
|
|
1072
1466
|
else {
|
|
1073
1467
|
stream?.remoteWriteClosed();
|