This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
510 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# https://hub.docker.com/_/node | ||
FROM node:20 as builder | ||
|
||
WORKDIR /usr/app | ||
|
||
COPY ./package.json ./package-lock.json ./ | ||
COPY ./packages/shared ./packages/shared | ||
COPY ./packages/socket/package.json \ | ||
./packages/socket/tsconfig.json \ | ||
./packages/socket/ | ||
COPY ./packages/socket/src/ ./packages/socket/src/ | ||
|
||
RUN npm install -w mzm-socket -w mzm-shared && npm run -w mzm-socket cleanbuild | ||
|
||
FROM node:20-alpine | ||
|
||
WORKDIR /usr/app | ||
|
||
COPY --from=builder /usr/app/package.json /usr/app/package-lock.json ./ | ||
COPY --from=builder /usr/app/packages/shared/dist/ packages/shared/dist/ | ||
COPY --from=builder /usr/app/packages/shared/package.json packages/shared/package.json | ||
COPY --from=builder /usr/app/packages/socket/package.json ./packages/socket/ | ||
COPY --from=builder /usr/app/packages/socket/dist/ packages/socket/dist/ | ||
RUN npm install -w mzm-socket -w mzm-shared --production | ||
|
||
ENV NODE_ENV=production | ||
CMD [ "node", "packages/socket/dist/server.js" ] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import type WebSocket from 'ws' | ||
import { verifyAccessToken } from 'mzm-shared/src/auth/index' | ||
import { randomUUID } from 'crypto' | ||
import { SocketToBackendType, TO_SERVER_CMD } from 'mzm-shared/src/type/socket' | ||
import { requestSocketAPI } from './lib/req.js' | ||
import { saveSocket, removeSocket } from './lib/sender.js' | ||
import { consume } from './lib/consumer.js' | ||
import { logger } from './lib/logger.js' | ||
import { JWT } from './config.js' | ||
import { ExtWebSocket } from './types.js' | ||
|
||
export const createApp = ({ wss }: { wss: WebSocket.Server }) => { | ||
consume() | ||
|
||
wss.on('connection', async function connection(ws: ExtWebSocket, req) { | ||
let userId: string | null = null | ||
|
||
if (!req.url) { | ||
return | ||
} | ||
const url = new URL(req.url, `http://${req.headers.host}`) | ||
const token = url.searchParams.get('token') | ||
if (token) { | ||
const { err, decoded } = await verifyAccessToken( | ||
token, | ||
JWT.accessTokenSecret, | ||
{ | ||
issuer: JWT.issuer, | ||
audience: JWT.audience | ||
} | ||
) | ||
logger.info({ | ||
label: 'ws:verifyAccessToken', | ||
err, | ||
decoded | ||
}) | ||
if (!err && decoded) { | ||
userId = decoded.user._id | ||
} | ||
} | ||
logger.info({ | ||
label: 'ws:connection', | ||
userId | ||
}) | ||
|
||
if (!userId) { | ||
ws.close() | ||
return | ||
} | ||
|
||
const id = randomUUID() | ||
ws.id = id | ||
saveSocket(id, userId, ws) | ||
|
||
ws.on('message', async function incoming(data) { | ||
const message = data.toString() | ||
logger.info({ | ||
label: 'ws:message', | ||
userId, | ||
id, | ||
message | ||
}) | ||
if (message === 'pong') { | ||
return | ||
} | ||
if (!userId) { | ||
return | ||
} | ||
try { | ||
const res = await requestSocketAPI(message, userId, id) | ||
if (res.body) { | ||
ws.send(res.body) | ||
logger.info({ | ||
label: 'ws:send', | ||
userId, | ||
id, | ||
body: res.body | ||
}) | ||
} | ||
} catch (e) { | ||
logger.error({ | ||
label: 'post:error', | ||
err: e | ||
}) | ||
} | ||
}) | ||
|
||
ws.on('close', function close() { | ||
if (!userId) { | ||
return | ||
} | ||
logger.info('ws:closed', userId, ws.id) | ||
removeSocket(ws.id, userId) | ||
}) | ||
|
||
ws.on('error', function error(e) { | ||
logger.error({ | ||
label: 'ws:error', | ||
err: e | ||
}) | ||
}) | ||
|
||
const data: SocketToBackendType = { | ||
cmd: TO_SERVER_CMD.CONNECTION | ||
} | ||
|
||
requestSocketAPI(JSON.stringify(data), userId, id) | ||
.then(({ body }) => { | ||
if (body) { | ||
ws.send(body) | ||
} | ||
}) | ||
.catch((e) => { | ||
logger.error({ | ||
label: 'post:error', | ||
err: e | ||
}) | ||
}) | ||
}) | ||
|
||
setInterval(() => { | ||
wss.clients.forEach((ws) => { | ||
ws.send('ping') | ||
}) | ||
}, 50000) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
import type { RedisOptions } from 'ioredis' | ||
import { config } from 'dotenv' | ||
config() | ||
if (process.env.NODE_ENV !== 'test') { | ||
config() | ||
} | ||
|
||
export const WORKER_NUM = Number(process.env.WORKER_NUM) ?? 1 | ||
|
||
export const WORKER_NUM = 2 | ||
export const SOCKET_LISTEN = 3000 | ||
export const PORT: number = process.env.PORT | ||
? parseInt(process.env.PORT, 10) | ||
: 3000 | ||
|
||
export const INTERNAL_API_URL = process.env.INTERNAL_API | ||
export const INTERNAL_API_URL = process.env.INTERNAL_API! | ||
|
||
export const redis = { | ||
export const REDIS = { | ||
options: { | ||
host: process.env.REDIS_HOST, | ||
enableOfflineQueue: false | ||
} | ||
} | ||
enableOfflineQueue: false, | ||
connectTimeout: Number(process.env.SESSION_REDIS_TIMEOUT) ?? 30000 | ||
} satisfies RedisOptions | ||
} as const | ||
|
||
export const JWT = { | ||
accessTokenSecret: process.env.ACCESS_TOKEN_SECRET!, | ||
internalAccessTokenSecret: process.env.INTERNAL_ACCESS_TOKEN_SECRET!, | ||
issuer: process.env.JWT_ISSURE ?? 'https://mzm.dev', | ||
audience: process.env.JWT_AUDIENCE | ||
? process.env.JWT_AUDIENCE.split(',') | ||
: (['https://mzm.dev'] as string[]) | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import bunyan from 'bunyan' | ||
|
||
const logger = bunyan.createLogger({ | ||
export const logger = bunyan.createLogger({ | ||
name: 'socket' | ||
}) | ||
|
||
export default logger |
Oops, something went wrong.