1// Flags: --no-warnings 2'use strict'; 3 4const common = require('../common'); 5const assert = require('assert'); 6 7const { 8 arrayBuffer, 9 blob, 10 buffer, 11 text, 12 json, 13} = require('stream/consumers'); 14 15const { 16 Readable, 17 PassThrough 18} = require('stream'); 19 20const { 21 TransformStream, 22} = require('stream/web'); 23 24const buf = Buffer.from('hellothere'); 25const kArrayBuffer = 26 buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); 27 28{ 29 const passthrough = new PassThrough(); 30 31 blob(passthrough).then(common.mustCall(async (blob) => { 32 assert.strictEqual(blob.size, 10); 33 assert.deepStrictEqual(await blob.arrayBuffer(), kArrayBuffer); 34 })); 35 36 passthrough.write('hello'); 37 setTimeout(() => passthrough.end('there'), 10); 38} 39 40{ 41 const passthrough = new PassThrough(); 42 43 arrayBuffer(passthrough).then(common.mustCall(async (ab) => { 44 assert.strictEqual(ab.byteLength, 10); 45 assert.deepStrictEqual(ab, kArrayBuffer); 46 })); 47 48 passthrough.write('hello'); 49 setTimeout(() => passthrough.end('there'), 10); 50} 51 52{ 53 const passthrough = new PassThrough(); 54 55 buffer(passthrough).then(common.mustCall(async (buf) => { 56 assert.strictEqual(buf.byteLength, 10); 57 assert.deepStrictEqual(buf.buffer, kArrayBuffer); 58 })); 59 60 passthrough.write('hello'); 61 setTimeout(() => passthrough.end('there'), 10); 62} 63 64 65{ 66 const passthrough = new PassThrough(); 67 68 text(passthrough).then(common.mustCall(async (str) => { 69 assert.strictEqual(str.length, 10); 70 assert.strictEqual(str, 'hellothere'); 71 })); 72 73 passthrough.write('hello'); 74 setTimeout(() => passthrough.end('there'), 10); 75} 76 77{ 78 const readable = new Readable({ 79 read() {} 80 }); 81 82 text(readable).then((data) => { 83 assert.strictEqual(data, 'foo\ufffd\ufffd\ufffd'); 84 }); 85 86 readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80])); 87 readable.push(null); 88} 89 90{ 91 const passthrough = new PassThrough(); 92 93 json(passthrough).then(common.mustCall(async (str) => { 94 assert.strictEqual(str.length, 10); 95 assert.strictEqual(str, 'hellothere'); 96 })); 97 98 passthrough.write('"hello'); 99 setTimeout(() => passthrough.end('there"'), 10); 100} 101 102{ 103 const { writable, readable } = new TransformStream(); 104 105 blob(readable).then(common.mustCall(async (blob) => { 106 assert.strictEqual(blob.size, 10); 107 assert.deepStrictEqual(await blob.arrayBuffer(), kArrayBuffer); 108 })); 109 110 const writer = writable.getWriter(); 111 writer.write('hello'); 112 setTimeout(() => { 113 writer.write('there'); 114 writer.close(); 115 }, 10); 116 117 assert.rejects(blob(readable), { code: 'ERR_INVALID_STATE' }); 118} 119 120{ 121 const { writable, readable } = new TransformStream(); 122 123 arrayBuffer(readable).then(common.mustCall(async (ab) => { 124 assert.strictEqual(ab.byteLength, 10); 125 assert.deepStrictEqual(ab, kArrayBuffer); 126 })); 127 128 const writer = writable.getWriter(); 129 writer.write('hello'); 130 setTimeout(() => { 131 writer.write('there'); 132 writer.close(); 133 }, 10); 134 135 assert.rejects(arrayBuffer(readable), { code: 'ERR_INVALID_STATE' }); 136} 137 138{ 139 const { writable, readable } = new TransformStream(); 140 141 text(readable).then(common.mustCall(async (str) => { 142 assert.strictEqual(str.length, 10); 143 assert.strictEqual(str, 'hellothere'); 144 })); 145 146 const writer = writable.getWriter(); 147 writer.write('hello'); 148 setTimeout(() => { 149 writer.write('there'); 150 writer.close(); 151 }, 10); 152 153 assert.rejects(text(readable), { code: 'ERR_INVALID_STATE' }); 154} 155 156{ 157 const { writable, readable } = new TransformStream(); 158 159 json(readable).then(common.mustCall(async (str) => { 160 assert.strictEqual(str.length, 10); 161 assert.strictEqual(str, 'hellothere'); 162 })); 163 164 const writer = writable.getWriter(); 165 writer.write('"hello'); 166 setTimeout(() => { 167 writer.write('there"'); 168 writer.close(); 169 }, 10); 170 171 assert.rejects(json(readable), { code: 'ERR_INVALID_STATE' }); 172} 173 174{ 175 const stream = new PassThrough({ 176 readableObjectMode: true, 177 writableObjectMode: true, 178 }); 179 180 blob(stream).then(common.mustCall((blob) => { 181 assert.strictEqual(blob.size, 30); 182 })); 183 184 stream.write({}); 185 stream.end({}); 186} 187 188{ 189 const stream = new PassThrough({ 190 readableObjectMode: true, 191 writableObjectMode: true, 192 }); 193 194 arrayBuffer(stream).then(common.mustCall((ab) => { 195 assert.strictEqual(ab.byteLength, 30); 196 assert.strictEqual( 197 Buffer.from(ab).toString(), 198 '[object Object][object Object]'); 199 })); 200 201 stream.write({}); 202 stream.end({}); 203} 204 205{ 206 const stream = new PassThrough({ 207 readableObjectMode: true, 208 writableObjectMode: true, 209 }); 210 211 buffer(stream).then(common.mustCall((buf) => { 212 assert.strictEqual(buf.byteLength, 30); 213 assert.strictEqual( 214 buf.toString(), 215 '[object Object][object Object]'); 216 })); 217 218 stream.write({}); 219 stream.end({}); 220} 221 222{ 223 const stream = new PassThrough({ 224 readableObjectMode: true, 225 writableObjectMode: true, 226 }); 227 228 assert.rejects(text(stream), { 229 code: 'ERR_INVALID_ARG_TYPE', 230 }); 231 232 stream.write({}); 233 stream.end({}); 234} 235 236{ 237 const stream = new PassThrough({ 238 readableObjectMode: true, 239 writableObjectMode: true, 240 }); 241 242 assert.rejects(json(stream), { 243 code: 'ERR_INVALID_ARG_TYPE', 244 }); 245 246 stream.write({}); 247 stream.end({}); 248} 249 250{ 251 const stream = new TransformStream(); 252 text(stream.readable).then(common.mustCall((str) => { 253 // Incomplete utf8 character is flushed as a replacement char 254 assert.strictEqual(str.charCodeAt(0), 0xfffd); 255 })); 256 const writer = stream.writable.getWriter(); 257 Promise.all([ 258 writer.write(new Uint8Array([0xe2])), 259 writer.write(new Uint8Array([0x82])), 260 writer.close(), 261 ]).then(common.mustCall()); 262} 263