Comments (7)
Shiming with isomorphic-ws
I get panicked at [...]/libp2p-websocket-websys-0.3.1/src/lib.rs:308:14: to have a window or worker context
from rust-libp2p.
I continued digging and found that the "polyfill" worked. This is no real solution, but it seems not as hard to support as expected.
// @ts-ignore
global.WebSocket = require('isomorphic-ws');
const globalSetInterval = global.setInterval;
// @ts-ignore
global.setInterval = (...args) => {
// @ts-ignore
let timeout = globalSetInterval(...args);
return timeout[Symbol.toPrimitive]();
}
// @ts-ignore
global.WorkerGlobalScope = global;
Setting WebSocket
to isomorphic-ws
is good enough as a polyfill, but ideally, one could choose the implementation used between browser
and ws
. The crate's name is websys
, so I do not know if this is a "good" idea, as it would go away from the websys
part, then again, it's essential to have some sort even if only documentation as to how to set it up in a node environment.
setInterval
on node
returns a Timeout
instance instead of a number, that then can be supplied to clearInterval.
Either the conversion happens in the WebContext
, or the value is no longer expected to be a number.
global.WorkerGlobalScope
is set as otherwise WebContext
panics, this "tricks" websocket-websys
into thinking we're a webworker, even though we aren't. To properly support Node.JS, WebContext
would likely need a new variant called Node
or similar.
from rust-libp2p.
Thank you for opening this. We don't currently have or test support for NodeJS and I am not entirely sure we want to support it in a first-class way. How do other npm packages solve this problem? Do they detect that they are within NodeJS and use the appropriate APIs available? it is been a while that I did JS development but I'd see the responsibility on the NodeJS side to offer Web-compatible APIs. In other words, go with the polyfill method that you've already found to be working.
from rust-libp2p.
In multi-context environments, packages usually use isomorphic-ws
.
I have no problem adding that to the global scope, but the other hacks to make WebContext
work worry me, as these changes to global functions have a non-negligible chance of impacting other running code.
In other words, I think what would need to happen is:
WebContext
gains the ability to detectNodeJS
environments instead of returningNone
(leading to a panic)setInterval
return values are no longer cast to a number and are taken as an opaque object.- Changes to the documentation to signal that the polyfill is needed.
I'd see the responsibility on the NodeJS side to offer Web-compatible APIs
Usually, I'd agree with you here, and this would be something I'd expect as well (figuring this out surprised me), but this is the typical problem with JavaScript, the language (ECMAScript), and JavaScript's implementation. Still, the usage of NodeJS in the JS ecosystem warrants some special casing, especially if the changes required are relatively small.
from rust-libp2p.
setInterval
return values are no longer cast to a number and are taken as an opaque object.
Yes, I think we've done something similar in the past actually. I can't find the piece of code now but I thought that we somehow managed to carry the interval handle as an opaque object. We might have to define our own bindings so that we can say it returns a JsValue
instead of a number. This seems like a simple enough and isolated change that streamlines the usage of it in NodeJS environments. Would you be willing to submit a PR?
from rust-libp2p.
WebContext
gains the ability to detectNodeJS
environments instead of returningNone
(leading to a panic)
Perhaps even easier than the above, if we can reliably detect NodeJS, then we can just do the "right" thing here:
rust-libp2p/transports/websocket-websys/src/web_context.rs
Lines 34 to 56 in b6bb02b
from rust-libp2p.
Thanks for the input. I have already created a PR, which should solve the problem using an approach similar to your suggested one!
from rust-libp2p.
Related Issues (20)
- Refactor file-sharing example to use tokio instead of async-std
- `Gossipsub`'s substreams keep all connections alive after `idle_connection_timeout`
- mergify continously approving approved pull requests HOT 6
- WebRTC connection doesn't close after timeout HOT 2
- Promote common dependencies to workspace level
- file-sharing works only on local interface HOT 1
- kad: FIND_NODE not conform to specs HOT 9
- Improve request/response documentation HOT 4
- Compilation on docs.rs is failing
- mdns tokio timer panics HOT 2
- Signature verification fails on handshake if 1024 bit RSA key is used HOT 2
- relay: panics due to unimplemented time in wasm HOT 2
- kad: consume `FromSwarm::NewExternalAddrOfPeer` HOT 14
- Reduce `NewExternalAddrCandidate` reports from `identify`
- kad: make `automatic_bootstrap_interval` publicly configurable
- request-response: Inconsistent documentation.
- request-response: Document request/response max sizes HOT 4
- Active streams affect `yamux::connection::rtt` HOT 10
- rendezvous: Example "discovering with identify" doesn't work HOT 4
- kad: make bucket size configurable HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-libp2p.