@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
83 lines • 3.79 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxyMiddleware = void 0;
const is_glob_1 = __importDefault(require("is-glob"));
const micromatch_1 = __importDefault(require("micromatch"));
const url_1 = __importDefault(require("url"));
const decorators_1 = require("../../../decorators");
const helpers_1 = require("../../../helpers");
const proxy_1 = require("../../../http/proxy");
let ProxyMiddleware = class ProxyMiddleware {
constructor(options) {
this.options = options;
}
async resolve(request, next) {
let shouldProxyOnWhen = true;
if (typeof this.options.when === 'function') {
if (this.options.when(request, (0, helpers_1.app)()) === false)
shouldProxyOnWhen = false;
}
if (shouldProxyOnWhen && this.match(this.options.context, request.url)) {
if (this.options.only && Array.isArray(this.options.only) && this.options.only.length) {
const hasOnly = this.options.only.some((uri) => this.match(uri, request.url));
if (!hasOnly)
return next();
}
if (this.options.except && Array.isArray(this.options.except) && this.options.except.length) {
const hasExcept = this.options.except.some((uri) => this.match(uri, request.url));
if (hasExcept)
return next();
}
const proxy = new proxy_1.HttpProxy()
.changeOrigin()
.target(typeof this.options.target === 'function' ? this.options.target() : this.options.target);
if (!this.options.rewrite)
this.options.rewrite = (p) => p;
if (typeof this.options.rewrite === 'function') {
proxy.rewrite(this.options.rewrite);
}
return proxy;
}
return next();
}
match(p, uri) {
var _a, _b;
if (this.isStringPath(p)) {
const pathname = (_a = this.getUrlPathName(uri)) !== null && _a !== void 0 ? _a : '';
return pathname.indexOf(p) === 0;
}
if (this.isGlobPath(p)) {
const pathname = (_b = this.getUrlPathName(uri)) !== null && _b !== void 0 ? _b : '';
const matches = (0, micromatch_1.default)([pathname], p);
return matches && matches.length > 0;
}
return false;
}
getUrlPathName(uri) {
return uri && url_1.default.parse(uri).pathname;
}
isStringPath(p) {
return typeof p === 'string' && !(0, is_glob_1.default)(p);
}
isGlobPath(p) {
return (0, is_glob_1.default)(p);
}
};
ProxyMiddleware = __decorate([
(0, decorators_1.Middleware)(),
__metadata("design:paramtypes", [Object])
], ProxyMiddleware);
exports.ProxyMiddleware = ProxyMiddleware;
//# sourceMappingURL=proxy.js.map