-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin.js
328 lines (276 loc) · 6.36 KB
/
min.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
module.exports = unpack
var inflate = require('inflate/min.js')
, binary = require('bops')
var OFS_DELTA = 6
, REF_DELTA = 7
function unpack(read, oncksum) {
var need_input = false
, states = []
, state = null
, ended = false
, buffer = []
, got = 0
var buffer_offset = 0
var inflate_stream = null
, inflated_fragments = []
, uninflated_fragments = []
, inflate_finished = false
, _inflate_wants
var offset = 12
, header_size = 0
var current_object_header = []
, current_ofs_header = []
var expanded_size
, object_count
, prev_object
, reference = null
, version
, cksum
, type
, last
var byte_need = 0
, byte_accum = []
var queued = []
, emit
oncksum = oncksum || function() {}
want_bytes(4); become(bytes, got_header)
stream.is = 'min-stream-pull-filter'
return stream
function stream(close, callback) {
if(queued.length) {
var out = queued.shift()
if(out === null) {
return callback()
}
return callback(null, out)
}
emit = callback
read(null, onread)
}
function onread(err, buf) {
if(buf === undefined) {
return read(err)
}
return write(buf)
}
function queue(obj) {
queued.push(obj)
if(emit) while(queued.length) {
emit(null, queued.shift())
}
}
function want_bytes(num) {
byte_need = num
byte_accum.length = 0
}
function write(buf) {
buffer.push(buf)
got += buf.length
if(!ended) {
execute()
}
read(null, onread)
}
function got_header() {
for(var i = 0, len = 4; i < len; ++i) {
if(last[i] !== 'PACK'.charCodeAt(i)) {
emit(new Error(
'invalid header'
))
return
}
}
want_bytes(4); become(bytes, got_header_version)
}
function got_header_version() {
// no-op for now
want_bytes(4); become(bytes, got_object_count)
}
function got_object_count() {
object_count = last[3] | (last[2] << 8) | (last[1] << 16) | (last[0] << 24)
object_count >>>= 0
want_bytes(1); become(bytes, start_object_header)
}
function start_object_header() {
current_object_header.length = 0
header_size = 0
iter_object_header()
}
function iter_object_header() {
var byt = last[0]
current_object_header.push(byt)
if(!(byt & 0x80)) {
finish_object_header()
} else {
want_bytes(1); become(bytes, iter_object_header)
}
}
function finish_object_header() {
var size = current_object_header[0] & 0x0F
, shift = 4
, idx = 1
, byt
header_size = current_object_header.length
type = current_object_header[0] >> 4 & 7
while(idx < current_object_header.length) {
size += (current_object_header[idx++] & 0x7F) << shift
shift += 7
}
expanded_size = size
if(type < 5) {
start_inflate()
} else if(type === OFS_DELTA) {
start_ofs_delta()
} else if(type === REF_DELTA) {
start_ref_delta()
}
}
function start_inflate() {
states[0] = write_inflate
inflate_stream = inflate_stream ? inflate_stream.recycle() : inflate(inflate_read, finish_inflate)
inflated_fragments.length = 0
inflate_finished = false
iter()
function iter() {
inflate_stream(null, function(err, data) {
if(err) {
return emit(err)
}
inflated_fragments.push(data)
iter()
})
}
}
function inflate_read(close, ready) {
if(close === true) {
return
}
if(close) {
return emit(close)
}
_inflate_wants = ready
}
function write_inflate() {
var next
while(buffer.length && !inflate_finished) {
next = buffer.shift()
if(buffer_offset) {
if(buffer_offset === next.length) {
buffer_offset = 0
continue
}
next = binary.subarray(next, buffer_offset)
buffer_offset = 0
}
got -= next.length
_inflate_wants(null, next)
}
if(!buffer.length && !inflate_finished) {
need_input = true
}
}
function finish_inflate(unused, read) {
inflate_finished = true
queue(prev_object = {
reference: reference
, data: binary.join(inflated_fragments)
, type: type
, offset: offset
, num: object_count - 1
})
offset += read + header_size + (reference ? reference.length : 0)
header_size = 0
--object_count
reference = null
if(unused.length) {
buffer = unused.concat(buffer)
for(var i = 0, len = unused.length; i < len; ++i) {
got += unused[i].length
}
buffer_offset = 0
}
if(!object_count) {
want_bytes(20); become(bytes, got_checksum)
} else {
want_bytes(1); become(bytes, start_object_header)
}
}
function start_ofs_delta() {
current_ofs_header.length = 0
want_bytes(1); become(bytes, iter_ofs_delta)
}
function iter_ofs_delta() {
var byt = last[0]
current_ofs_header.push(byt)
if(!(byt & 0x80)) {
reference = binary.from(current_ofs_header)
start_inflate()
} else {
want_bytes(1); become(bytes, iter_ofs_delta)
}
}
function start_ref_delta() {
want_bytes(20); become(bytes, got_ref_delta_reference)
}
function got_ref_delta_reference() {
reference = binary.from(last)
start_inflate()
}
function got_checksum() {
oncksum(binary.from(last))
queue(null)
ended = true
}
function execute() {
while(1) {
states[0]()
if(need_input || ended) {
break
}
}
need_input = false
}
function bytes() {
var value
while(byte_need--) {
value = take()
if(need_input) {
byte_need += 1
break
}
byte_accum[byte_accum.length] = value
}
if(!need_input) {
unbecome(byte_accum)
}
}
function take() {
var val
if(!buffer.length) {
need_input = true
} else if(buffer_offset === buffer[0].length) {
buffer.shift()
buffer_offset = 0
val = take()
} else {
val = buffer[0][buffer_offset++]
}
return val
}
function become(fn, then) {
if(typeof then !== 'function') {
throw new Error
}
last = null
if(states.length < 1) {
states.unshift(then)
} else {
states[0] = then
}
states.unshift(fn)
}
function unbecome(result) {
states.shift()
last = result
}
}