Phisn
Phisn
CDCloudflare Developers
Created by MetalGear on 9/12/2023 in #workers-help
What Architecture on workers?
The only public project that comes to my head is https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/. Its neither serverless nor nodejs but explains some nice architectural concepts. (github: https://github.com/dotnet-architecture/eShopOnContainers/wiki/Architecture)
5 replies
CDCloudflare Developers
Created by MetalGear on 9/12/2023 in #workers-help
What Architecture on workers?
You could maybe use some ddd related architecture to handle your events. One approach I have read about is to have your usual domain layer with entities. When you interact with these entities they generate events (saved in their internal state) which are later published when the entity is saved back into your storage. Now you could publish these events into a queue and have triggers to consume these and so forth.
5 replies
CDCloudflare Developers
Created by avnav on 9/9/2023 in #workers-help
DEV vs PROD envs
What you could do is use different scripts for different environments. One npm dev and npm dev:prod. They both would no run different scripts in your packages. Specifically in your worker you would start it with something like wrangler ... --vars=DEV_OR_PROD:PROD or :DEV. For deployment you would just use your wrangler.toml vars.
4 replies
CDCloudflare Developers
Created by Phisn on 9/8/2023 in #workers-help
Webassembly in libraries not correctly loading
import path from 'path';
import * as fs from 'fs';

// script to fix rust bindings generated by wasm-bindgen because they are incompatible with cloudflare workers.
// see: https://developers.cloudflare.com/workers/runtime-apis/webassembly/rust/#javascript-plumbing-wasm-bindgen

const scriptHead = "//! modified by custom application postprocess !"

function transformScript() {
const scriptBg = path.resolve('./node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.js')
const linesScriptBg = fs.readFileSync(scriptBg, 'utf8').split('\n')

// line 0: import * as wasm from './rapier_wasm2d_bg.wasm';
const [head, ...withoutWasmImport] = linesScriptBg

if (head === scriptHead) {
return
}

const withCustomWasm = [
scriptHead,
"let wasm",
"export function __setWasm(newWasm) {",
" wasm = newWasm;",
"}",
...withoutWasmImport,
]

fs.writeFileSync(scriptBg, withCustomWasm.join('\n'))
}

function transformScriptTypes() {
const scriptTypes = path.resolve('./node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.d.ts')

if (fs.existsSync(scriptTypes)) {
return
}

const lines = [
scriptHead,
"export function __setWasm(newWasm: WebAssembly.Exports)"
]

fs.writeFileSync(scriptTypes, lines.join('\n'))
}

transformScript()
transformScriptTypes()
import path from 'path';
import * as fs from 'fs';

// script to fix rust bindings generated by wasm-bindgen because they are incompatible with cloudflare workers.
// see: https://developers.cloudflare.com/workers/runtime-apis/webassembly/rust/#javascript-plumbing-wasm-bindgen

const scriptHead = "//! modified by custom application postprocess !"

function transformScript() {
const scriptBg = path.resolve('./node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.js')
const linesScriptBg = fs.readFileSync(scriptBg, 'utf8').split('\n')

// line 0: import * as wasm from './rapier_wasm2d_bg.wasm';
const [head, ...withoutWasmImport] = linesScriptBg

if (head === scriptHead) {
return
}

const withCustomWasm = [
scriptHead,
"let wasm",
"export function __setWasm(newWasm) {",
" wasm = newWasm;",
"}",
...withoutWasmImport,
]

fs.writeFileSync(scriptBg, withCustomWasm.join('\n'))
}

function transformScriptTypes() {
const scriptTypes = path.resolve('./node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.d.ts')

if (fs.existsSync(scriptTypes)) {
return
}

const lines = [
scriptHead,
"export function __setWasm(newWasm: WebAssembly.Exports)"
]

fs.writeFileSync(scriptTypes, lines.join('\n'))
}

transformScript()
transformScriptTypes()
Now I can simply import the wasm file inside my cloudflare worker:
// initialize rapier wasm special for cloudflare workers
import * as imports from "@dimforge/rapier2d/rapier_wasm2d_bg"
import _wasm from "../node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.wasm"

imports.__setWasm(new WebAssembly.Instance(_wasm, { "./rapier_wasm2d_bg.js": imports }).exports)
// initialize rapier wasm special for cloudflare workers
import * as imports from "@dimforge/rapier2d/rapier_wasm2d_bg"
import _wasm from "../node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.wasm"

imports.__setWasm(new WebAssembly.Instance(_wasm, { "./rapier_wasm2d_bg.js": imports }).exports)
4 replies
CDCloudflare Developers
Created by Phisn on 9/8/2023 in #workers-help
Webassembly in libraries not correctly loading
Solution: Found a solution to my problem. Probably the most hacky thing ever but works perfectly and there seems currently to not be a better solution. Rapier is build using wasm-bindgen for js so this applies to my problem https://developers.cloudflare.com/workers/runtime-apis/webassembly/rust/#javascript-plumbing-wasm-bindgen. Its said that we get a Module instead of an Instance and have therefore to adjust the processing that is normally done by bindgen. Problem is, that the proposed solution does not work because of how vite handles wasm files. Therefor my solution was to inject a __setWasm into the _bg file and set it somewhere inside my cloudflare worker. This way the compilation for vite stays uneffected. I am using the following script as "postinstall" in my package.json:
4 replies
CDCloudflare Developers
Created by Phisn on 9/8/2023 in #workers-help
Webassembly in libraries not correctly loading
Update: Wrangler seems to handle the package incorrectly. At least the bundled code does look abviously wrong without any WebAssembly.intiate call. Question is why vite is able to produce correct output and how I can fix the wrangler output.
4 replies