structured-clone.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. // src/primitives/structured-clone.js
  21. var structured_clone_exports = {};
  22. __export(structured_clone_exports, {
  23. structuredClone: () => esm_default
  24. });
  25. module.exports = __toCommonJS(structured_clone_exports);
  26. // ../../node_modules/.pnpm/@ungap+structured-clone@1.0.1/node_modules/@ungap/structured-clone/esm/types.js
  27. var VOID = -1;
  28. var PRIMITIVE = 0;
  29. var ARRAY = 1;
  30. var OBJECT = 2;
  31. var DATE = 3;
  32. var REGEXP = 4;
  33. var MAP = 5;
  34. var SET = 6;
  35. var ERROR = 7;
  36. var BIGINT = 8;
  37. // ../../node_modules/.pnpm/@ungap+structured-clone@1.0.1/node_modules/@ungap/structured-clone/esm/deserialize.js
  38. var env = typeof self === "object" ? self : globalThis;
  39. var deserializer = /* @__PURE__ */ __name(($, _) => {
  40. const as = /* @__PURE__ */ __name((out, index) => {
  41. $.set(index, out);
  42. return out;
  43. }, "as");
  44. const unpair = /* @__PURE__ */ __name((index) => {
  45. if ($.has(index))
  46. return $.get(index);
  47. const [type, value] = _[index];
  48. switch (type) {
  49. case PRIMITIVE:
  50. case VOID:
  51. return as(value, index);
  52. case ARRAY: {
  53. const arr = as([], index);
  54. for (const index2 of value)
  55. arr.push(unpair(index2));
  56. return arr;
  57. }
  58. case OBJECT: {
  59. const object = as({}, index);
  60. for (const [key, index2] of value)
  61. object[unpair(key)] = unpair(index2);
  62. return object;
  63. }
  64. case DATE:
  65. return as(new Date(value), index);
  66. case REGEXP: {
  67. const { source, flags } = value;
  68. return as(new RegExp(source, flags), index);
  69. }
  70. case MAP: {
  71. const map = as(/* @__PURE__ */ new Map(), index);
  72. for (const [key, index2] of value)
  73. map.set(unpair(key), unpair(index2));
  74. return map;
  75. }
  76. case SET: {
  77. const set = as(/* @__PURE__ */ new Set(), index);
  78. for (const index2 of value)
  79. set.add(unpair(index2));
  80. return set;
  81. }
  82. case ERROR: {
  83. const { name, message } = value;
  84. return as(new env[name](message), index);
  85. }
  86. case BIGINT:
  87. return as(BigInt(value), index);
  88. case "BigInt":
  89. return as(Object(BigInt(value)), index);
  90. }
  91. return as(new env[type](value), index);
  92. }, "unpair");
  93. return unpair;
  94. }, "deserializer");
  95. var deserialize = /* @__PURE__ */ __name((serialized) => deserializer(/* @__PURE__ */ new Map(), serialized)(0), "deserialize");
  96. // ../../node_modules/.pnpm/@ungap+structured-clone@1.0.1/node_modules/@ungap/structured-clone/esm/serialize.js
  97. var EMPTY = "";
  98. var { toString } = {};
  99. var { keys } = Object;
  100. var typeOf = /* @__PURE__ */ __name((value) => {
  101. const type = typeof value;
  102. if (type !== "object" || !value)
  103. return [PRIMITIVE, type];
  104. const asString = toString.call(value).slice(8, -1);
  105. switch (asString) {
  106. case "Array":
  107. return [ARRAY, EMPTY];
  108. case "Object":
  109. return [OBJECT, EMPTY];
  110. case "Date":
  111. return [DATE, EMPTY];
  112. case "RegExp":
  113. return [REGEXP, EMPTY];
  114. case "Map":
  115. return [MAP, EMPTY];
  116. case "Set":
  117. return [SET, EMPTY];
  118. }
  119. if (asString.includes("Array"))
  120. return [ARRAY, asString];
  121. if (asString.includes("Error"))
  122. return [ERROR, asString];
  123. return [OBJECT, asString];
  124. }, "typeOf");
  125. var shouldSkip = /* @__PURE__ */ __name(([TYPE, type]) => TYPE === PRIMITIVE && (type === "function" || type === "symbol"), "shouldSkip");
  126. var serializer = /* @__PURE__ */ __name((strict, json, $, _) => {
  127. const as = /* @__PURE__ */ __name((out, value) => {
  128. const index = _.push(out) - 1;
  129. $.set(value, index);
  130. return index;
  131. }, "as");
  132. const pair = /* @__PURE__ */ __name((value) => {
  133. if ($.has(value))
  134. return $.get(value);
  135. let [TYPE, type] = typeOf(value);
  136. switch (TYPE) {
  137. case PRIMITIVE: {
  138. let entry = value;
  139. switch (type) {
  140. case "bigint":
  141. TYPE = BIGINT;
  142. entry = value.toString();
  143. break;
  144. case "function":
  145. case "symbol":
  146. if (strict)
  147. throw new TypeError("unable to serialize " + type);
  148. entry = null;
  149. break;
  150. case "undefined":
  151. return as([VOID], value);
  152. }
  153. return as([TYPE, entry], value);
  154. }
  155. case ARRAY: {
  156. if (type)
  157. return as([type, [...value]], value);
  158. const arr = [];
  159. const index = as([TYPE, arr], value);
  160. for (const entry of value)
  161. arr.push(pair(entry));
  162. return index;
  163. }
  164. case OBJECT: {
  165. if (type) {
  166. switch (type) {
  167. case "BigInt":
  168. return as([type, value.toString()], value);
  169. case "Boolean":
  170. case "Number":
  171. case "String":
  172. return as([type, value.valueOf()], value);
  173. }
  174. }
  175. if (json && "toJSON" in value)
  176. return pair(value.toJSON());
  177. const entries = [];
  178. const index = as([TYPE, entries], value);
  179. for (const key of keys(value)) {
  180. if (strict || !shouldSkip(typeOf(value[key])))
  181. entries.push([pair(key), pair(value[key])]);
  182. }
  183. return index;
  184. }
  185. case DATE:
  186. return as([TYPE, value.toISOString()], value);
  187. case REGEXP: {
  188. const { source, flags } = value;
  189. return as([TYPE, { source, flags }], value);
  190. }
  191. case MAP: {
  192. const entries = [];
  193. const index = as([TYPE, entries], value);
  194. for (const [key, entry] of value) {
  195. if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
  196. entries.push([pair(key), pair(entry)]);
  197. }
  198. return index;
  199. }
  200. case SET: {
  201. const entries = [];
  202. const index = as([TYPE, entries], value);
  203. for (const entry of value) {
  204. if (strict || !shouldSkip(typeOf(entry)))
  205. entries.push(pair(entry));
  206. }
  207. return index;
  208. }
  209. }
  210. const { message } = value;
  211. return as([TYPE, { name: type, message }], value);
  212. }, "pair");
  213. return pair;
  214. }, "serializer");
  215. var serialize = /* @__PURE__ */ __name((value, { json, lossy } = {}) => {
  216. const _ = [];
  217. return serializer(!(json || lossy), !!json, /* @__PURE__ */ new Map(), _)(value), _;
  218. }, "serialize");
  219. // ../../node_modules/.pnpm/@ungap+structured-clone@1.0.1/node_modules/@ungap/structured-clone/esm/index.js
  220. var esm_default = typeof structuredClone === "function" ? (any, options) => options && ("json" in options || "lossy" in options) ? deserialize(serialize(any, options)) : structuredClone(any) : (any, options) => deserialize(serialize(any, options));
  221. // Annotate the CommonJS export names for ESM import in node:
  222. 0 && (module.exports = {
  223. structuredClone
  224. });