From 106ad437f19eec9cbcf1018210ef05aa56aecc93 Mon Sep 17 00:00:00 2001 From: Kieran Date: Wed, 6 Sep 2023 19:39:15 +0100 Subject: [PATCH] Use isomorphic-ws --- packages/system/package.json | 9 ++++++-- packages/system/src/connection.ts | 11 ++++----- packages/system/tests/node.ts | 14 ++++++++++++ yarn.lock | 37 ++++++++++++++++++++++++++----- 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 packages/system/tests/node.ts diff --git a/packages/system/package.json b/packages/system/package.json index dd30998e..434683af 100644 --- a/packages/system/package.json +++ b/packages/system/package.json @@ -1,6 +1,6 @@ { "name": "@snort/system", - "version": "1.0.18", + "version": "1.0.19", "description": "Snort nostr system package", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -20,10 +20,13 @@ "@peculiar/webcrypto": "^1.4.3", "@types/debug": "^4.1.8", "@types/jest": "^29.5.1", + "@types/node": "^20.5.9", "@types/uuid": "^9.0.2", + "@types/ws": "^8.5.5", "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", "ts-jest": "^29.1.0", + "ts-node": "^10.9.1", "typescript": "^5.2.2" }, "dependencies": { @@ -34,6 +37,8 @@ "@stablelib/xchacha20": "^1.0.1", "debug": "^4.3.4", "dexie": "^3.2.4", - "uuid": "^9.0.0" + "isomorphic-ws": "^5.0.0", + "uuid": "^9.0.0", + "ws": "^8.14.0" } } diff --git a/packages/system/src/connection.ts b/packages/system/src/connection.ts index 1650a46b..dc0f857a 100644 --- a/packages/system/src/connection.ts +++ b/packages/system/src/connection.ts @@ -1,5 +1,6 @@ import { v4 as uuid } from "uuid"; import debug from "debug"; +import WebSocket from "isomorphic-ws"; import { unwrap, ExternalStore, unixNowMs } from "@snort/shared"; import { DefaultConnectTimeout } from "./const"; @@ -138,7 +139,7 @@ export class Connection extends ExternalStore { this.#sendPendingRaw(); } - OnClose(e: CloseEvent) { + OnClose(e: WebSocket.CloseEvent) { if (this.ReconnectTimer) { clearTimeout(this.ReconnectTimer); this.ReconnectTimer = undefined; @@ -171,10 +172,10 @@ export class Connection extends ExternalStore { this.notifyChange(); } - OnMessage(e: MessageEvent) { + OnMessage(e: WebSocket.MessageEvent) { this.#activity = unixNowMs(); - if (e.data.length > 0) { - const msg = JSON.parse(e.data); + if ((e.data as string).length > 0) { + const msg = JSON.parse(e.data as string); const tag = msg[0]; switch (tag) { case "AUTH": { @@ -221,7 +222,7 @@ export class Connection extends ExternalStore { } } - OnError(e: Event) { + OnError(e: WebSocket.Event) { this.#log("Error: %O", e); this.notifyChange(); } diff --git a/packages/system/tests/node.ts b/packages/system/tests/node.ts new file mode 100644 index 00000000..8dd7862e --- /dev/null +++ b/packages/system/tests/node.ts @@ -0,0 +1,14 @@ +import {NostrSystem, SystemInterface} from ".."; + +const Relay = "wss://relay.snort.social/"; + +const system = new NostrSystem({}) as SystemInterface; + +async function test() { + await system.ConnectToRelay(Relay, {read: true, write: true}); + setTimeout(() => { + system.DisconnectRelay(Relay); + }, 1000); +} + +test().catch(console.error); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 4ec6f439..ed4e9b98 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2807,7 +2807,7 @@ __metadata: languageName: unknown linkType: soft -"@snort/shared@^1.0.4, @snort/shared@workspace:*, @snort/shared@workspace:packages/shared": +"@snort/shared@^1.0.5, @snort/shared@workspace:*, @snort/shared@workspace:packages/shared": version: 0.0.0-use.local resolution: "@snort/shared@workspace:packages/shared" dependencies: @@ -2826,15 +2826,15 @@ __metadata: version: 0.0.0-use.local resolution: "@snort/system-react@workspace:packages/system-react" dependencies: - "@snort/shared": ^1.0.4 - "@snort/system": ^1.0.17 + "@snort/shared": ^1.0.5 + "@snort/system": ^1.0.18 "@types/react": ^18.2.14 react: ^18.2.0 typescript: ^5.2.2 languageName: unknown linkType: soft -"@snort/system@^1.0.17, @snort/system@workspace:*, @snort/system@workspace:packages/system": +"@snort/system@^1.0.18, @snort/system@workspace:*, @snort/system@workspace:packages/system": version: 0.0.0-use.local resolution: "@snort/system@workspace:packages/system" dependencies: @@ -2843,18 +2843,23 @@ __metadata: "@noble/hashes": ^1.3.2 "@peculiar/webcrypto": ^1.4.3 "@scure/base": ^1.1.2 - "@snort/shared": ^1.0.4 + "@snort/shared": ^1.0.5 "@stablelib/xchacha20": ^1.0.1 "@types/debug": ^4.1.8 "@types/jest": ^29.5.1 + "@types/node": ^20.5.9 "@types/uuid": ^9.0.2 + "@types/ws": ^8.5.5 debug: ^4.3.4 dexie: ^3.2.4 + isomorphic-ws: ^5.0.0 jest: ^29.5.0 jest-environment-jsdom: ^29.5.0 ts-jest: ^29.1.0 + ts-node: ^10.9.1 typescript: ^5.2.2 uuid: ^9.0.0 + ws: ^8.14.0 languageName: unknown linkType: soft @@ -3463,6 +3468,13 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:^20.5.9": + version: 20.5.9 + resolution: "@types/node@npm:20.5.9" + checksum: 717490e94131722144878b4ca1a963ede1673bb8f2ef78c2f5b50b918df6dc9b35e7f8283e5c2a7a9f137730f7c08dc6228e53d4494a94c9ee16881e6ce6caed + languageName: node + linkType: hard + "@types/normalize-package-data@npm:^2.4.0": version: 2.4.1 resolution: "@types/normalize-package-data@npm:2.4.1" @@ -14651,6 +14663,21 @@ __metadata: languageName: node linkType: hard +"ws@npm:^8.14.0": + version: 8.14.0 + resolution: "ws@npm:8.14.0" + peerDependencies: + bufferutil: ^4.0.1 + utf-8-validate: ">=5.0.2" + peerDependenciesMeta: + bufferutil: + optional: true + utf-8-validate: + optional: true + checksum: dd91d055396c42552d8e2d26a0ab10221e73ca356de3db9109e337b8d9df216a0a308ace46a5e0520ed18ffcae3f54c2fa45a96711f94a063c816ef13a30b700 + languageName: node + linkType: hard + "xml-name-validator@npm:^4.0.0": version: 4.0.0 resolution: "xml-name-validator@npm:4.0.0"