Archived
0
0
Fork 0

Compare commits

...

10 commits

20 changed files with 1102 additions and 1048 deletions

View file

@ -7,8 +7,6 @@
}, },
"extends": [ "extends": [
"eslint:recommended", "eslint:recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended" "plugin:@typescript-eslint/recommended"
], ],
@ -122,27 +120,7 @@
{ {
"case": "camelCase" "case": "camelCase"
} }
],
// Import Rules
"import/order": [
"error",
{
"newlines-between": "always-and-inside-groups",
"groups": [
[
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object",
"type"
] ]
]
}
],
"import/newline-after-import": "error"
}, },
"reportUnusedDisableDirectives": true "reportUnusedDisableDirectives": true
} }

View file

@ -1,3 +0,0 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,7 @@
nodeLinker: node-modules nodeLinker: node-modules
nmMode: hardlinks-local
plugins: plugins:
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs - path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: "@yarnpkg/plugin-version" spec: "@yarnpkg/plugin-version"
@ -10,4 +12,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs - path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript" spec: "@yarnpkg/plugin-typescript"
yarnPath: .yarn/releases/yarn-sources.cjs yarnPath: .yarn/releases/yarn-3.0.0.cjs

18
Jenkinsfile vendored
View file

@ -17,6 +17,18 @@ pipeline {
} }
stages { stages {
stage('Checkout') {
steps {
script {
echo '==========Checking out=========='
if (sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)) {
error "'[ci skip]' found in git commit message. Aborting."
currentBuild.result = 'NOT_BUILT'
}
}
}
}
stage('Dependencies') { stage('Dependencies') {
steps { steps {
echo '==========Installing Dependencies==========' echo '==========Installing Dependencies=========='
@ -54,6 +66,9 @@ pipeline {
cleanWs() cleanWs()
} }
success { success {
sh 'tar -cf - dist/ | xz -9 -c - > neonjs-library.tar.xz'
archiveArtifacts artifacts: 'neonjs-library.tar.xz', fingerprint: true
discordSend description: 'CI build was a Success', discordSend description: 'CI build was a Success',
link: env.BUILD_URL, link: env.BUILD_URL,
result: 'SUCCESS', result: 'SUCCESS',
@ -61,6 +76,9 @@ pipeline {
webhookURL: env.DISCORD_WEBHOOK webhookURL: env.DISCORD_WEBHOOK
} }
unstable { unstable {
sh 'tar -cf - dist/ | xz -9 -c - > neonjs-library.tar.xz'
archiveArtifacts artifacts: 'neonjs-library.tar.xz', fingerprint: true
discordSend description: 'CI build was Unstable.', discordSend description: 'CI build was Unstable.',
link: env.BUILD_URL, link: env.BUILD_URL,
result: 'UNSTABLE', result: 'UNSTABLE',

View file

@ -29,6 +29,7 @@
"build": "tsc", "build": "tsc",
"commit": "cz", "commit": "cz",
"lint": "eslint --format=pretty src", "lint": "eslint --format=pretty src",
"lint:fix": "eslint --format=pretty src --fix",
"postinstall": "husky install", "postinstall": "husky install",
"prepublishOnly": "pinst --disable", "prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable", "postpublish": "pinst --enable",
@ -37,16 +38,19 @@
"dependencies": { "dependencies": {
"@sapphire/async-queue": "^1.1.4", "@sapphire/async-queue": "^1.1.4",
"abort-controller": "^3.0.0", "abort-controller": "^3.0.0",
"cache-manager": "^3.4.4",
"cache-manager-ioredis": "^2.1.0",
"fast-zlib": "^2.0.1", "fast-zlib": "^2.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"ioredis": "^4.27.7",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"ws": "^7.5.3" "ws": "^8.0.0"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "^12.1.4", "@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^12.1.4", "@commitlint/config-conventional": "^13.1.0",
"@commitlint/cz-commitlint": "^12.1.4", "@commitlint/cz-commitlint": "^13.1.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1", "@istanbuljs/nyc-config-typescript": "^1.0.1",
"@saithodev/semantic-release-gitea": "^2.1.0", "@saithodev/semantic-release-gitea": "^2.1.0",
"@semantic-release/changelog": "^5.0.1", "@semantic-release/changelog": "^5.0.1",
@ -54,35 +58,36 @@
"@semantic-release/git": "^9.0.0", "@semantic-release/git": "^9.0.0",
"@semantic-release/npm": "^7.1.3", "@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3", "@semantic-release/release-notes-generator": "^9.0.3",
"@types/cache-manager": "^3.4.2",
"@types/cache-manager-ioredis": "^2.0.2",
"@types/chai": "^4.2.21", "@types/chai": "^4.2.21",
"@types/eslint": "^7.28.0", "@types/eslint": "^7.28.0",
"@types/lodash": "^4.14.171", "@types/ioredis": "^4.26.7",
"@types/mocha": "^8.2.3", "@types/lodash": "^4.14.172",
"@types/node": "^16.3.2", "@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.5.11", "@types/node": "^16.4.13",
"@types/node-fetch": "^2.5.12",
"@types/semantic-release": "^17.2.1", "@types/semantic-release": "^17.2.1",
"@types/sinon": "^10.0.2", "@types/sinon": "^10.0.2",
"@types/source-map-support": "^0.5.4", "@types/source-map-support": "^0.5.4",
"@types/ws": "^7.4.6", "@types/ws": "^7.4.7",
"@typescript-eslint/eslint-plugin": "^4.28.3", "@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.28.3", "@typescript-eslint/parser": "^4.29.0",
"@typescript-eslint/typescript-estree": "^4.28.3", "@typescript-eslint/typescript-estree": "^4.29.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"commitizen": "^4.2.4", "commitizen": "^4.2.4",
"eslint": "^7.30.0", "eslint": "^7.32.0",
"eslint-formatter-pretty": "^4.1.0", "eslint-formatter-pretty": "^4.1.0",
"eslint-plugin-header": "^3.1.1", "eslint-plugin-unicorn": "^35.0.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-unicorn": "^34.0.1",
"husky": "^7.0.1", "husky": "^7.0.1",
"inquirer": "^8.1.2", "inquirer": "^8.1.2",
"lint-staged": "^11.0.1", "lint-staged": "^11.1.2",
"mocha": "^9.0.2", "mocha": "^9.0.3",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"pinst": "^2.1.6", "pinst": "^2.1.6",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"semantic-release": "^17.4.4", "semantic-release": "^17.4.4",
"sinon": "^11.1.1", "sinon": "^11.1.2",
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"ts-node": "^10.1.0", "ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1", "tsconfig-paths": "^3.10.1",
@ -106,5 +111,6 @@
"commitizen": { "commitizen": {
"path": "@commitlint/cz-commitlint" "path": "@commitlint/cz-commitlint"
} }
} },
"packageManager": "yarn@3.0.0"
} }

View file

@ -53,7 +53,7 @@ export class ApiClient {
} }
public async post<T>(options: IRequestOptions): Promise<T> { public async post<T>(options: IRequestOptions): Promise<T> {
return this.manager.createRequest<T>({ return await this.manager.createRequest<T>({
method: 'POST', method: 'POST',
body: options.body, body: options.body,
headers: options.headers, headers: options.headers,

View file

@ -2,12 +2,11 @@ import { AsyncQueue } from '@sapphire/async-queue';
import { AbortController } from 'abort-controller'; import { AbortController } from 'abort-controller';
import FormData from 'form-data'; import FormData from 'form-data';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import { sleep } from '../utils/sleep';
import type { ApiManager } from './apiManager';
import type { Client } from '../client/client'; import type { Client } from '../client/client';
import { sleep } from '../utils/sleep';
import type { IMakeRequestOptions, IRouteIdentifier } from '../utils/types'; import type { IMakeRequestOptions, IRouteIdentifier } from '../utils/types';
import type { ApiManager } from './apiManager';
function calculateReset(reset: number, resetAfter: number, serverDate: number): number { function calculateReset(reset: number, resetAfter: number, serverDate: number): number {
if (resetAfter) { if (resetAfter) {

View file

@ -1,7 +1,7 @@
import { ApiClient } from './apiClient'; import { ApiClient } from './apiClient';
import type { Client } from '../client/client'; import type { Client } from '../client/client';
import type { IApiCreateMessage } from '../utils/types'; import type { IApiCreateMessage, IApiCreateSlashCommand, IApiUser, IFile } from '../utils/types';
export class ApiHelper { export class ApiHelper {
public apiClient: ApiClient; public apiClient: ApiClient;
@ -16,11 +16,35 @@ export class ApiHelper {
this.apiClient = new ApiClient(this.client, this._token); this.apiClient = new ApiClient(this.client, this._token);
} }
public createMessage(channelId: string, options: IApiCreateMessage): void { // TODO: Return message object
this.apiClient.post({ public async createMessage(channelId: string, options: IApiCreateMessage, files?: IFile[]): Promise<void> {
await this.apiClient.post({
path: `/channels/${channelId}/messages`, path: `/channels/${channelId}/messages`,
requireAuth: true, requireAuth: true,
body: options, body: options,
files: files,
});
}
public async getCurrentUser(): Promise<IApiUser> {
return await this.apiClient.get<IApiUser>({
path: '/users/@me',
requireAuth: true,
});
}
public async createSlashCommand(options: IApiCreateSlashCommand): Promise<void> {
await this.apiClient.post({
path: `/applications/${this.client.user.id}/commands`,
requireAuth: true,
body: options,
});
}
public async getUser(id: string): Promise<IApiUser> {
return await this.apiClient.get<IApiUser>({
path: `/users/${id}`,
requireAuth: true,
}); });
} }
} }

View file

@ -1,9 +1,10 @@
import { ApiHandler } from './apiHandler';
import { Snowflake } from '../utils/snowflake'; import { Snowflake } from '../utils/snowflake';
import { ApiHandler } from './apiHandler';
import type { Client } from '../client/client'; import type { Client } from '../client/client';
import type { ApiMethods, IMakeRequestOptions, IRouteIdentifier } from '../utils/types'; import type { ApiMethods, IMakeRequestOptions, IRouteIdentifier } from '../utils/types';
export class ApiManager { export class ApiManager {
public client: Client; public client: Client;
public globalReset: number; public globalReset: number;

View file

@ -1,27 +1,29 @@
import _ from 'lodash'; import _ from 'lodash';
import EventEmitter from 'events'; import EventEmitter from 'events';
import { ApiHelper } from '../api/apiHelper'; import { ApiHelper } from '../api/apiHelper';
import { ClientUser } from '../structures/clientUser';
import { GatewayClient } from '../gateway/gatewayClient'; import { GatewayClient } from '../gateway/gatewayClient';
import { defaults } from '../utils/defaults'; import { defaults } from '../utils/defaults';
import type { DeepRequired, IClientOptions } from '../utils/types'; import type { DeepRequired, IClientOptions } from '../utils/types';
export class Client extends EventEmitter { export class Client extends EventEmitter {
public api: ApiHelper; public readonly api: ApiHelper;
public readonly options: DeepRequired<IClientOptions>; public readonly options: DeepRequired<IClientOptions>;
public ws: GatewayClient; public readonly user: ClientUser;
public readonly ws: GatewayClient;
private _token: string; private _token: string;
public constructor(token: string, options: IClientOptions = {}) { public constructor(token: string, options?: IClientOptions) {
super(); super();
this.options = _.merge(defaults.clientOptions, options as DeepRequired<IClientOptions>); this.options = _.merge(defaults.clientOptions, options as DeepRequired<IClientOptions>);
this._token = token; this._token = token;
this.api = new ApiHelper(this, this._token); this.api = new ApiHelper(this, this._token);
this.ws = new GatewayClient(this); this.user = new ClientUser(this);
this.ws = new GatewayClient(this, this._token);
} }
public async login(): Promise<void> { public async login(): Promise<void> {

View file

@ -1,31 +1,37 @@
import zlib from 'fast-zlib'; import zlib from 'fast-zlib';
import os from 'os';
import WebSocket from 'ws'; import WebSocket from 'ws';
import type { Client } from '../client/client';
import type { IGatewayMessage } from '../utils/types';
import type { Client } from '../client/client';
import type { GatewayReceiveMessage, GatewaySendMessage } from '../utils/types';
export class GatewayClient { export class GatewayClient {
public client: Client; public client: Client;
public connection: WebSocket | null; public connection: WebSocket | null;
public deflate: zlib.Deflate;
public inflate: zlib.Inflate; public inflate: zlib.Inflate;
private _heartbeatInterval: number;
private _heartbeatIntervalTimer: NodeJS.Timer | null;
private _sequence: number; private _sequence: number;
private _token: string;
public constructor(client: Client) { public constructor(client: Client, token: string) {
this.client = client; this.client = client;
this.connection = null; this.connection = null;
this.deflate = new zlib.Deflate();
this.inflate = new zlib.Inflate(); this.inflate = new zlib.Inflate();
this._heartbeatInterval = 0;
this._heartbeatIntervalTimer= null;
this._sequence = 0; this._sequence = 0;
this._token= token;
} }
public async close(): Promise<void> { public async close(): Promise<void> {
if (!this.connection) throw new Error('You are not connected to the Discord WebSocket Gateway!'); if (!this.connection) throw new Error('You are not connected to the Discord WebSocket Gateway!');
this.connection.close(1000); this.connection.close(1000);
if (this._heartbeatIntervalTimer) clearTimeout(this._heartbeatIntervalTimer);
} }
public async connect(): Promise<void> { public async connect(): Promise<void> {
@ -36,13 +42,50 @@ export class GatewayClient {
); );
this.connection.on('message', async (msg: Buffer | string) => { this.connection.on('message', async (msg: Buffer | string) => {
let parsedMessage: IGatewayMessage<null>; let parsedMessage: GatewayReceiveMessage;
if (this.client.options.ws.compression && typeof msg === 'object') parsedMessage = JSON.parse(this.inflate.process(msg).toString('utf8')); if (this.client.options.ws.compression && typeof msg === 'object') parsedMessage = JSON.parse(this.inflate.process(msg).toString('utf8'));
else if (!this.client.options.ws.compression && typeof msg === 'string') parsedMessage = JSON.parse(msg); else if (!this.client.options.ws.compression && typeof msg === 'string') parsedMessage = JSON.parse(msg);
else parsedMessage = JSON.parse(msg.toString('utf8')); else parsedMessage = JSON.parse(msg.toString('utf8'));
if (parsedMessage.s) this._sequence = parsedMessage.s; // if (parsedMessage.s) this._sequence = parsedMessage.s;
this.client.emit('raw', parsedMessage);
switch (parsedMessage.op) {
case 10:
this._heartbeatInterval = parsedMessage.d.heartbeat_interval;
this.send({
op: 2, d: {
compress: this.client.options.ws.compression,
intents: this.client.options.ws.intents,
large_threshold: this.client.options.ws.largeThreshold,
presence: this.client.options.presence,
properties: {
$browser: '@neonjs/library',
$device: '@neonjs/library',
$os: os.platform(),
},
token: this._token,
},
});
this._heartbeatIntervalTimer = setInterval(() => this._sendHeartbeat(), this._heartbeatInterval * 1000);
break;
default:
break;
}
}); });
} }
public send(data: GatewaySendMessage): void {
if (!this.connection) throw new Error('You are not connected to the Discord WebSocket Gateway!');
this.connection.send(JSON.stringify(data));
}
private _sendHeartbeat(): void {
this.send({ op: 1, d: this._sequence });
}
} }

View file

@ -0,0 +1,9 @@
import { User } from './user';
import type { Client } from '../client/client';
export class ClientUser extends User {
public constructor(client: Client) {
super(client);
}
}

18
src/structures/user.ts Normal file
View file

@ -0,0 +1,18 @@
import type { IApiUser } from '..';
import type { Client } from '../client/client';
export class User {
public client: Client;
public id: string;
private _options: IApiUser;
public constructor(client: Client, options: IApiUser) {
this.client = client;
this._options = options;
}
public async fetch(id: string): Promise<IApiUser> {
return this.client.api.getUser(id);
}
}

51
src/utils/cacheManager.ts Normal file
View file

@ -0,0 +1,51 @@
import cacheManager from 'cache-manager';
import redisStore from 'cache-manager-ioredis';
import type { Client } from '../client/client';
export class CacheManager {
public client: Client;
private readonly _cacheManager: cacheManager.Cache;
public constructor(client: Client) {
this.client = client;
if (this.client.options.cache.cache === 'redis') this._cacheManager = cacheManager.caching({
store: redisStore,
ttl: this.client.options.cache.ttl,
...this.client.options.cache.options,
});
else this._cacheManager = cacheManager.caching({
store: 'memory',
ttl: this.client.options.cache.ttl,
});
}
public async del(key: string): Promise<void> {
return new Promise((res, rej) => {
this._cacheManager.del(key, (err) => {
if (err) rej(err);
else res();
});
});
}
public async get<T>(key: string): Promise<T | undefined> {
return new Promise((res, rej) => {
this._cacheManager.get<T>(key, (err, result) => {
if (err) rej(err);
else res(result);
});
});
}
public async set<T>(key: string, value: T): Promise<T> {
return new Promise((res, rej) => {
this._cacheManager.set<T>(key, value, this.client.options.cache.ttl, (err) => {
if (err) rej(err);
else res(value);
});
});
}
}

View file

@ -10,9 +10,16 @@ export const defaults: IDefaultOptions = {
url: 'https://discord.com/api', url: 'https://discord.com/api',
version: 9, version: 9,
}, },
cache: {
cache: 'memory',
ttl: 60,
},
presence: {},
ws: { ws: {
compression: true, compression: true,
encoding: 'json', encoding: 'json',
intents: 0,
largeThreshold: 250,
url: 'wss://gateway.discord.gg', url: 'wss://gateway.discord.gg',
version: 9, version: 9,
}, },

View file

@ -1,3 +1,5 @@
import type IORedis from 'ioredis';
// Interfaces // Interfaces
export interface IApiClientOptions { export interface IApiClientOptions {
offset?: number; offset?: number;
@ -8,8 +10,21 @@ export interface IApiClientOptions {
version?: number; version?: number;
} }
export interface ICacheMemoryClientOptions {
cache?: 'memory';
ttl?: number;
}
export interface ICacheRedisClientOptions {
cache?: 'redis';
ttl?: number;
options: IORedis.RedisOptions;
}
export interface IClientOptions { export interface IClientOptions {
api?: IApiClientOptions; api?: IApiClientOptions;
cache?: ICacheMemoryClientOptions | ICacheRedisClientOptions;
presence?: IUpdatePresence;
ws?: IWebSocketClientOptions; ws?: IWebSocketClientOptions;
} }
@ -56,16 +71,64 @@ export interface IRouteIdentifier {
route: string; route: string;
} }
export interface IUpdatePresence {
activities?: {
name?: string;
type?: EActivityType;
url?: string;
}[];
afk?: boolean;
since?: number;
status?: EStatus;
}
export interface IWebSocketClientOptions { export interface IWebSocketClientOptions {
compression?: boolean; compression?: boolean;
encoding?: 'json'; encoding?: 'json';
intents?: number;
largeThreshold?: number;
url?: string; url?: string;
version?: number; version?: number;
} }
// Api Rest Request Interfaces // Api Interfaces
// TODO: Add message components // TODO: Add message components
// Dont add the files option. That goes into a separate option when creating a request
export interface IApiCreateMessage { export interface IApiCreateMessage {
allowed_mentions?: {
parse?: 'everyone' | 'roles' | 'users'[];
replied_user?: boolean;
roles?: string[];
users?: string[];
};
content?: string;
embeds?: IMessageEmbed[];
message_reference?: {
channel_id?: string;
fail_if_not_exists?: boolean;
guild_id?: string;
message_id?: string;
};
tts?: boolean;
}
export interface IApiUser {
avatar: string;
bot?: boolean;
discriminator: string;
email?: string;
flags?: number;
id: string;
locale?: string;
mfa_enabled?: boolean;
premium_type?: number;
public_flags?: number;
username: string;
system?: boolean;
verified?: boolean;
}
export interface IApiCreateSlashCommand {
allowed_mentions?: { allowed_mentions?: {
parse?: 'everyone' | 'roles' | 'users'[]; parse?: 'everyone' | 'roles' | 'users'[];
replied_user?: boolean; replied_user?: boolean;
@ -85,14 +148,55 @@ export interface IApiCreateMessage {
} }
// WebSocket Gateway Message Interfaces // WebSocket Gateway Message Interfaces
export interface IGatewayMessage<T> { export interface IGatewayHeartbeatSend {
d?: T; op: 1;
op: number; d: number;
s?: number; }
t?: string;
// TODO: Add sharding when i get to it
export interface IGatewayIdentify {
op: 2;
d: {
compress: boolean;
intents: number;
large_threshold: number;
presence?: IUpdatePresence;
properties: {
$browser: string;
$device: string;
$os: string;
};
token: string;
};
}
export interface IGatewayHeartbeat {
op: 10;
d: {
heartbeat_interval: number;
};
}
export interface IGatewayHeartbeatAck {
op: 11;
} }
// Enums // Enums
export enum EActivityType {
GAME = 0,
STREAMING = 1,
LISTENING = 2,
WATCHING = 3,
COMPETING = 5,
}
export enum EStatus {
DND = 'dnd',
IDLE = 'idle',
INVISIBLE = 'invisible',
OFFLINE = 'offline',
ONLINE = 'online',
}
// Type Aliases // Type Aliases
export type ApiMethods = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT' ; export type ApiMethods = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT' ;
@ -118,4 +222,6 @@ export type DeepRequired<T> = T extends Builtin
: T extends {} : T extends {}
? { [K in keyof T]-?: DeepRequired<T[K]> } ? { [K in keyof T]-?: DeepRequired<T[K]> }
: NonNullable<T>; : NonNullable<T>;
export type GatewayReceiveMessage = IGatewayHeartbeat | IGatewayHeartbeatAck;
export type GatewaySendMessage = IGatewayHeartbeatSend | IGatewayIdentify;
export type Primitives = bigint | boolean | null | number | string | symbol | undefined; export type Primitives = bigint | boolean | null | number | string | symbol | undefined;

1030
yarn.lock

File diff suppressed because it is too large Load diff