deno.land / x / lume@v2.1.4 / core / utils / read.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
import { isUrl } from "./path.ts";import { env } from "./env.ts";
const useCache = env<boolean>("LUME_NOCACHE") !== true;
/** * Read a local or remote file and return its content. * If the file is remote, it will be cached in the `lume_remote_files` cache. */export async function read( path: string, isBinary: boolean,): Promise<Uint8Array | string>;export async function read( path: string, isBinary: true, init?: RequestInit,): Promise<Uint8Array>;export async function read( path: string, isBinary: false, init?: RequestInit,): Promise<string>;export async function read( path: string, isBinary: boolean, init?: RequestInit,): Promise<string | Uint8Array> { if (!isUrl(path)) { if (path.startsWith("data:")) { const response = await fetch(path);
return isBinary ? new Uint8Array(await response.arrayBuffer()) : response.text(); }
return isBinary ? Deno.readFile(path) : Deno.readTextFile(path); }
const url = new URL(path);
if (url.protocol === "file:") { return isBinary ? Deno.readFile(url) : Deno.readTextFile(url); }
if (!useCache) { const response = await fetch(url, init);
return isBinary ? new Uint8Array(await response.arrayBuffer()) : response.text(); }
const cache = await caches.open("lume_remote_files");
// Prevent https://github.com/denoland/deno/issues/19696 try { const cached = await cache.match(url);
if (cached) { return isBinary ? new Uint8Array(await cached.arrayBuffer()) : cached.text(); } } catch { // ignore }
const response = await fetch(url, init); await cache.put(url, response.clone());
return isBinary ? new Uint8Array(await response.arrayBuffer()) : response.text();}
/** * Clear the cache of remote files. */export async function clearCache() { await caches.delete("lume_remote_files");}
lume

Version Info

Tagged at
7 months ago