Skip to content

Commit

Permalink
Don't panic when platformBindings.connect throws an exception (smol-d…
Browse files Browse the repository at this point in the history
…ot#795)

* Don't panic when platformBindings.connect throws an exception

* CHANGELOG link

* Docfix

* Tweak code a bit
  • Loading branch information
tomaka authored Jun 22, 2023
1 parent 8e5de78 commit 34d6a72
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- Fix not absorbing the JavaScript exception triggered by the browser when connecting to a `ws://` node when smoldot is embedded in a web page served over `https://`. ([#795](https://github.com/smol-dot/smoldot/pull/795))

## 1.0.10 - 2023-06-19

### Changed
Expand Down
84 changes: 49 additions & 35 deletions wasm-node/javascript/src/internals/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface PlatformBindings {
* Tries to open a new connection using the given configuration.
*
* @see Connection
* @throws {@link ConnectionError} If the multiaddress couldn't be parsed or contains an invalid protocol.
* @throws {@link Error}
*/
connect(config: ConnectionConfig): Connection;

Expand Down Expand Up @@ -331,40 +331,54 @@ export function start(options: ClientOptions, wasmModule: SmoldotBytecode | Prom
}
case "new-connection": {
const connectionId = event.connectionId;
state.connections.set(connectionId, platformBindings.connect({
address: event.address,
onConnectionReset(message) {
if (state.instance.status !== "ready")
throw new Error();
state.connections.delete(connectionId);
state.instance.instance.connectionReset(connectionId, message);
},
onMessage(message, streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamMessage(connectionId, message, streamId);
},
onStreamOpened(streamId, direction, initialWritableBytes) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamOpened(connectionId, streamId, direction, initialWritableBytes);
},
onOpen(info) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.connectionOpened(connectionId, info);
},
onWritableBytes(numExtra, streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamWritableBytes(connectionId, numExtra, streamId);
},
onStreamReset(streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamReset(connectionId, streamId);
},
}));

let connection;
try {
connection = platformBindings.connect({
address: event.address,
onConnectionReset(message) {
if (state.instance.status !== "ready")
throw new Error();
state.connections.delete(connectionId);
state.instance.instance.connectionReset(connectionId, message);
},
onMessage(message, streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamMessage(connectionId, message, streamId);
},
onStreamOpened(streamId, direction, initialWritableBytes) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamOpened(connectionId, streamId, direction, initialWritableBytes);
},
onOpen(info) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.connectionOpened(connectionId, info);
},
onWritableBytes(numExtra, streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamWritableBytes(connectionId, numExtra, streamId);
},
onStreamReset(streamId) {
if (state.instance.status !== "ready")
throw new Error();
state.instance.instance.streamReset(connectionId, streamId);
},
});

} catch(error) {
const errorMsg = error instanceof Error ? error.toString() : "Uncaught exception while connecting";
setTimeout(() => {
if (state.instance.status === 'ready')
state.instance.instance.connectionReset(connectionId, errorMsg);
}, 0);
break;
}

state.connections.set(connectionId, connection);
break;
}
case "connection-reset": {
Expand Down

0 comments on commit 34d6a72

Please sign in to comment.