Add files via upload
This commit is contained in:
76
node_modules/eris/lib/util/BrowserWebSocket.js
generated
vendored
Normal file
76
node_modules/eris/lib/util/BrowserWebSocket.js
generated
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
const util = require("util");
|
||||
const Base = require("../structures/Base");
|
||||
|
||||
let EventEmitter;
|
||||
try {
|
||||
EventEmitter = require("eventemitter3");
|
||||
} catch(err) {
|
||||
EventEmitter = require("events").EventEmitter;
|
||||
}
|
||||
|
||||
class BrowserWebSocketError extends Error {
|
||||
constructor(message, event) {
|
||||
super(message);
|
||||
this.event = event;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a browser's websocket usable by Eris
|
||||
* @extends EventEmitter
|
||||
* @prop {String} url The URL to connect to
|
||||
*/
|
||||
class BrowserWebSocket extends EventEmitter {
|
||||
constructor(url) {
|
||||
super();
|
||||
|
||||
if(typeof window === "undefined") {
|
||||
throw new Error("BrowserWebSocket cannot be used outside of a browser environment");
|
||||
}
|
||||
|
||||
this._ws = new window.WebSocket(url);
|
||||
this._ws.onopen = () => this.emit("open");
|
||||
this._ws.onmessage = this._onMessage.bind(this);
|
||||
this._ws.onerror = (event) => this.emit("error", new BrowserWebSocketError("Unknown error", event));
|
||||
this._ws.onclose = (event) => this.emit("close", event.code, event.reason);
|
||||
}
|
||||
|
||||
get readyState() {
|
||||
return this._ws.readyState;
|
||||
}
|
||||
|
||||
close(code, reason) {
|
||||
return this._ws.close(code, reason);
|
||||
}
|
||||
|
||||
removeEventListener(type, listener) {
|
||||
return this.removeListener(type, listener);
|
||||
}
|
||||
|
||||
send(data) {
|
||||
return this._ws.send(data);
|
||||
}
|
||||
|
||||
terminate() {
|
||||
return this._ws.close();
|
||||
}
|
||||
|
||||
async _onMessage(event) {
|
||||
if(event.data instanceof window.Blob) {
|
||||
this.emit("message", await event.data.arrayBuffer());
|
||||
} else {
|
||||
this.emit("message", event.data);
|
||||
}
|
||||
}
|
||||
|
||||
[util.inspect.custom]() {
|
||||
return Base.prototype[util.inspect.custom].call(this);
|
||||
}
|
||||
}
|
||||
|
||||
BrowserWebSocket.CONNECTING = 0;
|
||||
BrowserWebSocket.OPEN = 1;
|
||||
BrowserWebSocket.CLOSING = 2;
|
||||
BrowserWebSocket.CLOSED = 3;
|
||||
|
||||
module.exports = BrowserWebSocket;
|
89
node_modules/eris/lib/util/Bucket.js
generated
vendored
Normal file
89
node_modules/eris/lib/util/Bucket.js
generated
vendored
Normal file
@ -0,0 +1,89 @@
|
||||
"use strict";
|
||||
|
||||
const util = require("util");
|
||||
const Base = require("../structures/Base");
|
||||
|
||||
/**
|
||||
* Handle ratelimiting something
|
||||
* @prop {Number} interval How long (in ms) to wait between clearing used tokens
|
||||
* @prop {Number} lastReset Timestamp of last token clearing
|
||||
* @prop {Number} lastSend Timestamp of last token consumption
|
||||
* @prop {Number} tokenLimit The max number tokens the bucket can consume per interval
|
||||
* @prop {Number} tokens How many tokens the bucket has consumed in this interval
|
||||
*/
|
||||
class Bucket {
|
||||
/**
|
||||
* Construct a Bucket
|
||||
* @arg {Number} tokenLimit The max number of tokens the bucket can consume per interval
|
||||
* @arg {Number} interval How long (in ms) to wait between clearing used tokens
|
||||
* @arg {Object} [options] Optional parameters
|
||||
* @arg {Object} options.latencyRef A latency reference object
|
||||
* @arg {Number} options.latencyRef.latency Interval between consuming tokens
|
||||
* @arg {Number} options.reservedTokens How many tokens to reserve for priority operations
|
||||
*/
|
||||
constructor(tokenLimit, interval, options = {}) {
|
||||
this.tokenLimit = tokenLimit;
|
||||
this.interval = interval;
|
||||
this.latencyRef = options.latencyRef || {latency: 0};
|
||||
this.lastReset = this.tokens = this.lastSend = 0;
|
||||
this.reservedTokens = options.reservedTokens || 0;
|
||||
this._queue = [];
|
||||
}
|
||||
|
||||
check() {
|
||||
if(this.timeout || this._queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
if(this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency < Date.now()) {
|
||||
this.lastReset = Date.now();
|
||||
this.tokens = Math.max(0, this.tokens - this.tokenLimit);
|
||||
}
|
||||
|
||||
let val;
|
||||
let tokensAvailable = this.tokens < this.tokenLimit;
|
||||
let unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
||||
while(this._queue.length > 0 && (unreservedTokensAvailable || (tokensAvailable && this._queue[0].priority))) {
|
||||
this.tokens++;
|
||||
tokensAvailable = this.tokens < this.tokenLimit;
|
||||
unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
||||
const item = this._queue.shift();
|
||||
val = this.latencyRef.latency - Date.now() + this.lastSend;
|
||||
if(this.latencyRef.latency === 0 || val <= 0) {
|
||||
item.func();
|
||||
this.lastSend = Date.now();
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
item.func();
|
||||
}, val);
|
||||
this.lastSend = Date.now() + val;
|
||||
}
|
||||
}
|
||||
|
||||
if(this._queue.length > 0 && !this.timeout) {
|
||||
this.timeout = setTimeout(() => {
|
||||
this.timeout = null;
|
||||
this.check();
|
||||
}, this.tokens < this.tokenLimit ? this.latencyRef.latency : Math.max(0, this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency - Date.now()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue something in the Bucket
|
||||
* @arg {Function} func A callback to call when a token can be consumed
|
||||
* @arg {Boolean} [priority=false] Whether or not the callback should use reserved tokens
|
||||
*/
|
||||
queue(func, priority=false) {
|
||||
if(priority) {
|
||||
this._queue.unshift({func, priority});
|
||||
} else {
|
||||
this._queue.push({func, priority});
|
||||
}
|
||||
this.check();
|
||||
}
|
||||
|
||||
[util.inspect.custom]() {
|
||||
return Base.prototype[util.inspect.custom].call(this);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Bucket;
|
202
node_modules/eris/lib/util/Collection.js
generated
vendored
Normal file
202
node_modules/eris/lib/util/Collection.js
generated
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Hold a bunch of something
|
||||
* @extends Map
|
||||
* @prop {Class} baseObject The base class for all items
|
||||
* @prop {Number?} limit Max number of items to hold
|
||||
*/
|
||||
class Collection extends Map {
|
||||
/**
|
||||
* Construct a Collection
|
||||
* @arg {Class} baseObject The base class for all items
|
||||
* @arg {Number} [limit] Max number of items to hold
|
||||
*/
|
||||
constructor(baseObject, limit) {
|
||||
super();
|
||||
this.baseObject = baseObject;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an object
|
||||
* @arg {Object} obj The updated object data
|
||||
* @arg {String} obj.id The ID of the object
|
||||
* @arg {Class} [extra] An extra parameter the constructor may need
|
||||
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
||||
* @returns {Class} The updated object
|
||||
*/
|
||||
update(obj, extra, replace) {
|
||||
if(!obj.id && obj.id !== 0) {
|
||||
throw new Error("Missing object id");
|
||||
}
|
||||
const item = this.get(obj.id);
|
||||
if(!item) {
|
||||
return this.add(obj, extra, replace);
|
||||
}
|
||||
item.update(obj, extra);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an object
|
||||
* @arg {Object} obj The object data
|
||||
* @arg {String} obj.id The ID of the object
|
||||
* @arg {Class} [extra] An extra parameter the constructor may need
|
||||
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
||||
* @returns {Class} The existing or newly created object
|
||||
*/
|
||||
add(obj, extra, replace) {
|
||||
if(this.limit === 0) {
|
||||
return (obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name) ? obj : new this.baseObject(obj, extra);
|
||||
}
|
||||
if(obj.id == null) {
|
||||
throw new Error("Missing object id");
|
||||
}
|
||||
const existing = this.get(obj.id);
|
||||
if(existing && !replace) {
|
||||
return existing;
|
||||
}
|
||||
if(!(obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name)) {
|
||||
obj = new this.baseObject(obj, extra);
|
||||
}
|
||||
|
||||
this.set(obj.id, obj);
|
||||
|
||||
if(this.limit && this.size > this.limit) {
|
||||
const iter = this.keys();
|
||||
while(this.size > this.limit) {
|
||||
this.delete(iter.next().value);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all elements satisfy the condition
|
||||
* @arg {Function} func A function that takes an object and returns true or false
|
||||
* @returns {Boolean} Whether or not all elements satisfied the condition
|
||||
*/
|
||||
every(func) {
|
||||
for(const item of this.values()) {
|
||||
if(!func(item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the objects that make the function evaluate true
|
||||
* @arg {Function} func A function that takes an object and returns true if it matches
|
||||
* @returns {Array<Class>} An array containing all the objects that matched
|
||||
*/
|
||||
filter(func) {
|
||||
const arr = [];
|
||||
for(const item of this.values()) {
|
||||
if(func(item)) {
|
||||
arr.push(item);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first object to make the function evaluate true
|
||||
* @arg {Function} func A function that takes an object and returns true if it matches
|
||||
* @returns {Class?} The first matching object, or undefined if no match
|
||||
*/
|
||||
find(func) {
|
||||
for(const item of this.values()) {
|
||||
if(func(item)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array with the results of applying the given function to each element
|
||||
* @arg {Function} func A function that takes an object and returns something
|
||||
* @returns {Array} An array containing the results
|
||||
*/
|
||||
map(func) {
|
||||
const arr = [];
|
||||
for(const item of this.values()) {
|
||||
arr.push(func(item));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random object from the Collection
|
||||
* @returns {Class?} The random object, or undefined if there is no match
|
||||
*/
|
||||
random() {
|
||||
const index = Math.floor(Math.random() * this.size);
|
||||
const iter = this.values();
|
||||
for(let i = 0; i < index; ++i) {
|
||||
iter.next();
|
||||
}
|
||||
return iter.next().value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value resulting from applying a function to every element of the collection
|
||||
* @arg {Function} func A function that takes the previous value and the next item and returns a new value
|
||||
* @arg {any} [initialValue] The initial value passed to the function
|
||||
* @returns {any} The final result
|
||||
*/
|
||||
reduce(func, initialValue) {
|
||||
const iter = this.values();
|
||||
let val;
|
||||
let result = initialValue === undefined ? iter.next().value : initialValue;
|
||||
while((val = iter.next().value) !== undefined) {
|
||||
result = func(result, val);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an object
|
||||
* @arg {Object} obj The object
|
||||
* @arg {String} obj.id The ID of the object
|
||||
* @returns {Class?} The removed object, or null if nothing was removed
|
||||
*/
|
||||
remove(obj) {
|
||||
const item = this.get(obj.id);
|
||||
if(!item) {
|
||||
return null;
|
||||
}
|
||||
this.delete(obj.id);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if at least one element satisfies the condition
|
||||
* @arg {Function} func A function that takes an object and returns true or false
|
||||
* @returns {Boolean} Whether or not at least one element satisfied the condition
|
||||
*/
|
||||
some(func) {
|
||||
for(const item of this.values()) {
|
||||
if(func(item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `[Collection<${this.baseObject.name}>]`;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const json = {};
|
||||
for(const item of this.values()) {
|
||||
json[item.id] = item;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Collection;
|
66
node_modules/eris/lib/util/MultipartData.js
generated
vendored
Normal file
66
node_modules/eris/lib/util/MultipartData.js
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
"use strict";
|
||||
|
||||
class MultipartData {
|
||||
constructor() {
|
||||
this.boundary = "----------------Eris";
|
||||
this.bufs = [];
|
||||
}
|
||||
|
||||
attach(fieldName, data, filename) {
|
||||
if(data === undefined) {
|
||||
return;
|
||||
}
|
||||
let str = "\r\n--" + this.boundary + "\r\nContent-Disposition: form-data; name=\"" + fieldName + "\"";
|
||||
let contentType;
|
||||
if(filename) {
|
||||
str += "; filename=\"" + filename + "\"";
|
||||
const extension = filename.match(/\.(png|apng|gif|jpg|jpeg|webp|svg|json)$/i);
|
||||
if(extension) {
|
||||
let ext = extension[1].toLowerCase();
|
||||
switch(ext) {
|
||||
case "png":
|
||||
case "apng":
|
||||
case "gif":
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
case "webp":
|
||||
case "svg": {
|
||||
if(ext === "svg") {
|
||||
ext = "svg+xml";
|
||||
}
|
||||
contentType = "image/";
|
||||
break;
|
||||
}
|
||||
case "json": {
|
||||
contentType = "application/";
|
||||
break;
|
||||
}
|
||||
}
|
||||
contentType += ext;
|
||||
}
|
||||
}
|
||||
|
||||
if(contentType) {
|
||||
str += `\r\nContent-Type: ${contentType}`;
|
||||
} else if(ArrayBuffer.isView(data)) {
|
||||
str +="\r\nContent-Type: application/octet-stream";
|
||||
if(!(data instanceof Uint8Array)) {
|
||||
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
||||
}
|
||||
} else if(typeof data === "object") {
|
||||
str +="\r\nContent-Type: application/json";
|
||||
data = Buffer.from(JSON.stringify(data));
|
||||
} else {
|
||||
data = Buffer.from("" + data);
|
||||
}
|
||||
this.bufs.push(Buffer.from(str + "\r\n\r\n"));
|
||||
this.bufs.push(data);
|
||||
}
|
||||
|
||||
finish() {
|
||||
this.bufs.push(Buffer.from("\r\n--" + this.boundary + "--"));
|
||||
return this.bufs;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MultipartData;
|
34
node_modules/eris/lib/util/Opus.js
generated
vendored
Normal file
34
node_modules/eris/lib/util/Opus.js
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
|
||||
let NativeOpus;
|
||||
let OpusScript;
|
||||
|
||||
module.exports.createOpus = function createOpus(samplingRate, channels, bitrate) {
|
||||
if(!NativeOpus && !OpusScript) {
|
||||
try {
|
||||
NativeOpus = require("@discordjs/opus");
|
||||
} catch(err) {
|
||||
try {
|
||||
OpusScript = require("opusscript");
|
||||
} catch(err) { // eslint-disable no-empty
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let opus;
|
||||
if(NativeOpus) {
|
||||
opus = new NativeOpus.OpusEncoder(samplingRate, channels);
|
||||
} else if(OpusScript) {
|
||||
opus = new OpusScript(samplingRate, channels, OpusScript.Application.AUDIO);
|
||||
} else {
|
||||
throw new Error("No opus encoder found, playing non-opus audio will not work.");
|
||||
}
|
||||
|
||||
if(opus.setBitrate) {
|
||||
opus.setBitrate(bitrate);
|
||||
} else if(opus.encoderCTL) {
|
||||
opus.encoderCTL(4002, bitrate);
|
||||
}
|
||||
|
||||
return opus;
|
||||
};
|
83
node_modules/eris/lib/util/SequentialBucket.js
generated
vendored
Normal file
83
node_modules/eris/lib/util/SequentialBucket.js
generated
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
"use strict";
|
||||
|
||||
const util = require("util");
|
||||
const Base = require("../structures/Base");
|
||||
|
||||
/**
|
||||
* Ratelimit requests and release in sequence
|
||||
* TODO: add latencyref
|
||||
* @prop {Number} limit How many tokens the bucket can consume in the current interval
|
||||
* @prop {Boolean} processing Whether the queue is being processed
|
||||
* @prop {Number} remaining How many tokens the bucket has left in the current interval
|
||||
* @prop {Number} reset Timestamp of next reset
|
||||
*/
|
||||
class SequentialBucket {
|
||||
/**
|
||||
* Construct a SequentialBucket
|
||||
* @arg {Number} limit The max number of tokens the bucket can consume per interval
|
||||
* @arg {Object} [latencyRef] An object
|
||||
* @arg {Number} latencyRef.latency Interval between consuming tokens
|
||||
*/
|
||||
constructor(limit, latencyRef = {latency: 0}) {
|
||||
this.limit = this.remaining = limit;
|
||||
this.reset = 0;
|
||||
this.processing = false;
|
||||
this.latencyRef = latencyRef;
|
||||
this._queue = [];
|
||||
}
|
||||
|
||||
check(override) {
|
||||
if(this._queue.length === 0) {
|
||||
if(this.processing) {
|
||||
clearTimeout(this.processing);
|
||||
this.processing = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(this.processing && !override) {
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
const offset = this.latencyRef.latency;
|
||||
if(!this.reset || this.reset < now - offset) {
|
||||
this.reset = now - offset;
|
||||
this.remaining = this.limit;
|
||||
}
|
||||
this.last = now;
|
||||
if(this.remaining <= 0) {
|
||||
this.processing = setTimeout(() => {
|
||||
this.processing = false;
|
||||
this.check(true);
|
||||
}, Math.max(0, (this.reset || 0) - now + offset) + 1);
|
||||
return;
|
||||
}
|
||||
--this.remaining;
|
||||
this.processing = true;
|
||||
this._queue.shift()(() => {
|
||||
if(this._queue.length > 0) {
|
||||
this.check(true);
|
||||
} else {
|
||||
this.processing = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue something in the SequentialBucket
|
||||
* @arg {Function} func A function to call when a token can be consumed. The function will be passed a callback argument, which must be called to allow the bucket to continue to work
|
||||
*/
|
||||
queue(func, short) {
|
||||
if(short) {
|
||||
this._queue.unshift(func);
|
||||
} else {
|
||||
this._queue.push(func);
|
||||
}
|
||||
this.check();
|
||||
}
|
||||
|
||||
[util.inspect.custom]() {
|
||||
return Base.prototype[util.inspect.custom].call(this);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SequentialBucket;
|
Reference in New Issue
Block a user