1/* eslint-disable node-core/require-common-first, node-core/required-modules */ 2'use strict'; 3const { Duplex } = require('stream'); 4const assert = require('assert'); 5 6const kCallback = Symbol('Callback'); 7const kOtherSide = Symbol('Other'); 8 9class DuplexSocket extends Duplex { 10 constructor() { 11 super(); 12 this[kCallback] = null; 13 this[kOtherSide] = null; 14 } 15 16 _read() { 17 const callback = this[kCallback]; 18 if (callback) { 19 this[kCallback] = null; 20 callback(); 21 } 22 } 23 24 _write(chunk, encoding, callback) { 25 assert.notStrictEqual(this[kOtherSide], null); 26 assert.strictEqual(this[kOtherSide][kCallback], null); 27 if (chunk.length === 0) { 28 process.nextTick(callback); 29 } else { 30 this[kOtherSide].push(chunk); 31 this[kOtherSide][kCallback] = callback; 32 } 33 } 34 35 _final(callback) { 36 this[kOtherSide].on('end', callback); 37 this[kOtherSide].push(null); 38 } 39} 40 41function makeDuplexPair() { 42 const clientSide = new DuplexSocket(); 43 const serverSide = new DuplexSocket(); 44 clientSide[kOtherSide] = serverSide; 45 serverSide[kOtherSide] = clientSide; 46 return { clientSide, serverSide }; 47} 48 49module.exports = makeDuplexPair; 50