rslay/ZeroChat

View on GitHub

Showing 14 of 29 total issues

File chat.ts has 477 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { Request, Response } from 'express'
const express = require("express")
const fileUploader = require('express-fileupload');
const User = require("../classes/User")
const ConfigSetup = require('../utils/configSetup')
Severity: Minor
Found in routes/chat.ts - About 7 hrs to fix

    Function broadcast has a Cognitive Complexity of 33 (exceeds 5 allowed). Consider refactoring.
    Open

    const broadcast = async (user: typeof User | null, message: string, room: string, file: FileUpload | null | undefined = null) => {
        // A second check before sending. TODO It should not be possible to hit this branch, remove other checks elsewhere?
        if (message.trim() === '' && !file) {
            console.warn("User managed to send a post request with no file or message and pass initial checks!")
            user.disconnect()
    Severity: Minor
    Found in routes/chat.ts - About 4 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function broadcast has 58 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    const broadcast = async (user: typeof User | null, message: string, room: string, file: FileUpload | null | undefined = null) => {
        // A second check before sending. TODO It should not be possible to hit this branch, remove other checks elsewhere?
        if (message.trim() === '' && !file) {
            console.warn("User managed to send a post request with no file or message and pass initial checks!")
            user.disconnect()
    Severity: Major
    Found in routes/chat.ts - About 2 hrs to fix

      Function disconnect has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          disconnect() {
              if (this.frames.chatroom) {
                  try {
                      this.frames.chatroom.end()
                  } catch (error) { }
      Severity: Minor
      Found in classes/User.ts - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function loadThemes has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

          loadThemes: () => {
              return new Promise((resolve, reject) => {
                  try {
                      fs.readdir("public/themes", (err: Error, files: Array<String>) => {
                          files.forEach(file => {
      Severity: Minor
      Found in utils/configSetup.ts - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Consider simplifying this complex logical expression.
      Open

              } else if ((req.body.handle && req.body.handle.length > MAX_HANDLE_LENGTH)
                  || (req.body.passcode && req.body.passcode.length > MAX_PASSCODE_LENGTH)
                  || (req.body.room && req.body.room.length > MAX_ROOMNAME_LENGTH)) {
                  // Disconnect if user is logging in with a name/passcode/room too long
                  return res.render(VIEWS.LAYOUT, {
      Severity: Major
      Found in routes/chat.ts - About 40 mins to fix

        Function genTripcode has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

            genTripcode: (password: String) => {
                return new Promise(function (resolve, reject) {
                    if (password == null || password == "") {
                        return resolve("Anon")
                    }
        Severity: Minor
        Found in utils/security.ts - About 35 mins to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function filesLimitHandler has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

        const filesLimitHandler = (req: ZCRequest, res: Response, next: Function) => {
            let user = getUserByToken(req.query.token as string)
            if (!user) { return res.render(VIEWS.ERROR, ERRORS.INVALID_TOKEN) }
        
            if (req.files && Object.keys(req.files) && Object.keys(req.files).length > 1) {
        Severity: Minor
        Found in routes/chat.ts - About 35 mins to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Avoid too many return statements within this function.
        Open

                    return res.render(VIEWS.LAYOUT, {
                        page: VIEWS.ERROR_MESSAGE,
                        url: "_hidden",
                        error: "Someone with that handle is already in /" + req.body.room,
                        user: user,
        Severity: Major
        Found in routes/chat.ts - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                      return res.render(VIEWS.LAYOUT, {
                          page: VIEWS.ERROR_MESSAGE,
                          url: '_hidden',
                          error: 'Message or file required.',
                          user: user,
          Severity: Major
          Found in routes/chat.ts - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                        return res.render(VIEWS.LAYOUT, {
                            page: VIEWS.ERROR_MESSAGE,
                            url: "_hidden",
                            error: "Field missing",
                            user: user,
            Severity: Major
            Found in routes/chat.ts - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                      return next()
              Severity: Major
              Found in routes/chat.ts - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                            return res.render(VIEWS.LAYOUT, {
                                page: VIEWS.ERROR_MESSAGE,
                                url: "_hidden",
                                error: "A field you entered was too long",
                                user: user,
                Severity: Major
                Found in routes/chat.ts - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                              return res.render(VIEWS.LAYOUT, {
                                  page: VIEWS.FRONT_PAGE,
                                  handleMaxlen: MAX_HANDLE_LENGTH,
                                  passMaxlen: MAX_PASSCODE_LENGTH,
                                  roomNameMaxlen: MAX_ROOMNAME_LENGTH,
                  Severity: Major
                  Found in routes/chat.ts - About 30 mins to fix
                    Severity
                    Category
                    Status
                    Source
                    Language