-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathfile.ts
40 lines (37 loc) · 1.25 KB
/
file.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
import { promises as fs } from "fs";
import { ono } from "@jsdevtools/ono";
import * as url from "../util/url.js";
import { ResolverError } from "../util/errors.js";
import type { ResolverOptions } from "../types/index.js";
import type { FileInfo } from "../types/index.js";
export default {
/**
* The order that this resolver will run, in relation to other resolvers.
*/
order: 100,
/**
* Determines whether this resolver can read a given file reference.
* Resolvers that return true will be tried, in order, until one successfully resolves the file.
* Resolvers that return false will not be given a chance to resolve the file.
*/
canRead(file: FileInfo) {
return url.isFileSystemPath(file.url);
},
/**
* Reads the given file and returns its raw contents as a Buffer.
*/
async read(file: FileInfo): Promise<Buffer> {
let path: string | undefined;
try {
path = url.toFileSystemPath(file.url);
} catch (err: any) {
throw new ResolverError(ono.uri(err, `Malformed URI: ${file.url}`), file.url);
}
try {
const data = await fs.readFile(path);
return data;
} catch (err: any) {
throw new ResolverError(ono(err, `Error opening file "${path}"`), path);
}
},
} as ResolverOptions;