@bleedingdev/modern-js-create-request 3.2.0-ultramodern.98 → 3.4.0-ultramodern.0

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,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 TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
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 buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
75
- const routePath = operationContext?.routePath || path;
76
- const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
77
- const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
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
- operationId,
87
- routePath,
88
- method: operationMethod,
89
- ...operationContext?.schemaHash ? {
90
- schemaHash: operationContext.schemaHash
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 getConfiguredRequest = (requestId, fallback)=>{
161
- const configuredRequest = realRequest.get(requestId);
162
- if (configuredRequest) return configuredRequest;
163
- if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
164
- return fallback;
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 = getConfiguredRequest(requestId, fetch1);
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
- const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
292
- if (shouldRequireEnvelope) {
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 = getConfiguredRequest(requestId, originFetch);
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,
package/dist/esm/node.mjs CHANGED
@@ -2,10 +2,12 @@ import { storage } from "@modern-js/runtime-utils/node";
2
2
  import { compile } from "path-to-regexp";
3
3
  import { stringify } from "qs";
4
4
  import { handleRes } from "./handleRes.mjs";
5
+ import { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, TRACEPARENT_HEADER, attachOperationContextHeaders, buildEnvelopeHeaderValue, deleteHeader, extractPathParamNames, firstHeaderValue, isEmptyDomain, isSecuredRequestId, parseTraceparentValue, readHeader, resolveConfiguredRequest, toOrigin, writeHeader } from "./policyCore.mjs";
5
6
  import { executeWithResilience } from "./transport.mjs";
6
7
  import { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
7
8
  import { getUploadPayload } from "./utiles.mjs";
8
9
  export * from "./requestContext.mjs";
10
+ export * from "./traceparent.mjs";
9
11
  export * from "./types.mjs";
10
12
  const realRequest = new Map();
11
13
  const realAllowedHeaders = new Map();
@@ -16,51 +18,7 @@ const realTransportResilience = new Map();
16
18
  const realIdentityBinding = new Map();
17
19
  const realOperationContract = new Map();
18
20
  const domainMap = new Map();
19
- const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
20
- const TRACEPARENT_HEADER = 'traceparent';
21
21
  const OPERATION_CONTEXT_DETAIL_HEADER = BFF_OPERATION_CONTEXT_DETAIL_HEADER;
22
- const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
23
- const isStrictDefaultRequestIdEnabled = ()=>'true' === process.env.MODERN_BFF_STRICT_DEFAULT_REQUEST_ID;
24
- const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
25
- const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
26
- const findHeaderKey = (headers, header)=>{
27
- const normalized = header.toLowerCase();
28
- return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
29
- };
30
- const readHeader = (headers, header)=>{
31
- const key = findHeaderKey(headers, header);
32
- return 'string' == typeof key ? headers[key] : void 0;
33
- };
34
- const writeHeader = (headers, header, value)=>{
35
- if (void 0 === value) return;
36
- const key = findHeaderKey(headers, header);
37
- if ('string' == typeof key && key !== header) delete headers[key];
38
- headers[header] = value;
39
- };
40
- const deleteHeader = (headers, header)=>{
41
- const key = findHeaderKey(headers, header);
42
- if ('string' == typeof key) delete headers[key];
43
- };
44
- const toOrigin = (value)=>{
45
- if (!value) return;
46
- try {
47
- return new URL(value).origin;
48
- } catch (error) {
49
- return;
50
- }
51
- };
52
- const parseTraceparent = (value)=>{
53
- const traceparent = firstHeaderValue(value);
54
- if ('string' != typeof traceparent) return;
55
- const match = traceparent.trim().match(TRACEPARENT_REGEX);
56
- if (!match) return;
57
- const [, traceId, spanId] = match;
58
- if (!traceId || !spanId) return;
59
- return {
60
- traceId: traceId.toLowerCase(),
61
- spanId: spanId.toLowerCase()
62
- };
63
- };
64
22
  const resolveSourceOrigin = (headers)=>{
65
23
  const origin = toOrigin(firstHeaderValue(headers.origin));
66
24
  if (origin) return origin;
@@ -71,103 +29,43 @@ const resolveSourceOrigin = (headers)=>{
71
29
  const proto = firstHeaderValue(headers['x-forwarded-proto']) || 'http';
72
30
  return `${proto}://${host}`;
73
31
  };
74
- const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).map(([, key])=>key);
32
+ const readIncomingWebHeaders = ()=>{
33
+ try {
34
+ return storage.useContext().headers || {};
35
+ } catch (error) {
36
+ return {};
37
+ }
38
+ };
75
39
  const originFetch = (...params)=>{
76
40
  const [, init] = params;
77
41
  if (init?.method?.toLowerCase() === 'get') init.body = void 0;
78
42
  return fetch(...params).then(handleRes);
79
43
  };
80
- const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
81
- const routePath = operationContext?.routePath || path;
82
- const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
83
- const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
84
- const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
85
- const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
86
- const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
87
- traceId: operationContext.traceId,
88
- spanId: operationContext.spanId
89
- } : parseTraceparent(traceparentValue);
90
- return {
44
+ const attachEnvelopeHeaderIfRequired = (headers, requestId, url, webRequestHeaders)=>{
45
+ const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
46
+ if (!shouldRequireEnvelope) return;
47
+ headers[BFF_ENVELOPE_HEADER] = buildEnvelopeHeaderValue({
91
48
  requestId,
92
- operationId,
93
- routePath,
94
- method: operationMethod,
95
- ...operationContext?.schemaHash ? {
96
- schemaHash: operationContext.schemaHash
97
- } : {},
98
- ...'number' == typeof operationContext?.operationVersion ? {
99
- operationVersion: operationContext.operationVersion
100
- } : {},
101
- ...traceparentValue ? {
102
- traceparent: traceparentValue
103
- } : {},
104
- ...parsedTraceContext ? {
105
- traceId: parsedTraceContext.traceId,
106
- spanId: parsedTraceContext.spanId
107
- } : {}
108
- };
109
- };
110
- class ProducerClientNotInitializedError extends Error {
111
- constructor(requestId){
112
- 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';
113
- this.name = 'ProducerClientNotInitializedError';
114
- }
115
- }
116
- class ProducerDomainNotConfiguredError extends Error {
117
- constructor(requestId){
118
- super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
119
- this.name = 'ProducerDomainNotConfiguredError';
120
- }
121
- }
122
- class CrossOriginEnvelopePolicyError extends Error {
123
- constructor(requestId, sourceOrigin, targetOrigin){
124
- 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';
125
- this.name = 'CrossOriginEnvelopePolicyError';
126
- }
127
- }
128
- class IdentityBindingViolationError extends Error {
129
- constructor(violation){
130
- super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
131
- this.name = 'IdentityBindingViolationError';
132
- this.violation = violation;
133
- }
134
- }
135
- class OperationContractViolationError extends Error {
136
- constructor(violation){
137
- super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
138
- this.name = 'OperationContractViolationError';
139
- this.violation = violation;
140
- }
141
- }
142
- const validateOperationContract = (requestId, contextPayload)=>{
143
- const operationContract = realOperationContract.get(requestId);
144
- const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
145
- if (!operationContractEnabled) return;
146
- const strict = operationContract?.strict ?? true;
147
- const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
148
- const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
149
- const maybeReportViolation = (reason)=>{
150
- const violation = {
151
- requestId,
152
- target: 'server',
153
- operationId: contextPayload.operationId,
154
- routePath: contextPayload.routePath,
155
- method: contextPayload.method,
156
- schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
157
- operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
158
- reason
159
- };
160
- operationContract?.onViolation?.(violation);
161
- if (strict) throw new OperationContractViolationError(violation);
162
- };
163
- if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
164
- if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
49
+ target: 'server',
50
+ sourceOrigin: resolveSourceOrigin(webRequestHeaders),
51
+ targetOrigin: toOrigin(url),
52
+ traceContext: parseTraceparentValue(readHeader(headers, TRACEPARENT_HEADER)),
53
+ allowCrossOriginEnvelope: realAllowCrossOriginEnvelope.get(requestId)
54
+ });
165
55
  };
166
- const getConfiguredRequest = (requestId, fallback)=>{
167
- const configuredRequest = realRequest.get(requestId);
168
- if (configuredRequest) return configuredRequest;
169
- if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
170
- return fallback;
56
+ const attachSecuredOperationHeaders = (headers, requestId, method, path, operationContext)=>{
57
+ if (!isSecuredRequestId(requestId)) return;
58
+ attachOperationContextHeaders({
59
+ headers,
60
+ requestId,
61
+ target: 'server',
62
+ method,
63
+ path,
64
+ operationContext,
65
+ operationContract: realOperationContract.get(requestId),
66
+ operationContextHeader: BFF_OPERATION_CONTEXT_HEADER,
67
+ operationContextDetailHeader: OPERATION_CONTEXT_DETAIL_HEADER
68
+ });
171
69
  };
172
70
  const configure = (options)=>{
173
71
  const { request, interceptor, allowedHeaders, resolveHeaders, transport, requireEnvelope, allowCrossOriginEnvelope, identityBinding, operationContract, setDomain, requestId = 'default' } = options;
@@ -212,13 +110,8 @@ const createRequest = (...args)=>{
212
110
  });
213
111
  const keyNames = extractPathParamNames(path);
214
112
  const sender = (...args)=>{
215
- const fetcher = getConfiguredRequest(requestId, fetch1);
216
- let webRequestHeaders = {};
217
- try {
218
- webRequestHeaders = storage.useContext().headers || {};
219
- } catch (error) {
220
- webRequestHeaders = {};
221
- }
113
+ const fetcher = resolveConfiguredRequest(realRequest, requestId, fetch1);
114
+ const webRequestHeaders = readIncomingWebHeaders();
222
115
  let body;
223
116
  let headers;
224
117
  let url;
@@ -330,46 +223,8 @@ const createRequest = (...args)=>{
330
223
  if ('string' == typeof incomingTraceparent) writeHeader(headers, TRACEPARENT_HEADER, incomingTraceparent);
331
224
  }
332
225
  if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
333
- const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
334
- if (shouldRequireEnvelope) {
335
- const sourceOrigin = resolveSourceOrigin(webRequestHeaders);
336
- const targetOrigin = toOrigin(url);
337
- const traceContext = parseTraceparent(readHeader(headers, TRACEPARENT_HEADER));
338
- const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
339
- if (isCrossOrigin) {
340
- const policy = realAllowCrossOriginEnvelope.get(requestId);
341
- const isAllowed = 'function' == typeof policy ? policy({
342
- requestId,
343
- sourceOrigin,
344
- targetOrigin,
345
- target: 'server'
346
- }) : true === policy;
347
- if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
348
- }
349
- headers[BFF_ENVELOPE_HEADER] = JSON.stringify({
350
- requestId,
351
- target: 'server',
352
- timestamp: Date.now(),
353
- sourceOrigin,
354
- targetOrigin,
355
- ...traceContext ? {
356
- traceId: traceContext.traceId,
357
- spanId: traceContext.spanId
358
- } : {}
359
- });
360
- }
361
- if (isSecuredRequestId(requestId)) {
362
- const contextPayload = buildOperationContext({
363
- requestId,
364
- method,
365
- path,
366
- operationContext,
367
- traceparent: readHeader(headers, TRACEPARENT_HEADER)
368
- });
369
- validateOperationContract(requestId, contextPayload);
370
- if (void 0 === readHeader(headers, BFF_OPERATION_CONTEXT_HEADER)) writeHeader(headers, BFF_OPERATION_CONTEXT_HEADER, contextPayload.operationId);
371
- writeHeader(headers, OPERATION_CONTEXT_DETAIL_HEADER, JSON.stringify(contextPayload));
372
- }
226
+ attachEnvelopeHeaderIfRequired(headers, requestId, url, webRequestHeaders);
227
+ attachSecuredOperationHeaders(headers, requestId, method, path, operationContext);
373
228
  if ('get' === method.toLowerCase()) body = void 0;
374
229
  headers.accept = "application/json,*/*;q=0.8";
375
230
  return executeWithResilience({
@@ -388,12 +243,17 @@ const createRequest = (...args)=>{
388
243
  };
389
244
  return sender;
390
245
  };
391
- const createUploader = ({ path, requestId = 'default' })=>{
246
+ const createUploader = ({ path, requestId = 'default', operationContext })=>{
392
247
  const sender = (...args)=>{
393
- const fetcher = getConfiguredRequest(requestId, originFetch);
394
- const { body, headers } = getUploadPayload(args);
248
+ const fetcher = resolveConfiguredRequest(realRequest, requestId, originFetch);
249
+ const { body, headers: uploadHeaders } = getUploadPayload(args);
250
+ const headers = {
251
+ ...uploadHeaders
252
+ };
395
253
  const configDomain = domainMap.get(requestId);
396
254
  const finalURL = `${configDomain || ''}${path}`;
255
+ attachEnvelopeHeaderIfRequired(headers, requestId, finalURL, readIncomingWebHeaders());
256
+ attachSecuredOperationHeaders(headers, requestId, 'POST', path, operationContext);
397
257
  return fetcher(finalURL, {
398
258
  method: 'POST',
399
259
  body,