@discoveryjs/json-ext
Version:
A set of utilities that extend the use of JSON
707 lines (699 loc) • 21.8 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.jsonExt = factory());
}(typeof globalThis != 'undefined' ? globalThis : typeof window != 'undefined' ? window : typeof global != 'undefined' ? global : typeof self != 'undefined' ? self : this, (function () {
var exports = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.js
var src_exports = {};
__export(src_exports, {
createStringifyWebStream: () => createStringifyWebStream,
parseChunked: () => parseChunked,
parseFromWebStream: () => parseFromWebStream,
stringifyChunked: () => stringifyChunked,
stringifyInfo: () => stringifyInfo
});
// src/utils.js
function isIterable(value) {
return typeof value === "object" && value !== null && (typeof value[Symbol.iterator] === "function" || typeof value[Symbol.asyncIterator] === "function");
}
function replaceValue(holder, key, value, replacer) {
if (value && typeof value.toJSON === "function") {
value = value.toJSON();
}
if (replacer !== null) {
value = replacer.call(holder, String(key), value);
}
switch (typeof value) {
case "function":
case "symbol":
value = void 0;
break;
case "object":
if (value !== null) {
const cls = value.constructor;
if (cls === String || cls === Number || cls === Boolean) {
value = value.valueOf();
}
}
break;
}
return value;
}
function normalizeReplacer(replacer) {
if (typeof replacer === "function") {
return replacer;
}
if (Array.isArray(replacer)) {
const allowlist = new Set(
replacer.map((item) => {
const cls = item && item.constructor;
return cls === String || cls === Number ? String(item) : null;
}).filter((item) => typeof item === "string")
);
return [...allowlist];
}
return null;
}
function normalizeSpace(space) {
if (typeof space === "number") {
if (!Number.isFinite(space) || space < 1) {
return false;
}
return " ".repeat(Math.min(space, 10));
}
if (typeof space === "string") {
return space.slice(0, 10) || false;
}
return false;
}
// src/parse-chunked.js
var STACK_OBJECT = 1;
var STACK_ARRAY = 2;
var decoder = new TextDecoder();
function adjustPosition(error, parser) {
if (error.name === "SyntaxError" && parser.jsonParseOffset) {
error.message = error.message.replace(
/at position (\d+)/,
(_, pos) => "at position " + (Number(pos) + parser.jsonParseOffset)
);
}
return error;
}
function append(array, elements) {
const initialLength = array.length;
array.length += elements.length;
for (let i = 0; i < elements.length; i++) {
array[initialLength + i] = elements[i];
}
}
async function parseChunked(chunkEmitter) {
const iterable = typeof chunkEmitter === "function" ? chunkEmitter() : chunkEmitter;
if (isIterable(iterable)) {
let parser = new ChunkParser();
try {
for await (const chunk of iterable) {
if (typeof chunk !== "string" && !ArrayBuffer.isView(chunk)) {
throw new TypeError("Invalid chunk: Expected string, TypedArray or Buffer");
}
parser.push(chunk);
}
return parser.finish();
} catch (e) {
throw adjustPosition(e, parser);
}
}
throw new TypeError(
"Invalid chunk emitter: Expected an Iterable, AsyncIterable, generator, async generator, or a function returning an Iterable or AsyncIterable"
);
}
var ChunkParser = class {
constructor() {
this.value = void 0;
this.valueStack = null;
this.stack = new Array(100);
this.lastFlushDepth = 0;
this.flushDepth = 0;
this.stateString = false;
this.stateStringEscape = false;
this.pendingByteSeq = null;
this.pendingChunk = null;
this.chunkOffset = 0;
this.jsonParseOffset = 0;
}
parseAndAppend(fragment, wrap) {
if (this.stack[this.lastFlushDepth - 1] === STACK_OBJECT) {
if (wrap) {
this.jsonParseOffset--;
fragment = "{" + fragment + "}";
}
Object.assign(this.valueStack.value, JSON.parse(fragment));
} else {
if (wrap) {
this.jsonParseOffset--;
fragment = "[" + fragment + "]";
}
append(this.valueStack.value, JSON.parse(fragment));
}
}
prepareAddition(fragment) {
const { value } = this.valueStack;
const expectComma = Array.isArray(value) ? value.length !== 0 : Object.keys(value).length !== 0;
if (expectComma) {
if (fragment[0] === ",") {
this.jsonParseOffset++;
return fragment.slice(1);
}
if (fragment[0] !== "}" && fragment[0] !== "]") {
this.jsonParseOffset -= 3;
return "[[]" + fragment;
}
}
return fragment;
}
flush(chunk, start, end) {
let fragment = chunk.slice(start, end);
this.jsonParseOffset = this.chunkOffset + start;
if (this.pendingChunk !== null) {
fragment = this.pendingChunk + fragment;
this.jsonParseOffset -= this.pendingChunk.length;
this.pendingChunk = null;
}
if (this.flushDepth === this.lastFlushDepth) {
if (this.flushDepth > 0) {
this.parseAndAppend(this.prepareAddition(fragment), true);
} else {
this.value = JSON.parse(fragment);
this.valueStack = {
value: this.value,
prev: null
};
}
} else if (this.flushDepth > this.lastFlushDepth) {
for (let i = this.flushDepth - 1; i >= this.lastFlushDepth; i--) {
fragment += this.stack[i] === STACK_OBJECT ? "}" : "]";
}
if (this.lastFlushDepth === 0) {
this.value = JSON.parse(fragment);
this.valueStack = {
value: this.value,
prev: null
};
} else {
this.parseAndAppend(this.prepareAddition(fragment), true);
}
for (let i = this.lastFlushDepth || 1; i < this.flushDepth; i++) {
let value = this.valueStack.value;
if (this.stack[i - 1] === STACK_OBJECT) {
let key;
for (key in value) ;
value = value[key];
} else {
value = value[value.length - 1];
}
this.valueStack = {
value,
prev: this.valueStack
};
}
} else {
fragment = this.prepareAddition(fragment);
for (let i = this.lastFlushDepth - 1; i >= this.flushDepth; i--) {
this.jsonParseOffset--;
fragment = (this.stack[i] === STACK_OBJECT ? "{" : "[") + fragment;
}
this.parseAndAppend(fragment, false);
for (let i = this.lastFlushDepth - 1; i >= this.flushDepth; i--) {
this.valueStack = this.valueStack.prev;
}
}
this.lastFlushDepth = this.flushDepth;
}
push(chunk) {
if (typeof chunk !== "string") {
if (this.pendingByteSeq !== null) {
const origRawChunk = chunk;
chunk = new Uint8Array(this.pendingByteSeq.length + origRawChunk.length);
chunk.set(this.pendingByteSeq);
chunk.set(origRawChunk, this.pendingByteSeq.length);
this.pendingByteSeq = null;
}
if (chunk[chunk.length - 1] > 127) {
for (let seqLength = 0; seqLength < chunk.length; seqLength++) {
const byte = chunk[chunk.length - 1 - seqLength];
if (byte >> 6 === 3) {
seqLength++;
if (seqLength !== 4 && byte >> 3 === 30 || seqLength !== 3 && byte >> 4 === 14 || seqLength !== 2 && byte >> 5 === 6) {
this.pendingByteSeq = chunk.slice(chunk.length - seqLength);
chunk = chunk.slice(0, -seqLength);
}
break;
}
}
}
chunk = decoder.decode(chunk);
}
const chunkLength = chunk.length;
let lastFlushPoint = 0;
let flushPoint = 0;
scan: for (let i = 0; i < chunkLength; i++) {
if (this.stateString) {
for (; i < chunkLength; i++) {
if (this.stateStringEscape) {
this.stateStringEscape = false;
} else {
switch (chunk.charCodeAt(i)) {
case 34:
this.stateString = false;
continue scan;
case 92:
this.stateStringEscape = true;
}
}
}
break;
}
switch (chunk.charCodeAt(i)) {
case 34:
this.stateString = true;
this.stateStringEscape = false;
break;
case 44:
flushPoint = i;
break;
case 123:
flushPoint = i + 1;
this.stack[this.flushDepth++] = STACK_OBJECT;
break;
case 91:
flushPoint = i + 1;
this.stack[this.flushDepth++] = STACK_ARRAY;
break;
case 93:
case 125:
flushPoint = i + 1;
this.flushDepth--;
if (this.flushDepth < this.lastFlushDepth) {
this.flush(chunk, lastFlushPoint, flushPoint);
lastFlushPoint = flushPoint;
}
break;
case 9:
case 10:
case 13:
case 32:
if (lastFlushPoint === i) {
lastFlushPoint++;
}
if (flushPoint === i) {
flushPoint++;
}
break;
}
}
if (flushPoint > lastFlushPoint) {
this.flush(chunk, lastFlushPoint, flushPoint);
}
if (flushPoint < chunkLength) {
if (this.pendingChunk !== null) {
this.pendingChunk += chunk;
} else {
this.pendingChunk = chunk.slice(flushPoint, chunkLength);
}
}
this.chunkOffset += chunkLength;
}
finish() {
if (this.pendingChunk !== null) {
this.flush("", 0, 0);
this.pendingChunk = null;
}
return this.value;
}
};
// src/stringify-chunked.js
function encodeString(value) {
if (/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(value)) {
return JSON.stringify(value);
}
return '"' + value + '"';
}
function* stringifyChunked(value, optionsOrReplacer, space) {
if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== "object") {
optionsOrReplacer = {
replacer: optionsOrReplacer,
space
};
}
const highWaterMark = Number(optionsOrReplacer.highWaterMark) || 16384;
let replacer = normalizeReplacer(optionsOrReplacer.replacer);
space = normalizeSpace(optionsOrReplacer.space);
let buffer = "";
let depth = 0;
let stack = null;
let first = false;
let visited = /* @__PURE__ */ new WeakSet();
let processing = false;
let getKeys = Object.keys;
if (Array.isArray(replacer)) {
const allowlist = replacer;
getKeys = () => allowlist;
replacer = null;
}
pushStack(processRoot, value, null);
while (stack !== null) {
processing = true;
while (stack !== null && !stack.awaiting) {
stack.handler();
if (!processing) {
break;
}
}
processing = false;
yield buffer;
buffer = "";
}
function processRoot() {
const { value: value2 } = stack;
popStack();
processValue({ "": value2 }, "", value2, () => {
});
}
function processObjectEntry(key) {
if (first === false) {
first = true;
} else {
push(",");
}
if (space) {
push(`
${space.repeat(depth)}${encodeString(key)}: `);
} else {
push(encodeString(key) + ":");
}
}
function processObject() {
const current = stack;
if (current.index === current.keys.length) {
if (space && first) {
push(`
${space.repeat(depth - 1)}}`);
} else {
push("}");
}
popStack();
return;
}
const key = current.keys[current.index];
processValue(current.value, key, current.value[key], processObjectEntry);
current.index++;
}
function processArrayItem(index) {
if (index !== 0) {
push(",");
}
if (space) {
push(`
${space.repeat(depth)}`);
}
}
function processArray() {
const current = stack;
if (current.index === current.value.length) {
if (space && current.index !== 0) {
push(`
${space.repeat(depth - 1)}]`);
} else {
push("]");
}
popStack();
return;
}
processValue(current.value, current.index, current.value[current.index], processArrayItem);
current.index++;
}
function processValue(holder, key, value2, callback) {
value2 = replaceValue(holder, key, value2, replacer);
if (value2 === null || typeof value2 !== "object") {
if (callback !== processObjectEntry || value2 !== void 0) {
callback(key);
pushPrimitive(value2);
}
} else if (Array.isArray(value2)) {
callback(key);
circularCheck(value2);
depth++;
push("[");
pushStack(processArray, value2, null);
} else {
callback(key);
circularCheck(value2);
depth++;
push("{");
pushStack(processObject, value2, getKeys(value2));
}
}
function circularCheck(value2) {
if (visited.has(value2)) {
throw new TypeError("Converting circular structure to JSON");
}
visited.add(value2);
}
function pushPrimitive(value2) {
switch (typeof value2) {
case "string":
push(encodeString(value2));
break;
case "number":
push(Number.isFinite(value2) ? value2 : "null");
break;
case "boolean":
push(value2 ? "true" : "false");
break;
case "undefined":
case "object":
push("null");
break;
default:
throw new TypeError(`Do not know how to serialize a ${value2.constructor?.name || typeof value2}`);
}
}
function pushStack(handler, value2, keys) {
first = false;
return stack = {
handler,
value: value2,
index: 0,
keys,
prev: stack
};
}
function popStack() {
const { handler, value: value2 } = stack;
if (handler === processObject || handler === processArray) {
visited.delete(value2);
depth--;
}
stack = stack.prev;
first = true;
}
function push(data) {
buffer += data;
processing = buffer.length < highWaterMark;
}
}
// src/stringify-info.js
var hasOwn = typeof Object.hasOwn === "function" ? Object.hasOwn : (object, key) => Object.hasOwnProperty.call(object, key);
var escapableCharCodeSubstitution = {
// JSON Single Character Escape Sequences
8: "\\b",
9: "\\t",
10: "\\n",
12: "\\f",
13: "\\r",
34: '\\"',
92: "\\\\"
};
var charLength2048 = Array.from({ length: 2048 }).map((_, code) => {
if (hasOwn(escapableCharCodeSubstitution, code)) {
return 2;
}
if (code < 32) {
return 6;
}
return code < 128 ? 1 : 2;
});
function isLeadingSurrogate(code) {
return code >= 55296 && code <= 56319;
}
function isTrailingSurrogate(code) {
return code >= 56320 && code <= 57343;
}
function stringLength(str) {
let len = 0;
let prevLeadingSurrogate = false;
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (code < 2048) {
len += charLength2048[code];
} else if (isLeadingSurrogate(code)) {
len += 6;
prevLeadingSurrogate = true;
continue;
} else if (isTrailingSurrogate(code)) {
len = prevLeadingSurrogate ? len - 2 : len + 6;
} else {
len += 3;
}
prevLeadingSurrogate = false;
}
return len + 2;
}
function primitiveLength(value) {
switch (typeof value) {
case "string":
return stringLength(value);
case "number":
return Number.isFinite(value) ? String(value).length : 4;
case "boolean":
return value ? 4 : 5;
case "undefined":
case "object":
return 4;
default:
return 0;
}
}
function spaceLength(space) {
space = normalizeSpace(space);
return typeof space === "string" ? space.length : 0;
}
function stringifyInfo(value, optionsOrReplacer, space) {
if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== "object") {
optionsOrReplacer = {
replacer: optionsOrReplacer,
space
};
}
let allowlist = null;
let replacer = normalizeReplacer(optionsOrReplacer.replacer);
const continueOnCircular = Boolean(optionsOrReplacer.continueOnCircular);
if (Array.isArray(replacer)) {
allowlist = new Set(replacer);
replacer = null;
}
space = spaceLength(space);
const visited = /* @__PURE__ */ new WeakMap();
const stack = /* @__PURE__ */ new Set();
const circular = /* @__PURE__ */ new Set();
const root = { "": value };
let stop = false;
let bytes = 0;
walk(root, "", value);
return {
bytes: isNaN(bytes) ? Infinity : bytes,
circular: [...circular]
};
function walk(holder, key, value2) {
if (stop) {
return;
}
value2 = replaceValue(holder, key, value2, replacer);
if (value2 === null || typeof value2 !== "object") {
if (value2 !== void 0 || Array.isArray(holder)) {
bytes += primitiveLength(value2);
} else if (holder === root) {
bytes += 9;
}
} else {
if (stack.has(value2)) {
circular.add(value2);
bytes += 4;
if (!continueOnCircular) {
stop = true;
}
return;
}
if (visited.has(value2)) {
bytes += visited.get(value2);
return;
}
if (Array.isArray(value2)) {
const valueLength = bytes;
bytes += 2;
stack.add(value2);
for (let i = 0; i < value2.length; i++) {
walk(value2, i, value2[i]);
}
if (value2.length > 1) {
bytes += value2.length - 1;
}
stack.delete(value2);
if (space > 0 && value2.length > 0) {
bytes += (1 + (stack.size + 1) * space) * value2.length;
bytes += 1 + stack.size * space;
}
visited.set(value2, bytes - valueLength);
} else {
const valueLength = bytes;
let entries = 0;
bytes += 2;
stack.add(value2);
for (const key2 in value2) {
if (hasOwn(value2, key2) && (allowlist === null || allowlist.has(key2))) {
const prevLength = bytes;
walk(value2, key2, value2[key2]);
if (prevLength !== bytes) {
bytes += stringLength(key2) + 1;
entries++;
}
}
}
if (entries > 1) {
bytes += entries - 1;
}
stack.delete(value2);
if (space > 0 && entries > 0) {
bytes += (1 + (stack.size + 1) * space + 1) * entries;
bytes += 1 + stack.size * space;
}
visited.set(value2, bytes - valueLength);
}
}
}
}
// src/web-streams.js
function parseFromWebStream(stream) {
return parseChunked(isIterable(stream) ? stream : async function* () {
const reader = stream.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
yield value;
}
});
}
function createStringifyWebStream(value, replacer, space) {
if (typeof ReadableStream.from === "function") {
return ReadableStream.from(stringifyChunked(value, replacer, space));
}
return new ReadableStream({
start() {
this.generator = stringifyChunked(value, replacer, space);
},
pull(controller) {
const { value: value2, done } = this.generator.next();
if (done) {
controller.close();
} else {
controller.enqueue(value2);
}
},
cancel() {
this.generator = null;
}
});
}
return __toCommonJS(src_exports);
})();
return exports;
})));