@jsenv/server
Advanced tools
Comparing version 1.5.9 to 1.6.0-alpha.0
{ | ||
"name": "@jsenv/server", | ||
"version": "1.5.9", | ||
"version": "1.6.0-alpha.0", | ||
"description": "High level api for node.js server.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
export const trackConnections = (nodeServer) => { | ||
export const trackConnections = (nodeServer, { onConnectionError }) => { | ||
const connections = new Set() | ||
@@ -8,2 +8,3 @@ | ||
}) | ||
connection.on("error", onConnectionError) | ||
connections.add(connection) | ||
@@ -20,11 +21,2 @@ } | ||
return new Promise((resolve, reject) => { | ||
connection.on("error", (error) => { | ||
if (error === reason) { | ||
return | ||
} | ||
if (error.code === "ECONNRESET") { | ||
return | ||
} | ||
throw error | ||
}) | ||
connection.destroy(reason, (error) => { | ||
@@ -31,0 +23,0 @@ if (error) { |
@@ -9,2 +9,5 @@ /* eslint-disable import/max-dependencies */ | ||
createStoppableOperation, | ||
composeCancellationToken, | ||
createCancellationSource, | ||
isCancelError, | ||
} from "@jsenv/cancellation" | ||
@@ -96,11 +99,47 @@ import { interruptSignal, unadvisedCrashSignal, teardownSignal } from "@jsenv/node-signals" | ||
} = {}) => { | ||
if (port === 0 && forcePort) throw new Error(`no need to pass forcePort when port is 0`) | ||
if (protocol !== "http" && protocol !== "https") | ||
if (port === 0 && forcePort) { | ||
throw new Error(`no need to pass forcePort when port is 0`) | ||
} | ||
if (protocol !== "http" && protocol !== "https") { | ||
throw new Error(`protocol must be http or https, got ${protocol}`) | ||
} | ||
// https://github.com/nodejs/node/issues/14900 | ||
if (ip === "0.0.0.0" && process.platform === "win32") | ||
if (ip === "0.0.0.0" && process.platform === "win32") { | ||
throw new Error(`listening ${ip} not available on window`) | ||
} | ||
const logger = createLogger({ logLevel }) | ||
const internalCancellationSource = createCancellationSource() | ||
cancellationToken = composeCancellationToken(cancellationToken, internalCancellationSource.token) | ||
const { registerCleanupCallback, cleanup } = createTracker() | ||
if (stopOnCrash) { | ||
const unregister = unadvisedCrashSignal.addCallback((reason) => { | ||
internalCancellationSource.cancel(reason.value) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (stopOnSIGINT) { | ||
const unregister = interruptSignal.addCallback(() => { | ||
internalCancellationSource.cancel(STOP_REASON_PROCESS_SIGINT) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (stopOnExit) { | ||
const unregister = teardownSignal.addCallback((tearDownReason) => { | ||
internalCancellationSource.cancel( | ||
{ | ||
beforeExit: STOP_REASON_PROCESS_BEFORE_EXIT, | ||
hangupOrDeath: STOP_REASON_PROCESS_HANGUP_OR_DEATH, | ||
death: STOP_REASON_PROCESS_DEATH, | ||
exit: STOP_REASON_PROCESS_EXIT, | ||
}[tearDownReason], | ||
) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (forcePort) { | ||
@@ -121,6 +160,5 @@ await createOperation({ | ||
let status = "starting" | ||
let onConnectionError = () => {} | ||
const { registerCleanupCallback, cleanup } = createTracker() | ||
const connectionTracker = trackConnections(nodeServer) | ||
const connectionTracker = trackConnections(nodeServer, { onConnectionError }) | ||
// opened connection must be shutdown before the close event is emitted | ||
@@ -152,2 +190,14 @@ registerCleanupCallback(connectionTracker.stop) | ||
status = "stopping" | ||
onConnectionError = (error) => { | ||
if (error === reason) { | ||
return | ||
} | ||
if (error && error.code === "ECONNRESET") { | ||
return | ||
} | ||
if (isCancelError(reason)) { | ||
return | ||
} | ||
throw error | ||
} | ||
logger.info(`${serverName} stopped because ${reason}`) | ||
@@ -161,2 +211,3 @@ | ||
}) | ||
cancellationToken.register(stop) | ||
const startOperation = createStoppableOperation({ | ||
@@ -168,42 +219,2 @@ cancellationToken, | ||
if (stopOnCrash) { | ||
const unregister = unadvisedCrashSignal.addCallback((reason) => { | ||
stop(reason.value) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (stopOnInternalError) { | ||
const unregister = requestHandlerTracker.add((nodeRequest, nodeResponse) => { | ||
if ( | ||
nodeResponse.statusCode === 500 && | ||
nodeResponse.statusMessage === STATUS_TEXT_INTERNAL_ERROR | ||
) { | ||
stop(STOP_REASON_INTERNAL_ERROR) | ||
} | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (stopOnExit) { | ||
const unregister = teardownSignal.addCallback((tearDownReason) => { | ||
stop( | ||
{ | ||
beforeExit: STOP_REASON_PROCESS_BEFORE_EXIT, | ||
hangupOrDeath: STOP_REASON_PROCESS_HANGUP_OR_DEATH, | ||
death: STOP_REASON_PROCESS_DEATH, | ||
exit: STOP_REASON_PROCESS_EXIT, | ||
}[tearDownReason], | ||
) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
if (stopOnSIGINT) { | ||
const unregister = interruptSignal.addCallback(() => { | ||
stop(STOP_REASON_PROCESS_SIGINT) | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
port = await startOperation | ||
@@ -342,2 +353,14 @@ status = "opened" | ||
if (stopOnInternalError) { | ||
const unregister = requestHandlerTracker.add((nodeRequest, nodeResponse) => { | ||
if ( | ||
nodeResponse.statusCode === 500 && | ||
nodeResponse.statusMessage === STATUS_TEXT_INTERNAL_ERROR | ||
) { | ||
stop(STOP_REASON_INTERNAL_ERROR) | ||
} | ||
}) | ||
registerCleanupCallback(unregister) | ||
} | ||
return { | ||
@@ -344,0 +367,0 @@ getStatus: () => status, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
224511
4178
2