From 774aab1a21f24d8f536fa590f556a881c2a19a3b Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 6 Jul 2023 01:14:10 -0400 Subject: [PATCH] refactor: move to new sdks and build system --- .presetterrc.json | 9 ++ build.js | 14 -- package.json | 128 ++++-------------- patches/@libp2p+tcp+7.0.3.patch | 44 +++++++ patches/@libp2p__tcp@6.2.1.patch | 32 ----- patches/b4a@1.6.3.patch | 36 ----- polyfill.js | 13 -- rollup.config.ts | 76 +++++++++++ src-build/build.ts | 218 ------------------------------- src/index.ts | 53 ++++---- src/libp2p/transport.ts | 8 +- tsconfig.build.json | 14 -- tsconfig.json | 13 -- 13 files changed, 189 insertions(+), 469 deletions(-) create mode 100644 .presetterrc.json delete mode 100644 build.js create mode 100644 patches/@libp2p+tcp+7.0.3.patch delete mode 100644 patches/@libp2p__tcp@6.2.1.patch delete mode 100644 patches/b4a@1.6.3.patch delete mode 100644 polyfill.js create mode 100644 rollup.config.ts delete mode 100644 src-build/build.ts delete mode 100644 tsconfig.build.json delete mode 100644 tsconfig.json diff --git a/.presetterrc.json b/.presetterrc.json new file mode 100644 index 0000000..0eef0e5 --- /dev/null +++ b/.presetterrc.json @@ -0,0 +1,9 @@ +{ + "preset": [ + "@lumeweb/presetter-kernel-module-preset" + ], + "config": { + "official": true, + "browser": true + } +} diff --git a/build.js b/build.js deleted file mode 100644 index fe3e005..0000000 --- a/build.js +++ /dev/null @@ -1,14 +0,0 @@ -import esbuild from "esbuild"; - -esbuild.buildSync({ - entryPoints: ["src/index.ts"], - outfile: "dist/index.js", - format: "iife", - bundle: true, - legalComments: "external", - // minify: true - define: { - global: "self", - }, - inject: ["polyfill.js"], -}); diff --git a/package.json b/package.json index ac0adea..cf00e73 100644 --- a/package.json +++ b/package.json @@ -2,115 +2,43 @@ "name": "@lumeweb/kernel-ipfs", "version": "0.1.0", "type": "module", + "readme": "ERROR: No README data found!", + "_id": "@lumeweb/kernel-ipfs@0.1.0", "scripts": { - "test": "jest", - "build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs", - "compile": "npm run build-script && node build.js", - "build": "npm run compile && node ./dist-build/build.mjs dev" + "prepare": "presetter bootstrap", + "build": "shx echo 'export default undefined;' > nop.js; run build", + "semantic-release": "semantic-release", + "postinstall": "patch-package" }, "dependencies": { - "@chainsafe/libp2p-gossipsub": "^6.3.0", - "@chainsafe/libp2p-noise": "^11.0.4", - "@chainsafe/libp2p-yamux": "^3.0.10", - "@dao-xyz/libp2p-noise": "^11.1.3", - "@helia/ipns": "^1.1.0", - "@helia/unixfs": "^1.2.2", - "@libp2p/bootstrap": "^6.0.3", - "@libp2p/crypto": "^1.0.15", - "@libp2p/delegated-content-routing": "^4.0.3", - "@libp2p/delegated-peer-routing": "^4.0.5", - "@libp2p/interface-metrics": "^4.0.6", - "@libp2p/interfaces": "^3.3.1", - "@libp2p/logger": "^2.0.7", - "@libp2p/mplex": "^7.1.7", - "@libp2p/tcp": "^6.2.1", - "@libp2p/utils": "^3.0.8", - "@lumeweb/kernel-protomux-client": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git", - "@lumeweb/kernel-swarm-client": "git+https://git.lumeweb.com/LumeWeb/kernel-swarm-client.git", - "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", - "@multiformats/mafmt": "^11.1.2", - "@peculiar/webcrypto": "git+https://git.lumeweb.com/LumeWeb/webcrypto.git", - "b4a": "^1.6.3", - "blockstore-idb": "^1.1.0", - "buffer": "^6.0.3", - "compact-encoding": "^2.11.0", - "datastore-core": "^8.0.4", - "datastore-idb": "^2.1.1", - "helia": "^0.0.1", - "ipfs-http-client": "^60.0.0", - "ipfs-in-memory-repo": "^0.1.0", - "libkmodule": "^0.2.53", + "@chainsafe/libp2p-yamux": "^4.0.2", + "@helia/interface": "^1.2.1", + "@helia/ipns": "^1.1.3", + "@helia/unixfs": "^1.4.0", + "@libp2p/bootstrap": "^8.0.0", + "@libp2p/delegated-content-routing": "^4.0.6", + "@libp2p/delegated-peer-routing": "^4.0.9", + "@libp2p/peer-id": "^2.0.4", + "@lumeweb/kernel-swarm-client": "^0.0.2-develop.6", + "@lumeweb/libhyperproxy": "^0.0.2-develop.1", + "@lumeweb/libkernel": "0.1.0-develop.14", + "@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.30", + "blockstore-idb": "^1.1.1", + "datastore-idb": "^2.1.2", + "helia": "^1.3.5", + "ipfs-core": "^0.18.1", + "ipfs-http-client": "^60.0.1", "libp2p": "^0.42.2", - "multiformats": "^11.0.2", + "multiformats": "^12.0.1", "p-defer": "^4.0.0", - "p-queue": "^7.3.4", - "private-ip": "^3.0.0", - "rewire": "^6.0.0", + "path-browserify": "^1.0.1", "runes2": "^1.1.2", - "serialize-error": "^11.0.0", - "sodium-universal": "^4.0.0", - "streamx": "^2.13.2", "timers-browserify": "^2.0.12" }, "devDependencies": { - "@helia/interface": "^0.0.0", - "@libp2p/interface-connection": "^3.1.1", - "@libp2p/interface-peer-info": "^1.0.9", - "@libp2p/interface-transport": "^2.1.3", - "@libp2p/kad-dht": "^7.0.3", - "@libp2p/peer-id": "^2.0.3", - "@libp2p/peer-id-factory": "^2.0.3", - "@libp2p/websockets": "^5.0.10", - "@lumeweb/libhyperproxy": "git+https://git.lumeweb.com/LumeWeb/libhyperproxy.git", - "@multiformats/multiaddr": "^11.6.1", - "@scure/bip39": "^1.2.0", - "@skynetlabs/skynet-nodejs": "^2.9.0", - "@types/b4a": "^1.6.0", - "@types/events": "^3.0.0", - "@types/node": "^18.15.11", - "@types/read": "^0.0.29", - "@types/rewire": "^2.5.28", - "@types/streamx": "^2.9.1", - "@types/ws": "^8.5.4", - "cli-progress": "^3.12.0", - "crypto-browserify": "^3.12.0", - "esbuild": "^0.14.54", - "http-browserify": "^1.7.0", - "hyperswarm": "^4.4.0", - "interface-store": "^3.0.4", - "it-all": "^2.0.1", - "it-ws": "^5.0.6", - "libskynet": "^0.1.9", - "libskynetnode": "^0.1.4", - "net-browserify": "^0.2.4", + "@rollup/plugin-alias": "^5.0.0", "os-browserify": "^0.3.0", - "p-timeout": "^6.1.1", - "path-browserify": "^1.0.1", - "prettier": "^2.8.7", - "prom-client": "^14.2.0", - "read": "^1.0.7", - "stream-browserify": "^3.0.0", - "tslib": "^2.5.0", - "typescript": "^4.9.5", - "url-browser": "^0.1.7", - "util-browser": "^0.0.2" - }, - "browser": { - "libkernel": false, - "timers": "timers-browserify", - "path": "path-browserify", - "os": "os-browserify", - "net": "./src/net.ts", - "crypto": "crypto-browserify", - "stream": "stream-browserify" - }, - "pnpm": { - "overrides": { - "protomux": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git" - }, - "patchedDependencies": { - "@libp2p/tcp@6.2.1": "patches/@libp2p__tcp@6.2.1.patch", - "b4a@1.6.3": "patches/b4a@1.6.3.patch" - } + "patch-package": "^7.0.0", + "rollup-plugin-ignore-import": "^1.3.2" } } diff --git a/patches/@libp2p+tcp+7.0.3.patch b/patches/@libp2p+tcp+7.0.3.patch new file mode 100644 index 0000000..e2f9107 --- /dev/null +++ b/patches/@libp2p+tcp+7.0.3.patch @@ -0,0 +1,44 @@ +diff --git a/node_modules/@libp2p/tcp/dist/src/index.js b/node_modules/@libp2p/tcp/dist/src/index.js +index fe47701..d239a6a 100644 +--- a/node_modules/@libp2p/tcp/dist/src/index.js ++++ b/node_modules/@libp2p/tcp/dist/src/index.js +@@ -8,7 +8,7 @@ import { TCPListener } from './listener.js'; + import { toMultiaddrConnection } from './socket-to-conn.js'; + import { multiaddrToNetConfig } from './utils.js'; + const log = logger('libp2p:tcp'); +-class TCP { ++export class TCP { + opts; + metrics; + components; +diff --git a/node_modules/@libp2p/tcp/package.json b/node_modules/@libp2p/tcp/package.json +index e4524a9..74fa657 100644 +--- a/node_modules/@libp2p/tcp/package.json ++++ b/node_modules/@libp2p/tcp/package.json +@@ -32,12 +32,20 @@ + "!dist/test", + "!**/*.tsbuildinfo" + ], +- "exports": { +- ".": { +- "types": "./dist/src/index.d.ts", +- "import": "./dist/src/index.js" +- } +- }, ++ "exports": { ++ ".": { ++ "types": "./dist/src/index.d.ts", ++ "import": "./dist/src/index.js" ++ }, ++ "./socket-to-conn": { ++ "types": "./dist/src/socket-to-conn.d.ts", ++ "import": "./dist/src/socket-to-conn.js" ++ }, ++ "./utils": { ++ "types": "./dist/src/utils.d.ts", ++ "import": "./dist/src/utils.js" ++ } ++ }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { diff --git a/patches/@libp2p__tcp@6.2.1.patch b/patches/@libp2p__tcp@6.2.1.patch deleted file mode 100644 index d69702f..0000000 --- a/patches/@libp2p__tcp@6.2.1.patch +++ /dev/null @@ -1,32 +0,0 @@ -diff --git a/dist/src/index.js b/dist/src/index.js -index dab2ad35e64981f00c59a48ccbcbe407eb7749e6..7179d4ad8137be05e5f080ca67ea433c9dfaeeb0 100644 ---- a/dist/src/index.js -+++ b/dist/src/index.js -@@ -8,7 +8,7 @@ import { AbortError, CodeError } from '@libp2p/interfaces/errors'; - import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'; - import { symbol } from '@libp2p/interface-transport'; - const log = logger('libp2p:tcp'); --class TCP { -+export class TCP { - constructor(components, options = {}) { - this.opts = options; - this.components = components; -diff --git a/package.json b/package.json -index 2e38ab0bdeef25a898770aedd0459e8c0cd79a9b..9ec4c16e10347e5f9717998725387b30d5b59907 100644 ---- a/package.json -+++ b/package.json -@@ -36,6 +36,14 @@ - ".": { - "types": "./dist/src/index.d.ts", - "import": "./dist/src/index.js" -+ }, -+ "./socket-to-conn": { -+ "types": "./dist/src/socket-to-conn.d.ts", -+ "import": "./dist/src/socket-to-conn.js" -+ }, -+ "./utils": { -+ "types": "./dist/src/utils.d.ts", -+ "import": "./dist/src/utils.js" - } - }, - "eslintConfig": { \ No newline at end of file diff --git a/patches/b4a@1.6.3.patch b/patches/b4a@1.6.3.patch deleted file mode 100644 index 13e4b09..0000000 --- a/patches/b4a@1.6.3.patch +++ /dev/null @@ -1,36 +0,0 @@ -diff --git a/browser.js b/browser.js -index e07f78d17b7b4a2963e4f0062047cc96e6025f9e..a9f266a2644f3f8ebd0f6684b3e11a7519059851 100644 ---- a/browser.js -+++ b/browser.js -@@ -3,6 +3,7 @@ const base64 = require('./lib/base64') - const hex = require('./lib/hex') - const utf8 = require('./lib/utf8') - const utf16le = require('./lib/utf16le') -+const Buffer = require('buffer').Buffer - - const LE = new Uint8Array(Uint16Array.of(0xff).buffer)[0] === 0xff - -diff --git a/index.js b/index.js -index a751d6b4d27c7dd903efbfdc87a98f61044a62b8..8bd32640afdcfbc475168d458c51f8b9162d1656 100644 ---- a/index.js -+++ b/index.js -@@ -1,3 +1,5 @@ -+const Buffer = require('buffer').Buffer -+ - function isBuffer (value) { - return Buffer.isBuffer(value) || value instanceof Uint8Array - } -diff --git a/package.json b/package.json -index eb0f792709b27e66f26658054781b4099507b5ed..69c4ff39c48b0bdd5f0bc7ebbade517e7c5d76e4 100644 ---- a/package.json -+++ b/package.json -@@ -8,9 +8,6 @@ - "index.js", - "lib" - ], -- "browser": { -- "./index.js": "./browser.js" -- }, - "scripts": { - "test": "standard && brittle test/*.mjs" - }, \ No newline at end of file diff --git a/polyfill.js b/polyfill.js deleted file mode 100644 index 892bd4d..0000000 --- a/polyfill.js +++ /dev/null @@ -1,13 +0,0 @@ -import { Crypto } from "@peculiar/webcrypto"; -import { Buffer } from "buffer"; - -let globalCrypto = self.crypto; -if (!globalCrypto.subtle) { - let subtleCrypto = new Crypto().subtle; - Object.defineProperty(globalCrypto, "subtle", { - get() { - return subtleCrypto; - }, - }); -} -export { Buffer }; diff --git a/rollup.config.ts b/rollup.config.ts new file mode 100644 index 0000000..343deb5 --- /dev/null +++ b/rollup.config.ts @@ -0,0 +1,76 @@ +// @ts-nocheck +import * as import0 from "@rollup/plugin-json"; +import * as import1 from "@rollup/plugin-node-resolve"; +import * as import2 from "@rollup/plugin-commonjs"; +import * as import3 from "@rollup/plugin-graphql"; +import * as import4 from "@rollup/plugin-image"; +import * as import5 from "@rollup/plugin-yaml"; +import * as import6 from "rollup-plugin-postcss"; +import * as import7 from "rollup-plugin-visualizer"; +import * as import8 from "@rollup/plugin-wasm"; +import * as import9 from "@rollup/plugin-alias"; +import * as import10 from "rollup-plugin-ignore-import"; +export default { + input: "build/index.js", + output: [ + { + file: "lib/index.js", + format: "cjs", + sourcemap: true, + inlineDynamicImports: true, + }, + ], + plugins: [ + import0.default(...([] as const)), + import10.default({ + include: [ + "**/multiSocket/tcpSocket.js", + "**/node-fetch/**", + "**/@libp2p/tcp/dist/src/listener.js", + ], + exclude: [], + }), + import10.default({ + include: ["**/@achingbrain/nat-port-mapper/**"], + exclude: [], + body: "export default {}; export const upnpNat = {};", + }), + import10.default({ + include: ["**/@libp2p/tcp/dist/src/listener.js"], + exclude: [], + body: "export default {}; export const TCPListener = {};", + }), + import9.default({ + entries: { + "node-fetch": "./nop.js", + "stream": "./nop.js", + "path": "path-browserify", + "timers": "timers-browserify", + "os": "os-browserify", + "net": "./nop.js", + }, + }), + import1.default( + ...([ + { + browser: true, + preferBuiltins: false, + dedupe: [ + "@lumeweb/libkernel", + "@lumeweb/libweb", + "@lumeweb/libportal", + ], + }, + ] as const), + ), + import2.default( + ...([{ extensions: [".js", ".jsx", ".ts", ".tsx"] }] as const), + ), + import3.default(...([] as const)), + import4.default(...([] as const)), + import5.default(...([] as const)), + import6.default(...([{ inject: { insertAt: "top" } }] as const)), + import7.visualizer(...([] as const)), + import8.default(...([{ targetEnv: "auto-inline" }] as const)), + ], +}; diff --git a/src-build/build.ts b/src-build/build.ts deleted file mode 100644 index c0833e3..0000000 --- a/src-build/build.ts +++ /dev/null @@ -1,218 +0,0 @@ -// This is the standard build script for a kernel module. - -import * as fs from "fs"; -import read from "read"; -import * as bip39 from "@scure/bip39"; -import { wordlist } from "@scure/bip39/wordlists/english.js"; -//@ts-ignore -import { SkynetClient } from "@skynetlabs/skynet-nodejs"; - -// Helper variables to make it easier to return empty values alongside errors. -const nu8 = new Uint8Array(0); -const nkp = { - publicKey: nu8, - secretKey: nu8, -}; - -// readFile is a wrapper for fs.readFileSync that handles the try-catch for the -// caller. -function readFile(fileName: string): [string, string | null] { - try { - let data = fs.readFileSync(fileName, "utf8"); - return [data, null]; - } catch (err) { - return ["", "unable to read file: " + JSON.stringify(err)]; - } -} - -// readFileBinary is a wrapper for fs.readFileSync that handles the try-catch -// for the caller. -function readFileBinary(fileName: string): [Uint8Array, string | null] { - try { - let data = fs.readFileSync(fileName, null); - return [data, null]; - } catch (err) { - return [nu8, "unable to read file: " + JSON.stringify(err)]; - } -} - -// writeFile is a wrapper for fs.writeFileSync which handles the try-catch in a -// non-exception way. -function writeFile(fileName: string, fileData: string): string | null { - try { - fs.writeFileSync(fileName, fileData); - return null; - } catch (err) { - return "unable to write file: " + JSON.stringify(err); - } -} - -// handlePass handles all portions of the script that occur after the password -// has been requested. If no password needs to be requested, handlePass will be -// called with a null input. We need to structure the code this way because the -// password reader is async and we can only access the password when using a -// callback. -function handlePass(password: string) { - try { - // If we are running prod and the seed file does not exist, we - // need to confirm the password and also warn the user to use a - // secure password. - if (!fs.existsSync(seedFile) && process.argv[2] === "prod") { - // The file does not exist, we need to confirm the - // password. - console.log(); - console.log( - "No production entry found for module. Creating new production module..." - ); - console.log( - "If someone can guess the password, they can push arbitrary changes to your module." - ); - console.log("Please use a secure password."); - console.log(); - read( - { prompt: "Confirm Password: ", silent: true }, - function (err: any, confirmPassword: string) { - if (err) { - console.error("unable to fetch password:", err); - process.exit(1); - } - if (password !== confirmPassword) { - console.error("passwords do not match"); - process.exit(1); - } - handlePassConfirm(moduleSalt, password); - } - ); - } else { - // If the seed file does exist, or if we are using dev, - // there's no need to confirm the password but we do - // need to pass the logic off to the handlePassConfirm - // callback. - handlePassConfirm(moduleSalt, password); - } - } catch (err) { - console.error("Unable to read seedFile:", err); - process.exit(1); - } -} - -// handlePassConfirm handles the full script after the confirmation password -// has been provided. If not confirmation password is needed, this function -// will be called anyway using the unconfirmed password as input. -function handlePassConfirm(seed: string, password: string) { - // Create the seedFile if it does not exist. For dev we just save the - // seed to disk outright, because this is a dev build and therefore not - // security sensitive. Also the dev seed does not get pushed to the - // github repo. - // - // For prod, we use the seed to create a new seed (called the shield) - // which allows us to verify that the developer has provided the right - // password when deploying the module. The shield does get pushed to - // the github repo so that the production module is the same on all - // devices. - if (!fs.existsSync(seedFile) && process.argv[2] !== "prod") { - // Generate the seed phrase and write it to the file. - let seedPhrase = bip39.generateMnemonic(wordlist); - let errWF = writeFile(seedFile, seedPhrase); - if (errWF !== null) { - console.error("unable to write file:", errWF); - process.exit(1); - } - } else if (!fs.existsSync(seedFile) && process.argv[2] === "prod") { - // Generate the seed phrase. - let seedPhrase = bip39.generateMnemonic(wordlist); - // Write the registry link to the file. - } - - // Load or verify the seed. If this is prod, the password is used to - // create and verify the seed. If this is dev, we just load the seed - // with no password. - let seedPhrase: string; - let registryLink: string; - if (process.argv[2] === "prod") { - // Generate the seed phrase from the password. - seedPhrase = bip39.generateMnemonic(wordlist); - } else { - let [sp, errRF] = readFile(seedFile); - if (errRF !== null) { - console.error("unable to read seed phrase for dev command from disk"); - process.exit(1); - } - seedPhrase = sp; - } - - let metadata = { - Filename: "index.js", - }; - const client = new SkynetClient("https://web3portal.com"); - client - .uploadFile("dist/index.js") - .then((result: any) => { - console.log("Immutable Link for module:", result); - }) - .catch((err: any) => { - console.error("unable to upload file", err); - process.exit(1); - }); -} - -// Add a newline for readability. -console.log(); - -// Check for a 'dev' or 'prod' input to the script. -if (process.argv.length !== 3) { - console.error("need to provide either 'dev' or 'prod' as an input"); - process.exit(1); -} - -// Create the build folder if it does not exist. -if (!fs.existsSync("build")) { - fs.mkdirSync("build"); -} - -// Determine the seed file. -let seedFile: string; -if (process.argv[2] === "prod") { - seedFile = "module-skylink"; -} else if (process.argv[2] === "dev") { - seedFile = "build/dev-seed"; -} else { - console.error("need to provide either 'dev' or 'prod' as an input"); - process.exit(1); -} - -// If doing a prod deployment, check whether the salt file exists. If it does -// not, create it. -let moduleSalt: string; -if (!fs.existsSync(".module-salt")) { - moduleSalt = bip39.generateMnemonic(wordlist); - let errWF = writeFile(".module-salt", moduleSalt); - if (errWF !== null) { - console.error("unable to write module salt file:", errWF); - process.exit(1); - } -} else { - let [ms, errRF] = readFile(".module-salt"); - if (errRF !== null) { - console.error("unable to read moduleSalt"); - process.exit(1); - } - ms = ms.replace(/\n$/, ""); - moduleSalt = ms; -} - -// Need to get a password if this is a prod build. -if (process.argv[2] === "prod") { - read( - { prompt: "Password: ", silent: true }, - function (err: any, password: string) { - if (err) { - console.error("unable to fetch password:", err); - process.exit(1); - } - handlePass(password); - } - ); -} else { - handlePass(""); -} diff --git a/src/index.ts b/src/index.ts index 999e5a0..de5563a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,30 +1,28 @@ -import { createLibp2p } from "libp2p"; +import { createLibp2p, Libp2p } from "libp2p"; import { createHelia } from "helia"; import { yamux } from "@chainsafe/libp2p-yamux"; // @ts-ignore import Hyperswarm from "hyperswarm"; -import { Peer, MultiSocketProxy } from "@lumeweb/libhyperproxy"; -// @ts-ignore -import sodium from "sodium-universal"; -// @ts-ignore -import { CustomEvent } from "@libp2p/interfaces/events"; -// @ts-ignore -import { fixed32, raw } from "compact-encoding"; +import { MultiSocketProxy } from "@lumeweb/libhyperproxy"; import { mplex } from "@libp2p/mplex"; import { hypercoreTransport } from "./libp2p/transport.js"; import { UnixFS, unixfs } from "@helia/unixfs"; -// @ts-ignore + import { delegatedPeerRouting } from "@libp2p/delegated-peer-routing"; import { noise } from "@chainsafe/libp2p-noise"; import { create as createIpfsHttpClient } from "ipfs-http-client"; import { delegatedContentRouting } from "@libp2p/delegated-content-routing"; -// @ts-ignore + import type { Options } from "ipfs-core"; import { multiaddr } from "@multiformats/multiaddr"; import { DELEGATE_LIST, PROTOCOL } from "./constants.js"; -import { ActiveQuery, addHandler, handleMessage } from "libkmodule"; +import { + ActiveQuery, + addHandler, + handleMessage, +} from "@lumeweb/libkernel/module"; import { createClient } from "@lumeweb/kernel-swarm-client"; -import { ipns, ipnsValidator, ipnsSelector, IPNS } from "@helia/ipns"; +import { ipns, IPNS, ipnsSelector, ipnsValidator } from "@helia/ipns"; import { dht, pubsub } from "@helia/ipns/routing"; import { kadDHT } from "@libp2p/kad-dht"; // @ts-ignore @@ -39,9 +37,11 @@ import { IDBBlockstore } from "blockstore-idb"; import { IDBDatastore } from "datastore-idb"; import defer from "p-defer"; import { Helia } from "@helia/interface"; +// @ts-ignore +import type { Components } from "libp2p/src/components.js"; const basesByPrefix: { [prefix: string]: MultibaseDecoder } = Object.keys( - bases + bases, ).reduce((acc, curr) => { // @ts-ignore acc[bases[curr].prefix] = bases[curr]; @@ -65,7 +65,7 @@ BigInt.prototype.toJSON = function () { return this.toString(); }; -addHandler("presentSeed", handlePresentSeed); +addHandler("presentKey", handlePresentKey); addHandler("ready", handleReady); addHandler("stat", handleStat); addHandler("ls", handleLs, { receiveUpdates: true }); @@ -73,7 +73,7 @@ addHandler("cat", handleCat, { receiveUpdates: true }); addHandler("ipnsResolve", handleIpnsResolve); addHandler("getActivePeers", handleGetActivePeers); -async function handlePresentSeed() { +async function handlePresentKey() { swarm = createClient(); const client = createIpfsHttpClient(getDelegateConfig()); @@ -146,10 +146,10 @@ async function handlePresentSeed() { "/ip6/2a01:4ff:f0:3764::1/tcp/4001/p2p/12D3KooWSDj6JM2JmoHwE9AUUwqAFUEg9ndd3pMA8aF2bkYckZfo", "/ip6/2a01:4ff:f0:3764::1/udp/4001/quic/p2p/12D3KooWSDj6JM2JmoHwE9AUUwqAFUEg9ndd3pMA8aF2bkYckZfo", ], - }), + }) as Components, ], transports: [hypercoreTransport({ proxy })], - connectionEncryption: [noise()], + connectionEncryption: [noise() as Components], connectionManager: { autoDial: true, minConnections: 5, @@ -183,10 +183,9 @@ async function handlePresentSeed() { await datastore.open(); ipfs = await createHelia({ - // @ts-ignore blockstore, - // @ts-ignore datastore, + // @ts-ignore libp2p, }); @@ -245,10 +244,10 @@ async function handleStat(aq: ActiveQuery) { JSON.stringify( await fs.stat( getCID(aq.callerInput.cid), - aq.callerInput.options ?? {} - ) - ) - ) + aq.callerInput.options ?? {}, + ), + ), + ), ); } catch (e) { aq.reject((e as Error).message); @@ -278,7 +277,7 @@ async function handleLs(aq: ActiveQuery) { }); const iterable = fs.ls( getCID(aq.callerInput.cid), - aq.callerInput.options ?? {} + aq.callerInput.options ?? {}, ); for await (const item of iterable) { @@ -318,7 +317,7 @@ async function handleCat(aq: ActiveQuery) { const iterable = fs.cat( getCID(aq.callerInput.cid), - aq.callerInput.options ?? {} + aq.callerInput.options ?? {}, ); for await (const chunk of iterable) { @@ -355,9 +354,9 @@ async function handleIpnsResolve(aq: ActiveQuery) { ( await IPNS.resolve( peerIdFromCID(getCID(aq.callerInput.cid)), - aq.callerInput?.options + aq.callerInput?.options, ) - ).asCID.toString() + ).asCID.toString(), ); } catch (e: any) { aq.reject((e as Error).message); diff --git a/src/libp2p/transport.ts b/src/libp2p/transport.ts index 94dcee0..01139a6 100644 --- a/src/libp2p/transport.ts +++ b/src/libp2p/transport.ts @@ -50,6 +50,7 @@ class HypercoreTransport extends TCP { const socket = await this._connect(ma, options); // Avoid uncaught errors caused by unstable connections + // @ts-ignore socket.on("error", (err: any) => { log("socket error", err); }); @@ -113,9 +114,10 @@ class HypercoreTransport extends TCP { const err = new CodeError( `connection timeout after ${Date.now() - start}ms`, - "ERR_CONNECT_TIMEOUT" + "ERR_CONNECT_TIMEOUT", ); // Note: this will result in onError() being called + // @ts-ignore rawSocket?.emit("error", err); }; @@ -128,11 +130,13 @@ class HypercoreTransport extends TCP { const onAbort = (): void => { log("connection aborted %j", cOpts); this.metrics?.dialerEvents.increment({ abort: true }); + // @ts-ignore rawSocket?.destroy(); done(new AbortError()); }; const done = (err?: any): void => { + // @ts-ignore rawSocket?.removeListener("error", onError); // @ts-ignore rawSocket?.removeListener("timeout", onTimeout); @@ -199,7 +203,7 @@ class HypercoreTransport extends TCP { } export function hypercoreTransport( - init: HypercoreOptions = {} + init: HypercoreOptions = {}, ): (components?: TCPComponents) => HypercoreTransport { return (components: TCPComponents = {}) => { return new HypercoreTransport(components, init); diff --git a/tsconfig.build.json b/tsconfig.build.json deleted file mode 100644 index 90439ba..0000000 --- a/tsconfig.build.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "compilerOptions": { - "target": "esnext", - "module": "esnext", - "moduleResolution": "node", - "allowSyntheticDefaultImports": true, - "declaration": true, - "outDir": "./dist-build", - "strict": true, - "esModuleInterop": true - }, - "include": ["src-build"], - "exclude": ["node_modules", "**/__tests__/*"] -} diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index c24bef2..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "target": "esnext", - "declaration": true, - "moduleResolution": "node", - "outDir": "./build", - "strict": true, - "allowSyntheticDefaultImports": true, - "esModuleInterop": true - }, - "include": ["src"], - "exclude": ["node_modules", "**/__tests__/*"] -}