deno.land / std@0.166.0 / node / _tools / test / parallel / test-stream-readable-unshift.js

test-stream-readable-unshift.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');const { Readable } = require('stream');
{ // Check that strings are saved as Buffer const readable = new Readable({ read() {} });
const string = 'abc';
readable.on('data', common.mustCall((chunk) => { assert(Buffer.isBuffer(chunk)); assert.strictEqual(chunk.toString('utf8'), string); }, 1));
readable.unshift(string);
}
{ // Check that data goes at the beginning const readable = new Readable({ read() {} }); const unshift = 'front'; const push = 'back';
const expected = [unshift, push]; readable.on('data', common.mustCall((chunk) => { assert.strictEqual(chunk.toString('utf8'), expected.shift()); }, 2));

readable.push(push); readable.unshift(unshift);}
{ // Check that buffer is saved with correct encoding const readable = new Readable({ read() {} });
const encoding = 'base64'; const string = Buffer.from('abc').toString(encoding);
readable.on('data', common.mustCall((chunk) => { assert.strictEqual(chunk.toString(encoding), string); }, 1));
readable.unshift(string, encoding);
}
{
const streamEncoding = 'base64';
function checkEncoding(readable) {
// chunk encodings const encodings = ['utf8', 'binary', 'hex', 'base64']; const expected = [];
readable.on('data', common.mustCall((chunk) => { const { encoding, string } = expected.pop(); assert.strictEqual(chunk.toString(encoding), string); }, encodings.length));
for (const encoding of encodings) { const string = 'abc';
// If encoding is the same as the state.encoding the string is // saved as is const expect = encoding !== streamEncoding ? Buffer.from(string, encoding).toString(streamEncoding) : string;
expected.push({ encoding, string: expect });
readable.unshift(string, encoding); } }
const r1 = new Readable({ read() {} }); r1.setEncoding(streamEncoding); checkEncoding(r1);
const r2 = new Readable({ read() {}, encoding: streamEncoding }); checkEncoding(r2);
}
{ // Both .push & .unshift should have the same behaviour // When setting an encoding, each chunk should be emitted with that encoding const encoding = 'base64';
function checkEncoding(readable) { const string = 'abc'; readable.on('data', common.mustCall((chunk) => { assert.strictEqual(chunk, Buffer.from(string).toString(encoding)); }, 2));
readable.push(string); readable.unshift(string); }
const r1 = new Readable({ read() {} }); r1.setEncoding(encoding); checkEncoding(r1);
const r2 = new Readable({ read() {}, encoding }); checkEncoding(r2);
}
{ // Check that ObjectMode works const readable = new Readable({ objectMode: true, read() {} });
const chunks = ['a', 1, {}, []];
readable.on('data', common.mustCall((chunk) => { assert.strictEqual(chunk, chunks.pop()); }, chunks.length));
for (const chunk of chunks) { readable.unshift(chunk); }}
{
// Should not throw: https://github.com/nodejs/node/issues/27192 const highWaterMark = 50; class ArrayReader extends Readable { constructor(opt) { super({ highWaterMark }); // The error happened only when pushing above hwm this.buffer = new Array(highWaterMark * 2).fill(0).map(String); } _read(size) { while (this.buffer.length) { const chunk = this.buffer.shift(); if (!this.buffer.length) { this.push(chunk); this.push(null); return true; } if (!this.push(chunk)) return; } } }
function onRead() { while (null !== (stream.read())) { // Remove the 'readable' listener before unshifting stream.removeListener('readable', onRead); stream.unshift('a'); stream.on('data', (chunk) => { console.log(chunk.length); }); break; } }
const stream = new ArrayReader(); stream.once('readable', common.mustCall(onRead)); stream.on('end', common.mustCall(() => {}));
}
std

Version Info

Tagged at
a year ago