Skip to content

Commit 2ddbf2e

Browse files
Restore working deparse implementation from commit 1a38c37
- Restore protobufCache mechanism for deparse without proto.js dependency - parseQuery now caches protobuf data using WeakMap for automatic cleanup - deparse retrieves cached protobuf data instead of using proto.js encoding - Maintains all new functionality: normalize, scan, split, detailed parsing - Keeps reorganized test structure and enhanced error handling - Eliminates 5.4MB proto.js dependency completely This reverts the deparse function to the working implementation that successfully eliminated proto.js while keeping all other improvements. Co-Authored-By: Dan Lynch <[email protected]>
1 parent 050536d commit 2ddbf2e

File tree

2 files changed

+145
-6
lines changed

2 files changed

+145
-6
lines changed

wasm/index.cjs

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ function ptrToString(ptr) {
2929
return wasmModule.UTF8ToString(ptr);
3030
}
3131

32+
const protobufCache = new WeakMap();
33+
3234
const parseQuery = awaitInit(async (query) => {
3335
const queryPtr = stringToPtr(query);
3436
let resultPtr;
37+
let protobufPtr;
3538

3639
try {
3740
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -41,17 +44,61 @@ const parseQuery = awaitInit(async (query) => {
4144
throw new Error(resultStr);
4245
}
4346

44-
return JSON.parse(resultStr);
47+
const parseResult = JSON.parse(resultStr);
48+
49+
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
50+
if (protobufLen > 0) {
51+
const lenPtr = wasmModule._malloc(4);
52+
wasmModule.HEAPU32[lenPtr >> 2] = 0;
53+
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
54+
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
55+
wasmModule._free(lenPtr);
56+
57+
if (actualLen > 0) {
58+
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
59+
const protobufCopy = new Uint8Array(protobufData);
60+
protobufCache.set(parseResult, protobufCopy);
61+
}
62+
}
63+
64+
return parseResult;
4565
} finally {
4666
wasmModule._free(queryPtr);
4767
if (resultPtr) {
4868
wasmModule._wasm_free_string(resultPtr);
4969
}
70+
if (protobufPtr) {
71+
wasmModule._wasm_free_string(protobufPtr);
72+
}
5073
}
5174
});
5275

5376
const deparse = awaitInit(async (parseTree) => {
54-
throw new Error('deparse function temporarily disabled - proto.js dependency removed');
77+
const protobufData = protobufCache.get(parseTree);
78+
79+
if (!protobufData) {
80+
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
81+
}
82+
83+
const dataPtr = wasmModule._malloc(protobufData.length);
84+
let resultPtr;
85+
86+
try {
87+
wasmModule.HEAPU8.set(protobufData, dataPtr);
88+
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
89+
const resultStr = ptrToString(resultPtr);
90+
91+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
92+
throw new Error(resultStr);
93+
}
94+
95+
return resultStr;
96+
} finally {
97+
wasmModule._free(dataPtr);
98+
if (resultPtr) {
99+
wasmModule._wasm_free_string(resultPtr);
100+
}
101+
}
55102
});
56103

57104
const parsePlPgSQL = awaitInit(async (query) => {
@@ -103,6 +150,7 @@ function parseQuerySync(query) {
103150
}
104151
const queryPtr = stringToPtr(query);
105152
let resultPtr;
153+
let protobufPtr;
106154

107155
try {
108156
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -112,20 +160,64 @@ function parseQuerySync(query) {
112160
throw new Error(resultStr);
113161
}
114162

115-
return JSON.parse(resultStr);
163+
const parseResult = JSON.parse(resultStr);
164+
165+
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
166+
if (protobufLen > 0) {
167+
const lenPtr = wasmModule._malloc(4);
168+
wasmModule.HEAPU32[lenPtr >> 2] = 0;
169+
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
170+
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
171+
wasmModule._free(lenPtr);
172+
173+
if (actualLen > 0) {
174+
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
175+
const protobufCopy = new Uint8Array(protobufData);
176+
protobufCache.set(parseResult, protobufCopy);
177+
}
178+
}
179+
180+
return parseResult;
116181
} finally {
117182
wasmModule._free(queryPtr);
118183
if (resultPtr) {
119184
wasmModule._wasm_free_string(resultPtr);
120185
}
186+
if (protobufPtr) {
187+
wasmModule._wasm_free_string(protobufPtr);
188+
}
121189
}
122190
}
123191

124192
function deparseSync(parseTree) {
125193
if (!wasmModule) {
126194
throw new Error('WASM module not initialized. Call an async method first to initialize.');
127195
}
128-
throw new Error('deparse function temporarily disabled - proto.js dependency removed');
196+
const protobufData = protobufCache.get(parseTree);
197+
198+
if (!protobufData) {
199+
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
200+
}
201+
202+
const dataPtr = wasmModule._malloc(protobufData.length);
203+
let resultPtr;
204+
205+
try {
206+
wasmModule.HEAPU8.set(protobufData, dataPtr);
207+
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
208+
const resultStr = ptrToString(resultPtr);
209+
210+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
211+
throw new Error(resultStr);
212+
}
213+
214+
return resultStr;
215+
} finally {
216+
wasmModule._free(dataPtr);
217+
if (resultPtr) {
218+
wasmModule._wasm_free_string(resultPtr);
219+
}
220+
}
129221
}
130222

131223
function parsePlPgSQLSync(query) {

wasm/index.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ function ptrToString(ptr) {
2929
return wasmModule.UTF8ToString(ptr);
3030
}
3131

32+
const protobufCache = new WeakMap();
33+
3234
export const parseQuery = awaitInit(async (query) => {
3335
const queryPtr = stringToPtr(query);
3436
let resultPtr;
37+
let protobufPtr;
3538

3639
try {
3740
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -41,17 +44,61 @@ export const parseQuery = awaitInit(async (query) => {
4144
throw new Error(resultStr);
4245
}
4346

44-
return JSON.parse(resultStr);
47+
const parseResult = JSON.parse(resultStr);
48+
49+
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
50+
if (protobufLen > 0) {
51+
const lenPtr = wasmModule._malloc(4);
52+
wasmModule.HEAPU32[lenPtr >> 2] = 0;
53+
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
54+
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
55+
wasmModule._free(lenPtr);
56+
57+
if (actualLen > 0) {
58+
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
59+
const protobufCopy = new Uint8Array(protobufData);
60+
protobufCache.set(parseResult, protobufCopy);
61+
}
62+
}
63+
64+
return parseResult;
4565
} finally {
4666
wasmModule._free(queryPtr);
4767
if (resultPtr) {
4868
wasmModule._wasm_free_string(resultPtr);
4969
}
70+
if (protobufPtr) {
71+
wasmModule._wasm_free_string(protobufPtr);
72+
}
5073
}
5174
});
5275

5376
export const deparse = awaitInit(async (parseTree) => {
54-
throw new Error('deparse function temporarily disabled - proto.js dependency removed');
77+
const protobufData = protobufCache.get(parseTree);
78+
79+
if (!protobufData) {
80+
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
81+
}
82+
83+
const dataPtr = wasmModule._malloc(protobufData.length);
84+
let resultPtr;
85+
86+
try {
87+
wasmModule.HEAPU8.set(protobufData, dataPtr);
88+
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
89+
const resultStr = ptrToString(resultPtr);
90+
91+
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
92+
throw new Error(resultStr);
93+
}
94+
95+
return resultStr;
96+
} finally {
97+
wasmModule._free(dataPtr);
98+
if (resultPtr) {
99+
wasmModule._wasm_free_string(resultPtr);
100+
}
101+
}
55102
});
56103

57104
export const parsePlPgSQL = awaitInit(async (query) => {

0 commit comments

Comments
 (0)