@@ -24,7 +24,7 @@ type SolidityCompilersList = {
24
24
25
25
export class VersionRange {
26
26
public config : StrategyOptions ;
27
- public cache : Cache ;
27
+ public cache : Cache | null ;
28
28
29
29
constructor ( options : StrategyOptions ) {
30
30
const defaultConfig = {
@@ -41,19 +41,32 @@ export class VersionRange {
41
41
} ;
42
42
this . config = Object . assign ( { } , defaultConfig , options ) ;
43
43
44
- this . cache = new Cache ( ) ;
44
+ switch ( options . cache ) {
45
+ case "fileSystem" :
46
+ this . cache = new Cache ( ) ;
47
+ break ;
48
+ case "noCache" :
49
+ this . cache = null ;
50
+ break ;
51
+ default :
52
+ this . cache = new Cache ( ) ;
53
+ }
45
54
}
46
55
47
56
async load ( versionRange : string ) {
48
57
const rangeIsSingleVersion = semver . valid ( versionRange ) ;
49
- if ( rangeIsSingleVersion && this . versionIsCached ( versionRange ) ) {
58
+ if (
59
+ rangeIsSingleVersion &&
60
+ this . cache &&
61
+ this . versionIsCached ( versionRange )
62
+ ) {
50
63
return this . getCachedSolcByVersionRange ( versionRange ) ;
51
64
}
52
65
53
66
try {
54
67
return await this . getSolcFromCacheOrUrl ( versionRange ) ;
55
68
} catch ( error ) {
56
- if ( error . message . includes ( "Failed to complete request" ) ) {
69
+ if ( error . message . includes ( "Failed to complete request" ) && this . cache ) {
57
70
return this . getSatisfyingVersionFromCache ( versionRange ) ;
58
71
}
59
72
throw error ;
@@ -117,6 +130,9 @@ export class VersionRange {
117
130
}
118
131
119
132
getCachedSolcByFileName ( fileName : string ) {
133
+ if ( ! this . cache ) {
134
+ this . throwNoCacheError ( ) ;
135
+ }
120
136
const listeners = observeListeners ( ) ;
121
137
try {
122
138
const filePath = this . cache . resolve ( fileName ) ;
@@ -130,6 +146,9 @@ export class VersionRange {
130
146
131
147
// Range can also be a single version specification like "0.5.0"
132
148
getCachedSolcByVersionRange ( version : string ) {
149
+ if ( ! this . cache ) {
150
+ this . throwNoCacheError ( ) ;
151
+ }
133
152
const cachedCompilerFileNames = this . cache . list ( ) ;
134
153
const validVersions = cachedCompilerFileNames . filter ( fileName => {
135
154
const match = fileName . match ( / v \d + \. \d + \. \d + .* / ) ;
@@ -144,6 +163,9 @@ export class VersionRange {
144
163
}
145
164
146
165
getCachedSolcFileName ( commit : string ) {
166
+ if ( ! this . cache ) {
167
+ this . throwNoCacheError ( ) ;
168
+ }
147
169
const cachedCompilerFileNames = this . cache . list ( ) ;
148
170
return cachedCompilerFileNames . find ( fileName => {
149
171
return fileName . includes ( commit ) ;
@@ -182,7 +204,9 @@ export class VersionRange {
182
204
throw error ;
183
205
}
184
206
events . emit ( "downloadCompiler:succeed" ) ;
185
- this . cache . add ( response . data , fileName ) ;
207
+ if ( this . cache ) {
208
+ this . cache . add ( response . data , fileName ) ;
209
+ }
186
210
return this . compilerFromString ( response . data ) ;
187
211
}
188
212
@@ -219,7 +243,7 @@ export class VersionRange {
219
243
) ;
220
244
if ( ! fileName ) throw new NoVersionError ( versionToUse ) ;
221
245
222
- if ( this . cache . has ( fileName ) ) {
246
+ if ( this . cache ? .has ( fileName ) ) {
223
247
return this . getCachedSolcByFileName ( fileName ) ;
224
248
}
225
249
return await this . getAndCacheSolcByUrl ( fileName , index ) ;
@@ -275,6 +299,9 @@ export class VersionRange {
275
299
}
276
300
277
301
versionIsCached ( version : string ) {
302
+ if ( ! this . cache ) {
303
+ return false ;
304
+ }
278
305
const cachedCompilerFileNames = this . cache . list ( ) ;
279
306
const cachedVersions = cachedCompilerFileNames
280
307
. map ( fileName => {
@@ -286,6 +313,13 @@ export class VersionRange {
286
313
semver . satisfies ( cachedVersion , version )
287
314
) ;
288
315
}
316
+
317
+ throwNoCacheError ( ) : never {
318
+ throw new Error (
319
+ "You are trying to access the cache but your configuration specifies " +
320
+ "no cache."
321
+ ) ;
322
+ }
289
323
}
290
324
291
325
export class NoUrlError extends Error {
0 commit comments