@samouraiwallet/electrum-client
Advanced tools
Comparing version 1.5.0 to 1.5.1
# Changelog | ||
## 1.5.1 (2024-09-29) | ||
- Fixed socket connecting preemptively | ||
## 1.5.0 (2024-09-27) | ||
@@ -4,0 +7,0 @@ - Removed unnecessary boilerplate (TlsSocketWrapper) |
@@ -44,5 +44,5 @@ import { Client } from "./lib/client.js"; | ||
*/ | ||
static async createClient(params) { | ||
static createClient(params) { | ||
const client = new ElectrumClient(params.port, params.host, params.protocol, params.callbacks); | ||
return await client.initElectrum(params.electrumConfig, params.persistencePolicy); | ||
return client.initElectrum(params.electrumConfig, params.persistencePolicy); | ||
} | ||
@@ -49,0 +49,0 @@ /** |
@@ -8,2 +8,3 @@ import { EventEmitter } from "node:events"; | ||
private mp; | ||
private status; | ||
private readonly protocol; | ||
@@ -17,2 +18,3 @@ private conn; | ||
protected connect(): Promise<void>; | ||
private connectSocket; | ||
close(): void; | ||
@@ -19,0 +21,0 @@ protected request<T>(method: string, params: ElectrumRequestParams<T>): Promise<unknown>; |
@@ -11,2 +11,3 @@ import { EventEmitter } from "node:events"; | ||
this.port = port; | ||
this.status = 0; | ||
this.protocol = protocol; | ||
@@ -27,10 +28,5 @@ this.callback_message_queue = new Map(); | ||
this.protocol === "tls" || this.protocol === "ssl" | ||
? tls.connect({ host: this.host, port: this.port, rejectUnauthorized: false }, () => { | ||
this.conn?.setTimeout(0); | ||
this.onConnect(); | ||
}) | ||
: net.connect({ host: this.host, port: this.port }, () => { | ||
this.conn?.setTimeout(0); | ||
this.onConnect(); | ||
}); | ||
? // @ts-expect-error | ||
new tls.TLSSocket() | ||
: new net.Socket(); | ||
this.conn.setTimeout(TIMEOUT); | ||
@@ -40,2 +36,6 @@ this.conn.setEncoding("utf8"); | ||
this.conn.setNoDelay(true); | ||
this.conn.on("connect", () => { | ||
this.conn?.setTimeout(0); | ||
this.onConnect(); | ||
}); | ||
this.conn.on("close", () => { | ||
@@ -51,9 +51,20 @@ this.onClose(); | ||
}); | ||
this.status = 0; | ||
} | ||
connect() { | ||
return new Promise((resolve) => { | ||
if (this.conn && this.conn.readyState === "open") { | ||
return resolve(); | ||
if (this.conn) { | ||
if (this.status === 1) { | ||
return Promise.resolve(); | ||
} | ||
this.conn?.once(this.protocol === "tcp" ? "connect" : "secureConnect", () => { | ||
this.status = 1; | ||
return this.connectSocket(this.conn, this.port, this.host); | ||
} | ||
return Promise.reject(new Error("There is no socket to initialize connection on.")); | ||
} | ||
connectSocket(conn, port, host) { | ||
return new Promise((resolve, reject) => { | ||
const errorHandler = (e) => reject(e); | ||
conn.on("error", errorHandler); | ||
conn.connect(port, host, () => { | ||
conn.removeListener("error", errorHandler); | ||
resolve(); | ||
@@ -64,3 +75,3 @@ }); | ||
close() { | ||
if (this.conn && this.conn.readyState === "closed") { | ||
if (this.status === 0) { | ||
return; | ||
@@ -72,3 +83,3 @@ } | ||
request(method, params) { | ||
if (this.conn && this.conn.readyState === "closed") { | ||
if (this.status === 0) { | ||
return Promise.reject(new Error("Connection to server lost, please retry")); | ||
@@ -84,3 +95,3 @@ } | ||
requestBatch(method, params, secondParam) { | ||
if (this.conn && this.conn.readyState === "closed") { | ||
if (this.status === 0) { | ||
return Promise.reject(new Error("Connection to server lost, please retry")); | ||
@@ -87,0 +98,0 @@ } |
{ | ||
"name": "@samouraiwallet/electrum-client", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=18.6.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38061
691