From 1c8a640c48ec25ece28ac27f526808638bb9c7ea Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 17 Jul 2023 11:39:16 -0400 Subject: [PATCH] refactor: update imports and add missing dependencies --- package.json | 9 ++++- src/contentProviders/internalProvider.ts | 10 +++--- src/contextMenu.ts | 4 +-- src/dns.ts | 11 +++---- src/main/background.ts | 2 +- src/main/bootloader.ts | 20 +++++------ src/requestStream.ts | 6 ++-- src/util.ts | 13 +++----- src/webEngine.ts | 42 ++++++++++++------------ 9 files changed, 59 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 6581c2a..e0d5d17 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/contentProviders/internalProvider.ts b/src/contentProviders/internalProvider.ts index fd36025..288aa97 100644 --- a/src/contentProviders/internalProvider.ts +++ b/src/contentProviders/internalProvider.ts @@ -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 { return [ "http://kernel.lume/", @@ -22,7 +22,7 @@ export default class InternalProvider extends BaseProvider { } async handleRequest( - details: OnBeforeRequestDetailsType + details: OnBeforeRequestDetailsType, ): Promise { // 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 { if ( details.url === "http://kernel.lume/" || diff --git a/src/contextMenu.ts b/src/contextMenu.ts index 529c42a..4491748 100644 --- a/src/contextMenu.ts +++ b/src/contextMenu.ts @@ -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, diff --git a/src/dns.ts b/src/dns.ts index 53543dc..3cde4db 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -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 { - 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 { let dnsResult: boolean | DNSResult = false; diff --git a/src/main/background.ts b/src/main/background.ts index a61cac9..927082a 100644 --- a/src/main/background.ts +++ b/src/main/background.ts @@ -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; diff --git a/src/main/bootloader.ts b/src/main/bootloader.ts index 9322304..84fc90b 100644 --- a/src/main/bootloader.ts +++ b/src/main/bootloader.ts @@ -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 = 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(); diff --git a/src/requestStream.ts b/src/requestStream.ts index ba2ae78..69a2552 100644 --- a/src/requestStream.ts +++ b/src/requestStream.ts @@ -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 + contentFilter?: (data: Uint8Array) => Promise, ) { 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(); diff --git a/src/util.ts b/src/util.ts index 4401272..aaeef86 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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 + stream: ReadableStream, ): AsyncGenerator { let chunk; const reader = stream.getReader(); @@ -59,7 +54,7 @@ export async function* iterateStream( } export async function streamToArray( - stream: ReadableStream + stream: ReadableStream, ): Promise { let buffer = new Uint8Array(); diff --git a/src/webEngine.ts b/src/webEngine.ts index 3afc932..f3693c4 100644 --- a/src/webEngine.ts +++ b/src/webEngine.ts @@ -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: [""] }, - ["blocking", "responseHeaders"] + ["blocking", "responseHeaders"], ); browser.proxy.onRequest.addListener(this.proxyHandler.bind(this), { urls: [""], @@ -38,34 +39,34 @@ export default class WebEngine { browser.webRequest.onBeforeRequest.addListener( this.requestHandler.bind(this), { urls: [""] }, - ["blocking"] + ["blocking"], ); browser.webRequest.onBeforeSendHeaders.addListener( this.reqHeaderHandler.bind(this), { urls: [""] }, - ["requestHeaders", "blocking"] + ["requestHeaders", "blocking"], ); browser.webRequest.onCompleted.addListener( this.onCompletedHandler.bind(this), { urls: [""], - } + }, ); browser.webRequest.onErrorOccurred.addListener( this.onErrorHandler.bind(this), { urls: [""], - } + }, ); browser.webNavigation.onBeforeNavigate.addListener( - this.handleNavigationRequest.bind(this) + this.handleNavigationRequest.bind(this), ); } private async headerHandler( - details: OnHeadersReceivedDetailsType + details: OnHeadersReceivedDetailsType, ): Promise { return this.processHandler(details, "handleHeaders", { responseHeaders: details.responseHeaders, @@ -73,7 +74,7 @@ export default class WebEngine { } private async proxyHandler(details: OnRequestDetailsType): Promise { - 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 { 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 { return this.processHandler(details, "handleReqHeaders"); } + private async onCompletedHandler( - details: OnCompletedDetailsType + details: OnCompletedDetailsType, ): Promise { 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 { 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 { 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;