Big refactors and fixes
* Query regen * Fix anchor completion * Dependency fixes * Changelog update
This commit is contained in:
@@ -57,7 +57,7 @@ export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
if (this.assetBundle.has(name)) {
|
||||
console.warn("Attempted to write to read-only asset file", name);
|
||||
@@ -67,7 +67,7 @@ export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
|
||||
name,
|
||||
data,
|
||||
selfUpdate,
|
||||
lastModified,
|
||||
meta,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ export class DiskSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
_selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
const localPath = this.filenameToPath(name);
|
||||
try {
|
||||
@@ -87,9 +87,9 @@ export class DiskSpacePrimitives implements SpacePrimitives {
|
||||
// Actually write the file
|
||||
await Deno.write(file.rid, data);
|
||||
|
||||
if (lastModified) {
|
||||
console.log("Seting mtime to", new Date(lastModified));
|
||||
await Deno.futime(file.rid, new Date(), new Date(lastModified));
|
||||
if (meta?.lastModified) {
|
||||
// console.log("Seting mtime to", new Date(meta.lastModified));
|
||||
await Deno.futime(file.rid, new Date(), new Date(meta.lastModified));
|
||||
}
|
||||
file.close();
|
||||
|
||||
|
||||
@@ -20,13 +20,13 @@ export class EventedSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
const newMeta = await this.wrapped.writeFile(
|
||||
name,
|
||||
data,
|
||||
selfUpdate,
|
||||
lastModified,
|
||||
meta,
|
||||
);
|
||||
// This can happen async
|
||||
if (name.endsWith(".md")) {
|
||||
@@ -47,7 +47,7 @@ export class EventedSpacePrimitives implements SpacePrimitives {
|
||||
console.error("Error dispatching page:saved event", e);
|
||||
});
|
||||
}
|
||||
if (name.endsWith(".plug.js")) {
|
||||
if (name.startsWith("_plug/") && name.endsWith(".plug.js")) {
|
||||
await this.eventHook.dispatchEvent("plug:changed", name);
|
||||
}
|
||||
return newMeta;
|
||||
|
||||
@@ -35,9 +35,9 @@ export class FallbackSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean | undefined,
|
||||
lastModified?: number | undefined,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
return this.primary.writeFile(name, data, selfUpdate, lastModified);
|
||||
return this.primary.writeFile(name, data, selfUpdate, meta);
|
||||
}
|
||||
deleteFile(name: string): Promise<void> {
|
||||
return this.primary.deleteFile(name);
|
||||
|
||||
@@ -43,21 +43,40 @@ export class FileMetaSpacePrimitives implements SpacePrimitives {
|
||||
return this.wrapped.readFile(name);
|
||||
}
|
||||
|
||||
getFileMeta(name: string): Promise<FileMeta> {
|
||||
return this.wrapped.getFileMeta(name);
|
||||
async getFileMeta(name: string): Promise<FileMeta> {
|
||||
const meta = await this.wrapped.getFileMeta(name);
|
||||
if (name.endsWith(".md")) {
|
||||
const pageName = name.slice(0, -3);
|
||||
const additionalMeta = await this.indexSyscalls["index.get"](
|
||||
{} as any,
|
||||
pageName,
|
||||
"meta:",
|
||||
);
|
||||
if (additionalMeta) {
|
||||
for (const [k, v] of Object.entries(additionalMeta)) {
|
||||
if (
|
||||
["name", "lastModified", "size", "perm", "contentType"].includes(k)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
meta[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
return meta;
|
||||
}
|
||||
|
||||
writeFile(
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
return this.wrapped.writeFile(
|
||||
name,
|
||||
data,
|
||||
selfUpdate,
|
||||
lastModified,
|
||||
meta,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ export class FilteredSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean | undefined,
|
||||
lastModified?: number | undefined,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
return this.wrapped.writeFile(name, data, selfUpdate, lastModified);
|
||||
return this.wrapped.writeFile(name, data, selfUpdate, meta);
|
||||
}
|
||||
deleteFile(name: string): Promise<void> {
|
||||
return this.wrapped.deleteFile(name);
|
||||
|
||||
@@ -23,7 +23,7 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
|
||||
const result = await fetch(url, { ...options });
|
||||
if (
|
||||
result.status === 401
|
||||
this.getRealStatus(result) === 401
|
||||
) {
|
||||
// Invalid credentials, reloading the browser should trigger authentication
|
||||
console.log("Going to redirect after", url);
|
||||
@@ -33,13 +33,20 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
return result;
|
||||
}
|
||||
|
||||
getRealStatus(r: Response) {
|
||||
if (r.headers.get("X-Status")) {
|
||||
return +r.headers.get("X-Status")!;
|
||||
}
|
||||
return r.status;
|
||||
}
|
||||
|
||||
async fetchFileList(): Promise<FileMeta[]> {
|
||||
const resp = await this.authenticatedFetch(this.url, {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (
|
||||
resp.status === 200 &&
|
||||
this.getRealStatus(resp) === 200 &&
|
||||
this.expectedSpacePath &&
|
||||
resp.headers.get("X-Space-Path") !== this.expectedSpacePath
|
||||
) {
|
||||
@@ -60,7 +67,7 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
method: "GET",
|
||||
},
|
||||
);
|
||||
if (res.status === 404) {
|
||||
if (this.getRealStatus(res) === 404) {
|
||||
throw new Error(`Not found`);
|
||||
}
|
||||
return {
|
||||
@@ -73,13 +80,14 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
_selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/octet-stream",
|
||||
};
|
||||
if (lastModified) {
|
||||
headers["X-Last-Modified"] = "" + lastModified;
|
||||
if (meta) {
|
||||
headers["X-Last-Modified"] = "" + meta.lastModified;
|
||||
headers["X-Perm"] = "" + meta.perm;
|
||||
}
|
||||
|
||||
const res = await this.authenticatedFetch(
|
||||
@@ -101,7 +109,7 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
method: "DELETE",
|
||||
},
|
||||
);
|
||||
if (req.status !== 200) {
|
||||
if (this.getRealStatus(req) !== 200) {
|
||||
throw Error(`Failed to delete file: ${req.statusText}`);
|
||||
}
|
||||
}
|
||||
@@ -113,7 +121,7 @@ export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
method: "OPTIONS",
|
||||
},
|
||||
);
|
||||
if (res.status === 404) {
|
||||
if (this.getRealStatus(res) === 404) {
|
||||
throw new Error(`Not found`);
|
||||
}
|
||||
return this.responseToMeta(name, res);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { mime } from "../deps.ts";
|
||||
|
||||
export type FileContent = {
|
||||
name: string;
|
||||
meta: FileMeta;
|
||||
data: Uint8Array;
|
||||
};
|
||||
|
||||
@@ -35,10 +36,6 @@ export class IndexedDBSpacePrimitives implements SpacePrimitives {
|
||||
async readFile(
|
||||
name: string,
|
||||
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
||||
const fileMeta = await this.filesMetaTable.get(name);
|
||||
if (!fileMeta) {
|
||||
throw new Error("Not found");
|
||||
}
|
||||
const fileContent = await this.filesContentTable.get(name);
|
||||
if (!fileContent) {
|
||||
throw new Error("Not found");
|
||||
@@ -46,7 +43,7 @@ export class IndexedDBSpacePrimitives implements SpacePrimitives {
|
||||
|
||||
return {
|
||||
data: fileContent.data,
|
||||
meta: fileMeta,
|
||||
meta: fileContent.meta,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -54,18 +51,18 @@ export class IndexedDBSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
_selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
suggestedMeta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
const fileMeta: FileMeta = {
|
||||
const meta: FileMeta = {
|
||||
name,
|
||||
lastModified: lastModified || Date.now(),
|
||||
lastModified: suggestedMeta?.lastModified || Date.now(),
|
||||
contentType: mime.getType(name) || "application/octet-stream",
|
||||
size: data.byteLength,
|
||||
perm: "rw",
|
||||
perm: suggestedMeta?.perm || "rw",
|
||||
};
|
||||
await this.filesContentTable.put({ name, data });
|
||||
await this.filesMetaTable.put(fileMeta);
|
||||
return fileMeta;
|
||||
await this.filesContentTable.put({ name, data, meta });
|
||||
await this.filesMetaTable.put(meta);
|
||||
return meta;
|
||||
}
|
||||
|
||||
async deleteFile(name: string): Promise<void> {
|
||||
|
||||
@@ -4,11 +4,6 @@ import {
|
||||
NamespaceOperation,
|
||||
PageNamespaceHook,
|
||||
} from "../hooks/page_namespace.ts";
|
||||
import {
|
||||
base64DecodeDataUrl,
|
||||
base64EncodedDataUrl,
|
||||
} from "../../plugos/asset_bundle/base64.ts";
|
||||
import { mime } from "../deps.ts";
|
||||
|
||||
export class PlugSpacePrimitives implements SpacePrimitives {
|
||||
constructor(
|
||||
@@ -40,6 +35,7 @@ export class PlugSpacePrimitives implements SpacePrimitives {
|
||||
for (
|
||||
const { operation, pattern, plug, name, env } of this.hook.spaceFunctions
|
||||
) {
|
||||
// console.log("Going to match agains pattern", pattern, path);
|
||||
if (
|
||||
operation === type && path.match(pattern) &&
|
||||
(!this.env || (env && env === this.env))
|
||||
@@ -73,16 +69,13 @@ export class PlugSpacePrimitives implements SpacePrimitives {
|
||||
async readFile(
|
||||
name: string,
|
||||
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
||||
const result: { data: string; meta: FileMeta } | false = await this
|
||||
const result: { data: Uint8Array; meta: FileMeta } | false = await this
|
||||
.performOperation(
|
||||
"readFile",
|
||||
name,
|
||||
);
|
||||
if (result) {
|
||||
return {
|
||||
data: base64DecodeDataUrl(result.data),
|
||||
meta: result.meta,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
return this.wrapped.readFile(name);
|
||||
}
|
||||
@@ -99,16 +92,14 @@ export class PlugSpacePrimitives implements SpacePrimitives {
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta> {
|
||||
const result = this.performOperation(
|
||||
"writeFile",
|
||||
name,
|
||||
base64EncodedDataUrl(
|
||||
mime.getType(name) || "application/octet-stream",
|
||||
data,
|
||||
),
|
||||
data,
|
||||
selfUpdate,
|
||||
meta,
|
||||
);
|
||||
if (result) {
|
||||
return result;
|
||||
@@ -118,7 +109,7 @@ export class PlugSpacePrimitives implements SpacePrimitives {
|
||||
name,
|
||||
data,
|
||||
selfUpdate,
|
||||
lastModified,
|
||||
meta,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ export interface SpacePrimitives {
|
||||
data: Uint8Array,
|
||||
// Used to decide whether or not to emit change events
|
||||
selfUpdate?: boolean,
|
||||
lastModified?: number,
|
||||
meta?: FileMeta,
|
||||
): Promise<FileMeta>;
|
||||
deleteFile(name: string): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ export class SpaceSync {
|
||||
name,
|
||||
data,
|
||||
false,
|
||||
meta.lastModified,
|
||||
meta,
|
||||
);
|
||||
snapshot.set(name, [
|
||||
primaryHash,
|
||||
@@ -157,7 +157,7 @@ export class SpaceSync {
|
||||
name,
|
||||
data,
|
||||
false,
|
||||
meta.lastModified,
|
||||
meta,
|
||||
);
|
||||
snapshot.set(name, [
|
||||
writtenMeta.lastModified,
|
||||
@@ -219,7 +219,7 @@ export class SpaceSync {
|
||||
name,
|
||||
data,
|
||||
false,
|
||||
meta.lastModified,
|
||||
meta,
|
||||
);
|
||||
snapshot.set(name, [
|
||||
primaryHash,
|
||||
@@ -243,7 +243,7 @@ export class SpaceSync {
|
||||
name,
|
||||
data,
|
||||
false,
|
||||
meta.lastModified,
|
||||
meta,
|
||||
);
|
||||
snapshot.set(name, [
|
||||
writtenMeta.lastModified,
|
||||
|
||||
Reference in New Issue
Block a user