"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/cache/add-local-tarball.js" (8 Mar 2017, 5816 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 mkdir = require('mkdirp')
    2 var assert = require('assert')
    3 var fs = require('graceful-fs')
    4 var writeFileAtomic = require('write-file-atomic')
    5 var path = require('path')
    6 var sha = require('sha')
    7 var npm = require('../npm.js')
    8 var log = require('npmlog')
    9 var tar = require('../utils/tar.js')
   10 var pathIsInside = require('path-is-inside')
   11 var getCacheStat = require('./get-stat.js')
   12 var cachedPackageRoot = require('./cached-package-root.js')
   13 var chownr = require('chownr')
   14 var inflight = require('inflight')
   15 var once = require('once')
   16 var writeStreamAtomic = require('fs-write-stream-atomic')
   17 var tempFilename = require('../utils/temp-filename.js')
   18 var rimraf = require('rimraf')
   19 var packageId = require('../utils/package-id.js')
   20 
   21 module.exports = addLocalTarball
   22 
   23 function addLocalTarball (p, pkgData, shasum, cb) {
   24   assert(typeof p === 'string', 'must have path')
   25   assert(typeof cb === 'function', 'must have callback')
   26 
   27   if (!pkgData) pkgData = {}
   28 
   29   // If we don't have a shasum yet, compute it.
   30   if (!shasum) {
   31     return sha.get(p, function (er, shasum) {
   32       if (er) return cb(er)
   33       log.silly('addLocalTarball', 'shasum (computed)', shasum)
   34       addLocalTarball(p, pkgData, shasum, cb)
   35     })
   36   }
   37 
   38   if (pathIsInside(p, npm.cache)) {
   39     if (path.basename(p) !== 'package.tgz') {
   40       return cb(new Error('Not a valid cache tarball name: ' + p))
   41     }
   42     log.verbose('addLocalTarball', 'adding from inside cache', p)
   43     return addPlacedTarball(p, pkgData, shasum, cb)
   44   }
   45 
   46   addTmpTarball(p, pkgData, shasum, function (er, data) {
   47     if (data) {
   48       data._resolved = p
   49       data._shasum = data._shasum || shasum
   50     }
   51     return cb(er, data)
   52   })
   53 }
   54 
   55 function addPlacedTarball (p, pkgData, shasum, cb) {
   56   assert(pkgData, 'should have package data by now')
   57   assert(typeof cb === 'function', 'cb function required')
   58 
   59   getCacheStat(function (er, cs) {
   60     if (er) return cb(er)
   61     return addPlacedTarball_(p, pkgData, cs.uid, cs.gid, shasum, cb)
   62   })
   63 }
   64 
   65 function addPlacedTarball_ (p, pkgData, uid, gid, resolvedSum, cb) {
   66   var folder = path.join(cachedPackageRoot(pkgData), 'package')
   67 
   68   // First, make sure we have the shasum, if we don't already.
   69   if (!resolvedSum) {
   70     sha.get(p, function (er, shasum) {
   71       if (er) return cb(er)
   72       addPlacedTarball_(p, pkgData, uid, gid, shasum, cb)
   73     })
   74     return
   75   }
   76 
   77   mkdir(folder, function (er) {
   78     if (er) return cb(er)
   79     var pj = path.join(folder, 'package.json')
   80     var json = JSON.stringify(pkgData, null, 2)
   81     writeFileAtomic(pj, json, function (er) {
   82       cb(er, pkgData)
   83     })
   84   })
   85 }
   86 
   87 function addTmpTarball (tgz, pkgData, shasum, cb) {
   88   assert(typeof cb === 'function', 'must have callback function')
   89   assert(shasum, 'must have shasum by now')
   90 
   91   cb = inflight('addTmpTarball:' + tgz, cb)
   92   if (!cb) return log.verbose('addTmpTarball', tgz, 'already in flight; not adding')
   93   log.verbose('addTmpTarball', tgz, 'not in flight; adding')
   94 
   95   // we already have the package info, so just move into place
   96   if (pkgData && pkgData.name && pkgData.version) {
   97     log.verbose(
   98       'addTmpTarball',
   99       'already have metadata; skipping unpack for',
  100       packageId(pkgData)
  101     )
  102     return addTmpTarball_(tgz, pkgData, shasum, cb)
  103   }
  104 
  105   // This is a tarball we probably downloaded from the internet.  The shasum's
  106   // already been checked, but we haven't ever had a peek inside, so we unpack
  107   // it here just to make sure it is what it says it is.
  108   //
  109   // NOTE: we might not have any clue what we think it is, for example if the
  110   // user just did `npm install ./foo.tgz`
  111 
  112   var target = tempFilename('unpack')
  113   getCacheStat(function (er, cs) {
  114     if (er) return cb(er)
  115 
  116     log.verbose('addTmpTarball', 'validating metadata from', tgz)
  117     tar.unpack(tgz, target, null, null, cs.uid, cs.gid, function (unpackEr, data) {
  118       // cleanup the extracted package and move on with the metadata
  119       rimraf(target, function () {
  120         if (unpackEr) return cb(unpackEr)
  121         // check that this is what we expected.
  122         if (!data.name) {
  123           return cb(new Error('No name provided'))
  124         } else if (pkgData.name && data.name !== pkgData.name) {
  125           return cb(new Error('Invalid Package: expected ' + pkgData.name +
  126                               ' but found ' + data.name))
  127         }
  128 
  129         if (!data.version) {
  130           return cb(new Error('No version provided'))
  131         } else if (pkgData.version && data.version !== pkgData.version) {
  132           return cb(new Error('Invalid Package: expected ' +
  133                               packageId(pkgData) +
  134                               ' but found ' + packageId(data)))
  135         }
  136 
  137         addTmpTarball_(tgz, data, shasum, cb)
  138       })
  139     })
  140   })
  141 }
  142 
  143 function addTmpTarball_ (tgz, data, shasum, cb) {
  144   assert(typeof cb === 'function', 'must have callback function')
  145   cb = once(cb)
  146 
  147   assert(data.name, 'should have package name by now')
  148   assert(data.version, 'should have package version by now')
  149 
  150   var root = cachedPackageRoot(data)
  151   var pkg = path.resolve(root, 'package')
  152   var target = path.resolve(root, 'package.tgz')
  153   getCacheStat(function (er, cs) {
  154     if (er) return cb(er)
  155     mkdir(pkg, function (er, created) {
  156       // chown starting from the first dir created by mkdirp,
  157       // or the root dir, if none had to be created, so that
  158       // we know that we get all the children.
  159       function chown () {
  160         chownr(created || root, cs.uid, cs.gid, done)
  161       }
  162 
  163       if (er) return cb(er)
  164       var read = fs.createReadStream(tgz)
  165       var write = writeStreamAtomic(target, { mode: npm.modes.file })
  166       var fin = cs.uid && cs.gid ? chown : done
  167       read.on('error', cb).pipe(write).on('error', cb).on('close', fin)
  168     })
  169   })
  170 
  171   function done () {
  172     data._shasum = data._shasum || shasum
  173     cb(null, data)
  174   }
  175 }