@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/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,
@@ -0,0 +1,174 @@
1
+ import { parseTraceparent } from "./traceparent.mjs";
2
+ const TRACEPARENT_HEADER = 'traceparent';
3
+ const readProcessEnv = (key)=>{
4
+ if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
5
+ return process.env[key];
6
+ };
7
+ const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
8
+ const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
9
+ const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
10
+ const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
11
+ const findHeaderKey = (headers, header)=>{
12
+ const normalized = header.toLowerCase();
13
+ return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
14
+ };
15
+ const readHeader = (headers, header)=>{
16
+ const key = findHeaderKey(headers, header);
17
+ return 'string' == typeof key ? headers[key] : void 0;
18
+ };
19
+ const writeHeader = (headers, header, value)=>{
20
+ if (void 0 === value) return;
21
+ const key = findHeaderKey(headers, header);
22
+ if ('string' == typeof key && key !== header) delete headers[key];
23
+ headers[header] = value;
24
+ };
25
+ const deleteHeader = (headers, header)=>{
26
+ const key = findHeaderKey(headers, header);
27
+ if ('string' == typeof key) delete headers[key];
28
+ };
29
+ const toOrigin = (value)=>{
30
+ if (!value) return;
31
+ try {
32
+ return new URL(value).origin;
33
+ } catch (error) {
34
+ return;
35
+ }
36
+ };
37
+ const parseTraceparentValue = (value)=>parseTraceparent(firstHeaderValue(value));
38
+ const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
39
+ key
40
+ ] : []);
41
+ const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
42
+ const routePath = operationContext?.routePath || path;
43
+ const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
44
+ const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
45
+ const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
46
+ const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
47
+ const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
48
+ traceId: operationContext.traceId,
49
+ spanId: operationContext.spanId
50
+ } : parseTraceparentValue(traceparentValue);
51
+ return {
52
+ requestId,
53
+ operationId,
54
+ routePath,
55
+ method: operationMethod,
56
+ ...operationContext?.schemaHash ? {
57
+ schemaHash: operationContext.schemaHash
58
+ } : {},
59
+ ...'number' == typeof operationContext?.operationVersion ? {
60
+ operationVersion: operationContext.operationVersion
61
+ } : {},
62
+ ...traceparentValue ? {
63
+ traceparent: traceparentValue
64
+ } : {},
65
+ ...parsedTraceContext ? {
66
+ traceId: parsedTraceContext.traceId,
67
+ spanId: parsedTraceContext.spanId
68
+ } : {}
69
+ };
70
+ };
71
+ class ProducerClientNotInitializedError extends Error {
72
+ constructor(requestId){
73
+ 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';
74
+ this.name = 'ProducerClientNotInitializedError';
75
+ }
76
+ }
77
+ class ProducerDomainNotConfiguredError extends Error {
78
+ constructor(requestId){
79
+ super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
80
+ this.name = 'ProducerDomainNotConfiguredError';
81
+ }
82
+ }
83
+ class CrossOriginEnvelopePolicyError extends Error {
84
+ constructor(requestId, sourceOrigin, targetOrigin){
85
+ 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';
86
+ this.name = 'CrossOriginEnvelopePolicyError';
87
+ }
88
+ }
89
+ class IdentityBindingViolationError extends Error {
90
+ constructor(violation){
91
+ super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
92
+ this.name = 'IdentityBindingViolationError';
93
+ this.violation = violation;
94
+ }
95
+ }
96
+ class OperationContractViolationError extends Error {
97
+ constructor(violation){
98
+ super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
99
+ this.name = 'OperationContractViolationError';
100
+ this.violation = violation;
101
+ }
102
+ }
103
+ const validateOperationContract = ({ requestId, target, contextPayload, operationContract })=>{
104
+ const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
105
+ if (!operationContractEnabled) return;
106
+ const strict = operationContract?.strict ?? true;
107
+ const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
108
+ const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
109
+ const maybeReportViolation = (reason)=>{
110
+ const violation = {
111
+ requestId,
112
+ target,
113
+ operationId: contextPayload.operationId,
114
+ routePath: contextPayload.routePath,
115
+ method: contextPayload.method,
116
+ schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
117
+ operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
118
+ reason
119
+ };
120
+ operationContract?.onViolation?.(violation);
121
+ if (strict) throw new OperationContractViolationError(violation);
122
+ };
123
+ if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
124
+ if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
125
+ };
126
+ const resolveConfiguredRequest = (configuredRequests, requestId, fallback)=>{
127
+ const configuredRequest = configuredRequests.get(requestId);
128
+ if (configuredRequest) return configuredRequest;
129
+ if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
130
+ return fallback;
131
+ };
132
+ const buildEnvelopeHeaderValue = ({ requestId, target, sourceOrigin, targetOrigin, traceContext, allowCrossOriginEnvelope })=>{
133
+ const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
134
+ if (isCrossOrigin) {
135
+ const isAllowed = 'function' == typeof allowCrossOriginEnvelope ? allowCrossOriginEnvelope({
136
+ requestId,
137
+ sourceOrigin,
138
+ targetOrigin,
139
+ target
140
+ }) : true === allowCrossOriginEnvelope;
141
+ if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
142
+ }
143
+ return JSON.stringify({
144
+ requestId,
145
+ target,
146
+ timestamp: Date.now(),
147
+ sourceOrigin,
148
+ targetOrigin,
149
+ ...traceContext ? {
150
+ traceId: traceContext.traceId,
151
+ spanId: traceContext.spanId
152
+ } : {}
153
+ });
154
+ };
155
+ const attachOperationContextHeaders = ({ headers, requestId, target, method, path, operationContext, operationContract, operationContextHeader, operationContextDetailHeader })=>{
156
+ if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
157
+ const contextPayload = buildOperationContext({
158
+ requestId,
159
+ method,
160
+ path,
161
+ operationContext,
162
+ traceparent: readHeader(headers, TRACEPARENT_HEADER)
163
+ });
164
+ validateOperationContract({
165
+ requestId,
166
+ target,
167
+ contextPayload,
168
+ operationContract
169
+ });
170
+ if (void 0 === readHeader(headers, operationContextHeader)) writeHeader(headers, operationContextHeader, contextPayload.operationId);
171
+ writeHeader(headers, operationContextDetailHeader, JSON.stringify(contextPayload));
172
+ return contextPayload;
173
+ };
174
+ export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, TRACEPARENT_HEADER, attachOperationContextHeaders, buildEnvelopeHeaderValue, buildOperationContext, deleteHeader, extractPathParamNames, findHeaderKey, firstHeaderValue, isEmptyDomain, isSecuredRequestId, isStrictDefaultRequestIdEnabled, parseTraceparentValue, readHeader, resolveConfiguredRequest, toOrigin, validateOperationContract, writeHeader };
@@ -1,6 +1,6 @@
1
+ import { parseTraceparent } from "./traceparent.mjs";
1
2
  const BFF_LOCALE_HEADER = 'accept-language';
2
3
  const BFF_TRACEPARENT_HEADER = 'traceparent';
3
- const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
4
4
  const readHeader = (headers, header)=>{
5
5
  if (!headers) return;
6
6
  const normalized = header.toLowerCase();
@@ -10,17 +10,6 @@ const readHeader = (headers, header)=>{
10
10
  return Array.isArray(value) ? value[0] : value;
11
11
  };
12
12
  const readString = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
13
- function parseTraceparent(traceparent) {
14
- if (!traceparent) return;
15
- const match = traceparent.trim().match(TRACEPARENT_REGEX);
16
- if (!match) return;
17
- const [, traceId, spanId] = match;
18
- if (!traceId || !spanId) return;
19
- return {
20
- traceId: traceId.toLowerCase(),
21
- spanId: spanId.toLowerCase()
22
- };
23
- }
24
13
  function createOperationContextSnapshot(operationContext, safeContext) {
25
14
  if (!operationContext) return;
26
15
  const snapshot = {
@@ -0,0 +1,18 @@
1
+ const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i;
2
+ const isAllZeroHex = (value)=>/^0+$/.test(value);
3
+ function parseTraceparent(traceparent) {
4
+ if (!traceparent) return;
5
+ const match = traceparent.trim().match(TRACEPARENT_REGEX);
6
+ if (!match) return;
7
+ const [, rawTraceId, rawSpanId, rawFlags] = match;
8
+ if (!rawTraceId || !rawSpanId || !rawFlags) return;
9
+ const traceId = rawTraceId.toLowerCase();
10
+ const spanId = rawSpanId.toLowerCase();
11
+ if (isAllZeroHex(traceId) || isAllZeroHex(spanId)) return;
12
+ return {
13
+ traceId,
14
+ spanId,
15
+ sampled: (0x1 & Number.parseInt(rawFlags, 16)) === 1
16
+ };
17
+ }
18
+ export { parseTraceparent };