Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 74b4812

Browse files
committed
add hook into cacheing in version range strategy and adjust source fetcher
1 parent 071cd0f commit 74b4812

File tree

6 files changed

+75
-16
lines changed

6 files changed

+75
-16
lines changed

packages/compile-solidity/src/compilerSupplier/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import { Docker, Local, Native, VersionRange } from "./loadingStrategies";
66

77
const defaultSolcVersion = "0.5.16";
88

9+
type CompilerSupplierConstructorArgs = {
10+
events?: any;
11+
solcConfig: {
12+
version?: string;
13+
docker?: boolean;
14+
compilerRoots?: string[];
15+
dockerTagsUrl?: string;
16+
spawn: any;
17+
};
18+
cache?: string;
19+
};
20+
921
type CompilerSupplierStrategy =
1022
| Docker
1123
| Native
@@ -15,10 +27,10 @@ type CompilerSupplierStrategy =
1527

1628
export class CompilerSupplier {
1729
private version: string;
18-
private docker: boolean;
30+
private docker?: boolean;
1931
private strategyOptions: StrategyOptions;
2032

21-
constructor({ events, solcConfig }) {
33+
constructor({ events, solcConfig, cache }: CompilerSupplierConstructorArgs) {
2234
const { version, docker, compilerRoots, dockerTagsUrl, spawn } = solcConfig;
2335
this.version = version ? version : defaultSolcVersion;
2436
this.docker = docker;
@@ -28,6 +40,7 @@ export class CompilerSupplier {
2840
if (compilerRoots) this.strategyOptions.compilerRoots = compilerRoots;
2941
if (events) this.strategyOptions.events = events;
3042
if (spawn) this.strategyOptions.spawn = spawn;
43+
if (cache) this.strategyOptions.cache = cache;
3144
}
3245

3346
async load() {
@@ -36,9 +49,12 @@ export class CompilerSupplier {
3649
let strategy: CompilerSupplierStrategy;
3750
const useDocker = this.docker;
3851
const useNative = userSpecification === "native";
39-
const useSpecifiedLocal =
40-
userSpecification &&
41-
(fs.existsSync(userSpecification) || path.isAbsolute(userSpecification));
52+
let useSpecifiedLocal: boolean | string | undefined;
53+
if (!userSpecification) {
54+
useSpecifiedLocal =
55+
userSpecification &&
56+
(fs.existsSync(userSpecification) || path.isAbsolute(userSpecification));
57+
}
4258
const isValidVersionRange = semver.validRange(userSpecification);
4359

4460
if (useDocker) {

packages/compile-solidity/src/compilerSupplier/loadingStrategies/VersionRange.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type SolidityCompilersList = {
2424

2525
export class VersionRange {
2626
public config: StrategyOptions;
27-
public cache: Cache;
27+
public cache: Cache | null;
2828

2929
constructor(options: StrategyOptions) {
3030
const defaultConfig = {
@@ -41,19 +41,32 @@ export class VersionRange {
4141
};
4242
this.config = Object.assign({}, defaultConfig, options);
4343

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+
}
4554
}
4655

4756
async load(versionRange: string) {
4857
const rangeIsSingleVersion = semver.valid(versionRange);
49-
if (rangeIsSingleVersion && this.versionIsCached(versionRange)) {
58+
if (
59+
rangeIsSingleVersion &&
60+
this.cache &&
61+
this.versionIsCached(versionRange)
62+
) {
5063
return this.getCachedSolcByVersionRange(versionRange);
5164
}
5265

5366
try {
5467
return await this.getSolcFromCacheOrUrl(versionRange);
5568
} catch (error) {
56-
if (error.message.includes("Failed to complete request")) {
69+
if (error.message.includes("Failed to complete request") && this.cache) {
5770
return this.getSatisfyingVersionFromCache(versionRange);
5871
}
5972
throw error;
@@ -117,6 +130,9 @@ export class VersionRange {
117130
}
118131

119132
getCachedSolcByFileName(fileName: string) {
133+
if (!this.cache) {
134+
this.throwNoCacheError();
135+
}
120136
const listeners = observeListeners();
121137
try {
122138
const filePath = this.cache.resolve(fileName);
@@ -130,6 +146,9 @@ export class VersionRange {
130146

131147
// Range can also be a single version specification like "0.5.0"
132148
getCachedSolcByVersionRange(version: string) {
149+
if (!this.cache) {
150+
this.throwNoCacheError();
151+
}
133152
const cachedCompilerFileNames = this.cache.list();
134153
const validVersions = cachedCompilerFileNames.filter(fileName => {
135154
const match = fileName.match(/v\d+\.\d+\.\d+.*/);
@@ -144,6 +163,9 @@ export class VersionRange {
144163
}
145164

146165
getCachedSolcFileName(commit: string) {
166+
if (!this.cache) {
167+
this.throwNoCacheError();
168+
}
147169
const cachedCompilerFileNames = this.cache.list();
148170
return cachedCompilerFileNames.find(fileName => {
149171
return fileName.includes(commit);
@@ -182,7 +204,9 @@ export class VersionRange {
182204
throw error;
183205
}
184206
events.emit("downloadCompiler:succeed");
185-
this.cache.add(response.data, fileName);
207+
if (this.cache) {
208+
this.cache.add(response.data, fileName);
209+
}
186210
return this.compilerFromString(response.data);
187211
}
188212

@@ -219,7 +243,7 @@ export class VersionRange {
219243
);
220244
if (!fileName) throw new NoVersionError(versionToUse);
221245

222-
if (this.cache.has(fileName)) {
246+
if (this.cache?.has(fileName)) {
223247
return this.getCachedSolcByFileName(fileName);
224248
}
225249
return await this.getAndCacheSolcByUrl(fileName, index);
@@ -275,6 +299,9 @@ export class VersionRange {
275299
}
276300

277301
versionIsCached(version: string) {
302+
if (!this.cache) {
303+
return false;
304+
}
278305
const cachedCompilerFileNames = this.cache.list();
279306
const cachedVersions = cachedCompilerFileNames
280307
.map(fileName => {
@@ -286,6 +313,13 @@ export class VersionRange {
286313
semver.satisfies(cachedVersion, version)
287314
);
288315
}
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+
}
289323
}
290324

291325
export class NoUrlError extends Error {

packages/compile-solidity/src/compilerSupplier/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export type StrategyOptions = {
77
spawn?: {
88
maxBuffer: number;
99
};
10+
cache?: string;
1011
};

packages/compile-solidity/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ async function invokeCompiler({ compilerInput, options, solc }): Promise<{
243243
const supplierOptions = {
244244
parser: options.parser,
245245
events: options.events,
246-
solcConfig: options.compilers.solc
246+
solcConfig: options.compilers.solc,
247+
cache: options.compilers.cache
247248
};
248249

249250
if (!solc) {

packages/fetch-and-compile/lib/fetch.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ async function tryFetchAndCompileAddress(
134134
solc: options
135135
}
136136
});
137+
138+
// @ts-ignore
139+
if (window) {
140+
externalConfig.compilers.cache = "noCache";
141+
}
142+
137143
//if using docker, transform it (this does nothing if not using docker)
138144
externalConfig = transformIfUsingDocker(
139145
externalConfig,

packages/source-fetcher/lib/common.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//these imports aren't actually necessary, but why not :)
2-
import util from "util";
3-
import { setTimeout } from "timers";
42
import type * as Types from "./types";
53

64
export function makeFilename(name: string, extension: string = ".sol"): string {
@@ -14,8 +12,11 @@ export function makeFilename(name: string, extension: string = ".sol"): string {
1412
}
1513
}
1614

17-
export const makeTimer: (milliseconds: number) => Promise<void> =
18-
util.promisify(setTimeout);
15+
export const makeTimer = (milliseconds: number): Promise<void> => {
16+
return new Promise(resolve => {
17+
setTimeout(() => resolve(), milliseconds);
18+
});
19+
};
1920

2021
export function removeLibraries(
2122
settings: Types.SolcSettings,

0 commit comments

Comments
 (0)