@yxw007/translate 0.2.2 → 0.3.0

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.
@@ -1,4 +1,4 @@
1
- // translate v0.2.2 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.3.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -7,6 +7,7 @@ require('fs/promises');
7
7
  require('fs');
8
8
  require('path');
9
9
  var clientTranslate = require('@aws-sdk/client-translate');
10
+ var crypto = require('crypto');
10
11
 
11
12
  class TranslationError extends Error {
12
13
  region;
@@ -1595,6 +1596,121 @@ function openai$1(options) {
1595
1596
  };
1596
1597
  }
1597
1598
 
1599
+ function sha256(message, secret = "", encoding) {
1600
+ if (encoding) {
1601
+ return crypto.createHmac("sha256", secret).update(message).digest(encoding);
1602
+ }
1603
+ return crypto.createHmac("sha256", secret).update(message).digest();
1604
+ }
1605
+ function getHash(message, encoding = "hex") {
1606
+ return crypto.createHash("sha256").update(message).digest(encoding);
1607
+ }
1608
+ function getDate(timestamp) {
1609
+ const date = new Date(timestamp * 1000);
1610
+ return `${date.getUTCFullYear()}-${("0" + (date.getUTCMonth() + 1)).slice(-2)}-${("0" + date.getUTCDate()).slice(-2)}`;
1611
+ }
1612
+ function buildAuthorization({ secretId, secretKey, service, host, payload, httpRequestMethod, action, apiVersion, region }) {
1613
+ const timestamp = Math.floor(Date.now() / 1000);
1614
+ const date = getDate(timestamp);
1615
+ const canonicalUri = "/";
1616
+ const canonicalQueryString = "";
1617
+ const canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:" + host + "\n";
1618
+ const signedHeaders = "content-type;host";
1619
+ const hashedRequestPayload = getHash(payload);
1620
+ const canonicalRequest = [
1621
+ httpRequestMethod,
1622
+ canonicalUri,
1623
+ canonicalQueryString,
1624
+ canonicalHeaders,
1625
+ signedHeaders,
1626
+ hashedRequestPayload,
1627
+ ].join("\n");
1628
+ const algorithm = "TC3-HMAC-SHA256";
1629
+ const credentialScope = `${date}/${service}/tc3_request`;
1630
+ const hashedCanonicalRequest = getHash(canonicalRequest);
1631
+ const stringToSign = [algorithm, timestamp, credentialScope, hashedCanonicalRequest].join("\n");
1632
+ const kDate = sha256(date, "TC3" + secretKey);
1633
+ const kService = sha256(service, kDate);
1634
+ const kSigning = sha256("tc3_request", kService);
1635
+ const signature = sha256(stringToSign, kSigning, "hex");
1636
+ const authorization = `${algorithm} Credential=${secretId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
1637
+ const headers = {
1638
+ Authorization: authorization,
1639
+ "Content-Type": "application/json; charset=utf-8",
1640
+ Host: host,
1641
+ "X-TC-Action": action,
1642
+ "X-TC-Timestamp": timestamp,
1643
+ "X-TC-Version": apiVersion,
1644
+ };
1645
+ if (region)
1646
+ headers["X-TC-Region"] = region;
1647
+ return headers;
1648
+ }
1649
+ function tencent$2(options) {
1650
+ const { secretId, secretKey, region = "ap-guangzhou" } = options;
1651
+ const name = "tencent";
1652
+ const host = "tmt.tencentcloudapi.com";
1653
+ const endpoint = `https://${host}/`;
1654
+ const service = "tmt";
1655
+ const apiVersion = "2018-03-21";
1656
+ const action = "TextTranslate";
1657
+ function checkOptions() {
1658
+ if (!secretId || !secretKey) {
1659
+ throw new TranslationError(name, `${name} secretId and secretKey are required`);
1660
+ }
1661
+ }
1662
+ return {
1663
+ name,
1664
+ async translate(text, opts) {
1665
+ checkOptions();
1666
+ const { from = "auto", to } = opts;
1667
+ const source = from === "auto" ? "" : from;
1668
+ const payloadObj = {
1669
+ SourceText: Array.isArray(text) ? text.join("\n") : text,
1670
+ Source: source,
1671
+ Target: to,
1672
+ ProjectId: 0,
1673
+ };
1674
+ const payload = JSON.stringify(payloadObj);
1675
+ const headers = buildAuthorization({
1676
+ secretId,
1677
+ secretKey,
1678
+ service,
1679
+ host,
1680
+ payload,
1681
+ httpRequestMethod: "POST",
1682
+ action,
1683
+ apiVersion,
1684
+ region,
1685
+ });
1686
+ try {
1687
+ const res = await fetch(endpoint, {
1688
+ method: "POST",
1689
+ headers,
1690
+ body: payload,
1691
+ });
1692
+ if (!res.ok) {
1693
+ throw new TranslationError(name, `HTTP ${res.status}: ${await res.text()}`);
1694
+ }
1695
+ const data = await res.json();
1696
+ if (data.Response?.Error) {
1697
+ throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
1698
+ }
1699
+ const translatedResults = data.Response?.TargetText.split("\n") ?? [];
1700
+ if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
1701
+ throw new TranslationError(name, "Translate fail! No result returned");
1702
+ }
1703
+ return translatedResults;
1704
+ }
1705
+ catch (error) {
1706
+ if (error instanceof TranslationError)
1707
+ throw error;
1708
+ throw new TranslationError(name, `Translation failed: ${error}`);
1709
+ }
1710
+ },
1711
+ };
1712
+ }
1713
+
1598
1714
  const engines = {
1599
1715
  google,
1600
1716
  azure: azure$1,
@@ -1602,6 +1718,7 @@ const engines = {
1602
1718
  baidu: baidu$1,
1603
1719
  deepl: deepl$2,
1604
1720
  openai: openai$1,
1721
+ tencent: tencent$2,
1605
1722
  };
1606
1723
 
1607
1724
  var azure = {
@@ -2235,6 +2352,28 @@ var deepl$1 = {
2235
2352
  Ukrainian: "uk",
2236
2353
  };
2237
2354
 
2355
+ var tencent$1 = {
2356
+ Auto: "auto",
2357
+ Chinese: "zh",
2358
+ TraditionalChinese: "zh-TW",
2359
+ English: "en",
2360
+ Japanese: "ja",
2361
+ Korean: "ko",
2362
+ French: "fr",
2363
+ Spanish: "es",
2364
+ Italian: "it",
2365
+ German: "de",
2366
+ Turkish: "tr",
2367
+ Russian: "ru",
2368
+ Portuguese: "pt",
2369
+ Vietnamese: "vi",
2370
+ Indonesian: "id",
2371
+ Thai: "th",
2372
+ Malay: "ms",
2373
+ Arabic: "ar",
2374
+ Hindi: "hi",
2375
+ };
2376
+
2238
2377
  var amazon = {
2239
2378
  Afrikaans: "af",
2240
2379
  Albanian: "sq",
@@ -2320,6 +2459,7 @@ const originLanguages = {
2320
2459
  deepl: deepl$1,
2321
2460
  amazon: amazon,
2322
2461
  openai: openai,
2462
+ tencent: tencent$1,
2323
2463
  };
2324
2464
 
2325
2465
  var deepl = {
@@ -2361,6 +2501,27 @@ var deepl = {
2361
2501
  Ukrainian: "uk",
2362
2502
  };
2363
2503
 
2504
+ var tencent = {
2505
+ "Simplified Chinese": "zh",
2506
+ "Traditional Chinese": "zh-TW",
2507
+ English: "en",
2508
+ Japanese: "ja",
2509
+ Korean: "ko",
2510
+ French: "fr",
2511
+ Spanish: "es",
2512
+ Italian: "it",
2513
+ German: "de",
2514
+ Turkish: "tr",
2515
+ Russian: "ru",
2516
+ Portuguese: "pt",
2517
+ Vietnamese: "vi",
2518
+ Indonesian: "id",
2519
+ Thai: "th",
2520
+ Malay: "ms",
2521
+ Arabic: "ar",
2522
+ Hindi: "hi",
2523
+ };
2524
+
2364
2525
  const targetLanguages = {
2365
2526
  azure: azure,
2366
2527
  google: openai,
@@ -2368,6 +2529,7 @@ const targetLanguages = {
2368
2529
  deepl: deepl,
2369
2530
  amazon: amazon,
2370
2531
  openai: openai,
2532
+ tencent: tencent,
2371
2533
  };
2372
2534
 
2373
2535
  function normalFromLanguage(from, engine) {