From 5bb15b13fbb47e2abc8572d87d71cd3a535431b8 Mon Sep 17 00:00:00 2001 From: komapotter <36rafts@gmail.com> Date: Fri, 30 Aug 2024 16:05:34 +0900 Subject: [PATCH 1/3] fix(dev-server): Add null check and error handling for proxy disposal in cloudflareAdapter --- packages/dev-server/src/adapter/cloudflare.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/dev-server/src/adapter/cloudflare.ts b/packages/dev-server/src/adapter/cloudflare.ts index 0e5872f..3f98e7b 100644 --- a/packages/dev-server/src/adapter/cloudflare.ts +++ b/packages/dev-server/src/adapter/cloudflare.ts @@ -8,7 +8,7 @@ type CloudflareAdapterOptions = { proxy: Parameters[0] } -let proxy: Awaited>> +let proxy: Awaited>> | undefined = undefined export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise = async ( options @@ -16,7 +16,6 @@ export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise< proxy ??= await getPlatformProxy(options?.proxy) // Cache API provided by `getPlatformProxy` currently do nothing. Object.assign(globalThis, { caches: proxy.caches }) - if (typeof globalThis.navigator === 'undefined') { // @ts-expect-error not typed well globalThis.navigator = { @@ -31,7 +30,10 @@ export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise< Object.defineProperty(Request.prototype, 'cf', { get: function () { - return proxy.cf + if (proxy !== undefined) { + return proxy.cf + } + throw new Error('Proxy is not initialized') }, configurable: true, enumerable: true, @@ -41,12 +43,16 @@ export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise< env: proxy.env, executionContext: proxy.ctx, onServerClose: async () => { - try { - await proxy.dispose() - } catch { - /** - * It throws an error if server is not running. - */ + if (proxy !== undefined) { + try { + await proxy.dispose() + } catch (error) { + /** + * It throws an error if server is not running. + */ + } finally { + proxy = undefined as Awaited>> | undefined + } } }, } From 4c6888a6cd3304f6767a39bc3e71bd17575afc47 Mon Sep 17 00:00:00 2001 From: komapotter <36rafts@gmail.com> Date: Fri, 30 Aug 2024 17:01:04 +0900 Subject: [PATCH 2/3] add changeset --- .changeset/three-pumas-cough.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/three-pumas-cough.md diff --git a/.changeset/three-pumas-cough.md b/.changeset/three-pumas-cough.md new file mode 100644 index 0000000..f7ffb5c --- /dev/null +++ b/.changeset/three-pumas-cough.md @@ -0,0 +1,5 @@ +--- +'@hono/vite-dev-server': patch +--- + +fix(dev-server): Add null check and error handling for proxy disposal in cloudflareAdapter From e72a67e555beb9029ac781b170828a7ae4a59ddc Mon Sep 17 00:00:00 2001 From: komapotter <36rafts@gmail.com> Date: Fri, 30 Aug 2024 17:56:00 +0900 Subject: [PATCH 3/3] fix: Correct proxy type definition --- packages/dev-server/src/adapter/cloudflare.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev-server/src/adapter/cloudflare.ts b/packages/dev-server/src/adapter/cloudflare.ts index 3f98e7b..02f0ce3 100644 --- a/packages/dev-server/src/adapter/cloudflare.ts +++ b/packages/dev-server/src/adapter/cloudflare.ts @@ -51,7 +51,7 @@ export const cloudflareAdapter: (options?: CloudflareAdapterOptions) => Promise< * It throws an error if server is not running. */ } finally { - proxy = undefined as Awaited>> | undefined + proxy = undefined } } },