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