Skip to content

Commit b1baad5

Browse files
committed
ensure cached data filenames work on windows
1 parent eeddf96 commit b1baad5

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/loader.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ var AMDLoader;
17921792
callback();
17931793
}
17941794
else {
1795-
var cachedDataPath_1 = _this._path.join(opts.nodeCachedDataDir, scriptSrc.replace(/\\|\//g, '') + '.code');
1795+
var cachedDataPath_1 = _this._getCachedDataPath(opts.nodeCachedDataDir, scriptSrc);
17961796
_this._fs.readFile(cachedDataPath_1, function (err, data) {
17971797
// create script options
17981798
var scriptOptions = {
@@ -1841,6 +1841,11 @@ var AMDLoader;
18411841
});
18421842
}
18431843
};
1844+
NodeScriptLoader.prototype._getCachedDataPath = function (baseDir, filename) {
1845+
var hash = this._crypto.createHash('md5').update(filename, 'utf8').digest('hex');
1846+
var basename = this._path.basename(filename).replace(/\.js$/, '');
1847+
return this._path.join(baseDir, hash + "-" + basename + ".code");
1848+
};
18441849
NodeScriptLoader._runSoon = function (callback, minTimeout) {
18451850
var timeout = minTimeout + Math.ceil(Math.random() * minTimeout);
18461851
setTimeout(callback, timeout);

src/loader.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,7 @@ module AMDLoader {
22512251
interface INodePath {
22522252
dirname(filename:string): string;
22532253
normalize(filename: string):string;
2254+
basename(filename: string): string;
22542255
join(...parts: string[]): string;
22552256
}
22562257

@@ -2373,7 +2374,7 @@ module AMDLoader {
23732374

23742375
} else {
23752376

2376-
const cachedDataPath = this._path.join(opts.nodeCachedDataDir, scriptSrc.replace(/\\|\//g, '') + '.code');
2377+
const cachedDataPath = this._getCachedDataPath(opts.nodeCachedDataDir, scriptSrc);
23772378

23782379
this._fs.readFile(cachedDataPath, (err, data) => {
23792380

@@ -2431,6 +2432,12 @@ module AMDLoader {
24312432
}
24322433
}
24332434

2435+
private _getCachedDataPath(baseDir: string, filename: string): string {
2436+
const hash = this._crypto.createHash('md5').update(filename, 'utf8').digest('hex');
2437+
const basename = this._path.basename(filename).replace(/\.js$/, '');
2438+
return this._path.join(baseDir, `${hash}-${basename}.code`);
2439+
}
2440+
24342441
private static _runSoon(callback: Function, minTimeout: number): void {
24352442
const timeout = minTimeout + Math.ceil(Math.random() * minTimeout);
24362443
setTimeout(callback, timeout);

tests/node-specific/cached-data-create/_test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var loader = require('../_loader');
44
var control = require('../_control');
55

66
var nodeCachedDataDir = path.join(__dirname, 'cache-dir');
7-
var aFile = path.join(__dirname, 'a.js');
87

98
loader.config({
109
nodeRequire: require,
@@ -18,13 +17,15 @@ loader(['a'], function (a) {
1817

1918
setTimeout(() => {
2019

21-
var cachePath = path.join(nodeCachedDataDir, aFile.replace(/\\|\//g, '') + '.code');
22-
if (fs.existsSync(cachePath)) {
20+
const files = fs.readdirSync(nodeCachedDataDir).filter(p => /\.code$/.test(p));
21+
if (files.length === 1) {
2322
control.ok();
2423
} else {
25-
control.err('Expected file: ' + cachePath);
24+
control.err('Unexpected .code-files' + files.join(', '));
25+
}
26+
for (const f of files) {
27+
fs.unlinkSync(path.join(nodeCachedDataDir, f));
2628
}
27-
fs.unlinkSync(cachePath);
2829

2930
}, 100);
3031
});

0 commit comments

Comments
 (0)