| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901 |
- "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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
- 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);
- // src/index.js
- var index_exports = {};
- __export(index_exports, {
- BodyParser: () => BodyParser,
- CHARACTER_ENCODING_LIST: () => CHARACTER_ENCODING_LIST,
- CookiesParser: () => CookiesParser,
- DataSender: () => DataSender,
- EXPOSED_ERROR_PROPERTIES: () => EXPOSED_ERROR_PROPERTIES,
- ErrorSender: () => ErrorSender,
- HookInvoker: () => HookInvoker,
- HookRegistry: () => HookRegistry,
- HttpMethod: () => HttpMethod,
- METHODS_WITH_BODY: () => METHODS_WITH_BODY,
- QueryParser: () => QueryParser,
- RequestContext: () => RequestContext,
- RequestParser: () => RequestParser,
- Route: () => Route,
- RouteRegistry: () => RouteRegistry,
- RouterHookType: () => RouterHookType,
- RouterOptions: () => RouterOptions,
- TrieRouter: () => TrieRouter,
- UNPARSABLE_MEDIA_TYPES: () => UNPARSABLE_MEDIA_TYPES,
- cloneDeep: () => cloneDeep,
- createCookiesString: () => createCookiesString,
- createDebugger: () => createDebugger,
- createError: () => createError,
- createRequestMock: () => createRequestMock,
- createResponseMock: () => createResponseMock,
- createRouteMock: () => createRouteMock,
- fetchRequestBody: () => fetchRequestBody,
- getRequestPathname: () => getRequestPathname,
- isPromise: () => isPromise,
- isReadableStream: () => isReadableStream,
- isResponseSent: () => isResponseSent,
- isWritableStream: () => isWritableStream,
- parseContentType: () => parseContentType,
- parseCookies: () => parseCookies,
- parseJsonBody: () => parseJsonBody,
- toCamelCase: () => toCamelCase
- });
- module.exports = __toCommonJS(index_exports);
- // src/route.js
- var import_js_format13 = require("@e22m4u/js-format");
- var import_js_debug = require("@e22m4u/js-debug");
- // src/hooks/hook-invoker.js
- var import_js_format12 = require("@e22m4u/js-format");
- // src/debuggable-service.js
- var import_js_service = require("@e22m4u/js-service");
- var MODULE_DEBUG_NAMESPACE = "jsTrieRouter";
- var _DebuggableService = class _DebuggableService extends import_js_service.DebuggableService {
- /**
- * Constructor.
- *
- * @param {ServiceContainer} container
- */
- constructor(container = void 0) {
- super(container, {
- namespace: MODULE_DEBUG_NAMESPACE,
- noEnvironmentNamespace: true
- });
- }
- };
- __name(_DebuggableService, "DebuggableService");
- var DebuggableService = _DebuggableService;
- // src/utils/clone-deep.js
- function cloneDeep(value) {
- if (value == null || typeof value !== "object") {
- return value;
- }
- if (value instanceof Date) {
- return new Date(value.getTime());
- }
- if (Array.isArray(value)) {
- return value.map((item) => cloneDeep(item));
- }
- const proto = Object.getPrototypeOf(value);
- if (proto === Object.prototype || proto === null) {
- const newObj = proto === null ? /* @__PURE__ */ Object.create(null) : {};
- for (const key in value) {
- if (Object.prototype.hasOwnProperty.call(value, key)) {
- newObj[key] = cloneDeep(value[key]);
- }
- }
- return newObj;
- }
- return value;
- }
- __name(cloneDeep, "cloneDeep");
- // src/utils/is-promise.js
- function isPromise(value) {
- if (!value) return false;
- if (typeof value !== "object") return false;
- return typeof value.then === "function";
- }
- __name(isPromise, "isPromise");
- // src/utils/create-error.js
- var import_js_format = require("@e22m4u/js-format");
- function createError(errorCtor, message, ...args) {
- if (typeof errorCtor !== "function")
- throw new import_js_format.Errorf(
- 'The first argument of "createError" should be a constructor, but %v was given.',
- errorCtor
- );
- if (message != null && typeof message !== "string")
- throw new import_js_format.Errorf(
- 'The second argument of "createError" should be a String, but %v was given.',
- message
- );
- if (message == null) return new errorCtor();
- const interpolatedMessage = (0, import_js_format.format)(message, ...args);
- return new errorCtor(interpolatedMessage);
- }
- __name(createError, "createError");
- // src/utils/parse-cookies.js
- var import_js_format2 = require("@e22m4u/js-format");
- function parseCookies(input) {
- if (typeof input !== "string")
- throw new import_js_format2.Errorf(
- 'The first parameter of "parseCookies" should be a String, but %v was given.',
- input
- );
- return input.split(";").filter((v) => v !== "").map((v) => v.split("=")).reduce((cookies, tuple) => {
- const key = decodeURIComponent(tuple[0]).trim();
- const value = tuple[1] !== void 0 ? decodeURIComponent(tuple[1]).trim() : "";
- cookies[key] = value;
- return cookies;
- }, {});
- }
- __name(parseCookies, "parseCookies");
- // src/utils/to-camel-case.js
- var import_js_format3 = require("@e22m4u/js-format");
- function toCamelCase(input) {
- if (typeof input !== "string")
- throw new import_js_format3.Errorf(
- 'The first argument of "toCamelCase" should be a String, but %v was given.',
- input
- );
- return input.replace(/(^\w|[A-Z]|\b\w)/g, (c) => c.toUpperCase()).replace(/\W+/g, "").replace(/(^\w)/g, (c) => c.toLowerCase());
- }
- __name(toCamelCase, "toCamelCase");
- // src/utils/create-debugger.js
- var import_debug = __toESM(require("debug"), 1);
- var import_js_format4 = require("@e22m4u/js-format");
- function createDebugger(name) {
- if (typeof name !== "string")
- throw new import_js_format4.Errorf(
- 'The first argument of "createDebugger" should be a String, but %v was given.',
- name
- );
- const debug = (0, import_debug.default)(`jsTrieRouter:${name}`);
- return function(message, ...args) {
- const interpolatedMessage = (0, import_js_format4.format)(message, ...args);
- return debug(interpolatedMessage);
- };
- }
- __name(createDebugger, "createDebugger");
- // src/utils/is-response-sent.js
- var import_js_format5 = require("@e22m4u/js-format");
- function isResponseSent(response) {
- if (!response || typeof response !== "object" || Array.isArray(response) || typeof response.headersSent !== "boolean") {
- throw new import_js_format5.Errorf(
- 'The first argument of "isResponseSent" should be an instance of ServerResponse, but %v was given.',
- response
- );
- }
- return response.headersSent;
- }
- __name(isResponseSent, "isResponseSent");
- // src/utils/create-route-mock.js
- function createRouteMock(options = {}) {
- return new Route({
- method: options.method || HttpMethod.GET,
- path: options.path || "/",
- handler: options.handler || (() => "OK")
- });
- }
- __name(createRouteMock, "createRouteMock");
- // src/utils/is-readable-stream.js
- function isReadableStream(value) {
- if (!value || typeof value !== "object") return false;
- return typeof value.pipe === "function";
- }
- __name(isReadableStream, "isReadableStream");
- // src/utils/parse-content-type.js
- var import_js_format6 = require("@e22m4u/js-format");
- function parseContentType(input) {
- if (typeof input !== "string")
- throw new import_js_format6.Errorf(
- 'The parameter "input" of "parseContentType" should be a String, but %v was given.',
- input
- );
- const res = { mediaType: void 0, charset: void 0, boundary: void 0 };
- const re = /^\s*([^\s;/]+\/[^\s;/]+)(?:;\s*charset=([^\s;]+))?(?:;\s*boundary=([^\s;]+))?.*$/i;
- const matches = re.exec(input);
- if (matches && matches[1]) {
- res.mediaType = matches[1];
- if (matches[2]) res.charset = matches[2];
- if (matches[3]) res.boundary = matches[3];
- }
- return res;
- }
- __name(parseContentType, "parseContentType");
- // src/utils/is-writable-stream.js
- function isWritableStream(value) {
- if (!value || typeof value !== "object") return false;
- return typeof value.end === "function";
- }
- __name(isWritableStream, "isWritableStream");
- // src/utils/fetch-request-body.js
- var import_http_errors = __toESM(require("http-errors"), 1);
- var import_http = require("http");
- var import_js_format7 = require("@e22m4u/js-format");
- var CHARACTER_ENCODING_LIST = [
- "ascii",
- "utf8",
- "utf-8",
- "utf16le",
- "utf-16le",
- "ucs2",
- "ucs-2",
- "latin1"
- ];
- function fetchRequestBody(request, bodyBytesLimit = 0) {
- if (!(request instanceof import_http.IncomingMessage))
- throw new import_js_format7.Errorf(
- 'The first parameter of "fetchRequestBody" should be an IncomingMessage instance, but %v was given.',
- request
- );
- if (typeof bodyBytesLimit !== "number")
- throw new import_js_format7.Errorf(
- 'The parameter "bodyBytesLimit" of "fetchRequestBody" should be a number, but %v was given.',
- bodyBytesLimit
- );
- return new Promise((resolve, reject) => {
- const contentLength = parseInt(
- request.headers["content-length"] || "0",
- 10
- );
- if (bodyBytesLimit && contentLength && contentLength > bodyBytesLimit)
- throw createError(
- import_http_errors.default.PayloadTooLarge,
- "Request body limit is %s bytes, but %s bytes given.",
- bodyBytesLimit,
- contentLength
- );
- let encoding = "utf-8";
- const contentType = request.headers["content-type"] || "";
- if (contentType) {
- const parsedContentType = parseContentType(contentType);
- if (parsedContentType && parsedContentType.charset) {
- encoding = parsedContentType.charset.toLowerCase();
- if (!CHARACTER_ENCODING_LIST.includes(encoding))
- throw createError(
- import_http_errors.default.UnsupportedMediaType,
- "Request encoding %v is not supported.",
- encoding
- );
- }
- }
- const data = [];
- let receivedLength = 0;
- const onData = /* @__PURE__ */ __name((chunk) => {
- receivedLength += chunk.length;
- if (bodyBytesLimit && receivedLength > bodyBytesLimit) {
- request.removeAllListeners();
- const error = createError(
- import_http_errors.default.PayloadTooLarge,
- "Request body limit is %v bytes, but %v bytes given.",
- bodyBytesLimit,
- receivedLength
- );
- reject(error);
- return;
- }
- data.push(chunk);
- }, "onData");
- const onEnd = /* @__PURE__ */ __name(() => {
- request.removeAllListeners();
- if (contentLength && contentLength !== receivedLength) {
- const error = createError(
- import_http_errors.default.BadRequest,
- 'Received bytes do not match the "content-length" header.'
- );
- reject(error);
- return;
- }
- const buffer = Buffer.concat(data);
- const body = buffer.toString(encoding);
- resolve(body || void 0);
- }, "onEnd");
- const onError = /* @__PURE__ */ __name((error) => {
- request.removeAllListeners();
- reject((0, import_http_errors.default)(400, error));
- }, "onError");
- request.on("data", onData);
- request.on("end", onEnd);
- request.on("error", onError);
- request.resume();
- });
- }
- __name(fetchRequestBody, "fetchRequestBody");
- // src/utils/create-request-mock.js
- var import_net = require("net");
- var import_tls = require("tls");
- var import_http2 = require("http");
- var import_querystring = __toESM(require("querystring"), 1);
- var import_js_format9 = require("@e22m4u/js-format");
- // src/utils/create-cookies-string.js
- var import_js_format8 = require("@e22m4u/js-format");
- function createCookiesString(data) {
- if (!data || typeof data !== "object" || Array.isArray(data))
- throw new import_js_format8.Errorf(
- 'The first parameter of "createCookiesString" should be an Object, but %v was given.',
- data
- );
- let cookies = "";
- for (const key in data) {
- if (!Object.prototype.hasOwnProperty.call(data, key)) continue;
- const val = data[key];
- if (val == null) continue;
- cookies += `${key}=${val}; `;
- }
- return cookies.trim();
- }
- __name(createCookiesString, "createCookiesString");
- // src/utils/create-request-mock.js
- function createRequestMock(patch) {
- if (patch != null && typeof patch !== "object" || Array.isArray(patch)) {
- throw new import_js_format9.Errorf(
- 'The first parameter of "createRequestMock" should be an Object, but %v was given.',
- patch
- );
- }
- patch = patch || {};
- if (patch.host != null && typeof patch.host !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "host" of "createRequestMock" should be a String, but %v was given.',
- patch.host
- );
- if (patch.method != null && typeof patch.method !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "method" of "createRequestMock" should be a String, but %v was given.',
- patch.method
- );
- if (patch.secure != null && typeof patch.secure !== "boolean")
- throw new import_js_format9.Errorf(
- 'The parameter "secure" of "createRequestMock" should be a Boolean, but %v was given.',
- patch.secure
- );
- if (patch.path != null && typeof patch.path !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "path" of "createRequestMock" should be a String, but %v was given.',
- patch.path
- );
- if (patch.query != null && typeof patch.query !== "object" && typeof patch.query !== "string" || Array.isArray(patch.query)) {
- throw new import_js_format9.Errorf(
- 'The parameter "query" of "createRequestMock" should be a String or Object, but %v was given.',
- patch.query
- );
- }
- if (patch.cookies != null && typeof patch.cookies !== "string" && typeof patch.cookies !== "object" || Array.isArray(patch.cookies)) {
- throw new import_js_format9.Errorf(
- 'The parameter "cookies" of "createRequestMock" should be a String or Object, but %v was given.',
- patch.cookies
- );
- }
- if (patch.headers != null && typeof patch.headers !== "object" || Array.isArray(patch.headers)) {
- throw new import_js_format9.Errorf(
- 'The parameter "headers" of "createRequestMock" should be an Object, but %v was given.',
- patch.headers
- );
- }
- if (patch.stream != null && !isReadableStream(patch.stream))
- throw new import_js_format9.Errorf(
- 'The parameter "stream" of "createRequestMock" should be a Stream, but %v was given.',
- patch.stream
- );
- if (patch.encoding != null) {
- if (typeof patch.encoding !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "encoding" of "createRequestMock" should be a String, but %v was given.',
- patch.encoding
- );
- if (!CHARACTER_ENCODING_LIST.includes(patch.encoding))
- throw new import_js_format9.Errorf(
- "Character encoding %v is not supported.",
- patch.encoding
- );
- }
- if (patch.stream) {
- if (patch.secure != null)
- throw new import_js_format9.Errorf(
- 'The "createRequestMock" does not allow specifying the "stream" and "secure" options simultaneously.'
- );
- if (patch.body != null)
- throw new import_js_format9.Errorf(
- 'The "createRequestMock" does not allow specifying the "stream" and "body" options simultaneously.'
- );
- if (patch.encoding != null)
- throw new import_js_format9.Errorf(
- 'The "createRequestMock" does not allow specifying the "stream" and "encoding" options simultaneously.'
- );
- }
- const request = patch.stream || createRequestStream(patch.secure, patch.body, patch.encoding);
- request.url = createRequestUrl(patch.path || "/", patch.query);
- request.headers = createRequestHeaders(
- patch.host,
- patch.secure,
- patch.body,
- patch.cookies,
- patch.encoding,
- patch.headers
- );
- request.method = (patch.method || "get").toUpperCase();
- return request;
- }
- __name(createRequestMock, "createRequestMock");
- function createRequestStream(secure, body, encoding) {
- if (encoding != null && typeof encoding !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "encoding" of "createRequestStream" should be a String, but %v was given.',
- encoding
- );
- encoding = encoding || "utf-8";
- let socket = new import_net.Socket();
- if (secure) socket = new import_tls.TLSSocket(socket);
- const request = new import_http2.IncomingMessage(socket);
- if (body != null) {
- if (typeof body === "string") {
- request.push(body, encoding);
- } else if (Buffer.isBuffer(body)) {
- request.push(body);
- } else {
- request.push(JSON.stringify(body));
- }
- }
- request.push(null);
- return request;
- }
- __name(createRequestStream, "createRequestStream");
- function createRequestUrl(path, query) {
- if (typeof path !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "path" of "createRequestUrl" should be a String, but %v was given.',
- path
- );
- if (query != null && typeof query !== "string" && typeof query !== "object" || Array.isArray(query)) {
- throw new import_js_format9.Errorf(
- 'The parameter "query" of "createRequestUrl" should be a String or Object, but %v was given.',
- query
- );
- }
- let url = ("/" + path).replace("//", "/");
- if (typeof query === "object") {
- const qs = import_querystring.default.stringify(query);
- if (qs) url += `?${qs}`;
- } else if (typeof query === "string") {
- url += `?${query.replace(/^\?/, "")}`;
- }
- return url;
- }
- __name(createRequestUrl, "createRequestUrl");
- function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
- if (host != null && typeof host !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "host" of "createRequestHeaders" a non-empty String, but %v was given.',
- host
- );
- host = host || "localhost";
- if (secure != null && typeof secure !== "boolean")
- throw new import_js_format9.Errorf(
- 'The parameter "secure" of "createRequestHeaders" should be a String, but %v was given.',
- secure
- );
- secure = Boolean(secure);
- if (cookies != null && typeof cookies !== "object" && typeof cookies !== "string" || Array.isArray(cookies)) {
- throw new import_js_format9.Errorf(
- 'The parameter "cookies" of "createRequestHeaders" should be a String or Object, but %v was given.',
- cookies
- );
- }
- if (headers != null && typeof headers !== "object" || Array.isArray(headers)) {
- throw new import_js_format9.Errorf(
- 'The parameter "headers" of "createRequestHeaders" should be an Object, but %v was given.',
- headers
- );
- }
- headers = headers || {};
- if (encoding != null && typeof encoding !== "string")
- throw new import_js_format9.Errorf(
- 'The parameter "encoding" of "createRequestHeaders" should be a String, but %v was given.',
- encoding
- );
- encoding = encoding || "utf-8";
- const obj = { ...headers };
- obj["host"] = host;
- if (secure) obj["x-forwarded-proto"] = "https";
- if (cookies != null) {
- if (typeof cookies === "string") {
- obj["cookie"] = obj["cookie"] ? obj["cookie"] : "";
- obj["cookie"] += obj["cookie"] ? `; ${cookies}` : cookies;
- } else if (typeof cookies === "object") {
- obj["cookie"] = obj["cookie"] ? obj["cookie"] : "";
- const newCookies = createCookiesString(cookies);
- obj["cookie"] += obj["cookie"] ? `; ${newCookies}` : newCookies;
- }
- }
- if (obj["content-type"] == null) {
- if (typeof body === "string") {
- obj["content-type"] = "text/plain";
- } else if (Buffer.isBuffer(body)) {
- obj["content-type"] = "application/octet-stream";
- } else if (typeof body === "object" || typeof body === "boolean" || typeof body === "number") {
- obj["content-type"] = "application/json";
- }
- }
- if (body != null && obj["content-length"] == null) {
- if (typeof body === "string") {
- const length = Buffer.byteLength(body, encoding);
- obj["content-length"] = String(length);
- } else if (Buffer.isBuffer(body)) {
- const length = Buffer.byteLength(body);
- obj["content-length"] = String(length);
- } else if (typeof body === "object" || typeof body === "boolean" || typeof body === "number") {
- const json = JSON.stringify(body);
- const length = Buffer.byteLength(json, encoding);
- obj["content-length"] = String(length);
- }
- }
- return obj;
- }
- __name(createRequestHeaders, "createRequestHeaders");
- // src/utils/create-response-mock.js
- var import_stream = require("stream");
- function createResponseMock() {
- const response = new import_stream.PassThrough();
- patchEncoding(response);
- patchHeaders(response);
- patchBody(response);
- return response;
- }
- __name(createResponseMock, "createResponseMock");
- function patchEncoding(response) {
- Object.defineProperty(response, "_encoding", {
- configurable: true,
- writable: true,
- value: void 0
- });
- Object.defineProperty(response, "setEncoding", {
- configurable: true,
- value: /* @__PURE__ */ __name(function(enc) {
- this._encoding = enc;
- return this;
- }, "value")
- });
- Object.defineProperty(response, "getEncoding", {
- configurable: true,
- value: /* @__PURE__ */ __name(function() {
- return this._encoding;
- }, "value")
- });
- }
- __name(patchEncoding, "patchEncoding");
- function patchHeaders(response) {
- Object.defineProperty(response, "_headersSent", {
- configurable: true,
- writable: true,
- value: false
- });
- Object.defineProperty(response, "headersSent", {
- configurable: true,
- get() {
- return this._headersSent;
- }
- });
- Object.defineProperty(response, "_headers", {
- configurable: true,
- writable: true,
- value: {}
- });
- Object.defineProperty(response, "setHeader", {
- configurable: true,
- value: /* @__PURE__ */ __name(function(name, value) {
- if (this.headersSent)
- throw new Error(
- "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"
- );
- const key = name.toLowerCase();
- this._headers[key] = String(value);
- return this;
- }, "value")
- });
- Object.defineProperty(response, "getHeader", {
- configurable: true,
- value: /* @__PURE__ */ __name(function(name) {
- return this._headers[name.toLowerCase()];
- }, "value")
- });
- Object.defineProperty(response, "getHeaders", {
- configurable: true,
- value: /* @__PURE__ */ __name(function() {
- return JSON.parse(JSON.stringify(this._headers));
- }, "value")
- });
- }
- __name(patchHeaders, "patchHeaders");
- function patchBody(response) {
- let resolve, reject;
- const promise = new Promise((rsv, rej) => {
- resolve = rsv;
- reject = rej;
- });
- const data = [];
- response.on("data", (c) => data.push(c));
- response.on("error", (e) => reject(e));
- response.on("end", () => {
- resolve(Buffer.concat(data));
- });
- const originalEnd = response.end.bind(response);
- response.end = function(...args) {
- this._headersSent = true;
- return originalEnd(...args);
- };
- Object.defineProperty(response, "getBody", {
- configurable: true,
- value: /* @__PURE__ */ __name(function() {
- return promise.then((buffer) => {
- const enc = this.getEncoding();
- const str = buffer.toString(enc);
- return data.length ? str : void 0;
- });
- }, "value")
- });
- }
- __name(patchBody, "patchBody");
- // src/utils/get-request-pathname.js
- var import_js_format10 = require("@e22m4u/js-format");
- function getRequestPathname(request) {
- if (!request || typeof request !== "object" || Array.isArray(request) || typeof request.url !== "string") {
- throw new import_js_format10.Errorf(
- 'The first argument of "getRequestPathname" should be an instance of IncomingMessage, but %v was given.',
- request
- );
- }
- return (request.url || "/").replace(/\?.*$/, "");
- }
- __name(getRequestPathname, "getRequestPathname");
- // src/hooks/hook-registry.js
- var import_js_format11 = require("@e22m4u/js-format");
- var RouterHookType = {
- PRE_HANDLER: "preHandler",
- POST_HANDLER: "postHandler"
- };
- var _HookRegistry = class _HookRegistry {
- /**
- * Hooks.
- *
- * @type {Map<string, Function[]>}
- * @private
- */
- _hooks = /* @__PURE__ */ new Map();
- /**
- * Add hook.
- *
- * @param {string} type
- * @param {Function} hook
- * @returns {this}
- */
- addHook(type, hook) {
- if (!type || typeof type !== "string")
- throw new import_js_format11.Errorf("The hook type is required, but %v was given.", type);
- if (!Object.values(RouterHookType).includes(type))
- throw new import_js_format11.Errorf("The hook type %v is not supported.", type);
- if (!hook || typeof hook !== "function")
- throw new import_js_format11.Errorf(
- "The hook %v should be a Function, but %v was given.",
- type,
- hook
- );
- const hooks = this._hooks.get(type) || [];
- hooks.push(hook);
- this._hooks.set(type, hooks);
- return this;
- }
- /**
- * Has hook.
- *
- * @param {string} type
- * @param {Function} hook
- * @returns {boolean}
- */
- hasHook(type, hook) {
- if (!type || typeof type !== "string")
- throw new import_js_format11.Errorf("The hook type is required, but %v was given.", type);
- if (!Object.values(RouterHookType).includes(type))
- throw new import_js_format11.Errorf("The hook type %v is not supported.", type);
- if (!hook || typeof hook !== "function")
- throw new import_js_format11.Errorf(
- "The hook %v should be a Function, but %v was given.",
- type,
- hook
- );
- const hooks = this._hooks.get(type) || [];
- return hooks.indexOf(hook) > -1;
- }
- /**
- * Get hooks.
- *
- * @param {string} type
- * @returns {Function[]}
- */
- getHooks(type) {
- if (!type || typeof type !== "string")
- throw new import_js_format11.Errorf("The hook type is required, but %v was given.", type);
- if (!Object.values(RouterHookType).includes(type))
- throw new import_js_format11.Errorf("The hook type %v is not supported.", type);
- return this._hooks.get(type) || [];
- }
- };
- __name(_HookRegistry, "HookRegistry");
- var HookRegistry = _HookRegistry;
- // src/hooks/hook-invoker.js
- var _HookInvoker = class _HookInvoker extends DebuggableService {
- /**
- * Invoke and continue until value received.
- *
- * @param {Route} route
- * @param {string} hookType
- * @param {import('http').ServerResponse} response
- * @param {*[]} args
- * @returns {Promise<*>|*}
- */
- invokeAndContinueUntilValueReceived(route, hookType, response, ...args) {
- if (!route || !(route instanceof Route))
- throw new import_js_format12.Errorf(
- 'The parameter "route" of the HookInvoker.invokeAndContinueUntilValueReceived should be a Route instance, but %v was given.',
- route
- );
- if (!hookType || typeof hookType !== "string")
- throw new import_js_format12.Errorf(
- 'The parameter "hookType" of the HookInvoker.invokeAndContinueUntilValueReceived should be a non-empty String, but %v was given.',
- hookType
- );
- if (!Object.values(RouterHookType).includes(hookType))
- throw new import_js_format12.Errorf("The hook type %v is not supported.", hookType);
- if (!response || typeof response !== "object" || Array.isArray(response) || typeof response.headersSent !== "boolean") {
- throw new import_js_format12.Errorf(
- 'The parameter "response" of the HookInvoker.invokeAndContinueUntilValueReceived should be a ServerResponse instance, but %v was given.',
- response
- );
- }
- if (isResponseSent(response)) {
- return response;
- }
- const hooks = [
- ...this.getService(HookRegistry).getHooks(hookType),
- ...route.hookRegistry.getHooks(hookType)
- ];
- let result = void 0;
- for (let i = 0; i < hooks.length; i++) {
- const hook = hooks[i];
- result = hook(...args);
- if (isResponseSent(response)) {
- return response;
- }
- if (result != null) {
- if (isPromise(result)) {
- return (async () => {
- let asyncResult = await result;
- if (isResponseSent(response)) {
- return response;
- }
- if (asyncResult != null) {
- return asyncResult;
- }
- for (let j = i + 1; j < hooks.length; j++) {
- asyncResult = await hooks[j](...args);
- if (isResponseSent(response)) {
- return response;
- }
- if (asyncResult != null) {
- return asyncResult;
- }
- }
- return;
- })();
- }
- return result;
- }
- }
- return;
- }
- };
- __name(_HookInvoker, "HookInvoker");
- var HookInvoker = _HookInvoker;
- // src/route.js
- var HttpMethod = {
- GET: "GET",
- POST: "POST",
- PUT: "PUT",
- PATCH: "PATCH",
- DELETE: "DELETE"
- };
- var _Route = class _Route extends import_js_debug.Debuggable {
- /**
- * Method.
- *
- * @type {string}
- * @private
- */
- _method;
- /**
- * Getter of the method.
- *
- * @returns {string}
- */
- get method() {
- return this._method;
- }
- /**
- * Path template.
- *
- * @type {string}
- * @private
- */
- _path;
- /**
- * Getter of the path.
- *
- * @returns {string}
- */
- get path() {
- return this._path;
- }
- /**
- * Meta.
- *
- * @type {object}
- */
- _meta = {};
- /**
- * Getter of the meta.
- *
- * @returns {object}
- */
- get meta() {
- return this._meta;
- }
- /**
- * Handler.
- *
- * @type {RouteHandler}
- * @private
- */
- _handler;
- /**
- * Getter of the handler.
- *
- * @returns {*}
- */
- get handler() {
- return this._handler;
- }
- /**
- * Hook registry.
- *
- * @type {HookRegistry}
- * @private
- */
- _hookRegistry = new HookRegistry();
- /**
- * Getter of the hook registry.
- *
- * @returns {HookRegistry}
- */
- get hookRegistry() {
- return this._hookRegistry;
- }
- /**
- * Constructor.
- *
- * @param {RouteDefinition} routeDef
- */
- constructor(routeDef) {
- super({
- namespace: MODULE_DEBUG_NAMESPACE,
- noEnvironmentNamespace: true,
- noInstantiationMessage: true
- });
- if (!routeDef || typeof routeDef !== "object" || Array.isArray(routeDef))
- throw new import_js_format13.Errorf(
- "The first parameter of Route.constructor should be an Object, but %v was given.",
- routeDef
- );
- if (!routeDef.method || typeof routeDef.method !== "string")
- throw new import_js_format13.Errorf(
- 'The option "method" of the Route should be a non-empty String, but %v was given.',
- routeDef.method
- );
- this._method = routeDef.method.toUpperCase();
- if (typeof routeDef.path !== "string")
- throw new import_js_format13.Errorf(
- 'The option "path" of the Route should be a String, but %v was given.',
- routeDef.path
- );
- this._path = routeDef.path;
- if (typeof routeDef.handler !== "function")
- throw new import_js_format13.Errorf(
- 'The option "handler" of the Route should be a Function, but %v was given.',
- routeDef.handler
- );
- if (routeDef.meta != null) {
- if (typeof routeDef.meta !== "object" || Array.isArray(routeDef.meta))
- throw new import_js_format13.Errorf(
- 'The option "meta" of the Route should be a plain Object, but %v was given.',
- routeDef.meta
- );
- this._meta = cloneDeep(routeDef.meta);
- }
- this._handler = routeDef.handler;
- if (routeDef.preHandler != null) {
- const preHandlerHooks = Array.isArray(routeDef.preHandler) ? routeDef.preHandler : [routeDef.preHandler];
- preHandlerHooks.forEach((hook) => {
- this._hookRegistry.addHook(RouterHookType.PRE_HANDLER, hook);
- });
- }
- if (routeDef.postHandler != null) {
- const postHandlerHooks = Array.isArray(routeDef.postHandler) ? routeDef.postHandler : [routeDef.postHandler];
- postHandlerHooks.forEach((hook) => {
- this._hookRegistry.addHook(RouterHookType.POST_HANDLER, hook);
- });
- }
- this.ctorDebug("A new route %s %v was created.", this._method, this._path);
- }
- /**
- * Handle request.
- *
- * @param {RequestContext} context
- * @returns {*}
- */
- handle(context) {
- const debug = this.getDebuggerFor(this.handle);
- const requestPath = getRequestPathname(context.request);
- debug(
- "Invoking the Route handler for the request %s %v.",
- this.method.toUpperCase(),
- requestPath
- );
- return this._handler(context);
- }
- };
- __name(_Route, "Route");
- var Route = _Route;
- // src/parsers/body-parser.js
- var import_http_errors2 = __toESM(require("http-errors"), 1);
- var import_js_format15 = require("@e22m4u/js-format");
- // src/router-options.js
- var import_js_format14 = require("@e22m4u/js-format");
- var _RouterOptions = class _RouterOptions extends DebuggableService {
- /**
- * Request body bytes limit.
- *
- * @type {number}
- * @private
- */
- _requestBodyBytesLimit = 512e3;
- // 512kb
- /**
- * Getter of request body bytes limit.
- *
- * @returns {number}
- */
- get requestBodyBytesLimit() {
- return this._requestBodyBytesLimit;
- }
- /**
- * Set request body bytes limit.
- *
- * @param {number} input
- * @returns {RouterOptions}
- */
- setRequestBodyBytesLimit(input) {
- if (typeof input !== "number" || input < 0)
- throw new import_js_format14.Errorf(
- 'The option "requestBodyBytesLimit" must be a positive Number or 0, but %v was given.',
- input
- );
- this._requestBodyBytesLimit = input;
- return this;
- }
- };
- __name(_RouterOptions, "RouterOptions");
- var RouterOptions = _RouterOptions;
- // src/parsers/body-parser.js
- var METHODS_WITH_BODY = ["POST", "PUT", "PATCH", "DELETE"];
- var UNPARSABLE_MEDIA_TYPES = ["multipart/form-data"];
- var _BodyParser = class _BodyParser extends DebuggableService {
- /**
- * Parsers.
- *
- * @type {{[mime: string]: Function}}
- */
- _parsers = {
- "text/plain": /* @__PURE__ */ __name((v) => String(v), "text/plain"),
- "application/json": parseJsonBody
- };
- /**
- * Set parser.
- *
- * @param {string} mediaType
- * @param {Function} parser
- * @returns {this}
- */
- defineParser(mediaType, parser) {
- if (!mediaType || typeof mediaType !== "string")
- throw new import_js_format15.Errorf(
- 'The parameter "mediaType" of BodyParser.defineParser should be a non-empty String, but %v was given.',
- mediaType
- );
- if (!parser || typeof parser !== "function")
- throw new import_js_format15.Errorf(
- 'The parameter "parser" of BodyParser.defineParser should be a Function, but %v was given.',
- parser
- );
- this._parsers[mediaType] = parser;
- return this;
- }
- /**
- * Has parser.
- *
- * @param {string} mediaType
- * @returns {boolean}
- */
- hasParser(mediaType) {
- if (!mediaType || typeof mediaType !== "string")
- throw new import_js_format15.Errorf(
- 'The parameter "mediaType" of BodyParser.hasParser should be a non-empty String, but %v was given.',
- mediaType
- );
- return Boolean(this._parsers[mediaType]);
- }
- /**
- * Delete parser.
- *
- * @param {string} mediaType
- * @returns {this}
- */
- deleteParser(mediaType) {
- if (!mediaType || typeof mediaType !== "string")
- throw new import_js_format15.Errorf(
- 'The parameter "mediaType" of BodyParser.deleteParser should be a non-empty String, but %v was given.',
- mediaType
- );
- const parser = this._parsers[mediaType];
- if (!parser) throw new import_js_format15.Errorf("The parser of %v is not found.", mediaType);
- delete this._parsers[mediaType];
- return this;
- }
- /**
- * Parse.
- *
- * @param {import('http').IncomingMessage} request
- * @returns {Promise<*>|undefined}
- */
- parse(request) {
- const debug = this.getDebuggerFor(this.parse);
- if (!METHODS_WITH_BODY.includes(request.method.toUpperCase())) {
- debug(
- "Body parsing was skipped for the %s request.",
- request.method.toUpperCase()
- );
- return;
- }
- const contentType = (request.headers["content-type"] || "").replace(
- /^([^;]+);.*$/,
- "$1"
- );
- if (!contentType) {
- debug(
- "Body parsing was skipped because the request had no content type."
- );
- return;
- }
- const { mediaType } = parseContentType(contentType);
- if (!mediaType)
- throw createError(
- import_http_errors2.default.BadRequest,
- 'Unable to parse the "content-type" header.'
- );
- const parser = this._parsers[mediaType];
- if (!parser) {
- if (UNPARSABLE_MEDIA_TYPES.includes(mediaType)) {
- debug("Body parsing was skipped for %v.", mediaType);
- return;
- }
- throw createError(
- import_http_errors2.default.UnsupportedMediaType,
- "Media type %v is not supported.",
- mediaType
- );
- }
- const bodyBytesLimit = this.getService(RouterOptions).requestBodyBytesLimit;
- return fetchRequestBody(request, bodyBytesLimit).then((rawBody) => {
- if (rawBody != null) return parser(rawBody);
- return rawBody;
- });
- }
- };
- __name(_BodyParser, "BodyParser");
- var BodyParser = _BodyParser;
- function parseJsonBody(input) {
- if (typeof input !== "string") return void 0;
- try {
- return JSON.parse(input);
- } catch (error) {
- throw createError(import_http_errors2.default.BadRequest, error.message);
- }
- }
- __name(parseJsonBody, "parseJsonBody");
- // src/parsers/query-parser.js
- var import_querystring2 = __toESM(require("querystring"), 1);
- var _QueryParser = class _QueryParser extends DebuggableService {
- /**
- * Parse
- *
- * @param {import('http').IncomingMessage} request
- * @returns {object}
- */
- parse(request) {
- const debug = this.getDebuggerFor(this.parse);
- const queryStr = request.url.replace(/^[^?]*\??/, "");
- const query = queryStr ? import_querystring2.default.parse(queryStr) : {};
- const queryKeys = Object.keys(query);
- if (queryKeys.length) {
- queryKeys.forEach((key) => {
- debug("The query parameter %v had the value %v.", key, query[key]);
- });
- } else {
- debug(
- "The request %s %v had no query parameters.",
- request.method,
- getRequestPathname(request)
- );
- }
- return query;
- }
- };
- __name(_QueryParser, "QueryParser");
- var QueryParser = _QueryParser;
- // src/parsers/cookies-parser.js
- var _CookiesParser = class _CookiesParser extends DebuggableService {
- /**
- * Parse
- *
- * @param {import('http').IncomingMessage} request
- * @returns {object}
- */
- parse(request) {
- const debug = this.getDebuggerFor(this.parse);
- const cookiesString = request.headers["cookie"] || "";
- const cookies = parseCookies(cookiesString);
- const cookiesKeys = Object.keys(cookies);
- if (cookiesKeys.length) {
- cookiesKeys.forEach((key) => {
- debug("The cookie %v had the value %v.", key, cookies[key]);
- });
- } else {
- debug(
- "The request %s %v had no cookies.",
- request.method,
- getRequestPathname(request)
- );
- }
- return cookies;
- }
- };
- __name(_CookiesParser, "CookiesParser");
- var CookiesParser = _CookiesParser;
- // src/parsers/request-parser.js
- var import_http3 = require("http");
- var import_js_format16 = require("@e22m4u/js-format");
- var _RequestParser = class _RequestParser extends DebuggableService {
- /**
- * Parse.
- *
- * @param {IncomingMessage} request
- * @returns {Promise<object>|object}
- */
- parse(request) {
- if (!(request instanceof import_http3.IncomingMessage))
- throw new import_js_format16.Errorf(
- "The first argument of RequestParser.parse should be an instance of IncomingMessage, but %v was given.",
- request
- );
- const data = {};
- const promises = [];
- const parsedQuery = this.getService(QueryParser).parse(request);
- if (isPromise(parsedQuery)) {
- promises.push(parsedQuery.then((v) => data.query = v));
- } else {
- data.query = parsedQuery;
- }
- const parsedCookies = this.getService(CookiesParser).parse(request);
- if (isPromise(parsedCookies)) {
- promises.push(parsedCookies.then((v) => data.cookies = v));
- } else {
- data.cookies = parsedCookies;
- }
- const parsedBody = this.getService(BodyParser).parse(request);
- if (isPromise(parsedBody)) {
- promises.push(parsedBody.then((v) => data.body = v));
- } else {
- data.body = parsedBody;
- }
- data.headers = Object.assign({}, request.headers);
- return promises.length ? Promise.all(promises).then(() => data) : data;
- }
- };
- __name(_RequestParser, "RequestParser");
- var RequestParser = _RequestParser;
- // src/route-registry.js
- var import_js_format17 = require("@e22m4u/js-format");
- var import_js_path_trie = require("@e22m4u/js-path-trie");
- var import_js_service2 = require("@e22m4u/js-service");
- var _RouteRegistry = class _RouteRegistry extends DebuggableService {
- /**
- * Constructor.
- *
- * @param {ServiceContainer} container
- */
- constructor(container) {
- super(container);
- this._trie = new import_js_path_trie.PathTrie();
- }
- /**
- * Define route.
- *
- * @param {import('./route.js').RouteDefinition} routeDef
- * @returns {Route}
- */
- defineRoute(routeDef) {
- const debug = this.getDebuggerFor(this.defineRoute);
- if (!routeDef || typeof routeDef !== "object" || Array.isArray(routeDef))
- throw new import_js_format17.Errorf(
- "The route definition should be an Object, but %v was given.",
- routeDef
- );
- const route = new Route(routeDef);
- const triePath = `${route.method}/${route.path}`;
- this._trie.add(triePath, route);
- debug(
- "The route %s %v was registered.",
- route.method.toUpperCase(),
- route.path
- );
- return route;
- }
- /**
- * Match route by request.
- *
- * @param {import('http').IncomingRequest} request
- * @returns {ResolvedRoute|undefined}
- */
- matchRouteByRequest(request) {
- const debug = this.getDebuggerFor(this.matchRouteByRequest);
- const requestPath = (request.url || "/").replace(/\?.*$/, "");
- debug(
- "Matching routes with the request %s %v.",
- request.method.toUpperCase(),
- requestPath
- );
- const rawTriePath = `${request.method.toUpperCase()}/${requestPath}`;
- const triePath = rawTriePath.replace(/\/+/g, "/");
- const resolved = this._trie.match(triePath);
- if (resolved) {
- const route = resolved.value;
- debug(
- "The route %s %v was matched.",
- route.method.toUpperCase(),
- route.path
- );
- const paramNames = Object.keys(resolved.params);
- if (paramNames.length) {
- paramNames.forEach((name) => {
- debug(
- "The path parameter %v had the value %v.",
- name,
- resolved.params[name]
- );
- });
- } else {
- debug("No path parameters found.");
- }
- return { route, params: resolved.params };
- }
- debug(
- "No matched route for the request %s %v.",
- request.method.toUpperCase(),
- requestPath
- );
- }
- };
- __name(_RouteRegistry, "RouteRegistry");
- var RouteRegistry = _RouteRegistry;
- // src/request-context.js
- var import_js_format18 = require("@e22m4u/js-format");
- var import_js_service3 = require("@e22m4u/js-service");
- var _RequestContext = class _RequestContext {
- /**
- * Service container.
- *
- * @type {ServiceContainer}
- */
- _container;
- /**
- * Getter of service container.
- *
- * @type {ServiceContainer}
- */
- get container() {
- return this._container;
- }
- /**
- * Request.
- *
- * @type {import('http').IncomingMessage}
- */
- _request;
- /**
- * Getter of request.
- *
- * @type {import('http').IncomingMessage}
- */
- get request() {
- return this._request;
- }
- /**
- * Response.
- *
- * @type {import('http').ServerResponse}
- */
- _response;
- /**
- * Getter of response.
- *
- * @type {import('http').ServerResponse}
- */
- get response() {
- return this._response;
- }
- /**
- * Route
- *
- * @type {Route}
- */
- _route;
- /**
- * Getter of route.
- *
- * @type {Route}
- */
- get route() {
- return this._route;
- }
- /**
- * Query.
- *
- * @type {object}
- */
- query = {};
- /**
- * Path parameters.
- *
- * @type {object}
- */
- params = {};
- /**
- * Headers.
- *
- * @type {object}
- */
- headers = {};
- /**
- * Parsed cookies.
- *
- * @type {object}
- */
- cookies = {};
- /**
- * Parsed body.
- *
- * @type {*}
- */
- body;
- /**
- * State.
- *
- * @type {object}
- */
- state = {};
- /**
- * Route meta.
- *
- * @type {import('./route.js').RouteMeta}
- */
- get meta() {
- return this.route.meta;
- }
- /**
- * Method.
- *
- * @returns {string}
- */
- get method() {
- return this.request.method.toUpperCase();
- }
- /**
- * Path.
- *
- * @returns {string}
- */
- get path() {
- return this.request.url;
- }
- /**
- * Pathname.
- *
- * @type {string|undefined}
- * @private
- */
- _pathname = void 0;
- /**
- * Pathname.
- *
- * @returns {string}
- */
- get pathname() {
- if (this._pathname != null) return this._pathname;
- this._pathname = getRequestPathname(this.request);
- return this._pathname;
- }
- /**
- * Constructor.
- *
- * @param {ServiceContainer} container
- * @param {import('http').IncomingMessage} request
- * @param {import('http').ServerResponse} response
- * @param {Route} route
- */
- constructor(container, request, response, route) {
- if (!(0, import_js_service3.isServiceContainer)(container))
- throw new import_js_format18.Errorf(
- 'The parameter "container" of RequestContext.constructor should be an instance of ServiceContainer, but %v was given.',
- container
- );
- this._container = container;
- if (!request || typeof request !== "object" || Array.isArray(request) || !isReadableStream(request)) {
- throw new import_js_format18.Errorf(
- 'The parameter "request" of RequestContext.constructor should be an instance of IncomingMessage, but %v was given.',
- request
- );
- }
- this._request = request;
- if (!response || typeof response !== "object" || Array.isArray(response) || !isWritableStream(response)) {
- throw new import_js_format18.Errorf(
- 'The parameter "response" of RequestContext.constructor should be an instance of ServerResponse, but %v was given.',
- response
- );
- }
- this._response = response;
- if (!(route instanceof Route)) {
- throw new import_js_format18.Errorf(
- 'The parameter "route" of RequestContext.constructor should be an instance of Route, but %v was given.',
- route
- );
- }
- this._route = route;
- }
- };
- __name(_RequestContext, "RequestContext");
- var RequestContext = _RequestContext;
- // src/trie-router.js
- var import_js_service4 = require("@e22m4u/js-service");
- var import_http4 = require("http");
- // src/senders/data-sender.js
- var import_js_format19 = require("@e22m4u/js-format");
- var _DataSender = class _DataSender extends DebuggableService {
- /**
- * Send.
- *
- * @param {import('http').ServerResponse} response
- * @param {*} data
- * @returns {undefined}
- */
- send(response, data) {
- const debug = this.getDebuggerFor(this.send);
- if (data === response || response.headersSent) {
- debug(
- "Response sending was skipped because its headers where sent already."
- );
- return;
- }
- if (data == null) {
- response.statusCode = 204;
- response.end();
- debug("The empty response was sent.");
- return;
- }
- if (isReadableStream(data)) {
- response.setHeader("Content-Type", "application/octet-stream");
- data.pipe(response);
- debug("The stream response was sent.");
- return;
- }
- let debugMsg;
- switch (typeof data) {
- case "object":
- case "boolean":
- case "number":
- if (Buffer.isBuffer(data)) {
- response.setHeader("content-type", "application/octet-stream");
- debugMsg = "The Buffer was sent as binary data.";
- } else {
- response.setHeader("content-type", "application/json");
- debugMsg = (0, import_js_format19.format)("The %v was sent as JSON.", typeof data);
- data = JSON.stringify(data);
- }
- break;
- default:
- response.setHeader("content-type", "text/plain");
- debugMsg = "The response data was sent as plain text.";
- data = String(data);
- break;
- }
- response.end(data);
- debug(debugMsg);
- }
- };
- __name(_DataSender, "DataSender");
- var DataSender = _DataSender;
- // src/senders/error-sender.js
- var import_util = require("util");
- var import_statuses = __toESM(require("statuses"), 1);
- var EXPOSED_ERROR_PROPERTIES = ["code", "details"];
- var _ErrorSender = class _ErrorSender extends DebuggableService {
- /**
- * Handle.
- *
- * @param {import('http').IncomingMessage} request
- * @param {import('http').ServerResponse} response
- * @param {Error} error
- * @returns {undefined}
- */
- send(request, response, error) {
- const debug = this.getDebuggerFor(this.send);
- let safeError = {};
- if (error) {
- if (typeof error === "object") {
- safeError = error;
- } else {
- safeError = { message: String(error) };
- }
- }
- const statusCode = error.statusCode || error.status || 500;
- const body = { error: {} };
- if (safeError.message && typeof safeError.message === "string") {
- body.error.message = safeError.message;
- } else {
- body.error.message = (0, import_statuses.default)(statusCode);
- }
- EXPOSED_ERROR_PROPERTIES.forEach((name) => {
- if (name in safeError) body.error[name] = safeError[name];
- });
- const requestData = {
- url: request.url,
- method: request.method,
- headers: request.headers
- };
- const inspectOptions = {
- showHidden: false,
- depth: null,
- colors: true,
- compact: false
- };
- console.warn((0, import_util.inspect)(requestData, inspectOptions));
- console.warn((0, import_util.inspect)(body, inspectOptions));
- if (error.stack) {
- console.log(error.stack);
- } else {
- console.error(error);
- }
- response.statusCode = statusCode;
- response.setHeader("content-type", "application/json; charset=utf-8");
- response.end(JSON.stringify(body, null, 2), "utf-8");
- debug(
- "The %s error was sent for the request %s %v.",
- statusCode,
- request.method,
- getRequestPathname(request)
- );
- }
- /**
- * Send 404.
- *
- * @param {import('http').IncomingMessage} request
- * @param {import('http').ServerResponse} response
- * @returns {undefined}
- */
- send404(request, response) {
- const debug = this.getDebuggerFor(this.send404);
- response.statusCode = 404;
- response.setHeader("content-type", "text/plain; charset=utf-8");
- response.end("404 Not Found", "utf-8");
- debug(
- "The 404 error was sent for the request %s %v.",
- request.method,
- getRequestPathname(request)
- );
- }
- };
- __name(_ErrorSender, "ErrorSender");
- var ErrorSender = _ErrorSender;
- // src/trie-router.js
- var _TrieRouter = class _TrieRouter extends DebuggableService {
- /**
- * Define route.
- *
- * Example 1:
- * ```
- * const router = new TrieRouter();
- * router.defineRoute({
- * method: HttpMethod.GET, // Request method.
- * path: '/', // Path template.
- * handler: ctx => 'Hello world!', // Request handler.
- * });
- * ```
- *
- * Example 2:
- * ```
- * const router = new TrieRouter();
- * router.defineRoute({
- * method: HttpMethod.POST, // Request method.
- * path: '/users/:id', // The path template may have parameters.
- * preHandler(ctx) { ... }, // The "preHandler" executes before a route handler.
- * handler(ctx) { ... }, // Request handler function.
- * postHandler(ctx, data) { ... }, // The "postHandler" executes after a route handler.
- * });
- * ```
- *
- * @param {import('./route-registry.js').RouteDefinition} routeDef
- * @returns {import('./route.js').Route}
- */
- defineRoute(routeDef) {
- return this.getService(RouteRegistry).defineRoute(routeDef);
- }
- /**
- * Request listener.
- *
- * Example:
- * ```
- * import http from 'http';
- * import {TrieRouter} from '@e22m4u/js-trie-router';
- *
- * const router = new TrieRouter();
- * const server = new http.Server();
- * server.on('request', router.requestListener); // Sets the request listener.
- * server.listen(3000); // Starts listening for connections.
- * ```
- *
- * @returns {Function}
- */
- get requestListener() {
- return this._handleRequest.bind(this);
- }
- /**
- * Handle incoming request.
- *
- * @param {import('http').IncomingMessage} request
- * @param {import('http').ServerResponse} response
- * @returns {Promise<undefined>}
- * @private
- */
- async _handleRequest(request, response) {
- const debug = this.getDebuggerFor(this._handleRequest);
- const requestPath = (request.url || "/").replace(/\?.*$/, "");
- debug(
- "Preparing to handle an incoming request %s %v.",
- request.method,
- requestPath
- );
- const resolved = this.getService(RouteRegistry).matchRouteByRequest(request);
- if (!resolved) {
- debug("No route for the request %s %v.", request.method, requestPath);
- this.getService(ErrorSender).send404(request, response);
- } else {
- const { route, params } = resolved;
- const container = new import_js_service4.ServiceContainer(this.container);
- const context = new RequestContext(container, request, response, route);
- container.set(RequestContext, context);
- container.set(import_http4.IncomingMessage, request);
- container.set(import_http4.ServerResponse, response);
- context.params = params;
- let data;
- try {
- const reqDataOrPromise = this.getService(RequestParser).parse(request);
- if (isPromise(reqDataOrPromise)) {
- const reqData = await reqDataOrPromise;
- Object.assign(context, reqData);
- } else {
- Object.assign(context, reqDataOrPromise);
- }
- const hookInvoker = this.getService(HookInvoker);
- data = hookInvoker.invokeAndContinueUntilValueReceived(
- route,
- RouterHookType.PRE_HANDLER,
- response,
- context
- );
- if (isPromise(data)) data = await data;
- if (!isResponseSent(response) && data == null) {
- data = route.handle(context);
- if (isPromise(data)) data = await data;
- let postHandlerData = hookInvoker.invokeAndContinueUntilValueReceived(
- route,
- RouterHookType.POST_HANDLER,
- response,
- context,
- data
- );
- if (isPromise(postHandlerData))
- postHandlerData = await postHandlerData;
- if (postHandlerData != null) data = postHandlerData;
- }
- } catch (error) {
- this.getService(ErrorSender).send(request, response, error);
- return;
- }
- if (!isResponseSent(response)) {
- this.getService(DataSender).send(response, data);
- }
- }
- }
- /**
- * Add hook.
- *
- * Example:
- * ```
- * import {TrieRouter} from '@e22m4u/js-trie-router';
- * import {RouterHookType} from '@e22m4u/js-trie-router';
- *
- * // Router instance.
- * const router = new TrieRouter();
- *
- * // Adds the "preHandler" hook for each route.
- * router.addHook(
- * RouterHookType.PRE_HANDLER,
- * ctx => { ... },
- * );
- *
- * // Adds the "postHandler" hook for each route.
- * router.addHook(
- * RouterHookType.POST_HANDLER,
- * ctx => { ... },
- * );
- * ```
- *
- * @param {string} type
- * @param {Function} hook
- * @returns {this}
- */
- addHook(type, hook) {
- this.getService(HookRegistry).addHook(type, hook);
- return this;
- }
- /**
- * Add pre-handler hook.
- *
- * @param {Function} hook
- * @returns {this}
- */
- addPreHandler(hook) {
- this.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, hook);
- return this;
- }
- /**
- * Add post-handler hook.
- *
- * @param {Function} hook
- * @returns {this}
- */
- addPostHandler(hook) {
- this.getService(HookRegistry).addHook(RouterHookType.POST_HANDLER, hook);
- return this;
- }
- };
- __name(_TrieRouter, "TrieRouter");
- var TrieRouter = _TrieRouter;
- // Annotate the CommonJS export names for ESM import in node:
- 0 && (module.exports = {
- BodyParser,
- CHARACTER_ENCODING_LIST,
- CookiesParser,
- DataSender,
- EXPOSED_ERROR_PROPERTIES,
- ErrorSender,
- HookInvoker,
- HookRegistry,
- HttpMethod,
- METHODS_WITH_BODY,
- QueryParser,
- RequestContext,
- RequestParser,
- Route,
- RouteRegistry,
- RouterHookType,
- RouterOptions,
- TrieRouter,
- UNPARSABLE_MEDIA_TYPES,
- cloneDeep,
- createCookiesString,
- createDebugger,
- createError,
- createRequestMock,
- createResponseMock,
- createRouteMock,
- fetchRequestBody,
- getRequestPathname,
- isPromise,
- isReadableStream,
- isResponseSent,
- isWritableStream,
- parseContentType,
- parseCookies,
- parseJsonBody,
- toCamelCase
- });
|