code-server/packages/protocol
Anmol Sethi 02f62882b8 Propagate full env to browser (#756)
* Propogate full env to browser

* Add support for setting $ITEM_URL

* Add serviceURL getter
2019-06-11 11:54:49 -05:00
..
scripts Propagate full env to browser (#756) 2019-06-11 11:54:49 -05:00
src Propagate full env to browser (#756) 2019-06-11 11:54:49 -05:00
test Extra extensions directories (#694) 2019-05-19 17:58:47 -05:00
README.md Fix coping and moving files around using the file tree (#568) 2019-04-24 10:38:21 -05:00
package.json Propagate full env to browser (#756) 2019-06-11 11:54:49 -05:00
yarn.lock Update node to 10.15.1 (#472) 2019-04-15 19:48:12 -05:00

README.md

Protocol

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. 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 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).