Skip to content

Commit

Permalink
chore(api): websocket get test
Browse files Browse the repository at this point in the history
  • Loading branch information
JPBM135 committed May 4, 2023
1 parent 7c12fa3 commit 219824b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/api/src/routes/paths/scan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RouteManager } from '@structures/routeClass.js';
import { scanHandlerHandler } from 'routes/sub-routes/scan/CreateScan.js';
import { getRecentScanHandler, getScanHandler } from 'routes/sub-routes/scan/GetScan.js';
import { scanGetWebsocketHandler } from 'routes/sub-routes/scan/WebsocketScan.js';

export default class ScanRoute extends RouteManager {
public constructor() {
Expand All @@ -10,6 +11,10 @@ export default class ScanRoute extends RouteManager {
route: '/recent',
handler: getRecentScanHandler,
},
{
route: '/:scan_id/ws',
handler: scanGetWebsocketHandler,
},
{
route: '/:scan_id',
handler: getScanHandler,
Expand Down
13 changes: 13 additions & 0 deletions packages/api/src/routes/sub-routes/scan/WebsocketScan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Buffer } from 'node:buffer';
import type { IncomingMessage } from 'node:http';
import { HttpError } from '@structures/httpError.js';
import { HttpStatusCode } from '@types';
import { globToRegex } from '@utils/globToRegex.js';
import { errorResponse } from '@utils/respond.js';
import { validateId } from '@utils/validators.js';
import { OP_DELIMITER } from 'constants.js';
import type { Request, Response } from 'express';
import type { Redis } from 'ioredis';
import logger from 'logger.js';
import { kRedis, kWebSockets } from 'tokens.js';
Expand All @@ -12,6 +15,16 @@ import type { WebSocket, WebSocketServer } from 'ws';

const SCAN_WEBSOCKET_PATH_REGEX = globToRegex('/api/v1/scan/*/ws');

export async function scanGetWebsocketHandler(req: Request, res: Response): Promise<void> {
const wss = container.resolve<WebSocketServer>(kWebSockets);

try {
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), scanWebsocketHandler);
} catch (error) {
errorResponse(HttpError.fromError(error as Error), res);
}
}

export async function scanWebsocketHandler(ws: WebSocket, req: IncomingMessage): Promise<void> {
const redis = container.resolve<Redis>(kRedis);

Expand Down

0 comments on commit 219824b

Please sign in to comment.