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