From d1d7bbb52618d617d13b47dbbfa1f8aa1319cbed Mon Sep 17 00:00:00 2001 From: Victor Didenko Date: Tue, 6 Jun 2023 23:07:56 +0300 Subject: [PATCH] Loosen filename test --- package.json | 2 +- src/internal/source-checkers.ts | 17 ++++++++++------- test/internal/source-checkers.spec.ts | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 7a8c65c..f76fdb2 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ { "path": "pkg/dist-node/index.js", "webpack": false, - "limit": "4244 B" + "limit": "4293 B" } ], "@pika/pack": { diff --git a/src/internal/source-checkers.ts b/src/internal/source-checkers.ts index b4e9f70..c06f591 100644 --- a/src/internal/source-checkers.ts +++ b/src/internal/source-checkers.ts @@ -81,14 +81,17 @@ export const isIterable = ( /** * Check, if given argument is simple string, and presumably is is just file name - * I hope no one will wants to use filename longer than 50 symbols :) */ -const fileNameRE = /^[\p{L}\w\s\(\).,-]+\.[A-Za-z0-9]+$/u // tslint:disable-line no-empty-character-class -const MIN_FILE_NAME_LENGTH = 3 -const MAX_FILE_NAME_LENGTH = 50 +const filenameRE = /.+\..+/ +const filenameReservedRE = /[<>:"/\\|?*\u0000-\u001F]/g +const windowsReservedNameRE = /^(con|prn|aux|nul|com\d|lpt\d)$/i +const MAX_FILE_NAME_LENGTH = 255 export const isFileName = (x: Source | undefined | null) => isString(x) && - !x.startsWith('file:') && // in ideal world there should be `!isFileUri(x)`, but TypeScript sucks here - x.length >= MIN_FILE_NAME_LENGTH && x.length <= MAX_FILE_NAME_LENGTH && - fileNameRE.test(x) + x !== '.' && + x !== '..' && + !x.startsWith('file:') && // in ideal world there should be `!isFileUri(x)`, but TypeScript sucks here + !filenameReservedRE.test(x) && + !windowsReservedNameRE.test(x) && + filenameRE.test(x) diff --git a/test/internal/source-checkers.spec.ts b/test/internal/source-checkers.spec.ts index bb2fde2..c69302d 100644 --- a/test/internal/source-checkers.spec.ts +++ b/test/internal/source-checkers.spec.ts @@ -53,7 +53,8 @@ test('Test `isFileName` function', function () { expect(isFileName('Screenshot 2021-12-24 at 09.16.20.png')).toBe(true) // <- expect(isFileName('.test.png')).toBe(true) // <- expect(isFileName('.png')).toBe(false) - expect(isFileName('🙀.png')).toBe(false) + expect(isFileName('🙀.png')).toBe(true) // <- + expect(isFileName('ces La esencia del cristianismo Dios es persona (jóvenes).docx')).toBe(true) // <- expect(isFileName(url)).toBe(false) expect(isFileName(array)).toBe(false) expect(isFileName(map)).toBe(false)