diff options
author | Luca Casonato <hello@lcas.dev> | 2022-10-24 14:55:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 14:55:39 +0200 |
commit | c061538417cdb2b0a8f2d6eae7f5f11511ba34fe (patch) | |
tree | d8d8efc9e2a883d4ad21ebacf44e8e922fae794d /ext/net/02_tls.js | |
parent | f38666f5a348c5f40c66dd6e5896618b7f1b09bc (diff) |
refactor(ext/net): clean up variadic network ops (#16392)
Previously `op_net_listen`, `op_net_accept`, and various other ops in
ext/net where variadic on the transport. This created a lot of code
bloat. This commit updates the code to instead have separate ops for
each transport.
Diffstat (limited to 'ext/net/02_tls.js')
-rw-r--r-- | ext/net/02_tls.js | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/ext/net/02_tls.js b/ext/net/02_tls.js index 04b25caf9..d3f906dbd 100644 --- a/ext/net/02_tls.js +++ b/ext/net/02_tls.js @@ -5,20 +5,7 @@ const core = window.Deno.core; const ops = core.ops; const { Listener, Conn } = window.__bootstrap.net; - - function opConnectTls( - args, - ) { - return core.opAsync("op_tls_connect", args); - } - - function opAcceptTLS(rid) { - return core.opAsync("op_tls_accept", rid); - } - - function opListenTls(args) { - return ops.op_tls_listen(args); - } + const { TypeError } = window.__bootstrap.primordials; function opStartTls(args) { return core.opAsync("op_tls_start", args); @@ -44,23 +31,28 @@ privateKey = undefined, alpnProtocols = undefined, }) { - const res = await opConnectTls({ - port, - hostname, - transport, - certFile, - caCerts, - certChain, - privateKey, - alpnProtocols, - }); - return new TlsConn(res.rid, res.remoteAddr, res.localAddr); + if (transport !== "tcp") { + throw new TypeError(`Unsupported transport: '${transport}'`); + } + const [rid, localAddr, remoteAddr] = await core.opAsync( + "op_net_connect_tls", + { hostname, port }, + { certFile, caCerts, certChain, privateKey, alpnProtocols }, + ); + localAddr.transport = "tcp"; + remoteAddr.transport = "tcp"; + return new TlsConn(rid, remoteAddr, localAddr); } class TlsListener extends Listener { async accept() { - const res = await opAcceptTLS(this.rid); - return new TlsConn(res.rid, res.remoteAddr, res.localAddr); + const [rid, localAddr, remoteAddr] = await core.opAsync( + "op_net_accept_tls", + this.rid, + ); + localAddr.transport = "tcp"; + remoteAddr.transport = "tcp"; + return new TlsConn(rid, remoteAddr, localAddr); } } @@ -74,17 +66,14 @@ transport = "tcp", alpnProtocols = undefined, }) { - const res = opListenTls({ - port, - cert, - certFile, - key, - keyFile, - hostname, - transport, - alpnProtocols, - }); - return new TlsListener(res.rid, res.localAddr); + if (transport !== "tcp") { + throw new TypeError(`Unsupported transport: '${transport}'`); + } + const [rid, localAddr] = ops.op_net_listen_tls( + { hostname, port }, + { cert, certFile, key, keyFile, alpnProtocols }, + ); + return new TlsListener(rid, localAddr); } async function startTls( @@ -96,14 +85,14 @@ alpnProtocols = undefined, } = {}, ) { - const res = await opStartTls({ + const [rid, localAddr, remoteAddr] = await opStartTls({ rid: conn.rid, hostname, certFile, caCerts, alpnProtocols, }); - return new TlsConn(res.rid, res.remoteAddr, res.localAddr); + return new TlsConn(rid, remoteAddr, localAddr); } window.__bootstrap.tls = { |