Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 15, 2012 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4299905 to your computer and use it in GitHub Desktop.
Save isaacs/4299905 to your computer and use it in GitHub Desktop.
var Writable = require('stream').Writable;
var util = require('util');
// a lowlevel stderr writer
var TTY = process.binding('tty_wrap').TTY;
var handle = new TTY(2, false);
util.inherits(W, Writable);
function W(opts) {
Writable.call(this, opts);
}
W.prototype._write = function(chunk, cb) {
var str = util.format('chunk %j', chunk.toString()) + '\n';
var req = handle.writeUtf8String(str);
// here's the problem.
// it needs to tell the Writable machinery that it's ok to write
// more, but that the current buffer length is handle.writeQueueSize
req.oncomplete = afterWrite
req.cb = cb;
console.error('before write', handle.writeQueueSize);
}
function afterWrite(status, handle, req) {
console.error('afterWrite', status, handle.writeQueueSize);
req.cb();
}
var w = new W
w.write('foo');
w.write('bar');
w.write('baz');
process.exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment