forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplex.js
More file actions
137 lines (125 loc) · 3.59 KB
/
Copy pathduplex.js
File metadata and controls
137 lines (125 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
90
91
92
93
94
95
96
97
98
99
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
136
137
'use strict';
// New Streams API - Duplex Channel
//
// Creates a pair of connected channels where data written to one
// channel's writer appears in the other channel's readable.
const {
SymbolAsyncDispose,
SymbolAsyncIterator,
} = primordials;
const {
push,
} = require('internal/streams/iter/push');
const {
validateAbortSignal,
validateObject,
} = require('internal/validators');
/**
* Create a pair of connected duplex channels for bidirectional communication.
* @param {{ highWaterMark?: number, backpressure?: string, signal?: AbortSignal,
* a?: object, b?: object }} [options]
* @returns {[DuplexChannel, DuplexChannel]}
*/
function duplex(options = { __proto__: null }) {
validateObject(options, 'options');
const { highWaterMark, backpressure, signal, a, b } = options;
if (a !== undefined) {
validateObject(a, 'options.a');
}
if (b !== undefined) {
validateObject(b, 'options.b');
}
if (signal !== undefined) {
validateAbortSignal(signal, 'options.signal');
}
// Channel A writes to B's readable (A->B direction).
// Signal is NOT passed to push() -- we handle abort via close() below.
const { writer: aWriter, readable: bReadable } = push({
highWaterMark: a?.highWaterMark ?? highWaterMark,
backpressure: a?.backpressure ?? backpressure,
});
// Channel B writes to A's readable (B->A direction)
const { writer: bWriter, readable: aReadable } = push({
highWaterMark: b?.highWaterMark ?? highWaterMark,
backpressure: b?.backpressure ?? backpressure,
});
let aClosed = false;
let bClosed = false;
// Track active iterators so close() can call .return() on them
let aReadableIterator = null;
let bReadableIterator = null;
const channelA = {
__proto__: null,
get writer() { return aWriter; },
// Wrap readable to track the iterator for cleanup on close()
get readable() {
return {
__proto__: null,
[SymbolAsyncIterator]() {
const iter = aReadable[SymbolAsyncIterator]();
aReadableIterator = iter;
return iter;
},
};
},
async close() {
if (aClosed) return;
aClosed = true;
// End the writer (signals end-of-stream to B's readable)
aWriter.endSync();
// Stop iteration of this channel's readable
if (aReadableIterator?.return) {
await aReadableIterator.return();
aReadableIterator = null;
}
},
[SymbolAsyncDispose]() {
return this.close();
},
};
const channelB = {
__proto__: null,
get writer() { return bWriter; },
get readable() {
return {
__proto__: null,
[SymbolAsyncIterator]() {
const iter = bReadable[SymbolAsyncIterator]();
bReadableIterator = iter;
return iter;
},
};
},
async close() {
if (bClosed) return;
bClosed = true;
bWriter.endSync();
if (bReadableIterator?.return) {
await bReadableIterator.return();
bReadableIterator = null;
}
},
[SymbolAsyncDispose]() {
return this.close();
},
};
// Signal handler: fail both writers with the abort reason so consumers
// see the error. This is an error-path shutdown, not a clean close.
if (signal) {
const abortBoth = () => {
const reason = signal.reason;
aWriter.fail(reason);
bWriter.fail(reason);
};
if (signal.aborted) {
abortBoth();
} else {
signal.addEventListener('abort', abortBoth,
{ __proto__: null, once: true });
}
}
return [channelA, channelB];
}
module.exports = {
duplex,
};