Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat(std): Create net and net.Socket Node Polyfill #8949

Closed
wants to merge 1 commit into from
Closed

[WIP] feat(std): Create net and net.Socket Node Polyfill #8949

wants to merge 1 commit into from

Conversation

ebebbington
Copy link
Contributor

@ebebbington ebebbington commented Dec 31, 2020

Partially address #5385

Only adds the net.Socket part of the API, but creates the top level net API

Checklist

  • Complete TS conversion of net.ts
  • Add tests
  • Update std/node readme to mention net has been created, but only supports net.Socket

Resources

@ebebbington
Copy link
Contributor Author

The net module from Node uses a lot of internalBinding()'s, so i'm not even sure if this is going to be portable.

I will stop working on this for now until i get back a response

@jopemachine
Copy link
Contributor

jopemachine commented Jan 1, 2021

I'm not sure this could be possible, but I think the net module is necessary to implement other node polyfills.

This is a part of the example implementation of Socket.

class Socket extends stream.Duplex {
  constructor(options: any) {
    super();
  }

  private connecting: boolean = false;
  public pending: boolean = false;
  public readyState: boolean = false;
  public bufferSize: number = 0;

  pause = () => {
    // It seems to pause related functions not exists in Deno.Writer
    notImplemented();
  };

  resume = () => {
    // It seems to pause related functions not exists in Deno.Writer
    notImplemented();
  };

  public setNoDelay(enable: boolean) {
    // It seems to setNoDelay related functions not exists in Deno.Conn
    notImplemented();
  }

  public setKeepAlive(setting: any, msecs: any) {
    // It seems to setKeepAlive related functions not exists in Deno.Conn
    notImplemented();
  }

  public address() {
    return this.conn!.localAddr;
  }

  public remoteAddress() {
    return this.conn!.remoteAddr;
  }

  public remoteFamily() {
    // It seems to connection family related field not exists in Deno.Conn
  }

  public remotePort() {}

  public destorySoon() {
    if (this.writable) this.end();

    if (this.writableFinished) this.destroy();
    else this.once("finish", this.destroy);
  }

  public bytesWritten() {}

  public conn: Deno.Conn | undefined = undefined;

  public connect(
    options: TcpSocketConnectOpts | IpcSocketConnectOpts,
    connectionListener?: () => void
  ) {
    let connectionPromise: Promise<Deno.Conn>;

    if ("port" in options) {
      // tcp
      options = options as TcpSocketConnectOpts;
      connectionPromise = Deno.connect({
        port: options.port,
        transport: "tcp",
      });
    } else {
      // ipc (not support windows using named pipe)
      options = options as IpcSocketConnectOpts;
      connectionPromise = Deno.connect({
        path: options.path,
        transport: "unix",
      });
    }

    let cb = (con: Deno.Conn) => {
      this.conn = con;
    };

    cb = cb.bind(this);

    connectionPromise.then(cb);
  }

}

In the above code, I think this.conn (the return type of Deno.connect) corresponds to handle of nodejs.

And I think maybe in Server class, return type of Deno.listen would corresponds to handle of nodejs.


handle of nodejs is Pipe or TCP instance created in the below function.

(https://github.com/nodejs/node/blob/master/lib/net.js#L142)

This is the internal binding of tcp_wrap, pipe_wrap.


And according to what I've seen, some method or field in handle of nodejs seems to not exist on Deno side.

So, I think we need to migrate these sorts of functions to the rust side first.

(Please ignore this comment if my investigation was insufficient and there are some functions performing similar feature in Deno.)

@ebebbington
Copy link
Contributor Author

@jopemachine Definitely a good and useful writeup - so maybe a lot of the functionality just throws a not implemented. This gives me hope, but then i am worried that the Socket API will actually become unusable (thus like i said above, unableto be developed) as even the basic functionality uses internal bindings

@ebebbington ebebbington changed the title feat(std): Create net and net.Socket Node Polyfill [WIP] feat(std): Create net and net.Socket Node Polyfill Jan 2, 2021
@ebebbington ebebbington marked this pull request as draft January 2, 2021 23:06
@bartlomieju
Copy link
Member

Deno standard library was moved to denoland/deno_std, please reopen your PR there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants