WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
 
const utils = require('./utils.cjs');
 
function encodeString(value) {
    if (/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(value)) { // [^\x20-\uD799]|[\x22\x5c]
        return JSON.stringify(value);
    }
 
    return '"' + value + '"';
}
 
function* stringifyChunked(value, ...args) {
    const { replacer, getKeys, space, ...options } = utils.normalizeStringifyOptions(...args);
    const highWaterMark = Number(options.highWaterMark) || 0x4000; // 16kb by default
 
    const keyStrings = new Map();
    const stack = [];
    const rootValue = { '': value };
    let prevState = null;
    let state = () => printEntry('', value);
    let stateValue = rootValue;
    let stateEmpty = true;
    let stateKeys = [''];
    let stateIndex = 0;
    let buffer = '';
 
    while (true) {
        state();
 
        if (buffer.length >= highWaterMark || prevState === null) {
            // flush buffer
            yield buffer;
            buffer = '';
 
            if (prevState === null) {
                break;
            }
        }
    }
 
    function printObject() {
        if (stateIndex === 0) {
            stateKeys = getKeys(stateValue);
            buffer += '{';
        }
 
        // when no keys left
        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 (stateIndex === 0) {
            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 && prevState !== null) {
            buffer += `\n${space.repeat(stack.length)}`;
        }
 
        if (state === printObject) {
            let keyString = keyStrings.get(key);
 
            if (keyString === undefined) {
                keyStrings.set(key, keyString = encodeString(key) + (space ? ': ' : ':'));
            }
 
            buffer += keyString;
        }
    }
 
    function printEntry(key, value) {
        value = utils.replaceValue(stateValue, key, value, replacer);
 
        if (value === null || typeof value !== 'object') {
            // primitive
            if (state !== printObject || value !== undefined) {
                printEntryPrelude(key);
                pushPrimitive(value);
            }
        } else {
            // If the visited set does not change after adding a value, then it is already in the set
            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': // typeof null === '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;
 
        // restore state
        state = Array.isArray(value) ? printArray : printObject;
        stateValue = value;
        stateEmpty = false;
        stateKeys = prevState.keys;
        stateIndex = prevState.index;
 
        // pop state
        prevState = prevState.prev;
    }
}
 
exports.stringifyChunked = stringifyChunked;