Commit cb25fdb5 authored by Sebastiaan Deckers's avatar Sebastiaan Deckers 馃悜
Browse files

馃攼

parents
# fastify-tls-keygen
Fastify plugin to automatically generate a browser-compatible, trusted, self-signed, localhost-only, TLS certificate.
## Installation
```
npm install fastify-tls-keygen
```
## Usage
```js
const fastify = require('fastify')({
// Required: Enable TLS
https: true,
// Optional: Enable HTTP/2
http2: true
})
fastify.register(require('fastify-tls-keygen'), {
// Optional (default: ./key.pem)
key: '/path/to/save/private/key.pem',
// Optional (default: ./cert.pem)
cert: '/path/to/save/public/certificate.pem'
})
// Tip: Port 443 (HTTPS) requires root permissions. Use a port >1024.
fastify.listen(8443)
```
## See Also
- [tls-keygen](https://www.npmjs.com/package/tls-keygen) @ NPM
- [self-cert](https://github.com/jsumners/self-cert) @ NPM
- [LetsEncrypt](https://letsencrypt.org)
## Colophon
Made with 馃挐 by [Sebastiaan Deckers](https://twitter.com/sebdeckers) in 馃嚫馃嚞 Singapore.
const fp = require('fastify-plugin')
const {join, dirname} = require('path')
const {promisify} = require('util')
const {readFile} = require('fs')
const mkdirp = promisify(require('mkdirp'))
const {keygen} = require('tls-keygen')
const read = promisify(readFile)
async function tlsKeygen (fastify, {
key = join(process.cwd(), 'key.pem'),
cert = join(process.cwd(), 'cert.pem')
} = {}) {
if (typeof fastify.server.addContext !== 'function') {
throw new Error(
'TLS is not enabled. Use the Fastify option: `https: true`'
)
}
if (!fastify.server.key && !fastify.server.cert) {
try {
[key, cert] = await Promise.all([
read(key),
read(cert)
])
} catch (error) {
if (error.code === 'ENOENT') {
const paths = await keygen({key, cert})
await Promise.all([
mkdirp(dirname(paths.cert)),
mkdirp(dirname(paths.key))
]);
[key, cert] = await Promise.all([
read(paths.key),
read(paths.cert)
])
} else {
throw error
}
}
fastify.server.addContext('*', {key, cert})
}
}
module.exports = fp(tlsKeygen)
{
"name": "fastify-tls-keygen",
"version": "1.0.0",
"description": "Fastify plugin to automatically generate a browser-compatible, trusted, self-signed, localhost-only, TLS certificate.",
"author": "Sebastiaan Deckers <sebdeckers83@gmail.com>",
"license": "ISC",
"repository": "gitlab:sebdeckers/fastify-tls-keygen",
"main": "index.js",
"keywords": [
"fastify",
"plugin",
"tls",
"certificate"
],
"scripts": {
"test": "standard --verbose | snazzy"
},
"devDependencies": {
"fastify": "^1.0.0-rc.1",
"snazzy": "^7.0.0",
"standard": "^10.0.3"
},
"dependencies": {
"fastify-plugin": "^0.2.1",
"mkdirp": "^0.5.1",
"tls-keygen": "^2.0.0"
}
}
const fastify = require('fastify')
const plugin = require('.')
const server = fastify({
logger: true,
http2: true,
https: true
})
server.register(plugin, {
key: './private-key.pem',
cert: './public-cert.pem'
})
server.listen(8443)
// Open a browser to see if it works?
// curl --verbose --insecure https://localhost:8443/ ; echo
Supports Markdown
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