-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Androz2091 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# `@discord-player/core` | ||
|
||
Discord Player `@discord-player/core` library | ||
|
||
## Installation | ||
|
||
```sh | ||
$ yarn add @discord-player/core | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
import pkg from "@discord-player/core" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { add } from '../src'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
describe('Sum', () => { | ||
it('should add two numbers', () => { | ||
expect(add(2, 2)).toBe(4); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@discord-player/core", | ||
"version": "0.1.0", | ||
"description": "A complete framework to simplify the implementation of music commands for Discord bots", | ||
"keywords": [ | ||
"discord-player", | ||
"music", | ||
"bot", | ||
"discord.js", | ||
"javascript", | ||
"voip", | ||
"lavalink", | ||
"lavaplayer" | ||
], | ||
"author": "Androz2091 <androz2091@gmail.com>", | ||
"homepage": "https://discord-player.js.org", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Androz2091/discord-player.git" | ||
}, | ||
"scripts": { | ||
"build": "tsup", | ||
"build:check": "tsc --noEmit", | ||
"lint": "eslint src --ext .ts --fix", | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Androz2091/discord-player/issues" | ||
}, | ||
"devDependencies": { | ||
"@discord-player/tsconfig": "workspace:^" | ||
}, | ||
"dependencies": { | ||
"@discord-player/utils": "workspace:^", | ||
"discord-voip": "workspace:^" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum WorkerOp { | ||
OP_PING | ||
} | ||
|
||
export enum WorkerAckOp { | ||
OP_ACK_PING | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const add = (a: number, b: number) => { | ||
return a + b; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-inferrable-types | ||
export const version: string = '[VI]{{inject}}[/VI]'; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { isMainThread, parentPort } from 'node:worker_threads'; | ||
import { WorkerAckOp, WorkerOp } from '../common/constants'; | ||
|
||
interface WorkerMessage<T> { | ||
op: WorkerOp; | ||
d: T; | ||
} | ||
|
||
interface WorkerMessageAck<T> { | ||
op: WorkerAckOp; | ||
d: T; | ||
} | ||
|
||
class WorkerListener { | ||
public constructor() { | ||
if (!parentPort) throw new Error('This script must be run as a worker'); | ||
|
||
parentPort.on('message', async (message) => { | ||
try { | ||
this.validate(message); | ||
await this.handleMessage(message); | ||
} catch { | ||
// | ||
} | ||
}); | ||
} | ||
|
||
private validate(message: WorkerMessage<unknown>) { | ||
if (!('op' in message) || !('d' in message)) throw new Error('Invalid message format'); | ||
if (!(message.op in WorkerOp)) throw new Error('Invalid operation code'); | ||
|
||
return true; | ||
} | ||
|
||
private send<T>(message: WorkerMessageAck<T>) { | ||
parentPort!.postMessage(message); | ||
} | ||
|
||
private async handleMessage(message: WorkerMessage<unknown>) { | ||
switch (message.op) { | ||
case WorkerOp.OP_PING: | ||
return this.send({ op: WorkerAckOp.OP_ACK_PING, d: null }); | ||
} | ||
} | ||
} | ||
|
||
if (!isMainThread) { | ||
new WorkerListener(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Worker } from 'node:worker_threads'; | ||
import { Collection, EventEmitter } from '@discord-player/utils'; | ||
|
||
interface IWorkerStats { | ||
memoryUsed: number; | ||
subscriptions: number; | ||
} | ||
|
||
interface WorkerInfo { | ||
worker: Worker; | ||
lastAccess: number; | ||
estimatedClients: number; | ||
stats: IWorkerStats; | ||
} | ||
|
||
export enum WorkerDistributionMode { | ||
Balanced = 'balanced', | ||
LeastLoad = 'least-load', | ||
Random = 'random' | ||
} | ||
|
||
export interface WorkerConfig { | ||
maxWorkers: number; | ||
distributionMode: WorkerDistributionMode; | ||
} | ||
|
||
export interface WorkerEvents { | ||
error: (error: Error) => void; | ||
exit: () => void; | ||
ready: () => void; | ||
} | ||
|
||
export class WorkerManager extends EventEmitter<WorkerEvents> { | ||
private workers = new Collection<number, WorkerInfo>(); | ||
|
||
public constructor(public readonly config: WorkerConfig) { | ||
super(); | ||
} | ||
|
||
public getOptimalWorker(): WorkerInfo | undefined { | ||
if (this.workers.size === 0) return undefined; | ||
|
||
switch (this.config.distributionMode) { | ||
case WorkerDistributionMode.LeastLoad: | ||
return this.workers.sort((a, b) => a.stats.memoryUsed - b.stats.memoryUsed).first(); | ||
case WorkerDistributionMode.Balanced: | ||
return this.workers.sort((a, b) => a.estimatedClients - b.estimatedClients).first(); | ||
case WorkerDistributionMode.Random: | ||
return this.workers.random(); | ||
} | ||
} | ||
|
||
public canCreateWorker(): boolean { | ||
return this.workers.size < this.config.maxWorkers; | ||
} | ||
|
||
public create() { | ||
if (!this.canCreateWorker()) return this.getOptimalWorker()!; | ||
|
||
const thread = new Worker('./worker/entrypoint.js'); | ||
|
||
thread.on('online', () => { | ||
this.emit('ready'); | ||
}); | ||
|
||
thread.on('error', (error) => { | ||
void error; | ||
}); | ||
|
||
thread.once('exit', () => { | ||
this.workers.delete(thread.threadId); | ||
}); | ||
|
||
const workerInfo: WorkerInfo = { | ||
worker: thread, | ||
estimatedClients: 0, | ||
stats: { | ||
memoryUsed: 0, | ||
subscriptions: 0 | ||
}, | ||
lastAccess: Date.now() | ||
}; | ||
|
||
this.workers.set(thread.threadId, workerInfo); | ||
|
||
return workerInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "@discord-player/tsconfig/base.json", | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from '../../tsup.config'; | ||
import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; | ||
|
||
export default defineConfig({ | ||
esbuildPlugins: [esbuildPluginVersionInjector()], | ||
entry: ['./src'] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
dir: `${__dirname}/__test__`, | ||
passWithNoTests: true, | ||
watch: false | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters