Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): sync latest sanitizeFileName logic from rollup #1614

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/utils/src/module/sanitizeFileName.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// https://github.com/rollup/rollup/blob/69ff4181e701a0fe0026d0ba147f31bc86beffa8/src/utils/sanitizeFileName.ts
// https://datatracker.ietf.org/doc/html/rfc2396
// https://github.com/rollup/rollup/blob/dd1a6bee7b1b436113594d3de1b0fff37ea96eb6/src/utils/sanitizeFileName.ts

// eslint-disable-next-line no-control-regex
const INVALID_CHAR_REGEX = /[\x00-\x1F\x7F<>*#"{}|^[\]`;?:&=+$,]/g
const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$%&*+,:;<=>?[\]^`{|}\u007F]/g
const DRIVE_LETTER_REGEX = /^[a-z]:/i

export const sanitizeFileName = (name: string): string => {
const driveLetter = DRIVE_LETTER_REGEX.exec(name)?.[0] || ''

// A `:` is only allowed as part of a windows drive letter (ex: C:\foo)
// Otherwise, avoid them because they can refer to NTFS alternate data streams.
return (
driveLetter +
name
.substring(driveLetter.length)
.slice(driveLetter.length)
.replace(INVALID_CHAR_REGEX, '_')
.replace(/^_+/, '')
)
Expand Down
Loading