Verified Commit 85e26817 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add HTTP2 support

parent 4da52de6
......@@ -8,6 +8,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Nothing yet.
## [2.0.0] - 2019-02-28
### Added
- HTTP2 support
### Changed
- Start using [semver](https://semver.org/) properly (most of the previous releases should have been minor version bumps.) This is a major bump as there is a backwards-incompatible API change to the serve() method.
## [1.0.7] - 2019-02-28
### Fixes
......
const https = require('https')
const http2 = require('http2')
const fs = require('fs')
const express = require('express')
const morgan = require('morgan')
......@@ -33,9 +34,38 @@ class HttpsServer {
}
// Returns and HTTP2 Http2SecureServer instance, the same as you would get with
// require('http2').createSecureServer() – configured with your nodecert certificates.
// If you do pass a key and cert, they will be overwritten. Also the allowHTTP1 flag
// of the created server is set to true.
createSecureServer (options = {}, requestListener = undefined) {
const defaultOptions = {
key: fs.readFileSync(path.join(nodecertDirectory, 'localhost-key.pem')),
cert: fs.readFileSync(path.join(nodecertDirectory, 'localhost.pem')),
allowHTTP1: true
}
Object.assign(options, defaultOptions)
return http2.createSecureServer(options, requestListener)
}
// Starts a static server serving the contents of the passed path at the passed port
// and returns the server.
serve(pathToServe = '.', port = 443, callback = null) {
serve(pathToServe = '.', callback = null, options = {}) {
// Can be called as serve(pathToServe, callback) also.
if (typeof options === 'function') {
callback = options
options = {}
}
const port = options.port || 443
const isHTTP2 = options.isHTTP2 || false
const serverCreationMethod = isHTTP2 ? this.createSecureServer : this.createServer
this.ensureWeCanBindToPort(port, pathToServe)
// If a callback isn’t provided, fallback to a default one that gives a status update.
......@@ -57,7 +87,7 @@ class HttpsServer {
let server
try {
server = this.createServer({}, app).listen(port, callback)
server = serverCreationMethod({}, app).listen(port, callback)
} catch (error) {
console.log('\nError: could not start server', error)
throw error
......
{
"name": "@ind.ie/https-server",
"version": "1.0.7",
"version": "2.0.0",
"description": "HTTPS server that uses nodecert",
"main": "index.js",
"bin": "bin/https-server.js",
......
......@@ -24,22 +24,61 @@ async function secureGet (url) {
}
test('create server', t => {
test('create https server', t => {
t.plan(2)
const server = httpsServer.createServer()
t.ok(server instanceof https.Server, 'https.Server instance is returned')
t.ok(server instanceof https.Server, 'is https.Server')
server.listen(443, () => {
t.equal(server.address().port, 443, 'the requested port is set')
t.equal(server.address().port, 443, 'the requested port is set on https.Server')
t.end()
server.close()
})
})
test('static serve', t => {
test('create http2 server', t => {
t.plan(2)
const server = httpsServer.serve('test/site', 443, async () => {
const server = httpsServer.createSecureServer({isHTTP2: true})
// http2 does not export the Http2SecureServer class so we cannot use instanceof to test here.
t.equal(server.constructor.name, 'Http2SecureServer', 'is Http2SecureServer')
server.listen(443, () => {
t.equal(server.address().port, 443, 'the requested port is set on Http2SecureServer')
t.end()
server.close()
})
})
test('static serve https', t => {
t.plan(3)
const server = httpsServer.serve('test/site', async () => {
t.ok(server instanceof https.Server, 'is https.Server')
let response
try {
response = await secureGet('https://localhost/index.html')
} catch (error) {
console.log(error)
process.exit(1)
}
t.equal(response.statusCode, 200, 'request succeeds')
t.equal(response.body, indexHTML, 'index loads')
t.end()
server.close()
})
})
test('static serve HTTP2', t => {
t.plan(3)
const server = httpsServer.serve('test/site', async () => {
t.equal(server.constructor.name, 'Http2SecureServer', 'is Http2SecureServer')
let response
try {
......@@ -54,5 +93,7 @@ test('static serve', t => {
t.end()
server.close()
}, {
isHTTP2: true
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment