forked from nodejs/string_decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplexpair.js
More file actions
94 lines (76 loc) · 2.95 KB
/
duplexpair.js
File metadata and controls
94 lines (76 loc) · 2.95 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
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/*<replacement>*/
require('babel-polyfill');
var util = require('util');
for (var i in util) {
exports[i] = util[i];
} /*</replacement>*/ /*<replacement>*/
if (!global.setImmediate) {
global.setImmediate = function setImmediate(fn) {
return setTimeout(fn.bind.apply(fn, arguments), 4);
};
}
if (!global.clearImmediate) {
global.clearImmediate = function clearImmediate(i) {
return clearTimeout(i);
};
}
/*</replacement>*/
/* eslint-disable required-modules */
'use strict';
/*<replacement>*/
var objectKeys = objectKeys || function (obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
}return keys;
};
/*</replacement>*/
var _require = require('stream'),
Duplex = _require.Duplex;
var assert = require('assert');
var kCallback = Symbol('Callback');
var kOtherSide = Symbol('Other');
var DuplexSocket = function (_Duplex) {
_inherits(DuplexSocket, _Duplex);
function DuplexSocket() {
_classCallCheck(this, DuplexSocket);
var _this = _possibleConstructorReturn(this, _Duplex.call(this));
_this[kCallback] = null;
_this[kOtherSide] = null;
return _this;
}
DuplexSocket.prototype._read = function _read() {
var callback = this[kCallback];
if (callback) {
this[kCallback] = null;
callback();
}
};
DuplexSocket.prototype._write = function _write(chunk, encoding, callback) {
assert.notStrictEqual(this[kOtherSide], null);
assert.strictEqual(this[kOtherSide][kCallback], null);
this[kOtherSide][kCallback] = callback;
this[kOtherSide].push(chunk);
};
DuplexSocket.prototype._final = function _final(callback) {
this[kOtherSide].on('end', callback);
this[kOtherSide].push(null);
};
return DuplexSocket;
}(Duplex);
function makeDuplexPair() {
var clientSide = new DuplexSocket();
var serverSide = new DuplexSocket();
clientSide[kOtherSide] = serverSide;
serverSide[kOtherSide] = clientSide;
return { clientSide: clientSide, serverSide: serverSide };
}
module.exports = makeDuplexPair;
function forEach(xs, f) {
for (var i = 0, l = xs.length; i < l; i++) {
f(xs[i], i);
}
}