rwsdk 1.0.9 → 1.1.0

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.
@@ -231,10 +231,15 @@ export function createDeployment() {
231
231
  ? match[1]
232
232
  : Math.random().toString(36).substring(2, 15);
233
233
  const deployResult = await runRelease(projectDir, projectDir, resourceUniqueKey);
234
+ // A fresh *.workers.dev subdomain can return 200 with Cloudflare's
235
+ // "There is nothing here yet" placeholder before the worker code
236
+ // propagates globally. Wait until the response body contains the
237
+ // rwsdk-rendered marker so tests don't run against the placeholder.
234
238
  await poll(async () => {
235
239
  try {
236
240
  const response = await fetch(deployResult.url);
237
- return response.status > 0;
241
+ const body = await response.text();
242
+ return body.includes("__RWSDK_CONTEXT");
238
243
  }
239
244
  catch (e) {
240
245
  return false;
@@ -75,9 +75,9 @@ export async function createSmokeTestStylesheets(targetDir) {
75
75
  const urlStylesPath = join(appDir, "smokeTestUrlStyles.css");
76
76
  log("Creating smokeTestUrlStyles.css at: %s", urlStylesPath);
77
77
  await fs.writeFile(urlStylesPath, smokeTestUrlStylesCssTemplate);
78
- // Modify Document.tsx to include the URL stylesheet using CSS URL import
79
- const documentPath = join(appDir, "Document.tsx");
80
- log("Modifying Document.tsx to include URL stylesheet at: %s", documentPath);
78
+ // Modify document.tsx to include the URL stylesheet using CSS URL import
79
+ const documentPath = join(appDir, "document.tsx");
80
+ log("Modifying document.tsx to include URL stylesheet at: %s", documentPath);
81
81
  try {
82
82
  const documentContent = await fs.readFile(documentPath, "utf-8");
83
83
  const s = new MagicString(documentContent);
@@ -90,14 +90,14 @@ export async function createSmokeTestStylesheets(targetDir) {
90
90
  if (headTagEnd !== -1) {
91
91
  s.appendLeft(headTagEnd, ' <link rel="stylesheet" href={smokeTestUrlStyles} />\n');
92
92
  await fs.writeFile(documentPath, s.toString(), "utf-8");
93
- log("Successfully modified Document.tsx with CSS URL import pattern");
93
+ log("Successfully modified document.tsx with CSS URL import pattern");
94
94
  }
95
95
  else {
96
- log("Could not find </head> tag in Document.tsx");
96
+ log("Could not find </head> tag in document.tsx");
97
97
  }
98
98
  }
99
99
  catch (e) {
100
- log("Could not modify Document.tsx: %s", e);
100
+ log("Could not modify document.tsx: %s", e);
101
101
  }
102
102
  log("Smoke test stylesheets created successfully");
103
103
  }
@@ -9,6 +9,35 @@ export { $expect, deleteD1Database, deleteWorker, isRelatedToTest, listD1Databas
9
9
  export async function runRelease(cwd, projectDir, resourceUniqueKey) {
10
10
  return runE2ERelease(cwd, projectDir, resourceUniqueKey);
11
11
  }
12
+ async function waitForDeploymentContent(baseUrl, { timeoutMs = 60_000, intervalMs = 2_000, } = {}) {
13
+ const marker = "__RWSDK_CONTEXT";
14
+ const deadline = Date.now() + timeoutMs;
15
+ let attempt = 0;
16
+ let lastStatus;
17
+ let lastBytes = 0;
18
+ while (Date.now() < deadline) {
19
+ attempt += 1;
20
+ try {
21
+ const res = await fetch(baseUrl);
22
+ const body = await res.text();
23
+ lastStatus = res.status;
24
+ lastBytes = body.length;
25
+ if (body.includes(marker)) {
26
+ log("Deployment content verified at %s after %d attempt(s)", baseUrl, attempt);
27
+ console.log(`✅ Deployment content ready at ${baseUrl} (attempt ${attempt})`);
28
+ return;
29
+ }
30
+ log("Attempt %d: %s returned %d (%d bytes), no app marker yet", attempt, baseUrl, res.status, body.length);
31
+ }
32
+ catch (err) {
33
+ log("Attempt %d: fetch failed for %s: %O", attempt, baseUrl, err);
34
+ }
35
+ await setTimeout(intervalMs);
36
+ }
37
+ throw new Error(`Deployment at ${baseUrl} did not serve app content within ${timeoutMs}ms ` +
38
+ `(last status ${lastStatus ?? "n/a"}, ${lastBytes} bytes). ` +
39
+ `Likely Cloudflare *.workers.dev propagation still in progress.`);
40
+ }
12
41
  /**
13
42
  * Runs tests against the production deployment
14
43
  */
@@ -23,6 +52,12 @@ export async function runReleaseTest(artifactDir, resources, browserPath, headle
23
52
  await setTimeout(1000);
24
53
  // DRY: check both root and custom path
25
54
  await checkServerUp(url, "/");
55
+ // A fresh *.workers.dev subdomain can return 200 with Cloudflare's
56
+ // "There is nothing here yet" placeholder before the worker code is
57
+ // globally propagated. Poll the URL until the response body contains
58
+ // an rwsdk-rendered marker so we don't run the browser tests against
59
+ // the placeholder.
60
+ await waitForDeploymentContent(url);
26
61
  // Now run the tests with the custom path
27
62
  const testUrl = new URL("/__smoke_test", url).toString();
28
63
  await checkUrl(testUrl, artifactDir, browserPath, headless, bail, skipClient, "Production", realtime, resources.targetDir, // Add target directory parameter
@@ -11,6 +11,34 @@ type WrappedServerFunction<TArgs extends any[] = any[], TResult = any> = {
11
11
  (...args: TArgs): Promise<TResult>;
12
12
  method?: "GET" | "POST";
13
13
  };
14
+ export type ServerFunctionWrap = (fn: Function, args: any[], type: "action" | "query") => Promise<any>;
15
+ /**
16
+ * Register a wrapper that runs around every server action and query handler.
17
+ *
18
+ * Call this once in your worker entry point. The wrapper receives the main
19
+ * handler function, its arguments, and the type ("action" or "query").
20
+ * Interruptors run *outside* the wrapper.
21
+ *
22
+ * Throws if called more than once.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { registerServerFunctionWrap } from "rwsdk/worker";
27
+ * import * as Sentry from "@sentry/cloudflare";
28
+ *
29
+ * registerServerFunctionWrap((fn, args, type) =>
30
+ * Sentry.startSpan(
31
+ * { name: fn.name, op: `function.rsc_${type}` },
32
+ * () => fn(...args)
33
+ * )
34
+ * );
35
+ * ```
36
+ */
37
+ export declare function registerServerFunctionWrap(wrap: ServerFunctionWrap): void;
38
+ /**
39
+ * @internal Reset the global wrap — only for tests.
40
+ */
41
+ export declare function __resetServerFunctionWrap(): void;
14
42
  /**
15
43
  * Wrap a function to be used as a server query.
16
44
  *
@@ -1,4 +1,40 @@
1
1
  import { requestInfo } from "./requestInfo/worker";
2
+ let globalWrap;
3
+ /**
4
+ * Register a wrapper that runs around every server action and query handler.
5
+ *
6
+ * Call this once in your worker entry point. The wrapper receives the main
7
+ * handler function, its arguments, and the type ("action" or "query").
8
+ * Interruptors run *outside* the wrapper.
9
+ *
10
+ * Throws if called more than once.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { registerServerFunctionWrap } from "rwsdk/worker";
15
+ * import * as Sentry from "@sentry/cloudflare";
16
+ *
17
+ * registerServerFunctionWrap((fn, args, type) =>
18
+ * Sentry.startSpan(
19
+ * { name: fn.name, op: `function.rsc_${type}` },
20
+ * () => fn(...args)
21
+ * )
22
+ * );
23
+ * ```
24
+ */
25
+ export function registerServerFunctionWrap(wrap) {
26
+ if (globalWrap) {
27
+ throw new Error("registerServerFunctionWrap() has already been called. " +
28
+ "Only one wrapper can be registered.");
29
+ }
30
+ globalWrap = wrap;
31
+ }
32
+ /**
33
+ * @internal Reset the global wrap — only for tests.
34
+ */
35
+ export function __resetServerFunctionWrap() {
36
+ globalWrap = undefined;
37
+ }
2
38
  function createServerFunction(fns, mainFn, options) {
3
39
  const wrapped = async (...args) => {
4
40
  const { request, ctx } = requestInfo;
@@ -11,6 +47,9 @@ function createServerFunction(fns, mainFn, options) {
11
47
  return result;
12
48
  }
13
49
  }
50
+ if (globalWrap) {
51
+ return globalWrap(mainFn, args, options?.__type ?? "action");
52
+ }
14
53
  return mainFn(...args);
15
54
  };
16
55
  wrapped.method = options?.method ?? "POST";
@@ -45,7 +84,7 @@ export function serverQuery(fnsOrFn, options) {
45
84
  mainFn = fnsOrFn;
46
85
  }
47
86
  const method = options?.method ?? "GET"; // Default to GET for query
48
- const wrapped = createServerFunction(fns, mainFn, { ...options, method });
87
+ const wrapped = createServerFunction(fns, mainFn, { ...options, method, __type: "query" });
49
88
  wrapped.method = method;
50
89
  return wrapped;
51
90
  }
@@ -78,7 +117,7 @@ export function serverAction(fnsOrFn, options) {
78
117
  mainFn = fnsOrFn;
79
118
  }
80
119
  const method = options?.method ?? "POST"; // Default to POST for action
81
- const wrapped = createServerFunction(fns, mainFn, { ...options, method });
120
+ const wrapped = createServerFunction(fns, mainFn, { ...options, method, __type: "action" });
82
121
  wrapped.method = method;
83
122
  return wrapped;
84
123
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,130 @@
1
+ import { describe, expect, it, vi, beforeEach } from "vitest";
2
+ let mockRequestInfo;
3
+ vi.mock("./requestInfo/worker", () => ({
4
+ get requestInfo() {
5
+ return mockRequestInfo;
6
+ },
7
+ }));
8
+ import { serverAction, serverQuery, registerServerFunctionWrap, __resetServerFunctionWrap, } from "./server";
9
+ describe("registerServerFunctionWrap", () => {
10
+ beforeEach(() => {
11
+ __resetServerFunctionWrap();
12
+ mockRequestInfo = {
13
+ request: new Request("https://test.example/"),
14
+ ctx: {},
15
+ };
16
+ });
17
+ it("wraps serverAction handlers", async () => {
18
+ const wrapSpy = vi.fn((fn, args, _type) => fn(...args));
19
+ registerServerFunctionWrap(wrapSpy);
20
+ const action = serverAction(async function createThing(name) {
21
+ return `created ${name}`;
22
+ });
23
+ const result = await action("foo");
24
+ expect(wrapSpy).toHaveBeenCalledOnce();
25
+ expect(wrapSpy.mock.calls[0][2]).toBe("action");
26
+ expect(result).toBe("created foo");
27
+ });
28
+ it("wraps serverQuery handlers", async () => {
29
+ const wrapSpy = vi.fn((fn, args, _type) => fn(...args));
30
+ registerServerFunctionWrap(wrapSpy);
31
+ const query = serverQuery(async function getThings() {
32
+ return [1, 2, 3];
33
+ });
34
+ const result = await query();
35
+ expect(wrapSpy).toHaveBeenCalledOnce();
36
+ expect(wrapSpy.mock.calls[0][2]).toBe("query");
37
+ expect(result).toEqual([1, 2, 3]);
38
+ });
39
+ it("passes the main function and args to the wrapper", async () => {
40
+ const wrapSpy = vi.fn((fn, args, _type) => fn(...args));
41
+ registerServerFunctionWrap(wrapSpy);
42
+ const action = serverAction(async function multiply(a, b) {
43
+ return a * b;
44
+ });
45
+ await action(3, 7);
46
+ const [fn, args] = wrapSpy.mock.calls[0];
47
+ expect(fn.name).toBe("multiply");
48
+ expect(args).toEqual([3, 7]);
49
+ });
50
+ it("interruptors run outside the wrapper", async () => {
51
+ const order = [];
52
+ registerServerFunctionWrap((fn, args, _type) => {
53
+ order.push("wrap:start");
54
+ const result = fn(...args);
55
+ order.push("wrap:end");
56
+ return result;
57
+ });
58
+ const interruptor = async () => {
59
+ order.push("interruptor");
60
+ };
61
+ const action = serverAction([
62
+ interruptor,
63
+ async function doWork() {
64
+ order.push("handler");
65
+ return "done";
66
+ },
67
+ ]);
68
+ await action();
69
+ expect(order).toEqual([
70
+ "interruptor",
71
+ "wrap:start",
72
+ "handler",
73
+ "wrap:end",
74
+ ]);
75
+ });
76
+ it("wrapper is not called when an interruptor short-circuits", async () => {
77
+ const wrapSpy = vi.fn((fn, args, _type) => fn(...args));
78
+ registerServerFunctionWrap(wrapSpy);
79
+ const action = serverAction([
80
+ async () => new Response("blocked", { status: 403 }),
81
+ async function neverCalled() {
82
+ return "nope";
83
+ },
84
+ ]);
85
+ const result = await action();
86
+ expect(wrapSpy).not.toHaveBeenCalled();
87
+ expect(result).toBeInstanceOf(Response);
88
+ });
89
+ it("throws if called more than once", () => {
90
+ registerServerFunctionWrap(async (fn, args) => fn(...args));
91
+ expect(() => {
92
+ registerServerFunctionWrap(async (fn, args) => fn(...args));
93
+ }).toThrow("registerServerFunctionWrap() has already been called");
94
+ });
95
+ it("without registration, handlers work normally", async () => {
96
+ const action = serverAction(async function echo(msg) {
97
+ return msg;
98
+ });
99
+ const result = await action("hello");
100
+ expect(result).toBe("hello");
101
+ });
102
+ it("wrapper can transform the return value", async () => {
103
+ registerServerFunctionWrap(async (fn, args, _type) => {
104
+ const result = await fn(...args);
105
+ return { wrapped: true, result };
106
+ });
107
+ const action = serverAction(async function getValue() {
108
+ return 42;
109
+ });
110
+ const result = await action();
111
+ expect(result).toEqual({ wrapped: true, result: 42 });
112
+ });
113
+ it("works with array-style serverAction (interruptors + handler)", async () => {
114
+ const wrapSpy = vi.fn((fn, args, _type) => fn(...args));
115
+ registerServerFunctionWrap(wrapSpy);
116
+ const action = serverAction([
117
+ async ({ ctx }) => {
118
+ ctx.authed = true;
119
+ },
120
+ async function save(data) {
121
+ return `saved ${data}`;
122
+ },
123
+ ]);
124
+ const result = await action("test");
125
+ expect(result).toBe("saved test");
126
+ expect(wrapSpy).toHaveBeenCalledOnce();
127
+ const [fn] = wrapSpy.mock.calls[0];
128
+ expect(fn.name).toBe("save");
129
+ });
130
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {