-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathload.js
46 lines (41 loc) · 1.16 KB
/
load.js
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
/**
* Load content for 'source'
* @param { Source } source
* @param { Context } context
* @returns { Promise<void> }
*/
export async function load(source, context) {
if (!source.fileContent && source.filepath) {
// Raw buffer if image and not svg
const encoding =
source.type == 'image' && source.format != 'svg+xml' ? null : 'utf8';
try {
// @ts-ignore
source.fileContent = context.fs.readFileSync(source.filepath, encoding);
} catch (err) {
if (!source.isRemote) {
throw err;
}
}
if (source.isRemote) {
// node-fetch is esm-only, so support cjs via import()
const fetch = (await import('node-fetch')).default;
const res = await fetch(/** @type { string } */ (source.sourcepath));
if (!res.ok) {
throw Error(
res.status === 404 ? 'Not found' : `Fetch error: ${res.status}`,
);
}
const text = await res.text();
// Save for later
if (context.saveRemote) {
try {
context.fs.writeFileSync(source.filepath, text, 'utf8');
} catch (err) {
// Skip
}
}
source.fileContent = text;
}
}
}