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