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

lambda-lambda-lambda / router / 8086199887

28 Feb 2024 07:39PM UTC coverage: 98.228%. Remained the same
8086199887

push

github

nuxy
Updated package version (0.8.1)

155 of 162 branches covered (95.68%)

Branch coverage included in aggregate %.

233 of 233 relevant lines covered (100.0%)

871.51 hits per line

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

98.21
/src/router/Stack.js
1
/**
2
 *  lambda-lambda-lambda/router
3
 *  AWS Lambda@Edge serverless application router.
4
 *
5
 *  Copyright 2021-2023, Marc S. Brooks (https://mbrooks.info)
6
 *  Licensed under the MIT license:
7
 *  http://www.opensource.org/licenses/mit-license.php
8
 */
9

10
'use strict';
11

12
// Local modules.
13
const {RouterError} = require('./Error');
498✔
14
const {
15
  isAsyncFunc,
16
  isValidFunc,
17
  promiseEvents
18
} = require('./Utils');
498✔
19

20
/**
21
 * Provides Stack item handler and methods.
22
 */
23
class RouterStack {
24

25
  /**
26
   * Create new stack instance.
27
   */
28
  constructor() {
29
    this.middleware = [];
666✔
30
    this.routes     = [];
666✔
31
    this.resources  = [];
666✔
32
    this.fallback   = null;
666✔
33
  }
34

35
  /**
36
   * Add new function to stack items.
37
   *
38
   * Stack function types
39
   *  - middleware
40
   *  - route:<method>
41
   *  - resource:<method>
42
   *  - fallback
43
   *
44
   * @param {Function} func
45
   *   Route/middleware function.
46
   *
47
   * @example
48
   * const func1 = function(req, res, next) {
49
   *   if (req.method() === 'POST') {
50
   *     res.status(405).send();
51
   *   } else {
52
   *     next();
53
   *   }
54
   * };
55
   *
56
   * setFuncName(func1, 'middleware');
57
   * stack.add(func1);
58
   *
59
   *   ..
60
   *
61
   * const func2 = function(req, res) {
62
   *   res.setHeader('Content-Type', 'text/html');
63
   *   res.status(200).send('Hello World');
64
   * };
65
   *
66
   * setFuncName(func2, 'route:get');
67
   * stack.add(func2);
68
   */
69
  add(func) {
70
    if (isValidFunc(func)) {
1,680✔
71
      const name = func.name;
1,674✔
72

73
      switch (true) {
1,674✔
74
        case /^middleware/.test(name):
75
          this.middleware.push(func);
726✔
76
          break;
726✔
77

78
        case /^route:/.test(name):
79
          this.routes.push(func);
234✔
80
          break;
234✔
81

82
        case /^resource:/.test(name):
83
          this.resources.push(func);
210✔
84
          break;
210✔
85

86
        default:
87
          this.fallback = func;
504✔
88
      }
89
    }
90
  }
91

92
  /**
93
   * Execute stored functions (a)synchronously.
94
   *
95
   * Order by priority.
96
   * > Middleware, Routes, Resources, fallback
97
   *
98
   * @param {RouterRequest} req
99
   *   Request instance.
100
   *
101
   * @param {RouterResponse} res
102
   *   Response instance.
103
   *
104
   * @return {Promise|undefined}
105
   *
106
   * @example
107
   * stack.exec(req, res);
108
   *
109
   * // updated instance
110
   * res.data();
111
   *
112
   *   ..
113
   *
114
   * stack.exec(req, res)
115
   *   .then(() => res.data())
116
   */
117
  exec(req, res) {
118
    const funcs = [].concat(this.middleware, this.routes, this.resources, [this.fallback]);
534✔
119

120
    let lastItem = true;
534✔
121
    let nextItem = true;
534✔
122
    let promises = [];
534✔
123

124
    funcs.forEach((func, index) => {
534✔
125
      if (nextItem) {
1,758✔
126
        lastItem = index++ === funcs.length;
1,428✔
127
        nextItem = false;
1,428✔
128

129
        if (isAsyncFunc(func)) {
1,428✔
130

131
          // Asynchronous handling.
132
          promises.push(() => {
240✔
133
            return func(req, res, function() {
240✔
134
              throw new RouterError('Middleware next() is unsupported in async');
6✔
135
            });
136
          });
137

138
          nextItem = !lastItem;
240✔
139

140
        } else if (isValidFunc(func)) {
1,188✔
141

142
          // Synchronous handling.
143
          func(req, res, () => nextItem = !lastItem);
1,170✔
144
        }
145
      }
146
    });
147

148
    if (promises.length) {
534✔
149
      return promiseEvents(promises)
180✔
150
        .catch(err => {
151
          if (err instanceof RouterError) {
18✔
152
            throw err;
12✔
153
          } else if (err) {
6!
154
            console.info(err);
6✔
155
          }
156
        });
157
    }
158
  }
159
};
160

161
module.exports = RouterStack;
498✔
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

© 2026 Coveralls, Inc