1
1
'use strict' ;
2
2
3
- const path = require ( "path" ) ;
4
- const fs = require ( "fs" ) ;
5
- const assert = require ( "assert" ) ;
6
- const LRU = require ( "lru-cache" ) ;
7
- const utils = require ( "./utils" ) ;
8
- const pkgVersion = require ( "./package.json" ) . version ;
9
- const random = require ( "random-js" ) ( ) ;
3
+ var path = require ( "path" ) ;
4
+ var fs = require ( "fs" ) ;
5
+ var assert = require ( "assert" ) ;
6
+ var LRU = require ( "lru-cache" ) ;
7
+ var utils = require ( "./utils" ) ;
8
+ var pkgVersion = require ( "./package.json" ) . version ;
9
+ var random = require ( "random-js" ) ( ) ;
10
10
11
11
function ensureCacheDir ( cacheDir ) {
12
12
cacheDir = path . resolve (
@@ -29,113 +29,111 @@ function ensureCacheDir(cacheDir) {
29
29
return cacheDir ;
30
30
}
31
31
32
- class Cache {
32
+ function Cache ( compileFn , cacheDir ) {
33
+ assert . ok ( this instanceof Cache ) ;
34
+ assert . strictEqual ( typeof compileFn , "function" ) ;
33
35
34
- constructor ( compileFn , cacheDir ) {
35
- assert . strictEqual ( typeof compileFn , "function" ) ;
36
+ this . compileFn = compileFn ;
37
+ this . cacheDir = ensureCacheDir ( cacheDir ) ;
36
38
37
- this . compileFn = compileFn ;
38
- this . cacheDir = ensureCacheDir ( cacheDir ) ;
39
+ var maxSize = process . env . TYPESCRIPT_CACHE_SIZE ;
40
+ this . _cache = new LRU ( {
41
+ max : maxSize || 1024 * 10 * 10
42
+ } ) ;
43
+ } ;
39
44
40
- const maxSize = process . env . TYPESCRIPT_CACHE_SIZE ;
41
- this . _cache = new LRU ( {
42
- max : maxSize || 1024 * 10 * 10
43
- } ) ;
44
- }
45
+ exports . Cache = Cache ;
45
46
46
- get ( source , options ) {
47
- let cacheKey = utils . deepHash ( pkgVersion , source , options ) ;
48
- let compileResult = this . _cache . get ( cacheKey ) ;
47
+ var Cp = Cache . prototype ;
49
48
50
- if ( ! compileResult ) {
51
- compileResult = this . _readCache ( cacheKey ) ;
52
- }
49
+ Cp . get = function ( source , options ) {
50
+ var cacheKey = utils . deepHash ( pkgVersion , source , options ) ;
51
+ var compileResult = this . _cache . get ( cacheKey ) ;
53
52
54
- if ( ! compileResult ) {
55
- compileResult = this . compileFn ( source , options ) ;
56
- this . _cache . set ( cacheKey , compileResult ) ;
57
- this . _writeCacheAsync ( cacheKey , compileResult ) ;
58
- }
59
-
60
- return compileResult ;
53
+ if ( ! compileResult ) {
54
+ compileResult = this . _readCache ( cacheKey ) ;
61
55
}
62
56
63
- _cacheFilename ( cacheKey ) {
64
- // We want cacheKeys to be hex so that they work on any FS
65
- // and never end in .cache.
66
- if ( ! / ^ [ a - f 0 - 9 ] + $ / . test ( cacheKey ) ) {
67
- throw Error ( 'bad cacheKey: ' + cacheKey ) ;
68
- }
69
-
70
- return path . join ( this . cacheDir , cacheKey + '.cache' ) ;
57
+ if ( ! compileResult ) {
58
+ compileResult = this . compileFn ( source , options ) ;
59
+ this . _cache . set ( cacheKey , compileResult ) ;
60
+ this . _writeCacheAsync ( cacheKey , compileResult ) ;
71
61
}
72
62
73
- _readFileOrNull ( filename ) {
74
- try {
75
- return fs . readFileSync ( filename , 'utf8' ) ;
76
- } catch ( e ) {
77
- if ( e && e . code === 'ENOENT' )
78
- return null ;
79
- throw e ;
80
- }
81
- }
63
+ return compileResult ;
64
+ }
82
65
83
- _parseJSONOrNull ( json ) {
84
- try {
85
- return JSON . parse ( json ) ;
86
- } catch ( e ) {
87
- if ( e instanceof SyntaxError )
88
- return null ;
89
- throw e ;
90
- }
66
+ Cp . _cacheFilename = function ( cacheKey ) {
67
+ // We want cacheKeys to be hex so that they work on any FS
68
+ // and never end in .cache.
69
+ if ( ! / ^ [ a - f 0 - 9 ] + $ / . test ( cacheKey ) ) {
70
+ throw Error ( 'bad cacheKey: ' + cacheKey ) ;
91
71
}
92
72
93
- // Returns null if the file does not exist or can't be parsed; otherwise
94
- // returns the parsed compileResult in the file.
95
- _readAndParseCompileResultOrNull ( filename ) {
96
- var content = this . _readFileOrNull ( filename ) ;
97
- return this . _parseJSONOrNull ( content ) ;
98
- }
73
+ return path . join ( this . cacheDir , cacheKey + '.cache' ) ;
74
+ }
99
75
100
- _readCache ( cacheKey ) {
101
- if ( ! this . cacheDir ) {
76
+ Cp . _readFileOrNull = function ( filename ) {
77
+ try {
78
+ return fs . readFileSync ( filename , 'utf8' ) ;
79
+ } catch ( e ) {
80
+ if ( e && e . code === 'ENOENT' )
102
81
return null ;
103
- }
82
+ throw e ;
83
+ }
84
+ }
104
85
105
- var cacheFilename = this . _cacheFilename ( cacheKey ) ;
106
- var compileResult = this . _readAndParseCompileResultOrNull ( cacheFilename ) ;
107
- if ( ! compileResult ) {
86
+ Cp . _parseJSONOrNull = function ( json ) {
87
+ try {
88
+ return JSON . parse ( json ) ;
89
+ } catch ( e ) {
90
+ if ( e instanceof SyntaxError )
108
91
return null ;
109
- }
110
- this . _cache . set ( cacheKey , compileResult ) ;
111
-
112
- return compileResult ;
92
+ throw e ;
113
93
}
94
+ }
114
95
115
- // We want to write the file atomically.
116
- // But we also don't want to block processing on the file write.
117
- _writeFileAsync ( filename , contents ) {
118
- var tempFilename = filename + '.tmp.' + random . uuid4 ( ) ;
119
- fs . writeFile ( tempFilename , contents , ( err ) => {
120
- // ignore errors, it's just a cache
121
- if ( err ) {
122
- return ;
123
- }
124
- fs . rename ( tempFilename , filename , ( err ) => {
125
- // ignore this error too.
126
- } ) ;
127
- } ) ;
128
- }
96
+ // Returns null if the file does not exist or can't be parsed; otherwise
97
+ // returns the parsed compileResult in the file.
98
+ Cp . _readAndParseCompileResultOrNull = function ( filename ) {
99
+ var content = this . _readFileOrNull ( filename ) ;
100
+ return this . _parseJSONOrNull ( content ) ;
101
+ }
129
102
130
- _writeCacheAsync ( cacheKey , compileResult ) {
131
- if ( ! this . cacheDir ) return ;
103
+ Cp . _readCache = function ( cacheKey ) {
104
+ if ( ! this . cacheDir ) {
105
+ return null ;
106
+ }
132
107
133
- var cacheFilename = this . _cacheFilename ( cacheKey ) ;
134
- var cacheContents = JSON . stringify ( compileResult ) ;
135
- this . _writeFileAsync ( cacheFilename , cacheContents ) ;
108
+ var cacheFilename = this . _cacheFilename ( cacheKey ) ;
109
+ var compileResult = this . _readAndParseCompileResultOrNull ( cacheFilename ) ;
110
+ if ( ! compileResult ) {
111
+ return null ;
136
112
}
113
+ this . _cache . set ( cacheKey , compileResult ) ;
137
114
115
+ return compileResult ;
138
116
}
139
117
140
- exports . Cache = Cache ;
118
+ // We want to write the file atomically.
119
+ // But we also don't want to block processing on the file write.
120
+ Cp . _writeFileAsync = function ( filename , contents ) {
121
+ var tempFilename = filename + '.tmp.' + random . uuid4 ( ) ;
122
+ fs . writeFile ( tempFilename , contents , function ( err ) {
123
+ // ignore errors, it's just a cache
124
+ if ( err ) {
125
+ return ;
126
+ }
127
+ fs . rename ( tempFilename , filename , function ( err ) {
128
+ // ignore this error too.
129
+ } ) ;
130
+ } ) ;
131
+ }
141
132
133
+ Cp . _writeCacheAsync = function ( cacheKey , compileResult ) {
134
+ if ( ! this . cacheDir ) return ;
135
+
136
+ var cacheFilename = this . _cacheFilename ( cacheKey ) ;
137
+ var cacheContents = JSON . stringify ( compileResult ) ;
138
+ this . _writeFileAsync ( cacheFilename , cacheContents ) ;
139
+ }
0 commit comments