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
2 changes: 1 addition & 1 deletion __tests__/test-server-workerd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// build step for it. Instead, we're getting by configuring the worker in vitest.config.ts by
// just specifying the raw JS modules.

import { newWorkersRpcResponse } from "../dist/index.js";
import { newWorkersRpcResponse } from "../dist/index-workers.js";
import { RpcTarget, DurableObject } from "cloudflare:workers";

// TODO(cleanup): At present we clone the implementation of Counter and TestTarget because
Expand Down
2 changes: 1 addition & 1 deletion __tests__/workerd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <reference types="@cloudflare/workers-types" />
import { expect, it, describe } from "vitest";
import { RpcStub as NativeRpcStub, RpcTarget as NativeRpcTarget, env, DurableObject } from "cloudflare:workers";
import { newHttpBatchRpcSession, newWebSocketRpcSession, RpcStub, RpcTarget } from "../src/index.js";
import { newHttpBatchRpcSession, newWebSocketRpcSession, RpcStub, RpcTarget } from "../src/index-workers.js";
import { Counter, TestTarget } from "./test-util.js";

class JsCounter extends RpcTarget {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
],
"exports": {
".": {
"import": "./dist/index.js",
"import": {
"workerd": "./dist/index-workers.js",
"default": "./dist/index.js"
},
"types": "./dist/index.d.ts"
}
},
Expand Down
7 changes: 2 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// https://opensource.org/license/mit

import type { RpcTargetBranded, __RPC_TARGET_BRAND } from "./types.js";
import { WORKERS_MODULE_SYMBOL } from "./symbols.js"

// Polyfill Symbol.dispose for browsers that don't support it yet
if (!Symbol.dispose) {
Expand All @@ -12,11 +13,7 @@ if (!Symbol.asyncDispose) {
(Symbol as any).asyncDispose = Symbol.for('asyncDispose');
}

let workersModuleName = navigator.userAgent === "Cloudflare-Workers" ? "cloudflare:workers" : null;
let workersModule: any;
if (workersModuleName) {
workersModule = await import(/* @vite-ignore */workersModuleName);
}
let workersModule: any = (globalThis as any)[WORKERS_MODULE_SYMBOL];

export interface RpcTarget {
[__RPC_TARGET_BRAND]: never;
Expand Down
8 changes: 8 additions & 0 deletions src/index-workers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the MIT license found in the LICENSE.txt file or at:
// https://opensource.org/license/mit

// When building for Cloudflare Workers, this file is the top-level module, instead of index.ts.
// This ensures that inject-workers-module.js gets imported before the rest of the library.
import "./inject-workers-module.js";
export * from "./index.js";
14 changes: 14 additions & 0 deletions src/inject-workers-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the MIT license found in the LICENSE.txt file or at:
// https://opensource.org/license/mit

import { WORKERS_MODULE_SYMBOL } from "./symbols.js";

// Import cloudflare:workers and stick it in the global scope where in can be used conditionally.
// As long as inject-workers-module.ts is imported before the rest of the library, this allows the
// library to set up automatic interoperability with Cloudflare Workers' built-in RPC.
//
// Meanwhile, we define our `exports` in package.json such that when building on Workers, this
// module is in fact imported first.
import * as cfw from "cloudflare:workers";
(globalThis as any)[WORKERS_MODULE_SYMBOL] = cfw;
5 changes: 5 additions & 0 deletions src/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the MIT license found in the LICENSE.txt file or at:
// https://opensource.org/license/mit

export let WORKERS_MODULE_SYMBOL = Symbol("workers-module");
3 changes: 2 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
entry: ['src/index.ts', 'src/index-workers.ts'],
format: ['esm'],
external: ['cloudflare:workers'],
dts: true,
sourcemap: true,
clean: true,
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default defineConfig({
},
{
type: "ESModule",
path: "./dist/index.js",
path: "./dist/index-workers.js",
},
],
durableObjects: {
Expand Down