From e1ae79cb9cce5792eef559c9c2167530284da32b Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 15:53:59 +0100 Subject: [PATCH 1/9] server/tsconfig: remove redundant options --- server/tsconfig.json | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/server/tsconfig.json b/server/tsconfig.json index 992c7873..b62bf9eb 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -15,13 +15,8 @@ "files": true }, "compilerOptions": { - "outDir": "../dist" /* Specify an output folder for all emitted files. See more: https://www.typescriptlang.org/tsconfig#outDir */, "noEmit": false /* Disable emitting file from a compilation. See more: https://www.typescriptlang.org/tsconfig#noEmit */, - // TODO: Remove eventually "noImplicitAny": false /*Enable error reporting for expressions and declarations with an implied any type. See more: https://www.typescriptlang.org/tsconfig#noImplicitAny */ - } /* Instructs the TypeScript compiler how to compile .ts files. */, - "exclude": [ - "./dist" - ] /* Specifies a list of glob patterns that match files to be excluded from compilation. Requires TypeScript version 2.0 or later. */ + } /* Instructs the TypeScript compiler how to compile .ts files. */ } From 21d1eea6b8f73a236343402c962f1e79d21d6a8d Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 15:54:24 +0100 Subject: [PATCH 2/9] tsconfig: Add shared reference --- tsconfig.json | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index fe1b0c3a..bfd7b57a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,12 +6,9 @@ ] /* If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. When a 'files' property is specified, only those files and those specified by 'include' are included. */, // "exclude": [], "references": [ - { - "path": "./client" /* Path to referenced tsconfig or to folder containing tsconfig. */ - }, - { - "path": "./server" /* Path to referenced tsconfig or to folder containing tsconfig. */ - } + {"path": "./client"}, + {"path": "./server"}, + {"path": "./shared"} ] /* Referenced projects. Requires TypeScript version 3.0 or later. */, "compilerOptions": { // TODO: Remove eventually From a8149c0f1ab4d166c8fcada860b4bc9355b807ad Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 16:58:33 +0100 Subject: [PATCH 3/9] Extract linkify to shared directory This is the first step to sever any dependency of the server on the client --- server/plugins/irc-events/link.ts | 2 +- server/tsconfig.json | 3 +- shared/linkify.ts | 84 +++++++++++++++++++++++++++++++ shared/tsconfig.json | 13 +++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 shared/linkify.ts create mode 100644 shared/tsconfig.json diff --git a/server/plugins/irc-events/link.ts b/server/plugins/irc-events/link.ts index e370fc7a..9755d556 100644 --- a/server/plugins/irc-events/link.ts +++ b/server/plugins/irc-events/link.ts @@ -5,7 +5,7 @@ import mime from "mime-types"; import log from "../../log"; import Config from "../../config"; -import {findLinksWithSchema} from "../../../client/js/helpers/ircmessageparser/findLinks"; +import {findLinksWithSchema} from "../../../shared/linkify"; import storage from "../storage"; import Client from "../../client"; import Chan from "../../models/chan"; diff --git a/server/tsconfig.json b/server/tsconfig.json index b62bf9eb..1c012b95 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../tsconfig.base.json" /* Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later. */, "include": [ "**/*", - "../client/js/helpers/ircmessageparser/*.ts" + "../client/js/helpers/ircmessageparser/*.ts", + "../shared/" ] /* Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later. */, "files": [ "../client/js/constants.ts", diff --git a/shared/linkify.ts b/shared/linkify.ts new file mode 100644 index 00000000..b403b533 --- /dev/null +++ b/shared/linkify.ts @@ -0,0 +1,84 @@ +import LinkifyIt, {Match} from "linkify-it"; +import tlds from "tlds"; + +export type NoSchemaMatch = Match & { + noschema: boolean; +}; + +export type LinkPart = { + start: number; + end: number; + link: string; +}; + +LinkifyIt.prototype.normalize = function normalize(match: NoSchemaMatch) { + match.noschema = false; + + if (!match.schema) { + match.schema = "http:"; + match.url = "http://" + match.url; + match.noschema = true; + } + + if (match.schema === "//") { + match.schema = "http:"; + match.url = "http:" + match.url; + match.noschema = true; + } + + if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) { + match.url = "mailto:" + match.url; + } +}; + +const linkify = LinkifyIt().tlds(tlds).tlds("onion", true); + +// Known schemes to detect in text +const commonSchemes = [ + "sftp", + "smb", + "file", + "irc", + "ircs", + "svn", + "git", + "steam", + "mumble", + "ts3server", + "svn+ssh", + "ssh", + "gopher", + "gemini", +]; + +for (const schema of commonSchemes) { + linkify.add(schema + ":", "http:"); +} + +export function findLinks(text: string) { + const matches = linkify.match(text) as NoSchemaMatch[]; + + if (!matches) { + return []; + } + + return matches.map(makeLinkPart); +} + +export function findLinksWithSchema(text: string) { + const matches = linkify.match(text) as NoSchemaMatch[]; + + if (!matches) { + return []; + } + + return matches.filter((url) => !url.noschema).map(makeLinkPart); +} + +function makeLinkPart(url: NoSchemaMatch): LinkPart { + return { + start: url.index, + end: url.lastIndex, + link: url.url, + }; +} diff --git a/shared/tsconfig.json b/shared/tsconfig.json new file mode 100644 index 00000000..fedb8212 --- /dev/null +++ b/shared/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../tsconfig.base.json" /* Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later. */, + "include": [ + "*" + ] /* Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later. */, + "files": [] /* If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. When a 'files' property is specified, only those files and those specified by 'include' are included. */, + "ts-node": { + "files": true + }, + "compilerOptions": { + "noEmit": false /* Disable emitting file from a compilation. See more: https://www.typescriptlang.org/tsconfig#noEmit */ + } +} From 9d349558366f3e001ce162308dcdc73142129042 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 17:04:41 +0100 Subject: [PATCH 4/9] extract cleanIrcMessage from client to shared --- server/plugins/irc-events/message.ts | 2 +- server/tsconfig.json | 1 - shared/irc.ts | 6 ++++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 shared/irc.ts diff --git a/server/plugins/irc-events/message.ts b/server/plugins/irc-events/message.ts index 469ecabb..682d3b8a 100644 --- a/server/plugins/irc-events/message.ts +++ b/server/plugins/irc-events/message.ts @@ -1,6 +1,6 @@ import Msg, {MessageType} from "../../models/msg"; import LinkPrefetch from "./link"; -import cleanIrcMessage from "../../../client/js/helpers/ircmessageparser/cleanIrcMessage"; +import {cleanIrcMessage} from "../../../shared/irc"; import Helper from "../../helper"; import {IrcEventHandler} from "../../client"; import Chan, {ChanType} from "../../models/chan"; diff --git a/server/tsconfig.json b/server/tsconfig.json index 1c012b95..ef6e0817 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../tsconfig.base.json" /* Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later. */, "include": [ "**/*", - "../client/js/helpers/ircmessageparser/*.ts", "../shared/" ] /* Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later. */, "files": [ diff --git a/shared/irc.ts b/shared/irc.ts new file mode 100644 index 00000000..4a18613e --- /dev/null +++ b/shared/irc.ts @@ -0,0 +1,6 @@ +const matchFormatting = + /\x02|\x1D|\x1F|\x16|\x0F|\x11|\x1E|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?|\x04(?:[0-9a-f]{6}(?:,[0-9a-f]{6})?)?/gi; + +export function cleanIrcMessage(message: string) { + return message.replace(matchFormatting, "").trim(); +} From e305e23c43295a71ab839e51bdc3e32e812fa2a4 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 17:28:03 +0100 Subject: [PATCH 5/9] client: use the versions in shared/ where applicable --- .../ircmessageparser/cleanIrcMessage.ts | 4 - .../js/helpers/ircmessageparser/findLinks.ts | 83 ------------------- client/js/helpers/ircmessageparser/merge.ts | 2 +- client/js/helpers/parse.ts | 10 +-- client/js/socket-events/msg.ts | 2 +- client/tsconfig.json | 3 +- webpack.config.ts | 2 +- 7 files changed, 10 insertions(+), 96 deletions(-) delete mode 100644 client/js/helpers/ircmessageparser/cleanIrcMessage.ts delete mode 100644 client/js/helpers/ircmessageparser/findLinks.ts diff --git a/client/js/helpers/ircmessageparser/cleanIrcMessage.ts b/client/js/helpers/ircmessageparser/cleanIrcMessage.ts deleted file mode 100644 index 7e4fdf35..00000000 --- a/client/js/helpers/ircmessageparser/cleanIrcMessage.ts +++ /dev/null @@ -1,4 +0,0 @@ -const matchFormatting = - /\x02|\x1D|\x1F|\x16|\x0F|\x11|\x1E|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?|\x04(?:[0-9a-f]{6}(?:,[0-9a-f]{6})?)?/gi; - -export default (message: string) => message.replace(matchFormatting, "").trim(); diff --git a/client/js/helpers/ircmessageparser/findLinks.ts b/client/js/helpers/ircmessageparser/findLinks.ts deleted file mode 100644 index 15c38be9..00000000 --- a/client/js/helpers/ircmessageparser/findLinks.ts +++ /dev/null @@ -1,83 +0,0 @@ -import LinkifyIt, {Match} from "linkify-it"; -import {Part} from "./merge"; - -export type LinkPart = Part & { - link: string; -}; - -type OurMatch = Match & { - noschema?: boolean; -}; - -LinkifyIt.prototype.normalize = function normalize(match: OurMatch) { - if (!match.schema) { - match.schema = "http:"; - match.url = "http://" + match.url; - match.noschema = true; - } - - if (match.schema === "//") { - match.schema = "http:"; - match.url = "http:" + match.url; - match.noschema = true; - } - - if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) { - match.url = "mailto:" + match.url; - } -}; - -import tlds from "tlds"; -const linkify = LinkifyIt().tlds(tlds).tlds("onion", true); - -// Known schemes to detect in text -const commonSchemes = [ - "sftp", - "smb", - "file", - "irc", - "ircs", - "svn", - "git", - "steam", - "mumble", - "ts3server", - "svn+ssh", - "ssh", - "gopher", - "gemini", -]; - -for (const schema of commonSchemes) { - linkify.add(schema + ":", "http:"); -} - -function findLinks(text: string) { - const matches = linkify.match(text) as OurMatch[]; - - if (!matches) { - return []; - } - - return matches.map(returnUrl); -} - -function findLinksWithSchema(text: string) { - const matches = linkify.match(text) as OurMatch[]; - - if (!matches) { - return []; - } - - return matches.filter((url) => !url.noschema).map(returnUrl); -} - -function returnUrl(url: OurMatch): LinkPart { - return { - start: url.index, - end: url.lastIndex, - link: url.url, - }; -} - -export {findLinks, findLinksWithSchema}; diff --git a/client/js/helpers/ircmessageparser/merge.ts b/client/js/helpers/ircmessageparser/merge.ts index 9c728cec..707c495d 100644 --- a/client/js/helpers/ircmessageparser/merge.ts +++ b/client/js/helpers/ircmessageparser/merge.ts @@ -2,7 +2,7 @@ import anyIntersection from "./anyIntersection"; import fill from "./fill"; import {ChannelPart} from "./findChannels"; import {EmojiPart} from "./findEmoji"; -import {LinkPart} from "./findLinks"; +import {LinkPart} from "../../../../shared/linkify"; import {NamePart} from "./findNames"; export type Part = { diff --git a/client/js/helpers/parse.ts b/client/js/helpers/parse.ts index b120d70f..e69067a4 100644 --- a/client/js/helpers/parse.ts +++ b/client/js/helpers/parse.ts @@ -3,11 +3,11 @@ import {h as createElement, VNode} from "vue"; import parseStyle from "./ircmessageparser/parseStyle"; -import findChannels, {ChannelPart} from "./ircmessageparser/findChannels"; -import {findLinks, LinkPart} from "./ircmessageparser/findLinks"; -import findEmoji, {EmojiPart} from "./ircmessageparser/findEmoji"; -import findNames, {NamePart} from "./ircmessageparser/findNames"; -import merge, {MergedParts, Part} from "./ircmessageparser/merge"; +import findChannels from "./ircmessageparser/findChannels"; +import {findLinks} from "../../../shared/linkify"; +import findEmoji from "./ircmessageparser/findEmoji"; +import findNames from "./ircmessageparser/findNames"; +import merge, {MergedParts} from "./ircmessageparser/merge"; import emojiMap from "./fullnamemap.json"; import LinkPreviewToggle from "../../components/LinkPreviewToggle.vue"; import LinkPreviewFileSize from "../../components/LinkPreviewFileSize.vue"; diff --git a/client/js/socket-events/msg.ts b/client/js/socket-events/msg.ts index 523b2f56..4bb5e7c4 100644 --- a/client/js/socket-events/msg.ts +++ b/client/js/socket-events/msg.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/restrict-plus-operands */ import socket from "../socket"; -import cleanIrcMessage from "../helpers/ircmessageparser/cleanIrcMessage"; +import {cleanIrcMessage} from "../../../shared/irc"; import {store} from "../store"; import {switchToChannel} from "../router"; import {ClientChan, ClientMention, ClientMessage, NetChan} from "../types"; diff --git a/client/tsconfig.json b/client/tsconfig.json index f5434d2c..4054c4f1 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "../tsconfig.base.json" /* Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later. */, "include": [ - "./**/*" + "./**/*", + "../shared" ] /* Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later. */, "files": [ "../package.json", diff --git a/webpack.config.ts b/webpack.config.ts index b103b2a4..02a7ca54 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -58,7 +58,7 @@ const config: webpack.Configuration = { }, { test: /\.ts$/i, - include: [path.resolve(__dirname, "client")], + include: [path.resolve(__dirname, "client"), path.resolve(__dirname, "shared")], exclude: path.resolve(__dirname, "node_modules"), use: { loader: "babel-loader", From 60bb561e4941831aa15e48d1002dc32f9c018136 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 17:57:27 +0100 Subject: [PATCH 6/9] Extract tests to shared/ --- .../helpers/ircmessageparser => shared}/cleanIrcMessage.ts | 2 +- .../js/helpers/ircmessageparser => shared}/findLinks.ts | 5 +---- test/tsconfig.json | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) rename test/{client/js/helpers/ircmessageparser => shared}/cleanIrcMessage.ts (94%) rename test/{client/js/helpers/ircmessageparser => shared}/findLinks.ts (98%) diff --git a/test/client/js/helpers/ircmessageparser/cleanIrcMessage.ts b/test/shared/cleanIrcMessage.ts similarity index 94% rename from test/client/js/helpers/ircmessageparser/cleanIrcMessage.ts rename to test/shared/cleanIrcMessage.ts index 4abe23a2..1cc1ff49 100644 --- a/test/client/js/helpers/ircmessageparser/cleanIrcMessage.ts +++ b/test/shared/cleanIrcMessage.ts @@ -1,5 +1,5 @@ import {expect} from "chai"; -import cleanIrcMessage from "../../../../../client/js/helpers/ircmessageparser/cleanIrcMessage"; +import {cleanIrcMessage} from "../../shared/irc"; describe("cleanIrcMessage", function () { it("should remove all formatting", function () { diff --git a/test/client/js/helpers/ircmessageparser/findLinks.ts b/test/shared/findLinks.ts similarity index 98% rename from test/client/js/helpers/ircmessageparser/findLinks.ts rename to test/shared/findLinks.ts index 350830b2..c5be4608 100644 --- a/test/client/js/helpers/ircmessageparser/findLinks.ts +++ b/test/shared/findLinks.ts @@ -1,8 +1,5 @@ import {expect} from "chai"; -import { - findLinks, - findLinksWithSchema, -} from "../../../../../client/js/helpers/ircmessageparser/findLinks"; +import {findLinks, findLinksWithSchema} from "../../shared/linkify"; describe("findLinks", () => { it("should find url", () => { diff --git a/test/tsconfig.json b/test/tsconfig.json index 27207ccc..b4485495 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -3,7 +3,8 @@ "include": [ "**/*", "../client", - "../server" + "../server", + "../shared" ] /* Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later. */, "files": [ "../babel.config.cjs", From 6f13735a7f25e60394f85ec84bb76d51ec26b0fc Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 17:57:58 +0100 Subject: [PATCH 7/9] eslint: add shared/ --- .eslintrc.cjs | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 4b567364..4b04e87a 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -7,6 +7,7 @@ const projects = defineConfig({ "./tsconfig.json", "./client/tsconfig.json", "./server/tsconfig.json", + "./shared/tsconfig.json", "./test/tsconfig.json", ], }, From b7540b582765596eadbc3cd08a87b5b8f3a30210 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 29 Jan 2023 23:16:55 +0100 Subject: [PATCH 8/9] Move condensedTypes to shared/ This decouples the rest of the server from the client --- client/components/MessageCondensed.vue | 6 +++--- client/components/MessageList.vue | 10 +++------- client/js/constants.ts | 13 ------------- server/client.ts | 4 ++-- server/tsconfig.json | 1 - shared/irc.ts | 12 ++++++++++++ test/client/js/constantsTest.ts | 13 ------------- test/shared/irc.ts | 14 ++++++++++++++ 8 files changed, 34 insertions(+), 39 deletions(-) create mode 100644 test/shared/irc.ts diff --git a/client/components/MessageCondensed.vue b/client/components/MessageCondensed.vue index ba20283b..c45a7ac0 100644 --- a/client/components/MessageCondensed.vue +++ b/client/components/MessageCondensed.vue @@ -19,7 +19,7 @@