@bleedingdev/modern-js-create-request 3.2.0-ultramodern.120 → 3.2.0-ultramodern.121
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 +85 -214
- package/dist/cjs/node.js +103 -224
- package/dist/cjs/policyCore.js +275 -0
- package/dist/cjs/requestContext.js +8 -20
- package/dist/cjs/traceparent.js +56 -0
- package/dist/esm/browser.mjs +37 -185
- package/dist/esm/node.mjs +44 -184
- package/dist/esm/policyCore.mjs +174 -0
- package/dist/esm/requestContext.mjs +1 -12
- package/dist/esm/traceparent.mjs +18 -0
- package/dist/esm-node/browser.mjs +37 -185
- package/dist/esm-node/node.mjs +44 -184
- package/dist/esm-node/policyCore.mjs +175 -0
- package/dist/esm-node/requestContext.mjs +1 -12
- package/dist/esm-node/traceparent.mjs +19 -0
- package/dist/types/browser.d.ts +3 -23
- package/dist/types/node.d.ts +3 -23
- package/dist/types/policyCore.d.ts +108 -0
- package/dist/types/traceparent.d.ts +10 -0
- package/dist/types/types.d.ts +2 -1
- package/package.json +6 -5
|
@@ -0,0 +1,275 @@
|
|
|
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
|
+
isStrictDefaultRequestIdEnabled: ()=>isStrictDefaultRequestIdEnabled,
|
|
47
|
+
parseTraceparentValue: ()=>parseTraceparentValue,
|
|
48
|
+
readHeader: ()=>readHeader,
|
|
49
|
+
resolveConfiguredRequest: ()=>resolveConfiguredRequest,
|
|
50
|
+
toOrigin: ()=>toOrigin,
|
|
51
|
+
validateOperationContract: ()=>validateOperationContract,
|
|
52
|
+
writeHeader: ()=>writeHeader
|
|
53
|
+
});
|
|
54
|
+
const external_traceparent_js_namespaceObject = require("./traceparent.js");
|
|
55
|
+
const TRACEPARENT_HEADER = 'traceparent';
|
|
56
|
+
const readProcessEnv = (key)=>{
|
|
57
|
+
if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
|
|
58
|
+
return process.env[key];
|
|
59
|
+
};
|
|
60
|
+
const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
|
|
61
|
+
const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
|
|
62
|
+
const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
|
|
63
|
+
const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
|
|
64
|
+
const findHeaderKey = (headers, header)=>{
|
|
65
|
+
const normalized = header.toLowerCase();
|
|
66
|
+
return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
|
|
67
|
+
};
|
|
68
|
+
const readHeader = (headers, header)=>{
|
|
69
|
+
const key = findHeaderKey(headers, header);
|
|
70
|
+
return 'string' == typeof key ? headers[key] : void 0;
|
|
71
|
+
};
|
|
72
|
+
const writeHeader = (headers, header, value)=>{
|
|
73
|
+
if (void 0 === value) return;
|
|
74
|
+
const key = findHeaderKey(headers, header);
|
|
75
|
+
if ('string' == typeof key && key !== header) delete headers[key];
|
|
76
|
+
headers[header] = value;
|
|
77
|
+
};
|
|
78
|
+
const deleteHeader = (headers, header)=>{
|
|
79
|
+
const key = findHeaderKey(headers, header);
|
|
80
|
+
if ('string' == typeof key) delete headers[key];
|
|
81
|
+
};
|
|
82
|
+
const toOrigin = (value)=>{
|
|
83
|
+
if (!value) return;
|
|
84
|
+
try {
|
|
85
|
+
return new URL(value).origin;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const parseTraceparentValue = (value)=>(0, external_traceparent_js_namespaceObject.parseTraceparent)(firstHeaderValue(value));
|
|
91
|
+
const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
|
|
92
|
+
key
|
|
93
|
+
] : []);
|
|
94
|
+
const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
|
|
95
|
+
const routePath = operationContext?.routePath || path;
|
|
96
|
+
const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
|
|
97
|
+
const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
|
|
98
|
+
const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
|
|
99
|
+
const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
|
|
100
|
+
const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
|
|
101
|
+
traceId: operationContext.traceId,
|
|
102
|
+
spanId: operationContext.spanId
|
|
103
|
+
} : parseTraceparentValue(traceparentValue);
|
|
104
|
+
return {
|
|
105
|
+
requestId,
|
|
106
|
+
operationId,
|
|
107
|
+
routePath,
|
|
108
|
+
method: operationMethod,
|
|
109
|
+
...operationContext?.schemaHash ? {
|
|
110
|
+
schemaHash: operationContext.schemaHash
|
|
111
|
+
} : {},
|
|
112
|
+
...'number' == typeof operationContext?.operationVersion ? {
|
|
113
|
+
operationVersion: operationContext.operationVersion
|
|
114
|
+
} : {},
|
|
115
|
+
...traceparentValue ? {
|
|
116
|
+
traceparent: traceparentValue
|
|
117
|
+
} : {},
|
|
118
|
+
...parsedTraceContext ? {
|
|
119
|
+
traceId: parsedTraceContext.traceId,
|
|
120
|
+
spanId: parsedTraceContext.spanId
|
|
121
|
+
} : {}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
class ProducerClientNotInitializedError extends Error {
|
|
125
|
+
constructor(requestId){
|
|
126
|
+
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';
|
|
127
|
+
this.name = 'ProducerClientNotInitializedError';
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
class ProducerDomainNotConfiguredError extends Error {
|
|
131
|
+
constructor(requestId){
|
|
132
|
+
super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
|
|
133
|
+
this.name = 'ProducerDomainNotConfiguredError';
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
class CrossOriginEnvelopePolicyError extends Error {
|
|
137
|
+
constructor(requestId, sourceOrigin, targetOrigin){
|
|
138
|
+
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';
|
|
139
|
+
this.name = 'CrossOriginEnvelopePolicyError';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
class IdentityBindingViolationError extends Error {
|
|
143
|
+
constructor(violation){
|
|
144
|
+
super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
|
|
145
|
+
this.name = 'IdentityBindingViolationError';
|
|
146
|
+
this.violation = violation;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
class OperationContractViolationError extends Error {
|
|
150
|
+
constructor(violation){
|
|
151
|
+
super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
|
|
152
|
+
this.name = 'OperationContractViolationError';
|
|
153
|
+
this.violation = violation;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const validateOperationContract = ({ requestId, target, contextPayload, operationContract })=>{
|
|
157
|
+
const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
|
|
158
|
+
if (!operationContractEnabled) return;
|
|
159
|
+
const strict = operationContract?.strict ?? true;
|
|
160
|
+
const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
|
|
161
|
+
const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
|
|
162
|
+
const maybeReportViolation = (reason)=>{
|
|
163
|
+
const violation = {
|
|
164
|
+
requestId,
|
|
165
|
+
target,
|
|
166
|
+
operationId: contextPayload.operationId,
|
|
167
|
+
routePath: contextPayload.routePath,
|
|
168
|
+
method: contextPayload.method,
|
|
169
|
+
schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
|
|
170
|
+
operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
|
|
171
|
+
reason
|
|
172
|
+
};
|
|
173
|
+
operationContract?.onViolation?.(violation);
|
|
174
|
+
if (strict) throw new OperationContractViolationError(violation);
|
|
175
|
+
};
|
|
176
|
+
if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
|
|
177
|
+
if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
|
|
178
|
+
};
|
|
179
|
+
const resolveConfiguredRequest = (configuredRequests, requestId, fallback)=>{
|
|
180
|
+
const configuredRequest = configuredRequests.get(requestId);
|
|
181
|
+
if (configuredRequest) return configuredRequest;
|
|
182
|
+
if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
|
|
183
|
+
return fallback;
|
|
184
|
+
};
|
|
185
|
+
const buildEnvelopeHeaderValue = ({ requestId, target, sourceOrigin, targetOrigin, traceContext, allowCrossOriginEnvelope })=>{
|
|
186
|
+
const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
|
|
187
|
+
if (isCrossOrigin) {
|
|
188
|
+
const isAllowed = 'function' == typeof allowCrossOriginEnvelope ? allowCrossOriginEnvelope({
|
|
189
|
+
requestId,
|
|
190
|
+
sourceOrigin,
|
|
191
|
+
targetOrigin,
|
|
192
|
+
target
|
|
193
|
+
}) : true === allowCrossOriginEnvelope;
|
|
194
|
+
if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
|
|
195
|
+
}
|
|
196
|
+
return JSON.stringify({
|
|
197
|
+
requestId,
|
|
198
|
+
target,
|
|
199
|
+
timestamp: Date.now(),
|
|
200
|
+
sourceOrigin,
|
|
201
|
+
targetOrigin,
|
|
202
|
+
...traceContext ? {
|
|
203
|
+
traceId: traceContext.traceId,
|
|
204
|
+
spanId: traceContext.spanId
|
|
205
|
+
} : {}
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
const attachOperationContextHeaders = ({ headers, requestId, target, method, path, operationContext, operationContract, operationContextHeader, operationContextDetailHeader })=>{
|
|
209
|
+
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
210
|
+
const contextPayload = buildOperationContext({
|
|
211
|
+
requestId,
|
|
212
|
+
method,
|
|
213
|
+
path,
|
|
214
|
+
operationContext,
|
|
215
|
+
traceparent: readHeader(headers, TRACEPARENT_HEADER)
|
|
216
|
+
});
|
|
217
|
+
validateOperationContract({
|
|
218
|
+
requestId,
|
|
219
|
+
target,
|
|
220
|
+
contextPayload,
|
|
221
|
+
operationContract
|
|
222
|
+
});
|
|
223
|
+
if (void 0 === readHeader(headers, operationContextHeader)) writeHeader(headers, operationContextHeader, contextPayload.operationId);
|
|
224
|
+
writeHeader(headers, operationContextDetailHeader, JSON.stringify(contextPayload));
|
|
225
|
+
return contextPayload;
|
|
226
|
+
};
|
|
227
|
+
exports.CrossOriginEnvelopePolicyError = __webpack_exports__.CrossOriginEnvelopePolicyError;
|
|
228
|
+
exports.IdentityBindingViolationError = __webpack_exports__.IdentityBindingViolationError;
|
|
229
|
+
exports.OperationContractViolationError = __webpack_exports__.OperationContractViolationError;
|
|
230
|
+
exports.ProducerClientNotInitializedError = __webpack_exports__.ProducerClientNotInitializedError;
|
|
231
|
+
exports.ProducerDomainNotConfiguredError = __webpack_exports__.ProducerDomainNotConfiguredError;
|
|
232
|
+
exports.TRACEPARENT_HEADER = __webpack_exports__.TRACEPARENT_HEADER;
|
|
233
|
+
exports.attachOperationContextHeaders = __webpack_exports__.attachOperationContextHeaders;
|
|
234
|
+
exports.buildEnvelopeHeaderValue = __webpack_exports__.buildEnvelopeHeaderValue;
|
|
235
|
+
exports.buildOperationContext = __webpack_exports__.buildOperationContext;
|
|
236
|
+
exports.deleteHeader = __webpack_exports__.deleteHeader;
|
|
237
|
+
exports.extractPathParamNames = __webpack_exports__.extractPathParamNames;
|
|
238
|
+
exports.findHeaderKey = __webpack_exports__.findHeaderKey;
|
|
239
|
+
exports.firstHeaderValue = __webpack_exports__.firstHeaderValue;
|
|
240
|
+
exports.isEmptyDomain = __webpack_exports__.isEmptyDomain;
|
|
241
|
+
exports.isSecuredRequestId = __webpack_exports__.isSecuredRequestId;
|
|
242
|
+
exports.isStrictDefaultRequestIdEnabled = __webpack_exports__.isStrictDefaultRequestIdEnabled;
|
|
243
|
+
exports.parseTraceparentValue = __webpack_exports__.parseTraceparentValue;
|
|
244
|
+
exports.readHeader = __webpack_exports__.readHeader;
|
|
245
|
+
exports.resolveConfiguredRequest = __webpack_exports__.resolveConfiguredRequest;
|
|
246
|
+
exports.toOrigin = __webpack_exports__.toOrigin;
|
|
247
|
+
exports.validateOperationContract = __webpack_exports__.validateOperationContract;
|
|
248
|
+
exports.writeHeader = __webpack_exports__.writeHeader;
|
|
249
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
250
|
+
"CrossOriginEnvelopePolicyError",
|
|
251
|
+
"IdentityBindingViolationError",
|
|
252
|
+
"OperationContractViolationError",
|
|
253
|
+
"ProducerClientNotInitializedError",
|
|
254
|
+
"ProducerDomainNotConfiguredError",
|
|
255
|
+
"TRACEPARENT_HEADER",
|
|
256
|
+
"attachOperationContextHeaders",
|
|
257
|
+
"buildEnvelopeHeaderValue",
|
|
258
|
+
"buildOperationContext",
|
|
259
|
+
"deleteHeader",
|
|
260
|
+
"extractPathParamNames",
|
|
261
|
+
"findHeaderKey",
|
|
262
|
+
"firstHeaderValue",
|
|
263
|
+
"isEmptyDomain",
|
|
264
|
+
"isSecuredRequestId",
|
|
265
|
+
"isStrictDefaultRequestIdEnabled",
|
|
266
|
+
"parseTraceparentValue",
|
|
267
|
+
"readHeader",
|
|
268
|
+
"resolveConfiguredRequest",
|
|
269
|
+
"toOrigin",
|
|
270
|
+
"validateOperationContract",
|
|
271
|
+
"writeHeader"
|
|
272
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
273
|
+
Object.defineProperty(exports, '__esModule', {
|
|
274
|
+
value: true
|
|
275
|
+
});
|
|
@@ -27,9 +27,15 @@ var __webpack_require__ = {};
|
|
|
27
27
|
})();
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
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");
|
|
30
37
|
const BFF_LOCALE_HEADER = 'accept-language';
|
|
31
38
|
const BFF_TRACEPARENT_HEADER = 'traceparent';
|
|
32
|
-
const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
|
|
33
39
|
const readHeader = (headers, header)=>{
|
|
34
40
|
if (!headers) return;
|
|
35
41
|
const normalized = header.toLowerCase();
|
|
@@ -39,17 +45,6 @@ const readHeader = (headers, header)=>{
|
|
|
39
45
|
return Array.isArray(value) ? value[0] : value;
|
|
40
46
|
};
|
|
41
47
|
const readString = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
|
|
42
|
-
function parseTraceparent(traceparent) {
|
|
43
|
-
if (!traceparent) return;
|
|
44
|
-
const match = traceparent.trim().match(TRACEPARENT_REGEX);
|
|
45
|
-
if (!match) return;
|
|
46
|
-
const [, traceId, spanId] = match;
|
|
47
|
-
if (!traceId || !spanId) return;
|
|
48
|
-
return {
|
|
49
|
-
traceId: traceId.toLowerCase(),
|
|
50
|
-
spanId: spanId.toLowerCase()
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
48
|
function createOperationContextSnapshot(operationContext, safeContext) {
|
|
54
49
|
if (!operationContext) return;
|
|
55
50
|
const snapshot = {
|
|
@@ -75,7 +70,7 @@ function createRequestContextSnapshot(input = {}) {
|
|
|
75
70
|
const parsedTraceparent = input.operationContext?.traceId && input.operationContext?.spanId ? {
|
|
76
71
|
traceId: input.operationContext.traceId,
|
|
77
72
|
spanId: input.operationContext.spanId
|
|
78
|
-
} : parseTraceparent(traceparent);
|
|
73
|
+
} : (0, external_traceparent_js_namespaceObject.parseTraceparent)(traceparent);
|
|
79
74
|
const headers = {};
|
|
80
75
|
if (locale) headers[BFF_LOCALE_HEADER] = locale;
|
|
81
76
|
if (traceparent) headers[BFF_TRACEPARENT_HEADER] = traceparent;
|
|
@@ -111,13 +106,6 @@ function createRequestContextSnapshot(input = {}) {
|
|
|
111
106
|
function createRequestContextHeaders(input = {}) {
|
|
112
107
|
return createRequestContextSnapshot(input).headers;
|
|
113
108
|
}
|
|
114
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
115
|
-
createRequestContextHeaders: ()=>createRequestContextHeaders,
|
|
116
|
-
createRequestContextSnapshot: ()=>createRequestContextSnapshot
|
|
117
|
-
}, {
|
|
118
|
-
BFF_LOCALE_HEADER: BFF_LOCALE_HEADER,
|
|
119
|
-
BFF_TRACEPARENT_HEADER: BFF_TRACEPARENT_HEADER
|
|
120
|
-
});
|
|
121
109
|
exports.BFF_LOCALE_HEADER = __webpack_exports__.BFF_LOCALE_HEADER;
|
|
122
110
|
exports.BFF_TRACEPARENT_HEADER = __webpack_exports__.BFF_TRACEPARENT_HEADER;
|
|
123
111
|
exports.createRequestContextHeaders = __webpack_exports__.createRequestContextHeaders;
|
|
@@ -0,0 +1,56 @@
|
|
|
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 TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i;
|
|
31
|
+
const isAllZeroHex = (value)=>/^0+$/.test(value);
|
|
32
|
+
function parseTraceparent(traceparent) {
|
|
33
|
+
if (!traceparent) return;
|
|
34
|
+
const match = traceparent.trim().match(TRACEPARENT_REGEX);
|
|
35
|
+
if (!match) return;
|
|
36
|
+
const [, rawTraceId, rawSpanId, rawFlags] = match;
|
|
37
|
+
if (!rawTraceId || !rawSpanId || !rawFlags) return;
|
|
38
|
+
const traceId = rawTraceId.toLowerCase();
|
|
39
|
+
const spanId = rawSpanId.toLowerCase();
|
|
40
|
+
if (isAllZeroHex(traceId) || isAllZeroHex(spanId)) return;
|
|
41
|
+
return {
|
|
42
|
+
traceId,
|
|
43
|
+
spanId,
|
|
44
|
+
sampled: (0x1 & Number.parseInt(rawFlags, 16)) === 1
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
48
|
+
parseTraceparent: ()=>parseTraceparent
|
|
49
|
+
});
|
|
50
|
+
exports.parseTraceparent = __webpack_exports__.parseTraceparent;
|
|
51
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
52
|
+
"parseTraceparent"
|
|
53
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
54
|
+
Object.defineProperty(exports, '__esModule', {
|
|
55
|
+
value: true
|
|
56
|
+
});
|
package/dist/esm/browser.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { compile } from "path-to-regexp";
|
|
2
2
|
import { stringify } from "qs";
|
|
3
3
|
import { handleRes } from "./handleRes.mjs";
|
|
4
|
+
import { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, TRACEPARENT_HEADER, attachOperationContextHeaders, buildEnvelopeHeaderValue, deleteHeader, extractPathParamNames, isEmptyDomain, isSecuredRequestId, parseTraceparentValue, readHeader, resolveConfiguredRequest, toOrigin, writeHeader } from "./policyCore.mjs";
|
|
4
5
|
import { executeWithResilience } from "./transport.mjs";
|
|
5
6
|
import { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
|
|
6
7
|
import { getUploadPayload } from "./utiles.mjs";
|
|
7
8
|
export * from "./requestContext.mjs";
|
|
9
|
+
export * from "./traceparent.mjs";
|
|
8
10
|
export * from "./types.mjs";
|
|
9
11
|
const realRequest = new Map();
|
|
10
12
|
const realAllowedHeaders = new Map();
|
|
@@ -14,154 +16,38 @@ const realTransportResilience = new Map();
|
|
|
14
16
|
const realIdentityBinding = new Map();
|
|
15
17
|
const realOperationContract = new Map();
|
|
16
18
|
const domainMap = new Map();
|
|
17
|
-
const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
|
|
18
|
-
const TRACEPARENT_HEADER = 'traceparent';
|
|
19
19
|
const OPERATION_CONTEXT_DETAIL_HEADER = BFF_OPERATION_CONTEXT_DETAIL_HEADER;
|
|
20
|
-
const
|
|
21
|
-
const readProcessEnv = (key)=>{
|
|
22
|
-
if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
|
|
23
|
-
return process.env[key];
|
|
24
|
-
};
|
|
25
|
-
const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
|
|
26
|
-
const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
|
|
27
|
-
const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
|
|
28
|
-
const findHeaderKey = (headers, header)=>{
|
|
29
|
-
const normalized = header.toLowerCase();
|
|
30
|
-
return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
|
|
31
|
-
};
|
|
32
|
-
const readHeader = (headers, header)=>{
|
|
33
|
-
const key = findHeaderKey(headers, header);
|
|
34
|
-
return 'string' == typeof key ? headers[key] : void 0;
|
|
35
|
-
};
|
|
36
|
-
const writeHeader = (headers, header, value)=>{
|
|
37
|
-
if (void 0 === value) return;
|
|
38
|
-
const key = findHeaderKey(headers, header);
|
|
39
|
-
if ('string' == typeof key && key !== header) delete headers[key];
|
|
40
|
-
headers[header] = value;
|
|
41
|
-
};
|
|
42
|
-
const deleteHeader = (headers, header)=>{
|
|
43
|
-
const key = findHeaderKey(headers, header);
|
|
44
|
-
if ('string' == typeof key) delete headers[key];
|
|
45
|
-
};
|
|
46
|
-
const toOrigin = (value)=>{
|
|
47
|
-
if (!value) return;
|
|
48
|
-
try {
|
|
49
|
-
return new URL(value).origin;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const parseTraceparent = (value)=>{
|
|
55
|
-
const traceparent = firstHeaderValue(value);
|
|
56
|
-
if ('string' != typeof traceparent) return;
|
|
57
|
-
const match = traceparent.trim().match(TRACEPARENT_REGEX);
|
|
58
|
-
if (!match) return;
|
|
59
|
-
const [, traceId, spanId] = match;
|
|
60
|
-
if (!traceId || !spanId) return;
|
|
61
|
-
return {
|
|
62
|
-
traceId: traceId.toLowerCase(),
|
|
63
|
-
spanId: spanId.toLowerCase()
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
|
|
67
|
-
key
|
|
68
|
-
] : []);
|
|
20
|
+
const resolveBrowserOrigin = ()=>"u" > typeof window ? window.location.origin : void 0;
|
|
69
21
|
const originFetch = (...params)=>{
|
|
70
22
|
const [url, init] = params;
|
|
71
23
|
if (init?.method?.toLowerCase() === 'get') init.body = void 0;
|
|
72
24
|
return fetch(url, init).then(handleRes);
|
|
73
25
|
};
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
|
|
79
|
-
const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
|
|
80
|
-
const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
|
|
81
|
-
traceId: operationContext.traceId,
|
|
82
|
-
spanId: operationContext.spanId
|
|
83
|
-
} : parseTraceparent(traceparentValue);
|
|
84
|
-
return {
|
|
26
|
+
const attachEnvelopeHeaderIfRequired = (headers, requestId, finalURL)=>{
|
|
27
|
+
const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
|
|
28
|
+
if (!shouldRequireEnvelope) return;
|
|
29
|
+
headers[BFF_ENVELOPE_HEADER] = buildEnvelopeHeaderValue({
|
|
85
30
|
requestId,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
...'number' == typeof operationContext?.operationVersion ? {
|
|
93
|
-
operationVersion: operationContext.operationVersion
|
|
94
|
-
} : {},
|
|
95
|
-
...traceparentValue ? {
|
|
96
|
-
traceparent: traceparentValue
|
|
97
|
-
} : {},
|
|
98
|
-
...parsedTraceContext ? {
|
|
99
|
-
traceId: parsedTraceContext.traceId,
|
|
100
|
-
spanId: parsedTraceContext.spanId
|
|
101
|
-
} : {}
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
class ProducerClientNotInitializedError extends Error {
|
|
105
|
-
constructor(requestId){
|
|
106
|
-
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';
|
|
107
|
-
this.name = 'ProducerClientNotInitializedError';
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
class ProducerDomainNotConfiguredError extends Error {
|
|
111
|
-
constructor(requestId){
|
|
112
|
-
super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
|
|
113
|
-
this.name = 'ProducerDomainNotConfiguredError';
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
class CrossOriginEnvelopePolicyError extends Error {
|
|
117
|
-
constructor(requestId, sourceOrigin, targetOrigin){
|
|
118
|
-
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';
|
|
119
|
-
this.name = 'CrossOriginEnvelopePolicyError';
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
class IdentityBindingViolationError extends Error {
|
|
123
|
-
constructor(violation){
|
|
124
|
-
super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
|
|
125
|
-
this.name = 'IdentityBindingViolationError';
|
|
126
|
-
this.violation = violation;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
class OperationContractViolationError extends Error {
|
|
130
|
-
constructor(violation){
|
|
131
|
-
super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
|
|
132
|
-
this.name = 'OperationContractViolationError';
|
|
133
|
-
this.violation = violation;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
const validateOperationContract = (requestId, contextPayload)=>{
|
|
137
|
-
const operationContract = realOperationContract.get(requestId);
|
|
138
|
-
const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
|
|
139
|
-
if (!operationContractEnabled) return;
|
|
140
|
-
const strict = operationContract?.strict ?? true;
|
|
141
|
-
const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
|
|
142
|
-
const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
|
|
143
|
-
const maybeReportViolation = (reason)=>{
|
|
144
|
-
const violation = {
|
|
145
|
-
requestId,
|
|
146
|
-
target: 'browser',
|
|
147
|
-
operationId: contextPayload.operationId,
|
|
148
|
-
routePath: contextPayload.routePath,
|
|
149
|
-
method: contextPayload.method,
|
|
150
|
-
schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
|
|
151
|
-
operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
|
|
152
|
-
reason
|
|
153
|
-
};
|
|
154
|
-
operationContract?.onViolation?.(violation);
|
|
155
|
-
if (strict) throw new OperationContractViolationError(violation);
|
|
156
|
-
};
|
|
157
|
-
if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
|
|
158
|
-
if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
|
|
31
|
+
target: 'browser',
|
|
32
|
+
sourceOrigin: resolveBrowserOrigin(),
|
|
33
|
+
targetOrigin: toOrigin(finalURL),
|
|
34
|
+
traceContext: parseTraceparentValue(readHeader(headers, TRACEPARENT_HEADER)),
|
|
35
|
+
allowCrossOriginEnvelope: realAllowCrossOriginEnvelope.get(requestId)
|
|
36
|
+
});
|
|
159
37
|
};
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
38
|
+
const attachSecuredOperationHeaders = (headers, requestId, method, path, operationContext)=>{
|
|
39
|
+
if (!isSecuredRequestId(requestId)) return;
|
|
40
|
+
attachOperationContextHeaders({
|
|
41
|
+
headers,
|
|
42
|
+
requestId,
|
|
43
|
+
target: 'browser',
|
|
44
|
+
method,
|
|
45
|
+
path,
|
|
46
|
+
operationContext,
|
|
47
|
+
operationContract: realOperationContract.get(requestId),
|
|
48
|
+
operationContextHeader: BFF_OPERATION_CONTEXT_HEADER,
|
|
49
|
+
operationContextDetailHeader: OPERATION_CONTEXT_DETAIL_HEADER
|
|
50
|
+
});
|
|
165
51
|
};
|
|
166
52
|
const configure = (options)=>{
|
|
167
53
|
const { request, interceptor, allowedHeaders, transport, requireEnvelope, allowCrossOriginEnvelope, identityBinding, operationContract, setDomain, requestId = 'default' } = options;
|
|
@@ -205,7 +91,7 @@ const createRequest = (...args)=>{
|
|
|
205
91
|
});
|
|
206
92
|
const keyNames = extractPathParamNames(path);
|
|
207
93
|
const sender = async (...args)=>{
|
|
208
|
-
const fetcher =
|
|
94
|
+
const fetcher = resolveConfiguredRequest(realRequest, requestId, fetch1);
|
|
209
95
|
let body;
|
|
210
96
|
let finalURL;
|
|
211
97
|
let headers;
|
|
@@ -288,47 +174,8 @@ const createRequest = (...args)=>{
|
|
|
288
174
|
const configDomain = domainMap.get(requestId);
|
|
289
175
|
if ('default' !== requestId && isEmptyDomain(configDomain)) throw new ProducerDomainNotConfiguredError(requestId);
|
|
290
176
|
finalURL = `${configDomain || ''}${finalURL}`;
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const sourceOrigin = "u" > typeof window ? window.location.origin : void 0;
|
|
294
|
-
const targetOrigin = toOrigin(finalURL);
|
|
295
|
-
const traceContext = parseTraceparent(readHeader(headers, TRACEPARENT_HEADER));
|
|
296
|
-
const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
|
|
297
|
-
if (isCrossOrigin) {
|
|
298
|
-
const policy = realAllowCrossOriginEnvelope.get(requestId);
|
|
299
|
-
const isAllowed = 'function' == typeof policy ? policy({
|
|
300
|
-
requestId,
|
|
301
|
-
sourceOrigin,
|
|
302
|
-
targetOrigin,
|
|
303
|
-
target: 'browser'
|
|
304
|
-
}) : true === policy;
|
|
305
|
-
if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
|
|
306
|
-
}
|
|
307
|
-
headers[BFF_ENVELOPE_HEADER] = JSON.stringify({
|
|
308
|
-
requestId,
|
|
309
|
-
target: 'browser',
|
|
310
|
-
timestamp: Date.now(),
|
|
311
|
-
sourceOrigin,
|
|
312
|
-
targetOrigin,
|
|
313
|
-
...traceContext ? {
|
|
314
|
-
traceId: traceContext.traceId,
|
|
315
|
-
spanId: traceContext.spanId
|
|
316
|
-
} : {}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
if (isSecuredRequestId(requestId)) {
|
|
320
|
-
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
321
|
-
const contextPayload = buildOperationContext({
|
|
322
|
-
requestId,
|
|
323
|
-
method,
|
|
324
|
-
path,
|
|
325
|
-
operationContext,
|
|
326
|
-
traceparent: readHeader(headers, TRACEPARENT_HEADER)
|
|
327
|
-
});
|
|
328
|
-
validateOperationContract(requestId, contextPayload);
|
|
329
|
-
if (void 0 === readHeader(headers, BFF_OPERATION_CONTEXT_HEADER)) writeHeader(headers, BFF_OPERATION_CONTEXT_HEADER, contextPayload.operationId);
|
|
330
|
-
writeHeader(headers, OPERATION_CONTEXT_DETAIL_HEADER, JSON.stringify(contextPayload));
|
|
331
|
-
}
|
|
177
|
+
attachEnvelopeHeaderIfRequired(headers, requestId, finalURL);
|
|
178
|
+
attachSecuredOperationHeaders(headers, requestId, method, path, operationContext);
|
|
332
179
|
return executeWithResilience({
|
|
333
180
|
requestId,
|
|
334
181
|
target: 'browser',
|
|
@@ -345,16 +192,21 @@ const createRequest = (...args)=>{
|
|
|
345
192
|
};
|
|
346
193
|
return sender;
|
|
347
194
|
};
|
|
348
|
-
const createUploader = ({ path, domain, requestId = 'default' })=>{
|
|
195
|
+
const createUploader = ({ path, domain, requestId = 'default', operationContext })=>{
|
|
349
196
|
const getFinalPath = compile(path, {
|
|
350
197
|
encode: encodeURIComponent
|
|
351
198
|
});
|
|
352
199
|
const sender = (...args)=>{
|
|
353
|
-
const fetcher =
|
|
354
|
-
const { body, headers, params } = getUploadPayload(args);
|
|
200
|
+
const fetcher = resolveConfiguredRequest(realRequest, requestId, originFetch);
|
|
201
|
+
const { body, headers: uploadHeaders, params } = getUploadPayload(args);
|
|
202
|
+
const headers = {
|
|
203
|
+
...uploadHeaders
|
|
204
|
+
};
|
|
355
205
|
const finalPath = getFinalPath(params);
|
|
356
206
|
const configDomain = domainMap.get(requestId);
|
|
357
207
|
const finalURL = `${configDomain || domain || ''}${finalPath}`;
|
|
208
|
+
attachEnvelopeHeaderIfRequired(headers, requestId, finalURL);
|
|
209
|
+
attachSecuredOperationHeaders(headers, requestId, 'POST', path, operationContext);
|
|
358
210
|
return fetcher(finalURL, {
|
|
359
211
|
method: 'POST',
|
|
360
212
|
body,
|