-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcoce.js
More file actions
756 lines (676 loc) · 20.3 KB
/
coce.js
File metadata and controls
756 lines (676 loc) · 20.3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* eslint-disable no-undef */
/* eslint-disable no-eval */
const fs = require('fs');
const http = require('http');
const https = require('https');
const redis = require('redis');
const { logger } = require('./lib/logger');
// Safe config loading with error handling
let config;
try {
const configPath = process.env.CONFIG_PATH || 'config.json';
const configContent = fs.readFileSync(configPath, 'utf8');
config = JSON.parse(configContent);
logger.info('Configuration loaded successfully', {
configPath,
providers: config.providers,
port: config.port,
});
} catch (error) {
logger.error('Failed to load configuration', error, {
configPath: process.env.CONFIG_PATH || 'config.json',
});
process.exit(1);
}
exports.config = config;
const redisClient = redis.createClient(config.redis.port, config.redis.host);
// Redis error handling
redisClient.on('error', (error) => {
logger.error('Redis connection error', error, {
host: config.redis.host,
port: config.redis.port,
});
});
redisClient.on('connect', () => {
logger.info('Redis connected successfully', {
host: config.redis.host,
port: config.redis.port,
});
});
redisClient.on('reconnecting', () => {
logger.warn('Redis reconnecting', {
host: config.redis.host,
port: config.redis.port,
});
});
/**
* CoceFetcher class
*
* @class CoceFetcher
* @module coce
* @constructor
* @param {int} timeout Timeout in miliseconds. After this delay, the fetching
* is aborted, even if some providers haven't yet responded. Without param, the
* timeout is retrieved from config.json file.
*/
const CoceFetcher = function CoceFetcher(timeout) {
this.timeout = timeout === undefined ? config.timeout : timeout;
this.count = 0;
this.finished = false;
/**
* URLs found in the cache (or from providers)
* @property url
* @type Object
*/
this.url = {};
};
exports.CoceFetcher = CoceFetcher;
CoceFetcher.RegGb = /(zoom=5)/g;
/**
* Retrieve an ID from Amazon. Cover image direct URL are tested.
* @method aws_http
* @param {Array} ids The resource IDs to request to Amazon
*/
CoceFetcher.prototype.aws = function awsFetcher(ids) {
const repo = this;
const providerName = 'aws';
logger.info('Starting Amazon AWS provider fetch', {
provider: providerName,
ids,
count: ids.length,
});
let i = 0;
const checkoneurl = () => {
const id = ids[i];
let search = id;
// If ISBN13, transform it into ISBN10
search = search.replace(/-/g, '');
if (search.length === 13) {
// Remove the 978 prefix and get the first 9 digits
search = search.substr(3, 9);
// Calculate ISBN10 checksum using the correct algorithm
let checksum = 0;
for (let j = 0; j < 9; j += 1) {
checksum += parseInt(search[j], 10) * (10 - j);
}
checksum = (11 - (checksum % 11)) % 11;
checksum = checksum === 10 ? 'X' : checksum.toString();
search += checksum;
logger.debug('ISBN13 to ISBN10 conversion', {
provider: providerName,
original: id,
converted: search,
checksum,
});
}
const opts = {
hostname: 'images-na.ssl-images-amazon.com',
method: 'HEAD',
headers: { 'user-agent': 'Mozilla/5.0' },
path: `/images/P/${search}.01.MZZZZZZZZZ.jpg`,
};
const req = https.get(opts, (res) => {
const url = `https://${opts.hostname}${opts.path}`;
logger.debug('Amazon AWS response received', {
provider: providerName,
id,
statusCode: res.statusCode,
url,
});
if (res.statusCode === 200 || res.statusCode === 403) {
repo.addurl('aws', id, url);
logger.debug('Amazon AWS cover found', {
provider: providerName,
id,
url,
statusCode: res.statusCode,
});
} else {
logger.debug('Amazon AWS cover not found', {
provider: providerName,
id,
statusCode: res.statusCode,
});
}
repo.increment();
i += 1;
// timeout for next request
if (i < ids.length) {
setTimeout(checkoneurl, 30);
} else {
logger.info('Amazon AWS fetch completed', {
provider: providerName,
processed: ids.length,
});
}
});
req.on('error', (error) => {
logger.error('Amazon AWS request failed', error, {
provider: providerName,
id,
url: `https://${opts.hostname}${opts.path}`,
});
repo.increment();
i += 1;
if (i < ids.length) setTimeout(checkoneurl, 30);
});
req.on('timeout', () => {
logger.warn('Amazon AWS request timeout', {
provider: providerName,
id,
timeout: config.aws.timeout || 'default',
});
req.destroy();
repo.increment();
i += 1;
if (i < ids.length) setTimeout(checkoneurl, 30);
});
// Set timeout if configured
if (config.aws && config.aws.timeout) {
req.setTimeout(config.aws.timeout);
}
};
checkoneurl();
};
/**
* Retrieve an ID from Google Books
* @method gb
* @param {Array} ids The resource IDs to request to Google Books
*/
CoceFetcher.prototype.gb = function gb(ids) {
const repo = this;
const providerName = 'gb';
logger.info('Starting Google Books provider fetch', {
provider: providerName,
ids,
count: ids.length,
});
const opts = {
host: 'books.google.com',
port: 443,
path: `/books?bibkeys=${ids.join(',')}&jscmd=viewapi&hl=en`,
};
const req = https.get(opts, (res) => {
logger.debug('Google Books response received', {
provider: providerName,
statusCode: res.statusCode,
headers: res.headers,
});
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => {
store += data;
});
res.on('end', () => {
try {
// Safe evaluation with error handling
const GBSBookInfo = {};
// Validate response before eval
if (!store || store.trim().length === 0) {
logger.warn('Empty response from Google Books', { provider: providerName, ids });
repo.increment(ids.length);
return;
}
// Safe eval with isolated context to prevent variable conflicts
let bookInfo;
try {
// Clear any existing _GBSBookInfo to prevent conflicts
// eslint-disable-next-line no-underscore-dangle
if (typeof _GBSBookInfo !== 'undefined') {
// eslint-disable-next-line no-underscore-dangle
delete global._GBSBookInfo;
}
eval(store);
// eslint-disable-next-line no-underscore-dangle
bookInfo = _GBSBookInfo;
// Clean up global scope
// eslint-disable-next-line no-underscore-dangle
delete global._GBSBookInfo;
} catch (evalError) {
logger.error('Failed to evaluate Google Books response', evalError, {
provider: providerName,
responseLength: store.length,
responsePreview: store.substring(0, 200),
});
repo.increment(ids.length);
return;
}
if (typeof bookInfo === 'object' && bookInfo !== null) {
let foundCount = 0;
Object.values(bookInfo).forEach((item) => {
try {
const id = item.bib_key;
let url = item.thumbnail_url;
if (url === undefined) return;
// get the medium size cover image
url = url.replace(CoceFetcher.RegGb, 'zoom=1');
repo.addurl(providerName, id, url);
foundCount += 1;
logger.debug('Google Books cover found', {
provider: providerName,
id,
url,
});
} catch (itemError) {
logger.error('Error processing Google Books item', itemError, {
provider: providerName,
item,
});
}
});
logger.info('Google Books fetch completed', {
provider: providerName,
requested: ids.length,
found: foundCount,
});
} else {
logger.warn('Invalid Google Books response format', {
provider: providerName,
responseType: typeof GBSBookInfo,
});
}
repo.increment(ids.length);
} catch (error) {
logger.error('Error parsing Google Books response', error, {
provider: providerName,
responseLength: store.length,
});
repo.increment(ids.length);
}
});
res.on('error', (error) => {
logger.error('Google Books response stream error', error, {
provider: providerName,
ids,
});
repo.increment(ids.length);
});
});
req.on('error', (error) => {
logger.error('Google Books request failed', error, {
provider: providerName,
ids,
host: opts.host,
path: opts.path,
});
repo.increment(ids.length);
});
req.on('timeout', () => {
logger.warn('Google Books request timeout', {
provider: providerName,
ids,
timeout: config.gb.timeout || 'default',
});
req.destroy();
repo.increment(ids.length);
});
// Set timeout if configured
if (config.gb && config.gb.timeout) {
req.setTimeout(config.gb.timeout);
}
};
/**
* Retrieve an ID from ORB
* @method orb
* @param {Array} ids The resource IDs to request to ORB
*/
CoceFetcher.prototype.orb = function orb(ids) {
const repo = this;
const providerName = 'orb';
logger.info('Starting ORB provider fetch', {
provider: providerName,
ids,
count: ids.length,
});
if (!config.orb || !config.orb.user || !config.orb.key) {
logger.error('ORB configuration missing', null, {
provider: providerName,
hasConfig: !!config.orb,
hasUser: !!(config.orb && config.orb.user),
hasKey: !!(config.orb && config.orb.key),
});
repo.increment(ids.length);
return;
}
const opts = {
host: 'api.base-orb.fr',
auth: `${config.orb.user}:${config.orb.key}`,
port: 443,
path: `/v1/products?eans=${ids.join(',')}&sort=ean_asc`,
};
const req = https.get(opts, (res) => {
logger.debug('ORB response received', {
provider: providerName,
statusCode: res.statusCode,
headers: res.headers,
});
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => {
store += data;
});
res.on('end', () => {
try {
// Validate response before parsing
if (!store || store.trim().length === 0) {
logger.warn('Empty response from ORB', { provider: providerName, ids });
repo.increment(ids.length);
return;
}
// Safe JSON parsing with try-catch
let orbres;
try {
orbres = JSON.parse(store);
} catch (parseError) {
logger.error('Failed to parse ORB JSON response', parseError, {
provider: providerName,
responseLength: store.length,
responsePreview: store.substring(0, 200),
});
repo.increment(ids.length);
return;
}
if (orbres && orbres.data && Array.isArray(orbres.data)) {
let foundCount = 0;
orbres.data.forEach((item) => {
try {
const id = item.ean13;
const url = item.images
&& item.images.front
&& item.images.front.thumbnail
&& item.images.front.thumbnail.src;
if (url === undefined) return;
repo.addurl(providerName, id, url);
foundCount += 1;
logger.debug('ORB cover found', {
provider: providerName,
id,
url,
});
} catch (itemError) {
logger.error('Error processing ORB item', itemError, {
provider: providerName,
item,
});
}
});
logger.info('ORB fetch completed', {
provider: providerName,
requested: ids.length,
found: foundCount,
});
} else {
logger.warn('Invalid ORB response format', {
provider: providerName,
hasData: !!(orbres && orbres.data),
isArray: !!(orbres && orbres.data && Array.isArray(orbres.data)),
});
}
repo.increment(ids.length);
} catch (error) {
logger.error('Error processing ORB response', error, {
provider: providerName,
responseLength: store.length,
statusCode: res.statusCode,
});
repo.increment(ids.length);
}
});
res.on('error', (error) => {
logger.error('ORB response stream error', error, {
provider: providerName,
ids,
});
repo.increment(ids.length);
});
});
req.on('error', (error) => {
logger.error('ORB request failed', error, {
provider: providerName,
ids,
host: opts.host,
path: opts.path,
});
repo.increment(ids.length);
});
req.on('timeout', () => {
logger.warn('ORB request timeout', {
provider: providerName,
ids,
timeout: config.orb.timeout || 'default',
});
req.destroy();
repo.increment(ids.length);
});
// Set timeout if configured
if (config.orb && config.orb.timeout) {
req.setTimeout(config.orb.timeout);
}
};
/**
* Retrieve an ID from Open Library
* @method ol
* @param {Array} ids The resource IDs to request to Open Library
*/
CoceFetcher.prototype.ol = function ol(ids) {
const repo = this;
const opts = {
host: 'openlibrary.org',
port: 80,
path: `/api/books?bibkeys=${ids.join(',')}&jscmd=data`,
};
const req = http.get(opts, (res) => {
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => { store += data; });
res.on('end', () => {
try {
eval(store);
Object.keys(_OLBookInfo).forEach((id) => {
let url = _OLBookInfo[id].cover;
if (url === undefined) return;
url = url[config.ol.imageSize];
repo.addurl('ol', id, url);
});
} catch (error) {
logger.error('Failed to parse Open Library response', {
provider: 'ol',
responseLength: store.length,
responsePreview: store.substring(0, 100),
error: {
name: error.name,
message: error.message,
stack: error.stack,
},
process: {
pid: process.pid,
memory: process.memoryUsage().rss,
uptime: Math.floor(process.uptime()),
},
});
}
repo.increment(ids.length);
});
});
req.on('error', (error) => {
logger.error('Open Library request failed', {
provider: 'ol',
ids,
host: opts.host,
path: opts.path,
error: {
name: error.name,
message: error.message,
stack: error.stack,
},
process: {
pid: process.pid,
memory: process.memoryUsage().rss,
uptime: Math.floor(process.uptime()),
},
});
repo.increment(ids.length);
});
};
/**
* Add an url to redis
* Cache locally a file if necessary
*/
CoceFetcher.prototype.addurl = function addurl(provider, id, url) {
let storedUrl;
if (config[provider].cache) {
storedUrl = `${config.cache.url}/${provider}/${id}.jpg`;
const dest = `${config.cache.path}/${provider}/${id}.jpg`;
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => file.close());
}).on('error', () => fs.unlink(dest));
} else {
storedUrl = url;
}
redisClient.setex(`${provider}.${id}`, config[provider].timeout, storedUrl);
if (this.url[id] === undefined) this.url[id] = {};
this.url[id][provider] = storedUrl;
};
/**
* Increment the count of found URLs.
* Stop the timer if all URLs have been found.
* @param {int} increment Increment the number of found IDs. No parameter = 1.
*/
CoceFetcher.prototype.increment = function incr(increment) {
this.count += increment || 1;
if (this.count >= this.countMax) {
clearTimeout(this.timeoutId);
if (!this.finished) {
this.finished = true;
this.finish(this.url);
}
}
};
/**
* Retrieve IDs from a provider
* @method add
* @param {Array} ids Group of IDs
* @param {String} provider (aws|gb|ol) to search for
*/
CoceFetcher.prototype.add = function add(ids, provider) {
const repo = this;
const notcached = [];
let count = ids.length;
let timeoutId;
for (let i = 0; i < ids.length; i += 1) {
// eslint-disable-next-line no-loop-func
(function addId() {
const id = ids[i];
const key = `${provider}.${ids[i]}`;
redisClient.get(key, (err, reply) => {
count -= 1;
if (reply === null) {
// Not in the cache
notcached.push(id);
redisClient.setex(`${provider}.${id}`, config[provider].timeout, '');
} else if (reply === '') {
// In the cache, but no url via provider
repo.increment();
} else {
if (repo.url[id] === undefined) repo.url[id] = {};
repo.url[id][provider] = reply;
repo.increment();
}
if (count === 0 && timeoutId !== undefined) {
// We get all responses from Redis
clearTimeout(timeoutId);
if (notcached.length > 0) repo[provider](notcached);
}
});
}());
}
// Wait all Redis responses
timeoutId = setTimeout(() => {
if (notcached.length > 0) repo[provider](notcached);
}, config.redis.timeout);
};
/**
* Fetch all provided ID from cover image providers
* Wait for providers reponses, with a limitation of time
* @method fetch
* @param {Array} ids Array of images ID
* @param {Array} providers Array of images providers (gb, aws, ol)
* @param timeout {int} Max duration of the fetching
* @param finish {Function} Function to execute when all URLs are fetched or time has
* elasped
*/
CoceFetcher.prototype.fetch = function fetch(ids, providers, finish) {
logger.info('Starting fetch operation', {
ids,
providers,
idsCount: ids ? ids.length : 0,
providersCount: providers ? providers.length : 0,
});
// Validate providers parameter with proper null/undefined check FIRST
if (!providers || !Array.isArray(providers) || providers.length === 0) {
const error = 'At least, one provider is required';
logger.error('Fetch validation failed', null, {
error,
providedProviders: providers,
availableProviders: config.providers,
});
finish({ error });
return;
}
this.count = 0;
this.countMax = ids.length * providers.length;
this.finish = finish;
this.finished = false;
this.url = {};
// Validate that all providers are available
for (let i = 0; i < providers.length; i += 1) {
const provider = providers[i];
if (config.providers.indexOf(provider) === -1) {
const error = `Unavailable provider: ${provider}`;
logger.error('Invalid provider specified', null, {
error,
provider,
availableProviders: config.providers,
});
finish({ error });
return;
}
}
// Start provider operations
for (let i = 0; i < providers.length; i += 1) {
try {
logger.debug('Starting provider operation', { provider: providers[i], ids });
this.add(ids, providers[i]);
} catch (error) {
logger.error('Provider operation failed', error, { provider: providers[i], ids });
// Continue with other providers
}
}
if (this.count !== this.countMax) {
const repo = this;
this.timeoutId = setTimeout(() => {
if (!repo.finished) {
logger.warn('Fetch operation timeout', {
ids,
providers,
timeout: repo.timeout,
completed: repo.count,
expected: repo.countMax,
});
repo.finished = true;
logger.info('Fetch operation completed with timeout', {
ids,
providers,
foundIds: Object.keys(repo.url).length,
totalUrls: Object.values(repo.url).reduce((sum, urls) => sum + (typeof urls === 'object' ? Object.keys(urls).length : 1), 0),
});
repo.finish(repo.url);
}
}, this.timeout);
}
};
exports.set = function setex(provider, id, url) {
redisClient.setex(`aws.${id}`, 315360000, url);
};