Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## <small>2.3.2-stage.1 (2025-12-18)</small>

* fix: rustwasm preset build (#338) ([7ca086c](https://github.com/aziontech/lib/commit/7ca086c)), closes [#338](https://github.com/aziontech/lib/issues/338)

## <small>2.3.1 (2025-12-16)</small>

* Merge pull request #337 from aziontech/stage ([4f79d8a](https://github.com/aziontech/lib/commit/4f79d8a)), closes [#337](https://github.com/aziontech/lib/issues/337)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
55 changes: 26 additions & 29 deletions packages/presets/src/presets/rustwasm/config.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down
77 changes: 67 additions & 10 deletions packages/presets/src/presets/rustwasm/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,74 @@ const handler: AzionRuntimeModule = {
fetch: async (request: AzionRuntimeRequest): Promise<Response> => {
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<Response>
// @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<Response> };
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({
Expand Down
4 changes: 2 additions & 2 deletions packages/presets/src/presets/rustwasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion packages/presets/tsup.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/presets/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down