sonic-boom
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -10,2 +10,3 @@ 'use strict' | ||
var sonic = new SonicBoom(fd) | ||
var sonic4k = new SonicBoom(fd, 10000) | ||
@@ -21,2 +22,8 @@ setTimeout(doBench, 100) | ||
}, | ||
function benchSonic4k (cb) { | ||
sonic4k.once('drain', cb) | ||
for (var i = 0; i < 10000; i++) { | ||
sonic4k.write('hello world') | ||
} | ||
}, | ||
function benchCore (cb) { | ||
@@ -23,0 +30,0 @@ core.once('drain', cb) |
74
index.js
@@ -5,36 +5,8 @@ 'use strict' | ||
const EventEmitter = require('events') | ||
const reusify = require('reusify') | ||
const flatstr = require('flatstr') | ||
const inherits = require('util').inherits | ||
function Holder () { | ||
EventEmitter.call(this) | ||
this.sonic = null | ||
var that = this | ||
this.release = function (err) { | ||
var sonic = that.sonic | ||
if (err) { | ||
sonic.emit('error', err) | ||
return | ||
} | ||
pool.release(that) | ||
if (sonic._buf.length > 0) { | ||
actualWrite(sonic) | ||
} else if (sonic._ending === true) { | ||
actualClose(sonic) | ||
} else { | ||
sonic._writing = false | ||
sonic.emit('drain') | ||
} | ||
} | ||
} | ||
const pool = reusify(Holder) | ||
function SonicBoom (fd) { | ||
function SonicBoom (fd, minLength) { | ||
if (!(this instanceof SonicBoom)) { | ||
return new SonicBoom(fd) | ||
return new SonicBoom(fd, minLength) | ||
} | ||
@@ -48,2 +20,4 @@ | ||
this.minLength = minLength || 0 | ||
if (typeof fd === 'number') { | ||
@@ -64,4 +38,6 @@ this.fd = fd | ||
this._writing = false | ||
// start | ||
if (this._buf.length > 0) { | ||
var len = this._buf.length | ||
if (len > 0 && len > this.minLength) { | ||
actualWrite(this) | ||
@@ -73,2 +49,23 @@ } | ||
} | ||
this.release = (err) => { | ||
if (err) { | ||
this.emit('error', err) | ||
return | ||
} | ||
var len = this._buf.length | ||
if (this._buf.length > 0 && len > this.minLength) { | ||
actualWrite(this) | ||
} else if (this._ending === true) { | ||
if (len > 0) { | ||
actualWrite(this) | ||
} else { | ||
actualClose(this) | ||
} | ||
} else { | ||
this._writing = false | ||
this.emit('drain') | ||
} | ||
} | ||
} | ||
@@ -83,9 +80,16 @@ | ||
this._buf += data | ||
if (this._writing === false) { | ||
var len = this._buf.length | ||
if (this._writing === false && len > this.minLength) { | ||
actualWrite(this) | ||
} | ||
return this._buf.length < 16384 | ||
return len < 16384 | ||
} | ||
SonicBoom.prototype.end = function () { | ||
if (!this._writing && this._buf.length > 0 && this.fd >= 0) { | ||
actualWrite(this) | ||
this._ending = true | ||
return | ||
} | ||
if (this._writing === true) { | ||
@@ -114,7 +118,5 @@ this._ending = true | ||
function actualWrite (sonic) { | ||
const holder = pool.get() | ||
holder.sonic = sonic | ||
sonic._writing = true | ||
flatstr(sonic._buf) | ||
fs.write(sonic.fd, sonic._buf, 'utf8', holder.release) | ||
fs.write(sonic.fd, sonic._buf, 'utf8', sonic.release) | ||
sonic._buf = '' | ||
@@ -121,0 +123,0 @@ } |
{ | ||
"name": "sonic-boom", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Extremely fast utf8 only stream implementation", | ||
@@ -36,5 +36,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"flatstr": "^1.0.5", | ||
"reusify": "^1.0.3" | ||
"flatstr": "^1.0.5" | ||
} | ||
} |
@@ -37,3 +37,3 @@ # sonic-boom [![Build Status](https://travis-ci.org/mcollina/sonic-boom.svg?branch=master)](https://travis-ci.org/mcollina/sonic-boom) | ||
### SonicBoom(String|Number) | ||
### SonicBoom(String|Number, (minLength)) | ||
@@ -48,2 +48,5 @@ Creates a new instance of SonicBoom. | ||
The second argument is the minimum length of the internal buffer that is | ||
required before flushing. | ||
It will emit the `'ready'` event when a file descriptor is available. | ||
@@ -50,0 +53,0 @@ |
43
test.js
@@ -144,3 +144,3 @@ 'use strict' | ||
test('destroy', (t) => { | ||
t.plan(3) | ||
t.plan(4) | ||
@@ -155,4 +155,41 @@ const dest = file() | ||
const data = fs.readFileSync(dest, 'utf8') | ||
t.equal(data, 'hello world\n') | ||
fs.readFile(dest, 'utf8', function (err, data) { | ||
t.error(err) | ||
t.equal(data, 'hello world\n') | ||
}) | ||
}) | ||
test('minLength', (t) => { | ||
t.plan(7) | ||
const dest = file() | ||
const stream = new SonicBoom(dest, 4096) | ||
stream.on('ready', () => { | ||
t.pass('ready emitted') | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
t.ok(stream.write('something else\n')) | ||
var fail = t.fail | ||
stream.on('drain', fail) | ||
// bad use of timer | ||
// TODO refactor | ||
setTimeout(function () { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, '') | ||
stream.end() | ||
stream.on('finish', () => { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\nsomething else\n') | ||
}) | ||
}) | ||
}) | ||
}, 100) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11402
1
314
72
- Removedreusify@^1.0.3
- Removedreusify@1.0.4(transitive)