rslay/ZeroChat

View on GitHub

Showing 29 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

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

        httpServer.listen(Config.HTTP_PORT, (err: any) => {
            if (err) {
                console.error(err)
                process.exit(1)
            }
    Severity: Major
    Found in index.ts and 1 other location - About 4 hrs to fix
    index.ts on lines 63..73

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 121.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

        httpsServer.listen(Config.HTTPS_PORT, (err: any) => {
            if (err) {
                console.error(err)
                process.exit(1)
            }
    Severity: Major
    Found in index.ts and 1 other location - About 4 hrs to fix
    index.ts on lines 84..94

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 121.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

        if (req.files && Object.keys(req.files) && Object.keys(req.files).length > 1) {
            // Can only upload one file
            return res.render(VIEWS.LAYOUT, {
                page: VIEWS.ERROR_MESSAGE,
                url: "_hidden",
    Severity: Major
    Found in routes/chat.ts and 1 other location - About 3 hrs to fix
    routes/chat.ts on lines 239..248

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 106.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

        if (req.message && req.message.length > MAX_MESSAGE_LENGTH) {
            // Message too large
            return res.render(VIEWS.LAYOUT, {
                page: VIEWS.ERROR_MESSAGE,
                url: "_hidden",
    Severity: Major
    Found in routes/chat.ts and 1 other location - About 3 hrs to fix
    routes/chat.ts on lines 195..213

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 106.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    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

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

              if (!ConfigSetup.isValidTheme(req.body.theme)) {
                  return res.render(VIEWS.LAYOUT, {
                      page: VIEWS.ERROR_MESSAGE,
                      url: "_hidden",
                      error: "Invalid theme",
      Severity: Major
      Found in routes/chat.ts and 1 other location - About 1 hr to fix
      routes/chat.ts on lines 499..507

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 74.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

          if (!req.body.setSettings) {
              return res.render(VIEWS.LAYOUT, {
                  page: VIEWS.ERROR_MESSAGE,
                  url: "_hidden",
                  error: "Invalid request",
      Severity: Major
      Found in routes/chat.ts and 1 other location - About 1 hr to fix
      routes/chat.ts on lines 257..265

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 74.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      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

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

          if (!isPosting && (req.file || req.message)) {
              // File request IS present, but for the wrong page
              return res.render(VIEWS.LAYOUT, {
                  page: VIEWS.ERROR_MESSAGE,
                  url: '_hidden',
      Severity: Minor
      Found in routes/chat.ts and 1 other location - About 50 mins to fix
      routes/chat.ts on lines 317..337

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 52.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      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: Minor
      Found in routes/chat.ts and 1 other location - About 50 mins to fix
      routes/chat.ts on lines 268..338

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 52.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      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

        Similar blocks of code found in 5 locations. Consider refactoring.
        Open

                if (this.frames.upload) {
                    try {
                        this.frames.upload.end()
                    } catch (error) { }
                }
        Severity: Major
        Found in classes/User.ts and 4 other locations - About 35 mins to fix
        classes/User.ts on lines 69..73
        classes/User.ts on lines 74..78
        classes/User.ts on lines 84..88
        classes/User.ts on lines 89..93

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 5 locations. Consider refactoring.
        Open

                if (this.frames.chat) {
                    try {
                        this.frames.chat.end()
                    } catch (error) { }
                }
        Severity: Major
        Found in classes/User.ts and 4 other locations - About 35 mins to fix
        classes/User.ts on lines 69..73
        classes/User.ts on lines 74..78
        classes/User.ts on lines 79..83
        classes/User.ts on lines 89..93

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 5 locations. Consider refactoring.
        Open

                if (this.frames.post) {
                    try {
                        this.frames.post.end()
                    } catch (error) { }
                }
        Severity: Major
        Found in classes/User.ts and 4 other locations - About 35 mins to fix
        classes/User.ts on lines 69..73
        classes/User.ts on lines 79..83
        classes/User.ts on lines 84..88
        classes/User.ts on lines 89..93

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Similar blocks of code found in 5 locations. Consider refactoring.
        Open

                if (this.frames.settings) {
                    try {
                        this.frames.settings.end()
                    } catch (error) { }
                }
        Severity: Major
        Found in classes/User.ts and 4 other locations - About 35 mins to fix
        classes/User.ts on lines 69..73
        classes/User.ts on lines 74..78
        classes/User.ts on lines 79..83
        classes/User.ts on lines 84..88

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 46.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Severity
        Category
        Status
        Source
        Language