@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
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -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.649.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,10 +4082,10 @@ 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.649.0",
4028
- "@aws-sdk/client-sts": "3.649.0",
4029
- "@aws-sdk/core": "3.649.0",
4030
- "@aws-sdk/credential-provider-node": "3.649.0",
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",
4031
4089
  "@aws-sdk/middleware-host-header": "3.649.0",
4032
4090
  "@aws-sdk/middleware-logger": "3.649.0",
4033
4091
  "@aws-sdk/middleware-recursion-detection": "3.649.0",
@@ -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),
@@ -7103,7 +7163,7 @@ class Translator {
7103
7163
  const key = `${from}:${to}:${engine}:${text}`;
7104
7164
  //3. If the cache is matched, the cache is used directly
7105
7165
  if (cache.get(key)) {
7106
- return cache.get(key)?.value;
7166
+ return Promise.resolve(cache.get(key)?.value);
7107
7167
  }
7108
7168
  const engineInstance = this.engines.get(engine);
7109
7169
  if (!engineInstance) {