Skip to content

Instantly share code, notes, and snippets.

@iarna
iarna / example.js
Created May 15, 2015 06:43
Hoisting + CPS example
// This the code in the npm multi-stage branch that removes a package directory.
function removeDir (pkg, log, next) {
var modpath = path.join(path.dirname(pkg.path), '.' + path.basename(pkg.path) + '.MODULES')
// Preserve any modules installed in this package, if any
fs.rename(path.join(pkg.path, 'node_modules'), modpath, unbuildPackage)
function unbuildPackage (renameEr) {
npm.commands.unbuild(pkg.path, true, renameEr ? andRemoveEmptyParents(pkg.path) : moveModulesBack)
@iarna
iarna / gist:3de76d22abbe2aedf3ed
Created March 27, 2015 06:50
Perl style examples
LINE:
for (;;) {
statements;
last LINE if $foo;
next LINE if /^#/;
statements;
}
function thingy(cb)
doAsyncThing("arg1", "arg2", "argn", andWarnOnError(log, cb))
}
function andWarnOnError (log, cb) {
return function (er, result) {
if (er) {
log.warn("install", "Couldn't install optional dependency:", er.message)
log.verbose("install", er.stack)
}
@iarna
iarna / index.js
Created October 29, 2014 09:40
progress tracker
"use strict"
var EventEmitter = require("events").EventEmitter
var util = require("util")
var Tracker = exports.Tracker = function (done,todo) {
this.workDone = done || 0
this.workTodo = todo || 0
}
util.extend(Tracker, EventEmitter)
  1. Create a lockname.pid file.
  2. Readdir, looking for lockname.* files:
  3. If found file pid is < our pid, and it is alive/not stale, FAIL and unlink our lockfile
  4. If we make it through, we have the lock, continue.
var stream = require('stream');
var DuplexCombination = require('duplex-combination');
function dosomething() {
var input = new stream.PassThrough();
var output = new stream.PassThrough();
var combined = new DuplexCombination(output,input)
output.write('two');
input.pipe(output);
var stream = require('stream');
function dosomething() {
var out = new stream.PassThrough();
out.write('two');
return out;
}
var source = new stream.PassThrough()
@iarna
iarna / gearman timings.md
Last active August 29, 2015 13:59
Relative gearman speeds with varous combinations of client/worker and server.

We're just issuing an echo here, which is the simplest command in the Gearman protocol. You send an echo packet to the server, the server responds back with what you sent it. The numbers here give a sense of how much overhead different approaches are adding.

The test here is with the word "ping". Substantially larger payloads are likely to be interesting, along with measures on time-to-first packet in the reply, as the node.js implementation is streaming end-to-end and will start replying before the original echo request actually completes. This also means that the entire echo body never need be held in memory.

  1. Client, server in one process, no packet encode/decode:

    echo: 4.07 wallclock secs @ 12289.63/s (n=50000)

  2. Client, server in one process, stream stubs:

@iarna
iarna / example.js
Last active August 29, 2015 13:58
Round tripping full Gearman protocol transform filters
"use strict";
var streamify = require('stream-array');
var GearmanPacket = require('./gearman-packet');
var through = require('through2');
// Showing off the round tripping
// Below is are the example packets from the Gearman protocol docs, plus an admin command
// Notice how you can intermix these all without any issues
streamify([
@iarna
iarna / fail1.js
Created March 3, 2014 21:28
Error domain bug in Node 0.10 (seems fixed in 0.11)
var soon = process.nextTick;
require('domain').create()
.on('error', function(E){ console.log("All is well soon") })
.run(function() {
soon(function(){ throw new Error() })
});
// We expect "All is well soon" to be logged
// Instead we get:
/*