diff --git a/package-lock.json b/package-lock.json index 5a02c26ab..606fedfd0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,14 @@ "@types/node": "*" } }, + "@types/centra": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/centra/-/centra-2.2.0.tgz", + "integrity": "sha512-TUpM1QoIgXa2VL1LnWbTgde39+rRnsOtvb0v8ZrX8t51g8K1Wwu4HAEXcBg5a56GV5Vm/KazyE5+g4AW8YbKNg==", + "requires": { + "@types/node": "*" + } + }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", @@ -361,6 +369,11 @@ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" }, + "centra": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.4.2.tgz", + "integrity": "sha512-f1RaP0V1HqVNEXfLfjNBthB2yy3KnSGnPCnOPCFLUk9e/Z4rNJ8nBaJNnghflnp88mi1IT8mfmW+HlMS1/H+bg==" + }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", @@ -1128,11 +1141,6 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.0.0.tgz", "integrity": "sha512-sSHCgWfJ+Lui/u+0msF3oyCgvdkhxDbkCS6Q8uiJquzOimkJBvX6hl5aSSA7DR1XbMpdM8r7phjcF63sF4rkKg==" }, - "node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" - }, "node-pre-gyp": { "version": "0.15.0", "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.15.0.tgz", diff --git a/package.json b/package.json index e051ba316..84f900093 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typex", - "version": "2.1.3", + "version": "2.1.4", "scripts": { "build": "tsc -p .", "start": "node out/src" @@ -9,12 +9,14 @@ "@ayanaware/logger": "^2.2.1", "@overnightjs/core": "^1.6.15", "@types/bcrypt": "^3.0.0", + "@types/centra": "^2.2.0", "@types/cookie-parser": "^1.4.2", "@types/express-session": "^1.17.0", "@types/mime": "^2.0.1", "@types/multer": "^1.4.3", "@types/semver": "^7.3.1", "bcrypt": "^5.0.0", + "centra": "^2.4.2", "cookie-parser": "^1.4.5", "ejs": "^3.0.2", "express": "^4.17.1", @@ -22,7 +24,6 @@ "http-status-codes": "^1.4.0", "mime": "^2.4.4", "multer": "^1.4.2", - "node-fetch": "^2.6.1", "semver": "^7.3.2", "spectre.css": "^0.5.8", "typeorm": "^0.2.24" diff --git a/src/controllers/APIController.ts b/src/controllers/APIController.ts index ea46a7306..5a1bd7d40 100755 --- a/src/controllers/APIController.ts +++ b/src/controllers/APIController.ts @@ -4,7 +4,6 @@ import { Request, Response } from 'express'; import { ORMHandler } from '..'; import { randomId, getImage, findFile, getShorten, hashPassword } from '../util'; import { createReadStream, createWriteStream, unlinkSync, existsSync, mkdirSync, readFileSync } from 'fs' -import { getExtension } from 'mime'; import { User } from '../entities/User'; import { sep } from 'path'; import { cookiesForAPI } from '../middleware/cookiesForAPI'; diff --git a/src/entities/Image.ts b/src/entities/Image.ts index c62e9b218..f422ff42f 100755 --- a/src/entities/Image.ts +++ b/src/entities/Image.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; @Entity() export class Image { diff --git a/src/entities/Note.ts b/src/entities/Note.ts index 02919b051..f431394ed 100755 --- a/src/entities/Note.ts +++ b/src/entities/Note.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; @Entity() export class Note { diff --git a/src/entities/Shorten.ts b/src/entities/Shorten.ts index 05603ff37..14e539e21 100755 --- a/src/entities/Shorten.ts +++ b/src/entities/Shorten.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; @Entity() export class Shorten { diff --git a/src/entities/User.ts b/src/entities/User.ts index 243eac097..6ee0b2dd4 100755 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; import { randomId, findFile } from "../util"; import Logger from "@ayanaware/logger"; import { readFileSync } from 'fs'; diff --git a/src/index.ts b/src/index.ts index ee481f07b..65a32e5ce 100755 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,7 @@ import "./core/Console"; import { Repository, Connection, - createConnection, - ConnectionOptions + createConnection } from "typeorm"; import { User } from "./entities/User"; import { ZiplineServer } from "./server"; @@ -17,7 +16,6 @@ import { notes } from "./interval"; import { GitHub } from "./structures/GitHub"; import { compare, } from 'semver'; import chalk from 'chalk'; -import { execFileSync } from 'child_process'; if (!findFile('config.json', process.cwd())) { Logger.get('FS').error(`No config.json exists in ${__dirname}, exiting...`) @@ -59,6 +57,7 @@ const pk = JSON.parse(readFileSync(findFile('package.json', process.cwd()), 'utf note: connection.getRepository(Note) }, }; + if (orm.connection.isConnected) Logger.get(Connection).info( `Successfully initialized database type: ${config.database.type}` diff --git a/src/server.ts b/src/server.ts index 0b988331e..ff2a21407 100755 --- a/src/server.ts +++ b/src/server.ts @@ -1,6 +1,5 @@ import * as bodyParser from "body-parser"; import { Server } from "@overnightjs/core"; -import { Connection } from "typeorm"; import { ORMHandler } from "."; import Logger from "@ayanaware/logger"; import * as express from "express"; @@ -12,7 +11,6 @@ import cookies from "cookie-parser"; import { APIController } from "./controllers/APIController"; import { IndexController } from "./controllers/IndexController"; import { findFile } from "./util"; -import { ImageUtil } from "./structures/ImageUtil"; if (!findFile('config.json', process.cwd())) { Logger.get('FS').error(`No config.json exists in the ${__dirname}, exiting...`) diff --git a/src/structures/DiscordWebhook.ts b/src/structures/DiscordWebhook.ts index 5d6efd9ed..174e36a70 100755 --- a/src/structures/DiscordWebhook.ts +++ b/src/structures/DiscordWebhook.ts @@ -1,4 +1,4 @@ -import fetch from 'node-fetch'; +import req from 'centra'; import { User } from '../entities/User'; import { Shorten } from '../entities/Shorten'; import { Imaged } from './ImageUtil'; @@ -11,44 +11,38 @@ export class DiscordWebhook { this.checkExists(); } async checkExists(): Promise { - const json = await (await fetch(this.url)).json(); + const json = await (await req(this.url).send()).json(); if (json.code === 10015) throw new Error('Unknown Webhook') else if (json.code === 50027) throw new Error('Invalid Webhook Token') else if (json.code) throw new Error(`DiscordAPIError[${json.code}]: ${json.message}`); return json.code ? false : true; } async sendImageUpdate(user: User, image: Imaged, config: any) { - try { - await (await fetch(this.url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - user: config.discordWebhook.username, - avatar_url: config.discordWebhook.avatarURL, - content: `New image uploaded to <${image.origin}> by ${user.username} (${user.id}). ${image.url})` - }) - })); - } catch (e) { - throw new Error(`Coulndn't send webhook: ${e.message}`) - } + const res = await req(this.url, 'POST') + .header({ + 'Content-Type': 'application/json' + }) + .body({ + user: config.discordWebhook.username, + avatar_url: config.discordWebhook.avatarURL, + content: `New image uploaded to <${image.origin}> by ${user.username} (${user.id}). ${image.url})` + }) + .send(); + + if (res.statusCode !== 200) throw new Error(`Couldn't send webhook. (Status: ${res.statusCode})`); } async sendShortenUpdate(user: User, shorten: Shorten, ex: Shortened, config: any) { - try { - await (await fetch(this.url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - user: config.discordWebhook.username, - avatar_url: config.discordWebhook.avatarURL, - content: `New shortened url added to <${ex.origin}> by ${user.username} (${user.id}). <${shorten.origin}> -> <${shorten.url}>` - }) - })); - } catch (e) { - throw new Error(`Coulndn't send webhook: ${e.message}`) - } + const res = await req(this.url, 'POST') + .header({ + 'Content-Type': 'application/json' + }) + .body({ + user: config.discordWebhook.username, + avatar_url: config.discordWebhook.avatarURL, + content: `New shortened url added to <${ex.origin}> by ${user.username} (${user.id}). <${shorten.origin}> -> <${shorten.url}>` + }) + .send(); + + if (res.statusCode !== 200) throw new Error(`Couldn't send webhook. (Status: ${res.statusCode})`); } } \ No newline at end of file diff --git a/src/structures/GitHub.ts b/src/structures/GitHub.ts index fa37fe72e..1adf473a5 100755 --- a/src/structures/GitHub.ts +++ b/src/structures/GitHub.ts @@ -1,8 +1,8 @@ -import fetch from 'node-fetch'; +import req from 'centra'; export class GitHub { public static async getFile(filePath: string) { - const res = await fetch('https://raw.githubusercontent.com/ZiplineProject/Zipline/master/' + filePath); + const res = await req(`https://raw.githubusercontent.com/ZiplineProject/Zipline/master/${filePath}`).send(); return res.text(); } } \ No newline at end of file