-
Notifications
You must be signed in to change notification settings - Fork 83
/
fs.ts
122 lines (111 loc) · 3.85 KB
/
fs.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (c) 2022 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import * as crypto from "node:crypto";
import { stat as fsStat } from "node:fs/promises";
import * as fs from "node:fs";
const pathjoin = require("join-path"); // eslint-disable-line @typescript-eslint/no-var-requires
async function multiStat(
paths: string[],
): Promise<fs.Stats & { pathname: string }> {
// const pathname = paths.shift();
let err: any;
for (const pathname of paths) {
try {
const stat = await fsStat(pathname);
return { pathname, ...stat };
} catch (e: unknown) {
err = e;
}
}
throw err;
}
module.exports = function provider(options: any) {
const etagCache: Record<string, { timestamp: Date; value: string }> = {};
const cwd = options.cwd || process.cwd();
let publicPaths: string[] = options.public || ["."];
if (!Array.isArray(publicPaths)) {
publicPaths = [publicPaths];
}
async function fetchEtag(pathname: string, stat: fs.Stats): Promise<string> {
return new Promise((resolve, reject) => {
const cached = etagCache[pathname];
if (cached && cached.timestamp === stat.mtime) {
return resolve(cached.value);
}
// the file you want to get the hash
const fd = fs.createReadStream(pathname);
const hash = crypto.createHash("md5");
hash.setEncoding("hex");
fd.on("error", reject);
fd.on("end", () => {
hash.end();
const etag = hash.read();
etagCache[pathname] = {
timestamp: stat.mtime,
value: etag,
};
resolve(etag);
});
// read all file and pipe it (write it) to the hash object
return fd.pipe(hash);
});
}
return async function (
req: unknown,
pathname: string,
): Promise<{
modified: number;
size: number;
etag: string;
stream: fs.ReadStream;
} | null> {
pathname = decodeURI(pathname);
// jumping to parent directories is not allowed
if (
pathname.includes("../") ||
pathname.includes("..\\") ||
pathname.toLowerCase().includes("..%5c") ||
pathname.match(/\0/g) !== null ||
// A path that didn't start with a slash is not valid.
!pathname.startsWith("/")
) {
return Promise.resolve(null);
}
const fullPathnames: string[] = publicPaths.map((p) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
pathjoin(cwd, p, pathname),
);
try {
const stat = await multiStat(fullPathnames);
return {
modified: stat.mtime.getTime(),
size: stat.size,
etag: await fetchEtag(stat.pathname, stat),
stream: fs.createReadStream(stat.pathname),
};
} catch (err: any) {
if (["ENOENT", "ENOTDIR", "EISDIR", "EINVAL"].includes(err.code)) {
return null;
}
return Promise.reject(err);
}
};
};