feat: add s5 support

This commit is contained in:
Derrick Hammer 2023-11-17 21:02:35 -05:00
parent 9836b5de26
commit 0cf849a9d5
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 92 additions and 0 deletions

9
package-lock.json generated
View File

@ -17,6 +17,7 @@
"@lumeweb/kernel-ipfs-client": "^0.1.0-develop.25", "@lumeweb/kernel-ipfs-client": "^0.1.0-develop.25",
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.10", "@lumeweb/kernel-network-registry-client": "^0.1.0-develop.10",
"@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.18", "@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.18",
"@lumeweb/kernel-s5-client": "^0.1.0-develop.5",
"@lumeweb/kernel-swarm-client": "^0.1.0-develop.12", "@lumeweb/kernel-swarm-client": "^0.1.0-develop.12",
"@lumeweb/libresolver": "^0.1.0-develop.1", "@lumeweb/libresolver": "^0.1.0-develop.1",
"@lumeweb/sdk": "^0.1.0-develop.44", "@lumeweb/sdk": "^0.1.0-develop.44",
@ -2504,6 +2505,14 @@
"hyperswarm": "^4.3.7" "hyperswarm": "^4.3.7"
} }
}, },
"node_modules/@lumeweb/kernel-s5-client": {
"version": "0.1.0-develop.5",
"resolved": "https://registry.npmjs.org/@lumeweb/kernel-s5-client/-/kernel-s5-client-0.1.0-develop.5.tgz",
"integrity": "sha512-3yjq8F6JiLaY8ymtaVm/9uR0AtO9IHl/fOUmJyT2XCT+kbz0LJYZgMv0UMddnz/RHXiRMKRbIXxHhz2aEOw8kg==",
"dependencies": {
"@lumeweb/libkernel": "0.1.0-develop.68"
}
},
"node_modules/@lumeweb/kernel-swarm-client": { "node_modules/@lumeweb/kernel-swarm-client": {
"version": "0.1.0-develop.12", "version": "0.1.0-develop.12",
"resolved": "https://registry.npmjs.org/@lumeweb/kernel-swarm-client/-/kernel-swarm-client-0.1.0-develop.12.tgz", "resolved": "https://registry.npmjs.org/@lumeweb/kernel-swarm-client/-/kernel-swarm-client-0.1.0-develop.12.tgz",

View File

@ -21,6 +21,7 @@
"@lumeweb/kernel-ipfs-client": "^0.1.0-develop.25", "@lumeweb/kernel-ipfs-client": "^0.1.0-develop.25",
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.10", "@lumeweb/kernel-network-registry-client": "^0.1.0-develop.10",
"@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.18", "@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.18",
"@lumeweb/kernel-s5-client": "^0.1.0-develop.5",
"@lumeweb/kernel-swarm-client": "^0.1.0-develop.12", "@lumeweb/kernel-swarm-client": "^0.1.0-develop.12",
"@lumeweb/libresolver": "^0.1.0-develop.1", "@lumeweb/libresolver": "^0.1.0-develop.1",
"@lumeweb/sdk": "^0.1.0-develop.44", "@lumeweb/sdk": "^0.1.0-develop.44",

View File

@ -0,0 +1,79 @@
import type { ContentProvider } from "../types.js";
import * as nodePath from "path";
import { s5Client } from "@/clients.ts";
export default class IPFSProvider implements ContentProvider {
private _client = s5Client;
async fetchContent(
uri: string,
path: string,
query?: string,
): Promise<Response> {
let cid = translatePath(uri);
let err;
let urlPath = path;
const parsedPath = nodePath.parse(urlPath);
if (!cid.startsWith("/s5/") && cid.startsWith("/sia/")) {
err = "404";
}
if (err) {
throw new Error(err);
}
cid = cid.replace("/s5/", "").replace("/sia/", "");
let file;
try {
const meta = await this._client.stat(cid);
if (meta.type !== "web_app") {
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 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 = {};
headers["Content-Type"] = file.contentType as string;
return new Response(
new Blob([(await this._client.cat(cid)) as Uint8Array]),
{
headers,
},
);
}
supports(uri: string): boolean {
uri = translatePath(uri);
return uri.startsWith("/s5/") || uri.startsWith("/sia/");
}
}
function translatePath(uri: string) {
return uri.replace(/:\/\//, "/").replace(/^/, "/");
}

View File

@ -5,6 +5,7 @@ import { createClient as createPeerDiscoveryClient } from "@lumeweb/kernel-peer-
import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client"; import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client";
import { createClient as createHandshakeClient } from "@lumeweb/kernel-handshake-client"; import { createClient as createHandshakeClient } from "@lumeweb/kernel-handshake-client";
import { createClient as createEthClient } from "@lumeweb/kernel-eth-client"; import { createClient as createEthClient } from "@lumeweb/kernel-eth-client";
import { createClient as createS5Client } from "@lumeweb/kernel-s5-client";
const dnsClient = createDnsClient(); const dnsClient = createDnsClient();
const ipfsClient = createIpfsClient(); const ipfsClient = createIpfsClient();
@ -13,6 +14,7 @@ const peerDiscoveryClient = createPeerDiscoveryClient();
const networkRegistryClient = createNetworkRegistryClient(); const networkRegistryClient = createNetworkRegistryClient();
const handshakeClient = createHandshakeClient(); const handshakeClient = createHandshakeClient();
const ethClient = createEthClient(); const ethClient = createEthClient();
const s5Client = createS5Client();
export { export {
dnsClient, dnsClient,
@ -22,4 +24,5 @@ export {
networkRegistryClient, networkRegistryClient,
handshakeClient, handshakeClient,
ethClient, ethClient,
s5Client,
}; };