From 9e123405849383526e82ee7b16fc164aa0122e37 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2024 22:30:49 +0100 Subject: [PATCH 01/13] chore: prevent unchecked index access --- packages/tests/tsconfig.json | 3 ++- tsconfig.dev.json | 3 ++- tsconfig.json | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/tests/tsconfig.json b/packages/tests/tsconfig.json index 693102ecc2..36c4347b3b 100644 --- a/packages/tests/tsconfig.json +++ b/packages/tests/tsconfig.json @@ -3,7 +3,8 @@ "compilerOptions": { "outDir": "dist/", "rootDir": "src", - "tsBuildInfoFile": "dist/.tsbuildinfo" + "tsBuildInfoFile": "dist/.tsbuildinfo", + "noUncheckedIndexedAccess": false, }, "include": ["src", "src/run-tests.js"], "exclude": ["src/**/*.spec.ts", "src/test_utils"] diff --git a/tsconfig.dev.json b/tsconfig.dev.json index 17eb953a8b..4c108d3ed2 100644 --- a/tsconfig.dev.json +++ b/tsconfig.dev.json @@ -3,6 +3,7 @@ "compilerOptions": { "module": "ESNext", "moduleResolution": "Bundler", - "noEmit": true + "noEmit": true, + "noUncheckedIndexedAccess": false, } } diff --git a/tsconfig.json b/tsconfig.json index 5a268831bf..bbf3bcf06b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,6 +37,7 @@ /* Experimental Options */ // "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */, // "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */, + "noUncheckedIndexedAccess": true, "lib": ["es2022", "dom"], "types": ["node", "mocha"], From a9b81a944d0fb8c6f0e15917b86a594897e13415 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2024 23:26:18 +0100 Subject: [PATCH 02/13] fix errors in utils --- .../tests/tests/store/multiple_pubsub.spec.ts | 4 +- packages/tests/tests/store/utils.ts | 20 +++++++ packages/utils/src/common/random_subset.ts | 4 +- .../utils/src/common/relay_shard_codec.ts | 2 +- packages/utils/src/common/sharding.ts | 56 +++++++------------ packages/utils/src/libp2p/index.ts | 2 +- 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index d7ead85543..f9828bd51d 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -3,8 +3,7 @@ import type { ContentTopicInfo, IMessage, LightNode } from "@waku/interfaces"; import { createLightNode, Protocols } from "@waku/sdk"; import { contentTopicToPubsubTopic, - pubsubTopicToSingleShardInfo, - singleShardInfosToShardInfo + pubsubTopicToSingleShardInfo } from "@waku/utils"; import { expect } from "chai"; @@ -29,6 +28,7 @@ import { sendMessagesAutosharding, shardInfo1, shardInfoBothShards, + singleShardInfosToShardInfo, startAndConnectLightNode, totalMsgs } from "./utils.js"; diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index d1b88161d0..e1225358c8 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -148,3 +148,23 @@ export const adjustDate = (baseDate: Date, adjustMs: number): Date => { adjusted.setTime(adjusted.getTime() + adjustMs); return adjusted; }; + +export const singleShardInfosToShardInfo = ( + singleShardInfos: SingleShardInfo[] +): ShardInfo => { + if (singleShardInfos.length === 0) throw new Error("Invalid shard"); + + const clusterIds = singleShardInfos.map((shardInfo) => shardInfo.clusterId); + if (new Set(clusterIds).size !== 1) { + throw new Error("Passed shard infos have different clusterIds"); + } + + const shards = singleShardInfos + .map((shardInfo) => shardInfo.shard) + .filter((shard): shard is number => shard !== undefined); + + return { + clusterId: singleShardInfos[0].clusterId, + shards + }; +}; diff --git a/packages/utils/src/common/random_subset.ts b/packages/utils/src/common/random_subset.ts index 20d0ca83ce..84ad1d306f 100644 --- a/packages/utils/src/common/random_subset.ts +++ b/packages/utils/src/common/random_subset.ts @@ -22,8 +22,8 @@ function shuffle(arr: T[]): T[] { for (let i = 0; i < arr.length; i++) { const j = randInt(); - const tmp = arr[i]; - arr[i] = arr[j]; + const tmp = arr[i] as T; + arr[i] = arr[j] as T; arr[j] = tmp; } return arr; diff --git a/packages/utils/src/common/relay_shard_codec.ts b/packages/utils/src/common/relay_shard_codec.ts index 91dea7b4ea..063dd3be8a 100644 --- a/packages/utils/src/common/relay_shard_codec.ts +++ b/packages/utils/src/common/relay_shard_codec.ts @@ -52,7 +52,7 @@ export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => { // rs format (Index List) view.setUint8(2, shards.length); for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) { - view.setUint16(offset, shards[i]); + view.setUint16(offset, shards[i] as number); } } diff --git a/packages/utils/src/common/sharding.ts b/packages/utils/src/common/sharding.ts index 6e2b6de68b..5e835da9a7 100644 --- a/packages/utils/src/common/sharding.ts +++ b/packages/utils/src/common/sharding.ts @@ -19,26 +19,6 @@ export const singleShardInfoToPubsubTopic = ( return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`; }; -export const singleShardInfosToShardInfo = ( - singleShardInfos: SingleShardInfo[] -): ShardInfo => { - if (singleShardInfos.length === 0) throw new Error("Invalid shard"); - - const clusterIds = singleShardInfos.map((shardInfo) => shardInfo.clusterId); - if (new Set(clusterIds).size !== 1) { - throw new Error("Passed shard infos have different clusterIds"); - } - - const shards = singleShardInfos - .map((shardInfo) => shardInfo.shard) - .filter((shard): shard is number => shard !== undefined); - - return { - clusterId: singleShardInfos[0].clusterId, - shards - }; -}; - export const shardInfoToPubsubTopics = ( shardInfo: Partial ): PubsubTopic[] => { @@ -80,15 +60,15 @@ export const pubsubTopicToSingleShardInfo = ( const parts = pubsubTopics.split("/"); if ( - parts.length != 6 || + parts.length !== 6 || parts[1] !== "waku" || parts[2] !== "2" || parts[3] !== "rs" ) throw new Error("Invalid pubsub topic"); - const clusterId = parseInt(parts[4]); - const shard = parseInt(parts[5]); + const clusterId = parseInt(parts[4] as string); + const shard = parseInt(parts[5] as string); if (isNaN(clusterId) || isNaN(shard)) throw new Error("Invalid clusterId or shard"); @@ -131,7 +111,7 @@ export function ensureValidContentTopic(contentTopic: string): ContentTopic { // Validate generation field if present let generation = 0; if (parts.length == 6) { - generation = parseInt(parts[1]); + generation = parseInt(parts[1] as string); if (isNaN(generation)) { throw new Error("Invalid generation field in content topic"); } @@ -141,29 +121,33 @@ export function ensureValidContentTopic(contentTopic: string): ContentTopic { } // Validate remaining fields const fields = parts.splice(-4); - // Validate application field - if (fields[0].length == 0) { + + const application = fields[0]; + if (!application || application?.length == 0) { throw new Error("Application field cannot be empty"); } - // Validate version field - if (fields[1].length == 0) { + + const version = fields[1]; + if (!version || version?.length == 0) { throw new Error("Version field cannot be empty"); } - // Validate topic name field - if (fields[2].length == 0) { + + const topicName = fields[2]; + if (!topicName || topicName?.length == 0) { throw new Error("Topic name field cannot be empty"); } - // Validate encoding field - if (fields[3].length == 0) { + + const encoding = fields[3]; + if (!encoding || encoding?.length == 0) { throw new Error("Encoding field cannot be empty"); } return { generation, - application: fields[0], - version: fields[1], - topicName: fields[2], - encoding: fields[3] + application, + version, + topicName, + encoding }; } diff --git a/packages/utils/src/libp2p/index.ts b/packages/utils/src/libp2p/index.ts index 04c37c7d6d..348cc0278c 100644 --- a/packages/utils/src/libp2p/index.ts +++ b/packages/utils/src/libp2p/index.ts @@ -61,7 +61,7 @@ export async function getPeersForProtocol( const peers: Peer[] = []; await peerStore.forEach((peer) => { for (let i = 0; i < protocols.length; i++) { - if (peer.protocols.includes(protocols[i])) { + if (peer.protocols.includes(protocols[i] as string)) { peers.push(peer); break; } From 3c53ee1b5d3f92f7ae3931fd420b4d541d0f1507 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2024 23:30:42 +0100 Subject: [PATCH 03/13] fix message-encryption --- packages/message-encryption/src/crypto/ecies.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/message-encryption/src/crypto/ecies.ts b/packages/message-encryption/src/crypto/ecies.ts index a53d3ef558..5fc04141ee 100644 --- a/packages/message-encryption/src/crypto/ecies.ts +++ b/packages/message-encryption/src/crypto/ecies.ts @@ -185,8 +185,12 @@ export async function decrypt( (macKey) => [hash.slice(0, 16), macKey] ); + if (!encryptionKey || !macKey) { + throw Error("Failed to get parameters from the hash."); + } + if (!(await hmacSha256Verify(macKey, cipherAndIv, msgMac))) { - throw new Error("Incorrect MAC"); + throw Error("Incorrect MAC"); } return aesCtrDecrypt(iv, encryptionKey, ciphertext); From c4698d9447a61b127817bd2775054031557f6b88 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 2 Feb 2024 00:01:11 +0100 Subject: [PATCH 04/13] fix enr --- packages/enr/src/decoder.ts | 2 +- packages/enr/src/enr.ts | 14 ++++++-------- packages/enr/src/raw_enr.ts | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/enr/src/decoder.ts b/packages/enr/src/decoder.ts index e01cd5f189..06e3571b74 100644 --- a/packages/enr/src/decoder.ts +++ b/packages/enr/src/decoder.ts @@ -30,7 +30,7 @@ async function fromValues(values: Uint8Array[]): Promise { const obj: Record = {}; for (let i = 0; i < kvs.length; i += 2) { try { - obj[bytesToUtf8(kvs[i])] = kvs[i + 1]; + obj[bytesToUtf8(kvs[i] as Uint8Array)] = kvs[i + 1] as Uint8Array; } catch (e) { log.error("Failed to decode ENR key to UTF-8, skipping it", kvs[i], e); } diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 7dd9df0ee1..05422ff84a 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -73,25 +73,23 @@ export class ENR extends RawEnr implements IEnr { setLocationMultiaddr(multiaddr: Multiaddr): void { const protoNames = multiaddr.protoNames(); - if ( - protoNames.length !== 2 && - protoNames[1] !== "udp" && - protoNames[1] !== "tcp" - ) { + const protocol = protoNames[1]; + if (protoNames.length !== 2 && protocol !== "udp" && protocol !== "tcp") { throw new Error("Invalid multiaddr"); } + const tuples = multiaddr.tuples(); - if (!tuples[0][1] || !tuples[1][1]) { + if (!tuples[0]?.[1] || !tuples[1]?.[1] || !protocol) { throw new Error("Invalid multiaddr"); } // IPv4 if (tuples[0][0] === 4) { this.set("ip", tuples[0][1]); - this.set(protoNames[1], tuples[1][1]); + this.set(protocol, tuples[1][1]); } else { this.set("ip6", tuples[0][1]); - this.set(protoNames[1] + "6", tuples[1][1]); + this.set(protocol + "6", tuples[1][1]); } } diff --git a/packages/enr/src/raw_enr.ts b/packages/enr/src/raw_enr.ts index b7c607880e..f6373effd6 100644 --- a/packages/enr/src/raw_enr.ts +++ b/packages/enr/src/raw_enr.ts @@ -153,7 +153,7 @@ export class RawEnr extends Map { */ get waku2(): Waku2 | undefined { const raw = this.get("waku2"); - if (raw) return decodeWaku2(raw[0]); + if (raw && raw[0]) return decodeWaku2(raw[0]); return; } From a5e8c8d220372aa1fe164d6b4f95b6c074bd7d68 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 2 Feb 2024 00:13:42 +0100 Subject: [PATCH 05/13] fix core --- packages/core/src/lib/filter/index.ts | 6 +++--- packages/core/src/lib/filterPeers.ts | 5 ++++- packages/core/src/lib/store/index.ts | 9 ++++++++- packages/core/src/lib/wait_for_remote_peer.ts | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index 7e6353964c..90520ee571 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -110,7 +110,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length) { + if (!res || !res.length || !res[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); @@ -203,7 +203,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length) { + if (!res || !res.length || !res[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); @@ -246,7 +246,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length) { + if (!res || !res.length || !res[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); diff --git a/packages/core/src/lib/filterPeers.ts b/packages/core/src/lib/filterPeers.ts index 816c3bd5b5..1ac1ef76b9 100644 --- a/packages/core/src/lib/filterPeers.ts +++ b/packages/core/src/lib/filterPeers.ts @@ -43,7 +43,10 @@ export function filterPeersByDiscovery( // Fill up to numPeers with remaining random peers if needed while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) { const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length); - const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0]; + const randomPeer = nonBootstrapPeers[randomIndex]; + if (!randomPeer) { + continue; + } selectedPeers.push(randomPeer); } diff --git a/packages/core/src/lib/store/index.ts b/packages/core/src/lib/store/index.ts index d5ca1c1b47..7e8edd7ebb 100644 --- a/packages/core/src/lib/store/index.ts +++ b/packages/core/src/lib/store/index.ts @@ -237,9 +237,12 @@ class Store extends BaseProtocol implements IStore { ); } - // we can be certain that there is only one pubsub topic in the query const pubsubTopicForQuery = uniquePubsubTopicsInQuery[0]; + if (!pubsubTopicForQuery) { + throw Error("Cannot find a pubsub topic to use for query."); + } + ensurePubsubTopicIsConfigured(pubsubTopicForQuery, this.pubsubTopics); // check that the pubsubTopic from the Cursor and Decoder match @@ -287,6 +290,10 @@ class Store extends BaseProtocol implements IStore { }) )[0]; + if (!peer) { + throw Error("Failed executing query: missing a peer."); + } + for await (const messages of paginate( this.getStream.bind(this, peer), queryOpts, diff --git a/packages/core/src/lib/wait_for_remote_peer.ts b/packages/core/src/lib/wait_for_remote_peer.ts index 183e81d75c..d6ef075729 100644 --- a/packages/core/src/lib/wait_for_remote_peer.ts +++ b/packages/core/src/lib/wait_for_remote_peer.ts @@ -88,7 +88,7 @@ async function waitForConnectedPeer( if (peers.length) { if (!metadataService) { - log.info(`${codec} peer found: `, peers[0].id.toString()); + log.info(`${codec} peer found: `, peers[0]?.id.toString()); return; } From d785d7369d368b96d58e39fc9de1ba722e2b02f9 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 2 Feb 2024 00:15:27 +0100 Subject: [PATCH 06/13] fix relay --- packages/relay/src/constants.ts | 2 +- packages/relay/src/index.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/relay/src/constants.ts b/packages/relay/src/constants.ts index 067084d991..6c02e4d59b 100644 --- a/packages/relay/src/constants.ts +++ b/packages/relay/src/constants.ts @@ -4,7 +4,7 @@ export const minute = 60 * second; /** * RelayCodec is the libp2p identifier for the waku relay protocol */ -export const RelayCodecs = ["/vac/waku/relay/2.0.0"]; +export const RelayCodec = "/vac/waku/relay/2.0.0"; /** * RelayGossipFactor affects how many peers we will emit gossip to at each heartbeat. diff --git a/packages/relay/src/index.ts b/packages/relay/src/index.ts index 0f9ac329f1..c99322fcfa 100644 --- a/packages/relay/src/index.ts +++ b/packages/relay/src/index.ts @@ -32,7 +32,7 @@ import { import { pushOrInitMapSet } from "@waku/utils"; import { Logger } from "@waku/utils"; -import { RelayCodecs } from "./constants.js"; +import { RelayCodec } from "./constants.js"; import { messageValidator } from "./message_validator.js"; import { TopicOnlyDecoder } from "./topic_only_message.js"; @@ -54,7 +54,7 @@ class Relay implements IRelay { public readonly pubsubTopics: Set; private defaultDecoder: IDecoder; - public static multicodec: string = RelayCodecs[0]; + public static multicodec: string = RelayCodec; public readonly gossipSub: GossipSub; /** @@ -300,7 +300,7 @@ export function wakuGossipSub( fallbackToFloodsub: false }; const pubsub = new GossipSub(components, init); - pubsub.multicodecs = RelayCodecs; + pubsub.multicodecs = [RelayCodec]; return pubsub; }; } From e17daf9de42a740db9157d9140abeb197e6a25a1 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 2 Feb 2024 00:35:15 +0100 Subject: [PATCH 07/13] fix dns-discovery --- packages/dns-discovery/src/dns.ts | 34 +++++++++++++++++++-------- packages/dns-discovery/src/enrtree.ts | 17 +++++++++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/dns-discovery/src/dns.ts b/packages/dns-discovery/src/dns.ts index b17b25d4c5..04d0d6b88b 100644 --- a/packages/dns-discovery/src/dns.ts +++ b/packages/dns-discovery/src/dns.ts @@ -8,7 +8,7 @@ import type { import { Logger } from "@waku/utils"; import { DnsOverHttps } from "./dns_over_https.js"; -import { ENRTree } from "./enrtree.js"; +import { ENRTree, ENRTreeValues } from "./enrtree.js"; import { fetchNodesUntilCapabilitiesFulfilled, yieldNodesUntilCapabilitiesFulfilled @@ -41,8 +41,7 @@ export class DnsNodeDiscovery { enrTreeUrls: string[], wantedNodeCapabilityCount: Partial ): Promise { - const networkIndex = Math.floor(Math.random() * enrTreeUrls.length); - const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]); + const { publicKey, domain } = DnsNodeDiscovery.parseTree(enrTreeUrls); const context: SearchContext = { domain, publicKey, @@ -78,8 +77,7 @@ export class DnsNodeDiscovery { enrTreeUrls: string[], wantedNodeCapabilityCount: Partial ): AsyncGenerator { - const networkIndex = Math.floor(Math.random() * enrTreeUrls.length); - const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]); + const { publicKey, domain } = DnsNodeDiscovery.parseTree(enrTreeUrls); const context: SearchContext = { domain, publicKey, @@ -149,8 +147,9 @@ export class DnsNodeDiscovery { subdomain: string, context: SearchContext ): Promise { - if (this._DNSTreeCache[subdomain]) { - return this._DNSTreeCache[subdomain]; + const currentCache = this._DNSTreeCache[subdomain]; + if (currentCache) { + return currentCache; } // Location is either the top level tree entry host or a subdomain of it. @@ -161,9 +160,13 @@ export class DnsNodeDiscovery { const response = await this.dns.resolveTXT(location); - if (!response.length) + if (!response.length) { throw new Error("Received empty result array while fetching TXT record"); - if (!response[0].length) throw new Error("Received empty TXT record"); + } + + if (!response[0]?.length) { + throw new Error("Received empty TXT record"); + } // Branch entries can be an array of strings of comma delimited subdomains, with // some subdomain strings split across the array elements @@ -172,6 +175,17 @@ export class DnsNodeDiscovery { this._DNSTreeCache[subdomain] = result; return result; } + + private static parseTree(enrTreeUrls: string[]): ENRTreeValues { + const networkIndex = Math.floor(Math.random() * enrTreeUrls.length); + const enrTree = enrTreeUrls[networkIndex]; + + if (!enrTree) { + throw Error(`Failed to read ENR tree for the network: ${networkIndex}`); + } + + return ENRTree.parseTree(enrTree); + } } function getEntryType(entry: string): string { @@ -211,5 +225,5 @@ function selectRandomPath(branches: string[], context: SearchContext): string { index = Math.floor(Math.random() * branches.length); } while (circularRefs[index]); - return branches[index]; + return branches[index] as string; } diff --git a/packages/dns-discovery/src/enrtree.ts b/packages/dns-discovery/src/enrtree.ts index 7a1412c29b..f6023bb912 100644 --- a/packages/dns-discovery/src/enrtree.ts +++ b/packages/dns-discovery/src/enrtree.ts @@ -40,6 +40,10 @@ export class ENRTree { // (Trailing recovery bit must be trimmed to pass `ecdsaVerify` method) const signedComponent = root.split(" sig")[0]; + if (!signedComponent) { + throw Error(`Signature component is missing, got: ${signedComponent}`); + } + const signedComponentBuffer = utf8ToBytes(signedComponent); const signatureBuffer = fromString(rootValues.signature, "base64url").slice( 0, @@ -113,11 +117,18 @@ export class ENRTree { * either further branch entries or ENR records. */ static parseBranch(branch: string): string[] { - if (!branch.startsWith(this.BRANCH_PREFIX)) - throw new Error( + if (!branch.startsWith(this.BRANCH_PREFIX)) { + throw Error( `ENRTree branch entry must start with '${this.BRANCH_PREFIX}'` ); + } + + const enrPart = branch.split(this.BRANCH_PREFIX)[1]; + + if (!enrPart) { + throw Error(`ENRTree branch does not have data`); + } - return branch.split(this.BRANCH_PREFIX)[1].split(","); + return enrPart.split(","); } } From 38d02d24dab8f278f13796e7bcc9c71b7e482ad2 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 00:41:19 +0100 Subject: [PATCH 08/13] fix comment --- packages/core/src/lib/filter/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index 90520ee571..ea6e9275a3 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -110,7 +110,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length || !res[0]) { + if (!res?.[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); @@ -203,7 +203,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length || !res[0]) { + if (!res?.[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); @@ -246,7 +246,7 @@ class Subscription { async (source) => await all(source) ); - if (!res || !res.length || !res[0]) { + if (!res?.[0]) { throw Error( `No response received for request ${request.requestId}: ${res}` ); From b9154e87a41cddcf6f7a43427b6dfa18a9d21706 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 00:53:17 +0100 Subject: [PATCH 09/13] use tsconfig.dev only for tests --- packages/core/package.json | 2 +- packages/dns-discovery/package.json | 2 +- packages/enr/package.json | 2 +- packages/interfaces/package.json | 2 +- packages/message-encryption/package.json | 2 +- packages/message-hash/package.json | 2 +- packages/peer-exchange/package.json | 2 +- packages/proto/package.json | 2 +- packages/relay/package.json | 2 +- packages/sdk/package.json | 2 +- packages/utils/package.json | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 91ad2afc8c..57ed76b392 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -57,7 +57,7 @@ "fix": "run-s fix:*", "fix:lint": "eslint src *.js --fix", "check": "run-s check:*", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "test": "NODE_ENV=test run-s test:*", diff --git a/packages/dns-discovery/package.json b/packages/dns-discovery/package.json index b1ee184778..365f1febd7 100644 --- a/packages/dns-discovery/package.json +++ b/packages/dns-discovery/package.json @@ -40,7 +40,7 @@ "check": "run-s check:*", "check:lint": "eslint src --ext .ts", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build", "test": "NODE_ENV=test run-s test:*", diff --git a/packages/enr/package.json b/packages/enr/package.json index 02f94b7b67..1e7a449b69 100644 --- a/packages/enr/package.json +++ b/packages/enr/package.json @@ -40,7 +40,7 @@ "check": "run-s check:*", "check:lint": "eslint src --ext .ts", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "test": "NODE_ENV=test run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", "test:browser": "NODE_ENV=test karma start karma.conf.cjs", diff --git a/packages/interfaces/package.json b/packages/interfaces/package.json index 31fe13cc76..fd2f0508a8 100644 --- a/packages/interfaces/package.json +++ b/packages/interfaces/package.json @@ -39,7 +39,7 @@ "check": "run-s check:*", "check:lint": "eslint src", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/message-encryption/package.json b/packages/message-encryption/package.json index 43cfcdf5fb..be8b90404a 100644 --- a/packages/message-encryption/package.json +++ b/packages/message-encryption/package.json @@ -61,7 +61,7 @@ "check": "run-s check:*", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "test": "NODE_ENV=test run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", "test:browser": "NODE_ENV=test karma start karma.conf.cjs", diff --git a/packages/message-hash/package.json b/packages/message-hash/package.json index 5ff7eb9aaf..1faea1a673 100644 --- a/packages/message-hash/package.json +++ b/packages/message-hash/package.json @@ -35,7 +35,7 @@ "fix": "run-s fix:*", "fix:lint": "eslint src *.js --fix", "check": "run-s check:*", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "test": "NODE_ENV=test run-s test:*", diff --git a/packages/peer-exchange/package.json b/packages/peer-exchange/package.json index e6c4afb5c0..8822bb3061 100644 --- a/packages/peer-exchange/package.json +++ b/packages/peer-exchange/package.json @@ -40,7 +40,7 @@ "check": "run-s check:*", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/proto/package.json b/packages/proto/package.json index 77c20cc67c..c06e50d736 100644 --- a/packages/proto/package.json +++ b/packages/proto/package.json @@ -36,7 +36,7 @@ "build:esm": "tsc", "build:bundle": "rollup --config rollup.config.js", "proto": "rimraf src/lib/*.ts; protons src/lib/*.proto", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/relay/package.json b/packages/relay/package.json index f3bc6b52f1..03a6b64183 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -35,7 +35,7 @@ "fix": "run-s fix:*", "fix:lint": "eslint src *.js --fix", "check": "run-s check:*", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "test": "NODE_ENV=test run-s test:*", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index f834706840..ce761867d4 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -53,7 +53,7 @@ "check": "run-s check:*", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/utils/package.json b/packages/utils/package.json index c0298657f4..db50d60f2e 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -57,7 +57,7 @@ "check": "run-s check:*", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "check:tsc": "tsc -p tsconfig.dev.json", + "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build", "test": "NODE_ENV=test run-s test:*", From 7896dee3de1951df15ce1a5984213583184ace8f Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 01:02:20 +0100 Subject: [PATCH 10/13] use dev tsconfig on all test scripts --- package.json | 6 +++--- packages/core/package.json | 4 ++-- packages/dns-discovery/package.json | 4 ++-- packages/enr/package.json | 4 ++-- packages/message-encryption/package.json | 4 ++-- packages/message-hash/package.json | 4 ++-- packages/relay/package.json | 4 ++-- packages/tests/package.json | 6 +++--- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index e66246c66d..05328abfbc 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,9 @@ "check": "run-s check:*", "check:workspaces": "npm run check --workspaces --if-present", "check:ws": "[ $(ls -1 ./packages|wc -l) -eq $(cat package.json | jq '.workspaces | length') ] || exit 1 # check no packages left behind", - "test": "NODE_ENV=test npm run test --workspaces --if-present", - "test:browser": "NODE_ENV=test npm run test:browser --workspaces --if-present", - "test:node": "NODE_ENV=test npm run test:node --workspaces --if-present", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test --workspaces --if-present", + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test:browser --workspaces --if-present", + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test:node --workspaces --if-present", "proto": "npm run proto --workspaces --if-present", "deploy": "node ci/deploy.js", "doc": "run-s doc:*", diff --git a/packages/core/package.json b/packages/core/package.json index 57ed76b392..edf2877f5c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -60,9 +60,9 @@ "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "test": "NODE_ENV=test run-s test:*", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs", + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json karma start karma.conf.cjs", "watch:build": "tsc -p tsconfig.json -w", "watch:test": "mocha --watch", "prepublish": "npm run build", diff --git a/packages/dns-discovery/package.json b/packages/dns-discovery/package.json index 365f1febd7..b38fff1b9a 100644 --- a/packages/dns-discovery/package.json +++ b/packages/dns-discovery/package.json @@ -43,9 +43,9 @@ "check:tsc": "tsc -p tsconfig.json", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build", - "test": "NODE_ENV=test run-s test:*", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs" + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json karma start karma.conf.cjs" }, "engines": { "node": ">=18" diff --git a/packages/enr/package.json b/packages/enr/package.json index 1e7a449b69..a626092a96 100644 --- a/packages/enr/package.json +++ b/packages/enr/package.json @@ -41,9 +41,9 @@ "check:lint": "eslint src --ext .ts", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "check:tsc": "tsc -p tsconfig.json", - "test": "NODE_ENV=test run-s test:*", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs", + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json karma start karma.conf.cjs", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/message-encryption/package.json b/packages/message-encryption/package.json index be8b90404a..9b363c354a 100644 --- a/packages/message-encryption/package.json +++ b/packages/message-encryption/package.json @@ -62,9 +62,9 @@ "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "check:tsc": "tsc -p tsconfig.json", - "test": "NODE_ENV=test run-s test:*", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs", + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json karma start karma.conf.cjs", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, diff --git a/packages/message-hash/package.json b/packages/message-hash/package.json index 1faea1a673..d930dda150 100644 --- a/packages/message-hash/package.json +++ b/packages/message-hash/package.json @@ -38,9 +38,9 @@ "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "test": "NODE_ENV=test run-s test:*", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", - "test:browser": "NODE_ENV=test karma start karma.conf.cjs", + "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json karma start karma.conf.cjs", "watch:build": "tsc -p tsconfig.json -w", "watch:test": "mocha --watch", "prepublish": "npm run build", diff --git a/packages/relay/package.json b/packages/relay/package.json index 03a6b64183..90f9b8544f 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -38,8 +38,8 @@ "check:tsc": "tsc -p tsconfig.json", "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", - "test": "NODE_ENV=test run-s test:*", - "test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", "watch:build": "tsc -p tsconfig.json -w", "watch:test": "mocha --watch", "prepublish": "npm run build", diff --git a/packages/tests/package.json b/packages/tests/package.json index 8cf36c3e71..d86266136a 100644 --- a/packages/tests/package.json +++ b/packages/tests/package.json @@ -41,9 +41,9 @@ "check:lint": "eslint src tests", "check:spelling": "cspell \"{README.md,{tests,src}/**/*.ts}\"", "check:tsc": "tsc -p tsconfig.dev.json", - "test": "NODE_ENV=test run-s test:*", - "test:node": "NODE_ENV=test node ./src/run-tests.js \"tests/**/!(*.optional).spec.ts\"", - "test:optional": "NODE_ENV=test node ./src/run-tests.js \"tests/**/@(*.optional).spec.ts\"", + "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json run-s test:*", + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json node ./src/run-tests.js \"tests/**/!(*.optional).spec.ts\"", + "test:optional": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json node ./src/run-tests.js \"tests/**/@(*.optional).spec.ts\"", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, "engines": { From b4a323f03786d638005a002a6851a7181bc7903f Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 02:09:28 +0100 Subject: [PATCH 11/13] make karma use dev tsconfig --- karma.conf.cjs | 31 ++++++++++++++++++++----------- tsconfig.dev.json | 1 - 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/karma.conf.cjs b/karma.conf.cjs index dcdf6e3694..12ec027d8d 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -1,15 +1,18 @@ const webpack = require("webpack"); -const playwright = require('playwright'); +const playwright = require("playwright"); process.env.CHROME_BIN = playwright.chromium.executablePath(); process.env.FIREFOX_BIN = playwright.firefox.executablePath(); +const path = require("path"); +const tsConfigFile = path.resolve(__dirname, "./tsconfig.dev.json"); + module.exports = function (config) { config.set({ frameworks: ["webpack", "mocha"], files: ["src/**/!(node).spec.ts"], preprocessors: { - "src/**/!(node).spec.ts": ["webpack"] + "src/**/!(node).spec.ts": ["webpack"], }, envPreprocessor: ["CI"], reporters: ["progress"], @@ -17,13 +20,19 @@ module.exports = function (config) { singleRun: true, client: { mocha: { - timeout: 6000 // Default is 2s - } + timeout: 6000, // Default is 2s + }, }, webpack: { mode: "development", module: { - rules: [{ test: /\.([cm]?ts|tsx)$/, loader: "ts-loader" }] + rules: [ + { + test: /\.([cm]?ts|tsx)$/, + loader: "ts-loader", + options: { configFile: tsConfigFile }, + }, + ], }, plugins: [ new webpack.DefinePlugin({ @@ -31,19 +40,19 @@ module.exports = function (config) { "process.env.DISPLAY": "Browser", }), new webpack.ProvidePlugin({ - process: "process/browser.js" - }) + process: "process/browser.js", + }), ], resolve: { extensions: [".ts", ".tsx", ".js"], extensionAlias: { ".js": [".js", ".ts"], ".cjs": [".cjs", ".cts"], - ".mjs": [".mjs", ".mts"] - } + ".mjs": [".mjs", ".mts"], + }, }, stats: { warnings: false }, - devtool: "inline-source-map" - } + devtool: "inline-source-map", + }, }); }; diff --git a/tsconfig.dev.json b/tsconfig.dev.json index 4c108d3ed2..02ffa814d0 100644 --- a/tsconfig.dev.json +++ b/tsconfig.dev.json @@ -3,7 +3,6 @@ "compilerOptions": { "module": "ESNext", "moduleResolution": "Bundler", - "noEmit": true, "noUncheckedIndexedAccess": false, } } From 8913dca441d31dd52d2f8f12363a5afd9708d22e Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 02:10:24 +0100 Subject: [PATCH 12/13] remove not needed env from root package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 05328abfbc..1fa61a76e5 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,9 @@ "check": "run-s check:*", "check:workspaces": "npm run check --workspaces --if-present", "check:ws": "[ $(ls -1 ./packages|wc -l) -eq $(cat package.json | jq '.workspaces | length') ] || exit 1 # check no packages left behind", - "test": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test --workspaces --if-present", - "test:browser": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test:browser --workspaces --if-present", - "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json npm run test:node --workspaces --if-present", + "test": "npm run test --workspaces --if-present", + "test:browser": "npm run test:browser --workspaces --if-present", + "test:node": "npm run test:node --workspaces --if-present", "proto": "npm run proto --workspaces --if-present", "deploy": "node ci/deploy.js", "doc": "run-s doc:*", From 3514fb17365396bb58055539c519cdfb42c6665e Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 8 Feb 2024 23:08:39 +0100 Subject: [PATCH 13/13] fix bug --- packages/enr/src/raw_enr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/enr/src/raw_enr.ts b/packages/enr/src/raw_enr.ts index f6373effd6..b1e828a7e9 100644 --- a/packages/enr/src/raw_enr.ts +++ b/packages/enr/src/raw_enr.ts @@ -153,7 +153,7 @@ export class RawEnr extends Map { */ get waku2(): Waku2 | undefined { const raw = this.get("waku2"); - if (raw && raw[0]) return decodeWaku2(raw[0]); + if (raw) return decodeWaku2(raw[0] as number); return; }