@jsenv/server 12.7.0 → 12.7.1
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 +3 -2
- package/src/fetch_filesystem.js +22 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/server",
|
|
3
|
-
"version": "12.7.
|
|
3
|
+
"version": "12.7.1",
|
|
4
4
|
"description": "Write your Node.js server using pure functions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@jsenv/abort": "4.2.2",
|
|
39
39
|
"@jsenv/logger": "4.1.1",
|
|
40
|
-
"@jsenv/url-meta": "7.0.0"
|
|
40
|
+
"@jsenv/url-meta": "7.0.0",
|
|
41
|
+
"@jsenv/utils": "2.0.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@jsenv/fetch": "../fetch/",
|
package/src/fetch_filesystem.js
CHANGED
|
@@ -4,23 +4,20 @@
|
|
|
4
4
|
* It is meant to be used inside "requestToResponse"
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { createReadStream, statSync,
|
|
8
|
-
import { CONTENT_TYPE } from "@jsenv/utils/content_type/content_type.js"
|
|
9
|
-
|
|
7
|
+
import { createReadStream, statSync, readFile } from "node:fs"
|
|
10
8
|
import {
|
|
11
9
|
isFileSystemPath,
|
|
12
|
-
urlToFileSystemPath,
|
|
13
10
|
fileSystemPathToUrl,
|
|
14
11
|
} from "@jsenv/server/src/internal/filesystem.js"
|
|
15
12
|
import { bufferToEtag } from "@jsenv/server/src/internal/etag.js"
|
|
13
|
+
import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js"
|
|
14
|
+
|
|
16
15
|
import { composeTwoResponses } from "./internal/response_composition.js"
|
|
17
16
|
import { convertFileSystemErrorToResponseProperties } from "./internal/convertFileSystemErrorToResponseProperties.js"
|
|
18
17
|
import { timeFunction } from "./server_timing/timing_measure.js"
|
|
19
18
|
import { negotiateContentEncoding } from "./content_negotiation/negotiateContentEncoding.js"
|
|
20
19
|
import { serveDirectory } from "./serve_directory.js"
|
|
21
20
|
|
|
22
|
-
const ETAG_MEMORY_MAP = new Map()
|
|
23
|
-
|
|
24
21
|
export const fetchFileSystem = async (
|
|
25
22
|
filesystemUrl,
|
|
26
23
|
{
|
|
@@ -95,13 +92,11 @@ export const fetchFileSystem = async (
|
|
|
95
92
|
}
|
|
96
93
|
}
|
|
97
94
|
|
|
98
|
-
|
|
99
|
-
const sourceFilesystemPath = urlToFileSystemPath(filesystemUrl)
|
|
100
|
-
|
|
95
|
+
const sourceUrl = `file://${new URL(urlString).pathname}`
|
|
101
96
|
try {
|
|
102
97
|
const [readStatTiming, sourceStat] = await timeFunction(
|
|
103
98
|
"file service>read file stat",
|
|
104
|
-
() => statSync(
|
|
99
|
+
() => statSync(new URL(sourceUrl)),
|
|
105
100
|
)
|
|
106
101
|
if (sourceStat.isDirectory()) {
|
|
107
102
|
if (canReadDirectory) {
|
|
@@ -283,6 +278,7 @@ const getEtagResponse = async ({
|
|
|
283
278
|
}
|
|
284
279
|
}
|
|
285
280
|
|
|
281
|
+
const ETAG_MEMORY_MAP = new Map()
|
|
286
282
|
const computeEtag = async ({
|
|
287
283
|
etagMemory,
|
|
288
284
|
etagMemoryMaxSize,
|
|
@@ -298,9 +294,15 @@ const computeEtag = async ({
|
|
|
298
294
|
return etagMemoryEntry.eTag
|
|
299
295
|
}
|
|
300
296
|
}
|
|
301
|
-
const fileContentAsBuffer = await
|
|
302
|
-
|
|
303
|
-
|
|
297
|
+
const fileContentAsBuffer = await new Promise((resolve, reject) => {
|
|
298
|
+
readFile(new URL(sourceUrl), (error, buffer) => {
|
|
299
|
+
if (error) {
|
|
300
|
+
reject(error)
|
|
301
|
+
} else {
|
|
302
|
+
resolve(buffer)
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
})
|
|
304
306
|
const eTag = bufferToEtag(fileContentAsBuffer)
|
|
305
307
|
if (etagMemory) {
|
|
306
308
|
if (ETAG_MEMORY_MAP.size >= etagMemoryMaxSize) {
|
|
@@ -386,20 +388,23 @@ const getCompressedResponse = async ({ sourceUrl, headers }) => {
|
|
|
386
388
|
}
|
|
387
389
|
|
|
388
390
|
const fileUrlToReadableStream = (fileUrl) => {
|
|
389
|
-
return createReadStream(
|
|
391
|
+
return createReadStream(new URL(fileUrl), {
|
|
392
|
+
emitClose: true,
|
|
393
|
+
autoClose: true,
|
|
394
|
+
})
|
|
390
395
|
}
|
|
391
396
|
|
|
392
397
|
const availableCompressionFormats = {
|
|
393
398
|
br: async (fileReadableStream) => {
|
|
394
|
-
const { createBrotliCompress } = await import("zlib")
|
|
399
|
+
const { createBrotliCompress } = await import("node:zlib")
|
|
395
400
|
return fileReadableStream.pipe(createBrotliCompress())
|
|
396
401
|
},
|
|
397
402
|
deflate: async (fileReadableStream) => {
|
|
398
|
-
const { createDeflate } = await import("zlib")
|
|
403
|
+
const { createDeflate } = await import("node:zlib")
|
|
399
404
|
return fileReadableStream.pipe(createDeflate())
|
|
400
405
|
},
|
|
401
406
|
gzip: async (fileReadableStream) => {
|
|
402
|
-
const { createGzip } = await import("zlib")
|
|
407
|
+
const { createGzip } = await import("node:zlib")
|
|
403
408
|
return fileReadableStream.pipe(createGzip())
|
|
404
409
|
},
|
|
405
410
|
}
|