refactor: update imports and add missing dependencies

This commit is contained in:
Derrick Hammer 2023-07-17 11:39:16 -04:00
parent 188e9e4155
commit 1c8a640c48
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
9 changed files with 59 additions and 58 deletions

View File

@ -20,14 +20,21 @@
},
"dependencies": {
"@helia/unixfs": "^1.4.0",
"@lumeweb/kernel-dns-client": "^0.1.0-develop.2",
"@lumeweb/kernel-ipfs-client": "^0.0.2-develop.1",
"@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.10",
"@lumeweb/kernel-swarm-client": "^0.0.2-develop.9",
"@lumeweb/libkernel": "^0.1.0-develop.16",
"@lumeweb/libresolver": "^0.1.0-develop.1",
"@lumeweb/libweb": "^0.2.0-develop.22",
"@lumeweb/tld-enum": "^0.1.0-develop.1",
"@peculiar/webcrypto": "^1.4.3",
"@scure/bip39": "^1.2.1",
"ejs": "^3.1.9",
"file-type": "^18.5.0",
"is-ipfs": "^8.0.1",
"p-defer": "^4.0.0"
"node-cache": "^5.1.2",
"p-defer": "^4.0.0",
"webextension-polyfill": "^0.10.0"
}
}

View File

@ -5,14 +5,14 @@ import {
OnHeadersReceivedDetailsType,
OnRequestDetailsType,
} from "../types.js";
import browser from "@lumeweb/webextension-polyfill";
import { RequestOverrideResponse } from "libskynet";
import browser from "webextension-polyfill";
import { RequestOverrideResponse } from "@lumeweb/libweb";
import { queryKernel } from "../main/background.js";
import { requestProxies } from "../util.js";
export default class InternalProvider extends BaseProvider {
async shouldHandleRequest(
details: OnBeforeRequestDetailsType
details: OnBeforeRequestDetailsType,
): Promise<boolean> {
return [
"http://kernel.lume/",
@ -22,7 +22,7 @@ export default class InternalProvider extends BaseProvider {
}
async handleRequest(
details: OnBeforeRequestDetailsType
details: OnBeforeRequestDetailsType,
): Promise<BlockingResponse | boolean> {
// For the kernel, we swallow the entire page. The 'bootloader' content
// script will everything that we need.
@ -102,7 +102,7 @@ export default class InternalProvider extends BaseProvider {
}
async handleHeaders(
details: OnHeadersReceivedDetailsType
details: OnHeadersReceivedDetailsType,
): Promise<OnRequestDetailsType | boolean> {
if (
details.url === "http://kernel.lume/" ||

View File

@ -1,4 +1,4 @@
import browser from "@lumeweb/webextension-polyfill";
import browser from "webextension-polyfill";
import type WebEngine from "./webEngine.js";
import type { Menus, Tabs } from "./types.js";
import IpfsProvider from "./contentProviders/ipfsProvider.js";
@ -17,7 +17,7 @@ export default function setup(engine: WebEngine) {
return;
}
const provider = engine.getDomainContentProvider(
new URL(details.pageUrl as string).hostname
new URL(details.pageUrl as string).hostname,
);
browser.menus.update("clear-cache", {
visible: provider instanceof IpfsProvider,

View File

@ -4,20 +4,19 @@ import {
DNSResult,
ResolverOptions,
} from "@lumeweb/libresolver";
import { blake2b, bufToHex } from "libskynet/dist";
import { bufToHex } from "@lumeweb/libweb";
import { getDnsSetupDefer } from "./main/vars.js";
import { dnsClient } from "./clients.js";
import { blake3 } from "@noble/hashes/blake3";
const cache = new NodeCache({ stdTTL: 60 });
export async function resolve(
domain: string,
options?: ResolverOptions,
bypassCache = false
bypassCache = false,
): Promise<DNSResult | Error> {
let cacheId = `${domain}:${bufToHex(
blake2b(new TextEncoder().encode(JSON.stringify(options)))
)}`;
let cacheId = `${domain}:${bufToHex(blake3(JSON.stringify(options)))}`;
if (cache.has(cacheId)) {
cache.ttl(cacheId);
@ -43,7 +42,7 @@ export async function resolve(
export async function scanRecords(
domain: string,
recordTypes?: string[],
bypassCache = false
bypassCache = false,
): Promise<boolean | DNSResult> {
let dnsResult: boolean | DNSResult = false;

View File

@ -32,7 +32,7 @@ import {
weAreBooted,
} from "./vars.js";
// @ts-ignore
import browser from "@lumeweb/webextension-polyfill";
import browser from "webextension-polyfill";
function logLargeObjects() {
let queriesLen = Object.keys(getQueries()).length;

View File

@ -4,7 +4,7 @@ import {
Err,
hexToBuf,
objAsString,
} from "@siaweb/libweb";
} from "@lumeweb/libweb";
declare var browser: any; // tsc
@ -34,7 +34,7 @@ function bootloaderWLog(isErr: boolean, ...inputs: any) {
message,
},
},
"*"
"*",
);
}
function log(...inputs: any) {
@ -59,7 +59,7 @@ var handleIncomingMessage = function (event: MessageEvent) {
method: "response",
err: "message sent to kernel with no nonce",
},
event.origin
event.origin,
);
return;
}
@ -71,7 +71,7 @@ var handleIncomingMessage = function (event: MessageEvent) {
method: "response",
err: "message sent to kernel with no method",
},
event.origin
event.origin,
);
return;
}
@ -88,7 +88,7 @@ var handleIncomingMessage = function (event: MessageEvent) {
err:
"unrecognized method (user may need to log in): " + event.data.method,
},
event.origin
event.origin,
);
return;
};
@ -123,7 +123,7 @@ let blockForAuthPage: Promise<void> = new Promise((resolve) => {
});
} catch (err: any) {
kernelAuthPage = new TextEncoder().encode(
addContextToErr(err, "unable to load the kernel auth page")
addContextToErr(err, "unable to load the kernel auth page"),
);
resolve();
}
@ -152,7 +152,7 @@ function handleSkynetKernelRequestOverride(event: MessageEvent) {
body,
},
},
event.origin
event.origin,
);
};
@ -191,7 +191,7 @@ function handleSkynetKernelRequestOverride(event: MessageEvent) {
override: false,
},
},
event.origin
event.origin,
);
}
@ -237,7 +237,7 @@ var handleStorage = function (event: StorageEvent) {
window.addEventListener("storage", (event) => handleStorage(event));
function downloadKernel(
kernelSkylink: string
kernelSkylink: string,
): Promise<[kernelCode: string, err: Err]> {
return new Promise((resolve) => {
fetch(`https://web3portal.com/${kernelSkylink}`).then((result) => {
@ -317,7 +317,7 @@ function sendAuthUpdate() {
logoutComplete: logoutComplete,
},
},
"*"
"*",
);
}
sendAuthUpdate();

View File

@ -1,5 +1,5 @@
import { OnBeforeRequestDetailsType, StreamFilter } from "./types.js";
import browser from "@lumeweb/webextension-polyfill";
import browser from "webextension-polyfill";
import { iterateStream, streamToArray } from "./util.js";
export default class RequestStream {
@ -10,7 +10,7 @@ export default class RequestStream {
constructor(
request: OnBeforeRequestDetailsType,
contentFilter?: (data: Uint8Array) => Promise<Uint8Array>
contentFilter?: (data: Uint8Array) => Promise<Uint8Array>,
) {
this._request = request;
this._contentFilter = contentFilter;
@ -36,7 +36,7 @@ export default class RequestStream {
this._filter.onstop = async () => {
if (this._contentFilter) {
const data = await this._contentFilter(
await streamToArray(this._readableStream)
await streamToArray(this._readableStream),
);
this._filter.write(data);
this._filter.close();

View File

@ -1,17 +1,12 @@
import { defaultPortalList } from "libskynet";
defaultPortalList.unshift("https://web3portal.com");
defaultPortalList.pop();
export function isIp(ip: string) {
return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
ip
ip,
);
}
export function isDomain(domain: string) {
return /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/.test(
domain
domain,
);
}
@ -44,7 +39,7 @@ export function getTld(hostname: string): string {
: hostname;
}
export async function* iterateStream(
stream: ReadableStream<any>
stream: ReadableStream<any>,
): AsyncGenerator<Uint8Array> {
let chunk;
const reader = stream.getReader();
@ -59,7 +54,7 @@ export async function* iterateStream(
}
export async function streamToArray(
stream: ReadableStream<Uint8Array>
stream: ReadableStream<Uint8Array>,
): Promise<Uint8Array> {
let buffer = new Uint8Array();

View File

@ -1,4 +1,4 @@
import browser from "@lumeweb/webextension-polyfill";
import browser from "webextension-polyfill";
import BaseProvider from "./contentProviders/baseProvider.js";
import {
BlockingResponse,
@ -9,13 +9,14 @@ import {
OnErrorOccurredDetailsType,
OnHeadersReceivedDetailsType,
OnRequestDetailsType,
} from "./types";
} from "./types.js";
import { getTld, isDomain, isIp, normalizeDomain } from "./util.js";
import tldEnum from "@lumeweb/tld-enum";
import { scanRecords } from "./dns.js";
import { blake2b, bufToHex } from "libskynet";
import { bufToHex } from "@lumeweb/libweb";
import { getAuthStatus } from "./main/vars.js";
import { DNSResult } from "@lumeweb/libresolver";
import { blake3 } from "@noble/hashes/blake3";
import "./contentFilters/index.js";
@ -30,7 +31,7 @@ export default class WebEngine {
browser.webRequest.onHeadersReceived.addListener(
this.headerHandler.bind(this),
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
["blocking", "responseHeaders"],
);
browser.proxy.onRequest.addListener(this.proxyHandler.bind(this), {
urls: ["<all_urls>"],
@ -38,34 +39,34 @@ export default class WebEngine {
browser.webRequest.onBeforeRequest.addListener(
this.requestHandler.bind(this),
{ urls: ["<all_urls>"] },
["blocking"]
["blocking"],
);
browser.webRequest.onBeforeSendHeaders.addListener(
this.reqHeaderHandler.bind(this),
{ urls: ["<all_urls>"] },
["requestHeaders", "blocking"]
["requestHeaders", "blocking"],
);
browser.webRequest.onCompleted.addListener(
this.onCompletedHandler.bind(this),
{
urls: ["<all_urls>"],
}
},
);
browser.webRequest.onErrorOccurred.addListener(
this.onErrorHandler.bind(this),
{
urls: ["<all_urls>"],
}
},
);
browser.webNavigation.onBeforeNavigate.addListener(
this.handleNavigationRequest.bind(this)
this.handleNavigationRequest.bind(this),
);
}
private async headerHandler(
details: OnHeadersReceivedDetailsType
details: OnHeadersReceivedDetailsType,
): Promise<BlockingResponse> {
return this.processHandler(details, "handleHeaders", {
responseHeaders: details.responseHeaders,
@ -73,7 +74,7 @@ export default class WebEngine {
}
private async proxyHandler(details: OnRequestDetailsType): Promise<any> {
let handle = null;
let handle: BaseProvider | null = null;
for (const provider of this.contentProviders) {
if (await provider.shouldHandleRequest(details)) {
handle = provider;
@ -92,7 +93,7 @@ export default class WebEngine {
}
private async requestHandler(
details: OnBeforeRequestDetailsType
details: OnBeforeRequestDetailsType,
): Promise<BlockingResponse> {
const navId = this.getNavigationId(details);
let navRedirect: boolean | string = false;
@ -136,12 +137,13 @@ export default class WebEngine {
}
private async reqHeaderHandler(
details: OnBeforeSendHeadersDetailsType
details: OnBeforeSendHeadersDetailsType,
): Promise<BlockingResponse> {
return this.processHandler(details, "handleReqHeaders");
}
private async onCompletedHandler(
details: OnCompletedDetailsType
details: OnCompletedDetailsType,
): Promise<void> {
if (this.requests.has(details.requestId)) {
this.requests.delete(details.requestId);
@ -153,7 +155,7 @@ export default class WebEngine {
}
private async onErrorHandler(
details: OnErrorOccurredDetailsType
details: OnErrorOccurredDetailsType,
): Promise<void> {
if (this.requests.has(details.requestId)) {
this.requests.delete(details.requestId);
@ -197,7 +199,7 @@ export default class WebEngine {
private async processHandler(
details: any,
method: string,
def = {}
def = {},
): Promise<BlockingResponse> {
const provider = this.getRequestProvider(details.requestId);
@ -252,7 +254,7 @@ export default class WebEngine {
} catch {}
if (tldEnum.list.includes(getTld(queriedHost))) {
return false;
return;
}
if (isIp(queriedHost)) {
@ -306,9 +308,7 @@ export default class WebEngine {
}
private getNavigationId(details: any) {
return `${details.tabId}_${bufToHex(
blake2b(new TextEncoder().encode(details.url))
)}`;
return `${details.tabId}_${bufToHex(blake3(details.url))}`;
}
public getDomainContentProvider(domain: string): BaseProvider | null {
@ -316,7 +316,7 @@ export default class WebEngine {
}
private getRequestProvider(
requestId: string
requestId: string,
): { [p: string]: Function } | null {
const provider = this.requests.get(requestId) as unknown as {
[index: string]: Function;