1 import * as 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'
8 export class MockObjectStorage {
12 return new Promise<number>(res => {
15 app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}`
18 if (process.env.DEBUG) {
19 console.log('Receiving request on mocked server %s.', req.url)
20 console.log('Proxifying request to %s', url)
24 got.stream(url, { throwHttpErrors: false }),
26 (err: RequestError) => {
29 console.error('Pipeline failed.', err)
34 const port = 42301 + randomInt(1, 100)
35 this.server = app.listen(port, () => res(port))
40 if (this.server) this.server.close()