はじまりの大地
このコミットが含まれているのは:
@@ -0,0 +1,8 @@
|
||||
export * from './mock-429.js'
|
||||
export * from './mock-email.js'
|
||||
export * from './mock-http.js'
|
||||
export * from './mock-instances-index.js'
|
||||
export * from './mock-joinpeertube-versions.js'
|
||||
export * from './mock-object-storage.js'
|
||||
export * from './mock-plugin-blocklist.js'
|
||||
export * from './mock-proxy.js'
|
||||
@@ -0,0 +1,33 @@
|
||||
import express from 'express'
|
||||
import { Server } from 'http'
|
||||
import { getPort, randomListen, terminateServer } from './shared.js'
|
||||
|
||||
export class Mock429 {
|
||||
private server: Server
|
||||
private responseSent = false
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.get('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
|
||||
if (!this.responseSent) {
|
||||
this.responseSent = true
|
||||
|
||||
// Retry after 5 seconds
|
||||
res.header('retry-after', '2')
|
||||
return res.sendStatus(429)
|
||||
}
|
||||
|
||||
return res.sendStatus(200)
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import MailDev from '@peertube/maildev'
|
||||
import { randomInt } from '@peertube/peertube-core-utils'
|
||||
import { parallelTests } from '@peertube/peertube-node-utils'
|
||||
|
||||
class MockSmtpServer {
|
||||
|
||||
private static instance: MockSmtpServer
|
||||
private started = false
|
||||
private maildev: any
|
||||
private emails: object[]
|
||||
|
||||
private constructor () { }
|
||||
|
||||
collectEmails (emailsCollection: object[]) {
|
||||
return new Promise<number>((res, rej) => {
|
||||
const port = parallelTests() ? randomInt(1025, 2000) : 1025
|
||||
this.emails = emailsCollection
|
||||
|
||||
if (this.started) {
|
||||
return res(undefined)
|
||||
}
|
||||
|
||||
this.maildev = new MailDev({
|
||||
ip: '127.0.0.1',
|
||||
smtp: port,
|
||||
disableWeb: true,
|
||||
silent: true
|
||||
})
|
||||
|
||||
this.maildev.on('new', email => {
|
||||
this.emails.push(email)
|
||||
})
|
||||
|
||||
this.maildev.listen(err => {
|
||||
if (err) return rej(err)
|
||||
|
||||
this.started = true
|
||||
|
||||
return res(port)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
kill () {
|
||||
if (!this.maildev) return
|
||||
|
||||
this.maildev.close()
|
||||
|
||||
this.maildev = null
|
||||
MockSmtpServer.instance = null
|
||||
}
|
||||
|
||||
static get Instance () {
|
||||
return this.instance || (this.instance = new this())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
MockSmtpServer
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import express from 'express'
|
||||
import { Server } from 'http'
|
||||
import { getPort, randomListen, terminateServer } from './shared.js'
|
||||
|
||||
export class MockHTTP {
|
||||
private server: Server
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.get('/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
return res.sendStatus(200)
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import express from 'express'
|
||||
import { Server } from 'http'
|
||||
import { getPort, randomListen, terminateServer } from './shared.js'
|
||||
|
||||
export class MockInstancesIndex {
|
||||
private server: Server
|
||||
|
||||
private readonly indexInstances: { host: string, createdAt: string }[] = []
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
|
||||
|
||||
return next()
|
||||
})
|
||||
|
||||
app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
|
||||
const since = req.query.since
|
||||
|
||||
const filtered = this.indexInstances.filter(i => {
|
||||
if (!since) return true
|
||||
|
||||
return i.createdAt > since
|
||||
})
|
||||
|
||||
return res.json({
|
||||
total: filtered.length,
|
||||
data: filtered
|
||||
})
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
addInstance (host: string) {
|
||||
this.indexInstances.push({ host, createdAt: new Date().toISOString() })
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import express from 'express'
|
||||
import { Server } from 'http'
|
||||
import { getPort, randomListen } from './shared.js'
|
||||
|
||||
export class MockJoinPeerTubeVersions {
|
||||
private server: Server
|
||||
private latestVersion: string
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
|
||||
|
||||
return next()
|
||||
})
|
||||
|
||||
app.get('/versions.json', (req: express.Request, res: express.Response) => {
|
||||
return res.json({
|
||||
peertube: {
|
||||
latestVersion: this.latestVersion
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
setLatestVersion (latestVersion: string) {
|
||||
this.latestVersion = latestVersion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import express from 'express'
|
||||
import got, { RequestError } from 'got'
|
||||
import { Server } from 'http'
|
||||
import { pipeline } from 'stream'
|
||||
import { ObjectStorageCommand } from '@peertube/peertube-server-commands'
|
||||
import { getPort, randomListen, terminateServer } from './shared.js'
|
||||
|
||||
export class MockObjectStorageProxy {
|
||||
private server: Server
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getMockEndpointHost()}/${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)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import express, { Request, Response } from 'express'
|
||||
import { Server } from 'http'
|
||||
import { getPort, randomListen, terminateServer } from './shared.js'
|
||||
|
||||
type BlocklistResponse = {
|
||||
data: {
|
||||
value: string
|
||||
action?: 'add' | 'remove'
|
||||
updatedAt?: string
|
||||
}[]
|
||||
}
|
||||
|
||||
export class MockBlocklist {
|
||||
private body: BlocklistResponse
|
||||
private server: Server
|
||||
|
||||
async initialize () {
|
||||
const app = express()
|
||||
|
||||
app.get('/blocklist', (req: Request, res: Response) => {
|
||||
return res.json(this.body)
|
||||
})
|
||||
|
||||
this.server = await randomListen(app)
|
||||
|
||||
return getPort(this.server)
|
||||
}
|
||||
|
||||
replace (body: BlocklistResponse) {
|
||||
this.body = body
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { createServer, Server } from 'http'
|
||||
import { createProxy } from 'proxy'
|
||||
import { getPort, terminateServer } from './shared.js'
|
||||
|
||||
class MockProxy {
|
||||
private server: Server
|
||||
|
||||
initialize () {
|
||||
return new Promise<number>(res => {
|
||||
this.server = createProxy(createServer())
|
||||
this.server.listen(0, () => res(getPort(this.server)))
|
||||
})
|
||||
}
|
||||
|
||||
terminate () {
|
||||
return terminateServer(this.server)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
MockProxy
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Express } from 'express'
|
||||
import { Server } from 'http'
|
||||
import { AddressInfo } from 'net'
|
||||
|
||||
function randomListen (app: Express) {
|
||||
return new Promise<Server>(res => {
|
||||
const server = app.listen(0, () => res(server))
|
||||
})
|
||||
}
|
||||
|
||||
function getPort (server: Server) {
|
||||
const address = server.address() as AddressInfo
|
||||
|
||||
return address.port
|
||||
}
|
||||
|
||||
function terminateServer (server: Server) {
|
||||
if (!server) return Promise.resolve()
|
||||
|
||||
return new Promise<void>((res, rej) => {
|
||||
server.close(err => {
|
||||
if (err) return rej(err)
|
||||
|
||||
return res()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export {
|
||||
randomListen,
|
||||
getPort,
|
||||
terminateServer
|
||||
}
|
||||
新しい課題から参照
ユーザをブロックする