Skip to content

Commit

Permalink
Revert "Utilize IncomingMessage and ServerResponse classes (dirigeant…
Browse files Browse the repository at this point in the history
…s#124)"

This reverts commit da7e709.
  • Loading branch information
bdistin committed May 20, 2019
1 parent da7e709 commit 3d5331b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 149 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,3 @@ docs
\.vscode/
\.idea/
desktop.ini

pnpm-lock\.yaml
6 changes: 0 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module.exports = {
DashboardClient: require('./lib/Client'),
Client: require('./lib/Client'),
Server: require('./lib/http/Server'),
KlasaIncomingMessage: require('./lib/http/KlasaIncomingMessage'),
KlasaServerResponse: require('./lib/http/KlasaServerResponse'),
Middleware: require('./lib/structures/Middleware'),
MiddlewareStore: require('./lib/structures/MiddlewareStore'),
Route: require('./lib/structures/Route'),
Expand Down Expand Up @@ -60,7 +58,3 @@ module.exports = {
* @external ServerResponse
* @see {@link https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_class_http_serverresponse}
*/
/**
* @external Socket
* @see {@link https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_class_net_socket}
*/
89 changes: 0 additions & 89 deletions src/lib/http/KlasaIncomingMessage.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/lib/http/KlasaServerResponse.js

This file was deleted.

24 changes: 18 additions & 6 deletions src/lib/http/Server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const http = require('http');
const { parse } = require('url');

const { split } = require('../util/Util');
const { METHODS_LOWER } = require('../util/constants');

/**
* The http server for klasa-dashboard-hooks
Expand Down Expand Up @@ -35,7 +39,7 @@ class Server {
* @param {DashboardClient} client The Klasa client
*/
constructor(client) {
const { http2, serverOptions } = client.options.dashboardHooks;
const { http2, sslOptions } = client.options.dashboardHooks;
/**
* The Client that manages this Server instance
* @since 0.0.1
Expand All @@ -49,8 +53,8 @@ class Server {
* @type {external:HTTPServer}
*/
this.server = http2 ?
require('http2').createSecureServer(serverOptions) :
serverOptions.cert ? require('https').createServer(serverOptions) : http.createServer(serverOptions);
require('http2').createSecureServer(sslOptions) :
sslOptions ? require('https').createServer(sslOptions) : http.createServer();

/**
* The onError function called when a url does not match
Expand Down Expand Up @@ -78,11 +82,19 @@ class Server {
* @param {external:ServerResponse} response The response
*/
async handler(request, response) {
request.init(this.client);
const info = parse(request.url, true);
const splitURL = split(info.pathname);
const route = this.client.routes.findRoute(request.method, splitURL);

if (route) request.params = route.execute(splitURL);
request.originalUrl = request.originalUrl || request.url;
request.path = info.pathname;
request.search = info.search;
request.query = info.query;

try {
await this.client.middlewares.run(request, response, request.route);
await (request.route ? request.execute(response) : this.onNoMatch(request, response));
await this.client.middlewares.run(request, response, route);
await (route ? route[METHODS_LOWER[request.method]](request, response) : this.onNoMatch(request, response));
} catch (err) {
this.client.emit('error', err);
this.onError(err, request, response);
Expand Down
9 changes: 1 addition & 8 deletions src/lib/util/constants.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const { METHODS } = require('http');

const KlasaIncomingMessage = require('../http/KlasaIncomingMessage');
const KlasaServerResponse = require('../http/KlasaServerResponse');

exports.OPTIONS = {
dashboardHooks: {
apiPrefix: 'api/',
origin: '*',
port: 4000,
http2: false,
serverOptions: {
IncomingMessage: KlasaIncomingMessage,
ServerResponse: KlasaServerResponse
}
http2: false
},
pieceDefaults: {
routes: {
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = class extends Middleware {
}

unauthorized(response) {
return response.status(401).end(RESPONSES.UNAUTHORIZED);
response.writeHead(401);
return response.end(RESPONSES.UNAUTHORIZED);
}

};
4 changes: 2 additions & 2 deletions src/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = class extends Route {
}

get(request, response) {
return response.json({
return response.end(JSON.stringify({
users: this.client.users.size,
guilds: this.client.guilds.size,
channels: this.client.channels.size,
Expand All @@ -18,7 +18,7 @@ module.exports = class extends Route {
memory: process.memoryUsage().heapUsed / 1024 / 1024,
invite: this.client.invite,
...this.client.application
});
}));
}

};
10 changes: 6 additions & 4 deletions src/routes/oauthCallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ module.exports = class extends Route {
const body = await res.json();
const user = await oauthUser.api(body.access_token);

return response.json({
return response.end(JSON.stringify({
access_token: encrypt({
token: body.access_token,
scope: [user.id, ...user.guilds.filter(guild => guild.userCanManage).map(guild => guild.id)]
}, this.client.options.clientSecret),
user
});
}));
/* eslint-enable camelcase */
}

notReady(response) {
return response.status(500).end(RESPONSES.NOT_READY);
response.writeHead(500);
return response.end(RESPONSES.NOT_READY);
}

noCode(response) {
return response.status(400).end(RESPONSES.NO_CODE);
response.writeHead(400);
return response.end(RESPONSES.NO_CODE);
}

};
2 changes: 1 addition & 1 deletion src/routes/oauthUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = class extends Route {
}, this.client.options.clientSecret));
}

return response.json(dashboardUser);
return response.end(JSON.stringify(dashboardUser));
}

async post(request, response) {
Expand Down

0 comments on commit 3d5331b

Please sign in to comment.