Matthieu Sieben 61dc0d60e1
Add linting rule to sort imports (#3220)
* Add linting rule to sort imports

* remove spacing between import groups

* changeset

* changeset

* prettier config fine tuning

* forbid use of deprecated imports

* tidy
2025-02-05 15:06:58 +01:00

56 lines
1.4 KiB
TypeScript

import { once } from 'node:events'
import * as http from 'node:http'
import express from 'express'
import * as xrpc from '../src'
import { AuthRequiredError } from '../src'
export async function createServer({
router,
}: xrpc.Server): Promise<http.Server> {
const app = express()
app.use(router)
const httpServer = app.listen(0)
await once(httpServer, 'listening')
return httpServer
}
export async function closeServer(httpServer: http.Server) {
await new Promise((r) => {
httpServer.close(() => r(undefined))
})
}
export function createBasicAuth(allowed: {
username: string
password: string
}) {
return function (ctx: { req: http.IncomingMessage }) {
const header = ctx.req.headers.authorization ?? ''
if (!header.startsWith('Basic ')) {
throw new AuthRequiredError()
}
const original = header.replace('Basic ', '')
const [username, password] = Buffer.from(original, 'base64')
.toString()
.split(':')
if (username !== allowed.username || password !== allowed.password) {
throw new AuthRequiredError()
}
return {
credentials: { username },
artifacts: { original },
}
}
}
export function basicAuthHeaders(creds: {
username: string
password: string
}) {
return {
authorization:
'Basic ' +
Buffer.from(`${creds.username}:${creds.password}`).toString('base64'),
}
}