consume-until
Advanced tools
+3
-3
| { | ||
| "name": "consume-until", | ||
| "version": "2.0.0", | ||
| "version": "2.0.1", | ||
| "description": "Consume a stream until a given pattern is found", | ||
@@ -35,5 +35,5 @@ "main": "index.js", | ||
| "coordinates": [ | ||
| 55.77758524020799, | ||
| 12.58996683121336 | ||
| 55.77757947708978, | ||
| 12.58993180793459 | ||
| ] | ||
| } |
-34
| 'use strict' | ||
| var util = require('util') | ||
| var bindexOf = require('buffer-indexof') | ||
| var Transform = require('readable-stream').Transform | ||
| module.exports = ConsumeUntil | ||
| util.inherits(ConsumeUntil, Transform) | ||
| function ConsumeUntil (delimiter) { | ||
| Transform.call(this) | ||
| this.delimiter = typeof delimiter === 'string' ? Buffer(delimiter) : delimiter | ||
| this.bytes = 0 | ||
| } | ||
| ConsumeUntil.prototype._transform = function (chunk, enc, done) { | ||
| var index = bindexOf(chunk, this.delimiter) | ||
| if (index === -1) { | ||
| this.bytes += chunk.length | ||
| this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk], this.bytes) : chunk | ||
| done() | ||
| return | ||
| } | ||
| this.bytes += index | ||
| this.push(chunk.slice(index)) | ||
| chunk = chunk.slice(0, index) | ||
| this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk], this.bytes) : chunk | ||
| done() | ||
| } |
-31
| 'use strict' | ||
| var bindexOf = require('buffer-indexof') | ||
| module.exports = function (source, delimiter, cb) { | ||
| var len = 0 | ||
| var buffer | ||
| delimiter = typeof delimiter === 'string' ? Buffer(delimiter) : delimiter | ||
| source.on('data', onData) | ||
| function onData (chunk) { | ||
| var index = bindexOf(chunk, delimiter) | ||
| if (index === -1) { | ||
| len += chunk.length | ||
| buffer = buffer ? Buffer.concat([buffer, chunk], len) : chunk | ||
| return | ||
| } | ||
| source.removeListener('data', onData) | ||
| var rest = chunk.slice(index) | ||
| chunk = chunk.slice(0, index) | ||
| buffer = buffer ? Buffer.concat([buffer, chunk], len + index) : chunk | ||
| cb(buffer) | ||
| source.unshift(rest) | ||
| } | ||
| } |
-19
| 'use strict' | ||
| var test = require('tape') | ||
| var PassThrough = require('readable-stream').PassThrough | ||
| var ConsumeUntil = require('./index1') | ||
| test(function (t) { | ||
| var s1 = new PassThrough() | ||
| var s2 = new ConsumeUntil('bar') | ||
| var s3 = s1.pipe(s2) | ||
| s1.write('foobarbaz') | ||
| s3.on('data', function (chunk) { | ||
| t.equal(s2.buffer.toString(), 'foo') | ||
| t.equal(chunk.toString(), 'barbaz') | ||
| t.end() | ||
| }) | ||
| }) |
-19
| 'use strict' | ||
| var test = require('tape') | ||
| var PassThrough = require('readable-stream').PassThrough | ||
| var consumeUntil = require('./index2') | ||
| test(function (t) { | ||
| var s = new PassThrough() | ||
| s.write('foobarbaz') | ||
| consumeUntil(s, 'bar', function (head) { | ||
| t.equal(head.toString(), 'foo') | ||
| s.on('data', function (chunk) { | ||
| t.equal(chunk.toString(), 'barbaz') | ||
| t.end() | ||
| }) | ||
| }) | ||
| }) |
5824
-28.5%7
-36.36%93
-45.29%