-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.ts
236 lines (218 loc) · 6.72 KB
/
application.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { serve, Server, HTTPOptions } from './deps.ts'
import { Request, Response, IRequest, IResponse } from './mod.ts'
export type NextFunction = (err?: any) => void
export type Middleware<
Req extends IRequest = IRequest,
Res extends IResponse = IResponse,
Next extends NextFunction = NextFunction
> = (request: Req, response: Res, next: Next) => void
type MiddlewareStacks = {
before: Middleware [],
after: Middleware [],
main: Middleware [],
error: Middleware [],
}
type Stacks = 'before' | 'after' | 'main' | 'error'
/**
* A class which registers middleware (via `.use()`) and then processes
* inbound requests against that middleware (via `.listen()`).
*/
export class Mith {
private middlewareStacks: MiddlewareStacks = {
before: [],
after: [],
main: [],
error: []
}
private PORT = 8000
public server?: Server
/**
* Register middleware on the before stack.
* @param Middleware can be a single one or an array
* @return void
*/
public before(middleware: Middleware | Middleware[]) {
this.use(middleware, 'before')
}
/**
* Register middleware on the after stack.
* @param Middleware can be a single one or an array
* @return void
*/
public after(middleware: Middleware | Middleware[]) {
this.use(middleware, 'after')
}
/**
* Register middleware on the main stack.
* @param Middleware can be a single one or an array
* @return void
*/
public main(middleware: Middleware | Middleware[]) {
this.use(middleware, 'main')
}
/**
* Register middleware to be used when next(error) is called.
* @param Middleware can be a single one or an array
* @return void
*/
public error(middleware: Middleware | Middleware[]) {
this.use(middleware, 'error')
}
/**
* Register middleware to be used with the application.
* @param Middleware can be a single one or an array
* @return void
*/
public use(middleware: Middleware | Middleware[], stack: Stacks = 'main') {
if (Array.isArray(middleware)) {
this.getMiddlewareStack(stack).push(...middleware)
} else {
this.getMiddlewareStack(stack).push(middleware)
}
}
/**
* Create an HTTP server with given options
*
* const options = {
* hostname: "localhost",
* port: 8000,
* }
*
* mith.listen(options)
*
* @param options Server configuration
* @return void
*/
public listen(options: string | HTTPOptions = { port: this.PORT }) {
this.server = serve(options)
return this.setupListener()
}
/**
* Closes the ongoing server
*
* mith.close()
*/
public async close() {
this.server?.close()
}
/**
* Listens to the async iterable server instance for incoming requests
* Runs the stack of middleware for each request
*/
private async setupListener() {
if (this.server) {
for await (const req of this.server) {
this.dispatch(new Request(req), new Response(req), 'before')
}
}
}
/**
* Dispatch function will trigger the middleware in sequence based on the current stack
*
* @param request Deno Server Request Object
* @param response Mith Server Response Object
* @param stack indentifies the current middleware stack
* @param index number
* @param next function can be passed to cicle between Mith apps
* @return void
*/
public dispatch (request: IRequest, response: IResponse, stack: Stacks, index: number = 0, next?: NextFunction, error?: any): void {
let middleWare = this.getMiddlewareStack(stack)
if (!middleWare.length) {
if (stack === 'after') {
if (next) {
next(error)
}
return
}
this.dispatch(request, response, this.nextStack(stack), 0, next, error)
return
}
middleWare[index](
request,
response,
(error?: any): void => {
this.nextMiddleware(request, response, stack, index, next, error)
}
)
}
/**
* getMiddlewareStack based on the requested stack returns the correct middleware items
*
* @param stack String representing the stack
* @return Array of middlewares
*/
private getMiddlewareStack(stack: Stacks) {
return this.middlewareStacks[stack] || []
}
/**
* nextMiddleware function will trigger the next middleware in line
* In case an error is passed it moves to the error middleware stack
*
* @param request Deno Server Request Object
* @param response Mith Server Response Object
* @param stack indentifies the current middleware stack
* @param index number
* @param next function can be passed to cicle between Mith apps
* @param error received from a callback
* @return void
*/
private nextMiddleware(request: IRequest, response: IResponse, stack: Stacks, index: number, next?: NextFunction, error?: any) {
if (response.finished) {
return this.stackSendOrNext(request, response, stack, next, error)
}
if (error) {
response.error = error
if (this.getMiddlewareStack('error').length) {
if (stack === 'error') {
if (index + 1 < this.getMiddlewareStack('error').length) {
return this.dispatch(request, response, stack, index + 1, next, error)
}
} else {
return this.dispatch(request, response, 'error', 0, next, error)
}
}
} else if (index + 1 < this.getMiddlewareStack(stack).length) {
return this.dispatch(request, response, stack, index + 1, next)
}
return this.stackSendOrNext(request, response, stack, next, error)
}
/**
* Returns the next stack in line before > main > after || error > after
* @param stack current stack of the request
*/
private nextStack(stack: Stacks) {
if (stack === 'before') {
return 'main'
}
return 'after'
}
/**
* Calls sendResponse in case Mith server is already setup
* Calls the next function in case this is a sub application
* Stops execution otherwise
*
* @param request Deno Server Request Object
* @param response Mith Server Response Object
* @param stack on going middleware stack
* @param next function can be passed to cicle between Mith apps
* @param error in case of error, use it has the response body
* @return void
*/
private stackSendOrNext(req: IRequest, res: IResponse, stack: Stacks, next?: NextFunction, error?: any) {
if (stack === 'before') {
this.dispatch(req, res, error ? 'error' : this.nextStack(stack), 0, next, error)
} else if (stack === 'main' || stack === 'error') {
if (this.server) {
if (error) {
res.body = error
}
res.sendResponse()
}
this.dispatch(req, res, error && stack !== 'error' ? 'error' : this.nextStack(stack), 0, next, error)
} else if (next) {
next(error)
}
return
}
}