-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (46 loc) · 1.93 KB
/
index.js
File metadata and controls
58 lines (46 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const os = require('os')
const path = require('path')
const https = require('https')
const AutoEncrypt = require('@small-tech/auto-encrypt')
const AutoEncryptLocalhost = require('@small-tech/auto-encrypt-localhost')
function log(...args) {
if (process.env.QUIET) {
return
}
console.log(...args)
}
// Only modify this instance of the https module with our own createServer method.
const smallTechHttps = Object.assign({}, https)
// Enumeration. The server types expected by AutoEncrypt. Default is production.
AutoEncryptServerType = {
PRODUCTION: 0,
STAGING: 1,
PEBBLE: 2,
}
smallTechHttps.createServer = function (options, listener) {
// The first parameter is optional. If omitted, listener should be passed as the first argument.
if (typeof options === 'function') {
listener = options
options = {}
}
const defaultSettingsPath = path.join(os.homedir(), '.small-tech.org', 'https')
const serverScope = options.domains == undefined || options.domains.includes('localhost') ? 'local' : 'global'
options.settingsPath = options.settingsPath ? path.join(path.resolve(options.settingsPath), serverScope) : path.join(defaultSettingsPath, serverScope)
if (options.staging) { options.serverType = AutoEncryptServerType.STAGING }
if (options.pebble) { options.serverType = AutoEncryptServerType.PEBBLE }
delete options.staging
delete options.pebble
const logMessage = {
local: 'at localhost with locally-trusted certificates',
global: 'with globally-trusted Let’s Encrypt certificates'
}
const autoEncryptScope = {
local: AutoEncryptLocalhost,
global: AutoEncrypt
}
log(` 🔒 ❨@small-tech/https❩ Creating server ${logMessage[serverScope]}.`)
const server = autoEncryptScope[serverScope].https.createServer(options, listener)
log(' 🔒 ❨@small-tech/https❩ Created HTTPS server.')
return server
}
module.exports = smallTechHttps