-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·224 lines (178 loc) · 5.24 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /usr/bin/env node
'use strict'
var fs = require('fs')
var path = require('path')
var os = require('osenv')
var crypto = require('crypto')
var mkdirp = require('mkdirp')
var pull = require('pull-stream')
var pt = require('pull-traverse')
var paramap = require('pull-paramap')
var cont = require('continuable')
var cpara = require('continuable-hash')
//var _unpack = require('npmd-unpack').unpack
var deps = require('get-deps')
var clone = require('clone')
var tarfs = require('tar-fs')
var zlib = require('zlib')
var EventEmitter = require('events').EventEmitter
function empty (obj) {
for(var i in obj)
return false
return true
}
function emptyStream () {
return function (abort, cb) {
cb(true)
}
}
function map (ob, iter) {
if(Array.isArray(ob)) return ob.map(iter)
var a = {}
for(var k in ob)
a[k] = iter(ob[k], k, ob)
return a
}
function merge (a, b) {
for(var k in b)
if(!a[k])
a[k] = b[k]
return a
}
var tmpdir = os.tmpdir()
function randDir (pre) {
return path.join(tmpdir, (pre || '') + Date.now() + '-' + Math.random())
}
function clean(tree) {
function _clean (tree) {
if(!tree) return tree
delete tree.dependencies
delete tree.devDependencies
delete tree.optionalDependencies
delete tree.peerDependencies
return tree
}
if('string' === typeof tree.name)
return _clean(tree)
for(var k in tree)
_clean(tree[k])
return tree
}
function isFunction (f) {
return 'function' === typeof f
}
var inject = module.exports = function (cache, config) {
var uid, gid
var hasId = isFunction(process.getuid)
if(hasId) {
uid = process.getuid() || + process.env.SUDO_UID
gid = process.getgid() || + process.env.SUDO_GID
}
function unpack (pkg, opts, cb) {
var start = Date.now()
//get from the hash if it's already been downloaded!
//else download from the registry or the url.
//actually, it's better to use the url as the key
//so that the cache downloads the correct tarball.
//this would all be so much simpler if npm just
//used the hashes as the name of the tarball.
var key =
pkg.tarball ? pkg.tarball
: /[/]/.test(pkg.from) ? pkg.from
: (pkg.name+'@'+pkg.version)
var query = {
key: key, hash: pkg.shasum
}
return cache.createStream(query, function (err, stream) {
if(err) return cb(err)
if(!stream) throw new Error('did not return stream')
var hash = crypto.createHash('sha1')
stream
.on('data', function (d) { hash.update(d) })
.on('error', cb)
.pipe(zlib.createGunzip())
.on('error', function (err) {
err.stack = 'attempting to gunzip: ' + key + '\n' + err.stack
return cb(err)
})
.pipe(tarfs.extract(opts.target, {
utimes: false,
map: function (header) {
if(hasId) { header.uid = uid; header.gid = gid }
header.name = header.name.replace(/^[^\/]*\//, '')
return header
}
}))
.on('finish', function () {
cb(null, hash.digest('hex'))
})
})
}
var installTree = cont.to(function(tree, opts, cb) {
if(!cb)
cb = opts, opts = {}
//this only works on unix. in practice, you must pass in the config object.
var cache = opts.cache || path.join(process.env.HOME, '.npm')
var installPath = opts.path || process.cwd()
tree.path = (
'string' === typeof tree.name
? path.join(installPath, 'node_modules', tree.name)
: path.join(installPath, 'node_modules')
)
pull(
pt.widthFirst(tree, function (pkg) {
if(!pkg.dependencies)
return emptyStream()
return pull(
pull.values(pkg.dependencies),
pull.map(function (_pkg) {
_pkg.path = path.join(pkg.path, 'node_modules', _pkg.name)
return _pkg
})
)
}),
paramap(function (pkg, cb) {
unpack(pkg, {target: pkg.path}, function (err, hash) {
if(err) return cb(err)
if(hash !== pkg.shasum) return cb(new Error(
'expected ' + pkg.name +'@' + pkg.version +'\n' +
'to have shasum=' + pkg.shasum + ' but was='+hash))
cb(err, pkg)
})
}, 32),
pull.drain(null, cb)
)
})
return cont.to(function (tree, opts, cb) {
if(!cb) cb = opts, opts = {}
if('string' === typeof tree.name)
return installTree(tree, opts) (next)
cpara(map(tree, function (tree) {
return installTree(tree, opts)
})) (next)
function next (err) {
cb(err, clean(clone(tree)))
}
})
}
//process.on is test for !browserify
if(!module.parent && process.on) {
var config = require('npmd-config')
var cache = require('npmd-cache')(null, config)
var install = inject(cache, config)
if(config.version) {
console.log(require('./package').version)
process.exit(0)
}
var b = ''
process.stdin.on('data', function (data) {
b += data.toString()
})
.on('end', function () {
var tree = JSON.parse(b)
install(tree, config, function (err, installed) {
if(err) throw err
console.log(JSON.stringify(installed, null, 2))
})
})
}