rwsdk 1.0.0-beta.2 → 1.0.0-beta.20
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.
- package/dist/lib/constants.d.mts +1 -0
- package/dist/lib/constants.mjs +1 -0
- package/dist/lib/e2e/constants.d.mts +14 -0
- package/dist/lib/e2e/constants.mjs +74 -0
- package/dist/lib/e2e/dev.mjs +0 -1
- package/dist/lib/e2e/environment.d.mts +1 -1
- package/dist/lib/e2e/environment.mjs +118 -18
- package/dist/lib/e2e/index.d.mts +1 -0
- package/dist/lib/e2e/index.mjs +1 -0
- package/dist/lib/e2e/poll.d.mts +1 -1
- package/dist/lib/e2e/testHarness.d.mts +36 -3
- package/dist/lib/e2e/testHarness.mjs +192 -116
- package/dist/runtime/client/client.d.ts +1 -0
- package/dist/runtime/client/client.js +2 -0
- package/dist/runtime/client/navigation.d.ts +8 -0
- package/dist/runtime/client/navigation.js +39 -31
- package/dist/runtime/entries/clientSSR.d.ts +1 -0
- package/dist/runtime/entries/clientSSR.js +3 -0
- package/dist/runtime/entries/router.d.ts +1 -0
- package/dist/runtime/entries/worker.d.ts +2 -0
- package/dist/runtime/entries/worker.js +2 -0
- package/dist/runtime/lib/db/createDb.d.ts +1 -2
- package/dist/runtime/lib/db/createDb.js +4 -0
- package/dist/runtime/lib/manifest.d.ts +1 -1
- package/dist/runtime/lib/manifest.js +7 -4
- package/dist/runtime/lib/realtime/client.js +8 -2
- package/dist/runtime/lib/router.d.ts +16 -21
- package/dist/runtime/lib/router.js +67 -1
- package/dist/runtime/lib/router.test.js +241 -0
- package/dist/runtime/lib/{rwContext.d.ts → types.d.ts} +1 -0
- package/dist/runtime/render/renderDocumentHtmlStream.d.ts +1 -1
- package/dist/runtime/render/renderToStream.d.ts +4 -2
- package/dist/runtime/render/renderToStream.js +21 -2
- package/dist/runtime/render/renderToString.d.ts +3 -1
- package/dist/runtime/requestInfo/types.d.ts +4 -1
- package/dist/runtime/requestInfo/utils.d.ts +9 -0
- package/dist/runtime/requestInfo/utils.js +44 -0
- package/dist/runtime/requestInfo/worker.js +3 -2
- package/dist/runtime/script.d.ts +1 -3
- package/dist/runtime/script.js +1 -10
- package/dist/runtime/state.d.ts +3 -0
- package/dist/runtime/state.js +13 -0
- package/dist/runtime/worker.js +25 -0
- package/dist/scripts/debug-sync.mjs +18 -20
- package/dist/scripts/smoke-test.mjs +4 -2
- package/dist/scripts/worker-run.d.mts +1 -1
- package/dist/scripts/worker-run.mjs +50 -113
- package/dist/vite/buildApp.mjs +34 -2
- package/dist/vite/directiveModulesDevPlugin.mjs +1 -1
- package/dist/vite/envResolvers.d.mts +11 -0
- package/dist/vite/envResolvers.mjs +20 -0
- package/dist/vite/hmrStabilityPlugin.d.mts +2 -0
- package/dist/vite/hmrStabilityPlugin.mjs +68 -0
- package/dist/vite/knownDepsResolverPlugin.d.mts +0 -6
- package/dist/vite/knownDepsResolverPlugin.mjs +1 -12
- package/dist/vite/linkerPlugin.d.mts +2 -1
- package/dist/vite/linkerPlugin.mjs +11 -3
- package/dist/vite/linkerPlugin.test.mjs +15 -0
- package/dist/vite/redwoodPlugin.mjs +7 -9
- package/dist/vite/runDirectivesScan.mjs +57 -12
- package/dist/vite/ssrBridgePlugin.mjs +104 -34
- package/dist/vite/staleDepRetryPlugin.d.mts +2 -0
- package/dist/vite/staleDepRetryPlugin.mjs +69 -0
- package/dist/vite/statePlugin.d.mts +4 -0
- package/dist/vite/statePlugin.mjs +62 -0
- package/package.json +13 -7
- package/dist/vite/manifestPlugin.d.mts +0 -4
- package/dist/vite/manifestPlugin.mjs +0 -63
- /package/dist/runtime/lib/{rwContext.js → types.js} +0 -0
|
@@ -4,44 +4,12 @@ import path, { basename, dirname, join as pathJoin } from "path";
|
|
|
4
4
|
import puppeteer from "puppeteer-core";
|
|
5
5
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, test, } from "vitest";
|
|
6
6
|
import { launchBrowser } from "./browser.mjs";
|
|
7
|
+
import { DEPLOYMENT_CHECK_TIMEOUT, DEPLOYMENT_MIN_TRIES, DEPLOYMENT_TIMEOUT, DEV_SERVER_MIN_TRIES, DEV_SERVER_TIMEOUT, HYDRATION_TIMEOUT, INSTALL_DEPENDENCIES_RETRIES, PUPPETEER_TIMEOUT, SETUP_PLAYGROUND_ENV_TIMEOUT, SETUP_WAIT_TIMEOUT, TEST_MAX_RETRIES, TEST_MAX_RETRIES_PER_CODE, } from "./constants.mjs";
|
|
7
8
|
import { runDevServer } from "./dev.mjs";
|
|
8
9
|
import { poll, pollValue } from "./poll.mjs";
|
|
9
10
|
import { deleteD1Database, deleteWorker, isRelatedToTest, runRelease, } from "./release.mjs";
|
|
10
11
|
import { setupTarballEnvironment } from "./tarball.mjs";
|
|
11
|
-
|
|
12
|
-
.RWSDK_SETUP_PLAYGROUND_ENV_TIMEOUT
|
|
13
|
-
? parseInt(process.env.RWSDK_SETUP_PLAYGROUND_ENV_TIMEOUT, 10)
|
|
14
|
-
: 15 * 60 * 1000;
|
|
15
|
-
const DEPLOYMENT_TIMEOUT = process.env.RWSDK_DEPLOYMENT_TIMEOUT
|
|
16
|
-
? parseInt(process.env.RWSDK_DEPLOYMENT_TIMEOUT, 10)
|
|
17
|
-
: 5 * 60 * 1000;
|
|
18
|
-
const DEPLOYMENT_MIN_TRIES = process.env.RWSDK_DEPLOYMENT_MIN_TRIES
|
|
19
|
-
? parseInt(process.env.RWSDK_DEPLOYMENT_MIN_TRIES, 10)
|
|
20
|
-
: 5;
|
|
21
|
-
const DEPLOYMENT_CHECK_TIMEOUT = process.env.RWSDK_DEPLOYMENT_CHECK_TIMEOUT
|
|
22
|
-
? parseInt(process.env.RWSDK_DEPLOYMENT_CHECK_TIMEOUT, 10)
|
|
23
|
-
: 5 * 60 * 1000;
|
|
24
|
-
const PUPPETEER_TIMEOUT = process.env.RWSDK_PUPPETEER_TIMEOUT
|
|
25
|
-
? parseInt(process.env.RWSDK_PUPPETEER_TIMEOUT, 10)
|
|
26
|
-
: 60 * 1000 * 2;
|
|
27
|
-
const HYDRATION_TIMEOUT = process.env.RWSDK_HYDRATION_TIMEOUT
|
|
28
|
-
? parseInt(process.env.RWSDK_HYDRATION_TIMEOUT, 10)
|
|
29
|
-
: 5000;
|
|
30
|
-
const DEV_SERVER_TIMEOUT = process.env.RWSDK_DEV_SERVER_TIMEOUT
|
|
31
|
-
? parseInt(process.env.RWSDK_DEV_SERVER_TIMEOUT, 10)
|
|
32
|
-
: 5 * 60 * 1000;
|
|
33
|
-
const DEV_SERVER_MIN_TRIES = process.env.RWSDK_DEV_SERVER_MIN_TRIES
|
|
34
|
-
? parseInt(process.env.RWSDK_DEV_SERVER_MIN_TRIES, 10)
|
|
35
|
-
: 5;
|
|
36
|
-
const SETUP_WAIT_TIMEOUT = process.env.RWSDK_SETUP_WAIT_TIMEOUT
|
|
37
|
-
? parseInt(process.env.RWSDK_SETUP_WAIT_TIMEOUT, 10)
|
|
38
|
-
: 10 * 60 * 1000;
|
|
39
|
-
const TEST_MAX_RETRIES = process.env.RWSDK_TEST_MAX_RETRIES
|
|
40
|
-
? parseInt(process.env.RWSDK_TEST_MAX_RETRIES, 10)
|
|
41
|
-
: 10;
|
|
42
|
-
const TEST_MAX_RETRIES_PER_CODE = process.env.RWSDK_TEST_MAX_RETRIES_PER_CODE
|
|
43
|
-
? parseInt(process.env.RWSDK_TEST_MAX_RETRIES_PER_CODE, 10)
|
|
44
|
-
: 6;
|
|
12
|
+
export { DEPLOYMENT_CHECK_TIMEOUT, DEPLOYMENT_MIN_TRIES, DEPLOYMENT_TIMEOUT, DEV_SERVER_MIN_TRIES, DEV_SERVER_TIMEOUT, HYDRATION_TIMEOUT, INSTALL_DEPENDENCIES_RETRIES, PUPPETEER_TIMEOUT, SETUP_PLAYGROUND_ENV_TIMEOUT, SETUP_WAIT_TIMEOUT, TEST_MAX_RETRIES, TEST_MAX_RETRIES_PER_CODE, };
|
|
45
13
|
// Environment variable flags for skipping tests
|
|
46
14
|
const SKIP_DEV_SERVER_TESTS = process.env.RWSDK_SKIP_DEV === "1";
|
|
47
15
|
const SKIP_DEPLOYMENT_TESTS = process.env.RWSDK_SKIP_DEPLOY === "1";
|
|
@@ -52,6 +20,8 @@ let globalDevInstancePromise = null;
|
|
|
52
20
|
let globalDeploymentInstancePromise = null;
|
|
53
21
|
let globalDevInstance = null;
|
|
54
22
|
let globalDeploymentInstance = null;
|
|
23
|
+
const devInstances = [];
|
|
24
|
+
const deploymentInstances = [];
|
|
55
25
|
let hooksRegistered = false;
|
|
56
26
|
/**
|
|
57
27
|
* Registers global cleanup hooks automatically
|
|
@@ -62,11 +32,11 @@ function ensureHooksRegistered() {
|
|
|
62
32
|
// Register global afterAll to clean up the playground environment
|
|
63
33
|
afterAll(async () => {
|
|
64
34
|
const cleanupPromises = [];
|
|
65
|
-
|
|
66
|
-
cleanupPromises.push(
|
|
35
|
+
for (const instance of devInstances) {
|
|
36
|
+
cleanupPromises.push(instance.stopDev());
|
|
67
37
|
}
|
|
68
|
-
|
|
69
|
-
cleanupPromises.push(
|
|
38
|
+
for (const instance of deploymentInstances) {
|
|
39
|
+
cleanupPromises.push(instance.cleanup());
|
|
70
40
|
}
|
|
71
41
|
if (globalDevPlaygroundEnv) {
|
|
72
42
|
cleanupPromises.push(globalDevPlaygroundEnv.cleanup());
|
|
@@ -75,8 +45,8 @@ function ensureHooksRegistered() {
|
|
|
75
45
|
cleanupPromises.push(globalDeployPlaygroundEnv.cleanup());
|
|
76
46
|
}
|
|
77
47
|
await Promise.all(cleanupPromises);
|
|
78
|
-
|
|
79
|
-
|
|
48
|
+
devInstances.length = 0;
|
|
49
|
+
deploymentInstances.length = 0;
|
|
80
50
|
globalDevPlaygroundEnv = null;
|
|
81
51
|
globalDeployPlaygroundEnv = null;
|
|
82
52
|
});
|
|
@@ -114,8 +84,10 @@ function getPlaygroundDirFromImportMeta(importMetaUrl) {
|
|
|
114
84
|
* and installs dependencies using a tarball of the SDK.
|
|
115
85
|
* This ensures that tests run in a clean, isolated environment.
|
|
116
86
|
*/
|
|
117
|
-
export function setupPlaygroundEnvironment(options
|
|
118
|
-
const { sourceProjectDir, monorepoRoot, dev = true, deploy = true, } = typeof options === "string"
|
|
87
|
+
export function setupPlaygroundEnvironment(options) {
|
|
88
|
+
const { sourceProjectDir, monorepoRoot, dev = true, deploy = true, autoStartDevServer = true, } = typeof options === "string"
|
|
89
|
+
? { sourceProjectDir: options, autoStartDevServer: true }
|
|
90
|
+
: options;
|
|
119
91
|
ensureHooksRegistered();
|
|
120
92
|
beforeAll(async () => {
|
|
121
93
|
let projectDir;
|
|
@@ -141,16 +113,19 @@ export function setupPlaygroundEnvironment(options = {}) {
|
|
|
141
113
|
projectDir: devEnv.targetDir,
|
|
142
114
|
cleanup: devEnv.cleanup,
|
|
143
115
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
116
|
+
if (autoStartDevServer) {
|
|
117
|
+
const devControl = createDevServer();
|
|
118
|
+
globalDevInstancePromise = devControl.start().then((instance) => {
|
|
119
|
+
globalDevInstance = instance;
|
|
120
|
+
return instance;
|
|
121
|
+
});
|
|
122
|
+
// Prevent unhandled promise rejections. The error will be handled inside
|
|
123
|
+
// the test's beforeEach hook where this promise is awaited.
|
|
124
|
+
globalDevInstancePromise.catch(() => { });
|
|
125
|
+
}
|
|
151
126
|
}
|
|
152
127
|
else {
|
|
153
|
-
|
|
128
|
+
globalDevPlaygroundEnv = null;
|
|
154
129
|
}
|
|
155
130
|
if (deploy) {
|
|
156
131
|
const deployEnv = await setupTarballEnvironment({
|
|
@@ -162,7 +137,10 @@ export function setupPlaygroundEnvironment(options = {}) {
|
|
|
162
137
|
projectDir: deployEnv.targetDir,
|
|
163
138
|
cleanup: deployEnv.cleanup,
|
|
164
139
|
};
|
|
165
|
-
|
|
140
|
+
const deployControl = createDeployment();
|
|
141
|
+
globalDeploymentInstancePromise = deployControl
|
|
142
|
+
.start()
|
|
143
|
+
.then((instance) => {
|
|
166
144
|
globalDeploymentInstance = instance;
|
|
167
145
|
return instance;
|
|
168
146
|
});
|
|
@@ -170,7 +148,7 @@ export function setupPlaygroundEnvironment(options = {}) {
|
|
|
170
148
|
globalDeploymentInstancePromise.catch(() => { });
|
|
171
149
|
}
|
|
172
150
|
else {
|
|
173
|
-
|
|
151
|
+
globalDeployPlaygroundEnv = null;
|
|
174
152
|
}
|
|
175
153
|
}, SETUP_PLAYGROUND_ENV_TIMEOUT);
|
|
176
154
|
}
|
|
@@ -178,82 +156,106 @@ export function setupPlaygroundEnvironment(options = {}) {
|
|
|
178
156
|
* Creates a dev server instance using the shared playground environment.
|
|
179
157
|
* Automatically registers cleanup to run after the test.
|
|
180
158
|
*/
|
|
181
|
-
export
|
|
182
|
-
|
|
183
|
-
|
|
159
|
+
export function createDevServer() {
|
|
160
|
+
ensureHooksRegistered();
|
|
161
|
+
if (!globalDevPlaygroundEnv) {
|
|
162
|
+
throw new Error("Dev playground environment not initialized. Enable `dev: true` in setupPlaygroundEnvironment.");
|
|
184
163
|
}
|
|
164
|
+
const { projectDir } = globalDevPlaygroundEnv;
|
|
185
165
|
const packageManager = process.env.PACKAGE_MANAGER || "pnpm";
|
|
186
|
-
|
|
187
|
-
timeout: DEV_SERVER_TIMEOUT,
|
|
188
|
-
minTries: DEV_SERVER_MIN_TRIES,
|
|
189
|
-
onRetry: (error, tries) => {
|
|
190
|
-
console.log(`Retrying dev server creation (attempt ${tries})... Error: ${error.message}`);
|
|
191
|
-
},
|
|
192
|
-
});
|
|
166
|
+
let instance = null;
|
|
193
167
|
return {
|
|
194
|
-
|
|
195
|
-
|
|
168
|
+
projectDir,
|
|
169
|
+
start: async () => {
|
|
170
|
+
if (instance)
|
|
171
|
+
return instance;
|
|
172
|
+
if (SKIP_DEV_SERVER_TESTS) {
|
|
173
|
+
throw new Error("Dev server tests are skipped via RWSDK_SKIP_DEV=1");
|
|
174
|
+
}
|
|
175
|
+
const devResult = await pollValue(() => runDevServer(packageManager, projectDir), {
|
|
176
|
+
timeout: DEV_SERVER_TIMEOUT,
|
|
177
|
+
minTries: DEV_SERVER_MIN_TRIES,
|
|
178
|
+
onRetry: (error, tries) => {
|
|
179
|
+
console.log(`Retrying dev server creation (attempt ${tries})... Error: ${error.message}`);
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
instance = {
|
|
183
|
+
url: devResult.url,
|
|
184
|
+
projectDir,
|
|
185
|
+
stopDev: devResult.stopDev,
|
|
186
|
+
};
|
|
187
|
+
devInstances.push(instance);
|
|
188
|
+
return instance;
|
|
189
|
+
},
|
|
196
190
|
};
|
|
197
191
|
}
|
|
198
192
|
/**
|
|
199
193
|
* Creates a deployment instance using the shared playground environment.
|
|
200
194
|
* Automatically registers cleanup to run after the test.
|
|
201
195
|
*/
|
|
202
|
-
export
|
|
203
|
-
|
|
204
|
-
|
|
196
|
+
export function createDeployment() {
|
|
197
|
+
ensureHooksRegistered();
|
|
198
|
+
if (!globalDeployPlaygroundEnv) {
|
|
199
|
+
throw new Error("Deploy playground environment not initialized. Enable `deploy: true` in setupPlaygroundEnvironment.");
|
|
205
200
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// Poll the URL to ensure it's live before proceeding
|
|
216
|
-
await poll(async () => {
|
|
217
|
-
try {
|
|
218
|
-
const response = await fetch(deployResult.url);
|
|
219
|
-
// We consider any response (even 4xx or 5xx) as success,
|
|
220
|
-
// as it means the worker is routable.
|
|
221
|
-
return response.status > 0;
|
|
222
|
-
}
|
|
223
|
-
catch (e) {
|
|
224
|
-
return false;
|
|
201
|
+
const { projectDir } = globalDeployPlaygroundEnv;
|
|
202
|
+
let instance = null;
|
|
203
|
+
return {
|
|
204
|
+
projectDir,
|
|
205
|
+
start: async () => {
|
|
206
|
+
if (instance)
|
|
207
|
+
return instance;
|
|
208
|
+
if (SKIP_DEPLOYMENT_TESTS) {
|
|
209
|
+
throw new Error("Deployment tests are skipped via RWSDK_SKIP_DEPLOY=1");
|
|
225
210
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
211
|
+
const newInstance = await pollValue(async () => {
|
|
212
|
+
const dirName = basename(projectDir);
|
|
213
|
+
const match = dirName.match(/-e2e-test-([a-f0-9]+)$/);
|
|
214
|
+
const resourceUniqueKey = match
|
|
215
|
+
? match[1]
|
|
216
|
+
: Math.random().toString(36).substring(2, 15);
|
|
217
|
+
const deployResult = await runRelease(projectDir, projectDir, resourceUniqueKey);
|
|
218
|
+
await poll(async () => {
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetch(deployResult.url);
|
|
221
|
+
return response.status > 0;
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}, {
|
|
227
|
+
timeout: DEPLOYMENT_CHECK_TIMEOUT,
|
|
228
|
+
});
|
|
229
|
+
const cleanup = async () => {
|
|
230
|
+
const performCleanup = async () => {
|
|
231
|
+
if (isRelatedToTest(deployResult.workerName, resourceUniqueKey)) {
|
|
232
|
+
await deleteWorker(deployResult.workerName, projectDir, resourceUniqueKey);
|
|
233
|
+
}
|
|
234
|
+
await deleteD1Database(resourceUniqueKey, projectDir, resourceUniqueKey);
|
|
235
|
+
};
|
|
236
|
+
performCleanup().catch((error) => {
|
|
237
|
+
console.warn(`Warning: Background deployment cleanup failed: ${error.message}`);
|
|
238
|
+
});
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
};
|
|
241
|
+
return {
|
|
242
|
+
url: deployResult.url,
|
|
243
|
+
workerName: deployResult.workerName,
|
|
244
|
+
resourceUniqueKey,
|
|
245
|
+
projectDir: projectDir,
|
|
246
|
+
cleanup,
|
|
247
|
+
};
|
|
248
|
+
}, {
|
|
249
|
+
timeout: DEPLOYMENT_TIMEOUT,
|
|
250
|
+
minTries: DEPLOYMENT_MIN_TRIES,
|
|
251
|
+
onRetry: (error, tries) => {
|
|
252
|
+
console.log(`Retrying deployment creation (attempt ${tries})... Error: ${error.message}`);
|
|
253
|
+
},
|
|
240
254
|
});
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return {
|
|
244
|
-
url: deployResult.url,
|
|
245
|
-
workerName: deployResult.workerName,
|
|
246
|
-
resourceUniqueKey,
|
|
247
|
-
projectDir: projectDir,
|
|
248
|
-
cleanup,
|
|
249
|
-
};
|
|
250
|
-
}, {
|
|
251
|
-
timeout: DEPLOYMENT_TIMEOUT,
|
|
252
|
-
minTries: DEPLOYMENT_MIN_TRIES,
|
|
253
|
-
onRetry: (error, tries) => {
|
|
254
|
-
console.log(`Retrying deployment creation (attempt ${tries})... Error: ${error.message}`);
|
|
255
|
+
deploymentInstances.push(newInstance);
|
|
256
|
+
return newInstance;
|
|
255
257
|
},
|
|
256
|
-
}
|
|
258
|
+
};
|
|
257
259
|
}
|
|
258
260
|
/**
|
|
259
261
|
* Executes a test function with a retry mechanism for specific error codes.
|
|
@@ -302,7 +304,7 @@ function createTestRunner(testFn, envType) {
|
|
|
302
304
|
return (name, testLogic) => {
|
|
303
305
|
if ((envType === "dev" && SKIP_DEV_SERVER_TESTS) ||
|
|
304
306
|
(envType === "deploy" && SKIP_DEPLOYMENT_TESTS)) {
|
|
305
|
-
test.skip(name
|
|
307
|
+
test.skip(`${name} (${envType})`, () => { });
|
|
306
308
|
return;
|
|
307
309
|
}
|
|
308
310
|
describe.concurrent(name, () => {
|
|
@@ -364,12 +366,86 @@ function createTestRunner(testFn, envType) {
|
|
|
364
366
|
browser: browser,
|
|
365
367
|
page: page,
|
|
366
368
|
url: instance.url,
|
|
369
|
+
projectDir: instance
|
|
370
|
+
.projectDir,
|
|
367
371
|
});
|
|
368
372
|
});
|
|
369
373
|
});
|
|
370
374
|
});
|
|
371
375
|
};
|
|
372
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Creates a low-level test runner that provides utilities for creating
|
|
379
|
+
* tests that need to perform setup actions before the server starts.
|
|
380
|
+
*/
|
|
381
|
+
function createSDKTestRunner() {
|
|
382
|
+
const internalRunner = (testFn) => {
|
|
383
|
+
return (name, testLogic) => {
|
|
384
|
+
describe.concurrent(name, () => {
|
|
385
|
+
let page;
|
|
386
|
+
let browser;
|
|
387
|
+
beforeAll(async () => {
|
|
388
|
+
const tempDir = path.join(os.tmpdir(), "rwsdk-e2e-tests");
|
|
389
|
+
const wsEndpointFile = path.join(tempDir, "wsEndpoint");
|
|
390
|
+
try {
|
|
391
|
+
const wsEndpoint = await fs.readFile(wsEndpointFile, "utf-8");
|
|
392
|
+
browser = await puppeteer.connect({
|
|
393
|
+
browserWSEndpoint: wsEndpoint,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
console.warn("Failed to connect to existing browser instance. " +
|
|
398
|
+
"This might happen if you are running a single test file. " +
|
|
399
|
+
"Launching a new browser instance instead.");
|
|
400
|
+
browser = await launchBrowser();
|
|
401
|
+
}
|
|
402
|
+
}, SETUP_WAIT_TIMEOUT);
|
|
403
|
+
afterAll(async () => {
|
|
404
|
+
if (browser) {
|
|
405
|
+
await browser.disconnect();
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
beforeEach(async () => {
|
|
409
|
+
if (!globalDevPlaygroundEnv && !globalDeployPlaygroundEnv) {
|
|
410
|
+
throw new Error("Test environment not initialized. Call setupPlaygroundEnvironment() in your test file.");
|
|
411
|
+
}
|
|
412
|
+
page = await browser.newPage();
|
|
413
|
+
page.setDefaultTimeout(PUPPETEER_TIMEOUT);
|
|
414
|
+
}, SETUP_WAIT_TIMEOUT);
|
|
415
|
+
afterEach(async () => {
|
|
416
|
+
if (page) {
|
|
417
|
+
try {
|
|
418
|
+
await page.close();
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
console.warn(`Suppressing error during page.close() in test "${name}":`, error);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
testFn(">", async () => {
|
|
426
|
+
if (!browser) {
|
|
427
|
+
throw new Error("Test environment not ready.");
|
|
428
|
+
}
|
|
429
|
+
await runTestWithRetries(name, async () => {
|
|
430
|
+
await testLogic({
|
|
431
|
+
browser: browser,
|
|
432
|
+
page: page,
|
|
433
|
+
projectDir: globalDevPlaygroundEnv?.projectDir ||
|
|
434
|
+
globalDeployPlaygroundEnv?.projectDir ||
|
|
435
|
+
"",
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
const main = internalRunner(test);
|
|
443
|
+
return Object.assign(main, {
|
|
444
|
+
only: internalRunner(test.only),
|
|
445
|
+
skip: test.skip,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
export const testSDK = createSDKTestRunner();
|
|
373
449
|
/**
|
|
374
450
|
* High-level test wrapper for dev server tests.
|
|
375
451
|
* Automatically skips if RWSDK_SKIP_DEV=1
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "./setWebpackRequire";
|
|
2
2
|
export { default as React } from "react";
|
|
3
3
|
export { ClientOnly } from "./ClientOnly.js";
|
|
4
|
+
export { initClientNavigation, navigate } from "./navigation.js";
|
|
4
5
|
import type { HydrationOptions, Transport } from "./types";
|
|
5
6
|
export declare const fetchTransport: Transport;
|
|
6
7
|
export declare const initClient: ({ transport, hydrateRootOptions, handleResponse, }?: {
|
|
@@ -4,6 +4,7 @@ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
|
4
4
|
// context(justinvdm, 14 Aug 2025): `react-server-dom-webpack` uses this global
|
|
5
5
|
// to load modules, so we need to define it here before importing
|
|
6
6
|
// "react-server-dom-webpack."
|
|
7
|
+
// prettier-ignore
|
|
7
8
|
import "./setWebpackRequire";
|
|
8
9
|
import React from "react";
|
|
9
10
|
import { hydrateRoot } from "react-dom/client";
|
|
@@ -11,6 +12,7 @@ import { createFromFetch, createFromReadableStream, encodeReply, } from "react-s
|
|
|
11
12
|
import { rscStream } from "rsc-html-stream/client";
|
|
12
13
|
export { default as React } from "react";
|
|
13
14
|
export { ClientOnly } from "./ClientOnly.js";
|
|
15
|
+
export { initClientNavigation, navigate } from "./navigation.js";
|
|
14
16
|
export const fetchTransport = (transportContext) => {
|
|
15
17
|
const fetchCallServer = async (id, args) => {
|
|
16
18
|
const url = new URL(window.location.href);
|
|
@@ -4,6 +4,14 @@ export interface ClientNavigationOptions {
|
|
|
4
4
|
scrollBehavior?: "auto" | "smooth" | "instant";
|
|
5
5
|
}
|
|
6
6
|
export declare function validateClickEvent(event: MouseEvent, target: HTMLElement): boolean;
|
|
7
|
+
export interface NavigateOptions {
|
|
8
|
+
history?: "push" | "replace";
|
|
9
|
+
info?: {
|
|
10
|
+
scrollToTop?: boolean;
|
|
11
|
+
scrollBehavior?: "auto" | "smooth" | "instant";
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare function navigate(href: string, options?: NavigateOptions): Promise<void>;
|
|
7
15
|
export declare function initClientNavigation(opts?: ClientNavigationOptions): {
|
|
8
16
|
handleResponse: (response: Response) => boolean;
|
|
9
17
|
};
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
function saveScrollPosition(x, y) {
|
|
2
|
-
window.history.replaceState({
|
|
3
|
-
...window.history.state,
|
|
4
|
-
scrollX: x,
|
|
5
|
-
scrollY: y,
|
|
6
|
-
}, "", window.location.href);
|
|
7
|
-
}
|
|
8
1
|
export function validateClickEvent(event, target) {
|
|
9
2
|
// should this only work for left click?
|
|
10
3
|
if (event.button !== 0) {
|
|
@@ -37,19 +30,44 @@ export function validateClickEvent(event, target) {
|
|
|
37
30
|
}
|
|
38
31
|
return true;
|
|
39
32
|
}
|
|
33
|
+
let IS_CLIENT_NAVIGATION = false;
|
|
34
|
+
export async function navigate(href, options = { history: "push" }) {
|
|
35
|
+
if (!IS_CLIENT_NAVIGATION) {
|
|
36
|
+
window.location.href = href;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
saveScrollPosition(window.scrollX, window.scrollY);
|
|
40
|
+
const url = window.location.origin + href;
|
|
41
|
+
if (options.history === "push") {
|
|
42
|
+
window.history.pushState({ path: href }, "", url);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
window.history.replaceState({ path: href }, "", url);
|
|
46
|
+
}
|
|
47
|
+
// @ts-expect-error
|
|
48
|
+
await globalThis.__rsc_callServer();
|
|
49
|
+
const scrollToTop = options.info?.scrollToTop ?? true;
|
|
50
|
+
const scrollBehavior = options.info?.scrollBehavior ?? "instant";
|
|
51
|
+
if (scrollToTop && history.scrollRestoration === "auto") {
|
|
52
|
+
window.scrollTo({
|
|
53
|
+
top: 0,
|
|
54
|
+
left: 0,
|
|
55
|
+
behavior: scrollBehavior,
|
|
56
|
+
});
|
|
57
|
+
saveScrollPosition(0, 0);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function saveScrollPosition(x, y) {
|
|
61
|
+
window.history.replaceState({
|
|
62
|
+
...window.history.state,
|
|
63
|
+
scrollX: x,
|
|
64
|
+
scrollY: y,
|
|
65
|
+
}, "", window.location.href);
|
|
66
|
+
}
|
|
40
67
|
export function initClientNavigation(opts = {}) {
|
|
41
|
-
|
|
42
|
-
onNavigate: async function onNavigate() {
|
|
43
|
-
// @ts-expect-error
|
|
44
|
-
await globalThis.__rsc_callServer();
|
|
45
|
-
},
|
|
46
|
-
scrollToTop: true,
|
|
47
|
-
scrollBehavior: "instant",
|
|
48
|
-
...opts,
|
|
49
|
-
};
|
|
68
|
+
IS_CLIENT_NAVIGATION = true;
|
|
50
69
|
history.scrollRestoration = "auto";
|
|
51
70
|
document.addEventListener("click", async function handleClickEvent(event) {
|
|
52
|
-
// Prevent default navigation
|
|
53
71
|
if (!validateClickEvent(event, event.target)) {
|
|
54
72
|
return;
|
|
55
73
|
}
|
|
@@ -57,28 +75,18 @@ export function initClientNavigation(opts = {}) {
|
|
|
57
75
|
const el = event.target;
|
|
58
76
|
const a = el.closest("a");
|
|
59
77
|
const href = a?.getAttribute("href");
|
|
60
|
-
|
|
61
|
-
window.history.pushState({ path: href }, "", window.location.origin + href);
|
|
62
|
-
await options.onNavigate();
|
|
63
|
-
if (options.scrollToTop && history.scrollRestoration === "auto") {
|
|
64
|
-
window.scrollTo({
|
|
65
|
-
top: 0,
|
|
66
|
-
left: 0,
|
|
67
|
-
behavior: options.scrollBehavior,
|
|
68
|
-
});
|
|
69
|
-
saveScrollPosition(0, 0);
|
|
70
|
-
}
|
|
71
|
-
history.scrollRestoration = "auto";
|
|
78
|
+
navigate(href);
|
|
72
79
|
}, true);
|
|
73
80
|
window.addEventListener("popstate", async function handlePopState() {
|
|
74
|
-
|
|
75
|
-
await
|
|
81
|
+
// @ts-expect-error
|
|
82
|
+
await globalThis.__rsc_callServer();
|
|
76
83
|
});
|
|
77
84
|
// Return a handleResponse function for use with initClient
|
|
78
85
|
return {
|
|
79
86
|
handleResponse: function handleResponse(response) {
|
|
80
87
|
if (!response.ok) {
|
|
81
88
|
// Redirect to the current page (window.location) to show the error
|
|
89
|
+
// This means the page that produced the error is called twice.
|
|
82
90
|
window.location.href = window.location.href;
|
|
83
91
|
return false;
|
|
84
92
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import "./types/worker";
|
|
2
2
|
export * from "../error";
|
|
3
|
+
export * from "../lib/types";
|
|
3
4
|
export * from "../lib/utils";
|
|
4
5
|
export * from "../register/worker";
|
|
5
6
|
export * from "../render/renderToStream";
|
|
6
7
|
export * from "../render/renderToString";
|
|
7
8
|
export * from "../requestInfo/types";
|
|
9
|
+
export * from "../requestInfo/utils";
|
|
8
10
|
export * from "../requestInfo/worker";
|
|
9
11
|
export * from "../script";
|
|
10
12
|
export * from "../worker";
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import "./types/worker";
|
|
2
2
|
export * from "../error";
|
|
3
|
+
export * from "../lib/types";
|
|
3
4
|
export * from "../lib/utils";
|
|
4
5
|
export * from "../register/worker";
|
|
5
6
|
export * from "../render/renderToStream";
|
|
6
7
|
export * from "../render/renderToString";
|
|
7
8
|
export * from "../requestInfo/types";
|
|
9
|
+
export * from "../requestInfo/utils";
|
|
8
10
|
export * from "../requestInfo/worker";
|
|
9
11
|
export * from "../script";
|
|
10
12
|
export * from "../worker";
|
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
import { Kysely } from "kysely";
|
|
2
|
-
|
|
3
|
-
export declare function createDb<T>(durableObjectBinding: DurableObjectNamespace<SqliteDurableObject>, name?: string): Kysely<T>;
|
|
2
|
+
export declare function createDb<DatabaseType>(durableObjectBinding: DurableObjectNamespace<any>, name?: string): Kysely<DatabaseType>;
|
|
@@ -5,6 +5,10 @@ export function createDb(durableObjectBinding, name = "main") {
|
|
|
5
5
|
dialect: new DOWorkerDialect({
|
|
6
6
|
kyselyExecuteQuery: (...args) => {
|
|
7
7
|
const durableObjectId = durableObjectBinding.idFromName(name);
|
|
8
|
+
// context(justinvdm, 2 Oct 2025): First prize would be a type parameter
|
|
9
|
+
// for the durable object and then use it for `durableObjectBinding`'s
|
|
10
|
+
// type, rather than casting like this. However, that would prevent
|
|
11
|
+
// users from being able to do createDb<InferredDbType> then though.
|
|
8
12
|
const stub = durableObjectBinding.get(durableObjectId);
|
|
9
13
|
stub.initialize();
|
|
10
14
|
return stub.kyselyExecuteQuery(...args);
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
let manifest;
|
|
2
|
-
export
|
|
2
|
+
export async function getManifest() {
|
|
3
3
|
if (manifest) {
|
|
4
4
|
return manifest;
|
|
5
5
|
}
|
|
6
6
|
if (import.meta.env.VITE_IS_DEV_SERVER) {
|
|
7
|
+
// In dev, there's no manifest, so we can use an empty object.
|
|
7
8
|
manifest = {};
|
|
8
9
|
}
|
|
9
10
|
else {
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
// context(justinvdm, 2 Oct 2025): In production, the manifest is a
|
|
12
|
+
// placeholder string that will be replaced by the linker plugin with the
|
|
13
|
+
// actual manifest JSON object.
|
|
14
|
+
manifest = "__RWSDK_MANIFEST_PLACEHOLDER__";
|
|
12
15
|
}
|
|
13
16
|
return manifest;
|
|
14
|
-
}
|
|
17
|
+
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
// context(justinvdm, 14 Aug 2025): `react-server-dom-webpack` uses this globa ___webpack_require__ global,
|
|
2
|
+
// so we need to import our client entry point (which sets it), before importing
|
|
3
|
+
// prettier-ignore
|
|
2
4
|
import { initClient } from "../../client/client";
|
|
3
|
-
|
|
5
|
+
// prettier-ignore
|
|
6
|
+
import { createFromReadableStream } from "react-server-dom-webpack/client.browser";
|
|
7
|
+
// prettier-ignore
|
|
4
8
|
import { MESSAGE_TYPE } from "./shared";
|
|
9
|
+
// prettier-ignore
|
|
10
|
+
import { packMessage, unpackMessage, } from "./protocol";
|
|
5
11
|
const DEFAULT_KEY = "default";
|
|
6
12
|
export const initRealtimeClient = ({ key = DEFAULT_KEY, handleResponse, } = {}) => {
|
|
7
13
|
const transport = realtimeTransport({ key, handleResponse });
|