ngrok / ngrok-javascript

Embed ngrok secure ingress into your Node.js apps with a single line of code.
https://ngrok.com
Apache License 2.0
88 stars 18 forks source link

make listenable tunnels, default logging to info #14

Closed bobzilladev closed 1 year ago

bobzilladev commented 1 year ago

This PR adds a bind behavior on the tunnel builders' listen() which returns a tunnel object that includes a pre-bound TCP socket which is returned in a handle property, which satisfies the NodeJS HTTP server's listen().

const tunnel = await session.httpEndpoint().listen();
httpServer.listen(tunnel);

The behavior can be turned off with listen(false) on the builders:

const tunnel = await session.httpEndpoint().listen(false);

This PR also replaces getSocket() with listenable() that uses the same handle behavior, for a quick start like:

const tunnel = await ngrok.listenable();
httpServer.listen(tunnel);

Or as a one-liner:

httpServer.listen(await ngrok.listenable())

The module-level listen() quick start one-liner that takes an http (including express) server argument is still the best option, as this will attempt to use unix sockets or windows named pipes. The http server has to listen to files first before the tunnel is wired up, which is why unix/pipes can only be done for the user if we have a reference to the http server:

await ngrok.listen(httpServer)