D1 bind(): typed arrays and DataView store text or an empty blob instead of their bytes · Issue #7785 · cloudflare/workerd · GitHub
Skip to content

D1 bind(): typed arrays and DataView store text or an empty blob instead of their bytes #7785

Open

Description

@spa5k

What happened

D1Database.prepare(...).bind(...) converts typed arrays and DataView with Array.from(view) in src/cloudflare/internal/d1-api.ts:660. Array.from enumerates the view element-wise, so the raw bytes never reach the storage layer, and the Uint8Array fast path is bypassed.

A Uint16Array([300]) is stored as the text "300", and a DataView becomes an empty blob. Binding the same bytes through a raw Durable Object SqlStorage stores them correctly, which isolates the problem to this one conversion.

What I expected

A typed array or DataView bind parameter stores its raw bytes — the documented way to bind binary data.

What I saw (production D1 and the local simulator)

bound value production stores local simulator stores
new Uint16Array([300]) TEXT "300" truncated blob 0x2C
new Float64Array([300, 1.5]) TEXT "300,1.5" truncated blob
new DataView(new ArrayBuffer(4)) empty blob empty blob
[1.5] (plain number array) byte 01 (truncated) byte 01

So the same code behaves differently in wrangler dev and in production, and binary data is not byte-recoverable.

How to reproduce

  1. Create a table with a BLOB column.
  2. Bind each value above on both paths, then read the value back (typeof, length, hex).
  3. Control: ctx.storage.sql.exec("insert ...", new Uint8Array([1,2])) from a Durable Object stores byte-wise correctly.

Reproduction scripts and the full local/remote matrices are available if useful.

Suggested fix

Replace Array.from(view) with view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength), or new Uint8Array(view.buffer, view.byteOffset, view.byteLength).

Environment

  • wrangler 4.151.1, production D1 on my own account, and the local simulator.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions