Fix module resolution
Fixes a bug where a directory with the same name as a file would be used as the matching entry instead of that file depending on the order they appear in the tarball.
This commit is contained in:
parent
f88edc7c6b
commit
bfecd1955a
|
@ -30,10 +30,18 @@ function stripLeadingSegment(name) {
|
||||||
return name.replace(/^[^/]+\/?/, '');
|
return name.replace(/^[^/]+\/?/, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search the given tarball for entries that match the given name.
|
||||||
|
* Follows node's resolution algorithm.
|
||||||
|
* https://nodejs.org/api/modules.html#modules_all_together
|
||||||
|
*/
|
||||||
function searchEntries(tarballStream, entryName, wantsIndex) {
|
function searchEntries(tarballStream, entryName, wantsIndex) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const jsEntryName = `${entryName}.js`;
|
||||||
|
const jsonEntryName = `${entryName}.json`;
|
||||||
const entries = {};
|
const entries = {};
|
||||||
let foundEntry = null;
|
|
||||||
|
let foundEntry;
|
||||||
|
|
||||||
if (entryName === '') {
|
if (entryName === '') {
|
||||||
foundEntry = entries[''] = { name: '', type: 'directory' };
|
foundEntry = entries[''] = { name: '', type: 'directory' };
|
||||||
|
@ -41,7 +49,6 @@ function searchEntries(tarballStream, entryName, wantsIndex) {
|
||||||
|
|
||||||
tarballStream
|
tarballStream
|
||||||
.on('error', reject)
|
.on('error', reject)
|
||||||
.on('finish', () => resolve({ entries, foundEntry }))
|
|
||||||
.on('entry', (header, stream, next) => {
|
.on('entry', (header, stream, next) => {
|
||||||
const entry = {
|
const entry = {
|
||||||
// Most packages have header names that look like `package/index.js`
|
// Most packages have header names that look like `package/index.js`
|
||||||
|
@ -52,7 +59,7 @@ function searchEntries(tarballStream, entryName, wantsIndex) {
|
||||||
type: header.type
|
type: header.type
|
||||||
};
|
};
|
||||||
|
|
||||||
// We are only interested in files that match the entryName.
|
// Skip non-files and files that don't match the entryName.
|
||||||
if (entry.type !== 'file' || entry.name.indexOf(entryName) !== 0) {
|
if (entry.type !== 'file' || entry.name.indexOf(entryName) !== 0) {
|
||||||
stream.resume();
|
stream.resume();
|
||||||
stream.on('end', next);
|
stream.on('end', next);
|
||||||
|
@ -63,62 +70,74 @@ function searchEntries(tarballStream, entryName, wantsIndex) {
|
||||||
|
|
||||||
// Dynamically create "directory" entries for all directories
|
// Dynamically create "directory" entries for all directories
|
||||||
// that are in this file's path. Some tarballs omit these entries
|
// that are in this file's path. Some tarballs omit these entries
|
||||||
// for some reason, so this is the brute force method.
|
// for some reason, so this is the "brute force" method.
|
||||||
let dirname = path.dirname(entry.name);
|
let dir = path.dirname(entry.name);
|
||||||
while (dirname !== '.') {
|
while (dir !== '.') {
|
||||||
const directoryEntry = { name: dirname, type: 'directory' };
|
entries[dir] = entries[dir] || { name: dir, type: 'directory' };
|
||||||
|
dir = path.dirname(dir);
|
||||||
if (!entries[dirname]) {
|
|
||||||
entries[dirname] = directoryEntry;
|
|
||||||
|
|
||||||
if (directoryEntry.name === entryName) {
|
|
||||||
foundEntry = directoryEntry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dirname = path.dirname(dirname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the foundEntry variable if this entry name
|
|
||||||
// matches exactly or if it's an index.html file
|
|
||||||
// and the client wants HTML.
|
|
||||||
if (
|
if (
|
||||||
entry.name === entryName ||
|
entry.name === entryName ||
|
||||||
// Allow accessing e.g. `/index.js` or `/index.json` using
|
// Allow accessing e.g. `/index.js` or `/index.json`
|
||||||
// `/index` for compatibility with CommonJS
|
// using `/index` for compatibility with npm
|
||||||
(!wantsIndex && entry.name === `${entryName}.js`) ||
|
(!wantsIndex && entry.name === jsEntryName) ||
|
||||||
(!wantsIndex && entry.name === `${entryName}.json`)
|
(!wantsIndex && entry.name === jsonEntryName)
|
||||||
) {
|
) {
|
||||||
foundEntry = entry;
|
if (foundEntry) {
|
||||||
|
if (
|
||||||
|
foundEntry.name !== entryName &&
|
||||||
|
(entry.name === entryName ||
|
||||||
|
(entry.name === jsEntryName &&
|
||||||
|
foundEntry.name === jsonEntryName))
|
||||||
|
) {
|
||||||
|
// This entry is higher priority than the one
|
||||||
|
// we already found. Replace it.
|
||||||
|
delete foundEntry.content;
|
||||||
|
foundEntry = entry;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foundEntry = entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
|
|
||||||
stream.on('data', chunk => chunks.push(chunk)).on('end', () => {
|
stream
|
||||||
const content = Buffer.concat(chunks);
|
.on('data', chunk => chunks.push(chunk))
|
||||||
|
.on('end', () => {
|
||||||
|
const content = Buffer.concat(chunks);
|
||||||
|
|
||||||
// Set some extra properties for files that we will
|
// Set some extra properties for files that we will
|
||||||
// need to serve them and for ?meta listings.
|
// need to serve them and for ?meta listings.
|
||||||
entry.contentType = getContentType(entry.name);
|
entry.contentType = getContentType(entry.name);
|
||||||
entry.integrity = getIntegrity(content);
|
entry.integrity = getIntegrity(content);
|
||||||
entry.lastModified = header.mtime.toUTCString();
|
entry.lastModified = header.mtime.toUTCString();
|
||||||
entry.size = content.length;
|
entry.size = content.length;
|
||||||
|
|
||||||
// Set the content only for the foundEntry and
|
// Set the content only for the foundEntry and
|
||||||
// discard the buffer for all others.
|
// discard the buffer for all others.
|
||||||
if (entry === foundEntry) {
|
if (entry === foundEntry) {
|
||||||
entry.content = content;
|
entry.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.on('finish', () => {
|
||||||
|
resolve({
|
||||||
|
entries,
|
||||||
|
// If we didn't find a matching file entry,
|
||||||
|
// try a directory entry with the same name.
|
||||||
|
foundEntry: foundEntry || entries[entryName] || null
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const leadingSlash = /^\//;
|
const leadingSlash = /^\//;
|
||||||
const trailingSlash = /\/$/;
|
|
||||||
const multipleSlash = /\/\/+/;
|
const multipleSlash = /\/\/+/;
|
||||||
|
const trailingSlash = /\/$/;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch and search the archive to try and find the requested file.
|
* Fetch and search the archive to try and find the requested file.
|
||||||
|
@ -126,11 +145,13 @@ const multipleSlash = /\/\/+/;
|
||||||
*/
|
*/
|
||||||
export default function findFile(req, res, next) {
|
export default function findFile(req, res, next) {
|
||||||
fetchNpmPackage(req.packageConfig).then(tarballStream => {
|
fetchNpmPackage(req.packageConfig).then(tarballStream => {
|
||||||
|
const wantsIndex = trailingSlash.test(req.filename);
|
||||||
|
|
||||||
|
// The name of the file/directory we're looking for.
|
||||||
const entryName = req.filename
|
const entryName = req.filename
|
||||||
.replace(multipleSlash, '/')
|
.replace(multipleSlash, '/')
|
||||||
.replace(trailingSlash, '')
|
.replace(trailingSlash, '')
|
||||||
.replace(leadingSlash, '');
|
.replace(leadingSlash, '');
|
||||||
const wantsIndex = trailingSlash.test(req.filename);
|
|
||||||
|
|
||||||
searchEntries(tarballStream, entryName, wantsIndex).then(
|
searchEntries(tarballStream, entryName, wantsIndex).then(
|
||||||
({ entries, foundEntry }) => {
|
({ entries, foundEntry }) => {
|
||||||
|
|
Loading…
Reference in New Issue