-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathloader-sha512.mjs
More file actions
50 lines (38 loc) · 1.25 KB
/
loader-sha512.mjs
File metadata and controls
50 lines (38 loc) · 1.25 KB
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
import crypto from 'crypto';
import path from 'path';
const shaRegExp = /-sha512(\.mjs)$/;
function getSourcePath(p) {
if (p.protocol !== `file:`)
return p;
const pString = p.toString();
const pFixed = pString.replace(shaRegExp, `$1`);
if (pFixed === pString)
return p;
return new URL(pFixed);
}
export function getFileSystem(defaultGetFileSystem) {
const fileSystem = defaultGetFileSystem();
return {
readFileSync(p) {
const fixedP = getSourcePath(p);
if (fixedP === p)
return fileSystem.readFileSync(p);
const content = fileSystem.readFileSync(fixedP);
const hash = crypto.createHash(`sha512`).update(content).digest(`hex`);
return Buffer.from(`export default ${JSON.stringify(hash)};`);
},
statEntrySync(p) {
const fixedP = getSourcePath(p);
return fileSystem.statEntrySync(fixedP);
},
realpathSync(p) {
const fixedP = getSourcePath(p);
if (fixedP === p)
return fileSystem.realpathSync(p);
const realpath = fileSystem.realpathSync(fixedP);
if (path.extname(realpath) !== `.mjs`)
throw new Error(`Paths must be .mjs extension to go through the sha512 loader`);
return realpath.replace(/\.mjs$/, `-sha512.mjs`);
},
};
}