1 import express from 'express'
2 import { Server } from 'http'
3 import { randomInt } from '@shared/core-utils'
4 import { terminateServer } from './utils'
6 export class MockInstancesIndex {
9 private readonly indexInstances: { host: string, createdAt: string }[] = []
12 return new Promise<number>(res => {
15 app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
21 app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
22 const since = req.query.since
24 const filtered = this.indexInstances.filter(i => {
25 if (!since) return true
27 return i.createdAt > since
31 total: filtered.length,
36 const port = 42000 + randomInt(1, 1000)
37 this.server = app.listen(port, () => res(port))
41 addInstance (host: string) {
42 this.indexInstances.push({ host, createdAt: new Date().toISOString() })
46 return terminateServer(this.server)