ztechno_core 0.0.105 → 0.0.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/lib/core/crypto_service.d.ts +23 -0
  2. package/lib/core/crypto_service.js +100 -0
  3. package/lib/core/engine_base.d.ts +11 -0
  4. package/lib/core/engine_base.js +9 -0
  5. package/lib/core/index.d.ts +14 -0
  6. package/lib/core/index.js +101 -0
  7. package/lib/core/mail_service.d.ts +85 -0
  8. package/lib/core/mail_service.js +156 -0
  9. package/lib/core/orm/mail_blacklist_orm.d.ts +21 -0
  10. package/lib/core/orm/mail_blacklist_orm.js +79 -0
  11. package/lib/core/orm/orm.d.ts +27 -0
  12. package/lib/core/orm/orm.js +67 -0
  13. package/lib/core/sql_service.d.ts +195 -0
  14. package/lib/core/sql_service.js +333 -0
  15. package/lib/core/translate_service.d.ts +48 -0
  16. package/lib/core/translate_service.js +913 -0
  17. package/lib/core/types/crypto_types.d.ts +4 -0
  18. package/lib/core/types/crypto_types.js +2 -0
  19. package/lib/core/types/mail_types.d.ts +95 -0
  20. package/lib/core/types/mail_types.js +2 -0
  21. package/lib/core/types/site_config.d.ts +46 -0
  22. package/lib/core/types/site_config.js +2 -0
  23. package/lib/core/types/translate_types.d.ts +69 -0
  24. package/lib/core/types/translate_types.js +46 -0
  25. package/lib/core/types/user_types.d.ts +41 -0
  26. package/lib/core/types/user_types.js +2 -0
  27. package/lib/core/user_service.d.ts +87 -0
  28. package/lib/core/user_service.js +216 -0
  29. package/lib/express/index.d.ts +1 -0
  30. package/lib/express/index.js +18 -0
  31. package/lib/index.d.ts +17 -8
  32. package/lib/index.js +60 -16
  33. package/lib/mollie/index.d.ts +5 -0
  34. package/lib/mollie/index.js +62 -0
  35. package/lib/mollie/orm/customers_orm.d.ts +16 -0
  36. package/lib/mollie/orm/customers_orm.js +115 -0
  37. package/lib/mollie/orm/invoice_items_orm.d.ts +9 -0
  38. package/lib/mollie/orm/invoice_items_orm.js +71 -0
  39. package/lib/mollie/orm/invoice_payments_orm.d.ts +10 -0
  40. package/lib/mollie/orm/invoice_payments_orm.js +70 -0
  41. package/lib/mollie/orm/invoices_orm.d.ts +40 -0
  42. package/lib/mollie/orm/invoices_orm.js +172 -0
  43. package/lib/mollie/orm/subscription_items_orm.d.ts +9 -0
  44. package/lib/mollie/orm/subscription_items_orm.js +45 -0
  45. package/lib/mollie/orm/subscriptions_orm.d.ts +17 -0
  46. package/lib/mollie/orm/subscriptions_orm.js +122 -0
  47. package/lib/mollie/services/customer_service.d.ts +14 -0
  48. package/lib/mollie/services/customer_service.js +53 -0
  49. package/lib/mollie/services/invoice_service.d.ts +102 -0
  50. package/lib/mollie/services/invoice_service.js +866 -0
  51. package/lib/mollie/services/mollie_service.d.ts +42 -0
  52. package/lib/mollie/services/mollie_service.js +370 -0
  53. package/lib/mollie/services/subscription_service.d.ts +32 -0
  54. package/lib/mollie/services/subscription_service.js +134 -0
  55. package/lib/mollie/types/internal_types.d.ts +19 -0
  56. package/lib/mollie/types/internal_types.js +2 -0
  57. package/lib/mollie/types/mollie_types.d.ts +187 -0
  58. package/lib/mollie/types/mollie_types.js +3 -0
  59. package/lib/mollie/util/subscription_utils.d.ts +8 -0
  60. package/lib/mollie/util/subscription_utils.js +37 -0
  61. package/lib/schema/MySQLSchemaExtractor.d.ts +1 -1
  62. package/lib/schema/MySQLSchemaImporter.d.ts +1 -1
  63. package/package.json +3 -1
@@ -0,0 +1,913 @@
1
+ 'use strict';
2
+ var __importDefault =
3
+ (this && this.__importDefault) ||
4
+ function (mod) {
5
+ return mod && mod.__esModule ? mod : { default: mod };
6
+ };
7
+ Object.defineProperty(exports, '__esModule', { value: true });
8
+ exports.ZTranslateService = void 0;
9
+ const translate_types_1 = require('./types/translate_types');
10
+ const dist_1 = require('../vendor/dom-parser/dist');
11
+ const translate_1 = __importDefault(require('translate'));
12
+ class ZTranslateService {
13
+ get sql() {
14
+ return this.opt.sqlService;
15
+ }
16
+ getLanguages() {
17
+ return (
18
+ this.opt.languages || [
19
+ { lang: 'en', text: 'English' },
20
+ { lang: 'nl', text: 'Nederlands' },
21
+ ]
22
+ );
23
+ }
24
+ getSourceLang() {
25
+ return this.opt.sourceLang || 'en';
26
+ }
27
+ getDefaultLang() {
28
+ return this.opt.defaultLang || 'en';
29
+ }
30
+ constructor(opt) {
31
+ this.opt = opt;
32
+ this.localCache = {};
33
+ this.surpressErrors = true;
34
+ this.maxRetries = 3;
35
+ this.retryDelay = 1000;
36
+ this.fallbackText = '?';
37
+ this.codes = {
38
+ // Quotes and apostrophes
39
+ [`'`]: `'`,
40
+ [`"`]: `"`,
41
+ [`“`]: `"`,
42
+ [`”`]: `"`,
43
+ [`‘`]: `'`,
44
+ [`’`]: `'`,
45
+ [`‚`]: `‚`,
46
+ [`„`]: `„`,
47
+ [`«`]: `«`,
48
+ [`»`]: `»`,
49
+ [`‹`]: `‹`,
50
+ [`›`]: `›`,
51
+ // Currency symbols
52
+ [`©`]: `©`,
53
+ [`®`]: `®`,
54
+ [`€`]: `€`,
55
+ [`£`]: `£`,
56
+ [`¥`]: `¥`,
57
+ [`¢`]: `¢`,
58
+ [`™`]: `™`,
59
+ [`$`]: `$`,
60
+ // Mathematical and special symbols
61
+ [`–`]: `–`,
62
+ [`—`]: `—`,
63
+ [`…`]: `…`,
64
+ [`•`]: `•`,
65
+ [`→`]: `→`,
66
+ [`←`]: `←`,
67
+ [`↑`]: `↑`,
68
+ [`↓`]: `↓`,
69
+ [`×`]: `×`,
70
+ [`÷`]: `÷`,
71
+ [`±`]: `±`,
72
+ [`≤`]: `≤`,
73
+ [`≥`]: `≥`,
74
+ [`≠`]: `≠`,
75
+ [`∞`]: `∞`,
76
+ [`°`]: `°`,
77
+ [`‰`]: `‰`,
78
+ [`†`]: `†`,
79
+ [`‡`]: `‡`,
80
+ [`§`]: `§`,
81
+ [`¶`]: `¶`,
82
+ // Accented characters
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
+ // Common spaces and breaks
146
+ [` `]: ` `,
147
+ [`­`]: `­`,
148
+ [`​`]: ``,
149
+ // Punctuation
150
+ [`¡`]: `¡`,
151
+ [`¿`]: `¿`,
152
+ [`·`]: `·`,
153
+ [`¸`]: `¸`,
154
+ // Fractions
155
+ [`¼`]: `¼`,
156
+ [`½`]: `½`,
157
+ [`¾`]: `¾`,
158
+ [`⅓`]: `⅓`,
159
+ [`⅔`]: `⅔`,
160
+ [`⅕`]: `⅕`,
161
+ [`⅖`]: `⅖`,
162
+ [`⅗`]: `⅗`,
163
+ [`⅘`]: `⅘`,
164
+ [`⅙`]: `⅙`,
165
+ [`⅚`]: `⅚`,
166
+ [`⅛`]: `⅛`,
167
+ [`⅜`]: `⅜`,
168
+ [`⅝`]: `⅝`,
169
+ [`⅞`]: `⅞`,
170
+ // Greek letters (common ones)
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
+ // Uppercase Greek letters
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
+ // Additional common symbols
221
+ [`₺`]: `₪`,
222
+ [`₽`]: `₽`,
223
+ [`₹`]: `₹`,
224
+ [`¤`]: `¤`,
225
+ [`¦`]: `¦`,
226
+ [`¨`]: `¨`,
227
+ [`ª`]: `ª`,
228
+ [`¬`]: `¬`,
229
+ [`¯`]: `¯`,
230
+ [`²`]: `²`,
231
+ [`³`]: `³`,
232
+ [`¹`]: `¹`,
233
+ [`º`]: `º`,
234
+ // Card suits and misc symbols
235
+ [`♠`]: `♠`,
236
+ [`♣`]: `♣`,
237
+ [`♥`]: `♥`,
238
+ [`♦`]: `♦`,
239
+ [`★`]: `★`,
240
+ [`☆`]: `☆`,
241
+ [`☎`]: `☎`,
242
+ [`☕`]: `☕`,
243
+ [`☺`]: `☺`,
244
+ [`☻`]: `☻`,
245
+ [`☼`]: `☼`,
246
+ [`♀`]: `♀`,
247
+ [`♂`]: `♂`,
248
+ [`❤`]: `❤`,
249
+ // Arabic characters (U+0600 to U+06FF range)
250
+ [`؀`]: `؀`,
251
+ [`؁`]: `؁`,
252
+ [`؂`]: `؂`,
253
+ [`؃`]: `؃`,
254
+ [`؄`]: `؄`,
255
+ [`؅`]: `؅`,
256
+ [`؆`]: `؆`,
257
+ [`؇`]: `؇`,
258
+ [`؈`]: `؈`,
259
+ [`؉`]: `؉`,
260
+ [`؊`]: `؊`,
261
+ [`؋`]: `؋`,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
318
+ [`ل`]: `ل`,
319
+ [`م`]: `م`,
320
+ [`ن`]: `ن`,
321
+ [`ه`]: `ه`,
322
+ [`و`]: `و`,
323
+ [`ى`]: `ى`,
324
+ [`ي`]: `ي`,
325
+ // Arabic diacritics (most common ones)
326
+ [`ً`]: `ً`,
327
+ [`ٌ`]: `ٌ`,
328
+ [`ٍ`]: `ٍ`,
329
+ [`َ`]: `َ`,
330
+ [`ُ`]: `ُ`,
331
+ [`ِ`]: `ِ`,
332
+ [`ّ`]: `ّ`,
333
+ [`ْ`]: `ْ`,
334
+ [`ٓ`]: `ٓ`,
335
+ [`ٔ`]: `ٔ`,
336
+ [`ٕ`]: `ٕ`,
337
+ // Arabic-Indic digits
338
+ [`٠`]: `٠`,
339
+ [`١`]: `١`,
340
+ [`٢`]: `٢`,
341
+ [`٣`]: `٣`,
342
+ [`٤`]: `٤`,
343
+ [`٥`]: `٥`,
344
+ [`٦`]: `٦`,
345
+ [`٧`]: `٧`,
346
+ [`٨`]: `٨`,
347
+ [`٩`]: `٩`,
348
+ // Extended Arabic characters (commonly used)
349
+ [`٪`]: `٪`,
350
+ [`٫`]: `٫`,
351
+ [`٬`]: `٬`,
352
+ [`٭`]: `٭`,
353
+ [`ٮ`]: `ٮ`,
354
+ [`ٯ`]: `ٯ`,
355
+ [`ٰ`]: `ٰ`,
356
+ [`ٱ`]: `ٱ`,
357
+ [`ٲ`]: `ٲ`,
358
+ [`ٳ`]: `ٳ`,
359
+ [`ٴ`]: `ٴ`,
360
+ [`ٵ`]: `ٵ`,
361
+ [`ٶ`]: `ٶ`,
362
+ [`ٷ`]: `ٷ`,
363
+ [`ٸ`]: `ٸ`,
364
+ [`ٹ`]: `ٹ`,
365
+ [`ٺ`]: `ٺ`,
366
+ [`ٻ`]: `ٻ`,
367
+ [`ټ`]: `ټ`,
368
+ [`ٽ`]: `ٽ`,
369
+ [`پ`]: `پ`,
370
+ [`ٿ`]: `ٿ`,
371
+ [`ڀ`]: `ڀ`,
372
+ [`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]: `ڳ`,
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
+ [`ۓ`]: `ۓ`, // Arabic Letter Yeh Barree with Hamza Above
455
+ };
456
+ if (!opt.googleApiKey) {
457
+ throw new translate_types_1.ValidationError('googleApiKey', opt.googleApiKey);
458
+ }
459
+ if (!opt.sqlService) {
460
+ throw new translate_types_1.ValidationError('sqlService', opt.sqlService);
461
+ }
462
+ translate_1.default.key = opt.googleApiKey;
463
+ this.surpressErrors = opt.surpressErrors ?? true;
464
+ this.maxRetries = opt.maxRetries ?? 3;
465
+ this.retryDelay = opt.retryDelay ?? 1000;
466
+ this.fallbackText = opt.fallbackText ?? '?';
467
+ this.getLanguages().map((lang) => (this.localCache[lang.lang] = {}));
468
+ setInterval(() => this.clearLocalCache(), 1000 * 60 * 60); // Every Hour
469
+ }
470
+ getLang(cookies) {
471
+ try {
472
+ const defaultLang = this.getDefaultLang();
473
+ const langKey = (cookies?.lang || defaultLang).toLowerCase();
474
+ const foundLang = this.getLanguages().find((l) => l.lang === langKey);
475
+ return foundLang === undefined ? defaultLang : foundLang.lang;
476
+ } catch (error) {
477
+ this.logError(new translate_types_1.ValidationError('cookies', cookies), 'getLang');
478
+ return this.getDefaultLang();
479
+ }
480
+ }
481
+ async sleep(ms) {
482
+ return new Promise((resolve) => setTimeout(resolve, ms));
483
+ }
484
+ logError(error, context) {
485
+ if (this.opt.log) {
486
+ this.opt.log(error, { context, timestamp: new Date().toISOString() });
487
+ }
488
+ }
489
+ async retryOperation(operation, operationName, maxRetries = this.maxRetries) {
490
+ let lastError;
491
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
492
+ try {
493
+ return await operation();
494
+ } catch (error) {
495
+ lastError = error;
496
+ this.logError(error, `${operationName} - Attempt ${attempt}/${maxRetries}`);
497
+ if (attempt < maxRetries) {
498
+ await this.sleep(this.retryDelay * attempt); // Exponential backoff
499
+ }
500
+ }
501
+ }
502
+ throw lastError;
503
+ }
504
+ async translateText(langOrReq, text) {
505
+ try {
506
+ // Input validation
507
+ if (!text || typeof text !== 'string') {
508
+ throw new translate_types_1.ValidationError('text', text);
509
+ }
510
+ const lang = typeof langOrReq === 'string' ? langOrReq : this.getLang(langOrReq.cookies);
511
+ text = text.trim();
512
+ if (text.length === 0) {
513
+ return text;
514
+ }
515
+ if (text.length === 1) {
516
+ return text;
517
+ }
518
+ // Process HTML entities with better error handling
519
+ text = await this.processHtmlEntities(text);
520
+ // Check local cache
521
+ const localCached = this.checkLocalCache(text, lang);
522
+ if (localCached !== false) {
523
+ return localCached.value;
524
+ }
525
+ // Check remote cache
526
+ const remoteCached = await this.fetch(text, lang);
527
+ if (remoteCached !== false) {
528
+ return remoteCached.value;
529
+ }
530
+ // Perform translation with retry logic
531
+ let result;
532
+ try {
533
+ result = await this.retryOperation(async () => {
534
+ return await (0, translate_1.default)(text, {
535
+ from: this.getSourceLang(),
536
+ to: lang,
537
+ });
538
+ }, 'translateText');
539
+ } catch (err) {
540
+ const translationError = new translate_types_1.ApiTranslationError(err, text, lang);
541
+ this.logError(translationError, 'translateText');
542
+ if (!this.surpressErrors) {
543
+ throw translationError;
544
+ }
545
+ result = this.fallbackText;
546
+ }
547
+ // Save translation to cache
548
+ try {
549
+ await this.insert(text, lang, { value: result });
550
+ } catch (err) {
551
+ this.logError(new translate_types_1.DatabaseError(err, 'insert translation'), 'translateText');
552
+ // Don't throw here, translation still succeeded
553
+ }
554
+ return result;
555
+ } catch (error) {
556
+ if (error instanceof translate_types_1.TranslateError) {
557
+ throw error;
558
+ }
559
+ const wrappedError = new translate_types_1.TranslateError(
560
+ `Unexpected error in translateText: ${error.message}`,
561
+ 'UNEXPECTED_ERROR',
562
+ { originalError: error },
563
+ );
564
+ this.logError(wrappedError, 'translateText');
565
+ if (!this.surpressErrors) {
566
+ throw wrappedError;
567
+ }
568
+ return this.fallbackText;
569
+ }
570
+ }
571
+ async processHtmlEntities(text) {
572
+ let replaceCount = 0;
573
+ const maxReplacements = 1000;
574
+ while (text.includes('&#')) {
575
+ const codeIndexStart = text.indexOf('&#');
576
+ const first = text.substring(codeIndexStart);
577
+ const semicolonIndex = first.indexOf(';');
578
+ if (semicolonIndex === -1) {
579
+ // No closing semicolon found, break to avoid infinite loop
580
+ break;
581
+ }
582
+ const codeLength = semicolonIndex + 1;
583
+ const code = first.substring(0, codeLength);
584
+ if (this.codes[code] === undefined) {
585
+ const entityError = new translate_types_1.HtmlEntityError(code, text);
586
+ this.logError(entityError, 'processHtmlEntities');
587
+ if (!this.surpressErrors) {
588
+ throw entityError;
589
+ }
590
+ // Skip this entity and continue
591
+ text = text.substring(0, codeIndexStart) + code + text.substring(codeIndexStart + codeLength);
592
+ break;
593
+ }
594
+ text = text.substring(0, codeIndexStart) + this.codes[code] + text.substring(codeIndexStart + codeLength);
595
+ if (replaceCount++ > maxReplacements) {
596
+ const loopError = new translate_types_1.TranslateError(
597
+ `HTML entity replacement exceeded maximum count (${maxReplacements})`,
598
+ 'MAX_REPLACEMENTS_EXCEEDED',
599
+ { code, text, replaceCount },
600
+ );
601
+ this.logError(loopError, 'processHtmlEntities');
602
+ if (!this.surpressErrors) {
603
+ throw loopError;
604
+ }
605
+ break;
606
+ }
607
+ }
608
+ return text;
609
+ }
610
+ async translateHtml(html, cookies) {
611
+ try {
612
+ if (!html || typeof html !== 'string') {
613
+ throw new translate_types_1.ValidationError('html', html);
614
+ }
615
+ if (!cookies) {
616
+ throw new translate_types_1.ValidationError('cookies', cookies);
617
+ }
618
+ const lang = this.getLang(cookies);
619
+ const srcLang = this.getSourceLang();
620
+ let dom;
621
+ try {
622
+ dom = (0, dist_1.parseFromString)(html);
623
+ } catch (error) {
624
+ const parseError = new translate_types_1.TranslateError('Failed to parse HTML', 'HTML_PARSE_ERROR', {
625
+ error,
626
+ html,
627
+ });
628
+ this.logError(parseError, 'translateHtml');
629
+ if (!this.surpressErrors) {
630
+ throw parseError;
631
+ }
632
+ return html;
633
+ }
634
+ const htmlNodes = dom.getElementsByTagName('html');
635
+ const mainNodes = dom.getElementsByTagName('main');
636
+ const isView = htmlNodes.length === 0;
637
+ const domNode = isView ? mainNodes[0] : htmlNodes[0];
638
+ if (lang !== srcLang && domNode) {
639
+ const node = isView ? domNode : domNode.getElementsByTagName('body')[0];
640
+ if (node) {
641
+ const promises = [];
642
+ this.translateHtmlRec(lang, node, promises);
643
+ try {
644
+ await Promise.all(promises);
645
+ } catch (error) {
646
+ this.logError(
647
+ new translate_types_1.TranslateError('Failed to translate HTML nodes', 'HTML_TRANSLATION_ERROR', {
648
+ error,
649
+ }),
650
+ 'translateHtml',
651
+ );
652
+ if (!this.surpressErrors) {
653
+ throw error;
654
+ }
655
+ }
656
+ }
657
+ }
658
+ const output = domNode ? domNode.outerHTML : html;
659
+ return output.startsWith(`<!DOCTYPE html>`) ? output : `<!DOCTYPE html>\r\n${output}`;
660
+ } catch (error) {
661
+ if (error instanceof translate_types_1.TranslateError) {
662
+ throw error;
663
+ }
664
+ const wrappedError = new translate_types_1.TranslateError(
665
+ `Unexpected error in translateHtml: ${error.message}`,
666
+ 'UNEXPECTED_ERROR',
667
+ { originalError: error },
668
+ );
669
+ this.logError(wrappedError, 'translateHtml');
670
+ if (!this.surpressErrors) {
671
+ throw wrappedError;
672
+ }
673
+ return html;
674
+ }
675
+ }
676
+ translateHtmlRec(lang, node, promises, skipTranslate = false) {
677
+ try {
678
+ if (this.opt.verbose) this.opt.verbose(node.nodeName, node);
679
+ if (node.getAttribute && node.getAttribute('notranslate') != null) {
680
+ skipTranslate = true;
681
+ }
682
+ if (node.nodeName === '#comment') {
683
+ // Skip HTML comments
684
+ return;
685
+ }
686
+ if (node.nodeName === 'script') {
687
+ // Skip script tags
688
+ return;
689
+ }
690
+ if (node.nodeName === 'style') {
691
+ // Skip style tags
692
+ return;
693
+ }
694
+ if (node.nodeName === '#text') {
695
+ const nodeText = node;
696
+ const text = nodeText.text.replace(/[\r|\n|\r\n]+/g, ' ').replace(/\s\s+/g, ' ');
697
+ const value = text.trim();
698
+ const meta = {
699
+ prefix: genSpaces(text.length - text.trimStart().length),
700
+ suffix: genSpaces(text.length - text.trimEnd().length),
701
+ };
702
+ if (skipTranslate === true || text.length === 0 || !strContainsLetters(text)) {
703
+ node.text = meta.prefix + text + meta.suffix;
704
+ return;
705
+ }
706
+ promises.push(
707
+ this.translateText(lang, value)
708
+ .then((translatedText) => {
709
+ node.text = meta.prefix + translatedText + meta.suffix;
710
+ })
711
+ .catch((err) => {
712
+ node.text = text;
713
+ this.logError(err, 'translateHtmlRec');
714
+ if (!this.surpressErrors) {
715
+ throw err;
716
+ }
717
+ }),
718
+ );
719
+ return;
720
+ }
721
+ // Process child nodes safely
722
+ if (node.childNodes && Array.isArray(node.childNodes)) {
723
+ for (const child of node.childNodes) {
724
+ this.translateHtmlRec(lang, child, promises, skipTranslate);
725
+ }
726
+ }
727
+ } catch (error) {
728
+ this.logError(
729
+ new translate_types_1.TranslateError(`Error processing HTML node: ${error.message}`, 'HTML_NODE_ERROR', {
730
+ error,
731
+ nodeName: node?.nodeName,
732
+ }),
733
+ 'translateHtmlRec',
734
+ );
735
+ if (!this.surpressErrors) {
736
+ throw error;
737
+ }
738
+ }
739
+ }
740
+ async update(key, lang, data) {
741
+ try {
742
+ if (!key || !lang || !data) {
743
+ throw new translate_types_1.ValidationError('update parameters', { key, lang, data });
744
+ }
745
+ const res = await this.retryOperation(async () => {
746
+ return await this.sql.query(
747
+ `
748
+ INSERT INTO translations
749
+ (\`key\`, \`lang\`, \`value\`)
750
+ VALUES
751
+ (:key, :lang, :value)
752
+ ON DUPLICATE KEY UPDATE value=:value
753
+ `,
754
+ { key, lang, value: data.value },
755
+ );
756
+ }, 'update translation');
757
+ if (res.affectedRows) {
758
+ this.insertLocalCache(key, lang, data);
759
+ }
760
+ return res;
761
+ } catch (error) {
762
+ const dbError = new translate_types_1.DatabaseError(error, 'update translation');
763
+ this.logError(dbError, 'update');
764
+ throw dbError;
765
+ }
766
+ }
767
+ checkLocalCache(key, lang) {
768
+ try {
769
+ if (!key || !lang || !this.localCache[lang]) {
770
+ return false;
771
+ }
772
+ const hasLocal = !this.localCache[lang].hasOwnProperty(key);
773
+ return hasLocal ? false : this.localCache[lang][key];
774
+ } catch (error) {
775
+ this.logError(
776
+ new translate_types_1.TranslateError('Local cache check failed', 'CACHE_ERROR', { error, key, lang }),
777
+ 'checkLocalCache',
778
+ );
779
+ return false;
780
+ }
781
+ }
782
+ insertLocalCache(key, lang, data) {
783
+ try {
784
+ if (!key || !lang || !data) {
785
+ return;
786
+ }
787
+ if (!this.localCache[lang]) {
788
+ this.localCache[lang] = {};
789
+ }
790
+ this.localCache[lang][key] = data;
791
+ } catch (error) {
792
+ this.logError(
793
+ new translate_types_1.TranslateError('Local cache insertion failed', 'CACHE_ERROR', { error, key, lang }),
794
+ 'insertLocalCache',
795
+ );
796
+ }
797
+ }
798
+ clearLocalCache() {
799
+ try {
800
+ Object.keys(this.localCache).map((k) => {
801
+ this.localCache[k] = {};
802
+ });
803
+ } catch (error) {
804
+ this.logError(
805
+ new translate_types_1.TranslateError('Failed to clear local cache', 'CACHE_ERROR', { error }),
806
+ 'clearLocalCache',
807
+ );
808
+ }
809
+ }
810
+ async fetch(key, lang) {
811
+ try {
812
+ if (!key || !lang) {
813
+ return false;
814
+ }
815
+ const results = await this.retryOperation(async () => {
816
+ return await this.sql.query(
817
+ `SELECT \`value\` FROM translations WHERE \`lang\`=? AND \`key\`=CONVERT(? USING utf8mb4)`,
818
+ [lang, key],
819
+ );
820
+ }, 'fetch translation');
821
+ if (results.length > 0) {
822
+ const { value } = results[0];
823
+ const data = { value };
824
+ this.insertLocalCache(key, lang, data);
825
+ return data;
826
+ }
827
+ return false;
828
+ } catch (error) {
829
+ const dbError = new translate_types_1.DatabaseError(error, 'fetch translation');
830
+ this.logError(dbError, 'fetch');
831
+ return false; // Don't throw, let translation proceed
832
+ }
833
+ }
834
+ async insert(key, lang, data) {
835
+ try {
836
+ if (!key || !lang || !data) {
837
+ throw new translate_types_1.ValidationError('insert parameters', { key, lang, data });
838
+ }
839
+ await this.retryOperation(async () => {
840
+ return await this.sql.query(`INSERT IGNORE INTO translations (\`key\`, \`lang\`, \`value\`) VALUES (?, ?, ?)`, [
841
+ key,
842
+ lang,
843
+ data.value,
844
+ ]);
845
+ }, 'insert translation');
846
+ } catch (error) {
847
+ const dbError = new translate_types_1.DatabaseError(error, 'insert translation');
848
+ this.logError(dbError, 'insert');
849
+ throw dbError;
850
+ }
851
+ }
852
+ async fetchLang(lang) {
853
+ try {
854
+ if (!lang) {
855
+ throw new translate_types_1.ValidationError('lang', lang);
856
+ }
857
+ return await this.retryOperation(async () => {
858
+ return await this.sql.query(
859
+ `SELECT \`key\`, \`lang\`, \`value\`, \`verified\`, \`created_at\` FROM translations WHERE \`lang\`=?`,
860
+ [lang],
861
+ );
862
+ }, 'fetchLang');
863
+ } catch (error) {
864
+ const dbError = new translate_types_1.DatabaseError(error, 'fetch language translations');
865
+ this.logError(dbError, 'fetchLang');
866
+ throw dbError;
867
+ }
868
+ }
869
+ async fetchAllGrouped() {
870
+ try {
871
+ const output = {};
872
+ const allTranslations = await this.fetchAll();
873
+ allTranslations.forEach((translation) => {
874
+ const { key } = translation;
875
+ if (!output.hasOwnProperty(key)) {
876
+ output[key] = [];
877
+ }
878
+ output[key].push(translation);
879
+ });
880
+ return output;
881
+ } catch (error) {
882
+ const dbError = new translate_types_1.DatabaseError(error, 'fetch all grouped translations');
883
+ this.logError(dbError, 'fetchAllGrouped');
884
+ throw dbError;
885
+ }
886
+ }
887
+ async fetchAll() {
888
+ try {
889
+ return await this.retryOperation(async () => {
890
+ return await this.sql.query(
891
+ `SELECT \`key\`, \`lang\`, \`value\`, \`verified\`, \`created_at\` FROM translations`,
892
+ );
893
+ }, 'fetchAll');
894
+ } catch (error) {
895
+ const dbError = new translate_types_1.DatabaseError(error, 'fetch all translations');
896
+ this.logError(dbError, 'fetchAll');
897
+ throw dbError;
898
+ }
899
+ }
900
+ }
901
+ exports.ZTranslateService = ZTranslateService;
902
+ function strContainsLetters(text) {
903
+ // Updated regex to include Arabic characters (U+0600-U+06FF) and other scripts
904
+ const regExp = /[a-zA-Z\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/g;
905
+ return regExp.test(text);
906
+ }
907
+ function genSpaces(length) {
908
+ let output = '';
909
+ for (let i = 0; i < length; i++) {
910
+ output += ' ';
911
+ }
912
+ return output;
913
+ }