UNPKG

vhost-ts

Version:

vhost fork made with typescript

1 lines 5.58 kB
{"version":3,"file":"index.modern.mjs","sources":["../src/index.ts"],"sourcesContent":["type vhostPopulation = {\n host: string;\n hostname: string;\n length: number;\n [n: number]: string | undefined;\n};\n\ndeclare global {\n namespace Express {\n interface Request {\n vhost: vhostPopulation;\n }\n }\n}\n\nimport { Request, RequestHandler } from \"express\";\n\nconst ASTERISK_REGEXP = /\\*/g;\nconst ASTERISK_REPLACE = \"([^.]+)\";\nconst END_ANCHORED_REGEXP = /(?:^|[^\\\\])(?:\\\\\\\\)*\\$$/;\nconst ESCAPE_REGEXP = /([.+?^=!:${}()|[\\]/\\\\])/g;\nconst ESCAPE_REPLACE = \"\\\\$1\";\n\n/**\n * Create a vhost middleware.\n *\n * @param {string|RegExp} hostname\n * @param {function} handle\n * @return {Function}\n * @public\n */\n\nexport default (\n hostname: string | RegExp,\n handle: RequestHandler\n): RequestHandler => {\n if (!hostname) {\n throw new TypeError(\"argument hostname is required\");\n }\n\n if (!handle) {\n throw new TypeError(\"argument handle is required\");\n }\n\n if (typeof handle !== \"function\") {\n throw new TypeError(\"argument handle must be a function\");\n }\n\n // create regular expression for hostname\n const regexp = hostregexp(hostname);\n\n const vhost: RequestHandler = (req, res, next) => {\n const vhostdata = vhostof(req, regexp);\n\n if (!vhostdata) {\n return next();\n }\n\n // populate\n req.vhost = vhostdata;\n\n // handle\n handle(req, res, next);\n };\n\n return vhost;\n};\n\n/**\n * Get hostname of request.\n *\n * @param (object} req\n * @return {string}\n * @private\n */\n\nfunction hostnameof(req: Request): string | undefined {\n const host =\n req.hostname || // express v4\n req.host || // express v3\n req.headers.host; // http;\n\n if (!host) {\n return;\n }\n\n const offset = host[0] === \"[\" ? host.indexOf(\"]\") + 1 : 0;\n const index = host.indexOf(\":\", offset);\n\n return index !== -1 ? host.substring(0, index) : host;\n}\n\n/**\n * Generate RegExp for given hostname value.\n *\n * @param (string|RegExp} val\n * @private\n */\n\nfunction hostregexp(val: string | RegExp) {\n let source =\n val instanceof RegExp\n ? val.source\n : String(val)\n .replace(ESCAPE_REGEXP, ESCAPE_REPLACE)\n .replace(ASTERISK_REGEXP, ASTERISK_REPLACE);\n\n // force leading anchor matching\n if (source[0] !== \"^\") {\n source = \"^\" + source;\n }\n\n // force trailing anchor matching\n if (!END_ANCHORED_REGEXP.test(source)) {\n source += \"$\";\n }\n\n return new RegExp(source, \"i\");\n}\n\n/**\n * Get the vhost data of the request for RegExp\n *\n * @param (object} req\n * @param (RegExp} regexp\n * @return {object}\n * @private\n */\n\nfunction vhostof(req: Request, regexp: RegExp): vhostPopulation | undefined {\n const host = req.headers.host;\n const hostname = hostnameof(req);\n\n if (!host || !hostname) {\n return;\n }\n\n const match = regexp.exec(hostname);\n\n if (!match) {\n return;\n }\n\n const matches: { [n: number]: string } = {};\n\n for (let i = 1; i < match.length; i++) {\n matches[i - 1] = match[i];\n }\n return {\n host,\n hostname,\n length: match.length - 1,\n ...matches,\n };\n}\n"],"names":["ASTERISK_REGEXP","ASTERISK_REPLACE","END_ANCHORED_REGEXP","ESCAPE_REGEXP","ESCAPE_REPLACE","hostname","handle","TypeError","regexp","hostregexp","vhost","req","res","next","vhostdata","vhostof","hostnameof","host","headers","offset","indexOf","index","substring","val","source","RegExp","String","replace","test","match","exec","matches","i","length"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,MAAMA,eAAe,GAAG,KAAxB;AACA,MAAMC,gBAAgB,GAAG,SAAzB;AACA,MAAMC,mBAAmB,GAAG,yBAA5B;AACA,MAAMC,aAAa,GAAG,0BAAtB;AACA,MAAMC,cAAc,GAAG,MAAvB;AAWA,aAAe,CACbC,QADa,EAEbC,MAFa;MAIT,CAACD,QAAL,EAAe;UACP,IAAIE,SAAJ,CAAc,+BAAd,CAAN;;;MAGE,CAACD,MAAL,EAAa;UACL,IAAIC,SAAJ,CAAc,6BAAd,CAAN;;;MAGE,OAAOD,MAAP,KAAkB,UAAtB,EAAkC;UAC1B,IAAIC,SAAJ,C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