Skip to content

Commit 043cdac

Browse files
authored
fix: prevent vitest from hanging (#12830)
fixes #12305 closes #12306 This is an alternative to #12306 . Instead of creating the emulator every time the Vite dev and preview middlewares are invoked, we await Cloudflare's platform proxy only when the adapter emulated platform is accessed, rather than much earlier when adapter.adapt is invoked.
1 parent d4bcfcc commit 043cdac

File tree

3 files changed

+55
-38
lines changed

3 files changed

+55
-38
lines changed

.changeset/six-goats-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: prevent vitest from hanging

packages/adapter-cloudflare-workers/index.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,35 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {})
149149
builder.writePrerendered(bucket_dir);
150150
},
151151

152-
async emulate() {
153-
const proxy = await getPlatformProxy(platformProxy);
154-
const platform = /** @type {App.Platform} */ ({
155-
env: proxy.env,
156-
context: proxy.ctx,
157-
caches: proxy.caches,
158-
cf: proxy.cf
159-
});
152+
emulate() {
153+
// we want to invoke `getPlatformProxy` only once, but await it only when it is accessed.
154+
// If we would await it here, it would hang indefinitely because the platform proxy only resolves once a request happens
155+
const getting_platform = (async () => {
156+
const proxy = await getPlatformProxy(platformProxy);
157+
const platform = /** @type {App.Platform} */ ({
158+
env: proxy.env,
159+
context: proxy.ctx,
160+
caches: proxy.caches,
161+
cf: proxy.cf
162+
});
160163

161-
/** @type {Record<string, any>} */
162-
const env = {};
163-
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));
164+
/** @type {Record<string, any>} */
165+
const env = {};
166+
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));
164167

165-
for (const key in proxy.env) {
166-
Object.defineProperty(env, key, {
167-
get: () => {
168-
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
169-
}
170-
});
171-
}
168+
for (const key in proxy.env) {
169+
Object.defineProperty(env, key, {
170+
get: () => {
171+
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
172+
}
173+
});
174+
}
175+
return { platform, prerender_platform };
176+
})();
172177

173178
return {
174-
platform: ({ prerender }) => {
179+
platform: async ({ prerender }) => {
180+
const { platform, prerender_platform } = await getting_platform;
175181
return prerender ? prerender_platform : platform;
176182
}
177183
};

packages/adapter-cloudflare/index.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,35 @@ export default function (options = {}) {
6767
}
6868
});
6969
},
70-
async emulate() {
71-
const proxy = await getPlatformProxy(options.platformProxy);
72-
const platform = /** @type {App.Platform} */ ({
73-
env: proxy.env,
74-
context: proxy.ctx,
75-
caches: proxy.caches,
76-
cf: proxy.cf
77-
});
70+
emulate() {
71+
// we want to invoke `getPlatformProxy` only once, but await it only when it is accessed.
72+
// If we would await it here, it would hang indefinitely because the platform proxy only resolves once a request happens
73+
const getting_platform = (async () => {
74+
const proxy = await getPlatformProxy(options.platformProxy);
75+
const platform = /** @type {App.Platform} */ ({
76+
env: proxy.env,
77+
context: proxy.ctx,
78+
caches: proxy.caches,
79+
cf: proxy.cf
80+
});
7881

79-
/** @type {Record<string, any>} */
80-
const env = {};
81-
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));
82+
/** @type {Record<string, any>} */
83+
const env = {};
84+
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));
8285

83-
for (const key in proxy.env) {
84-
Object.defineProperty(env, key, {
85-
get: () => {
86-
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
87-
}
88-
});
89-
}
86+
for (const key in proxy.env) {
87+
Object.defineProperty(env, key, {
88+
get: () => {
89+
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
90+
}
91+
});
92+
}
93+
return { platform, prerender_platform };
94+
})();
9095

9196
return {
92-
platform: ({ prerender }) => {
97+
platform: async ({ prerender }) => {
98+
const { platform, prerender_platform } = await getting_platform;
9399
return prerender ? prerender_platform : platform;
94100
}
95101
};

0 commit comments

Comments
 (0)