Skip to content

Commit

Permalink
chore: actually add files
Browse files Browse the repository at this point in the history
  • Loading branch information
Deivu committed Aug 14, 2024
1 parent 2679a30 commit 6c1ae48
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
7 changes: 1 addition & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
const { readJSONSync } = require('fs-extra');
const config = readJSONSync('./config.json');
const Formidable = require('./src/Formidable.js');

const host = process.env.HOST ?? config.host ?? '0.0.0.0';
const port = process.env.PORT ?? config.port;

new Formidable()
.load()
.listen(host, port);
.listen();
18 changes: 18 additions & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const port = !isNaN(Number(process.env.PORT)) ? Number(process.env.PORT) : 1024;
const distance = !isNaN(Number(process.env.DISTANCE)) ? Number(process.env.DISTANCE) : 0.5;
const threads = !isNaN(Number(process.env.THREADS)) ? Number(process.env.THREADS) : 'auto';
const maxQueue = !isNaN(Number(process.env.MAX_QUEUE)) ? Number(process.env.MAX_QUEUE) : 'auto';
const maxResults = !isNaN(Number(process.env.MAX_RESULTS)) ? Number(process.env.MAX_RESULTS) : 10;
const autoUpdateInterval = !isNaN(Number(process.env.AUTO_UPDATE_INTERVAL)) ? Number(process.env.AUTO_UPDATE_INTERVAL) : 10;

module.exports = {
level: process.env.LEVEL ?? 'info',
host: process.env.HOST ?? '0.0.0.0',
auth: process.env.AUTH,
port,
distance,
threads,
maxQueue,
maxResults,
autoUpdateInterval
};
33 changes: 18 additions & 15 deletions src/Formidable.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
const { readdirSync } = require('fs-extra');

const Os = require('os');
const Server = require('fastify');

const Static = require('@fastify/static');
const Cors = require('@fastify/cors');
const Compress = require('@fastify/compress');

const Pino = require('pino');
const Piscina = require('piscina');
const Path = require('path');

const Limiter = require('./struct/Limiter.js');
const { readdirSync, readJSONSync } = require('fs-extra');
const { workers, maxQueue, abortTimeout } = require('./struct/Utils.js');
const { level } = readJSONSync('./config.json');
const Config = require('./Config.js');

const config = readJSONSync('./config.json');
const authorization = process.env.AUTHORIZATION ?? config.auth;
const { abortTimeout } = require('./struct/Utils.js');

class Formidable {
constructor() {
this.logger = new Pino(
{ name: `Formidable [${process.pid}]`, level: level || 'info' },
{ name: `Formidable [${process.pid}]`, level: Config.level },
Pino.destination({ sync: false })
);
this.pool = new Piscina({
filename: `${__dirname}/FormidableWorker.js`,
idleTimeout: 30000,
minThreads: 0,
maxThreads: workers,
maxQueue
maxThreads: Config.threads === 'auto' ? Os.cpus().length : Config.threads,
maxQueue: Config.maxQueue
});
this.endpoints = [];
this.pool.on('error', error => this.logger.error(error));
this.logger.info(`[Server] Using ${workers} workers`);
this.logger.info(`[Server] Using ${this.pool.maxThreads} workers`);
this.server = Server();
this.server.addContentTypeParser('*', (_, body, done) => done(null, body));
this.server
Expand All @@ -40,10 +43,10 @@ class Formidable {
async handle(command, endpoint, request, reply) {
try {
let body = request.query;
if (!authorization && command.locked) {
if (!Config.auth && command.locked) {
this.logger.warn(`[Server] ${endpoint} requires an auth, but user didn't set "auth" on config or AUTH in process enviroment variables`);
}
if (authorization && command.locked && authorization !== request.headers?.authorization) {
if (Config.auth && command.locked && Config.auth !== request.headers?.authorization) {
reply.code(401);
return 'Unauthorized';
}
Expand Down Expand Up @@ -183,10 +186,10 @@ class Formidable {
return this;
}

async listen(host, port) {
if (!port) this.logger.warn('[Server] User didn\'t set a port, will use a random open port');
if (!authorization) this.logger.warn('[Server] User didn\'t set an auth, locked endpoints will execute without authorization');
const address = await this.server.listen({ host, port });
async listen() {
if (!Config.port) this.logger.warn('[Server] User didn\'t set a port, will use a random open port');
if (!Config.auth) this.logger.warn('[Server] User didn\'t set an auth, locked endpoints will execute without authorization');
const address = await this.server.listen({ host: Config.host, port: Config.port });
this.logger.info(`[Server] Server Loaded, Listening at ${address}`);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/struct/Cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { watchFile } = require('fs');
const { readJSONSync } = require('fs-extra');
const { FILES, SEARCH, DIRECTORY, Create } = require('./Updater.js');
const { distance } = readJSONSync('./config.json');

const Config = require('../Config.js');

const Fuse = require('fuse.js');

Expand All @@ -28,7 +29,7 @@ class Cache {
this.data[key.toLowerCase()] = data;
const keys = SEARCH[key];
if (!keys) return;
const dist = distance || 0.5;
const dist = Config.distance;
this.fuse[key.toLowerCase()] = new Fuse(this.data[key.toLowerCase()], { keys, distance: dist });
}

Expand Down
5 changes: 0 additions & 5 deletions src/struct/Utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const Https = require('https');
const { cpus } = require('os');
const { readJSONSync } = require('fs-extra');
const { threads, maxQueue } = readJSONSync('./config.json');

module.exports = {
workers: isNaN(threads) ? Math.floor(50 * cpus().length / 100) : Number(threads),
maxQueue: maxQueue || 'auto',
abortTimeout: 10000,
fetch: (url, options) => new Promise((resolve, reject) => {
const request = Https.request(url, options, response => {
Expand Down

0 comments on commit 6c1ae48

Please sign in to comment.