mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: send status to the frontend
This commit is contained in:
1 parent
622d64dd45
commit
039da53bbe
5 files changed
+8
-46
No files matched your search
@@ -6,8 +6,6 @@ import { pushToSession } from '../socket.js'
|
||||
import { finishSession } from '../../models/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
|
||||
const commandCompleteCallbacks = new Map()
|
||||
|
||||
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { command, pwd, output, error, params } = matchedData(req)
|
||||
|
||||
@@ -53,40 +51,12 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
|
||||
session.responses.push(response.responses)
|
||||
|
||||
if (response.stage === 'END') {
|
||||
if ((session.status as string) === 'finished') {
|
||||
await finishSession(session)
|
||||
}
|
||||
// commandCompleteCallbacks.set(session.id, response.callback)
|
||||
pushToSession(session.id, 'response', response)
|
||||
}
|
||||
|
||||
res.status(200).end()
|
||||
await session.save()
|
||||
})
|
||||
|
||||
export const completedCommand = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { sessionId } = req.user as any
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'session not found' })
|
||||
return
|
||||
}
|
||||
|
||||
if (session.status === 'inactive') {
|
||||
res.status(400).json({ message: 'session is not active' })
|
||||
return
|
||||
}
|
||||
|
||||
const callback = commandCompleteCallbacks.get(session.id)
|
||||
if (!callback) {
|
||||
res.status(200).json({ message: 'no callback found' })
|
||||
return
|
||||
}
|
||||
callback()
|
||||
commandCompleteCallbacks.delete(session.id)
|
||||
res.json({ message: 'ok' })
|
||||
}
|
||||
)
|
||||
@@ -5,6 +5,5 @@ import { newCommandValidator } from '../validators/commandValidator.js'
|
||||
const router = Router()
|
||||
|
||||
router.post('/', newCommandValidator, commandController.newCommand)
|
||||
router.post('/completed', commandController.completedCommand)
|
||||
|
||||
export default router
|
||||
Reference in new issue
Block a user