UNPKG

web-dev-server

Version:

Node.js simple http server for common development or training purposes.

167 lines 7.04 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.Params = void 0; var tslib_1 = require("tslib"); var StringHelper_1 = require("../Tools/Helpers/StringHelper"); var Protected_1 = require("./Protected"); var Params = /** @class */ (function () { function Params() { } Params.prototype.IsCompleted = function () { var httpReq = this['http']; return httpReq.complete; }; Params.prototype.GetBody = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var httpReq; var _this = this; return tslib_1.__generator(this, function (_a) { httpReq = this['http']; return [2 /*return*/, new Promise(function (resolve, reject) { if (httpReq.complete) resolve(_this.body); httpReq.on('body-loaded', function () { resolve(_this.body); }); })]; }); }); }; Params.prototype.SetParams = function (params) { this.params = params; return this; }; Params.prototype.GetParams = function (nameReplaceFilter, valueReplaceFilter, onlyNames) { if (nameReplaceFilter === void 0) { nameReplaceFilter = "\-\._a-zA-Z0-9"; } if (valueReplaceFilter === void 0) { valueReplaceFilter = { pattern: /[\<\>\'"]/g, replace: '' }; } if (onlyNames === void 0) { onlyNames = []; } if (this.params == null) this.initParams(); var result = {}, cleanedName, noNameFiltering = !nameReplaceFilter || nameReplaceFilter === '.*', noValueFiltering = !valueReplaceFilter || valueReplaceFilter === '.*'; if (noNameFiltering && noValueFiltering) { if (onlyNames.length > 0) { for (var name in this.params) { if (onlyNames.indexOf(name) != -1) result[name] = this.params[name]; } } else { result = this.params; } return result; } for (var name in this.params) { cleanedName = noNameFiltering ? name : Protected_1.Protected.CleanParamValue(name, nameReplaceFilter); result[cleanedName] = noValueFiltering ? this.params[name] : Protected_1.Protected.GetParamFromCollection(this.params, name, nameReplaceFilter, valueReplaceFilter, null); } return result; }; Params.prototype.SetParam = function (name, value) { if (this.params == null) this.initParams(); this.params[name] = value; return this; }; Params.prototype.GetParam = function (name, valueReplaceFilter, ifNullValue) { if (valueReplaceFilter === void 0) { valueReplaceFilter = "a-zA-Z0-9_;, /\-\@\:"; } if (ifNullValue === void 0) { ifNullValue = null; } if (this.params == null) this.initParams(); return Protected_1.Protected.GetParamFromCollection(this.params, name, false, valueReplaceFilter, ifNullValue); }; Params.prototype.RemoveParam = function (name) { if (this.params == null) this.initParams(); delete this.params[name]; return this; }; Params.prototype.HasParam = function (name) { if (this.params == null) this.initParams(); return name in this.params; }; /** * Initialize params from GET (or also from post, if request is already completed). */ Params.prototype.initParams = function () { var httpReq = this['http'], postParams, getParams, method; getParams = StringHelper_1.StringHelper.QueryStringDecode(this['query'], false); this.params = getParams; if (!httpReq.complete) return; method = httpReq.method.toUpperCase(); if (method != 'POST' && method != 'PUT') return; var contentType = this['GetHeader']('content-type'); var multiPartHeader = 'multipart/form-data'; var multiPartContent = contentType.indexOf(multiPartHeader) != -1; // @see https://stackoverflow.com/a/37046109/7032987 if (!multiPartContent) postParams = this.parseBodyParams(contentType); if (postParams == null) return; for (var key in postParams) this.params[key] = postParams[key]; }; /** * @summary Read and return unserialized POST/PUT request body by "content-type" header. */ Params.prototype.parseBodyParams = function (contentType) { if (this.body == null) return null; var result = null, httpReq = this['http'], server = httpReq.socket['server']['__wds'], // @ts-ignore errorsHandler = server.errorsHandler; var urlEncType = contentType.indexOf('application/x-www-form-urlencoded') != -1; if (urlEncType) { try { result = StringHelper_1.StringHelper.QueryStringDecode(this.body, false); } catch (e1) { errorsHandler.LogError(e1, 500, this, this.response); } } else { var jsonType = (contentType.indexOf('application/json') != -1 || contentType.indexOf('text/javascript') != -1 || contentType.indexOf('application/ld+json') != -1); if (jsonType) { try { result = JSON.parse(this.body); } catch (e2) { errorsHandler.LogError(e2, 500, this, this.response); } } else { // if content type header is not recognized, // try JSON decoding first, then fallback to query string: var probablyAJsonType = !StringHelper_1.StringHelper.IsQueryString(this.body); if (probablyAJsonType) { try { result = JSON.parse(this.body); } catch (e3) { errorsHandler.LogError(e3, 500, this, this.response); probablyAJsonType = false; // fall back to query string parsing } } if (!probablyAJsonType) { try { result = StringHelper_1.StringHelper.QueryStringDecode(this.body, false); } catch (e4) { errorsHandler.LogError(e4, 500, this, this.response); } } } } return result; }; return Params; }()); exports.Params = Params; //# sourceMappingURL=Params.js.map