Skip to content

Commit

Permalink
fix: fix isInPathList detection with different separators
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 16, 2024
1 parent d6b7096 commit a259cee
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/brisa/src/utils/compile-files/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/brisa/src/utils/get-client-code-in-page/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
97 changes: 97 additions & 0 deletions packages/brisa/src/utils/is-in-path-list/index.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof spyOn>;
let mockReadFileSync: ReturnType<typeof spyOn>;

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();
});
});
29 changes: 29 additions & 0 deletions packages/brisa/src/utils/is-in-path-list/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 1 addition & 13 deletions packages/brisa/src/utils/render-to-readable-stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof contextProvider>;

Expand Down Expand Up @@ -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(/\/$/, '');
Expand Down

0 comments on commit a259cee

Please sign in to comment.