UNPKG

@rspack/cli

Version:
211 lines (210 loc) • 7.18 kB
Symbol('empty'); new TextDecoder(); function utils_replaceValue(holder, key, value, replacer) { if (value && 'function' == typeof value.toJSON) value = value.toJSON(); if (null !== replacer) value = replacer.call(holder, String(key), value); switch(typeof value){ case 'function': case 'symbol': value = void 0; break; case 'object': if (null !== value) { const cls = value.constructor; if (cls === String || cls === Number || cls === Boolean) value = value.valueOf(); } break; } return value; } function normalizeReplacer(replacer) { if ('function' == typeof replacer) 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)=>'string' == typeof item)); return [ ...allowlist ]; } return null; } function normalizeSpace(space) { if ('number' == typeof space) { if (!Number.isFinite(space) || space < 1) return false; return ' '.repeat(Math.min(space, 10)); } if ('string' == typeof space) return space.slice(0, 10) || false; return false; } function utils_normalizeStringifyOptions(optionsOrReplacer, space) { if (null === optionsOrReplacer || Array.isArray(optionsOrReplacer) || 'object' != typeof optionsOrReplacer) optionsOrReplacer = { replacer: optionsOrReplacer, space }; let replacer = normalizeReplacer(optionsOrReplacer.replacer); let getKeys = Object.keys; if (Array.isArray(replacer)) { const allowlist = replacer; getKeys = ()=>allowlist; replacer = null; } return { ...optionsOrReplacer, replacer, getKeys, space: normalizeSpace(optionsOrReplacer.space) }; } function utils_resolveStringifyMode(mode = 'json') { if ('json' === mode || 'jsonl' === mode) return mode; throw new TypeError('Invalid options: `mode` should be "json" or "jsonl"'); } function encodeString(value) { if (/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(value)) return JSON.stringify(value); return '"' + value + '"'; } function* stringify_chunked_stringifyChunked(value, ...args) { const { replacer, getKeys, space, ...options } = utils_normalizeStringifyOptions(...args); const highWaterMark = Number(options.highWaterMark) || 0x4000; const roots = 'jsonl' === utils_resolveStringifyMode(options.mode) && Array.isArray(value) ? value : [ value ]; const rootCount = roots.length; const keyStrings = new Map(); const stack = []; let rootValue = null; let prevState = null; let state = null; let stateValue = null; let stateEmpty = true; let stateKeys = []; let stateIndex = 0; let buffer = ''; for(let i = 0; i < rootCount; i++){ if (null !== rootValue) buffer += '\n'; rootValue = { '': roots[i] }; prevState = null; state = ()=>printEntry('', roots[i]); stateValue = rootValue; stateEmpty = true; stateKeys = [ '' ]; stateIndex = 0; do { state(); if (buffer.length >= highWaterMark || null === prevState && i === rootCount - 1) { yield buffer; buffer = ''; } }while (null !== prevState); } function printObject() { if (0 === stateIndex) { stateKeys = getKeys(stateValue); buffer += '{'; } if (stateIndex === stateKeys.length) { buffer += space && !stateEmpty ? `\n${space.repeat(stack.length - 1)}}` : '}'; popState(); return; } const key = stateKeys[stateIndex++]; printEntry(key, stateValue[key]); } function printArray() { if (0 === stateIndex) buffer += '['; if (stateIndex === stateValue.length) { buffer += space && !stateEmpty ? `\n${space.repeat(stack.length - 1)}]` : ']'; popState(); return; } printEntry(stateIndex, stateValue[stateIndex++]); } function printEntryPrelude(key) { if (stateEmpty) stateEmpty = false; else buffer += ','; if (space && null !== prevState) buffer += `\n${space.repeat(stack.length)}`; if (state === printObject) { let keyString = keyStrings.get(key); if (void 0 === keyString) keyStrings.set(key, keyString = encodeString(key) + (space ? ': ' : ':')); buffer += keyString; } } function printEntry(key, value) { value = utils_replaceValue(stateValue, key, value, replacer); if (null === value || 'object' != typeof value) { if (state !== printObject || void 0 !== value) { printEntryPrelude(key); pushPrimitive(value); } } else { if (stack.includes(value)) throw new TypeError('Converting circular structure to JSON'); printEntryPrelude(key); stack.push(value); pushState(); state = Array.isArray(value) ? printArray : printObject; stateValue = value; stateEmpty = true; stateIndex = 0; } } function pushPrimitive(value) { switch(typeof value){ case 'string': buffer += encodeString(value); break; case 'number': buffer += Number.isFinite(value) ? String(value) : 'null'; break; case 'boolean': buffer += value ? 'true' : 'false'; break; case 'undefined': case 'object': buffer += 'null'; break; default: throw new TypeError(`Do not know how to serialize a ${value.constructor?.name || typeof value}`); } } function pushState() { prevState = { keys: stateKeys, index: stateIndex, prev: prevState }; } function popState() { stack.pop(); const value = stack.length > 0 ? stack[stack.length - 1] : rootValue; state = Array.isArray(value) ? printArray : printObject; stateValue = value; stateEmpty = false; stateKeys = prevState.keys; stateIndex = prevState.index; prevState = prevState.prev; } } const hasOwn = 'function' == typeof Object.hasOwn ? Object.hasOwn : (object, key)=>Object.hasOwnProperty.call(object, key); const escapableCharCodeSubstitution = { 0x08: '\\b', 0x09: '\\t', 0x0a: '\\n', 0x0c: '\\f', 0x0d: '\\r', 0x22: '\\\"', 0x5c: '\\\\' }; Uint8Array.from({ length: 2048 }, (_, code)=>{ if (hasOwn(escapableCharCodeSubstitution, code)) return 2; if (code < 0x20) return 6; return code < 128 ? 1 : 2; }); export { stringify_chunked_stringifyChunked as stringifyChunked };