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

Commit 31aa2c2

Browse files
committed
Merge branch 'master' into feat/conf-d
2 parents 55a8f7a + 96b64ef commit 31aa2c2

File tree

16 files changed

+68
-42
lines changed

16 files changed

+68
-42
lines changed

docs/content/providers/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
## Providers
1+
# Providers
22

33
The provider system allows for flexible configuration of backends. Each provider provides a different option for how certain functionality should be implemented. As opposed to a generic plugin system, this allows for more defined customizations.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"lint": "sh -c \"tsc --noEmit ; eslint .\"",
88
"lint:strict": "sh -c \"tsc --noEmit -p tsconfig.strict.json ; eslint .\"",
9-
"start": "node --enable-source-maps dist/server/index.js",
10-
"migrate": "sh -c \"yarn build:ts && RCTF_DATABASE_MIGRATE=only yarn start\"",
9+
"start": "node --enable-source-maps --unhandled-rejections=strict dist/server/index.js",
10+
"migrate": "yarn build:ts && cross-env RCTF_DATABASE_MIGRATE=only yarn start",
1111
"build:client": "preact build --src client/src --template client/index.html --dest dist/build --no-prerender --no-inline-css",
1212
"build:ts": "tsc && yarn copy-static",
1313
"build": "yarn build:ts && yarn build:client",

server/challenges/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const onUpdate = (newChallenges: Challenge[]): void => {
3535
}
3636

3737
void import(path.join('../providers', config.challengeProvider.name))
38-
.then(({ default: Provider }: { default: ProviderConstructor }) => {
38+
.then(({ default: Provider }: { default: ProviderConstructor }): void => {
3939
provider = new Provider(config.challengeProvider.options ?? {})
4040

4141
provider.on('update', onUpdate)
@@ -53,11 +53,11 @@ export function getCleanedChallenges (): CleanedChallenge[] {
5353
return cleanedChallenges
5454
}
5555

56-
export function getChallenge (id: string): Challenge {
56+
export function getChallenge (id: string): Challenge | undefined {
5757
return challengesMap.get(id)
5858
}
5959

60-
export function getCleanedChallenge (id: string): CleanedChallenge {
60+
export function getCleanedChallenge (id: string): CleanedChallenge | undefined {
6161
return cleanedChallengesMap.get(id)
6262
}
6363

server/database/challenges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const getAllChallenges = (): Promise<DatabaseChallenge[]> => {
1111
.then(res => res.rows)
1212
}
1313

14-
export const getChallengeById = ({ id }: Pick<DatabaseChallenge, 'id'>): Promise<DatabaseChallenge> => {
14+
export const getChallengeById = ({ id }: Pick<DatabaseChallenge, 'id'>): Promise<DatabaseChallenge | undefined> => {
1515
return db.query('SELECT * FROM challenges WHERE id = $1', [id])
1616
.then(res => res.rows[0])
1717
}
@@ -23,7 +23,7 @@ export const createChallenge = ({ id, data }: DatabaseChallenge): Promise<Databa
2323
.then(res => res.rows[0])
2424
}
2525

26-
export const removeChallengeById = ({ id }: Pick<DatabaseChallenge, 'id'>): Promise<DatabaseChallenge> => {
26+
export const removeChallengeById = ({ id }: Pick<DatabaseChallenge, 'id'>): Promise<DatabaseChallenge | undefined> => {
2727
return db.query('DELETE FROM challenges WHERE id = $1 RETURNING *', [id])
2828
.then(res => res.rows[0])
2929
}
File renamed without changes.

server/database/members.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const makeMember = ({ id, userid, email }: UserMember): Promise<UserMembe
1818
.then(res => res.rows[0])
1919
}
2020

21-
export const removeMember = ({ id, userid }: Pick<UserMember, 'id' | 'userid'>): Promise<UserMember> => {
21+
export const removeMember = ({ id, userid }: Pick<UserMember, 'id' | 'userid'>): Promise<UserMember | undefined> => {
2222
return db.query('DELETE FROM user_members WHERE id = $1 and userid = $2 RETURNING *', [id, userid])
2323
.then(res => res.rows[0])
2424
}

server/database/migrate.js renamed to server/database/migrate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import path from 'path'
22
import pgMigrate from 'node-pg-migrate'
33
import config from '../config/server'
44

5-
const sleep = (time) => new Promise(resolve => setTimeout(resolve, time))
5+
const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time))
66

7-
const migrate = async (attempt) => {
7+
const migrate = async (attempt: number): Promise<void> => {
88
try {
99
await pgMigrate({
1010
databaseUrl: config.database.sql,
1111
dir: path.join(__dirname, '../../migrations'),
1212
direction: 'up',
1313
migrationsTable: 'pgmigrations',
14-
verbose: true
14+
verbose: true,
15+
count: Infinity
1516
})
1617
} catch (e) {
1718
if (attempt > 10) {
@@ -23,4 +24,4 @@ const migrate = async (attempt) => {
2324
}
2425
}
2526

26-
export default () => migrate(0)
27+
export default (): Promise<void> => migrate(0)

server/database/solves.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const getSolveByUserIdAndChallId = ({ userid, challengeid }: Pick<Solve,
2929
.then(res => res.rows[0])
3030
}
3131

32-
export const newSolve = ({ id, userid, challengeid, createdat }: Solve): Promise<Solve | undefined> => {
32+
export const newSolve = ({ id, userid, challengeid, createdat }: Solve): Promise<Solve> => {
3333
return db.query('INSERT INTO solves (id, challengeid, userid, createdat) VALUES ($1, $2, $3, $4) RETURNING *', [id, challengeid, userid, createdat])
3434
.then(res => res.rows[0])
3535
}

server/database/users.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ export const getAllUsers = (): Promise<Pick<User, 'id' | 'name' | 'division'>[]>
1717
.then(res => res.rows)
1818
}
1919

20-
export const getUserById = ({ id }: Pick<User, 'id'>): Promise<User> => {
20+
export const getUserById = ({ id }: Pick<User, 'id'>): Promise<User | undefined> => {
2121
return db.query('SELECT * FROM users WHERE id = $1', [id])
2222
.then(res => res.rows[0])
2323
}
2424

25-
export const getUserByEmail = ({ email }: Pick<User, 'email'>): Promise<User> => {
25+
export const getUserByEmail = ({ email }: Pick<User, 'email'>): Promise<User | undefined> => {
2626
return db.query('SELECT * FROM users WHERE email = $1', [email])
2727
.then(res => res.rows[0])
2828
}
2929

30-
export const getUserByCtftimeId = ({ ctftimeId }: Pick<User, 'ctftimeId'>): Promise<User> => {
30+
export const getUserByCtftimeId = ({ ctftimeId }: Pick<User, 'ctftimeId'>): Promise<User | undefined> => {
3131
return db.query('SELECT * FROM users WHERE ctftime_id = $1', [ctftimeId])
3232
.then(res => res.rows[0])
3333
}
3434

35-
export const getUserByIdAndEmail = ({ id, email }: Pick<User, 'id' | 'email'>): Promise<User> => {
35+
export const getUserByIdAndEmail = ({ id, email }: Pick<User, 'id' | 'email'>): Promise<User | undefined> => {
3636
return db.query('SELECT * FROM users WHERE id = $1 AND email = $2', [id, email])
3737
.then(res => res.rows[0])
3838
}
3939

40-
export const getUserByNameOrEmail = ({ name, email }: Pick<User, 'name' | 'email'>): Promise<User> => {
40+
export const getUserByNameOrEmail = ({ name, email }: Pick<User, 'name' | 'email'>): Promise<User | undefined> => {
4141
return db.query('SELECT * FROM users WHERE name = $1 OR email = $2', [name, email])
4242
.then(res => res.rows[0])
4343
}
4444

45-
export const getUserByNameOrCtftimeId = ({ name, ctftimeId }: Pick<User, 'name' | 'ctftimeId'>): Promise<User> => {
45+
export const getUserByNameOrCtftimeId = ({ name, ctftimeId }: Pick<User, 'name' | 'ctftimeId'>): Promise<User | undefined> => {
4646
return db.query('SELECT * FROM users WHERE name = $1 OR ctftime_id = $2', [name, ctftimeId])
4747
.then(res => res.rows[0])
4848
}
4949

50-
export const removeUserByEmail = ({ email }: Pick<User, 'email'>): Promise<User> => {
50+
export const removeUserByEmail = ({ email }: Pick<User, 'email'>): Promise<User | undefined> => {
5151
return db.query('DELETE FROM users WHERE email = $1 RETURNING *', [email])
5252
.then(res => res.rows[0])
5353
}
5454

55-
export const removeUserById = ({ id }: Pick<User, 'id'>): Promise<User> => {
55+
export const removeUserById = ({ id }: Pick<User, 'id'>): Promise<User | undefined> => {
5656
return db.query('DELETE FROM users WHERE id = $1 RETURNING *', [id])
5757
.then(res => res.rows[0])
5858
}
@@ -67,20 +67,24 @@ export const makeUser = ({ id, name, email, division, ctftimeId, perms }: User):
6767
.then(res => res.rows[0])
6868
}
6969

70-
export const removeCtftimeId = ({ id }: Pick<User, 'id'>): Promise<User> => {
70+
export const removeCtftimeId = ({ id }: Pick<User, 'id'>): Promise<User | undefined> => {
7171
return db.query('UPDATE users SET ctftime_id = NULL WHERE id = $1 AND ctftime_id IS NOT NULL RETURNING *', [id])
7272
.then(res => res.rows[0])
7373
}
7474

75-
export const removeEmail = ({ id }: Pick<User, 'id'>): Promise<User> => {
75+
export const removeEmail = ({ id }: Pick<User, 'id'>): Promise<User | undefined> => {
7676
return db.query('UPDATE users SET email = NULL WHERE id = $1 AND email IS NOT NULL RETURNING *', [id])
7777
.then(res => res.rows[0])
7878
}
7979

80-
export const updateUser = async ({ id, name, email, division, ctftimeId, perms }: Partial<User>): Promise<User> => {
80+
export const updateUser = async ({ id, name, email, division, ctftimeId, perms }: Pick<User, 'id'> & Partial<User>): Promise<User | undefined> => {
8181
if (config.email && config.divisionACLs) {
8282
if (!email || !division) {
8383
const user = await getUserById({ id })
84+
if (user === undefined) {
85+
// User does not exist, bail
86+
return undefined
87+
}
8488
email = email || user.email
8589
division = division || user.division
8690
}

server/providers/challenges/database/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DatabaseProvider extends EventEmitter implements Provider {
1717

1818
private async update (): Promise<void> {
1919
try {
20-
const dbchallenges = await db.challenges.getAllChallenges() as DatabaseChallenge[]
20+
const dbchallenges = await db.challenges.getAllChallenges()
2121

2222
this.challenges = dbchallenges.map(({ id, data }) => {
2323
return {
@@ -52,7 +52,7 @@ class DatabaseProvider extends EventEmitter implements Provider {
5252
async updateChallenge (chall: Challenge): Promise<void> {
5353
const originalData = await db.challenges.getChallengeById({
5454
id: chall.id
55-
}) as DatabaseChallenge
55+
})
5656

5757
// If we're inserting, have sane defaults
5858
if (originalData === undefined) {

0 commit comments

Comments
 (0)