Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTPS #274

Merged
merged 10 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions bin/serve.js
Original file line number Diff line number Diff line change
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline after this line 😊

Copy link
Contributor Author

@justanotherdot justanotherdot Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 🙂


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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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'
]
}
Loading