serializers.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var serializers_exports = {};
  20. __export(serializers_exports, {
  21. parseSerializedValue: () => parseSerializedValue,
  22. serializeValue: () => serializeValue
  23. });
  24. module.exports = __toCommonJS(serializers_exports);
  25. function parseSerializedValue(value, handles) {
  26. return innerParseSerializedValue(value, handles, /* @__PURE__ */ new Map(), []);
  27. }
  28. function innerParseSerializedValue(value, handles, refs, accessChain) {
  29. if (value.ref !== void 0)
  30. return refs.get(value.ref);
  31. if (value.n !== void 0)
  32. return value.n;
  33. if (value.s !== void 0)
  34. return value.s;
  35. if (value.b !== void 0)
  36. return value.b;
  37. if (value.v !== void 0) {
  38. if (value.v === "undefined")
  39. return void 0;
  40. if (value.v === "null")
  41. return null;
  42. if (value.v === "NaN")
  43. return NaN;
  44. if (value.v === "Infinity")
  45. return Infinity;
  46. if (value.v === "-Infinity")
  47. return -Infinity;
  48. if (value.v === "-0")
  49. return -0;
  50. }
  51. if (value.d !== void 0)
  52. return new Date(value.d);
  53. if (value.u !== void 0)
  54. return new URL(value.u);
  55. if (value.bi !== void 0)
  56. return BigInt(value.bi);
  57. if (value.e !== void 0) {
  58. const error = new Error(value.e.m);
  59. error.name = value.e.n;
  60. error.stack = value.e.s;
  61. return error;
  62. }
  63. if (value.r !== void 0)
  64. return new RegExp(value.r.p, value.r.f);
  65. if (value.ta !== void 0) {
  66. const ctor = typedArrayKindToConstructor[value.ta.k];
  67. return new ctor(value.ta.b.buffer, value.ta.b.byteOffset, value.ta.b.length / ctor.BYTES_PER_ELEMENT);
  68. }
  69. if (value.a !== void 0) {
  70. const result = [];
  71. refs.set(value.id, result);
  72. for (let i = 0; i < value.a.length; i++)
  73. result.push(innerParseSerializedValue(value.a[i], handles, refs, [...accessChain, i]));
  74. return result;
  75. }
  76. if (value.o !== void 0) {
  77. const result = {};
  78. refs.set(value.id, result);
  79. for (const { k, v } of value.o)
  80. result[k] = innerParseSerializedValue(v, handles, refs, [...accessChain, k]);
  81. return result;
  82. }
  83. if (value.h !== void 0) {
  84. if (handles === void 0)
  85. throw new Error("Unexpected handle");
  86. return handles[value.h];
  87. }
  88. throw new Error(`Attempting to deserialize unexpected value${accessChainToDisplayString(accessChain)}: ${value}`);
  89. }
  90. function serializeValue(value, handleSerializer) {
  91. return innerSerializeValue(value, handleSerializer, { lastId: 0, visited: /* @__PURE__ */ new Map() }, []);
  92. }
  93. function innerSerializeValue(value, handleSerializer, visitorInfo, accessChain) {
  94. const handle = handleSerializer(value);
  95. if ("fallThrough" in handle)
  96. value = handle.fallThrough;
  97. else
  98. return handle;
  99. if (typeof value === "symbol")
  100. return { v: "undefined" };
  101. if (Object.is(value, void 0))
  102. return { v: "undefined" };
  103. if (Object.is(value, null))
  104. return { v: "null" };
  105. if (Object.is(value, NaN))
  106. return { v: "NaN" };
  107. if (Object.is(value, Infinity))
  108. return { v: "Infinity" };
  109. if (Object.is(value, -Infinity))
  110. return { v: "-Infinity" };
  111. if (Object.is(value, -0))
  112. return { v: "-0" };
  113. if (typeof value === "boolean")
  114. return { b: value };
  115. if (typeof value === "number")
  116. return { n: value };
  117. if (typeof value === "string")
  118. return { s: value };
  119. if (typeof value === "bigint")
  120. return { bi: value.toString() };
  121. if (isError(value))
  122. return { e: { n: value.name, m: value.message, s: value.stack || "" } };
  123. if (isDate(value))
  124. return { d: value.toJSON() };
  125. if (isURL(value))
  126. return { u: value.toJSON() };
  127. if (isRegExp(value))
  128. return { r: { p: value.source, f: value.flags } };
  129. const typedArrayKind = constructorToTypedArrayKind.get(value.constructor);
  130. if (typedArrayKind)
  131. return { ta: { b: Buffer.from(value.buffer, value.byteOffset, value.byteLength), k: typedArrayKind } };
  132. const id = visitorInfo.visited.get(value);
  133. if (id)
  134. return { ref: id };
  135. if (Array.isArray(value)) {
  136. const a = [];
  137. const id2 = ++visitorInfo.lastId;
  138. visitorInfo.visited.set(value, id2);
  139. for (let i = 0; i < value.length; ++i)
  140. a.push(innerSerializeValue(value[i], handleSerializer, visitorInfo, [...accessChain, i]));
  141. return { a, id: id2 };
  142. }
  143. if (typeof value === "object") {
  144. const o = [];
  145. const id2 = ++visitorInfo.lastId;
  146. visitorInfo.visited.set(value, id2);
  147. for (const name of Object.keys(value))
  148. o.push({ k: name, v: innerSerializeValue(value[name], handleSerializer, visitorInfo, [...accessChain, name]) });
  149. return { o, id: id2 };
  150. }
  151. throw new Error(`Attempting to serialize unexpected value${accessChainToDisplayString(accessChain)}: ${value}`);
  152. }
  153. function accessChainToDisplayString(accessChain) {
  154. const chainString = accessChain.map((accessor, i) => {
  155. if (typeof accessor === "string")
  156. return i ? `.${accessor}` : accessor;
  157. return `[${accessor}]`;
  158. }).join("");
  159. return chainString.length > 0 ? ` at position "${chainString}"` : "";
  160. }
  161. function isRegExp(obj) {
  162. return obj instanceof RegExp || Object.prototype.toString.call(obj) === "[object RegExp]";
  163. }
  164. function isDate(obj) {
  165. return obj instanceof Date || Object.prototype.toString.call(obj) === "[object Date]";
  166. }
  167. function isURL(obj) {
  168. return obj instanceof URL || Object.prototype.toString.call(obj) === "[object URL]";
  169. }
  170. function isError(obj) {
  171. const proto = obj ? Object.getPrototypeOf(obj) : null;
  172. return obj instanceof Error || proto?.name === "Error" || proto && isError(proto);
  173. }
  174. const typedArrayKindToConstructor = {
  175. i8: Int8Array,
  176. ui8: Uint8Array,
  177. ui8c: Uint8ClampedArray,
  178. i16: Int16Array,
  179. ui16: Uint16Array,
  180. i32: Int32Array,
  181. ui32: Uint32Array,
  182. f32: Float32Array,
  183. f64: Float64Array,
  184. bi64: BigInt64Array,
  185. bui64: BigUint64Array
  186. };
  187. const constructorToTypedArrayKind = new Map(Object.entries(typedArrayKindToConstructor).map(([k, v]) => [v, k]));
  188. // Annotate the CommonJS export names for ESM import in node:
  189. 0 && (module.exports = {
  190. parseSerializedValue,
  191. serializeValue
  192. });