aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-object-storage.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-10-22 14:31:38 +0200
committerChocobozzz <me@florianbigard.com>2021-10-22 14:31:38 +0200
commitd1bfbdeb203b0e4f37e9468861c690171156ee29 (patch)
tree2a6051ef212b964eb2ded1021a625acd0ba32053 /shared/extra-utils/mock-servers/mock-object-storage.ts
parent5480933b7f088bf099b25fb467faace814f0da58 (diff)
downloadPeerTube-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.ts44
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'
2import got, { RequestError } from 'got' 2import got, { RequestError } from 'got'
3import { Server } from 'http' 3import { Server } from 'http'
4import { pipeline } from 'stream' 4import { pipeline } from 'stream'
5import { randomInt } from '@shared/core-utils'
6import { ObjectStorageCommand } from '../server' 5import { ObjectStorageCommand } from '../server'
7import { terminateServer } from './utils' 6import { getPort, randomListen, terminateServer } from './utils'
8 7
9export class MockObjectStorage { 8export 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 () {