Skip to content

Commit

Permalink
Remove usage of a WebWorker/worker thread (#2498)
Browse files Browse the repository at this point in the history
* Remove liveness pings

* Remove "current task" system

* Rename instance.ts -> raw-instance.ts

* Turn worker.ts into a simple object

* Remove the utilities that deal with the worker

* Provide the configuration when starting and not asychronously

* Use a Promise for state rather than an array

* Use queueOperation instead of handleMessage

* Turn generic handleMessage into individual functions

* Make worker.addChain return a Promise

* addChain is now properly async

* Make removeChain, request, and databaseContent always synchronous

* Track the active chains within worker.ts

* Fix state wasn't being set properly

* Make databaseContent return a Promise

* Provide logCallback in config and finish removing messages

* Remove mention of the worker

* Remove unused node:worker_threads import

* Finish removing the ToWorker messages

* Get rid of messages module

* Add some console.asserts

* No longer track chains in client.ts

* Move the number handling of databaseContent to worker.ts

* Rename worker to instance

* Pass onProcExit rather than throwing directly

* Add onPanic to smoldot bindings

* Clarify panic handling situation

* Finally properly handle crashes

* Properly format instance.ts

* Remove obsolete code

* Implement clean shutdown of the instance

* Fix removeChain and databaseContent still callable

* Document start_shutdown

* Clarify alreadyDestroyedError

* CHANGELOG

* Fix startShutdown handling of errors

* Spellcheck

* Rename

* More rename

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tomaka and mergify[bot] authored Jul 13, 2022
1 parent a5d9c67 commit c8af315
Show file tree
Hide file tree
Showing 22 changed files with 497 additions and 772 deletions.
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- No WebWorker/worker thread is spawned anymore by the JavaScript code. The WebAssembly virtual machine that runs smoldot is now directly instantiated by the `start` function. This should fix compatibility issues with various JavaScript bundlers. ([#2498](https://github.com/paritytech/smoldot/pull/2498))

## 0.6.23 - 2022-07-11

### Fixed
Expand Down
19 changes: 0 additions & 19 deletions bin/wasm-node/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,3 @@ chains must be passed as parameter to `addChain` as well. In situations where th
specifications passed to `addChain` are not trusted, it is important for security reasons to not
establish a parachain-relay-chain link between two chains that aren't part of the same "trust
sandbox".

# About the worker

The code in this package uses a web worker (in browsers) or a worker thread (on NodeJS). The
line of JavaScript that creates the worker is of the following form:

``` js
new Worker(new URL('./worker.js', import.meta.url), { type: "module" });
```
This format is compatible [with Webpack 5](https://webpack.js.org/guides/web-workers/), meaning
that Webpack will be able to resolve the imports in `worker.js` and adjust this little snippet.
This format also works in NodeJS without any issue.
However, at the time of writing of this comment, this format doesn't work with Parcel (both 1 and
2) due to various bugs.
As a general warning, be aware of the fact that this line might cause issues if you use a bundler.
3 changes: 1 addition & 2 deletions bin/wasm-node/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"test": "node prepare.js --debug && rimraf ./dist && tsc && ava --timeout=2m --concurrency 2 --no-worker-threads"
},
"browser": {
"./dist/compat/index.js": "./dist/compat/index-browser-overwrite.js",
"./dist/worker/spawn.js": "./dist/worker/spawn-browser-overwrite.js"
"./dist/compat/index.js": "./dist/compat/index-browser-overwrite.js"
},
"dependencies": {
"buffer": "^6.0.1",
Expand Down
10 changes: 5 additions & 5 deletions bin/wasm-node/javascript/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ child_process.execSync(
{ 'stdio': 'inherit' }
);

// The code below will write a variable number of files to the `src/worker/autogen` directory.
// The code below will write a variable number of files to the `src/instance/autogen` directory.
// Start by clearing all existing files from this directory in case there are some left from past
// builds.
const filesToRemove = fs.readdirSync('./src/worker/autogen');
const filesToRemove = fs.readdirSync('./src/instance/autogen');
for (const file of filesToRemove) {
if (!file.startsWith('.')) // Don't want to remove the `.gitignore` or `.npmignore` or similar
fs.unlinkSync(path.join("./src/worker/autogen", file));
fs.unlinkSync(path.join("./src/instance/autogen", file));
}

// We then do an optimization pass on the Wasm file, using `wasm-opt`.
Expand Down Expand Up @@ -119,13 +119,13 @@ try {
const chunk = base64Data.slice(0, 1024 * 1024);
// We could simply export the chunk instead of a function that returns the chunk, but that
// would cause TypeScript to generate a definitions file containing a copy of the entire chunk.
fs.writeFileSync('./src/worker/autogen/wasm' + fileNum + '.ts', 'export default function(): string { return "' + chunk + '"; }');
fs.writeFileSync('./src/instance/autogen/wasm' + fileNum + '.ts', 'export default function(): string { return "' + chunk + '"; }');
imports += 'import { default as wasm' + fileNum + ' } from \'./wasm' + fileNum + '.js\';\n';
chunksSum += ' + wasm' + fileNum + '()';
fileNum += 1;
base64Data = base64Data.slice(1024 * 1024);
}
fs.writeFileSync('./src/worker/autogen/wasm.ts', imports + 'export default ' + chunksSum + ';');
fs.writeFileSync('./src/instance/autogen/wasm.ts', imports + 'export default ' + chunksSum + ';');

} finally {
fs.rmSync(tmpDir, { recursive: true });
Expand Down
Loading

0 comments on commit c8af315

Please sign in to comment.