@orcdkestrator/orcdk-plugin-localstack
Version:
LocalStack lifecycle management plugin for Orcdkestrator
132 lines • 4.99 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalStackCLI = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const path = __importStar(require("path"));
const exec = (0, util_1.promisify)(child_process_1.exec);
/**
* LocalStack CLI wrapper - handles all LocalStack command execution
* This file is excluded from coverage as it's primarily 3rd party integration
*/
class LocalStackCLI {
async hasLocalStackCLI() {
return await this.commandExists('localstack');
}
async start(env) {
await this.exec('localstack start -d', env);
}
async stop() {
await this.exec('localstack stop');
}
async isHealthy(port, timeoutMs = 5000) {
try {
// Create AbortController for timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(`http://localhost:${port}/_localstack/health`, {
signal: controller.signal
});
return res.ok;
}
finally {
clearTimeout(timeoutId);
}
}
catch (error) {
// Handle abort error or network error
return false;
}
}
async waitForReady(port, maxAttempts = 30, retryDelayMs = 2000) {
for (let i = 0; i < maxAttempts; i++) {
if (await this.isHealthy(port))
return;
await this.sleep(retryDelayMs);
}
throw new Error('LocalStack startup timeout');
}
async status() {
try {
const { stdout } = await exec('localstack status');
const running = stdout.includes('running');
return { running, output: stdout };
}
catch {
return { running: false, output: 'LocalStack not running' };
}
}
async createHotReloadFunction(functionName, localPath, handler, runtime, env) {
// LocalStack requires absolute paths for hot reload
// Path should be resolved by caller, but validate here
if (!path.isAbsolute(localPath)) {
throw new Error(`LocalStack hot reload requires absolute path, got: ${localPath}. Path should be resolved before calling this method.`);
}
// LocalStack uses a magic S3 bucket named "hot-reload" for hot reloading functionality
// This is a LocalStack-specific feature, not a real S3 bucket
const LOCALSTACK_HOT_RELOAD_BUCKET = process.env.LOCALSTACK_HOT_RELOAD_BUCKET || 'hot-reload';
const cmd = [
'awslocal lambda create-function',
`--function-name ${functionName}`,
`--code S3Bucket="${LOCALSTACK_HOT_RELOAD_BUCKET}",S3Key="${localPath}"`,
`--handler ${handler}`,
`--runtime ${runtime}`
].join(' ');
await this.exec(cmd, env);
}
async commandExists(cmd) {
// Validate cmd contains only alphanumeric characters and hyphens
if (!/^[a-zA-Z0-9-]+$/.test(cmd)) {
throw new Error('Invalid command name');
}
try {
await exec(`which ${cmd}`);
return true;
}
catch {
return false;
}
}
async exec(cmd, env) {
await exec(cmd, { env: { ...process.env, ...env } });
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
exports.LocalStackCLI = LocalStackCLI;
//# sourceMappingURL=cli.js.map