tinify webpacks (#1975)

* tinify webpack

* meme

* fix long version

* more changes

* even smoler

* fix up logic

* fix build

* undo changes to user agent manager because its not webpack'd anymore

* the heck

* fix stupid

* clean up browser rules

* typo
This commit is contained in:
Gus Caplan
2017-09-26 00:18:12 -05:00
committed by Crawl
parent 4d4d2f2db7
commit 27ccad1f1c
21 changed files with 85 additions and 80 deletions

View File

@@ -1,5 +1,6 @@
exports.Package = require('../../package.json');
const { Error, RangeError } = require('../errors');
exports.browser = typeof window !== 'undefined';
/**
* Options for a client.
@@ -56,9 +57,9 @@ exports.DefaultOptions = {
*/
ws: {
large_threshold: 250,
compress: require('os').platform() !== 'browser',
compress: !exports.browser,
properties: {
$os: process ? process.platform : 'discord.js',
$os: exports.browser ? 'browser' : process.platform,
$browser: 'discord.js',
$device: 'discord.js',
},

View File

@@ -3,6 +3,7 @@ const fs = require('fs');
const snekfetch = require('snekfetch');
const Util = require('../util/Util');
const { Error, TypeError } = require('../errors');
const { browser } = require('../util/Constants');
/**
* The DataResolver identifies different objects and tries to resolve a specific piece of information from them.
@@ -35,15 +36,14 @@ class DataResolver {
/**
* Resolves a Base64Resolvable, a string, or a BufferResolvable to a Base 64 image.
* @param {BufferResolvable|Base64Resolvable} image The image to be resolved
* @param {boolean} browser Whether this should resolve for a browser
* @returns {Promise<?string>}
*/
static async resolveImage(image, browser) {
static async resolveImage(image) {
if (!image) return null;
if (typeof image === 'string' && image.startsWith('data:')) {
return image;
}
const file = await this.resolveFile(image, browser);
const file = await this.resolveFile(image);
return DataResolver.resolveBase64(file);
}
@@ -80,10 +80,9 @@ class DataResolver {
/**
* Resolves a BufferResolvable to a Buffer.
* @param {BufferResolvable|Stream} resource The buffer or stream resolvable to resolve
* @param {boolean} browser Whether this should resolve for a browser
* @returns {Promise<Buffer>}
*/
static resolveFile(resource, browser) {
static resolveFile(resource) {
if (resource instanceof Buffer) return Promise.resolve(resource);
if (browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource));
@@ -97,7 +96,7 @@ class DataResolver {
return resolve(res.body);
});
} else {
const file = path.resolve(resource);
const file = browser ? resource : path.resolve(resource);
fs.stat(file, (err, stats) => {
if (err) return reject(err);
if (!stats || !stats.isFile()) return reject(new Error('FILE_NOT_FOUND', file));

View File

@@ -3,6 +3,7 @@ const snekfetch = require('snekfetch');
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
/**
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
@@ -307,6 +308,14 @@ class Util {
updatedItems = updatedItems.map((r, i) => ({ id: r.id, position: i }));
return route.patch({ data: updatedItems, reason });
}
static basename(path, ext) {
let f = splitPathRe.exec(path).slice(1)[2];
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
}
}
module.exports = Util;