diff --git a/LOADING_WASM.md b/LOADING_WASM.md index 39244a6..81b72a7 100644 --- a/LOADING_WASM.md +++ b/LOADING_WASM.md @@ -31,4 +31,13 @@ import { loadModule } from 'libpg-query'; await loadModule({ wasm: bytes }); // ArrayBuffer, typed array, or a compiled WebAssembly.Module ``` +On Cloudflare Workers, import the `.wasm` as a module and pass it in: + +```ts +import { loadModule } from 'libpg-query'; +import wasm from 'libpg-query/wasm/libpg-query.wasm'; + +await loadModule({ wasm }); +``` + Pass only the `libpg-query.wasm` shipped with the package (or bytes you trust as much): whatever you pass runs as the parser. diff --git a/scripts/copy-templates.js b/scripts/copy-templates.js index c2c9c56..5893038 100755 --- a/scripts/copy-templates.js +++ b/scripts/copy-templates.js @@ -65,7 +65,8 @@ const TEMPLATE_FILES = [ { src: 'LICENSE', dest: 'LICENSE', header: false, hasFullVariant: false }, { src: 'wasm_wrapper.c', dest: 'src/wasm_wrapper.c', header: HEADER, hasFullVariant: true }, { src: 'libpg-query.d.ts', dest: 'src/libpg-query.d.ts', header: HEADER, hasFullVariant: true }, - { src: 'index.ts', dest: 'src/index.ts', header: HEADER, hasFullVariant: true } + { src: 'index.ts', dest: 'src/index.ts', header: HEADER, hasFullVariant: true }, + { src: 'pre.js', dest: 'src/pre.js', header: HEADER, hasFullVariant: false } ]; const SLIM_EXPORTED_FUNCTIONS = "['_malloc','_free','_wasm_parse_query_raw','_wasm_free_parse_result']"; diff --git a/templates/Makefile.template b/templates/Makefile.template index b3163b4..44b89a1 100644 --- a/templates/Makefile.template +++ b/templates/Makefile.template @@ -37,6 +37,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -68,7 +69,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -81,6 +82,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/templates/pre.js b/templates/pre.js new file mode 100644 index 0000000..af108bd --- /dev/null +++ b/templates/pre.js @@ -0,0 +1,7 @@ +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/13/Makefile b/versions/13/Makefile index 08d42f8..8d1e8a3 100644 --- a/versions/13/Makefile +++ b/versions/13/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -72,7 +73,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -85,6 +86,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/13/src/pre.js b/versions/13/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/13/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/13/test/load-module.test.js b/versions/13/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/13/test/load-module.test.js +++ b/versions/13/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0); diff --git a/versions/14/Makefile b/versions/14/Makefile index db44a9b..dbd16c9 100644 --- a/versions/14/Makefile +++ b/versions/14/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -67,7 +68,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -80,6 +81,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/14/src/pre.js b/versions/14/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/14/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/14/test/load-module.test.js b/versions/14/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/14/test/load-module.test.js +++ b/versions/14/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0); diff --git a/versions/15/Makefile b/versions/15/Makefile index 8969dc3..63d01fa 100644 --- a/versions/15/Makefile +++ b/versions/15/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -67,7 +68,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -80,6 +81,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/15/src/pre.js b/versions/15/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/15/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/15/test/load-module.test.js b/versions/15/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/15/test/load-module.test.js +++ b/versions/15/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0); diff --git a/versions/16/Makefile b/versions/16/Makefile index 0b537cf..18be477 100644 --- a/versions/16/Makefile +++ b/versions/16/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -67,7 +68,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -80,6 +81,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/16/src/pre.js b/versions/16/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/16/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/16/test/load-module.test.js b/versions/16/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/16/test/load-module.test.js +++ b/versions/16/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0); diff --git a/versions/17/Makefile b/versions/17/Makefile index 36624df..e45062f 100644 --- a/versions/17/Makefile +++ b/versions/17/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -67,7 +68,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -80,6 +81,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/17/src/pre.js b/versions/17/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/17/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/17/test/load-module.test.js b/versions/17/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/17/test/load-module.test.js +++ b/versions/17/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0); diff --git a/versions/18/Makefile b/versions/18/Makefile index 268e2f1..19bcc66 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -42,6 +42,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c +PRE_JS := src/pre.js LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h @@ -67,7 +68,7 @@ $(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) cd $(LIBPG_QUERY_DIR); $(MAKE) build # Build libpg-query-node WASM module -$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) +$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) $(PRE_JS) ifdef EMSCRIPTEN mkdir -p $(WASM_OUT_DIR) $(CC) \ @@ -80,6 +81,7 @@ ifdef EMSCRIPTEN -sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','getValue','UTF8ToString','HEAPU8','HEAPU32']" \ -sEXPORT_NAME="$(WASM_MODULE_NAME)" \ -sENVIRONMENT="web,node,worker" \ + --pre-js $(PRE_JS) \ -sASSERTIONS=0 \ -sSINGLE_FILE=0 \ -sMODULARIZE=1 \ diff --git a/versions/18/src/pre.js b/versions/18/src/pre.js new file mode 100644 index 0000000..c2ec536 --- /dev/null +++ b/versions/18/src/pre.js @@ -0,0 +1,14 @@ +/** + * DO NOT MODIFY MANUALLY — this is generated from the templates dir + * + * To make changes, edit the files in the templates/ directory and run: + * npm run copy:templates + */ + +// The glue picks Node from `process.versions.node` and a worker from +// `WorkerGlobalScope`, then reads `__dirname` or `self.location` for them. +// Hosts that fake those markers without the rest (Cloudflare Workers with +// `nodejs_compat`, CommonJS bundled into ESM) would throw on the read, before +// `loadModule({ wasm })` can use the bytes it was given. +if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false; +if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false; diff --git a/versions/18/test/load-module.test.js b/versions/18/test/load-module.test.js index f54b6df..256afd0 100644 --- a/versions/18/test/load-module.test.js +++ b/versions/18/test/load-module.test.js @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); +const os = require('node:os'); const WASM = path.join(__dirname, '../wasm/libpg-query.wasm'); const RESULT = 'RESULT '; @@ -92,6 +93,59 @@ describe('loadModule({ wasm }) when the wrapper cannot read libpg-query.wasm', { }); }); +// Cloudflare Workers with `nodejs_compat` define `process.versions.node` and +// `WorkerGlobalScope`, but no `__dirname`, `__filename` or `self.location`. +// Loads the wrapper and the glue as plain functions, so neither sees a module +// scope, in a process that also has those two markers. +function inWorkerLikeHost(body) { + const dir = path.join(__dirname, '../wasm'); + const script = ` + globalThis.WorkerGlobalScope = class WorkerGlobalScope {}; + globalThis.self = globalThis; + const fs = require('node:fs'); + const path = require('node:path'); + const dir = ${JSON.stringify(dir)}; + const load = (file, req) => { + const module = { exports: {} }; + new Function('module', 'exports', 'require', fs.readFileSync(path.join(dir, file), 'utf8'))(module, module.exports, req); + return module.exports; + }; + const glue = load('libpg-query.js', (id) => { throw new Error('the glue required ' + id); }); + const lib = load('index.cjs', (id) => id === './libpg-query.js' ? glue : require(require.resolve(id, { paths: [dir] }))); + const bytes = fs.readFileSync(path.join(dir, 'libpg-query.wasm')); + (async () => { ${body} })().then( + (result) => console.log(${JSON.stringify(RESULT)} + JSON.stringify(result)), + (error) => { console.log(${JSON.stringify(RESULT)} + JSON.stringify({ error: String(error && error.message || error) })); } + ); + `; + // A file, not `-e`: eval code gets `__dirname` and `__filename` as globals. + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-like-')); + const file = path.join(tmp, 'host.js'); + fs.writeFileSync(file, script); + let run; + try { + run = spawnSync(process.execPath, [file], { encoding: 'utf8', timeout: 120000 }); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + const line = (run.stdout || '').split('\n').reverse().find((l) => l.startsWith(RESULT)); + if (!line) { + const how = run.error ? run.error.message : run.signal ? `signal ${run.signal}` : `exit ${run.status}`; + throw new Error(`child process gave no result (${how}):\n${(run.stderr || '').slice(-4000)}`); + } + return JSON.parse(line.slice(RESULT.length)); +} + +describe('loadModule({ wasm }) in a host without __dirname or self.location', { timeout: 300000 }, () => { + it('loads from the bytes and parses', () => { + const result = inWorkerLikeHost(` + await lib.loadModule({ wasm: bytes }); + return { stmts: (await lib.parse('SELECT 1')).stmts.length }; + `); + assert.deepEqual(result, { stmts: 1 }); + }); +}); + describe('loadModule() arity', () => { it('stays 0 so test runners can pass it directly as a hook', () => { assert.equal(require('../wasm/index.cjs').loadModule.length, 0);