All files / context / tcp.ts

100.00% Branches 0/0
4.55% Lines 1/22
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
 
 
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 




























import type { Fn } from '../index.ts';
import type { Socket } from '../deps.ts';
import { createTcpServer, createConnection } from '../deps.ts';

export type Listen = {};
export function Listen (at: number|string|URL, handler: Fn<[Socket]>) {
  const { port, hostname } = tcpAddr(at);
  const server = createTcpServer(handler);
  server.on('connecton', handler);
  return new Promise((resolve, reject) => {
    server.once('error', reject);
    server.listen(port, hostname as any, () => {
      server.off('error', reject);
      resolve(server);
    });
  });
};

export type Connect = {};
export function Connect (to: number|string|URL) {
  const { port, hostname } = tcpAddr(to);
  return createConnection({ port: Number(port), host: hostname });
};

/** Resolve TCP address. */
export function tcpAddr (to: number|string|URL): URL {
  if (!isNaN(Number(to))) to = `tcp://127.0.0.1:${to}`;
  if (typeof to === 'string') to = new URL(to);
  return to as URL
}