fix(sqlite3): handle Bun structured-clone surfacing null as zero-length ArrayBuffer#250
Open
subsetpark wants to merge 1 commit into
Open
Conversation
…th ArrayBuffer Bun's worker_threads structured-clone has regressed across versions (notably the build shipped in Trigger.dev's container) and surfaces a host-side `null` dbBuffer as a zero-length ArrayBuffer inside the worker. The prior `if (data.dbBuffer)` guard treated that as truthy, so `new SQL.Database(arrayBuffer)` ran with a bare ArrayBuffer — which sql.js rejects with "Expected ArrayBuffer for the first argument" (it wants Uint8Array, not bare ArrayBuffer). Route the buffer through a `coerceDbBuffer` helper that: - returns a Uint8Array as-is (covers Buffer too, which extends Uint8Array), - wraps a non-empty ArrayBuffer in a Uint8Array, - and returns null for null / undefined / zero-length buffers — letting executeQuery fall back to `new SQL.Database()` (fresh in-memory db), which matches the host's intent for :memory: databases. No behavior change under Node; fixes :memory: invocations under affected Bun builds. Six unit tests pin each branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
@subsetpark is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
2 tasks
cramforce
reviewed
May 24, 2026
| * matching the host's intent for :memory: databases. | ||
| */ | ||
| export function coerceDbBuffer(raw: unknown): Uint8Array | null { | ||
| if (raw instanceof Uint8Array) { |
Contributor
There was a problem hiding this comment.
Explain how this change is not a security risk
Contributor
Author
There was a problem hiding this comment.
What's the vector you've got in mind?
Contributor
There was a problem hiding this comment.
It's a sensitive area of code going from simple to complicated, so it is worth asking
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bun's
worker_threadsstructured-clone has regressed across versions — most visibly in the build shipped in Trigger.dev's runtime container — and surfaces a host-sidenulldbBufferas a zero-lengthArrayBufferinside the worker thread.The current call site treats any truthy
data.dbBufferas a database image:An empty
ArrayBufferis truthy in JS, so the buggy clone slips past the guard and reaches sql.js — which rejects bareArrayBuffer(it wantsUint8Array) with:Net effect: every
:memory:sqlite3invocation throws under affected Bun builds.Change
Introduce
coerceDbBuffer(raw: unknown): Uint8Array | nullin the worker and route the buffer through it:Behavior:
Uint8Array(andBuffer, which extendsUint8Array) — returned as-isArrayBuffer— wrapped in aUint8Arraynull,undefined, zero-length buffers — collapsed tonull, soexecuteQueryfalls back tonew SQL.Database()(a fresh in-memory db, which is what the host meant for:memory:)No Node-side behavior change. Fixes
:memory:invocations under affected Bun builds.Test plan
pnpm --filter just-bash typecheck— cleanpnpm --filter just-bash exec vitest run src/commands/sqlite3/— 157/157 pass (6 new unit tests pin each branch ofcoerceDbBuffer, including the zero-lengthArrayBufferregression that previously broke)pnpm --filter just-bash check:worker-sync— clean (worker.jsrebuilds deterministically; the.jsis gitignored so it isn't shipped in this diff)Background
We've been carrying this in
flowglad/just-bashsince Trigger.dev's container shipped a Bun version that exhibits the regression. The fix is orthogonal to the dot-command preprocessor work in #249; opening as a separate PR for cleaner review.🤖 Generated with Claude Code