forked from stoplightio/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
259 lines (219 loc) · 9.16 KB
/
server.ts
File metadata and controls
259 lines (219 loc) · 9.16 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import {
createInstance,
IHttpNameValue,
IHttpNameValues,
ProblemJsonError,
VIOLATIONS,
IHttpConfig,
} from '@stoplight/prism-http';
import { DiagnosticSeverity, HttpMethod, IHttpOperation, Dictionary } from '@stoplight/types';
import { IncomingMessage, ServerResponse, IncomingHttpHeaders } from 'http';
import { AddressInfo } from 'net';
import { IPrismHttpServer, IPrismHttpServerOpts } from './types';
import { IPrismDiagnostic } from '@stoplight/prism-core';
import { MicriHandler } from 'micri';
import micri, { Router, json, send, text } from 'micri';
import typeIs from 'type-is';
import { getHttpConfigFromRequest } from './getHttpConfigFromRequest';
import { serialize } from './serialize';
import { merge } from 'lodash/fp';
import { pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
import * as E from 'fp-ts/Either';
import * as IOE from 'fp-ts/IOEither';
function searchParamsToNameValues(searchParams: URLSearchParams): IHttpNameValues {
const params = {};
for (const key of searchParams.keys()) {
const values = searchParams.getAll(key);
params[key] = values.length === 1 ? values[0] : values;
}
return params;
}
function addressInfoToString(addressInfo: AddressInfo | string | null) {
if (!addressInfo) return '';
if (typeof addressInfo === 'string') return addressInfo;
return `http://${addressInfo.address}:${addressInfo.port}`;
}
type ValidationError = {
location: string[];
severity: string;
code: string | number | undefined;
message: string | undefined;
};
const MAX_SAFE_HEADER_LENGTH = 8 * 1024 - 100; // 8kb minus some
function addViolationHeader(reply: ServerResponse, validationErrors: ValidationError[]) {
if (validationErrors.length === 0) {
return;
}
let value = JSON.stringify(validationErrors);
if (value.length > MAX_SAFE_HEADER_LENGTH) {
value = `Too many violations! ${value.substring(0, MAX_SAFE_HEADER_LENGTH)}`;
}
reply.setHeader('sl-violations', value);
}
function parseRequestBody(request: IncomingMessage) {
// if no body provided then return null instead of empty string
if (
// If the body size is null, it means the body itself is null so the promise can resolve with a null value
request.headers['content-length'] === '0' ||
// Per HTTP 1.1 - these 2 headers are the valid way to indicate that a body exists:
// > The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field.
// https://httpwg.org/specs/rfc9112.html#message.body
(request.headers['transfer-encoding'] === undefined && request.headers['content-length'] === undefined)
) {
return Promise.resolve(null);
}
if (typeIs(request, ['application/json', 'application/*+json'])) {
return json(request, { limit: '10mb' });
} else {
return text(request, { limit: '10mb' });
}
}
export const createServer = (operations: IHttpOperation[], opts: IPrismHttpServerOpts): IPrismHttpServer => {
const { components, config } = opts;
const handler: MicriHandler = async (request, reply) => {
const { url, method, headers } = request;
const body = await parseRequestBody(request);
const { searchParams, pathname } = new URL(
url!, // url can't be empty for HTTP request
'http://example.com' // needed because URL can't handle relative URLs
);
const input = {
method: (method ? method.toLowerCase() : 'get') as HttpMethod,
url: {
path: pathname,
baseUrl: searchParams.get('__server') || undefined,
query: searchParamsToNameValues(searchParams),
},
headers: headers as IHttpNameValue,
body,
};
components.logger.info({ input }, 'Request received');
const requestConfig: E.Either<Error, IHttpConfig> = pipe(
getHttpConfigFromRequest(input),
E.map(operationSpecificConfig => ({ ...config, mock: merge(config.mock, operationSpecificConfig) }))
);
pipe(
TE.fromEither(requestConfig),
TE.chain(requestConfig => prism.request(input, operations, requestConfig)),
TE.chainIOEitherK(response => {
const { output } = response;
const inputValidationErrors = response.validations.input.map(createErrorObjectWithPrefix('request'));
const outputValidationErrors = response.validations.output.map(createErrorObjectWithPrefix('response'));
const inputOutputValidationErrors = inputValidationErrors.concat(outputValidationErrors);
if (inputOutputValidationErrors.length > 0) {
addViolationHeader(reply, inputOutputValidationErrors);
const errorViolations = outputValidationErrors.filter(
v => v.severity === DiagnosticSeverity[DiagnosticSeverity.Error]
);
if (opts.config.errors && errorViolations.length > 0) {
return IOE.left(
ProblemJsonError.fromTemplate(
VIOLATIONS,
'Your request/response is not valid and the --errors flag is set, so Prism is generating this error for you.',
{ validation: errorViolations }
)
);
}
}
inputOutputValidationErrors.forEach(validation => {
const message = `Violation: ${validation.location.join('.') || ''} ${validation.message}`;
if (validation.severity === DiagnosticSeverity[DiagnosticSeverity.Error]) {
components.logger.error({ name: 'VALIDATOR' }, message);
} else if (validation.severity === DiagnosticSeverity[DiagnosticSeverity.Warning]) {
components.logger.warn({ name: 'VALIDATOR' }, message);
} else {
components.logger.info({ name: 'VALIDATOR' }, message);
}
});
return IOE.fromEither(
E.tryCatch(() => {
if (output.headers) Object.entries(output.headers).forEach(([name, value]) => reply.setHeader(name, value));
send(
reply,
output.statusCode,
serialize(output.body, reply.getHeader('content-type') as string | undefined)
);
}, E.toError)
);
}),
TE.mapLeft((e: Error & { status?: number; additional?: { headers?: Dictionary<string> } }) => {
if (!reply.writableEnded) {
reply.setHeader('content-type', 'application/problem+json');
if (e.additional && e.additional.headers)
Object.entries(e.additional.headers).forEach(([name, value]) => reply.setHeader(name, value));
send(reply, e.status || 500, JSON.stringify(ProblemJsonError.toProblemJson(e)));
} else {
reply.end();
}
components.logger.error({ input }, `Request terminated with error: ${e}`);
})
)();
};
function setCommonCORSHeaders(incomingHeaders: IncomingHttpHeaders, res: ServerResponse) {
res.setHeader('Access-Control-Allow-Origin', incomingHeaders['origin'] || '*');
res.setHeader('Access-Control-Allow-Headers', incomingHeaders['access-control-request-headers'] || '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Expose-Headers', incomingHeaders['access-control-expose-headers'] || '*');
}
const server = micri(
Router.router(
Router.on.options(
() => opts.cors,
(req: IncomingMessage, res: ServerResponse) => {
setCommonCORSHeaders(req.headers, res);
if (!!req.headers['origin'] && !!req.headers['access-control-request-method']) {
// This is a preflight request, so we'll respond with the appropriate CORS response
res.setHeader(
'Access-Control-Allow-Methods',
req.headers['access-control-request-method'] || 'GET,DELETE,HEAD,PATCH,POST,PUT,OPTIONS'
);
res.setHeader('Vary', 'origin');
// This should not be required since we're responding with a 204, which has no content by definition. However
// Safari does not really understand that and throws a Network Error. Explicit is better than implicit.
res.setHeader('Content-Length', '0');
return send(res, 204);
}
return handler(req, res);
}
),
Router.otherwise((req, res, options) => {
if (opts.cors) setCommonCORSHeaders(req.headers, res);
return handler(req, res, options);
})
)
);
const prism = createInstance(config, components);
return {
get prism() {
return prism;
},
get logger() {
return components.logger;
},
close() {
return new Promise((resolve, reject) =>
server.close(error => {
if (error) {
reject(error);
}
resolve();
})
);
},
listen: (port: number, ...args: any[]) =>
new Promise((resolve, reject) => {
server.once('error', e => reject(e.message));
server.listen(port, ...args, (err: unknown) => {
if (err) return reject(err);
return resolve(addressInfoToString(server.address()));
});
}),
};
};
const createErrorObjectWithPrefix = (locationPrefix: string) => (detail: IPrismDiagnostic) => ({
location: [locationPrefix].concat(detail.path || []),
severity: DiagnosticSeverity[detail.severity],
code: detail.code,
message: detail.message,
});