forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: nodejs#32020
- Loading branch information
Showing
4 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict'; | ||
|
||
const pipeline = require('internal/streams/pipeline'); | ||
const Duplex = require('internal/streams/duplex'); | ||
const { destroyer } = require('internal/streams/destroy'); | ||
|
||
module.exports = function pipe(...streams) { | ||
let ondrain; | ||
let onfinish; | ||
let onreadable; | ||
let onclose; | ||
let d; | ||
|
||
const r = pipeline(streams, function(err) { | ||
if (onclose) { | ||
onclose(err); | ||
} else if (err) { | ||
d.destroy(err); | ||
} | ||
onclose = null; | ||
}); | ||
const w = streams[0]; | ||
|
||
// TODO (ronag): Avoid double buffering. | ||
// Implement Writable/Readable/Duplex traits. | ||
// See, https://github.com/nodejs/node/pull/33515. | ||
d = new Duplex({ | ||
writable: w.writable, | ||
readable: r.readable, | ||
objectMode: w.readableObjectMode | ||
}); | ||
|
||
if (w.writable) { | ||
d._write = function(chunk, encoding, callback) { | ||
if (w.write(chunk, encoding)) { | ||
callback(); | ||
} else { | ||
ondrain = callback; | ||
} | ||
}; | ||
|
||
d._final = function(callback) { | ||
w.end(); | ||
onfinish = callback; | ||
}; | ||
|
||
w.on('drain', function() { | ||
if (ondrain) { | ||
const cb = ondrain; | ||
ondrain = null; | ||
cb(); | ||
} | ||
}); | ||
|
||
r.on('finish', function() { | ||
if (onfinish) { | ||
const cb = onfinish; | ||
onfinish = null; | ||
cb(); | ||
} | ||
}); | ||
} | ||
|
||
if (r.readable) { | ||
r.on('readable', function() { | ||
if (onreadable) { | ||
const cb = onreadable; | ||
onreadable = null; | ||
cb(); | ||
} | ||
}); | ||
|
||
r.on('end', function() { | ||
d.push(null); | ||
}); | ||
|
||
d._read = function() { | ||
while (true) { | ||
const buf = r.read(); | ||
|
||
if (buf === null) { | ||
onreadable = d._read; | ||
return; | ||
} | ||
|
||
if (!d.push(buf)) { | ||
return; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
d._destroy = function(err, callback) { | ||
onreadable = null; | ||
ondrain = null; | ||
onfinish = null; | ||
|
||
if (onclose === null) { | ||
callback(err); | ||
} else { | ||
onclose = callback; | ||
destroyer(r, err); | ||
} | ||
}; | ||
|
||
return d; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { | ||
Readable, | ||
Transform, | ||
Writable, | ||
pipelinify | ||
} = require('stream'); | ||
const assert = require('assert'); | ||
|
||
{ | ||
let res = ''; | ||
pipelinify( | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk + chunk); | ||
}) | ||
}), | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk.toString().toUpperCase()); | ||
}) | ||
}) | ||
) | ||
.end('asd') | ||
.on('data', common.mustCall((buf) => { | ||
res += buf; | ||
})) | ||
.on('end', common.mustCall(() => { | ||
assert.strictEqual(res, 'ASDASD'); | ||
})); | ||
} | ||
|
||
{ | ||
let res = ''; | ||
pipelinify( | ||
Readable.from(['asd']), | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk.toString().toUpperCase()); | ||
}) | ||
}) | ||
) | ||
.on('data', common.mustCall((buf) => { | ||
res += buf; | ||
})) | ||
.on('end', common.mustCall(() => { | ||
assert.strictEqual(res, 'ASD'); | ||
})); | ||
} | ||
|
||
{ | ||
let res = ''; | ||
pipelinify( | ||
async function* () { | ||
yield 'asd'; | ||
}, | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk.toString().toUpperCase()); | ||
}) | ||
}) | ||
) | ||
.on('data', common.mustCall((buf) => { | ||
res += buf; | ||
})) | ||
.on('end', common.mustCall(() => { | ||
assert.strictEqual(res, 'ASD'); | ||
})); | ||
} | ||
|
||
{ | ||
let res = ''; | ||
pipelinify( | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk.toString().toUpperCase()); | ||
}) | ||
}), | ||
async function*(source) { | ||
for await (const chunk of source) { | ||
yield chunk; | ||
} | ||
}, | ||
new Writable({ | ||
write: common.mustCall((chunk, encoding, callback) => { | ||
res += chunk; | ||
callback(null); | ||
}) | ||
}) | ||
) | ||
.end('asd') | ||
.on('finish', common.mustCall(() => { | ||
assert.strictEqual(res, 'ASD'); | ||
})); | ||
} | ||
|
||
{ | ||
let res = ''; | ||
pipelinify( | ||
new Transform({ | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk.toString().toUpperCase()); | ||
}) | ||
}), | ||
async function*(source) { | ||
for await (const chunk of source) { | ||
yield chunk; | ||
} | ||
}, | ||
async function(source) { | ||
for await (const chunk of source) { | ||
res += chunk; | ||
} | ||
} | ||
) | ||
.end('asd') | ||
.on('finish', common.mustCall(() => { | ||
assert.strictEqual(res, 'ASD'); | ||
})); | ||
} | ||
|
||
{ | ||
let res; | ||
pipelinify( | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, { chunk }); | ||
}) | ||
}), | ||
async function*(source) { | ||
for await (const chunk of source) { | ||
yield chunk; | ||
} | ||
}, | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, { chunk }); | ||
}) | ||
}) | ||
) | ||
.end(true) | ||
.on('data', common.mustCall((buf) => { | ||
res = buf; | ||
})) | ||
.on('end', common.mustCall(() => { | ||
assert.strictEqual(res.chunk.chunk, true); | ||
})); | ||
} | ||
|
||
{ | ||
const _err = new Error('asd'); | ||
pipelinify( | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(_err); | ||
}) | ||
}), | ||
async function*(source) { | ||
for await (const chunk of source) { | ||
yield chunk; | ||
} | ||
}, | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustNotCall((chunk, encoding, callback) => { | ||
callback(null, { chunk }); | ||
}) | ||
}) | ||
) | ||
.end(true) | ||
.on('data', common.mustNotCall()) | ||
.on('end', common.mustNotCall()) | ||
.on('error', (err) => { | ||
assert.strictEqual(err, _err); | ||
}); | ||
} | ||
|
||
{ | ||
const _err = new Error('asd'); | ||
pipelinify( | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustCall((chunk, encoding, callback) => { | ||
callback(null, chunk); | ||
}) | ||
}), | ||
async function*(source) { | ||
let tmp = ''; | ||
for await (const chunk of source) { | ||
tmp += chunk; | ||
throw _err; | ||
} | ||
return tmp; | ||
}, | ||
new Transform({ | ||
objectMode: true, | ||
transform: common.mustNotCall((chunk, encoding, callback) => { | ||
callback(null, { chunk }); | ||
}) | ||
}) | ||
) | ||
.end(true) | ||
.on('data', common.mustNotCall()) | ||
.on('end', common.mustNotCall()) | ||
.on('error', (err) => { | ||
assert.strictEqual(err, _err); | ||
}); | ||
} |