@jsenv/server
Advanced tools
Comparing version 14.1.16 to 15.0.0
{ | ||
"name": "@jsenv/server", | ||
"version": "14.1.16", | ||
"version": "15.0.0", | ||
"description": "Write your Node.js server using pure functions", | ||
@@ -39,3 +39,3 @@ "license": "MIT", | ||
"@jsenv/log": "3.3.4", | ||
"@jsenv/url-meta": "7.0.2", | ||
"@jsenv/url-meta": "7.1.0", | ||
"@jsenv/utils": "2.0.1", | ||
@@ -42,0 +42,0 @@ "ws": "8.13.0" |
@@ -9,3 +9,2 @@ # server [](https://www.npmjs.com/package/@jsenv/server) | ||
await startServer({ | ||
protocol: "http", | ||
port: 8080, | ||
@@ -77,5 +76,6 @@ services: [ | ||
await startServer({ | ||
protocol: "https", | ||
certificate: readFileSync(new URL("./server.crt", import.meta.url), "utf8"), | ||
privateKey: readFileSync(new URL("./server.key", import.meta.url), "utf8"), | ||
https: { | ||
certificate: readFileSync(new URL("./server.crt", import.meta.url), "utf8"), | ||
privateKey: readFileSync(new URL("./server.key", import.meta.url), "utf8"), | ||
}, | ||
allowHttpRequestOnHttps: true, | ||
@@ -82,0 +82,0 @@ services: [ |
import { isIP } from "node:net" | ||
import http from "node:http" | ||
import cluster from "node:cluster" | ||
@@ -53,3 +52,3 @@ import { createDetailedMessage, createLogger } from "@jsenv/log" | ||
protocol = "http", | ||
https = false, | ||
http2 = false, | ||
@@ -64,4 +63,2 @@ http1Allowed = true, | ||
portHint, | ||
privateKey, | ||
certificate, | ||
@@ -99,46 +96,56 @@ // when inside a worker, we should not try to stop server on SIGINT | ||
requestBodyLifetime = 60_000 * 2, // 2s | ||
...rest | ||
} = {}) => { | ||
// param validations | ||
{ | ||
const unexpectedParamNames = Object.keys(rest) | ||
if (unexpectedParamNames.length > 0) { | ||
throw new TypeError( | ||
`${unexpectedParamNames.join(",")}: there is no such param`, | ||
) | ||
} | ||
if (https) { | ||
if (typeof https !== "object") { | ||
throw new TypeError(`https must be an object, got ${https}`) | ||
} | ||
const { certificate, privateKey } = https | ||
if (!certificate || !privateKey) { | ||
throw new TypeError( | ||
`https must be an object with { certificate, privateKey }`, | ||
) | ||
} | ||
} | ||
if (http2 && !https) { | ||
throw new Error(`http2 needs https`) | ||
} | ||
} | ||
const logger = createLogger({ logLevel }) | ||
if (protocol !== "http" && protocol !== "https") { | ||
throw new Error(`protocol must be http or https, got ${protocol}`) | ||
} | ||
if (protocol === "https") { | ||
if (!certificate) { | ||
throw new Error(`missing certificate for https server`) | ||
// param warnings and normalization | ||
{ | ||
if ( | ||
redirectHttpToHttps === undefined && | ||
https && | ||
!allowHttpRequestOnHttps | ||
) { | ||
redirectHttpToHttps = true | ||
} | ||
if (!privateKey) { | ||
throw new Error(`missing privateKey for https server`) | ||
if (redirectHttpToHttps && !https) { | ||
logger.warn(`redirectHttpToHttps ignored because protocol is http`) | ||
redirectHttpToHttps = false | ||
} | ||
} | ||
if (http2 && protocol !== "https") { | ||
throw new Error(`http2 needs "https" but protocol is "${protocol}"`) | ||
} | ||
if (allowHttpRequestOnHttps && redirectHttpToHttps) { | ||
logger.warn( | ||
`redirectHttpToHttps ignored because allowHttpRequestOnHttps is enabled`, | ||
) | ||
redirectHttpToHttps = false | ||
} | ||
if ( | ||
redirectHttpToHttps === undefined && | ||
protocol === "https" && | ||
!allowHttpRequestOnHttps | ||
) { | ||
redirectHttpToHttps = true | ||
if (allowHttpRequestOnHttps && !https) { | ||
logger.warn(`allowHttpRequestOnHttps ignored because protocol is http`) | ||
allowHttpRequestOnHttps = false | ||
} | ||
} | ||
if (redirectHttpToHttps && protocol === "http") { | ||
logger.warn(`redirectHttpToHttps ignored because protocol is http`) | ||
redirectHttpToHttps = false | ||
} | ||
if (allowHttpRequestOnHttps && redirectHttpToHttps) { | ||
logger.warn( | ||
`redirectHttpToHttps ignored because allowHttpRequestOnHttps is enabled`, | ||
) | ||
redirectHttpToHttps = false | ||
} | ||
if (allowHttpRequestOnHttps && protocol === "http") { | ||
logger.warn(`allowHttpRequestOnHttps ignored because protocol is http`) | ||
allowHttpRequestOnHttps = false | ||
} | ||
const server = {} | ||
const serviceController = createServiceController(services) | ||
const processTeardownEvents = { | ||
@@ -170,7 +177,5 @@ SIGHUP: stopOnExit, | ||
nodeServer = await createNodeServer({ | ||
protocol, | ||
https, | ||
redirectHttpToHttps, | ||
allowHttpRequestOnHttps, | ||
certificate, | ||
privateKey, | ||
http2, | ||
@@ -187,2 +192,3 @@ http1Allowed, | ||
const createOrigin = (hostname) => { | ||
const protocol = https ? "https" : "http" | ||
if (isIP(hostname) === 6) { | ||
@@ -948,6 +954,5 @@ return `${protocol}://[${hostname}]` | ||
let websocketServer = new WebSocketServer({ noServer: true }) | ||
const websocketOrigin = | ||
protocol === "https" | ||
? `wss://${hostname}:${port}` | ||
: `ws://${hostname}:${port}` | ||
const websocketOrigin = https | ||
? `wss://${hostname}:${port}` | ||
: `ws://${hostname}:${port}` | ||
server.websocketOrigin = websocketOrigin | ||
@@ -1029,28 +1034,26 @@ const upgradeCallback = (nodeRequest, socket, head) => { | ||
const createNodeServer = async ({ | ||
protocol, | ||
https, | ||
redirectHttpToHttps, | ||
allowHttpRequestOnHttps, | ||
certificate, | ||
privateKey, | ||
http2, | ||
http1Allowed, | ||
}) => { | ||
if (protocol === "http") { | ||
return http.createServer() | ||
} | ||
if (redirectHttpToHttps || allowHttpRequestOnHttps) { | ||
return createPolyglotServer({ | ||
certificate, | ||
privateKey, | ||
http2, | ||
http1Allowed, | ||
if (https) { | ||
const { certificate, privateKey } = https | ||
if (redirectHttpToHttps || allowHttpRequestOnHttps) { | ||
return createPolyglotServer({ | ||
certificate, | ||
privateKey, | ||
http2, | ||
http1Allowed, | ||
}) | ||
} | ||
const { createServer } = await import("node:https") | ||
return createServer({ | ||
cert: certificate, | ||
key: privateKey, | ||
}) | ||
} | ||
const { createServer } = await import("node:https") | ||
return createServer({ | ||
cert: certificate, | ||
key: privateKey, | ||
}) | ||
const { createServer } = await import("node:http") | ||
return createServer() | ||
} | ||
@@ -1057,0 +1060,0 @@ |
150886
4780
+ Added@jsenv/url-meta@7.1.0(transitive)
- Removed@jsenv/url-meta@7.0.2(transitive)
Updated@jsenv/url-meta@7.1.0