-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (55 loc) · 1.37 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
var request = require('hyperquest');
var tar = require('tar');
var zlib = require('zlib');
var concat = require('concat-stream');
var EventEmitter = require('events').EventEmitter;
/**
* Expose `files`.
*/
module.exports = files;
/**
* Get the published source files of module `name`.
*
* @param {String} name
* @param {Object?} opts
* @return {EventEmitter}
*/
function files(name, opts) {
if ('function' == typeof opts) {
fn = opts;
opts = fn;
}
opts = opts || {};
var version = opts.version || 'latest';
var registry = opts.registry || 'http://registry.npmjs.org';
var url = registry + '/' + name + '/' + version;
var ee = new EventEmitter;
var error = ee.emit.bind(ee, 'error');
var req = request(url);
req.on('error', error);
req.pipe(concat(function(res) {
try {
var body = JSON.parse(res);
} catch (err) {
return error(err);
}
req = request(body.dist.tarball)
.on('error', error);
var unzip = zlib.createGunzip()
.on('error', error);
var parse = new tar.Parse();
parse.on('error', error);
parse.on('entry', function(entry) {
entry.path = entry.path.slice(8);
ee.emit('file', entry);
});
parse.on('end', function() {
ee.emit('end');
});
req.pipe(unzip).pipe(parse);
}));
ee.destroy = function() {
req.destroy();
};
return ee;
}