Skip to content

Commit f9c411e

Browse files
authored
Merge pull request #686 from ethereum/web-worker-example
Add web worker example in README
2 parents c572d36 + bd7a86f commit f9c411e

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

README.md

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -318,64 +318,47 @@ var output = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode)
318318

319319
## Browser Usage
320320

321-
Add the version of `solc` you want to use into `index.html`:
321+
Compilation is generally a long-running and resource intensive task that cannot reasonably be performed in the main thread of the browser.
322+
Some browsers even dissallow synchronous compilation on the main thread if the module is larger than 4KB.
323+
Thus, the only supported way to use `solc` in a web browser is through a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).
322324

323-
```html
324-
<script
325-
type="text/javascript"
326-
src="https://binaries.soliditylang.org/bin/{{ SOLC_VERSION }}.js"
327-
integrity="sha256-{{ BASE64_ENCODED_HASH_OF_SOLC_VERSION }}"
328-
crossorigin="anonymous"
329-
></script>
330-
```
325+
### Loading solc with web workers
331326

332-
This will load `solc` into the global variable `window.Module`.
333-
Alternatively, use `soljson-latest.js` as `{{ SOLC_VERSION }}.js` in the code snippet above to load the latest version.
327+
Web Workers allow you to run javascript in the background in the browser, letting the browser's main thread free to do whatever it needs to do.
328+
Please, see the minimal example of how to use `solc` with web workers below or check out this [repository](https://github.com/r0qs/solcjs-webworker-example) for a full demo.
334329

335-
It is recommended that you check the integrity of the resource being fetched before using it in your application.
336-
For that, you can use the [Subresource Integrity (SRI)](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) feature.
337-
Adding SRI configuration to your HTML script tag ensures that the resource will only be loaded by the browser if the cryptographic hashes match.
330+
* index.html
331+
```html
332+
<!DOCTYPE html>
333+
<html>
338334

339-
You can generate the SRI hash yourself based on the base64-encoded version of the sha256 hash of the release.
340-
For example, after downloading version `v0.8.16+commit.07a7930e`, run:
341-
```bash
342-
node -e "console.log(crypto.createHash('sha256').update(fs.readFileSync('./soljson-v0.8.16+commit.07a7930e.js', 'utf8')).digest('base64'))"
343-
```
344-
```
345-
J7KCDvk4BaZcdreUWklDJYLTBv0XoomFcJpR5kA2d8I=
346-
```
335+
<head>
336+
<meta charset="utf-8" />
337+
</head>
347338

348-
And update your `index.html` to:
349-
```html
350-
<script
351-
type="text/javascript"
352-
src="https://binaries.soliditylang.org/bin/soljson-v0.8.16+commit.07a7930e.js"
353-
integrity="sha256-J7KCDvk4BaZcdreUWklDJYLTBv0XoomFcJpR5kA2d8I="
354-
crossorigin="anonymous"
355-
></script>
356-
```
339+
<body>
340+
<script>
341+
var worker = new Worker('./dist/bundle.js');
342+
worker.addEventListener('message', function (e) {
343+
console.log(e.data.version)
344+
}, false);
357345
358-
Then use this inside JavaScript as:
346+
worker.postMessage({})
347+
</script>
348+
</body>
359349

360-
```javascript
361-
var wrapper = require('solc/wrapper');
362-
var solc = wrapper(window.Module);
350+
</html>
363351
```
364352

365-
Or in ES6 syntax:
366-
353+
* worker.js:
367354
```javascript
355+
importScripts('https://binaries.soliditylang.org/bin/soljson-v0.8.19+commit.7dd6d404.js')
368356
import wrapper from 'solc/wrapper';
369-
const solc = wrapper(window.Module);
370-
```
371357

372-
Alternatively, to iterate the releases, one can load `list.js` from `solc-bin`:
373-
374-
```html
375-
<script
376-
type="text/javascript"
377-
src="https://binaries.soliditylang.org/bin/list.js"
378-
></script>
358+
self.addEventListener('message', () => {
359+
const compiler = wrapper(self.Module)
360+
self.postMessage({
361+
version: compiler.version()
362+
})
363+
}, false)
379364
```
380-
381-
This will result in two global variables, `window.soljsonReleases` listing all releases and `window.soljsonSources` listing all nightly builds and releases.

0 commit comments

Comments
 (0)