Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove stream.emit('close') on stream end #2242

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Query extends Command {
}

/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */
row(packet, _connection) {
row(packet, _connection) {
if (packet.isEOF()) {
const status = packet.eofStatusFlags();
const moreResults = status & ServerStatus.SERVER_MORE_RESULTS_EXISTS;
Expand Down Expand Up @@ -279,10 +279,13 @@ class Query extends Command {
});
this.on('end', () => {
stream.push(null); // pushing null, indicating EOF
stream.emit('close'); // notify readers that query has completed
});
this.on('fields', fields => {
stream.emit('fields', fields); // replicate old emitter
// Node >= 18 Error: Premature close
if (process.versions.node.split(".", 1)[0] <= 14) {
stream.emit('close'); // notify readers that query has completed
}
});
return stream;
}
Expand All @@ -302,7 +305,7 @@ class Query extends Command {
Timers.clearTimeout(this.queryTimeout);
this.queryTimeout = null;
}

const err = new Error('Query inactivity timeout');
err.errorno = 'PROTOCOL_SEQUENCE_TIMEOUT';
err.code = 'PROTOCOL_SEQUENCE_TIMEOUT';
Expand Down
24 changes: 24 additions & 0 deletions test/integration/connection/test-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const common = require('../../common');
const connection = common.createConnection();
const assert = require('assert');
const { finished } = require("stream/promises");

let rows;
const rows1 = [];
Expand Down Expand Up @@ -62,8 +63,31 @@ connection.execute('SELECT * FROM announcements', (err, _rows) => {
});
});


const rowsStreamForAwait = []
async function testStreamForAwait() {
const stream = connection.execute('SELECT * FROM announcements').stream()
for await (const row of stream) {
rowsStreamForAwait.push(row)
}
}
testStreamForAwait().then()


const rowsStreamFinished = []
async function testStreamFinished() {
const stream = connection.execute('SELECT * FROM announcements').stream()
stream.on('result', row => {
rowsStreamFinished.push(row)
})
await finished(stream)
}
testStreamFinished().then()

process.on('exit', () => {
assert.deepEqual(rows.length, 2);
assert.deepEqual(rows, rows1);
assert.deepEqual(rows, rows2);
assert.deepEqual(rows, rowsStreamForAwait);
assert.deepEqual(rows, rowsStreamFinished);
});
Loading