Skip to content

Commit d3dbedf

Browse files
committed
wip
1 parent 074f4cd commit d3dbedf

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

packages/adapter-sql-js/src/SQLJSAdapter.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
113113
};
114114
}
115115

116+
/**
117+
* Preprocesses parameters to convert ArrayBuffer objects to Uint8Array
118+
* which SQL.js can properly handle in React Native environments
119+
*/
120+
protected preprocessParams(params?: any[]): any[] | undefined {
121+
if (!params) {
122+
return params;
123+
}
124+
125+
return params.map((param) => {
126+
// Only convert actual ArrayBuffer instances, be very specific to avoid
127+
// accidentally converting numeric values or other legitimate types
128+
if (param instanceof ArrayBuffer) {
129+
return new Uint8Array(param);
130+
}
131+
132+
// More conservative check for cross-context ArrayBuffer detection
133+
// Only convert if it's clearly an ArrayBuffer with the right properties
134+
if (
135+
param &&
136+
typeof param === 'object' &&
137+
param.constructor?.name === 'ArrayBuffer' &&
138+
typeof param.byteLength === 'number' &&
139+
typeof param.slice === 'function'
140+
) {
141+
return new Uint8Array(param);
142+
}
143+
144+
// Leave all other parameters unchanged (numbers, strings, etc.)
145+
return param;
146+
});
147+
}
148+
116149
protected async init(): Promise<SQLJs.Database> {
117150
const SQL = await SQLJs({
118151
locateFile: (filename: any) => `../dist/${filename}`,
@@ -143,8 +176,9 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
143176
const rawResults: any[][] = [];
144177
let columnNames: string[] | null = null;
145178
try {
146-
if (params) {
147-
statement.bind(params);
179+
const processedParams = this.preprocessParams(params);
180+
if (processedParams) {
181+
statement.bind(processedParams);
148182
}
149183
while (statement.step()) {
150184
if (!columnNames) {
@@ -194,8 +228,9 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
194228
const statement = db.prepare(query);
195229
const rawResults: any[][] = [];
196230
try {
197-
if (params) {
198-
statement.bind(params);
231+
const processedParams = this.preprocessParams(params);
232+
if (processedParams) {
233+
statement.bind(processedParams);
199234
}
200235
while (statement.step()) {
201236
rawResults.push(statement.get());
@@ -230,7 +265,8 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
230265
const stmt = db.prepare(query);
231266
try {
232267
for (const paramSet of params) {
233-
stmt.run(paramSet);
268+
const processedParams = this.preprocessParams(paramSet);
269+
stmt.run(processedParams);
234270
totalRowsAffected += db.getRowsModified();
235271
}
236272

0 commit comments

Comments
 (0)