forked from npm/npm-fullfat-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullfat.js
675 lines (569 loc) · 17 KB
/
fullfat.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
var follow = require('follow')
var fs = require('fs')
var EE = require('events').EventEmitter
var util = require('util')
var url = require('url')
var path = require('path')
var tmp = path.resolve(__dirname, 'tmp')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var assert = require('assert')
var stream = require('stream')
var util = require('util')
var crypto = require('crypto')
var once = require('once')
var parse = require('parse-json-response')
var hh = require('http-https')
var version = require('./package.json').version
var ua = 'npm FullFat/' + version + ' node/' + process.version
var readmeTrim = require('npm-registry-readme-trim')
util.inherits(FullFat, EE)
module.exports = FullFat
function FullFat(conf) {
if (!conf.skim || !conf.fat) {
throw new Error('skim and fat database urls required')
}
this.skim = url.parse(conf.skim).href
this.skim = this.skim.replace(/\/+$/, '')
var f = url.parse(conf.fat)
this.fat = f.href
this.fat = this.fat.replace(/\/+$/, '')
delete f.auth
this.publicFat = url.format(f)
this.publicFat = this.publicFat.replace(/\/+$/, '')
this.registry = null
if (conf.registry) {
this.registry = url.parse(conf.registry).href
this.registry = this.registry.replace(/\/+$/, '')
}
this.ua = conf.ua || ua
this.inactivity_ms = conf.inactivity_ms || 1000 * 60 * 60
this.seqFile = conf.seq_file
this.writingSeq = false
this.error = false
this.since = 0
this.follow = null
// set to true to log missing attachments only.
// otherwise, emits an error.
this.missingLog = conf.missing_log || false
this.whitelist = conf.whitelist || [ /.*/ ]
this.tmp = conf.tmp
if (!this.tmp) {
var rand = crypto.randomBytes(6).toString('hex')
this.tmp = path.resolve('npm-fullfat-tmp-' + process.pid + '-' + rand)
}
this.boundary = 'npmFullFat-' + crypto.randomBytes(6).toString('base64')
this.readSeq(this.seqFile)
}
FullFat.prototype.readSeq = function(file) {
if (!this.seqFile)
process.nextTick(this.start.bind(this))
else
fs.readFile(file, 'ascii', this.gotSeq.bind(this))
}
FullFat.prototype.gotSeq = function(er, data) {
if (er && er.code === 'ENOENT')
data = '0'
else if (er)
return this.emit('error', er)
data = +data || 0
this.since = data
this.start()
}
FullFat.prototype.start = function() {
if (this.follow)
return this.emit('error', new Error('already started'))
this.emit('start')
this.follow = follow({
db: this.skim,
since: this.since,
inactivity_ms: this.inactivity_ms
}, this.onchange.bind(this))
this.follow.on('error', this.emit.bind(this, 'error'))
}
FullFat.prototype._emit = function(ev, arg) {
// Don't emit errors while writing seq
if (ev === 'error' && this.writingSeq) {
this.error = arg
} else {
EventEmitter.prototype.emit.apply(this, arguments)
}
}
FullFat.prototype.writeSeq = function() {
var seq = +this.since
if (this.seqFile && !this.writingSeq && seq > 0) {
var data = seq + '\n'
var file = this.seqFile + '.' + seq
this.writingSeq = true
fs.writeFile(file, data, 'ascii', function(writeEr) {
var er = this.error
if (er)
this.emit('error', er)
else if (!writeEr) {
fs.rename(file, this.seqFile, function(mvEr) {
this.writingSeq = false
var er = this.error
if (er)
this.emit('error', er)
else if (!mvEr)
this.emit('sequence', seq)
}.bind(this))
}
}.bind(this))
}
}
FullFat.prototype.onchange = function(er, change) {
if (er)
return this.emit('error', er)
if (!change.id)
return
this.pause()
this.since = change.seq
this.emit('change', change)
if (change.deleted)
this.delete(change)
else
this.getDoc(change)
}
FullFat.prototype.getDoc = function(change) {
var q = '?revs=true&att_encoding_info=true'
var opt = url.parse(this.skim + '/' + change.id + q)
opt.method = 'GET'
opt.headers = {
'user-agent': this.ua,
'connection': 'close'
}
var req = hh.get(opt)
req.on('error', this.emit.bind(this, 'error'))
req.on('response', parse(this.ongetdoc.bind(this, change)))
}
FullFat.prototype.ongetdoc = function(change, er, data, res) {
if (er)
this.emit('error', er)
else {
change.doc = data
if (change.id.match(/^_design\//))
this.putDesign(change)
else if (data.time && data.time.unpublished)
this.unpublish(change)
else
this.putDoc(change)
}
}
FullFat.prototype.unpublish = function(change) {
change.fat = change.doc
this.put(change, [])
}
FullFat.prototype.putDoc = function(change) {
var q = '?revs=true&att_encoding_info=true'
var opt = url.parse(this.fat + '/' + change.id + q)
opt.method = 'GET'
opt.headers = {
'user-agent': this.ua,
'connection': 'close'
}
var req = hh.get(opt)
req.on('error', this.emit.bind(this, 'error'))
req.on('response', parse(this.onfatget.bind(this, change)))
}
FullFat.prototype.putDesign = function(change) {
var doc = change.doc
this.pause()
var opt = url.parse(this.fat + '/' + change.id + '?new_edits=false')
var b = new Buffer(JSON.stringify(doc), 'utf8')
opt.method = 'PUT'
opt.headers = {
'user-agent': this.ua,
'content-type': 'application/json',
'content-length': b.length,
'connection': 'close'
}
var req = hh.request(opt)
req.on('response', parse(this.onputdesign.bind(this, change)))
req.on('error', this.emit.bind(this, 'error'))
req.end(b)
}
FullFat.prototype.onputdesign = function(change, er, data, res) {
if (er)
return this.emit('error', er)
this.emit('putDesign', change, data)
this.resume()
}
FullFat.prototype.delete = function(change) {
var name = change.id
var opt = url.parse(this.fat + '/' + name)
opt.headers = {
'user-agent': this.ua,
'connection': 'close'
}
opt.method = 'HEAD'
var req = hh.request(opt)
req.on('response', this.ondeletehead.bind(this, change))
req.on('error', this.emit.bind(this, 'error'))
req.end()
}
FullFat.prototype.ondeletehead = function(change, res) {
// already gone? totally fine. move on, nothing to delete here.
if (res.statusCode === 404)
return this.afterDelete(change)
var rev = res.headers.etag.replace(/^"|"$/g, '')
opt = url.parse(this.fat + '/' + change.id + '?rev=' + rev)
opt.headers = {
'user-agent': this.ua,
'connection': 'close'
}
opt.method = 'DELETE'
var req = hh.request(opt)
req.on('response', parse(this.ondelete.bind(this, change)))
req.on('error', this.emit.bind(this, 'error'))
req.end()
}
FullFat.prototype.ondelete = function(change, er, data, res) {
if (er && er.statusCode === 404)
this.afterDelete(change)
else if (er)
this.emit('error', er)
else
// scorch the earth! remove fully! repeat until 404!
this.delete(change)
}
FullFat.prototype.afterDelete = function(change) {
this.emit('delete', change)
this.resume()
}
FullFat.prototype.onfatget = function(change, er, f, res) {
if (er && er.statusCode !== 404)
return this.emit('error', er)
if (er)
f = JSON.parse(JSON.stringify(change.doc))
f._attachments = f._attachments || {}
change.fat = f
this.merge(change)
}
FullFat.prototype.merge = function(change) {
var s = change.doc
var f = change.fat
// if no versions in the skim record, then nothing to fetch
if (!s.versions)
return this.resume()
// Only fetch attachments if it's on the list.
var pass = true
if (this.whitelist.length) {
pass = false
for (var i = 0; !pass && i < this.whitelist.length; i++) {
var w = this.whitelist[i]
if (typeof w === 'string')
pass = w === change.id
else
pass = w.exec(change.id)
}
if (!pass) {
f._attachments = {}
return this.fetchAll(change, [], [])
}
}
var need = []
var changed = false
for (var v in s.versions) {
var tgz = s.versions[v].dist.tarball
var att = path.basename(url.parse(tgz).pathname)
var ver = s.versions[v]
f.versions = f.versions || {}
if (!f.versions[v] || f.versions[v].dist.shasum !== ver.dist.shasum) {
f.versions[v] = s.versions[v]
need.push(v)
changed = true
} else if (!f._attachments[att]) {
need.push(v)
changed = true
}
}
// remove any versions that s removes, or which lack attachments
for (var v in f.versions) {
if (!s.versions[v])
delete f.versions[v]
}
for (var a in f._attachments) {
var found = false
for (var v in f.versions) {
var tgz = f.versions[v].dist.tarball
var b = path.basename(url.parse(tgz).pathname)
if (b === a) {
found = true
break
}
}
if (!found) {
delete f._attachments[a]
changed = true
}
}
for (var k in s) {
if (k !== '_attachments' && k !== 'versions') {
if (changed)
f[k] = s[k]
else if (JSON.stringify(f[k]) !== JSON.stringify(s[k])) {
f[k] = s[k]
changed = true
}
}
}
changed = readmeTrim(f) || changed
if (!changed)
this.resume()
else
this.fetchAll(change, need, [])
}
FullFat.prototype.put = function(change, did) {
var f = change.fat
change.did = did
// at this point, all the attachments have been fetched into
// {this.tmp}/{change.id}-{change.seq}/{attachment basename}
// make a multipart PUT with all of the missing ones set to
// follows:true
var boundaries = []
var boundary = this.boundary
var bSize = 0
var attSize = 0
var atts = f._attachments = f._attachments || {}
// It's important that we do everything in enumeration order,
// because couchdb is a jerk, and ignores disposition headers.
// Still include the filenames, though, so at least we dtrt.
did.forEach(function(att) {
atts[att.name] = {
length: att.length,
follows: true
}
if (att.type)
atts[att.name].type = att.type
})
var send = []
Object.keys(atts).forEach(function (name) {
var att = atts[name]
if (att.follows !== true)
return
send.push([name, att])
attSize += att.length
var b = '\r\n--' + boundary + '\r\n' +
'content-length: ' + att.length + '\r\n' +
'content-disposition: attachment; filename=' +
JSON.stringify(name) + '\r\n'
if (att.type)
b += 'content-type: ' + att.type + '\r\n'
b += '\r\n'
boundaries.push(b)
bSize += b.length
})
// one last boundary at the end
var b = '\r\n--' + boundary + '--'
bSize += b.length
boundaries.push(b)
// put with new_edits=false to retain the same rev
// this assumes that NOTHING else is writing to this database!
var p = url.parse(this.fat + '/' + f.name + '?new_edits=false')
p.method = 'PUT'
p.headers = {
'user-agent': this.ua,
'content-type': 'multipart/related;boundary="' + boundary + '"',
'connection': 'close'
}
var doc = new Buffer(JSON.stringify(f), 'utf8')
var len = 0
// now, for the document
var b = '--' + boundary + '\r\n' +
'content-type: application/json\r\n' +
'content-length: ' + doc.length + '\r\n\r\n'
bSize += b.length
p.headers['content-length'] = attSize + bSize + doc.length
var req = hh.request(p)
req.on('error', this.emit.bind(this, 'error'))
req.write(b, 'ascii')
req.write(doc)
this.putAttachments(req, change, boundaries, send)
req.on('response', parse(this.onputres.bind(this, change)))
}
FullFat.prototype.putAttachments = function(req, change, boundaries, send) {
// send is the ordered list of [[name, attachment object],...]
var b = boundaries.shift()
var ns = send.shift()
// last one!
if (!ns) {
req.write(b, 'ascii')
return req.end()
}
var name = ns[0]
req.write(b, 'ascii')
var file = path.join(this.tmp, change.id + '-' + change.seq, name)
var fstr = fs.createReadStream(file)
fstr.on('end', function() {
this.emit('upload', {
change: change,
name: name
})
this.putAttachments(req, change, boundaries, send)
}.bind(this))
fstr.on('error', this.emit.bind(this, 'error'))
fstr.pipe(req, { end: false })
}
FullFat.prototype.onputres = function(change, er, data, res) {
if (!change.id)
throw new Error('wtf?')
// In some oddball cases, it looks like CouchDB will report stubs that
// it doesn't in fact have. It's possible that this is due to old bad
// data in a past FullfatDB implementation, but whatever the case, we
// ought to catch such errors and DTRT. In this case, the "right thing"
// is to re-try the PUT as if it had NO attachments, so that it no-ops
// the attachments that ARE there, and fills in the blanks.
// We do that by faking the onfatget callback with a 404 error.
if (er && er.statusCode === 412 &&
0 === er.message.indexOf('{"error":"missing_stub"') &&
!change.didFake404){
change.didFake404 = true
this.onfatget(change, { statusCode: 404 }, {}, {})
} else if (er)
this.emit('error', er)
else {
this.emit('put', change, data)
// Just a best-effort cleanup. No big deal, really.
rimraf(this.tmp + '/' + change.id + '-' + change.seq, function() {})
this.resume()
}
}
FullFat.prototype.fetchAll = function(change, need, did) {
var f = change.fat
var tmp = path.resolve(this.tmp, change.id + '-' + change.seq)
var len = need.length
if (!len)
return this.put(change, did)
var errState = null
mkdirp(tmp, function(er) {
if (er)
return this.emit('error', er)
need.forEach(this.fetchOne.bind(this, change, need, did))
}.bind(this))
}
FullFat.prototype.fetchOne = function(change, need, did, v) {
var f = change.fat
var r = url.parse(change.doc.versions[v].dist.tarball)
if (this.registry) {
var p = '/' + change.id + '/-/' + path.basename(r.pathname)
r = url.parse(this.registry + p)
}
r.method = 'GET'
r.headers = {
'user-agent': this.ua,
'connection': 'close'
}
var req = hh.request(r)
req.on('error', this.emit.bind(this, 'error'))
req.on('response', this.onattres.bind(this, change, need, did, v, r))
req.end()
}
FullFat.prototype.onattres = function(change, need, did, v, r, res) {
var f = change.fat
var att = r.href
var sum = f.versions[v].dist.shasum
var filename = f.name + '-' + v + '.tgz'
var file = path.join(this.tmp, change.id + '-' + change.seq, filename)
// TODO: If the file already exists, get its size.
// If the size matches content-length, get the md5
// If the md5 matches content-md5, then don't bother downloading!
function skip() {
rimraf(file, function() {})
delete f.versions[v]
if (f._attachments)
delete f._attachments[file]
need.splice(need.indexOf(v), 1)
maybeDone(null)
}
var maybeDone = function maybeDone(a) {
if (a)
this.emit('download', a)
if (need.length === did.length)
this.put(change, did)
}.bind(this)
// if the attachment can't be found, then skip that version
// it's uninstallable as of right now, and may or may not get
// fixed in a future update
if (res.statusCode !== 200) {
var er = new Error('Error fetching attachment: ' + att)
er.statusCode = res.statusCode
er.code = 'attachment-fetch-fail'
if (this.missingLog)
return fs.appendFile(this.missingLog, att + '\n', skip)
else
return this.emit('error', er)
}
var fstr = fs.createWriteStream(file)
// check the shasum while we're at it
var sha = crypto.createHash('sha1')
var shaOk = false
var errState = null
sha.on('data', function(c) {
c = c.toString('hex')
if (c === sum)
shaOk = true
}.bind(this))
if (!res.headers['content-length']) {
var counter = new Counter()
res.pipe(counter)
}
res.pipe(sha)
res.pipe(fstr)
fstr.on('error', function(er) {
er.change = change
er.version = v
er.path = file
er.url = att
this.emit('error', errState = errState || er)
}.bind(this))
fstr.on('close', function() {
if (errState || !shaOk) {
// something didn't work, but the error was squashed
// take that as a signal to just delete this version
return skip()
}
// it worked! change the dist.tarball url to point to the
// registry where this is being stored. It'll be rewritten by
// the _show/pkg function when going through the rewrites, anyway,
// but this url will work if the couch itself is accessible.
var newatt = this.publicFat + '/' + change.id +
'/' + change.id + '-' + v + '.tgz'
f.versions[v].dist.tarball = newatt
if (res.headers['content-length'])
var cl = +res.headers['content-length']
else
var cl = counter.count
var a = {
change: change,
version: v,
name: path.basename(file),
length: cl,
type: res.headers['content-type']
}
did.push(a)
maybeDone(a)
}.bind(this))
}
FullFat.prototype.destroy = function() {
if (this.follow)
this.follow.die()
}
FullFat.prototype.pause = function() {
if (this.follow)
this.follow.pause()
}
FullFat.prototype.resume = function() {
this.writeSeq()
if (this.follow)
this.follow.resume()
}
util.inherits(Counter, stream.Writable)
function Counter(options) {
stream.Writable.call(this, options)
this.count = 0
}
Counter.prototype._write = function(chunk, encoding, cb) {
this.count += chunk.length
cb()
}