MCPcopy
hub / github.com/cloudflare/capnweb / devaluateImpl

Method devaluateImpl

src/serialize.ts:132–526  ·  view source on GitHub ↗
(value: unknown, parent: object | undefined, depth: number)

Source from the content-addressed store, hash-verified

130 private exports?: Array<ExportId>;
131
132 private devaluateImpl(value: unknown, parent: object | undefined, depth: number): unknown {
133 if (depth >= 64) {
134 throw new Error(
135 "Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
136 }
137
138 let kind = typeForRpc(value);
139 switch (kind) {
140 case "unsupported": {
141 let msg;
142 try {
143 msg = `Cannot serialize value: ${value}`;
144 } catch (err) {
145 msg = "Cannot serialize value: (couldn't stringify value)";
146 }
147 throw new TypeError(msg);
148 }
149
150 case "primitive":
151 if (typeof value === "number" && !isFinite(value)) {
152 // At structuredClonable level, keep Infinity/NaN as native values
153 if (this.encodingLevel === "structuredClonable") {
154 return value;
155 }
156 if (value === Infinity) {
157 return ["inf"];
158 } else if (value === -Infinity) {
159 return ["-inf"];
160 } else {
161 return ["nan"];
162 }
163 } else {
164 // Supported directly by JSON.
165 return value;
166 }
167
168 case "object": {
169 let object = <Record<string, unknown>>value;
170 let result: Record<string, unknown> = {};
171 for (let key in object) {
172 result[key] = this.devaluateImpl(object[key], object, depth + 1);
173 }
174 return result;
175 }
176
177 case "array": {
178 let array = <Array<unknown>>value;
179 let len = array.length;
180 let result = new Array(len);
181 for (let i = 0; i < len; i++) {
182 result[i] = this.devaluateImpl(array[i], array, depth + 1);
183 }
184 // Wrap literal arrays in an outer one-element array, to "escape" them.
185 return [result];
186 }
187
188 case "bigint":
189 // At structuredClonable level, keep BigInt as native value

Callers 2

devaluateMethod · 0.95
capturePropMethod · 0.95

Calls 14

devaluateHookMethod · 0.95
typeForRpcFunction · 0.85
unwrapStubAndPathFunction · 0.85
toBase64Method · 0.80
getHookForRpcTargetMethod · 0.80
createPipeMethod · 0.65
onSendErrorMethod · 0.65
getImportMethod · 0.65
dupMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected