@yxw007/translate 0.1.4 → 0.1.6
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.
- package/dist/browser/index.cjs +111 -88
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +111 -88
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/index.esm.min.js +1 -1
- package/dist/browser/index.esm.min.js.map +1 -1
- package/dist/browser/index.min.cjs +1 -1
- package/dist/browser/index.min.cjs.map +1 -1
- package/dist/browser/index.umd.js +111 -88
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/browser/index.umd.min.js +1 -1
- package/dist/browser/index.umd.min.js.map +1 -1
- package/dist/node/index.cjs +111 -88
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +111 -88
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/browser/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.1.
|
|
1
|
+
// translate v0.1.6 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -40,6 +40,101 @@ const OPEN_AI_MODELS = [
|
|
|
40
40
|
"gpt-3.5-turbo-instruct-0914",
|
|
41
41
|
];
|
|
42
42
|
|
|
43
|
+
class Cache {
|
|
44
|
+
cache;
|
|
45
|
+
constructor() {
|
|
46
|
+
this.cache = new Map();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param key
|
|
50
|
+
* @param value
|
|
51
|
+
* @param time millisecond
|
|
52
|
+
*/
|
|
53
|
+
set(key, value, time) {
|
|
54
|
+
if (time <= 0) {
|
|
55
|
+
throw new Error("time must be greater than 0");
|
|
56
|
+
}
|
|
57
|
+
const oldRecord = this.cache.get(key);
|
|
58
|
+
if (oldRecord) {
|
|
59
|
+
clearTimeout(oldRecord.timeout);
|
|
60
|
+
}
|
|
61
|
+
const record = {
|
|
62
|
+
value,
|
|
63
|
+
expire: Date.now() + time,
|
|
64
|
+
};
|
|
65
|
+
if (!isNaN(record.expire)) {
|
|
66
|
+
record.timeout = setTimeout(() => this.del(key), time);
|
|
67
|
+
}
|
|
68
|
+
this.cache.set(key, record);
|
|
69
|
+
}
|
|
70
|
+
get(key) {
|
|
71
|
+
return this.cache.get(key);
|
|
72
|
+
}
|
|
73
|
+
delete(key) {
|
|
74
|
+
let canDelete = true;
|
|
75
|
+
const oldRecord = this.cache.get(key);
|
|
76
|
+
if (oldRecord) {
|
|
77
|
+
if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {
|
|
78
|
+
canDelete = false;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
clearTimeout(oldRecord.timeout);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
canDelete = false;
|
|
86
|
+
}
|
|
87
|
+
if (canDelete) {
|
|
88
|
+
this.del(key);
|
|
89
|
+
}
|
|
90
|
+
return canDelete;
|
|
91
|
+
}
|
|
92
|
+
del(key) {
|
|
93
|
+
this.cache.delete(key);
|
|
94
|
+
}
|
|
95
|
+
clear() {
|
|
96
|
+
for (const [, val] of this.cache.entries()) {
|
|
97
|
+
clearTimeout(val.timeout);
|
|
98
|
+
}
|
|
99
|
+
this.cache.clear();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function useLogger(name = "") {
|
|
104
|
+
return {
|
|
105
|
+
log(...args) {
|
|
106
|
+
this.info(...args);
|
|
107
|
+
},
|
|
108
|
+
info(...args) {
|
|
109
|
+
console.log(`[translate ${name}]`, ...args);
|
|
110
|
+
},
|
|
111
|
+
error(...args) {
|
|
112
|
+
console.error(`[translate ${name}]`, ...args);
|
|
113
|
+
},
|
|
114
|
+
warn(...args) {
|
|
115
|
+
console.warn(`[translate ${name}]`, ...args);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function getGapLine() {
|
|
121
|
+
return "-".repeat(20);
|
|
122
|
+
}
|
|
123
|
+
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
124
|
+
if (e instanceof TypeError) {
|
|
125
|
+
return prefix + (e.cause?.message ?? e.message);
|
|
126
|
+
}
|
|
127
|
+
return prefix + e.message;
|
|
128
|
+
}
|
|
129
|
+
async function throwResponseError(name, res) {
|
|
130
|
+
let bodyRes = null;
|
|
131
|
+
try {
|
|
132
|
+
bodyRes = await res.json();
|
|
133
|
+
}
|
|
134
|
+
catch (e) { }
|
|
135
|
+
return new TranslationError(name, `Translate fail ! ${res.status}: ${res.statusText} ${bodyRes?.message ?? ""}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
43
138
|
function google(options) {
|
|
44
139
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
45
140
|
return {
|
|
@@ -52,6 +147,9 @@ function google(options) {
|
|
|
52
147
|
const textStr = text.join("\n");
|
|
53
148
|
const url = `${base}?client=gtx&sl=${from}&tl=${to}&dt=t&q=${encodeURI(textStr)}`;
|
|
54
149
|
const res = await fetch(url);
|
|
150
|
+
if (!res.ok) {
|
|
151
|
+
throw await throwResponseError(this.name, res);
|
|
152
|
+
}
|
|
55
153
|
const body = await res.json();
|
|
56
154
|
if (!body || body.length === 0) {
|
|
57
155
|
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
@@ -100,6 +198,9 @@ function azure$1(options) {
|
|
|
100
198
|
},
|
|
101
199
|
body: JSON.stringify(text.map((it) => ({ Text: it }))),
|
|
102
200
|
});
|
|
201
|
+
if (!res.ok) {
|
|
202
|
+
throw await throwResponseError(this.name, res);
|
|
203
|
+
}
|
|
103
204
|
const bodyRes = await res.json();
|
|
104
205
|
if (bodyRes.error) {
|
|
105
206
|
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
@@ -1313,6 +1414,9 @@ function baidu$1(options) {
|
|
|
1313
1414
|
},
|
|
1314
1415
|
body: body.toString(),
|
|
1315
1416
|
});
|
|
1417
|
+
if (!res.ok) {
|
|
1418
|
+
throw await throwResponseError(this.name, res);
|
|
1419
|
+
}
|
|
1316
1420
|
const data = await res.json();
|
|
1317
1421
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1318
1422
|
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
@@ -1361,6 +1465,9 @@ function deepl$2(options) {
|
|
|
1361
1465
|
target_lang: to,
|
|
1362
1466
|
}),
|
|
1363
1467
|
});
|
|
1468
|
+
if (!res.ok) {
|
|
1469
|
+
throw await throwResponseError(this.name, res);
|
|
1470
|
+
}
|
|
1364
1471
|
const bodyRes = await res.json();
|
|
1365
1472
|
if (bodyRes.error) {
|
|
1366
1473
|
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
@@ -1375,93 +1482,6 @@ function deepl$2(options) {
|
|
|
1375
1482
|
};
|
|
1376
1483
|
}
|
|
1377
1484
|
|
|
1378
|
-
class Cache {
|
|
1379
|
-
cache;
|
|
1380
|
-
constructor() {
|
|
1381
|
-
this.cache = new Map();
|
|
1382
|
-
}
|
|
1383
|
-
/**
|
|
1384
|
-
* @param key
|
|
1385
|
-
* @param value
|
|
1386
|
-
* @param time millisecond
|
|
1387
|
-
*/
|
|
1388
|
-
set(key, value, time) {
|
|
1389
|
-
if (time <= 0) {
|
|
1390
|
-
throw new Error("time must be greater than 0");
|
|
1391
|
-
}
|
|
1392
|
-
const oldRecord = this.cache.get(key);
|
|
1393
|
-
if (oldRecord) {
|
|
1394
|
-
clearTimeout(oldRecord.timeout);
|
|
1395
|
-
}
|
|
1396
|
-
const record = {
|
|
1397
|
-
value,
|
|
1398
|
-
expire: Date.now() + time,
|
|
1399
|
-
};
|
|
1400
|
-
if (!isNaN(record.expire)) {
|
|
1401
|
-
record.timeout = setTimeout(() => this.del(key), time);
|
|
1402
|
-
}
|
|
1403
|
-
this.cache.set(key, record);
|
|
1404
|
-
}
|
|
1405
|
-
get(key) {
|
|
1406
|
-
return this.cache.get(key);
|
|
1407
|
-
}
|
|
1408
|
-
delete(key) {
|
|
1409
|
-
let canDelete = true;
|
|
1410
|
-
const oldRecord = this.cache.get(key);
|
|
1411
|
-
if (oldRecord) {
|
|
1412
|
-
if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {
|
|
1413
|
-
canDelete = false;
|
|
1414
|
-
}
|
|
1415
|
-
else {
|
|
1416
|
-
clearTimeout(oldRecord.timeout);
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
else {
|
|
1420
|
-
canDelete = false;
|
|
1421
|
-
}
|
|
1422
|
-
if (canDelete) {
|
|
1423
|
-
this.del(key);
|
|
1424
|
-
}
|
|
1425
|
-
return canDelete;
|
|
1426
|
-
}
|
|
1427
|
-
del(key) {
|
|
1428
|
-
this.cache.delete(key);
|
|
1429
|
-
}
|
|
1430
|
-
clear() {
|
|
1431
|
-
for (const [, val] of this.cache.entries()) {
|
|
1432
|
-
clearTimeout(val.timeout);
|
|
1433
|
-
}
|
|
1434
|
-
this.cache.clear();
|
|
1435
|
-
}
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1438
|
-
function useLogger(name = "") {
|
|
1439
|
-
return {
|
|
1440
|
-
log(...args) {
|
|
1441
|
-
this.info(...args);
|
|
1442
|
-
},
|
|
1443
|
-
info(...args) {
|
|
1444
|
-
console.log(`[translate ${name}]`, ...args);
|
|
1445
|
-
},
|
|
1446
|
-
error(...args) {
|
|
1447
|
-
console.error(`[translate ${name}]`, ...args);
|
|
1448
|
-
},
|
|
1449
|
-
warn(...args) {
|
|
1450
|
-
console.warn(`[translate ${name}]`, ...args);
|
|
1451
|
-
},
|
|
1452
|
-
};
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
function getGapLine() {
|
|
1456
|
-
return "-".repeat(20);
|
|
1457
|
-
}
|
|
1458
|
-
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
1459
|
-
if (e instanceof TypeError) {
|
|
1460
|
-
return prefix + (e.cause?.message ?? e.message);
|
|
1461
|
-
}
|
|
1462
|
-
return prefix + e.message;
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
1485
|
const logger$1 = useLogger("openai");
|
|
1466
1486
|
function openai$1(options) {
|
|
1467
1487
|
const { apiKey, model, maxTokens = 2000, outputLog = false } = options;
|
|
@@ -1516,6 +1536,9 @@ function openai$1(options) {
|
|
|
1516
1536
|
temperature: 0,
|
|
1517
1537
|
}),
|
|
1518
1538
|
});
|
|
1539
|
+
if (!res.ok) {
|
|
1540
|
+
throw await throwResponseError(this.name, res);
|
|
1541
|
+
}
|
|
1519
1542
|
const bodyRes = await res.json();
|
|
1520
1543
|
if (bodyRes.error) {
|
|
1521
1544
|
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|