Compare commits
No commits in common. "d672797bbdf57541a58c66a85041473212bc61b4" and "bf7a7a96804a7db9c28009251e434791d43bd35f" have entirely different histories.
d672797bbd
...
bf7a7a9680
|
@ -8,7 +8,6 @@
|
|||
"axios": "^1.6.7",
|
||||
"axios-request-throttle": "^1.0.0",
|
||||
"axios-retry": "^4.0.0",
|
||||
"bottleneck": "^2.19.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"form-data": "^4.0.0",
|
||||
"fs": "^0.0.1-security",
|
||||
|
@ -247,11 +246,6 @@
|
|||
"readable-stream": "^3.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/bottleneck": {
|
||||
"version": "2.19.5",
|
||||
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",
|
||||
"integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw=="
|
||||
},
|
||||
"node_modules/brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
|
@ -603,9 +597,9 @@
|
|||
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.15.6",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
|
||||
"version": "1.15.5",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
||||
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
|
@ -2196,11 +2190,6 @@
|
|||
"readable-stream": "^3.4.0"
|
||||
}
|
||||
},
|
||||
"bottleneck": {
|
||||
"version": "2.19.5",
|
||||
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",
|
||||
"integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw=="
|
||||
},
|
||||
"brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
|
@ -2460,9 +2449,9 @@
|
|||
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.15.6",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
|
||||
"version": "1.15.5",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
||||
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw=="
|
||||
},
|
||||
"form-data": {
|
||||
"version": "4.0.0",
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"axios": "^1.6.7",
|
||||
"axios-request-throttle": "^1.0.0",
|
||||
"axios-retry": "^4.0.0",
|
||||
"bottleneck": "^2.19.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"form-data": "^4.0.0",
|
||||
"fs": "^0.0.1-security",
|
||||
|
@ -22,4 +21,4 @@
|
|||
"start": "node dist/index.js",
|
||||
"test": "tsc -p tsconfig.tests.json"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,13 @@
|
|||
import axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios';
|
||||
import axios, {AxiosInstance, AxiosError} from 'axios';
|
||||
import axiosRetry from 'axios-retry';
|
||||
import Bottleneck from 'bottleneck';
|
||||
import axiosThrottle from 'axios-request-throttle';
|
||||
|
||||
class SessionManager {
|
||||
private static instance: SessionManager;
|
||||
public readonly axiosInstance: AxiosInstance;
|
||||
private limiter: Bottleneck;
|
||||
|
||||
private constructor() {
|
||||
// Initialize the Bottleneck limiter
|
||||
this.limiter = new Bottleneck({
|
||||
maxConcurrent: 1, // Maximum number of concurrent requests
|
||||
minTime: 1000 // Minimum time between dispatches of requests in milliseconds
|
||||
});
|
||||
axiosThrottle.use(axios, { requestsPerSecond: 1 }); // Throttle setup
|
||||
|
||||
this.axiosInstance = axios.create({
|
||||
baseURL: 'https://rdrama.net/',
|
||||
|
@ -21,9 +16,6 @@ class SessionManager {
|
|||
},
|
||||
});
|
||||
|
||||
// Wrap axios requests with the limiter
|
||||
this.wrapAxiosInstance(this.axiosInstance);
|
||||
|
||||
axiosRetry(this.axiosInstance, {
|
||||
retries: 3,
|
||||
retryDelay: this.retryDelayStrategy,
|
||||
|
@ -49,33 +41,7 @@ class SessionManager {
|
|||
|
||||
private retryCondition(error: AxiosError): boolean {
|
||||
const status = error.response?.status ?? 0;
|
||||
return status === 429 || status >= 500;
|
||||
}
|
||||
|
||||
private wrapAxiosInstance(instance: AxiosInstance): void {
|
||||
// Wrap the get method
|
||||
const originalGet = instance.get;
|
||||
instance.get = <T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> => {
|
||||
return this.limiter.schedule(() => originalGet.apply(instance, [url, config])) as Promise<R>;
|
||||
};
|
||||
|
||||
// Wrap the post method
|
||||
const originalPost = instance.post;
|
||||
instance.post = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
|
||||
return this.limiter.schedule(() => originalPost.apply(instance, [url, data, config])) as Promise<R>;
|
||||
};
|
||||
|
||||
// Wrap the put method
|
||||
const originalPut = instance.put;
|
||||
instance.put = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
|
||||
return this.limiter.schedule(() => originalPut.apply(instance, [url, data, config])) as Promise<R>;
|
||||
};
|
||||
|
||||
// Wrap the delete method
|
||||
const originalDelete = instance.delete;
|
||||
instance.delete = <T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> => {
|
||||
return this.limiter.schedule(() => originalDelete.apply(instance, [url, config])) as Promise<R>;
|
||||
};
|
||||
return status === 429 || status >= 400;
|
||||
}
|
||||
|
||||
public setAuthorizationToken(token: string): void {
|
||||
|
|
Loading…
Reference in New Issue