npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

  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
  }

| 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

  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

| 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 }