11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst rl = require('readline');
71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst BOM = '\uFEFF';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Get the data using a non-stream way to compare with the streamed data.
121cb0ef41Sopenharmony_ciconst modelData = fixtures.readSync('file-to-read-without-bom.txt', 'utf8');
131cb0ef41Sopenharmony_ciconst modelDataFirstCharacter = modelData[0];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Detect the number of forthcoming 'line' events for mustCall() 'expected' arg.
161cb0ef41Sopenharmony_ciconst lineCount = modelData.match(/\n/g).length;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Ensure both without-bom and with-bom test files are textwise equal.
191cb0ef41Sopenharmony_ciassert.strictEqual(fixtures.readSync('file-to-read-with-bom.txt', 'utf8'),
201cb0ef41Sopenharmony_ci                   `${BOM}${modelData}`
211cb0ef41Sopenharmony_ci);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// An unjustified BOM stripping with a non-BOM character unshifted to a stream.
241cb0ef41Sopenharmony_ciconst inputWithoutBOM =
251cb0ef41Sopenharmony_ci  fs.createReadStream(fixtures.path('file-to-read-without-bom.txt'), 'utf8');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciinputWithoutBOM.once('readable', common.mustCall(() => {
281cb0ef41Sopenharmony_ci  const maybeBOM = inputWithoutBOM.read(1);
291cb0ef41Sopenharmony_ci  assert.strictEqual(maybeBOM, modelDataFirstCharacter);
301cb0ef41Sopenharmony_ci  assert.notStrictEqual(maybeBOM, BOM);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  inputWithoutBOM.unshift(maybeBOM);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  let streamedData = '';
351cb0ef41Sopenharmony_ci  rl.createInterface({
361cb0ef41Sopenharmony_ci    input: inputWithoutBOM,
371cb0ef41Sopenharmony_ci  }).on('line', common.mustCall((line) => {
381cb0ef41Sopenharmony_ci    streamedData += `${line}\n`;
391cb0ef41Sopenharmony_ci  }, lineCount)).on('close', common.mustCall(() => {
401cb0ef41Sopenharmony_ci    assert.strictEqual(streamedData, modelData);
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci}));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// A justified BOM stripping.
451cb0ef41Sopenharmony_ciconst inputWithBOM =
461cb0ef41Sopenharmony_ci  fs.createReadStream(fixtures.path('file-to-read-with-bom.txt'), 'utf8');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciinputWithBOM.once('readable', common.mustCall(() => {
491cb0ef41Sopenharmony_ci  const maybeBOM = inputWithBOM.read(1);
501cb0ef41Sopenharmony_ci  assert.strictEqual(maybeBOM, BOM);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  let streamedData = '';
531cb0ef41Sopenharmony_ci  rl.createInterface({
541cb0ef41Sopenharmony_ci    input: inputWithBOM,
551cb0ef41Sopenharmony_ci  }).on('line', common.mustCall((line) => {
561cb0ef41Sopenharmony_ci    streamedData += `${line}\n`;
571cb0ef41Sopenharmony_ci  }, lineCount)).on('close', common.mustCall(() => {
581cb0ef41Sopenharmony_ci    assert.strictEqual(streamedData, modelData);
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci}));
61