diff --git a/package.json b/package.json index 2dc656e58..c237ec8d6 100644 --- a/package.json +++ b/package.json @@ -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, diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 6fc724f78..c44ea5b34 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -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); }); diff --git a/src/client/rest/APIRequest.js b/src/client/rest/APIRequest.js index 537cb6443..518017ae3 100644 --- a/src/client/rest/APIRequest.js +++ b/src/client/rest/APIRequest.js @@ -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; } } diff --git a/src/util/Util.js b/src/util/Util.js index 7eea58cde..6027c637e 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -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); diff --git a/test/webpack.html b/test/webpack.html index 967712e33..19c1b803e 100644 --- a/test/webpack.html +++ b/test/webpack.html @@ -5,7 +5,7 @@ - +