geonix 1.20.3 → 1.20.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geonix",
3
- "version": "1.20.3",
3
+ "version": "1.20.5",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "bin": {
@@ -16,14 +16,13 @@
16
16
  "author": "Davor Tarandek <dtarandek@tria.hr>",
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
- "cookie-parser": "^1.4.6",
20
- "express": "^4.19.2",
21
- "express-async-errors": "^3.1.1",
22
- "express-ws": "^5.0.2",
23
- "multer": "^1.4.5-lts.1",
24
- "nats": "^2.28.2",
19
+ "cookie-parser": "1.4.6",
20
+ "express": "4.20.0",
21
+ "express-async-errors": "3.1.1",
22
+ "express-ws": "5.0.2",
23
+ "nats": "2.28.2",
25
24
  "semver": "^7.6.3",
26
- "ws": "^8.18.0"
25
+ "ws": "8.18.0"
27
26
  },
28
27
  "publishConfig": {
29
28
  "registry": "https://registry.npmjs.org/"
@@ -32,4 +31,4 @@
32
31
  "eslint": "^9.10.0",
33
32
  "typescript": "^5.5.4"
34
33
  }
35
- }
34
+ }
package/src/Connection.js CHANGED
@@ -155,7 +155,7 @@ class Connection {
155
155
 
156
156
  // if payload is too big, convert it to Stream
157
157
  if (payload.length > this.getMaxPayloadSize()) {
158
- payload = encode(Stream(JSON.stringify({ $r: respondTo, p: json })));
158
+ payload = encode(Stream(Buffer.from(payload)));
159
159
  }
160
160
 
161
161
  const nc = this.#getConnection();
package/src/Service.js CHANGED
@@ -140,7 +140,9 @@ export class Service {
140
140
  for await (let event of subscription) {
141
141
  let call = decode(event.data);
142
142
 
143
- if (isStream(call)) { call = JSON.parse(await streamToString(call)); }
143
+ if (isStream(call)) {
144
+ call = JSON.parse(await streamToString(call));
145
+ }
144
146
 
145
147
  if (call.$r && call.p) { this.#onCall(call.p, (json) => connection.publish(call.$r, json)); }
146
148
  }
package/src/Stream.js CHANGED
@@ -74,12 +74,16 @@ export function isStream(object) {
74
74
  }
75
75
 
76
76
  export async function getReadable(object) {
77
+ if (!object) {
78
+ throw Error("Stream.getReadable: invalid object");
79
+ }
80
+
77
81
  if (!isStream(object)) {
78
82
  return object;
79
83
  }
80
84
 
81
85
  // get stream via HTTP
82
- if (object.a.length > 0) {
86
+ if (object.a && object.a.length > 0) {
83
87
  for (const address of object.a) {
84
88
  try {
85
89
  const uri = `http://${address}/!!_____stream/${object.id}`;
@@ -130,7 +134,7 @@ export async function streamToBuffer(object) {
130
134
  }
131
135
 
132
136
  export async function streamToString(object, encoding) {
133
- return streamToBuffer(object).toString(encoding);
137
+ return (await streamToBuffer(object)).toString(encoding);
134
138
  }
135
139
 
136
140
  export async function streamToJSON(object) {
@@ -0,0 +1,39 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { createHash } from "node:crypto";
3
+ import { Remote, Service } from "geonix";
4
+
5
+ const hash = data => createHash("sha512").update(data).digest("base64");
6
+
7
+ const payload = randomBytes(1024 * 1024 * 100);
8
+
9
+ class Sender extends Service {
10
+
11
+ async onStart() {
12
+
13
+ const payloadHash = hash(payload);
14
+
15
+ const recipient = Remote("Recipient");
16
+
17
+ const result = await recipient.validate(payloadHash, payload.toString("base64"));
18
+
19
+ if (result) {
20
+ console.log("MATCH");
21
+ } else {
22
+ console.error("Destination does not match the source!");
23
+ }
24
+ }
25
+
26
+ }
27
+
28
+ class Recipient extends Service {
29
+
30
+ async validate(originalHash, data) {
31
+ const validationHash = hash(Buffer.from(data, "base64"));
32
+
33
+ return validationHash === originalHash;
34
+ }
35
+
36
+ }
37
+
38
+ Sender.start();
39
+ Recipient.start();