Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 577adc7

Browse filesBrowse files
vsemozhetbytcjihrig
authored andcommitted
test: ensure stream preprocessing order
Sometimes it is necessary to preprocess some initial bit of a stream data before giving the entire stream to the main processing function. Sometimes this bit should be extracted from the stream before the main processing; sometimes it should be returned to the stream. This test checks an order of stream modes, methods and events for a possible preprocessing algorithm. Stream BOM stripping is selected as a use case. See nodejs/help#221 as the prehistory. PR-URL: #7741 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 5c4b938 commit 577adc7
Copy full SHA for 577adc7

File tree

Expand file treeCollapse file tree

3 files changed

+73
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+73
-0
lines changed
Open diff view settings
Collapse file
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abc
2+
def
3+
ghi
Collapse file
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abc
2+
def
3+
ghi
Collapse file
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.