UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

205 lines (204 loc) 6.65 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var path_exports = {}; __export(path_exports, { default: () => Path }); module.exports = __toCommonJS(path_exports); var import_parsePath = __toESM(require("../functions/parsePath")); var import_types = require("util/types"); class Path { /** * Create a new Path object * @since 8.4.0 */ constructor(method, path) { this.method = method; this.path = path; if ((0, import_types.isRegExp)(path)) { this.data = { type: "regexp", value: path, prefix: "/", segments: [{ raw: "", paramsRegExp: new RegExp(""), params: [] }, { raw: "", paramsRegExp: new RegExp(""), params: [] }] }; } else if (typeof path === "string") { const segments = (0, import_parsePath.default)(path).split("/"); this.data = { type: "normal", value: segments.join("/"), segments: [] }; for (const segment of segments) { this.data.segments.push({ raw: segment, paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, "(.*)")), params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1)) }); } } else { throw new Error(`Invalid Path (${typeof path})`); } } /** * Add a Prefix to the Path * @since 8.4.0 */ addPrefix(prefix) { switch (this.data.type) { case "normal": { const segments = (0, import_parsePath.default)([prefix, this.data.value]).split("/"); this.path = segments.join("/"); this.data = { type: "normal", value: segments.join("/"), segments: [] }; for (const segment of segments) { this.data.segments.push({ raw: segment, paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, "(.*)")), params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1)) }); } break; } case "regexp": { const segments = (0, import_parsePath.default)([prefix, this.data.prefix]).split("/"); this.data = { type: "regexp", value: this.data.value, prefix: segments.join("/"), segments: [] }; for (const segment of segments) { this.data.segments.push({ raw: segment, paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, "(.*)")), params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1)) }); } break; } } return this; } /** * Add a Suffix to the Path * @since 8.4.0 */ addSuffix(prefix) { switch (this.data.type) { case "normal": { const segments = (0, import_parsePath.default)([this.data.value, prefix]).split("/"); this.path = segments.join("/"); this.data = { type: "normal", value: segments.join("/"), segments: [] }; for (const segment of segments) { this.data.segments.push({ raw: segment, paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, "(.*)")), params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1)) }); } break; } case "regexp": { const segments = (0, import_parsePath.default)([this.data.prefix, prefix]).split("/"); this.data = { type: "regexp", value: this.data.value, prefix: segments.join("/"), segments: [] }; for (const segment of segments) { this.data.segments.push({ raw: segment, paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, "(.*)")), params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1)) }); } break; } } return this; } /** * Test the Path against the Request Path * @since 8.4.0 */ matches(method, collection, requestPath, requestPathSplit) { if (this.method !== method) return false; if (this.data.type === "normal" && this.data.segments.length !== requestPathSplit.length) return false; if (this.data.type === "regexp" && !this.data.value.test((0, import_parsePath.default)(requestPath.replace(this.data.prefix, "")))) return false; let found = false; for (let i = 1; i < requestPathSplit.length; i++) { const reqSegment = requestPathSplit[i], segment = this.data.segments[i]; if (!segment) { if (this.data.type === "normal") break; found = true; break; } if (segment.params.length === 0) { if (segment.raw === reqSegment) { if (i === requestPathSplit.length - 1 && this.data.type === "normal") { found = true; break; } } else break; } const params = reqSegment.match(segment.paramsRegExp); if (params) { if (i === requestPathSplit.length) found = true; for (let i2 = 0; i2 < segment.params.length; i2++) { collection.set(segment.params[i2], params[i2 + 1]); } } else break; } return found; } }