Skip to content

Commit ec882d2

Browse files
authored
Merge pull request #51 from launchql/wasm/origin-branch
Wasm -> 15-latest
2 parents f351a42 + 0f42732 commit ec882d2

File tree

12 files changed

+2384
-10
lines changed

12 files changed

+2384
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ libs/
44
npm-debug.log
55
libpg_query/**/*.a
66
libpg_query/**/*.h
7+
wasm/libpg-query.js
8+
*.wasm
9+
.cache

Makefile

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
WASM_OUT_DIR := wasm
2+
WASM_OUT_NAME := libpg-query
3+
WASM_MODULE_NAME := PgQueryModule
4+
LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git
5+
LIBPG_QUERY_TAG := 15-4.2.4
6+
CACHE_DIR := .cache
7+
8+
OS ?= $(shell uname -s)
9+
ARCH ?= $(shell uname -m)
10+
11+
ifdef EMSCRIPTEN
12+
PLATFORM := emscripten
13+
else ifeq ($(OS),Darwin)
14+
PLATFORM := darwin
15+
else ifeq ($(OS),Linux)
16+
PLATFORM := linux
17+
else
18+
$(error Unsupported platform: $(OS))
19+
endif
20+
21+
ifdef EMSCRIPTEN
22+
ARCH := wasm
23+
endif
24+
25+
PLATFORM_ARCH := $(PLATFORM)-$(ARCH)
26+
SRC_FILES := $(wildcard src/*.cc)
27+
LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG)
28+
LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a
29+
LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h
30+
CXXFLAGS := -O3
31+
32+
ifdef EMSCRIPTEN
33+
OUT_FILES := $(foreach EXT,.js .wasm,$(WASM_OUT_DIR)/$(WASM_OUT_NAME)$(EXT))
34+
else
35+
OUT_FILES := build/Release/queryparser.node $(wildcard build/*)
36+
endif
37+
38+
# Clone libpg_query source (lives in CACHE_DIR)
39+
$(LIBPG_QUERY_DIR):
40+
mkdir -p $(CACHE_DIR)
41+
git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR)
42+
43+
$(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR)
44+
45+
# Build libpg_query
46+
$(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR)
47+
cd $(LIBPG_QUERY_DIR); $(MAKE) build
48+
49+
# Build libpg-query-node (based on platform)
50+
$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES)
51+
ifdef EMSCRIPTEN
52+
@ $(CXX) \
53+
$(CXXFLAGS) \
54+
-DNAPI_HAS_THREADS \
55+
-I$(LIBPG_QUERY_DIR) \
56+
-I./node_modules/emnapi/include \
57+
-I./node_modules/node-addon-api \
58+
-L./node_modules/emnapi/lib/wasm32-emscripten \
59+
-L$(LIBPG_QUERY_DIR) \
60+
--js-library=./node_modules/emnapi/dist/library_napi.js \
61+
-sEXPORTED_FUNCTIONS="['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']" \
62+
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \
63+
-sENVIRONMENT="web" \
64+
-sMODULARIZE=1 \
65+
-sEXPORT_ES6=1 \
66+
-fexceptions \
67+
-lpg_query \
68+
-lemnapi-basic \
69+
-o $@ \
70+
$(SRC_FILES)
71+
else
72+
# if not wasm, defer to node-gyp
73+
yarn rebuild
74+
endif
75+
76+
# Commands
77+
build: $(OUT_FILES)
78+
79+
build-cache: $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER)
80+
81+
rebuild: clean build
82+
83+
rebuild-cache: clean-cache build-cache
84+
85+
clean:
86+
-@ rm -r $(OUT_FILES) > /dev/null 2>&1
87+
88+
clean-cache:
89+
-@ rm -rf $(LIBPG_QUERY_DIR)
90+
91+
.PHONY: build build-cache rebuild rebuild-cache clean clean-cache

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const PgQuery = require('./build/Release/queryparser');
1+
const PgQuery = require('./build/Release/queryparser.node');
22

33
module.exports = {
44
parseQuery(query) {
@@ -26,14 +26,14 @@ module.exports = {
2626
},
2727

2828
fingerprint(query) {
29-
return new Promise((resolve, reject) =>{
29+
return new Promise((resolve, reject) => {
3030
PgQuery.fingerprintAsync(query, (err, result) => {
3131
err ? reject(err) : resolve(result);
32-
})
32+
});
3333
});
3434
},
3535

3636
fingerprintSync(query) {
3737
return PgQuery.fingerprintSync(query);
38-
}
38+
},
3939
};

package.json

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,36 @@
88
"publishConfig": {
99
"access": "public"
1010
},
11+
"files": [
12+
"binding.gyp",
13+
"index.js",
14+
"index.d.ts",
15+
"libpg_query/*",
16+
"script/*",
17+
"src/*",
18+
"wasm/*"
19+
],
20+
"exports": {
21+
".": {
22+
"types": "./index.d.ts",
23+
"browser": "./wasm/index.js",
24+
"node": "./index.js",
25+
"default": "./index.js"
26+
},
27+
"./wasm": {
28+
"types": "./index.d.ts",
29+
"default": "./wasm/index.js"
30+
}
31+
},
1132
"scripts": {
1233
"configure": "node-pre-gyp configure",
1334
"install": "node-pre-gyp install --fallback-to-build",
1435
"rebuild": "node-pre-gyp configure rebuild",
36+
"make:wasm": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make",
37+
"build:wasm": "yarn make:wasm build",
38+
"rebuild:wasm": "yarn make:wasm rebuild",
39+
"clean:wasm": "yarn make:wasm clean",
40+
"clean-cache:wasm": "yarn make:wasm clean-cache",
1541
"test": "mocha --timeout 5000",
1642
"binary:build": "node-pre-gyp rebuild package",
1743
"binary:publish": "AWS_PROFILE=supabase-dev node-pre-gyp publish"
@@ -24,12 +50,14 @@
2450
},
2551
"devDependencies": {
2652
"chai": "^3.5.0",
53+
"emnapi": "^0.43.1",
2754
"lodash": "^4.17.15",
2855
"mocha": "^5.2.0"
2956
},
3057
"dependencies": {
58+
"@emnapi/runtime": "^0.43.1",
3159
"@mapbox/node-pre-gyp": "^1.0.8",
32-
"node-addon-api": "^1.6.3",
60+
"node-addon-api": "^7.0.0",
3361
"node-gyp": "^8.0.0"
3462
},
3563
"keywords": [
@@ -47,4 +75,4 @@
4775
"host": "https://supabase-public-artifacts-bucket.s3.amazonaws.com",
4876
"remote_path": "./libpg-query-node/"
4977
}
50-
}
78+
}

test/webpack/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/*.js

test/webpack/dist/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>libpg-query web</title>
7+
</head>
8+
9+
<body>
10+
<script src="main.js"></script>
11+
<h2>Check the console for the parsed SQL.</h2>
12+
</body>
13+
14+
</html>

test/webpack/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "webpack-test",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "webpack serve --open",
7+
"build": "webpack"
8+
},
9+
"devDependencies": {
10+
"webpack": "^5.89.0",
11+
"webpack-cli": "^5.1.4",
12+
"webpack-dev-server": "^4.15.1"
13+
}
14+
}

test/webpack/src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { parseQuery } from '../../../wasm';
2+
3+
const sql = 'select * from customers;';
4+
const result = await parseQuery(sql);
5+
6+
console.log(sql);
7+
console.log(result);

test/webpack/webpack.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './src/index.js',
5+
output: {
6+
filename: 'main.js',
7+
path: path.resolve(__dirname, 'dist'),
8+
},
9+
devServer: {
10+
static: './dist',
11+
client: {
12+
overlay: false,
13+
},
14+
},
15+
};

0 commit comments

Comments
 (0)