diff --git a/.changeset/rare-berries-retire.md b/.changeset/rare-berries-retire.md new file mode 100644 index 000000000000..0017a576bbfa --- /dev/null +++ b/.changeset/rare-berries-retire.md @@ -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) diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index be1d98245cbd..5da3fe275022 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -152,7 +152,7 @@ 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, @@ -160,11 +160,9 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {}) caches: proxy.caches, cf: proxy.cf }); - /** @type {Record} */ const env = {}; const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env })); - for (const key in proxy.env) { Object.defineProperty(env, key, { get: () => { @@ -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; } }; } diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index fa47122abe29..e00eb9889929 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -70,7 +70,7 @@ 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, @@ -78,11 +78,9 @@ export default function (options = {}) { caches: proxy.caches, cf: proxy.cf }); - /** @type {Record} */ const env = {}; const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env })); - for (const key in proxy.env) { Object.defineProperty(env, key, { get: () => { @@ -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; } }; }