release/2026-04-03 - Clean up localstack endpoints / env check

This commit is contained in:
Dave
2026-04-01 14:39:34 -04:00
parent efdcd06921
commit 7688f22161
7 changed files with 73 additions and 44 deletions

View File

@@ -7,14 +7,24 @@
* @property { string | object | function } promanager Return this prop if Rome.
* @property { string | object | function } imex Return this prop if Rome.
*/
const { isString, isEmpty } = require("lodash");
function InstanceManager({ args, instance, debug, executeFunction, rome, promanager, imex }) {
/**
* InstanceManager is a utility function that determines which property to return based on the current instance type.
* @param param0
* @param param0.args
* @param param0.instance
* @param param0.debug
* @param param0.executeFunction
* @param param0.rome
* @param param0.promanager
* @param param0.imex
* @returns {*|null}
* @constructor
*/
const InstanceManager = ({ args, instance, debug, executeFunction, rome, promanager, imex }) => {
let propToReturn = null;
//TODO: Remove after debugging.
if (promanager) {
console.trace("ProManager Prop was used");
}
switch (instance || process.env.INSTANCE) {
case "IMEX":
propToReturn = imex;
@@ -50,15 +60,42 @@ function InstanceManager({ args, instance, debug, executeFunction, rome, promana
}
if (executeFunction && typeof propToReturn === "function") return propToReturn(...args);
return propToReturn === undefined ? null : propToReturn;
}
};
exports.InstanceRegion = () =>
/**
* Returns the AWS region to be used for the current instance, which is determined by the INSTANCE environment variable.
* @returns {*}
* @constructor
*/
const InstanceRegion = () =>
InstanceManager({
imex: "ca-central-1",
rome: "us-east-2"
});
exports.InstanceEndpoints = () =>
/**
* Checks if the instance is configured to use LocalStack by verifying the presence of the LOCALSTACK_HOSTNAME
* environment variable.
* @returns {boolean}
* @constructor
*/
const InstanceIsLocalStackEnabled = () =>
isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
/**
* Returns the LocalStack endpoint URL based on the LOCALSTACK_HOSTNAME environment variable.
* @returns {`http://${*}:4566`}
* @constructor
*/
const InstanceLocalStackEndpoint = () => `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
/**
* Returns the appropriate endpoints for the current instance, which can be used for making API calls or other network
* requests.
* @returns {*|null}
* @constructor
*/
const InstanceEndpoints = () =>
InstanceManager({
imex:
process.env?.NODE_ENV === "development"
@@ -74,4 +111,11 @@ exports.InstanceEndpoints = () =>
: "https://romeonline.io"
});
exports.default = InstanceManager;
module.exports = {
InstanceManager,
InstanceRegion,
InstanceIsLocalStackEnabled,
InstanceLocalStackEndpoint,
InstanceEndpoints,
default: InstanceManager
};

View File

@@ -2,10 +2,9 @@
const InstanceManager = require("../utils/instanceMgr").default;
const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");
const { isString, isEmpty } = require("lodash");
const { uploadFileToS3 } = require("./s3");
const { v4 } = require("uuid");
const { InstanceRegion } = require("./instanceMgr");
const { InstanceRegion, InstanceIsLocalStackEnabled, InstanceLocalStackEndpoint } = require("./instanceMgr");
const getHostNameOrIP = require("./getHostNameOrIP");
const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
@@ -48,7 +47,7 @@ const normalizeLevel = (level) => (level ? level.toLowerCase() : LOG_LEVELS.debu
const createLogger = () => {
try {
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
const isLocal = InstanceIsLocalStackEnabled();
const logGroupName = isLocal ? "development" : process.env.CLOUDWATCH_LOG_GROUP;
const winstonCloudwatchTransportDefaults = {
@@ -60,7 +59,7 @@ const createLogger = () => {
};
if (isLocal) {
winstonCloudwatchTransportDefaults.awsOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
winstonCloudwatchTransportDefaults.awsOptions.endpoint = InstanceLocalStackEndpoint();
}
const levelFilter = (levels) => {

View File

@@ -7,8 +7,7 @@ const {
CopyObjectCommand
} = require("@aws-sdk/client-s3");
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const { InstanceRegion } = require("./instanceMgr");
const { isString, isEmpty } = require("lodash");
const { InstanceRegion, InstanceIsLocalStackEnabled, InstanceLocalStackEndpoint } = require("./instanceMgr");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
const createS3Client = () => {
@@ -17,10 +16,8 @@ const createS3Client = () => {
credentials: defaultProvider()
};
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
if (isLocal) {
S3Options.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
if (InstanceIsLocalStackEnabled()) {
S3Options.endpoint = InstanceLocalStackEndpoint();
S3Options.forcePathStyle = true; // Needed for LocalStack to avoid bucket name as hostname
}
@@ -105,7 +102,7 @@ const createS3Client = () => {
});
const presignedUrl = await getSignedUrl(s3Client, command, { expiresIn: 360 });
return presignedUrl;
}
};
return {
uploadFileToS3,
@@ -119,7 +116,4 @@ const createS3Client = () => {
};
};
module.exports = createS3Client();