@yxw007/translate 0.0.2 → 0.0.3

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.3 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -742,16 +742,6 @@ const resolveEndpoint = (ruleSetObject, options) => {
742
742
  }
743
743
  }
744
744
  const endpoint = evaluateRules(rules, { endpointParams, logger, referenceRecord: {} });
745
- if (options.endpointParams?.Endpoint) {
746
- try {
747
- const givenEndpoint = new URL(options.endpointParams.Endpoint);
748
- const { protocol, port } = givenEndpoint;
749
- endpoint.url.protocol = protocol;
750
- endpoint.url.port = port;
751
- }
752
- catch (e) {
753
- }
754
- }
755
745
  options.logger?.debug?.(`${debugId} Resolved endpoint: ${toDebugString(endpoint)}`);
756
746
  return endpoint;
757
747
  };
@@ -1726,7 +1716,7 @@ const getEndpointUrlConfig = (serviceId) => ({
1726
1716
  default: undefined,
1727
1717
  });
1728
1718
 
1729
- const getEndpointFromConfig = async (serviceId) => loadConfig(getEndpointUrlConfig(serviceId))();
1719
+ const getEndpointFromConfig = async (serviceId) => loadConfig(getEndpointUrlConfig(serviceId ?? ""))();
1730
1720
 
1731
1721
  function parseQueryString(querystring) {
1732
1722
  const query = {};
@@ -1782,7 +1772,13 @@ const toEndpointV1 = (endpoint) => {
1782
1772
 
1783
1773
  const getEndpointFromInstructions = async (commandInput, instructionsSupplier, clientConfig, context) => {
1784
1774
  if (!clientConfig.endpoint) {
1785
- const endpointFromConfig = await getEndpointFromConfig(clientConfig.serviceId || "");
1775
+ let endpointFromConfig;
1776
+ if (clientConfig.serviceConfiguredEndpoint) {
1777
+ endpointFromConfig = await clientConfig.serviceConfiguredEndpoint();
1778
+ }
1779
+ else {
1780
+ endpointFromConfig = await getEndpointFromConfig(clientConfig.serviceId);
1781
+ }
1786
1782
  if (endpointFromConfig) {
1787
1783
  clientConfig.endpoint = () => Promise.resolve(toEndpointV1(endpointFromConfig));
1788
1784
  }
@@ -1936,7 +1932,7 @@ const resolveEndpointConfig = (input) => {
1936
1932
  const { endpoint } = input;
1937
1933
  const customEndpointProvider = endpoint != null ? async () => toEndpointV1(await normalizeProvider$1(endpoint)()) : undefined;
1938
1934
  const isCustomEndpoint = !!endpoint;
1939
- return {
1935
+ const resolvedConfig = {
1940
1936
  ...input,
1941
1937
  endpoint: customEndpointProvider,
1942
1938
  tls,
@@ -1944,6 +1940,14 @@ const resolveEndpointConfig = (input) => {
1944
1940
  useDualstackEndpoint: normalizeProvider$1(input.useDualstackEndpoint ?? false),
1945
1941
  useFipsEndpoint: normalizeProvider$1(input.useFipsEndpoint ?? false),
1946
1942
  };
1943
+ let configuredEndpointPromise = undefined;
1944
+ resolvedConfig.serviceConfiguredEndpoint = async () => {
1945
+ if (input.serviceId && !configuredEndpointPromise) {
1946
+ configuredEndpointPromise = getEndpointFromConfig(input.serviceId);
1947
+ }
1948
+ return configuredEndpointPromise;
1949
+ };
1950
+ return resolvedConfig;
1947
1951
  };
1948
1952
 
1949
1953
  const httpAuthSchemeEndpointRuleSetMiddlewareOptions = {
@@ -2667,13 +2671,31 @@ const priorityWeights = {
2667
2671
 
2668
2672
  class Client {
2669
2673
  constructor(config) {
2670
- this.middlewareStack = constructStack();
2671
2674
  this.config = config;
2675
+ this.middlewareStack = constructStack();
2672
2676
  }
2673
2677
  send(command, optionsOrCb, cb) {
2674
2678
  const options = typeof optionsOrCb !== "function" ? optionsOrCb : undefined;
2675
2679
  const callback = typeof optionsOrCb === "function" ? optionsOrCb : cb;
2676
- const handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2680
+ const useHandlerCache = options === undefined && this.config.cacheMiddleware === true;
2681
+ let handler;
2682
+ if (useHandlerCache) {
2683
+ if (!this.handlers) {
2684
+ this.handlers = new WeakMap();
2685
+ }
2686
+ const handlers = this.handlers;
2687
+ if (handlers.has(command.constructor)) {
2688
+ handler = handlers.get(command.constructor);
2689
+ }
2690
+ else {
2691
+ handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2692
+ handlers.set(command.constructor, handler);
2693
+ }
2694
+ }
2695
+ else {
2696
+ delete this.handlers;
2697
+ handler = command.resolveMiddleware(this.middlewareStack, this.config, options);
2698
+ }
2677
2699
  if (callback) {
2678
2700
  handler(command)
2679
2701
  .then((result) => callback(null, result.output), (err) => callback(err))
@@ -2684,8 +2706,8 @@ class Client {
2684
2706
  }
2685
2707
  }
2686
2708
  destroy() {
2687
- if (this.config.requestHandler.destroy)
2688
- this.config.requestHandler.destroy();
2709
+ this.config?.requestHandler?.destroy?.();
2710
+ delete this.handlers;
2689
2711
  }
2690
2712
  }
2691
2713
 
@@ -2822,42 +2844,77 @@ const getTransformedHeaders = (headers) => {
2822
2844
  return transformedHeaders;
2823
2845
  };
2824
2846
 
2847
+ const DEFER_EVENT_LISTENER_TIME$2 = 1000;
2825
2848
  const setConnectionTimeout = (request, reject, timeoutInMs = 0) => {
2826
2849
  if (!timeoutInMs) {
2827
- return;
2828
- }
2829
- const timeoutId = setTimeout(() => {
2830
- request.destroy();
2831
- reject(Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), {
2832
- name: "TimeoutError",
2833
- }));
2834
- }, timeoutInMs);
2835
- request.on("socket", (socket) => {
2836
- if (socket.connecting) {
2837
- socket.on("connect", () => {
2850
+ return -1;
2851
+ }
2852
+ const registerTimeout = (offset) => {
2853
+ const timeoutId = setTimeout(() => {
2854
+ request.destroy();
2855
+ reject(Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), {
2856
+ name: "TimeoutError",
2857
+ }));
2858
+ }, timeoutInMs - offset);
2859
+ const doWithSocket = (socket) => {
2860
+ if (socket?.connecting) {
2861
+ socket.on("connect", () => {
2862
+ clearTimeout(timeoutId);
2863
+ });
2864
+ }
2865
+ else {
2838
2866
  clearTimeout(timeoutId);
2839
- });
2867
+ }
2868
+ };
2869
+ if (request.socket) {
2870
+ doWithSocket(request.socket);
2840
2871
  }
2841
2872
  else {
2842
- clearTimeout(timeoutId);
2873
+ request.on("socket", doWithSocket);
2843
2874
  }
2844
- });
2875
+ };
2876
+ if (timeoutInMs < 2000) {
2877
+ registerTimeout(0);
2878
+ return 0;
2879
+ }
2880
+ return setTimeout(registerTimeout.bind(null, DEFER_EVENT_LISTENER_TIME$2), DEFER_EVENT_LISTENER_TIME$2);
2845
2881
  };
2846
2882
 
2847
- const setSocketKeepAlive = (request, { keepAlive, keepAliveMsecs }) => {
2883
+ const DEFER_EVENT_LISTENER_TIME$1 = 3000;
2884
+ const setSocketKeepAlive = (request, { keepAlive, keepAliveMsecs }, deferTimeMs = DEFER_EVENT_LISTENER_TIME$1) => {
2848
2885
  if (keepAlive !== true) {
2849
- return;
2886
+ return -1;
2850
2887
  }
2851
- request.on("socket", (socket) => {
2852
- socket.setKeepAlive(keepAlive, keepAliveMsecs || 0);
2853
- });
2888
+ const registerListener = () => {
2889
+ if (request.socket) {
2890
+ request.socket.setKeepAlive(keepAlive, keepAliveMsecs || 0);
2891
+ }
2892
+ else {
2893
+ request.on("socket", (socket) => {
2894
+ socket.setKeepAlive(keepAlive, keepAliveMsecs || 0);
2895
+ });
2896
+ }
2897
+ };
2898
+ if (deferTimeMs === 0) {
2899
+ registerListener();
2900
+ return 0;
2901
+ }
2902
+ return setTimeout(registerListener, deferTimeMs);
2854
2903
  };
2855
2904
 
2905
+ const DEFER_EVENT_LISTENER_TIME = 3000;
2856
2906
  const setSocketTimeout = (request, reject, timeoutInMs = 0) => {
2857
- request.setTimeout(timeoutInMs, () => {
2858
- request.destroy();
2859
- reject(Object.assign(new Error(`Connection timed out after ${timeoutInMs} ms`), { name: "TimeoutError" }));
2860
- });
2907
+ const registerTimeout = (offset) => {
2908
+ request.setTimeout(timeoutInMs - offset, () => {
2909
+ request.destroy();
2910
+ reject(Object.assign(new Error(`Connection timed out after ${timeoutInMs} ms`), { name: "TimeoutError" }));
2911
+ });
2912
+ };
2913
+ if (0 < timeoutInMs && timeoutInMs < 6000) {
2914
+ registerTimeout(0);
2915
+ return 0;
2916
+ }
2917
+ return setTimeout(registerTimeout.bind(null, timeoutInMs === 0 ? 0 : DEFER_EVENT_LISTENER_TIME), DEFER_EVENT_LISTENER_TIME);
2861
2918
  };
2862
2919
 
2863
2920
  const MIN_WAIT_TIME = 1000;
@@ -2988,17 +3045,17 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf
2988
3045
  if (!this.config) {
2989
3046
  this.config = await this.configProvider;
2990
3047
  }
2991
- let socketCheckTimeoutId;
2992
3048
  return new Promise((_resolve, _reject) => {
2993
3049
  let writeRequestBodyPromise = undefined;
3050
+ const timeouts = [];
2994
3051
  const resolve = async (arg) => {
2995
3052
  await writeRequestBodyPromise;
2996
- clearTimeout(socketCheckTimeoutId);
3053
+ timeouts.forEach(clearTimeout);
2997
3054
  _resolve(arg);
2998
3055
  };
2999
3056
  const reject = async (arg) => {
3000
3057
  await writeRequestBodyPromise;
3001
- clearTimeout(socketCheckTimeoutId);
3058
+ timeouts.forEach(clearTimeout);
3002
3059
  _reject(arg);
3003
3060
  };
3004
3061
  if (!this.config) {
@@ -3012,10 +3069,10 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf
3012
3069
  }
3013
3070
  const isSSL = request.protocol === "https:";
3014
3071
  const agent = isSSL ? this.config.httpsAgent : this.config.httpAgent;
3015
- socketCheckTimeoutId = setTimeout(() => {
3072
+ timeouts.push(setTimeout(() => {
3016
3073
  this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage(agent, this.socketWarningTimestamp, this.config.logger);
3017
3074
  }, this.config.socketAcquisitionWarningTimeout ??
3018
- (this.config.requestTimeout ?? 2000) + (this.config.connectionTimeout ?? 1000));
3075
+ (this.config.requestTimeout ?? 2000) + (this.config.connectionTimeout ?? 1000)));
3019
3076
  const queryString = buildQueryString(request.query || {});
3020
3077
  let auth = undefined;
3021
3078
  if (request.username != null || request.password != null) {
@@ -3057,8 +3114,6 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf
3057
3114
  reject(err);
3058
3115
  }
3059
3116
  });
3060
- setConnectionTimeout(req, reject, this.config.connectionTimeout);
3061
- setSocketTimeout(req, reject, this.config.requestTimeout);
3062
3117
  if (abortSignal) {
3063
3118
  const onAbort = () => {
3064
3119
  req.destroy();
@@ -3075,15 +3130,17 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf
3075
3130
  abortSignal.onabort = onAbort;
3076
3131
  }
3077
3132
  }
3133
+ timeouts.push(setConnectionTimeout(req, reject, this.config.connectionTimeout));
3134
+ timeouts.push(setSocketTimeout(req, reject, this.config.requestTimeout));
3078
3135
  const httpAgent = nodeHttpsOptions.agent;
3079
3136
  if (typeof httpAgent === "object" && "keepAlive" in httpAgent) {
3080
- setSocketKeepAlive(req, {
3137
+ timeouts.push(setSocketKeepAlive(req, {
3081
3138
  keepAlive: httpAgent.keepAlive,
3082
3139
  keepAliveMsecs: httpAgent.keepAliveMsecs,
3083
- });
3140
+ }));
3084
3141
  }
3085
3142
  writeRequestBodyPromise = writeRequestBody(req, request, this.config.requestTimeout).catch((e) => {
3086
- clearTimeout(socketCheckTimeoutId);
3143
+ timeouts.forEach(clearTimeout);
3087
3144
  return _reject(e);
3088
3145
  });
3089
3146
  });