@cadenza.io/service 1.10.0 → 1.10.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/dist/index.js CHANGED
@@ -338,16 +338,16 @@ var ServiceRegistry = class _ServiceRegistry {
338
338
  "Handle Instance Update",
339
339
  (ctx, emit) => {
340
340
  var _a2;
341
- const { serviceInstance } = ctx;
342
- const { id, serviceName, address, port, exposed } = serviceInstance;
341
+ const { service_instance } = ctx;
342
+ const { id, serviceName, address, port, exposed } = service_instance;
343
343
  if (!this.instances.has(serviceName))
344
344
  this.instances.set(serviceName, []);
345
345
  const instances = this.instances.get(serviceName);
346
346
  const existing = instances.find((i) => i.id === id);
347
347
  if (existing) {
348
- Object.assign(existing, serviceInstance);
348
+ Object.assign(existing, service_instance);
349
349
  } else {
350
- if (this.deputies.has(serviceName) || this.remoteSignals.has(serviceName) || this.remoteSignals.has("*")) {
350
+ if (this.deputies.has(serviceName) || this.remoteSignals.has(serviceName) || this.remoteSignals.has("*") && this.serviceName !== serviceName) {
351
351
  const communicationTypes = Array.from(
352
352
  new Set(
353
353
  (_a2 = this.deputies.get(serviceName).map((d) => d.communicationType)) != null ? _a2 : []
@@ -364,7 +364,7 @@ var ServiceRegistry = class _ServiceRegistry {
364
364
  protocol: exposed ? "https" : "http",
365
365
  communicationTypes
366
366
  });
367
- serviceInstance.clientCreated = true;
367
+ service_instance.clientCreated = true;
368
368
  for (const instance of this.instances.get(serviceName)) {
369
369
  if (instance.clientCreated) continue;
370
370
  instance.clientCreated = true;
@@ -378,7 +378,7 @@ var ServiceRegistry = class _ServiceRegistry {
378
378
  });
379
379
  }
380
380
  }
381
- instances.push(serviceInstance);
381
+ instances.push(service_instance);
382
382
  }
383
383
  return true;
384
384
  },
@@ -1344,53 +1344,60 @@ var SocketController = class _SocketController {
1344
1344
  if (!ctx.__useSocket) {
1345
1345
  return;
1346
1346
  }
1347
- const server = new import_socket.Server((_a2 = ctx.__httpsServer) != null ? _a2 : ctx.__httpServer);
1348
- ctx.__socketServer = server;
1349
- const profile = (_b2 = ctx.__securityProfile) != null ? _b2 : "medium";
1350
- server.use((socket, next) => {
1351
- var _a3;
1352
- const origin = socket.handshake.headers.origin;
1353
- const allowedOrigins = ["*"];
1354
- const networkType = (_a3 = ctx.__networkType) != null ? _a3 : "internal";
1355
- let effectiveOrigin = origin || "unknown";
1356
- if (networkType === "internal") effectiveOrigin = "internal";
1357
- if (profile !== "low" && !allowedOrigins.includes(effectiveOrigin) && !allowedOrigins.includes("*")) {
1358
- return next(new Error("Unauthorized origin"));
1359
- }
1360
- const limiterOptions = {
1361
- low: { points: Infinity, duration: 300 },
1362
- medium: { points: 100, duration: 300 },
1363
- high: { points: 50, duration: 60, blockDuration: 300 }
1364
- };
1365
- const limiter = new import_rate_limiter_flexible2.RateLimiterMemory(limiterOptions[profile]);
1366
- socket.use((packet, next2) => {
1367
- limiter.consume(socket.handshake.address).then(() => next2()).catch((rej) => {
1368
- if (rej.msBeforeNext > 0) {
1369
- socket.emit("error", {
1370
- message: "Rate limit exceeded",
1371
- retryAfter: rej.msBeforeNext / 1e3
1372
- });
1373
- } else {
1374
- socket.disconnect(true);
1375
- }
1347
+ console.log("SocketServer: Setting up", ctx);
1348
+ try {
1349
+ const server = new import_socket.Server((_a2 = ctx.__httpsServer) != null ? _a2 : ctx.__httpServer);
1350
+ ctx.__socketServer = server;
1351
+ const profile = (_b2 = ctx.__securityProfile) != null ? _b2 : "medium";
1352
+ server.use((socket, next) => {
1353
+ var _a3;
1354
+ const origin = socket.handshake.headers.origin;
1355
+ const allowedOrigins = ["*"];
1356
+ const networkType = (_a3 = ctx.__networkType) != null ? _a3 : "internal";
1357
+ let effectiveOrigin = origin || "unknown";
1358
+ if (networkType === "internal") effectiveOrigin = "internal";
1359
+ if (profile !== "low" && !allowedOrigins.includes(effectiveOrigin) && !allowedOrigins.includes("*")) {
1360
+ return next(new Error("Unauthorized origin"));
1361
+ }
1362
+ const limiterOptions = {
1363
+ low: { points: Infinity, duration: 300 },
1364
+ medium: { points: 100, duration: 300 },
1365
+ high: { points: 50, duration: 60, blockDuration: 300 }
1366
+ };
1367
+ const limiter = new import_rate_limiter_flexible2.RateLimiterMemory(limiterOptions[profile]);
1368
+ socket.use((packet, next2) => {
1369
+ limiter.consume(socket.handshake.address).then(() => next2()).catch((rej) => {
1370
+ if (rej.msBeforeNext > 0) {
1371
+ socket.emit("error", {
1372
+ message: "Rate limit exceeded",
1373
+ retryAfter: rej.msBeforeNext / 1e3
1374
+ });
1375
+ } else {
1376
+ socket.disconnect(true);
1377
+ }
1378
+ });
1376
1379
  });
1377
- });
1378
- socket.use((packet, next2) => {
1379
- if (profile !== "low") {
1380
- const sanitize = (data) => {
1381
- if (typeof data === "string") return (0, import_xss.default)(data);
1382
- if (typeof data === "object") {
1383
- for (const key in data) {
1384
- data[key] = sanitize(data[key]);
1380
+ socket.use((packet, next2) => {
1381
+ if (profile !== "low") {
1382
+ const sanitize = (data) => {
1383
+ if (typeof data === "string") return (0, import_xss.default)(data);
1384
+ if (typeof data === "object") {
1385
+ for (const key in data) {
1386
+ data[key] = sanitize(data[key]);
1387
+ }
1385
1388
  }
1386
- }
1387
- return data;
1388
- };
1389
- packet[1] = sanitize(packet[1]);
1390
- }
1391
- next2();
1389
+ return data;
1390
+ };
1391
+ packet[1] = sanitize(packet[1]);
1392
+ }
1393
+ next2();
1394
+ });
1392
1395
  });
1393
- });
1396
+ } catch (err) {
1397
+ console.error("Socket setup error:", err);
1398
+ return false;
1399
+ }
1400
+ return true;
1394
1401
  }).then(
1395
1402
  CadenzaService.createMetaTask(
1396
1403
  "Start SocketServer",
@@ -1649,6 +1656,9 @@ var SignalController = class _SignalController {
1649
1656
  const firstChar = __signalName.charAt(0);
1650
1657
  if (firstChar === firstChar.toUpperCase() && firstChar !== firstChar.toLowerCase() || firstChar === "*") {
1651
1658
  const serviceName = __signalName.split(".")[0];
1659
+ if (CadenzaService.serviceRegistry.serviceName === serviceName) {
1660
+ return false;
1661
+ }
1652
1662
  ctx.__listenerServiceName = CadenzaService.serviceRegistry.serviceName;
1653
1663
  ctx.__emitterSignalName = __signalName.split(".").slice(1).join(".");
1654
1664
  ctx.__signalName = "meta.signal_controller.foreign_signal_registered";
@@ -3630,8 +3640,8 @@ var CadenzaService = class {
3630
3640
  if ((_g = options.cadenzaDB) == null ? void 0 : _g.connect) {
3631
3641
  import_core3.default.broker.emit("meta.initializing_service", {
3632
3642
  // Seed the CadenzaDB
3633
- serviceInstance: {
3634
- id: "cadenza-db",
3643
+ service_instance: {
3644
+ uuid: "cadenza-db",
3635
3645
  serviceName: "CadenzaDB",
3636
3646
  address: (_h = options.cadenzaDB) == null ? void 0 : _h.address,
3637
3647
  port: (_i = options.cadenzaDB) == null ? void 0 : _i.port,
@@ -3648,8 +3658,8 @@ var CadenzaService = class {
3648
3658
  (_j = options.relatedServices) == null ? void 0 : _j.forEach((service) => {
3649
3659
  var _a3;
3650
3660
  import_core3.default.broker.emit("meta.initializing_service", {
3651
- serviceInstance: {
3652
- id: service[0],
3661
+ service_instance: {
3662
+ uuid: service[0],
3653
3663
  serviceName: service[1],
3654
3664
  address: service[2].split(":")[0],
3655
3665
  port: (_a3 = service[2].split(":")[1]) != null ? _a3 : 3e3,