diff --git a/README.md b/README.md index 0c818fb..ba1dedc 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ fs.createReadStream('utf8-file-with-bom.txt') ### `removeBOM(encoding)` -Returns a `through2` stream that will remove a BOM, if the argument `encoding` is `'utf-8'` and the given data is a UTF8 Buffer with a BOM at the beginning. If the `encoding` is not `'utf-8'` or does not have a BOM, the data is not changed and this becomes a normal passthrough stream. +Returns a `Transform` stream that will remove a BOM, if the argument `encoding` is `'utf-8'` and the given data is a UTF8 Buffer with a BOM at the beginning. If the `encoding` is not `'utf-8'` or does not have a BOM, the data is not changed and this becomes a no-op `Transform` stream. ## License diff --git a/index.js b/index.js index 32ad9ec..bf9a6a6 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -var through = require('through2'); +var Transform = require('streamx').Transform; var TextDecoder = require('util').TextDecoder; var BOM = '\ufeff'; @@ -11,7 +11,7 @@ function removeBomStream(encoding) { // Needed due to https://github.com/nodejs/node/pull/42779 if (!isUTF8) { - return through(); + return new Transform(); } // Only used if encoding is UTF-8 @@ -19,9 +19,11 @@ function removeBomStream(encoding) { var state = 0; // 0:Not removed, -1:In removing, 1:Already removed - return through(onChunk); + return new Transform({ + transform: onChunk + }); - function onChunk(data, _, cb) { + function onChunk(data, cb) { if (state === 1) { cb(null, data); return; diff --git a/package.json b/package.json index be77b04..bf69d0d 100644 --- a/package.json +++ b/package.json @@ -22,15 +22,14 @@ "test": "nyc mocha --async-only" }, "dependencies": { - "through2": "^4.0.2" + "streamx": "^2.12.4" }, "devDependencies": { - "buffer-equal": "^1.0.0", + "concat-stream": "^2.0.0", "eslint": "^7.32.0", "eslint-config-gulp": "^5.0.1", "eslint-plugin-node": "^11.1.0", "expect": "^27.4.2", - "mississippi": "^4.0.0", "mocha": "^8.4.0", "nyc": "^15.1.0", "stream-chunker": "^1.2.8" diff --git a/test/index.js b/test/index.js index bffa706..fc2f06e 100644 --- a/test/index.js +++ b/test/index.js @@ -2,17 +2,15 @@ var fs = require('fs'); var path = require('path'); +var pipeline = require('stream').pipeline; var expect = require('expect'); -var miss = require('mississippi'); -var isEqual = require('buffer-equal'); +var concat = require('concat-stream'); var chunker = require('stream-chunker'); +var Readable = require('streamx').Readable; var removeBomStream = require('../'); -var pipe = miss.pipe; -var concat = miss.concat; - describe('removeBomStream', function () { it('ignores UTF8 buffer without a BOM', function (done) { var filepath = path.join(__dirname, './fixtures/test.txt'); @@ -20,10 +18,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [fs.createReadStream(filepath), removeBomStream('utf-8'), concat(assert)], done ); @@ -31,18 +29,24 @@ describe('removeBomStream', function () { it('ignores UTF8 buffer without a BOM even if first chunk is shorter than 7 chars but second and subsequent are larger', function (done) { var filepath = path.join(__dirname, './fixtures/test.txt'); - var fileContent = fs.readFileSync(filepath, 'utf-8'); - - var rmBom = removeBomStream('utf8'); - var output = ''; - rmBom.on('data', function (d) { - output += d.toString(); - }); - rmBom.write(Buffer.from(fileContent.slice(0, 5))); - rmBom.write(Buffer.from(fileContent.slice(5))); - - expect(output).toEqual(fileContent); - done(); + var fileContent = fs.readFileSync(filepath); + + var expected = fileContent; + + function assert(data) { + expect(data).toEqual(expected); + } + + var reader = new Readable(); + pipeline([ + reader, + removeBomStream('utf-8'), + concat(assert) + ], done); + + reader.push(fileContent.slice(0, 5)); + reader.push(fileContent.slice(5)); + reader.push(null); }); it('removes the BOM from a UTF8 buffer', function (done) { @@ -51,10 +55,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath).slice(3); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)], done ); @@ -66,10 +70,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath).slice(3); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [ fs.createReadStream(filepath), chunker(1), @@ -88,10 +92,10 @@ describe('removeBomStream', function () { function assert(data) { expect(data.length < 7).toEqual(true); expect(expected.length < 7).toEqual(true); - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)], done ); @@ -99,18 +103,25 @@ describe('removeBomStream', function () { it('remove the BOM from a UTF8 buffer even if first chunk is shorter than 7 chars but second and subsequent are larger', function (done) { var filepath = path.join(__dirname, './fixtures/bom-utf8.txt'); - var fileContent = fs.readFileSync(filepath, 'utf-8'); - - var rmBom = removeBomStream('utf-8'); - var output = ''; - rmBom.on('data', function (d) { - output += d.toString(); - }); - rmBom.write(Buffer.from(fileContent.slice(0, 5))); - rmBom.write(Buffer.from(fileContent.slice(5))); - - expect(output).toEqual(fileContent.slice(1)); - done(); + var fileContent = fs.readFileSync(filepath); + + // UTF8 BOM takes up 3 characters in the buffer + var expected = fileContent.slice(3); + + function assert(data) { + expect(data).toEqual(expected); + } + + var reader = new Readable(); + pipeline([ + reader, + removeBomStream('utf-8'), + concat(assert) + ], done); + + reader.push(fileContent.slice(0, 5)); + reader.push(fileContent.slice(5)); + reader.push(null); }); it('does not remove the BOM from a UTF16BE buffer', function (done) { @@ -119,10 +130,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [ fs.createReadStream(filepath), removeBomStream('utf-16be'), @@ -138,10 +149,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [ fs.createReadStream(filepath), removeBomStream('utf-16be'), @@ -157,10 +168,10 @@ describe('removeBomStream', function () { var expected = fs.readFileSync(filepath); function assert(data) { - expect(isEqual(data, expected)).toEqual(true); + expect(data).toEqual(expected); } - pipe( + pipeline( [ fs.createReadStream(filepath), removeBomStream('utf-16le'),