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

machine

v15.2.3

Published

Build functions in standardized containers.

Downloads

235,998

Readme

machine (runner)

Gitter

A runner for functions written in JavaScript. Machines are atomic, context-free bits of code which conform to the machine specification: an open standard for functions and subroutines.

Chances are you don't need to use this module directly- see About This Module below for info.

Installation   NPM version Build Status

With node installed:

# Get the machine runner
$ npm install machine --save

Usage

.build()

Call Machine.build() (or just Machine()) with a machine definition to build a callable function:

const Machine = require('machine');

const callable = Machine({
  identity: 'do-something',
  inputs: {
    foo: { type: 'string', required: true }
  },
  fn: function(inputs, exits){
    let result = `The result, based on ${inputs.foo}.`;
    return exits.success(result);
  }
});//ƒ

let result = await callable({foo: 'bar'});
console.log(result);

// => 'The result, based on "bar"'.
Machine definitions

A machine definition is a dictionary (plain JavaScript object) that describes the implementation of a function, according to the specification.

By itself, a machine definition can be programmatically parsed for things like generating documentation and tests, performing static analysis, enabling IDE plugins with features such as code completion and high-level linting, inferring user interface elements such as forms, and much more.

But to actually use a machine definition in your code, you need some kind of runner. That's what this module is for:


const Machine = require('machine');
const def = { /* … */ };
const callable = Machine(def);
Callables

A "callable" (or "wet machine") is a function returned after building a machine. It has standard usage out of the box (unless you used buildWithCustomUsage()).

let argins = { /*…*/ };
let result = await callable(argins);

It also has some additional custom methods, available as properties:

  • callable.getDef()
  • callable.customize()
Deferreds

Invoking a "callable" returns a Deferred instance, that can be used with async/await:

let deferred = callable(argins);
let result = await deferred;

It also supports some other methods, such as:

  • deferred.log()
  • deferred.then()
  • deferred.catch()
  • deferred.exec()
  • deferred.switch()
  • deferred.meta()

See parley for more information.

.pack()

Call Machine.pack() to construct a "machinepack", a JavaScript (usually Node.js) package of callable functions:

const mp = Machine.pack({
  dir: __dirname,
  pkg: require('./package.json')
});
Machinepacks

Machinepacks are simple dictionaries (plain JavaScript objects) that expose a set of "callables" as methods.

They also support one built-in method:

  • mp.customize()

.VERSION

The current version of the machine runner.

console.log(Machine.VERSION);

// => '15.0.0-3'

.getMethodName()

Get the proper method name for a machine definition.

const methodName = Machine.getMethodName({
  identity: 'do-something-cool',
  friendlyName: 'Do summthin real neat',
  description: 'Do something quite skillful and well-balanced.',
  inputs: {/*…*/},
  exits: {/*…*/},
  fn: function(inputs, exits) { /*…*/ return exits.success(); }
});//ƒ

console.log('.'+methodName+'()');

//=> '.doSomethingCool()'

.buildWithCustomUsage()

Experimental.

const customCallable = Machine.buildWithCustomUsage({
  arginStyle: 'serial',
  execStyle: 'immediate',
  def: {
    identity: 'do-something',
    sync: true,
    args: ['foo', 'bar'],
    inputs: {
      foo: { type: 'string', required: true },
      bar: { type: 'number' },
    },
    fn: function(inputs, exits){
      let result = `The result, based on ${inputs.foo}`;
      if (inputs.bar) {
        result += ` and ${inputs.bar}.`;
      }
      return exits.success(result);
    }
  }
});//ƒ

let result = customCallable('abc', 123);
console.log(result);

// => 'The result, based on "abc" and "123"'.

Benchmarks

As of morning, Friday, August 18, 2017, without any build-time normalization of definitions, and with runtime data type validation disabled:

∑ NODE_ENV=production npm run bench

> [email protected] bench /Users/mikermcneil/code/machine
> node ./node_modules/mocha/bin/mocha -R dot --recursive -b test/benchmarks/



   o

       •
      o                  .
       •                •
        •                •
                •       o
                            •        o
 o   •              •          o   •
      o              o         •
  •  •      •       •      •    •
           •      •              o
  •    b e n c h m a r k s      •
   •        •
 •                        ___  •
    • o •    •      •    /o/•\_   •
       •   •  o    •    /_/\ o \_ •
       o    O   •   o • •   \ o .\_
          •       o  •       \. O  \

 • sanity_check x 794,661 ops/sec ±0.47% (85 runs sampled)
 • build_very_simple_machine x 152,975 ops/sec ±0.54% (84 runs sampled)
 • build_machine_with_inputs_and_exits_but_nothing_crazy x 129,236 ops/sec ±0.56% (84 runs sampled)
 • build_machine_with_inputs_and_exits_that_have_big_ole_exemplars x 122,899 ops/sec ±0.91% (82 runs sampled)
 • build_machine_with_crazy_numbers_of_inputs_and_exits x 81,030 ops/sec ±0.77% (81 runs sampled)
 • build_machine_with_crazy_numbers_of_inputs_and_exits_and_is_cacheable x 79,177 ops/sec ±0.75% (85 runs sampled)
 • build_machine_with_crazy_numbers_of_inputs_and_exits_with_huge_exemplars x 89,338 ops/sec ±0.64% (83 runs sampled)
 • build_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars x 76,974 ops/sec ±0.67% (84 runs sampled)
Fastest is sanity_check
Slowest is build_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars

  ․ • sanity_check x 755,851 ops/sec ±0.77% (85 runs sampled)
 • exec_very_simple_machine x 37,835 ops/sec ±3.80% (69 runs sampled)
 • exec_machine_with_inputs_and_exits_but_nothing_crazy x 32,015 ops/sec ±3.66% (62 runs sampled)
 • exec_machine_with_inputs_and_exits_that_have_big_ole_exemplars x 30,895 ops/sec ±3.49% (62 runs sampled)
 • exec_machine_with_crazy_numbers_of_inputs_and_exits x 22,669 ops/sec ±2.97% (72 runs sampled)
 • exec_machine_with_crazy_numbers_of_inputs_and_exits_and_is_cacheable x 21,708 ops/sec ±3.04% (69 runs sampled)
 • exec_machine_with_crazy_numbers_of_inputs_and_exits_with_huge_exemplars x 23,585 ops/sec ±3.41% (69 runs sampled)
 • exec_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars x 21,805 ops/sec ±2.41% (65 runs sampled)
Fastest is sanity_check
Slowest is exec_machine_with_crazy_numbers_of_inputs_and_exits_and_is_cacheable,exec_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars
․ • sanity_check x 712,777 ops/sec ±1.00% (81 runs sampled)
 • execSync_very_simple_machine x 36,050 ops/sec ±4.42% (69 runs sampled)
 • execSync_machine_with_inputs_and_exits_but_nothing_crazy x 30,311 ops/sec ±2.72% (57 runs sampled)
 • execSync_machine_with_inputs_and_exits_that_have_big_ole_exemplars x 29,416 ops/sec ±3.81% (62 runs sampled)
 • execSync_machine_with_crazy_numbers_of_inputs_and_exits x 21,353 ops/sec ±3.84% (69 runs sampled)
 • execSync_machine_with_crazy_numbers_of_inputs_and_exits_and_is_cacheable x 21,188 ops/sec ±3.76% (59 runs sampled)
 • execSync_machine_with_crazy_numbers_of_inputs_and_exits_with_huge_exemplars x 22,878 ops/sec ±3.49% (72 runs sampled)
 • execSync_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars x 21,315 ops/sec ±2.43% (69 runs sampled)
Fastest is sanity_check
Slowest is execSync_machine_with_crazy_numbers_of_inputs_and_exits_and_is_cacheable,execSync_machine_with_crazy_numbers_of_inputs_and_exits,execSync_machine_with_crazy_numbers_of_inputs_and_exits_with_ref_exemplars

About this module

Before you read any further, let's stop and make sure you're in the right place. The documentation in this README file is for low-level usage of machine, the JavaScript machine runner. You don't need to use this module directly unless you're building machines.

You can find more information about the node-machine project on http://node-machine.org. There you'll also find a short video from the introductory talk at dotjs.eu, an up-to-date list of all available machines on NPM, and standardized documentation pages with code examples you can copy and paste into your Node.js app (e.g. Github.createRepo()).

Building a machinepack? Here are some tips:

  • Start with tutorial for implementors
  • Join the newsgroup for the machine specification to get help from other machine implementors
  • Don't forget to add the "repository" key to your package.json file so folks can find your source code (this enables the View Source button in the generated documentation on node-machine.org)
  • Hit up @mikermcneil on Twitter and let me know what you're working on!

OK what is this? How does it work?

This is a low-level module for building, configuring, and running machines.

Normal users of machines won't interact with this module directly very often-- however it is a dependency of every machinepack. Its full list of responsibilities includes exposing the conventional machine usage, a .exec() helper (for familiarity with Waterline), as well as validating input expectations, coercing return values from exits, and more.

The .build() method accepts a machine definition object and returns a new ready-to-use machine instance function. The .pack() method accepts a filesystem path and returns a ready-to-use machinepack obtained by requiring the module located at the path, loading its machine definitions into live machines (calling .build() on each definition), and validating that everything is up to spec.

So when you require a machinepack from NPM like so:

var Github = require('machinepack-github');

what's actually happening is that the index.js file in the machinepack module is calling .pack() and returning an object of ready-to-go machine instances.

Where would I use this module directly?

There are only two use-cases for requiring this module directly:

1. Your machinepack's boilerplate index.js file

Note that this is taken care of for you if you used the Yeoman generator to create your machinepack.

If you're implementing a machinepack, you'll need to use this module to .pack() your machines in your index.js file. Here's an example of an index.js file (this example happens to come from machinepack-urls- your pack's index.js file should always look the same, no matter what).

2. A machine which uses another machine from the same pack

Normally, if you want to use a machine from inside of one of your machines, you just install and require the other machinepack in your pack and use it just like you would in app-level code. But if you want to use another machine in the same pack, or you want the machine to call itself recursively, you should use this module directly. You can read more information on this in the FAQ for implementors.

Can I use this module outside of a machinepack?

You can use it anywhere you like! For instance, you might want to implement a one-off machine in your app, perhaps to take advantage of caching or type-checking this module provides.

If you're using Sails, check out sails-hook-machines, a hook which allows you to use custom closed-source machines in your Sails app by dropping files into the api/machines/ folder.

Contributing

If you're interested in contributing to the machine specification, please request to join the project's Google Group and introduce yourself to the rest of the core team. In the mean time, you can check out the tests for information on how to use all the lower-level features of this module. There is also a guide for direct usage of this module in docs/DIRECT_USAGE.md. Note that you can run the tests for this module using npm test. To re-generate the recursive dependency report (via the licensing module by 3rdEden), run npm run licensing.

Issue Submission

Make sure you've read the issue submission guidelines from Sails before opening a new issue - the node-machine project uses the same rules.

Click here to search/post issues.

License

MIT © 2014 Mike McNeil; © 2015-2017 The Sails Company

Like the Sails framework, this package is available under the MIT license.

image_squidhome@2x.png