c12
Version:
Smart Config Loader
182 lines (181 loc) • 6.01 kB
JavaScript
import { createHash } from "node:crypto";
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
let target = {};
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
return target;
};
function serialize(o) {
return typeof o == "string" ? `'${o}'` : new c().serialize(o);
}
const c = /* @__PURE__ */ function() {
class o {
#t = /* @__PURE__ */ new Map();
compare(t, r) {
const e = typeof t, n = typeof r;
return e === "string" && n === "string" ? t.localeCompare(r) : e === "number" && n === "number" ? t - r : String.prototype.localeCompare.call(this.serialize(t, true), this.serialize(r, true));
}
serialize(t, r) {
if (t === null) return "null";
switch (typeof t) {
case "string": return r ? t : `'${t}'`;
case "bigint": return `${t}n`;
case "object": return this.$object(t);
case "function": return this.$function(t);
}
return String(t);
}
serializeObject(t) {
const r = Object.prototype.toString.call(t);
if (r !== "[object Object]") return this.serializeBuiltInType(r.length < 10 ? `unknown:${r}` : r.slice(8, -1), t);
const e = t.constructor, n = e === Object || e === void 0 ? "" : e.name;
if (n !== "" && globalThis[n] === e) return this.serializeBuiltInType(n, t);
if (typeof t.toJSON == "function") {
const i = t.toJSON();
return n + (i !== null && typeof i == "object" ? this.$object(i) : `(${this.serialize(i)})`);
}
return this.serializeObjectEntries(n, Object.entries(t));
}
serializeBuiltInType(t, r) {
const e = this["$" + t];
if (e) return e.call(this, r);
if (typeof r?.entries == "function") return this.serializeObjectEntries(t, r.entries());
throw new Error(`Cannot serialize ${t}`);
}
serializeObjectEntries(t, r) {
const e = Array.from(r).sort((i, a) => this.compare(i[0], a[0]));
let n = `${t}{`;
for (let i = 0; i < e.length; i++) {
const [a, l] = e[i];
n += `${this.serialize(a, true)}:${this.serialize(l)}`, i < e.length - 1 && (n += ",");
}
return n + "}";
}
$object(t) {
let r = this.#t.get(t);
return r === void 0 && (this.#t.set(t, `#${this.#t.size}`), r = this.serializeObject(t), this.#t.set(t, r)), r;
}
$function(t) {
const r = Function.prototype.toString.call(t);
return r.slice(-15) === "[native code] }" ? `${t.name || ""}()[native]` : `${t.name}(${t.length})${r.replace(/\s*\n\s*/g, "")}`;
}
$Array(t) {
let r = "[";
for (let e = 0; e < t.length; e++) r += this.serialize(t[e]), e < t.length - 1 && (r += ",");
return r + "]";
}
$Date(t) {
try {
return `Date(${t.toISOString()})`;
} catch {
return "Date(null)";
}
}
$ArrayBuffer(t) {
return `ArrayBuffer[${new Uint8Array(t).join(",")}]`;
}
$Set(t) {
return `Set${this.$Array(Array.from(t).sort((r, e) => this.compare(r, e)))}`;
}
$Map(t) {
return this.serializeObjectEntries("Map", t.entries());
}
}
for (const s of [
"Error",
"RegExp",
"URL"
]) o.prototype["$" + s] = function(t) {
return `${s}(${t})`;
};
for (const s of [
"Int8Array",
"Uint8Array",
"Uint8ClampedArray",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array"
]) o.prototype["$" + s] = function(t) {
return `${s}[${t.join(",")}]`;
};
for (const s of ["BigInt64Array", "BigUint64Array"]) o.prototype["$" + s] = function(t) {
return `${s}[${t.join("n,")}${t.length > 0 ? "n" : ""}]`;
};
return o;
}();
const e = globalThis.process?.getBuiltinModule?.("crypto")?.hash, r = "sha256", s = "base64url";
function digest(t) {
if (e) return e(r, t, s);
const o = createHash(r).update(t);
return globalThis.process?.versions?.webcontainer ? o.digest().toString(s) : o.digest(s);
}
var dist_exports = /* @__PURE__ */ __exportAll({ digest: () => digest });
var utils_exports = /* @__PURE__ */ __exportAll({ diff: () => diff });
function diff(obj1, obj2) {
return _diff(_toHashedObject(obj1), _toHashedObject(obj2));
}
function _diff(h1, h2) {
const diffs = [];
const allProps = /* @__PURE__ */ new Set([...Object.keys(h1.props || {}), ...Object.keys(h2.props || {})]);
if (h1.props && h2.props) for (const prop of allProps) {
const p1 = h1.props[prop];
const p2 = h2.props[prop];
if (p1 && p2) diffs.push(..._diff(h1.props?.[prop], h2.props?.[prop]));
else if (p1 || p2) diffs.push(new DiffEntry((p2 || p1).key, p1 ? "removed" : "added", p2, p1));
}
if (allProps.size === 0 && h1.hash !== h2.hash) diffs.push(new DiffEntry((h2 || h1).key, "changed", h2, h1));
return diffs;
}
function _toHashedObject(obj, key = "") {
if (obj && typeof obj !== "object") return new DiffHashedObject(key, obj, serialize(obj));
const props = {};
const hashes = [];
for (const _key in obj) {
props[_key] = _toHashedObject(obj[_key], key ? `${key}.${_key}` : _key);
hashes.push(props[_key].hash);
}
return new DiffHashedObject(key, obj, `{${hashes.join(":")}}`, props);
}
var DiffEntry = class {
constructor(key, type, newValue, oldValue) {
this.key = key;
this.type = type;
this.newValue = newValue;
this.oldValue = oldValue;
}
toString() {
return this.toJSON();
}
toJSON() {
switch (this.type) {
case "added": return `Added \`${this.key}\``;
case "removed": return `Removed \`${this.key}\``;
case "changed": return `Changed \`${this.key}\` from \`${this.oldValue?.toString() || "-"}\` to \`${this.newValue.toString()}\``;
}
}
};
var DiffHashedObject = class {
constructor(key, value, hash, props) {
this.key = key;
this.value = value;
this.hash = hash;
this.props = props;
}
toString() {
if (this.props) return `{${Object.keys(this.props).join(",")}}`;
else return JSON.stringify(this.value);
}
toJSON() {
const k = this.key || ".";
if (this.props) return `${k}({${Object.keys(this.props).join(",")}})`;
return `${k}(${this.value})`;
}
};
export { dist_exports as n, utils_exports as t };