Skip to content

Commit 1b13f15

Browse files
Fix cloudflare workers (#437)
* Remove the circular dependency for the logging * Add cloudflare workers module * Add a check on the shell script for the dist directory
1 parent df7f511 commit 1b13f15

17 files changed

+1962
-63
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
}
6969
set "@restatedev/restate-sdk-core"
7070
set "@restatedev/restate-sdk"
71+
set "@restatedev/restate-sdk-cloudflare-workers"
7172
set "@restatedev/restate-sdk-clients"
7273
npm --workspaces update
7374
# We use dist-tag dev for the snapshot releases, see https://docs.npmjs.com/cli/v9/commands/npm-dist-tag for more info

.tools/build_core.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ PROJECT_ROOT="$(dirname "$SELF_PATH")/.."
66
pushd $PROJECT_ROOT/sdk-shared-core-wasm-bindings
77
wasm-pack build --target web
88
npx wasm-pack-inline ./pkg --dir ../packages/restate-sdk/src/endpoint/handlers/vm --name sdk_shared_core_wasm_bindings
9+
wasm-pack build --target bundler -d ../packages/restate-sdk-cloudflare-workers/patches/vm
910
popd

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"workspaces": [
1818
"packages/restate-sdk-core",
1919
"packages/restate-sdk",
20+
"packages/restate-sdk-cloudflare-workers",
2021
"packages/restate-sdk-clients",
2122
"packages/restate-sdk-examples",
2223
"packages/restate-e2e-services"
@@ -29,7 +30,7 @@
2930
"format": "npm run format -ws --if-present",
3031
"format-check": "npm run format-check -ws --if-present",
3132
"verify": "npm run verify -ws --if-present",
32-
"clean": "rm -rf packages/restate-sdk/dist && rm -rf packages/restate-sdk-examples/dist && rm -rf packages/restate-sdk-ingress/dist && rm -rf packages/restate-e2e-services/dist && rm -rf packages/restate-sdk-core/dist"
33+
"clean": "rm -rf packages/restate-sdk/dist && rm -rf packages/restate-sdk-cloudflare-workers/dist && rm -rf packages/restate-sdk-examples/dist && rm -rf packages/restate-sdk-ingress/dist && rm -rf packages/restate-e2e-services/dist && rm -rf packages/restate-sdk-core/dist"
3334
},
3435
"devDependencies": {
3536
"@arethetypeswrong/cli": "^0.15.3",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"name": "@restatedev/restate-sdk-cloudflare-workers",
3+
"version": "1.3.1",
4+
"description": "Typescript SDK for Restate",
5+
"author": "Restate Developers",
6+
"license": "MIT",
7+
"email": "code@restate.dev",
8+
"homepage": "https://github.com/restatedev/sdk-typescript#readme",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/restatedev/sdk-typescript.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/restatedev/sdk-typescript/issues"
15+
},
16+
"type": "module",
17+
"sideEffects": false,
18+
"main": "./dist/cjs/src/public_api.js",
19+
"module": "./dist/esm/src/public_api.js",
20+
"exports": {
21+
".": {
22+
"import": {
23+
"types": "./dist/esm/src/public_api.d.ts",
24+
"default": "./dist/esm/src/public_api.js"
25+
},
26+
"require": {
27+
"types": "./dist/cjs/src/public_api.d.ts",
28+
"default": "./dist/cjs/src/public_api.js"
29+
}
30+
},
31+
"./fetch": {
32+
"import": {
33+
"types": "./dist/esm/src/fetch.d.ts",
34+
"default": "./dist/esm/src/fetch.js"
35+
},
36+
"require": {
37+
"types": "./dist/cjs/src/fetch.d.ts",
38+
"default": "./dist/cjs/src/fetch.js"
39+
}
40+
},
41+
"./lambda": {
42+
"import": {
43+
"types": "./dist/esm/src/lambda.d.ts",
44+
"default": "./dist/esm/src/lambda.js"
45+
},
46+
"require": {
47+
"types": "./dist/cjs/src/lambda.d.ts",
48+
"default": "./dist/cjs/src/lambda.js"
49+
}
50+
}
51+
},
52+
"typesVersions": {
53+
"*": {
54+
"fetch": [
55+
"dist/cjs/src/fetch.d.ts"
56+
],
57+
"lambda": [
58+
"dist/cjs/src/lambda.d.ts"
59+
]
60+
}
61+
},
62+
"types": "./dist/cjs/src/public_api.d.ts",
63+
"files": [
64+
"dist"
65+
],
66+
"scripts": {
67+
"build": "./patch_sdk_cf_workers.sh",
68+
"release": "release-it"
69+
},
70+
"dependencies": {
71+
"@restatedev/restate-sdk-core": "^1.3.1"
72+
},
73+
"devDependencies": {
74+
"@types/aws-lambda": "^8.10.115"
75+
},
76+
"engines": {
77+
"node": ">= 18.13"
78+
},
79+
"publishConfig": {
80+
"@restatedev:registry": "https://registry.npmjs.org"
81+
}
82+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ ! -d ../restate-sdk/dist ]]; then
4+
echo "ERROR - You need to build the restate-sdk module first!"
5+
exit 1
6+
fi
7+
8+
cp -r ../restate-sdk/dist .
9+
rm -r dist/cjs
10+
11+
# Copy fetch.js
12+
cp patches/fetch.js dist/esm/src/fetch.js
13+
14+
# Copy vm
15+
rm -r dist/esm/src/endpoint/handlers/vm
16+
cp -r patches/vm dist/esm/src/endpoint/handlers
17+
18+
# Copy vm entrypoint
19+
cp patches/sdk_shared_core_wasm_bindings.js dist/esm/src/endpoint/handlers/vm

packages/restate-sdk-cloudflare-workers/patches/fetch.js

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as imports from "./sdk_shared_core_wasm_bindings_bg.js";
2+
3+
// switch between both syntax for node and for workerd
4+
import wkmod from "./sdk_shared_core_wasm_bindings_bg.wasm";
5+
6+
const instance = new WebAssembly.Instance(wkmod, {
7+
"./sdk_shared_core_wasm_bindings_bg.js": imports,
8+
});
9+
imports.__wbg_set_wasm(instance.exports);
10+
11+
export * from "./sdk_shared_core_wasm_bindings_bg.js";
12+
13+
instance.exports.__wbindgen_start();
14+
15+
export function cloudflareWorkersBundlerPatch() {
16+
// This is the result of many hours of debugging.
17+
// The patch described here https://developers.cloudflare.com/workers/languages/rust/#javascript-plumbing-wasm-bindgen
18+
// won't "just work", because the CF worker bundler has some bug that will eliminate this file.
19+
// To prevent the elimination, we call this empty function from the fetch.js file, which is the user entrypoint of the SDK.
20+
}

0 commit comments

Comments
 (0)