-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #34111 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
6a5120f
commit 7c21bb9
Showing
32 changed files
with
230 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
const { | ||
Symbol, | ||
} = primordials; | ||
|
||
const { Duplex } = require('stream'); | ||
const assert = require('internal/assert'); | ||
|
||
const kCallback = Symbol('Callback'); | ||
const kInitOtherSide = Symbol('InitOtherSide'); | ||
|
||
class DuplexSide extends Duplex { | ||
#otherSide = null; | ||
|
||
constructor(options) { | ||
super(options); | ||
this[kCallback] = null; | ||
this.#otherSide = null; | ||
} | ||
|
||
[kInitOtherSide](otherSide) { | ||
// Ensure this can only be set once, to enforce encapsulation. | ||
if (this.#otherSide === null) { | ||
this.#otherSide = otherSide; | ||
} else { | ||
assert(this.#otherSide === null); | ||
} | ||
} | ||
|
||
_read() { | ||
const callback = this[kCallback]; | ||
if (callback) { | ||
this[kCallback] = null; | ||
callback(); | ||
} | ||
} | ||
|
||
_write(chunk, encoding, callback) { | ||
assert(this.#otherSide !== null); | ||
assert(this.#otherSide[kCallback] === null); | ||
if (chunk.length === 0) { | ||
process.nextTick(callback); | ||
} else { | ||
this.#otherSide.push(chunk); | ||
this.#otherSide[kCallback] = callback; | ||
} | ||
} | ||
|
||
_final(callback) { | ||
this.#otherSide.on('end', callback); | ||
this.#otherSide.push(null); | ||
} | ||
} | ||
|
||
function duplexPair(options) { | ||
const side0 = new DuplexSide(options); | ||
const side1 = new DuplexSide(options); | ||
side0[kInitOtherSide](side1); | ||
side1[kInitOtherSide](side0); | ||
return [ side0, side1 ]; | ||
} | ||
module.exports = duplexPair; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.