streamline

Asynchronous Javascript for dummies

streamline.js

streamline.js is a language tool to simplify asynchronous Javascript programming.

Instead of writing hairy code like:

function archiveOrders(datecb) {
  db.connect(function(errconn) {
    if (err) return cb(err);
    conn.query("select * from orders where date < ?", [date], function(errorders) {
      if (err) return cb(err);
      helper.each(orders, function(ordernext) {
        conn.execute("insert into archivedOrders ...", [order.id, ...], function(err) {
          if (err) return cb(err);
          conn.execute("delete from orders where id=?", [order.id], function(err) {
            if (err) return cb(err);
            next();
          });
        });
      }, function() {
        console.log("orders have been archived");
        cb();
      });
    });
  });
}

you write:

function archiveOrders(date_) {
  var conn = db.connect(_);
  conn.query("select * from orders where date < ?", [date], _).forEach_(_, function(_order) {
    conn.execute("insert into archivedOrders ...", [order.id, ...], _);
    conn.execute("delete from orders where id=?", [order.id], _);
  });
  console.log("orders have been archived");
}

and streamline transforms the code and takes care of the callbacks!

No control flow APIs to learn! You just have to follow a simple rule:

Replace all callbacks by an underscore and write your code as if all functions were synchronous.

Streamline is not limited to a subset of Javascript. You can use all the features of Javascript in your asynchronous code: conditionals, loops, try/catch/finally blocks, anonymous functions, chaining, this, etc.

Streamline also provides futures, and asynchronous variants of the EcmaScript 5 array functions (forEach, map, etc.).

Installation

NPM, of course:

npm install streamline -g

Warning: you may get errors when installing streamline versions >= 0.10.11 because fibers and galaxy are now installed as optional packages and they are not compatible with all versions of node.js. But these packages are optional and streamline itself should install fine. Just check these dependencies if you plan to use the fibers or generators modes.

The -g option installs streamline globally. You can also install it locally, without -g but then the _node and _coffee commands will not be in your default PATH.

Note: If you encounter a permission error when installing on UNIX systems, you should retry with sudo.

Cool demo

http://coolwanglu.github.io/vim.js/web/vim.html (emscripten + streamline.js + @coolwanglu's magic touch).

Hello World

Streamline modules have ._js or ._coffee extensions and you run them with the _node or _coffee loader.

Javascripters:

$ cat > hello._js
console.log('hello ...');
setTimeout(_, 1000);
console.log('... world');
^D
$ _node hello

Coffeescripters:

$ cat > hello._coffee
console.log 'hello ...'
setTimeout _, 1000
console.log '... world'
^D
$ _coffee hello

You can also create standalone shell utilities:

$ cat > hello._js
#!/usr/bin/env _node 
console.log('hello ...');
setTimeout(_, 1000);
console.log('... world');
^D
$ chmod +x hello._js
$ ./hello._js

or:

$ cat > hello._coffee
#!/usr/bin/env _coffee 
console.log 'hello ...'
setTimeout _, 1000
console.log '... world'
^D
$ chmod +x hello._coffee
$ ./hello._coffee

Compiling and writing loaders

You can also set up your code so that it can be run directly with node or coffee. You have two options here:

The first one is to compile your source with _node -c or _coffee -c:

$ _node -c .

This command compiles all the *._js and *._coffee source files in the current directory and its sub-directories. It generates *.js files that you can run directly with node.

The second one is to create your own loader with the register API. See the loader example for details.

Compiling will give you the fastest startup time because node will directly load the compiled *.js files but the register API has a cache option which comes close and the loader saves you a compilation pass.

Browser-side use

You have three options to use streamline in the browser:

  • The first one is to compile the source with _node --standalone -c. The compiler generates vanilla Javascript code that you can load with <script> directives in an HTML page. See the eval unit test for an example.
  • You can also transform the code in the browser with the transform API. All the necessary JS code is available as a single lib/callbacks/transform-all.js file. See the streamlineMe example.
  • A third option is to use the streamline-require infrastructure. This is a very efficient browser-side implementation of require that lets you load streamlined modules as well as vanilla Javascript modules in the browser.

Generation options

Streamline gives you the choice between generating regular callback-based asynchronous code, generating code that takes advantage of the fibers library, or generating code for JavaScript generators.

The callback option produces code that does not have any special runtime dependencies.

The fibers option produces simpler code but requires that you install the fibers library (easy: npm install fibers). This option gives superior development experience: line numbers and comments are preserved in the transformed code; you can step with the debugger through asynchronous calls without having to go through complex callbacks, etc.

The fibers option can be activated by passing the --fibers option to the _node command or by setting the fibers option when registering streamline (see the streamline.register(options) function.

The generators option produces code for harmony generators. It uses the galaxy module as runtime. It requires node.js version >= 0.11.4 or an experimental browser (latest Chrome Canary). This options produces code which is similar to what you get with the fibers option, just a bit heavier because of the yield keywords.

The generators option can be activated by passing the --generators option to the _node command or by setting the ganerators option when registering streamline. If you run it with a loader you have to pass the --harmony option to node.

There are also fast variants of the fibers and generators options. See below.

Interoperability with standard node.js code

You can call standard node functions from streamline code. For example the fs.readFile function:

function lineCount(path_) {
  return fs.readFile(path, "utf8", _).split('\n').length;
}

You can also call streamline functions as if they were standard node functions. For example, the lineCount function defined above can be called as follows from non-streamlined modules:

lineCount("README.md", function(errresult) {
  if (err) return console.error("ERROR: " + err.message);
  console.log("README has " + result + " lines.");
});

And you can mix streamline functions, classical callback based code and synchrononous functions in the same file. Streamline only transforms the functions that have the special _ parameter.

Note: this works with all transformation options. Even if you use the fibers option, you can seamlessly call standard callback based node APIs and the asynchronous functions that you create with streamline have the standard node callback signature.

Interoperability with Promises

Streamline also provides seamless interoperability with Promise libraries, in both directions.

First, you can consume promises from streamline code, by passing two underscores to their then method:

function myStreamlineFunction(p1, p2, _) {
  var result = functionReturningAPromise(p1, p2).then(_, _);
  // do something with result
}

Note: if the promise fails the error will be propagated as an exception and you can catch it with try/catch.

And you can also consume libraries implemented with streamline as if they had been implemented with promises. All you have to do is omit the _ parameter when calling streamlined functions and you will get a promise in return.

function callingStreamlineAsPromise(p1, p2) {
  var p = myStreamlineFunction(p1, p2);
  p.then(function(result) {
    // do something with result
  }, function(err) {
    // handle error
  });
}

Note: you can also pass null or undefined as callback. This is useful when _ is not the last parameter.

Promise interoperability is not enabled by default but you can enable it easily:

  • If you start your program with _node or _coffee, just pass the --promise option.
  • If you start it with a loader (see above), just set the promise option to true in your streamline.register(options) call.

Streamline will use the JavaScript built-in Promise class by default if available (node v11.13 and up). If this built-in class is not available it will try to load the es6-promise module instead (you should install it with npm install es6-promise).

Note: the loader also gives you the option to pick a promise library of your choice (but reasonably compliant with ES6 specs). To do this, set the promise option to the name of your promise library, instead of true.

Futures

Streamline also provides futures. Futures are like promises, without all the bells and whistles. They let you parallelize I/O operations in a very simple manner. They are always bundled with streamline and they have a very simple API.

If you pass !_ instead of _ when calling a streamline function, the function returns a future. The future is just a regular node.js asynchronous function that you can call later to obtain the result. Here is an example:

function countLines(path_) {
  return fs.readFile(path, "utf8", _).split('\n').length;
}
 
function compareLineCounts(path1path2_) {
  // parallelize the two countLines operations 
  var n1 = countLines(path1, !_);
  var n2 = countLines(path2, !_);
  // get the results and diff them 
  return n1(_) - n2(_);
}

In this example, countLines is called twice with !_. These calls start the fs.readFile asynchronous operations and return immediately two futures (n1 and n2). The return statement retrieves the results with n1(_) and n2(_) calls and computes their difference.

Futures are very flexible. In the example above, the results are retrieved from the same function, but you can also pass futures to other functions, store them in objects, call them to get the results from a different module, etc. You can also have several readers on the same future.

See the futures wiki page for details.

The flows module contains utilities to deal with futures. For example flows.collect to wait on an array of futures and flows.funnel to limit the number of concurrent operations.

Asynchronous Array functions

Streamline extends the Array prototype with asynchronous variants of the EcmaScript 5 forEach, map, filter, reduce, ... functions. These asynchronous variants are postfixed with an underscore and they take an extra _ argument (their callback too), but they are otherwise similar to the standard ES5 functions. Here is an example with the map_ function:

function dirLines(dir_) {
  return fs.readdir(dir, _).map_(_, function(_file) {
    return fs.readFile(dir + '/' + file, 'utf8', _).split('\n').length;
  });
}

Parallelizing loops is easy: just pass the number of parallel operations as second argument to the call:

function dirLines(dir_) {
  // process 8 files in parallel 
  return fs.readdir(dir, _).map_(_, 8, function(_file) {
    return fs.readFile(dir + '/' + file, 'utf8', _).split('\n').length;
  });
}

If you don't want to limit the level of parallelism, just pass -1.

See the documentation of the builtins module for details.

Exception Handling

Streamline lets you do your exception handling with the usual try/catch construct. The finally clause is also fully supported.

Streamline overrides the ex.stack getter to give you complete comprehensive stacktrace information. In callbacks and generators modes you get two stack traces:

  • the raw stack trace of the last callback.
  • the async stack trace of the asynchronous calls that caused the exception.

In fibers mode there is a single stack trace.

Note: you must install the companion galaxy-stack package to get async stack traces in generators mode.

Exception handling also works with futures and promises. If a future throws an exception before you try to read its result, the exception is memorized by the future and you get it at the point where your try to read the future's result. For example:

try {
  var n1 = countLines(badPath, !_);
  var n2 = countLines(goodPath, !_);
  setTimeout(_, 1000); // n1 fails, exception is memorized 
  return n1(_) - n2(_); // exception is thrown by n1(_) expression. 
} catch (ex) {
  console.error(ex.stack); // exception caught here 
}

Callbacks with multiple results

Some APIs return several results through their callback. For example:

request(options, function(errresponsebody) {
  // ... 
});

You can get all the results by passing [_] instead of _:

var results = request(options, [_]);
// will be better with destructuring assignment. 
var response = results[0];
var body = results[1];

Note: if you only need the first result you can pass _:

var response = request(options, _);

CoffeeScript support

CoffeeScript is fully supported.

You can even use language features which are not available in JavaScript. For example you can specify a default callback (see #218 for full details):

fn = (p1p2= (e) -> throw e if e) ->
  # do something 

Fast mode

Streamline has a fast mode which produces leaner and faster code at the expense of a few more keystrokes and a bit of extra care when writing the code.

This mode only applies to fibers and generators modes. It has no impact in callbacks mode.

For details see the fast mode wiki page

Stream Wrappers

Streamline also provides stream wrappers that simplify stream programming. These wrappers used to be included in the streamline npm package but they have now been moved to a separate ez-streams package.

Debugging with source maps

You can seamlessly debug streamline code thanks to JavaScript source maps. See this video for a quick demo.

To activate this feature, pass the --source-map options to _node or _coffee, or set the sourceMap option if you register via a loader.

Monitoring performance with flame graphs

Streamline code can be instrumented to produce flame graphs.

Two cool things about these flame graphs:

  • The pyramids correspond to stacks of asynchronous calls, not to raw V8 stacks.
  • The tool generates two graphs: a pure CPU graph and a graph that combines CPU and I/O delays.

See streamline-flamegraph for details.

Examples

The tutorial shows streamline.js in action on a simple search aggregator application.

The diskUsage examples show an asynchronous directory traversal that computes disk usage.

Online demo

You can see how streamline transforms the code by playing with the online demo.

Troubleshooting

Read the FAQ.

If you don't find your answer in the FAQ, post to the mailing list, or file an issue in GitHub's issue tracking.

Related Packages

The following package contains a complete yet simple streaming API for streamline.js:

  • ez-streams: easy streams, with array-like API (filter, map, reduce, foreach, some, every), transforms (json, csv and xml streaming parsers and formatters), parallelization, buffering, etc.

The following packages contain API wrappers for streamline.js:

  • streamline-fs: wrapper for node's fs module. It fixes the fs.exists call and it wraps the entire API for streamline's fast mode.
  • streamline-streams: historical streaming module for streamline. It implements the low level bits of the ez-steams module (see above). If you want a rich streaming APIs you should use ez-streams instead.
  • streamline-mongodb: wrappers for mongodb's native node.js driver. You only need this wrapper if you use the fast mode.

There are also some helper packages for express:

The following packages use streamline.js:

Resources

The tutorial and FAQ are must-reads for starters.

The API is documented here.

For support and discussion, please join the streamline.js mailing list.

Credits

See the AUTHORS file.

Special thanks to Marcel Laverdet who contributed the fibers implementation and to Geoffry Song who contributed source map support.

License

Streamline.js is licensed under the MIT license.