-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
314 lines (273 loc) · 9.81 KB
/
mod.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { dirname, join, parse, relative } from "@std/path"
import { readAll, readerFromStreamReader } from "@std/io"
import { decodeBase64Url, encodeBase64Url } from "@std/encoding/base64url"
import { contentType } from "@std/media-types"
/**
* Represents metadata associated with a piece of content.
*/
export interface Metadata {
/**
* The actual content, such as text or raw data.
*/
content: string
/**
* The content type (MIME type) of the content, e.g., "text/plain", "image/jpeg".
*/
contentType: string
/**
* Timestamp (in Unix epoch time or similar) indicating when the content was last modified.
*/
lastModified: number
}
/**
* Bundlee main class
*/
export class Bundlee {
private loadedBundle?: Record<string, Metadata>
private cache: Record<string, Metadata> = {}
/**
* Factory function that loads a bundle JSON file and creates a new instance of `Bundlee`.
* @param fileUrl The URL of the bundle JSON file.
* @param importType The type of import to use. Possible values are "import", "fetch", and "local". The default value is "local".
* @returns A promise that resolves to a new instance of `Bundlee`.
*/
static async load(
fileUrl: string,
importType: "import" | "fetch" | "local" = "local",
): Promise<Bundlee> {
const inst = new Bundlee()
await inst.import(fileUrl, importType)
return inst
}
/**
* Recursively read a directory and return a list of files.
* @param path The directory path.
* @param extensionFilter An optional list of extensions to filter files.
* @returns A promise that resolves to an array of file paths.
*/
private async recursiveReaddir(
path: string,
extensionFilter?: string[],
): Promise<string[]> {
const files: string[] = []
const getFiles = async (path: string) => {
for await (const dirEntry of Deno.readDir(path)) {
if (dirEntry.isDirectory) {
await getFiles(join(path, dirEntry.name))
} else if (dirEntry.isFile) {
const pathInfo = parse(dirEntry.name)
if (!extensionFilter || extensionFilter.includes(pathInfo.ext)) {
files.push(join(path, dirEntry.name))
}
}
}
}
await getFiles(path)
return files
}
/**
* Bundle files into a single JSON object.
* @param basePath The base path for file paths.
* @param fileList A list of file paths.
* @returns A promise that resolves to a JSON object containing encodeBase64Urld file contents.
*/
private async bundleFiles(
basePath: string,
fileList: string[],
): Promise<Record<string, Metadata>> {
const result: Record<string, Metadata> = {}
for (const file of fileList) {
const src = await Deno.open(file)
const dst = new TransformStream()
src.readable
.pipeThrough(new CompressionStream("gzip"))
.pipeTo(dst.writable)
const relativePath = relative(basePath, file).replaceAll("\\", "/")
const content = encodeBase64Url(
await readAll(readerFromStreamReader(dst.readable.getReader())),
)
const contentType = await this.getContentType(file)
const lastModified = (await Deno.stat(file)).mtime?.getTime() || 0
result[relativePath] = { content, contentType, lastModified }
}
return result
}
/**
* Determines the likely content type (MIME type) of a file based on its file extension.
*
* @param filePath - The path to the file.
* @returns The inferred content type, or "application/octet-stream" if unknown.
*/
private getContentType(filePath: string): string {
const filePathInParts = parse(filePath)
return contentType(filePathInParts.ext) || "application/octet-stream"
}
/**
* Bundle files from a directory into a single JSON object.
* @param {string} basePath - The base path for file paths.
* @param {string} path - The directory path.
* @param {string[]} [exts] - An optional list of extensions to filter files.
* @returns {Promise<Record<string, Metadata>>} - A promise that resolves to a JSON object containing encodeBase64Urld file contents.
*/
async bundle(
basePath: string,
path: string,
exts?: string[],
): Promise<Record<string, Metadata>> {
const fileList = await this.recursiveReaddir(join(basePath, path), exts)
if (!fileList || fileList.length === 0) {
throw new Error("No input files found")
}
return await this.bundleFiles(basePath, fileList)
}
/**
* Checks if a file exists in a JSON bundle.
* @param filePath The path of the file to retrieve.
* @returns true if the file exists in the bundle, false otherwise.
*/
has(filePath: string): boolean {
if (this.loadedBundle && (this.loadedBundle[filePath] !== undefined)) {
return true
} else {
return false
}
}
/**
* Get the content of a file from a JSON bundle.
* @param filePath The path of the file to retrieve.
* @returns A promise that resolves to the metadata object for the file, containing its content, content type, and last modified time.
*/
async get(filePath: string): Promise<Metadata> {
if (!this.loadedBundle) {
throw new Error("No bundle loaded.")
}
if (this.cache[filePath]) {
return this.cache[filePath]
}
const metadata = this.loadedBundle[filePath]
if (metadata) {
// decodeBase64Url base64 encodeBase64Urld string to Uint8Array
const compressedContent = decodeBase64Url(metadata.content)
// Set up a stream source
const src = new TransformStream<Uint8Array>()
// Set up a stream destination
const dest = new TransformStream<Uint8Array>()
// Run compressed data through DecompressionStream
src.readable
.pipeThrough(new DecompressionStream("gzip"))
.pipeTo(dest.writable)
// Feed the source
const writer = src.writable.getWriter()
const reader = dest.readable.getReader()
writer.write(compressedContent)
await writer.close()
// decodeBase64Url Uint8Array to string
const text = new TextDecoder().decode(
await readAll(readerFromStreamReader(reader)),
)
// Update the content in the Metadata object and cache it
const updatedMetadata: Metadata = {
content: text,
contentType: metadata.contentType,
lastModified: metadata.lastModified,
}
this.cache[filePath] = updatedMetadata
return updatedMetadata
} else {
throw new Error("Requested file not found in bundle.")
}
}
/**
* Imports a bundle JSON file.
*
* Used by the load()-factory
*
* @param fileUrl The URL of the bundle JSON file.
* @param importType The type of import to use. Possible values are "import", "fetch", and "local". The default value is "local".
* @returns A promise that resolves when the bundle is loaded.
*/
async import(
fileUrl: string,
importType: "import" | "fetch" | "local" = "import",
) {
if (importType === "fetch") {
await this.importRemote(fileUrl)
} else if (importType === "import") {
await this.importAsModule(fileUrl)
} else {
await this.importLocal(fileUrl)
}
}
/**
* Imports a bundle JSON file using the `fetch()` function.
* @param fileUrl The URL of the bundle JSON file.
* @returns A promise that resolves when the bundle is loaded.
*/
async importRemote(fileUrl: string) {
const response = await fetch(fileUrl)
if (!response.ok) {
throw new Error(`Failed to fetch bundle from ${fileUrl}`)
}
this.loadedBundle = await response.json()
}
/**
* Imports a bundle JSON file from the local filesystem.
* @param fileUrl The path of the bundle JSON file.
* @returns A promise that resolves when the bundle is loaded.
*/
async importLocal(fileUrl: string) {
const fileContent = await Deno.readTextFile(fileUrl)
this.loadedBundle = JSON.parse(fileContent)
}
/**
* Imports a bundle JSON file using the `import()` function.
* @param fileUrl The URL of the bundle JSON file.
* @returns A promise that resolves when the bundle is loaded.
*/
async importAsModule(fileUrl: string) {
this.loadedBundle = await import(fileUrl, {
with: { type: "json" },
})
}
/**
* Preloads the cache with all the files in the bundle.
* @returns A promise that resolves when the cache is loaded.
*/
async preloadCache(): Promise<void> {
if (!this.loadedBundle) {
throw new Error("No bundle loaded.")
}
const fileKeys = Object.keys(this.loadedBundle)
for (const key of fileKeys) {
await this.get(key)
}
}
/**
* Restores all files in the bundle to the filesystem, including their modified time.
* @param targetPath The path to restore files to.
* @returns A promise that resolves when all files are restored.
*/
async restore(targetPath: string): Promise<void> {
if (!this.loadedBundle) {
throw new Error("No bundle loaded.")
}
const fileKeys = Object.keys(this.loadedBundle)
// Create a promise array to wait for all file write and utime operations
const writePromises: Promise<void>[] = []
for (const key of fileKeys) {
const metadata = await this.get(key)
const restoredFilePath = join(targetPath, key)
// Ensure the target directory exists
await Deno.mkdir(dirname(restoredFilePath), { recursive: true })
// Write the content to the file
const contentArrayBuffer = new TextEncoder().encode(metadata.content)
const writePromise = Deno.writeFile(restoredFilePath, contentArrayBuffer)
writePromises.push(writePromise)
// Update the modified and access times
const utimePromise = writePromise.then(() => Deno.utime(restoredFilePath, metadata.lastModified / 1000, metadata.lastModified / 1000))
writePromises.push(utimePromise)
}
// Wait for all file write and utime operations to complete
await Promise.all(writePromises)
}
}