Skip to content

Commit bf45d85

Browse files
committed
ensureLoaded
1 parent 59c3c6d commit bf45d85

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ifdef EMSCRIPTEN
6161
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \
6262
-sENVIRONMENT="web,node" \
6363
-sMODULARIZE=1 \
64+
-sEXPORT_ES6=0 \
6465
-sALLOW_MEMORY_GROWTH=1 \
6566
-lpg_query \
6667
-o $@ \

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "libpg-query",
3-
"version": "17.3.0",
3+
"version": "17.3.1",
44
"description": "The real PostgreSQL query parser",
55
"homepage": "https://github.com/launchql/libpg-query-node",
66
"main": "./wasm/index.cjs",
7+
"module": "./wasm/index.js",
78
"typings": "./wasm/index.d.ts",
89
"publishConfig": {
910
"access": "public"
@@ -12,13 +13,6 @@
1213
"wasm/*",
1314
"proto.js"
1415
],
15-
"exports": {
16-
".": {
17-
"types": "./wasm/index.d.ts",
18-
"import": "./wasm/index.js",
19-
"require": "./wasm/index.cjs"
20-
}
21-
},
2216
"scripts": {
2317
"clean": "yarn wasm:clean && rimraf cjs esm",
2418
"build:js": "node scripts/build.js",

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const initPromise = PgQueryModule().then((module: WasmModule) => {
4343
wasmModule = module;
4444
});
4545

46+
function ensureLoaded() {
47+
if (!wasmModule) throw new Error("WASM module not initialized. Call `loadModule()` first.");
48+
}
49+
4650
export async function loadModule(): Promise<void> {
4751
if (!wasmModule) {
4852
await initPromise;
@@ -57,6 +61,7 @@ function awaitInit<T extends (...args: any[]) => Promise<any>>(fn: T): T {
5761
}
5862

5963
function stringToPtr(str: string): number {
64+
ensureLoaded();
6065
const len = wasmModule.lengthBytesUTF8(str) + 1;
6166
const ptr = wasmModule._malloc(len);
6267
try {
@@ -69,6 +74,7 @@ function stringToPtr(str: string): number {
6974
}
7075

7176
function ptrToString(ptr: number): string {
77+
ensureLoaded();
7278
return wasmModule.UTF8ToString(ptr);
7379
}
7480

wasm/index.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ let wasmModule;
3434
const initPromise = (0, libpg_query_js_1.default)().then((module) => {
3535
wasmModule = module;
3636
});
37+
function ensureLoaded() {
38+
if (!wasmModule)
39+
throw new Error("WASM module not initialized. Call `loadModule()` first.");
40+
}
3741
async function loadModule() {
3842
if (!wasmModule) {
3943
await initPromise;
@@ -46,6 +50,7 @@ function awaitInit(fn) {
4650
});
4751
}
4852
function stringToPtr(str) {
53+
ensureLoaded();
4954
const len = wasmModule.lengthBytesUTF8(str) + 1;
5055
const ptr = wasmModule._malloc(len);
5156
try {
@@ -58,6 +63,7 @@ function stringToPtr(str) {
5863
}
5964
}
6065
function ptrToString(ptr) {
66+
ensureLoaded();
6167
return wasmModule.UTF8ToString(ptr);
6268
}
6369
exports.parse = awaitInit(async (query) => {

wasm/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ let wasmModule;
77
const initPromise = PgQueryModule().then((module) => {
88
wasmModule = module;
99
});
10+
function ensureLoaded() {
11+
if (!wasmModule)
12+
throw new Error("WASM module not initialized. Call `loadModule()` first.");
13+
}
1014
export async function loadModule() {
1115
if (!wasmModule) {
1216
await initPromise;
@@ -19,6 +23,7 @@ function awaitInit(fn) {
1923
});
2024
}
2125
function stringToPtr(str) {
26+
ensureLoaded();
2227
const len = wasmModule.lengthBytesUTF8(str) + 1;
2328
const ptr = wasmModule._malloc(len);
2429
try {
@@ -31,6 +36,7 @@ function stringToPtr(str) {
3136
}
3237
}
3338
function ptrToString(ptr) {
39+
ensureLoaded();
3440
return wasmModule.UTF8ToString(ptr);
3541
}
3642
export const parse = awaitInit(async (query) => {

0 commit comments

Comments
 (0)