Skip to content

Commit 13c90d6

Browse files
feat(stable-hash, transport): support workerd (#5077)
Related-to: GH-759. Closes GH-5076.
1 parent d0aeb84 commit 13c90d6

File tree

9 files changed

+65
-12
lines changed

9 files changed

+65
-12
lines changed

stable-hash/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ hasher.js
136136
hasher.d.ts
137137
index.js
138138
index.d.ts
139+
workerd.js
140+
workerd.d.ts
139141
test/*.js

stable-hash/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"types": "./index.d.ts",
3333
"exports": {
3434
"edge-light": "./edge-light.js",
35+
"workerd": "./workerd.js",
3536
"default": "./index.js"
3637
},
3738
"files": [
@@ -40,7 +41,9 @@
4041
"hasher.d.ts",
4142
"hasher.js",
4243
"index.d.ts",
43-
"index.js"
44+
"index.js",
45+
"workerd.d.ts",
46+
"workerd.js"
4447
],
4548
"scripts": {
4649
"build": "rollup --config rollup.config.js",

stable-hash/workerd.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { makeHasher } from "./hasher.js";
2+
3+
export * from "./hasher.js";
4+
5+
// @ts-ignore: this value exists in Workerd, as it implements the DOM type for it.
6+
// See <https://developers.cloudflare.com/workers/runtime-apis/web-crypto/>.
7+
// This can be verified by adding `/// <reference lib="dom" />` above.
8+
// But we don’t want to load the entire DOM types or workerd-specific types.
9+
export const hash = makeHasher(crypto.subtle);

transport/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ edge-light.js
136136
edge-light.d.ts
137137
index.js
138138
index.d.ts
139+
workerd.js
140+
workerd.d.ts
139141
test/*.js

transport/bun.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Bun doesn't properly support connect-node so we need to use connect-web
1+
// This file is used when running in Bun.
2+
// It uses DOM based APIs (`@connectrpc/connect-web`) to connect to the API.
3+
// Bun slightly differs in how it implements Node APIs and that causes problems.
24
import { createConnectTransport } from "@connectrpc/connect-web";
35

46
export function createTransport(baseUrl: string) {

transport/edge-light.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
// This file is used when running on the `edge-light` condition.
2+
// Specifically Edge by Vercel.
3+
// It is the same as `workerd.ts`, which runs on Cloudflare.
4+
// It uses DOM based APIs (`@connectrpc/connect-web`) to connect to the API.
5+
// Differing from `bun.ts` this solves the `redirect` option set to `error`
6+
// inside `connect` as that does not work on the edge.
7+
//
8+
// For more information, see:
9+
//
10+
// * <https://github.com/connectrpc/connect-es/pull/589>
11+
// * <https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516>
12+
// * <https://github.com/connectrpc/connect-es/pull/1082>
13+
// * <https://github.com/e2b-dev/E2B/pull/669/files>
114
import { createConnectTransport } from "@connectrpc/connect-web";
215

316
export function createTransport(baseUrl: string) {
4-
// The Connect Node client doesn't work on edge runtimes: https://github.com/bufbuild/connect-es/pull/589
5-
// so set the transport using connect-web. The interceptor is required for it work in the edge runtime.
617
return createConnectTransport({
718
baseUrl,
819
interceptors: [
9-
/**
10-
* Ensures redirects are followed to properly support the Next.js/Vercel Edge
11-
* Runtime.
12-
* @see
13-
* https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516
14-
*/
1520
(next) => (req) => {
1621
req.init.redirect = "follow";
1722
return next(req);

transport/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"exports": {
3434
"bun": "./bun.js",
3535
"edge-light": "./edge-light.js",
36+
"workerd": "./workerd.js",
3637
"default": "./index.js"
3738
},
3839
"files": [
@@ -41,7 +42,9 @@
4142
"edge-light.d.ts",
4243
"edge-light.js",
4344
"index.d.ts",
44-
"index.js"
45+
"index.js",
46+
"workerd.d.ts",
47+
"workerd.js"
4548
],
4649
"scripts": {
4750
"build": "rollup --config rollup.config.js",

transport/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../tsconfig.base.json",
3-
"include": ["bun.ts", "edge-light.ts", "index.ts"]
3+
"include": ["bun.ts", "edge-light.ts", "index.ts", "workerd.ts"]
44
}

transport/workerd.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This file is used when running on the `workerd`.
2+
// Specifically workers on Cloudflare.
3+
// It is the same as `edge-light.ts`, which runs on Vercel.
4+
// It uses DOM based APIs (`@connectrpc/connect-web`) to connect to the API.
5+
// Differing from `bun.ts` this solves the `redirect` option set to `error`
6+
// inside `connect` as that does not work on the edge.
7+
//
8+
// For more information, see:
9+
//
10+
// * <https://github.com/connectrpc/connect-es/pull/589>
11+
// * <https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516>
12+
// * <https://github.com/connectrpc/connect-es/pull/1082>
13+
// * <https://github.com/e2b-dev/E2B/pull/669/files>
14+
import { createConnectTransport } from "@connectrpc/connect-web";
15+
16+
export function createTransport(baseUrl: string) {
17+
return createConnectTransport({
18+
baseUrl,
19+
interceptors: [
20+
(next) => (req) => {
21+
req.init.redirect = "follow";
22+
return next(req);
23+
},
24+
],
25+
fetch,
26+
});
27+
}

0 commit comments

Comments
 (0)