@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
145 lines (144 loc) • 4.41 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 url_exports = {};
__export(url_exports, {
createRelativeUrl: () => createRelativeUrl,
isUrl: () => isUrl,
parseUrl: () => parseUrl,
urlSearchParamAsArray: () => urlSearchParamAsArray,
urlSearchParamAsBoolean: () => urlSearchParamAsBoolean,
urlSearchParamAsNumber: () => urlSearchParamAsNumber,
urlSearchParamAsString: () => urlSearchParamAsString,
urlSearchParamsGetArray: () => urlSearchParamsGetArray,
urlSearchParamsGetBoolean: () => urlSearchParamsGetBoolean
});
module.exports = __toCommonJS(url_exports);
const BooleanCtor = Boolean;
const UrlCtor = URL;
// @__NO_SIDE_EFFECTS__
function isUrl(value) {
return (typeof value === "string" && value !== "" || value !== null && typeof value === "object") && !!/* @__PURE__ */ parseUrl(value);
}
// @__NO_SIDE_EFFECTS__
function parseUrl(value) {
try {
return new UrlCtor(value);
} catch {
}
return void 0;
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamAsArray(value) {
return typeof value === "string" ? value.trim().split(/, */).map((v) => v.trim()).filter(BooleanCtor) : [];
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamAsBoolean(value, options) {
const { defaultValue = false } = {
__proto__: null,
...options
};
if (typeof value === "string") {
const trimmed = value.trim();
return trimmed === "1" || trimmed.toLowerCase() === "true";
}
if (value === null || value === void 0) {
return !!defaultValue;
}
return !!value;
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamsGetArray(params, key) {
if (params && typeof params.getAll === "function") {
const values = params.getAll(key);
const firstValue = values[0];
if (values.length === 1 && firstValue && firstValue.includes(",")) {
return /* @__PURE__ */ urlSearchParamAsArray(firstValue);
}
return values;
}
return [];
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamsGetBoolean(params, key, options) {
const { defaultValue = false } = {
__proto__: null,
...options
};
if (params && typeof params.get === "function") {
const value = params.get(key);
return value !== null ? /* @__PURE__ */ urlSearchParamAsBoolean(value, { defaultValue }) : defaultValue;
}
return defaultValue;
}
// @__NO_SIDE_EFFECTS__
function createRelativeUrl(path, options) {
const { base = "" } = {
__proto__: null,
...options
};
const relativePath = path.replace(/^\//, "");
if (base) {
let baseUrl = base;
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
return baseUrl + relativePath;
}
return relativePath;
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamAsString(params, key, options) {
const { defaultValue = "" } = {
__proto__: null,
...options
};
if (params && typeof params.get === "function") {
const value = params.get(key);
return value !== null ? value : defaultValue;
}
return defaultValue;
}
// @__NO_SIDE_EFFECTS__
function urlSearchParamAsNumber(params, key, options) {
const { defaultValue = 0 } = {
__proto__: null,
...options
};
if (params && typeof params.get === "function") {
const value = params.get(key);
if (value !== null) {
const num = Number(value);
return !Number.isNaN(num) ? num : defaultValue;
}
}
return defaultValue;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createRelativeUrl,
isUrl,
parseUrl,
urlSearchParamAsArray,
urlSearchParamAsBoolean,
urlSearchParamAsNumber,
urlSearchParamAsString,
urlSearchParamsGetArray,
urlSearchParamsGetBoolean
});