Revert "Fix extraction race condition"

This reverts commit 4aba460a63.
This commit is contained in:
MICHAEL JACKSON 2017-06-21 11:22:35 +10:00
parent 4aba460a63
commit 141e440b94
2 changed files with 61 additions and 85 deletions

View File

@ -4,7 +4,7 @@ const mkdirp = require('mkdirp')
const tar = require('tar-fs') const tar = require('tar-fs')
const RegistryCache = require('./RegistryCache') const RegistryCache = require('./RegistryCache')
const fetchPackageInfoFromRegistry = (registryURL, packageName) => { const getPackageInfoFromRegistry = (registryURL, packageName) => {
let encodedPackageName let encodedPackageName
if (packageName.charAt(0) === '@') { if (packageName.charAt(0) === '@') {
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}` encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`
@ -23,15 +23,14 @@ const fetchPackageInfoFromRegistry = (registryURL, packageName) => {
const PackageNotFound = 'PackageNotFound' const PackageNotFound = 'PackageNotFound'
const fetchPackageInfo = (registryURL, packageName) => const getPackageInfo = (registryURL, packageName, callback) => {
new Promise((resolve, reject) => {
RegistryCache.get(packageName, (error, value) => { RegistryCache.get(packageName, (error, value) => {
if (error) { if (error) {
reject(error) callback(error)
} else if (value) { } else if (value) {
resolve(value === PackageNotFound ? null : value) callback(null, value === PackageNotFound ? null : value)
} else { } else {
fetchPackageInfoFromRegistry(registryURL, packageName).then(value => { getPackageInfoFromRegistry(registryURL, packageName).then(value => {
if (value == null) { if (value == null) {
// Keep 404s in the cache for 5 minutes. This prevents us // Keep 404s in the cache for 5 minutes. This prevents us
// from making unnecessary requests to the registry for // from making unnecessary requests to the registry for
@ -43,15 +42,15 @@ const fetchPackageInfo = (registryURL, packageName) =>
RegistryCache.set(packageName, value, 60) RegistryCache.set(packageName, value, 60)
} }
resolve(value) callback(null, value)
}, error => { }, error => {
// Do not cache errors. // Do not cache errors.
RegistryCache.del(packageName) RegistryCache.del(packageName)
reject(error) callback(error)
}) })
} }
}) })
}) }
const normalizeTarHeader = (header) => { const normalizeTarHeader = (header) => {
// Most packages have header names that look like "package/index.js" // Most packages have header names that look like "package/index.js"
@ -62,12 +61,13 @@ const normalizeTarHeader = (header) => {
return header return header
} }
const fetchAndExtractPackage = (tarballURL, outputDir) => const getPackage = (tarballURL, outputDir, callback) => {
new Promise((resolve, reject) => {
mkdirp(outputDir, (error) => { mkdirp(outputDir, (error) => {
if (error) { if (error) {
reject(error) callback(error)
} else { } else {
let callbackWasCalled = false
fetch(tarballURL).then(response => { fetch(tarballURL).then(response => {
response.body response.body
.pipe(gunzip()) .pipe(gunzip())
@ -78,46 +78,19 @@ const fetchAndExtractPackage = (tarballURL, outputDir) =>
map: normalizeTarHeader map: normalizeTarHeader
}) })
) )
.on('finish', resolve) .on('finish', callback)
.on('error', reject) .on('error', (error) => {
}, reject) if (callbackWasCalled) // LOL node streams
} return
callbackWasCalled = true
callback(error)
}) })
}) })
}
const runCache = {}
// A helper that prevents running multiple async operations
// identified by the same key concurrently. Instead, the operation
// is performed only once the first time it is requested and all
// subsequent calls get that same result until it is completed.
const runOnce = (key, perform) => {
let promise = runCache[key]
if (!promise) {
promise = runCache[key] = perform()
// Clear the cache when we're done.
promise.then(() => {
delete runCache[key]
}, () => {
delete runCache[key]
}) })
} }
return promise
}
const getPackageInfo = (registryURL, packageName, callback) => {
runOnce(registryURL + packageName, () => fetchPackageInfo(registryURL, packageName))
.then(info => callback(null, info), callback)
}
const getPackage = (tarballURL, outputDir, callback) => {
runOnce(tarballURL + outputDir, () => fetchAndExtractPackage(tarballURL, outputDir))
.then(() => callback(null), callback)
}
module.exports = { module.exports = {
getPackageInfo, getPackageInfo,
getPackage getPackage

View File

@ -22,11 +22,14 @@ const oneMinute = 60
const oneDay = oneMinute * 60 * 24 const oneDay = oneMinute * 60 * 24
const oneYear = oneDay * 365 const oneYear = oneDay * 365
const checkLocalCache = (dir, filename, callback) => const checkLocalCache = (dir, callback) =>
statFile(joinPaths(dir, filename || 'package.json'), (error, stats) => { statFile(joinPaths(dir, 'package.json'), (error, stats) => {
callback(stats && stats.isFile()) callback(stats && stats.isFile())
}) })
const createTempPath = (name) =>
joinPaths(tmpdir(), `unpkg-${name}`)
const ResolveExtensions = [ '', '.js', '.json' ] const ResolveExtensions = [ '', '.js', '.json' ]
/** /**
@ -107,9 +110,9 @@ const createRequestHandler = (options = {}) => {
// Step 1: Fetch the package from the registry and store a local copy. // Step 1: Fetch the package from the registry and store a local copy.
// Redirect if the URL does not specify an exact version number. // Redirect if the URL does not specify an exact version number.
const fetchPackage = (next) => { const fetchPackage = (next) => {
const packageDir = joinPaths(tmpdir(), `unpkg-${displayName}`) const packageDir = createTempPath(displayName)
checkLocalCache(packageDir, filename, (isCached) => { checkLocalCache(packageDir, (isCached) => {
if (isCached) if (isCached)
return next(packageDir) // Best case: we already have this package on disk. return next(packageDir) // Best case: we already have this package on disk.