@bleedingdev/modern-js-runtime-extensions 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
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/buildIdentity.js +73 -0
- package/dist/cjs/contextExtensions.js +74 -0
- package/dist/cjs/headRuntime.js +91 -0
- package/dist/cjs/helmetContext.js +54 -0
- package/dist/cjs/index.js +18 -4
- package/dist/cjs/localisedLoaderIdentity.js +51 -0
- package/dist/cjs/prefetchPolicy.js +52 -0
- package/dist/cjs/reactContext.js +45 -0
- package/dist/cjs/request-policy/browser.js +139 -0
- package/dist/cjs/request-policy/client.js +234 -0
- package/dist/cjs/request-policy/node.js +149 -0
- package/dist/cjs/request-policy/policyCore.js +266 -0
- package/dist/cjs/request-policy/requestContext.js +137 -0
- package/dist/cjs/request-policy/traceparent.js +56 -0
- package/dist/cjs/request-policy/transport.js +234 -0
- package/dist/cjs/request-policy/types.js +57 -0
- package/dist/cjs/routeHydration.js +127 -0
- package/dist/cjs/routerPrefetchPolicy.js +153 -0
- package/dist/cjs/routerProvider.js +170 -0
- package/dist/cjs/routerState.js +167 -0
- package/dist/cjs/routerStatePlugin.js +86 -0
- package/dist/cjs/routerStateTypes.js +18 -0
- package/dist/cjs/rsc-disabled/rscDisabledClientBrowserRuntime.js +69 -0
- package/dist/cjs/rsc-disabled/rscDisabledClientEdgeRuntime.js +61 -0
- package/dist/cjs/rsc-disabled/rscDisabledClientNodeRuntime.js +65 -0
- package/dist/cjs/rsc-disabled/rscDisabledRuntimeError.js +42 -0
- package/dist/cjs/rsc-disabled/rscDisabledServerEdgeRuntime.js +93 -0
- package/dist/cjs/rsc-disabled/rscDisabledServerNodeRuntime.js +101 -0
- package/dist/cjs/runtimeContextProjection.js +112 -0
- package/dist/cjs/server-config/bffRuntime.js +18 -0
- package/dist/cjs/server-config/index.js +18 -0
- package/dist/cjs/server-config/serverTelemetry.js +18 -0
- package/dist/cjs/shellStream.js +34 -2
- package/dist/cjs/workspaceEvents.js +159 -0
- package/dist/esm/buildIdentity.mjs +35 -0
- package/dist/esm/contextExtensions.mjs +33 -0
- package/dist/esm/headRuntime.mjs +43 -0
- package/dist/esm/helmetContext.mjs +13 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/localisedLoaderIdentity.mjs +13 -0
- package/dist/esm/prefetchPolicy.mjs +11 -0
- package/dist/esm/reactContext.mjs +7 -0
- package/dist/esm/request-policy/browser.mjs +11 -0
- package/dist/esm/request-policy/client.mjs +195 -0
- package/dist/esm/request-policy/policyCore.mjs +171 -0
- package/dist/esm/request-policy/requestContext.mjs +75 -0
- package/dist/esm/request-policy/traceparent.mjs +18 -0
- package/dist/esm/request-policy/transport.mjs +196 -0
- package/dist/esm/request-policy/types.mjs +10 -0
- package/dist/esm/routeHydration.mjs +77 -0
- package/dist/esm/routerPrefetchPolicy.mjs +115 -0
- package/dist/esm/routerProvider.mjs +117 -0
- package/dist/esm/routerState.mjs +105 -0
- package/dist/esm/routerStatePlugin.mjs +48 -0
- package/dist/esm/routerStateTypes.mjs +0 -0
- package/dist/esm/rsc-disabled/rscDisabledClientBrowserRuntime.mjs +10 -0
- package/dist/esm/rsc-disabled/rscDisabledClientEdgeRuntime.mjs +8 -0
- package/dist/esm/rsc-disabled/rscDisabledClientNodeRuntime.mjs +9 -0
- package/dist/esm/rsc-disabled/rscDisabledRuntimeError.mjs +4 -0
- package/dist/esm/rsc-disabled/rscDisabledServerEdgeRuntime.mjs +16 -0
- package/dist/esm/rsc-disabled/rscDisabledServerNodeRuntime.mjs +18 -0
- package/dist/esm/runtimeContextProjection.mjs +74 -0
- package/dist/esm/server-config/bffRuntime.mjs +0 -0
- package/dist/esm/server-config/index.mjs +0 -0
- package/dist/esm/server-config/serverTelemetry.mjs +0 -0
- package/dist/esm/shellStream.mjs +34 -2
- package/dist/esm/workspaceEvents.mjs +66 -0
- package/dist/esm-node/buildIdentity.mjs +36 -0
- package/dist/esm-node/contextExtensions.mjs +34 -0
- package/dist/esm-node/headRuntime.mjs +44 -0
- package/dist/esm-node/helmetContext.mjs +14 -0
- package/dist/esm-node/index.mjs +2 -0
- package/dist/esm-node/localisedLoaderIdentity.mjs +14 -0
- package/dist/esm-node/prefetchPolicy.mjs +12 -0
- package/dist/esm-node/reactContext.mjs +8 -0
- package/dist/esm-node/request-policy/browser.mjs +12 -0
- package/dist/esm-node/request-policy/client.mjs +196 -0
- package/dist/esm-node/request-policy/node.mjs +22 -0
- package/dist/esm-node/request-policy/policyCore.mjs +172 -0
- package/dist/esm-node/request-policy/requestContext.mjs +76 -0
- package/dist/esm-node/request-policy/traceparent.mjs +19 -0
- package/dist/esm-node/request-policy/transport.mjs +197 -0
- package/dist/esm-node/request-policy/types.mjs +11 -0
- package/dist/esm-node/routeHydration.mjs +78 -0
- package/dist/esm-node/routerPrefetchPolicy.mjs +116 -0
- package/dist/esm-node/routerProvider.mjs +118 -0
- package/dist/esm-node/routerState.mjs +106 -0
- package/dist/esm-node/routerStatePlugin.mjs +49 -0
- package/dist/esm-node/routerStateTypes.mjs +1 -0
- package/dist/esm-node/rsc-disabled/rscDisabledClientBrowserRuntime.mjs +11 -0
- package/dist/esm-node/rsc-disabled/rscDisabledClientEdgeRuntime.mjs +9 -0
- package/dist/esm-node/rsc-disabled/rscDisabledClientNodeRuntime.mjs +10 -0
- package/dist/esm-node/rsc-disabled/rscDisabledRuntimeError.mjs +5 -0
- package/dist/esm-node/rsc-disabled/rscDisabledServerEdgeRuntime.mjs +17 -0
- package/dist/esm-node/rsc-disabled/rscDisabledServerNodeRuntime.mjs +19 -0
- package/dist/esm-node/runtimeContextProjection.mjs +75 -0
- package/dist/esm-node/server-config/bffRuntime.mjs +1 -0
- package/dist/esm-node/server-config/index.mjs +1 -0
- package/dist/esm-node/server-config/serverTelemetry.mjs +1 -0
- package/dist/esm-node/shellStream.mjs +34 -2
- package/dist/esm-node/workspaceEvents.mjs +67 -0
- package/dist/types/buildIdentity.d.ts +37 -0
- package/dist/types/contextExtensions.d.ts +34 -0
- package/dist/types/headRuntime.d.ts +12 -0
- package/dist/types/helmetContext.d.ts +10 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/localisedLoaderIdentity.d.ts +10 -0
- package/dist/types/prefetchPolicy.d.ts +4 -0
- package/dist/types/reactContext.d.ts +6 -0
- package/dist/types/request-policy/browser.d.ts +10 -0
- package/dist/types/request-policy/client.d.ts +14 -0
- package/dist/types/request-policy/node.d.ts +10 -0
- package/dist/types/request-policy/policyCore.d.ts +107 -0
- package/dist/types/request-policy/requestContext.d.ts +22 -0
- package/dist/types/request-policy/traceparent.d.ts +10 -0
- package/dist/types/request-policy/transport.d.ts +12 -0
- package/dist/types/request-policy/types.d.ts +144 -0
- package/dist/types/routeHydration.d.ts +24 -0
- package/dist/types/routerPrefetchPolicy.d.ts +28 -0
- package/dist/types/routerProvider.d.ts +77 -0
- package/dist/types/routerState.d.ts +12 -0
- package/dist/types/routerStatePlugin.d.ts +19 -0
- package/dist/types/routerStateTypes.d.ts +40 -0
- package/dist/types/rsc-disabled/rscDisabledClientBrowserRuntime.d.ts +9 -0
- package/dist/types/rsc-disabled/rscDisabledClientEdgeRuntime.d.ts +7 -0
- package/dist/types/rsc-disabled/rscDisabledClientNodeRuntime.d.ts +8 -0
- package/dist/types/rsc-disabled/rscDisabledRuntimeError.d.ts +1 -0
- package/dist/types/rsc-disabled/rscDisabledServerEdgeRuntime.d.ts +15 -0
- package/dist/types/rsc-disabled/rscDisabledServerNodeRuntime.d.ts +17 -0
- package/dist/types/runtimeContextProjection.d.ts +30 -0
- package/dist/types/server-config/bffRuntime.d.ts +122 -0
- package/dist/types/server-config/index.d.ts +2 -0
- package/dist/types/server-config/serverTelemetry.d.ts +322 -0
- package/dist/types/workspaceEvents.d.ts +62 -0
- package/package.json +238 -13
- package/rslib.config.mts +17 -2
- package/rstest.config.mts +33 -2
- package/src/buildIdentity.ts +61 -0
- package/src/contextExtensions.ts +81 -0
- package/src/headRuntime.tsx +77 -0
- package/src/helmetContext.ts +31 -0
- package/src/index.ts +2 -0
- package/src/localisedLoaderIdentity.ts +30 -0
- package/src/prefetchPolicy.ts +33 -0
- package/src/reactContext.ts +16 -0
- package/src/request-policy/browser.ts +22 -0
- package/src/request-policy/client.ts +404 -0
- package/src/request-policy/node.ts +41 -0
- package/src/request-policy/policyCore.ts +424 -0
- package/src/request-policy/requestContext.ts +149 -0
- package/src/request-policy/traceparent.ts +43 -0
- package/src/request-policy/transport.ts +349 -0
- package/src/request-policy/types.ts +229 -0
- package/src/routeHydration.ts +208 -0
- package/src/routerPrefetchPolicy.ts +195 -0
- package/src/routerProvider.ts +340 -0
- package/src/routerState.ts +208 -0
- package/src/routerStatePlugin.ts +86 -0
- package/src/routerStateTypes.ts +46 -0
- package/src/rsc-disabled/rscDisabledClientBrowserRuntime.ts +10 -0
- package/src/rsc-disabled/rscDisabledClientEdgeRuntime.ts +8 -0
- package/src/rsc-disabled/rscDisabledClientNodeRuntime.ts +9 -0
- package/src/rsc-disabled/rscDisabledRuntimeError.ts +3 -0
- package/src/rsc-disabled/rscDisabledServerEdgeRuntime.ts +16 -0
- package/src/rsc-disabled/rscDisabledServerNodeRuntime.ts +18 -0
- package/src/runtimeContextProjection.ts +105 -0
- package/src/server-config/bffRuntime.ts +136 -0
- package/src/server-config/index.ts +2 -0
- package/src/server-config/serverTelemetry.ts +337 -0
- package/src/shellStream.ts +55 -25
- package/src/workspaceEvents.ts +324 -0
- package/tests/buildIdentity.test.ts +70 -0
- package/tests/contextExtensions.test.ts +71 -0
- package/tests/headRuntime.test.tsx +369 -0
- package/tests/nativeSafeFailureCompatibility.test.ts +117 -0
- package/tests/prefetchPolicy.test.ts +51 -0
- package/tests/rendererHead.test.ts +1 -72
- package/tests/request-policy/browser-policy.test.ts +608 -0
- package/tests/request-policy/client-isolation.test.ts +149 -0
- package/tests/request-policy/identityBinding.test.ts +75 -0
- package/tests/request-policy/node-policy.test.ts +741 -0
- package/tests/request-policy/policyCore.test.ts +250 -0
- package/tests/request-policy/requestContext.test.ts +80 -0
- package/tests/request-policy/requestFactory.outbound-contract.test.ts +691 -0
- package/tests/request-policy/traceparent.test.ts +55 -0
- package/tests/request-policy/transport-retry.test.ts +370 -0
- package/tests/routeHydration.test.ts +180 -0
- package/tests/routerPrefetchPolicy.test.ts +102 -0
- package/tests/routerProvider.test.ts +38 -0
- package/tests/routerState.test.ts +157 -0
- package/tests/routerStatePlugin.test.ts +108 -0
- package/tests/runtimeContextProjection.test.ts +135 -0
- package/tests/shellStream.test.ts +37 -0
- package/tests/workspaceEvents.test.ts +391 -0
- package/dist/cjs/rscHtmlStream.js +0 -128
- package/dist/esm/rscHtmlStream.mjs +0 -86
- package/dist/esm-node/rscHtmlStream.mjs +0 -87
- package/dist/types/rscHtmlStream.d.ts +0 -4
- package/src/rscHtmlStream.ts +0 -142
- package/tests/rscHtmlStream.test.ts +0 -153
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import react from "react";
|
|
4
|
+
import { Helmet, HelmetProvider } from "react-helmet-async";
|
|
5
|
+
import * as __rspack_external__rendererHead_mjs_b8160cc7 from "./rendererHead.mjs";
|
|
6
|
+
function createHeadRuntime() {
|
|
7
|
+
const HeadContext = /*#__PURE__*/ react.createContext(void 0);
|
|
8
|
+
const collectServerHelmet = ({ runtimeContext, helmetContext }, props)=>{
|
|
9
|
+
const createRecord = ()=>__rspack_external__rendererHead_mjs_b8160cc7.createHelmetRecord(react, props);
|
|
10
|
+
const deriveState = (records)=>__rspack_external__rendererHead_mjs_b8160cc7.deriveHelmetServerState(react, records);
|
|
11
|
+
const marker = __rspack_external__rendererHead_mjs_b8160cc7.collectHeadState(runtimeContext, createRecord, deriveState, helmetContext);
|
|
12
|
+
if (void 0 !== marker) return marker;
|
|
13
|
+
helmetContext.helmet = __rspack_external__rendererHead_mjs_b8160cc7.collectImmediateHelmetState(react, helmetContext, createRecord());
|
|
14
|
+
};
|
|
15
|
+
const Head = (props)=>{
|
|
16
|
+
const state = react.useContext(HeadContext);
|
|
17
|
+
if (void 0 !== state) return __rspack_external__rendererHead_mjs_b8160cc7.renderHeadMarker(react, collectServerHelmet(state, props));
|
|
18
|
+
return /*#__PURE__*/ react.createElement(Helmet, props);
|
|
19
|
+
};
|
|
20
|
+
const ClientRoot = ({ children })=>{
|
|
21
|
+
const state = react.useContext(HeadContext);
|
|
22
|
+
return void 0 === state ? /*#__PURE__*/ jsx(HelmetProvider, {
|
|
23
|
+
children: children
|
|
24
|
+
}) : children;
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
Head,
|
|
28
|
+
wrapServerRoot (root, state) {
|
|
29
|
+
return /*#__PURE__*/ jsx(HeadContext.Provider, {
|
|
30
|
+
value: state,
|
|
31
|
+
children: /*#__PURE__*/ jsx(HelmetProvider, {
|
|
32
|
+
context: state.helmetContext,
|
|
33
|
+
children: root
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
wrapClientRoot (root) {
|
|
38
|
+
return /*#__PURE__*/ jsx(ClientRoot, {
|
|
39
|
+
children: root
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export { createHeadRuntime };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createRuntimeContextExtension } from "./contextExtensions.mjs";
|
|
3
|
+
const helmetContextExtension = createRuntimeContextExtension('@modern-js/runtime:helmet-context');
|
|
4
|
+
function getHelmetContext(context) {
|
|
5
|
+
return helmetContextExtension.get(context);
|
|
6
|
+
}
|
|
7
|
+
function ensureHelmetContext(context) {
|
|
8
|
+
const existing = helmetContextExtension.get(context);
|
|
9
|
+
if (void 0 !== existing) return existing;
|
|
10
|
+
const created = {};
|
|
11
|
+
helmetContextExtension.set(context, created);
|
|
12
|
+
return created;
|
|
13
|
+
}
|
|
14
|
+
export { ensureHelmetContext, getHelmetContext };
|
package/dist/esm-node/index.mjs
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
function resolveLocalisedLoaderRouteId(routes, requestedId, matchedIds = []) {
|
|
3
|
+
if (matchedIds.includes(requestedId)) return requestedId;
|
|
4
|
+
const matched = new Set(matchedIds);
|
|
5
|
+
const visit = (nodes)=>{
|
|
6
|
+
for (const route of nodes){
|
|
7
|
+
if (route.id && matched.has(route.id) && route.modernLocalisedRoute?.id === requestedId) return route.id;
|
|
8
|
+
const child = route.children && visit(route.children);
|
|
9
|
+
if (child) return child;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
return visit(routes) ?? requestedId;
|
|
13
|
+
}
|
|
14
|
+
export { resolveLocalisedLoaderRouteId };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
function resolvePreloadFromPrefetch(prefetch, preload) {
|
|
3
|
+
if (void 0 !== preload) return preload;
|
|
4
|
+
if ('none' === prefetch) return false;
|
|
5
|
+
if ('intent' === prefetch || 'render' === prefetch || 'viewport' === prefetch) return prefetch;
|
|
6
|
+
return 'viewport';
|
|
7
|
+
}
|
|
8
|
+
function normalizePreloadBehavior(preload, prefetch) {
|
|
9
|
+
const resolved = resolvePreloadFromPrefetch(prefetch, preload);
|
|
10
|
+
return false === resolved ? 'none' : resolved;
|
|
11
|
+
}
|
|
12
|
+
export { normalizePreloadBehavior, resolvePreloadFromPrefetch };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
function getRuntimeReactContext(kind, create) {
|
|
3
|
+
const key = Symbol.for(`@modern-js/runtime:react-context:v1:${kind}`);
|
|
4
|
+
const contexts = globalThis;
|
|
5
|
+
contexts[key] ??= create();
|
|
6
|
+
return contexts[key];
|
|
7
|
+
}
|
|
8
|
+
export { getRuntimeReactContext };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createClient } from "@modern-js/create-request/client";
|
|
3
|
+
import { createPolicyClient } from "./client.mjs";
|
|
4
|
+
export * from "./requestContext.mjs";
|
|
5
|
+
export * from "./types.mjs";
|
|
6
|
+
const browser_createClient = ()=>createPolicyClient(createClient(), {
|
|
7
|
+
target: 'browser',
|
|
8
|
+
resolveSourceOrigin: ()=>"u" > typeof window ? window.location.origin : void 0
|
|
9
|
+
});
|
|
10
|
+
const { configure: configure, createRequest: createRequest, createUploader: createUploader } = browser_createClient();
|
|
11
|
+
export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError } from "./policyCore.mjs";
|
|
12
|
+
export { browser_createClient as createClient, configure, createRequest, createUploader };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { IdentityBindingViolationError, ProducerDomainNotConfiguredError, TRACEPARENT_HEADER, attachOperationContextHeaders, buildEnvelopeHeaderValue, deleteHeader, firstHeaderValue, isEmptyDomain, isSecuredRequestId, parseTraceparentValue, readHeader, resolveConfiguredRequest, toOrigin, writeHeader } from "./policyCore.mjs";
|
|
3
|
+
import { executeWithResilience } from "./transport.mjs";
|
|
4
|
+
import { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
|
|
5
|
+
const createPolicyClient = (nativeClient, environment)=>{
|
|
6
|
+
const isServerTarget = 'server' === environment.target;
|
|
7
|
+
const configuredRequests = new Map();
|
|
8
|
+
const realResolveHeaders = new Map();
|
|
9
|
+
const realRequireEnvelope = new Map();
|
|
10
|
+
const realAllowCrossOriginEnvelope = new Map();
|
|
11
|
+
const realTransportResilience = new Map();
|
|
12
|
+
const realIdentityBinding = new Map();
|
|
13
|
+
const realOperationContract = new Map();
|
|
14
|
+
const domainMap = new Map();
|
|
15
|
+
const attachEnvelopeHeaderIfRequired = (headers, requestId, url, incomingHeaders)=>{
|
|
16
|
+
const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
|
|
17
|
+
if (!shouldRequireEnvelope) return;
|
|
18
|
+
headers[BFF_ENVELOPE_HEADER] = buildEnvelopeHeaderValue({
|
|
19
|
+
requestId,
|
|
20
|
+
target: environment.target,
|
|
21
|
+
sourceOrigin: environment.resolveSourceOrigin(incomingHeaders),
|
|
22
|
+
targetOrigin: toOrigin(url),
|
|
23
|
+
traceContext: parseTraceparentValue(readHeader(headers, TRACEPARENT_HEADER)),
|
|
24
|
+
allowCrossOriginEnvelope: realAllowCrossOriginEnvelope.get(requestId)
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
const attachSecuredOperationHeaders = (headers, requestId, method, path, operationContext)=>{
|
|
28
|
+
if (!isSecuredRequestId(requestId)) return;
|
|
29
|
+
attachOperationContextHeaders({
|
|
30
|
+
headers,
|
|
31
|
+
requestId,
|
|
32
|
+
target: environment.target,
|
|
33
|
+
method,
|
|
34
|
+
path,
|
|
35
|
+
operationContext,
|
|
36
|
+
operationContract: realOperationContract.get(requestId),
|
|
37
|
+
operationContextHeader: BFF_OPERATION_CONTEXT_HEADER,
|
|
38
|
+
operationContextDetailHeader: BFF_OPERATION_CONTEXT_DETAIL_HEADER
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const applyIdentityAndForwardedHeaders = ({ headers, requestId, incomingHeaders, forwardedHeaders, allowedHeaders: targetAllowedHeaders })=>{
|
|
42
|
+
const identityBinding = realIdentityBinding.get(requestId);
|
|
43
|
+
const identityBindingEnabled = identityBinding?.enabled ?? isSecuredRequestId(requestId);
|
|
44
|
+
const identityBindingStrict = identityBinding?.strict ?? isSecuredRequestId(requestId);
|
|
45
|
+
const protectedIdentityHeaders = (identityBinding?.protectedHeaders || BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS).map((header)=>header.toLowerCase());
|
|
46
|
+
if (identityBindingEnabled) {
|
|
47
|
+
const derivedIdentityHeaders = {};
|
|
48
|
+
if (isServerTarget) for (const header of protectedIdentityHeaders){
|
|
49
|
+
const incomingHeaderValue = readHeader(incomingHeaders, header);
|
|
50
|
+
if (void 0 !== incomingHeaderValue) writeHeader(derivedIdentityHeaders, header, incomingHeaderValue);
|
|
51
|
+
}
|
|
52
|
+
const customDerivedHeaders = identityBinding?.deriveHeaders?.({
|
|
53
|
+
requestId,
|
|
54
|
+
target: environment.target,
|
|
55
|
+
incomingHeaders: isServerTarget ? {
|
|
56
|
+
...incomingHeaders
|
|
57
|
+
} : {},
|
|
58
|
+
protectedHeaders: [
|
|
59
|
+
...protectedIdentityHeaders
|
|
60
|
+
]
|
|
61
|
+
});
|
|
62
|
+
if (customDerivedHeaders && 'object' == typeof customDerivedHeaders) for (const header of protectedIdentityHeaders){
|
|
63
|
+
const customValue = readHeader(customDerivedHeaders, header);
|
|
64
|
+
if (void 0 !== customValue) writeHeader(derivedIdentityHeaders, header, customValue);
|
|
65
|
+
}
|
|
66
|
+
for (const header of protectedIdentityHeaders){
|
|
67
|
+
const attemptedValue = readHeader(headers, header);
|
|
68
|
+
if (void 0 === attemptedValue) continue;
|
|
69
|
+
const violation = {
|
|
70
|
+
requestId,
|
|
71
|
+
target: environment.target,
|
|
72
|
+
header,
|
|
73
|
+
attemptedValue,
|
|
74
|
+
derivedValue: readHeader(derivedIdentityHeaders, header),
|
|
75
|
+
reason: 'client_override_blocked'
|
|
76
|
+
};
|
|
77
|
+
identityBinding?.onViolation?.(violation);
|
|
78
|
+
if (identityBindingStrict) throw new IdentityBindingViolationError(violation);
|
|
79
|
+
deleteHeader(headers, header);
|
|
80
|
+
}
|
|
81
|
+
Object.keys(derivedIdentityHeaders).forEach((header)=>{
|
|
82
|
+
isServerTarget ? writeHeader(forwardedHeaders, header, derivedIdentityHeaders[header]) : writeHeader(headers, header, derivedIdentityHeaders[header]);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (isServerTarget) {
|
|
86
|
+
const resolveHeaders = realResolveHeaders.get(requestId);
|
|
87
|
+
if (resolveHeaders) {
|
|
88
|
+
const resolvedHeaders = resolveHeaders({
|
|
89
|
+
requestId,
|
|
90
|
+
allowedHeaders: targetAllowedHeaders,
|
|
91
|
+
incomingHeaders: {
|
|
92
|
+
...forwardedHeaders
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
if (resolvedHeaders && 'object' == typeof resolvedHeaders) for (const key of targetAllowedHeaders){
|
|
96
|
+
const resolvedValue = readHeader(resolvedHeaders, key);
|
|
97
|
+
if (void 0 !== resolvedValue) {
|
|
98
|
+
if (identityBindingEnabled && protectedIdentityHeaders.includes(key.toLowerCase())) {
|
|
99
|
+
writeHeader(forwardedHeaders, key.toLowerCase(), resolvedValue);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
writeHeader(forwardedHeaders, key, resolvedValue);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return headers;
|
|
108
|
+
};
|
|
109
|
+
const configure = (options)=>{
|
|
110
|
+
const { request, interceptor, allowedHeaders, resolveHeaders, transport, requireEnvelope, allowCrossOriginEnvelope, identityBinding, operationContract, setDomain, requestId = 'default' } = options;
|
|
111
|
+
const hasExistingDomain = domainMap.has(requestId);
|
|
112
|
+
if ('default' !== requestId && !setDomain && !hasExistingDomain) throw new ProducerDomainNotConfiguredError(requestId);
|
|
113
|
+
let resolvedDomain;
|
|
114
|
+
nativeClient.configure({
|
|
115
|
+
request,
|
|
116
|
+
interceptor,
|
|
117
|
+
allowedHeaders,
|
|
118
|
+
requestId,
|
|
119
|
+
setDomain: setDomain ? (options)=>{
|
|
120
|
+
resolvedDomain = setDomain(options);
|
|
121
|
+
if ('default' !== requestId && isEmptyDomain(resolvedDomain)) throw new ProducerDomainNotConfiguredError(requestId);
|
|
122
|
+
return resolvedDomain;
|
|
123
|
+
} : void 0
|
|
124
|
+
});
|
|
125
|
+
realResolveHeaders.delete(requestId);
|
|
126
|
+
realTransportResilience.delete(requestId);
|
|
127
|
+
realIdentityBinding.delete(requestId);
|
|
128
|
+
realOperationContract.delete(requestId);
|
|
129
|
+
realRequireEnvelope.delete(requestId);
|
|
130
|
+
realAllowCrossOriginEnvelope.delete(requestId);
|
|
131
|
+
if (isServerTarget && 'function' == typeof resolveHeaders) realResolveHeaders.set(requestId, resolveHeaders);
|
|
132
|
+
if (transport && 'object' == typeof transport) realTransportResilience.set(requestId, transport);
|
|
133
|
+
if (identityBinding && 'object' == typeof identityBinding) realIdentityBinding.set(requestId, identityBinding);
|
|
134
|
+
if (operationContract && 'object' == typeof operationContract) realOperationContract.set(requestId, operationContract);
|
|
135
|
+
if ('boolean' == typeof requireEnvelope) realRequireEnvelope.set(requestId, requireEnvelope);
|
|
136
|
+
if ('boolean' == typeof allowCrossOriginEnvelope || 'function' == typeof allowCrossOriginEnvelope) realAllowCrossOriginEnvelope.set(requestId, allowCrossOriginEnvelope);
|
|
137
|
+
if ('string' == typeof resolvedDomain) domainMap.set(requestId, resolvedDomain);
|
|
138
|
+
configuredRequests.set(requestId, true);
|
|
139
|
+
};
|
|
140
|
+
const createHooks = (path, method, requestId, operationContext, upload = false)=>({
|
|
141
|
+
onStart: ({ configuredDomain })=>{
|
|
142
|
+
resolveConfiguredRequest(configuredRequests, requestId, false);
|
|
143
|
+
if (!upload && 'default' !== requestId && isEmptyDomain(configuredDomain)) throw new ProducerDomainNotConfiguredError(requestId);
|
|
144
|
+
},
|
|
145
|
+
prepareHeaders: applyIdentityAndForwardedHeaders,
|
|
146
|
+
dispatch: (context)=>{
|
|
147
|
+
const { incomingHeaders, url, init: { headers } } = context;
|
|
148
|
+
if (isServerTarget && !upload) {
|
|
149
|
+
if (void 0 === readHeader(headers, TRACEPARENT_HEADER)) {
|
|
150
|
+
const incomingTraceparent = firstHeaderValue(readHeader(incomingHeaders, TRACEPARENT_HEADER));
|
|
151
|
+
if ('string' == typeof incomingTraceparent) writeHeader(headers, TRACEPARENT_HEADER, incomingTraceparent);
|
|
152
|
+
}
|
|
153
|
+
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
154
|
+
}
|
|
155
|
+
attachEnvelopeHeaderIfRequired(headers, requestId, url, incomingHeaders);
|
|
156
|
+
attachSecuredOperationHeaders(headers, requestId, method, path, operationContext);
|
|
157
|
+
return executeWithResilience({
|
|
158
|
+
requestId,
|
|
159
|
+
target: environment.target,
|
|
160
|
+
method,
|
|
161
|
+
url,
|
|
162
|
+
init: context.init,
|
|
163
|
+
fetcher: context.fetcher,
|
|
164
|
+
transport: realTransportResilience.get(requestId)
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const createRequest = (...args)=>{
|
|
169
|
+
const options = 'object' == typeof args[0] && null !== args[0] ? args[0] : {
|
|
170
|
+
path: args[0],
|
|
171
|
+
method: args[1],
|
|
172
|
+
port: args[2],
|
|
173
|
+
httpMethodDecider: args[3],
|
|
174
|
+
fetch: args[4],
|
|
175
|
+
requestId: args[5],
|
|
176
|
+
operationContext: args[6]
|
|
177
|
+
};
|
|
178
|
+
const { operationContext, requestId = 'default', ...nativeOptions } = options;
|
|
179
|
+
return nativeClient.createRequest({
|
|
180
|
+
...nativeOptions,
|
|
181
|
+
requestId,
|
|
182
|
+
hooks: createHooks(options.path, options.method, requestId, operationContext)
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
const createUploader = ({ operationContext, requestId = 'default', ...options })=>nativeClient.createUploader({
|
|
186
|
+
...options,
|
|
187
|
+
requestId,
|
|
188
|
+
hooks: createHooks(options.path, 'POST', requestId, operationContext, true)
|
|
189
|
+
});
|
|
190
|
+
return {
|
|
191
|
+
configure,
|
|
192
|
+
createRequest,
|
|
193
|
+
createUploader
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
export { createPolicyClient };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createClient } from "@modern-js/create-request/server";
|
|
3
|
+
import { createPolicyClient } from "./client.mjs";
|
|
4
|
+
import { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, firstHeaderValue, toOrigin } from "./policyCore.mjs";
|
|
5
|
+
export * from "./requestContext.mjs";
|
|
6
|
+
export * from "./types.mjs";
|
|
7
|
+
const resolveSourceOrigin = (headers)=>{
|
|
8
|
+
const origin = toOrigin(firstHeaderValue(headers.origin));
|
|
9
|
+
if (origin) return origin;
|
|
10
|
+
const referer = toOrigin(firstHeaderValue(headers.referer));
|
|
11
|
+
if (referer) return referer;
|
|
12
|
+
const host = firstHeaderValue(headers.host);
|
|
13
|
+
if (!host) return;
|
|
14
|
+
const proto = firstHeaderValue(headers['x-forwarded-proto']) || 'http';
|
|
15
|
+
return `${proto}://${host}`;
|
|
16
|
+
};
|
|
17
|
+
const node_createClient = ()=>createPolicyClient(createClient(), {
|
|
18
|
+
target: 'server',
|
|
19
|
+
resolveSourceOrigin: resolveSourceOrigin
|
|
20
|
+
});
|
|
21
|
+
const { configure: configure, createRequest: createRequest, createUploader: createUploader } = node_createClient();
|
|
22
|
+
export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, configure, createRequest, createUploader, node_createClient as createClient };
|
|
@@ -0,0 +1,172 @@
|
|
|
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 buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
|
|
40
|
+
const routePath = operationContext?.routePath || path;
|
|
41
|
+
const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
|
|
42
|
+
const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
|
|
43
|
+
const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
|
|
44
|
+
const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
|
|
45
|
+
const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
|
|
46
|
+
traceId: operationContext.traceId,
|
|
47
|
+
spanId: operationContext.spanId
|
|
48
|
+
} : parseTraceparentValue(traceparentValue);
|
|
49
|
+
return {
|
|
50
|
+
requestId,
|
|
51
|
+
operationId,
|
|
52
|
+
routePath,
|
|
53
|
+
method: operationMethod,
|
|
54
|
+
...operationContext?.schemaHash ? {
|
|
55
|
+
schemaHash: operationContext.schemaHash
|
|
56
|
+
} : {},
|
|
57
|
+
...'number' == typeof operationContext?.operationVersion ? {
|
|
58
|
+
operationVersion: operationContext.operationVersion
|
|
59
|
+
} : {},
|
|
60
|
+
...traceparentValue ? {
|
|
61
|
+
traceparent: traceparentValue
|
|
62
|
+
} : {},
|
|
63
|
+
...parsedTraceContext ? {
|
|
64
|
+
traceId: parsedTraceContext.traceId,
|
|
65
|
+
spanId: parsedTraceContext.spanId
|
|
66
|
+
} : {}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
class ProducerClientNotInitializedError extends Error {
|
|
70
|
+
constructor(requestId){
|
|
71
|
+
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';
|
|
72
|
+
this.name = 'ProducerClientNotInitializedError';
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
class ProducerDomainNotConfiguredError extends Error {
|
|
76
|
+
constructor(requestId){
|
|
77
|
+
super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
|
|
78
|
+
this.name = 'ProducerDomainNotConfiguredError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class CrossOriginEnvelopePolicyError extends Error {
|
|
82
|
+
constructor(requestId, sourceOrigin, targetOrigin){
|
|
83
|
+
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';
|
|
84
|
+
this.name = 'CrossOriginEnvelopePolicyError';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class IdentityBindingViolationError extends Error {
|
|
88
|
+
constructor(violation){
|
|
89
|
+
super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
|
|
90
|
+
this.name = 'IdentityBindingViolationError';
|
|
91
|
+
this.violation = violation;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
class OperationContractViolationError extends Error {
|
|
95
|
+
constructor(violation){
|
|
96
|
+
super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
|
|
97
|
+
this.name = 'OperationContractViolationError';
|
|
98
|
+
this.violation = violation;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const validateOperationContract = ({ requestId, target, contextPayload, operationContract })=>{
|
|
102
|
+
const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
|
|
103
|
+
if (!operationContractEnabled) return;
|
|
104
|
+
const strict = operationContract?.strict ?? true;
|
|
105
|
+
const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
|
|
106
|
+
const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
|
|
107
|
+
const maybeReportViolation = (reason)=>{
|
|
108
|
+
const violation = {
|
|
109
|
+
requestId,
|
|
110
|
+
target,
|
|
111
|
+
operationId: contextPayload.operationId,
|
|
112
|
+
routePath: contextPayload.routePath,
|
|
113
|
+
method: contextPayload.method,
|
|
114
|
+
schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
|
|
115
|
+
operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
|
|
116
|
+
reason
|
|
117
|
+
};
|
|
118
|
+
operationContract?.onViolation?.(violation);
|
|
119
|
+
if (strict) throw new OperationContractViolationError(violation);
|
|
120
|
+
};
|
|
121
|
+
if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
|
|
122
|
+
if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
|
|
123
|
+
};
|
|
124
|
+
const resolveConfiguredRequest = (configuredRequests, requestId, fallback)=>{
|
|
125
|
+
const configuredRequest = configuredRequests.get(requestId);
|
|
126
|
+
if (configuredRequest) return configuredRequest;
|
|
127
|
+
if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
|
|
128
|
+
return fallback;
|
|
129
|
+
};
|
|
130
|
+
const buildEnvelopeHeaderValue = ({ requestId, target, sourceOrigin, targetOrigin, traceContext, allowCrossOriginEnvelope })=>{
|
|
131
|
+
const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
|
|
132
|
+
if (isCrossOrigin) {
|
|
133
|
+
const isAllowed = 'function' == typeof allowCrossOriginEnvelope ? true === allowCrossOriginEnvelope({
|
|
134
|
+
requestId,
|
|
135
|
+
sourceOrigin,
|
|
136
|
+
targetOrigin,
|
|
137
|
+
target
|
|
138
|
+
}) : true === allowCrossOriginEnvelope;
|
|
139
|
+
if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
|
|
140
|
+
}
|
|
141
|
+
return JSON.stringify({
|
|
142
|
+
requestId,
|
|
143
|
+
target,
|
|
144
|
+
timestamp: Date.now(),
|
|
145
|
+
sourceOrigin,
|
|
146
|
+
targetOrigin,
|
|
147
|
+
...traceContext ? {
|
|
148
|
+
traceId: traceContext.traceId,
|
|
149
|
+
spanId: traceContext.spanId
|
|
150
|
+
} : {}
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
const attachOperationContextHeaders = ({ headers, requestId, target, method, path, operationContext, operationContract, operationContextHeader, operationContextDetailHeader })=>{
|
|
154
|
+
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
155
|
+
const contextPayload = buildOperationContext({
|
|
156
|
+
requestId,
|
|
157
|
+
method,
|
|
158
|
+
path,
|
|
159
|
+
operationContext,
|
|
160
|
+
traceparent: readHeader(headers, TRACEPARENT_HEADER)
|
|
161
|
+
});
|
|
162
|
+
validateOperationContract({
|
|
163
|
+
requestId,
|
|
164
|
+
target,
|
|
165
|
+
contextPayload,
|
|
166
|
+
operationContract
|
|
167
|
+
});
|
|
168
|
+
if (void 0 === readHeader(headers, operationContextHeader)) writeHeader(headers, operationContextHeader, contextPayload.operationId);
|
|
169
|
+
writeHeader(headers, operationContextDetailHeader, JSON.stringify(contextPayload));
|
|
170
|
+
return contextPayload;
|
|
171
|
+
};
|
|
172
|
+
export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, TRACEPARENT_HEADER, attachOperationContextHeaders, buildEnvelopeHeaderValue, buildOperationContext, deleteHeader, findHeaderKey, firstHeaderValue, isEmptyDomain, isSecuredRequestId, parseTraceparentValue, readHeader, resolveConfiguredRequest, toOrigin, validateOperationContract, writeHeader };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { parseTraceparent } from "./traceparent.mjs";
|
|
3
|
+
const BFF_LOCALE_HEADER = 'accept-language';
|
|
4
|
+
const BFF_TRACEPARENT_HEADER = 'traceparent';
|
|
5
|
+
const readHeader = (headers, header)=>{
|
|
6
|
+
if (!headers) return;
|
|
7
|
+
const normalized = header.toLowerCase();
|
|
8
|
+
const key = Object.keys(headers).find((current)=>current.toLowerCase() === normalized);
|
|
9
|
+
if (!key) return;
|
|
10
|
+
const value = headers[key];
|
|
11
|
+
return Array.isArray(value) ? value[0] : value;
|
|
12
|
+
};
|
|
13
|
+
const readString = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
|
|
14
|
+
function createOperationContextSnapshot(operationContext, safeContext) {
|
|
15
|
+
if (!operationContext) return;
|
|
16
|
+
const snapshot = {
|
|
17
|
+
...operationContext,
|
|
18
|
+
...safeContext.locale || operationContext?.locale ? {
|
|
19
|
+
locale: safeContext.locale || operationContext?.locale
|
|
20
|
+
} : {},
|
|
21
|
+
...safeContext.traceparent || operationContext?.traceparent ? {
|
|
22
|
+
traceparent: safeContext.traceparent || operationContext?.traceparent
|
|
23
|
+
} : {},
|
|
24
|
+
...safeContext.traceId || operationContext?.traceId ? {
|
|
25
|
+
traceId: safeContext.traceId || operationContext?.traceId
|
|
26
|
+
} : {},
|
|
27
|
+
...safeContext.spanId || operationContext?.spanId ? {
|
|
28
|
+
spanId: safeContext.spanId || operationContext?.spanId
|
|
29
|
+
} : {}
|
|
30
|
+
};
|
|
31
|
+
return Object.keys(snapshot).some((key)=>void 0 !== snapshot[key]) ? snapshot : void 0;
|
|
32
|
+
}
|
|
33
|
+
function createRequestContextSnapshot(input = {}) {
|
|
34
|
+
const locale = readString(input.locale) || readString(readHeader(input.headers, BFF_LOCALE_HEADER));
|
|
35
|
+
const traceparent = readString(input.traceparent) || readString(input.operationContext?.traceparent) || readString(readHeader(input.headers, BFF_TRACEPARENT_HEADER));
|
|
36
|
+
const parsedTraceparent = input.operationContext?.traceId && input.operationContext?.spanId ? {
|
|
37
|
+
traceId: input.operationContext.traceId,
|
|
38
|
+
spanId: input.operationContext.spanId
|
|
39
|
+
} : parseTraceparent(traceparent);
|
|
40
|
+
const headers = {};
|
|
41
|
+
if (locale) headers[BFF_LOCALE_HEADER] = locale;
|
|
42
|
+
if (traceparent) headers[BFF_TRACEPARENT_HEADER] = traceparent;
|
|
43
|
+
const operationContext = createOperationContextSnapshot(input.operationContext, {
|
|
44
|
+
...locale ? {
|
|
45
|
+
locale
|
|
46
|
+
} : {},
|
|
47
|
+
...traceparent ? {
|
|
48
|
+
traceparent
|
|
49
|
+
} : {},
|
|
50
|
+
...parsedTraceparent ? {
|
|
51
|
+
traceId: parsedTraceparent.traceId,
|
|
52
|
+
spanId: parsedTraceparent.spanId
|
|
53
|
+
} : {}
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
headers,
|
|
57
|
+
...locale ? {
|
|
58
|
+
locale
|
|
59
|
+
} : {},
|
|
60
|
+
...traceparent ? {
|
|
61
|
+
traceparent
|
|
62
|
+
} : {},
|
|
63
|
+
...parsedTraceparent ? {
|
|
64
|
+
traceId: parsedTraceparent.traceId,
|
|
65
|
+
spanId: parsedTraceparent.spanId
|
|
66
|
+
} : {},
|
|
67
|
+
...operationContext ? {
|
|
68
|
+
operationContext
|
|
69
|
+
} : {}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function createRequestContextHeaders(input = {}) {
|
|
73
|
+
return createRequestContextSnapshot(input).headers;
|
|
74
|
+
}
|
|
75
|
+
export { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
|
|
76
|
+
export { BFF_LOCALE_HEADER, BFF_TRACEPARENT_HEADER, createRequestContextHeaders, createRequestContextSnapshot, parseTraceparent };
|
|
@@ -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 };
|