/
AsCallback.js
477 lines (454 loc) · 14.3 KB
/
AsCallback.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// NoFlo - Flow-Based Programming for JavaScript
// (c) 2017-2018 Flowhub UG
// NoFlo may be freely distributed under the MIT license
/* eslint-disable
no-param-reassign,
import/prefer-default-export,
*/
import { Graph } from 'fbp-graph';
import { ComponentLoader } from './ComponentLoader';
import { Network } from './Network';
import IP from './IP';
import * as internalSocket from './InternalSocket';
// ## asCallback embedding API
//
// asCallback is a helper for embedding NoFlo components or
// graphs in other JavaScript programs.
//
// By using the `noflo.asCallback` function, you can turn any
// NoFlo component or NoFlo Graph instance into a regular,
// Node.js-style JavaScript function.
//
// Each call to that function starts a new NoFlo network where
// the given input arguments are sent as IP objects to matching
// inports. Once the network finishes, the IP objects received
// from the network will be sent to the callback function.
//
// If there was anything sent to an `error` outport, this will
// be provided as the error argument to the callback.
// ### Option normalization
//
// Here we handle the input valus given to the `asCallback`
// function. This allows passing things like a pre-initialized
// NoFlo ComponentLoader, or giving the component loading
// baseDir context.
/**
* @typedef {Graph | string} AsCallbackComponent
*/
/**
* @typedef {Object} AsCallbackOptions
* @property {string} [name] - Name for the wrapped network
* @property {ComponentLoader} [loader] - Component loader instance to use, if any
* @property {string} [baseDir] - Project base directory for component loading
* @property {Object} [flowtrace] - Flowtrace instance to use for tracing this network run
* @property {NetworkCallback} [networkCallback] - Access to Network instance
* @property {boolean} [raw] - Whether the callback should operate on raw noflo.IP objects
* @property {boolean} [asyncDelivery] - Make Information Packet delivery asynchronous
*/
/**
* @typedef {Array<Object<string, IP>>} OutputMap
*/
/**
* @typedef {Object<string, Array<IP|any>>|Array<Object<string, IP|any>>} InputMap
*/
/**
* @param {AsCallbackOptions} options
* @param {AsCallbackComponent} component
* @returns {AsCallbackOptions}
*/
function normalizeOptions(options, component) {
if (!options) { options = {}; }
if (!options.name && typeof component === 'string') {
options.name = component;
}
if (options.loader) {
options.baseDir = options.loader.baseDir;
}
if (!options.baseDir && process && process.cwd) {
options.baseDir = process.cwd();
}
if (options.baseDir && !options.loader) {
options.loader = new ComponentLoader(options.baseDir);
}
if (!options.raw) {
options.raw = false;
}
if (!options.asyncDelivery) {
options.asyncDelivery = false;
}
return options;
}
// ### Network preparation
//
// Each invocation of the asCallback-wrapped NoFlo graph
// creates a new network. This way we can isolate multiple
// executions of the function in their own contexts.
/**
* @param {AsCallbackComponent} component
* @param {AsCallbackOptions} options
* @returns {Promise<Network>}
*/
function prepareNetwork(component, options) {
// If we were given a graph instance, then just create a network
if (typeof component === 'object') {
// This is a graph object
const network = new Network(component, {
...options,
componentLoader: options.loader,
});
// Wire the network up
return network.connect();
}
if (!options.loader) {
return Promise.reject(new Error('No component loader provided'));
}
// Start by loading the component
return options.loader.load(component, {})
.then((instance) => {
// Prepare a graph wrapping the component
const graph = new Graph(options.name);
const nodeName = options.name || 'AsCallback';
graph.addNode(nodeName, component);
// Expose ports
const inPorts = instance.inPorts.ports;
const outPorts = instance.outPorts.ports;
Object.keys(inPorts).forEach((port) => {
graph.addInport(port, nodeName, port);
});
Object.keys(outPorts).forEach((port) => {
graph.addOutport(port, nodeName, port);
});
// Prepare network
const network = new Network(graph, {
...options,
componentLoader: options.loader,
});
// Wire the network up and start execution
return network.connect();
});
}
// ### Network execution
//
// Once network is ready, we connect to all of its exported
// in and outports and start the network.
//
// Input data is sent to the inports, and we collect IP
// packets received on the outports.
//
// Once the network finishes, we send the resulting IP
// objects to the callback.
/**
* @param {Network} network
* @param {any} inputs
* @returns {Promise<OutputMap>}
*/
function runNetwork(network, inputs) {
return new Promise((resolve, reject) => {
// Prepare inports
/** @type {Object<string, import("./InternalSocket").InternalSocket>} */
let inSockets = {};
// Subscribe outports
/** @type {Array<Object<string, IP>>} */
const received = [];
const outPorts = Object.keys(network.graph.outports);
/** @type {Object<string, import("./InternalSocket").InternalSocket>} */
let outSockets = {};
outPorts.forEach((outport) => {
const portDef = network.graph.outports[outport];
const process = network.getNode(portDef.process);
if (!process || !process.component) {
return;
}
outSockets[outport] = internalSocket.createSocket({}, {
debug: false,
});
network.subscribeSocket(outSockets[outport]);
process.component.outPorts.ports[portDef.port].attach(outSockets[outport]);
outSockets[outport].from = {
process,
port: portDef.port,
};
outSockets[outport].on('ip', (ip) => {
/** @type Object<string, IP> */
const res = {};
res[outport] = ip;
received.push(res);
});
});
// Subscribe to process errors
/**
* @callback EndListener
* @returns {void}
*/
/**
* @callback ErrorListener
* @param {import("./InternalSocket").SocketError} err
* @returns {void}
*/
/** @type {EndListener} */
let onEnd;
/** @type {ErrorListener} */
const onError = (err) => {
reject(err.error);
network.removeListener('end', onEnd);
};
network.once('process-error', onError);
// Subscribe network finish
onEnd = () => {
// Clear listeners
Object.keys(outSockets).forEach((port) => {
const socket = outSockets[port];
socket.from.process.component.outPorts[socket.from.port].detach(socket);
});
outSockets = {};
inSockets = {};
resolve(received);
network.removeListener('process-error', onError);
};
network.once('end', onEnd);
// Start network
network.start()
.then(() => {
// Send inputs
for (let i = 0; i < inputs.length; i += 1) {
const inputMap = inputs[i];
const keys = Object.keys(inputMap);
for (let j = 0; j < keys.length; j += 1) {
const port = keys[j];
const value = inputMap[port];
if (!inSockets[port]) {
const portDef = network.graph.inports[port];
if (!portDef) {
reject(new Error(`Port ${port} not available in the graph`));
return;
}
const process = network.getNode(portDef.process);
if (!process || !process.component) {
reject(new Error(`Process ${portDef.process} for port ${port} not available in the graph`));
return;
}
inSockets[port] = internalSocket.createSocket({}, {
debug: false,
});
network.subscribeSocket(inSockets[port]);
inSockets[port].to = {
process,
port,
};
process.component.inPorts.ports[portDef.port].attach(inSockets[port]);
}
try {
if (IP.isIP(value)) {
inSockets[port].post(value);
} else {
inSockets[port].post(new IP('data', value));
}
} catch (e) {
reject(e);
network.removeListener('process-error', onError);
network.removeListener('end', onEnd);
return;
}
}
}
}, reject);
});
}
/**
* @param {any} inputs
* @param {Network} network
* @returns {string}
*/
function getType(inputs, network) {
// Scalar values are always simple inputs
if (typeof inputs !== 'object' || !inputs) { return 'simple'; }
if (Array.isArray(inputs)) {
const maps = inputs.filter((entry) => getType(entry, network) === 'map');
// If each member if the array is an input map, this is a sequence
if (maps.length === inputs.length) { return 'sequence'; }
// Otherwise arrays must be simple inputs
return 'simple';
}
// Empty objects can't be maps
const keys = Object.keys(inputs);
if (!keys.length) { return 'simple'; }
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
if (!network.graph.inports[key]) { return 'simple'; }
}
return 'map';
}
/**
* @param {any} inputs
* @param {string} inputType
* @param {Network} network
* @returns {InputMap}
*/
function prepareInputMap(inputs, inputType, network) {
// Sequence we can use as-is
if (inputType === 'sequence') { return inputs; }
// We can turn a map to a sequence by wrapping it in an array
if (inputType === 'map') { return [inputs]; }
// Simple inputs need to be converted to a sequence
let inPort = Object.keys(network.graph.inports)[0];
if (!inPort) {
return {};
}
// If we have a port named "IN", send to that
if (network.graph.inports.in) {
inPort = 'in';
}
/** @type {InputMap} */
const map = {};
map[inPort] = inputs;
return [map];
}
/**
* @param {Array<IP>} values
* @param {AsCallbackOptions} options
* @returns {Array<any>}
*/
function normalizeOutput(values, options) {
if (options.raw) { return values; }
/** @type {Array<any>} */
const result = [];
/** @type {Array<any>|null} */
let previous = null;
let current = result;
values.forEach((packet) => {
if (packet.type === 'openBracket') {
previous = current;
current = [];
previous.push(current);
}
if (packet.type === 'data') {
current.push(packet.data);
}
if (packet.type === 'closeBracket') {
current = /** @type {Array<any>} */ (previous);
}
});
if (result.length === 1) {
return result[0];
}
return result;
}
/**
* @param {OutputMap} outputs
* @param {string} resultType
* @param {AsCallbackOptions} options
*/
function sendOutputMap(outputs, resultType, options) {
// First check if the output sequence contains errors
const errors = outputs.filter((map) => map.error != null).map((map) => map.error);
if (errors.length) {
return Promise.reject(normalizeOutput(errors, options));
}
if (resultType === 'sequence') {
return Promise.resolve(outputs.map((map) => {
/** @type {Object<string, any|IP>} */
const res = {};
Object.keys(map).forEach((key) => {
const val = map[key];
if (options.raw) {
res[key] = val;
return;
}
res[key] = normalizeOutput([val], options);
});
return res;
}));
}
// Flatten the sequence
/** @type {Object<string, Array<any|IP>>} */
const mappedOutputs = {};
outputs.forEach((map) => {
Object.keys(map).forEach((key) => {
const val = map[key];
if (!mappedOutputs[key]) {
mappedOutputs[key] = [];
}
mappedOutputs[key].push(val);
});
});
const outputKeys = Object.keys(mappedOutputs);
const withValue = outputKeys.filter((outport) => mappedOutputs[outport].length > 0);
if (withValue.length === 0) {
// No output
return Promise.resolve(null);
}
if ((withValue.length === 1) && (resultType === 'simple')) {
// Single outport
return Promise.resolve(normalizeOutput(mappedOutputs[withValue[0]], options));
}
/** @type {Object<string, any|IP>} */
const result = {};
Object.keys(mappedOutputs).forEach((port) => {
const packets = mappedOutputs[port];
result[port] = normalizeOutput(packets, options);
});
return Promise.resolve(result);
}
/**
* @callback ResultCallback
* @param {Error | null} err
* @param {any} [output]
* @returns {void}
*/
/**
* @callback NetworkAsCallback
* @param {any} input
* @param {ResultCallback} callback
* @returns void
*/
/**
* @callback NetworkAsPromise
* @param {any} input
* @returns {Promise<any>}
*/
/**
* @callback NetworkCallback
* @param {Network} network
* @returns void
*/
/**
* @param {Graph | string} component - Graph or component to load
* @param {Object} options
* @param {string} [options.name] - Name for the wrapped network
* @param {ComponentLoader} [options.loader] - Component loader instance to use, if any
* @param {string} [options.baseDir] - Project base directory for component loading
* @param {Object} [options.flowtrace] - Flowtrace instance to use for tracing this network run
* @param {NetworkCallback} [options.networkCallback] - Access to Network instance
* @param {boolean} [options.raw] - Whether the callback should operate on raw noflo.IP objects
* @returns {NetworkAsPromise}
*/
export function asPromise(component, options) {
if (!component) {
throw new Error('No component or graph provided');
}
options = normalizeOptions(options, component);
return (inputs) => prepareNetwork(component, options)
.then((network) => {
if (options.networkCallback) {
options.networkCallback(network);
}
const resultType = getType(inputs, network);
const inputMap = prepareInputMap(inputs, resultType, network);
return runNetwork(network, inputMap)
.then((outputMap) => sendOutputMap(outputMap, resultType, options));
});
}
/**
* @param {AsCallbackComponent} component - Graph or component to load
* @param {AsCallbackOptions} options
* @returns {NetworkAsCallback}
*/
export function asCallback(component, options) {
const promised = asPromise(component, options);
return (inputs, callback) => {
promised(inputs)
.then((output) => {
callback(null, output);
}, callback);
};
}