snowtransfer
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -25,3 +25,3 @@ /** | ||
/** | ||
* Timeout that calls the reset function once the timeframe passed | ||
* The Date time in which the bucket will reset | ||
*/ | ||
@@ -28,0 +28,0 @@ resetAt: number | null; |
@@ -81,13 +81,5 @@ /// <reference types="node" /> | ||
/** | ||
* Calculate the time difference between the local server and discord | ||
* @param dateHeader Date header value returned by discord | ||
* @returns Offset in milliseconds | ||
*/ | ||
private _getOffsetDateFromHeader; | ||
/** | ||
* Apply the received ratelimit headers to the ratelimit bucket | ||
* @param bkt Ratelimit bucket to apply the headers to | ||
* @param headers Http headers received from discord | ||
* @param offsetDate Unix timestamp of the current date + offset to discord time | ||
* @param reactions Whether to use reaction ratelimits (1/250ms) | ||
*/ | ||
@@ -94,0 +86,0 @@ private _applyRatelimitHeaders; |
@@ -87,3 +87,2 @@ "use strict"; | ||
const reqID = crypto_1.default.randomBytes(20).toString("hex"); | ||
const latency = Date.now(); | ||
try { | ||
@@ -109,11 +108,7 @@ this.emit("request", reqID, { endpoint, method, dataType, data }); | ||
} | ||
if (request.headers["date"]) { | ||
this.latency = Date.now() - latency; | ||
const offsetDate = this._getOffsetDateFromHeader(request.headers["date"]); | ||
const match = endpoint.match(/\/reactions\//); | ||
this._applyRatelimitHeaders(bkt, request.headers, offsetDate, !!match); | ||
} | ||
if (request.statusCode && [429, 502].includes(request.statusCode)) { | ||
if (request.statusCode === 429) | ||
if (request.statusCode === 429) { | ||
this._applyRatelimitHeaders(bkt, request.headers); | ||
this.emit("rateLimit", { timeout: bkt.reset, limit: bkt.limit, method: method, path: endpoint, route: this.ratelimiter.routify(endpoint, method) }); | ||
} | ||
return this.request(endpoint, method, dataType, data, amount++); | ||
@@ -146,32 +141,11 @@ } | ||
/** | ||
* Calculate the time difference between the local server and discord | ||
* @param dateHeader Date header value returned by discord | ||
* @returns Offset in milliseconds | ||
*/ | ||
_getOffsetDateFromHeader(dateHeader) { | ||
const discordDate = Date.parse(dateHeader); | ||
const offset = Date.now() - discordDate; | ||
return Date.now() + offset; | ||
} | ||
/** | ||
* Apply the received ratelimit headers to the ratelimit bucket | ||
* @param bkt Ratelimit bucket to apply the headers to | ||
* @param headers Http headers received from discord | ||
* @param offsetDate Unix timestamp of the current date + offset to discord time | ||
* @param reactions Whether to use reaction ratelimits (1/250ms) | ||
*/ | ||
_applyRatelimitHeaders(bkt, headers, offsetDate, reactions = false) { | ||
_applyRatelimitHeaders(bkt, headers) { | ||
if (headers["x-ratelimit-global"]) { | ||
bkt.ratelimiter.global = true; | ||
bkt.ratelimiter.globalResetAt = Date.now() + (parseFloat(headers["retry_after"]) * 1000); | ||
bkt.ratelimiter.globalResetAt = Date.now() + (parseFloat(headers["retry-after"]) * 1000); | ||
} | ||
if (headers["x-ratelimit-reset"]) { | ||
const reset = (headers["x-ratelimit-reset"] * 1000) - offsetDate; | ||
if (reactions) { | ||
bkt.reset = Math.max(reset, 250); | ||
} | ||
else { | ||
bkt.reset = reset; | ||
} | ||
} | ||
if (headers["x-ratelimit-remaining"]) { | ||
@@ -188,2 +162,5 @@ bkt.remaining = parseInt(headers["x-ratelimit-remaining"]); | ||
} | ||
if (headers["retry-after"] && !headers["x-ratelimit-global"]) { // The ms precision is not strictly necessary. It always rounds up, which is safe. | ||
bkt.resetAt = Date.now() + (parseInt(headers["retry-after"]) * 1000); // retry-after is in seconds. | ||
} | ||
} | ||
@@ -190,0 +167,0 @@ /** |
{ | ||
"name": "snowtransfer", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Minimalistic Rest client for the Discord Api", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
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
282499
0
5740