]>
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' | |
c55e3d72 C |
5 | import { ObjectStorageCommand } from '@shared/server-commands' |
6 | import { getPort, randomListen, terminateServer } from './shared' | |
0305db28 | 7 | |
8059e050 | 8 | export class MockObjectStorageProxy { |
0305db28 JB |
9 | private server: Server |
10 | ||
d1bfbdeb C |
11 | async initialize () { |
12 | const app = express() | |
0305db28 | 13 | |
d1bfbdeb | 14 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
9ab330b9 | 15 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getMockEndpointHost()}/${req.params.path}` |
0305db28 | 16 | |
d1bfbdeb C |
17 | if (process.env.DEBUG) { |
18 | console.log('Receiving request on mocked server %s.', req.url) | |
19 | console.log('Proxifying request to %s', url) | |
20 | } | |
0305db28 | 21 | |
d1bfbdeb C |
22 | return pipeline( |
23 | got.stream(url, { throwHttpErrors: false }), | |
24 | res, | |
25 | (err: RequestError) => { | |
26 | if (!err) return | |
0305db28 | 27 | |
d1bfbdeb C |
28 | console.error('Pipeline failed.', err) |
29 | } | |
30 | ) | |
0305db28 | 31 | }) |
d1bfbdeb C |
32 | |
33 | this.server = await randomListen(app) | |
34 | ||
35 | return getPort(this.server) | |
0305db28 JB |
36 | } |
37 | ||
38 | terminate () { | |
70430c27 | 39 | return terminateServer(this.server) |
0305db28 JB |
40 | } |
41 | } |