node-package-api
v1.0.0
Published
Create child processes and run pre-installed npm packages inside them
Downloads
1
Readme
node-package-api
a nodejs module for loading nodejs/js packages or modules in a separate process with full support for Linux, Mac and Windows.
Security Do not expose this package to the web without any security to prevent unauthorised access to your system.
Contents
Installation
npm install node-package-api
Config
{
"inactivityTimeout" : 3600000,
"paths" : false
}
| variable | type | default value | documentation |--------------|--------|---------------|--------------- | inactivityTimeout | Number | 3600000 | Timeout for destroying space after no request is recieved | paths | Array | module.paths | Array with strings where to look for installed packages
Instantiate
const config = require('./config');
const npa = require('./node-package-api')(config);
Tips
- The success parameter returns true if package and function is found and executed, not if the function itself returns an error or response.
- Only one space per node-package-api can be build.
- Multiple packages can be loaded per space/node-package-api.
- Packages must be installed on your system in a path specified in native module.paths or custom config.paths.
- Websockets in packages can be used. On first callback the result of opening the websocket is returned. Subsequent calls return the websocket data.
- Nested functions can be called by using a dot, like "websockets.websocketfunction".
- Any place where you want to insert a callback in a functions parameters, just add 'cb:' with the number of parameters it has in de parameters array, like ['param1', 'param2', 'cb:2', 'param3', 'cb:1']. All callbacks parameters will be returned in an array in the main npa.call callback as third parameter (called res in documentation).
- A space is the forked childprocess.
- Communicating with the childprocess is async, so make sure to wait for the package to be added before calling functions on it.
Functions
add adds specified package to the space (childprocess)
npa.add(package, parameters, callback);
npa.add('path', [], (req, status, res) => {
console.log('request:', req);
console.log('status: ', status);
console.log('response:', res);
});
request: { id: 472930,
package: 'path',
parameters: [],
type: 'add',
paths:
[ 'E:\\Projects\\Coding\\GitHub\\node-package-api\\node_modules',
'E:\\Projects\\Coding\\GitHub\\node_modules',
'E:\\Projects\\Coding\\node_modules',
'E:\\Projects\\node_modules',
'E:\\node_modules' ] }
status: true
response: Package is added to space
| variable | type | default | required |documentation |--------------|----------|---------|----------|--------------- | package | String | | yes | Name of package which should be added to space | parameters | Array | [] | no | Array where every index represents a parameter needed when instantiating the package. | callback | function | null | no | Callback function which will be called when the package is instantiated
call calls a function on the specified package
npa.call(package, function, parameters, promise, callback);
npa.call('path', 'dirname', [__dirname], false, (req, status, res) => {
console.log('request:', req);
console.log('status: ', status);
console.log('response:', res);
});
request: { id: 238795,
package: 'path',
parameters: [ 'E:\\Projects\\Coding\\GitHub\\node-package-api' ],
type: 'call',
function: 'dirname',
promise: false }
status: true
response: E:\Projects\Coding\GitHub
| variable | type | default | required |documentation |--------------|----------|---------|----------|--------------- | package | String | | yes | Package in space to call function on | function | String | | yes | Function to call on specified package | parameters | Array | [] | no | Array where every index represents a parameter needed when calling a package's function | promise | Boolean | false | no | Pass true if package's function returns a promise | callback | function | null | no | Callback function which will be called the functions result is returned from the space. This is not the callback for package's function itself.
If the packacge's function has callback(s), you need to insert them in the parameters array, like following example. The 'callback strings' are automatically converted to a callback with the number of wanted parameters specified behind the ':'. The main npa.call function cb will be called for every 'cb:' parameter passed.
// EXAMPLE
let parameters = [
'parameter1',
'parameter2',
'cb:2'
]
// OR
let parameters = [
'test_parameter_1',
'test_parameter_2',
'cb:4',
'test_parameter_3',
'cb:1'
]
destroy Destroys space/childprocess and thus all containing packages won't be available anymore
npa.destroy();
let destroyed = npa.destroy();
console.log(destroy);
ChildProcess {
_events:
[Object: null prototype] { internalMessage: [Function], message: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
_closesNeeded: 2,
_closesGot: 0,
connected: true,
signalCode: null,
exitCode: null,
killed: true,
spawnfile: 'C:\\Program Files\\nodejs\\node.exe',
_handle:
Process { onexit: [Function], pid: 15440, [Symbol(owner)]: [Circular] },
spawnargs: [ 'C:\\Program Files\\nodejs\\node.exe', './space.js' ],
pid: 15440,
stdin: null,
stdout: null,
stderr: null,
stdio: [ null, null, null, null ],
channel:
Pipe {
buffering: false,
pendingHandle: null,
onread: [Function],
sockets: { got: {}, send: {} } },
_channel: [Getter/Setter],
_handleQueue: null,
_pendingMessage: null,
send: [Function],
_send: [Function],
disconnect: [Function],
_disconnect: [Function] }
build creates new space/childprocess. Only when space is destroyed before (so there is no existing space yet)
npa.build();
let build = npa.build();
console.log(build);
ChildProcess {
_events:
[Object: null prototype] { internalMessage: [Function], message: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
_closesNeeded: 2,
_closesGot: 0,
connected: true,
signalCode: null,
exitCode: null,
killed: false,
spawnfile: 'C:\\Program Files\\nodejs\\node.exe',
_handle:
Process { onexit: [Function], pid: 8388, [Symbol(owner)]: [Circular] },
spawnargs: [ 'C:\\Program Files\\nodejs\\node.exe', './space.js' ],
pid: 8388,
stdin: null,
stdout: null,
stderr: null,
stdio: [ null, null, null, null ],
channel:
Pipe {
buffering: false,
pendingHandle: null,
onread: [Function],
sockets: { got: {}, send: {} } },
_channel: [Getter/Setter],
_handleQueue: null,
_pendingMessage: null,
send: [Function],
_send: [Function],
disconnect: [Function],
_disconnect: [Function] }
isKilled see if space is killed
npa.isKilled();
let isKilled = npa.isKilled();
console.log(isKilled);
false
resetInactivity reset inactivity timer to prevent killing of space after inactivity
npa.resetInactivity();
let resetInactivity = npa.resetInactivity();
console.log(resetInactivity);
Timeout {
_idleTimeout: 3600000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 5081,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: true,
[Symbol(asyncId)]: 30,
[Symbol(triggerId)]: 10 }