Skip to content

Commit e532a2a

Browse files
committed
add opfs_util and fix
1 parent 64c7288 commit e532a2a

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

packages/duckdb-wasm/src/bindings/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ export interface DuckDBFilesystemConfig {
3131

3232
export interface DuckDBOPFSConfig {
3333
/**
34-
* Auto Opfs File Registration
34+
* Defines how `opfs://` files are handled during SQL execution.
35+
* - "auto": Automatically register `opfs://` files and drop them after execution.
36+
* - "manual": Files must be manually registered and dropped.
3537
*/
36-
autoFileRegistration?: boolean;
38+
fileHandling?: "auto" | "manual";
3739
}
3840

3941
export enum DuckDBAccessMode {

packages/duckdb-wasm/src/parallel/async_bindings.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import { InstantiationProgress } from '../bindings/progress';
1818
import { arrowToSQLField } from '../json_typedef';
1919
import { WebFile } from '../bindings/web_file';
2020
import { DuckDBDataProtocol } from '../bindings';
21+
import { searchOPFSFiles, isOPFSProtocol } from "../utils/opfs_util";
2122

2223
const TEXT_ENCODER = new TextEncoder();
23-
const REGEX_OPFS_FILE = /'(opfs:\/\/\S*?)'/g;
24-
const REGEX_OPFS_PROTOCOL = /(opfs:\/\/\S*?)/g;
2524

2625
export class AsyncDuckDB implements AsyncDuckDBBindings {
2726
/** The message handler */
@@ -404,8 +403,8 @@ export class AsyncDuckDB implements AsyncDuckDBBindings {
404403

405404
/** Run a query */
406405
public async runQuery(conn: ConnectionID, text: string): Promise<Uint8Array> {
407-
if( this.isOpenedOPFSAutoFileRegistration() ){
408-
const files = await this._preFileRegistration(text);
406+
if( this.shouldOPFSFileHandling() ){
407+
const files = await this.registerOPFSFileFromSQL(text);
409408
try {
410409
return await this._runQueryAsync(conn, text);
411410
} finally {
@@ -432,8 +431,8 @@ export class AsyncDuckDB implements AsyncDuckDBBindings {
432431
text: string,
433432
allowStreamResult: boolean = false,
434433
): Promise<Uint8Array | null> {
435-
if( this.isOpenedOPFSAutoFileRegistration() ){
436-
const files = await this._preFileRegistration(text);
434+
if( this.shouldOPFSFileHandling() ){
435+
const files = await this.registerOPFSFileFromSQL(text);
437436
try {
438437
return await this._startPendingQueryAsync(conn, text, allowStreamResult);
439438
} finally {
@@ -693,16 +692,15 @@ export class AsyncDuckDB implements AsyncDuckDBBindings {
693692
await this.postTask(task);
694693
}
695694

696-
private isOpenedOPFSAutoFileRegistration():boolean {
697-
let path = this.config.path ?? "";
698-
if( path.search(REGEX_OPFS_PROTOCOL) > -1){
699-
return this.config.opfs?.autoFileRegistration ?? false;
695+
private shouldOPFSFileHandling():boolean {
696+
if( isOPFSProtocol(this.config.path ?? "")){
697+
return this.config.opfs?.fileHandling == "auto";
700698
}
701699
return false;
702700
}
703701

704-
private async _preFileRegistration(text: string) {
705-
const files = [...text.matchAll(REGEX_OPFS_FILE)].map(match => match[1]);
702+
private async registerOPFSFileFromSQL(text: string) {
703+
const files = searchOPFSFiles(text);
706704
const result: string[] = [];
707705
for (const file of files) {
708706
try {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const REGEX_OPFS_FILE = /'(opfs:\/\/\S*?)'/g;
2+
export const REGEX_OPFS_PROTOCOL = /(opfs:\/\/\S*?)/g;
3+
4+
export function isOPFSProtocol(path: string): boolean {
5+
return path.search(REGEX_OPFS_PROTOCOL) > -1;
6+
}
7+
8+
export function searchOPFSFiles(text: string) {
9+
return [...text.matchAll(REGEX_OPFS_FILE)].map(match => match[1]);
10+
}

packages/duckdb-wasm/test/opfs.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
284284
await db.reset();
285285
await db.dropFile('test.parquet');
286286
db.config.opfs = {
287-
autoFileRegistration: true
287+
fileHandling: "auto"
288288
};
289289
conn = await db.connect();
290290
//2. send query
@@ -307,7 +307,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
307307
await db.reset();
308308
await db.dropFile('test.parquet');
309309
db.config.opfs = {
310-
autoFileRegistration: true
310+
fileHandling: "auto"
311311
};
312312
conn = await db.connect();
313313
//2. send query
@@ -332,7 +332,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
332332
await db.reset();
333333
await db.dropFile('datadir/test.parquet');
334334
db.config.opfs = {
335-
autoFileRegistration: true
335+
fileHandling: "auto"
336336
};
337337
conn = await db.connect();
338338
//2. send query
@@ -365,7 +365,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
365365
it('Copy CSV to OPFS + Load CSV', async () => {
366366
//1. data preparation
367367
db.config.opfs = {
368-
autoFileRegistration: true
368+
fileHandling: "auto"
369369
};
370370
await conn.query(`COPY ( SELECT 32 AS value ) TO 'opfs://file.csv'`);
371371
await conn.query(`COPY ( SELECT 42 AS value ) TO 'opfs://file.csv'`);

0 commit comments

Comments
 (0)