-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseService.js
More file actions
98 lines (85 loc) · 3.57 KB
/
BaseService.js
File metadata and controls
98 lines (85 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* DownloadLib service module
* Base class for manga services
* @module services/BaseService
* @license MIT
* @author ivanvit
* @version 1.0.2
*/
'use strict';
(function(global) {
class BaseService {
constructor(config) {
this.config = config;
this.name = config.name;
this.baseUrl = config.baseUrl;
console.log(`[BaseService] Created service: ${this.name}`);
}
async fetchMangaMetadata(slug) {
throw new Error('fetchMangaMetadata must be implemented');
}
async fetchChaptersList(slug) {
throw new Error('fetchChaptersList must be implemented');
}
async fetchChapter(slug, number, volume) {
throw new Error('fetchChapter must be implemented');
}
extractPages(chapterData) {
const keys = ['pages', 'images', 'pages_list', 'content'];
for (const key of keys)
if (Array.isArray(chapterData[key]) && chapterData[key].length)
return chapterData[key].slice();
return [];
}
async loadPageAsBase64(url, opts = {}) {
const response = await this.fetchWithRetry(url, opts);
const blob = await response.blob();
return this.blobToBase64(blob);
}
async fetchWithRetry(url, opts, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
return await fetch(url, opts);
} catch (e) {
if (i === retries - 1) throw e;
await this.delay(1000 * (i + 1));
}
}
}
async fetchWithRateLimitRetry(url, opts, maxRetries = 5) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
const response = await fetch(url, opts);
if (response.status === 429) {
const retryAfter = parseInt(response.headers.get('Retry-After'), 10);
const waitMs = (retryAfter && retryAfter > 0) ? retryAfter * 1000 : 30000;
console.warn(`[${this.name}] 429 Too Many Requests (attempt ${attempt + 1}/${maxRetries}), waiting ${waitMs}ms...`);
this._on429 && this._on429(waitMs);
if (typeof global !== 'undefined' && global.globalRateLimiter && global.globalRateLimiter.throttle)
global.globalRateLimiter.throttle(waitMs);
else if (typeof self !== 'undefined' && self.globalRateLimiter && self.globalRateLimiter.throttle)
self.globalRateLimiter.throttle(waitMs);
else console.warn(`[${this.name}] No globalRateLimiter found, proceeding with local delay.`);
await this.delay(waitMs);
continue;
}
return response;
}
throw new Error(`Rate limited after ${maxRetries} retries (429)`);
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
static matches(url) {
throw new Error('matches must be implemented');
}
}
global.BaseService = BaseService;
})(typeof window !== 'undefined' ? window : self);