Skip to content

Commit

Permalink
feat: add context
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tkachenko committed Aug 12, 2023
1 parent b5c3896 commit 08c8b9b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,19 @@ const requestHandlers = [
{
// function to test the incoming request
// if returns true `handle` function will process the request
isMatching: (method: HttpMethod, path: string): boolean => true,
isMatching: (method: HttpMethod, path: string, context: Record<string, any>): boolean => true,

/**
* Request handler
*/
handle: async (req: IncomingMesssage, res: ServerResponse, proxyRequest: ProxyRequest): Promise<void> => {
handle: async (
req: IncomingMesssage,
res: ServerResponse,
proxyRequest: ProxyRequest,
method: HttpMethod,
path: string,
context: Record<string, any>
): Promise<void> => {
// proxy incoming request to the upstream
// optionally pass ProxyRequestConfiguration object as a parameter
await proxyRequest({
Expand Down Expand Up @@ -142,15 +149,22 @@ const webSocketHandlers = [
{
// function to test the incoming request
// if returns true `handle` function will process the request
isMatching: (path: string): boolean => true,
isMatching: (path: string, context: Record<string, any>): boolean => true,

/**
* Request handler
*/
handle: async (req: IncomingMessage, socket: Duplex, head: Buffer, handle: () => Promise<void>): Promise<void> => {
handle: async (
req: IncomingMessage,
socket: Socket,
head: Buffer,
proxyRequest: ProxyRequest,
path: string,
context: Record<string, any>
): Promise<void> => {
// proxy incoming request to the upstream
// optionally pass ProxyRequestConfiguration object as a parameter
await handle(
await proxyRequest(
// optionally provide alternative path for the upstream request
url: '/another/path',

Expand Down
22 changes: 14 additions & 8 deletions src/Prxi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Prxi {

this.logInfo(`[${requestId}] [Prxi] Handling incoming request for method: ${req.method} and path: ${path}`);

const {handler, upstream, proxy} = Prxi.findRequestHandler(proxies, upstreamConfigurations, <HttpMethod> req.method, path);
const {handler, upstream, proxy, context} = Prxi.findRequestHandler(proxies, upstreamConfigurations, <HttpMethod> req.method, path);
if (handler) {
/* istanbul ignore next */
if (upstream.errorHandler) {
Expand All @@ -89,7 +89,6 @@ export class Prxi {
);
RequestUtils.updateResponseHeaders(res, headersToSet);


handler.handle(
req,
res,
Expand All @@ -98,7 +97,7 @@ export class Prxi {
): Promise<void> => {
this.logInfo(`[${requestId}] [Prxi] Handling HTTP proxy request for path: ${path}`);
await proxy.http.proxy(requestId, req, res, proxyConfiguration);
}, path).catch((err) => {
}, <HttpMethod> req.method, path, context).catch((err) => {
this.logError(`[${requestId}] [Prxi] Error occurred upon making the "${req.method}:${path}" request`, err);
errorHandler(req, res, err).catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle error with errorHandler`, err);
Expand All @@ -121,7 +120,7 @@ export class Prxi {
const requestId = id++;

const path = RequestUtils.getPath(req);
const {handler, proxy} = Prxi.findWebSocketHandler(proxies, upstreamConfigurations, path);
const {handler, proxy, context} = Prxi.findWebSocketHandler(proxies, upstreamConfigurations, path);

this.logInfo(`[${requestId}] [Prxi] Upgrade event received on path: ${path}`);
// handle websocket
Expand All @@ -133,7 +132,7 @@ export class Prxi {
handler.handle(req, socket, head, async (proxyConfiguration?: ProxyRequestConfiguration): Promise<void> => {
this.logInfo(`[${requestId}] [Prxi] Handling WS proxy request for path: ${path}`);
await proxy.ws.proxy(requestId, req, socket, head, proxyConfiguration);
}, path)
}, path, context)
.catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle websocket request`, err);

Expand Down Expand Up @@ -182,15 +181,18 @@ export class Prxi {
proxy: Proxy | null,
handler: RequestHandlerConfig | null,
upstream: UpstreamConfiguration | null,
context: Record<string, any>
} | null {
const context = {};
for (const upstream of configs) {
const handler = upstream.requestHandlers?.find(i => i.isMatching(method, path));
const handler = upstream.requestHandlers?.find(i => i.isMatching(method, path, context));
if (handler) {
const proxy = proxies.find(p => p.upstream === upstream);
return {
proxy,
handler,
upstream,
context
};
}
}
Expand All @@ -199,6 +201,7 @@ export class Prxi {
proxy: null,
handler: null,
upstream: null,
context,
};
}

Expand All @@ -212,15 +215,18 @@ export class Prxi {
proxy: Proxy | null,
handler: WebSocketHandlerConfig | null,
upstream: UpstreamConfiguration | null,
context: Record<string, any>
} | null {
const context = {};
for (const upstream of configs) {
const handler = upstream.webSocketHandlers?.find(i => i.isMatching(path));
const handler = upstream.webSocketHandlers?.find(i => i.isMatching(path, context));
if (handler) {
const proxy = proxies.find(p => p.upstream === upstream);
return {
proxy,
handler,
upstream,