Skip to content

Commit

Permalink
feat!: Switch to streamx for streams (#10)
Browse files Browse the repository at this point in the history
chore: Drop `buffer-equal` dependency
  • Loading branch information
phated committed Apr 19, 2022
1 parent fca6c3c commit 9867811
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions 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';
Expand All @@ -11,17 +11,19 @@ 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
var decoder = new TextDecoder('utf-8', { ignoreBOM: false });

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;
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -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"
Expand Down
97 changes: 54 additions & 43 deletions test/index.js
Expand Up @@ -2,47 +2,51 @@

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');

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
);
});

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) {
Expand All @@ -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
);
Expand All @@ -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),
Expand All @@ -88,29 +92,36 @@ 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
);
});

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) {
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down

0 comments on commit 9867811

Please sign in to comment.