Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
91 lines (79 loc) · 2.46 KB
/
Copy pathserver.ts
File metadata and controls
91 lines (79 loc) · 2.46 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
import type { Envelope } from '@sentry/core';
import { parseEnvelope } from '@sentry/core';
import express from 'express';
import type { AddressInfo } from 'net';
/**
* Creates a basic Sentry server that accepts POST to the envelope endpoint
*
* This does no checks on the envelope, it just calls the callback if it managed to parse an envelope from the raw POST
* body data.
*/
export function createBasicSentryServer(onEnvelope: (env: Envelope) => void): Promise<[number, () => void]> {
const app = express();
app.use(express.raw({ type: () => true, inflate: true, limit: '100mb' }));
app.post('/api/:id/envelope/', (req, res) => {
try {
const env = parseEnvelope(req.body as Buffer);
onEnvelope(env);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
res.status(200).send();
});
return new Promise(resolve => {
const server = app.listen(0, () => {
const address = server.address() as AddressInfo;
resolve([
address.port,
() => {
server.close();
},
]);
});
});
}
type HeaderAssertCallback = (headers: Record<string, string | string[] | undefined>) => void;
interface TestServer {
get: (path: string, callback: HeaderAssertCallback, result?: number) => TestServer;
start: () => Promise<[string, () => void]>;
}
/** Creates a test server that can be used to check headers */
export function createTestServer(): TestServer {
const gets: Array<[string, HeaderAssertCallback, number]> = [];
let error: unknown | undefined;
const server: TestServer = {
get(path: string, callback: HeaderAssertCallback, result = 200): TestServer {
gets.push([path, callback, result]);
return server;
},
start: async (): Promise<[string, () => void]> => {
const app = express();
for (const [path, callback, result] of gets) {
app.get(path, (req, res) => {
try {
callback(req.headers);
} catch (e) {
error = e;
}
res.status(result).send();
});
}
return new Promise(resolve => {
const server = app.listen(0, () => {
const address = server.address() as AddressInfo;
resolve([
`http://localhost:${address.port}`,
() => {
server.close();
if (error) {
throw error;
}
},
]);
});
});
},
};
return server;
}