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

Commit cdd6c2e

Browse files
committed
set up utility for compiling in webworker, tweak webpack config, and re-organize VersionRange strategy
1 parent 74b4812 commit cdd6c2e

File tree

15 files changed

+163
-15
lines changed

15 files changed

+163
-15
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"BUNDLE_CHAIN_FILENAME": "readonly",
1010
"BUNDLE_ANALYTICS_FILENAME": "readonly",
1111
"BUNDLE_LIBRARY_FILENAME": "readonly",
12-
"BUNDLE_CONSOLE_CHILD_FILENAME": "readonly"
12+
"BUNDLE_CONSOLE_CHILD_FILENAME": "readonly",
13+
"WORKER_CODE_BASE64": "readonly"
1314
},
1415
"extends": [
1516
"plugin:react-hooks/recommended"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
2+
src/compileInWebWorker/bundledWorker.js
23
node_modules
34
yarn.lock
45
package-lock.json

packages/compile-solidity/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@
4646
"babel-preset-env": "^1.6.1",
4747
"chai": "^4.2.0",
4848
"glob": "^7.1.6",
49+
"https-browserify": "^1.0.0",
4950
"mocha": "10.1.0",
51+
"process": "^0.11.10",
5052
"sinon": "^9.0.2",
53+
"stream-browserify": "^3.0.0",
54+
"stream-http": "^3.2.0",
5155
"tmp": "^0.2.1",
5256
"ts-node": "10.7.0",
5357
"ttypescript": "1.5.13",
5458
"typescript": "^4.7.4",
55-
"typescript-transform-paths": "3.3.1"
59+
"typescript-transform-paths": "3.3.1",
60+
"webpack": "^5.74.0",
61+
"webpack-cli": "^4.10.0"
5662
},
5763
"keywords": [
5864
"compile",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-env browser */
2+
export async function compileInWebWorker({
3+
soljson,
4+
compilerInput
5+
}): Promise<any> {
6+
// @ts-ignore
7+
const dataUrl = `data:text/javascript;base64,${WORKER_CODE_BASE64}`;
8+
const worker = new Worker(dataUrl);
9+
return new Promise(resolve => {
10+
worker.addEventListener(
11+
"message",
12+
function (e) {
13+
const output = e.data;
14+
resolve({
15+
compilerOutput: output,
16+
// TODO: figure out where the version information is
17+
solcVersion: ""
18+
});
19+
},
20+
false
21+
);
22+
23+
worker.postMessage({ soljson, compilerInput });
24+
});
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-env worker */
2+
import solcWrap from "solc/wrapper";
3+
4+
self.addEventListener(
5+
"message",
6+
e => {
7+
const { soljson, compilerInput } = e.data;
8+
const solc = solcWrap(eval(soljson + "; Module;"));
9+
const output = JSON.parse(solc.compile(JSON.stringify(compilerInput)));
10+
self.postMessage(output);
11+
},
12+
false
13+
);

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class CompilerSupplier {
4343
if (cache) this.strategyOptions.cache = cache;
4444
}
4545

46-
async load() {
46+
getStrategy() {
4747
const userSpecification = this.version;
4848

4949
let strategy: CompilerSupplierStrategy;
@@ -53,7 +53,8 @@ export class CompilerSupplier {
5353
if (!userSpecification) {
5454
useSpecifiedLocal =
5555
userSpecification &&
56-
(fs.existsSync(userSpecification) || path.isAbsolute(userSpecification));
56+
(fs.existsSync(userSpecification) ||
57+
path.isAbsolute(userSpecification));
5758
}
5859
const isValidVersionRange = semver.validRange(userSpecification);
5960

@@ -66,7 +67,24 @@ export class CompilerSupplier {
6667
} else if (isValidVersionRange) {
6768
strategy = new VersionRange(this.strategyOptions);
6869
}
70+
return {
71+
strategy,
72+
userSpecification
73+
};
74+
}
6975

76+
async loadSoljson() {
77+
const { strategy, userSpecification } = this.getStrategy();
78+
if (strategy) {
79+
const soljson = await strategy.loadSoljson(userSpecification);
80+
return { soljson };
81+
} else {
82+
throw new BadInputError(userSpecification);
83+
}
84+
}
85+
86+
async load() {
87+
const { strategy, userSpecification } = this.getStrategy();
7088
if (strategy) {
7189
const solc = await strategy.load(userSpecification);
7290
return { solc };

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class Docker {
2626
this.cache = new Cache();
2727
}
2828

29+
loadSoljson() {
30+
throw new Error("Method loadSoljson not implemented for Docker strategy.");
31+
}
32+
2933
async load() {
3034
// Set a sensible limit for maxBuffer
3135
// See https://github.com/nodejs/node/pull/23027

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import solcWrap from "solc/wrapper";
44
import { observeListeners } from "../observeListeners";
55

66
export class Local {
7+
loadSoljson() {
8+
throw new Error("Method loadSoljson not implemented for Local strategy.");
9+
}
10+
711
load(localPath: string) {
812
const listeners = observeListeners();
913
try {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const { normalizeSolcVersion } = require("../normalizeSolcVersion");
33
const { NoVersionError } = require("../errors");
44

55
export class Native {
6+
loadSoljson() {
7+
throw new Error("Method loadSoljson not implemented for Native strategy.");
8+
}
9+
610
load() {
711
const versionString = this.validateAndGetSolcVersion();
812
const command = "solc --standard-json";

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class VersionRange {
5353
}
5454
}
5555

56-
async load(versionRange: string) {
56+
async loadSoljson(versionRange: string) {
5757
const rangeIsSingleVersion = semver.valid(versionRange);
5858
if (
5959
rangeIsSingleVersion &&
@@ -73,6 +73,11 @@ export class VersionRange {
7373
}
7474
}
7575

76+
async load(versionRange: string) {
77+
const soljson = await this.loadSoljson(versionRange);
78+
return await this.compilerFromString(soljson);
79+
}
80+
7681
async list(index = 0) {
7782
if (index >= this.config.compilerRoots!.length) {
7883
throw new Error(
@@ -109,7 +114,7 @@ export class VersionRange {
109114
};
110115
}
111116

112-
compilerFromString(code: string) {
117+
async compilerFromString(code: string) {
113118
const listeners = observeListeners();
114119
try {
115120
const soljson = requireFromString(code);
@@ -190,7 +195,7 @@ export class VersionRange {
190195
throw new NoVersionError(versionRange);
191196
}
192197

193-
async getAndCacheSolcByUrl(fileName: string, index: number) {
198+
async getAndCacheSoljsonByUrl(fileName: string, index: number) {
194199
const { events, compilerRoots } = this.config;
195200
const url = `${compilerRoots![index].replace(/\/+$/, "")}/${fileName}`;
196201
events.emit("downloadCompiler:start", {
@@ -207,7 +212,7 @@ export class VersionRange {
207212
if (this.cache) {
208213
this.cache.add(response.data, fileName);
209214
}
210-
return this.compilerFromString(response.data);
215+
return response.data;
211216
}
212217

213218
async getSolcFromCacheOrUrl(versionConstraint: string, index: number = 0) {
@@ -246,8 +251,9 @@ export class VersionRange {
246251
if (this.cache?.has(fileName)) {
247252
return this.getCachedSolcByFileName(fileName);
248253
}
249-
return await this.getAndCacheSolcByUrl(fileName, index);
254+
return await this.getAndCacheSoljsonByUrl(fileName, index);
250255
} catch (error) {
256+
debug("there was an error fetching soljson -- %o", error.message);
251257
const attemptNumber = index + 1;
252258
return await this.getSolcFromCacheOrUrl(versionConstraint, attemptNumber);
253259
}
@@ -264,6 +270,10 @@ export class VersionRange {
264270
const url = `${urlRoot.replace(/\/+$/, "")}/list.json`;
265271
try {
266272
const response = await axios.get(url, { maxRedirects: 50 });
273+
debug(
274+
`obtained the following version list from ${url} -- %o`,
275+
response.data
276+
);
267277
return response.data;
268278
} catch (error) {
269279
events.emit("fetchSolcList:fail");
@@ -292,8 +302,13 @@ export class VersionRange {
292302
}
293303

294304
const versionToUse = this.findNewestValidVersion(version, allVersions);
295-
296-
if (versionToUse) return allVersions.releases[versionToUse];
305+
if (versionToUse) {
306+
debug(
307+
"the solc filename that we will use -- %o",
308+
allVersions.releases[versionToUse]
309+
);
310+
return allVersions.releases[versionToUse];
311+
}
297312

298313
return null;
299314
}

0 commit comments

Comments
 (0)