rwsdk 0.1.19-test.20250717130914 → 0.1.19

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.
@@ -22,13 +22,7 @@ export class SqliteDurableObject extends DurableObject {
22
22
  }
23
23
  log("Initializing Durable Object database");
24
24
  const migrator = createMigrator(this.kysely, this.migrations, this.migrationTableName);
25
- const result = await migrator.migrateToLatest();
26
- if (result.error) {
27
- console.log("rwsdk/db: Migrations failed, rolling back and throwing with the migration error: %O", result.results);
28
- await migrator.migrateDown();
29
- throw result.error;
30
- }
31
- log("Migrations results", result.results);
25
+ await migrator.migrateToLatest();
32
26
  this.initialized = true;
33
27
  log("Database initialization complete");
34
28
  }
@@ -1,5 +1,5 @@
1
1
  import { Kysely } from "kysely";
2
- import { requestInfo, waitForRequestInfo } from "../../requestInfo/worker.js";
2
+ import { requestInfo } from "../../requestInfo/worker.js";
3
3
  import { DOWorkerDialect } from "./DOWorkerDialect.js";
4
4
  const createDurableObjectDb = (durableObjectBinding, name = "main") => {
5
5
  const durableObjectId = durableObjectBinding.idFromName(name);
@@ -12,13 +12,6 @@ const createDurableObjectDb = (durableObjectBinding, name = "main") => {
12
12
  export function createDb(durableObjectBinding, name = "main") {
13
13
  const cacheKey = `${durableObjectBinding}_${name}`;
14
14
  const doCreateDb = () => {
15
- if (!requestInfo.rw) {
16
- throw new Error(`
17
- rwsdk: A database created using createDb() was accessed before requestInfo was available.
18
-
19
- Please make sure database access is happening in a request handler or action handler.
20
- `);
21
- }
22
15
  let db = requestInfo.rw.databases.get(cacheKey);
23
16
  if (!db) {
24
17
  db = createDurableObjectDb(durableObjectBinding, name);
@@ -26,7 +19,7 @@ export function createDb(durableObjectBinding, name = "main") {
26
19
  }
27
20
  return db;
28
21
  };
29
- waitForRequestInfo().then(() => doCreateDb());
22
+ doCreateDb();
30
23
  return new Proxy({}, {
31
24
  get(target, prop, receiver) {
32
25
  const db = doCreateDb();
@@ -1,8 +1,5 @@
1
1
  import { RequestInfo, DefaultAppContext } from "./types";
2
- type DefaultRequestInfo = RequestInfo<DefaultAppContext>;
3
- export declare const requestInfo: DefaultRequestInfo;
2
+ export declare const requestInfo: RequestInfo<DefaultAppContext>;
4
3
  export declare function getRequestInfo(): RequestInfo;
5
- export declare function waitForRequestInfo(): Promise<DefaultRequestInfo>;
6
- export declare function runWithRequestInfo<Result>(nextRequestInfo: DefaultRequestInfo, fn: () => Result): Result;
7
- export declare function runWithRequestInfoOverrides<Result>(overrides: Partial<DefaultRequestInfo>, fn: () => Result): Result;
8
- export {};
4
+ export declare function runWithRequestInfo<Result>(context: Record<string, any>, fn: () => Result): Result;
5
+ export declare function runWithRequestInfoOverrides<Result>(overrides: Record<string, any>, fn: () => Result): Result;
@@ -1,5 +1,4 @@
1
1
  import { AsyncLocalStorage } from "async_hooks";
2
- const requestInfoDeferred = Promise.withResolvers();
3
2
  const requestInfoStore = new AsyncLocalStorage();
4
3
  const requestInfoBase = {};
5
4
  const REQUEST_INFO_KEYS = ["request", "params", "ctx", "headers", "rw", "cf"];
@@ -21,15 +20,8 @@ export function getRequestInfo() {
21
20
  }
22
21
  return store;
23
22
  }
24
- export function waitForRequestInfo() {
25
- return requestInfoDeferred.promise;
26
- }
27
- export function runWithRequestInfo(nextRequestInfo, fn) {
28
- const runWithRequestInfoFn = () => {
29
- requestInfoDeferred.resolve(nextRequestInfo);
30
- return fn();
31
- };
32
- return requestInfoStore.run(nextRequestInfo, runWithRequestInfoFn);
23
+ export function runWithRequestInfo(context, fn) {
24
+ return requestInfoStore.run(context, fn);
33
25
  }
34
26
  export function runWithRequestInfoOverrides(overrides, fn) {
35
27
  const requestInfo = requestInfoStore.getStore();
@@ -37,5 +29,5 @@ export function runWithRequestInfoOverrides(overrides, fn) {
37
29
  ...requestInfo,
38
30
  ...overrides,
39
31
  };
40
- return runWithRequestInfo(newRequestInfo, fn);
32
+ return requestInfoStore.run(newRequestInfo, fn);
41
33
  }
@@ -1,5 +1,5 @@
1
1
  export declare const defineScript: (fn: ({ env }: {
2
- env: Cloudflare.Env;
2
+ env: Env;
3
3
  }) => Promise<unknown>) => {
4
- fetch: (request: Request, env: Env, cf: ExecutionContext) => Promise<Response>;
4
+ fetch(request: Request, env: Env): Promise<Response>;
5
5
  };
@@ -1,11 +1,8 @@
1
- import { defineApp } from "./worker";
2
- import { env } from "cloudflare:workers";
3
1
  export const defineScript = (fn) => {
4
- const app = defineApp([
5
- async () => {
2
+ return {
3
+ async fetch(request, env) {
6
4
  await fn({ env });
7
5
  return new Response("Done!");
8
6
  },
9
- ]);
10
- return app;
7
+ };
11
8
  };
@@ -136,8 +136,10 @@ export const debugSync = async (opts) => {
136
136
  throw e;
137
137
  }
138
138
  // Initial sync for watch mode. We do it *after* acquiring the lock.
139
+ let initialSyncOk = false;
139
140
  try {
140
141
  await performSync(sdkDir, targetDir);
142
+ initialSyncOk = true;
141
143
  }
142
144
  catch (error) {
143
145
  console.error("❌ Initial sync failed:", error);
@@ -194,9 +196,9 @@ export const debugSync = async (opts) => {
194
196
  };
195
197
  process.on("SIGINT", cleanup);
196
198
  process.on("SIGTERM", cleanup);
197
- // Run the watched command even if the initial sync fails. This allows the
198
- // user to see application errors and iterate more quickly.
199
- runWatchedCommand();
199
+ if (initialSyncOk) {
200
+ runWatchedCommand();
201
+ }
200
202
  };
201
203
  if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
202
204
  const args = process.argv.slice(2);
@@ -1,4 +1,3 @@
1
- import path from "path";
2
1
  import { resolve } from "path";
3
2
  import { writeFile } from "fs/promises";
4
3
  import { unstable_readConfig } from "wrangler";
@@ -24,38 +23,27 @@ export const runWorkerScript = async (relativeScriptPath) => {
24
23
  const workerConfig = unstable_readConfig({
25
24
  config: workerConfigPath,
26
25
  });
27
- const workerEntryRelativePath = workerConfig.main;
28
- const workerEntryPath = workerEntryRelativePath ?? path.join(process.cwd(), "src/worker.tsx");
29
- const tmpDir = await tmp.dir({
30
- prefix: "rw-worker-run-",
31
- unsafeCleanup: true,
26
+ const tmpWorkerPath = await tmp.file({
27
+ postfix: ".json",
32
28
  });
33
- const relativeTmpWorkerEntryPath = "worker.tsx";
34
- const tmpWorkerPath = path.join(tmpDir.path, "wrangler.json");
35
- const tmpWorkerEntryPath = path.join(tmpDir.path, relativeTmpWorkerEntryPath);
36
29
  const scriptWorkerConfig = {
37
30
  ...workerConfig,
38
- configPath: tmpWorkerPath,
39
- userConfigPath: tmpWorkerPath,
40
- main: relativeTmpWorkerEntryPath,
31
+ configPath: tmpWorkerPath.path,
32
+ userConfigPath: tmpWorkerPath.path,
33
+ main: scriptPath,
41
34
  };
42
35
  try {
43
- await writeFile(tmpWorkerPath, JSON.stringify(scriptWorkerConfig, null, 2));
44
- await writeFile(tmpWorkerEntryPath, `
45
- export * from "${workerEntryPath}";
46
- export { default } from "${scriptPath}";
47
- `);
48
- debug("Worker config written to: %s", tmpWorkerPath);
49
- debug("Worker entry written to: %s", tmpWorkerEntryPath);
36
+ await writeFile(tmpWorkerPath.path, JSON.stringify(scriptWorkerConfig, null, 2));
37
+ debug("Worker config written to: %s", tmpWorkerPath.path);
50
38
  process.env.RWSDK_WORKER_RUN = "1";
51
39
  const server = await createViteServer({
52
40
  configFile: false,
53
41
  plugins: [
54
42
  redwood({
55
- configPath: tmpWorkerPath,
43
+ configPath: tmpWorkerPath.path,
56
44
  includeCloudflarePlugin: true,
57
45
  entry: {
58
- worker: tmpWorkerEntryPath,
46
+ worker: scriptPath,
59
47
  },
60
48
  }),
61
49
  ],
@@ -83,6 +71,7 @@ export { default } from "${scriptPath}";
83
71
  }
84
72
  finally {
85
73
  debug("Closing inspector servers...");
74
+ await tmpWorkerPath.cleanup();
86
75
  debug("Temporary files cleaned up");
87
76
  }
88
77
  // todo(justinvdm, 01 Apr 2025): Investigate what handles are remaining open
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.19-test.20250717130914",
3
+ "version": "0.1.19",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {