Skip to content

Commit 23e8602

Browse files
Add parseQueryDetailedSync exports and update README to use npm commands
- Add parseQueryDetailedSync function and export to index.js - Ensure parseQueryDetailedSync is properly exported in wasm/index.cjs - Update README to use npm commands instead of yarn for documentation - All 32 tests passing, confirming functionality works correctly - Skip esbuild implementation as requested Co-Authored-By: Dan Lynch <[email protected]>
1 parent f4e9c4f commit 23e8602

File tree

4 files changed

+238
-12
lines changed

4 files changed

+238
-12
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,21 @@ This package uses a **WASM-only build system** for true cross-platform compatibi
216216
1. **Install dependencies:**
217217
```bash
218218
npm install
219-
# or
220-
yarn install
221219
```
222220

223221
2. **Build WASM artifacts:**
224222
```bash
225223
npm run wasm:build
226-
# or
227-
yarn wasm:build
228224
```
229225

230226
3. **Clean WASM build (if needed):**
231227
```bash
232228
npm run wasm:clean
233-
# or
234-
yarn wasm:clean
235229
```
236230

237231
4. **Rebuild WASM artifacts from scratch:**
238232
```bash
239233
npm run wasm:clean && npm run wasm:build
240-
# or
241-
yarn wasm:clean && yarn wasm:build
242234
```
243235

244236
### Build Process Details
@@ -255,8 +247,6 @@ The WASM build process:
255247

256248
```bash
257249
npm test
258-
# or
259-
yarn test
260250
```
261251

262252
### Test Requirements

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function normalizeSync(query) {
2121
return wasmModule.normalizeSync(query);
2222
}
2323

24-
24+
function parseQueryDetailedSync(query) {
25+
return wasmModule.parseQueryDetailedSync(query);
26+
}
2527

2628
module.exports = {
2729
parseQuery: wasmModule.parseQuery,
@@ -35,5 +37,6 @@ module.exports = {
3537
deparseSync,
3638
parsePlPgSQLSync,
3739
fingerprintSync,
38-
normalizeSync
40+
normalizeSync,
41+
parseQueryDetailedSync
3942
};

wasm/index.cjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,48 @@ function normalizeSync(query) {
354354
}
355355
}
356356

357+
function parseQueryDetailedSync(query) {
358+
if (!wasmModule) {
359+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
360+
}
361+
const queryPtr = stringToPtr(query);
362+
let resultPtr;
363+
364+
try {
365+
resultPtr = wasmModule._wasm_parse_query_detailed(queryPtr);
366+
367+
const hasError = wasmModule.HEAPU32[resultPtr >> 2];
368+
369+
if (hasError) {
370+
const messagePtr = wasmModule.HEAPU32[(resultPtr + 4) >> 2];
371+
const funcnamePtr = wasmModule.HEAPU32[(resultPtr + 8) >> 2];
372+
const filenamePtr = wasmModule.HEAPU32[(resultPtr + 12) >> 2];
373+
const lineno = wasmModule.HEAPU32[(resultPtr + 16) >> 2];
374+
const cursorpos = wasmModule.HEAPU32[(resultPtr + 20) >> 2];
375+
const contextPtr = wasmModule.HEAPU32[(resultPtr + 24) >> 2];
376+
377+
const error = {
378+
message: messagePtr ? ptrToString(messagePtr) : '',
379+
funcname: funcnamePtr ? ptrToString(funcnamePtr) : null,
380+
filename: filenamePtr ? ptrToString(filenamePtr) : null,
381+
lineno: lineno,
382+
cursorpos: cursorpos,
383+
context: contextPtr ? ptrToString(contextPtr) : null
384+
};
385+
386+
wasmModule._wasm_free_detailed_result(resultPtr);
387+
throw new Error(error.message);
388+
} else {
389+
const dataPtr = wasmModule.HEAPU32[(resultPtr + 28) >> 2];
390+
const result = JSON.parse(ptrToString(dataPtr));
391+
wasmModule._wasm_free_detailed_result(resultPtr);
392+
return result;
393+
}
394+
} finally {
395+
wasmModule._free(queryPtr);
396+
}
397+
}
398+
357399

358400

359401
module.exports = {
@@ -368,5 +410,6 @@ module.exports = {
368410
parsePlPgSQLSync,
369411
fingerprintSync,
370412
normalizeSync,
413+
parseQueryDetailedSync,
371414
initPromise
372415
};

wasm/index.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,193 @@ export const parseQueryDetailed = awaitInit(async (query) => {
204204
wasmModule._free(queryPtr);
205205
}
206206
});
207+
208+
export function parseQuerySync(query) {
209+
if (!wasmModule) {
210+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
211+
}
212+
const queryPtr = stringToPtr(query);
213+
let resultPtr;
214+
let protobufPtr;
215+
216+
try {
217+
resultPtr = wasmModule._wasm_parse_query(queryPtr);
218+
const resultStr = ptrToString(resultPtr);
219+
220+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
221+
throw new Error(resultStr);
222+
}
223+
224+
const parseResult = JSON.parse(resultStr);
225+
226+
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
227+
if (protobufLen > 0) {
228+
const lenPtr = wasmModule._malloc(4);
229+
wasmModule.HEAPU32[lenPtr >> 2] = 0;
230+
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
231+
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
232+
wasmModule._free(lenPtr);
233+
234+
if (actualLen > 0) {
235+
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
236+
const protobufCopy = new Uint8Array(protobufData);
237+
protobufCache.set(parseResult, protobufCopy);
238+
}
239+
}
240+
241+
return parseResult;
242+
} finally {
243+
wasmModule._free(queryPtr);
244+
if (resultPtr) {
245+
wasmModule._wasm_free_string(resultPtr);
246+
}
247+
if (protobufPtr) {
248+
wasmModule._wasm_free_string(protobufPtr);
249+
}
250+
}
251+
}
252+
253+
export function deparseSync(parseTree) {
254+
if (!wasmModule) {
255+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
256+
}
257+
const protobufData = protobufCache.get(parseTree);
258+
259+
if (!protobufData) {
260+
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
261+
}
262+
263+
const dataPtr = wasmModule._malloc(protobufData.length);
264+
let resultPtr;
265+
266+
try {
267+
wasmModule.HEAPU8.set(protobufData, dataPtr);
268+
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
269+
const resultStr = ptrToString(resultPtr);
270+
271+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
272+
throw new Error(resultStr);
273+
}
274+
275+
return resultStr;
276+
} finally {
277+
wasmModule._free(dataPtr);
278+
if (resultPtr) {
279+
wasmModule._wasm_free_string(resultPtr);
280+
}
281+
}
282+
}
283+
284+
export function parsePlPgSQLSync(query) {
285+
if (!wasmModule) {
286+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
287+
}
288+
const queryPtr = stringToPtr(query);
289+
let resultPtr;
290+
291+
try {
292+
resultPtr = wasmModule._wasm_parse_plpgsql(queryPtr);
293+
const resultStr = ptrToString(resultPtr);
294+
295+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
296+
throw new Error(resultStr);
297+
}
298+
299+
return JSON.parse(resultStr);
300+
} finally {
301+
wasmModule._free(queryPtr);
302+
if (resultPtr) {
303+
wasmModule._wasm_free_string(resultPtr);
304+
}
305+
}
306+
}
307+
308+
export function fingerprintSync(query) {
309+
if (!wasmModule) {
310+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
311+
}
312+
const queryPtr = stringToPtr(query);
313+
let resultPtr;
314+
315+
try {
316+
resultPtr = wasmModule._wasm_fingerprint(queryPtr);
317+
const resultStr = ptrToString(resultPtr);
318+
319+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
320+
throw new Error(resultStr);
321+
}
322+
323+
return resultStr;
324+
} finally {
325+
wasmModule._free(queryPtr);
326+
if (resultPtr) {
327+
wasmModule._wasm_free_string(resultPtr);
328+
}
329+
}
330+
}
331+
332+
export function normalizeSync(query) {
333+
if (!wasmModule) {
334+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
335+
}
336+
const queryPtr = stringToPtr(query);
337+
let resultPtr;
338+
339+
try {
340+
resultPtr = wasmModule._wasm_normalize_query(queryPtr);
341+
const resultStr = ptrToString(resultPtr);
342+
343+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
344+
throw new Error(resultStr);
345+
}
346+
347+
return resultStr;
348+
} finally {
349+
wasmModule._free(queryPtr);
350+
if (resultPtr) {
351+
wasmModule._wasm_free_string(resultPtr);
352+
}
353+
}
354+
}
355+
356+
export function parseQueryDetailedSync(query) {
357+
if (!wasmModule) {
358+
throw new Error('WASM module not initialized. Call an async method first to initialize.');
359+
}
360+
const queryPtr = stringToPtr(query);
361+
let resultPtr;
362+
363+
try {
364+
resultPtr = wasmModule._wasm_parse_query_detailed(queryPtr);
365+
366+
const hasError = wasmModule.HEAPU32[resultPtr >> 2];
367+
368+
if (hasError) {
369+
const messagePtr = wasmModule.HEAPU32[(resultPtr + 4) >> 2];
370+
const funcnamePtr = wasmModule.HEAPU32[(resultPtr + 8) >> 2];
371+
const filenamePtr = wasmModule.HEAPU32[(resultPtr + 12) >> 2];
372+
const lineno = wasmModule.HEAPU32[(resultPtr + 16) >> 2];
373+
const cursorpos = wasmModule.HEAPU32[(resultPtr + 20) >> 2];
374+
const contextPtr = wasmModule.HEAPU32[(resultPtr + 24) >> 2];
375+
376+
const error = {
377+
message: messagePtr ? ptrToString(messagePtr) : '',
378+
funcname: funcnamePtr ? ptrToString(funcnamePtr) : null,
379+
filename: filenamePtr ? ptrToString(filenamePtr) : null,
380+
lineno: lineno,
381+
cursorpos: cursorpos,
382+
context: contextPtr ? ptrToString(contextPtr) : null
383+
};
384+
385+
wasmModule._wasm_free_detailed_result(resultPtr);
386+
throw new Error(error.message);
387+
} else {
388+
const dataPtr = wasmModule.HEAPU32[(resultPtr + 28) >> 2];
389+
const result = JSON.parse(ptrToString(dataPtr));
390+
wasmModule._wasm_free_detailed_result(resultPtr);
391+
return result;
392+
}
393+
} finally {
394+
wasmModule._free(queryPtr);
395+
}
396+
}

0 commit comments

Comments
 (0)