1 import express from 'express'
2 import got, { RequestError } from 'got'
3 import { Server } from 'http'
4 import { pipeline } from 'stream'
5 import { ObjectStorageCommand } from '@shared/server-commands'
6 import { getPort, randomListen, terminateServer } from './shared'
8 export class MockObjectStorageProxy {
14 app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getMockEndpointHost()}/${req.params.path}`
17 if (process.env.DEBUG) {
18 console.log('Receiving request on mocked server %s.', req.url)
19 console.log('Proxifying request to %s', url)
23 got.stream(url, { throwHttpErrors: false }),
25 (err: RequestError) => {
28 console.error('Pipeline failed.', err)
33 this.server = await randomListen(app)
35 return getPort(this.server)
39 return terminateServer(this.server)