Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit ae7a28f

Browse files
committed
refactor(server): cleanup remaining lint rule violations
1 parent ab98564 commit ae7a28f

File tree

10 files changed

+43
-27
lines changed

10 files changed

+43
-27
lines changed

server/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = {
2323
'@typescript-eslint'
2424
],
2525
rules: {
26-
'@typescript-eslint/no-unnecessary-condition': 'error'
26+
'@typescript-eslint/no-unnecessary-condition': 'error',
27+
'@typescript-eslint/require-await': 'off'
2728
}
2829
}, {
2930
files: ['.eslintrc.js'],

server/challenges/Provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export interface Provider extends EventEmitter {
99
1010
Challenge equality is determined by chall.id.
1111
*/
12-
updateChallenge(chall: Challenge): void;
13-
deleteChallenge(id: string): void;
12+
updateChallenge(chall: Challenge): Promise<void>;
13+
deleteChallenge(id: string): Promise<void>;
1414
cleanup (): void;
1515
}
1616

server/challenges/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path'
33
import { Challenge, CleanedChallenge } from './types'
44
import { Provider, ProviderConstructor } from './Provider'
55
import { challUpdateEmitter, publishChallUpdate } from '../cache/challs'
6+
import { EventEmitter } from 'events'
67

78
let provider: Provider
89

@@ -41,7 +42,8 @@ void import(path.join('../providers', config.challengeProvider.name))
4142
provider.on('update', onUpdate)
4243
})
4344

44-
challUpdateEmitter.on('update', () => {
45+
// FIXME: remove cast once cache is typed
46+
;(challUpdateEmitter as EventEmitter).on('update', () => {
4547
provider.forceUpdate()
4648
})
4749

server/config/server.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,27 @@ export type ServerConfig = {
8080
loginTimeout: number;
8181
}
8282

83-
const fileConfigLoaders: Map<string, (file: string) => unknown> = new Map([
84-
['json', file => JSON.parse(file)],
85-
['yaml', file => yaml.parse(file)],
86-
['yml', file => yaml.parse(file)]
83+
const jsonLoader = (file: string) => JSON.parse(file) as PartialDeep<ServerConfig>
84+
const yamlLoader = (file: string) => yaml.parse(file) as PartialDeep<ServerConfig>
85+
86+
const fileConfigLoaders: Map<string, (file: string) => PartialDeep<ServerConfig>> = new Map([
87+
['json', jsonLoader],
88+
['yaml', yamlLoader],
89+
['yml', yamlLoader]
8790
])
8891

8992
const configPath = process.env.RCTF_CONF_PATH ?? path.join(__dirname, '../../../conf.d')
9093
const fileConfigs: PartialDeep<ServerConfig>[] = []
9194
fs.readdirSync(configPath).sort().forEach((name) => {
92-
const matched = name.match(/\.(.+)$/)
95+
const matched = /\.(.+)$/.exec(name)
9396
if (matched === null) {
9497
return
9598
}
9699
const loader = fileConfigLoaders.get(matched[1])
97100
if (loader === undefined) {
98101
return
99102
}
100-
const config = loader(fs.readFileSync(path.join(configPath, name)).toString()) as PartialDeep<ServerConfig>
103+
const config = loader(fs.readFileSync(path.join(configPath, name)).toString())
101104
fileConfigs.push(config)
102105
})
103106

server/providers/emails/smtp/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class SmtpProvider implements Provider {
1717
this.mailer = nodemailer.createTransport(options.smtpUrl)
1818
}
1919

20-
send (mail: Mail): Promise<void> {
21-
return this.mailer.sendMail(mail)
20+
async send (mail: Mail): Promise<void> {
21+
await this.mailer.sendMail(mail)
2222
}
2323
}

server/providers/uploads/gcs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class GcsProvider implements Provider {
1313

1414
constructor (_options: Partial<GcsProviderOptions>) {
1515
const options: Required<GcsProviderOptions> = {
16-
credentials: _options.credentials || JSON.parse(process.env.RCTF_GCS_CREDENTIALS as string),
16+
credentials: _options.credentials || JSON.parse(process.env.RCTF_GCS_CREDENTIALS as string) as GcsProviderOptions['credentials'],
1717
bucketName: _options.bucketName || process.env.RCTF_GCS_BUCKET as string
1818
}
1919
// TODO: validate that all options are indeed provided

server/providers/uploads/local/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ export default class LocalProvider implements Provider {
3838
this.uploadDirectory = path.resolve(options.uploadDirectory)
3939
this.endpoint = options.endpoint || '/uploads'
4040

41-
this.uploadMap = new Map()
41+
this.uploadMap = new Map<string, Upload>()
4242

43-
app.register(async (fastify) => {
44-
fastify.register(fastifyStatic, {
43+
void app.register(async (fastify) => {
44+
void fastify.register(fastifyStatic, {
4545
root: this.uploadDirectory,
4646
serve: false
4747
})
4848

49+
// Fastify bug #2466
50+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
4951
fastify.setNotFoundHandler(async (req, res) => {
50-
res.status(404)
52+
void res.status(404)
5153
return 'Not found'
5254
})
5355

@@ -70,9 +72,9 @@ export default class LocalProvider implements Provider {
7072

7173
const upload = this.uploadMap.get(key)
7274
if (upload != null) {
73-
reply.header('Cache-Control', 'public, max-age=31557600, immutable')
74-
reply.header('Content-Disposition', contentDisposition(upload.name))
75-
reply.sendFile(path.relative(this.uploadDirectory, upload.filePath))
75+
void reply.header('Cache-Control', 'public, max-age=31557600, immutable')
76+
void reply.header('Content-Disposition', contentDisposition(upload.name))
77+
void reply.sendFile(path.relative(this.uploadDirectory, upload.filePath))
7678
} else {
7779
reply.callNotFound()
7880
}

server/uploads/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config from '../config/server'
22
import path from 'path'
3-
import { Provider } from './types'
3+
import { Provider, ProviderConstructor } from './types'
44
import { FastifyInstance } from 'fastify'
55

66
let provider: Provider | null = null
@@ -10,7 +10,7 @@ export const init = (app: FastifyInstance | null): void => {
1010

1111
// FIXME: use async loading
1212
// eslint-disable-next-line @typescript-eslint/no-var-requires
13-
const ProviderClass = require(path.join('../providers', name)).default
13+
const { default: ProviderClass } = require(path.join('../providers', name)) as { default: ProviderConstructor }
1414

1515
provider = new ProviderClass(config.uploadProvider.options ?? {}, app)
1616
}

server/uploads/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { FastifyInstance } from 'fastify'
2+
13
export interface Provider {
24
// Returns a string, the url of the uploaded file
35
upload (data: Buffer, name: string): Promise<string>;
46

57
// Returns a string, the url of the previously uploaded file. Returns null if the file was not previously uploaded.
68
getUrl (sha256: string, name: string): Promise<string|null>;
79
}
10+
11+
export interface ProviderConstructor {
12+
new (options: unknown, app: FastifyInstance | null): Provider
13+
}

server/util/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export * as restrict from './restrict'
1212
* Perform a deep-copy of a JSON-stringifiable object
1313
*/
1414
export const deepCopy = <T>(data: T): T => {
15-
return JSON.parse(JSON.stringify(data))
15+
return JSON.parse(JSON.stringify(data)) as T
1616
}
1717

1818
export const serveIndex: FastifyPluginAsync<{ indexPath: string; }> = async (fastify, opts) => {
@@ -27,19 +27,21 @@ export const serveIndex: FastifyPluginAsync<{ indexPath: string; }> = async (fas
2727
})
2828

2929
const routeHandler: RouteHandlerMethod = async (req, reply) => {
30-
reply.type('text/html; charset=UTF-8')
31-
reply.send(rendered)
30+
void reply.type('text/html; charset=UTF-8')
31+
void reply.send(rendered)
3232
}
3333

3434
fastify.get('/', routeHandler)
3535
fastify.get('/index.html', async (req, reply) => reply.redirect(301, '/'))
3636
fastify.get('//*', async (req, reply) => reply.redirect(302, '/'))
37+
// Fastify bug #2466
38+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
3739
fastify.setNotFoundHandler(routeHandler)
3840
}
3941

4042
// Parse Cloudflare CF-Connecting-IP header
4143
export const getRealIp = (req: FastifyRequest): string => {
4244
// Use `get` on req.__proto__ since getRealIp is used in req's getter
43-
return req.headers['cf-connecting-ip'] ||
44-
Reflect.get(Object.getPrototypeOf(req), 'ip', req)
45+
return req.headers['cf-connecting-ip'] as string ||
46+
Reflect.get(Object.getPrototypeOf(req), 'ip', req) as string
4547
}

0 commit comments

Comments
 (0)