Closed
Description
If you don't know about it, pump is a module to better handle stream pipe (better error handling and better close handling to prevent resource leak) https://www.npmjs.com/package/pump.
It is considered by many as safer than pipe and may be part of node core in the future (nodejs/node#13506).
mysql2 streams are not working well with pump: a "premature close" error is thrown at the end of the stream.
Here is an example code to reproduce the error:
const mysql = require('mysql2/promise');
const pump = require('pump-promise');
const {Writable} = require('stream');
/**
* Main test function
*/
async function main () {
const connection = await mysql.createConnection({
/* ... */
});
const sqlReadable = connection.connection.query('select 1')
.stream();
const testWritable = new Writable({
objectMode: true,
write (chunk, encoding, callback) {
console.log(chunk);
callback();
}
});
// Not working
await pump([
sqlReadable,
testWritable
]);
// Working
//sqlReadable.pipe(testWritable);
//await new Promise(resolve => testWritable.on('finish', resolve));
connection.close();
}
main()
.then(() => console.log('ok'))
.catch((e) => {
process.exitCode = 1;
console.error(e);
});
You can uncomment the "working" code (and comment the "not working" code) to use the working pipe version.
Metadata
Metadata
Assignees
Labels
No labels