UNPKG

@chix/flow

Version:
103 lines 4.11 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var loader_1 = require("@chix/loader"); var packet_1 = require("../packet/packet"); var node_1 = require("../sandbox/node"); var compact = require("./compact"); var createPortBox_1 = require("./createPortBox"); var fix_1 = require("./fix"); var mockDependencyLoader = new loader_1.MockDependencyLoader(); function compile(nodeDefinition) { if (typeof nodeDefinition.fn !== 'string') { throw Error('Could not find function body `fn` at nodeDefinition.fn'); } var fn = nodeDefinition.fn; delete nodeDefinition.fn; var nodebox = new node_1.NodeBox(fix_1.fix(nodeDefinition.name)); var state = {}; var output = {}; var input = {}; if (nodeDefinition.ports.input) { var inputPorts = nodeDefinition.ports.input; if (Object.keys(inputPorts).length) { Object.keys(inputPorts).forEach(function (key) { input[key] = packet_1.Packet.create(true); }); } } nodebox.set('input', input); nodebox.set('$', input); nodebox.set('done', function () { }); nodebox.set('cb', function () { }); nodebox.set('state', state); if (nodeDefinition.dependencies && nodeDefinition.dependencies.npm) { nodebox.addDependencies(nodeDefinition.dependencies.npm, mockDependencyLoader); } if (nodeDefinition.expose) { nodebox.expose(nodeDefinition.expose); } nodebox.set('output', output); if (/output(\.[A-z]+)?\s+=/.test(fn) && /output\s*=\s*function/.test(fn) === false) { if (/output\s*=\s*\[/.test(fn)) { nodeDefinition.fn = nodebox.compile(compact.createBody(compact.parseBody(fn), nodeDefinition)); } else { nodebox.compile(fn); nodeDefinition.fn = nodebox.fn; } return nodeDefinition; } else { nodebox.compile(fn); nodebox.run(); if (nodeDefinition.ports.input) { Object.keys(nodeDefinition.ports.input).forEach(function (port) { var nodeboxOnInput = nodebox.on.input; if (!nodeboxOnInput) { throw Error('nodebox.on.input not defined'); } if (nodeboxOnInput[port]) { var pb = createPortBox_1.createPortBox(nodeDefinition, nodeboxOnInput[port].toString(), ('__' + port + '__').toUpperCase()); nodeDefinition.ports.input[port].fn = pb.fn; nodeDefinition.state = state; } }); } var preloadedNodeDefinition_1 = __assign(__assign({}, nodeDefinition), { on: {} }); ['start', 'shutdown'].forEach(function (key) { if (nodebox.on[key]) { var pb = createPortBox_1.createPortBox(preloadedNodeDefinition_1, nodebox.on[key].toString(), ('__on' + key + '__').toUpperCase()); pb.compile(); preloadedNodeDefinition_1.on[key] = pb.fn; } }); if (typeof nodebox.output === 'object' && nodebox.output.out) { preloadedNodeDefinition_1.fn = nodebox.fn; } else if (typeof nodebox.output === 'function') { preloadedNodeDefinition_1.fn = nodebox.output .toString() .replace(/function.*{/, '') .replace(/}.*$/, '') .trim() .replace(/cb\s*\(/g, 'output('); nodebox.clear(); preloadedNodeDefinition_1.fn = nodebox.compile(preloadedNodeDefinition_1.fn); } return preloadedNodeDefinition_1; } } exports.compile = compile; //# sourceMappingURL=compile.js.map