diodejs 0.1.3 → 0.1.5

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/README.md CHANGED
@@ -69,7 +69,7 @@ async function main() {
69
69
  // Multiple or single port binding with configuration object
70
70
  const portsConfig = {
71
71
  3002: { targetPort: 80, deviceIdHex: "5365baf29cb7ab58de588dfc448913cb609283e2" },
72
- 3003: { targetPort: 443, deviceIdHex: "5365baf29cb7ab58de588dfc448913cb609283e2" }
72
+ 3003: { targetPort: 443, deviceIdHex: "0x5365baf29cb7ab58de588dfc448913cb609283e2" } // Works with or without 0x prefix
73
73
  };
74
74
 
75
75
  const portForward = new BindPort(connection, portsConfig);
@@ -185,17 +185,18 @@ main();
185
185
  - `connection` (DiodeConnection): An instance of `DiodeConnection`.
186
186
  - `localPort` (number): The local port to bind.
187
187
  - `targetPort` (number): The target port on the device.
188
- - `deviceIdHex` (string): The device ID in hexadecimal format.
188
+ - `deviceIdHex` (string): The device ID in hexadecimal format (with or without '0x' prefix).
189
189
 
190
190
  New Constructor:
191
191
  - `new BindPort(connection, portsConfig)`
192
192
  - `connection` (DiodeConnection): An instance of `DiodeConnection`.
193
193
  - `portsConfig` (object): A configuration object where keys are local ports and values are objects with `targetPort` and `deviceIdHex`.
194
194
  Example: `{ 3002: { targetPort: 80, deviceIdHex: "5365baf29cb7ab58de588dfc448913cb609283e2" } }`
195
+ Note: deviceIdHex can be provided with or without the '0x' prefix.
195
196
 
196
197
  - **Methods**:
197
198
  - `bind()`: Binds all configured local ports to their target ports on the devices.
198
- - `addPort(localPort, targetPort, deviceIdHex)`: Adds a new port binding configuration.
199
+ - `addPort(localPort, targetPort, deviceIdHex)`: Adds a new port binding configuration. deviceIdHex can include '0x' prefix.
199
200
  - `removePort(localPort)`: Removes a port binding configuration.
200
201
  - `bindSinglePort(localPort)`: Binds a single local port to its target.
201
202
  - `closeAllServers()`: Closes all active server instances.
package/bindPort.js CHANGED
@@ -10,11 +10,21 @@ class BindPort {
10
10
  // Handle legacy constructor (connection, localPort, targetPort, deviceIdHex)
11
11
  if (typeof localPortOrPortsConfig === 'number' && targetPort !== undefined && deviceIdHex !== undefined) {
12
12
  this.portsConfig = {
13
- [localPortOrPortsConfig]: { targetPort, deviceIdHex }
13
+ [localPortOrPortsConfig]: {
14
+ targetPort,
15
+ deviceIdHex: this._stripHexPrefix(deviceIdHex)
16
+ }
14
17
  };
15
18
  } else {
16
19
  // New constructor (connection, portsConfig)
17
20
  this.portsConfig = localPortOrPortsConfig || {};
21
+
22
+ // Strip 0x prefix from all deviceIdHex values in portsConfig
23
+ for (const port in this.portsConfig) {
24
+ if (this.portsConfig[port].deviceIdHex) {
25
+ this.portsConfig[port].deviceIdHex = this._stripHexPrefix(this.portsConfig[port].deviceIdHex);
26
+ }
27
+ }
18
28
  }
19
29
 
20
30
  this.servers = new Map(); // Track server instances by localPort
@@ -24,6 +34,14 @@ class BindPort {
24
34
  this._setupMessageListener();
25
35
  }
26
36
 
37
+ // Helper method to strip 0x prefix from hex strings
38
+ _stripHexPrefix(hexString) {
39
+ if (typeof hexString === 'string' && hexString.toLowerCase().startsWith('0x')) {
40
+ return hexString.slice(2);
41
+ }
42
+ return hexString;
43
+ }
44
+
27
45
  _setupMessageListener() {
28
46
  // Listen for data events from the device
29
47
  this.connection.on('unsolicited', (message) => {
@@ -88,12 +106,12 @@ class BindPort {
88
106
  return false;
89
107
  }
90
108
 
91
- this.portsConfig[localPort] = { targetPort, deviceIdHex };
109
+ this.portsConfig[localPort] = {
110
+ targetPort,
111
+ deviceIdHex: this._stripHexPrefix(deviceIdHex)
112
+ };
92
113
 
93
- // If we're already bound, set up this new port immediately
94
- if (this.servers.size > 0) {
95
- this.bindSinglePort(localPort);
96
- }
114
+ this.bindSinglePort(localPort);
97
115
 
98
116
  return true;
99
117
  }
@@ -9,7 +9,7 @@ async function main() {
9
9
  await connection.connect();
10
10
 
11
11
  const portForward = new BindPort(connection, {
12
- 3003: { targetPort: 8080, deviceIdHex: "ca1e71d8105a598810578fb6042fa8cbc1e7f039" },
12
+ 3003: { targetPort: 8080, deviceIdHex: "0xca1e71d8105a598810578fb6042fa8cbc1e7f039" },
13
13
  3004: { targetPort: 443, deviceIdHex: "5365baf29cb7ab58de588dfc448913cb609283e2" }
14
14
  });
15
15
  portForward.bind();
@@ -23,7 +23,7 @@ async function main() {
23
23
  // after 10 seconds, add port 3003 back
24
24
  setTimeout(() => {
25
25
  console.log("Adding port 3003 back");
26
- portForward.addPort(3003, 8080, "ca1e71d8105a598810578fb6042fa8cbc1e7f039");
26
+ portForward.addPort(3003, 8080, "0xca1e71d8105a598810578fb6042fa8cbc1e7f039");
27
27
  }, 10000);
28
28
 
29
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diodejs",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
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": {