451 lines
15 KiB
JavaScript
451 lines
15 KiB
JavaScript
//#region ../node_modules/.pnpm/devalue@5.8.1/node_modules/devalue/src/constants.js
|
|
var MAX_ARRAY_LEN = 2 ** 32 - 1;
|
|
var MAX_ARRAY_INDEX = MAX_ARRAY_LEN - 1;
|
|
//#endregion
|
|
//#region ../node_modules/.pnpm/devalue@5.8.1/node_modules/devalue/src/utils.js
|
|
/** @type {Record<string, string>} */
|
|
var escaped = {
|
|
"<": "\\u003C",
|
|
"\\": "\\\\",
|
|
"\b": "\\b",
|
|
"\f": "\\f",
|
|
"\n": "\\n",
|
|
"\r": "\\r",
|
|
" ": "\\t",
|
|
"\u2028": "\\u2028",
|
|
"\u2029": "\\u2029"
|
|
};
|
|
var DevalueError = class extends Error {
|
|
/**
|
|
* @param {string} message
|
|
* @param {string[]} keys
|
|
* @param {any} [value] - The value that failed to be serialized
|
|
* @param {any} [root] - The root value being serialized
|
|
*/
|
|
constructor(message, keys, value, root) {
|
|
super(message);
|
|
this.name = "DevalueError";
|
|
this.path = keys.join("");
|
|
this.value = value;
|
|
this.root = root;
|
|
}
|
|
};
|
|
/** @param {any} thing */
|
|
function is_primitive(thing) {
|
|
return thing === null || typeof thing !== "object" && typeof thing !== "function";
|
|
}
|
|
var object_proto_names = /* @__PURE__ */ Object.getOwnPropertyNames(Object.prototype).sort().join("\0");
|
|
/** @param {any} thing */
|
|
function is_plain_object(thing) {
|
|
const proto = Object.getPrototypeOf(thing);
|
|
return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null || Object.getOwnPropertyNames(proto).sort().join("\0") === object_proto_names;
|
|
}
|
|
/** @param {any} thing */
|
|
function get_type(thing) {
|
|
return Object.prototype.toString.call(thing).slice(8, -1);
|
|
}
|
|
/** @param {string} char */
|
|
function get_escaped_char(char) {
|
|
switch (char) {
|
|
case "\"": return "\\\"";
|
|
case "<": return "\\u003C";
|
|
case "\\": return "\\\\";
|
|
case "\n": return "\\n";
|
|
case "\r": return "\\r";
|
|
case " ": return "\\t";
|
|
case "\b": return "\\b";
|
|
case "\f": return "\\f";
|
|
case "\u2028": return "\\u2028";
|
|
case "\u2029": return "\\u2029";
|
|
default: return char < " " ? `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}` : "";
|
|
}
|
|
}
|
|
/** @param {string} str */
|
|
function stringify_string(str) {
|
|
let result = "";
|
|
let last_pos = 0;
|
|
const len = str.length;
|
|
for (let i = 0; i < len; i += 1) {
|
|
const char = str[i];
|
|
const replacement = get_escaped_char(char);
|
|
if (replacement) {
|
|
result += str.slice(last_pos, i) + replacement;
|
|
last_pos = i + 1;
|
|
}
|
|
}
|
|
return `"${last_pos === 0 ? str : result + str.slice(last_pos)}"`;
|
|
}
|
|
/** @param {Record<string | symbol, any>} object */
|
|
function enumerable_symbols(object) {
|
|
return Object.getOwnPropertySymbols(object).filter((symbol) => Object.getOwnPropertyDescriptor(object, symbol).enumerable);
|
|
}
|
|
var is_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
|
|
/** @param {string} key */
|
|
function stringify_key(key) {
|
|
return is_identifier.test(key) ? "." + key : "[" + JSON.stringify(key) + "]";
|
|
}
|
|
/** @param {number} n */
|
|
function is_valid_array_index(n) {
|
|
if (!Number.isInteger(n)) return false;
|
|
if (n < 0) return false;
|
|
if (n > MAX_ARRAY_INDEX) return false;
|
|
return true;
|
|
}
|
|
/** @param {number} n */
|
|
function is_valid_array_len(n) {
|
|
if (!Number.isInteger(n)) return false;
|
|
if (n < 0) return false;
|
|
if (n > MAX_ARRAY_LEN) return false;
|
|
return true;
|
|
}
|
|
/** @param {string} s */
|
|
function is_valid_array_index_string(s) {
|
|
if (s.length === 0) return false;
|
|
if (s.length > 1 && s.charCodeAt(0) === 48) return false;
|
|
for (let i = 0; i < s.length; i++) {
|
|
const c = s.charCodeAt(i);
|
|
if (c < 48 || c > 57) return false;
|
|
}
|
|
return is_valid_array_index(+s);
|
|
}
|
|
/**
|
|
* Finds the populated indices of an array.
|
|
* @param {unknown[]} array
|
|
*/
|
|
function valid_array_indices(array) {
|
|
const keys = Object.keys(array);
|
|
for (var i = keys.length - 1; i >= 0; i--) if (is_valid_array_index_string(keys[i])) break;
|
|
keys.length = i + 1;
|
|
return keys;
|
|
}
|
|
//#endregion
|
|
//#region ../node_modules/.pnpm/devalue@5.8.1/node_modules/devalue/src/uneval.js
|
|
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";
|
|
var unsafe_chars = /[<\b\f\n\r\t\0\u2028\u2029]/g;
|
|
var reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
|
|
/**
|
|
* Turn a value into the JavaScript that creates an equivalent value
|
|
* @param {any} value
|
|
* @param {(value: any, uneval: (value: any) => string) => string | void} [replacer]
|
|
*/
|
|
function uneval(value, replacer) {
|
|
const counts = /* @__PURE__ */ new Map();
|
|
/** @type {string[]} */
|
|
const keys = [];
|
|
const custom = /* @__PURE__ */ new Map();
|
|
/** @param {any} thing */
|
|
function walk(thing) {
|
|
if (!is_primitive(thing)) {
|
|
if (counts.has(thing)) {
|
|
counts.set(thing, counts.get(thing) + 1);
|
|
return;
|
|
}
|
|
counts.set(thing, 1);
|
|
if (replacer) {
|
|
const str = replacer(thing, (value) => uneval(value, replacer));
|
|
if (typeof str === "string") {
|
|
custom.set(thing, str);
|
|
return;
|
|
}
|
|
}
|
|
if (typeof thing === "function") throw new DevalueError(`Cannot stringify a function`, keys, thing, value);
|
|
switch (get_type(thing)) {
|
|
case "Number":
|
|
case "BigInt":
|
|
case "String":
|
|
case "Boolean":
|
|
case "Date":
|
|
case "RegExp":
|
|
case "URL":
|
|
case "URLSearchParams": return;
|
|
case "Array":
|
|
/** @type {any[]} */ thing.forEach((value, i) => {
|
|
keys.push(`[${i}]`);
|
|
walk(value);
|
|
keys.pop();
|
|
});
|
|
break;
|
|
case "Set":
|
|
Array.from(thing).forEach(walk);
|
|
break;
|
|
case "Map":
|
|
for (const [key, value] of thing) {
|
|
keys.push(`.get(${is_primitive(key) ? stringify_primitive(key) : "..."})`);
|
|
walk(value);
|
|
keys.pop();
|
|
}
|
|
break;
|
|
case "Int8Array":
|
|
case "Uint8Array":
|
|
case "Uint8ClampedArray":
|
|
case "Int16Array":
|
|
case "Uint16Array":
|
|
case "Float16Array":
|
|
case "Int32Array":
|
|
case "Uint32Array":
|
|
case "Float32Array":
|
|
case "Float64Array":
|
|
case "BigInt64Array":
|
|
case "BigUint64Array":
|
|
case "DataView":
|
|
walk(thing.buffer);
|
|
return;
|
|
case "ArrayBuffer": return;
|
|
case "Temporal.Duration":
|
|
case "Temporal.Instant":
|
|
case "Temporal.PlainDate":
|
|
case "Temporal.PlainTime":
|
|
case "Temporal.PlainDateTime":
|
|
case "Temporal.PlainMonthDay":
|
|
case "Temporal.PlainYearMonth":
|
|
case "Temporal.ZonedDateTime": return;
|
|
default:
|
|
if (!is_plain_object(thing)) throw new DevalueError(`Cannot stringify arbitrary non-POJOs`, keys, thing, value);
|
|
if (enumerable_symbols(thing).length > 0) throw new DevalueError(`Cannot stringify POJOs with symbolic keys`, keys, thing, value);
|
|
for (const key of Object.keys(thing)) {
|
|
if (key === "__proto__") throw new DevalueError(`Cannot stringify objects with __proto__ keys`, keys, thing, value);
|
|
keys.push(stringify_key(key));
|
|
walk(thing[key]);
|
|
keys.pop();
|
|
}
|
|
}
|
|
} else if (typeof thing === "symbol") throw new DevalueError(`Cannot stringify a Symbol primitive`, keys, thing, value);
|
|
}
|
|
walk(value);
|
|
const names = /* @__PURE__ */ new Map();
|
|
Array.from(counts).filter((entry) => entry[1] > 1).sort((a, b) => b[1] - a[1]).forEach((entry, i) => {
|
|
names.set(entry[0], get_name(i));
|
|
});
|
|
/**
|
|
* @param {any} thing
|
|
* @returns {string}
|
|
*/
|
|
function stringify(thing) {
|
|
if (names.has(thing)) return names.get(thing);
|
|
if (is_primitive(thing)) return stringify_primitive(thing);
|
|
if (custom.has(thing)) return custom.get(thing);
|
|
const type = get_type(thing);
|
|
switch (type) {
|
|
case "Number":
|
|
case "String":
|
|
case "Boolean":
|
|
case "BigInt": return `Object(${stringify(thing.valueOf())})`;
|
|
case "RegExp":
|
|
const { source, flags } = thing;
|
|
return flags ? `new RegExp(${stringify_string(source)},"${flags}")` : `new RegExp(${stringify_string(source)})`;
|
|
case "Date": return `new Date(${thing.getTime()})`;
|
|
case "URL": return `new URL(${stringify_string(thing.toString())})`;
|
|
case "URLSearchParams": return `new URLSearchParams(${stringify_string(thing.toString())})`;
|
|
case "Array": {
|
|
let has_holes = false;
|
|
let result = "[";
|
|
for (let i = 0; i < thing.length; i += 1) {
|
|
if (i > 0) result += ",";
|
|
if (Object.hasOwn(thing, i)) result += stringify(thing[i]);
|
|
else if (!has_holes) {
|
|
const populated_keys = valid_array_indices(thing);
|
|
const population = populated_keys.length;
|
|
const d = String(thing.length).length;
|
|
if (thing.length + 2 > 25 + d + population * (d + 2)) {
|
|
const entries = populated_keys.map((k) => `${k}:${stringify(thing[k])}`).join(",");
|
|
return `Object.assign(Array(${thing.length}),{${entries}})`;
|
|
}
|
|
has_holes = true;
|
|
i -= 1;
|
|
}
|
|
}
|
|
const tail = thing.length === 0 || thing.length - 1 in thing ? "" : ",";
|
|
return result + tail + "]";
|
|
}
|
|
case "Set":
|
|
case "Map": return `new ${type}([${Array.from(thing).map(stringify).join(",")}])`;
|
|
case "Int8Array":
|
|
case "Uint8Array":
|
|
case "Uint8ClampedArray":
|
|
case "Int16Array":
|
|
case "Uint16Array":
|
|
case "Float16Array":
|
|
case "Int32Array":
|
|
case "Uint32Array":
|
|
case "Float32Array":
|
|
case "Float64Array":
|
|
case "BigInt64Array":
|
|
case "BigUint64Array": {
|
|
let str = `new ${type}`;
|
|
if (!names.has(thing.buffer)) {
|
|
const array = new thing.constructor(thing.buffer);
|
|
str += `([${array}])`;
|
|
} else str += `(${stringify(thing.buffer)})`;
|
|
if (thing.byteLength !== thing.buffer.byteLength) {
|
|
const start = thing.byteOffset / thing.BYTES_PER_ELEMENT;
|
|
const end = start + thing.length;
|
|
str += `.subarray(${start},${end})`;
|
|
}
|
|
return str;
|
|
}
|
|
case "DataView": {
|
|
let str = `new DataView`;
|
|
if (!names.has(thing.buffer)) str += `(new Uint8Array([${new Uint8Array(thing.buffer)}]).buffer`;
|
|
else str += `(${stringify(thing.buffer)}`;
|
|
if (thing.byteLength !== thing.buffer.byteLength) str += `,${thing.startOffset},${thing.byteLength}`;
|
|
return str + ")";
|
|
}
|
|
case "ArrayBuffer": return `new Uint8Array([${new Uint8Array(thing).toString()}]).buffer`;
|
|
case "Temporal.Duration":
|
|
case "Temporal.Instant":
|
|
case "Temporal.PlainDate":
|
|
case "Temporal.PlainTime":
|
|
case "Temporal.PlainDateTime":
|
|
case "Temporal.PlainMonthDay":
|
|
case "Temporal.PlainYearMonth":
|
|
case "Temporal.ZonedDateTime": return `${type}.from(${stringify_string(thing.toString())})`;
|
|
default:
|
|
const keys = Object.keys(thing);
|
|
const obj = keys.map((key) => `${safe_key(key)}:${stringify(thing[key])}`).join(",");
|
|
if (Object.getPrototypeOf(thing) === null) return keys.length > 0 ? `{${obj},__proto__:null}` : `{__proto__:null}`;
|
|
return `{${obj}}`;
|
|
}
|
|
}
|
|
const str = stringify(value);
|
|
if (names.size) {
|
|
/** @type {string[]} */
|
|
const params = [];
|
|
/** @type {string[]} */
|
|
const statements = [];
|
|
/** @type {string[]} */
|
|
const values = [];
|
|
names.forEach((name, thing) => {
|
|
params.push(name);
|
|
if (custom.has(thing)) {
|
|
values.push(custom.get(thing));
|
|
return;
|
|
}
|
|
if (is_primitive(thing)) {
|
|
values.push(stringify_primitive(thing));
|
|
return;
|
|
}
|
|
const type = get_type(thing);
|
|
switch (type) {
|
|
case "Number":
|
|
case "String":
|
|
case "Boolean":
|
|
case "BigInt":
|
|
values.push(`Object(${stringify(thing.valueOf())})`);
|
|
break;
|
|
case "RegExp":
|
|
const { source, flags } = thing;
|
|
const regexp = flags ? `new RegExp(${stringify_string(source)},"${flags}")` : `new RegExp(${stringify_string(source)})`;
|
|
values.push(regexp);
|
|
break;
|
|
case "Date":
|
|
values.push(`new Date(${thing.getTime()})`);
|
|
break;
|
|
case "URL":
|
|
values.push(`new URL(${stringify_string(thing.toString())})`);
|
|
break;
|
|
case "URLSearchParams":
|
|
values.push(`new URLSearchParams(${stringify_string(thing.toString())})`);
|
|
break;
|
|
case "Array":
|
|
values.push(`Array(${thing.length})`);
|
|
/** @type {any[]} */ thing.forEach((v, i) => {
|
|
statements.push(`${name}[${i}]=${stringify(v)}`);
|
|
});
|
|
break;
|
|
case "Set":
|
|
values.push(`new Set`);
|
|
statements.push(`${name}.${Array.from(thing).map((v) => `add(${stringify(v)})`).join(".")}`);
|
|
break;
|
|
case "Map":
|
|
values.push(`new Map`);
|
|
statements.push(`${name}.${Array.from(thing).map(([k, v]) => `set(${stringify(k)}, ${stringify(v)})`).join(".")}`);
|
|
break;
|
|
case "Int8Array":
|
|
case "Uint8Array":
|
|
case "Uint8ClampedArray":
|
|
case "Int16Array":
|
|
case "Uint16Array":
|
|
case "Float16Array":
|
|
case "Int32Array":
|
|
case "Uint32Array":
|
|
case "Float32Array":
|
|
case "Float64Array":
|
|
case "BigInt64Array":
|
|
case "BigUint64Array": {
|
|
let str = `new ${type}`;
|
|
if (!names.has(thing.buffer)) {
|
|
const array = new thing.constructor(thing.buffer);
|
|
str += `([${array}])`;
|
|
} else str += `(${stringify(thing.buffer)})`;
|
|
if (thing.byteLength !== thing.buffer.byteLength) {
|
|
const start = thing.byteOffset / thing.BYTES_PER_ELEMENT;
|
|
const end = start + thing.length;
|
|
str += `.subarray(${start},${end})`;
|
|
}
|
|
values.push(`{}`);
|
|
statements.push(`${name}=${str}`);
|
|
break;
|
|
}
|
|
case "DataView": {
|
|
let str = `new DataView`;
|
|
if (!names.has(thing.buffer)) str += `(new Uint8Array([${new Uint8Array(thing.buffer)}]).buffer`;
|
|
else str += `(${stringify(thing.buffer)}`;
|
|
if (thing.byteLength !== thing.buffer.byteLength) str += `,${thing.byteOffset},${thing.byteLength}`;
|
|
str += ")";
|
|
values.push(`{}`);
|
|
statements.push(`${name}=${str}`);
|
|
break;
|
|
}
|
|
case "ArrayBuffer":
|
|
values.push(`new Uint8Array([${new Uint8Array(thing)}]).buffer`);
|
|
break;
|
|
default:
|
|
values.push(Object.getPrototypeOf(thing) === null ? "Object.create(null)" : "{}");
|
|
Object.keys(thing).forEach((key) => {
|
|
statements.push(`${name}${safe_prop(key)}=${stringify(thing[key])}`);
|
|
});
|
|
}
|
|
});
|
|
statements.push(`return ${str}`);
|
|
return `(function(${params.join(",")}){${statements.join(";")}}(${values.join(",")}))`;
|
|
} else return str;
|
|
}
|
|
/** @param {number} num */
|
|
function get_name(num) {
|
|
let name = "";
|
|
do {
|
|
name = chars[num % 54] + name;
|
|
num = ~~(num / 54) - 1;
|
|
} while (num >= 0);
|
|
return reserved.test(name) ? `${name}0` : name;
|
|
}
|
|
/** @param {string} c */
|
|
function escape_unsafe_char(c) {
|
|
return escaped[c] || c;
|
|
}
|
|
/** @param {string} str */
|
|
function escape_unsafe_chars(str) {
|
|
return str.replace(unsafe_chars, escape_unsafe_char);
|
|
}
|
|
/** @param {string} key */
|
|
function safe_key(key) {
|
|
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : escape_unsafe_chars(JSON.stringify(key));
|
|
}
|
|
/** @param {string} key */
|
|
function safe_prop(key) {
|
|
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? `.${key}` : `[${escape_unsafe_chars(JSON.stringify(key))}]`;
|
|
}
|
|
/** @param {any} thing */
|
|
function stringify_primitive(thing) {
|
|
const type = typeof thing;
|
|
if (type === "string") return stringify_string(thing);
|
|
if (thing === void 0) return "void 0";
|
|
if (thing === 0 && 1 / thing < 0) return "-0";
|
|
const str = String(thing);
|
|
if (type === "number") return str.replace(/^(-)?0\./, "$1.");
|
|
if (type === "bigint") return thing + "n";
|
|
return str;
|
|
}
|
|
//#endregion
|
|
export { is_plain_object as a, is_valid_array_len as c, valid_array_indices as d, MAX_ARRAY_INDEX as f, get_type as i, stringify_key as l, DevalueError as n, is_primitive as o, enumerable_symbols as r, is_valid_array_index as s, uneval as t, stringify_string as u };
|