Skip to content
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

fix: prevent Vitest from hanging #13373

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
benmccann marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 9 additions & 6 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,19 @@ 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
});

eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
Expand All @@ -173,12 +173,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
15 changes: 9 additions & 6 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ 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
});

eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
Expand All @@ -91,12 +91,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