Skip to content

fix: prevent Vitest from hanging #13373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rare-berries-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-cloudflare': patch
---

fix: prevent Vitest from hanging, which was not fully addressed in [#12830](https://github.com/sveltejs/kit/pull/12830)
13 changes: 7 additions & 6 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,17 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {})
emulate() {
// we want to invoke `getPlatformProxy` only once, but await it only when it is accessed.
// If we would await it here, it would hang indefinitely because the platform proxy only resolves once a request happens
const getting_platform = (async () => {
const get_emulated = async () => {
const proxy = await getPlatformProxy(platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
caches: proxy.caches,
cf: proxy.cf
});

/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
Expand All @@ -173,12 +171,15 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {})
});
}
return { platform, prerender_platform };
})();
};

/** @type {{ platform: App.Platform, prerender_platform: App.Platform }} */
let emulated;

return {
platform: async ({ prerender }) => {
const { platform, prerender_platform } = await getting_platform;
return prerender ? prerender_platform : platform;
emulated ??= await get_emulated();
return prerender ? emulated.prerender_platform : emulated.platform;
}
};
}
Expand Down
13 changes: 7 additions & 6 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,17 @@ export default function (options = {}) {
emulate() {
// we want to invoke `getPlatformProxy` only once, but await it only when it is accessed.
// If we would await it here, it would hang indefinitely because the platform proxy only resolves once a request happens
const getting_platform = (async () => {
const get_emulated = async () => {
const proxy = await getPlatformProxy(options.platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
caches: proxy.caches,
cf: proxy.cf
});

/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
Expand All @@ -91,12 +89,15 @@ export default function (options = {}) {
});
}
return { platform, prerender_platform };
})();
};

/** @type {{ platform: App.Platform, prerender_platform: App.Platform }} */
let emulated;

return {
platform: async ({ prerender }) => {
const { platform, prerender_platform } = await getting_platform;
return prerender ? prerender_platform : platform;
emulated ??= await get_emulated();
return prerender ? emulated.prerender_platform : emulated.platform;
}
};
}
Expand Down
Loading