From ad68d92c5718fc6d379221721f693a93a118d1a6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 18 Nov 2023 07:39:23 -0500 Subject: [PATCH] refactor: restructure --- src/backend/providers/s5.ts | 58 ++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/backend/providers/s5.ts b/src/backend/providers/s5.ts index 6a1d22e..683584c 100644 --- a/src/backend/providers/s5.ts +++ b/src/backend/providers/s5.ts @@ -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 = {};