Skip to content

Commit

Permalink
Merge pull request #156 from tursodatabase/wasm-create-client
Browse files Browse the repository at this point in the history
Make Wasm createClient() sync
  • Loading branch information
penberg authored Jan 19, 2024
2 parents 38404e5 + 04ce27a commit 029e4f9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 45 deletions.
24 changes: 8 additions & 16 deletions packages/libsql-client-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"license": "MIT",
"type": "module",
"main": "lib-cjs/wasm.js",
"main": "lib-esm/wasm.js",
"types": "lib-esm/wasm.d.ts",
"exports": {
".": {
Expand All @@ -35,28 +35,23 @@
"wasm": "./lib-esm/wasm.js",
"browser": "./lib-esm/web.js",
"default": "./lib-esm/wasm.js"
},
"require": "./lib-cjs/wasm.js"
}
},
"./http": {
"types": "./lib-esm/http.d.ts",
"import": "./lib-esm/http.js",
"require": "./lib-cjs/http.js"
"import": "./lib-esm/http.js"
},
"./ws": {
"types": "./lib-esm/ws.d.ts",
"import": "./lib-esm/ws.js",
"require": "./lib-cjs/ws.js"
"import": "./lib-esm/ws.js"
},
"./wasm": {
"types": "./lib-esm/wasm.d.ts",
"import": "./lib-esm/wasm.js",
"require": "./lib-cjs/wasm.js"
"import": "./lib-esm/wasm.js"
},
"./web": {
"types": "./lib-esm/web.d.ts",
"import": "./lib-esm/web.js",
"require": "./lib-cjs/web.js"
"import": "./lib-esm/web.js"
}
},
"typesVersions": {
Expand All @@ -79,16 +74,13 @@
}
},
"files": [
"lib-cjs/**",
"lib-esm/**"
],
"scripts": {
"prepublishOnly": "npm run build",
"prebuild": "rm -rf ./lib-cjs ./lib-esm",
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "tsc -p tsconfig.build-cjs.json",
"prebuild": "rm -rf ./lib-esm",
"build": "npm run build:esm",
"build:esm": "tsc -p tsconfig.build-esm.json",
"postbuild": "cp package-cjs.json ./lib-cjs/package.json",
"test": "jest --runInBand",
"typecheck": "tsc --noEmit",
"typedoc": "rm -rf ./docs && typedoc"
Expand Down
15 changes: 4 additions & 11 deletions packages/libsql-client-wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { supportedUrlLink, transactionModeToBegin, ResultSetImpl } from "@libsql

export * from "@libsql/core/api";

export async function createClient(config: Config): Promise<Client> {
const sqlite3 = await sqlite3InitModule();

export function createClient(config: Config): Client {
return _createClient(expandConfig(config, true));
}

/** @private */
export async function _createClient(config: ExpandedConfig): Promise<Client> {
export function _createClient(config: ExpandedConfig): Client {
if (config.scheme !== "file") {
throw new LibsqlError(
`URL scheme ${JSON.stringify(config.scheme + ":")} is not supported by the local sqlite3 client. ` +
Expand Down Expand Up @@ -54,15 +56,6 @@ export async function _createClient(config: ExpandedConfig): Promise<Client> {
syncUrl: config.syncUrl,
};

const initOptions: InitOptions = {};
if(config.sqliteWasmPath) {
initOptions.locateFile = function() {
return config.sqliteWasmPath!;
};
}

const sqlite3 = await sqlite3InitModule(initOptions);

const db: Database = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');

executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened", config.intMode);
Expand Down
3 changes: 2 additions & 1 deletion packages/libsql-client-wasm/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"module": "es2022",
"moduleResolution": "node",
"lib": ["esnext", "dom"],
"target": "esnext",
"target": "es2022",
"esModuleInterop": true,
"isolatedModules": true,
"rootDir": "src/",
Expand Down
9 changes: 0 additions & 9 deletions packages/libsql-client-wasm/tsconfig.build-cjs.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/libsql-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export interface Config {
* with the Web `Response`.
*/
fetch?: Function;

/**
* Location of the sqlite3.wasm binary path.
*/
sqliteWasmPath?: string;
}

/** Representation of integers from database as JavaScript values. See {@link Config.intMode}. */
Expand Down
3 changes: 0 additions & 3 deletions packages/libsql-core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface ExpandedConfig {
syncUrl: string | undefined;
intMode: IntMode;
fetch: Function | undefined;
sqliteWasmPath?: string;
}

export type ExpandedScheme = "wss" | "ws" | "https" | "http" | "file";
Expand Down Expand Up @@ -47,7 +46,6 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi
tls: false,
authToken: undefined,
authority: undefined,
sqliteWasmPath: config.sqliteWasmPath,
};
}

Expand Down Expand Up @@ -119,6 +117,5 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi
syncUrl,
intMode,
fetch: config.fetch,
sqliteWasmPath: config.sqliteWasmPath,
};
}

0 comments on commit 029e4f9

Please sign in to comment.