MCPcopy Create free account
hub / github.com/agentrpc/agentrpc / destroyCircular

Function destroyCircular

sdk-node/src/serialize-error.js:85–181  ·  view source on GitHub ↗
({
  from,
  seen,
  to,
  forceEnumerable,
  maxDepth,
  depth,
  useToJSON,
  serialize,
})

Source from the content-addressed store, hash-verified

83
84// eslint-disable-next-line complexity
85const destroyCircular = ({
86 from,
87 seen,
88 to,
89 forceEnumerable,
90 maxDepth,
91 depth,
92 useToJSON,
93 serialize,
94}) => {
95 if (!to) {
96 if (Array.isArray(from)) {
97 to = [];
98 } else if (!serialize && isErrorLike(from)) {
99 const Error = getErrorConstructor(from.name);
100 to = new Error();
101 } else {
102 to = {};
103 }
104 }
105
106 seen.push(from);
107
108 if (depth >= maxDepth) {
109 return to;
110 }
111
112 if (
113 useToJSON &&
114 typeof from.toJSON === "function" &&
115 !toJsonWasCalled.has(from)
116 ) {
117 return toJSON(from);
118 }
119
120 const continueDestroyCircular = (value) =>
121 destroyCircular({
122 from: value,
123 seen: [...seen],
124 forceEnumerable,
125 maxDepth,
126 depth,
127 useToJSON,
128 serialize,
129 });
130
131 for (const [key, value] of Object.entries(from)) {
132 // eslint-disable-next-line node/prefer-global/buffer
133 if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
134 to[key] = "[object Buffer]";
135 continue;
136 }
137
138 // TODO: Use `stream.isReadable()` when targeting Node.js 18.
139 if (
140 value !== null &&
141 typeof value === "object" &&
142 typeof value.pipe === "function"

Callers 2

continueDestroyCircularFunction · 0.85
serializeErrorFunction · 0.85

Calls 4

isErrorLikeFunction · 0.85
getErrorConstructorFunction · 0.85
toJSONFunction · 0.85
continueDestroyCircularFunction · 0.85

Tested by

no test coverage detected