summaryrefslogtreecommitdiff
path: root/ext/net/04_net_unstable.js
blob: e22c9bf937d61a9b0c02d07c31e36b93b7ee89d6 (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
// 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 net.Listener(res.rid, res.localAddr);
    } else {
      return net.listen(options);
    }
  }

  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);
    }
  }

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