forked from pkrumins/dnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (107 loc) · 3.13 KB
/
index.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var dnode = require('./lib/dnode');
var parseArgs = require('./lib/parse_args');
var net = require('net');
var util = require('util');
var weak = require('weak');
exports = module.exports = function (cons, opts) {
return new D(cons, opts);
};
exports.connect = function () {
var d = new D();
return d.connect.apply(d, arguments);
};
exports.listen = function () {
var d = new D();
return d.listen.apply(d, arguments);
};
util.inherits(D, dnode);
function D (cons, opts) {
var self = this;
if (!opts) opts = {};
if (opts.weak !== false && !opts.proto) {
opts.proto = {};
opts.proto.wrap = function (cb, id) {
return weak(cb, function () {
if (self.proto) self.proto.cull(id);
});
};
opts.proto.unwrap = function (ref, id) {
var cb = weak.get(ref);
return cb || function () {};
};
}
return dnode.call(self, cons, opts);
}
D.prototype.connect = function () {
var self = this;
var params = parseArgs(arguments);
var stream;
if (params.path) {
stream = net.connect(params.path);
}
else if (params.port) {
stream = net.connect(params.port, params.host);
}
else {
throw new Error('no port or unix path given');
}
if (params.block) self.on('remote', params.block);
stream.on('error', function (err) {
if (err && err.code === 'EPIPE') return; // eat EPIPEs
self.emit('error', err);
});
self.id = randomId();
self.stream = stream;
stream.pipe(self);
self.pipe(stream);
return self;
};
dnode.prototype.listen = function () {
var self = this;
// just copy over the opts and cons, the rest will need to be re-created
var cons = self.cons, opts = self.opts;
self.cons = function () {};
self.end();
var params = parseArgs(arguments);
var server = net.createServer(function (stream) {
var d = new dnode(cons, opts);
do { d.id = randomId() }
while (server.sessions[d.id]);
server.sessions[d.id] = d;
d.on('end', function () {
delete server.sessions[d.id];
});
d.on('local', function (ref) {
server.emit('local', ref, d);
});
d.on('remote', function (remote) {
server.emit('remote', remote, d);
});
stream.on('error', function (err) {
if (err && err.code === 'EPIPE') return; // eat EPIPEs
d.emit('error', err);
});
d.stream = stream;
stream.pipe(d);
d.pipe(stream);
});
server.sessions = {};
if (params.port) {
server.listen(params.port, params.host);
}
else if (params.path) {
server.listen(params.path);
}
else {
throw new Error('no port or path provided');
}
if (params.block) server.on('listening', params.block);
return server;
};
function randomId () {
var s = '';
for (var i = 0; i < 4; i++) {
s += Math.random().toString(16).slice(2);
}
return s;
}