@bleedingdev/modern-js-create-request 3.9.0-ultramodern.1 → 3.9.0-ultramodern.11
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.
- package/dist/cjs/browser.js +22 -79
- package/dist/cjs/{traceparent.js → headers.js} +24 -21
- package/dist/cjs/node.js +21 -87
- package/dist/cjs/requestFactory.js +58 -166
- package/dist/cjs/types.js +1 -40
- package/dist/esm/browser.mjs +13 -18
- package/dist/esm/headers.mjs +15 -0
- package/dist/esm/node.mjs +11 -25
- package/dist/esm/requestFactory.mjs +50 -160
- package/dist/esm/types.mjs +0 -10
- package/dist/esm-node/browser.mjs +13 -18
- package/dist/esm-node/headers.mjs +16 -0
- package/dist/esm-node/node.mjs +11 -25
- package/dist/esm-node/requestFactory.mjs +50 -160
- package/dist/esm-node/types.mjs +0 -10
- package/dist/types/browser.d.ts +1 -3
- package/dist/types/headers.d.ts +3 -0
- package/dist/types/node.d.ts +1 -3
- package/dist/types/requestFactory.d.ts +5 -9
- package/dist/types/types.d.ts +38 -127
- package/package.json +6 -6
- package/dist/cjs/policyCore.js +0 -272
- package/dist/cjs/requestContext.js +0 -121
- package/dist/cjs/transport.js +0 -234
- package/dist/esm/policyCore.mjs +0 -174
- package/dist/esm/requestContext.mjs +0 -74
- package/dist/esm/traceparent.mjs +0 -18
- package/dist/esm/transport.mjs +0 -196
- package/dist/esm-node/policyCore.mjs +0 -175
- package/dist/esm-node/requestContext.mjs +0 -75
- package/dist/esm-node/traceparent.mjs +0 -19
- package/dist/esm-node/transport.mjs +0 -197
- package/dist/types/policyCore.d.ts +0 -108
- package/dist/types/requestContext.d.ts +0 -19
- package/dist/types/traceparent.d.ts +0 -10
- package/dist/types/transport.d.ts +0 -12
package/dist/cjs/policyCore.js
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
-
var define = (defs, kind)=>{
|
|
6
|
-
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
[kind]: defs[key]
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
define(getters, "get");
|
|
12
|
-
define(values, "value");
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
(()=>{
|
|
16
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
-
})();
|
|
18
|
-
(()=>{
|
|
19
|
-
__webpack_require__.r = (exports1)=>{
|
|
20
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
-
value: 'Module'
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
-
value: true
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
})();
|
|
28
|
-
var __webpack_exports__ = {};
|
|
29
|
-
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
CrossOriginEnvelopePolicyError: ()=>CrossOriginEnvelopePolicyError,
|
|
32
|
-
IdentityBindingViolationError: ()=>IdentityBindingViolationError,
|
|
33
|
-
OperationContractViolationError: ()=>OperationContractViolationError,
|
|
34
|
-
ProducerClientNotInitializedError: ()=>ProducerClientNotInitializedError,
|
|
35
|
-
ProducerDomainNotConfiguredError: ()=>ProducerDomainNotConfiguredError,
|
|
36
|
-
TRACEPARENT_HEADER: ()=>TRACEPARENT_HEADER,
|
|
37
|
-
attachOperationContextHeaders: ()=>attachOperationContextHeaders,
|
|
38
|
-
buildEnvelopeHeaderValue: ()=>buildEnvelopeHeaderValue,
|
|
39
|
-
buildOperationContext: ()=>buildOperationContext,
|
|
40
|
-
deleteHeader: ()=>deleteHeader,
|
|
41
|
-
extractPathParamNames: ()=>extractPathParamNames,
|
|
42
|
-
findHeaderKey: ()=>findHeaderKey,
|
|
43
|
-
firstHeaderValue: ()=>firstHeaderValue,
|
|
44
|
-
isEmptyDomain: ()=>isEmptyDomain,
|
|
45
|
-
isSecuredRequestId: ()=>isSecuredRequestId,
|
|
46
|
-
parseTraceparentValue: ()=>parseTraceparentValue,
|
|
47
|
-
readHeader: ()=>readHeader,
|
|
48
|
-
resolveConfiguredRequest: ()=>resolveConfiguredRequest,
|
|
49
|
-
toOrigin: ()=>toOrigin,
|
|
50
|
-
validateOperationContract: ()=>validateOperationContract,
|
|
51
|
-
writeHeader: ()=>writeHeader
|
|
52
|
-
});
|
|
53
|
-
const external_traceparent_js_namespaceObject = require("./traceparent.js");
|
|
54
|
-
const TRACEPARENT_HEADER = 'traceparent';
|
|
55
|
-
const readProcessEnv = (key)=>{
|
|
56
|
-
if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
|
|
57
|
-
return process.env[key];
|
|
58
|
-
};
|
|
59
|
-
const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
|
|
60
|
-
const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
|
|
61
|
-
const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
|
|
62
|
-
const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
|
|
63
|
-
const findHeaderKey = (headers, header)=>{
|
|
64
|
-
const normalized = header.toLowerCase();
|
|
65
|
-
return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
|
|
66
|
-
};
|
|
67
|
-
const readHeader = (headers, header)=>{
|
|
68
|
-
const key = findHeaderKey(headers, header);
|
|
69
|
-
return 'string' == typeof key ? headers[key] : void 0;
|
|
70
|
-
};
|
|
71
|
-
const writeHeader = (headers, header, value)=>{
|
|
72
|
-
if (void 0 === value) return;
|
|
73
|
-
const key = findHeaderKey(headers, header);
|
|
74
|
-
if ('string' == typeof key && key !== header) delete headers[key];
|
|
75
|
-
headers[header] = value;
|
|
76
|
-
};
|
|
77
|
-
const deleteHeader = (headers, header)=>{
|
|
78
|
-
const key = findHeaderKey(headers, header);
|
|
79
|
-
if ('string' == typeof key) delete headers[key];
|
|
80
|
-
};
|
|
81
|
-
const toOrigin = (value)=>{
|
|
82
|
-
if (!value) return;
|
|
83
|
-
try {
|
|
84
|
-
return new URL(value).origin;
|
|
85
|
-
} catch (error) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
const parseTraceparentValue = (value)=>(0, external_traceparent_js_namespaceObject.parseTraceparent)(firstHeaderValue(value));
|
|
90
|
-
const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
|
|
91
|
-
key
|
|
92
|
-
] : []);
|
|
93
|
-
const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
|
|
94
|
-
const routePath = operationContext?.routePath || path;
|
|
95
|
-
const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
|
|
96
|
-
const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
|
|
97
|
-
const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
|
|
98
|
-
const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
|
|
99
|
-
const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
|
|
100
|
-
traceId: operationContext.traceId,
|
|
101
|
-
spanId: operationContext.spanId
|
|
102
|
-
} : parseTraceparentValue(traceparentValue);
|
|
103
|
-
return {
|
|
104
|
-
requestId,
|
|
105
|
-
operationId,
|
|
106
|
-
routePath,
|
|
107
|
-
method: operationMethod,
|
|
108
|
-
...operationContext?.schemaHash ? {
|
|
109
|
-
schemaHash: operationContext.schemaHash
|
|
110
|
-
} : {},
|
|
111
|
-
...'number' == typeof operationContext?.operationVersion ? {
|
|
112
|
-
operationVersion: operationContext.operationVersion
|
|
113
|
-
} : {},
|
|
114
|
-
...traceparentValue ? {
|
|
115
|
-
traceparent: traceparentValue
|
|
116
|
-
} : {},
|
|
117
|
-
...parsedTraceContext ? {
|
|
118
|
-
traceId: parsedTraceContext.traceId,
|
|
119
|
-
spanId: parsedTraceContext.spanId
|
|
120
|
-
} : {}
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
class ProducerClientNotInitializedError extends Error {
|
|
124
|
-
constructor(requestId){
|
|
125
|
-
super(`Producer client "${requestId}" is not initialized. Call initProducerClient() (or configure()) before using generated APIs for this requestId.`), this.code = 'BFF_PRODUCER_CLIENT_NOT_INITIALIZED';
|
|
126
|
-
this.name = 'ProducerClientNotInitializedError';
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
class ProducerDomainNotConfiguredError extends Error {
|
|
130
|
-
constructor(requestId){
|
|
131
|
-
super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
|
|
132
|
-
this.name = 'ProducerDomainNotConfiguredError';
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
class CrossOriginEnvelopePolicyError extends Error {
|
|
136
|
-
constructor(requestId, sourceOrigin, targetOrigin){
|
|
137
|
-
super(`Cross-origin envelope is not allowed for producer "${requestId}" (${sourceOrigin || 'unknown-origin'} -> ${targetOrigin || 'unknown-origin'}). Configure allowCrossOriginEnvelope to explicitly allow this flow.`), this.code = 'BFF_CROSS_ORIGIN_ENVELOPE_NOT_ALLOWED';
|
|
138
|
-
this.name = 'CrossOriginEnvelopePolicyError';
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
class IdentityBindingViolationError extends Error {
|
|
142
|
-
constructor(violation){
|
|
143
|
-
super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
|
|
144
|
-
this.name = 'IdentityBindingViolationError';
|
|
145
|
-
this.violation = violation;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
class OperationContractViolationError extends Error {
|
|
149
|
-
constructor(violation){
|
|
150
|
-
super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
|
|
151
|
-
this.name = 'OperationContractViolationError';
|
|
152
|
-
this.violation = violation;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
const validateOperationContract = ({ requestId, target, contextPayload, operationContract })=>{
|
|
156
|
-
const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
|
|
157
|
-
if (!operationContractEnabled) return;
|
|
158
|
-
const strict = operationContract?.strict ?? true;
|
|
159
|
-
const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
|
|
160
|
-
const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
|
|
161
|
-
const maybeReportViolation = (reason)=>{
|
|
162
|
-
const violation = {
|
|
163
|
-
requestId,
|
|
164
|
-
target,
|
|
165
|
-
operationId: contextPayload.operationId,
|
|
166
|
-
routePath: contextPayload.routePath,
|
|
167
|
-
method: contextPayload.method,
|
|
168
|
-
schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
|
|
169
|
-
operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
|
|
170
|
-
reason
|
|
171
|
-
};
|
|
172
|
-
operationContract?.onViolation?.(violation);
|
|
173
|
-
if (strict) throw new OperationContractViolationError(violation);
|
|
174
|
-
};
|
|
175
|
-
if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
|
|
176
|
-
if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
|
|
177
|
-
};
|
|
178
|
-
const resolveConfiguredRequest = (configuredRequests, requestId, fallback)=>{
|
|
179
|
-
const configuredRequest = configuredRequests.get(requestId);
|
|
180
|
-
if (configuredRequest) return configuredRequest;
|
|
181
|
-
if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
|
|
182
|
-
return fallback;
|
|
183
|
-
};
|
|
184
|
-
const buildEnvelopeHeaderValue = ({ requestId, target, sourceOrigin, targetOrigin, traceContext, allowCrossOriginEnvelope })=>{
|
|
185
|
-
const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
|
|
186
|
-
if (isCrossOrigin) {
|
|
187
|
-
const isAllowed = 'function' == typeof allowCrossOriginEnvelope ? true === allowCrossOriginEnvelope({
|
|
188
|
-
requestId,
|
|
189
|
-
sourceOrigin,
|
|
190
|
-
targetOrigin,
|
|
191
|
-
target
|
|
192
|
-
}) : true === allowCrossOriginEnvelope;
|
|
193
|
-
if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
|
|
194
|
-
}
|
|
195
|
-
return JSON.stringify({
|
|
196
|
-
requestId,
|
|
197
|
-
target,
|
|
198
|
-
timestamp: Date.now(),
|
|
199
|
-
sourceOrigin,
|
|
200
|
-
targetOrigin,
|
|
201
|
-
...traceContext ? {
|
|
202
|
-
traceId: traceContext.traceId,
|
|
203
|
-
spanId: traceContext.spanId
|
|
204
|
-
} : {}
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
const attachOperationContextHeaders = ({ headers, requestId, target, method, path, operationContext, operationContract, operationContextHeader, operationContextDetailHeader })=>{
|
|
208
|
-
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
209
|
-
const contextPayload = buildOperationContext({
|
|
210
|
-
requestId,
|
|
211
|
-
method,
|
|
212
|
-
path,
|
|
213
|
-
operationContext,
|
|
214
|
-
traceparent: readHeader(headers, TRACEPARENT_HEADER)
|
|
215
|
-
});
|
|
216
|
-
validateOperationContract({
|
|
217
|
-
requestId,
|
|
218
|
-
target,
|
|
219
|
-
contextPayload,
|
|
220
|
-
operationContract
|
|
221
|
-
});
|
|
222
|
-
if (void 0 === readHeader(headers, operationContextHeader)) writeHeader(headers, operationContextHeader, contextPayload.operationId);
|
|
223
|
-
writeHeader(headers, operationContextDetailHeader, JSON.stringify(contextPayload));
|
|
224
|
-
return contextPayload;
|
|
225
|
-
};
|
|
226
|
-
exports.CrossOriginEnvelopePolicyError = __webpack_exports__.CrossOriginEnvelopePolicyError;
|
|
227
|
-
exports.IdentityBindingViolationError = __webpack_exports__.IdentityBindingViolationError;
|
|
228
|
-
exports.OperationContractViolationError = __webpack_exports__.OperationContractViolationError;
|
|
229
|
-
exports.ProducerClientNotInitializedError = __webpack_exports__.ProducerClientNotInitializedError;
|
|
230
|
-
exports.ProducerDomainNotConfiguredError = __webpack_exports__.ProducerDomainNotConfiguredError;
|
|
231
|
-
exports.TRACEPARENT_HEADER = __webpack_exports__.TRACEPARENT_HEADER;
|
|
232
|
-
exports.attachOperationContextHeaders = __webpack_exports__.attachOperationContextHeaders;
|
|
233
|
-
exports.buildEnvelopeHeaderValue = __webpack_exports__.buildEnvelopeHeaderValue;
|
|
234
|
-
exports.buildOperationContext = __webpack_exports__.buildOperationContext;
|
|
235
|
-
exports.deleteHeader = __webpack_exports__.deleteHeader;
|
|
236
|
-
exports.extractPathParamNames = __webpack_exports__.extractPathParamNames;
|
|
237
|
-
exports.findHeaderKey = __webpack_exports__.findHeaderKey;
|
|
238
|
-
exports.firstHeaderValue = __webpack_exports__.firstHeaderValue;
|
|
239
|
-
exports.isEmptyDomain = __webpack_exports__.isEmptyDomain;
|
|
240
|
-
exports.isSecuredRequestId = __webpack_exports__.isSecuredRequestId;
|
|
241
|
-
exports.parseTraceparentValue = __webpack_exports__.parseTraceparentValue;
|
|
242
|
-
exports.readHeader = __webpack_exports__.readHeader;
|
|
243
|
-
exports.resolveConfiguredRequest = __webpack_exports__.resolveConfiguredRequest;
|
|
244
|
-
exports.toOrigin = __webpack_exports__.toOrigin;
|
|
245
|
-
exports.validateOperationContract = __webpack_exports__.validateOperationContract;
|
|
246
|
-
exports.writeHeader = __webpack_exports__.writeHeader;
|
|
247
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
248
|
-
"CrossOriginEnvelopePolicyError",
|
|
249
|
-
"IdentityBindingViolationError",
|
|
250
|
-
"OperationContractViolationError",
|
|
251
|
-
"ProducerClientNotInitializedError",
|
|
252
|
-
"ProducerDomainNotConfiguredError",
|
|
253
|
-
"TRACEPARENT_HEADER",
|
|
254
|
-
"attachOperationContextHeaders",
|
|
255
|
-
"buildEnvelopeHeaderValue",
|
|
256
|
-
"buildOperationContext",
|
|
257
|
-
"deleteHeader",
|
|
258
|
-
"extractPathParamNames",
|
|
259
|
-
"findHeaderKey",
|
|
260
|
-
"firstHeaderValue",
|
|
261
|
-
"isEmptyDomain",
|
|
262
|
-
"isSecuredRequestId",
|
|
263
|
-
"parseTraceparentValue",
|
|
264
|
-
"readHeader",
|
|
265
|
-
"resolveConfiguredRequest",
|
|
266
|
-
"toOrigin",
|
|
267
|
-
"validateOperationContract",
|
|
268
|
-
"writeHeader"
|
|
269
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
270
|
-
Object.defineProperty(exports, '__esModule', {
|
|
271
|
-
value: true
|
|
272
|
-
});
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
-
var define = (defs, kind)=>{
|
|
6
|
-
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
[kind]: defs[key]
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
define(getters, "get");
|
|
12
|
-
define(values, "value");
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
(()=>{
|
|
16
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
-
})();
|
|
18
|
-
(()=>{
|
|
19
|
-
__webpack_require__.r = (exports1)=>{
|
|
20
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
-
value: 'Module'
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
-
value: true
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
})();
|
|
28
|
-
var __webpack_exports__ = {};
|
|
29
|
-
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
BFF_LOCALE_HEADER: ()=>BFF_LOCALE_HEADER,
|
|
32
|
-
BFF_TRACEPARENT_HEADER: ()=>BFF_TRACEPARENT_HEADER,
|
|
33
|
-
createRequestContextHeaders: ()=>createRequestContextHeaders,
|
|
34
|
-
createRequestContextSnapshot: ()=>createRequestContextSnapshot
|
|
35
|
-
});
|
|
36
|
-
const external_traceparent_js_namespaceObject = require("./traceparent.js");
|
|
37
|
-
const BFF_LOCALE_HEADER = 'accept-language';
|
|
38
|
-
const BFF_TRACEPARENT_HEADER = 'traceparent';
|
|
39
|
-
const readHeader = (headers, header)=>{
|
|
40
|
-
if (!headers) return;
|
|
41
|
-
const normalized = header.toLowerCase();
|
|
42
|
-
const key = Object.keys(headers).find((current)=>current.toLowerCase() === normalized);
|
|
43
|
-
if (!key) return;
|
|
44
|
-
const value = headers[key];
|
|
45
|
-
return Array.isArray(value) ? value[0] : value;
|
|
46
|
-
};
|
|
47
|
-
const readString = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
|
|
48
|
-
function createOperationContextSnapshot(operationContext, safeContext) {
|
|
49
|
-
if (!operationContext) return;
|
|
50
|
-
const snapshot = {
|
|
51
|
-
...operationContext,
|
|
52
|
-
...safeContext.locale || operationContext?.locale ? {
|
|
53
|
-
locale: safeContext.locale || operationContext?.locale
|
|
54
|
-
} : {},
|
|
55
|
-
...safeContext.traceparent || operationContext?.traceparent ? {
|
|
56
|
-
traceparent: safeContext.traceparent || operationContext?.traceparent
|
|
57
|
-
} : {},
|
|
58
|
-
...safeContext.traceId || operationContext?.traceId ? {
|
|
59
|
-
traceId: safeContext.traceId || operationContext?.traceId
|
|
60
|
-
} : {},
|
|
61
|
-
...safeContext.spanId || operationContext?.spanId ? {
|
|
62
|
-
spanId: safeContext.spanId || operationContext?.spanId
|
|
63
|
-
} : {}
|
|
64
|
-
};
|
|
65
|
-
return Object.keys(snapshot).some((key)=>void 0 !== snapshot[key]) ? snapshot : void 0;
|
|
66
|
-
}
|
|
67
|
-
function createRequestContextSnapshot(input = {}) {
|
|
68
|
-
const locale = readString(input.locale) || readString(readHeader(input.headers, BFF_LOCALE_HEADER));
|
|
69
|
-
const traceparent = readString(input.traceparent) || readString(input.operationContext?.traceparent) || readString(readHeader(input.headers, BFF_TRACEPARENT_HEADER));
|
|
70
|
-
const parsedTraceparent = input.operationContext?.traceId && input.operationContext?.spanId ? {
|
|
71
|
-
traceId: input.operationContext.traceId,
|
|
72
|
-
spanId: input.operationContext.spanId
|
|
73
|
-
} : (0, external_traceparent_js_namespaceObject.parseTraceparent)(traceparent);
|
|
74
|
-
const headers = {};
|
|
75
|
-
if (locale) headers[BFF_LOCALE_HEADER] = locale;
|
|
76
|
-
if (traceparent) headers[BFF_TRACEPARENT_HEADER] = traceparent;
|
|
77
|
-
const operationContext = createOperationContextSnapshot(input.operationContext, {
|
|
78
|
-
...locale ? {
|
|
79
|
-
locale
|
|
80
|
-
} : {},
|
|
81
|
-
...traceparent ? {
|
|
82
|
-
traceparent
|
|
83
|
-
} : {},
|
|
84
|
-
...parsedTraceparent ? {
|
|
85
|
-
traceId: parsedTraceparent.traceId,
|
|
86
|
-
spanId: parsedTraceparent.spanId
|
|
87
|
-
} : {}
|
|
88
|
-
});
|
|
89
|
-
return {
|
|
90
|
-
headers,
|
|
91
|
-
...locale ? {
|
|
92
|
-
locale
|
|
93
|
-
} : {},
|
|
94
|
-
...traceparent ? {
|
|
95
|
-
traceparent
|
|
96
|
-
} : {},
|
|
97
|
-
...parsedTraceparent ? {
|
|
98
|
-
traceId: parsedTraceparent.traceId,
|
|
99
|
-
spanId: parsedTraceparent.spanId
|
|
100
|
-
} : {},
|
|
101
|
-
...operationContext ? {
|
|
102
|
-
operationContext
|
|
103
|
-
} : {}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
function createRequestContextHeaders(input = {}) {
|
|
107
|
-
return createRequestContextSnapshot(input).headers;
|
|
108
|
-
}
|
|
109
|
-
exports.BFF_LOCALE_HEADER = __webpack_exports__.BFF_LOCALE_HEADER;
|
|
110
|
-
exports.BFF_TRACEPARENT_HEADER = __webpack_exports__.BFF_TRACEPARENT_HEADER;
|
|
111
|
-
exports.createRequestContextHeaders = __webpack_exports__.createRequestContextHeaders;
|
|
112
|
-
exports.createRequestContextSnapshot = __webpack_exports__.createRequestContextSnapshot;
|
|
113
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
114
|
-
"BFF_LOCALE_HEADER",
|
|
115
|
-
"BFF_TRACEPARENT_HEADER",
|
|
116
|
-
"createRequestContextHeaders",
|
|
117
|
-
"createRequestContextSnapshot"
|
|
118
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
119
|
-
Object.defineProperty(exports, '__esModule', {
|
|
120
|
-
value: true
|
|
121
|
-
});
|
package/dist/cjs/transport.js
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
-
var define = (defs, kind)=>{
|
|
6
|
-
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
[kind]: defs[key]
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
define(getters, "get");
|
|
12
|
-
define(values, "value");
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
(()=>{
|
|
16
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
-
})();
|
|
18
|
-
(()=>{
|
|
19
|
-
__webpack_require__.r = (exports1)=>{
|
|
20
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
-
value: 'Module'
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
-
value: true
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
})();
|
|
28
|
-
var __webpack_exports__ = {};
|
|
29
|
-
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
const DEFAULT_RETRYABLE_STATUS_CODES = [
|
|
31
|
-
408,
|
|
32
|
-
425,
|
|
33
|
-
429,
|
|
34
|
-
500,
|
|
35
|
-
502,
|
|
36
|
-
503,
|
|
37
|
-
504
|
|
38
|
-
];
|
|
39
|
-
const DEFAULT_BASE_DELAY_MS = 100;
|
|
40
|
-
const DEFAULT_MAX_DELAY_MS = 1000;
|
|
41
|
-
const DEFAULT_JITTER_RATIO = 0.1;
|
|
42
|
-
const DEFAULT_RETRYABLE_METHODS = new Set([
|
|
43
|
-
'DELETE',
|
|
44
|
-
'GET',
|
|
45
|
-
'HEAD',
|
|
46
|
-
'OPTIONS',
|
|
47
|
-
'PUT',
|
|
48
|
-
'TRACE'
|
|
49
|
-
]);
|
|
50
|
-
const createTimeoutError = (timeoutMs)=>{
|
|
51
|
-
const error = new Error(`Request timed out after ${timeoutMs}ms`);
|
|
52
|
-
error.name = 'TimeoutError';
|
|
53
|
-
return error;
|
|
54
|
-
};
|
|
55
|
-
const unrefTimer = (timer)=>{
|
|
56
|
-
if ('object' == typeof timer && null !== timer && 'unref' in timer && 'function' == typeof timer.unref) timer.unref();
|
|
57
|
-
};
|
|
58
|
-
const wait = (ms)=>new Promise((resolve)=>{
|
|
59
|
-
const timer = setTimeout(()=>resolve(), ms);
|
|
60
|
-
unrefTimer(timer);
|
|
61
|
-
});
|
|
62
|
-
const toStatusCode = (error)=>{
|
|
63
|
-
if (!error || 'object' != typeof error) return;
|
|
64
|
-
const status = error.status;
|
|
65
|
-
if ('number' == typeof status) return status;
|
|
66
|
-
const responseStatus = error.response?.status;
|
|
67
|
-
if ('number' == typeof responseStatus) return responseStatus;
|
|
68
|
-
};
|
|
69
|
-
const isRetryableNetworkError = (error)=>{
|
|
70
|
-
if (!error || 'object' != typeof error) return false;
|
|
71
|
-
const name = error.name;
|
|
72
|
-
if ('FetchError' === name || 'TimeoutError' === name || 'TypeError' === name) return true;
|
|
73
|
-
const code = error.code;
|
|
74
|
-
return 'ECONNRESET' === code || 'ETIMEDOUT' === code || 'ECONNREFUSED' === code || 'EPIPE' === code;
|
|
75
|
-
};
|
|
76
|
-
const normalizePositiveNumber = (value, fallback)=>'number' == typeof value && Number.isFinite(value) && value > 0 ? value : fallback;
|
|
77
|
-
const normalizeNonNegativeInt = (value, fallback)=>'number' == typeof value && Number.isFinite(value) && value >= 0 ? Math.floor(value) : fallback;
|
|
78
|
-
const normalizeJitterRatio = (value)=>{
|
|
79
|
-
if ('number' != typeof value || !Number.isFinite(value)) return DEFAULT_JITTER_RATIO;
|
|
80
|
-
return Math.max(0, Math.min(1, value));
|
|
81
|
-
};
|
|
82
|
-
const shouldRetryWithDefaults = (method, statusCode, error, retryableStatusCodes)=>{
|
|
83
|
-
if (!DEFAULT_RETRYABLE_METHODS.has(method.toUpperCase())) return false;
|
|
84
|
-
if ('number' == typeof statusCode) return retryableStatusCodes.includes(statusCode);
|
|
85
|
-
return isRetryableNetworkError(error);
|
|
86
|
-
};
|
|
87
|
-
const shouldRetry = (retryOptions, context, retryableStatusCodes)=>{
|
|
88
|
-
if ('function' == typeof retryOptions?.shouldRetry) try {
|
|
89
|
-
return retryOptions.shouldRetry(context);
|
|
90
|
-
} catch (error) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return shouldRetryWithDefaults(context.method, context.statusCode, context.error, retryableStatusCodes);
|
|
94
|
-
};
|
|
95
|
-
const getBackoffMs = (retryOptions, attempt)=>{
|
|
96
|
-
const baseDelayMs = normalizePositiveNumber(retryOptions?.baseDelayMs, DEFAULT_BASE_DELAY_MS);
|
|
97
|
-
const maxDelayMs = normalizePositiveNumber(retryOptions?.maxDelayMs, DEFAULT_MAX_DELAY_MS);
|
|
98
|
-
const jitterRatio = normalizeJitterRatio(retryOptions?.jitterRatio);
|
|
99
|
-
const exponentialDelay = Math.min(maxDelayMs, baseDelayMs * 2 ** Math.max(0, attempt - 1));
|
|
100
|
-
const jitterFactor = 0 === jitterRatio ? 1 : 1 + (2 * Math.random() - 1) * jitterRatio;
|
|
101
|
-
return Math.max(0, Math.floor(exponentialDelay * jitterFactor));
|
|
102
|
-
};
|
|
103
|
-
const emitDegradedEvent = (transport, event)=>{
|
|
104
|
-
if ('function' != typeof transport?.onDegraded) return;
|
|
105
|
-
try {
|
|
106
|
-
transport.onDegraded(event);
|
|
107
|
-
} catch (error) {}
|
|
108
|
-
};
|
|
109
|
-
const withTimeout = async (promise, timeoutMs, signalBuilder)=>{
|
|
110
|
-
if (!timeoutMs || timeoutMs <= 0) {
|
|
111
|
-
const result = await promise;
|
|
112
|
-
return {
|
|
113
|
-
result
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
const controller = signalBuilder?.();
|
|
117
|
-
let timeoutTriggered = false;
|
|
118
|
-
let rejectTimeout;
|
|
119
|
-
const timeoutPromise = new Promise((_, reject)=>{
|
|
120
|
-
rejectTimeout = reject;
|
|
121
|
-
});
|
|
122
|
-
const timer = setTimeout(()=>{
|
|
123
|
-
timeoutTriggered = true;
|
|
124
|
-
if (controller) controller.abort();
|
|
125
|
-
rejectTimeout?.(createTimeoutError(timeoutMs));
|
|
126
|
-
}, timeoutMs);
|
|
127
|
-
unrefTimer(timer);
|
|
128
|
-
try {
|
|
129
|
-
const result = await Promise.race([
|
|
130
|
-
promise,
|
|
131
|
-
timeoutPromise
|
|
132
|
-
]);
|
|
133
|
-
return {
|
|
134
|
-
result,
|
|
135
|
-
timeoutTriggered
|
|
136
|
-
};
|
|
137
|
-
} catch (error) {
|
|
138
|
-
if (timeoutTriggered && error?.name === 'AbortError') throw createTimeoutError(timeoutMs);
|
|
139
|
-
throw error;
|
|
140
|
-
} finally{
|
|
141
|
-
clearTimeout(timer);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
const executeWithResilience = async ({ requestId, target, method, url, init, fetcher, transport })=>{
|
|
145
|
-
const retries = normalizeNonNegativeInt(transport?.retry?.retries, 0);
|
|
146
|
-
const timeoutMs = 'number' == typeof transport?.timeoutMs && transport.timeoutMs > 0 ? transport.timeoutMs : void 0;
|
|
147
|
-
const maxAttempts = retries + 1;
|
|
148
|
-
const retryableStatusCodes = transport?.retry?.retryableStatusCodes && transport.retry.retryableStatusCodes.length > 0 ? transport.retry.retryableStatusCodes : DEFAULT_RETRYABLE_STATUS_CODES;
|
|
149
|
-
let attempt = 0;
|
|
150
|
-
while(attempt < maxAttempts){
|
|
151
|
-
attempt += 1;
|
|
152
|
-
const canUseAbortController = "u" > typeof AbortController;
|
|
153
|
-
let controller;
|
|
154
|
-
if (timeoutMs && canUseAbortController) controller = new AbortController();
|
|
155
|
-
const callerSignal = init.signal;
|
|
156
|
-
const requestSignal = controller ? callerSignal && !callerSignal.aborted ? AbortSignal.any([
|
|
157
|
-
callerSignal,
|
|
158
|
-
controller.signal
|
|
159
|
-
]) : callerSignal || controller.signal : callerSignal;
|
|
160
|
-
const nextInit = requestSignal ? {
|
|
161
|
-
...init,
|
|
162
|
-
signal: requestSignal
|
|
163
|
-
} : init;
|
|
164
|
-
try {
|
|
165
|
-
const { result } = await withTimeout(fetcher(url, nextInit), timeoutMs, ()=>controller);
|
|
166
|
-
return result;
|
|
167
|
-
} catch (error) {
|
|
168
|
-
const statusCode = toStatusCode(error);
|
|
169
|
-
const decisionContext = {
|
|
170
|
-
requestId,
|
|
171
|
-
target,
|
|
172
|
-
method,
|
|
173
|
-
url,
|
|
174
|
-
attempt,
|
|
175
|
-
maxAttempts,
|
|
176
|
-
error,
|
|
177
|
-
statusCode
|
|
178
|
-
};
|
|
179
|
-
if (error?.name === 'TimeoutError') emitDegradedEvent(transport, {
|
|
180
|
-
requestId,
|
|
181
|
-
target,
|
|
182
|
-
method,
|
|
183
|
-
url,
|
|
184
|
-
reason: 'timeout',
|
|
185
|
-
attempt,
|
|
186
|
-
maxAttempts,
|
|
187
|
-
timeoutMs,
|
|
188
|
-
statusCode,
|
|
189
|
-
error
|
|
190
|
-
});
|
|
191
|
-
const canRetry = attempt < maxAttempts && shouldRetry(transport?.retry, decisionContext, retryableStatusCodes);
|
|
192
|
-
if (!canRetry) {
|
|
193
|
-
if (retries > 0 || error?.name === 'TimeoutError') emitDegradedEvent(transport, {
|
|
194
|
-
requestId,
|
|
195
|
-
target,
|
|
196
|
-
method,
|
|
197
|
-
url,
|
|
198
|
-
reason: 'retry_exhausted',
|
|
199
|
-
attempt,
|
|
200
|
-
maxAttempts,
|
|
201
|
-
timeoutMs,
|
|
202
|
-
statusCode,
|
|
203
|
-
error
|
|
204
|
-
});
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
const backoffMs = getBackoffMs(transport?.retry, attempt);
|
|
208
|
-
emitDegradedEvent(transport, {
|
|
209
|
-
requestId,
|
|
210
|
-
target,
|
|
211
|
-
method,
|
|
212
|
-
url,
|
|
213
|
-
reason: 'retry',
|
|
214
|
-
attempt,
|
|
215
|
-
maxAttempts,
|
|
216
|
-
timeoutMs,
|
|
217
|
-
backoffMs,
|
|
218
|
-
statusCode,
|
|
219
|
-
error
|
|
220
|
-
});
|
|
221
|
-
if (backoffMs > 0) await wait(backoffMs);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
__webpack_require__.d(__webpack_exports__, {}, {
|
|
226
|
-
executeWithResilience: executeWithResilience
|
|
227
|
-
});
|
|
228
|
-
exports.executeWithResilience = __webpack_exports__.executeWithResilience;
|
|
229
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
230
|
-
"executeWithResilience"
|
|
231
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
232
|
-
Object.defineProperty(exports, '__esModule', {
|
|
233
|
-
value: true
|
|
234
|
-
});
|