pandora
Version:
53 lines • 1.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const pandora_dollar_1 = require("pandora-dollar");
/**
* Abstract class, provide basic HTTP behaviours
*/
class HTTPApplet {
constructor() {
this.type = 'HTTP';
}
/**
* Implement Applet.start(), it will create a server and listening on the port ( given by this.getPort() )
* @returns {Promise<any>}
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
/* istanbul ignore next */
if (this.server) {
yield this.stop();
}
this.server = this.createServer();
return pandora_dollar_1.promise.fromCallback(cb => this.server.listen(this.getPort(), cb));
});
}
/**
* Implement Applet.stop(), it will close the server
* @returns {Promise<any>}
*/
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (this.server) {
return pandora_dollar_1.promise.fromCallback(cb => this.server.close(cb));
}
});
}
/**
* Get which port should listening on
* @returns {number}
*/
getPort /* istanbul ignore next */() {
return process.env.PORT || 6001;
}
}
exports.HTTPApplet = HTTPApplet;
//# sourceMappingURL=HTTPApplet.js.map