Skip to content

Commit

Permalink
Merge pull request #13 from anzusystems/upgrade
Browse files Browse the repository at this point in the history
Upgrade dependencies
  • Loading branch information
pulzarraider authored May 9, 2024
2 parents 874588c + eb0681c commit 6ed5c6b
Show file tree
Hide file tree
Showing 17 changed files with 2,644 additions and 2,521 deletions.
5 changes: 0 additions & 5 deletions .eslintignore

This file was deleted.

26 changes: 0 additions & 26 deletions .eslintrc.js

This file was deleted.

54 changes: 54 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import eslint from '@eslint/js'
import prettierPluginRecommended from 'eslint-plugin-prettier/recommended'
import tsEslint from 'typescript-eslint'
import unicornPlugin from 'eslint-plugin-unicorn'
import jestPlugin from 'eslint-plugin-jest'

export default tsEslint.config(
{
ignores: [
'node_modules/**',
'coverage/**',
'eslint.config.mjs',
'jest.config.js',
'dist/**',
],
},

eslint.configs.recommended,
...tsEslint.configs.recommended,
...tsEslint.configs.stylisticTypeChecked,
unicornPlugin.configs['flat/recommended'],
jestPlugin.configs['flat/recommended'],
prettierPluginRecommended,
{
rules: {
'prettier/prettier': 2,
'unicorn/no-null': 'off',
'unicorn/filename-case': 'off',
'unicorn/prefer-node-protocol': 'off',
'unicorn/import-style': [
'error',
{
extendDefaultStyles: false,
},
],
},
},
{
files: ['**/*.ts'],
languageOptions: {
parserOptions: {
project: ['tsconfig.json'],
},
},
},
{
files: ['test/**/*.ts'],
languageOptions: {
parserOptions: {
project: ['test.tsconfig.json'],
},
},
},
)
35 changes: 18 additions & 17 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module.exports = {
'preset': 'ts-jest',
'testEnvironment': 'node',
'testMatch': ['**/test/**/?(*.)+(spec|test).[jt]s?(x)'],
'collectCoverage': true,
'collectCoverageFrom': ['src/**/*.ts'],
'coverageReporters': ['text-summary', 'json', 'html', 'cobertura'],
'reporters': [
'default',
[ 'jest-junit', {
suiteName: 'Notification jest tests',
outputName: 'TEST-junit.xml'
} ]
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/test/**/?(*.)+(spec|test).[jt]s?(x)'],
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
coverageReporters: ['text-summary', 'json', 'html', 'cobertura'],
reporters: [
'default',
[
'jest-junit',
{
suiteName: 'Notification jest tests',
outputName: 'TEST-junit.xml',
},
],
'verbose': true,
'setupFiles': [
'<rootDir>/test/setup-tests.ts'
],
};
],
verbose: true,
setupFiles: ['<rootDir>/test/setup-tests.ts'],
}
59 changes: 30 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notification-server",
"packageManager": "yarn@4.0.1",
"packageManager": "yarn@4.2.1",
"version": "1.0.0",
"description": "",
"license": "Apache-2.0",
Expand All @@ -13,7 +13,7 @@
],
"main": "dist/index.js",
"scripts": {
"lint": "eslint . --ext .js,.ts",
"lint": "eslint .",
"clean": "rimraf dist",
"tsc": "tsc",
"build": "yarn clean && yarn lint --fix && tsc",
Expand All @@ -24,35 +24,36 @@
"test": "jest --coverage=true --runInBand"
},
"dependencies": {
"@google-cloud/pubsub": "^3.1.0",
"cookies": "^0.8.0",
"dotenv": "^16.3.1",
"jsonwebtoken": "^9.0.0",
"reflect-metadata": "^0.1.13",
"@google-cloud/pubsub": "^4.4.0",
"cookies": "^0.9.1",
"dotenv": "^16.4.5",
"jsonwebtoken": "^9.0.2",
"reflect-metadata": "^0.2.2",
"typedi": "^0.10.0",
"typescript": "^4.7.4",
"winston": "^3.8.1",
"ws": "^8.8.1"
"typescript": "^5.4.5",
"winston": "^3.13.0",
"ws": "^8.17.0"
},
"devDependencies": {
"@types/cookies": "^0.7.7",
"@types/jest": "^28.1.6",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.6.3",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "^5.31.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unicorn": "^43.0.2",
"jest": "^28.1.3",
"jest-junit": "^14.0.0",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.7",
"ts-node": "^10.9.1",
"tslib": "^2.4.0"
"@types/cookies": "^0.9.0",
"@types/jest": "^29.5.12",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.12.11",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"eslint": "^9.2.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.5.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^52.0.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"nodemon": "^3.1.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"typescript-eslint": "^7.8.0"
}
}
8 changes: 4 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Config {
return Config.jwtPublicKey
}

Config.jwtPublicKey = this.base64ToAscii(process.env.SSO_JWT_PUBLIC_KEY as string)
Config.jwtPublicKey = this.base64ToAscii(process.env.SSO_JWT_PUBLIC_KEY!)

return Config.jwtPublicKey
}
Expand All @@ -40,7 +40,7 @@ export class Config {
return Config.jwtPrivateKey
}

Config.jwtPrivateKey = this.base64ToAscii(process.env.SSO_JWT_PRIVATE_KEY as string)
Config.jwtPrivateKey = this.base64ToAscii(process.env.SSO_JWT_PRIVATE_KEY!)

return Config.jwtPrivateKey
}
Expand All @@ -60,7 +60,7 @@ export class Config {
return Config.jwtCookies
}

Config.jwtCookies = (process.env.SSO_JWT_COOKIES as string).split(',')
Config.jwtCookies = process.env.SSO_JWT_COOKIES!.split(',')

return Config.jwtCookies
}
Expand All @@ -80,7 +80,7 @@ export class Config {
return Config.webSocketServerCors
}

Config.webSocketServerCors = process.env.WEBSOCKET_SERVER_CORS as string
Config.webSocketServerCors = process.env.WEBSOCKET_SERVER_CORS!

return Config.webSocketServerCors
}
Expand Down
7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import AppLogger from './logger/app-logger'
import {Container} from 'typedi'
import {WebsocketServer} from './server/websocket-server'

declare module 'ws' {
interface WebSocket {
isAlive: boolean
ssoUserId: string
}
}

declare module 'http' {
interface IncomingMessage {
ssoUserId: string
Expand Down
16 changes: 8 additions & 8 deletions src/model/user-connections.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {Service} from 'typedi'
import {WebSocket} from 'ws'
import {UserWebSocket} from './user-web-socket'

@Service({global: true})
export class UserConnections {
private readonly users: Map<string, Set<WebSocket>> = new Map<string, Set<WebSocket>>()
private readonly users: Map<string, Set<UserWebSocket>> = new Map<string, Set<UserWebSocket>>()

add(socket: WebSocket): void {
add(socket: UserWebSocket): void {
const userSockets = this.users.get(socket.ssoUserId)
if (userSockets && userSockets.add(socket)) {
if (userSockets?.add(socket)) {
return
}

this.users.set(socket.ssoUserId, new Set<WebSocket>().add(socket))
this.users.set(socket.ssoUserId, new Set<UserWebSocket>().add(socket))
}

remove(socket: WebSocket): void {
remove(socket: UserWebSocket): void {
this.users.get(socket.ssoUserId)?.delete(socket)
}

getAllForUser(ssoUserId: string): Set<WebSocket> {
return this.users.get(ssoUserId) ?? new Set<WebSocket>()
getAllForUser(ssoUserId: string): Set<UserWebSocket> {
return this.users.get(ssoUserId) ?? new Set<UserWebSocket>()
}
}
6 changes: 6 additions & 0 deletions src/model/user-web-socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {WebSocket} from 'ws'

export interface UserWebSocket extends WebSocket {
isAlive: boolean
ssoUserId: string
}
4 changes: 2 additions & 2 deletions src/pubsub/notification-pub-sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {UserConnections} from '../model/user-connections'

@Service()
export class NotificationPubSub {
private readonly topicName = process.env.PUBSUB_TOPIC as string
private readonly instanceName = process.env.APP_INSTANCE_NAME as string
private readonly topicName = process.env.PUBSUB_TOPIC!
private readonly instanceName = process.env.APP_INSTANCE_NAME!
private _topic: Topic | undefined
private _subscription: Subscription | undefined
private readonly pubSubClient: PubSub
Expand Down
36 changes: 21 additions & 15 deletions src/server/websocket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Inject, Service} from 'typedi'
import {UserConnections} from '../model/user-connections'
import AppLogger from '../logger/app-logger'
import {Config} from '../config/config'
import {UserWebSocket} from '../model/user-web-socket'

@Service()
export class WebsocketServer {
Expand Down Expand Up @@ -49,33 +50,38 @@ export class WebsocketServer {
return
}

wss.handleUpgrade(request, socket, head, (ws) => {
ws.ssoUserId = request.ssoUserId
userConnections.add(ws)
wss.emit('connection', ws, request)
wss.handleUpgrade(request, socket, head, (ws: WebSocket) => {
const userWs = ws as UserWebSocket

userWs.ssoUserId = request.ssoUserId
userConnections.add(userWs)
wss.emit('connection', userWs, request)
})
})

wss.on('connection', function connection(ws) {
ws.isAlive = true
ws.on('pong', () => (ws.isAlive = true))
ws.on('close', () => userConnections.remove(ws))
wss.on('connection', function connection(ws: WebSocket) {
const userWs = ws as UserWebSocket

userWs.isAlive = true
userWs.on('pong', () => (userWs.isAlive = true))
userWs.on('close', () => userConnections.remove(userWs))
})

this.heartBeatInterval = setInterval(function ping() {
for (const ws of wss.clients) {
if (ws.isAlive === false) {
userConnections.remove(ws)
ws.terminate()
const userWs = ws as UserWebSocket
if (userWs.isAlive === false) {
userConnections.remove(userWs)
userWs.terminate()

continue
}

ws.isAlive = false
ws.ping(null, false, (error) => {
userWs.isAlive = false
userWs.ping(null, false, (error) => {
if (error) {
userConnections.remove(ws)
ws.terminate()
userConnections.remove(userWs)
userWs.terminate()
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/util/user-token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export function verifyAuthorization(request: IncomingMessage, logger: AppLogger)
const jwtRaw = Config.getJwtCookies()
.map((cookieName) => cookies.get(cookieName))
.join('.')

// eslint-disable-next-line unicorn/prefer-string-replace-all
const jwtPartsCount = jwtRaw.replace(/[^.]/gi, '').length
if (!jwtRaw || jwtPartsCount !== 2) {
throw new Error('Invalid JWT token')
Expand Down
6 changes: 6 additions & 0 deletions test.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"include": [
"test/**/*"
],
"extends": "./tsconfig.json"
}
Loading

0 comments on commit 6ed5c6b

Please sign in to comment.