Skip to content

Commit daf4051

Browse files
authored
Merge pull request #53 from launchql/wasm/15-latest
Wasm/15 latest
2 parents d0f0a64 + a9f5ca8 commit daf4051

File tree

19 files changed

+3116
-469
lines changed

19 files changed

+3116
-469
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build Dry Run
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build:
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
matrix:
11+
os: [windows-latest, macos-latest, ubuntu-latest]
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Build project
15+
run: |
16+
yarn
17+
- uses: actions/upload-artifact@v2
18+
with:
19+
name: build-artifact-${{ matrix.os }}
20+
path: |
21+
${{ matrix.os == 'macos-latest' && './libpg_query/osx/libpg_query.a' ||
22+
matrix.os == 'ubuntu-latest' && './libpg_query/linux/libpg_query.a' ||
23+
'' }}
24+
prepare-and-publish:
25+
needs: build
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v2
29+
- uses: actions/download-artifact@v2
30+
with:
31+
path: downloaded-artifacts
32+
- name: Prepare artifacts
33+
run: |
34+
# move or prepare artifacts
35+
ls downloaded-artifacts/*
36+
# - name: Publish to NPM
37+
# run: |
38+
# # Assuming you've set up your package.json and .npmrc correctly
39+
# npm publish
40+
# env:
41+
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/run-tests.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Run Tests 🧪
2+
3+
on:
4+
pull_request:
5+
types: [opened, push, reopened]
6+
workflow_dispatch:
7+
8+
jobs:
9+
run-tests:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Repository 📥
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Node.js 🌐
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: '20.x'
19+
cache: 'yarn'
20+
21+
- name: Install Dependencies 🧶
22+
run: |
23+
yarn
24+
25+
- name: Test 🔍
26+
run: yarn test

.gitignore

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

.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

binding.gyp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88
"src/sync.cc",
99
"src/async.cc"
1010
],
11-
"actions": [
12-
{
13-
"outputs": ['libpg_query/include/pg_query.h'],
14-
"inputs": [],
15-
"action": ['script/buildAddon.sh'],
16-
"action_name": 'prebuild_dependencies'
17-
}
18-
],
1911
'cflags!': [ '-fno-exceptions' ],
2012
'cflags_cc!': [ '-fno-exceptions' ],
2113
'include_dirs': [
@@ -25,22 +17,58 @@
2517
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
2618
'conditions': [
2719
['OS=="linux"', {
28-
"libraries": [ "-L<!(pwd)/libpg_query/linux", "-lpg_query" ]
20+
"libraries": [ "-L<!(pwd)/libpg_query/linux", "-lpg_query" ],
21+
"actions": [
22+
{
23+
"outputs": ['libpg_query/include/pg_query.h'],
24+
"inputs": [],
25+
"action": ['script/buildAddon.sh'],
26+
"action_name": 'prebuild_dependencies'
27+
}
28+
],
2929
}],
3030
['OS=="mac"', {
3131
"libraries": [ "-L<!(pwd)/libpg_query/osx", "-lpg_query" ],
3232
"xcode_settings": {
3333
"CLANG_CXX_LIBRARY": "libc++",
3434
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
3535
'MACOSX_DEPLOYMENT_TARGET': '10.7'
36-
}
36+
},
37+
"actions": [
38+
{
39+
"outputs": ['libpg_query/include/pg_query.h'],
40+
"inputs": [],
41+
"action": ['script/buildAddon.sh'],
42+
"action_name": 'prebuild_dependencies'
43+
}
44+
],
3745
}],
3846
['OS=="win"', {
47+
"link_settings": {
48+
"library_dirs": [
49+
"../libpg_query/windows"
50+
],
51+
"libraries": [
52+
"../libpg_query/windows/pg_query.lib"
53+
],
54+
},
3955
"msvs_settings": {
4056
"VCCLCompilerTool": {
41-
"ExceptionHandling": 1
57+
"ExceptionHandling": 0,
58+
"AdditionalOptions": ["/EHsc"]
59+
}
60+
},
61+
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
62+
"actions":[
63+
{
64+
"outputs": [
65+
""
66+
],
67+
"inputs": [],
68+
"action": ['../script/buildAddon.bat'],
69+
"action_name": 'prebuild_dependencies'
4270
}
43-
}
71+
]
4472
}]
4573
]
4674
}

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:12.18.2-alpine3.11
1+
FROM node:20-alpine
22
RUN apk --no-cache add git
33
RUN apk add --no-cache bash
44
RUN apk add --no-cache make

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: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,42 @@
22
"name": "libpg-query",
33
"version": "15.0.3",
44
"description": "The real PostgreSQL query parser",
5-
"homepage": "https://github.com/pyramation/libpg-query-node",
5+
"homepage": "https://github.com/launchql/libpg-query-node",
66
"main": "index.js",
77
"typings": "index.d.ts",
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"
@@ -20,17 +46,19 @@
2046
"license": "LICENSE IN LICENSE",
2147
"repository": {
2248
"type": "git",
23-
"url": "git://github.com/pyramation/libpg-query-node.git"
49+
"url": "git://github.com/launchql/libpg-query-node.git"
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",
33-
"node-gyp": "^8.0.0"
60+
"node-addon-api": "^7.0.0",
61+
"node-gyp": "^10.0.1"
3462
},
3563
"keywords": [
3664
"sql",
@@ -47,4 +75,4 @@
4775
"host": "https://supabase-public-artifacts-bucket.s3.amazonaws.com",
4876
"remote_path": "./libpg-query-node/"
4977
}
50-
}
78+
}

0 commit comments

Comments
 (0)