geonix 1.20.2 → 1.20.3

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.2",
3
+ "version": "1.20.3",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "bin": {
@@ -29,7 +29,7 @@
29
29
  "registry": "https://registry.npmjs.org/"
30
30
  },
31
31
  "devDependencies": {
32
- "eslint": "^9.9.1",
32
+ "eslint": "^9.10.0",
33
33
  "typescript": "^5.5.4"
34
34
  }
35
- }
35
+ }
package/src/Codec.js ADDED
@@ -0,0 +1,16 @@
1
+ import { JSONCodec } from "nats";
2
+
3
+ export const codec = JSONCodec();
4
+
5
+ export function encode(data) {
6
+ return codec.encode(data);
7
+ }
8
+
9
+ export function decode(data) {
10
+ // check if data is json
11
+ if (Buffer.isBuffer(data) && data.readUInt8(0) === "{".charCodeAt(0)) {
12
+ return codec.decode(data);
13
+ }
14
+
15
+ throw new Error("Codec.decode: unknown data type");
16
+ }
package/src/Connection.js CHANGED
@@ -1,8 +1,9 @@
1
- import { connect, JSONCodec } from "nats";
1
+ import { connect } from "nats";
2
2
  import { getFirstItemFromAsyncIterable, parseURL, picoid, sleep } from "./Util.js";
3
3
  import { Stream } from "./Stream.js";
4
4
  import { webserver } from "./WebServer.js";
5
5
  import { logger } from "./Logger.js";
6
+ import { decode, encode } from "./Codec.js";
6
7
 
7
8
  // -------------------------------------------------------------------------------------------------
8
9
  const CONNECTION_TIMEOUT = 10000;
@@ -109,11 +110,11 @@ class Connection {
109
110
  return;
110
111
  }
111
112
 
112
- let payload = codec.encode(json);
113
+ let payload = encode(json);
113
114
 
114
115
  // if payload is too big, convert it to Stream
115
116
  if (payload.length > this.getMaxPayloadSize()) {
116
- payload = codec.encode(Stream(JSON.stringify(json)));
117
+ payload = encode(Stream(JSON.stringify(json)));
117
118
  }
118
119
 
119
120
  await this.#getConnection().publish(subject, payload);
@@ -150,11 +151,11 @@ class Connection {
150
151
 
151
152
  const respondTo = `gx2.r.${picoid(16)}`;
152
153
 
153
- let payload = codec.encode({ $r: respondTo, p: json });
154
+ let payload = encode({ $r: respondTo, p: json });
154
155
 
155
156
  // if payload is too big, convert it to Stream
156
157
  if (payload.length > this.getMaxPayloadSize()) {
157
- payload = codec.encode(Stream(JSON.stringify({ $r: respondTo, p: json })));
158
+ payload = encode(Stream(JSON.stringify({ $r: respondTo, p: json })));
158
159
  }
159
160
 
160
161
  const nc = this.#getConnection();
@@ -163,7 +164,7 @@ class Connection {
163
164
  await nc.publish(subject, payload);
164
165
 
165
166
  const event = await getFirstItemFromAsyncIterable(response);
166
- return codec.decode(event.data);
167
+ return decode(event.data);
167
168
  }
168
169
 
169
170
  async subscribe(subject, options) {
@@ -195,8 +196,6 @@ class Connection {
195
196
 
196
197
  }
197
198
 
198
- export const codec = JSONCodec();
199
-
200
199
  export const connection = new Connection();
201
200
  connection.start();
202
201
 
package/src/Registry.js CHANGED
@@ -1,8 +1,9 @@
1
- import { connection, codec } from "./Connection.js";
1
+ import { connection } from "./Connection.js";
2
2
  import { sleep } from "./Util.js";
3
3
  import semver from "semver";
4
4
  import EventEmitter from "events";
5
5
  import { directRequest } from "./Request.js";
6
+ import { decode } from "./Codec.js";
6
7
 
7
8
  const REGISTRY_ENTRY_TIMEOUT = 5000;
8
9
 
@@ -30,7 +31,7 @@ class Registry extends EventEmitter {
30
31
  const subscription = await connection.subscribe("gx2.beacon");
31
32
 
32
33
  for await (const event of subscription) {
33
- let data = codec.decode(event.data);
34
+ let data = decode(event.data);
34
35
 
35
36
  const exists = this.#registry[data.i] !== undefined;
36
37
 
package/src/Service.js CHANGED
@@ -1,4 +1,4 @@
1
- import { codec, connection } from "./Connection.js";
1
+ import { connection } from "./Connection.js";
2
2
  import { picoid, sleep, hash, getSecondsSinceMidnight, OverlayObject, GeonixVersion, getFirstItemFromAsyncIterable, getNetworkAddresses } from "./Util.js";
3
3
  import { webserver } from "./WebServer.js";
4
4
  import { createConnection } from "net";
@@ -7,6 +7,7 @@ import cookieParser from "cookie-parser";
7
7
  import express from "express";
8
8
  import { isStream, streamToString } from "./Stream.js";
9
9
  import { logger } from "./Logger.js";
10
+ import { decode } from "./Codec.js";
10
11
 
11
12
  const protectedMethodNames = ["constructor", "onStart"];
12
13
  const endpointMatcher = /^((?<options>.+)\|)?(?<verb>WS|SUB|GET|POST|PATCH|PUT|DELETE|HEAD|OPTIONS|ALL)\s(?<url>.*)/;
@@ -137,7 +138,7 @@ export class Service {
137
138
  const subscription = await connection.subscribe(`gx2.service.${hash(identifier)}`, { queue: identifier });
138
139
 
139
140
  for await (let event of subscription) {
140
- let call = codec.decode(event.data);
141
+ let call = decode(event.data);
141
142
 
142
143
  if (isStream(call)) { call = JSON.parse(await streamToString(call)); }
143
144
 
@@ -153,7 +154,7 @@ export class Service {
153
154
  const subscription = await connection.subscribe(`gx2.service.${hash(identifier)}`, { queue: identifier });
154
155
 
155
156
  for await (let event of subscription) {
156
- let call = codec.decode(event.data);
157
+ let call = decode(event.data);
157
158
 
158
159
  if (isStream(call)) {
159
160
  call = JSON.parse(await streamToString(call));
package/src/Stream.js CHANGED
@@ -129,11 +129,10 @@ export async function streamToBuffer(object) {
129
129
  return Buffer.concat(await readable.toArray());
130
130
  }
131
131
 
132
- export async function streamToString(object) {
133
- let readable = object;
134
- if (isStream(readable)) {
135
- readable = await getReadable(readable);
136
- }
132
+ export async function streamToString(object, encoding) {
133
+ return streamToBuffer(object).toString(encoding);
134
+ }
137
135
 
138
- return Buffer.concat(await readable.toArray()).toString();
139
- }
136
+ export async function streamToJSON(object) {
137
+ return JSON.parse(await streamToBuffer(object));
138
+ }
@@ -0,0 +1,35 @@
1
+ import { Remote, Service } from "../exports.js";
2
+ import { sleep } from "../src/Util.js";
3
+
4
+ class TimeService extends Service {
5
+
6
+ #timestamp() {
7
+ return new Date().toISOString();
8
+ }
9
+
10
+ getCurrentTime() {
11
+ const [prefix] = this.context;
12
+
13
+ return `${prefix} ${this.#timestamp()}`;
14
+ }
15
+
16
+ }
17
+
18
+ class ApplicationService extends Service {
19
+
20
+ #timeService = Remote("TimeService", "prefix");
21
+
22
+ async onStart() {
23
+ while (true) {
24
+ const time = await this.#timeService.getCurrentTime();
25
+
26
+ console.log("TIME =", time);
27
+
28
+ await sleep(1000);
29
+ }
30
+ }
31
+
32
+ }
33
+
34
+ TimeService.start();
35
+ ApplicationService.start();
package/test/simple.js ADDED
@@ -0,0 +1,29 @@
1
+ import { Remote, Service } from "../exports.js";
2
+ import { sleep } from "../src/Util.js";
3
+
4
+ class TimeService extends Service {
5
+
6
+ getCurrentTime() {
7
+ return new Date().toISOString();
8
+ }
9
+
10
+ }
11
+
12
+ class ApplicationService extends Service {
13
+
14
+ #timeService = Remote("TimeService");
15
+
16
+ async onStart() {
17
+ while (true) {
18
+ const time = await this.#timeService.getCurrentTime();
19
+
20
+ console.log("TIME =", time);
21
+
22
+ await sleep(1000);
23
+ }
24
+ }
25
+
26
+ }
27
+
28
+ TimeService.start();
29
+ ApplicationService.start();