-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
246 lines (180 loc) · 7.17 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
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
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* @license Apache-2.0
*/
var Addon = require('./addon.js').Addon;
var WishAppInner = require('./wish-app-inner.js').WishAppInner;
var MistNodeInner = require('./mist-node-inner.js').MistNodeInner;
var EventEmitter = require("events").EventEmitter;
var inherits = require('util').inherits;
function Mist(opts) {
//console.log("Nodejs new Mist()", opts);
var self = this;
this.requests = {};
this.invokeCb = {};
this.writeCb = function() {};
//console.log("2. Creating the MistApi.StreamingWorker.");
if (typeof opts === 'string') { opts = { name: opts }; }
if (!opts) { opts = {}; }
// Default to MistApi
if (!opts.type) { opts.type = 2; }
this.opts = opts;
if (process.env.CORE && this.opts.corePort) { console.log('Failed setting WishCore port from env, already set in options!'); }
if (process.env.CORE && !this.opts.corePort) { this.opts.corePort = parseInt(process.env.CORE); }
//console.log('Starting with opts:', opts);
setTimeout(function() { self.emit('ready'); }, 200);
this.sandboxes = {};
this.addon = new Addon(opts);
this.addon.on('sandboxed', function(msg) {
var id = msg.ack || msg.sig || msg.end || msg.err || msg.fin;
//console.log("the answer is:", require('util').inspect(msg, { colors: true, depth: 10 }));
if(typeof self.requests[id] === 'function') {
self.requests[id](msg);
if(!msg.sig) {
delete self.requests[id];
}
} else {
console.log('addon.on(\'sandboxed\' cb: Request not found for response:', msg, self, self.requests);
}
});
this.addon.on('mist', function(msg) {
var id = msg.ack || msg.sig || msg.end || msg.err || msg.fin;
//console.log("mist: the answer is:", require('util').inspect(msg, { colors: true, depth: 10 }));
if(typeof self.requests[id] === 'function') {
self.requests[id](msg);
if(!msg.sig) {
delete self.requests[id];
}
} else {
console.log('addon.on(\'mist\' cb: Request not found for response:', msg, self, self.requests);
}
});
this.node = new MistNodeInner(this.addon);
this.wish = new WishAppInner(this.addon);
this.node.request = function(op, args, cb) { cb(true, { code: 1100, msg: 'node.request is not supported from MistApi. Use request on MistAPi and prepend mist., i.e. mist.control.invoke instead of control.invoke.' }) }
}
inherits(Mist, EventEmitter);
Mist.prototype.request = function(op, args, cb) {
return this.requestBare(op, args, function(res) {
//console.log('requestBare cb:', arguments);
if(res.fin) { return cb(true, { end: true }); }
if(res.err) { return cb(true, res.data); }
cb(null, res.data);
});
};
Mist.prototype.requestBare = function(op, args, cb) {
var id = ++this.addon.sharedRequestId;
var request = { op: op, args: args, id: id };
// store callback for response
this.requests[id] = cb;
//console.log("Making request", request, this);
this.addon.request("mist", request);
return id;
};
Mist.prototype.requestCancel = function(id) {
var request = { cancel: id };
this.addon.request("mist", request);
};
Mist.prototype.registerSandbox = function(sandbox) {
this.sandbox = sandbox;
};
Mist.prototype.shutdown = function() {
this.addon.request("kill", { kill: true });
};
function MistNode(opts) {
if (typeof opts === 'string') { opts = { name: opts }; }
if (!opts) { opts = {}; }
// Default to MistApi
if (!opts.type) { opts.type = 3; }
if (process.env.CORE && opts.corePort) { console.log('Failed setting WishCore port from env, already set in options!'); }
if (process.env.CORE && !opts.corePort) { opts.corePort = parseInt(process.env.CORE); }
this.addon = new Addon(opts);
var node = new MistNodeInner(this.addon);
node.wish = new WishAppInner(this.addon);
return node;
}
function Sandboxed(mist, sandboxId) {
if (!mist || !mist.opts || !mist.opts.type === 2) {
throw new Error('Sandbox constructor parameter 1 must be Mist of type 2.');
}
if ( !Buffer.isBuffer(sandboxId) || sandboxId.length !== 32 ) {
console.log("sandboxId:", sandboxId);
throw new Error('Sandbox constructor parameter 2 must be Buffer(len:32).');
}
this.addon = mist.addon;
this.sandboxId = sandboxId;
this.mist = mist;
mist.registerSandbox(this);
}
Sandboxed.prototype.request = function(op, args, cb) {
return this.requestBare(op, args, function(res) {
if(res.fin) { return cb(true, { end: true }); }
if(res.err) { return cb(true, res.data); }
cb(null, res.data);
});
};
Sandboxed.prototype.requestBare = function(op, args, cb) {
var id = ++this.addon.sharedRequestId;
var sandboxArgs = [this.sandboxId].concat(args);
//console.log('sandboxed.'+op+'(', sandboxArgs, '):', id);
var request = { op: 'sandboxed.'+op, args: sandboxArgs, id: id };
// store callback for response in the mist object
this.mist.requests[id] = cb;
this.addon.request('sandboxed', request);
return id;
};
Sandboxed.prototype.requestCancel = function(id) {
var self = this;
var request = { cancel: id, sandbox: this.sandboxId };
setTimeout(() => {
if (typeof self.mist.requests[id] === 'function') {
self.mist.requests[id](true, { timeout: true });
delete self.mist.requests[id];
}
}, 1500);
this.addon.request('sandboxed', request);
};
function copy(that) {
var copy = {};
for(var i in that) { copy[i] = that[i]; }
return copy;
}
function WishApp(opts) {
if (typeof opts === 'string') { opts = { name: opts }; }
if (!opts) { opts = {}; }
opts = copy(opts);
// force type to WishApp
opts.type = 4;
if ( Array.isArray(opts.protocols) ) {
if (opts.protocols.length === 1) {
opts.protocols = opts.protocols[0];
} else if (opts.protocols.length === 0) {
delete opts.protocols;
} else {
throw new Error('WishApp requires 0 or one protocols (multiple not yet supported)');
}
} else if (!opts.protocols) {
// fine
} else {
throw new Error('WishApp protocols must be array or non-existing.');
}
if (process.env.CORE && opts.corePort) { console.log('Failed setting WishCore port from env, already set in options!'); }
if (process.env.CORE && !opts.corePort) { opts.corePort = parseInt(process.env.CORE); }
var addon = new Addon(opts);
var wish = new WishAppInner(addon);
// FIXME this is required by multi-mist.js test, but should be removed
wish.opts = opts;
return wish;
}
module.exports = {
Mist: Mist,
MistNode: MistNode,
Sandboxed: Sandboxed,
WishApp: WishApp };