feat: consolidate utility functions into P2NS Plugin SDK

- Centralized common formatting, DOM, and status utilities in `includes/plugins/sdk.js`
- Created `sdk.utils.format`, `sdk.utils.dom`, and `sdk.utils.status` namespaces
- Refactored `domain.consensus` and `peer.visualize` plugins to use the global SDK
- Updated `plugin-handler` to serve SDK utilities globally via `/sdk-utils.js`
- Enhanced SDK validation by delegating to the core validation infrastructure
- Fixed bugs in metric rendering and missing frontend utility functions
This commit is contained in:
Raven Scott
2025-12-17 22:21:58 -05:00
parent 5f5ed3848e
commit ad4784d05d
12 changed files with 1090 additions and 373 deletions
+4 -11
View File
@@ -2,6 +2,7 @@ const { createBackup, listBackups, restoreBackup, cleanupOldBackups } = require(
const { logError } = require('../../../infrastructure/logger');
const { trackRequest, trackRequestWithTiming } = require('../../../maintenance/metrics');
const { createErrorResponse } = require('../../../infrastructure/error_handler');
const sdk = require('../../../plugins/sdk');
const fs = require('fs').promises;
const path = require('path');
@@ -20,7 +21,7 @@ async function handleBackupsRoutes(req, res) {
return {
...backup,
size: backup.size || 0,
sizeFormatted: formatBytes(backup.size || 0)
sizeFormatted: sdk.utils.format.formatBytes(backup.size || 0)
};
});
@@ -218,14 +219,14 @@ async function handleBackupsRoutes(req, res) {
return {
name: fileName,
size: stats.size,
sizeFormatted: formatBytes(stats.size),
sizeFormatted: sdk.utils.format.formatBytes(stats.size),
modified: stats.mtime.toISOString()
};
} catch (err) {
return {
name: fileName,
size: 0,
sizeFormatted: '0 B',
sizeFormatted: '0 Bytes',
modified: null
};
}
@@ -264,13 +265,5 @@ async function handleBackupsRoutes(req, res) {
return false;
}
function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}
module.exports = { handleBackupsRoutes };
@@ -565,10 +565,12 @@ async function handleSettingsRoutes(req, res) {
}));
// Trigger graceful shutdown after sending response
// Add a longer delay to ensure storage cleanup is complete and state is reset
setTimeout(() => {
logInfo('Admin', 'Initiating graceful shutdown after DNS storage cleanup...');
logInfo('Admin', '🔄 SYSTEM IS SHUTTING DOWN - Storage has been cleaned and will reinitialize on restart');
process.emit('SIGTERM');
}, 1000); // Give time for response to be sent
}, 3000); // Give more time for response to be sent and cleanup to complete
} catch (err) {
logError('Admin', `Failed to clean DNS storage: ${err.message}`);
res.writeHead(500, { 'Content-Type': 'application/json' });