cloudstorm
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -37,3 +37,7 @@ "use strict"; | ||
intents: 0, | ||
token: "" | ||
token: "", | ||
ws: { | ||
compress: true, | ||
socket: undefined | ||
} | ||
}; | ||
@@ -169,5 +173,6 @@ this._restClient = options.snowtransferInstance ? options.snowtransferInstance : new snowtransfer_1.SnowTransfer(token); | ||
_updateEndpoint(gatewayUrl) { | ||
this.options.endpoint = `${gatewayUrl}?v=${Constants_1.default.GATEWAY_VERSION}&encoding=${Erlpack ? "etf" : "json"}&compress=zlib-stream`; | ||
var _a; | ||
this.options.endpoint = `${gatewayUrl}?v=${Constants_1.default.GATEWAY_VERSION}&encoding=${Erlpack ? "etf" : "json"}${((_a = this.options.ws) === null || _a === void 0 ? void 0 : _a.compress) ? "&compress=zlib-stream" : ""}`; | ||
} | ||
} | ||
module.exports = Client; |
@@ -44,7 +44,7 @@ "use strict"; | ||
if (!this.betterWs) { | ||
this.betterWs = new BetterWs_1.default(this.options.endpoint); | ||
this.betterWs = new BetterWs_1.default(this.options.endpoint, this.options.ws); | ||
} | ||
else { | ||
this.betterWs.removeAllListeners(); | ||
this.betterWs.recreateWs(this.options.endpoint); | ||
this.betterWs.recreateWs(this.options.endpoint, this.options.ws); | ||
} | ||
@@ -51,0 +51,0 @@ this.betterWs.on("ws_open", () => { |
@@ -36,8 +36,9 @@ /// <reference types="node" /> | ||
presenceBucket: RatelimitBucket; | ||
zlibInflate: zlib.Inflate; | ||
options: WebSocket.ClientOptions; | ||
zlibInflate: zlib.Inflate | null; | ||
options: import("../Types").IClientWSOptions; | ||
compress: boolean; | ||
/** | ||
* Create a new BetterWs instance. | ||
*/ | ||
constructor(address: string, options?: import("ws").ClientOptions); | ||
constructor(address: string, options?: import("../Types").IClientWSOptions); | ||
/** | ||
@@ -57,3 +58,3 @@ * Get the raw websocket connection currently used. | ||
*/ | ||
recreateWs(address: string, options?: import("ws").ClientOptions): void; | ||
recreateWs(address: string, options?: import("../Types").IClientWSOptions): void; | ||
/** | ||
@@ -60,0 +61,0 @@ * Called upon opening of the websocket connection. |
@@ -26,7 +26,14 @@ "use strict"; | ||
super(); | ||
this.ws = new ws_1.default(address, options); | ||
this.zlibInflate = null; | ||
this.ws = new ws_1.default(address, options.socket); | ||
this.bindWs(this.ws); | ||
this.wsBucket = new RatelimitBucket_1.default(120, 60000); | ||
this.presenceBucket = new RatelimitBucket_1.default(5, 20000); | ||
this.zlibInflate = new zlib_sync_1.default.Inflate({ chunkSize: 65535 }); | ||
if (options.compress) { | ||
this.zlibInflate = new zlib_sync_1.default.Inflate({ chunkSize: 65535 }); | ||
this.compress = true; | ||
} | ||
else | ||
this.compress = false; | ||
this.options = options; | ||
} | ||
@@ -60,6 +67,14 @@ /** | ||
this.ws.removeAllListeners(); | ||
this.zlibInflate = new zlib_sync_1.default.Inflate({ chunkSize: 65535 }); | ||
this.ws = new ws_1.default(address, options); | ||
if (options.compress) { | ||
this.zlibInflate = new zlib_sync_1.default.Inflate({ chunkSize: 65535 }); | ||
this.compress = true; | ||
} | ||
else { | ||
this.zlibInflate = null; | ||
this.compress = false; | ||
} | ||
this.ws = new ws_1.default(address, options.socket); | ||
this.options = options; | ||
this.wsBucket.dropQueue(); | ||
this.presenceBucket.dropQueue(); | ||
this.wsBucket = new RatelimitBucket_1.default(120, 60000); | ||
@@ -83,16 +98,22 @@ this.presenceBucket = new RatelimitBucket_1.default(5, 60000); | ||
try { | ||
const length = message.length; | ||
const flush = length >= 4 && | ||
message[length - 4] === 0x00 && | ||
message[length - 3] === 0x00 && | ||
message[length - 2] === 0xFF && | ||
message[length - 1] === 0xFF; | ||
this.zlibInflate.push(message, flush ? zlib_sync_1.default.Z_SYNC_FLUSH : false); | ||
if (!flush) | ||
return; | ||
let msg; | ||
if (this.compress && this.zlibInflate) { | ||
const length = message.length; | ||
const flush = length >= 4 && | ||
message[length - 4] === 0x00 && | ||
message[length - 3] === 0x00 && | ||
message[length - 2] === 0xFF && | ||
message[length - 1] === 0xFF; | ||
this.zlibInflate.push(message, flush ? zlib_sync_1.default.Z_SYNC_FLUSH : false); | ||
if (!flush) | ||
return; | ||
msg = this.zlibInflate.result; | ||
} | ||
else | ||
msg = message; | ||
if (Erlpack) { | ||
parsed = Erlpack.unpack(this.zlibInflate.result); | ||
parsed = Erlpack.unpack(msg); | ||
} | ||
else { | ||
parsed = JSON.parse(String(this.zlibInflate.result)); | ||
parsed = JSON.parse(String(msg)); | ||
} | ||
@@ -99,0 +120,0 @@ } |
@@ -9,2 +9,3 @@ /// <reference types="node" /> | ||
callback: () => any; | ||
error: Error; | ||
}>; | ||
@@ -11,0 +12,0 @@ limit: number; |
@@ -24,2 +24,4 @@ "use strict"; | ||
queue(fn) { | ||
// More debug-ability | ||
const error = new Error("An Error occurred in the bucket queue"); | ||
return new Promise((res, rej) => { | ||
@@ -29,9 +31,23 @@ const wrapFn = () => { | ||
if (!this.resetTimeout) { | ||
this.resetTimeout = setTimeout(() => this.resetRemaining(), this.limitReset); | ||
this.resetTimeout = setTimeout(() => { | ||
try { | ||
this.resetRemaining(); | ||
} | ||
catch (e) { | ||
rej(e); | ||
} | ||
}, this.limitReset); | ||
} | ||
if (this.remaining !== 0) { | ||
this.checkQueue(); | ||
this.checkQueue().catch(rej); | ||
} | ||
if (fn instanceof Promise) { | ||
return fn.then(res).catch(rej); | ||
return fn.then(res).catch((e) => { | ||
if (e) { | ||
e.stack = error.stack; | ||
return rej(e); | ||
} | ||
else | ||
return rej(error); | ||
}); | ||
} | ||
@@ -42,5 +58,5 @@ return res(fn()); | ||
this.fnQueue.push({ | ||
fn, callback: wrapFn | ||
fn, callback: wrapFn, error | ||
}); | ||
this.checkQueue(); | ||
this.checkQueue().catch(rej); | ||
} | ||
@@ -55,6 +71,16 @@ else { | ||
*/ | ||
checkQueue() { | ||
async checkQueue() { | ||
if (this.fnQueue.length > 0 && this.remaining !== 0) { | ||
const queuedFunc = this.fnQueue.splice(0, 1)[0]; | ||
queuedFunc.callback(); | ||
try { | ||
queuedFunc.callback(); | ||
} | ||
catch (e) { | ||
if (e) { | ||
e.stack = queuedFunc.error.stack; | ||
throw e; | ||
} | ||
else | ||
throw queuedFunc.error; | ||
} | ||
} | ||
@@ -61,0 +87,0 @@ } |
@@ -50,2 +50,3 @@ import Constants from "./Constants"; | ||
snowtransferInstance?: import("snowtransfer").SnowTransfer; | ||
ws?: IClientWSOptions; | ||
} | ||
@@ -67,1 +68,5 @@ export interface IVoiceStateUpdate { | ||
} | ||
export interface IClientWSOptions { | ||
compress?: boolean; | ||
socket?: import("ws").ClientOptions; | ||
} |
{ | ||
"name": "cloudstorm", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Minimalistic Discord Gateway library", | ||
@@ -24,4 +24,4 @@ "main": "./dist/index.js", | ||
"@types/ws": "^8.2.0", | ||
"@typescript-eslint/eslint-plugin": "^4.32.0", | ||
"@typescript-eslint/parser": "^4.32.0", | ||
"@typescript-eslint/eslint-plugin": "^4.33.0", | ||
"@typescript-eslint/parser": "^4.33.0", | ||
"eslint": "^7.32.0", | ||
@@ -28,0 +28,0 @@ "typedoc": "^0.22.5", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
102433
2094
0