Skip to content

Commit

Permalink
fix: fix detecting WC in Windows (#548)
Browse files Browse the repository at this point in the history
* fix: fix detecting WC in Windows

Co-authored-by: Adrià Blancafort <[email protected]>

* fix: fix isInPathList detection with different separators

* chore: update GH Action yml

---------

Co-authored-by: Adrià Blancafort <[email protected]>
  • Loading branch information
aralroca and adriablancafort authored Oct 16, 2024
1 parent d3bb505 commit 3f036e1
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
- name: Install dependencies
run: bun install
- name: Build project
run: bun run build && bun run create-brisa:build
run: bun run build:all
- name: Publish commits
run: bunx pkg-pr-new publish './packages/*'
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
42 changes: 41 additions & 1 deletion packages/brisa/src/utils/get-web-components-list/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
type Mock,
} from 'bun:test';
import path from 'node:path';
import getWebComponentsList, { getWebComponentListFromFilePaths } from '.';
import getWebComponentsList, {
formatWCSelector,
getWebComponentListFromFilePaths,
} from '.';
import { getConstants } from '@/constants';
import { boldLog } from '@/utils/log/log-color';

Expand Down Expand Up @@ -298,6 +301,43 @@ describe('utils', () => {
});
});

describe('formatWCSelector', () => {
it('should return a kebab-case string (Mac/Linux)', () => {
const result = formatWCSelector('/custom-counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string (Windows)', () => {
const result = formatWCSelector('\\custom-counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string with folder separators (Mac/Linux)', () => {
const result = formatWCSelector('/custom/counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string with folder separators (Windows)', () => {
const result = formatWCSelector('\\custom\\counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string with folder separators (Mac/Linux)', () => {
const result = formatWCSelector('//custom//counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string with repeated folder separators (Windows)', () => {
const result = formatWCSelector('\\\\custom\\\\counter');
expect(result).toEqual('custom-counter');
});

it('should return a kebab-case string with mixing folder separators', () => {
const result = formatWCSelector('\\\\//custom\\\\//counter');
expect(result).toEqual('custom-counter');
});
});

describe('getWebComponentListFromFilePaths', () => {
it('should return a list of web components from file paths', () => {
const filePaths = [
Expand Down
13 changes: 8 additions & 5 deletions packages/brisa/src/utils/get-web-components-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import resolveImportSync from '@/utils/resolve-import-sync';
import type { WebComponentIntegrations } from '@/types';

const CONTEXT_PROVIDER = 'context-provider';
const separator = path.sep === '\\' ? '\\\\' : '/';
const ALLOWED_SEPARATORS_REGEX = new RegExp(`^${separator}(_)?`, 'g');
const SEPS = '\\\\|/';
const SEPS_REGEX = new RegExp(`(${SEPS})+`, 'g');
const PREFIX_REGEX = new RegExp(`^(${SEPS})+(_)?`, 'g');
const EXTENSION_REGEX = /\.[^/.]+$/;

export default async function getWebComponentsList(
Expand Down Expand Up @@ -101,9 +102,7 @@ export function routesEntriesToWebComponents(
key.includes(NATIVE_FOLDER),
)
.map(([key, path]) => {
const selector = key
.replace(ALLOWED_SEPARATORS_REGEX, '')
.replaceAll('/', '-');
const selector = formatWCSelector(key);

if (selector === CONTEXT_PROVIDER) {
logError({
Expand Down Expand Up @@ -143,3 +142,7 @@ export function routesEntriesToWebComponents(
}),
);
}

export function formatWCSelector(key: string): string {
return key.replace(PREFIX_REGEX, '').replace(SEPS_REGEX, '-').toLowerCase();
}
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 3f036e1

Please sign in to comment.