|
| 1 | +// |
| 2 | +// This code was copied from |
| 3 | +// https://github.com/nodejs/node/blob/c506660f3267/test/common/duplexpair.js |
| 4 | +// |
| 5 | +// Copyright Node.js contributors. All rights reserved. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to |
| 9 | +// deal in the Software without restriction, including without limitation the |
| 10 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +// IN THE SOFTWARE. |
| 24 | +// |
| 25 | +'use strict'; |
| 26 | + |
| 27 | +const assert = require('assert'); |
| 28 | +const { Duplex } = require('stream'); |
| 29 | + |
| 30 | +const kCallback = Symbol('Callback'); |
| 31 | +const kOtherSide = Symbol('Other'); |
| 32 | + |
| 33 | +class DuplexSocket extends Duplex { |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + this[kCallback] = null; |
| 37 | + this[kOtherSide] = null; |
| 38 | + } |
| 39 | + |
| 40 | + _read() { |
| 41 | + const callback = this[kCallback]; |
| 42 | + if (callback) { |
| 43 | + this[kCallback] = null; |
| 44 | + callback(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + _write(chunk, encoding, callback) { |
| 49 | + assert.notStrictEqual(this[kOtherSide], null); |
| 50 | + assert.strictEqual(this[kOtherSide][kCallback], null); |
| 51 | + if (chunk.length === 0) { |
| 52 | + process.nextTick(callback); |
| 53 | + } else { |
| 54 | + this[kOtherSide].push(chunk); |
| 55 | + this[kOtherSide][kCallback] = callback; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + _final(callback) { |
| 60 | + this[kOtherSide].on('end', callback); |
| 61 | + this[kOtherSide].push(null); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function makeDuplexPair() { |
| 66 | + const clientSide = new DuplexSocket(); |
| 67 | + const serverSide = new DuplexSocket(); |
| 68 | + clientSide[kOtherSide] = serverSide; |
| 69 | + serverSide[kOtherSide] = clientSide; |
| 70 | + return { clientSide, serverSide }; |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = makeDuplexPair; |
0 commit comments