MCPcopy Create free account
hub / github.com/chimurai/http-proxy-middleware / createPathRewriter

Function createPathRewriter

src/path-rewriter.ts:16–47  ·  view source on GitHub ↗
(rewriteConfig: PathRewriteConfig<TReq, TRes> | undefined)

Source from the content-addressed store, hash-verified

14 * Create rewrite function, to cache parsed rewrite rules.
15 */
16export function createPathRewriter<
17 TReq extends IncomingMessage = IncomingMessage,
18 TRes extends ServerResponse = ServerResponse,
19>(rewriteConfig: PathRewriteConfig<TReq, TRes> | undefined) {
20 let rulesCache: RewriteRule[];
21
22 if (!isValidRewriteConfig(rewriteConfig)) {
23 return;
24 }
25
26 if (typeof rewriteConfig === 'function') {
27 const customRewriteFn = rewriteConfig;
28 return customRewriteFn;
29 } else {
30 rulesCache = parsePathRewriteRules(rewriteConfig);
31 return rewritePath;
32 }
33
34 function rewritePath(path: string) {
35 let result = path;
36
37 for (const rule of rulesCache) {
38 if (rule.regex.test(path)) {
39 result = result.replace(rule.regex, rule.value);
40 debug('rewriting path from "%s" to "%s"', path, result);
41 break;
42 }
43 }
44
45 return result;
46 }
47}
48
49function isValidRewriteConfig<
50 TReq extends IncomingMessage = IncomingMessage,

Callers 2

constructorMethod · 0.85

Calls 2

isValidRewriteConfigFunction · 0.85
parsePathRewriteRulesFunction · 0.85

Tested by

no test coverage detected