File tree Expand file tree Collapse file tree 3 files changed +20
-7
lines changed
tests/node-specific/cached-data-create Expand file tree Collapse file tree 3 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -1792,7 +1792,7 @@ var AMDLoader;
1792
1792
callback ( ) ;
1793
1793
}
1794
1794
else {
1795
- var cachedDataPath_1 = _this . _path . join ( opts . nodeCachedDataDir , scriptSrc . replace ( / \\ | \/ / g , '' ) + '.code' ) ;
1795
+ var cachedDataPath_1 = _this . _getCachedDataPath ( opts . nodeCachedDataDir , scriptSrc ) ;
1796
1796
_this . _fs . readFile ( cachedDataPath_1 , function ( err , data ) {
1797
1797
// create script options
1798
1798
var scriptOptions = {
@@ -1841,6 +1841,11 @@ var AMDLoader;
1841
1841
} ) ;
1842
1842
}
1843
1843
} ;
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 ( / \. j s $ / , '' ) ;
1847
+ return this . _path . join ( baseDir , hash + "-" + basename + ".code" ) ;
1848
+ } ;
1844
1849
NodeScriptLoader . _runSoon = function ( callback , minTimeout ) {
1845
1850
var timeout = minTimeout + Math . ceil ( Math . random ( ) * minTimeout ) ;
1846
1851
setTimeout ( callback , timeout ) ;
Original file line number Diff line number Diff line change @@ -2251,6 +2251,7 @@ module AMDLoader {
2251
2251
interface INodePath {
2252
2252
dirname ( filename :string ) : string ;
2253
2253
normalize ( filename : string ) :string ;
2254
+ basename ( filename : string ) : string ;
2254
2255
join ( ...parts : string [ ] ) : string ;
2255
2256
}
2256
2257
@@ -2373,7 +2374,7 @@ module AMDLoader {
2373
2374
2374
2375
} else {
2375
2376
2376
- const cachedDataPath = this . _path . join ( opts . nodeCachedDataDir , scriptSrc . replace ( / \\ | \/ / g , '' ) + '.code' ) ;
2377
+ const cachedDataPath = this . _getCachedDataPath ( opts . nodeCachedDataDir , scriptSrc ) ;
2377
2378
2378
2379
this . _fs . readFile ( cachedDataPath , ( err , data ) => {
2379
2380
@@ -2431,6 +2432,12 @@ module AMDLoader {
2431
2432
}
2432
2433
}
2433
2434
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 ( / \. j s $ / , '' ) ;
2438
+ return this . _path . join ( baseDir , `${ hash } -${ basename } .code` ) ;
2439
+ }
2440
+
2434
2441
private static _runSoon ( callback : Function , minTimeout : number ) : void {
2435
2442
const timeout = minTimeout + Math . ceil ( Math . random ( ) * minTimeout ) ;
2436
2443
setTimeout ( callback , timeout ) ;
Original file line number Diff line number Diff line change @@ -4,7 +4,6 @@ var loader = require('../_loader');
4
4
var control = require ( '../_control' ) ;
5
5
6
6
var nodeCachedDataDir = path . join ( __dirname , 'cache-dir' ) ;
7
- var aFile = path . join ( __dirname , 'a.js' ) ;
8
7
9
8
loader . config ( {
10
9
nodeRequire : require ,
@@ -18,13 +17,15 @@ loader(['a'], function (a) {
18
17
19
18
setTimeout ( ( ) => {
20
19
21
- var cachePath = path . join ( nodeCachedDataDir , aFile . replace ( / \\ | \/ / g , '' ) + ' .code' ) ;
22
- if ( fs . existsSync ( cachePath ) ) {
20
+ const files = fs . readdirSync ( nodeCachedDataDir ) . filter ( p => / \ .c o d e $ / . test ( p ) ) ;
21
+ if ( files . length === 1 ) {
23
22
control . ok ( ) ;
24
23
} 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 ) ) ;
26
28
}
27
- fs . unlinkSync ( cachePath ) ;
28
29
29
30
} , 100 ) ;
30
31
} ) ;
You can’t perform that action at this time.
0 commit comments