@jsenv/server 12.5.3 → 12.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/server",
3
- "version": "12.5.3",
3
+ "version": "12.5.6",
4
4
  "description": "Write your Node.js server using pure functions",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -89,11 +89,17 @@ export const fetchFileSystem = async (
89
89
  () => statSync(sourceFilesystemPath),
90
90
  )
91
91
  if (sourceStat.isDirectory()) {
92
- return serveDirectory(urlString, {
93
- headers,
94
- canReadDirectory,
95
- rootDirectoryUrl,
96
- })
92
+ if (canReadDirectory) {
93
+ return serveDirectory(urlString, {
94
+ headers,
95
+ canReadDirectory,
96
+ rootDirectoryUrl,
97
+ })
98
+ }
99
+ return {
100
+ status: 403,
101
+ statusText: "not allowed to read directory",
102
+ }
97
103
  }
98
104
  // not a file, give up
99
105
  if (!sourceStat.isFile()) {
@@ -11,6 +11,7 @@ export const composeTwoResponses = (firstResponse, secondResponse) => {
11
11
  const RESPONSE_KEYS_COMPOSITION = {
12
12
  status: (prevStatus, status) => status,
13
13
  statusText: (prevStatusText, statusText) => statusText,
14
+ statusMessage: (prevStatusMessage, statusMessage) => statusMessage,
14
15
  headers: composeTwoHeaders,
15
16
  body: (prevBody, body) => body,
16
17
  bodyEncoding: (prevEncoding, encoding) => encoding,
@@ -5,18 +5,8 @@ import { negotiateContentType } from "./content_negotiation/negotiateContentType
5
5
 
6
6
  export const serveDirectory = (
7
7
  url,
8
- {
9
- headers = {},
10
- canReadDirectory = false,
11
- rootDirectoryUrl = `${pathToFileURL(process.cwd())}/`,
12
- } = {},
8
+ { headers = {}, rootDirectoryUrl = `${pathToFileURL(process.cwd())}/` } = {},
13
9
  ) => {
14
- if (canReadDirectory === false) {
15
- return {
16
- status: 403,
17
- statusText: "not allowed to read directory",
18
- }
19
- }
20
10
  url = String(url)
21
11
  url = url[url.length - 1] === "/" ? url : `${url}/`
22
12
  const directoryContentArray = readdirSync(new URL(url))
@@ -291,26 +291,30 @@ export const startServer = async ({
291
291
  // keep buffering until root request write logs for everyone
292
292
  return
293
293
  }
294
-
294
+ const prefixLines = (string, prefix) => {
295
+ return string.replace(/^(?!\s*$)/gm, prefix)
296
+ }
295
297
  const writeLog = ({ type, value }, depth) => {
296
298
  if (depth > 0) {
297
- value = value.replace(/^(?!\s*$)/gm, " ".repeat(depth))
299
+ value = prefixLines(value, " ".repeat(depth))
298
300
  }
299
301
  logger[type](value)
300
302
  }
301
-
302
303
  const visitRequestNodeToLog = (requestNode, depth) => {
303
- const logsExceptLast = requestNode.logs.slice(0, -1)
304
- logsExceptLast.forEach((log) => {
304
+ const firstLog = requestNode.logs.shift()
305
+ const lastLog = requestNode.logs.pop()
306
+ const middleLogs = requestNode.logs
307
+ writeLog(firstLog, depth)
308
+ middleLogs.forEach((log) => {
305
309
  writeLog(log, depth)
306
310
  })
307
311
  requestNode.children.forEach((child) => {
308
312
  visitRequestNodeToLog(child, depth + 1)
309
313
  })
310
- const lastLog = requestNode.logs.pop()
311
- writeLog(lastLog, depth)
314
+ if (lastLog) {
315
+ writeLog(lastLog, depth + 1)
316
+ }
312
317
  }
313
-
314
318
  visitRequestNodeToLog(rootRequestNode, 0)
315
319
  }
316
320
  nodeRequest.on("error", (error) => {
@@ -589,6 +593,7 @@ export const startServer = async ({
589
593
  const {
590
594
  status = 501,
591
595
  statusText,
596
+ statusMessage,
592
597
  headers = {},
593
598
  body,
594
599
  ...rest
@@ -596,6 +601,7 @@ export const startServer = async ({
596
601
  const responseProperties = {
597
602
  status,
598
603
  statusText,
604
+ statusMessage,
599
605
  headers,
600
606
  body,
601
607
  ...rest,
@@ -672,7 +678,9 @@ export const startServer = async ({
672
678
  onHeadersSent: ({ status, statusText }) => {
673
679
  addRequestLog(requestNode, {
674
680
  type: "info",
675
- value: `${colorizeResponseStatus(status)} ${statusText}`,
681
+ value: `${colorizeResponseStatus(status)} ${
682
+ responseProperties.statusMessage || statusText
683
+ }`,
676
684
  })
677
685
  },
678
686
  onEnd: () => {