]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
0305db28 JB |
2 | import got, { RequestError } from 'got' |
3 | import { Server } from 'http' | |
4 | import { pipeline } from 'stream' | |
5 | import { randomInt } from '@shared/core-utils' | |
6 | import { ObjectStorageCommand } from '../server' | |
70430c27 | 7 | import { terminateServer } from './utils' |
0305db28 JB |
8 | |
9 | export class MockObjectStorage { | |
10 | private server: Server | |
11 | ||
12 | initialize () { | |
13 | return new Promise<number>(res => { | |
14 | const app = express() | |
15 | ||
16 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
17 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}` | |
18 | ||
19 | if (process.env.DEBUG) { | |
20 | console.log('Receiving request on mocked server %s.', req.url) | |
21 | console.log('Proxifying request to %s', url) | |
22 | } | |
23 | ||
24 | return pipeline( | |
25 | got.stream(url, { throwHttpErrors: false }), | |
26 | res, | |
27 | (err: RequestError) => { | |
28 | if (!err) return | |
29 | ||
30 | console.error('Pipeline failed.', err) | |
31 | } | |
32 | ) | |
33 | }) | |
34 | ||
35 | const port = 42301 + randomInt(1, 100) | |
36 | this.server = app.listen(port, () => res(port)) | |
37 | }) | |
38 | } | |
39 | ||
40 | terminate () { | |
70430c27 | 41 | return terminateServer(this.server) |
0305db28 JB |
42 | } |
43 | } |