UNPKG

940 BJavaScriptView Raw
1import localtunnel from '@chromaui/localtunnel';
2import setupDebug from 'debug';
3
4import { CHROMATIC_TUNNEL_URL } from '../constants';
5
6const debug = setupDebug('chromatic-cli:tunnel');
7
8export default async function openTunnel({ port, https, host = 'localhost', ...rest }) {
9 if (!port) {
10 throw new Error('Need to pass a port into `openTunnel`');
11 }
12
13 const tunnel = await localtunnel({
14 // upstream
15 host: CHROMATIC_TUNNEL_URL,
16 port,
17
18 // local
19 local_host: host, // not a typo
20 ...rest,
21
22 // I have no idea, these seem to go unused in the unlaying lib
23 https: !!https,
24 cert: https && https.cert,
25 key: https && https.key,
26 ca: https && https.ca,
27 });
28
29 tunnel.on('url', url => debug(`Got tunnel url: %s`, url));
30 tunnel.on('request', request => debug(`Got request: %O`, request));
31 tunnel.tunnelCluster.on('error', error => debug(`Got tunnel cluster error: %O`, error));
32
33 return tunnel;
34}