Properly set mime time and mtime from asset bundle

deno-express
Zef Hemel 2023-05-30 14:39:47 +02:00
parent b5a8cd7d1b
commit b3876e80b6
3 changed files with 9 additions and 14 deletions

View File

@ -1,9 +1,7 @@
import { FileMeta } from "../types.ts"; import { FileMeta } from "../types.ts";
import { SpacePrimitives } from "./space_primitives.ts"; import { SpacePrimitives } from "./space_primitives.ts";
import { AssetBundle } from "../../plugos/asset_bundle/bundle.ts"; import { AssetBundle } from "../../plugos/asset_bundle/bundle.ts";
import { mime } from "../deps.ts";
const bootTime = Date.now();
export class AssetBundlePlugSpacePrimitives implements SpacePrimitives { export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
constructor( constructor(
private wrapped: SpacePrimitives, private wrapped: SpacePrimitives,
@ -16,8 +14,8 @@ export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
return this.assetBundle.listFiles() return this.assetBundle.listFiles()
.map((p) => ({ .map((p) => ({
name: p, name: p,
contentType: mime.getType(p) || "application/octet-stream", contentType: this.assetBundle.getMimeType(p),
lastModified: bootTime, lastModified: this.assetBundle.getMtime(p),
perm: "ro", perm: "ro",
size: -1, size: -1,
} as FileMeta)).concat(files); } as FileMeta)).concat(files);
@ -32,10 +30,10 @@ export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
return Promise.resolve({ return Promise.resolve({
data, data,
meta: { meta: {
lastModified: bootTime, contentType: this.assetBundle.getMimeType(name),
lastModified: this.assetBundle.getMtime(name),
size: data.byteLength, size: data.byteLength,
perm: "ro", perm: "ro",
contentType: this.assetBundle.getMimeType(name),
} as FileMeta, } as FileMeta,
}); });
} }
@ -46,10 +44,10 @@ export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
if (this.assetBundle.has(name)) { if (this.assetBundle.has(name)) {
const data = this.assetBundle.readFileSync(name); const data = this.assetBundle.readFileSync(name);
return Promise.resolve({ return Promise.resolve({
lastModified: bootTime, contentType: this.assetBundle.getMimeType(name),
lastModified: this.assetBundle.getMtime(name),
size: data.byteLength, size: data.byteLength,
perm: "ro", perm: "ro",
contentType: this.assetBundle.getMimeType(name),
} as FileMeta); } as FileMeta);
} }
return this.wrapped.getFileMeta(name); return this.wrapped.getFileMeta(name);

View File

@ -83,6 +83,7 @@ self.addEventListener("fetch", (event: any) => {
const requestUrl = new URL(event.request.url); const requestUrl = new URL(event.request.url);
const pathname = requestUrl.pathname; const pathname = requestUrl.pathname;
// console.log("In service worker, pathname is", pathname);
// If this is a /.fs request, this can either be a plug worker load or an attachment load // If this is a /.fs request, this can either be a plug worker load or an attachment load
if (pathname.startsWith("/.fs")) { if (pathname.startsWith("/.fs")) {
if (fileContentTable && !event.request.headers.has("x-sync-mode")) { if (fileContentTable && !event.request.headers.has("x-sync-mode")) {

View File

@ -200,13 +200,9 @@ export class Space extends EventEmitter<SpaceEvents> {
writeAttachment( writeAttachment(
name: string, name: string,
data: Uint8Array, data: Uint8Array,
selfUpdate?: boolean | undefined, selfUpdate?: boolean,
): Promise<AttachmentMeta> { ): Promise<AttachmentMeta> {
return this.spacePrimitives.writeFile( return this.spacePrimitives.writeFile(name, data, selfUpdate);
name,
data as Uint8Array,
selfUpdate,
);
} }
deleteAttachment(name: string): Promise<void> { deleteAttachment(name: string): Promise<void> {