1// Flags: --no-warnings --expose-internals 2'use strict'; 3 4const common = require('../common'); 5 6const assert = require('assert'); 7 8const { 9 newReadableWritablePairFromDuplex, 10} = require('internal/webstreams/adapters'); 11 12const { 13 PassThrough, 14} = require('stream'); 15 16{ 17 // Destroying the duplex without an error should close 18 // the readable and error the writable. 19 20 const duplex = new PassThrough(); 21 const { 22 readable, 23 writable, 24 } = newReadableWritablePairFromDuplex(duplex); 25 26 const reader = readable.getReader(); 27 const writer = writable.getWriter(); 28 29 assert.rejects(reader.closed, { 30 code: 'ABORT_ERR', 31 }); 32 33 assert.rejects(writer.closed, { 34 code: 'ABORT_ERR', 35 }); 36 37 duplex.destroy(); 38 39 duplex.on('close', common.mustCall()); 40} 41 42{ 43 // Destroying the duplex with an error should error 44 // both the readable and writable 45 46 const error = new Error('boom'); 47 const duplex = new PassThrough(); 48 const { 49 readable, 50 writable, 51 } = newReadableWritablePairFromDuplex(duplex); 52 53 duplex.on('close', common.mustCall()); 54 duplex.on('error', common.mustCall((reason) => { 55 assert.strictEqual(reason, error); 56 })); 57 58 const reader = readable.getReader(); 59 const writer = writable.getWriter(); 60 61 assert.rejects(reader.closed, error); 62 assert.rejects(writer.closed, error); 63 64 duplex.destroy(error); 65} 66 67{ 68 const error = new Error('boom'); 69 const duplex = new PassThrough(); 70 const { 71 readable, 72 writable, 73 } = newReadableWritablePairFromDuplex(duplex); 74 75 duplex.on('close', common.mustCall()); 76 duplex.on('error', common.mustCall((reason) => { 77 assert.strictEqual(reason, error); 78 })); 79 80 const reader = readable.getReader(); 81 const writer = writable.getWriter(); 82 83 reader.closed.then(common.mustCall()); 84 assert.rejects(writer.closed, error); 85 86 reader.cancel(error).then(common.mustCall()); 87} 88 89{ 90 const duplex = new PassThrough(); 91 const { 92 readable, 93 writable, 94 } = newReadableWritablePairFromDuplex(duplex); 95 96 duplex.on('close', common.mustCall()); 97 duplex.on('error', common.mustNotCall()); 98 99 const reader = readable.getReader(); 100 const writer = writable.getWriter(); 101 102 reader.closed.then(common.mustCall()); 103 writer.closed.then(common.mustCall()); 104 105 writer.close().then(common.mustCall()); 106} 107 108{ 109 const error = new Error('boom'); 110 const duplex = new PassThrough(); 111 const { 112 readable, 113 writable, 114 } = newReadableWritablePairFromDuplex(duplex); 115 116 duplex.on('close', common.mustCall()); 117 duplex.on('error', common.mustCall((reason) => { 118 assert.strictEqual(reason, error); 119 })); 120 121 const reader = readable.getReader(); 122 const writer = writable.getWriter(); 123 124 assert.rejects(reader.closed, error); 125 assert.rejects(writer.closed, error); 126 127 writer.abort(error).then(common.mustCall()); 128} 129 130{ 131 const duplex = new PassThrough(); 132 const { 133 readable, 134 writable, 135 } = newReadableWritablePairFromDuplex(duplex); 136 137 duplex.on('close', common.mustCall()); 138 139 duplex.on('error', common.mustCall((error) => { 140 assert.strictEqual(error.code, 'ABORT_ERR'); 141 })); 142 143 const reader = readable.getReader(); 144 const writer = writable.getWriter(); 145 146 assert.rejects(writer.closed, { 147 code: 'ABORT_ERR', 148 }); 149 150 reader.cancel(); 151} 152 153{ 154 const duplex = new PassThrough(); 155 const { 156 readable, 157 writable, 158 } = newReadableWritablePairFromDuplex(duplex); 159 160 duplex.on('close', common.mustCall()); 161 duplex.on('error', common.mustNotCall()); 162 163 const reader = readable.getReader(); 164 const writer = writable.getWriter(); 165 166 reader.closed.then(common.mustCall()); 167 assert.rejects(writer.closed, { 168 code: 'ABORT_ERR', 169 }); 170 171 duplex.end(); 172} 173 174{ 175 const duplex = new PassThrough(); 176 const { 177 readable, 178 writable, 179 } = newReadableWritablePairFromDuplex(duplex); 180 181 duplex.on('data', common.mustCall(2)); 182 duplex.on('close', common.mustCall()); 183 duplex.on('end', common.mustCall()); 184 duplex.on('finish', common.mustCall()); 185 186 const writer = writable.getWriter(); 187 const reader = readable.getReader(); 188 189 const ec = new TextEncoder(); 190 const dc = new TextDecoder(); 191 192 Promise.all([ 193 writer.write(ec.encode('hello')), 194 reader.read().then(common.mustCall(({ done, value }) => { 195 assert(!done); 196 assert.strictEqual(dc.decode(value), 'hello'); 197 })), 198 reader.read().then(common.mustCall(({ done, value }) => { 199 assert(!done); 200 assert.strictEqual(dc.decode(value), 'there'); 201 })), 202 writer.write(ec.encode('there')), 203 writer.close(), 204 reader.read().then(common.mustCall(({ done, value }) => { 205 assert(done); 206 assert.strictEqual(value, undefined); 207 })), 208 ]).then(common.mustCall()); 209} 210 211{ 212 const duplex = new PassThrough(); 213 duplex.destroy(); 214 const { 215 readable, 216 writable, 217 } = newReadableWritablePairFromDuplex(duplex); 218 const reader = readable.getReader(); 219 const writer = writable.getWriter(); 220 reader.closed.then(common.mustCall()); 221 writer.closed.then(common.mustCall()); 222} 223 224{ 225 const duplex = new PassThrough({ writable: false }); 226 assert(duplex.readable); 227 assert(!duplex.writable); 228 const { 229 readable, 230 writable, 231 } = newReadableWritablePairFromDuplex(duplex); 232 const reader = readable.getReader(); 233 const writer = writable.getWriter(); 234 writer.closed.then(common.mustCall()); 235 reader.cancel().then(common.mustCall()); 236} 237 238{ 239 const duplex = new PassThrough({ readable: false }); 240 assert(!duplex.readable); 241 assert(duplex.writable); 242 const { 243 readable, 244 writable, 245 } = newReadableWritablePairFromDuplex(duplex); 246 const reader = readable.getReader(); 247 const writer = writable.getWriter(); 248 reader.closed.then(common.mustCall()); 249 writer.close().then(common.mustCall()); 250} 251