-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
187 lines (160 loc) · 5.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var dnode = require('dnode');
var Promise = require('bluebird');
var ClosedStreamError = exports.ClosedStreamError = function (message) {
this.name = 'ClosedStreamError';
this.message = message;
this.stack = (new Error()).stack;
}
ClosedStreamError.prototype = new Error;
var RemoteError = exports.RemoteError = function (err) {
this.name = 'RemoteError';
this.message = err.message;
this.remote = err;
}
RemoteError.prototype = new Error;
var RemoteOperationalError = exports.RemoteOperationalError = function (err) {
this.name = 'RemoteOperationalError';
this.message = err.message;
this.remote = err;
}
RemoteOperationalError.prototype = new Promise.OperationalError;
function getNestedKeys(obj) {
all_keys = {};
function get_internal(obj) {
var keys = Object.keys(obj);
var k_l = keys.length;
var value, key;
for (var i = 0; i < k_l; i++) {
key = keys[i];
all_keys[key] = 1;
value = obj[key];
if (value instanceof Object) {
get_internal(value);
}
}
}
get_internal(obj);
return Object.keys(all_keys);
}
var remotepromise = function (stream, promises) {
var ended = false;
var fnrejects = [];
var promisify = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return new Promise(function(fnresolve, fnreject) {
if(ended) {
fnreject(new ClosedStreamError("Stream Closed!"));
return;
}
fnrejects.push(fnreject);
args.push(function () {
var idx = fnrejects.indexOf(fnreject);
if(idx != -1) {
fnrejects.splice(idx, 1);
}
var cbargs = Array.prototype.slice.call(arguments);
var err = cbargs.shift();
if(err) {
if(err instanceof Object && typeof err.message == "string" && typeof err.name == "string") {
if(err.name == "OperationalError") {
err = new RemoteOperationalError(err);
} else if (/Error$/.test(err.name)) {
err = new RemoteError(err);
}
}
fnreject(err);
} else {
fnresolve.apply(this, cbargs);
}
})
fn.apply(this, args);
});
}
}
var callbackify = function (pr) {
return function () {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
Promise.resolve()
.then(function () {
return pr.apply(this, args);
})
.then(function (retval) {
cb(null, retval);
})
.catch(function (err) {
cb((err instanceof Object) ? JSON.parse(JSON.stringify(err, getNestedKeys(err).concat(['name', 'message', 'stack']))) : err);
})
}
}
function streamTimeout(s, timeout, cb) {
var tm = setTimeout(function() {
s.end();
if(cb) {
cb();
}
}, timeout);
s.on('remote', function() {
clearTimeout(tm);
})
s.on('error', function() {
clearTimeout(tm);
})
}
return new Promise(function(resolve, reject) {
var remotefns;
if(promises instanceof Object) {
remotefns = {};
for(var i in promises) {
remotefns[i] = callbackify(promises[i]);
}
}
var d = dnode(remotefns);
d.on('remote', function (remote) {
for(var i in remote) {
remote[i] = promisify(remote[i]);
}
if(resolve) {
resolve(remote);
resolve = null;
reject = null;
}
});
d.on('fail', function (err) {
if(reject) {
reject(new ClosedStreamError("Stream error: " + err));
resolve = null;
reject = null;
}
});
d.on('error', function (err) {
if(reject) {
reject(new ClosedStreamError("Stream error: " + err));
resolve = null;
reject = null;
}
});
d.on('end', function (err) {
ended = true;
fnrejects.forEach(function (fnreject) {
fnreject(new ClosedStreamError("Stream Closed!"));
})
fnrejects = [];
if(reject) {
reject(new ClosedStreamError("Stream Closed!"));
resolve = null;
reject = null;
}
})
stream.pipe(d).pipe(stream);
streamTimeout(d, 20000, function() {
if(reject) {
reject(new ClosedStreamError("Stream Timeout!"));
resolve = null;
reject = null;
}
});
});
}
exports.instantiate = remotepromise;