@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
113 lines • 4.58 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dispatcher = void 0;
const fs = __importStar(require("fs"));
const mime_types_1 = __importDefault(require("mime-types"));
const path = __importStar(require("path"));
const util_1 = require("util");
const container_1 = require("../../container");
const not_found_http_error_1 = require("../../errors/not-found-http-error");
const response_1 = require("../response");
const defaultPublicOptions = {
maxage: 0,
gzip: true,
br: true,
};
class Dispatcher {
constructor(request, route) {
this.app = container_1.Container.get('app');
this.request = request;
this.route = route;
this.publicOptions = Object.assign({}, defaultPublicOptions);
}
isReadRequest() {
return this.request.isHead() || this.request.isGet();
}
decodePath(undecodePath) {
try {
return decodeURIComponent(undecodePath);
}
catch (err) {
return false;
}
}
async resolve() {
if (!this.isReadRequest() ||
!this.app.get('config').get('app.public'))
return this.dispatchToRoute();
let publicPrefix = this.app.get('config').get('app.publicPrefix', '');
publicPrefix = publicPrefix.substr(path.parse(publicPrefix).root.length);
let requestPath = this.request.getPath();
requestPath = this.decodePath(requestPath.substr(path.parse(requestPath).root.length));
if (requestPath === false)
return new response_1.Response().BadRequest('failed to decode url');
if (!requestPath.startsWith(publicPrefix))
return this.dispatchToRoute();
const assetsPath = path.relative(publicPrefix, requestPath);
const staticPath = path.resolve(this.app.publicPath, assetsPath);
let stats;
try {
stats = await (0, util_1.promisify)(fs.stat)(staticPath);
}
catch (err) {
if (['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'].includes(err.code)) {
return this.dispatchToRoute();
}
}
if (!stats || (stats === null || stats === void 0 ? void 0 : stats.isDirectory())) {
return this.dispatchToRoute();
}
const response = new response_1.Response();
response.setHeader('Content-Length', stats.size);
if (!this.request.getHeader('Last-Modified'))
response.setHeader('Last-Modified', stats.mtime.toUTCString());
if (!this.request.getHeader('Cache-Control')) {
const directives = [`max-age=${this.publicOptions.maxage / 1000 | 0}`];
response.setHeader('Cache-Control', directives.join(','));
}
response.setHeader('Content-Type', mime_types_1.default.lookup(path.extname(staticPath)));
response.setData(fs.createReadStream(staticPath));
return response;
}
async dispatchToRoute() {
if (!this.route) {
throw this.createNotFountError();
}
return this.route.middleware
.handle(this.request, async (request) => this.route.resolve(request))
.catch((err) => {
throw err;
});
}
createNotFountError() {
return new not_found_http_error_1.NotFoundHttpError('Not Found');
}
}
exports.Dispatcher = Dispatcher;
//# sourceMappingURL=dispatcher.js.map