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

stackpress / ingest / 26237124062

21 May 2026 03:52PM UTC coverage: 90.296% (+22.3%) from 68.006%
26237124062

push

github

cblanquera
version bump

243 of 301 branches covered (80.73%)

Branch coverage included in aggregate %.

520 of 544 relevant lines covered (95.59%)

10.89 hits per line

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

87.5
/ingest/src/plugin/ViewRouter.ts
1
//node
2
import type { Method } from '@stackpress/lib/types';
3
//common
4
import type { 
5
  ActionRouterArgs,
6
  ViewRouterEngine,
7
  ViewRouterRender,
8
  ViewRouterTaskItem,
9
  ActionRouterListener
10
} from '../types.js';
11
//local
12
import type ActionRouter from './ActionRouter.js';
13

14
export default class ViewRouter<R, S, X> {
15
  //A route map to task queues
16
  //event -> [ ...{ entry, priority } ]
17
  public readonly views = new Map<string, Set<ViewRouterTaskItem>>();
44✔
18
  //engine
19
  protected _engine: ViewRouterEngine<R, S, X> = () => void 0;
44✔
20
  //render
21
  protected _render: ViewRouterRender = () => null;
44✔
22
  //parent router
23
  protected _router: ActionRouter<R, S, X>;
24
  //listener straight to the end
25
  protected _listen: ActionRouterListener<R, S, X>;
26

27
  /**
28
   * Get the view engine method
29
   */
30
  public get engine() {
31
    return this._engine;
×
32
  }
33

34
  /**
35
   * Set the view engine method
36
   */
37
  public set engine(engine: ViewRouterEngine<R, S, X>) {
38
    this._engine = engine;
4✔
39
  }
40

41
  /**
42
   * Get the render method
43
   */
44
  public get render() {
45
    return this._render;
1✔
46
  }
47

48
  /**
49
   * Set the render method
50
   */
51
  public set render(render: ViewRouterRender) {
52
    this._render = render;
1✔
53
  }
54

55
  /**
56
   * Sets the router
57
   */
58
  public constructor(
59
    router: ActionRouter<R, S, X>,
60
    listen: ActionRouterListener<R, S, X>
61
  ) {
62
    this._router = router;
44✔
63
    this._listen = listen;
44✔
64
  }
65

66
  /**
67
   * Makes an action from an entry pathname string
68
   * Register the entry, a provision for builders
69
   */
70
  public action(event: string, action: string, priority = 0) {
×
71
    //if the listener group does not exist, create it
72
    if (!this.views.has(event)) {
14!
73
      this.views.set(event, new Set());
14✔
74
    }
75
    //add the listener to the group
76
    this.views.get(event)?.add({ entry: action, priority });
14✔
77
    const router = this;
14✔
78
    return async function TemplateFileAction(
14✔
79
      ...[ props ]: ActionRouterArgs<R, S, X>
80
    ) {
81
      if (!router._engine) return;
4✔
82
      await router._engine(action, props);
3✔
83
    }
84
  }
85

86
  /**
87
   * Route for any method
88
   */
89
  public all(
90
    path: string, 
91
    action: string, 
92
    priority?: number
93
  ) {
94
    return this.route('ALL', path, action, priority);
1✔
95
  }
96

97
  /**
98
   * Route for CONNECT method
99
   */
100
  public connect(
101
    path: string, 
102
    action: string, 
103
    priority?: number
104
  ) {
105
    return this.route('CONNECT', path, action, priority);
1✔
106
  }
107

108
  /**
109
   * Route for DELETE method
110
   */
111
  public delete(
112
    path: string, 
113
    action: string, 
114
    priority?: number
115
  ) {
116
    return this.route('DELETE', path, action, priority);
1✔
117
  }
118

119
  /**
120
   * Route for GET method
121
   */
122
  public get(
123
    path: string, 
124
    action: string, 
125
    priority?: number
126
  ) {
127
    return this.route('GET', path, action, priority);
2✔
128
  }
129

130
  /**
131
   * Route for HEAD method
132
   */
133
  public head(
134
    path: string, 
135
    action: string, 
136
    priority?: number
137
  ) {
138
    return this.route('HEAD', path, action, priority);
1✔
139
  }
140
    
141
  /**
142
   * Adds a callback to the given event listener
143
   */
144
  public on(
145
    event: string|RegExp, 
146
    entry: string,
147
    priority = 0
×
148
  ) {
149
    const key = this._router.eventName(event);
1✔
150
    const action = this.action(key, entry, priority);
1✔
151
    this._listen(key, action, priority);
1✔
152
    return this;
1✔
153
  }
154

155
  /**
156
   * Route for OPTIONS method
157
   */
158
  public options(
159
    path: string, 
160
    action: string, 
161
    priority?: number
162
  ) {
163
    return this.route('OPTIONS', path, action, priority);
1✔
164
  }
165

166
  /**
167
   * Route for PATCH method
168
   */
169
  public patch(
170
    path: string, 
171
    action: string, 
172
    priority?: number
173
  ) {
174
    return this.route('PATCH', path, action, priority);
1✔
175
  }
176

177
  /**
178
   * Route for POST method
179
   */
180
  public post(
181
    path: string, 
182
    action: string, 
183
    priority?: number
184
  ) {
185
    return this.route('POST', path, action, priority);
1✔
186
  }
187

188
  /**
189
   * Route for PUT method
190
   */
191
  public put(
192
    path: string, 
193
    action: string, 
194
    priority?: number
195
  ) {
196
    return this.route('PUT', path, action, priority);
1✔
197
  }
198

199
  /**
200
   * Returns a route
201
   */
202
  public route(
203
    method: Method, 
204
    path: string, 
205
    entry: string, 
206
    priority = 0
×
207
  ) {
208
    const event = this._router.eventName(method, path);
12✔
209
    const action = this.action(event, entry, priority);
12✔
210
    this._listen(event, action, priority);
12✔
211
    return this;
12✔
212
  }
213

214
  /**
215
   * Route for TRACE method
216
   */
217
  public trace(
218
    path: string, 
219
    action: string, 
220
    priority?: number
221
  ) {
222
    return this.route('TRACE', path, action, priority);
1✔
223
  }
224
  
225
  /**
226
   * Allows views from other routers to apply here
227
   */
228
  public use(router: ViewRouter<R, S, X>) {
229
    //first concat their routes with this one
230
    //event -> [ ...{ entry, priority } ]
231
    router.views.forEach((tasks, event) => {
2✔
232
      //if the listener group does not exist
233
      if (!this.views.has(event)) {
1!
234
        //create the listener group
235
        this.views.set(event, new Set());
1✔
236
      }
237
      //add all the tasks to the listener group
238
      //[ ...{ entry, priority } ]
239
      //NOTE: possible duplicate tasks
240
      tasks.forEach(task => this.views.get(event)?.add(task));
1✔
241
    });
242
    return this;
2✔
243
  }
244
};
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