test: ensure stream preprocessing order · nodejs/node@ea725ed

1+

'use strict';

2+

const common = require('../common');

3+

const assert = require('assert');

4+5+

const fs = require('fs');

6+

const path = require('path');

7+

const rl = require('readline');

8+9+

const BOM = '\uFEFF';

10+11+

// Get the data using a non-stream way to compare with the streamed data.

12+

const modelData = fs.readFileSync(

13+

path.join(common.fixturesDir, 'file-to-read-without-bom.txt'), 'utf8'

14+

);

15+

const modelDataFirstCharacter = modelData[0];

16+17+

// Detect the number of forthcoming 'line' events for mustCall() 'expected' arg.

18+

const lineCount = modelData.match(/\n/g).length;

19+20+

// Ensure both without-bom and with-bom test files are textwise equal.

21+

assert.strictEqual(

22+

fs.readFileSync(

23+

path.join(common.fixturesDir, 'file-to-read-with-bom.txt'), 'utf8'

24+

),

25+

`${BOM}${modelData}`

26+

);

27+28+

// An unjustified BOM stripping with a non-BOM character unshifted to a stream.

29+

const inputWithoutBOM = fs.createReadStream(

30+

path.join(common.fixturesDir, 'file-to-read-without-bom.txt'), 'utf8'

31+

);

32+33+

inputWithoutBOM.once('readable', common.mustCall(() => {

34+

const maybeBOM = inputWithoutBOM.read(1);

35+

assert.strictEqual(maybeBOM, modelDataFirstCharacter);

36+

assert.notStrictEqual(maybeBOM, BOM);

37+38+

inputWithoutBOM.unshift(maybeBOM);

39+40+

let streamedData = '';

41+

rl.createInterface({

42+

input: inputWithoutBOM,

43+

}).on('line', common.mustCall((line) => {

44+

streamedData += `${line}\n`;

45+

}, lineCount)).on('close', common.mustCall(() => {

46+

assert.strictEqual(streamedData, modelData);

47+

}));

48+

}));

49+50+

// A justified BOM stripping.

51+

const inputWithBOM = fs.createReadStream(

52+

path.join(common.fixturesDir, 'file-to-read-with-bom.txt'), 'utf8'

53+

);

54+55+

inputWithBOM.once('readable', common.mustCall(() => {

56+

const maybeBOM = inputWithBOM.read(1);

57+

assert.strictEqual(maybeBOM, BOM);

58+59+

let streamedData = '';

60+

rl.createInterface({

61+

input: inputWithBOM,

62+

}).on('line', common.mustCall((line) => {

63+

streamedData += `${line}\n`;

64+

}, lineCount)).on('close', common.mustCall(() => {

65+

assert.strictEqual(streamedData, modelData);

66+

}));

67+

}));