diodejs 0.0.3 → 0.0.4
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.
- package/connection.js +3 -3
- package/package.json +1 -1
- package/publishPort.js +16 -5
- package/rpc.js +46 -16
package/connection.js
CHANGED
|
@@ -47,7 +47,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
47
47
|
// Send the ticketv2 command
|
|
48
48
|
try {
|
|
49
49
|
const ticketCommand = await this.createTicketCommand();
|
|
50
|
-
const response = await this.sendCommand(ticketCommand);
|
|
50
|
+
const response = await this.sendCommand(ticketCommand).catch(reject);
|
|
51
51
|
console.log('Ticket accepted:', response);
|
|
52
52
|
resolve();
|
|
53
53
|
} catch (error) {
|
|
@@ -137,10 +137,10 @@ class DiodeConnection extends EventEmitter {
|
|
|
137
137
|
} else if (responseType === 'error') {
|
|
138
138
|
if (responseData.length > 1) {
|
|
139
139
|
const reason = parseReason(responseData[1]);
|
|
140
|
-
reject(
|
|
140
|
+
reject(reason);
|
|
141
141
|
} else {
|
|
142
142
|
const reason = parseReason(responseData[0]);
|
|
143
|
-
reject(
|
|
143
|
+
reject(reason);
|
|
144
144
|
}
|
|
145
145
|
} else {
|
|
146
146
|
resolve(responseData);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diodejs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "A JavaScript client for interacting with the Diode network. It provides functionalities to bind and publish ports, send RPC commands, and handle responses.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/publishPort.js
CHANGED
|
@@ -8,7 +8,7 @@ const { Buffer } = require('buffer');
|
|
|
8
8
|
const EventEmitter = require('events');
|
|
9
9
|
const { Duplex } = require('stream');
|
|
10
10
|
const DiodeRPC = require('./rpc');
|
|
11
|
-
|
|
11
|
+
const { makeReadable } = require('./utils');
|
|
12
12
|
class DiodeSocket extends Duplex {
|
|
13
13
|
constructor(ref, rpc) {
|
|
14
14
|
super();
|
|
@@ -70,15 +70,26 @@ class PublishPort extends EventEmitter {
|
|
|
70
70
|
const deviceIdRaw = messageContent[3];
|
|
71
71
|
|
|
72
72
|
const sessionId = Buffer.from(sessionIdRaw);
|
|
73
|
-
const portString =
|
|
73
|
+
const portString = makeReadable(portStringRaw);
|
|
74
74
|
const ref = Buffer.from(refRaw);
|
|
75
75
|
const deviceId = Buffer.from(deviceIdRaw).toString('hex');
|
|
76
76
|
|
|
77
77
|
console.log(`Received portopen request for portString ${portString} with ref ${ref.toString('hex')} from device ${deviceId}`);
|
|
78
78
|
|
|
79
79
|
// Extract protocol and port number from portString
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
var protocol = 'tcp';
|
|
81
|
+
var port = 0;
|
|
82
|
+
if (typeof portString == 'number') {
|
|
83
|
+
port = portString;
|
|
84
|
+
} else {
|
|
85
|
+
var [protocol, portStr] = portString.split(':');
|
|
86
|
+
console.log(`Protocol: ${protocol}, Port: ${portStr}`);
|
|
87
|
+
if (!portStr) {
|
|
88
|
+
portStr = protocol;
|
|
89
|
+
protocol = 'tcp';
|
|
90
|
+
}
|
|
91
|
+
port = parseInt(portStr, 10);
|
|
92
|
+
}
|
|
82
93
|
|
|
83
94
|
// Check if the port is published
|
|
84
95
|
if (!this.publishedPorts.includes(port)) {
|
|
@@ -233,7 +244,7 @@ class PublishPort extends EventEmitter {
|
|
|
233
244
|
|
|
234
245
|
const sessionId = Buffer.from(sessionIdRaw);
|
|
235
246
|
const ref = Buffer.from(refRaw);
|
|
236
|
-
const data = Buffer.from(dataRaw)
|
|
247
|
+
const data = Buffer.from(dataRaw)//.slice(4);
|
|
237
248
|
|
|
238
249
|
const connectionInfo = this.connections.get(ref.toString('hex'));
|
|
239
250
|
if (connectionInfo) {
|
package/rpc.js
CHANGED
|
@@ -25,35 +25,47 @@ class DiodeRPC {
|
|
|
25
25
|
throw new Error('Invalid block number format. response:', makeReadable(responseData));
|
|
26
26
|
}
|
|
27
27
|
return blockNumber;
|
|
28
|
+
}).catch((error) => {
|
|
29
|
+
console.error('Error during get block peak:', error);
|
|
30
|
+
return;
|
|
28
31
|
});
|
|
29
32
|
}
|
|
30
33
|
getBlockHeader(index) {
|
|
31
34
|
return this.connection.sendCommand(['getblockheader', index]).then((responseData) => {
|
|
32
35
|
return responseData[0]; // block_header
|
|
36
|
+
}).catch((error) => {
|
|
37
|
+
console.error('Error during get block header:', error);
|
|
38
|
+
return;
|
|
33
39
|
});
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
getBlock(index) {
|
|
37
43
|
return this.connection.sendCommand(['getblock', index]).then((responseData) => {
|
|
38
44
|
return responseData[0]; // block
|
|
45
|
+
}).catch((error) => {
|
|
46
|
+
console.error('Error during get block:', error);
|
|
47
|
+
return;
|
|
39
48
|
});
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
ping() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
});
|
|
52
|
+
return this.connection.sendCommand(['ping']).then((responseData) => {
|
|
53
|
+
// responseData is an array containing [status]
|
|
54
|
+
const statusRaw = responseData[0];
|
|
55
|
+
const status = parseResponseType(statusRaw);
|
|
56
|
+
|
|
57
|
+
if (status === 'pong') {
|
|
58
|
+
return true;
|
|
59
|
+
} else if (status === 'error') {
|
|
60
|
+
throw new Error('Ping failed');
|
|
61
|
+
} else {
|
|
62
|
+
throw new Error(`Unknown status in response: '${status}'`);
|
|
56
63
|
}
|
|
64
|
+
}).catch((error) => {
|
|
65
|
+
console.error('Error during ping:', error);
|
|
66
|
+
return false;
|
|
67
|
+
})
|
|
68
|
+
}
|
|
57
69
|
|
|
58
70
|
|
|
59
71
|
|
|
@@ -77,6 +89,9 @@ class DiodeRPC {
|
|
|
77
89
|
} else {
|
|
78
90
|
throw new Error(`Unknown status in response: '${status}'`);
|
|
79
91
|
}
|
|
92
|
+
}).catch((error) => {
|
|
93
|
+
console.error('Error during port open:', error);
|
|
94
|
+
return;
|
|
80
95
|
});
|
|
81
96
|
}
|
|
82
97
|
|
|
@@ -94,7 +109,10 @@ class DiodeRPC {
|
|
|
94
109
|
if (status === 'ok') {
|
|
95
110
|
try {
|
|
96
111
|
const ticketCommand = await this.connection.createTicketCommand();
|
|
97
|
-
const ticketResponse = await this.connection.sendCommand(ticketCommand)
|
|
112
|
+
const ticketResponse = await this.connection.sendCommand(ticketCommand).catch((error) => {
|
|
113
|
+
console.error('Error during ticket command:', error);
|
|
114
|
+
throw error;
|
|
115
|
+
});
|
|
98
116
|
console.log('Ticket updated:', ticketResponse);
|
|
99
117
|
} catch (error) {
|
|
100
118
|
console.error('Error updating ticket:', error);
|
|
@@ -106,6 +124,9 @@ class DiodeRPC {
|
|
|
106
124
|
} else {
|
|
107
125
|
throw new Error(`Unknown status in response: '${status}'`);
|
|
108
126
|
}
|
|
127
|
+
}).catch((error) => {
|
|
128
|
+
console.error('Error during port send:', error);
|
|
129
|
+
return;
|
|
109
130
|
});
|
|
110
131
|
}
|
|
111
132
|
|
|
@@ -124,15 +145,24 @@ class DiodeRPC {
|
|
|
124
145
|
} else {
|
|
125
146
|
throw new Error(`Unknown status in response: '${status}'`);
|
|
126
147
|
}
|
|
148
|
+
}).catch((error) => {
|
|
149
|
+
console.error('Error during port close:', error);
|
|
150
|
+
return;
|
|
127
151
|
});
|
|
128
152
|
}
|
|
129
153
|
|
|
130
154
|
sendError(sessionId, ref, error) {
|
|
131
|
-
return this.connection.sendCommandWithSessionId(['response', ref, 'error', error], sessionId)
|
|
155
|
+
return this.connection.sendCommandWithSessionId(['response', ref, 'error', error], sessionId).catch((error) => {
|
|
156
|
+
console.error('Error during send error:', error);
|
|
157
|
+
return;
|
|
158
|
+
});
|
|
132
159
|
}
|
|
133
160
|
|
|
134
161
|
sendResponse(sessionId, ref, response) {
|
|
135
|
-
return this.connection.sendCommandWithSessionId(['response', ref, response], sessionId)
|
|
162
|
+
return this.connection.sendCommandWithSessionId(['response', ref, response], sessionId).catch((error) => {
|
|
163
|
+
console.error('Error during send response:', error);
|
|
164
|
+
return;
|
|
165
|
+
});
|
|
136
166
|
}
|
|
137
167
|
|
|
138
168
|
async getEpoch() {
|