马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const Parser = require('jsonparse')
const { Minipass } = require('minipass')
 
class JSONStreamError extends Error {
  constructor (err, caller) {
    super(err.message)
    Error.captureStackTrace(this, caller || this.constructor)
  }
 
  get name () {
    return 'JSONStreamError'
  }
}
 
const check = (x, y) =>
  typeof x === 'string' ? String(y) === x
  : x && typeof x.test === 'function' ? x.test(y)
  : typeof x === 'boolean' || typeof x === 'object' ? x
  : typeof x === 'function' ? x(y)
  : false
 
class JSONStream extends Minipass {
  #count = 0
  #ending = false
  #footer = null
  #header = null
  #map = null
  #onTokenOriginal
  #parser
  #path = null
  #root = null
 
  constructor (opts) {
    super({
      ...opts,
      objectMode: true,
    })
 
    const parser = this.#parser = new Parser()
    parser.onValue = value => this.#onValue(value)
    this.#onTokenOriginal = parser.onToken
    parser.onToken = (token, value) => this.#onToken(token, value)
    parser.onError = er => this.#onError(er)
 
    this.#path = typeof opts.path === 'string'
      ? opts.path.split('.').map(e =>
        e === '$*' ? { emitKey: true }
        : e === '*' ? true
        : e === '' ? { recurse: true }
        : e)
      : Array.isArray(opts.path) && opts.path.length ? opts.path
      : null
 
    if (typeof opts.map === 'function') {
      this.#map = opts.map
    }
  }
 
  #setHeaderFooter (key, value) {
    // header has not been emitted yet
    if (this.#header !== false) {
      this.#header = this.#header || {}
      this.#header[key] = value
    }
 
    // footer has not been emitted yet but header has
    if (this.#footer !== false && this.#header === false) {
      this.#footer = this.#footer || {}
      this.#footer[key] = value
    }
  }
 
  #onError (er) {
    // error will always happen during a write() call.
    const caller = this.#ending ? this.end : this.write
    this.#ending = false
    return this.emit('error', new JSONStreamError(er, caller))
  }
 
  #onToken (token, value) {
    const parser = this.#parser
    this.#onTokenOriginal.call(this.#parser, token, value)
    if (parser.stack.length === 0) {
      if (this.#root) {
        const root = this.#root
        if (!this.#path) {
          super.write(root)
        }
        this.#root = null
        this.#count = 0
      }
    }
  }
 
  #onValue (value) {
    const parser = this.#parser
    // the LAST onValue encountered is the root object.
    // just overwrite it each time.
    this.#root = value
 
    if (!this.#path) {
      return
    }
 
    let i = 0 // iterates on path
    let j = 0 // iterates on stack
    let emitKey = false
    while (i < this.#path.length) {
      const key = this.#path[i]
      j++
 
      if (key && !key.recurse) {
        const c = (j === parser.stack.length) ? parser : parser.stack[j]
        if (!c) {
          return
        }
        if (!check(key, c.key)) {
          this.#setHeaderFooter(c.key, value)
          return
        }
        emitKey = !!key.emitKey
        i++
      } else {
        i++
        if (i >= this.#path.length) {
          return
        }
        const nextKey = this.#path[i]
        if (!nextKey) {
          return
        }
        while (true) {
          const c = (j === parser.stack.length) ? parser : parser.stack[j]
          if (!c) {
            return
          }
          if (check(nextKey, c.key)) {
            i++
            if (!Object.isFrozen(parser.stack[j])) {
              parser.stack[j].value = null
            }
            break
          } else {
            this.#setHeaderFooter(c.key, value)
          }
          j++
        }
      }
    }
 
    // emit header
    if (this.#header) {
      const header = this.#header
      this.#header = false
      this.emit('header', header)
    }
    if (j !== parser.stack.length) {
      return
    }
 
    this.#count++
    const actualPath = parser.stack.slice(1)
      .map(e => e.key).concat([parser.key])
    if (value !== null && value !== undefined) {
      const data = this.#map ? this.#map(value, actualPath) : value
      if (data !== null && data !== undefined) {
        const emit = emitKey ? { value: data } : data
        if (emitKey) {
          emit.key = parser.key
        }
        super.write(emit)
      }
    }
 
    if (parser.value) {
      delete parser.value[parser.key]
    }
 
    for (const k of parser.stack) {
      k.value = null
    }
  }
 
  write (chunk, encoding) {
    if (typeof chunk === 'string') {
      chunk = Buffer.from(chunk, encoding)
    } else if (!Buffer.isBuffer(chunk)) {
      return this.emit('error', new TypeError(
        'Can only parse JSON from string or buffer input'))
    }
    this.#parser.write(chunk)
    return this.flowing
  }
 
  end (chunk, encoding) {
    this.#ending = true
    if (chunk) {
      this.write(chunk, encoding)
    }
 
    const h = this.#header
    this.#header = null
    const f = this.#footer
    this.#footer = null
    if (h) {
      this.emit('header', h)
    }
    if (f) {
      this.emit('footer', f)
    }
    return super.end()
  }
 
  static get JSONStreamError () {
    return JSONStreamError
  }
 
  static parse (path, map) {
    return new JSONStream({ path, map })
  }
}
 
module.exports = JSONStream