refactor: restructure

This commit is contained in:
Derrick Hammer 2023-11-18 07:39:23 -05:00
parent 9767e82751
commit ad68d92c57
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 29 additions and 29 deletions

View File

@ -1,6 +1,7 @@
import type { ContentProvider } from "../types.js";
import * as nodePath from "path";
import { createClient } from "@lumeweb/kernel-s5-client";
import { CID, CID_TYPES } from "@lumeweb/libs5";
export default class S5Provider implements ContentProvider {
private _client = createClient();
@ -27,36 +28,35 @@ export default class S5Provider implements ContentProvider {
let file;
try {
const meta = await this._client.stat(cid);
if (meta.type !== "web_app") {
switch (CID.decode(cid).type) {
case CID_TYPES.METADATA_WEBAPP:
const meta = await this._client.stat(cid);
if (!parsedPath.base.length || !parsedPath.ext.length) {
let found = false;
for (const indexFile of meta.tryFiles) {
urlPath = nodePath.join(urlPath, indexFile);
if (urlPath.startsWith("/")) {
urlPath = urlPath.substring(1);
}
if (urlPath in meta.paths) {
found = true;
break;
}
}
if (!found) {
throw new Error("404");
}
file = meta.paths[urlPath];
} else {
if (!(urlPath in meta.paths)) {
throw new Error("404");
}
}
break;
default:
throw new Error("404");
}
if (!parsedPath.base.length || !parsedPath.ext.length) {
let found = false;
for (const indexFile of meta.tryFiles) {
urlPath = nodePath.join(urlPath, indexFile);
if (urlPath.startsWith("/")) {
urlPath = urlPath.substring(1);
}
if (urlPath in meta.paths) {
found = true;
break;
}
}
if (!found) {
throw new Error("404");
}
file = meta.paths[urlPath];
} else {
if (!(urlPath in meta.paths)) {
throw new Error("404");
}
}
} catch (e) {
throw new Error(err);
}
const headers: HeadersInit = {};