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