Skip to content

fix: prevent TypeError in PreparedStatement.execute() when streaming without callback#1849

Merged
dhensby merged 1 commit into
tediousjs:masterfrom
dhensby:fix/ps-execute-stream
May 5, 2026
Merged

fix: prevent TypeError in PreparedStatement.execute() when streaming without callback#1849
dhensby merged 1 commit into
tediousjs:masterfrom
dhensby:fix/ps-execute-stream

Conversation

@dhensby
Copy link
Copy Markdown
Collaborator

@dhensby dhensby commented Apr 24, 2026

Summary

Fixes a TypeError: callback is not a function that occurs when calling ps.execute(values) with ps.stream = true and no callback provided.

Root Cause

In _execute(), the inner req.execute('sp_execute', callback) unconditionally calls callback(null, result) on completion — but when streaming without a callback, callback is undefined.

Fix

Guard the callback parameter in _execute() so a no-op function is used when no callback is provided. This allows stream consumers to receive results via row/error/done events on the returned Request object, consistent with how Request.query(), Request.batch(), and Request.execute() handle streaming.

Test

Added a unit test that verifies ps.execute({}) does not throw when stream = true and no callback is given. The test confirms the TypeError regression on master and passes with the fix.

Closes #1848

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug in the base PreparedStatement execution path so streaming prepared statements can be executed without supplying a callback, matching the event-driven streaming pattern already used elsewhere in the library.

Changes:

  • Guarded the callback passed from PreparedStatement._execute() to the inner Request.execute() call.
  • Added a regression test for ps.execute({}) with ps.stream = true and no callback.
  • Kept the change localized to the base prepared-statement implementation and unit tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/base/prepared-statement.js Replaces the unconditional callback handoff with a guarded no-op fallback in prepared-statement execution.
test/common/unit.js Adds a regression test covering streaming PreparedStatement.execute() without a callback.

Comment thread test/common/unit.js Outdated
@dhensby
Copy link
Copy Markdown
Collaborator Author

dhensby commented May 4, 2026

Good point — added an integration test (prepared statement with streaming and no callback) that exercises the full driver-specific streaming path with a real prepared statement, asserting that rows and done events fire correctly without a callback. See fixup commit 4bcf8fd.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread test/common/unit.js Outdated
…without callback

When stream=true and no callback is provided, _execute() would call
callback(null, result) on the undefined callback, throwing TypeError.

Guard the callback in _execute() so a no-op is used when no callback
is provided, allowing stream consumers to receive results via events
on the returned Request object.

Closes tediousjs#1848

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dhensby dhensby force-pushed the fix/ps-execute-stream branch from 127190a to 7934ff1 Compare May 4, 2026 22:50
@dhensby dhensby merged commit dd22da2 into tediousjs:master May 5, 2026
47 checks passed
@dhensby dhensby deleted the fix/ps-execute-stream branch May 5, 2026 06:16
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🎉 This PR is included in version 12.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PreparedStatement.execute() throws TypeError when stream=true and no callback provided

2 participants