blade
Version:
React at the edge.
143 lines (141 loc) • 4 kB
JavaScript
//#region ../../node_modules/serialize-error/error-constructors.js
const list = [
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError,
globalThis.DOMException,
globalThis.AssertionError,
globalThis.SystemError
].filter(Boolean).map((constructor) => [constructor.name, constructor]);
const errorConstructors = new Map(list);
var error_constructors_default = errorConstructors;
//#endregion
//#region ../../node_modules/serialize-error/index.js
var NonError = class NonError extends Error {
name = "NonError";
constructor(message) {
super(NonError._prepareSuperMessage(message));
}
static _prepareSuperMessage(message) {
try {
return JSON.stringify(message);
} catch {
return String(message);
}
}
};
const commonProperties = [
{
property: "name",
enumerable: false
},
{
property: "message",
enumerable: false
},
{
property: "stack",
enumerable: false
},
{
property: "code",
enumerable: true
},
{
property: "cause",
enumerable: false
}
];
const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
const toJSON = (from) => {
toJsonWasCalled.add(from);
const json = from.toJSON();
toJsonWasCalled.delete(from);
return json;
};
const getErrorConstructor = (name) => error_constructors_default.get(name) ?? Error;
const destroyCircular = ({ from, seen, to, forceEnumerable, maxDepth, depth, useToJSON, serialize }) => {
if (!to) if (Array.isArray(from)) to = [];
else if (!serialize && isErrorLike(from)) to = new (getErrorConstructor(from.name))();
else to = {};
seen.push(from);
if (depth >= maxDepth) return to;
if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) return toJSON(from);
const continueDestroyCircular = (value) => destroyCircular({
from: value,
seen: [...seen],
forceEnumerable,
maxDepth,
depth,
useToJSON,
serialize
});
for (const [key, value] of Object.entries(from)) {
if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
to[key] = "[object Buffer]";
continue;
}
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
to[key] = "[object Stream]";
continue;
}
if (typeof value === "function") continue;
if (!value || typeof value !== "object") {
try {
to[key] = value;
} catch {}
continue;
}
if (!seen.includes(from[key])) {
depth++;
to[key] = continueDestroyCircular(from[key]);
continue;
}
to[key] = "[Circular]";
}
for (const { property, enumerable } of commonProperties) if (typeof from[property] !== "undefined" && from[property] !== null) Object.defineProperty(to, property, {
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
configurable: true,
writable: true
});
return to;
};
function serializeError(value, options = {}) {
const { maxDepth = Number.POSITIVE_INFINITY, useToJSON = true } = options;
if (typeof value === "object" && value !== null) return destroyCircular({
from: value,
seen: [],
forceEnumerable: true,
maxDepth,
depth: 0,
useToJSON,
serialize: true
});
if (typeof value === "function") return `[Function: ${value.name || "anonymous"}]`;
return value;
}
function deserializeError(value, options = {}) {
const { maxDepth = Number.POSITIVE_INFINITY } = options;
if (value instanceof Error) return value;
if (isMinimumViableSerializedError(value)) return destroyCircular({
from: value,
seen: [],
to: new (getErrorConstructor(value.name))(),
maxDepth,
depth: 0,
serialize: false
});
return new NonError(value);
}
function isErrorLike(value) {
return Boolean(value) && typeof value === "object" && "name" in value && "message" in value && "stack" in value;
}
function isMinimumViableSerializedError(value) {
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
}
//#endregion
export { serializeError as n, deserializeError as t };