-
Notifications
You must be signed in to change notification settings - Fork 3
/
embed.ts
179 lines (147 loc) · 4.85 KB
/
embed.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Code in this module is used by embedded files/directories. You should not
* rely on it directly.
*
* @module
*/
import {decodeBase64} from "./deps/std/encoding/base64.ts";
type CompressionFormat = ConstructorParameters<typeof DecompressionStream>[0]
// This is a type, not a var. It's used in a JSDoc {@link} below.
// deno-lint-ignore no-unused-vars
import type { Mapping } from "./mod.ts"
// All generated files will end in this, to make sure they can't conflict
// w/ other files we may place in this directory, which won't end with this.
export const GENERATED_SUFFIX = "_.ts"
export const importMeta = import.meta
const decoder = new TextDecoder()
/**
* Represents the contents of a file that's been embedded into TypeScript.
*/
export class File {
readonly size: number
#encodedBytes: string;
#compression: CompressionFormat | undefined;
constructor(meta: FileMeta) {
this.size = meta.size
// Only decode bytes as necessary to save memory/time at startup:
this.#encodedBytes = meta.encoded
this.#compression = meta.compression
}
#decodedBytes: Uint8Array | undefined = undefined
async bytes(): Promise<Uint8Array> {
if (this.#decodedBytes !== undefined) {
return this.#decodedBytes
}
let bytes = decodeBase64(this.#encodedBytes)
if (this.#compression) {
bytes = await decompress(bytes, this.#compression)
}
this.#decodedBytes = bytes
this.#encodedBytes = "" // maybe release garbage.
return bytes
}
/**
* Parse the bytes as utf-8 text.
*/
async text(): Promise<string> {
if (this.#cachedText === undefined) {
this.#cachedText = decoder.decode(await this.bytes())
}
return this.#cachedText
}
#cachedText: undefined|string = undefined
}
/**
* Embedder stores each file in its own _.ts file, and then lists them all in
* an object of this type:
*/
type EmbedImports = Readonly<Record<string, File>>
/**
* The data we expect to find inside embedded *_.ts files.
*/
interface FileMeta {
size: number
// The base-64 encoded representation:
encoded: string
compression?: CompressionFormat
// TODO: sha256, modified time, etc.
}
/** Shortcut for `new File(opts)` */
export function F(opts: FileMeta) { return new File(opts) }
async function decompress(data: Uint8Array, compression: CompressionFormat): Promise<Uint8Array> {
let input = new Blob([data])
let ds = new DecompressionStream(compression)
let stream = input.stream().pipeThrough(ds)
let outParts: Uint8Array[] = []
let writer = new WritableStream<Uint8Array>({
write(chunk) {
outParts.push(chunk)
}
})
await stream.pipeTo(writer)
let buf = await new Blob(outParts).arrayBuffer()
return new Uint8Array(buf)
}
/**
* Makes a type-safe get() function for a dir.ts.
*
* You'll be able to call get(fileName), and the file names will be type-checked
* by TypeScript.
*/
export function G<T extends Record<string, File>>(files: T): (filePath: keyof T) => Promise<File> {
// This may one day require async for dynamic imports, so we'll require it
// now:
// deno-lint-ignore require-await
let fn = async (filePath: keyof T) => {
return files[filePath]
}
return fn
}
/**
* Allows accessing all files embedded by a {@link Mapping}.
*
* Each `dir.ts` in your Mapping `destDir` exposes an instance
* of this class as its default export.
*/
export class Embeds<T extends Record<string, File>> {
#embeds: T
constructor(embeds: T) {
this.#embeds = embeds
}
/**
* Returns a list of embed file keys.
*
* This method can be used to retrieve the keys of the embed files for
* iteration or other purposes.
*/
list(): Array<keyof T> {
return Object.keys(this.#embeds) as Array<keyof T>;
}
/**
* Type-safe method to load a known embed file.
*
* If you know you need a particular embed at compile time, using this method
* lets TypeScript check that you have specified a correct (existing) file
* path.
*/
// May eventually require async, so using async now:
// deno-lint-ignore require-await
async load(filePath: keyof T): Promise<File> {
return this.#embeds[filePath]
}
/**
* Method to do runtime loading of a file.
*
* If you're loading user-specified file paths, use this method. It will
* return `null` if no such file exists.
*/
// May eventually require async, so using async now:
// deno-lint-ignore require-await
async get(filePath: string): Promise<File|null> {
return this.#embeds[filePath] ?? null
}
}
/** Shortcut for `new Embeds(embeds)` */
export function E<T extends Record<string, File>>(embeds: T): Embeds<T> {
return new Embeds(embeds)
}