swaf/dist/components/MaintenanceComponent.js
2020-04-22 17:49:58 +02:00

42 lines
5.1 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
import { ServiceUnavailableHttpError } from "../HttpError";
export default class MaintenanceComponent extends ApplicationComponent {
constructor(application, canServe) {
super();
this.application = application;
this.canServe = canServe;
}
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
router.use((req, res, next) => {
if (res.headersSent) {
return next();
}
if (!this.application.isReady()) {
res.header({ 'Retry-After': 60 });
res.locals.refresh_after = 5;
throw new ServiceUnavailableHttpError('Watch My Stream is readying up. Please wait a few seconds...');
}
if (!this.canServe()) {
res.locals.refresh_after = 30;
throw new ServiceUnavailableHttpError('Watch My Stream is unavailable due to failure of dependent services.');
}
next();
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTWFpbnRlbmFuY2VDb21wb25lbnQuanMiLCJzb3VyY2VSb290IjoiLi8iLCJzb3VyY2VzIjpbImNvbXBvbmVudHMvTWFpbnRlbmFuY2VDb21wb25lbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O0FBQUEsT0FBTyxvQkFBb0IsTUFBTSx5QkFBeUIsQ0FBQztBQUUzRCxPQUFPLEVBQUMsMkJBQTJCLEVBQUMsTUFBTSxjQUFjLENBQUM7QUFHekQsTUFBTSxDQUFDLE9BQU8sT0FBTyxvQkFBcUIs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