Skip to content

Commit

Permalink
fix(api-server): nanoid v5
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhongqiao committed Jan 25, 2024
1 parent 0eb9184 commit 8edbdfb
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 38 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
"packageManager": "yarn@3.3.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/api-server/src/services/contest.services.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MongoServerError, contestCollection, contestProblemCollection, contestProblemListCollection, contestSeriesCollection, domainProblemCollection, mongoClient, ranklistRedis, recalculateTeamTotalScore, teamScoreCollection } from '@argoncs/common'
import { type NewContestSeries, type ConetstProblemList, type Contest, type ContestProblem, type NewContest, type TeamScore, type ContestSeries } from '@argoncs/types'
import { ConflictError, MethodNotAllowedError, NotFoundError } from 'http-errors-enhanced'
import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'
import { CONTEST_CACHE_KEY, CONTEST_PATH_CACHE_KEY, PROBLEMLIST_CACHE_KEY, deleteCache, fetchCacheUntilLockAcquired, releaseLock, setCache } from './cache.services.js'

export async function createContest ({ newContest, domainId }: { newContest: NewContest, domainId: string }): Promise<{ contestId: string }> {
const id = await nanoid()
const id = nanoid()
const contest: Contest = { ...newContest, domainId, id, published: false }
const session = mongoClient.startSession()
try {
Expand All @@ -24,7 +24,7 @@ export async function createContest ({ newContest, domainId }: { newContest: New
}

export async function createContestSeries ({ newContestSeries, domainId }: { newContestSeries: NewContestSeries, domainId: string }): Promise<{ seriesId: string }> {
const id = await nanoid()
const id = nanoid()
await contestSeriesCollection.insertOne({ ...newContestSeries, contests: [], id, domainId })
return { seriesId: id }
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api-server/src/services/domain.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { type NewDomain, type Domain, type DomainMembers } from '@argoncs/types'
import { mongoClient, domainCollection, userCollection } from '@argoncs/common'
import { NotFoundError } from 'http-errors-enhanced'

import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'
import { USER_CACHE_KEY, deleteCache } from './cache.services.js'

export async function createDomain ({ newDomain }: { newDomain: NewDomain }): Promise<{ domainId: string }> {
const domainId = await nanoid()
const domainId = nanoid()
const domain: Domain = { ...newDomain, id: domainId, members: [] }
await domainCollection.insertOne(domain)
return { domainId }
Expand Down
4 changes: 2 additions & 2 deletions packages/api-server/src/services/problem.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { NotFoundError } from 'http-errors-enhanced'
import { mongoClient, domainProblemCollection, submissionCollection, testcaseUploadCollection } from '@argoncs/common'
import { testcaseExists } from './testcase.services.js'

import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'

export async function createDomainProblem ({ newProblem, domainId }: { newProblem: NewProblem, domainId: string }): Promise<{ problemId: string }> {
const problemId = await nanoid()
const problemId = nanoid()
const problem: Problem = { ...newProblem, id: problemId, domainId }
await domainProblemCollection.insertOne(problem)
return { problemId }
Expand Down
6 changes: 3 additions & 3 deletions packages/api-server/src/services/session.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { pbkdf2 } from 'node:crypto'

import { promisify } from 'node:util'

import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'

import { SESSION_CACHE_KEY, deleteCache, fetchCacheUntilLockAcquired, releaseLock, setCache } from './cache.services.js'

Expand All @@ -20,8 +20,8 @@ export async function authenticateUser ({ usernameOrEmail, password, loginIP, us

const hash = (await pbkdf2Async(password, user.credential.salt, 100000, 512, 'sha512')).toString('base64')
if (hash === user.credential.hash) {
const sessionId = await nanoid()
const token = await nanoid()
const sessionId = nanoid()
const token = nanoid(32)
await sessionCollection.insertOne({ id: sessionId, token, userId, userAgent, loginIP })
return { userId, sessionId, token }
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-server/src/services/submission.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { rabbitMQ, judgerExchange, judgerTasksKey, submissionCollection, fetchDomainProblem, fetchContestProblem } from '@argoncs/common'
import { languageConfigs } from '../../configs/language.configs.js'

import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'
import { MethodNotAllowedError } from 'http-errors-enhanced'

async function createSubmission ({ submission, userId, target }: { submission: NewSubmission, userId: string, target: { problemId: string, domainId: string } | { problemId: string, contestId: string, teamId?: string } }): Promise<{ submissionId: string }> {
Expand All @@ -25,7 +25,7 @@ async function createSubmission ({ submission, userId, target }: { submission: N
throw new MethodNotAllowedError('Problem does not have testcases uploaded')
}

const submissionId = await nanoid()
const submissionId = nanoid()
const pendingSubmission: Submission = {
...submission,
id: submissionId,
Expand Down
6 changes: 3 additions & 3 deletions packages/api-server/src/services/team.services.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { mongoClient, submissionCollection, teamCollection, teamInvitationCollection, teamScoreCollection, userCollection } from '@argoncs/common'
import { type TeamInvitation, type NewTeam, type Team, type TeamMembers } from '@argoncs/types'
import { ConflictError, MethodNotAllowedError, NotFoundError } from 'http-errors-enhanced'
import { nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'
import gravatarUrl from 'gravatar-url'

export async function createTeam ({ newTeam, contestId, userId }: { newTeam: NewTeam, contestId: string, userId: string }): Promise<{ teamId: string }> {
const id = await nanoid()
const id = nanoid()
const team: Team = {
...newTeam,
id,
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function fetchTeamMembers ({ teamId, contestId }: { teamId: string,
}

export async function createTeamInvitation ({ teamId, contestId, userId }: { teamId: string, contestId: string, userId: string }): Promise<{ invitationId: string }> {
const id = await nanoid()
const id = nanoid()
const user = await userCollection.findOne({ id: userId })
if (user == null) {
throw new NotFoundError('User not found')
Expand Down
4 changes: 2 additions & 2 deletions packages/api-server/src/services/testcase.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NotFoundError } from 'http-errors-enhanced'
import { fetchDomainProblem, minio, testcaseUploadCollection } from '@argoncs/common'

import path = require('node:path')
import { longNanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'

export async function testcaseExists ({ problemId, domainId, filename, versionId }: { problemId: string, domainId: string, filename: string, versionId: string }): Promise<void> {
const objectName = path.join(domainId, problemId, filename)
Expand All @@ -18,7 +18,7 @@ export async function testcaseExists ({ problemId, domainId, filename, versionId
}

export async function createUploadSession ({ problemId, domainId }: { problemId: string, domainId: string }): Promise<{ uploadId: string }> {
const id = await longNanoid()
const id = nanoid(32)
await fetchDomainProblem({ problemId, domainId }) // Could throw not found
await testcaseUploadCollection.insertOne({ id, problemId, domainId, createdAt: (new Date()).getTime() })
return { uploadId: id }
Expand Down
6 changes: 3 additions & 3 deletions packages/api-server/src/services/user.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { randomBytes, pbkdf2 } from 'node:crypto'

import { promisify } from 'node:util'

import { longNanoid, nanoid } from '../utils/nanoid.utils.js'
import { nanoid } from 'nanoid'

import { sendEmail } from './emails.services.js'
import { USER_CACHE_KEY, USER_PATH_CACHE_KEY, deleteCache, fetchCacheUntilLockAcquired, releaseLock, setCache } from './cache.services.js'
Expand All @@ -15,7 +15,7 @@ const pbkdf2Async = promisify(pbkdf2)
export async function registerUser ({ newUser }: { newUser: NewUser }): Promise<{ userId: string, email: string }> {
const salt = (await randomBytesAsync(32)).toString('base64')
const hash = (await pbkdf2Async(newUser.password, salt, 100000, 512, 'sha512')).toString('base64')
const userId = await nanoid()
const userId = nanoid()
const user: User = {
id: userId,
name: newUser.name,
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function initiateVerification ({ userId }: { userId: string }): Pro
throw new NotFoundError('User does not have an email pending verification')
}

const id = await longNanoid()
const id = nanoid(32)
await emailVerificationCollection.insertOne({
id,
userId,
Expand Down
4 changes: 0 additions & 4 deletions packages/api-server/src/utils/nanoid.utils.ts

This file was deleted.

21 changes: 7 additions & 14 deletions packages/api-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"outDir": "./dist",
"lib": [
"es2021"
],
"lib": ["es2021"],
"module": "node16",
"skipLibCheck": true
"skipLibCheck": true,
},
"include": [
"./src",
"./configs",
"./package.json",
"./tests"
],
"include": ["./src", "./configs", "./package.json", "./tests"],
"references": [
{
"path": "../common"
"path": "../common",
},
{
"path": "../types"
}
]
"path": "../types",
},
],
}
Loading

0 comments on commit 8edbdfb

Please sign in to comment.