@yxw007/translate 0.0.4 → 0.0.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.
@@ -1,4 +1,4 @@
1
- // translate v0.0.4 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.0.6 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -44,7 +44,7 @@
44
44
  name: "azure",
45
45
  async translate(text, opts) {
46
46
  const { from, to } = opts;
47
- const url = `${base}&to=${to}${from ? `&from=${from}` : ""}`;
47
+ const url = `${base}&to=${to}${from && from !== "auto" ? `&from=${from}` : ""}`;
48
48
  if (!Array.isArray(text)) {
49
49
  text = [text];
50
50
  }
@@ -57,7 +57,11 @@
57
57
  },
58
58
  body: JSON.stringify(text.map((it) => ({ Text: it }))),
59
59
  });
60
- const body = await res.json();
60
+ const bodyRes = await res.json();
61
+ if (bodyRes.error) {
62
+ throw new Error(`Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
63
+ }
64
+ const body = bodyRes;
61
65
  if (!body || body.length === 0) {
62
66
  throw new Error("Translate fail ! translate's result is null or empty");
63
67
  }
@@ -331,6 +335,56 @@
331
335
  };
332
336
  }
333
337
 
338
+ class EndpointCache {
339
+ constructor({ size, params }) {
340
+ this.data = new Map();
341
+ this.parameters = [];
342
+ this.capacity = size ?? 50;
343
+ if (params) {
344
+ this.parameters = params;
345
+ }
346
+ }
347
+ get(endpointParams, resolver) {
348
+ const key = this.hash(endpointParams);
349
+ if (key === false) {
350
+ return resolver();
351
+ }
352
+ if (!this.data.has(key)) {
353
+ if (this.data.size > this.capacity + 10) {
354
+ const keys = this.data.keys();
355
+ let i = 0;
356
+ while (true) {
357
+ const { value, done } = keys.next();
358
+ this.data.delete(value);
359
+ if (done || ++i > 10) {
360
+ break;
361
+ }
362
+ }
363
+ }
364
+ this.data.set(key, resolver());
365
+ }
366
+ return this.data.get(key);
367
+ }
368
+ size() {
369
+ return this.data.size;
370
+ }
371
+ hash(endpointParams) {
372
+ let buffer = "";
373
+ const { parameters } = this;
374
+ if (parameters.length === 0) {
375
+ return false;
376
+ }
377
+ for (const param of parameters) {
378
+ const val = String(endpointParams[param] ?? "");
379
+ if (val.includes("|;")) {
380
+ return false;
381
+ }
382
+ buffer += val + "|;";
383
+ }
384
+ return buffer;
385
+ }
386
+ }
387
+
334
388
  const IP_V4_REGEX = new RegExp(`^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}$`);
335
389
  const isIpAddress = (value) => IP_V4_REGEX.test(value) || (value.startsWith("[") && value.endsWith("]"));
336
390
 
@@ -2501,6 +2555,7 @@
2501
2555
  headers: new Headers(request.headers),
2502
2556
  method: method,
2503
2557
  credentials,
2558
+ cache: this.config.cache ?? "default",
2504
2559
  };
2505
2560
  if (body) {
2506
2561
  requestOptions.duplex = "half";
@@ -2511,6 +2566,9 @@
2511
2566
  if (keepAliveSupport.supported) {
2512
2567
  requestOptions.keepalive = keepAlive;
2513
2568
  }
2569
+ if (typeof this.config.requestInit === "function") {
2570
+ Object.assign(requestOptions, this.config.requestInit(request));
2571
+ }
2514
2572
  let removeSignalEventListener = () => { };
2515
2573
  const fetchRequest = new Request(url, requestOptions);
2516
2574
  const raceOfPromises = [
@@ -4007,7 +4065,7 @@ ${toHex(hashedRequest)}`;
4007
4065
 
4008
4066
  var name = "@aws-sdk/client-translate";
4009
4067
  var description = "AWS SDK for JavaScript Translate Client for Node.js, Browser and React Native";
4010
- var version = "3.645.0";
4068
+ var version = "3.651.1";
4011
4069
  var scripts = {
4012
4070
  build: "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
4013
4071
  "build:cjs": "node ../../scripts/compilation/inline client-translate",
@@ -4026,43 +4084,43 @@ ${toHex(hashedRequest)}`;
4026
4084
  var dependencies = {
4027
4085
  "@aws-crypto/sha256-browser": "5.2.0",
4028
4086
  "@aws-crypto/sha256-js": "5.2.0",
4029
- "@aws-sdk/client-sso-oidc": "3.645.0",
4030
- "@aws-sdk/client-sts": "3.645.0",
4031
- "@aws-sdk/core": "3.635.0",
4032
- "@aws-sdk/credential-provider-node": "3.645.0",
4033
- "@aws-sdk/middleware-host-header": "3.620.0",
4034
- "@aws-sdk/middleware-logger": "3.609.0",
4035
- "@aws-sdk/middleware-recursion-detection": "3.620.0",
4036
- "@aws-sdk/middleware-user-agent": "3.645.0",
4037
- "@aws-sdk/region-config-resolver": "3.614.0",
4038
- "@aws-sdk/types": "3.609.0",
4039
- "@aws-sdk/util-endpoints": "3.645.0",
4040
- "@aws-sdk/util-user-agent-browser": "3.609.0",
4041
- "@aws-sdk/util-user-agent-node": "3.614.0",
4042
- "@smithy/config-resolver": "^3.0.5",
4043
- "@smithy/core": "^2.4.0",
4044
- "@smithy/fetch-http-handler": "^3.2.4",
4045
- "@smithy/hash-node": "^3.0.3",
4046
- "@smithy/invalid-dependency": "^3.0.3",
4047
- "@smithy/middleware-content-length": "^3.0.5",
4048
- "@smithy/middleware-endpoint": "^3.1.0",
4049
- "@smithy/middleware-retry": "^3.0.15",
4050
- "@smithy/middleware-serde": "^3.0.3",
4051
- "@smithy/middleware-stack": "^3.0.3",
4052
- "@smithy/node-config-provider": "^3.1.4",
4053
- "@smithy/node-http-handler": "^3.1.4",
4054
- "@smithy/protocol-http": "^4.1.0",
4055
- "@smithy/smithy-client": "^3.2.0",
4056
- "@smithy/types": "^3.3.0",
4057
- "@smithy/url-parser": "^3.0.3",
4087
+ "@aws-sdk/client-sso-oidc": "3.651.1",
4088
+ "@aws-sdk/client-sts": "3.651.1",
4089
+ "@aws-sdk/core": "3.651.1",
4090
+ "@aws-sdk/credential-provider-node": "3.651.1",
4091
+ "@aws-sdk/middleware-host-header": "3.649.0",
4092
+ "@aws-sdk/middleware-logger": "3.649.0",
4093
+ "@aws-sdk/middleware-recursion-detection": "3.649.0",
4094
+ "@aws-sdk/middleware-user-agent": "3.649.0",
4095
+ "@aws-sdk/region-config-resolver": "3.649.0",
4096
+ "@aws-sdk/types": "3.649.0",
4097
+ "@aws-sdk/util-endpoints": "3.649.0",
4098
+ "@aws-sdk/util-user-agent-browser": "3.649.0",
4099
+ "@aws-sdk/util-user-agent-node": "3.649.0",
4100
+ "@smithy/config-resolver": "^3.0.6",
4101
+ "@smithy/core": "^2.4.1",
4102
+ "@smithy/fetch-http-handler": "^3.2.5",
4103
+ "@smithy/hash-node": "^3.0.4",
4104
+ "@smithy/invalid-dependency": "^3.0.4",
4105
+ "@smithy/middleware-content-length": "^3.0.6",
4106
+ "@smithy/middleware-endpoint": "^3.1.1",
4107
+ "@smithy/middleware-retry": "^3.0.16",
4108
+ "@smithy/middleware-serde": "^3.0.4",
4109
+ "@smithy/middleware-stack": "^3.0.4",
4110
+ "@smithy/node-config-provider": "^3.1.5",
4111
+ "@smithy/node-http-handler": "^3.2.0",
4112
+ "@smithy/protocol-http": "^4.1.1",
4113
+ "@smithy/smithy-client": "^3.3.0",
4114
+ "@smithy/types": "^3.4.0",
4115
+ "@smithy/url-parser": "^3.0.4",
4058
4116
  "@smithy/util-base64": "^3.0.0",
4059
4117
  "@smithy/util-body-length-browser": "^3.0.0",
4060
4118
  "@smithy/util-body-length-node": "^3.0.0",
4061
- "@smithy/util-defaults-mode-browser": "^3.0.15",
4062
- "@smithy/util-defaults-mode-node": "^3.0.15",
4063
- "@smithy/util-endpoints": "^2.0.5",
4064
- "@smithy/util-middleware": "^3.0.3",
4065
- "@smithy/util-retry": "^3.0.3",
4119
+ "@smithy/util-defaults-mode-browser": "^3.0.16",
4120
+ "@smithy/util-defaults-mode-node": "^3.0.16",
4121
+ "@smithy/util-endpoints": "^2.1.0",
4122
+ "@smithy/util-middleware": "^3.0.4",
4123
+ "@smithy/util-retry": "^3.0.4",
4066
4124
  "@smithy/util-utf8": "^3.0.0",
4067
4125
  tslib: "^2.6.2",
4068
4126
  uuid: "^9.0.1"
@@ -4719,11 +4777,15 @@ ${toHex(hashedRequest)}`;
4719
4777
  const _data = { version: "1.0", parameters: { Region: h, UseDualStack: i, UseFIPS: i, Endpoint: h }, rules: [{ conditions: [{ [t]: b, [u]: [j] }], rules: [{ conditions: p, error: "Invalid Configuration: FIPS and custom endpoint are not supported", type: d }, { conditions: q, error: "Invalid Configuration: Dualstack and custom endpoint are not supported", type: d }, { endpoint: { url: j, properties: m, headers: m }, type: e }], type: f }, { conditions: [{ [t]: b, [u]: r }], rules: [{ conditions: [{ [t]: "aws.partition", [u]: r, assign: g }], rules: [{ conditions: [k, l], rules: [{ conditions: [{ [t]: c, [u]: [a, n] }, o], rules: [{ endpoint: { url: "https://translate-fips.{Region}.{PartitionResult#dualStackDnsSuffix}", properties: m, headers: m }, type: e }], type: f }, { error: "FIPS and DualStack are enabled, but this partition does not support one or both", type: d }], type: f }, { conditions: p, rules: [{ conditions: [{ [t]: c, [u]: [n, a] }], rules: [{ endpoint: { url: "https://translate-fips.{Region}.{PartitionResult#dnsSuffix}", properties: m, headers: m }, type: e }], type: f }, { error: "FIPS is enabled but this partition does not support FIPS", type: d }], type: f }, { conditions: q, rules: [{ conditions: [o], rules: [{ endpoint: { url: "https://translate.{Region}.{PartitionResult#dualStackDnsSuffix}", properties: m, headers: m }, type: e }], type: f }, { error: "DualStack is enabled but this partition does not support DualStack", type: d }], type: f }, { endpoint: { url: "https://translate.{Region}.{PartitionResult#dnsSuffix}", properties: m, headers: m }, type: e }], type: f }], type: f }, { error: "Invalid Configuration: Missing Region", type: d }] };
4720
4778
  const ruleSet = _data;
4721
4779
 
4780
+ const cache$1 = new EndpointCache({
4781
+ size: 50,
4782
+ params: ["Endpoint", "Region", "UseDualStack", "UseFIPS"],
4783
+ });
4722
4784
  const defaultEndpointResolver = (endpointParams, context = {}) => {
4723
- return resolveEndpoint(ruleSet, {
4785
+ return cache$1.get(endpointParams, () => resolveEndpoint(ruleSet, {
4724
4786
  endpointParams: endpointParams,
4725
4787
  logger: context.logger,
4726
- });
4788
+ }));
4727
4789
  };
4728
4790
  customEndpointFunctions.aws = awsEndpointFunctions;
4729
4791
 
@@ -5370,9 +5432,7 @@ ${toHex(hashedRequest)}`;
5370
5432
 
5371
5433
  class TranslateTextCommand extends Command
5372
5434
  .classBuilder()
5373
- .ep({
5374
- ...commonParams,
5375
- })
5435
+ .ep(commonParams)
5376
5436
  .m(function (Command, cs, config, o) {
5377
5437
  return [
5378
5438
  getSerdePlugin(config, this.serialize, this.deserialize),
@@ -6742,13 +6802,255 @@ ${toHex(hashedRequest)}`;
6742
6802
  zul: "zu",
6743
6803
  };
6744
6804
 
6745
- const isoKeys = Object.values(iso).sort();
6746
- function language(name) {
6805
+ var languageNames = {
6806
+ afar: "aa",
6807
+ abkhazian: "ab",
6808
+ afrikaans: "af",
6809
+ akan: "ak",
6810
+ albanian: "sq",
6811
+ amharic: "am",
6812
+ arabic: "ar",
6813
+ aragonese: "an",
6814
+ armenian: "hy",
6815
+ assamese: "as",
6816
+ avaric: "av",
6817
+ avestan: "ae",
6818
+ aymara: "ay",
6819
+ azerbaijani: "az",
6820
+ bashkir: "ba",
6821
+ bambara: "bm",
6822
+ basque: "eu",
6823
+ belarusian: "be",
6824
+ bengali: "bn",
6825
+ "bihari languages": "bh",
6826
+ bislama: "bi",
6827
+ tibetan: "bo",
6828
+ bosnian: "bs",
6829
+ breton: "br",
6830
+ bulgarian: "bg",
6831
+ burmese: "my",
6832
+ catalan: "ca",
6833
+ valencian: "ca",
6834
+ czech: "cs",
6835
+ chamorro: "ch",
6836
+ chechen: "ce",
6837
+ chinese: "zh",
6838
+ "church slavic": "cu",
6839
+ "old slavonic": "cu",
6840
+ "church slavonic": "cu",
6841
+ "old bulgarian": "cu",
6842
+ "old church slavonic": "cu",
6843
+ chuvash: "cv",
6844
+ cornish: "kw",
6845
+ corsican: "co",
6846
+ cree: "cr",
6847
+ welsh: "cy",
6848
+ danish: "da",
6849
+ german: "de",
6850
+ divehi: "dv",
6851
+ dhivehi: "dv",
6852
+ maldivian: "dv",
6853
+ dutch: "nl",
6854
+ flemish: "nl",
6855
+ dzongkha: "dz",
6856
+ greek: "el",
6857
+ english: "en",
6858
+ esperanto: "eo",
6859
+ estonian: "et",
6860
+ ewe: "ee",
6861
+ faroese: "fo",
6862
+ persian: "fa",
6863
+ fijian: "fj",
6864
+ finnish: "fi",
6865
+ french: "fr",
6866
+ "western frisian": "fy",
6867
+ fulah: "ff",
6868
+ georgian: "ka",
6869
+ gaelic: "gd",
6870
+ "scottish gaelic": "gd",
6871
+ irish: "ga",
6872
+ galician: "gl",
6873
+ manx: "gv",
6874
+ guarani: "gn",
6875
+ gujarati: "gu",
6876
+ haitian: "ht",
6877
+ "haitian creole": "ht",
6878
+ hausa: "ha",
6879
+ hebrew: "he",
6880
+ herero: "hz",
6881
+ hindi: "hi",
6882
+ "hiri motu": "ho",
6883
+ croatian: "hr",
6884
+ hungarian: "hu",
6885
+ igbo: "ig",
6886
+ icelandic: "is",
6887
+ ido: "io",
6888
+ "sichuan yi": "ii",
6889
+ nuosu: "ii",
6890
+ inuktitut: "iu",
6891
+ interlingue: "ie",
6892
+ occidental: "ie",
6893
+ interlingua: "ia",
6894
+ indonesian: "id",
6895
+ inupiaq: "ik",
6896
+ italian: "it",
6897
+ javanese: "jv",
6898
+ japanese: "ja",
6899
+ kalaallisut: "kl",
6900
+ greenlandic: "kl",
6901
+ kannada: "kn",
6902
+ kashmiri: "ks",
6903
+ kanuri: "kr",
6904
+ kazakh: "kk",
6905
+ "central khmer": "km",
6906
+ kikuyu: "ki",
6907
+ gikuyu: "ki",
6908
+ kinyarwanda: "rw",
6909
+ kirghiz: "ky",
6910
+ kyrgyz: "ky",
6911
+ komi: "kv",
6912
+ kongo: "kg",
6913
+ korean: "ko",
6914
+ kuanyama: "kj",
6915
+ kwanyama: "kj",
6916
+ kurdish: "ku",
6917
+ lao: "lo",
6918
+ latin: "la",
6919
+ latvian: "lv",
6920
+ limburgan: "li",
6921
+ limburger: "li",
6922
+ limburgish: "li",
6923
+ lingala: "ln",
6924
+ lithuanian: "lt",
6925
+ luxembourgish: "lb",
6926
+ letzeburgesch: "lb",
6927
+ "luba-katanga": "lu",
6928
+ ganda: "lg",
6929
+ macedonian: "mk",
6930
+ marshallese: "mh",
6931
+ malayalam: "ml",
6932
+ maori: "mi",
6933
+ marathi: "mr",
6934
+ malay: "ms",
6935
+ malagasy: "mg",
6936
+ maltese: "mt",
6937
+ mongolian: "mn",
6938
+ nauru: "na",
6939
+ navajo: "nv",
6940
+ navaho: "nv",
6941
+ "ndebele, south": "nr",
6942
+ "south ndebele": "nr",
6943
+ "ndebele, north": "nd",
6944
+ "north ndebele": "nd",
6945
+ ndonga: "ng",
6946
+ nepali: "ne",
6947
+ "norwegian nynorsk": "nn",
6948
+ "nynorsk, norwegian": "nn",
6949
+ "norwegian bokmål": "nb",
6950
+ "bokmål, norwegian": "nb",
6951
+ norwegian: "no",
6952
+ chichewa: "ny",
6953
+ chewa: "ny",
6954
+ nyanja: "ny",
6955
+ occitan: "oc",
6956
+ ojibwa: "oj",
6957
+ oriya: "or",
6958
+ oromo: "om",
6959
+ ossetian: "os",
6960
+ ossetic: "os",
6961
+ panjabi: "pa",
6962
+ punjabi: "pa",
6963
+ pali: "pi",
6964
+ polish: "pl",
6965
+ portuguese: "pt",
6966
+ pushto: "ps",
6967
+ pashto: "ps",
6968
+ quechua: "qu",
6969
+ romansh: "rm",
6970
+ romanian: "ro",
6971
+ moldavian: "ro",
6972
+ moldovan: "ro",
6973
+ rundi: "rn",
6974
+ russian: "ru",
6975
+ sango: "sg",
6976
+ sanskrit: "sa",
6977
+ sinhala: "si",
6978
+ sinhalese: "si",
6979
+ slovak: "sk",
6980
+ slovenian: "sl",
6981
+ "northern sami": "se",
6982
+ samoan: "sm",
6983
+ shona: "sn",
6984
+ sindhi: "sd",
6985
+ somali: "so",
6986
+ "sotho, southern": "st",
6987
+ spanish: "es",
6988
+ castilian: "es",
6989
+ sardinian: "sc",
6990
+ serbian: "sr",
6991
+ swati: "ss",
6992
+ sundanese: "su",
6993
+ swahili: "sw",
6994
+ swedish: "sv",
6995
+ tahitian: "ty",
6996
+ tamil: "ta",
6997
+ tatar: "tt",
6998
+ telugu: "te",
6999
+ tajik: "tg",
7000
+ tagalog: "tl",
7001
+ thai: "th",
7002
+ tigrinya: "ti",
7003
+ tonga: "to",
7004
+ tswana: "tn",
7005
+ tsonga: "ts",
7006
+ turkmen: "tk",
7007
+ turkish: "tr",
7008
+ twi: "tw",
7009
+ uighur: "ug",
7010
+ uyghur: "ug",
7011
+ ukrainian: "uk",
7012
+ urdu: "ur",
7013
+ uzbek: "uz",
7014
+ venda: "ve",
7015
+ vietnamese: "vi",
7016
+ volapük: "vo",
7017
+ walloon: "wa",
7018
+ wolof: "wo",
7019
+ xhosa: "xh",
7020
+ yiddish: "yi",
7021
+ yoruba: "yo",
7022
+ zhuang: "za",
7023
+ chuang: "za",
7024
+ zulu: "zu",
7025
+ };
7026
+
7027
+ const nameKeys = Object.keys(languageNames).sort();
7028
+ const isoKeys = Object.keys(iso).sort();
7029
+ const iosValues = Object.values(iso);
7030
+ function isValidLanguage(name) {
7031
+ if (!name) {
7032
+ return false;
7033
+ }
7034
+ if (name === "auto") {
7035
+ return true;
7036
+ }
6747
7037
  if (name.length > 100) {
6748
7038
  throw new Error(`The "language" is too long at ${name.length} characters`);
6749
7039
  }
6750
- if (!isoKeys.includes(name)) {
6751
- throw new Error(`The language "${name}" is not part of the ISO 639-1`);
7040
+ return isoKeys.includes(name) || nameKeys.includes(name) || iosValues.includes(name);
7041
+ }
7042
+ function getISO(name) {
7043
+ if (name === "auto") {
7044
+ return name;
7045
+ }
7046
+ if (nameKeys.includes(name)) {
7047
+ return languageNames[name];
7048
+ }
7049
+ if (isoKeys.includes(name)) {
7050
+ return iso[name];
7051
+ }
7052
+ if (iosValues.includes(name)) {
7053
+ return name;
6752
7054
  }
6753
7055
  return name;
6754
7056
  }
@@ -6845,19 +7147,25 @@ ${toHex(hashedRequest)}`;
6845
7147
  this.engines.set(engine.name, engine);
6846
7148
  }
6847
7149
  translate(text, options) {
6848
- const { from, to, engine = "google", cache_time = 60 * 1000 } = options;
7150
+ const { engine = "google", cache_time = 60 * 1000 } = options;
7151
+ let { from = "auto", to } = options;
7152
+ from = options.from = getISO(from);
7153
+ to = options.to = getISO(to);
6849
7154
  //1. Check if engine exists
6850
7155
  if (!this.engines.has(engine)) {
6851
7156
  throw new Error(`Engine ${engine} not found`);
6852
7157
  }
6853
7158
  //2. Check if language exists
6854
- if (!language(to)) {
6855
- throw new Error(`Language ${to} not found`);
7159
+ if (!isValidLanguage(to)) {
7160
+ throw new Error(`The language "${to}" is not part of the ISO 639-1 or is not part of the language names`);
7161
+ }
7162
+ if (!isValidLanguage(from)) {
7163
+ throw new Error(`The language "${from}" is not part of the ISO 639-1 or is not part of the language names`);
6856
7164
  }
6857
7165
  const key = `${from}:${to}:${engine}:${text}`;
6858
7166
  //3. If the cache is matched, the cache is used directly
6859
7167
  if (cache.get(key)) {
6860
- return cache.get(key)?.value;
7168
+ return Promise.resolve(cache.get(key)?.value);
6861
7169
  }
6862
7170
  const engineInstance = this.engines.get(engine);
6863
7171
  if (!engineInstance) {
@@ -6873,11 +7181,14 @@ ${toHex(hashedRequest)}`;
6873
7181
  var index = {
6874
7182
  engines,
6875
7183
  translator,
7184
+ Cache,
7185
+ languageNames,
6876
7186
  };
6877
7187
 
6878
7188
  exports.Cache = Cache;
6879
7189
  exports.default = index;
6880
7190
  exports.engines = engines;
7191
+ exports.languageNames = languageNames;
6881
7192
  exports.translator = translator;
6882
7193
 
6883
7194
  Object.defineProperty(exports, '__esModule', { value: true });