|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { |
| 5 | + Readable, |
| 6 | + Transform, |
| 7 | + Writable, |
| 8 | + pipelinify |
| 9 | +} = require('stream'); |
| 10 | +const assert = require('assert'); |
| 11 | + |
| 12 | +{ |
| 13 | + let res = ''; |
| 14 | + pipelinify( |
| 15 | + new Transform({ |
| 16 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 17 | + callback(null, chunk + chunk); |
| 18 | + }) |
| 19 | + }), |
| 20 | + new Transform({ |
| 21 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 22 | + callback(null, chunk.toString().toUpperCase()); |
| 23 | + }) |
| 24 | + }) |
| 25 | + ) |
| 26 | + .end('asd') |
| 27 | + .on('data', common.mustCall((buf) => { |
| 28 | + res += buf; |
| 29 | + })) |
| 30 | + .on('end', common.mustCall(() => { |
| 31 | + assert.strictEqual(res, 'ASDASD'); |
| 32 | + })); |
| 33 | +} |
| 34 | + |
| 35 | +{ |
| 36 | + let res = ''; |
| 37 | + pipelinify( |
| 38 | + Readable.from(['asd']), |
| 39 | + new Transform({ |
| 40 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 41 | + callback(null, chunk.toString().toUpperCase()); |
| 42 | + }) |
| 43 | + }) |
| 44 | + ) |
| 45 | + .on('data', common.mustCall((buf) => { |
| 46 | + res += buf; |
| 47 | + })) |
| 48 | + .on('end', common.mustCall(() => { |
| 49 | + assert.strictEqual(res, 'ASD'); |
| 50 | + })); |
| 51 | +} |
| 52 | + |
| 53 | +{ |
| 54 | + let res = ''; |
| 55 | + pipelinify( |
| 56 | + async function* () { |
| 57 | + yield 'asd'; |
| 58 | + }, |
| 59 | + new Transform({ |
| 60 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 61 | + callback(null, chunk.toString().toUpperCase()); |
| 62 | + }) |
| 63 | + }) |
| 64 | + ) |
| 65 | + .on('data', common.mustCall((buf) => { |
| 66 | + res += buf; |
| 67 | + })) |
| 68 | + .on('end', common.mustCall(() => { |
| 69 | + assert.strictEqual(res, 'ASD'); |
| 70 | + })); |
| 71 | +} |
| 72 | + |
| 73 | +{ |
| 74 | + let res = ''; |
| 75 | + pipelinify( |
| 76 | + new Transform({ |
| 77 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 78 | + callback(null, chunk.toString().toUpperCase()); |
| 79 | + }) |
| 80 | + }), |
| 81 | + async function*(source) { |
| 82 | + for await (const chunk of source) { |
| 83 | + yield chunk; |
| 84 | + } |
| 85 | + }, |
| 86 | + new Writable({ |
| 87 | + write: common.mustCall((chunk, encoding, callback) => { |
| 88 | + res += chunk; |
| 89 | + callback(null); |
| 90 | + }) |
| 91 | + }) |
| 92 | + ) |
| 93 | + .end('asd') |
| 94 | + .on('finish', common.mustCall(() => { |
| 95 | + assert.strictEqual(res, 'ASD'); |
| 96 | + })); |
| 97 | +} |
| 98 | + |
| 99 | +{ |
| 100 | + let res = ''; |
| 101 | + pipelinify( |
| 102 | + new Transform({ |
| 103 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 104 | + callback(null, chunk.toString().toUpperCase()); |
| 105 | + }) |
| 106 | + }), |
| 107 | + async function*(source) { |
| 108 | + for await (const chunk of source) { |
| 109 | + yield chunk; |
| 110 | + } |
| 111 | + }, |
| 112 | + async function(source) { |
| 113 | + for await (const chunk of source) { |
| 114 | + res += chunk; |
| 115 | + } |
| 116 | + } |
| 117 | + ) |
| 118 | + .end('asd') |
| 119 | + .on('finish', common.mustCall(() => { |
| 120 | + assert.strictEqual(res, 'ASD'); |
| 121 | + })); |
| 122 | +} |
| 123 | + |
| 124 | +{ |
| 125 | + let res; |
| 126 | + pipelinify( |
| 127 | + new Transform({ |
| 128 | + objectMode: true, |
| 129 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 130 | + callback(null, { chunk }); |
| 131 | + }) |
| 132 | + }), |
| 133 | + async function*(source) { |
| 134 | + for await (const chunk of source) { |
| 135 | + yield chunk; |
| 136 | + } |
| 137 | + }, |
| 138 | + new Transform({ |
| 139 | + objectMode: true, |
| 140 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 141 | + callback(null, { chunk }); |
| 142 | + }) |
| 143 | + }) |
| 144 | + ) |
| 145 | + .end(true) |
| 146 | + .on('data', common.mustCall((buf) => { |
| 147 | + res = buf; |
| 148 | + })) |
| 149 | + .on('end', common.mustCall(() => { |
| 150 | + assert.strictEqual(res.chunk.chunk, true); |
| 151 | + })); |
| 152 | +} |
| 153 | + |
| 154 | +{ |
| 155 | + const _err = new Error('asd'); |
| 156 | + pipelinify( |
| 157 | + new Transform({ |
| 158 | + objectMode: true, |
| 159 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 160 | + callback(_err); |
| 161 | + }) |
| 162 | + }), |
| 163 | + async function*(source) { |
| 164 | + for await (const chunk of source) { |
| 165 | + yield chunk; |
| 166 | + } |
| 167 | + }, |
| 168 | + new Transform({ |
| 169 | + objectMode: true, |
| 170 | + transform: common.mustNotCall((chunk, encoding, callback) => { |
| 171 | + callback(null, { chunk }); |
| 172 | + }) |
| 173 | + }) |
| 174 | + ) |
| 175 | + .end(true) |
| 176 | + .on('data', common.mustNotCall()) |
| 177 | + .on('end', common.mustNotCall()) |
| 178 | + .on('error', (err) => { |
| 179 | + assert.strictEqual(err, _err); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +{ |
| 184 | + const _err = new Error('asd'); |
| 185 | + pipelinify( |
| 186 | + new Transform({ |
| 187 | + objectMode: true, |
| 188 | + transform: common.mustCall((chunk, encoding, callback) => { |
| 189 | + callback(null, chunk); |
| 190 | + }) |
| 191 | + }), |
| 192 | + async function*(source) { |
| 193 | + let tmp = ''; |
| 194 | + for await (const chunk of source) { |
| 195 | + tmp += chunk; |
| 196 | + throw _err; |
| 197 | + } |
| 198 | + return tmp; |
| 199 | + }, |
| 200 | + new Transform({ |
| 201 | + objectMode: true, |
| 202 | + transform: common.mustNotCall((chunk, encoding, callback) => { |
| 203 | + callback(null, { chunk }); |
| 204 | + }) |
| 205 | + }) |
| 206 | + ) |
| 207 | + .end(true) |
| 208 | + .on('data', common.mustNotCall()) |
| 209 | + .on('end', common.mustNotCall()) |
| 210 | + .on('error', (err) => { |
| 211 | + assert.strictEqual(err, _err); |
| 212 | + }); |
| 213 | +} |
0 commit comments