@iota-big3/sdk-production
Version:
Production readiness tools and utilities for SDK
342 lines • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductionManager = void 0;
exports.createProductionManager = createProductionManager;
const events_1 = require("events");
class HealthChecker {
constructor() {
this.checks = new Map();
}
register(name, check) {
this.checks.set(name, check);
}
async runCheck(name) {
const check = this.checks.get(name);
if (!check) {
return {
status: 'unhealthy',
message: `Check '${name}' not found`,
timestamp: new Date()
};
}
const startTime = Date.now();
try {
const status = await check();
return {
status,
message: `Check '${name}' ${status}`,
duration: Date.now() - startTime,
timestamp: new Date()
};
}
catch (error) {
return {
status: 'unhealthy',
message: `Check '${name}' failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
duration: Date.now() - startTime,
timestamp: new Date(),
error: error instanceof Error ? error : new Error('Unknown error')
};
}
}
async runAllChecks() {
const results = new Map();
for (const [name, _check] of this.checks) {
results.set(name, await this.runCheck(name));
}
return results;
}
}
class ProductionManager extends events_1.EventEmitter {
constructor() {
super();
this._isEnabled = true;
this.healthChecker = new HealthChecker();
this.initialize();
}
async initialize() {
this.log('info', 'Initializing Production Manager');
this.registerDefaultHealthChecks();
this.emit('initialized');
}
registerDefaultHealthChecks() {
this.healthChecker.register('memory', async () => {
const usage = process.memoryUsage();
const percentUsed = (usage.heapUsed / usage.heapTotal) * 100;
if (percentUsed > 90)
return 'unhealthy';
if (percentUsed > 70)
return 'degraded';
return 'healthy';
});
this.healthChecker.register('process', async () => {
return process.uptime() > 0 ? 'healthy' : 'unhealthy';
});
this.healthChecker.register('system', async () => {
return 'healthy';
});
}
configureHealthChecks(config) {
if (!this._isEnabled) {
throw new Error('Production Manager is disabled');
}
if (config.interval && config.interval > 0) {
this.startHealthMonitoring(config.interval);
}
if (config.checks) {
for (const check of config.checks) {
this.registerHealthCheck(check.name, check.check);
}
}
this.log('info', 'Health checks configured', {
interval: config.interval,
checkCount: config.checks?.length || 0
});
}
registerHealthCheck(name, check) {
this.healthChecker.register(name, check);
this.log('info', `Health check '${name}' registered`);
}
async runHealthCheck(name) {
try {
if (!this._isEnabled) {
throw new Error('Production Manager is disabled');
}
if (name) {
const result = await this.healthChecker.runCheck(name);
this.emitProductionEvent('health.check.complete', { name, result });
return {
success: true,
data: result
};
}
else {
const results = await this.healthChecker.runAllChecks();
this.emitProductionEvent('health.check.complete', {
all: true,
count: results.size
});
return {
success: true,
data: results
};
}
}
catch (error) {
return {
success: false,
error: {
code: 'HEALTH_CHECK_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
}
};
}
}
startHealthMonitoring(interval) {
if (this.checkInterval) {
clearInterval(this.checkInterval);
}
this.checkInterval = setInterval(async () => {
if (this._isEnabled) {
await this.runHealthCheck();
}
}, interval);
this.log('info', 'Started health monitoring', { interval });
}
configureApiDocs(config) {
this.apiDocConfig = config;
this.log('info', 'API documentation configured');
}
async generateApiDocs() {
try {
if (!this._isEnabled) {
throw new Error('Production Manager is disabled');
}
if (!this.apiDocConfig) {
throw new Error('API documentation not configured');
}
const outputPath = this.apiDocConfig.outputDir || './docs';
this.emitProductionEvent('docs.generated', {
outputPath,
config: this.apiDocConfig
});
return {
success: true,
data: outputPath
};
}
catch (error) {
return {
success: false,
error: {
code: 'API_DOC_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
}
};
}
}
configurePlayground(config) {
this.playgroundConfig = config;
this.log('info', 'SDK playground configured');
}
async startPlayground() {
try {
if (!this._isEnabled) {
throw new Error('Production Manager is disabled');
}
if (!this.playgroundConfig) {
throw new Error('Playground not configured');
}
const { port = 3000, host = 'localhost' } = this.playgroundConfig;
const url = `http://${host}:${port}`;
this.emitProductionEvent('playground.started', { url, config: this.playgroundConfig });
return {
success: true,
data: { url }
};
}
catch (error) {
return {
success: false,
error: {
code: 'PLAYGROUND_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
}
};
}
}
configureRelease(_config) {
this.log('info', 'Release management configured');
}
async createRelease(version, notes) {
try {
if (!this._isEnabled) {
throw new Error('Production Manager is disabled');
}
const release = {
version,
date: new Date(),
notes: notes || '',
changelog: this.generateChangelog(version),
artifacts: []
};
this.emitProductionEvent('release.created', { release });
return {
success: true,
data: release
};
}
catch (error) {
return {
success: false,
error: {
code: 'RELEASE_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
}
};
}
}
generateChangelog(version) {
return `# Changelog for version ${version}\n\n- Various improvements and bug fixes`;
}
generateDeploymentChecklist() {
const checklist = [
{
category: 'pre-deployment',
item: 'Run all tests',
required: true,
checked: false
},
{
category: 'pre-deployment',
item: 'Update documentation',
required: true,
checked: false
},
{
category: 'deployment',
item: 'Tag release',
required: true,
checked: false
},
{
category: 'deployment',
item: 'Deploy to staging',
required: true,
checked: false
},
{
category: 'post-deployment',
item: 'Verify deployment',
required: true,
checked: false
},
{
category: 'post-deployment',
item: 'Monitor metrics',
required: false,
checked: false
}
];
this.emitProductionEvent('checklist.generated', { count: checklist.length });
return checklist;
}
validateProductionConfig(config) {
const errors = [];
if (!config) {
errors.push('Configuration is required');
}
else {
if (!config.name)
errors.push('Name is required');
if (!config.version)
errors.push('Version is required');
if (!config.environment)
errors.push('Environment is required');
if (config.environment && !['development', 'staging', 'production'].includes(config.environment)) {
errors.push('Invalid environment');
}
}
return {
success: errors.length === 0,
data: {
valid: errors.length === 0,
errors
}
};
}
enable() {
this._isEnabled = true;
this.log('info', 'Production Manager enabled');
}
disable() {
this._isEnabled = false;
this.log('info', 'Production Manager disabled');
}
get isEnabled() {
return this._isEnabled;
}
destroy() {
if (this.checkInterval) {
clearInterval(this.checkInterval);
}
this.removeAllListeners();
}
emitProductionEvent(type, data = {}) {
const event = {
type,
timestamp: new Date(),
data
};
this.emit('production:event', event);
this.emit(type, event);
}
log(level, message, data) {
console.log(`[ProductionManager] ${level.toUpperCase()}: ${message}`, data || '');
}
}
exports.ProductionManager = ProductionManager;
function createProductionManager() {
return new ProductionManager();
}
//# sourceMappingURL=production-manager.js.map