This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·291 lines (291 loc) · 8.46 KB
/
index.js
File metadata and controls
executable file
·291 lines (291 loc) · 8.46 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var DnodeScrubber, DnodeSession, DnodeStore, EventEmitter, Scrubber, createId, exports, getAt, parseArgs, setAt, stream, _ref;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
EventEmitter = require('events').EventEmitter;
_ref = require('jspath'), getAt = _ref.getAt, setAt = _ref.setAt;
Scrubber = require('scrubber');
createId = require('hat').rack();
stream = process.title === "browser" ? {} : require("stream");
JSON || (JSON = require('jsonify'));
exports = module.exports = function(wrapper) {
return {
sessions: {},
create: function() {
var id;
id = createId();
return this.sessions[id] = new DnodeSession(id, wrapper);
},
destroy: function(id) {
return delete this.sessions[id];
}
};
};
/**
* @class DnodeSession
* @description an implementation of the Session class from dnode-protocol
*/
exports.Session = DnodeSession = (function() {
var apply;
__extends(DnodeSession, EventEmitter);
function DnodeSession(id, wrapper) {
this.id = id;
this.parse = __bind(this.parse, this);
this.remote = {};
this.instance = 'function' === typeof wrapper ? new wrapper(this.remote, this) : wrapper || {};
this.localStore = new DnodeStore;
this.remoteStore = new DnodeStore;
this.localStore.on('cull', __bind(function(id) {
return this.emit('request', {
method: 'cull',
arguments: [id],
callbacks: {}
});
}, this));
}
DnodeSession.prototype.start = function() {
return this.request('methods', [this.instance]);
};
DnodeSession.prototype.request = function(method, args) {
var scrubber;
scrubber = new DnodeScrubber(this.localStore);
return scrubber.scrub(args, __bind(function() {
var scrubbed;
scrubbed = scrubber.toDnodeProtocol();
scrubbed.method = method;
return this.emit('request', scrubbed);
}, this));
};
DnodeSession.prototype.parse = function(line) {
var msg;
try {
msg = JSON.parse(line);
} catch (err) {
this.emit('error', new SyntaxError("JSON parsing error: " + err));
}
return this.handle(msg);
};
DnodeSession.prototype.handle = function(msg) {
var args, method, scrubber;
scrubber = new DnodeScrubber(this.localStore);
args = scrubber.unscrub(msg, __bind(function(callbackId) {
if (!this.remoteStore.has(callbackId)) {
this.remoteStore.add(callbackId, __bind(function() {
return this.request(callbackId, [].slice.call(arguments));
}, this));
}
return this.remoteStore.get(callbackId);
}, this));
method = msg.method;
switch (method) {
case 'methods':
return this.handleMethods(args[0]);
case 'error':
return this.emit('remoteError', args[0]);
case 'cull':
return args.forEach(__bind(function(id) {
return this.remoteStore.cull(id);
}, this));
default:
switch (typeof method) {
case 'string':
if (this.instance.propertyIsEnumerable(method)) {
return apply(this.instance[method], this.instance, args);
} else {
return this.emit('error', new Error("Request for non-enumerable method: " + method));
}
break;
case 'number':
return apply(this.localStore.get(method), this.instance, args);
}
}
};
DnodeSession.prototype.handleMethods = function(methods) {
if (methods == null) {
methods = {};
}
Object.keys(this.remote).forEach(__bind(function(key) {
return delete this.remote[key];
}, this));
Object.keys(methods).forEach(__bind(function(key) {
return this.remote[key] = methods[key];
}, this));
this.emit('remote', this.remote);
return this.emit('ready');
};
apply = function(fn, ctx, args) {
return fn.apply(ctx, args);
};
return DnodeSession;
})();
/**
* @class DnodeScrubber
* @description an implementation of the Scrubber class from dnode-protocol that supports a middleware stack
*/
exports.Scrubber = DnodeScrubber = (function() {
__extends(DnodeScrubber, Scrubber);
function DnodeScrubber(store) {
var dnodeMutators, userStack;
if (store == null) {
store = new DnodeStore;
}
this.paths = {};
this.links = [];
dnodeMutators = [
function(cursor) {
var i, id, node, path;
node = cursor.node, path = cursor.path;
if ('function' === typeof node) {
i = store.indexOf(node);
if (~i && !(i in this.paths)) {
this.paths[i] = path;
} else {
id = store.add(node);
this.paths[id] = path;
}
return cursor.update('[Function]', true);
}
}, function(cursor) {
if (cursor.circular) {
this.links.push({
from: cursor.circular.path,
to: cursor.path
});
return cursor.update('[Circular]', true);
}
}
];
userStack = DnodeScrubber.stack || [];
Scrubber.apply(this, dnodeMutators.concat(userStack));
}
DnodeScrubber.prototype.unscrub = function(msg, getCallback) {
var args;
args = msg.arguments || [];
Object.keys(msg.callbacks || {}).forEach(function(strId) {
var callback, id, path;
id = parseInt(strId, 10);
path = msg.callbacks[id];
callback = getCallback(id);
callback.id = id;
return setAt(args, path, callback);
});
(msg.links || []).forEach(function(link) {
return setAt(args, link.to, getAt(args, link.from));
});
return args;
};
DnodeScrubber.prototype.toDnodeProtocol = function() {
var out;
out = {
arguments: this.out
};
out.callbacks = this.paths;
if (this.links.length) {
out.links = this.links;
}
return out;
};
return DnodeScrubber;
})();
/**
* @class DnodeStore
* @description an implementation of the Store class from dnode-protocol
*/
exports.Store = DnodeStore = (function() {
var autoCull, wrap;
__extends(DnodeStore, EventEmitter);
function DnodeStore() {
this.items = [];
}
DnodeStore.prototype.has = function(id) {
return this.items[id] != null;
};
DnodeStore.prototype.get = function(id) {
var item;
item = this.items[id];
if (item == null) {
return null;
}
return wrap(item);
};
DnodeStore.prototype.add = function(id, fn) {
var _ref2;
if (!fn) {
_ref2 = [id, fn], fn = _ref2[0], id = _ref2[1];
}
if (id == null) {
id = this.items.length;
}
this.items[id] = fn;
return id;
};
DnodeStore.prototype.cull = function(arg) {
if ('function' === typeof arg) {
arg = this.items.indexOf(arg);
}
delete this.items[arg];
return arg;
};
DnodeStore.prototype.indexOf = function(fn) {
return this.items.indexOf(fn);
};
wrap = function(fn) {
return function() {
fn.apply(this, arguments);
return autoCull(fn);
};
};
autoCull = function(fn) {
var id;
if ('number' === typeof fn.times) {
fn.times--;
if (fn.times === 0) {
id = this.cull(fn);
return this.emit('cull', id);
}
}
};
return DnodeStore;
})();
parseArgs = exports.parseArgs = function(argv) {
var params;
params = {};
[].slice.call(argv).forEach(function(arg) {
switch (typeof arg) {
case 'string':
if (arg.match(/^\d+$/)) {
return params.port = parseInt(arg, 10);
} else if (arg.match("^/")) {
return params.path = arg;
} else {
return params.host = arg;
}
break;
case 'number':
return params.port = arg;
case 'function':
return params.block = arg;
case 'object':
if (arg.__proto__ === Object.prototype) {
return Object.keys(arg).forEach(function(key) {
return params[key] = arg[key];
});
} else if (stream.Stream && arg instanceof stream.Stream) {
return params.stream = arg;
} else {
return params.server = arg;
}
break;
case 'undefined':
break;
default:
throw new Error('Not sure what to do about ' + typeof arg + ' objects');
}
});
return params;
};