From 109502dea9c163799ebab928d020a26669d87600 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Thu, 16 May 2024 15:32:56 +0200 Subject: [PATCH] feat(vercel): add wrapper for generated wasm, adapt vercel middleware with new wrapper --- Makefile.in | 3 +++ pkg/package.json | 24 ++++++++++++++++++++++++ pkg/redirectionio.d.ts | 3 +++ pkg/redirectionio.js | 36 ++++++++++++++++++++++++++++++++++++ pkg/tsconfig.json | 21 +++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 pkg/package.json create mode 100644 pkg/redirectionio.d.ts create mode 100644 pkg/redirectionio.js create mode 100644 pkg/tsconfig.json diff --git a/Makefile.in b/Makefile.in index 94a0650..93bd230 100644 --- a/Makefile.in +++ b/Makefile.in @@ -36,6 +36,9 @@ install: @RUST_TARGET_DIR@/@RUST_TARGET_SUBDIR@/libredirectionio.a redirectionio mkdir -p $(DESTDIR)@libdir@/pkgconfig/ install -c -m 644 libredirectionio.pc $(DESTDIR)@libdir@/pkgconfig/ +build-wasm: + wasm-pack build --scope redirection.io --no-default-features --out-dir pkg/wasm --no-pack --release + .PHONY: clean clean: rm -f redirectionio.h diff --git a/pkg/package.json b/pkg/package.json new file mode 100644 index 0000000..b58f2c8 --- /dev/null +++ b/pkg/package.json @@ -0,0 +1,24 @@ +{ + "name": "@redirection.io/redirectionio", + "collaborators": [ + "Joel Wurtz " + ], + "description": "Redirection IO Library to handle matching rule, redirect and filtering headers and body.", + "version": "2.11.8", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/redirectionio/libredirectionio" + }, + "files": [ + "wasm/redirectionio.d.ts", + "wasm/redirectionio.js", + "wasm/redirectionio_bg.js", + "wasm/redirectionio_bg.wasm", + "wasm/redirectionio_bg.wasm.d.ts", + "redirectionio.js", + "redirectionio.d.ts" + ], + "module": "redirectionio.js", + "types": "redirectionio.d.ts" +} diff --git a/pkg/redirectionio.d.ts b/pkg/redirectionio.d.ts new file mode 100644 index 0000000..324af3b --- /dev/null +++ b/pkg/redirectionio.d.ts @@ -0,0 +1,3 @@ +export * from './wasm/redirectionio'; + +export function init(instantiate?: () => Promise): Promise; diff --git a/pkg/redirectionio.js b/pkg/redirectionio.js new file mode 100644 index 0000000..a3bfba8 --- /dev/null +++ b/pkg/redirectionio.js @@ -0,0 +1,36 @@ +import * as redirectionio from './wasm/redirectionio_bg.js'; +import wasmModule from './wasm/redirectionio_bg.wasm?module'; + +export * from './wasm/redirectionio_bg.js'; + +function getImports() { + const imports = {"./redirectionio_bg.js": {}}; + + for (const functionName of Object.keys(redirectionio)) { + if (functionName.startsWith("__") && functionName !== "__wbg_set_wasm") { + imports["./redirectionio_bg.js"][functionName] = redirectionio[functionName]; + } + } + + return imports; +} + +let loadedModule = null; + +export async function init(instantiate) { + if (loadedModule) { + return; + } + + if (!instantiate) { + instantiate = async () => { + return await WebAssembly.instantiate(wasmModule, getImports()); + }; + } + + const module = await instantiate(); + + redirectionio.__wbg_set_wasm(module.exports); + loadedModule = module; + redirectionio.init_log(); +} diff --git a/pkg/tsconfig.json b/pkg/tsconfig.json new file mode 100644 index 0000000..d30e381 --- /dev/null +++ b/pkg/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "lib": [ + "es2020", + "DOM" + ], + "allowJs": true, + "skipLibCheck": true, + "strict": true, + "noEmit": true, + "strictNullChecks": true, + "esModuleInterop": true, + "moduleResolution": "bundler", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "preserve", + "incremental": true + } +} \ No newline at end of file