Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

add cache for module with same version #8617

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var runInThisContext = require('vm').runInThisContext;
var runInNewContext = require('vm').runInNewContext;
var assert = require('assert').ok;
var fs = NativeModule.require('fs');

var moduleVersionCache = {};

// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
Expand Down Expand Up @@ -104,7 +104,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;
Expand All @@ -116,9 +116,24 @@ function readPackage(requestPath) {
function tryPackage(requestPath, exts) {
var pkg = readPackage(requestPath);

if (!pkg) return false;
if (!(pkg && pkg.main)) return false;

var name = pkg.name,
version = pkg.version,
main = pkg.main;

var cache = moduleVersionCache[name];
if(!cache){
cache = moduleVersionCache[name] = {};
}

var filename = cache[version];

if(!filename) {
filename = path.resolve(requestPath, main);
cache[version] = filename;
}

var filename = path.resolve(requestPath, pkg);
return tryFile(filename) || tryExtensions(filename, exts) ||
tryExtensions(path.resolve(filename, 'index'), exts);
}
Expand Down