From 58c0a59a999399b02a53510a8f35c5cdabf9774f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Tue, 2 Jul 2019 17:33:41 +0200 Subject: [PATCH] remove/updated dependencies - Updated to lowest LTS (node 8+) - Changed the main function to a class - got rid of inherit - made all test file use `new` - got rid of typedarray - got rid of buffer-from (only needed for node < 6) --- .travis.yml | 2 +- index.js | 115 ++++++++++++++++++++------------------------- package.json | 7 +-- test/array.js | 4 +- test/buffer.js | 17 +++---- test/infer.js | 7 ++- test/nothing.js | 8 ++-- test/objects.js | 6 +-- test/server/ls.js | 4 +- test/string.js | 47 +++++++++--------- test/typedarray.js | 27 +++++------ 11 files changed, 109 insertions(+), 135 deletions(-) diff --git a/.travis.yml b/.travis.yml index 262c501..ca85a04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js node_js: - - '6.0' + - '8.0' diff --git a/index.js b/index.js index dd672a7..8139d17 100644 --- a/index.js +++ b/index.js @@ -1,74 +1,61 @@ var Writable = require('readable-stream').Writable -var inherits = require('inherits') -var bufferFrom = require('buffer-from') -if (typeof Uint8Array === 'undefined') { - var U8 = require('typedarray').Uint8Array -} else { - var U8 = Uint8Array -} - -function ConcatStream(opts, cb) { - if (!(this instanceof ConcatStream)) return new ConcatStream(opts, cb) - - if (typeof opts === 'function') { - cb = opts - opts = {} - } - if (!opts) opts = {} - - var encoding = opts.encoding - var shouldInferEncoding = false +class ConcatStream extends Writable { + constructor (opts, cb) { + super({ objectMode: true }) - if (!encoding) { - shouldInferEncoding = true - } else { - encoding = String(encoding).toLowerCase() - if (encoding === 'u8' || encoding === 'uint8') { - encoding = 'uint8array' + if (typeof opts === 'function') { + cb = opts + opts = {} } - } + if (!opts) opts = {} - Writable.call(this, { objectMode: true }) + var encoding = opts.encoding + var shouldInferEncoding = false - this.encoding = encoding - this.shouldInferEncoding = shouldInferEncoding + if (!encoding) { + shouldInferEncoding = true + } else { + encoding = String(encoding).toLowerCase() + if (encoding === 'u8' || encoding === 'uint8') { + encoding = 'uint8array' + } + } - if (cb) this.on('finish', function () { cb(this.getBody()) }) - this.body = [] -} + this.encoding = encoding + this.shouldInferEncoding = shouldInferEncoding -module.exports = ConcatStream -inherits(ConcatStream, Writable) + if (cb) this.on('finish', function () { cb(this.getBody()) }) + this.body = [] + } -ConcatStream.prototype._write = function(chunk, enc, next) { - this.body.push(chunk) - next() -} + _write (chunk, enc, next) { + this.body.push(chunk) + next() + } -ConcatStream.prototype.inferEncoding = function (buff) { - var firstBuffer = buff === undefined ? this.body[0] : buff; - if (Buffer.isBuffer(firstBuffer)) return 'buffer' - if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array' - if (Array.isArray(firstBuffer)) return 'array' - if (typeof firstBuffer === 'string') return 'string' - if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object' - return 'buffer' -} + inferEncoding (buff) { + var firstBuffer = buff === undefined ? this.body[0] : buff; + if (Buffer.isBuffer(firstBuffer)) return 'buffer' + if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array' + if (Array.isArray(firstBuffer)) return 'array' + if (typeof firstBuffer === 'string') return 'string' + if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object' + return 'buffer' + } -ConcatStream.prototype.getBody = function () { - if (!this.encoding && this.body.length === 0) return [] - if (this.shouldInferEncoding) this.encoding = this.inferEncoding() - if (this.encoding === 'array') return arrayConcat(this.body) - if (this.encoding === 'string') return stringConcat(this.body) - if (this.encoding === 'buffer') return bufferConcat(this.body) - if (this.encoding === 'uint8array') return u8Concat(this.body) - return this.body + getBody () { + if (!this.encoding && this.body.length === 0) return [] + if (this.shouldInferEncoding) this.encoding = this.inferEncoding() + if (this.encoding === 'array') return arrayConcat(this.body) + if (this.encoding === 'string') return stringConcat(this.body) + if (this.encoding === 'buffer') return bufferConcat(this.body) + if (this.encoding === 'uint8array') return u8Concat(this.body) + return this.body + } } -var isArray = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]' -} +module.exports = ConcatStream function isArrayish (arr) { return /Array\]$/.test(Object.prototype.toString.call(arr)) @@ -88,9 +75,9 @@ function stringConcat (parts) { } else if (Buffer.isBuffer(p)) { strings.push(p) } else if (isBufferish(p)) { - strings.push(bufferFrom(p)) + strings.push(Buffer.from(p)) } else { - strings.push(bufferFrom(String(p))) + strings.push(Buffer.from(String(p))) } } if (Buffer.isBuffer(parts[0])) { @@ -109,9 +96,9 @@ function bufferConcat (parts) { if (Buffer.isBuffer(p)) { bufs.push(p) } else if (isBufferish(p)) { - bufs.push(bufferFrom(p)) + bufs.push(Buffer.from(p)) } else { - bufs.push(bufferFrom(String(p))) + bufs.push(Buffer.from(String(p))) } } return Buffer.concat(bufs) @@ -129,11 +116,11 @@ function u8Concat (parts) { var len = 0 for (var i = 0; i < parts.length; i++) { if (typeof parts[i] === 'string') { - parts[i] = bufferFrom(parts[i]) + parts[i] = Buffer.from(parts[i]) } len += parts[i].length } - var u8 = new U8(len) + var u8 = new Uint8Array(len) for (var i = 0, offset = 0; i < parts.length; i++) { var part = parts[i] for (var j = 0; j < part.length; j++) { diff --git a/package.json b/package.json index 3797828..111cfc1 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "url": "http://github.com/maxogden/concat-stream/issues" }, "engines": [ - "node >= 6.0" + "node >= 8.0" ], "main": "index.js", "files": [ @@ -28,10 +28,7 @@ }, "license": "MIT", "dependencies": { - "buffer-from": "^1.0.0", - "inherits": "^2.0.3", - "readable-stream": "^3.0.2", - "typedarray": "^0.0.6" + "readable-stream": "^3.4.0" }, "devDependencies": { "tape": "^4.6.3" diff --git a/test/array.js b/test/array.js index 86e7dd4..dbce552 100644 --- a/test/array.js +++ b/test/array.js @@ -1,9 +1,9 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') test('array stream', function (t) { t.plan(1) - var arrays = concat({ encoding: 'array' }, function(out) { + var arrays = new Concat({ encoding: 'array' }, function(out) { t.deepEqual(out, [1,2,3,4,5,6]) }) arrays.write([1,2,3]) diff --git a/test/buffer.js b/test/buffer.js index 2aec997..6a0791d 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -1,30 +1,27 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') -var TA = require('typedarray') -var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array -var bufferFrom = require('buffer-from') test('buffer stream', function (t) { t.plan(2) - var buffers = concat(function(out) { + var buffers = new Concat(function(out) { t.ok(Buffer.isBuffer(out)) t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat') }) - buffers.write(bufferFrom('pizza Array is not a ', 'utf8')) - buffers.write(bufferFrom('stringy cat')) + buffers.write(Buffer.from('pizza Array is not a ', 'utf8')) + buffers.write(Buffer.from('stringy cat')) buffers.end() }) test('buffer mixed writes', function (t) { t.plan(2) - var buffers = concat(function(out) { + var buffers = new Concat(function(out) { t.ok(Buffer.isBuffer(out)) t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat555') }) - buffers.write(bufferFrom('pizza')) + buffers.write(Buffer.from('pizza')) buffers.write(' Array is not a ') buffers.write([ 115, 116, 114, 105, 110, 103, 121 ]) - var u8 = new U8(4) + var u8 = new Uint8Array(4) u8[0] = 32; u8[1] = 99; u8[2] = 97; u8[3] = 116 buffers.write(u8) buffers.write(555) diff --git a/test/infer.js b/test/infer.js index 4b9edfa..cc3e647 100644 --- a/test/infer.js +++ b/test/infer.js @@ -1,11 +1,10 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') -var bufferFrom = require('buffer-from') test('type inference works as expected', function(t) { - var stream = concat() + var stream = new Concat() t.equal(stream.inferEncoding(['hello']), 'array', 'array') - t.equal(stream.inferEncoding(bufferFrom('hello')), 'buffer', 'buffer') + t.equal(stream.inferEncoding(Buffer.from('hello')), 'buffer', 'buffer') t.equal(stream.inferEncoding(undefined), 'buffer', 'buffer') t.equal(stream.inferEncoding(new Uint8Array(1)), 'uint8array', 'uint8array') t.equal(stream.inferEncoding('hello'), 'string', 'string') diff --git a/test/nothing.js b/test/nothing.js index 6ac6049..e743345 100644 --- a/test/nothing.js +++ b/test/nothing.js @@ -1,15 +1,15 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') test('no callback stream', function (t) { - var stream = concat() + var stream = new Concat() stream.write('space') stream.end(' cats') t.end() }) test('no encoding set, no data', function (t) { - var stream = concat(function(data) { + var stream = new Concat(function(data) { t.deepEqual(data, []) t.end() }) @@ -17,7 +17,7 @@ test('no encoding set, no data', function (t) { }) test('encoding set to string, no data', function (t) { - var stream = concat({ encoding: 'string' }, function(data) { + var stream = new Concat({ encoding: 'string' }, function(data) { t.deepEqual(data, '') t.end() }) diff --git a/test/objects.js b/test/objects.js index ad921ed..0867902 100644 --- a/test/objects.js +++ b/test/objects.js @@ -1,8 +1,8 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') test('writing objects', function (t) { - var stream = concat({encoding: "objects"}, concatted) + var stream = new Concat({encoding: "objects"}, concatted) function concatted(objs) { t.equal(objs.length, 2) t.deepEqual(objs[0], {"foo": "bar"}) @@ -16,7 +16,7 @@ test('writing objects', function (t) { test('switch to objects encoding if no encoding specified and objects are written', function (t) { - var stream = concat(concatted) + var stream = new Concat(concatted) function concatted(objs) { t.equal(objs.length, 2) t.deepEqual(objs[0], {"foo": "bar"}) diff --git a/test/server/ls.js b/test/server/ls.js index 3258d8d..36ac6ee 100644 --- a/test/server/ls.js +++ b/test/server/ls.js @@ -1,4 +1,4 @@ -var concat = require('../../') +var Concat = require('../../') var spawn = require('child_process').spawn var exec = require('child_process').exec var test = require('tape') @@ -7,7 +7,7 @@ test('ls command', function (t) { t.plan(1) var cmd = spawn('ls', [ __dirname ]) cmd.stdout.pipe( - concat(function(out) { + new Concat(function(out) { exec('ls ' + __dirname, function (err, body) { t.equal(out.toString('utf8'), body.toString('utf8')) }) diff --git a/test/string.js b/test/string.js index d44f75d..8e294ff 100644 --- a/test/string.js +++ b/test/string.js @@ -1,62 +1,59 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') -var TA = require('typedarray') -var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array -var bufferFrom = require('buffer-from') test('string -> buffer stream', function (t) { t.plan(2) - var strings = concat({ encoding: 'buffer'}, function(out) { + var strings = new Concat({ encoding: 'buffer'}, function(out) { t.ok(Buffer.isBuffer(out)) t.equal(out.toString('utf8'), 'nacho dogs') }) - strings.write("nacho ") - strings.write("dogs") + strings.write('nacho ') + strings.write('dogs') strings.end() }) test('string stream', function (t) { t.plan(2) - var strings = concat({ encoding: 'string' }, function(out) { + var strings = new Concat({ encoding: 'string' }, function(out) { t.equal(typeof out, 'string') t.equal(out, 'nacho dogs') }) - strings.write("nacho ") - strings.write("dogs") + strings.write('nacho ') + strings.write('dogs') strings.end() }) test('end chunk', function (t) { t.plan(1) - var endchunk = concat({ encoding: 'string' }, function(out) { + var endchunk = new Concat({ encoding: 'string' }, function(out) { t.equal(out, 'this is the end') }) - endchunk.write("this ") - endchunk.write("is the ") - endchunk.end("end") + endchunk.write('this ') + endchunk.write('is the ') + endchunk.end('end') }) test('string from mixed write encodings', function (t) { t.plan(2) - var strings = concat({ encoding: 'string' }, function(out) { + var strings = new Concat({ encoding: 'string' }, function(out) { t.equal(typeof out, 'string') t.equal(out, 'nacho dogs') }) strings.write('na') - strings.write(bufferFrom('cho')) + strings.write(Buffer.from('cho')) strings.write([ 32, 100 ]) - var u8 = new U8(3) - u8[0] = 111; u8[1] = 103; u8[2] = 115; + var u8 = new Uint8Array(3) + u8[0] = 111; u8[1] = 103; u8[2] = 115 strings.end(u8) }) test('string from buffers with multibyte characters', function (t) { t.plan(2) - var strings = concat({ encoding: 'string' }, function(out) { + var strings = new Concat({ encoding: 'string' }, function(out) { t.equal(typeof out, 'string') t.equal(out, '☃☃☃☃☃☃☃☃') }) - var snowman = bufferFrom('☃') + var snowman = Buffer.from('☃') for (var i = 0; i < 8; i++) { strings.write(snowman.slice(0, 1)) strings.write(snowman.slice(1)) @@ -66,18 +63,18 @@ test('string from buffers with multibyte characters', function (t) { test('string infer encoding with empty string chunk', function (t) { t.plan(2) - var strings = concat(function(out) { + var strings = new Concat(function(out) { t.equal(typeof out, 'string') t.equal(out, 'nacho dogs') }) - strings.write("") - strings.write("nacho ") - strings.write("dogs") + strings.write('') + strings.write('nacho ') + strings.write('dogs') strings.end() }) test('to string numbers', function (t) { - var write = concat(function (str) { + var write = new Concat(function (str) { t.equal(str, 'a1000') t.end() }) diff --git a/test/typedarray.js b/test/typedarray.js index 83bf401..5351f5c 100644 --- a/test/typedarray.js +++ b/test/typedarray.js @@ -1,21 +1,18 @@ -var concat = require('../') +var Concat = require('../') var test = require('tape') -var TA = require('typedarray') -var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array -var bufferFrom = require('buffer-from') test('typed array stream', function (t) { t.plan(2) - var a = new U8(5) - a[0] = 97; a[1] = 98; a[2] = 99; a[3] = 100; a[4] = 101; - var b = new U8(3) - b[0] = 32; b[1] = 102; b[2] = 103; - var c = new U8(4) - c[0] = 32; c[1] = 120; c[2] = 121; c[3] = 122; + var a = new Uint8Array(5) + a[0] = 97; a[1] = 98; a[2] = 99; a[3] = 100; a[4] = 101 + var b = new Uint8Array(3) + b[0] = 32; b[1] = 102; b[2] = 103 + var c = new Uint8Array(4) + c[0] = 32; c[1] = 120; c[2] = 121; c[3] = 122 - var arrays = concat({ encoding: 'Uint8Array' }, function(out) { + var arrays = new Concat({ encoding: 'Uint8Array' }, function(out) { t.equal(typeof out.subarray, 'function') - t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz') + t.deepEqual(Buffer.from(out).toString('utf8'), 'abcde fg xyz') }) arrays.write(a) arrays.write(b) @@ -24,11 +21,11 @@ test('typed array stream', function (t) { test('typed array from strings, buffers, and arrays', function (t) { t.plan(2) - var arrays = concat({ encoding: 'Uint8Array' }, function(out) { + var arrays = new Concat({ encoding: 'Uint8Array' }, function(out) { t.equal(typeof out.subarray, 'function') - t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz') + t.deepEqual(Buffer.from(out).toString('utf8'), 'abcde fg xyz') }) arrays.write('abcde') - arrays.write(bufferFrom(' fg ')) + arrays.write(Buffer.from(' fg ')) arrays.end([ 120, 121, 122 ]) })