1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5const fs = require('fs'); 6const rl = require('readline'); 7const fixtures = require('../common/fixtures'); 8 9const BOM = '\uFEFF'; 10 11// Get the data using a non-stream way to compare with the streamed data. 12const modelData = fixtures.readSync('file-to-read-without-bom.txt', 'utf8'); 13const modelDataFirstCharacter = modelData[0]; 14 15// Detect the number of forthcoming 'line' events for mustCall() 'expected' arg. 16const lineCount = modelData.match(/\n/g).length; 17 18// Ensure both without-bom and with-bom test files are textwise equal. 19assert.strictEqual(fixtures.readSync('file-to-read-with-bom.txt', 'utf8'), 20 `${BOM}${modelData}` 21); 22 23// An unjustified BOM stripping with a non-BOM character unshifted to a stream. 24const inputWithoutBOM = 25 fs.createReadStream(fixtures.path('file-to-read-without-bom.txt'), 'utf8'); 26 27inputWithoutBOM.once('readable', common.mustCall(() => { 28 const maybeBOM = inputWithoutBOM.read(1); 29 assert.strictEqual(maybeBOM, modelDataFirstCharacter); 30 assert.notStrictEqual(maybeBOM, BOM); 31 32 inputWithoutBOM.unshift(maybeBOM); 33 34 let streamedData = ''; 35 rl.createInterface({ 36 input: inputWithoutBOM, 37 }).on('line', common.mustCall((line) => { 38 streamedData += `${line}\n`; 39 }, lineCount)).on('close', common.mustCall(() => { 40 assert.strictEqual(streamedData, modelData); 41 })); 42})); 43 44// A justified BOM stripping. 45const inputWithBOM = 46 fs.createReadStream(fixtures.path('file-to-read-with-bom.txt'), 'utf8'); 47 48inputWithBOM.once('readable', common.mustCall(() => { 49 const maybeBOM = inputWithBOM.read(1); 50 assert.strictEqual(maybeBOM, BOM); 51 52 let streamedData = ''; 53 rl.createInterface({ 54 input: inputWithBOM, 55 }).on('line', common.mustCall((line) => { 56 streamedData += `${line}\n`; 57 }, lineCount)).on('close', common.mustCall(() => { 58 assert.strictEqual(streamedData, modelData); 59 })); 60})); 61