dwnpm
Version:
Decentralized Registry Package Manager (DRPM) helps developers publish, install, find and manage Decentralized Packages (DPKs) published to Decentralized Web Nodes (DWNs). DRPM does this by looking up a Decentralized Identifier (DID) to find its DID docum
73 lines • 2.51 kB
JavaScript
import cors from 'cors';
import express from 'express';
import http from 'http';
import { DRPM_REGISTRY_URL } from '../config.js';
import { Logger } from '../utils/logger.js';
import handlers from './handlers.js';
export class Registry {
app;
server;
port;
constructor(port = process.env.PORT || 2092) {
this.app = express();
this.port = this.normalizePort(port);
this.app.set('port', this.port);
// Load configurations and middleware here
this.loadConfigs();
this.setupRoutes();
}
// Initialize configurations and middleware
loadConfigs() {
this.app.use(cors());
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: true }));
this.app.use(express.raw({ type: 'application/octet-stream', limit: '10gb' }));
this.app.use((req, _, next) => {
req.url = decodeURIComponent(req.url);
Logger.log(`${req.method} ${req.url}`);
next();
});
}
// Define routes or import from external routes file
setupRoutes() {
// Assuming registry has specific routes
this.app.use(handlers); // Use routes from handlers
}
// Start the server for development or production
start() {
this.server = http.createServer(this.app);
this.server.listen(this.port);
this.server.on('error', this.onError.bind(this));
this.server.on('listening', this.onListening.bind(this));
}
// Normalize port for consistency
normalizePort(val) {
const port = typeof val === 'string' ? parseInt(val, 10) : val;
return isNaN(port) ? val : port >= 0 ? port : false;
}
onError(error) {
if (error.syscall !== 'listen')
throw error;
const bind = typeof this.port === 'string' ? `Pipe ${this.port}` : `Port ${this.port}`;
switch (error.code) {
case 'EACCES':
Logger.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
Logger.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
onListening() {
Logger.log(`Listening on ${DRPM_REGISTRY_URL}`);
}
// Expose Express instance for external configuration if needed
getApp() {
return this.app;
}
}
//# sourceMappingURL=registry.js.map