fixing tests

This commit is contained in:
ennmichael 2023-04-08 21:14:08 +02:00
parent 8dfe0f25d4
commit 5f29ae0140
No known key found for this signature in database
GPG Key ID: 6E6E183431A26AF7
7 changed files with 40 additions and 19 deletions

View File

@ -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

View File

@ -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",

View File

@ -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);

View File

@ -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"

View File

@ -7,7 +7,10 @@ import { RelayInfo } from "./RelayInfo";
import { unwrap } from "./Util";
export type CustomHook = (state: Readonly<StateSnapshot>) => void;
export type AuthHandler = (challenge: string, relay: string) => Promise<RawEvent | undefined>;
export type AuthHandler = (
challenge: string,
relay: string
) => Promise<RawEvent | undefined>;
/**
* Relay settings
@ -15,7 +18,7 @@ export type AuthHandler = (challenge: string, relay: string) => Promise<RawEvent
export interface RelaySettings {
read: boolean;
write: boolean;
};
}
/**
* Snapshot of connection stats
@ -32,7 +35,7 @@ export interface StateSnapshot {
pendingRequests: Array<string>;
activeRequests: Array<string>;
id: string;
};
}
export class Connection {
Id: string;
@ -64,7 +67,12 @@ export class Connection {
EphemeralTimeout: ReturnType<typeof setTimeout> | 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;

View File

@ -27,5 +27,9 @@ module.exports = {
output: {
filename: "[name].js",
path: `${__dirname}/dist`,
library: {
type: "umd",
name: "Nostr",
},
},
}

View File

@ -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==