2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2021-08-17 01:26:20 -05:00
|
|
|
import got, { RequestError } from 'got'
|
|
|
|
import { Server } from 'http'
|
|
|
|
import { pipeline } from 'stream'
|
|
|
|
import { randomInt } from '@shared/core-utils'
|
|
|
|
import { ObjectStorageCommand } from '../server'
|
2021-09-06 01:13:11 -05:00
|
|
|
import { terminateServer } from './utils'
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
export class MockObjectStorage {
|
|
|
|
private server: Server
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
return new Promise<number>(res => {
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}`
|
|
|
|
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
console.log('Receiving request on mocked server %s.', req.url)
|
|
|
|
console.log('Proxifying request to %s', url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipeline(
|
|
|
|
got.stream(url, { throwHttpErrors: false }),
|
|
|
|
res,
|
|
|
|
(err: RequestError) => {
|
|
|
|
if (!err) return
|
|
|
|
|
|
|
|
console.error('Pipeline failed.', err)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
const port = 42301 + randomInt(1, 100)
|
|
|
|
this.server = app.listen(port, () => res(port))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate () {
|
2021-09-06 01:13:11 -05:00
|
|
|
return terminateServer(this.server)
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
}
|