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": { "dependencies": {
"@helia/unixfs": "^1.4.0", "@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-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/libkernel": "^0.1.0-develop.16",
"@lumeweb/libresolver": "^0.1.0-develop.1", "@lumeweb/libresolver": "^0.1.0-develop.1",
"@lumeweb/libweb": "^0.2.0-develop.22", "@lumeweb/libweb": "^0.2.0-develop.22",
"@lumeweb/tld-enum": "^0.1.0-develop.1", "@lumeweb/tld-enum": "^0.1.0-develop.1",
"@peculiar/webcrypto": "^1.4.3",
"@scure/bip39": "^1.2.1", "@scure/bip39": "^1.2.1",
"ejs": "^3.1.9",
"file-type": "^18.5.0", "file-type": "^18.5.0",
"is-ipfs": "^8.0.1", "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, OnHeadersReceivedDetailsType,
OnRequestDetailsType, OnRequestDetailsType,
} from "../types.js"; } from "../types.js";
import browser from "@lumeweb/webextension-polyfill"; import browser from "webextension-polyfill";
import { RequestOverrideResponse } from "libskynet"; import { RequestOverrideResponse } from "@lumeweb/libweb";
import { queryKernel } from "../main/background.js"; import { queryKernel } from "../main/background.js";
import { requestProxies } from "../util.js"; import { requestProxies } from "../util.js";
export default class InternalProvider extends BaseProvider { export default class InternalProvider extends BaseProvider {
async shouldHandleRequest( async shouldHandleRequest(
details: OnBeforeRequestDetailsType details: OnBeforeRequestDetailsType,
): Promise<boolean> { ): Promise<boolean> {
return [ return [
"http://kernel.lume/", "http://kernel.lume/",
@ -22,7 +22,7 @@ export default class InternalProvider extends BaseProvider {
} }
async handleRequest( async handleRequest(
details: OnBeforeRequestDetailsType details: OnBeforeRequestDetailsType,
): Promise<BlockingResponse | boolean> { ): Promise<BlockingResponse | boolean> {
// For the kernel, we swallow the entire page. The 'bootloader' content // For the kernel, we swallow the entire page. The 'bootloader' content
// script will everything that we need. // script will everything that we need.
@ -102,7 +102,7 @@ export default class InternalProvider extends BaseProvider {
} }
async handleHeaders( async handleHeaders(
details: OnHeadersReceivedDetailsType details: OnHeadersReceivedDetailsType,
): Promise<OnRequestDetailsType | boolean> { ): Promise<OnRequestDetailsType | boolean> {
if ( if (
details.url === "http://kernel.lume/" || 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 WebEngine from "./webEngine.js";
import type { Menus, Tabs } from "./types.js"; import type { Menus, Tabs } from "./types.js";
import IpfsProvider from "./contentProviders/ipfsProvider.js"; import IpfsProvider from "./contentProviders/ipfsProvider.js";
@ -17,7 +17,7 @@ export default function setup(engine: WebEngine) {
return; return;
} }
const provider = engine.getDomainContentProvider( const provider = engine.getDomainContentProvider(
new URL(details.pageUrl as string).hostname new URL(details.pageUrl as string).hostname,
); );
browser.menus.update("clear-cache", { browser.menus.update("clear-cache", {
visible: provider instanceof IpfsProvider, visible: provider instanceof IpfsProvider,

View File

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

View File

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

View File

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

View File

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