http rewrite (actually works this time!!1!) (#1257)

* rewrite http

* browser fun

* all the mimes dammit

* i need a newline

* whoops

* forgot about this

* use promises and HTTPRequest.method

* fluent

* move httpclient to external module

* branding

* middleware

* revert middleware
This commit is contained in:
Gus Caplan
2017-04-01 02:04:01 -05:00
committed by Crawl
parent a4e0af2e45
commit cb3f6d9646
5 changed files with 18 additions and 19 deletions

View File

@@ -35,7 +35,7 @@
"@types/node": "^7.0.0",
"long": "^3.2.0",
"prism-media": "hydrabolt/prism-media",
"superagent": "^3.4.0",
"snekfetch": "github:guscaplan/snekfetch",
"tweetnacl": "^0.14.0",
"ws": "^2.0.0"
},
@@ -66,6 +66,7 @@
"node-opus": false,
"tweetnacl": false,
"sodium": false,
"node-fetch": false,
"src/sharding/Shard.js": false,
"src/sharding/ShardClientUtil.js": false,
"src/sharding/ShardingManager.js": false,

View File

@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs');
const request = require('superagent');
const snekfetch = require('snekfetch');
const Constants = require('../util/Constants');
const convertToBuffer = require('../util/Util').convertToBuffer;
@@ -211,11 +211,9 @@ class ClientDataResolver {
if (typeof resource === 'string') {
return new Promise((resolve, reject) => {
if (/^https?:\/\//.test(resource)) {
const req = request.get(resource).set('Content-Type', 'blob');
if (this.client.browser) req.responseType('arraybuffer');
req.end((err, res) => {
snekfetch.get(resource)
.end((err, res) => {
if (err) return reject(err);
if (this.client.browser) return resolve(convertToBuffer(res.xhr.response));
if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.'));
return resolve(res.body);
});

View File

@@ -1,4 +1,4 @@
const request = require('superagent');
const snekfetch = require('snekfetch');
const Constants = require('../../util/Constants');
class APIRequest {
@@ -34,17 +34,16 @@ class APIRequest {
gen() {
const API = `${this.client.options.http.host}/api/v${this.client.options.http.version}`;
const apiRequest = request[this.method](`${API}${this.path}`);
if (this.auth) apiRequest.set('authorization', this.getAuth());
const request = snekfetch[this.method](`${API}${this.path}`);
if (this.auth) request.set('Authorization', this.getAuth());
if (!this.rest.client.browser) request.set('User-Agent', this.rest.userAgentManager.userAgent);
if (this.files) {
for (const file of this.files) if (file && file.file) apiRequest.attach(file.name, file.file, file.name);
this.data = this.data || {};
apiRequest.field('payload_json', JSON.stringify(this.data));
for (const file of this.files) if (file && file.file) request.attach(file.name, file.file, file.name);
if (typeof this.data !== 'undefined') request.attach('payload_json', JSON.stringify(this.data));
} else if (this.data) {
apiRequest.send(this.data);
request.send(this.data);
}
if (!this.client.browser) apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
return apiRequest;
return request;
}
}

View File

@@ -1,4 +1,4 @@
const superagent = require('superagent');
const snekfetch = require('snekfetch');
const Constants = require('./Constants');
/**
@@ -54,7 +54,7 @@ class Util {
static fetchRecommendedShards(token, guildsPerShard = 1000) {
return new Promise((resolve, reject) => {
if (!token) throw new Error('A token must be provided.');
superagent.get(Constants.Endpoints.gateway.bot)
snekfetch.get(Constants.Endpoints.gateway.bot)
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
.end((err, res) => {
if (err) reject(err);

View File

@@ -5,7 +5,7 @@
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript" src="../webpack/discord.10.0.1.js"></script>
<script type="text/javascript" src="../webpack/discord.js"></script>
<script type="text/javascript" src="auth.js"></script>
<script type="text/javascript">
(() => {
@@ -23,7 +23,8 @@
console.log(message.author.username, message.author.id, message.content);
});
client.login(window.token || prompt('token pls', 'abcdef123456'));
client.login(localStorage.token || window.token || prompt('token pls', 'abcdef123456'))
.then((token) => localStorage.token = token);
})();
</script>
</body>