• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

silvermine / lambda-express / 9602089925

20 Jun 2024 05:51PM UTC coverage: 98.669% (-0.7%) from 99.398%
9602089925

Pull #90

github

onebytegone
refactor: switch to ALB types provided by @types/aws-lambda
Pull Request #90: build: bump @types/aws-lambda

207 of 212 branches covered (97.64%)

Branch coverage included in aggregate %.

4 of 6 new or added lines in 1 file covered. (66.67%)

460 of 464 relevant lines covered (99.14%)

1989.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/chains/SubRouterProcessorChain.ts
1
import { IRequestMatchingProcessorChain } from './ProcessorChain';
2
import { PathParams, IRouter, NextCallback, RouterOptions } from '../interfaces';
3
import { Request, Response } from '..';
4
const pathToRegexp = require('path-to-regexp');
4✔
5

6
export class SubRouterProcessorChain implements IRequestMatchingProcessorChain {
4✔
7

8
   private readonly _matcher: RegExp;
9
   private readonly _router: IRouter;
10

11
   public constructor(path: PathParams, router: IRouter, parentRouterOptions: RouterOptions) {
12
      // Although we use the subrouter to handle matched routes, we use the parent
13
      // router's case-sensitivity setting to match the route path that this subrouter is
14
      // mounted to because that's what Express does. For example:
15
      //
16
      // ```
17
      // const express = require('express'),
18
      //       app = express(),
19
      //       router = express.Router({ caseSensitive: false });
20
      //
21
      // app.enable('case sensitive routing');
22
      // app.use('/hello', router);
23
      // router.get('/world', (req, resp) => { resp.send('Hello world'); });
24
      // ```
25
      //
26
      // In the example above, the `/hello` part of the path is case-sensitive, and the
27
      // `/world` part of the path is case-insensitive. Therefore, GET requests to
28
      // `/hello/WORLD` would match the `/world` handler but requests to `/HELLO/world`
29
      // would not. To replicate this in Lambda Express, we have to use the parent
30
      // router's case sensitivity settings (`app`, in this example) for the mounting
31
      // point (`/world`).
32
      this._matcher = pathToRegexp(path, [], { sensitive: parentRouterOptions.caseSensitive, strict: false, end: false });
128✔
33
      this._router = router;
128✔
34
   }
35

36
   public run(err: unknown, req: Request, resp: Response, done: NextCallback): void {
37
      const result = this._matcher.exec(req.path);
528✔
38

39
      if (!result || result.length === 0) {
528✔
40
         throw new Error(`This subrouter does not match URL "${req.path}": ${this._matcher}`);
4✔
41
      }
42

43
      const baseURL = result[0].replace(/\/$/, ''),
524✔
44
            subRequest = req.makeSubRequest(baseURL);
524✔
45

46
      this._router.handle(err, subRequest, resp, done);
524✔
47
   }
48

49
   public matches(req: Request): boolean {
50
      return this._matcher.test(req.path);
640✔
51
   }
52

53
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc