| 48 | } |
| 49 | |
| 50 | const getContents = async (manifest, tarball) => { |
| 51 | const files = [] |
| 52 | const bundled = new Set() |
| 53 | let totalEntries = 0 |
| 54 | let totalEntrySize = 0 |
| 55 | |
| 56 | // reads contents of tarball |
| 57 | const stream = tar.t({ |
| 58 | onentry (entry) { |
| 59 | totalEntries++ |
| 60 | totalEntrySize += entry.size |
| 61 | const p = entry.path |
| 62 | if (p.startsWith('package/node_modules/') && p !== 'package/node_modules/') { |
| 63 | const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1] |
| 64 | bundled.add(name) |
| 65 | } |
| 66 | files.push({ |
| 67 | path: entry.path.replace(/^package\//, ''), |
| 68 | size: entry.size, |
| 69 | mode: entry.mode, |
| 70 | }) |
| 71 | }, |
| 72 | }) |
| 73 | stream.end(tarball) |
| 74 | |
| 75 | const integrity = ssri.fromData(tarball, { |
| 76 | algorithms: ['sha1', 'sha512'], |
| 77 | }) |
| 78 | |
| 79 | const comparator = ({ path: a }, { path: b }) => localeCompare(a, b) |
| 80 | |
| 81 | const isUpper = str => { |
| 82 | const ch = str.charAt(0) |
| 83 | return ch === ch.toUpperCase() |
| 84 | } |
| 85 | |
| 86 | const uppers = files.filter(file => isUpper(file.path)) |
| 87 | const others = files.filter(file => !isUpper(file.path)) |
| 88 | |
| 89 | uppers.sort(comparator) |
| 90 | others.sort(comparator) |
| 91 | |
| 92 | const shasum = integrity.sha1[0].hexDigest() |
| 93 | return { |
| 94 | id: manifest._id || `${manifest.name}@${manifest.version}`, |
| 95 | name: manifest.name, |
| 96 | version: manifest.version, |
| 97 | size: tarball.length, |
| 98 | unpackedSize: totalEntrySize, |
| 99 | shasum, |
| 100 | integrity: ssri.parse(integrity.sha512[0]), |
| 101 | // @scope/packagename.tgz => scope-packagename.tgz |
| 102 | // we can safely use these global replace rules due to npm package naming rules |
| 103 | filename: `${manifest.name.replace('@', '').replace('/', '-')}-${manifest.version}.tgz`, |
| 104 | files: uppers.concat(others), |
| 105 | entryCount: totalEntries, |
| 106 | bundled: Array.from(bundled), |
| 107 | } |