@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
314 lines (313 loc) • 9.69 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 objects_exports = {};
__export(objects_exports, {
createConstantsObject: () => createConstantsObject,
createLazyGetter: () => createLazyGetter,
defineGetter: () => defineGetter,
defineLazyGetter: () => defineLazyGetter,
defineLazyGetters: () => defineLazyGetters,
entryKeyComparator: () => entryKeyComparator,
getKeys: () => getKeys,
getOwn: () => getOwn,
getOwnPropertyValues: () => getOwnPropertyValues,
hasKeys: () => hasKeys,
hasOwn: () => hasOwn,
isObject: () => isObject,
isObjectObject: () => isObjectObject,
merge: () => merge,
objectAssign: () => objectAssign,
objectEntries: () => objectEntries,
objectFreeze: () => objectFreeze,
toSortedObject: () => toSortedObject,
toSortedObjectFromEntries: () => toSortedObjectFromEntries
});
module.exports = __toCommonJS(objects_exports);
var import_core = require("#constants/core");
var import_arrays = require("./arrays");
var import_sorts = require("./sorts");
const ObjectDefineProperties = Object.defineProperties;
const ObjectDefineProperty = Object.defineProperty;
const ObjectFreeze = Object.freeze;
const ObjectFromEntries = Object.fromEntries;
const ObjectGetOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;
const ObjectGetOwnPropertyNames = Object.getOwnPropertyNames;
const ObjectGetPrototypeOf = Object.getPrototypeOf;
const ObjectHasOwn = Object.hasOwn;
const ObjectKeys = Object.keys;
const ObjectPrototype = Object.prototype;
const ObjectSetPrototypeOf = Object.setPrototypeOf;
const ReflectOwnKeys = Reflect.ownKeys;
// @__NO_SIDE_EFFECTS__
function createLazyGetter(name, getter, stats) {
let lazyValue = import_core.UNDEFINED_TOKEN;
const { [name]: lazyGetter } = {
[name]() {
if (lazyValue === import_core.UNDEFINED_TOKEN) {
stats?.initialized?.add(name);
lazyValue = getter();
}
return lazyValue;
}
};
return lazyGetter;
}
// @__NO_SIDE_EFFECTS__
function createConstantsObject(props, options_) {
const options = { __proto__: null, ...options_ };
const attributes = ObjectFreeze({
__proto__: null,
getters: options.getters ? ObjectFreeze(
ObjectSetPrototypeOf(/* @__PURE__ */ toSortedObject(options.getters), null)
) : void 0,
internals: options.internals ? ObjectFreeze(
ObjectSetPrototypeOf(/* @__PURE__ */ toSortedObject(options.internals), null)
) : void 0,
mixin: options.mixin ? ObjectFreeze(
ObjectDefineProperties(
{ __proto__: null },
ObjectGetOwnPropertyDescriptors(options.mixin)
)
) : void 0,
props: props ? ObjectFreeze(ObjectSetPrototypeOf(/* @__PURE__ */ toSortedObject(props), null)) : void 0
});
const lazyGetterStats = ObjectFreeze({
__proto__: null,
initialized: /* @__PURE__ */ new Set()
});
const object = defineLazyGetters(
{
__proto__: null,
[import_core.kInternalsSymbol]: ObjectFreeze({
__proto__: null,
get attributes() {
return attributes;
},
get lazyGetterStats() {
return lazyGetterStats;
},
...attributes.internals
}),
kInternalsSymbol: import_core.kInternalsSymbol,
...attributes.props
},
attributes.getters,
lazyGetterStats
);
if (attributes.mixin) {
ObjectDefineProperties(
object,
/* @__PURE__ */ toSortedObjectFromEntries(
(/* @__PURE__ */ objectEntries(ObjectGetOwnPropertyDescriptors(attributes.mixin))).filter(
(p) => !ObjectHasOwn(object, p[0])
)
)
);
}
return ObjectFreeze(object);
}
function defineGetter(object, propKey, getter) {
ObjectDefineProperty(object, propKey, {
get: getter,
enumerable: false,
configurable: true
});
return object;
}
function defineLazyGetter(object, propKey, getter, stats) {
return defineGetter(object, propKey, /* @__PURE__ */ createLazyGetter(propKey, getter, stats));
}
function defineLazyGetters(object, getterDefObj, stats) {
if (getterDefObj !== null && typeof getterDefObj === "object") {
const keys = ReflectOwnKeys(getterDefObj);
for (let i = 0, { length } = keys; i < length; i += 1) {
const key = keys[i];
defineLazyGetter(object, key, getterDefObj[key], stats);
}
}
return object;
}
// @__NO_SIDE_EFFECTS__
function entryKeyComparator(a, b) {
const keyA = a[0];
const keyB = b[0];
const strKeyA = typeof keyA === "string" ? keyA : String(keyA);
const strKeyB = typeof keyB === "string" ? keyB : String(keyB);
return (0, import_sorts.localeCompare)(strKeyA, strKeyB);
}
// @__NO_SIDE_EFFECTS__
function getKeys(obj) {
return /* @__PURE__ */ isObject(obj) ? ObjectKeys(obj) : [];
}
// @__NO_SIDE_EFFECTS__
function getOwn(obj, propKey) {
if (obj === null || obj === void 0) {
return void 0;
}
return ObjectHasOwn(obj, propKey) ? obj[propKey] : void 0;
}
// @__NO_SIDE_EFFECTS__
function getOwnPropertyValues(obj) {
if (obj === null || obj === void 0) {
return [];
}
const keys = ObjectGetOwnPropertyNames(obj);
const { length } = keys;
const values = Array(length);
for (let i = 0; i < length; i += 1) {
values[i] = obj[keys[i]];
}
return values;
}
// @__NO_SIDE_EFFECTS__
function hasKeys(obj) {
if (obj === null || obj === void 0) {
return false;
}
for (const key in obj) {
if (ObjectHasOwn(obj, key)) {
return true;
}
}
return false;
}
// @__NO_SIDE_EFFECTS__
function hasOwn(obj, propKey) {
if (obj === null || obj === void 0) {
return false;
}
return ObjectHasOwn(obj, propKey);
}
// @__NO_SIDE_EFFECTS__
function isObject(value) {
return value !== null && typeof value === "object";
}
// @__NO_SIDE_EFFECTS__
function isObjectObject(value) {
if (value === null || typeof value !== "object" || (0, import_arrays.isArray)(value)) {
return false;
}
const proto = ObjectGetPrototypeOf(value);
return proto === null || proto === ObjectPrototype;
}
const objectAssign = Object.assign;
// @__NO_SIDE_EFFECTS__
function objectEntries(obj) {
if (obj === null || obj === void 0) {
return [];
}
const keys = ReflectOwnKeys(obj);
const { length } = keys;
const entries = Array(length);
const record = obj;
for (let i = 0; i < length; i += 1) {
const key = keys[i];
entries[i] = [key, record[key]];
}
return entries;
}
const objectFreeze = Object.freeze;
// @__NO_SIDE_EFFECTS__
function merge(target, source) {
if (!/* @__PURE__ */ isObject(target) || !/* @__PURE__ */ isObject(source)) {
return target;
}
const queue = [[target, source]];
let pos = 0;
let { length: queueLength } = queue;
while (pos < queueLength) {
if (pos === import_core.LOOP_SENTINEL) {
throw new Error("Detected infinite loop in object crawl of merge");
}
const { 0: currentTarget, 1: currentSource } = queue[pos++];
if (!currentSource || !currentTarget) {
continue;
}
const isSourceArray = (0, import_arrays.isArray)(currentSource);
const isTargetArray = (0, import_arrays.isArray)(currentTarget);
if (isSourceArray || isTargetArray) {
continue;
}
const keys = ReflectOwnKeys(currentSource);
for (let i = 0, { length } = keys; i < length; i += 1) {
const key = keys[i];
const srcVal = currentSource[key];
const targetVal = currentTarget[key];
if ((0, import_arrays.isArray)(srcVal)) {
currentTarget[key] = srcVal;
} else if (/* @__PURE__ */ isObject(srcVal)) {
if (/* @__PURE__ */ isObject(targetVal) && !(0, import_arrays.isArray)(targetVal)) {
queue[queueLength++] = [targetVal, srcVal];
} else {
currentTarget[key] = srcVal;
}
} else {
currentTarget[key] = srcVal;
}
}
}
return target;
}
// @__NO_SIDE_EFFECTS__
function toSortedObject(obj) {
return /* @__PURE__ */ toSortedObjectFromEntries(/* @__PURE__ */ objectEntries(obj));
}
// @__NO_SIDE_EFFECTS__
function toSortedObjectFromEntries(entries) {
const otherEntries = [];
const symbolEntries = [];
for (const entry of entries) {
if (typeof entry[0] === "symbol") {
symbolEntries.push(entry);
} else {
otherEntries.push(entry);
}
}
if (!otherEntries.length && !symbolEntries.length) {
return {};
}
return ObjectFromEntries([
// The String constructor is safe to use with symbols.
...symbolEntries.sort(entryKeyComparator),
...otherEntries.sort(entryKeyComparator)
]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createConstantsObject,
createLazyGetter,
defineGetter,
defineLazyGetter,
defineLazyGetters,
entryKeyComparator,
getKeys,
getOwn,
getOwnPropertyValues,
hasKeys,
hasOwn,
isObject,
isObjectObject,
merge,
objectAssign,
objectEntries,
objectFreeze,
toSortedObject,
toSortedObjectFromEntries
});