168
const WINDOWS_FORBIDDEN_NAMES = /^(con|prn|aux|clock\$|nul|lpt[0-9]|com[0-9])(\.(.*?))?$/i;
169
export function isValidBasename(name: string | null | undefined, isWindowsOS: boolean = isWindows): boolean {
170
>
const invalidFileChars = isWindowsOS ? WINDOWS_INVALID_FILE_CHARS : UNIX_INVALID_FILE_CHARS;
extpath.ts
171
>
172
>
if (!name || name.length === 0 || /^\s+$/.test(name)) {
173
>
return false; // require a name that is not just whitespace
174
>
}
175
>
176
>
invalidFileChars.lastIndex = 0; // the holy grail of software development
177
>
if (invalidFileChars.test(name)) {
178
>
return false; // check for certain invalid file characters
179
>
}
180
>
181
>
if (isWindowsOS && WINDOWS_FORBIDDEN_NAMES.test(name)) {
182
return false; // check for certain invalid file names
183
}
185
>
if (name === '.' || name === '..') {
186
return false; // check for reserved values
187
}
189
>
if (isWindowsOS && name[name.length - 1] === '.') {
190
return false; // Windows: file cannot end with a "."
191
}
193
>
if (isWindowsOS && name.length !== name.trim().length) {
194
return false; // Windows: file cannot end with a whitespace
195
}
197
>
if (name.length > 255) {
198
return false; // most file systems do not allow files > 255 length
199
}
201
>
return true;
202
>
}
203
204
/**