From 7ca086c9307932ea5a66ae3aa66f0393a271c7d2 Mon Sep 17 00:00:00 2001 From: Magnun A V F <103958633+MagnunAVFAzion@users.noreply.github.com> Date: Thu, 18 Dec 2025 13:44:04 -0300 Subject: [PATCH 1/2] fix: rustwasm preset build (#338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Filho --- .../presets/src/presets/rustwasm/config.ts | 55 +++++++------ .../presets/src/presets/rustwasm/handler.ts | 77 ++++++++++++++++--- .../presets/src/presets/rustwasm/index.ts | 4 +- packages/presets/tsup.config.json | 2 +- packages/presets/tsup.config.ts | 2 +- 5 files changed, 97 insertions(+), 43 deletions(-) diff --git a/packages/presets/src/presets/rustwasm/config.ts b/packages/presets/src/presets/rustwasm/config.ts index 02202bce..ed263095 100644 --- a/packages/presets/src/presets/rustwasm/config.ts +++ b/packages/presets/src/presets/rustwasm/config.ts @@ -1,42 +1,39 @@ -import type { AzionBuild, AzionConfig } from 'azion/config'; -import webpack, { Configuration } from 'webpack'; +import type { AzionConfig } from 'azion/config'; const config: AzionConfig = { build: { entry: 'handler.js', polyfills: false, - bundler: 'webpack', - extend: (context: Configuration) => { - context = { - ...context, - optimization: { - minimize: false, - }, - performance: { - maxEntrypointSize: 2097152, - maxAssetSize: 2097152, - }, - module: { - rules: [ - { - test: /\.wasm$/, - type: 'asset/inline', - }, - ], - }, - plugins: [ - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: 1, - }), - ], - }; - return context; + }, + storage: [ + { + name: '$BUCKET_NAME', + prefix: '$BUCKET_PREFIX', + dir: '.wasm-bindgen', + edgeAccess: 'read_only', + }, + ], + connectors: [ + { + name: '$CONNECTOR_NAME', + active: true, + type: 'storage', + attributes: { + bucket: '$BUCKET_NAME', + prefix: '$BUCKET_PREFIX', + }, }, - } as AzionBuild, + ], functions: [ { name: '$FUNCTION_NAME', path: './functions/handler.js', + bindings: { + storage: { + bucket: '$BUCKET_NAME', + prefix: '$BUCKET_PREFIX', + }, + }, }, ], applications: [ diff --git a/packages/presets/src/presets/rustwasm/handler.ts b/packages/presets/src/presets/rustwasm/handler.ts index d5085390..ca160e68 100644 --- a/packages/presets/src/presets/rustwasm/handler.ts +++ b/packages/presets/src/presets/rustwasm/handler.ts @@ -13,17 +13,74 @@ const handler: AzionRuntimeModule = { fetch: async (request: AzionRuntimeRequest): Promise => { try { if (!wasmPromise) { - wasmPromise = fetch('./.wasm-bindgen/azion_rust_edge_function_bg.wasm') - .then((response) => response.arrayBuffer()) - .then(async (buffer) => { - // @ts-expect-error - Module will be generated during build - return import('./.wasm-bindgen/azion_rust_edge_function').then((module) => { - return module.default(buffer).then(() => module); - }); - }); + const wasmJsUrl = new URL('./azion_rust_edge_function.js', 'file://'); + const wasmDataUrl = new URL('./azion_rust_edge_function_bg.wasm', 'file://'); + + wasmPromise = Promise.all([ + fetch(wasmJsUrl).then((response) => response.text()), + fetch(wasmDataUrl).then((response) => response.arrayBuffer()), + ]).then(async ([jsCode, buffer]) => { + // Create a CommonJS-like environment + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const module: any = { exports: {} }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const exports: any = module.exports; + + // Transform ES module syntax to CommonJS + const transformedCode = jsCode + .replace(/export\s+default\s+/g, 'module.exports.default = ') + .replace(/export\s+function\s+(\w+)/g, 'module.exports.$1 = function $1') + .replace(/export\s+const\s+(\w+)\s*=/g, 'module.exports.$1 =') + .replace(/export\s+let\s+(\w+)\s*=/g, 'module.exports.$1 =') + .replace(/export\s+\{([^}]+)\}/g, (match, exports) => { + return exports + .split(',') + .map((exp: string) => { + const name = exp.trim(); + return `module.exports.${name} = ${name};`; + }) + .join('\n'); + }) + .replace(/import\s+.*?from\s+['"].*?['"];?/g, ''); + + // Execute the code in a function scope with module and exports + const moduleFunc = new Function('module', 'exports', transformedCode); + moduleFunc(module, exports); + + const wasmExports = module.exports; + + // With --target=web, the default export is the init function + const init = wasmExports.default; + + if (typeof init !== 'function') { + throw new Error('Could not find init function in WASM module'); + } + + // Initialize with the ArrayBuffer + await init(buffer); + + return wasmExports; + }); + } + + const WasmModule = await wasmPromise; + + // Call fetch_listener - it should return a Promise + // @ts-expect-error - wasm-bindgen + const wasmResponse = await WasmModule.fetch_listener({ request }); + + // WASM returns a Response-like object, but not a native Response instance + // We need to create a new Response from the WASM response data + if (!wasmResponse) { + throw new Error('WASM fetch_listener returned null or undefined'); } - const WasmModule = (await wasmPromise) as { fetch_listener: (request: AzionRuntimeRequest) => Promise }; - return WasmModule.fetch_listener(request); + + // Create a proper Response object from the WASM response + return new Response(wasmResponse.body, { + status: wasmResponse.status || 200, + statusText: wasmResponse.statusText || '', + headers: wasmResponse.headers || {}, + }); } catch (e) { return new Response( JSON.stringify({ diff --git a/packages/presets/src/presets/rustwasm/index.ts b/packages/presets/src/presets/rustwasm/index.ts index f6836ad3..ce0a2ccb 100644 --- a/packages/presets/src/presets/rustwasm/index.ts +++ b/packages/presets/src/presets/rustwasm/index.ts @@ -2,7 +2,7 @@ import type { AzionBuildPreset } from 'azion/config'; import config from './config'; import handler from './handler'; import metadata from './metadata'; -// import prebuild from './prebuild'; // import postbuild from './postbuild'; +import prebuild from './prebuild'; -export const rustwasm: AzionBuildPreset = { config, metadata, handler }; +export const rustwasm: AzionBuildPreset = { config, metadata, handler, prebuild }; diff --git a/packages/presets/tsup.config.json b/packages/presets/tsup.config.json index ad6a5ad6..e2cea0b6 100644 --- a/packages/presets/tsup.config.json +++ b/packages/presets/tsup.config.json @@ -10,7 +10,7 @@ "minifyWhitespace": true, "external": [ "./build/module", - "./.wasm-bindgen/azion_rust_edge_function", + "./.wasm-bindgen/azion_rust_edge_function.js", "./.wasm-bindgen/azion_rust_edge_function_bg.wasm", "signale", "fast-glob", diff --git a/packages/presets/tsup.config.ts b/packages/presets/tsup.config.ts index a1c92fbc..fe52fcb7 100644 --- a/packages/presets/tsup.config.ts +++ b/packages/presets/tsup.config.ts @@ -34,7 +34,7 @@ export default defineConfig({ minifyWhitespace: true, external: [ './build/module', - './.wasm-bindgen/azion_rust_edge_function', + './.wasm-bindgen/azion_rust_edge_function.js', './.wasm-bindgen/azion_rust_edge_function_bg.wasm', 'signale', 'fast-glob', From 33e5d407a29ed01536e6d72fdbce3ed0e323c1aa Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 18 Dec 2025 16:45:18 +0000 Subject: [PATCH 2/2] chore(release): 2.3.2-stage.1 [skip ci] ## 2.3.2-stage.1 (2025-12-18) * fix: rustwasm preset build (#338) ([7ca086c](https://github.com/aziontech/lib/commit/7ca086c)), closes [#338](https://github.com/aziontech/lib/issues/338) --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c03f803..3d16b3f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.3.2-stage.1 (2025-12-18) + +* fix: rustwasm preset build (#338) ([7ca086c](https://github.com/aziontech/lib/commit/7ca086c)), closes [#338](https://github.com/aziontech/lib/issues/338) + ## 2.3.1 (2025-12-16) * Merge pull request #337 from aziontech/stage ([4f79d8a](https://github.com/aziontech/lib/commit/4f79d8a)), closes [#337](https://github.com/aziontech/lib/issues/337) diff --git a/package.json b/package.json index 26374bbb..5b525c45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "azion", - "version": "2.3.1", + "version": "2.3.2-stage.1", "description": "Azion Packages for Edge Computing.", "scripts": { "prepare": "husky",