From 5f29ae014049f59aa377b527a94c7a627157f721 Mon Sep 17 00:00:00 2001 From: ennmichael Date: Sat, 8 Apr 2023 21:14:08 +0200 Subject: [PATCH] fixing tests --- .github/workflows/test-lint.yaml | 2 +- packages/app/package.json | 2 -- packages/app/src/System/Query.test.ts | 8 +++-- packages/nostr/package.json | 2 ++ packages/nostr/src/legacy/Connection.ts | 39 ++++++++++++++++--------- packages/nostr/webpack.config.js | 4 +++ yarn.lock | 2 +- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/.github/workflows/test-lint.yaml b/.github/workflows/test-lint.yaml index 352a0a4..bc3a2cf 100644 --- a/.github/workflows/test-lint.yaml +++ b/.github/workflows/test-lint.yaml @@ -11,7 +11,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v3 with: - node-version: 16 + node-version: 19 - name: Install Dependencies run: yarn install - name: Build packages diff --git a/packages/app/package.json b/packages/app/package.json index d9e1eb3..051b63d 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -36,7 +36,6 @@ "throttle-debounce": "^5.0.0", "unist-util-visit": "^4.1.2", "use-long-press": "^2.0.3", - "uuid": "^9.0.0", "workbox-background-sync": "^6.4.2", "workbox-broadcast-update": "^6.4.2", "workbox-cacheable-response": "^6.4.2", @@ -85,7 +84,6 @@ "@types/node": "^18.11.18", "@types/react": "^18.0.26", "@types/react-dom": "^18.0.10", - "@types/uuid": "^9.0.0", "@types/webscopeio__react-textarea-autocomplete": "^4.7.2", "@types/webtorrent": "^0.109.3", "@webscopeio/react-textarea-autocomplete": "^4.9.2", diff --git a/packages/app/src/System/Query.test.ts b/packages/app/src/System/Query.test.ts index 70cfcd0..61a0369 100644 --- a/packages/app/src/System/Query.test.ts +++ b/packages/app/src/System/Query.test.ts @@ -1,6 +1,10 @@ -import { Connection, RelaySettings } from "@snort/nostr"; +import { Connection } from "@snort/nostr"; import { unixNow } from "Util"; import { Query } from "./Query"; +import { getRandomValues } from "crypto"; + +window.crypto = {} as any; +window.crypto.getRandomValues = getRandomValues as any; describe("query", () => { test("progress", () => { @@ -16,7 +20,7 @@ describe("query", () => { const opt = { read: true, write: true, - } as RelaySettings; + }; const c1 = new Connection("wss://one.com", opt); c1.Down = false; const c2 = new Connection("wss://two.com", opt); diff --git a/packages/nostr/package.json b/packages/nostr/package.json index e9c5d6e..fa27def 100644 --- a/packages/nostr/package.json +++ b/packages/nostr/package.json @@ -31,12 +31,14 @@ "@noble/hashes": "^1.2.0", "@noble/secp256k1": "^1.7.1", "@types/chai": "^4.3.4", + "@types/uuid": "^9.0.1", "base64-js": "^1.5.1", "bech32": "^2.0.0", "chai": "^4.3.7", "events": "^3.3.0", "isomorphic-ws": "^5.0.0", "ts-loader": "^9.4.2", + "uuid": "^9.0.0", "webpack": "^5.77.0", "webpack-cli": "^5.0.1", "ws": "^8.12.1" diff --git a/packages/nostr/src/legacy/Connection.ts b/packages/nostr/src/legacy/Connection.ts index 05f5576..f5f7056 100644 --- a/packages/nostr/src/legacy/Connection.ts +++ b/packages/nostr/src/legacy/Connection.ts @@ -7,7 +7,10 @@ import { RelayInfo } from "./RelayInfo"; import { unwrap } from "./Util"; export type CustomHook = (state: Readonly) => void; -export type AuthHandler = (challenge: string, relay: string) => Promise; +export type AuthHandler = ( + challenge: string, + relay: string +) => Promise; /** * Relay settings @@ -15,7 +18,7 @@ export type AuthHandler = (challenge: string, relay: string) => Promise; activeRequests: Array; id: string; -}; +} export class Connection { Id: string; @@ -64,7 +67,12 @@ export class Connection { EphemeralTimeout: ReturnType | undefined; Down = true; - constructor(addr: string, options: RelaySettings, auth?: AuthHandler, ephemeral: boolean = false) { + constructor( + addr: string, + options: RelaySettings, + auth?: AuthHandler, + ephemeral: boolean = false + ) { this.Id = uuid(); this.Address = addr; this.Settings = options; @@ -106,11 +114,14 @@ export class Connection { try { if (this.Info === undefined) { const u = new URL(this.Address); - const rsp = await fetch(`${u.protocol === "wss:" ? "https:" : "http:"}//${u.host}`, { - headers: { - accept: "application/nostr+json", - }, - }); + const rsp = await fetch( + `${u.protocol === "wss:" ? "https:" : "http:"}//${u.host}`, + { + headers: { + accept: "application/nostr+json", + }, + } + ); if (rsp.ok) { const data = await rsp.json(); for (const [k, v] of Object.entries(data)) { @@ -327,8 +338,8 @@ export class Connection { #ResetQueues() { //send EOSE on disconnect for active subs - this.ActiveRequests.forEach(v => this.OnEose?.(v)) - this.PendingRequests.forEach(v => this.OnEose?.(v[1])); + this.ActiveRequests.forEach((v) => this.OnEose?.(v)); + this.PendingRequests.forEach((v) => this.OnEose?.(v[1])); this.ActiveRequests.clear(); this.PendingRequests = []; @@ -343,12 +354,14 @@ export class Connection { this.CurrentState.avgLatency = this.Stats.Latency.length > 0 ? this.Stats.Latency.reduce((acc, v) => acc + v, 0) / - this.Stats.Latency.length + this.Stats.Latency.length : 0; this.CurrentState.disconnects = this.Stats.Disconnects; this.CurrentState.info = this.Info; this.CurrentState.id = this.Id; - this.CurrentState.pendingRequests = [...this.PendingRequests.map(a => a[1])]; + this.CurrentState.pendingRequests = [ + ...this.PendingRequests.map((a) => a[1]), + ]; this.CurrentState.activeRequests = [...this.ActiveRequests]; this.Stats.Latency = this.Stats.Latency.slice(-20); // trim this.HasStateChange = true; diff --git a/packages/nostr/webpack.config.js b/packages/nostr/webpack.config.js index 87d1585..a3b5063 100644 --- a/packages/nostr/webpack.config.js +++ b/packages/nostr/webpack.config.js @@ -27,5 +27,9 @@ module.exports = { output: { filename: "[name].js", path: `${__dirname}/dist`, + library: { + type: "umd", + name: "Nostr", + }, }, } diff --git a/yarn.lock b/yarn.lock index 1acb55e..465d766 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2470,7 +2470,7 @@ resolved "https://registry.yarnpkg.com/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz#b6725d5f4af24ace33b36fafd295136e75509f43" integrity sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA== -"@types/uuid@^9.0.0": +"@types/uuid@^9.0.1": version "9.0.1" resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.1.tgz#98586dc36aee8dacc98cc396dbca8d0429647aa6" integrity sha512-rFT3ak0/2trgvp4yYZo5iKFEPsET7vKydKF+VRCxlQ9bpheehyAJH89dAkaLEq/j/RZXJIqcgsmPJKUP1Z28HA==