-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
cache module with same version and same registry #3289
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ const path = require('path'); | |
const internalModuleReadFile = process.binding('fs').internalModuleReadFile; | ||
const internalModuleStat = process.binding('fs').internalModuleStat; | ||
|
||
|
||
var moduleCache = {}; | ||
// If obj.hasOwnProperty has been overridden, then calling | ||
// obj.hasOwnProperty(prop) will break. | ||
// See: https://github.com/joyent/node/issues/1707 | ||
|
@@ -76,7 +76,7 @@ function readPackage(requestPath) { | |
} | ||
|
||
try { | ||
var pkg = packageMainCache[requestPath] = JSON.parse(json).main; | ||
var pkg = packageMainCache[requestPath] = JSON.parse(json); | ||
} catch (e) { | ||
e.path = jsonPath; | ||
e.message = 'Error parsing ' + jsonPath + ': ' + e.message; | ||
|
@@ -88,9 +88,18 @@ function readPackage(requestPath) { | |
function tryPackage(requestPath, exts) { | ||
var pkg = readPackage(requestPath); | ||
|
||
if (!pkg) return false; | ||
if (!(pkg && pkg.main)) return false; | ||
|
||
var resolved = pkg['_resolved'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the _resolved property come from? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. What if _resolved is not as unique as this patch assumes? Things will break badly, won't they? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or replace to shasum? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean the _shasum field? Same issue: what if it's not unique, e.g., because someone accidentally copy-pasted it from one package.json to another? You could perhaps take the SHA-1 checksum of the whole package.json file but that requires that node is compiled with openssl support. You can add workarounds for the non-openssl case but I don't know, that gets complex awfully fast. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when running npm install , does it not download the source code from the _resolved url ? so the _resolved url got be unique, otherwise things will break badly, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or tarball |
||
main = pkg.main; | ||
|
||
var filename; | ||
if(resolved) { | ||
filename = moduleCache[resolved] || (moduleCache[resolved] = path.resolve(requestPath, main)); | ||
}else{ | ||
filename = path.resolve(requestPath, main); | ||
} | ||
|
||
var filename = path.resolve(requestPath, pkg); | ||
return tryFile(filename) || tryExtensions(filename, exts) || | ||
tryExtensions(path.resolve(filename, 'index'), exts); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个会导致内存中带有很多无关信息。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
相比于多次加载模块使用的内存少多了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use English please?