"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-registry-client/lib/fetch.js" (7 Feb 2017, 2477 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var assert = require('assert')
    2 var url = require('url')
    3 
    4 var request = require('request')
    5 var once = require('once')
    6 
    7 module.exports = fetch
    8 
    9 function fetch (uri, params, cb) {
   10   assert(typeof uri === 'string', 'must pass uri to request')
   11   assert(params && typeof params === 'object', 'must pass params to request')
   12   assert(typeof cb === 'function', 'must pass callback to request')
   13 
   14   cb = once(cb)
   15 
   16   var client = this
   17   this.attempt(function (operation) {
   18     makeRequest.call(client, uri, params, function (er, req) {
   19       if (er) return cb(er)
   20 
   21       req.on('error', function (er) {
   22         if (operation.retry(er)) {
   23           client.log.info('retry', 'will retry, error on last attempt: ' + er)
   24         } else {
   25           cb(er)
   26         }
   27       })
   28 
   29       req.on('response', function (res) {
   30         client.log.http('fetch', '' + res.statusCode, uri)
   31 
   32         var er
   33         var statusCode = res && res.statusCode
   34         if (statusCode === 200) {
   35           // Work around bug in node v0.10.0 where the CryptoStream
   36           // gets stuck and never starts reading again.
   37           res.resume()
   38           if (process.version === 'v0.10.0') unstick(res)
   39 
   40           return cb(null, res)
   41           // Only retry on 408, 5xx or no `response`.
   42         } else if (statusCode === 408) {
   43           er = new Error('request timed out')
   44         } else if (statusCode >= 500) {
   45           er = new Error('server error ' + statusCode)
   46         }
   47 
   48         if (er && operation.retry(er)) {
   49           client.log.info('retry', 'will retry, error on last attempt: ' + er)
   50         } else {
   51           cb(new Error('fetch failed with status code ' + statusCode))
   52         }
   53       })
   54     })
   55   })
   56 }
   57 
   58 function unstick (response) {
   59   response.resume = (function (orig) {
   60     return function () {
   61       var ret = orig.apply(response, arguments)
   62       if (response.socket.encrypted) response.socket.encrypted.read(0)
   63       return ret
   64     }
   65   })(response.resume)
   66 }
   67 
   68 function makeRequest (remote, params, cb) {
   69   var parsed = url.parse(remote)
   70   this.log.http('fetch', 'GET', parsed.href)
   71 
   72   var headers = params.headers || {}
   73   var er = this.authify(
   74     params.auth && params.auth.alwaysAuth,
   75     parsed,
   76     headers,
   77     params.auth
   78   )
   79   if (er) return cb(er)
   80 
   81   var opts = this.initialize(
   82     parsed,
   83     'GET',
   84     'application/x-tar, application/vnd.github+json; q=0.1',
   85     headers
   86   )
   87   // always want to follow redirects for fetch
   88   opts.followRedirect = true
   89 
   90   cb(null, request(opts))
   91 }