Giter Site home page Giter Site logo

Comments (7)

indietyp avatar indietyp commented on May 30, 2024

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.

indietyp avatar indietyp commented on May 30, 2024

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.

thomaseizinger avatar thomaseizinger commented on May 30, 2024

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.

indietyp avatar indietyp commented on May 30, 2024

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 detect NodeJS environments instead of returning None (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.

thomaseizinger avatar thomaseizinger commented on May 30, 2024

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.

thomaseizinger avatar thomaseizinger commented on May 30, 2024

WebContext gains the ability to detect NodeJS environments instead of returning None (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:

pub(crate) fn set_interval_with_callback_and_timeout_and_arguments(
&self,
handler: &::js_sys::Function,
timeout: i32,
arguments: &::js_sys::Array,
) -> Result<i32, JsValue> {
match self {
WebContext::Window(w) => {
w.set_interval_with_callback_and_timeout_and_arguments(handler, timeout, arguments)
}
WebContext::Worker(w) => {
w.set_interval_with_callback_and_timeout_and_arguments(handler, timeout, arguments)
}
}
}
/// The `clearInterval()` method.
pub(crate) fn clear_interval_with_handle(&self, handle: i32) {
match self {
WebContext::Window(w) => w.clear_interval_with_handle(handle),
WebContext::Worker(w) => w.clear_interval_with_handle(handle),
}
}

from rust-libp2p.

indietyp avatar indietyp commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.