|
3 | 3 | const common = require('../common');
|
4 | 4 | const assert = require('assert');
|
5 | 5 | const { Duplex, Readable, Writable, pipeline } = require('stream');
|
| 6 | +const { Blob } = require('buffer'); |
6 | 7 |
|
7 | 8 | {
|
8 | 9 | const d = Duplex.from({
|
@@ -144,3 +145,124 @@ const { Duplex, Readable, Writable, pipeline } = require('stream');
|
144 | 145 | common.mustCall(() => {}),
|
145 | 146 | );
|
146 | 147 | }
|
| 148 | + |
| 149 | + |
| 150 | +// Ensure that isDuplexNodeStream was called |
| 151 | +{ |
| 152 | + const duplex = new Duplex(); |
| 153 | + assert.strictEqual(Duplex.from(duplex), duplex); |
| 154 | +} |
| 155 | + |
| 156 | +// Ensure that Duplex.from works for blobs |
| 157 | +{ |
| 158 | + const blob = new Blob(['blob']); |
| 159 | + const expecteByteLength = blob.size; |
| 160 | + const duplex = Duplex.from(blob); |
| 161 | + duplex.on('data', common.mustCall((arrayBuffer) => { |
| 162 | + assert.strictEqual(arrayBuffer.byteLength, expecteByteLength); |
| 163 | + })); |
| 164 | +} |
| 165 | + |
| 166 | +// Ensure that given a promise rejection it emits an error |
| 167 | +{ |
| 168 | + const myErrorMessage = 'myCustomError'; |
| 169 | + Duplex.from(Promise.reject(myErrorMessage)) |
| 170 | + .on('error', common.mustCall((error) => { |
| 171 | + assert.strictEqual(error, myErrorMessage); |
| 172 | + })); |
| 173 | +} |
| 174 | + |
| 175 | +// Ensure that given a promise rejection on an async function it emits an error |
| 176 | +{ |
| 177 | + const myErrorMessage = 'myCustomError'; |
| 178 | + async function asyncFn() { |
| 179 | + return Promise.reject(myErrorMessage); |
| 180 | + } |
| 181 | + |
| 182 | + Duplex.from(asyncFn) |
| 183 | + .on('error', common.mustCall((error) => { |
| 184 | + assert.strictEqual(error, myErrorMessage); |
| 185 | + })); |
| 186 | +} |
| 187 | + |
| 188 | +// Ensure that Duplex.from throws an Invalid return value when function is void |
| 189 | +{ |
| 190 | + assert.throws(() => Duplex.from(() => {}), { |
| 191 | + code: 'ERR_INVALID_RETURN_VALUE', |
| 192 | + }); |
| 193 | +} |
| 194 | + |
| 195 | +// Ensure data if a sub object has a readable stream it's duplexified |
| 196 | +{ |
| 197 | + const msg = Buffer.from('hello'); |
| 198 | + const duplex = Duplex.from({ |
| 199 | + readable: Readable({ |
| 200 | + read() { |
| 201 | + this.push(msg); |
| 202 | + this.push(null); |
| 203 | + } |
| 204 | + }) |
| 205 | + }).on('data', common.mustCall((data) => { |
| 206 | + assert.strictEqual(data, msg); |
| 207 | + })); |
| 208 | + |
| 209 | + assert.strictEqual(duplex.writable, false); |
| 210 | +} |
| 211 | + |
| 212 | +// Ensure data if a sub object has a writable stream it's duplexified |
| 213 | +{ |
| 214 | + const msg = Buffer.from('hello'); |
| 215 | + const duplex = Duplex.from({ |
| 216 | + writable: Writable({ |
| 217 | + write: common.mustCall((data) => { |
| 218 | + assert.strictEqual(data, msg); |
| 219 | + }) |
| 220 | + }) |
| 221 | + }); |
| 222 | + |
| 223 | + duplex.write(msg); |
| 224 | + assert.strictEqual(duplex.readable, false); |
| 225 | +} |
| 226 | + |
| 227 | +// Ensure data if a sub object has a writable and readable stream it's duplexified |
| 228 | +{ |
| 229 | + const msg = Buffer.from('hello'); |
| 230 | + |
| 231 | + const duplex = Duplex.from({ |
| 232 | + readable: Readable({ |
| 233 | + read() { |
| 234 | + this.push(msg); |
| 235 | + this.push(null); |
| 236 | + } |
| 237 | + }), |
| 238 | + writable: Writable({ |
| 239 | + write: common.mustCall((data) => { |
| 240 | + assert.strictEqual(data, msg); |
| 241 | + }) |
| 242 | + }) |
| 243 | + }); |
| 244 | + |
| 245 | + duplex.pipe(duplex) |
| 246 | + .on('data', common.mustCall((data) => { |
| 247 | + assert.strictEqual(data, msg); |
| 248 | + assert.strictEqual(duplex.readable, true); |
| 249 | + assert.strictEqual(duplex.writable, true); |
| 250 | + })) |
| 251 | + .on('end', common.mustCall()); |
| 252 | +} |
| 253 | + |
| 254 | +// Ensure that given writable stream that throws an error it calls destroy |
| 255 | +{ |
| 256 | + const myErrorMessage = 'error!'; |
| 257 | + const duplex = Duplex.from(Writable({ |
| 258 | + write(chunk, enc, cb) { |
| 259 | + cb(myErrorMessage); |
| 260 | + } |
| 261 | + })); |
| 262 | + |
| 263 | + duplex.on('error', common.mustCall((msg) => { |
| 264 | + assert.strictEqual(msg, myErrorMessage); |
| 265 | + })); |
| 266 | + |
| 267 | + duplex.write('test'); |
| 268 | +} |
0 commit comments