UNPKG

node-httpx-server

Version:

A node HTTP/2 server and beyond dealing with streams

147 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processRoutes = exports.getRouteEntry = exports.getPathParameters = exports.incomingContainsRouterPath = exports.recurseCallbacks = void 0; const http2_1 = require("http2"); const constants_1 = require("./constants"); const streamRouter_1 = require("./streamRouter"); const recurseCallbacks = ({ callbacks, onComplete, onError, pathParameters, searchParams, source, truncatedPath, }) => { const [callback, ...restOfCallbacks] = callbacks; const handleComplete = (message) => onComplete({ message, stream: source.stream }); const handleError = (error) => onError({ error, stream: source.stream }); const handleNext = () => { if (callback instanceof streamRouter_1.StreamRouter) { callback.process({ currentPath: truncatedPath, onComplete, onError, pathParameters, searchParams, source, }); return; } if (restOfCallbacks.length === 0) return; (0, exports.recurseCallbacks)({ callbacks: restOfCallbacks, onComplete, onError, pathParameters, searchParams, source, truncatedPath, }); }; if (callback instanceof streamRouter_1.StreamRouter) { callback.process({ currentPath: truncatedPath, onComplete, onError, pathParameters, searchParams, source, }); return; } callback({ complete: handleComplete, error: handleError, next: handleNext, pathParameters, searchParams, source, }); }; exports.recurseCallbacks = recurseCallbacks; const incomingContainsRouterPath = ({ incomingPathArr, routerPathArr, }) => { if (!incomingPathArr.length || !routerPathArr.length) return false; if (incomingPathArr.length !== routerPathArr.length && routerPathArr.length > incomingPathArr.length) return false; return routerPathArr.every((currentRouterPathValue, idx) => (currentRouterPathValue.startsWith('{') && currentRouterPathValue.endsWith('}')) || incomingPathArr[idx] === currentRouterPathValue); }; exports.incomingContainsRouterPath = incomingContainsRouterPath; const getPathParameters = ({ incomingPathArr, routePathArr, }) => { let pathParameter = {}; routePathArr.forEach((routePath, idx) => { if (!routePath.startsWith('{') || !routePath.endsWith('}')) return; pathParameter[routePath.substring(1, routePath.length - 1)] = incomingPathArr[idx]; }); return pathParameter; }; exports.getPathParameters = getPathParameters; const getRouteEntry = ({ incomingPathArr, routersArray, source, }) => { const routeEntry = routersArray.find(([[path, method]]) => { if (!incomingPathArr.length || !path || path.indexOf('/') === -1) return false; const routerPathArr = path.split('/').slice(1); const routerPathFound = (0, exports.incomingContainsRouterPath)({ incomingPathArr, routerPathArr, }); return (routerPathFound && (!method || source.headers[http2_1.constants.HTTP2_HEADER_METHOD] === method)); }); return routeEntry; }; exports.getRouteEntry = getRouteEntry; const processRoutes = ({ currentPath, onComplete, onError, pathParameters, routers, searchParams, source, }) => { if (!currentPath || currentPath.indexOf('/') === -1) { const serverError = new Error('Server error'); serverError.name = constants_1.routerError.SERVER_ERROR; console.error('Malformed currentPath. Must include current path with /'); onError({ error: serverError, stream: source.stream, }); return; } if (currentPath.indexOf('?') > -1) { const searchSplitArr = currentPath.split('?'); currentPath = searchSplitArr[0]; searchParams = new URLSearchParams(searchSplitArr[1]); } const incomingPathArr = currentPath.split('/').slice(1); const routeEntry = (0, exports.getRouteEntry)({ incomingPathArr, routersArray: [...routers.entries()], source, }); if (!routeEntry) { const notFoundError = new Error('Not found'); notFoundError.name = constants_1.routerError.NOT_FOUND; onError({ error: notFoundError, stream: source.stream, }); return; } const [[path], callbacks] = routeEntry; const routePathArr = path.split('/').slice(1); pathParameters = { ...pathParameters, ...(0, exports.getPathParameters)({ incomingPathArr, routePathArr, }), }; const newCurrentPathArr = incomingPathArr.slice(routePathArr.length); const truncatedPath = `/${newCurrentPathArr.join('/')}`; (0, exports.recurseCallbacks)({ callbacks, onComplete, onError, pathParameters, searchParams, source, truncatedPath, }); }; exports.processRoutes = processRoutes; //# sourceMappingURL=helpers.js.map