+_ = 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
 

	if !apps[appId]?
		return Promise.resolve()
	apps[appId] = apps[appId].then (url) ->
		ngrok.disconnectAsync(url)
		.then ->
			# We throw an error so that `.start` will catch and restart the session.

apps = {}
nextPort = 81
exports.start = (app) ->
	init()
	apps[app.id] ?= Promise.rejected()
	return apps[app.id] = apps[app.id].catch ->
		port = nextPort++
