@sabaaa1/common 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@hazae41/echalote"),m=require("@hazae41/cadenas"),S=require("@hazae41/fleche"),p=require("@hazae41/ed25519"),b=require("@hazae41/x25519"),g=require("@hazae41/chacha20poly1305"),y=require("@hazae41/sha1"),u=require("@brumewallet/wallet.wasm"),E=require("./WebSocketDuplex.cjs"),a=require("../../error-handling/logger.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/activities.types.cjs");const _=require("../../types/routing.types.cjs"),x=require("../../functions/utils/serialize.utils.cjs"),k=require("buffer");typeof globalThis.Buffer>"u"&&(globalThis.Buffer=k.Buffer);class w{static initializationPromise;cryptoInitialized=!1;socket;tor;consensus;abortController;middles=[];exits=[];circuits=[];circuitIndex=0;CIRCUIT_POOL_SIZE=5;TOP_RELAY_COUNT=50;static async create(){return this.initializationPromise?this.initializationPromise:(this.initializationPromise=(async()=>{try{const e=new w;return await e.initialize(),e}catch(e){throw this.initializationPromise=void 0,e}})(),this.initializationPromise)}async initialize(){await this.initializeCryptoOrThrow(),this.socket=await this.createSnowflakeSocketOrThrow(),this.tor=await this.createTorClientOrThrow(this.socket),await this.loadConsensus();const e=Array(this.CIRCUIT_POOL_SIZE).fill(null).map((i,r)=>this.createNewCircuit().catch(s=>(a.Logger.error(`Failed to build initial circuit ${r}`,s),null))),t=await Promise.all(e);if(this.circuits=t.filter(i=>i!==null),this.circuits.length===0)throw new Error("Failed to build any circuits")}async initializeCryptoOrThrow(){this.cryptoInitialized||(await u.WalletWasm.initBundled(),y.Sha1.set(y.Sha1.fromWasm(u.WalletWasm)),p.Ed25519.set(await p.Ed25519.fromNativeOrWasm(u.WalletWasm)),b.X25519.set(b.X25519.fromWasm(u.WalletWasm)),g.ChaCha20Poly1305.set(g.ChaCha20Poly1305.fromWasm(u.WalletWasm)),this.cryptoInitialized=!0)}async createSnowflakeSocketOrThrow(){const e=new WebSocket("wss://snowflake.torproject.net/");return e.binaryType="arraybuffer",await new Promise((t,i)=>{e.onopen=t,e.onerror=i}),e}async createTorClientOrThrow(e){const t=new E.WebSocketDuplex(e),i=h.createSnowflakeStream(t),r=new h.TorClientDuplex;return this.abortController=new AbortController,i.outer.readable.pipeTo(r.inner.writable,{signal:this.abortController.signal}).catch(s=>{a.Logger.error("Pipe to Tor failed:",s)}),r.inner.readable.pipeTo(i.outer.writable,{signal:this.abortController.signal}).catch(s=>{a.Logger.error("Pipe to TCP failed:",s)}),await r.waitOrThrow(),r}async loadConsensus(){if(!this.tor)throw new Error("Tor client not initialized");const e=await this.tor.createOrThrow();if(this.consensus=await h.Consensus.fetchOrThrow(e),this.middles=this.consensus.microdescs.filter(t=>t.flags.includes("Fast")&&t.flags.includes("Stable")&&t.flags.includes("V2Dir")),this.middles.sort((t,i)=>{const r=Number(t.bandwidth?.Bandwidth)||0;return(Number(i.bandwidth?.Bandwidth)||0)-r}),this.middles=this.middles.slice(0,this.TOP_RELAY_COUNT),this.exits=this.consensus.microdescs.filter(t=>t.flags.includes("Fast")&&t.flags.includes("Stable")&&t.flags.includes("Exit")&&!t.flags.includes("BadExit")),this.exits.sort((t,i)=>{const r=Number(t.bandwidth?.Bandwidth)||0;return(Number(i.bandwidth?.Bandwidth)||0)-r}),this.exits=this.exits.slice(0,this.TOP_RELAY_COUNT),this.middles.length===0||this.exits.length===0)throw new Error("No suitable relays found")}async createNewCircuit(){if(!this.tor)throw new Error("Tor client not initialized");const e=await this.tor.createOrThrow();try{const t=this.middles[Math.floor(Math.random()*this.middles.length)];if(!t)throw new Error("No middle relays available");const i=await h.Consensus.Microdesc.fetchOrThrow(e,t);await e.extendOrThrow(i);const r=this.exits[Math.floor(Math.random()*this.exits.length)];if(!r)throw new Error("No exit relays available");const s=await h.Consensus.Microdesc.fetchOrThrow(e,r);return await e.extendOrThrow(s),e}catch(t){throw a.Logger.error("Failed to build circuit",t),t}}async openTlsConnectionOrThrow(e,t,i=443){const r=await e.openOrThrow(t,i),s=[m.Ciphers.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,m.Ciphers.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384],n=new m.TlsClientDuplex({host_name:t,ciphers:s});return r.outer.readable.pipeTo(n.inner.writable).catch(()=>{}),n.inner.readable.pipeTo(r.outer.writable).catch(()=>{}),n.outer}async fetchViaTor(e,t,i,r){if(this.circuits.length===0)throw new Error("Circuit pool is empty, client may be closed or failed to init.");const s=new URL(t),n=this.circuits.length;let C;for(let f=0;f<n;f+=1){const c=(this.circuitIndex+f)%n,T=this.circuits[c];if(T)try{const d=await this.openTlsConnectionOrThrow(T,s.hostname,443),o={Host:s.hostname,Connection:"keep-alive",...r},l={method:e,stream:d,headers:o};i&&e!==_.HttpMethod.GET&&(l.body=typeof i=="string"?i:x.CustomJSONStringify(i),!o["Content-Type"]&&!o["content-type"]&&(o["Content-Type"]="application/json"));const O=await S.fetch(t,l);return this.circuitIndex=c,O}catch(d){C=d;const o=d.message?.toLowerCase()||"";a.Logger.error(`[TorClient] Circuit ${c} failed:`,o),(o.includes("circuit")||o.includes("destroyed")||o.includes("closed"))&&this.createNewCircuit().then(l=>{this.circuits[c]=l,a.Logger.log(`[TorClient] Replaced circuit ${c}`)}).catch(l=>{a.Logger.error(`[TorClient] Failed to replace circuit ${c}`,l)})}}throw new Error(`[TorClient] All ${n} circuits failed. Last error: ${C?.message||"unknown"}`)}async close(){this.abortController&&this.abortController.abort(),this.socket&&(this.socket.readyState===WebSocket.OPEN||this.socket.readyState===WebSocket.CONNECTING)&&this.socket.close(),this.circuits=[],this.socket=void 0,this.tor=void 0,this.consensus=void 0,this.abortController=void 0,w.initializationPromise=void 0}}exports.TorClient=w;
1
+ "use strict";var E=Object.create;var p=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var N=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of x(t))!P.call(o,i)&&i!==e&&p(o,i,{get:()=>t[i],enumerable:!(r=_(t,i))||r.enumerable});return o};var z=(o,t,e)=>(e=o!=null?E(k(o)):{},N(t||!o||!o.__esModule?p(e,"default",{value:o,enumerable:!0}):e,o));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@hazae41/echalote"),C=require("@hazae41/cadenas"),L=require("@hazae41/fleche"),b=require("@hazae41/ed25519"),g=require("@hazae41/x25519"),y=require("@hazae41/chacha20poly1305"),O=require("@hazae41/sha1"),q=require("./WebSocketDuplex.cjs"),c=require("../../error-handling/logger.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/activities.types.cjs");const I=require("../../types/routing.types.cjs"),A=require("../../functions/utils/serialize.utils.cjs"),B=require("buffer");typeof globalThis.Buffer>"u"&&(globalThis.Buffer=B.Buffer);class w{static initializationPromise;cryptoInitialized=!1;socket;tor;consensus;abortController;middles=[];exits=[];circuits=[];circuitIndex=0;CIRCUIT_POOL_SIZE=5;TOP_RELAY_COUNT=50;static async create(){return this.initializationPromise?this.initializationPromise:(this.initializationPromise=(async()=>{try{const t=new w;return await t.initialize(),t}catch(t){throw this.initializationPromise=void 0,t}})(),this.initializationPromise)}async initialize(){await this.initializeCryptoOrThrow(),this.socket=await this.createSnowflakeSocketOrThrow(),this.tor=await this.createTorClientOrThrow(this.socket),await this.loadConsensus();const t=Array(this.CIRCUIT_POOL_SIZE).fill(null).map((r,i)=>this.createNewCircuit().catch(s=>(c.Logger.error(`Failed to build initial circuit ${i}`,s),null))),e=await Promise.all(t);if(this.circuits=e.filter(r=>r!==null),this.circuits.length===0)throw new Error("Failed to build any circuits")}async initializeCryptoOrThrow(){if(this.cryptoInitialized)return;const{WalletWasm:t}=await import("@brumewallet/wallet.wasm");await t.initBundled(),O.Sha1.set(O.Sha1.fromWasm(t)),b.Ed25519.set(await b.Ed25519.fromNativeOrWasm(t)),g.X25519.set(g.X25519.fromWasm(t)),y.ChaCha20Poly1305.set(y.ChaCha20Poly1305.fromWasm(t)),this.cryptoInitialized=!0}async createSnowflakeSocketOrThrow(){const t=new WebSocket("wss://snowflake.torproject.net/");return t.binaryType="arraybuffer",await new Promise((e,r)=>{t.onopen=e,t.onerror=r}),t}async createTorClientOrThrow(t){const e=new q.WebSocketDuplex(t),r=u.createSnowflakeStream(e),i=new u.TorClientDuplex;return this.abortController=new AbortController,r.outer.readable.pipeTo(i.inner.writable,{signal:this.abortController.signal}).catch(s=>{c.Logger.error("Pipe to Tor failed:",s)}),i.inner.readable.pipeTo(r.outer.writable,{signal:this.abortController.signal}).catch(s=>{c.Logger.error("Pipe to TCP failed:",s)}),await i.waitOrThrow(),i}async loadConsensus(){if(!this.tor)throw new Error("Tor client not initialized");const t=await this.tor.createOrThrow();if(this.consensus=await u.Consensus.fetchOrThrow(t),this.middles=this.consensus.microdescs.filter(e=>e.flags.includes("Fast")&&e.flags.includes("Stable")&&e.flags.includes("V2Dir")),this.middles.sort((e,r)=>{const i=Number(e.bandwidth?.Bandwidth)||0;return(Number(r.bandwidth?.Bandwidth)||0)-i}),this.middles=this.middles.slice(0,this.TOP_RELAY_COUNT),this.exits=this.consensus.microdescs.filter(e=>e.flags.includes("Fast")&&e.flags.includes("Stable")&&e.flags.includes("Exit")&&!e.flags.includes("BadExit")),this.exits.sort((e,r)=>{const i=Number(e.bandwidth?.Bandwidth)||0;return(Number(r.bandwidth?.Bandwidth)||0)-i}),this.exits=this.exits.slice(0,this.TOP_RELAY_COUNT),this.middles.length===0||this.exits.length===0)throw new Error("No suitable relays found")}async createNewCircuit(){if(!this.tor)throw new Error("Tor client not initialized");const t=await this.tor.createOrThrow();try{const e=this.middles[Math.floor(Math.random()*this.middles.length)];if(!e)throw new Error("No middle relays available");const r=await u.Consensus.Microdesc.fetchOrThrow(t,e);await t.extendOrThrow(r);const i=this.exits[Math.floor(Math.random()*this.exits.length)];if(!i)throw new Error("No exit relays available");const s=await u.Consensus.Microdesc.fetchOrThrow(t,i);return await t.extendOrThrow(s),t}catch(e){throw c.Logger.error("Failed to build circuit",e),e}}async openTlsConnectionOrThrow(t,e,r=443){const i=await t.openOrThrow(e,r),s=[C.Ciphers.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,C.Ciphers.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384],a=new C.TlsClientDuplex({host_name:e,ciphers:s});return i.outer.readable.pipeTo(a.inner.writable).catch(()=>{}),a.inner.readable.pipeTo(i.outer.writable).catch(()=>{}),a.outer}async fetchViaTor(t,e,r,i){if(this.circuits.length===0)throw new Error("Circuit pool is empty, client may be closed or failed to init.");const s=new URL(e),a=this.circuits.length;let T;for(let f=0;f<a;f+=1){const l=(this.circuitIndex+f)%a,m=this.circuits[l];if(m)try{const d=await this.openTlsConnectionOrThrow(m,s.hostname,443),n={Host:s.hostname,Connection:"keep-alive",...i},h={method:t,stream:d,headers:n};r&&t!==I.HttpMethod.GET&&(h.body=typeof r=="string"?r:A.CustomJSONStringify(r),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json"));const S=await L.fetch(e,h);return this.circuitIndex=l,S}catch(d){T=d;const n=d.message?.toLowerCase()||"";c.Logger.error(`[TorClient] Circuit ${l} failed:`,n),(n.includes("circuit")||n.includes("destroyed")||n.includes("closed"))&&this.createNewCircuit().then(h=>{this.circuits[l]=h,c.Logger.log(`[TorClient] Replaced circuit ${l}`)}).catch(h=>{c.Logger.error(`[TorClient] Failed to replace circuit ${l}`,h)})}}throw new Error(`[TorClient] All ${a} circuits failed. Last error: ${T?.message||"unknown"}`)}async close(){this.abortController&&this.abortController.abort(),this.socket&&(this.socket.readyState===WebSocket.OPEN||this.socket.readyState===WebSocket.CONNECTING)&&this.socket.close(),this.circuits=[],this.socket=void 0,this.tor=void 0,this.consensus=void 0,this.abortController=void 0,w.initializationPromise=void 0}}exports.TorClient=w;
@@ -1,21 +1,20 @@
1
- import { createSnowflakeStream as S, TorClientDuplex as E, Consensus as u } from "@hazae41/echalote";
2
- import { Ciphers as T, TlsClientDuplex as _ } from "@hazae41/cadenas";
3
- import { fetch as x } from "@hazae41/fleche";
4
- import { Ed25519 as C } from "@hazae41/ed25519";
5
- import { X25519 as b } from "@hazae41/x25519";
6
- import { ChaCha20Poly1305 as y } from "@hazae41/chacha20poly1305";
7
- import { Sha1 as O } from "@hazae41/sha1";
8
- import { WalletWasm as h } from "@brumewallet/wallet.wasm";
9
- import { WebSocketDuplex as k } from "./WebSocketDuplex.mjs";
1
+ import { createSnowflakeStream as g, TorClientDuplex as S, Consensus as w } from "@hazae41/echalote";
2
+ import { Ciphers as p, TlsClientDuplex as E } from "@hazae41/cadenas";
3
+ import { fetch as _ } from "@hazae41/fleche";
4
+ import { Ed25519 as T } from "@hazae41/ed25519";
5
+ import { X25519 as C } from "@hazae41/x25519";
6
+ import { ChaCha20Poly1305 as b } from "@hazae41/chacha20poly1305";
7
+ import { Sha1 as y } from "@hazae41/sha1";
8
+ import { WebSocketDuplex as x } from "./WebSocketDuplex.mjs";
10
9
  import { Logger as a } from "../../error-handling/logger.mjs";
11
10
  import "../../types/circom-data.types.mjs";
12
11
  import "../../types/transactions.types.mjs";
13
12
  import "../../types/activities.types.mjs";
14
- import { HttpMethod as P } from "../../types/routing.types.mjs";
15
- import { CustomJSONStringify as N } from "../../functions/utils/serialize.utils.mjs";
16
- import { Buffer as z } from "buffer";
17
- typeof globalThis.Buffer > "u" && (globalThis.Buffer = z);
18
- class m {
13
+ import { HttpMethod as k } from "../../types/routing.types.mjs";
14
+ import { CustomJSONStringify as P } from "../../functions/utils/serialize.utils.mjs";
15
+ import { Buffer as N } from "buffer";
16
+ typeof globalThis.Buffer > "u" && (globalThis.Buffer = N);
17
+ class u {
19
18
  static initializationPromise;
20
19
  cryptoInitialized = !1;
21
20
  socket;
@@ -31,32 +30,35 @@ class m {
31
30
  static async create() {
32
31
  return this.initializationPromise ? this.initializationPromise : (this.initializationPromise = (async () => {
33
32
  try {
34
- const i = new m();
35
- return await i.initialize(), i;
36
- } catch (i) {
37
- throw this.initializationPromise = void 0, i;
33
+ const t = new u();
34
+ return await t.initialize(), t;
35
+ } catch (t) {
36
+ throw this.initializationPromise = void 0, t;
38
37
  }
39
38
  })(), this.initializationPromise);
40
39
  }
41
40
  async initialize() {
42
41
  await this.initializeCryptoOrThrow(), this.socket = await this.createSnowflakeSocketOrThrow(), this.tor = await this.createTorClientOrThrow(this.socket), await this.loadConsensus();
43
- const i = Array(this.CIRCUIT_POOL_SIZE).fill(null).map(
42
+ const t = Array(this.CIRCUIT_POOL_SIZE).fill(null).map(
44
43
  (r, e) => this.createNewCircuit().catch((o) => (a.error(`Failed to build initial circuit ${e}`, o), null))
45
- ), t = await Promise.all(i);
46
- if (this.circuits = t.filter((r) => r !== null), this.circuits.length === 0)
44
+ ), i = await Promise.all(t);
45
+ if (this.circuits = i.filter((r) => r !== null), this.circuits.length === 0)
47
46
  throw new Error("Failed to build any circuits");
48
47
  }
49
48
  async initializeCryptoOrThrow() {
50
- this.cryptoInitialized || (await h.initBundled(), O.set(O.fromWasm(h)), C.set(await C.fromNativeOrWasm(h)), b.set(b.fromWasm(h)), y.set(y.fromWasm(h)), this.cryptoInitialized = !0);
49
+ if (this.cryptoInitialized)
50
+ return;
51
+ const { WalletWasm: t } = await import("@brumewallet/wallet.wasm");
52
+ await t.initBundled(), y.set(y.fromWasm(t)), T.set(await T.fromNativeOrWasm(t)), C.set(C.fromWasm(t)), b.set(b.fromWasm(t)), this.cryptoInitialized = !0;
51
53
  }
52
54
  async createSnowflakeSocketOrThrow() {
53
- const i = new WebSocket("wss://snowflake.torproject.net/");
54
- return i.binaryType = "arraybuffer", await new Promise((t, r) => {
55
- i.onopen = t, i.onerror = r;
56
- }), i;
55
+ const t = new WebSocket("wss://snowflake.torproject.net/");
56
+ return t.binaryType = "arraybuffer", await new Promise((i, r) => {
57
+ t.onopen = i, t.onerror = r;
58
+ }), t;
57
59
  }
58
- async createTorClientOrThrow(i) {
59
- const t = new k(i), r = S(t), e = new E();
60
+ async createTorClientOrThrow(t) {
61
+ const i = new x(t), r = g(i), e = new S();
60
62
  return this.abortController = new AbortController(), r.outer.readable.pipeTo(e.inner.writable, { signal: this.abortController.signal }).catch((o) => {
61
63
  a.error("Pipe to Tor failed:", o);
62
64
  }), e.inner.readable.pipeTo(r.outer.writable, { signal: this.abortController.signal }).catch((o) => {
@@ -66,16 +68,16 @@ class m {
66
68
  async loadConsensus() {
67
69
  if (!this.tor)
68
70
  throw new Error("Tor client not initialized");
69
- const i = await this.tor.createOrThrow();
70
- if (this.consensus = await u.fetchOrThrow(i), this.middles = this.consensus.microdescs.filter(
71
- (t) => t.flags.includes("Fast") && t.flags.includes("Stable") && t.flags.includes("V2Dir")
72
- ), this.middles.sort((t, r) => {
73
- const e = Number(t.bandwidth?.Bandwidth) || 0;
71
+ const t = await this.tor.createOrThrow();
72
+ if (this.consensus = await w.fetchOrThrow(t), this.middles = this.consensus.microdescs.filter(
73
+ (i) => i.flags.includes("Fast") && i.flags.includes("Stable") && i.flags.includes("V2Dir")
74
+ ), this.middles.sort((i, r) => {
75
+ const e = Number(i.bandwidth?.Bandwidth) || 0;
74
76
  return (Number(r.bandwidth?.Bandwidth) || 0) - e;
75
77
  }), this.middles = this.middles.slice(0, this.TOP_RELAY_COUNT), this.exits = this.consensus.microdescs.filter(
76
- (t) => t.flags.includes("Fast") && t.flags.includes("Stable") && t.flags.includes("Exit") && !t.flags.includes("BadExit")
77
- ), this.exits.sort((t, r) => {
78
- const e = Number(t.bandwidth?.Bandwidth) || 0;
78
+ (i) => i.flags.includes("Fast") && i.flags.includes("Stable") && i.flags.includes("Exit") && !i.flags.includes("BadExit")
79
+ ), this.exits.sort((i, r) => {
80
+ const e = Number(i.bandwidth?.Bandwidth) || 0;
79
81
  return (Number(r.bandwidth?.Bandwidth) || 0) - e;
80
82
  }), this.exits = this.exits.slice(0, this.TOP_RELAY_COUNT), this.middles.length === 0 || this.exits.length === 0)
81
83
  throw new Error("No suitable relays found");
@@ -83,52 +85,52 @@ class m {
83
85
  async createNewCircuit() {
84
86
  if (!this.tor)
85
87
  throw new Error("Tor client not initialized");
86
- const i = await this.tor.createOrThrow();
88
+ const t = await this.tor.createOrThrow();
87
89
  try {
88
- const t = this.middles[Math.floor(Math.random() * this.middles.length)];
89
- if (!t)
90
+ const i = this.middles[Math.floor(Math.random() * this.middles.length)];
91
+ if (!i)
90
92
  throw new Error("No middle relays available");
91
- const r = await u.Microdesc.fetchOrThrow(i, t);
92
- await i.extendOrThrow(r);
93
+ const r = await w.Microdesc.fetchOrThrow(t, i);
94
+ await t.extendOrThrow(r);
93
95
  const e = this.exits[Math.floor(Math.random() * this.exits.length)];
94
96
  if (!e)
95
97
  throw new Error("No exit relays available");
96
- const o = await u.Microdesc.fetchOrThrow(i, e);
97
- return await i.extendOrThrow(o), i;
98
- } catch (t) {
99
- throw a.error("Failed to build circuit", t), t;
98
+ const o = await w.Microdesc.fetchOrThrow(t, e);
99
+ return await t.extendOrThrow(o), t;
100
+ } catch (i) {
101
+ throw a.error("Failed to build circuit", i), i;
100
102
  }
101
103
  }
102
- async openTlsConnectionOrThrow(i, t, r = 443) {
103
- const e = await i.openOrThrow(t, r), o = [T.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, T.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384], n = new _({ host_name: t, ciphers: o });
104
+ async openTlsConnectionOrThrow(t, i, r = 443) {
105
+ const e = await t.openOrThrow(i, r), o = [p.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, p.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384], n = new E({ host_name: i, ciphers: o });
104
106
  return e.outer.readable.pipeTo(n.inner.writable).catch(() => {
105
107
  }), n.inner.readable.pipeTo(e.outer.writable).catch(() => {
106
108
  }), n.outer;
107
109
  }
108
- async fetchViaTor(i, t, r, e) {
110
+ async fetchViaTor(t, i, r, e) {
109
111
  if (this.circuits.length === 0)
110
112
  throw new Error("Circuit pool is empty, client may be closed or failed to init.");
111
- const o = new URL(t), n = this.circuits.length;
112
- let f;
113
- for (let w = 0; w < n; w += 1) {
114
- const c = (this.circuitIndex + w) % n, p = this.circuits[c];
115
- if (p)
113
+ const o = new URL(i), n = this.circuits.length;
114
+ let m;
115
+ for (let d = 0; d < n; d += 1) {
116
+ const c = (this.circuitIndex + d) % n, f = this.circuits[c];
117
+ if (f)
116
118
  try {
117
- const d = await this.openTlsConnectionOrThrow(p, o.hostname, 443), s = {
119
+ const h = await this.openTlsConnectionOrThrow(f, o.hostname, 443), s = {
118
120
  Host: o.hostname,
119
121
  Connection: "keep-alive",
120
122
  ...e
121
123
  }, l = {
122
- method: i,
123
- stream: d,
124
+ method: t,
125
+ stream: h,
124
126
  headers: s
125
127
  };
126
- r && i !== P.GET && (l.body = typeof r == "string" ? r : N(r), !s["Content-Type"] && !s["content-type"] && (s["Content-Type"] = "application/json"));
127
- const g = await x(t, l);
128
- return this.circuitIndex = c, g;
129
- } catch (d) {
130
- f = d;
131
- const s = d.message?.toLowerCase() || "";
128
+ r && t !== k.GET && (l.body = typeof r == "string" ? r : P(r), !s["Content-Type"] && !s["content-type"] && (s["Content-Type"] = "application/json"));
129
+ const O = await _(i, l);
130
+ return this.circuitIndex = c, O;
131
+ } catch (h) {
132
+ m = h;
133
+ const s = h.message?.toLowerCase() || "";
132
134
  a.error(`[TorClient] Circuit ${c} failed:`, s), (s.includes("circuit") || s.includes("destroyed") || s.includes("closed")) && this.createNewCircuit().then((l) => {
133
135
  this.circuits[c] = l, a.log(`[TorClient] Replaced circuit ${c}`);
134
136
  }).catch((l) => {
@@ -136,12 +138,12 @@ class m {
136
138
  });
137
139
  }
138
140
  }
139
- throw new Error(`[TorClient] All ${n} circuits failed. Last error: ${f?.message || "unknown"}`);
141
+ throw new Error(`[TorClient] All ${n} circuits failed. Last error: ${m?.message || "unknown"}`);
140
142
  }
141
143
  async close() {
142
- this.abortController && this.abortController.abort(), this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING) && this.socket.close(), this.circuits = [], this.socket = void 0, this.tor = void 0, this.consensus = void 0, this.abortController = void 0, m.initializationPromise = void 0;
144
+ this.abortController && this.abortController.abort(), this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING) && this.socket.close(), this.circuits = [], this.socket = void 0, this.tor = void 0, this.consensus = void 0, this.abortController = void 0, u.initializationPromise = void 0;
143
145
  }
144
146
  }
145
147
  export {
146
- m as TorClient
148
+ u as TorClient
147
149
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sabaaa1/common",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "homepage": "hinkal.pro",
5
5
  "author": {
6
6
  "name": "Hinkal Protocol"
@@ -1 +1 @@
1
- "use strict";const e=""+(typeof document>"u"?require("url").pathToFileURL(__dirname+"/../../assets/snarkjsWorkerLauncher-C7k0eivz.js").href:new URL("../../assets/snarkjsWorkerLauncher-C7k0eivz.js",document.currentScript&&document.currentScript.src||document.baseURI).href);module.exports=e;
1
+ "use strict";const e=""+(typeof document>"u"?require("url").pathToFileURL(__dirname+"/../../assets/snarkjsWorkerLauncher-Bi0P4lHT.js").href:new URL("../../assets/snarkjsWorkerLauncher-Bi0P4lHT.js",document.currentScript&&document.currentScript.src||document.baseURI).href);module.exports=e;
@@ -1,4 +1,4 @@
1
- const r = "" + new URL("../../assets/snarkjsWorkerLauncher-C7k0eivz.js", import.meta.url).href;
1
+ const r = "" + new URL("../../assets/snarkjsWorkerLauncher-Bi0P4lHT.js", import.meta.url).href;
2
2
  export {
3
3
  r as default
4
4
  };