33 lines
4.7 KiB
JavaScript
33 lines
4.7 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const path_1 = __importDefault(require("path"));
|
||
|
const os_family_1 = require("os-family");
|
||
|
const sanitize_filename_1 = __importDefault(require("sanitize-filename"));
|
||
|
const SAFE_CHAR = '_';
|
||
|
const ALLOWED_CHARS_LIST = [path_1.default.win32.sep, path_1.default.posix.sep, '.', '..'];
|
||
|
function correctForbiddenCharsList(forbiddenCharsList, filePath) {
|
||
|
const isWinAbsolutePath = os_family_1.win && path_1.default.isAbsolute(filePath);
|
||
|
const hasDriveSeparatorInList = forbiddenCharsList.length && forbiddenCharsList[0].chars === ':' && forbiddenCharsList[0].index === 1;
|
||
|
if (isWinAbsolutePath && hasDriveSeparatorInList)
|
||
|
forbiddenCharsList.shift();
|
||
|
}
|
||
|
function addForbiddenCharsToList(forbiddenCharsList, forbiddenCharsInfo) {
|
||
|
const { chars } = forbiddenCharsInfo;
|
||
|
if (!ALLOWED_CHARS_LIST.includes(chars))
|
||
|
forbiddenCharsList.push(forbiddenCharsInfo);
|
||
|
return SAFE_CHAR.repeat(chars.length);
|
||
|
}
|
||
|
function default_1(filePath) {
|
||
|
const forbiddenCharsList = [];
|
||
|
(0, sanitize_filename_1.default)(filePath, {
|
||
|
replacement: (chars, index) => addForbiddenCharsToList(forbiddenCharsList, { chars, index }),
|
||
|
});
|
||
|
correctForbiddenCharsList(forbiddenCharsList, filePath);
|
||
|
return forbiddenCharsList;
|
||
|
}
|
||
|
exports.default = default_1;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hlY2stZmlsZS1wYXRoLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0a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