Only load ngrok/tty on demand.

This commit is contained in:
Pagan Gazzard 2015-03-08 12:51:55 +00:00 committed by Pablo Carranza Vélez
parent d578bbfba3
commit 2c94ea57a2

View File

@ -1,8 +1,15 @@
_ = require 'lodash'
Promise = require 'bluebird'
ngrok = Promise.promisifyAll require 'ngrok'
tty = Promise.promisifyAll require 'tty.js'
TypedError = require 'typed-error'
# Only load ngrok/tty when they are actually needed,
# to reduce memory in the likely case they are never used.
ngrok = null
tty = null
init = _.once ->
ngrok = Promise.promisifyAll require 'ngrok'
tty = Promise.promisifyAll require 'tty.js'
class DisconnectedError extends TypedError
# socat UNIX:/data/host -,raw,echo=0
@ -10,6 +17,7 @@ class DisconnectedError extends TypedError
apps = {}
nextPort = 81
exports.start = (app) ->
init()
apps[app.id] ?= Promise.rejected()
return apps[app.id] = apps[app.id].catch ->
port = nextPort++
@ -27,6 +35,7 @@ exports.stop = (appId) ->
if !apps[appId]?
return Promise.resolve()
apps[appId] = apps[appId].then (url) ->
# ngrok must have been loaded already or we wouldn't have a url to disconnect from.
ngrok.disconnectAsync(url)
.then ->
# We throw an error so that `.start` will catch and restart the session.