Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
82aa3b9
Add C.md documentation for libpg_query functions and WASM wrapper imp…
devin-ai-integration[bot] Jun 15, 2025
b65965b
Implement WASM wrapper improvements to eliminate proto.js dependency
devin-ai-integration[bot] Jun 15, 2025
1a38c37
Fix deparse error messages to match test expectations
devin-ai-integration[bot] Jun 15, 2025
1845097
Add reorganized test structure and new function implementations
devin-ai-integration[bot] Jun 15, 2025
b5248d9
Update package-lock.json
devin-ai-integration[bot] Jun 15, 2025
050536d
Fix C compilation errors and remove proto.js dependency
devin-ai-integration[bot] Jun 15, 2025
2ddbf2e
Restore working deparse implementation from commit 1a38c37
devin-ai-integration[bot] Jun 15, 2025
40b56e5
Complete implementation of additional libpg_query functions
devin-ai-integration[bot] Jun 15, 2025
b10fd34
Fix all remaining test failures
devin-ai-integration[bot] Jun 15, 2025
b121a0e
Add verbose logging to Emscripten build process
devin-ai-integration[bot] Jun 15, 2025
7d9ea24
Fix WASM wrapper systematic issues
devin-ai-integration[bot] Jun 15, 2025
a303b88
Fix WASM wrapper systematic issues
devin-ai-integration[bot] Jun 15, 2025
d70394b
Remove wasm_scan_query and wasm_split_statements functions
devin-ai-integration[bot] Jun 15, 2025
e67134d
Add input validation to remaining functions and fix safe_strdup
devin-ai-integration[bot] Jun 15, 2025
0585f66
Fix hardcoded buffer size additions with exact calculations
devin-ai-integration[bot] Jun 15, 2025
a124e18
Standardize error handling across WASM wrapper functions
devin-ai-integration[bot] Jun 15, 2025
07195b9
Fix critical memory management bugs in WASM wrapper
devin-ai-integration[bot] Jun 15, 2025
caa0768
Remove unused scripts/ directory and protogen script reference
devin-ai-integration[bot] Jun 15, 2025
03e1621
Complete function documentation and remove broken scan/split exports
devin-ai-integration[bot] Jun 15, 2025
83cabaf
Remove C.md planning document
devin-ai-integration[bot] Jun 15, 2025
f4e9c4f
Remove proto.js file and update dependencies
devin-ai-integration[bot] Jun 15, 2025
23e8602
Add parseQueryDetailedSync exports and update README to use npm commands
devin-ai-integration[bot] Jun 15, 2025
2241142
Remove docker folder and update build documentation
devin-ai-integration[bot] Jun 15, 2025
c836d05
Restore docker commands in package.json
devin-ai-integration[bot] Jun 15, 2025
389e2e7
Remove unused dependencies from package.json
devin-ai-integration[bot] Jun 15, 2025
8f40283
Remove test/index.js and replace lodash with pure JavaScript
devin-ai-integration[bot] Jun 15, 2025
f95b0d0
Add isReady function to check WASM module initialization
devin-ai-integration[bot] Jun 15, 2025
f88d859
Update CHANGELOG.md with comprehensive v17.2.1 improvements
devin-ai-integration[bot] Jun 15, 2025
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
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test
.travis.yml
package.json
build
docker

libpg_query/**/*.a
libpg_query/**/*.h

Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 17.2.1
* Remove proto.js dependency (5.4MB bundle size reduction)
* Add normalize() and normalizeSync() functions for SQL normalization
* Add parseQueryDetailed() and parseQueryDetailedSync() with enhanced error reporting
* Add fingerprint() and fingerprintSync() functions for query fingerprinting
* Add isReady() function to check WASM module initialization status
* Improve memory management and error handling in WASM wrapper
* Remove unused dependencies (lodash, deasync, @launchql/protobufjs)
* Split test suite into separate files for better organization
* Update documentation with comprehensive function examples

# 1.2.1
* Rename package

Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR)
# Build libpg-query-node WASM module
$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES)
ifdef EMSCRIPTEN
@ $(CC) \
$(CC) \
-v \
$(CXXFLAGS) \
-I$(LIBPG_QUERY_DIR) \
-L$(LIBPG_QUERY_DIR) \
-sEXPORTED_FUNCTIONS="['_malloc','_free','_wasm_parse_query','_wasm_deparse_protobuf','_wasm_parse_plpgsql','_wasm_fingerprint','_wasm_free_string']" \
-sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','UTF8ToString','HEAPU8']" \
-sEXPORTED_FUNCTIONS="['_malloc','_free','_wasm_parse_query','_wasm_parse_query_protobuf','_wasm_get_protobuf_len','_wasm_deparse_protobuf','_wasm_parse_plpgsql','_wasm_fingerprint','_wasm_normalize_query','_wasm_parse_query_detailed','_wasm_free_detailed_result','_wasm_free_string']" \
-sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','UTF8ToString','HEAPU8','HEAPU32']" \
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \
-sENVIRONMENT="web,node" \
-sMODULARIZE=1 \
Expand Down
114 changes: 98 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,100 @@ const sql = deparseSync(parseTree[0]);
// Returns: string - reconstructed SQL query
```

### `fingerprint(sql: string): Promise<string>`

Generates a unique fingerprint for a SQL query that can be used for query identification and caching. Returns a Promise for a 16-character fingerprint string.

```typescript
import { fingerprint } from 'libpg-query';

const fp = await fingerprint('SELECT * FROM users WHERE active = $1');
// Returns: string - unique 16-character fingerprint (e.g., "50fde20626009aba")
```

### `fingerprintSync(sql: string): string`

Synchronous version that generates a unique fingerprint for a SQL query directly.

```typescript
import { fingerprintSync } from 'libpg-query';

const fp = fingerprintSync('SELECT * FROM users WHERE active = $1');
// Returns: string - unique 16-character fingerprint
```

### `normalize(sql: string): Promise<string>`

Normalizes a SQL query by removing comments, standardizing whitespace, and converting to a canonical form. Returns a Promise for the normalized SQL string.

```typescript
import { normalize } from 'libpg-query';

const normalized = await normalize('SELECT * FROM users WHERE active = true');
// Returns: string - normalized SQL query
```

### `normalizeSync(sql: string): string`

Synchronous version that normalizes a SQL query directly.

```typescript
import { normalizeSync } from 'libpg-query';

const normalized = normalizeSync('SELECT * FROM users WHERE active = true');
// Returns: string - normalized SQL query
```

### `parseQueryDetailed(sql: string): Promise<DetailedParseResult>`

Parses a SQL query with enhanced error reporting that includes detailed location information for parse errors. Returns a Promise for either the parse tree or detailed error information.

```typescript
import { parseQueryDetailed } from 'libpg-query';

try {
const result = await parseQueryDetailed('SELECT * FROM users WHERE');
} catch (error) {
// Enhanced error with line number, position, and context information
console.log(error.message); // "Parse error: syntax error at end of input at line 1, position 26"
}
```

### `parseQueryDetailedSync(sql: string): DetailedParseResult`

Synchronous version of detailed parsing with enhanced error reporting.

```typescript
import { parseQueryDetailedSync } from 'libpg-query';

const result = parseQueryDetailedSync('SELECT * FROM users WHERE active = true');
// Returns: DetailedParseResult with enhanced error information if parsing fails
```

### `isReady(): boolean`

Checks if the WebAssembly module is initialized and ready for synchronous operations. This is useful when using sync methods to avoid "WASM module not initialized" errors.

```typescript
import { isReady, parseQuerySync, parseQuery } from 'libpg-query';

// Check if module is ready before using sync methods
if (isReady()) {
const result = parseQuerySync('SELECT * FROM users');
} else {
// Initialize by calling any async method first
await parseQuery('SELECT 1');
// Now sync methods will work
const result = parseQuerySync('SELECT * FROM users');
}

// Alternative pattern - always safe
if (!isReady()) {
await parseQuery('SELECT 1'); // Initialize module
}
const result = parseQuerySync('SELECT * FROM users');
```

### Type Definitions

```typescript
Expand All @@ -138,43 +232,33 @@ This package uses a **WASM-only build system** for true cross-platform compatibi
### Prerequisites

- Node.js (version 16 or higher recommended)
- Docker (for WASM compilation using Emscripten)
- yarn or npm

### Building WASM Artifacts

1. **Install dependencies:**
```bash
npm install
# or
yarn install
```

2. **Build WASM artifacts:**
```bash
npm run wasm:build
# or
yarn wasm:build
```

3. **Clean WASM build (if needed):**
```bash
npm run wasm:clean
# or
yarn wasm:clean
```

4. **Rebuild WASM artifacts from scratch:**
```bash
npm run wasm:clean && npm run wasm:build
# or
yarn wasm:clean && yarn wasm:build
```

### Build Process Details

The WASM build process:
- Uses Docker with Emscripten SDK for compilation
- Uses Emscripten SDK for compilation
- Compiles C wrapper code to WebAssembly
- Generates `wasm/libpg-query.js` and `wasm/libpg-query.wasm` files
- No native compilation or node-gyp dependencies required
Expand All @@ -185,8 +269,6 @@ The WASM build process:

```bash
npm test
# or
yarn test
```

### Test Requirements
Expand Down Expand Up @@ -228,9 +310,9 @@ Our latest is built with `17-latest` branch from libpg_query
- Ensure you call an async method first to initialize the WASM module
- Or use the async versions of methods which handle initialization automatically

**Docker permission errors:**
- Ensure Docker is running and accessible
- On Linux, you may need to add your user to the docker group
**Build environment issues:**
- Ensure Emscripten SDK is properly installed and configured
- Check that all required build dependencies are available

### Build Artifacts

Expand Down
8 changes: 0 additions & 8 deletions docker/Dockerfile

This file was deleted.

67 changes: 0 additions & 67 deletions docker/readme.md

This file was deleted.

8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ParseResult } from "@pgsql/types";

export function parseQuery(sql: string): Promise<ParseResult>;
export function parsePlPgSQL(funcsSql: string): Promise<any>;
export function parseQuerySync(sql: string): ParseResult;
Expand All @@ -7,4 +8,9 @@ export function deparse(parseTree: any): Promise<string>;
export function deparseSync(parseTree: any): any;
export function fingerprint(sql: string): Promise<string>;
export function fingerprintSync(sql: string): string;
export * from '@pgsql/types';
export function normalize(sql: string): Promise<string>;
export function normalizeSync(sql: string): string;
export function parseQueryDetailed(sql: string): Promise<ParseResult>;
export function parseQueryDetailedSync(sql: string): ParseResult;
export function isReady(): boolean;
export * from '@pgsql/types';
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ function fingerprintSync(query) {
return wasmModule.fingerprintSync(query);
}

function normalizeSync(query) {
return wasmModule.normalizeSync(query);
}

function parseQueryDetailedSync(query) {
return wasmModule.parseQueryDetailedSync(query);
}

function isReady() {
return wasmModule.isReady();
}

module.exports = {
parseQuery: wasmModule.parseQuery,
deparse: wasmModule.deparse,
parsePlPgSQL: wasmModule.parsePlPgSQL,
fingerprint: wasmModule.fingerprint,
normalize: wasmModule.normalize,
parseQueryDetailed: wasmModule.parseQueryDetailed,

parseQuerySync,
deparseSync,
parsePlPgSQLSync,
fingerprintSync
fingerprintSync,
normalizeSync,
parseQueryDetailedSync,
isReady
};
Loading