-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
209 lines (169 loc) · 4.32 KB
/
index.js
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
const turbo = require('turbo-json-parse')
module.exports = jitson
function jitson (opts) {
if (!opts) opts = {}
const schemas = []
const inputs = []
const partial = !!opts.partial
var sampleInterval = opts.sampleInterval || 100
var tick = sampleInterval
var compiled = null
var failures = 0
parsePartial.pointer = 0
const result = partial ? parsePartial : parse
result.recompiles = 0
result.schema = null
result.turbo = null
return result
function sample (src) {
const data = JSON.parse(src)
if (--tick > 0 && inputs.length === 8 && compiled) return data
tick = sampleInterval
if (inputs.length === 8) {
inputs.shift()
schemas.shift()
}
const sch = getSchema(data)
if (!sch) return data
inputs.push(src)
schemas.push(sch)
const schema = result.schema = joinSchemas(schemas)
if (!schema) {
if (++failures >= 7) {
sampleInterval *= 2
}
return data
}
failures = 0
opts.buffer = inputs.every(isBuffer)
opts.defaults = false
result.recompiles++
compiled = result.turbo = turbo(schema, opts)
return data
}
function parse (src) {
try {
if (compiled) return compiled(src, 0)
} catch (err) {
compiled = result.compiled = result.turbo = null
}
return sample(src)
}
function parsePartial (src, ptr) {
try {
if (compiled) {
const data = compiled(src, ptr)
parse.pointer = compiled.pointer
return data
}
} catch (err) {}
throw new Error('not impl')
}
}
function isBuffer (buf) {
return typeof buf !== 'string'
}
function getSchema (object) {
const type = Array.isArray(object) ? 'array' : typeof object
switch (type) {
case 'number':
case 'string':
case 'boolean': {
return { type }
}
case 'object': {
if (!object) return null
const res = { type: 'object', ordered: true, properties: {} }
for (const key of Object.keys(object)) {
const sch = res.properties[key] = getSchema(object[key])
if (!sch) return null
}
return res
}
case 'array': {
const res = { type: 'array', items: null }
if (object.length) {
res.items = getSchema(object[0])
if (!res.items) return null
}
return res
}
}
return null
}
function joinSchemas (samples) {
if (!samples.length) return null
const s = samples[0]
if (samples.length === 1) return s
for (const sample of samples) {
if (!sample || sample.type !== s.type) return null
}
switch (s.type) {
case 'number':
case 'string':
case 'boolean':
case 'integer': {
return { type: s.type }
}
case 'object': {
const isOrdered = create()
const res = { type: 'object', ordered: true, properties: {} }
const props = {}
for (const sample of samples) {
if (res.ordered && !isOrdered(Object.keys(sample.properties))) {
res.ordered = false
}
for (const prop of Object.keys(sample.properties)) {
if (!props[prop]) props[prop] = []
props[prop].push(sample.properties[prop])
}
}
for (const prop of Object.keys(props)) {
const val = res.properties[prop] = joinSchemas(props[prop])
if (!val) return null
}
return res
}
case 'array': {
const items = []
for (const sample of samples) {
if (sample.items) items.push(sample.items)
}
const res = { type: 'array', items: joinSchemas(items) }
return items.length && !res.items ? null : res
}
}
}
function upsert (map, key, val) {
if (map.has(key)) return map.get(key)
map.set(key, val)
return val
}
function create () {
const lts = new Uint32Array(32)
const gts = new Uint32Array(32)
const tokens = new Map()
return isOrdered
function isOrdered (keys) {
const ids = new Uint8Array(keys.length)
for (var i = 0; i < keys.length; i++) {
const id = upsert(tokens, keys[i], tokens.size)
if (id >= 32) return false
ids[i] = id
}
var l = 0
var g = 0
for (const id of ids) {
l |= (1 << id)
}
for (const id of ids) {
if (lts[id] & g) return false
if (gts[id] & l) return false
lts[id] |= l
gts[id] |= g
l &= ~(1 << id)
g |= (1 << id)
}
return true
}
}