*Add initial skynet provider

This commit is contained in:
Derrick Hammer 2022-07-31 00:58:39 -04:00
parent 67285ccc08
commit 236f64bc43
1 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,91 @@
import BaseProvider from "./baseProvider.js";
import {
BlockingResponse,
HttpHeaders,
OnBeforeRequestDetailsType,
OnHeadersReceivedDetailsType,
OnRequestDetailsType,
} from "../types.js";
import { validSkylink } from "libskynet";
import { downloadSkylink, requestProxies } from "../util.js";
import browser from "@lumeweb/webextension-polyfill";
export default class SkynetProvider extends BaseProvider {
async shouldHandleRequest(
details: OnBeforeRequestDetailsType
): Promise<boolean> {
const dns = await this.resolveDns(details);
if (dns && validSkylink(dns)) {
return true;
}
return false;
}
async handleProxy(details: OnRequestDetailsType): Promise<any> {
return requestProxies;
}
async handleRequest(
details: OnBeforeRequestDetailsType
): Promise<BlockingResponse | boolean> {
const dns = await this.resolveDns(details);
let path = new URL(details.url).pathname;
let fileData: any, err;
try {
[fileData, err] = await downloadSkylink(dns, path);
} catch (e: any) {
debugger;
this.setData(details, "error", (e as Error).message);
return {};
}
if (err) {
this.setData(details, "error", err);
return {};
}
this.setData(details, "headers", fileData.response?.headers);
const filter = browser.webRequest.filterResponseData(details.requestId);
filter.ondata = () => {};
filter.onstop = () => {
fileData.response.arrayBuffer().then((data: any) => {
filter.write(data);
filter.close();
});
};
return true;
}
async handleHeaders(
details: OnHeadersReceivedDetailsType
): Promise<BlockingResponse | boolean> {
const err = this.getData(details, "error");
let headers: Headers = this.getData(details, "headers") as Headers;
if (err) {
return {
responseHeaders: [
{
name: "Status-Code",
value: err == "404" ? "404" : "400",
},
{
name: "Content-Type",
value: "text/html; charset=utf8",
},
],
};
}
return {
responseHeaders: Array.from(headers).map((item: string[]) => {
return { name: item[0], value: item[1] };
}),
};
}
}