Files
plainleaf/lib/spaces/asset_bundle_space_primitives.ts
T
Zef HemelandGitHub dc2987a3f9 Sync engine rearchitecture (#1516)
* Don't require auth for .ping
* Finally introducing loggers
* Remove file list caching from disk_space_primitives
I don't think this is required anymore with the sync model
* Simplification of space primitives and more tests
* More reliable online status
* Add .zed settings
* Remove selfUpdate argument from `writeFile` no longer used
- Add `sync` config schema and document sync options
- Move sync config handling to client boot logic
- Move BootConfig type to ui_types.ts
- Remove sync env vars from server
- Update documentation for sync and architecture
* Improve sync logic and proxy routing for full sync state
* Add BoxProxy for late-bound client and improve Lua boot
- Add isMobile syscall for editor
* Add concurrency to sync process and log indexing duration
* Better service worker lifecycle management
2025-09-12 14:43:57 +02:00

84 lines
2.3 KiB
TypeScript

import type { SpacePrimitives } from "./space_primitives.ts";
import type { AssetBundle } from "../../lib/asset_bundle/bundle.ts";
import type { FileMeta } from "../../type/index.ts";
export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private assetBundle: AssetBundle,
) {
}
async fetchFileList(): Promise<FileMeta[]> {
const files = await this.wrapped.fetchFileList();
return this.assetBundle.listFiles()
.map((p) => ({
name: p,
contentType: this.assetBundle.getMimeType(p),
created: this.assetBundle.getMtime(p),
lastModified: this.assetBundle.getMtime(p),
perm: "ro",
size: -1,
} as FileMeta)).concat(files);
}
readFile(
path: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
if (this.assetBundle.has(path)) {
const data = this.assetBundle.readFileSync(path);
// console.log("Requested encoding", encoding);
return Promise.resolve({
data,
meta: {
contentType: this.assetBundle.getMimeType(path),
created: this.assetBundle.getMtime(path),
lastModified: this.assetBundle.getMtime(path),
size: data.byteLength,
perm: "ro",
} as FileMeta,
});
}
return this.wrapped.readFile(path);
}
getFileMeta(path: string, observing?: boolean): Promise<FileMeta> {
if (this.assetBundle.has(path)) {
const data = this.assetBundle.readFileSync(path);
return Promise.resolve({
contentType: this.assetBundle.getMimeType(path),
created: this.assetBundle.getMtime(path),
lastModified: this.assetBundle.getMtime(path),
size: data.byteLength,
perm: "ro",
} as FileMeta);
}
return this.wrapped.getFileMeta(path, observing);
}
writeFile(
path: string,
data: Uint8Array,
meta?: FileMeta,
): Promise<FileMeta> {
if (this.assetBundle.has(path)) {
console.warn("Attempted to write to read-only asset file", path);
return this.getFileMeta(path);
}
return this.wrapped.writeFile(
path,
data,
meta,
);
}
deleteFile(path: string): Promise<void> {
if (this.assetBundle.has(path)) {
// Quietly ignore
return Promise.resolve();
}
return this.wrapped.deleteFile(path);
}
}