what's up with node 0.12

Bert Belder
(aka @piscisaureus)


node and libuv

Streams3

  • Problem: streams1 and streams2 duality
  • define what happens when modes are mixed
  • A data event is fired every time read() is called
  • resume() calls read repeatedly, pause() stops that
  • pipe(dest) and on('data', fn) resume the stream

VM improvements

  • Problem: difficult to create a javascript sandbox in node
  • Problem: node APIs assume a single context
  • Contextify pulled into core
  • Node APIs are made context-aware
  • A step towards full isolate support

Cluster round-robin

  • Problem: the 'cluster' module doesn't distribute incoming connections evenly.
  • Before: workers compete for new connections
  • After: master accepts connections, hands them out to workers
            
var cluster = require('cluster');

// This is the default:
cluster.schedulingPolicy = cluster.SCHED_RR;
// .. or Set this before calling other cluster functions.
cluster.schedulingPolicy = cluster.SCHED_NONE;

// Spawn as many workers as there are CPUs in the system.
for (var i = 0, n = os.cpus().length; i < n; i += 1)
  cluster.fork();
            
          

spawnSync / execSync

  • Problem: people go to great lengths to run a shell command and block
  • Add execSync and spawnSync APIs
  • Requires a nested event loop
  • Not landed yet, but the hard work is done
            
var child_process = require('child_process');
var fs = require('fs');

function execSync(command) {
  // Run the command in a subshell
  child_process.exec(command + ' 2>&1 1>output && echo done! > done');

  // Block the event loop until the command has executed.
  while (!fs.existsSync('done')) {
    // Do nothing
  }

  // Read the output
  var output = fs.readFileSync('output');

  // Delete the output and done files
  fs.unlinkSync('output');
  fs.unlinkSync('done');

  return output;
}
            
          
            
var spawnSync = require('child_process').spawnSync;

var result = spawnSync('cat',
                       ['-'],
                       { input: 'hello world!',
                         encoding: 'utf8' });

console.log('exit code: %d', result.exitCode);
console.log('output: %s', result.stdout);
console.log('error: %s', resullt.stderr);
            
          

Profiling APIs

  • Problem: profiling can only be enabled on startup
  • Problem: heap dumps requires a native addon
  • Add APIs to enable profiling
  • Make (heap) profiling tabs work in node-inspector
            
var v8 = require('v8');

v8.cpuProfiler.setSamplingInterval(1);
v8.cpuProfiler.start();

v8.on('gc', function() {
  console.log('Garbage collection just happened!');
});
            
          

When..?

Questions?




Free e-book: ”Node.js in Action“
http://strongloop.com/promotions/cultivate-meetup-sf/