Skip to content

Commit 332ddf6

Browse files
t3dotggclaude
andcommitted
fix(shared): large-binding check no longer runs getters or misses raw buffers
Measuring a named-parameter object read its values, so SQLite ran each getter a second time. The check now reads data properties only and sends an object with a getter to a throwaway statement. ArrayBuffer and SharedArrayBuffer blobs now count toward the limit like buffer views do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 36f5f64 commit 332ddf6

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎packages/shared/src/nodeSqliteClient.test.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,18 @@ it.effect("keeps large text and blob parameters off cached statements", () =>
153153

154154
const small = "small";
155155
const queries = [
156-
(value: string | Uint8Array) => sql`SELECT length(${value}) AS size`,
157-
(value: string | Uint8Array) =>
156+
(value: string | Uint8Array | ArrayBuffer) => sql`SELECT length(${value}) AS size`,
157+
(value: string | Uint8Array | ArrayBuffer) =>
158158
sql.unsafe("SELECT length($value) AS size", [{ $value: value }]),
159-
(value: string | Uint8Array) =>
159+
(value: string | Uint8Array | ArrayBuffer) =>
160160
sql.unsafe("SELECT length($value) AS callable", [
161161
Object.assign(() => {}, { $value: value }),
162162
]),
163163
];
164164
const cached = new Set<NodeSqlite.StatementSync>();
165165
const large = new Set<NodeSqlite.StatementSync>();
166-
for (const value of [small, largeText, new Uint8Array(64 * 1024 + 1), small]) {
166+
const largeBlobs = [new Uint8Array(64 * 1024 + 1), new ArrayBuffer(64 * 1024 + 1)];
167+
for (const value of [small, largeText, ...largeBlobs, small]) {
167168
const expected = typeof value === "string" ? value.length : value.byteLength;
168169
for (const query of queries) {
169170
assert.deepEqual((yield* query(value)).map(Object.values), [[expected]]);
@@ -177,7 +178,7 @@ it.effect("keeps large text and blob parameters off cached statements", () =>
177178
// Small parameters keep reusing the one cached statement for each query.
178179
assert.equal(cached.size, queries.length);
179180
// Each large execution gets a statement that nothing holds on to afterwards.
180-
assert.equal(large.size, queries.length * 4);
181+
assert.equal(large.size, queries.length * 6);
181182
assert.isTrue(cached.isDisjointFrom(large));
182183
}).pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:" }))),
183184
);
@@ -213,6 +214,16 @@ it.effect("leaves a parameter it cannot measure for the statement to handle", ()
213214
};
214215
const error = yield* sql.unsafe("SELECT length($value) AS size", [throwing]).pipe(Effect.flip);
215216
assert.equal(error.reason.operation, "execute");
217+
218+
// Only the statement reads a getter, so it runs once as it would natively.
219+
let reads = 0;
220+
const counting = {
221+
get $value(): number {
222+
return ++reads;
223+
},
224+
};
225+
assert.deepEqual(yield* sql.unsafe("SELECT $value AS value", [counting]), [{ value: 1 }]);
226+
assert.equal(reads, 1);
216227
}).pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:" }))),
217228
);
218229

‎packages/shared/src/nodeSqliteClient.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @module SqliteClient
66
*/
77
import * as NodeSqlite from "node:sqlite";
8+
import * as NodeUtil from "node:util";
89
import * as NodeWorkerThreads from "node:worker_threads";
910

1011
import * as Cache from "effect/Cache";
@@ -131,15 +132,20 @@ const classifyError = (cause: unknown, message: string, operation: string) => {
131132
* statement is bound again or finalized, and `node:sqlite` cannot clear them.
132133
* A cached statement would hold its last payload for as long as it stays
133134
* cached, so executions binding more than this many string code units and
134-
* blob bytes get a throwaway statement instead.
135+
* blob bytes, or a named parameter behind a getter, get a throwaway statement
136+
* instead.
135137
*/
136138
const LARGE_BINDING_SIZE = 64 * 1024;
137139

140+
// `node:sqlite` binds both buffer views and raw buffers as blobs.
141+
const isBlob = (value: unknown): value is ArrayBufferView | ArrayBufferLike =>
142+
ArrayBuffer.isView(value) || NodeUtil.types.isAnyArrayBuffer(value);
143+
138144
const bindingSize = (value: unknown): number => {
139145
if (typeof value === "string") {
140146
return value.length;
141147
}
142-
return ArrayBuffer.isView(value) ? value.byteLength : 0;
148+
return isBlob(value) ? value.byteLength : 0;
143149
};
144150

145151
const hasLargeBinding = (params: ReadonlyArray<unknown>): boolean => {
@@ -149,11 +155,14 @@ const hasLargeBinding = (params: ReadonlyArray<unknown>): boolean => {
149155
if (
150156
(typeof param === "object" || typeof param === "function") &&
151157
param !== null &&
152-
!ArrayBuffer.isView(param)
158+
!isBlob(param)
153159
) {
154160
// `node:sqlite` takes named parameters as one object, callable or not.
155-
for (const value of Object.values(param)) {
156-
size += bindingSize(value);
161+
// Read data properties only: calling a getter here would run it twice.
162+
for (const property of Object.values(Object.getOwnPropertyDescriptors(param))) {
163+
if (!property.enumerable) continue;
164+
if (!("value" in property)) return true;
165+
size += bindingSize(property.value);
157166
}
158167
} else {
159168
size += bindingSize(param);

0 commit comments

Comments
 (0)