From 1ae7b77d162a16d26c2a941c822212cd6b6e7761 Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Tue, 27 Aug 2024 18:39:34 +0200 Subject: [PATCH] fix: ignore openInEditor in dev when runtimes are different than Bun --- .../src/cli/serve/serve-options.test.tsx | 27 +++++++++++++++++++ .../brisa/src/cli/serve/serve-options.tsx | 8 ++++++ packages/brisa/src/constants.ts | 1 + packages/brisa/src/types/index.d.ts | 1 + .../get-client-code-in-page/index.test.ts | 2 +- 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/brisa/src/cli/serve/serve-options.test.tsx b/packages/brisa/src/cli/serve/serve-options.test.tsx index 53cf71c95..eefd9e1c7 100644 --- a/packages/brisa/src/cli/serve/serve-options.test.tsx +++ b/packages/brisa/src/cli/serve/serve-options.test.tsx @@ -1579,6 +1579,31 @@ describe.each(BASE_PATHS)('CLI: serve %s', (basePath) => { line: 1, column: 1, }); + mockOpenInEditor.mockRestore(); + }); + + it('should not call Bun.openInEditor in Node.js and return 404', async () => { + globalThis.mockConstants = { + ...globalThis.mockConstants, + IS_PRODUCTION: false, + IS_DEVELOPMENT: true, + JS_RUNTIME: 'node', + }; + const mockOpenInEditor = spyOn(Bun, 'openInEditor').mockImplementation( + () => {}, + ); + const response = await testRequest( + new Request( + `http://localhost:1234/__brisa_dev_file__?file=${encodeURIComponent( + 'src/pages/somepage.tsx', + )}&line=1&column=1`, + { method: 'POST' }, + ), + ); + + expect(response.status).toBe(404); + expect(mockOpenInEditor).not.toHaveBeenCalled(); + mockOpenInEditor.mockRestore(); }); it('should open the editor calling /__brisa_dev_file__ with internal brisa file from build with line and column', async () => { @@ -1612,6 +1637,7 @@ describe.each(BASE_PATHS)('CLI: serve %s', (basePath) => { line: 1, column: 1, }); + mockOpenInEditor.mockRestore(); }); it('should return 404 trying to open the editor calling /__brisa_dev_file__ with file, line and column with method GET', async () => { @@ -1634,6 +1660,7 @@ describe.each(BASE_PATHS)('CLI: serve %s', (basePath) => { expect(response.status).toBe(404); expect(mockOpenInEditor).not.toHaveBeenCalled(); + mockOpenInEditor.mockRestore(); }); it('should work declarative shadow DOM on server actions when is form call without RPC', async () => { diff --git a/packages/brisa/src/cli/serve/serve-options.tsx b/packages/brisa/src/cli/serve/serve-options.tsx index 11adfb691..d4d5ac76d 100644 --- a/packages/brisa/src/cli/serve/serve-options.tsx +++ b/packages/brisa/src/cli/serve/serve-options.tsx @@ -38,6 +38,7 @@ export async function getServeOptions() { PAGES_DIR, ASSETS_DIR, CONFIG, + JS_RUNTIME, LOG_PREFIX, HEADERS: { CACHE_CONTROL }, } = getConstants(); @@ -100,6 +101,13 @@ export async function getServeOptions() { url.pathname === '/__brisa_dev_file__' && req.method === 'POST' ) { + if (JS_RUNTIME !== 'bun') { + // Note: This only happens on development, and we use + // the Bun runtime in development. Other runtimes like + // Node.js for now are only supported in production. + return new Response(null, { status: 404 }); + } + let file = url.searchParams.get('file'); const line = url.searchParams.get('line'); const column = url.searchParams.get('column'); diff --git a/packages/brisa/src/constants.ts b/packages/brisa/src/constants.ts index be8ff117b..9f0a8d6f5 100644 --- a/packages/brisa/src/constants.ts +++ b/packages/brisa/src/constants.ts @@ -111,6 +111,7 @@ const defaultConfig = { }; const constants = { + JS_RUNTIME: typeof Bun !== 'undefined' ? 'bun' : 'node', PAGE_404, PAGE_500, VERSION: version, diff --git a/packages/brisa/src/types/index.d.ts b/packages/brisa/src/types/index.d.ts index 5db8d8c59..3949ee1d3 100644 --- a/packages/brisa/src/types/index.d.ts +++ b/packages/brisa/src/types/index.d.ts @@ -31,6 +31,7 @@ declare module 'bun' { * Internal types used by Brisa and output adapters. */ export type BrisaConstants = { + JS_RUNTIME: 'bun' | 'node'; PAGE_404: string; PAGE_500: string; VERSION: string; diff --git a/packages/brisa/src/utils/get-client-code-in-page/index.test.ts b/packages/brisa/src/utils/get-client-code-in-page/index.test.ts index 5e7dc6615..dd13eee52 100644 --- a/packages/brisa/src/utils/get-client-code-in-page/index.test.ts +++ b/packages/brisa/src/utils/get-client-code-in-page/index.test.ts @@ -22,7 +22,7 @@ const i18nCode = 3072; const brisaSize = 5959; // TODO: Reduce this size :/ const webComponents = 792; const unsuspenseSize = 217; -const rpcSize = 2468; // TODO: Reduce this size +const rpcSize = 2467; // TODO: Reduce this size const lazyRPCSize = 4187; // TODO: Reduce this size // lazyRPC is loaded after user interaction (action, link), // so it's not included in the initial size