json-ext.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.jsonExt = factory());
  5. }(typeof globalThis != 'undefined' ? globalThis : typeof window != 'undefined' ? window : typeof global != 'undefined' ? global : typeof self != 'undefined' ? self : this, (function () {
  6. var exports = (() => {
  7. var __defProp = Object.defineProperty;
  8. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  9. var __getOwnPropNames = Object.getOwnPropertyNames;
  10. var __hasOwnProp = Object.prototype.hasOwnProperty;
  11. var __export = (target, all) => {
  12. for (var name in all)
  13. __defProp(target, name, { get: all[name], enumerable: true });
  14. };
  15. var __copyProps = (to, from, except, desc) => {
  16. if (from && typeof from === "object" || typeof from === "function") {
  17. for (let key of __getOwnPropNames(from))
  18. if (!__hasOwnProp.call(to, key) && key !== except)
  19. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  20. }
  21. return to;
  22. };
  23. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  24. // src/index.js
  25. var src_exports = {};
  26. __export(src_exports, {
  27. createStringifyWebStream: () => createStringifyWebStream,
  28. parseChunked: () => parseChunked,
  29. parseFromWebStream: () => parseFromWebStream,
  30. stringifyChunked: () => stringifyChunked,
  31. stringifyInfo: () => stringifyInfo
  32. });
  33. // src/utils.js
  34. function isIterable(value) {
  35. return typeof value === "object" && value !== null && (typeof value[Symbol.iterator] === "function" || typeof value[Symbol.asyncIterator] === "function");
  36. }
  37. function replaceValue(holder, key, value, replacer) {
  38. if (value && typeof value.toJSON === "function") {
  39. value = value.toJSON();
  40. }
  41. if (replacer !== null) {
  42. value = replacer.call(holder, String(key), value);
  43. }
  44. switch (typeof value) {
  45. case "function":
  46. case "symbol":
  47. value = void 0;
  48. break;
  49. case "object":
  50. if (value !== null) {
  51. const cls = value.constructor;
  52. if (cls === String || cls === Number || cls === Boolean) {
  53. value = value.valueOf();
  54. }
  55. }
  56. break;
  57. }
  58. return value;
  59. }
  60. function normalizeReplacer(replacer) {
  61. if (typeof replacer === "function") {
  62. return replacer;
  63. }
  64. if (Array.isArray(replacer)) {
  65. const allowlist = new Set(
  66. replacer.map((item) => {
  67. const cls = item && item.constructor;
  68. return cls === String || cls === Number ? String(item) : null;
  69. }).filter((item) => typeof item === "string")
  70. );
  71. return [...allowlist];
  72. }
  73. return null;
  74. }
  75. function normalizeSpace(space) {
  76. if (typeof space === "number") {
  77. if (!Number.isFinite(space) || space < 1) {
  78. return false;
  79. }
  80. return " ".repeat(Math.min(space, 10));
  81. }
  82. if (typeof space === "string") {
  83. return space.slice(0, 10) || false;
  84. }
  85. return false;
  86. }
  87. function normalizeStringifyOptions(optionsOrReplacer, space) {
  88. if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== "object") {
  89. optionsOrReplacer = {
  90. replacer: optionsOrReplacer,
  91. space
  92. };
  93. }
  94. let replacer = normalizeReplacer(optionsOrReplacer.replacer);
  95. let getKeys = Object.keys;
  96. if (Array.isArray(replacer)) {
  97. const allowlist = replacer;
  98. getKeys = () => allowlist;
  99. replacer = null;
  100. }
  101. return {
  102. ...optionsOrReplacer,
  103. replacer,
  104. getKeys,
  105. space: normalizeSpace(optionsOrReplacer.space)
  106. };
  107. }
  108. // src/parse-chunked.js
  109. var STACK_OBJECT = 1;
  110. var STACK_ARRAY = 2;
  111. var decoder = new TextDecoder();
  112. function adjustPosition(error, parser) {
  113. if (error.name === "SyntaxError" && parser.jsonParseOffset) {
  114. error.message = error.message.replace(
  115. /at position (\d+)/,
  116. (_, pos) => "at position " + (Number(pos) + parser.jsonParseOffset)
  117. );
  118. }
  119. return error;
  120. }
  121. function append(array, elements) {
  122. const initialLength = array.length;
  123. array.length += elements.length;
  124. for (let i = 0; i < elements.length; i++) {
  125. array[initialLength + i] = elements[i];
  126. }
  127. }
  128. async function parseChunked(chunkEmitter) {
  129. const iterable = typeof chunkEmitter === "function" ? chunkEmitter() : chunkEmitter;
  130. if (isIterable(iterable)) {
  131. let parser = new ChunkParser();
  132. try {
  133. for await (const chunk of iterable) {
  134. if (typeof chunk !== "string" && !ArrayBuffer.isView(chunk)) {
  135. throw new TypeError("Invalid chunk: Expected string, TypedArray or Buffer");
  136. }
  137. parser.push(chunk);
  138. }
  139. return parser.finish();
  140. } catch (e) {
  141. throw adjustPosition(e, parser);
  142. }
  143. }
  144. throw new TypeError(
  145. "Invalid chunk emitter: Expected an Iterable, AsyncIterable, generator, async generator, or a function returning an Iterable or AsyncIterable"
  146. );
  147. }
  148. var ChunkParser = class {
  149. constructor() {
  150. this.value = void 0;
  151. this.valueStack = null;
  152. this.stack = new Array(100);
  153. this.lastFlushDepth = 0;
  154. this.flushDepth = 0;
  155. this.stateString = false;
  156. this.stateStringEscape = false;
  157. this.pendingByteSeq = null;
  158. this.pendingChunk = null;
  159. this.chunkOffset = 0;
  160. this.jsonParseOffset = 0;
  161. }
  162. parseAndAppend(fragment, wrap) {
  163. if (this.stack[this.lastFlushDepth - 1] === STACK_OBJECT) {
  164. if (wrap) {
  165. this.jsonParseOffset--;
  166. fragment = "{" + fragment + "}";
  167. }
  168. Object.assign(this.valueStack.value, JSON.parse(fragment));
  169. } else {
  170. if (wrap) {
  171. this.jsonParseOffset--;
  172. fragment = "[" + fragment + "]";
  173. }
  174. append(this.valueStack.value, JSON.parse(fragment));
  175. }
  176. }
  177. prepareAddition(fragment) {
  178. const { value } = this.valueStack;
  179. const expectComma = Array.isArray(value) ? value.length !== 0 : Object.keys(value).length !== 0;
  180. if (expectComma) {
  181. if (fragment[0] === ",") {
  182. this.jsonParseOffset++;
  183. return fragment.slice(1);
  184. }
  185. if (fragment[0] !== "}" && fragment[0] !== "]") {
  186. this.jsonParseOffset -= 3;
  187. return "[[]" + fragment;
  188. }
  189. }
  190. return fragment;
  191. }
  192. flush(chunk, start, end) {
  193. let fragment = chunk.slice(start, end);
  194. this.jsonParseOffset = this.chunkOffset + start;
  195. if (this.pendingChunk !== null) {
  196. fragment = this.pendingChunk + fragment;
  197. this.jsonParseOffset -= this.pendingChunk.length;
  198. this.pendingChunk = null;
  199. }
  200. if (this.flushDepth === this.lastFlushDepth) {
  201. if (this.flushDepth > 0) {
  202. this.parseAndAppend(this.prepareAddition(fragment), true);
  203. } else {
  204. this.value = JSON.parse(fragment);
  205. this.valueStack = {
  206. value: this.value,
  207. prev: null
  208. };
  209. }
  210. } else if (this.flushDepth > this.lastFlushDepth) {
  211. for (let i = this.flushDepth - 1; i >= this.lastFlushDepth; i--) {
  212. fragment += this.stack[i] === STACK_OBJECT ? "}" : "]";
  213. }
  214. if (this.lastFlushDepth === 0) {
  215. this.value = JSON.parse(fragment);
  216. this.valueStack = {
  217. value: this.value,
  218. prev: null
  219. };
  220. } else {
  221. this.parseAndAppend(this.prepareAddition(fragment), true);
  222. }
  223. for (let i = this.lastFlushDepth || 1; i < this.flushDepth; i++) {
  224. let value = this.valueStack.value;
  225. if (this.stack[i - 1] === STACK_OBJECT) {
  226. let key;
  227. for (key in value) ;
  228. value = value[key];
  229. } else {
  230. value = value[value.length - 1];
  231. }
  232. this.valueStack = {
  233. value,
  234. prev: this.valueStack
  235. };
  236. }
  237. } else {
  238. fragment = this.prepareAddition(fragment);
  239. for (let i = this.lastFlushDepth - 1; i >= this.flushDepth; i--) {
  240. this.jsonParseOffset--;
  241. fragment = (this.stack[i] === STACK_OBJECT ? "{" : "[") + fragment;
  242. }
  243. this.parseAndAppend(fragment, false);
  244. for (let i = this.lastFlushDepth - 1; i >= this.flushDepth; i--) {
  245. this.valueStack = this.valueStack.prev;
  246. }
  247. }
  248. this.lastFlushDepth = this.flushDepth;
  249. }
  250. push(chunk) {
  251. if (typeof chunk !== "string") {
  252. if (this.pendingByteSeq !== null) {
  253. const origRawChunk = chunk;
  254. chunk = new Uint8Array(this.pendingByteSeq.length + origRawChunk.length);
  255. chunk.set(this.pendingByteSeq);
  256. chunk.set(origRawChunk, this.pendingByteSeq.length);
  257. this.pendingByteSeq = null;
  258. }
  259. if (chunk[chunk.length - 1] > 127) {
  260. for (let seqLength = 0; seqLength < chunk.length; seqLength++) {
  261. const byte = chunk[chunk.length - 1 - seqLength];
  262. if (byte >> 6 === 3) {
  263. seqLength++;
  264. if (seqLength !== 4 && byte >> 3 === 30 || seqLength !== 3 && byte >> 4 === 14 || seqLength !== 2 && byte >> 5 === 6) {
  265. this.pendingByteSeq = chunk.slice(chunk.length - seqLength);
  266. chunk = chunk.slice(0, -seqLength);
  267. }
  268. break;
  269. }
  270. }
  271. }
  272. chunk = decoder.decode(chunk);
  273. }
  274. const chunkLength = chunk.length;
  275. let lastFlushPoint = 0;
  276. let flushPoint = 0;
  277. scan: for (let i = 0; i < chunkLength; i++) {
  278. if (this.stateString) {
  279. for (; i < chunkLength; i++) {
  280. if (this.stateStringEscape) {
  281. this.stateStringEscape = false;
  282. } else {
  283. switch (chunk.charCodeAt(i)) {
  284. case 34:
  285. this.stateString = false;
  286. continue scan;
  287. case 92:
  288. this.stateStringEscape = true;
  289. }
  290. }
  291. }
  292. break;
  293. }
  294. switch (chunk.charCodeAt(i)) {
  295. case 34:
  296. this.stateString = true;
  297. this.stateStringEscape = false;
  298. break;
  299. case 44:
  300. flushPoint = i;
  301. break;
  302. case 123:
  303. flushPoint = i + 1;
  304. this.stack[this.flushDepth++] = STACK_OBJECT;
  305. break;
  306. case 91:
  307. flushPoint = i + 1;
  308. this.stack[this.flushDepth++] = STACK_ARRAY;
  309. break;
  310. case 93:
  311. /* ] */
  312. case 125:
  313. flushPoint = i + 1;
  314. this.flushDepth--;
  315. if (this.flushDepth < this.lastFlushDepth) {
  316. this.flush(chunk, lastFlushPoint, flushPoint);
  317. lastFlushPoint = flushPoint;
  318. }
  319. break;
  320. case 9:
  321. /* \t */
  322. case 10:
  323. /* \n */
  324. case 13:
  325. /* \r */
  326. case 32:
  327. if (lastFlushPoint === i) {
  328. lastFlushPoint++;
  329. }
  330. if (flushPoint === i) {
  331. flushPoint++;
  332. }
  333. break;
  334. }
  335. }
  336. if (flushPoint > lastFlushPoint) {
  337. this.flush(chunk, lastFlushPoint, flushPoint);
  338. }
  339. if (flushPoint < chunkLength) {
  340. if (this.pendingChunk !== null) {
  341. this.pendingChunk += chunk;
  342. } else {
  343. this.pendingChunk = chunk.slice(flushPoint, chunkLength);
  344. }
  345. }
  346. this.chunkOffset += chunkLength;
  347. }
  348. finish() {
  349. if (this.pendingChunk !== null) {
  350. this.flush("", 0, 0);
  351. this.pendingChunk = null;
  352. }
  353. return this.value;
  354. }
  355. };
  356. // src/stringify-chunked.js
  357. function encodeString(value) {
  358. if (/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(value)) {
  359. return JSON.stringify(value);
  360. }
  361. return '"' + value + '"';
  362. }
  363. function* stringifyChunked(value, ...args) {
  364. const { replacer, getKeys, space, ...options } = normalizeStringifyOptions(...args);
  365. const highWaterMark = Number(options.highWaterMark) || 16384;
  366. const keyStrings = /* @__PURE__ */ new Map();
  367. const stack = [];
  368. const rootValue = { "": value };
  369. let prevState = null;
  370. let state = () => printEntry("", value);
  371. let stateValue = rootValue;
  372. let stateEmpty = true;
  373. let stateKeys = [""];
  374. let stateIndex = 0;
  375. let buffer = "";
  376. while (true) {
  377. state();
  378. if (buffer.length >= highWaterMark || prevState === null) {
  379. yield buffer;
  380. buffer = "";
  381. if (prevState === null) {
  382. break;
  383. }
  384. }
  385. }
  386. function printObject() {
  387. if (stateIndex === 0) {
  388. stateKeys = getKeys(stateValue);
  389. buffer += "{";
  390. }
  391. if (stateIndex === stateKeys.length) {
  392. buffer += space && !stateEmpty ? `
  393. ${space.repeat(stack.length - 1)}}` : "}";
  394. popState();
  395. return;
  396. }
  397. const key = stateKeys[stateIndex++];
  398. printEntry(key, stateValue[key]);
  399. }
  400. function printArray() {
  401. if (stateIndex === 0) {
  402. buffer += "[";
  403. }
  404. if (stateIndex === stateValue.length) {
  405. buffer += space && !stateEmpty ? `
  406. ${space.repeat(stack.length - 1)}]` : "]";
  407. popState();
  408. return;
  409. }
  410. printEntry(stateIndex, stateValue[stateIndex++]);
  411. }
  412. function printEntryPrelude(key) {
  413. if (stateEmpty) {
  414. stateEmpty = false;
  415. } else {
  416. buffer += ",";
  417. }
  418. if (space && prevState !== null) {
  419. buffer += `
  420. ${space.repeat(stack.length)}`;
  421. }
  422. if (state === printObject) {
  423. let keyString = keyStrings.get(key);
  424. if (keyString === void 0) {
  425. keyStrings.set(key, keyString = encodeString(key) + (space ? ": " : ":"));
  426. }
  427. buffer += keyString;
  428. }
  429. }
  430. function printEntry(key, value2) {
  431. value2 = replaceValue(stateValue, key, value2, replacer);
  432. if (value2 === null || typeof value2 !== "object") {
  433. if (state !== printObject || value2 !== void 0) {
  434. printEntryPrelude(key);
  435. pushPrimitive(value2);
  436. }
  437. } else {
  438. if (stack.includes(value2)) {
  439. throw new TypeError("Converting circular structure to JSON");
  440. }
  441. printEntryPrelude(key);
  442. stack.push(value2);
  443. pushState();
  444. state = Array.isArray(value2) ? printArray : printObject;
  445. stateValue = value2;
  446. stateEmpty = true;
  447. stateIndex = 0;
  448. }
  449. }
  450. function pushPrimitive(value2) {
  451. switch (typeof value2) {
  452. case "string":
  453. buffer += encodeString(value2);
  454. break;
  455. case "number":
  456. buffer += Number.isFinite(value2) ? String(value2) : "null";
  457. break;
  458. case "boolean":
  459. buffer += value2 ? "true" : "false";
  460. break;
  461. case "undefined":
  462. case "object":
  463. buffer += "null";
  464. break;
  465. default:
  466. throw new TypeError(`Do not know how to serialize a ${value2.constructor?.name || typeof value2}`);
  467. }
  468. }
  469. function pushState() {
  470. prevState = {
  471. keys: stateKeys,
  472. index: stateIndex,
  473. prev: prevState
  474. };
  475. }
  476. function popState() {
  477. stack.pop();
  478. const value2 = stack.length > 0 ? stack[stack.length - 1] : rootValue;
  479. state = Array.isArray(value2) ? printArray : printObject;
  480. stateValue = value2;
  481. stateEmpty = false;
  482. stateKeys = prevState.keys;
  483. stateIndex = prevState.index;
  484. prevState = prevState.prev;
  485. }
  486. }
  487. // src/stringify-info.js
  488. var hasOwn = typeof Object.hasOwn === "function" ? Object.hasOwn : (object, key) => Object.hasOwnProperty.call(object, key);
  489. var escapableCharCodeSubstitution = {
  490. // JSON Single Character Escape Sequences
  491. 8: "\\b",
  492. 9: "\\t",
  493. 10: "\\n",
  494. 12: "\\f",
  495. 13: "\\r",
  496. 34: '\\"',
  497. 92: "\\\\"
  498. };
  499. var charLength2048 = Uint8Array.from({ length: 2048 }, (_, code) => {
  500. if (hasOwn(escapableCharCodeSubstitution, code)) {
  501. return 2;
  502. }
  503. if (code < 32) {
  504. return 6;
  505. }
  506. return code < 128 ? 1 : 2;
  507. });
  508. function isLeadingSurrogate(code) {
  509. return code >= 55296 && code <= 56319;
  510. }
  511. function isTrailingSurrogate(code) {
  512. return code >= 56320 && code <= 57343;
  513. }
  514. function stringLength(str) {
  515. if (!/[^\x20\x21\x23-\x5B\x5D-\x7F]/.test(str)) {
  516. return str.length + 2;
  517. }
  518. let len = 0;
  519. let prevLeadingSurrogate = false;
  520. for (let i = 0; i < str.length; i++) {
  521. const code = str.charCodeAt(i);
  522. if (code < 2048) {
  523. len += charLength2048[code];
  524. } else if (isLeadingSurrogate(code)) {
  525. len += 6;
  526. prevLeadingSurrogate = true;
  527. continue;
  528. } else if (isTrailingSurrogate(code)) {
  529. len = prevLeadingSurrogate ? len - 2 : len + 6;
  530. } else {
  531. len += 3;
  532. }
  533. prevLeadingSurrogate = false;
  534. }
  535. return len + 2;
  536. }
  537. function intLength(num) {
  538. let len = 0;
  539. if (num < 0) {
  540. len = 1;
  541. num = -num;
  542. }
  543. if (num >= 1e9) {
  544. len += 9;
  545. num = (num - num % 1e9) / 1e9;
  546. }
  547. if (num >= 1e4) {
  548. if (num >= 1e6) {
  549. return len + (num >= 1e8 ? 9 : num >= 1e7 ? 8 : 7);
  550. }
  551. return len + (num >= 1e5 ? 6 : 5);
  552. }
  553. return len + (num >= 100 ? num >= 1e3 ? 4 : 3 : num >= 10 ? 2 : 1);
  554. }
  555. function primitiveLength(value) {
  556. switch (typeof value) {
  557. case "string":
  558. return stringLength(value);
  559. case "number":
  560. return Number.isFinite(value) ? Number.isInteger(value) ? intLength(value) : String(value).length : 4;
  561. case "boolean":
  562. return value ? 4 : 5;
  563. case "undefined":
  564. case "object":
  565. return 4;
  566. /* null */
  567. default:
  568. return 0;
  569. }
  570. }
  571. function stringifyInfo(value, ...args) {
  572. const { replacer, getKeys, ...options } = normalizeStringifyOptions(...args);
  573. const continueOnCircular = Boolean(options.continueOnCircular);
  574. const space = options.space?.length || 0;
  575. const keysLength = /* @__PURE__ */ new Map();
  576. const visited = /* @__PURE__ */ new Map();
  577. const circular = /* @__PURE__ */ new Set();
  578. const stack = [];
  579. const root = { "": value };
  580. let stop = false;
  581. let bytes = 0;
  582. let spaceBytes = 0;
  583. let objects = 0;
  584. walk(root, "", value);
  585. if (bytes === 0) {
  586. bytes += 9;
  587. }
  588. return {
  589. bytes: isNaN(bytes) ? Infinity : bytes + spaceBytes,
  590. spaceBytes: space > 0 && isNaN(bytes) ? Infinity : spaceBytes,
  591. circular: [...circular]
  592. };
  593. function walk(holder, key, value2) {
  594. if (stop) {
  595. return;
  596. }
  597. value2 = replaceValue(holder, key, value2, replacer);
  598. if (value2 === null || typeof value2 !== "object") {
  599. if (value2 !== void 0 || Array.isArray(holder)) {
  600. bytes += primitiveLength(value2);
  601. }
  602. } else {
  603. if (stack.includes(value2)) {
  604. circular.add(value2);
  605. bytes += 4;
  606. if (!continueOnCircular) {
  607. stop = true;
  608. }
  609. return;
  610. }
  611. if (visited.has(value2)) {
  612. bytes += visited.get(value2);
  613. return;
  614. }
  615. objects++;
  616. const prevObjects = objects;
  617. const valueBytes = bytes;
  618. let valueLength = 0;
  619. stack.push(value2);
  620. if (Array.isArray(value2)) {
  621. valueLength = value2.length;
  622. for (let i = 0; i < valueLength; i++) {
  623. walk(value2, i, value2[i]);
  624. }
  625. } else {
  626. let prevLength = bytes;
  627. for (const key2 of getKeys(value2)) {
  628. walk(value2, key2, value2[key2]);
  629. if (prevLength !== bytes) {
  630. let keyLen = keysLength.get(key2);
  631. if (keyLen === void 0) {
  632. keysLength.set(key2, keyLen = stringLength(key2) + 1);
  633. }
  634. bytes += keyLen;
  635. valueLength++;
  636. prevLength = bytes;
  637. }
  638. }
  639. }
  640. bytes += valueLength === 0 ? 2 : 1 + valueLength;
  641. if (space > 0 && valueLength > 0) {
  642. spaceBytes += // a space between ":" and a value for each object entry
  643. (Array.isArray(value2) ? 0 : valueLength) + // the formula results from folding the following components:
  644. // - for each key-value or element: ident + newline
  645. // (1 + stack.length * space) * valueLength
  646. // - ident (one space less) before "}" or "]" + newline
  647. // (stack.length - 1) * space + 1
  648. (1 + stack.length * space) * (valueLength + 1) - space;
  649. }
  650. stack.pop();
  651. if (prevObjects !== objects) {
  652. visited.set(value2, bytes - valueBytes);
  653. }
  654. }
  655. }
  656. }
  657. // src/web-streams.js
  658. function parseFromWebStream(stream) {
  659. return parseChunked(isIterable(stream) ? stream : async function* () {
  660. const reader = stream.getReader();
  661. try {
  662. while (true) {
  663. const { value, done } = await reader.read();
  664. if (done) {
  665. break;
  666. }
  667. yield value;
  668. }
  669. } finally {
  670. reader.releaseLock();
  671. }
  672. });
  673. }
  674. function createStringifyWebStream(value, replacer, space) {
  675. if (typeof ReadableStream.from === "function") {
  676. return ReadableStream.from(stringifyChunked(value, replacer, space));
  677. }
  678. return new ReadableStream({
  679. start() {
  680. this.generator = stringifyChunked(value, replacer, space);
  681. },
  682. pull(controller) {
  683. const { value: value2, done } = this.generator.next();
  684. if (done) {
  685. controller.close();
  686. } else {
  687. controller.enqueue(value2);
  688. }
  689. },
  690. cancel() {
  691. this.generator = null;
  692. }
  693. });
  694. }
  695. return __toCommonJS(src_exports);
  696. })();
  697. return exports;
  698. })));