Skip to content

Commit

Permalink
refactor: make the worker file a JS file
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Nov 11, 2023
1 parent a681cca commit c3d61f9
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 587 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
uses: actions/checkout@v3
- name: Add problem matchers
run: echo "::add-matcher::.github/problemMatchers/eslint.json"
- name: Use Node.js v18
- name: Use Node.js v20
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: yarn
- name: Install Dependencies
run: yarn --immutable
Expand All @@ -35,10 +35,10 @@ jobs:
submodules: true
- name: Add problem matchers
run: echo "::add-matcher::.github/problemMatchers/tsc.json"
- name: Use Node.js v18
- name: Use Node.js v20
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: yarn
- name: Install Dependencies
run: yarn --immutable
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-tlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
steps:
- name: Checkout Project
uses: actions/checkout@v3
- name: Use Node.js v18
- name: Use Node.js v20
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: yarn
- name: Install Dependencies
run: yarn --immutable
Expand Down
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"clean": "node scripts/build/clean.mjs",
"start": "node --enable-source-maps dist/Skyra.js",
"start:profiler:0x": "0x --collect-only dist/Skyra.js",
"test": "NODE_OPTIONS=\"--import tsx/esm\" vitest",
"test": "vitest",
"lint": "eslint --fix --ext ts src tests",
"format": "prettier --write --log-level=warn \"{src,tests}/**/*.{js,ts,json}\"",
"scripts:tlds": "node scripts/tlds.mjs",
Expand Down Expand Up @@ -66,8 +66,8 @@
"@sapphire/stopwatch": "^1.5.0",
"@sapphire/time-utilities": "^1.7.10",
"@sapphire/utilities": "^3.13.0",
"@sentry/integrations": "^7.79.0",
"@sentry/node": "^7.79.0",
"@sentry/integrations": "^7.80.0",
"@sentry/node": "^7.80.0",
"@skyra/ai": "^1.2.0",
"@skyra/char": "^1.0.3",
"@skyra/env-utilities": "^1.2.1",
Expand All @@ -77,7 +77,7 @@
"colorette": "^2.0.20",
"confusables": "^1.1.1",
"diff": "^5.1.0",
"discord-api-types": "~0.37.62",
"discord-api-types": "~0.37.63",
"discord.js": "^14.13.0",
"he": "^1.2.0",
"pg": "^8.11.3",
Expand All @@ -87,8 +87,8 @@
},
"devDependencies": {
"0x": "^5.7.0",
"@commitlint/cli": "^18.2.0",
"@commitlint/config-conventional": "^18.1.0",
"@commitlint/cli": "^18.4.0",
"@commitlint/config-conventional": "^18.4.0",
"@sapphire/eslint-config": "^5.0.2",
"@sapphire/prettier-config": "^2.0.0",
"@sapphire/ts-config": "^5.0.0",
Expand All @@ -105,11 +105,9 @@
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"lint-staged": "^15.0.2",
"lint-staged": "^15.1.0",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"tsc-watch": "^6.0.4",
"tsx": "^4.0.0",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
},
Expand Down
3 changes: 1 addition & 2 deletions src/lib/moderation/workers/WorkerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { WorkerResponseHandler } from '#lib/moderation/workers/WorkerResponseHan
import { OutgoingType, type IncomingPayload, type NoId, type OutgoingPayload } from '#lib/moderation/workers/types';
import { AsyncQueue } from '@sapphire/async-queue';
import { container } from '@sapphire/framework';
import { envParseString } from '@skyra/env-utilities';
import { cyan, green, red, yellow } from 'colorette';
import { once } from 'node:events';
import { SHARE_ENV, Worker } from 'node:worker_threads';
Expand Down Expand Up @@ -127,7 +126,7 @@ export class WorkerHandler {
}

private static readonly logsEnabled = process.env.NODE_ENV !== 'test';
private static readonly filename = new URL(`./worker.${envParseString('NODE_ENV') === 'test' ? 't' : 'j'}s`, import.meta.url);
private static readonly filename = new URL('worker.mjs', import.meta.url);

private static readonly maximumId = Number.MAX_SAFE_INTEGER;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import type { IncomingPayload, IncomingRunRegExpPayload, OutgoingPayload } from '#lib/moderation/workers/types';
import { remove as removeConfusables } from 'confusables';
import { isMainThread, parentPort } from 'node:worker_threads';

if (isMainThread || parentPort === null) throw new Error('The Worker may only be ran via the worker_threads fork method!');

const { IncomingType, OutgoingType } = (await import(
process.env.NODE_ENV === 'test' ? './types.ts' : '#lib/moderation/workers/types'
)) as typeof import('./types.js');
/** @typedef {import('./types.js').IncomingType} IncomingType */
/** @typedef {import('./types.js').IncomingRunRegExpPayload} IncomingRunRegExpPayload */
/** @typedef {import('./types.js').OutgoingPayload} OutgoingPayload */
/** @typedef {import('./types.js').OutgoingRegExpMatchPayload} OutgoingRegExpMatchPayload */

function post(message: OutgoingPayload) {
return parentPort!.postMessage(message);
const IncomingType = {
RunRegExp: 0
};

const OutgoingType = {
Heartbeat: 0,
UnknownCommand: 1,
NoContent: 2,
RegExpMatch: 3
};

/**
* @param {OutgoingPayload} message
*/
function post(message) {
return parentPort.postMessage(message);
}

post({ type: OutgoingType.Heartbeat });

setInterval(() => post({ type: OutgoingType.Heartbeat }), 45000).unref();

parentPort.on('message', (message: IncomingPayload) => post(handleMessage(message)));
parentPort.on('message', (message) => post(handleMessage(message)));

function handleMessage(message: IncomingPayload): OutgoingPayload {
/**
* @param {IncomingPayload} message
* @returns {OutgoingPayload}
*/
function handleMessage(message) {
switch (message.type) {
case IncomingType.RunRegExp:
return handleRunRegExp(message);
Expand All @@ -27,7 +45,12 @@ function handleMessage(message: IncomingPayload): OutgoingPayload {
}
}

function handleRunRegExp(message: IncomingRunRegExpPayload): OutgoingPayload {
/**
* Handles running a regular expression filter on a message's content after removing confusables.
* @param {IncomingRunRegExpPayload} message - The message object to filter.
* @returns {OutgoingPayload} - The filtered message content, if any.
*/
function handleRunRegExp(message) {
// Remove confusables and run filter:
const result = filter(removeConfusables(message.content), message.regExp);
if (result === null) return { id: message.id, type: OutgoingType.NoContent };
Expand All @@ -36,15 +59,24 @@ function handleRunRegExp(message: IncomingRunRegExpPayload): OutgoingPayload {
return { id: message.id, type: OutgoingType.RegExpMatch, filtered: result.filtered, highlighted: result.highlighted };
}

function filter(str: string, regex: RegExp) {
/** @typedef {Pick<OutgoingRegExpMatchPayload, 'filtered' | 'highlighted'>} RegExpMatchResult */

/**
* Filters a string based on a regular expression, replacing matching sections with asterisks and returning both the filtered and highlighted versions.
*
* @param {string} str - The string to filter.
* @param {RegExp} regex - The regular expression to match against.
* @returns {RegExpMatchResult | null}
*/
function filter(str, regex) {
const matches = str.match(regex);
if (matches === null) return null;

let last = 0;
let next = 0;

const filtered: string[] = [];
const highlighted: string[] = [];
const filtered = [];
const highlighted = [];
for (const match of matches) {
next = str.indexOf(match, last);
const section = str.slice(last, next);
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": ["@sapphire/ts-config", "@sapphire/ts-config/extra-strict", "@sapphire/ts-config/decorators", "@sapphire/ts-config/verbatim"],
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"removeComments": true
}
Expand Down
Loading

0 comments on commit c3d61f9

Please sign in to comment.