@hivedev/hivesdk 1.0.43 → 1.0.44

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/hive-server.cjs CHANGED
@@ -684,32 +684,65 @@ async function parseRequestQuery(request) {
684
684
  }
685
685
 
686
686
  // Server/RequestHandlers/GetServiceUrls.js
687
+ var serviceUpdateTimestamps = {};
687
688
  async function getServiceUrls(request, response) {
688
689
  await parseRequestQuery(request);
689
- const serviceName = request.query.serviceName;
690
- const localOnly = request.query.localOnly === "true";
691
- const remoteOnly = request.query.remoteOnly === "true";
692
- if (!serviceName) {
690
+ const requestedServiceName = request.query.serviceName;
691
+ const isLocalOnly = request.query.localOnly === "true";
692
+ const isRemoteOnly = request.query.remoteOnly === "true";
693
+ if (!requestedServiceName) {
693
694
  response.statusCode = 404;
694
695
  response.end();
695
696
  return;
696
697
  }
697
- const serviceUrls = HiveServerGlobals_default.getServiceUrls(serviceName);
698
- if (serviceUrls) {
699
- if (localOnly) {
700
- response.setHeader("Content-Type", "application/json");
701
- response.end(JSON.stringify(serviceUrls[serviceName]["local"]));
702
- } else if (remoteOnly) {
703
- response.setHeader("Content-Type", "application/json");
704
- response.end(JSON.stringify(serviceUrls[serviceName]["remote"]));
698
+ const FIVE_MINUTES_IN_MILLISECONDS = 5 * 60 * 1e3;
699
+ const cachedAddresses = HiveServerGlobals_default.getServiceUrls(requestedServiceName);
700
+ const lastUpdateAttempt = serviceUpdateTimestamps[requestedServiceName] || 0;
701
+ if (cachedAddresses && Date.now() - lastUpdateAttempt < FIVE_MINUTES_IN_MILLISECONDS) {
702
+ sendFormattedResponse(response, cachedAddresses, isLocalOnly, isRemoteOnly);
703
+ return;
704
+ }
705
+ try {
706
+ const hivePortalLanIp = process.env.HIVE_PORTAL_LAN_IP || "127.0.0.1";
707
+ const registryRequestUrl = `http://${hivePortalLanIp}:49152/ServiceUrls`;
708
+ const registryResponse = await fetch(
709
+ registryRequestUrl,
710
+ {
711
+ method: "POST",
712
+ headers: { "Content-Type": "application/json" },
713
+ body: JSON.stringify({ serviceName: requestedServiceName })
714
+ }
715
+ );
716
+ if (registryResponse.ok) {
717
+ const serviceDiscoveryResult = await registryResponse.json();
718
+ const extractedServiceUrls = serviceDiscoveryResult.urls;
719
+ if (extractedServiceUrls) {
720
+ HiveServerGlobals_default.setServiceUrls(requestedServiceName, extractedServiceUrls);
721
+ serviceUpdateTimestamps[requestedServiceName] = Date.now();
722
+ sendFormattedResponse(response, extractedServiceUrls, isLocalOnly, isRemoteOnly);
723
+ } else {
724
+ response.statusCode = 404;
725
+ response.end(JSON.stringify({ message: "Service found but no URLs provided." }));
726
+ }
705
727
  } else {
706
- response.setHeader("Content-Type", "application/json");
707
- response.end(JSON.stringify(serviceUrls[serviceName]));
728
+ response.statusCode = registryResponse.status;
729
+ response.end();
708
730
  }
709
- return;
731
+ } catch (networkError) {
732
+ console.error(`Error fetching service URLs for ${requestedServiceName}:`, networkError.message);
733
+ response.statusCode = 500;
734
+ response.end();
735
+ }
736
+ }
737
+ function sendFormattedResponse(response, serviceAddresses, isLocalOnly, isRemoteOnly) {
738
+ response.setHeader("Content-Type", "application/json");
739
+ let finalResult = serviceAddresses;
740
+ if (isLocalOnly) {
741
+ finalResult = serviceAddresses.local;
742
+ } else if (isRemoteOnly) {
743
+ finalResult = serviceAddresses.remote;
710
744
  }
711
- response.statusCode = 404;
712
- response.end();
745
+ response.end(JSON.stringify(finalResult));
713
746
  }
714
747
 
715
748
  // Common/Enumerations/ClientConnectionTypeFlags.js
package/hive-server.js CHANGED
@@ -656,32 +656,65 @@ async function parseRequestQuery(request) {
656
656
  }
657
657
 
658
658
  // Server/RequestHandlers/GetServiceUrls.js
659
+ var serviceUpdateTimestamps = {};
659
660
  async function getServiceUrls(request, response) {
660
661
  await parseRequestQuery(request);
661
- const serviceName = request.query.serviceName;
662
- const localOnly = request.query.localOnly === "true";
663
- const remoteOnly = request.query.remoteOnly === "true";
664
- if (!serviceName) {
662
+ const requestedServiceName = request.query.serviceName;
663
+ const isLocalOnly = request.query.localOnly === "true";
664
+ const isRemoteOnly = request.query.remoteOnly === "true";
665
+ if (!requestedServiceName) {
665
666
  response.statusCode = 404;
666
667
  response.end();
667
668
  return;
668
669
  }
669
- const serviceUrls = HiveServerGlobals_default.getServiceUrls(serviceName);
670
- if (serviceUrls) {
671
- if (localOnly) {
672
- response.setHeader("Content-Type", "application/json");
673
- response.end(JSON.stringify(serviceUrls[serviceName]["local"]));
674
- } else if (remoteOnly) {
675
- response.setHeader("Content-Type", "application/json");
676
- response.end(JSON.stringify(serviceUrls[serviceName]["remote"]));
670
+ const FIVE_MINUTES_IN_MILLISECONDS = 5 * 60 * 1e3;
671
+ const cachedAddresses = HiveServerGlobals_default.getServiceUrls(requestedServiceName);
672
+ const lastUpdateAttempt = serviceUpdateTimestamps[requestedServiceName] || 0;
673
+ if (cachedAddresses && Date.now() - lastUpdateAttempt < FIVE_MINUTES_IN_MILLISECONDS) {
674
+ sendFormattedResponse(response, cachedAddresses, isLocalOnly, isRemoteOnly);
675
+ return;
676
+ }
677
+ try {
678
+ const hivePortalLanIp = process.env.HIVE_PORTAL_LAN_IP || "127.0.0.1";
679
+ const registryRequestUrl = `http://${hivePortalLanIp}:49152/ServiceUrls`;
680
+ const registryResponse = await fetch(
681
+ registryRequestUrl,
682
+ {
683
+ method: "POST",
684
+ headers: { "Content-Type": "application/json" },
685
+ body: JSON.stringify({ serviceName: requestedServiceName })
686
+ }
687
+ );
688
+ if (registryResponse.ok) {
689
+ const serviceDiscoveryResult = await registryResponse.json();
690
+ const extractedServiceUrls = serviceDiscoveryResult.urls;
691
+ if (extractedServiceUrls) {
692
+ HiveServerGlobals_default.setServiceUrls(requestedServiceName, extractedServiceUrls);
693
+ serviceUpdateTimestamps[requestedServiceName] = Date.now();
694
+ sendFormattedResponse(response, extractedServiceUrls, isLocalOnly, isRemoteOnly);
695
+ } else {
696
+ response.statusCode = 404;
697
+ response.end(JSON.stringify({ message: "Service found but no URLs provided." }));
698
+ }
677
699
  } else {
678
- response.setHeader("Content-Type", "application/json");
679
- response.end(JSON.stringify(serviceUrls[serviceName]));
700
+ response.statusCode = registryResponse.status;
701
+ response.end();
680
702
  }
681
- return;
703
+ } catch (networkError) {
704
+ console.error(`Error fetching service URLs for ${requestedServiceName}:`, networkError.message);
705
+ response.statusCode = 500;
706
+ response.end();
707
+ }
708
+ }
709
+ function sendFormattedResponse(response, serviceAddresses, isLocalOnly, isRemoteOnly) {
710
+ response.setHeader("Content-Type", "application/json");
711
+ let finalResult = serviceAddresses;
712
+ if (isLocalOnly) {
713
+ finalResult = serviceAddresses.local;
714
+ } else if (isRemoteOnly) {
715
+ finalResult = serviceAddresses.remote;
682
716
  }
683
- response.statusCode = 404;
684
- response.end();
717
+ response.end(JSON.stringify(finalResult));
685
718
  }
686
719
 
687
720
  // Common/Enumerations/ClientConnectionTypeFlags.js
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hivedev/hivesdk",
3
3
  "type": "module",
4
- "version": "1.0.43",
4
+ "version": "1.0.44",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "scripts": {