-
Notifications
You must be signed in to change notification settings - Fork 36
/
exegesisRunner.ts
245 lines (216 loc) · 8.22 KB
/
exegesisRunner.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
237
238
239
240
241
242
243
244
245
import * as http from 'http';
import { Readable } from 'stream';
import { asError, HttpError } from '../errors';
import { invokeController } from '../controllers/invoke';
import stringToStream from '../utils/stringToStream';
import { ValidationError } from '../errors';
import bufferToStream from '../utils/bufferToStream';
import { isReadable } from '../utils/typeUtils';
import {
ApiInterface,
ExegesisRunner,
HttpResult,
ExegesisContext,
ResponseValidationCallback,
ResolvedOperation,
ExegesisOptions,
ExegesisResponse,
} from '../types';
import ExegesisContextImpl from './ExegesisContextImpl';
import PluginsManager from './PluginsManager';
import { IValidationError } from '../types/validation';
import { HandleErrorFunction } from '../types/options';
async function handleSecurity(operation: ResolvedOperation, context: ExegesisContext) {
const authenticated = await operation.authenticate(context);
context.security = authenticated;
if (authenticated) {
const matchedSchemes = Object.keys(authenticated);
if (matchedSchemes.length === 1) {
context.user = authenticated[matchedSchemes[0]].user;
}
}
}
function setDefaultContentType(res: ExegesisResponse) {
const body = res.body;
if (res.headers['content-type']) {
// Nothing to do!
} else if (body === undefined || body === null) {
// Do nothing
} else if (body instanceof Buffer) {
res.headers['content-type'] = 'text/plain';
} else if (typeof body === 'string') {
res.headers['content-type'] = 'text/plain';
} else if (isReadable(body)) {
res.headers['content-type'] = 'text/plain';
} else {
res.headers['content-type'] = 'application/json';
}
}
function resultToHttpResponse(context: ExegesisContext, result: any): HttpResult {
let output: Readable | undefined;
const headers = context.res.headers;
if (result) {
if (result instanceof Buffer) {
output = bufferToStream(result);
} else if (typeof result === 'string') {
output = stringToStream(result);
} else if (isReadable(result)) {
output = result;
} else {
if (!headers['content-type']) {
headers['content-type'] = 'application/json';
}
output = stringToStream(JSON.stringify(result), 'utf-8');
}
}
return {
status: context.res.statusCode,
headers,
body: output,
};
}
function handleError(err: Error) {
if (err instanceof ValidationError) {
// TODO: Allow customization of validation error? Or even
// just throw the error instead of turning it into a message?
const jsonError = {
message: 'Validation errors',
errors: err.errors.map((error: IValidationError) => {
return {
message: error.message,
location: error.location,
};
}),
};
return {
status: err.status,
headers: { 'content-type': 'application/json' },
body: stringToStream(JSON.stringify(jsonError), 'utf-8'),
};
} else if (Number.isInteger((err as any).status)) {
return {
status: (err as any).status,
headers: (err as any).headers || { 'content-type': 'application/json' },
body: stringToStream(JSON.stringify({ message: err.message }), 'utf-8'),
};
} else {
throw err;
}
}
/**
* Returns a `(req, res) => Promise<boolean>` function, which handles incoming
* HTTP requests. The returned function will return true if the request was
* handled, and false otherwise.
*
* @returns runner function.
*/
export default async function generateExegesisRunner<T>(
api: ApiInterface<T>,
options: {
autoHandleHttpErrors: boolean | HandleErrorFunction;
plugins: PluginsManager;
onResponseValidationError?: ResponseValidationCallback;
validateDefaultResponses: boolean;
originalOptions: ExegesisOptions;
}
): Promise<ExegesisRunner> {
const plugins = options.plugins;
return async function exegesisRunner(
req: http.IncomingMessage,
res: http.ServerResponse
): Promise<HttpResult | undefined> {
const method = req.method || 'get';
const url = req.url || '/';
let result: HttpResult | undefined;
try {
await plugins.preRouting({ req, res });
const resolved = api.resolve(method, url, req.headers);
if (!resolved) {
return result;
}
if (!resolved.operation) {
const error: any = new Error(`Method ${method} not allowed for ${url}`);
error.status = 405;
error.headers = {
allow: resolved.allowedMethods.join(',').toUpperCase(),
'content-type': 'application/json',
};
return handleError(error);
}
const context = new ExegesisContextImpl<T>(
req,
res,
resolved.api,
options.originalOptions
);
if (!context.isResponseFinished()) {
await plugins.postRouting(context);
}
const { operation } = resolved;
context._setOperation(resolved.baseUrl, resolved.path, operation);
if (!operation.controller) {
throw new Error(`No controller found for ${method} ${url}`);
}
await handleSecurity(operation, context);
if (!context.isResponseFinished()) {
await plugins.postSecurity(context);
}
if (!context.isResponseFinished()) {
// Fill in context.params and context.requestBody.
await context.getParams();
await context.getRequestBody();
}
if (!context.isResponseFinished()) {
await invokeController(operation.controllerModule, operation.controller, context);
}
if (!context.origRes.headersSent) {
// Set _afterController to allow postController() plugins to
// modify the response.
context.res._afterController = true;
await plugins.postController(context);
}
if (!context.origRes.headersSent) {
// Before response validation, if there is a body and no
// content-type has been set, set a reasonable default.
setDefaultContentType(context.res);
if (options.onResponseValidationError) {
const responseValidationResult = resolved.operation.validateResponse(
context.res,
options.validateDefaultResponses
);
try {
if (
responseValidationResult.errors &&
responseValidationResult.errors.length
) {
options.onResponseValidationError({
errors: responseValidationResult.errors,
isDefault: responseValidationResult.isDefault,
context,
});
}
} catch (e) {
const err = asError(e) as HttpError;
(err as any).status = err.status || 500;
throw err;
}
}
await plugins.postResponseValidation(context);
}
if (!context.origRes.headersSent) {
result = resultToHttpResponse(context, context.res.body);
}
return result;
} catch (e) {
const err = asError(e);
if (options.autoHandleHttpErrors) {
if (options.autoHandleHttpErrors instanceof Function) {
return options.autoHandleHttpErrors(err, { req });
}
return handleError(err);
} else {
throw err;
}
}
};
}