@yxw007/translate 0.0.5 → 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.5 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) :
@@ -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.649.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,10 +4084,10 @@ ${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.649.0",
4030
- "@aws-sdk/client-sts": "3.649.0",
4031
- "@aws-sdk/core": "3.649.0",
4032
- "@aws-sdk/credential-provider-node": "3.649.0",
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",
4033
4091
  "@aws-sdk/middleware-host-header": "3.649.0",
4034
4092
  "@aws-sdk/middleware-logger": "3.649.0",
4035
4093
  "@aws-sdk/middleware-recursion-detection": "3.649.0",
@@ -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),
@@ -7105,7 +7165,7 @@ ${toHex(hashedRequest)}`;
7105
7165
  const key = `${from}:${to}:${engine}:${text}`;
7106
7166
  //3. If the cache is matched, the cache is used directly
7107
7167
  if (cache.get(key)) {
7108
- return cache.get(key)?.value;
7168
+ return Promise.resolve(cache.get(key)?.value);
7109
7169
  }
7110
7170
  const engineInstance = this.engines.get(engine);
7111
7171
  if (!engineInstance) {