@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
449 lines (448 loc) • 13.9 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var path_exports = {};
__export(path_exports, {
isAbsolute: () => isAbsolute,
isNodeModules: () => isNodeModules,
isPath: () => isPath,
isRelative: () => isRelative,
normalizePath: () => normalizePath,
pathLikeToString: () => pathLikeToString,
relativeResolve: () => relativeResolve,
splitPath: () => splitPath,
trimLeadingDotSlash: () => trimLeadingDotSlash
});
module.exports = __toCommonJS(path_exports);
var import_platform = require("#constants/platform");
var import_strings = require("./strings");
const CHAR_BACKWARD_SLASH = 92;
const CHAR_COLON = 58;
const CHAR_FORWARD_SLASH = 47;
const CHAR_LOWERCASE_A = 97;
const CHAR_LOWERCASE_Z = 122;
const CHAR_UPPERCASE_A = 65;
const CHAR_UPPERCASE_Z = 90;
const slashRegExp = /[/\\]/;
const nodeModulesPathRegExp = /(?:^|[/\\])node_modules(?:[/\\]|$)/;
// @__NO_SIDE_EFFECTS__
function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}
// @__NO_SIDE_EFFECTS__
function isWindowsDeviceRoot(code) {
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z || code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
}
let _buffer;
// @__NO_SIDE_EFFECTS__
function getBuffer() {
if (_buffer === void 0) {
_buffer = require("node:buffer");
}
return _buffer;
}
let _url;
// @__NO_SIDE_EFFECTS__
function getUrl() {
if (_url === void 0) {
_url = require("node:url");
}
return _url;
}
// @__NO_SIDE_EFFECTS__
function isNodeModules(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
return nodeModulesPathRegExp.test(filepath);
}
// @__NO_SIDE_EFFECTS__
function isAbsolute(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
const { length } = filepath;
if (length === 0) {
return false;
}
const code = filepath.charCodeAt(0);
if (code === CHAR_FORWARD_SLASH) {
return true;
}
if (code === CHAR_BACKWARD_SLASH) {
return true;
}
if (import_platform.WIN32 && length > 2) {
if (/* @__PURE__ */ isWindowsDeviceRoot(code) && filepath.charCodeAt(1) === CHAR_COLON && /* @__PURE__ */ isPathSeparator(filepath.charCodeAt(2))) {
return true;
}
}
return false;
}
// @__NO_SIDE_EFFECTS__
function isPath(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
if (typeof filepath !== "string" || filepath.length === 0) {
return false;
}
if (/^[a-z][a-z0-9+.-]+:/i.test(filepath)) {
return false;
}
if (filepath === "." || filepath === "..") {
return true;
}
if (/* @__PURE__ */ isAbsolute(filepath)) {
return true;
}
if (filepath.includes("/") || filepath.includes("\\")) {
if (filepath.startsWith("@") && !filepath.startsWith("@/")) {
const parts = filepath.split("/");
if (parts.length <= 2 && !parts[1]?.includes("\\")) {
return false;
}
}
return true;
}
return false;
}
// @__NO_SIDE_EFFECTS__
function isRelative(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
if (typeof filepath !== "string") {
return false;
}
if (filepath.length === 0) {
return true;
}
return !/* @__PURE__ */ isAbsolute(filepath);
}
// @__NO_SIDE_EFFECTS__
function normalizePath(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
const { length } = filepath;
if (length === 0) {
return ".";
}
if (length < 2) {
return length === 1 && filepath.charCodeAt(0) === 92 ? "/" : filepath;
}
let code = 0;
let start = 0;
let prefix = "";
if (length > 4 && filepath.charCodeAt(3) === 92) {
const code2 = filepath.charCodeAt(2);
if ((code2 === 63 || code2 === 46) && filepath.charCodeAt(0) === 92 && filepath.charCodeAt(1) === 92) {
start = 2;
prefix = "//";
}
}
if (start === 0) {
if (length > 2 && (filepath.charCodeAt(0) === 92 && filepath.charCodeAt(1) === 92 && filepath.charCodeAt(2) !== 92 || filepath.charCodeAt(0) === 47 && filepath.charCodeAt(1) === 47 && filepath.charCodeAt(2) !== 47)) {
let firstSegmentEnd = -1;
let hasSecondSegment = false;
let i = 2;
while (i < length && (filepath.charCodeAt(i) === 47 || filepath.charCodeAt(i) === 92)) {
i++;
}
while (i < length) {
const char = filepath.charCodeAt(i);
if (char === 47 || char === 92) {
firstSegmentEnd = i;
break;
}
i++;
}
if (firstSegmentEnd > 2) {
i = firstSegmentEnd;
while (i < length && (filepath.charCodeAt(i) === 47 || filepath.charCodeAt(i) === 92)) {
i++;
}
if (i < length) {
hasSecondSegment = true;
}
}
if (firstSegmentEnd > 2 && hasSecondSegment) {
start = 2;
prefix = "//";
} else {
code = filepath.charCodeAt(start);
while (code === 47 || code === 92) {
start += 1;
code = filepath.charCodeAt(start);
}
if (start) {
prefix = "/";
}
}
} else {
code = filepath.charCodeAt(start);
while (code === 47 || code === 92) {
start += 1;
code = filepath.charCodeAt(start);
}
if (start) {
prefix = "/";
}
}
}
let nextIndex = (0, import_strings.search)(filepath, slashRegExp, { fromIndex: start });
if (nextIndex === -1) {
const segment = filepath.slice(start);
if (segment === "." || segment.length === 0) {
return prefix || ".";
}
if (segment === "..") {
return prefix ? prefix.slice(0, -1) || "/" : "..";
}
return prefix + segment;
}
let collapsed = "";
let segmentCount = 0;
let leadingDotDots = 0;
while (nextIndex !== -1) {
const segment = filepath.slice(start, nextIndex);
if (segment.length > 0 && segment !== ".") {
if (segment === "..") {
if (segmentCount > 0) {
const lastSeparatorIndex = collapsed.lastIndexOf("/");
if (lastSeparatorIndex === -1) {
collapsed = "";
segmentCount = 0;
if (leadingDotDots > 0 && !prefix) {
collapsed = "..";
leadingDotDots = 1;
}
} else {
const lastSegmentStart = lastSeparatorIndex + 1;
const lastSegmentValue = collapsed.slice(lastSegmentStart);
if (lastSegmentValue === "..") {
collapsed = `${collapsed}/${segment}`;
leadingDotDots += 1;
} else {
collapsed = collapsed.slice(0, lastSeparatorIndex);
segmentCount -= 1;
}
}
} else if (!prefix) {
collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
leadingDotDots += 1;
}
} else {
collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
segmentCount += 1;
}
}
start = nextIndex + 1;
code = filepath.charCodeAt(start);
while (code === 47 || code === 92) {
start += 1;
code = filepath.charCodeAt(start);
}
nextIndex = (0, import_strings.search)(filepath, slashRegExp, { fromIndex: start });
}
const lastSegment = filepath.slice(start);
if (lastSegment.length > 0 && lastSegment !== ".") {
if (lastSegment === "..") {
if (segmentCount > 0) {
const lastSeparatorIndex = collapsed.lastIndexOf("/");
if (lastSeparatorIndex === -1) {
collapsed = "";
segmentCount = 0;
if (leadingDotDots > 0 && !prefix) {
collapsed = "..";
leadingDotDots = 1;
}
} else {
const lastSegmentStart = lastSeparatorIndex + 1;
const lastSegmentValue = collapsed.slice(lastSegmentStart);
if (lastSegmentValue === "..") {
collapsed = `${collapsed}/${lastSegment}`;
leadingDotDots += 1;
} else {
collapsed = collapsed.slice(0, lastSeparatorIndex);
segmentCount -= 1;
}
}
} else if (!prefix) {
collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
leadingDotDots += 1;
}
} else {
collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
segmentCount += 1;
}
}
if (collapsed.length === 0) {
return prefix || ".";
}
return prefix + collapsed;
}
// @__NO_SIDE_EFFECTS__
function pathLikeToString(pathLike) {
if (pathLike === null || pathLike === void 0) {
return "";
}
if (typeof pathLike === "string") {
return pathLike;
}
const { Buffer: Buffer2 } = /* @__PURE__ */ getBuffer();
if (Buffer2.isBuffer(pathLike)) {
return pathLike.toString("utf8");
}
const url = /* @__PURE__ */ getUrl();
if (pathLike instanceof URL) {
try {
return url.fileURLToPath(pathLike);
} catch {
const pathname = pathLike.pathname;
const decodedPathname = decodeURIComponent(pathname);
if (import_platform.WIN32 && decodedPathname.startsWith("/")) {
const letter = decodedPathname.charCodeAt(1) | 32;
const hasValidDriveLetter = decodedPathname.length >= 3 && letter >= 97 && // 'a' to 'z'
letter <= 122 && decodedPathname.charAt(2) === ":";
if (!hasValidDriveLetter) {
return decodedPathname;
}
}
return decodedPathname;
}
}
return String(pathLike);
}
// @__NO_SIDE_EFFECTS__
function splitPath(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
if (filepath === "") {
return [];
}
return filepath.split(slashRegExp);
}
// @__NO_SIDE_EFFECTS__
function trimLeadingDotSlash(pathLike) {
const filepath = /* @__PURE__ */ pathLikeToString(pathLike);
if (filepath.startsWith("./") || filepath.startsWith(".\\")) {
return filepath.slice(2);
}
return filepath;
}
// @__NO_SIDE_EFFECTS__
function resolve(...segments) {
let resolvedPath = "";
let resolvedAbsolute = false;
for (let i = segments.length - 1; i >= 0 && !resolvedAbsolute; i -= 1) {
const segment = segments[i];
if (typeof segment !== "string" || segment.length === 0) {
continue;
}
resolvedPath = segment + (resolvedPath.length === 0 ? "" : `/${resolvedPath}`);
resolvedAbsolute = /* @__PURE__ */ isAbsolute(segment);
}
if (!resolvedAbsolute) {
const cwd = /* @__PURE__ */ require("node:process").cwd();
resolvedPath = cwd + (resolvedPath.length === 0 ? "" : `/${resolvedPath}`);
}
return /* @__PURE__ */ normalizePath(resolvedPath);
}
// @__NO_SIDE_EFFECTS__
function relative(from, to) {
if (from === to) {
return "";
}
const actualFrom = /* @__PURE__ */ resolve(from);
const actualTo = /* @__PURE__ */ resolve(to);
if (actualFrom === actualTo) {
return "";
}
if (import_platform.WIN32) {
const fromLower = actualFrom.toLowerCase();
const toLower = actualTo.toLowerCase();
if (fromLower === toLower) {
return "";
}
}
const fromStart = 1;
const fromEnd = actualFrom.length;
const fromLen = fromEnd - fromStart;
const toStart = 1;
const toEnd = actualTo.length;
const toLen = toEnd - toStart;
const length = fromLen < toLen ? fromLen : toLen;
let lastCommonSep = -1;
let i = 0;
for (; i < length; i += 1) {
let fromCode = actualFrom.charCodeAt(fromStart + i);
let toCode = actualTo.charCodeAt(toStart + i);
if (import_platform.WIN32) {
if (fromCode >= CHAR_UPPERCASE_A && fromCode <= CHAR_UPPERCASE_Z) {
fromCode += 32;
}
if (toCode >= CHAR_UPPERCASE_A && toCode <= CHAR_UPPERCASE_Z) {
toCode += 32;
}
}
if (fromCode !== toCode) {
break;
}
if (/* @__PURE__ */ isPathSeparator(actualFrom.charCodeAt(fromStart + i))) {
lastCommonSep = i;
}
}
if (i === length) {
if (toLen > length) {
const toCode = actualTo.charCodeAt(toStart + i);
if (/* @__PURE__ */ isPathSeparator(toCode)) {
return actualTo.slice(toStart + i + 1);
}
if (i === 0) {
return actualTo.slice(toStart + i);
}
} else if (fromLen > length) {
const fromCode = actualFrom.charCodeAt(fromStart + i);
if (/* @__PURE__ */ isPathSeparator(fromCode)) {
lastCommonSep = i;
} else if (i === 0) {
lastCommonSep = 0;
}
}
}
let out = "";
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; i += 1) {
const code = actualFrom.charCodeAt(i);
if (i === fromEnd || /* @__PURE__ */ isPathSeparator(code)) {
out += out.length === 0 ? ".." : "/..";
}
}
return out + actualTo.slice(toStart + lastCommonSep);
}
// @__NO_SIDE_EFFECTS__
function relativeResolve(from, to) {
const rel = /* @__PURE__ */ relative(from, to);
if (rel === "") {
return "";
}
return /* @__PURE__ */ normalizePath(rel);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
isAbsolute,
isNodeModules,
isPath,
isRelative,
normalizePath,
pathLikeToString,
relativeResolve,
splitPath,
trimLeadingDotSlash
});