• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  Symbol,
5} = primordials;
6
7const { Duplex } = require('stream');
8
9const kCallback = Symbol('Callback');
10const kOtherSide = Symbol('Other');
11
12class DuplexSocket extends Duplex {
13  constructor() {
14    super();
15    this[kCallback] = null;
16    this[kOtherSide] = null;
17  }
18
19  _read() {
20    const callback = this[kCallback];
21    if (callback) {
22      this[kCallback] = null;
23      callback();
24    }
25  }
26
27  _write(chunk, encoding, callback) {
28    if (chunk.length === 0) {
29      process.nextTick(callback);
30    } else {
31      this[kOtherSide].push(chunk);
32      this[kOtherSide][kCallback] = callback;
33    }
34  }
35
36  _final(callback) {
37    this[kOtherSide].on('end', callback);
38    this[kOtherSide].push(null);
39  }
40}
41
42class DuplexPair {
43  constructor() {
44    this.socket1 = new DuplexSocket();
45    this.socket2 = new DuplexSocket();
46    this.socket1[kOtherSide] = this.socket2;
47    this.socket2[kOtherSide] = this.socket1;
48  }
49}
50
51module.exports = DuplexPair;
52