]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/mock-servers/mock-object-storage.ts
Try to fix mock server ports
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-object-storage.ts
CommitLineData
41fb13c3 1import express from 'express'
0305db28
JB
2import got, { RequestError } from 'got'
3import { Server } from 'http'
4import { pipeline } from 'stream'
5import { randomInt } from '@shared/core-utils'
6import { ObjectStorageCommand } from '../server'
70430c27 7import { terminateServer } from './utils'
0305db28
JB
8
9export 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
3c25d37a 35 const port = 44000 + randomInt(1, 1000)
0305db28
JB
36 this.server = app.listen(port, () => res(port))
37 })
38 }
39
40 terminate () {
70430c27 41 return terminateServer(this.server)
0305db28
JB
42 }
43}