UNPKG

4.75 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6var __export = (target, all) => {
7 for (var name in all)
8 __defProp(target, name, { get: all[name], enumerable: true });
9};
10var __copyProps = (to, from, except, desc) => {
11 if (from && typeof from === "object" || typeof from === "function") {
12 for (let key of __getOwnPropNames(from))
13 if (!__hasOwnProp.call(to, key) && key !== except)
14 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15 }
16 return to;
17};
18var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
20// src/index.ts
21var src_exports = {};
22__export(src_exports, {
23 getUserAgentMiddlewareOptions: () => getUserAgentMiddlewareOptions,
24 getUserAgentPlugin: () => getUserAgentPlugin,
25 resolveUserAgentConfig: () => resolveUserAgentConfig,
26 userAgentMiddleware: () => userAgentMiddleware
27});
28module.exports = __toCommonJS(src_exports);
29
30// src/configurations.ts
31function resolveUserAgentConfig(input) {
32 return {
33 ...input,
34 customUserAgent: typeof input.customUserAgent === "string" ? [[input.customUserAgent]] : input.customUserAgent
35 };
36}
37__name(resolveUserAgentConfig, "resolveUserAgentConfig");
38
39// src/user-agent-middleware.ts
40var import_util_endpoints = require("@aws-sdk/util-endpoints");
41var import_protocol_http = require("@smithy/protocol-http");
42
43// src/constants.ts
44var USER_AGENT = "user-agent";
45var X_AMZ_USER_AGENT = "x-amz-user-agent";
46var SPACE = " ";
47var UA_NAME_SEPARATOR = "/";
48var UA_NAME_ESCAPE_REGEX = /[^\!\$\%\&\'\*\+\-\.\^\_\`\|\~\d\w]/g;
49var UA_VALUE_ESCAPE_REGEX = /[^\!\$\%\&\'\*\+\-\.\^\_\`\|\~\d\w\#]/g;
50var UA_ESCAPE_CHAR = "-";
51
52// src/user-agent-middleware.ts
53var userAgentMiddleware = /* @__PURE__ */ __name((options) => (next, context) => async (args) => {
54 var _a, _b;
55 const { request } = args;
56 if (!import_protocol_http.HttpRequest.isInstance(request))
57 return next(args);
58 const { headers } = request;
59 const userAgent = ((_a = context == null ? void 0 : context.userAgent) == null ? void 0 : _a.map(escapeUserAgent)) || [];
60 const defaultUserAgent = (await options.defaultUserAgentProvider()).map(escapeUserAgent);
61 const customUserAgent = ((_b = options == null ? void 0 : options.customUserAgent) == null ? void 0 : _b.map(escapeUserAgent)) || [];
62 const prefix = (0, import_util_endpoints.getUserAgentPrefix)();
63 const sdkUserAgentValue = (prefix ? [prefix] : []).concat([...defaultUserAgent, ...userAgent, ...customUserAgent]).join(SPACE);
64 const normalUAValue = [
65 ...defaultUserAgent.filter((section) => section.startsWith("aws-sdk-")),
66 ...customUserAgent
67 ].join(SPACE);
68 if (options.runtime !== "browser") {
69 if (normalUAValue) {
70 headers[X_AMZ_USER_AGENT] = headers[X_AMZ_USER_AGENT] ? `${headers[USER_AGENT]} ${normalUAValue}` : normalUAValue;
71 }
72 headers[USER_AGENT] = sdkUserAgentValue;
73 } else {
74 headers[X_AMZ_USER_AGENT] = sdkUserAgentValue;
75 }
76 return next({
77 ...args,
78 request
79 });
80}, "userAgentMiddleware");
81var escapeUserAgent = /* @__PURE__ */ __name((userAgentPair) => {
82 var _a;
83 const name = userAgentPair[0].split(UA_NAME_SEPARATOR).map((part) => part.replace(UA_NAME_ESCAPE_REGEX, UA_ESCAPE_CHAR)).join(UA_NAME_SEPARATOR);
84 const version = (_a = userAgentPair[1]) == null ? void 0 : _a.replace(UA_VALUE_ESCAPE_REGEX, UA_ESCAPE_CHAR);
85 const prefixSeparatorIndex = name.indexOf(UA_NAME_SEPARATOR);
86 const prefix = name.substring(0, prefixSeparatorIndex);
87 let uaName = name.substring(prefixSeparatorIndex + 1);
88 if (prefix === "api") {
89 uaName = uaName.toLowerCase();
90 }
91 return [prefix, uaName, version].filter((item) => item && item.length > 0).reduce((acc, item, index) => {
92 switch (index) {
93 case 0:
94 return item;
95 case 1:
96 return `${acc}/${item}`;
97 default:
98 return `${acc}#${item}`;
99 }
100 }, "");
101}, "escapeUserAgent");
102var getUserAgentMiddlewareOptions = {
103 name: "getUserAgentMiddleware",
104 step: "build",
105 priority: "low",
106 tags: ["SET_USER_AGENT", "USER_AGENT"],
107 override: true
108};
109var getUserAgentPlugin = /* @__PURE__ */ __name((config) => ({
110 applyToStack: (clientStack) => {
111 clientStack.add(userAgentMiddleware(config), getUserAgentMiddlewareOptions);
112 }
113}), "getUserAgentPlugin");
114// Annotate the CommonJS export names for ESM import in node:
115
1160 && (module.exports = {
117 resolveUserAgentConfig,
118 userAgentMiddleware,
119 getUserAgentMiddlewareOptions,
120 getUserAgentPlugin
121});
122