feat: parseCookie
This commit is contained in:
parent
d3b5cbe1e3
commit
bd2475b3c6
@ -66,6 +66,7 @@
|
|||||||
"json-bigint": "^1.0.0",
|
"json-bigint": "^1.0.0",
|
||||||
"node-fetch": "^2.6.9",
|
"node-fetch": "^2.6.9",
|
||||||
"safe-base64": "^2.0.1-0",
|
"safe-base64": "^2.0.1-0",
|
||||||
|
"set-cookie-parser": "^2.6.0",
|
||||||
"string_decoder": "^1.3.0",
|
"string_decoder": "^1.3.0",
|
||||||
"string-similarity": "^4.0.4",
|
"string-similarity": "^4.0.4",
|
||||||
"ws": "^8.13.0"
|
"ws": "^8.13.0"
|
||||||
|
@ -4,6 +4,7 @@ const { setTimeout } = require('node:timers');
|
|||||||
const { setTimeout: sleep } = require('node:timers/promises');
|
const { setTimeout: sleep } = require('node:timers/promises');
|
||||||
const { inspect } = require('util');
|
const { inspect } = require('util');
|
||||||
const { AsyncQueue } = require('@sapphire/async-queue');
|
const { AsyncQueue } = require('@sapphire/async-queue');
|
||||||
|
const parseCookie = require('set-cookie-parser');
|
||||||
const DiscordAPIError = require('./DiscordAPIError');
|
const DiscordAPIError = require('./DiscordAPIError');
|
||||||
const HTTPError = require('./HTTPError');
|
const HTTPError = require('./HTTPError');
|
||||||
const RateLimitError = require('./RateLimitError');
|
const RateLimitError = require('./RateLimitError');
|
||||||
@ -11,25 +12,6 @@ const {
|
|||||||
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST, CAPTCHA_REQUIRED },
|
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST, CAPTCHA_REQUIRED },
|
||||||
} = require('../util/Constants');
|
} = require('../util/Constants');
|
||||||
|
|
||||||
const cookieFilter = str => {
|
|
||||||
const blackList = ['expires', 'path', 'domain', 'httponly', 'secure', 'max-age', 'samesite'];
|
|
||||||
if (blackList.some(s => str.toLowerCase().includes(`${s}`))) return false;
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseCookie(str, old) {
|
|
||||||
const oldProps = old.split(';').filter(cookieFilter);
|
|
||||||
const allProps = str.split(';').filter(cookieFilter);
|
|
||||||
// Update data from all to old
|
|
||||||
allProps.forEach(prop => {
|
|
||||||
const key = prop.split('=')[0];
|
|
||||||
const index = oldProps.findIndex(s => s.startsWith(key));
|
|
||||||
if (index !== -1) oldProps[index] = prop;
|
|
||||||
else oldProps.push(prop);
|
|
||||||
});
|
|
||||||
return oldProps.filter(s => s).join('; ');
|
|
||||||
}
|
|
||||||
|
|
||||||
const captchaMessage = [
|
const captchaMessage = [
|
||||||
'incorrect-captcha',
|
'incorrect-captcha',
|
||||||
'response-already-used',
|
'response-already-used',
|
||||||
@ -259,19 +241,21 @@ class RequestHandler {
|
|||||||
|
|
||||||
let sublimitTimeout;
|
let sublimitTimeout;
|
||||||
if (res.headers) {
|
if (res.headers) {
|
||||||
// Cookie:
|
const cookie = res.headers.raw()['set-cookie'];
|
||||||
const cookie = res.headers.get('set-cookie');
|
if (cookie && Array.isArray(cookie)) {
|
||||||
if (cookie) {
|
const oldCookie = parseCookie((this.manager.client.options.http.headers.Cookie || '').split('; '), {
|
||||||
if (typeof cookie == 'string') {
|
map: true,
|
||||||
this.manager.client.options.http.headers.Cookie = parseCookie(
|
});
|
||||||
cookie,
|
const parse = parseCookie(cookie, {
|
||||||
this.manager.client.options.http.headers.Cookie || '',
|
map: true,
|
||||||
);
|
});
|
||||||
this.manager.client.emit(
|
for (const key in parse) {
|
||||||
'debug',
|
oldCookie[key] = parse[key];
|
||||||
`[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
this.manager.client.options.http.headers.Cookie = Object.entries(oldCookie)
|
||||||
|
.map(([key, value]) => `${key}=${value.value}`)
|
||||||
|
.join('; ');
|
||||||
|
this.manager.client.emit('debug', `[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`);
|
||||||
}
|
}
|
||||||
const serverDate = res.headers.get('date');
|
const serverDate = res.headers.get('date');
|
||||||
const limit = res.headers.get('x-ratelimit-limit');
|
const limit = res.headers.get('x-ratelimit-limit');
|
||||||
|
Loading…
Reference in New Issue
Block a user