diodejs 0.2.1 → 0.2.2
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 +19 -18
- package/package.json +1 -1
- package/rpc.js +42 -15
- package/utils.js +0 -4
package/connection.js
CHANGED
|
@@ -114,7 +114,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
this.socket.on('data', (data) => {
|
|
117
|
-
logger.debug(`Received data: ${data.toString('hex')}`);
|
|
117
|
+
// logger.debug(`Received data: ${data.toString('hex')}`);
|
|
118
118
|
try {
|
|
119
119
|
this._handleData(data);
|
|
120
120
|
} catch (error) {
|
|
@@ -274,7 +274,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
274
274
|
_handleData(data) {
|
|
275
275
|
// Append new data to the receive buffer
|
|
276
276
|
this.receiveBuffer = Buffer.concat([this.receiveBuffer, data]);
|
|
277
|
-
logger.debug(`Received data: ${data.toString('hex')}`);
|
|
277
|
+
// logger.debug(`Received data: ${data.toString('hex')}`);
|
|
278
278
|
|
|
279
279
|
let offset = 0;
|
|
280
280
|
while (offset + 2 <= this.receiveBuffer.length) {
|
|
@@ -292,7 +292,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
292
292
|
|
|
293
293
|
try {
|
|
294
294
|
const decodedMessage = RLP.decode(Uint8Array.from(messageBuffer));
|
|
295
|
-
logger.debug(`Decoded message: ${makeReadable(decodedMessage)}`);
|
|
295
|
+
// logger.debug(`Decoded message: ${makeReadable(decodedMessage)}`);
|
|
296
296
|
|
|
297
297
|
if (Array.isArray(decodedMessage) && decodedMessage.length > 1) {
|
|
298
298
|
const requestIdRaw = decodedMessage[0];
|
|
@@ -349,7 +349,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
349
349
|
this.pendingRequests.delete(requestId);
|
|
350
350
|
} else {
|
|
351
351
|
// This is an unsolicited message
|
|
352
|
-
logger.debug(`Received unsolicited message
|
|
352
|
+
logger.debug(`Received unsolicited message`);
|
|
353
353
|
this.emit('unsolicited', decodedMessage);
|
|
354
354
|
}
|
|
355
355
|
} else {
|
|
@@ -406,7 +406,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
406
406
|
const message = Buffer.concat([lengthBuffer, commandBuffer]);
|
|
407
407
|
|
|
408
408
|
logger.debug(`Sending command with requestId ${requestId}: ${commandArray}`);
|
|
409
|
-
logger.debug(`Command buffer: ${message.toString('hex')}`);
|
|
409
|
+
// logger.debug(`Command buffer: ${message.toString('hex')}`);
|
|
410
410
|
|
|
411
411
|
this.socket.write(message);
|
|
412
412
|
}).catch(reject);
|
|
@@ -433,7 +433,7 @@ class DiodeConnection extends EventEmitter {
|
|
|
433
433
|
const message = Buffer.concat([lengthBuffer, commandBuffer]);
|
|
434
434
|
|
|
435
435
|
logger.debug(`Sending command with requestId ${requestId}: ${commandArray}`);
|
|
436
|
-
logger.debug(`Command buffer: ${message.toString('hex')}`);
|
|
436
|
+
// logger.debug(`Command buffer: ${message.toString('hex')}`);
|
|
437
437
|
|
|
438
438
|
this.socket.write(message);
|
|
439
439
|
}).catch(reject);
|
|
@@ -644,21 +644,22 @@ class DiodeConnection extends EventEmitter {
|
|
|
644
644
|
const timeSinceLastUpdate = Date.now() - this.lastTicketUpdate;
|
|
645
645
|
|
|
646
646
|
if (force ||
|
|
647
|
-
|
|
648
|
-
|
|
647
|
+
(this.accumulatedBytes > 0 &&
|
|
648
|
+
(this.accumulatedBytes >= this.ticketUpdateThreshold ||
|
|
649
|
+
timeSinceLastUpdate >= this.ticketUpdateInterval))) {
|
|
649
650
|
|
|
650
651
|
try {
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
652
|
+
if (this.accumulatedBytes > 0 || force) {
|
|
653
|
+
logger.debug(`Updating ticket: accumulated ${this.accumulatedBytes} bytes, ${timeSinceLastUpdate}ms since last update`);
|
|
654
|
+
const ticketCommand = await this.createTicketCommand();
|
|
655
|
+
await this.sendCommand(ticketCommand);
|
|
656
|
+
|
|
657
|
+
// Reset counters
|
|
658
|
+
this.accumulatedBytes = 0;
|
|
659
|
+
this.lastTicketUpdate = Date.now();
|
|
660
|
+
}
|
|
660
661
|
} catch (error) {
|
|
661
|
-
|
|
662
|
+
logger.error(`Error updating ticket: ${error}`);
|
|
662
663
|
}
|
|
663
664
|
}
|
|
664
665
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diodejs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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/rpc.js
CHANGED
|
@@ -102,24 +102,51 @@ class DiodeRPC {
|
|
|
102
102
|
const bytesToSend = data.length;
|
|
103
103
|
this.connection.addBytes(bytesToSend);
|
|
104
104
|
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
105
|
+
// Maximum size that can be sent in a single message (less than 65535 to be safe)
|
|
106
|
+
const MAX_CHUNK_SIZE = 65000;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
// If data is too large, split it into chunks
|
|
110
|
+
if (data.length > MAX_CHUNK_SIZE) {
|
|
111
|
+
logger.debug(`Chunking large data of ${data.length} bytes into pieces of max ${MAX_CHUNK_SIZE} bytes`);
|
|
112
|
+
let offset = 0;
|
|
113
|
+
|
|
114
|
+
while (offset < data.length) {
|
|
115
|
+
const chunkSize = Math.min(MAX_CHUNK_SIZE, data.length - offset);
|
|
116
|
+
const chunk = data.slice(offset, offset + chunkSize);
|
|
117
|
+
|
|
118
|
+
// Send this chunk
|
|
119
|
+
const responseData = await this.connection.sendCommand(['portsend', ref, chunk]);
|
|
120
|
+
const [statusRaw] = responseData;
|
|
121
|
+
const status = parseResponseType(statusRaw);
|
|
122
|
+
|
|
123
|
+
if (status !== 'ok') {
|
|
124
|
+
throw new Error(`Error during chunked port send: ${status}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
offset += chunkSize;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return; // All chunks sent successfully
|
|
116
131
|
} else {
|
|
117
|
-
|
|
132
|
+
// Small enough to send in one piece
|
|
133
|
+
return this.connection.sendCommand(['portsend', ref, data]).then((responseData) => {
|
|
134
|
+
const [statusRaw] = responseData;
|
|
135
|
+
const status = parseResponseType(statusRaw);
|
|
136
|
+
|
|
137
|
+
if (status === 'ok') {
|
|
138
|
+
return;
|
|
139
|
+
} else if (status === 'error') {
|
|
140
|
+
throw new Error('Error during port send');
|
|
141
|
+
} else {
|
|
142
|
+
throw new Error(`Unknown status in response: '${status}'`);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
118
145
|
}
|
|
119
|
-
}
|
|
146
|
+
} catch (error) {
|
|
120
147
|
logger.error(`Error during port send: ${error}`);
|
|
121
|
-
|
|
122
|
-
}
|
|
148
|
+
throw error; // Rethrow to allow proper error handling upstream
|
|
149
|
+
}
|
|
123
150
|
}
|
|
124
151
|
|
|
125
152
|
portClose(ref) {
|
package/utils.js
CHANGED
|
@@ -52,10 +52,6 @@ function parseRequestId(requestIdRaw) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
function parseResponseType(responseTypeRaw) {
|
|
55
|
-
logger.debug(`responseTypeRaw: ${responseTypeRaw}`);
|
|
56
|
-
logger.debug(`Type of responseTypeRaw: ${typeof responseTypeRaw}`);
|
|
57
|
-
logger.debug(`Instance of responseTypeRaw: ${responseTypeRaw instanceof Uint8Array}`);
|
|
58
|
-
logger.debug(`Is Array: ${Array.isArray(responseTypeRaw)}`);
|
|
59
55
|
if (responseTypeRaw instanceof Uint8Array || Buffer.isBuffer(responseTypeRaw)) {
|
|
60
56
|
return Buffer.from(responseTypeRaw).toString('utf8');
|
|
61
57
|
} else if (Array.isArray(responseTypeRaw)) {
|