1
0
silverbullet/common/spaces/asset_bundle_space_primitives.ts

82 lines
2.2 KiB
TypeScript
Raw Normal View History

import { SpacePrimitives } from "./space_primitives.ts";
2022-10-12 09:47:13 +00:00
import { AssetBundle } from "../../plugos/asset_bundle/bundle.ts";
2023-08-20 15:51:00 +00:00
import { FileMeta } from "$sb/types.ts";
export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private assetBundle: AssetBundle,
) {
}
async fetchFileList(): Promise<FileMeta[]> {
2023-01-13 14:41:29 +00:00
const files = await this.wrapped.fetchFileList();
return this.assetBundle.listFiles()
2022-10-12 09:47:13 +00:00
.map((p) => ({
name: p,
contentType: this.assetBundle.getMimeType(p),
lastModified: this.assetBundle.getMtime(p),
2022-10-12 09:47:13 +00:00
perm: "ro",
size: -1,
2023-01-13 14:41:29 +00:00
} as FileMeta)).concat(files);
}
readFile(
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
2022-10-12 09:47:13 +00:00
if (this.assetBundle.has(name)) {
const data = this.assetBundle.readFileSync(name);
// console.log("Requested encoding", encoding);
return Promise.resolve({
data,
meta: {
contentType: this.assetBundle.getMimeType(name),
lastModified: this.assetBundle.getMtime(name),
size: data.byteLength,
perm: "ro",
} as FileMeta,
});
}
return this.wrapped.readFile(name);
}
getFileMeta(name: string): Promise<FileMeta> {
2022-10-12 09:47:13 +00:00
if (this.assetBundle.has(name)) {
const data = this.assetBundle.readFileSync(name);
return Promise.resolve({
contentType: this.assetBundle.getMimeType(name),
lastModified: this.assetBundle.getMtime(name),
size: data.byteLength,
perm: "ro",
} as FileMeta);
}
return this.wrapped.getFileMeta(name);
}
writeFile(
name: string,
data: Uint8Array,
2023-01-13 14:41:29 +00:00
selfUpdate?: boolean,
meta?: FileMeta,
): Promise<FileMeta> {
if (this.assetBundle.has(name)) {
console.warn("Attempted to write to read-only asset file", name);
return this.getFileMeta(name);
}
return this.wrapped.writeFile(
name,
data,
selfUpdate,
meta,
);
}
deleteFile(name: string): Promise<void> {
2022-10-12 09:47:13 +00:00
if (this.assetBundle.has(name)) {
// Quietly ignore
return Promise.resolve();
}
return this.wrapped.deleteFile(name);
}
}