sonic-boom
Advanced tools
Comparing version 2.3.2 to 2.4.0
@@ -91,3 +91,3 @@ 'use strict' | ||
let { fd, dest, minLength, sync, append = true, mkdir, retryEAGAIN } = opts || {} | ||
let { fd, dest, minLength, maxLength, sync, append = true, mkdir, retryEAGAIN } = opts || {} | ||
@@ -108,2 +108,3 @@ fd = fd || dest | ||
this.minLength = minLength || 0 | ||
this.maxLength = maxLength || 0 | ||
this.sync = sync || false | ||
@@ -226,2 +227,6 @@ this.append = append || false | ||
if (this.maxLength && len > this.maxLength) { | ||
this.emit('drop', data) | ||
return this._len < this._hwm | ||
} | ||
if (!this._writing && len > MAX_WRITE) { | ||
@@ -228,0 +233,0 @@ bufs.push(data) |
{ | ||
"name": "sonic-boom", | ||
"version": "2.3.2", | ||
"version": "2.4.0", | ||
"description": "Extremely fast utf8 only stream implementation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -63,2 +63,4 @@ # sonic-boom | ||
required to be full before flushing. | ||
* `maxLength`: the maximum length of the internal buffer. If a write operation would cause the buffer | ||
to exceed `maxLength`, the data written is dropped and a `drop` event is emitted with the dropped data | ||
* `sync`: perform writes synchronously (similar to `console.log`). | ||
@@ -65,0 +67,0 @@ * `append`: appends writes to dest file instead of truncating it (default `true`). |
64
test.js
@@ -1344,2 +1344,66 @@ 'use strict' | ||
test('write should not drop new data if buffer is not full', (t) => { | ||
t.plan(2) | ||
const fakeFs = Object.create(fs) | ||
const SonicBoom = proxyquire('.', { | ||
fs: fakeFs | ||
}) | ||
const dest = file() | ||
const fd = fs.openSync(dest, 'w') | ||
const stream = new SonicBoom({ fd, minLength: 101, maxLength: 102, sync: false }) | ||
const buf = Buffer.alloc(100).fill('x').toString() | ||
fakeFs.write = function (fd, _buf, enc, cb) { | ||
t.equal(_buf.length, buf.length + 2) | ||
setImmediate(cb, null, _buf.length) | ||
fakeFs.write = () => t.error('shouldnt call write again') | ||
stream.end() | ||
} | ||
stream.on('drop', (data) => { | ||
t.error('should not drop') | ||
}) | ||
stream.write(buf) | ||
stream.write('aa') | ||
stream.on('close', () => { | ||
t.pass('close emitted') | ||
}) | ||
}) | ||
test('write should drop new data if buffer is full', (t) => { | ||
t.plan(3) | ||
const fakeFs = Object.create(fs) | ||
const SonicBoom = proxyquire('.', { | ||
fs: fakeFs | ||
}) | ||
const dest = file() | ||
const fd = fs.openSync(dest, 'w') | ||
const stream = new SonicBoom({ fd, minLength: 101, maxLength: 102, sync: false }) | ||
const buf = Buffer.alloc(100).fill('x').toString() | ||
fakeFs.write = function (fd, _buf, enc, cb) { | ||
t.equal(_buf.length, buf.length) | ||
setImmediate(cb, null, _buf.length) | ||
fakeFs.write = () => t.error('shouldnt call write more than once') | ||
} | ||
stream.on('drop', (data) => { | ||
t.equal(data.length, 3) | ||
stream.end() | ||
}) | ||
stream.write(buf) | ||
stream.write('aaa') | ||
stream.on('close', () => { | ||
t.pass('close emitted') | ||
}) | ||
}) | ||
test('should throw if minLength >= MAX_WRITE', (t) => { | ||
@@ -1346,0 +1410,0 @@ t.plan(1) |
@@ -12,2 +12,3 @@ // Type definitions for sonic-boom 0.7 | ||
dest?: string | number | ||
maxLength?: number | ||
minLength?: number | ||
@@ -17,2 +18,3 @@ sync?: boolean | ||
mkdir?: boolean | ||
retryEAGAIN?: (err: Error, writeBufferLen: number, remainingBufferLen: number) => boolean | ||
} | ||
@@ -19,0 +21,0 @@ |
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
52450
1651
117