Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic when platformBindings.connect throws an exception #795

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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