1 import express from 'express'
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'
7 import { terminateServer } from './utils'
9 export class MockObjectStorage {
10 private server: Server
13 return new Promise<number>(res => {
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}`
19 if (process.env.DEBUG) {
20 console.log('Receiving request on mocked server %s.', req.url)
21 console.log('Proxifying request to %s', url)
25 got.stream(url, { throwHttpErrors: false }),
27 (err: RequestError) => {
30 console.error('Pipeline failed.', err)
35 const port = 44000 + randomInt(1, 1000)
36 this.server = app.listen(port, () => res(port))
41 return terminateServer(this.server)