rwsdk 1.0.0-beta.30 → 1.0.0-beta.30-test.20251119220440

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,6 @@ import { DefaultAppContext, RequestInfo } from "./types";
2
2
  type DefaultRequestInfo = RequestInfo<DefaultAppContext>;
3
3
  export declare const requestInfo: DefaultRequestInfo;
4
4
  export declare function getRequestInfo(): RequestInfo;
5
- export declare function waitForRequestInfo(): Promise<DefaultRequestInfo>;
6
5
  export declare function runWithRequestInfo<Result>(nextRequestInfo: DefaultRequestInfo, fn: () => Result): Result;
7
6
  export declare function runWithRequestInfoOverrides<Result>(overrides: Partial<DefaultRequestInfo>, fn: () => Result): Result;
8
7
  export {};
@@ -1,6 +1,5 @@
1
1
  import { AsyncLocalStorage } from "async_hooks";
2
2
  import { defineRwState } from "rwsdk/__state";
3
- const requestInfoDeferred = defineRwState("requestInfoDeferred", () => Promise.withResolvers());
4
3
  const requestInfoStore = defineRwState("requestInfoStore", () => new AsyncLocalStorage());
5
4
  const requestInfoBase = {};
6
5
  const REQUEST_INFO_KEYS = ["request", "params", "ctx", "rw", "cf", "response"];
@@ -22,15 +21,8 @@ export function getRequestInfo() {
22
21
  }
23
22
  return store;
24
23
  }
25
- export function waitForRequestInfo() {
26
- return requestInfoDeferred.promise;
27
- }
28
24
  export function runWithRequestInfo(nextRequestInfo, fn) {
29
- const runWithRequestInfoFn = () => {
30
- requestInfoDeferred.resolve(nextRequestInfo);
31
- return fn();
32
- };
33
- return requestInfoStore.run(nextRequestInfo, runWithRequestInfoFn);
25
+ return requestInfoStore.run(nextRequestInfo, fn);
34
26
  }
35
27
  export function runWithRequestInfoOverrides(overrides, fn) {
36
28
  const requestInfo = requestInfoStore.getStore();
@@ -42,6 +42,10 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
42
42
  log("Development mode: %s", isDev);
43
43
  },
44
44
  configureServer(server) {
45
+ // context(justinvdm, 19 Nov 2025): This hook simply saves a reference
46
+ // to the dev server instance for use in other hooks. Unlike plugins that
47
+ // must run before the Cloudflare plugin to prevent startup deadlocks,
48
+ // its execution order is not critical, so `enforce: 'pre'` is not needed.
45
49
  devServer = server;
46
50
  },
47
51
  configEnvironment(env, viteConfig) {
@@ -6,6 +6,10 @@ export const devServerTimingPlugin = () => {
6
6
  return {
7
7
  name: "rwsdk:dev-server-timing",
8
8
  configureServer(server) {
9
+ // context(justinvdm, 19 Nov 2025): This hook adds a middleware for
10
+ // logging the time to first response. Unlike other plugins that must
11
+ // run before the Cloudflare plugin to prevent startup deadlocks, its
12
+ // execution order is not critical, so `enforce: 'pre'` is not needed.
9
13
  server.middlewares.use((_req, res, next) => {
10
14
  if (!hasLoggedFirstResponse) {
11
15
  res.on("finish", () => {
@@ -37,7 +37,15 @@ export const directiveModulesDevPlugin = ({ clientFiles, serverFiles, projectRoo
37
37
  const APP_SERVER_BARREL_PATH = path.join(tempDir, "app-server-barrel.js");
38
38
  return {
39
39
  name: "rwsdk:directive-modules-dev",
40
+ enforce: "pre",
40
41
  configureServer(server) {
42
+ // context(justinvdm, 19 Nov 2025): We must run this hook before the
43
+ // Cloudflare plugin's `configureServer` hook. The Cloudflare plugin makes
44
+ // a request back to the dev server to determine worker exports, which
45
+ // triggers Vite's dependency optimizer. Our esbuild plugin for the
46
+ // optimizer blocks on `scanPromise`. By running this first with `enforce: 'pre'`,
47
+ // we ensure our scan is kicked off before the Cloudflare plugin can trigger
48
+ // the optimizer, preventing a deadlock.
41
49
  if (!process.env.VITE_IS_DEV_SERVER) {
42
50
  resolveScanPromise();
43
51
  return;
@@ -35,6 +35,10 @@ export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, })
35
35
  isBuild = config.command === "build";
36
36
  },
37
37
  configureServer(server) {
38
+ // context(justinvdm, 19 Nov 2025): This hook adds a middleware to track
39
+ // when the first server response has finished. Unlike plugins that must
40
+ // run before the Cloudflare plugin to prevent startup deadlocks, its
41
+ // execution order is not critical, so `enforce: 'pre'` is not needed.
38
42
  devServer = server;
39
43
  devServer.middlewares.use((_req, res, next) => {
40
44
  // context(justinvdm, 15 Jun 2025): We want to watch for new client and server modules
@@ -41,7 +41,12 @@ export function hmrStabilityPlugin() {
41
41
  return null;
42
42
  },
43
43
  configureServer(server) {
44
- // Return a function to ensure our middleware is placed after internal middlewares
44
+ // context(justinvdm, 19 Nov 2025): This hook adds an error handling
45
+ // middleware for stale dependency errors. It runs in a returned function,
46
+ // which intentionally places it late in the middleware stack. Unlike
47
+ // other plugins that must run before the Cloudflare plugin to prevent
48
+ // startup deadlocks, its timing is not critical, so `enforce: 'pre'`
49
+ // is not needed.
45
50
  return () => {
46
51
  server.middlewares.use(async function rwsdkStaleBundleErrorHandler(err, req, res, next) {
47
52
  if (err &&
@@ -95,15 +95,32 @@ export const knownDepsResolverPlugin = ({ projectRootDir, }) => {
95
95
  if (!mappings) {
96
96
  return null;
97
97
  }
98
+ // Create reverse mapping from slugified names to original imports
99
+ // Vite converts "react-dom/server.edge" -> "react-dom_server__edge"
100
+ // Pattern: / becomes _, . becomes __
101
+ const slugifiedToOriginal = new Map();
102
+ for (const [original, resolved] of mappings) {
103
+ const slugified = original.replace(/\//g, "_").replace(/\./g, "__");
104
+ slugifiedToOriginal.set(slugified, original);
105
+ }
98
106
  return {
99
107
  name: `rwsdk:known-dependencies-resolver-esbuild-${envName}`,
100
108
  setup(build) {
101
109
  build.onResolve({ filter: /.*/ }, (args) => {
102
110
  let resolved = mappings.get(args.path);
111
+ // If not found, check if it's a slugified version
112
+ if (!resolved) {
113
+ const originalImport = slugifiedToOriginal.get(args.path);
114
+ if (originalImport) {
115
+ resolved = mappings.get(originalImport);
116
+ }
117
+ }
103
118
  if (!resolved) {
104
119
  resolved = resolveKnownImport(args.path, envName, projectRootDir);
105
120
  }
106
- if (resolved && args.importer !== "") {
121
+ // Resolve for both entry points (importer === '') and regular imports
122
+ // Entry points come from optimizeDeps.include and are critical to intercept
123
+ if (resolved) {
107
124
  if (args.path === "react-server-dom-webpack/client.edge") {
108
125
  return;
109
126
  }
@@ -118,11 +135,23 @@ export const knownDepsResolverPlugin = ({ projectRootDir, }) => {
118
135
  return [
119
136
  {
120
137
  name: "rwsdk:known-dependencies-resolver:config",
121
- enforce: "post",
138
+ enforce: "pre",
122
139
  config(config, { command }) {
123
140
  isBuild = command === "build";
124
141
  log("Configuring plugin for command=%s", command);
125
142
  },
143
+ async configureServer(server) {
144
+ // context(justinvdm, 19 Nov 2025): This hook must run before the
145
+ // Cloudflare plugin's `configureServer` hook, so we use `enforce: 'pre'`.
146
+ // The Cloudflare plugin's hook executes the worker entry file to discover
147
+ // its exports. This can trigger the evaluation of SSR code. We must initialize
148
+ // the SSR dependency optimizer *before* that happens to ensure that any
149
+ // dependencies in `optimizeDeps.include` (like `react-dom/server.edge`)
150
+ // are correctly registered before they are discovered lazily.
151
+ if (server.environments.ssr?.depsOptimizer) {
152
+ await server.environments.ssr.depsOptimizer.init();
153
+ }
154
+ },
126
155
  configResolved(config) {
127
156
  log("Setting up resolve aliases and optimizeDeps for each environment");
128
157
  // Set up aliases and optimizeDeps for each environment
@@ -37,6 +37,11 @@ export const miniflareHMRPlugin = (givenOptions) => [
37
37
  {
38
38
  name: "rwsdk:miniflare-hmr",
39
39
  configureServer(server) {
40
+ // context(justinvdm, 19 Nov 2025): This hook sets up an error handler
41
+ // middleware. It runs in a returned function, which intentionally
42
+ // places it late in the middleware stack. Unlike plugins that must
43
+ // run before the Cloudflare plugin to prevent startup deadlocks, its
44
+ // timing is not critical, so `enforce: 'pre'` is not needed.
40
45
  return () => {
41
46
  server.middlewares.use(function rwsdkDevServerErrorHandler(err, _req, _res, next) {
42
47
  if (err) {
@@ -12,6 +12,10 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
12
12
  name: "rwsdk:ssr-bridge",
13
13
  enforce: "pre",
14
14
  configureServer(server) {
15
+ // context(justinvdm, 19 Nov 2025): This plugin patches the dev server's
16
+ // HMR and optimizer behavior to coordinate the `ssr` and `worker`
17
+ // environments. It runs with `enforce: 'pre'` to ensure these patches
18
+ // are in place before other plugins start interacting with the server.
15
19
  devServer = server;
16
20
  const ssrHot = server.environments.ssr.hot;
17
21
  const originalSsrHotSend = ssrHot.send;
@@ -43,7 +43,12 @@ export function staleDepRetryPlugin() {
43
43
  return null;
44
44
  },
45
45
  configureServer(server) {
46
- // Return a function to ensure our middleware is placed after internal middlewares
46
+ // context(justinvdm, 19 Nov 2025): This hook adds an error handling
47
+ // middleware for stale dependency errors. It runs in a returned function,
48
+ // which intentionally places it late in the middleware stack. Unlike
49
+ // other plugins that must run before the Cloudflare plugin to prevent
50
+ // startup deadlocks, its timing is not critical, so `enforce: 'pre'`
51
+ // is not needed.
47
52
  return () => {
48
53
  server.middlewares.use(async function rwsdkStaleBundleErrorHandler(err, req, res, next) {
49
54
  if (err &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-beta.30",
3
+ "version": "1.0.0-beta.30-test.20251119220440",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {