diff options
author | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
commit | d1bfbdeb203b0e4f37e9468861c690171156ee29 (patch) | |
tree | 2a6051ef212b964eb2ded1021a625acd0ba32053 /shared/extra-utils/mock-servers/mock-object-storage.ts | |
parent | 5480933b7f088bf099b25fb467faace814f0da58 (diff) | |
download | PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.gz PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.zst PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.zip |
Random listen for mocked servers
Diffstat (limited to 'shared/extra-utils/mock-servers/mock-object-storage.ts')
-rw-r--r-- | shared/extra-utils/mock-servers/mock-object-storage.ts | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/shared/extra-utils/mock-servers/mock-object-storage.ts b/shared/extra-utils/mock-servers/mock-object-storage.ts index 144f2819d..d135c2631 100644 --- a/shared/extra-utils/mock-servers/mock-object-storage.ts +++ b/shared/extra-utils/mock-servers/mock-object-storage.ts | |||
@@ -2,39 +2,37 @@ import express from 'express' | |||
2 | import got, { RequestError } from 'got' | 2 | import got, { RequestError } from 'got' |
3 | import { Server } from 'http' | 3 | import { Server } from 'http' |
4 | import { pipeline } from 'stream' | 4 | import { pipeline } from 'stream' |
5 | import { randomInt } from '@shared/core-utils' | ||
6 | import { ObjectStorageCommand } from '../server' | 5 | import { ObjectStorageCommand } from '../server' |
7 | import { terminateServer } from './utils' | 6 | import { getPort, randomListen, terminateServer } from './utils' |
8 | 7 | ||
9 | export class MockObjectStorage { | 8 | export class MockObjectStorage { |
10 | private server: Server | 9 | private server: Server |
11 | 10 | ||
12 | initialize () { | 11 | async initialize () { |
13 | return new Promise<number>(res => { | 12 | const app = express() |
14 | const app = express() | ||
15 | 13 | ||
16 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 14 | 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}` | 15 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}` |
18 | 16 | ||
19 | if (process.env.DEBUG) { | 17 | if (process.env.DEBUG) { |
20 | console.log('Receiving request on mocked server %s.', req.url) | 18 | console.log('Receiving request on mocked server %s.', req.url) |
21 | console.log('Proxifying request to %s', url) | 19 | console.log('Proxifying request to %s', url) |
22 | } | 20 | } |
23 | |||
24 | return pipeline( | ||
25 | got.stream(url, { throwHttpErrors: false }), | ||
26 | res, | ||
27 | (err: RequestError) => { | ||
28 | if (!err) return | ||
29 | 21 | ||
30 | console.error('Pipeline failed.', err) | 22 | return pipeline( |
31 | } | 23 | got.stream(url, { throwHttpErrors: false }), |
32 | ) | 24 | res, |
33 | }) | 25 | (err: RequestError) => { |
26 | if (!err) return | ||
34 | 27 | ||
35 | const port = 44000 + randomInt(1, 1000) | 28 | console.error('Pipeline failed.', err) |
36 | this.server = app.listen(port, () => res(port)) | 29 | } |
30 | ) | ||
37 | }) | 31 | }) |
32 | |||
33 | this.server = await randomListen(app) | ||
34 | |||
35 | return getPort(this.server) | ||
38 | } | 36 | } |
39 | 37 | ||
40 | terminate () { | 38 | terminate () { |