From 05d9799baad753e61f138fa3ea17a51e14ad2e24 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 21 Oct 2015 20:06:53 +0100 Subject: [PATCH] Fixed support for node v0.10 and v0.12 with corkable stream. --- package.json | 1 + test.js | 28 ++++++++++++++++++++++++++++ writeToStream.js | 13 ++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 13a0d6d..542ab35 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "dev-null": "^0.1.1", "faucet": "0.0.1", "pre-commit": "^1.1.1", + "readable-stream": "^2.0.2", "tape": "^4.2.0" }, "dependencies": { diff --git a/test.js b/test.js index a0b7c4c..3928c9a 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,7 @@ var test = require('tape') , mqtt = require('./') + , WS = require('readable-stream').Writable function testParseGenerate(name, object, buffer, opts) { test(name + ' parse', function(t) { @@ -851,3 +852,30 @@ testGenerateError('Invalid password', { , username: 'username' , password: 42 }) + +test('support cork', function (t) { + t.plan(9) + + var dest = WS() + , count = 0 + + dest._write = function (chunk, enc, cb) { + t.pass('_write called') + cb() + } + + mqtt.writeToStream({ + cmd: 'connect' + , retain: false + , qos: 0 + , dup: false + , length: 18 + , protocolId: 'MQIsdp' + , protocolVersion: 3 + , clean: false + , keepalive: 30 + , clientId: 'test' + }, dest) + + dest.end() +}) diff --git a/writeToStream.js b/writeToStream.js index a2c58c5..00ddcc6 100644 --- a/writeToStream.js +++ b/writeToStream.js @@ -5,11 +5,22 @@ var protocol = require('./constants') , empty = new Buffer(0) , zeroBuf = new Buffer([0]) , numCache = require('./numbers') + , nextTick = process.nextTick + +if (process.version.indexOf('v0.1') === 0) { + (function () { + nextTick = function tickShim (func, stream) { + return function () { + return func(stream) + } + } + })() +} function generate(packet, stream) { if (stream.cork) { stream.cork() - process.nextTick(uncork, stream) + nextTick(uncork, stream) } switch (packet.cmd) {