aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-object-storage.ts
blob: 19ea7c87c03a80fb8a66d5219b939cd35d92951a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as express from 'express'
import got, { RequestError } from 'got'
import { Server } from 'http'
import { pipeline } from 'stream'
import { randomInt } from '@shared/core-utils'
import { ObjectStorageCommand } from '../server'

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 () {
    if (this.server) this.server.close()
  }
}