1+ // Manual Gun.js data query tool using proper WebSocket connections
2+ import Gun from 'gun' ;
3+
4+ async function queryGunData ( ) {
5+ console . log ( '🔍 Manual Gun.js WebSocket Query Tool' ) ;
6+ console . log ( '━' . repeat ( 50 ) ) ;
7+
8+ // Initialize Gun.js with your relay (using the correct port)
9+ const gun = Gun ( {
10+ peers : [ 'http://nostalgiagame.go.ro:30878/gun' ] , // Using port 30878 like your relay
11+ retry : 1000 ,
12+ timeout : 15000 ,
13+ localStorage : false // Disable to avoid local cache interference
14+ } ) ;
15+
16+ console . log ( '🔗 Connecting to Gun.js relay: http://nostalgiagame.go.ro:30878/gun' ) ;
17+ console . log ( '⏳ Waiting for WebSocket connection to establish...\n' ) ;
18+
19+ // Wait for connection to establish
20+ await new Promise ( resolve => setTimeout ( resolve , 3000 ) ) ;
21+
22+ return new Promise ( ( resolve ) => {
23+ let hasReceivedData = false ;
24+
25+ // Set a timeout to resolve if no data is received
26+ const timeout = setTimeout ( ( ) => {
27+ if ( ! hasReceivedData ) {
28+ console . log ( '⏰ Timeout reached - no data received from Gun.js' ) ;
29+ console . log ( 'This could mean:' ) ;
30+ console . log ( ' • The relay is not accessible' ) ;
31+ console . log ( ' • No data exists in the namespace' ) ;
32+ console . log ( ' • Network/firewall issues' ) ;
33+ resolve ( ) ;
34+ }
35+ } , 10000 ) ;
36+
37+ // Query the namespace root
38+ console . log ( '📊 Querying namespace root: dig-nat-tools-test' ) ;
39+ gun . get ( 'dig-nat-tools-test' ) . once ( ( data ) => {
40+ hasReceivedData = true ;
41+ clearTimeout ( timeout ) ;
42+
43+ console . log ( '📋 Namespace root data received:' ) ;
44+ console . log ( ' Raw data:' , JSON . stringify ( data , null , 2 ) ) ;
45+
46+ if ( data ) {
47+ const keys = Object . keys ( data ) . filter ( key => key !== '_' ) ;
48+ console . log ( ' Keys (excluding Gun.js metadata):' , keys ) ;
49+
50+ if ( keys . includes ( 'hosts' ) ) {
51+ console . log ( '\n🏠 Found hosts key! Querying hosts data...' ) ;
52+
53+ // Query hosts data
54+ gun . get ( 'dig-nat-tools-test' ) . get ( 'hosts' ) . once ( ( hostsData ) => {
55+ console . log ( '📋 Hosts data received:' ) ;
56+ console . log ( ' Raw hosts data:' , JSON . stringify ( hostsData , null , 2 ) ) ;
57+
58+ if ( hostsData ) {
59+ const hostKeys = Object . keys ( hostsData ) . filter ( key => key !== '_' ) ;
60+ console . log ( ' Host keys:' , hostKeys ) ;
61+
62+ // Query each individual host
63+ let processedHosts = 0 ;
64+ hostKeys . forEach ( ( hostKey , index ) => {
65+ console . log ( `\n🔍 Querying individual host: ${ hostKey } ` ) ;
66+
67+ gun . get ( 'dig-nat-tools-test' ) . get ( 'hosts' ) . get ( hostKey ) . once ( ( hostData ) => {
68+ console . log ( `📄 Host ${ hostKey } data:` , JSON . stringify ( hostData , null , 2 ) ) ;
69+
70+ processedHosts ++ ;
71+ if ( processedHosts === hostKeys . length ) {
72+ console . log ( '\n✅ All hosts processed' ) ;
73+ resolve ( ) ;
74+ }
75+ } ) ;
76+ } ) ;
77+
78+ if ( hostKeys . length === 0 ) {
79+ console . log ( '❌ No host keys found in hosts data' ) ;
80+ resolve ( ) ;
81+ }
82+ } else {
83+ console . log ( '❌ No hosts data received' ) ;
84+ resolve ( ) ;
85+ }
86+ } ) ;
87+ } else {
88+ console . log ( '❌ No "hosts" key found in namespace' ) ;
89+ resolve ( ) ;
90+ }
91+ } else {
92+ console . log ( '❌ No data received for namespace' ) ;
93+ resolve ( ) ;
94+ }
95+ } ) ;
96+
97+ // Also listen for real-time updates
98+ console . log ( '\n👂 Listening for real-time updates...' ) ;
99+ gun . get ( 'dig-nat-tools-test' ) . on ( ( data , key ) => {
100+ console . log ( `🔔 Real-time update received - Key: ${ key } ` ) ;
101+ console . log ( ' Data:' , JSON . stringify ( data , null , 2 ) ) ;
102+ } ) ;
103+ } ) ;
104+ }
105+
106+ // Run the query
107+ queryGunData ( )
108+ . then ( ( ) => {
109+ console . log ( '\n✅ Manual query complete' ) ;
110+ process . exit ( 0 ) ;
111+ } )
112+ . catch ( ( error ) => {
113+ console . error ( '❌ Error during query:' , error ) ;
114+ process . exit ( 1 ) ;
115+ } ) ;
0 commit comments