@yxw007/translate 0.0.2 → 0.0.4

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.2 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.0.4 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) :
@@ -10,7 +10,7 @@
10
10
  return {
11
11
  name: "google",
12
12
  async translate(text, opts) {
13
- const { from, to } = opts;
13
+ const { from = "auto", to } = opts;
14
14
  if (!Array.isArray(text)) {
15
15
  text = [text];
16
16
  }
@@ -44,7 +44,7 @@
44
44
  name: "azure",
45
45
  async translate(text, opts) {
46
46
  const { from, to } = opts;
47
- const url = `${base}&from=${from}&to=${to}`;
47
+ const url = `${base}&to=${to}${from ? `&from=${from}` : ""}`;
48
48
  if (!Array.isArray(text)) {
49
49
  text = [text];
50
50
  }
@@ -729,16 +729,6 @@
729
729
  }
730
730
  }
731
731
  const endpoint = evaluateRules(rules, { endpointParams, logger, referenceRecord: {} });
732
- if (options.endpointParams?.Endpoint) {
733
- try {
734
- const givenEndpoint = new URL(options.endpointParams.Endpoint);
735
- const { protocol, port } = givenEndpoint;
736
- endpoint.url.protocol = protocol;
737
- endpoint.url.port = port;
738
- }
739
- catch (e) {
740
- }
741
- }
742
732
  options.logger?.debug?.(`${debugId} Resolved endpoint: ${toDebugString(endpoint)}`);
743
733
  return endpoint;
744
734
  };
@@ -1359,7 +1349,13 @@
1359
1349
 
1360
1350
  const getEndpointFromInstructions = async (commandInput, instructionsSupplier, clientConfig, context) => {
1361
1351
  if (!clientConfig.endpoint) {
1362
- const endpointFromConfig = await getEndpointFromConfig(clientConfig.serviceId || "");
1352
+ let endpointFromConfig;
1353
+ if (clientConfig.serviceConfiguredEndpoint) {
1354
+ endpointFromConfig = await clientConfig.serviceConfiguredEndpoint();
1355
+ }
1356
+ else {
1357
+ endpointFromConfig = await getEndpointFromConfig(clientConfig.serviceId);
1358
+ }
1363
1359
  if (endpointFromConfig) {
1364
1360
  clientConfig.endpoint = () => Promise.resolve(toEndpointV1(endpointFromConfig));
1365
1361
  }
@@ -1513,7 +1509,7 @@
1513
1509
  const { endpoint } = input;
1514
1510
  const customEndpointProvider = endpoint != null ? async () => toEndpointV1(await normalizeProvider$1(endpoint)()) : undefined;
1515
1511
  const isCustomEndpoint = !!endpoint;
1516
- return {
1512
+ const resolvedConfig = {
1517
1513
  ...input,
1518
1514
  endpoint: customEndpointProvider,
1519
1515
  tls,
@@ -1521,6 +1517,14 @@
1521
1517
  useDualstackEndpoint: normalizeProvider$1(input.useDualstackEndpoint ?? false),
1522
1518
  useFipsEndpoint: normalizeProvider$1(input.useFipsEndpoint ?? false),
1523
1519
  };
1520
+ let configuredEndpointPromise = undefined;
1521
+ resolvedConfig.serviceConfiguredEndpoint = async () => {
1522
+ if (input.serviceId && !configuredEndpointPromise) {
1523
+ configuredEndpointPromise = getEndpointFromConfig(input.serviceId);
1524
+ }
1525
+ return configuredEndpointPromise;
1526
+ };
1527
+ return resolvedConfig;
1524
1528
  };
1525
1529
 
1526
1530
  const httpAuthSchemeEndpointRuleSetMiddlewareOptions = {
@@ -2212,13 +2216,31 @@
2212
2216
 
2213
2217
  class Client {
2214
2218
  constructor(config) {
2215
- this.middlewareStack = constructStack();
2216
2219
  this.config = config;
2220
+ this.middlewareStack = constructStack();
2217
2221
  }
2218
2222
  send(command, optionsOrCb, cb) {
2219
2223
  const options = typeof optionsOrCb !== "function" ? optionsOrCb : undefined;
2220
2224
  const callback = typeof optionsOrCb === "function" ? optionsOrCb : cb;
2221
- const handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2225
+ const useHandlerCache = options === undefined && this.config.cacheMiddleware === true;
2226
+ let handler;
2227
+ if (useHandlerCache) {
2228
+ if (!this.handlers) {
2229
+ this.handlers = new WeakMap();
2230
+ }
2231
+ const handlers = this.handlers;
2232
+ if (handlers.has(command.constructor)) {
2233
+ handler = handlers.get(command.constructor);
2234
+ }
2235
+ else {
2236
+ handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2237
+ handlers.set(command.constructor, handler);
2238
+ }
2239
+ }
2240
+ else {
2241
+ delete this.handlers;
2242
+ handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2243
+ }
2222
2244
  if (callback) {
2223
2245
  handler(command)
2224
2246
  .then((result) => callback(null, result.output), (err) => callback(err))
@@ -2229,8 +2251,8 @@
2229
2251
  }
2230
2252
  }
2231
2253
  destroy() {
2232
- if (this.config.requestHandler.destroy)
2233
- this.config.requestHandler.destroy();
2254
+ this.config?.requestHandler?.destroy?.();
2255
+ delete this.handlers;
2234
2256
  }
2235
2257
  }
2236
2258
 
@@ -5370,7 +5392,7 @@ ${toHex(hashedRequest)}`;
5370
5392
  return {
5371
5393
  name: "amazon",
5372
5394
  async translate(text, opts) {
5373
- const { from, to } = opts;
5395
+ const { from = "auto", to } = opts;
5374
5396
  const translateClient = new TranslateClient({ region: region, credentials: { accessKeyId, secretAccessKey } });
5375
5397
  if (!Array.isArray(text)) {
5376
5398
  text = [text];
@@ -6489,7 +6511,7 @@ ${toHex(hashedRequest)}`;
6489
6511
  return {
6490
6512
  name: "baidu",
6491
6513
  async translate(text, opts) {
6492
- const { to, from, domain = "it" } = opts;
6514
+ const { to, from = "auto", domain = "it" } = opts;
6493
6515
  if (!Array.isArray(text)) {
6494
6516
  text = [text];
6495
6517
  }
@@ -6829,9 +6851,6 @@ ${toHex(hashedRequest)}`;
6829
6851
  throw new Error(`Engine ${engine} not found`);
6830
6852
  }
6831
6853
  //2. Check if language exists
6832
- if (!language(from)) {
6833
- throw new Error(`Language ${from} not found`);
6834
- }
6835
6854
  if (!language(to)) {
6836
6855
  throw new Error(`Language ${to} not found`);
6837
6856
  }