Giter Site home page Giter Site logo

node-browser's Introduction

node-browser

This module provides a way for the browser to run Node modules like fs, net, etc.

Internals

Server-side proxies

The server-side proxies are regular classes that call native Node functions. The only thing special about them is that they must return promises and they must return serializable values.

The only exception to the promise rule are event-related methods such as onEvent and onDone (these are synchronous). The server will simply immediately bind and push all events it can to the client. It doesn't wait for the client to start listening. This prevents issues with the server not receiving the client's request to start listening in time.

However, there is a way to specify events that should not bind immediately and should wait for the client to request it, because some events (like data on a stream) cannot be bound immediately (because doing so changes how the stream behaves).

Client-side proxies

Client-side proxies are Proxy instances. They simply make remote calls for any method you call on it using a web socket. The only exception is for events. Each client proxy has a local emitter which it uses in place of a remote call (this allows the call to be completed synchronously on the client). Then when an event is received from the server, it gets emitted on that local emitter.

When an event is listened to, the proxy also notifies the server so it can start listening in case it isn't already (see the data example above). This only works for events that only fire after they are bound.

Client-side fills

The client-side fills implement the actual Node API and make calls to the server-side proxies using the client-side proxies.

When a proxy returns a proxy (for example fs.createWriteStream), that proxy is a promise (since communicating with the server is asynchronous). We have to return the fill from fs.createWriteStream synchronously, so that means the fill has to contain a proxy promise. To eliminate the need for calling then and to keep the code looking clean every time you use the proxy, the proxy is itself wrapped in another proxy which just calls the method after a then. This works since all the methods return promises (aside from the event methods, but those are not used by the fills directly—they are only used internally to forward events to the fill if it is an event emitter).

node-browser's People

Contributors

code-asher avatar

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.