ipfsd-ctl
Version:
Spawn IPFS Daemons, Kubo or...
58 lines • 1.38 kB
JavaScript
import Hapi from '@hapi/hapi';
import routes from './routes.js';
/**
* Creates an instance of Server
*/
class Server {
options;
server;
port;
host;
ipfsd;
nodes;
constructor(options = { port: 43134, host: 'localhost' }, factory) {
this.options = options;
this.server = null;
this.port = this.options.port ?? 43134;
this.host = this.options.host ?? 'localhost';
this.ipfsd = factory;
this.nodes = {};
}
/**
* Start the server
*/
async start(port = this.port) {
this.port = port;
this.server = new Hapi.Server({
port,
host: this.host,
routes: {
cors: true
}
});
routes(this.server, this.ipfsd, this.nodes);
await this.server.start();
return this;
}
/**
* Stop the server
*/
async stop(options) {
if (this.server != null) {
await this.server.stop(options);
}
await this.clean();
}
/**
* Stop any nodes created by this server
*/
async clean() {
await this.ipfsd.clean();
// remove references to nodes
for (const key of Object.getOwnPropertyNames(this.nodes)) {
delete this.nodes[key];
}
}
}
export default Server;
//# sourceMappingURL=server.js.map