1.0.0 • Published 4 years ago

node-package-api v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

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

  1. Installation
  2. Config
  3. Instantiate
  4. Tips
  5. Functions
    1. add
    2. call
    3. destroy
    4. build
    5. isKilled
    6. resetInactivity

Installation

npm install node-package-api

Config

  {
    "inactivityTimeout"     :     3600000,
    "paths"                 :     false
  }
variabletypedefault valuedocumentation
inactivityTimeoutNumber3600000Timeout for destroying space after no request is recieved
pathsArraymodule.pathsArray with strings where to look for installed packages

Instantiate

const config = require('./config');
const npa = require('./node-package-api')(config);

Tips

  1. The success parameter returns true if package and function is found and executed, not if the function itself returns an error or response.
  2. Only one space per node-package-api can be build.
  3. Multiple packages can be loaded per space/node-package-api.
  4. Packages must be installed on your system in a path specified in native module.paths or custom config.paths.
  5. Websockets in packages can be used. On first callback the result of opening the websocket is returned. Subsequent calls return the websocket data.
  6. Nested functions can be called by using a dot, like "websockets.websocketfunction".
  7. 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).
  8. A space is the forked childprocess.
  9. 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
variabletypedefaultrequireddocumentation
packageStringyesName of package which should be added to space
parametersArray[]noArray where every index represents a parameter needed when instantiating the package.
callbackfunctionnullnoCallback 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
variabletypedefaultrequireddocumentation
packageStringyesPackage in space to call function on
functionStringyesFunction to call on specified package
parametersArray[]noArray where every index represents a parameter needed when calling a package's function
promiseBooleanfalsenoPass true if package's function returns a promise
callbackfunctionnullnoCallback 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 }