summaryrefslogtreecommitdiff
path: root/ext/net/04_net_unstable.js
blob: fcdb3c547c1be2c2abe321216e9220ad90a3059f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
"use strict";

((window) => {
  const net = window.__bootstrap.net;

  function listen(options) {
    if (options.transport === "unix") {
      const res = net.opListen(options);
      return new Listener(res.rid, res.localAddr);
    } else {
      return net.listen(options, Listener);
    }
  }

  function listenDatagram(
    options,
  ) {
    let res;
    if (options.transport === "unixpacket") {
      res = net.opListen(options);
    } else {
      res = net.opListen({
        transport: "udp",
        hostname: "127.0.0.1",
        ...options,
      });
    }

    return new net.Datagram(res.rid, res.localAddr);
  }

  async function connect(
    options,
  ) {
    if (options.transport === "unix") {
      const res = await net.opConnect(options);
      return new net.Conn(res.rid, res.remoteAddr, res.localAddr);
    } else {
      return net.connect(options);
    }
  }

  class Listener extends net.Listener {
    ref() {
      this[net.listenerRef]();
    }

    unref() {
      this[net.listenerUnref]();
    }
  }

  window.__bootstrap.netUnstable = {
    connect,
    listenDatagram,
    listen,
    Listener,
  };
})(this);