 fs.watch 'index.html', prepareHTML
 jsFiles.forEach (f) -> fs.watch f, prepareJS
 cssFiles.forEach (f) -> fs.watch f, prepareCSS
-
 ttf = fs.readFileSync 'apl385.ttf'
-
-b64 = (s) -> Buffer(s).toString 'base64'
-b64d = (s) -> '' + Buffer s, 'base64'
-getTag = (tagName, xml) -> (///^[^]*<#{tagName}>([^]*)</#{tagName}>[^]*$///.exec xml)?[1]
 
 app = express()
 app.get '/',           (req, res) -> res.header('Content-Type', 'text/html'               ).send html

server = https.createServer(httpsOptions, app).listen (httpsPort = 8443),
  -> log "HTTPS server listening on :#{httpsPort}"

io.listen(server).on 'connection', (socket) ->
  log 'browser connected'
  client = net.connect {host: '127.0.0.1', port: 4502}, -> log 'interpreter connected'

  toInterpreter = (s) ->
    log 'to interpreter: ' + JSON.stringify(s)[..1000]
    console.assert /[\x01-\x7f]*/.test s
    b = Buffer s.length + 8
    b.writeInt32BE b.length, 0
    b.write 'RIDE' + s, 4
    client.write b

  toBrowser = (m...) -> log 'to browser: ' + JSON.stringify(m)[..1000]; socket.emit m...


]

log = (s) -> console.info process.uptime().toFixed(3) + ' ' + s
b64 = (s) -> Buffer(s).toString 'base64'
b64d = (s) -> '' + Buffer s, 'base64'
getTag = (tagName, xml) -> (///^[^]*<#{tagName}>([^]*)</#{tagName}>[^]*$///.exec xml)?[1]

# preload files into memory so we can serve them faster
html = css = js = ''
do prepareHTML = ->
  html = fs.readFileSync 'index.html', 'utf8'
