Skip to content

Commit

Permalink
Support for HTTPS in development added (#274)
Browse files Browse the repository at this point in the history
* Support HTTPS

This should hopefully resolve #178. It uses the example given in the
`micro` examples of `with-https`. It has some rough edges still that
needed to be sanded down:

* Choosing a specific short flag name for ssl (and if the initial flag
should still be called ssl; I'm tempted to rename it to `https` or what
have you).

* The example always acks with 200 and the example object.

* It does not support compression and merely overwrites the previous
server var.

* Pass appropriate handler to https server

This also calls `compress' on the handler when the `unzipped' flag is
not present.

* Move https import to appropriate section

* Change SSL flag copy

* Merge in master to update package-lock.json

* Address conflicts

* Use master package-lock.json to avoid conflicts

* Make travis happy by updating ava snapshots

* Fix sha1 to sha512 flipping per npm 5.3.0 issue
  • Loading branch information
justanotherdot authored and leo committed Oct 17, 2017
1 parent 35bf03b commit c6336ea
Show file tree
Hide file tree
Showing 7 changed files with 758 additions and 1,620 deletions.
24 changes: 21 additions & 3 deletions bin/serve.js
Expand Up @@ -2,6 +2,7 @@

// Native
const path = require('path')
const https = require('https')

// Packages
const micro = require('micro')
Expand All @@ -12,6 +13,7 @@ const { coroutine } = require('bluebird')
const updateNotifier = require('update-notifier')
const { red } = require('chalk')
const nodeVersion = require('node-version')
const cert = require('openssl-self-signed-certificate')

// Utilities
const pkg = require('../package')
Expand Down Expand Up @@ -49,7 +51,11 @@ if (flags.silent) {
console.log = () => {}
}

process.env.ASSET_DIR = '/' + Math.random().toString(36).substr(2, 10)
process.env.ASSET_DIR =
'/' +
Math.random()
.toString(36)
.substr(2, 10)

let current = process.cwd()

Expand All @@ -67,7 +73,18 @@ const handler = coroutine(function*(req, res) {
yield serverHandler(req, res, flags, current, ignoredFiles)
})

const server = flags.unzipped ? micro(handler) : micro(compress(handler))
const httpsOpts = {
key: cert.key,
cert: cert.cert,
passphrase: cert.passphrase
}

const microHttps = fn =>
https.createServer(httpsOpts, (req, res) => micro.run(req, res, fn))
const server = flags.ssl
? microHttps(flags.unzipped ? handler : compress(handler))
: micro(flags.unzipped ? handler : compress(handler))

let port = flags.port

detect(port).then(open => {
Expand All @@ -87,7 +104,8 @@ detect(port).then(open => {
current,
inUse,
flags.clipless !== true,
flags.open
flags.open,
flags.ssl
]

server.listen(port, listening.bind(this, ...listenArgs))
Expand Down
13 changes: 10 additions & 3 deletions lib/listening.js
Expand Up @@ -10,7 +10,14 @@ const boxen = require('boxen')
const { coroutine } = require('bluebird')
const opn = require('opn')

module.exports = coroutine(function*(server, current, inUse, clipboard, open) {
module.exports = coroutine(function*(
server,
current,
inUse,
clipboard,
open,
ssl
) {
const details = server.address()
const isTTY = process.stdout.isTTY

Expand Down Expand Up @@ -56,12 +63,12 @@ module.exports = coroutine(function*(server, current, inUse, clipboard, open) {

message += '\n\n'

const localURL = `http://localhost:${details.port}`
const localURL = `http${ssl ? 's' : ''}://localhost:${details.port}`
message += `- ${chalk.bold('Local: ')} ${localURL}`

try {
const ipAddress = ip.address()
const url = `http://${ipAddress}:${details.port}`
const url = `http${ssl ? 's' : ''}://${ipAddress}:${details.port}`

message += `\n- ${chalk.bold('On Your Network: ')} ${url}`
} catch (err) {}
Expand Down
8 changes: 7 additions & 1 deletion lib/options.js
Expand Up @@ -50,6 +50,11 @@ exports.options = [
name: 'treeless',
description: `Don't display statics tree`,
defaultValue: false
},
{
name: 'ssl',
description: 'Serve content using SSL',
defaultValue: false
}
]

Expand All @@ -72,6 +77,7 @@ exports.minimist = {
'unzipped',
'clipless',
'open',
'treeless'
'treeless',
'ssl'
]
}

0 comments on commit c6336ea

Please sign in to comment.