Skip to content

Commit

Permalink
use got instead of raw http
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Jan 17, 2016
1 parent 7741f22 commit 7cedf86
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 203 deletions.
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var config = {
globalBin: process.env.IED_GLOBAL_BIN || path.resolve(process.execPath, '..'),
httpProxy: process.env.IED_HTTP_PROXY || process.env.HTTP_PROXY || null,
httpsProxy: process.env.IED_HTTPS_PROXY || process.env.HTTPS_PROXY || null,
requestRetries: 5,
sh: process.env.IED_SH || (process.platform === 'win32' ? process.env.comspec || 'cmd' : process.env.SHELL || 'bash'),
shFlag: process.env.IED_SH_FLAG || (process.platform === 'win32' ? '/d /s /c' : '-c')
}
Expand Down
49 changes: 20 additions & 29 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var http = require('http')
var got = require('got')
var gunzip = require('gunzip-maybe')
var tar = require('tar-fs')
var crypto = require('crypto')
Expand All @@ -10,8 +10,6 @@ var debug = require('./debuglog')('fetch')
var progress = require('./progress')
var config = require('./config')
var cache = require('./cache')
var url = require('url')
var protocolToAgent = require('./protocol_to_agent')

function fetchFromRegistry (dest, tarball, uid, shasum, cb) {
// Fixes flickering progress bar bug for sequential downloads.
Expand All @@ -25,45 +23,38 @@ function fetchFromRegistry (dest, tarball, uid, shasum, cb) {
// We verify the actual shasum to detect "corrupted" packages.
var actualShasum = crypto.createHash('sha1')

var opts = url.parse(tarball)
opts.agent = protocolToAgent[opts.protocol]
if (!opts.agent) return cb(new Error(tarball + ' uses an unsupported protocol'))

http.get(opts, function (res) {
var stream = got.stream(tarball).on('response', function (res) {
if (res.statusCode !== 200) {
return cb(new Error('Unexpected status code ' + res.statusCode + ' for ' + tarball))
}

// Write to cache.
var cached = res.pipe(cache.write()).on('error', cb)

// Check if we know the final content-length. If we do, we can render a
// progress bar.
var hasContentLength = 'content-length' in res.headers
if (hasContentLength) {
progress.start(parseInt(res.headers['content-length'], 10))
res.on('data', function (chunk) { progress.complete(chunk.length) })
}
})

function onFinish () {
progress.complete(1)
var expectedShasum = actualShasum.digest('hex')
if (shasum && expectedShasum !== shasum) {
debug('fetched tarball has incorrect shasum %s, expected %s', shasum, expectedShasum)
return cb(new Error('Downloaded tarball has incorrect shasum'))
}
debug('cached %s in %s', uid, cached.path)
return fs.rename(cached.path, path.join(config.cacheDir, uid), cb)
}
// Write to cache.
var cached = stream.pipe(cache.write()).on('error', cb)

// Used for progress indicator.
res.on('data', actualShasum.update.bind(actualShasum))
stream.on('data', actualShasum.update.bind(actualShasum))
.on('error', cb)
.pipe(gunzip()).on('error', cb)
.pipe(untar).on('error', cb)
.on('finish', onFinish)

res.on('error', cb)
.pipe(gunzip()).on('error', cb)
.pipe(untar).on('error', cb)
.on('finish', onFinish)
}).on('error', cb)
function onFinish () {
progress.complete(1)
var expectedShasum = actualShasum.digest('hex')
if (shasum && expectedShasum !== shasum) {
debug('fetched tarball has incorrect shasum %s, expected %s', shasum, expectedShasum)
return cb(new Error('Downloaded tarball has incorrect shasum'))
}
debug('cached %s in %s', uid, cached.path)
return fs.rename(cached.path, path.join(config.cacheDir, uid), cb)
}
}

// Fetches the specified tarball. Verifies the passed in shasum if not cached.
Expand Down
22 changes: 0 additions & 22 deletions lib/protocol_to_agent.js

This file was deleted.

39 changes: 9 additions & 30 deletions lib/resolvers/npm-registry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var http = require('http')
var urlModule = require('url')
var semver = require('semver')
var got = require('got')
var packageUid = require('../util/package_uid')
var protocolToAgent = require('../protocol_to_agent')
var debug = require('../debuglog')('resolver:npm-registry')
var config = require('../config')

Expand Down Expand Up @@ -31,36 +29,17 @@ function fetch (url, cb) {

pending[url] = [cb]
var resolve = resolvePending.bind(null, url)
var opts = urlModule.parse(url)
opts.agent = protocolToAgent[opts.protocol]
if (!opts.agent) return cb(new Error(url + ' uses an unsupported protocol'))

http.get(opts, function (res) {
got(url, {json: true, retries: config.requestRetries}, function (err, body, res) {
if (err) {
return cb(err)
}
if (res.statusCode !== 200) {
return cb(new Error('Unexpected status code ' + res.statusCode + ' for ' + url))
cb(new Error('Unexpected status code ' + res.statusCode + ' for ' + url))
}

var raw = ''

res.on('data', function (chunk) { raw += chunk })
res.on('end', function () {
var err = null
var parsed
try {
parsed = JSON.parse(raw)
if (!parsed.versions || typeof parsed.versions !== 'object') {
err = new SyntaxError('package.json should have plain object property "versions"')
}
if (!err) {
cache[url] = parsed
}
} catch (e) {
err = e
}
resolve(err, parsed)
})
res.on('error', resolve)
}).on('error', resolve)
resolve(err, body)
cache[url] = body
})
}

module.exports = function resolveFromNpm (name, version, cb) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"archy": "^1.0.0",
"async": "^1.4.2",
"easy-table": "^1.0.0",
"got": "^5.3.1",
"gunzip-maybe": "^1.2.1",
"https-proxy-agent": "^1.0.0",
"init-package-json": "^1.9.1",
Expand All @@ -24,6 +25,7 @@
"mocha": "^2.3.3",
"mock-fs": "^3.4.0",
"mockmock": "0.0.5",
"nock": "^5.2.1",
"proxyquire": "^1.7.3",
"rimraf": "^2.4.3",
"snazzy": "^2.0.1",
Expand Down
Loading

0 comments on commit 7cedf86

Please sign in to comment.