Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions templates/full/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ interface WasmModule {
_wasm_fingerprint: (queryPtr: number) => number;
_wasm_normalize_query: (queryPtr: number) => number;
_wasm_scan: (queryPtr: number) => number;
_wasm_free_detailed_result: (ptr: number) => void;
lengthBytesUTF8: (str: string) => number;
stringToUTF8: (str: string, ptr: number, len: number) => void;
UTF8ToString: (ptr: number) => string;
Expand Down Expand Up @@ -244,6 +245,28 @@ function ptrToString(ptr: number): string {
return wasmModule.UTF8ToString(ptr);
}

function readDetailedResult(resultPtr: number): string {
if (!resultPtr) {
throw new Error('libpg_query returned no result: out of WASM memory (heap limit is WASM_MAXIMUM_MEMORY, 1 GiB); parse large inputs in smaller pieces');
}
const ptrAt = (offset: number) => wasmModule.getValue(resultPtr + offset, 'i32');
const str = (ptr: number) => (ptr ? wasmModule.UTF8ToString(ptr) : undefined);
if (ptrAt(0)) {
const message = str(ptrAt(4)) ?? 'Unknown error';
const lineno = ptrAt(16);
const cursorpos = ptrAt(20);
throw new SqlError(message, {
message,
cursorPosition: cursorpos > 0 ? cursorpos - 1 : 0,
fileName: str(ptrAt(12)),
functionName: str(ptrAt(8)),
lineNumber: lineno > 0 ? lineno : undefined,
context: str(ptrAt(24))
});
}
return str(ptrAt(28)) ?? '';
}

export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
// Input validation
if (query === null || query === undefined) {
Expand Down Expand Up @@ -338,18 +361,14 @@ export const fingerprint = awaitInit(async (query: string): Promise<string> => {

try {
resultPtr = callWasm(() => wasmModule._wasm_fingerprint(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return resultStr;
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand All @@ -362,18 +381,14 @@ export const normalize = awaitInit(async (query: string): Promise<string> => {

try {
resultPtr = callWasm(() => wasmModule._wasm_normalize_query(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return resultStr;
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand Down Expand Up @@ -484,18 +499,14 @@ export function fingerprintSync(query: string): string {

try {
resultPtr = callWasm(() => wasmModule._wasm_fingerprint(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return resultStr;
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand All @@ -511,18 +522,14 @@ export function normalizeSync(query: string): string {

try {
resultPtr = callWasm(() => wasmModule._wasm_normalize_query(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return resultStr;
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand All @@ -535,18 +542,14 @@ export const scan = awaitInit(async (query: string): Promise<ScanResult> => {

try {
resultPtr = callWasm(() => wasmModule._wasm_scan(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return JSON.parse(resultStr);
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand All @@ -562,18 +565,14 @@ export function scanSync(query: string): ScanResult {

try {
resultPtr = callWasm(() => wasmModule._wasm_scan(queryPtr));
const resultStr = ptrToString(resultPtr);

if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.startsWith('ERROR')) {
throw new Error(resultStr);
}
const resultStr = readDetailedResult(resultPtr);

return JSON.parse(resultStr);
} finally {
if (failures === failuresBefore) {
wasmModule._free(queryPtr);
if (resultPtr) {
wasmModule._wasm_free_string(resultPtr);
wasmModule._wasm_free_detailed_result(resultPtr);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions templates/full/libpg-query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ declare module './libpg-query.js' {
_wasm_parse_plpgsql: (queryPtr: number) => number;
_wasm_fingerprint: (queryPtr: number) => number;
_wasm_normalize_query: (queryPtr: number) => number;
_wasm_scan: (queryPtr: number) => number;
_wasm_free_detailed_result: (ptr: number) => void;
lengthBytesUTF8: (str: string) => number;
stringToUTF8: (str: string, ptr: number, len: number) => void;
UTF8ToString: (ptr: number) => string;
getValue: (ptr: number, type: string) => number;
HEAPU8: Uint8Array;
}

Expand Down
Loading
Loading