diff --git a/packages/brisa/src/utils/compile-files/index.test.ts b/packages/brisa/src/utils/compile-files/index.test.ts index 1d8503d3e..e0bcf2d55 100644 --- a/packages/brisa/src/utils/compile-files/index.test.ts +++ b/packages/brisa/src/utils/compile-files/index.test.ts @@ -278,7 +278,7 @@ describe('utils', () => { ${info} ${info}Route | JS server | JS client (gz) ${info}---------------------------------------------------------------- - ${info}ƒ /middleware | 828 B | + ${info}ƒ /middleware | 818 B | ${info}Δ /layout | 604 B | ${info}Ω /i18n | 162 B | ${info}Ψ /websocket | 207 B | @@ -290,7 +290,7 @@ describe('utils', () => { ${info}λ /pages/user/[username] | 437 B | ${greenLog('0 B')} ${info}λ /pages/_404 | 822 B | ${greenLog('5 kB')} ${info}λ /pages/_500 | 828 B | ${greenLog('5 kB')} - ${info}λ /api/example | 283 B | + ${info}λ /api/example | 281 B | ${info} ${info}λ Server entry-points ${info}Δ Layout @@ -536,7 +536,7 @@ describe('utils', () => { ${info} ${info}Route | JS server | JS client (gz) ${info}---------------------------------------------- - ${info}λ /pages/index | 444 B | ${greenLog('187 B')} + ${info}λ /pages/index | 444 B | ${greenLog('186 B')} ${info}Δ /layout | 790 B | ${info} ${info}λ Server entry-points 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 f25912906..c381ebf39 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 @@ -19,11 +19,11 @@ const pageWebComponents = { }; const i18nCode = 3072; -const brisaSize = 5820; // TODO: Reduce this size :/ -const webComponents = 1145; -const unsuspenseSize = 217; -const rpcSize = 2468; // TODO: Reduce this size -const lazyRPCSize = 4171; // TODO: Reduce this size +const brisaSize = 5720; // TODO: Reduce this size :/ +const webComponents = 1107; +const unsuspenseSize = 213; +const rpcSize = 2436; // TODO: Reduce this size +const lazyRPCSize = 4139; // TODO: Reduce this size // lazyRPC is loaded after user interaction (action, link), // so it's not included in the initial size const initialSize = unsuspenseSize + rpcSize; diff --git a/packages/brisa/src/utils/is-in-path-list/index.test.ts b/packages/brisa/src/utils/is-in-path-list/index.test.ts new file mode 100644 index 000000000..9ce74f165 --- /dev/null +++ b/packages/brisa/src/utils/is-in-path-list/index.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, it, beforeEach, afterEach, spyOn } from 'bun:test'; +import fs from 'node:fs'; +import isInPathList from '.'; + +const listPath = 'foo.txt'; +let mockExistsSync: ReturnType; +let mockReadFileSync: ReturnType; + +describe('utils / isInPathList', () => { + beforeEach(() => { + mockExistsSync = spyOn(fs, 'existsSync').mockReturnValue(true); + }); + + afterEach(() => { + mockExistsSync.mockRestore(); + }); + + it('should return false when the list is empty', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue(''); + const result = await isInPathList(listPath, { + route: { filePath: '/foo' }, + } as any); + expect(result).toBeFalse(); + }); + + it('should return true when the path is in the list', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); + const result = await isInPathList(listPath, { + route: { filePath: '/foo' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return false when the path is not in the list', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); + const result = await isInPathList(listPath, { + route: { filePath: '/baz' }, + } as any); + expect(result).toBeFalse(); + }); + + it('should return true when the path is in the list with different separators', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( + '\\foo\n\\bar', + ); + const result = await isInPathList(listPath, { + route: { filePath: '/foo' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return true when the path is in the list with multi different separators', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( + '\\foo\\bar\n\\baz', + ); + const result = await isInPathList(listPath, { + route: { filePath: '/foo/bar' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return true when the path is in the list with multi duplicated different separators', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( + '\\\\foo\\\\bar\n\\\\baz', + ); + const result = await isInPathList(listPath, { + route: { filePath: '/foo/bar' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return true when the path is in the list with different separators in other way', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); + const result = await isInPathList(listPath, { + route: { filePath: '\\foo' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return true when the path is in the list with different separators in other way', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( + '/foo/bar\n/bar', + ); + const result = await isInPathList(listPath, { + route: { filePath: '\\foo\\bar' }, + } as any); + expect(result).toBeTrue(); + }); + + it('should return false when the list is empty', async () => { + mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue(''); + const result = await isInPathList(listPath, { + route: { filePath: '/baz' }, + } as any); + expect(result).toBeFalse(); + }); +}); diff --git a/packages/brisa/src/utils/is-in-path-list/index.ts b/packages/brisa/src/utils/is-in-path-list/index.ts new file mode 100644 index 000000000..0556b4a5e --- /dev/null +++ b/packages/brisa/src/utils/is-in-path-list/index.ts @@ -0,0 +1,29 @@ +import fs from 'node:fs'; +import { getConstants } from '@/constants'; +import type { RequestContext } from '@/types'; + +const SEPS_REGEX = /(\\|\/)+/g; +const TEMP_SEP = '|'; + +export default async function isInPathList( + pathname: string, + request: RequestContext, +) { + const { BUILD_DIR } = getConstants(); + const listText = fs.existsSync(pathname) + ? fs.readFileSync(pathname, 'utf-8') + : ''; + + if (!listText) return false; + + const route = (request.route?.filePath ?? '') + .replace(BUILD_DIR, '') + .replace(SEPS_REGEX, TEMP_SEP); + const list = listText.split('\n'); + + for (let i = 0; i < list.length; i += 1) { + if (list[i].replace(SEPS_REGEX, TEMP_SEP) === route) return true; + } + + return false; +} diff --git a/packages/brisa/src/utils/render-to-readable-stream/index.ts b/packages/brisa/src/utils/render-to-readable-stream/index.ts index f5a7ee954..59b499d55 100644 --- a/packages/brisa/src/utils/render-to-readable-stream/index.ts +++ b/packages/brisa/src/utils/render-to-readable-stream/index.ts @@ -26,6 +26,7 @@ import { RenderInitiator } from '@/public-constants'; import get404ClientScript from '@/utils/not-found/client-script'; import escapeHTML from '@/utils/escape-html'; import { isArrawOfJSXContent } from '@/jsx-runtime'; +import isInPathList from '@/utils/is-in-path-list'; type ProviderType = ReturnType; @@ -659,19 +660,6 @@ function getValueOfComponent( }); } -async function isInPathList(pathname: string, request: RequestContext) { - const { BUILD_DIR } = getConstants(); - const listText = fs.existsSync(pathname) - ? fs.readFileSync(pathname, 'utf-8') - : ''; - - if (!listText) return false; - - const route = (request.route?.filePath ?? '').replace(BUILD_DIR, ''); - - return new Set(listText.split('\n')).has(route); -} - function enqueueCSSFiles(controller: Controller, suspenseId?: number) { const { CONFIG, CSS_FILES } = getConstants(); const basePath = (CONFIG.basePath || '').replace(/\/$/, '');