Add a js implementation of wasm-ext. (#1454)

* Squashed commit of the following:

commit 8d11f27165aecb6275c6a435e36d4fc38193b601
Author: Ashley Ruglys <ashley.ruglys@gmail.com>
Date:   Thu Feb 13 16:27:26 2020 +0100

    Fix implementation

commit 8f60187bcc3b57069eb8bd7ac530d9da3da4176e
Author: Ashley Ruglys <ashley.ruglys@gmail.com>
Date:   Thu Feb 13 12:38:35 2020 +0100

    Add implementation

commit 445fadea709c930b83b4f7905c030bb29c776eed
Author: Ashley Ruglys <ashley.ruglys@gmail.com>
Date:   Wed Feb 12 16:45:31 2020 +0100

    Use different features on different targets

commit e5d47c19fc06b96bad16f433a1526ecf4d9ce99b
Author: Ashley Ruglys <ashley.ruglys@gmail.com>
Date:   Mon Feb 10 13:23:50 2020 +0100

    Use ring on native

commit 8def133f8230885535765f8e244162cdb8901a34
Author: Ashley Ruglys <ashley.ruglys@gmail.com>
Date:   Fri Feb 7 14:46:52 2020 +0100

    Use the sha2 crate for sha512 hashing

commit 268880d2ed6510a0339d70184d7ad88f3efb39c4
Author: Ashley <ashley.ruglys@gmail.com>
Date:   Fri Feb 7 01:25:29 2020 +0100

    Fix documentation

commit 9769bc5242972c2c74e9b5e36960ca2513ddbb33
Author: Ashley <ashley.ruglys@gmail.com>
Date:   Fri Feb 7 01:17:20 2020 +0100

    Switch snow resolver to default

commit ac22537385ff81b274aec14a8c6f47a0b59606ce
Author: Ashley <ashley.ruglys@gmail.com>
Date:   Thu Feb 6 20:34:50 2020 +0100

    hmm...

* Feature gate

* Rename -> websocket_transport

* Update transports/wasm-ext/Cargo.toml

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update transports/wasm-ext/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
Ashley
2020-02-19 15:10:07 +01:00
committed by GitHub
parent bfc1934dc0
commit e84cbf278a
3 changed files with 163 additions and 1 deletions

View File

@@ -16,3 +16,6 @@ libp2p-core = { version = "0.16.0", path = "../../core" }
parity-send-wrapper = "0.1.0"
wasm-bindgen = "0.2.42"
wasm-bindgen-futures = "0.4.4"
[features]
websocket = []

View File

@@ -72,7 +72,7 @@ pub mod ffi {
#[wasm_bindgen(method, catch)]
pub fn listen_on(this: &Transport, multiaddr: &str) -> Result<js_sys::Iterator, JsValue>;
/// Returns a `ReadableStream` that .
/// Returns a `ReadableStream`.
#[wasm_bindgen(method, getter)]
pub fn read(this: &Connection) -> js_sys::Iterator;
@@ -123,6 +123,13 @@ pub mod ffi {
#[wasm_bindgen(method, getter)]
pub fn local_addr(this: &ConnectionEvent) -> String;
}
#[cfg(feature = "websocket")]
#[wasm_bindgen(module = "/src/websockets.js")]
extern "C" {
/// Returns a `Transport` implemented using websockets.
pub fn websocket_transport() -> Transport;
}
}
/// Implementation of `Transport` whose implementation is handled by some FFI.

View File

@@ -0,0 +1,152 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
export const websocket_transport = () => {
return {
dial: dial,
listen_on: (addr) => {
let err = new Error("Listening on WebSockets is not possible from within a browser");
err.name = "NotSupportedError";
throw err;
},
};
}
/// Turns a string multiaddress into a WebSockets string URL.
// TODO: support dns addresses as well
const multiaddr_to_ws = (addr) => {
let parsed = addr.match(/^\/(ip4|ip6|dns4|dns6)\/(.*?)\/tcp\/(.*?)\/(ws|wss|x-parity-ws\/(.*)|x-parity-wss\/(.*))$/);
let proto = 'wss';
if (parsed[4] == 'ws' || parsed[4] == 'x-parity-ws') {
proto = 'ws';
}
let url = decodeURIComponent(parsed[5] || parsed[6] || '');
if (parsed != null) {
if (parsed[1] == 'ip6') {
return proto + "://[" + parsed[2] + "]:" + parsed[3] + url;
} else {
return proto + "://" + parsed[2] + ":" + parsed[3] + url;
}
}
let err = new Error("Address not supported: " + addr);
err.name = "NotSupportedError";
throw err;
}
// Attempt to dial a multiaddress.
const dial = (addr) => {
let ws = new WebSocket(multiaddr_to_ws(addr));
let reader = read_queue();
return new Promise((resolve, reject) => {
// TODO: handle ws.onerror properly after dialing has happened
ws.onerror = (ev) => reject(ev);
ws.onmessage = (ev) => reader.inject_blob(ev.data);
ws.onclose = () => reader.inject_eof();
ws.onopen = () => resolve({
read: (function*() { while(ws.readyState == 1) { yield reader.next(); } })(),
write: (data) => {
if (ws.readyState == 1) {
ws.send(data);
return promise_when_ws_finished(ws);
} else {
return Promise.reject("WebSocket is closed");
}
},
shutdown: () => {},
close: () => ws.close()
});
});
}
// Takes a WebSocket object and returns a Promise that resolves when bufferedAmount is 0.
const promise_when_ws_finished = (ws) => {
if (ws.bufferedAmount == 0) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
setTimeout(function check() {
if (ws.bufferedAmount == 0) {
resolve();
} else {
setTimeout(check, 100);
}
}, 2);
})
}
// Creates a queue reading system.
const read_queue = () => {
// State of the queue.
let state = {
// Array of promises resolving to `ArrayBuffer`s, that haven't been transmitted back with
// `next` yet.
queue: new Array(),
// If `resolve` isn't null, it is a "resolve" function of a promise that has already been
// returned by `next`. It should be called with some data.
resolve: null,
};
return {
// Inserts a new Blob in the queue.
inject_blob: (blob) => {
if (state.resolve != null) {
var resolve = state.resolve;
state.resolve = null;
var reader = new FileReader();
reader.addEventListener("loadend", () => resolve(reader.result));
reader.readAsArrayBuffer(blob);
} else {
state.queue.push(new Promise((resolve, reject) => {
var reader = new FileReader();
reader.addEventListener("loadend", () => resolve(reader.result));
reader.readAsArrayBuffer(blob);
}));
}
},
// Inserts an EOF message in the queue.
inject_eof: () => {
if (state.resolve != null) {
var resolve = state.resolve;
state.resolve = null;
resolve(null);
} else {
state.queue.push(Promise.resolve(null));
}
},
// Returns a Promise that yields the next entry as an ArrayBuffer.
next: () => {
if (state.queue.length != 0) {
return state.queue.shift(0);
} else {
if (state.resolve !== null)
throw "Internal error: already have a pending promise";
return new Promise((resolve, reject) => {
state.resolve = resolve;
});
}
}
};
};