deno.land / x / esm@v135_2 / packages / esm-worker / src / utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { HttpMetadata, WorkerStorage, WorkerStorageKV } from "../types/index";import { targets } from "esm-compat";import { fixedPkgVersions } from "./consts.ts";
export function hasTargetSegment(path: string) { const parts = path.slice(1).split("/"); return parts.length >= 2 && parts.some((p) => targets.has(p));}
export function asKV( storage?: R2Bucket | WorkerStorage,): WorkerStorageKV | undefined { if (!storage) { return undefined; } return globalThis.__AS_KV__ ?? (globalThis.__AS_KV__ = { async getWithMetadata( key: string, _type: "stream", ): Promise< { value: ReadableStream | null; metadata: HttpMetadata | null } > { const ret = await storage.get(key); if (ret === null) { return { value: null, metadata: null }; } return { value: ret.body, metadata: ret.customMetadata as HttpMetadata | undefined ?? null, }; }, async put( key: string, value: ArrayBuffer | Uint8Array | ReadableStream, options?: { metadata?: HttpMetadata }, ): Promise<void> { await storage.put(key, value, { customMetadata: options?.metadata }); }, });}
export function fixPkgVersion(pkg: string, version: string) { for (const [k, v] of Object.entries(fixedPkgVersions)) { if (`${pkg}@${version}`.startsWith(k)) { return v; } } return version;}
export function trimPrefix(s: string, prefix: string): string { if (prefix !== "" && s.startsWith(prefix)) { return s.slice(prefix.length); } return s;}
export function splitBy( s: string, searchString: string, fromLast = false,): [string, string] { const i = fromLast ? s.lastIndexOf(searchString) : s.indexOf(searchString); if (i >= 0) { return [s.slice(0, i), s.slice(i + searchString.length)]; } return [s, ""];}
/** create redirect response. */export function redirect( url: URL | string, status: 301 | 302, cacheMaxAge = 600,) { const headers = corsHeaders(); headers.set("Location", url.toString()); if (status === 301) { headers.set("Cache-Control", "public, max-age=31536000, immutable"); } else { headers.set( "Cache-Control", `public, max-age=${cacheMaxAge}`, ); } return new Response(null, { status, headers });}
export function err(message: string, status: number = 500) { return new Response( message, { status, headers: corsHeaders() }, );}
export function errPkgNotFound(pkg: string) { const headers = corsHeaders(); headers.set("Content-Type", "application/javascript; charset=utf-8"); headers.set("Cache-Control", "private, no-cache, no-store, must-revalidate"); return new Response( [ `/* esm.sh - error */`, `throw new Error("[esm.sh] " + "npm: package '${pkg}' not found");`, `export default null;`, ].join("\n"), { status: 404, headers }, );}
export function checkPreflight(req: Request): Response | undefined { if (req.method === "OPTIONS" && req.headers.has("Origin")) { const headers = new Headers({ "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": req.headers.get( "Access-Control-Request-Method", )!, "Access-Control-Allow-Headers": req.headers.get( "Access-Control-Request-Headers", )!, }); headers.append("Vary", "Origin"); headers.append("Vary", "Access-Control-Request-Method"); headers.append("Vary", "Access-Control-Request-Headers"); return new Response(null, { headers }); } return void 0;}
export function corsHeaders() { return new Headers({ "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "*", "Vary": "Origin", });}
export function copyHeaders(dst: Headers, src: Headers, ...keys: string[]) { for (const k of keys) { if (src.has(k)) { dst.set(k, src.get(k)!); } }}
export async function hashText(s: string): Promise<string> { const buffer = await crypto.subtle.digest( "SHA-1", new TextEncoder().encode(s), ); return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");}
esm

Version Info

Tagged at
a year ago