@bleedingdev/modern-js-plugin-bff 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.
Files changed (72) hide show
  1. package/dist/cjs/cli.js +9 -5
  2. package/dist/cjs/constants.js +13 -9
  3. package/dist/cjs/index.js +9 -5
  4. package/dist/cjs/loader.js +9 -5
  5. package/dist/cjs/runtime/create-request/index.js +9 -5
  6. package/dist/cjs/runtime/data-platform/index.js +11 -18
  7. package/dist/cjs/runtime/effect/adapter.js +87 -14
  8. package/dist/cjs/runtime/effect/context.js +9 -5
  9. package/dist/cjs/runtime/effect/edge.js +26 -26
  10. package/dist/cjs/runtime/effect/endpoint-contracts.js +130 -0
  11. package/dist/cjs/runtime/effect/handler.js +107 -63
  12. package/dist/cjs/runtime/effect/index.js +28 -16
  13. package/dist/cjs/runtime/effect/module.js +71 -35
  14. package/dist/cjs/runtime/effect/operation-context.js +10 -18
  15. package/dist/cjs/runtime/effect-client/index.js +10 -6
  16. package/dist/cjs/runtime/effect-client/runtime.js +266 -0
  17. package/dist/cjs/runtime/hono/adapter.js +30 -14
  18. package/dist/cjs/runtime/hono/index.js +9 -5
  19. package/dist/cjs/runtime/hono/operators.js +9 -5
  20. package/dist/cjs/runtime/safe-failure.js +83 -0
  21. package/dist/cjs/server.js +9 -5
  22. package/dist/cjs/utils/clientGenerator.js +13 -9
  23. package/dist/cjs/utils/createHonoRoutes.js +9 -5
  24. package/dist/cjs/utils/crossProjectApiPlugin.js +9 -5
  25. package/dist/cjs/utils/crossProjectServerPolicy.js +104 -0
  26. package/dist/cjs/utils/effectClientGenerator.js +99 -488
  27. package/dist/cjs/utils/pluginGenerator.js +9 -5
  28. package/dist/cjs/utils/runtimeGenerator.js +9 -5
  29. package/dist/esm/runtime/data-platform/index.mjs +2 -13
  30. package/dist/esm/runtime/effect/adapter.mjs +78 -9
  31. package/dist/esm/runtime/effect/edge.mjs +2 -9
  32. package/dist/esm/runtime/effect/endpoint-contracts.mjs +68 -0
  33. package/dist/esm/runtime/effect/handler.mjs +41 -8
  34. package/dist/esm/runtime/effect/index.mjs +2 -0
  35. package/dist/esm/runtime/effect/module.mjs +63 -31
  36. package/dist/esm/runtime/effect/operation-context.mjs +1 -13
  37. package/dist/esm/runtime/effect-client/index.mjs +1 -1
  38. package/dist/esm/runtime/effect-client/runtime.mjs +228 -0
  39. package/dist/esm/runtime/hono/adapter.mjs +21 -9
  40. package/dist/esm/runtime/safe-failure.mjs +45 -0
  41. package/dist/esm/utils/clientGenerator.mjs +5 -5
  42. package/dist/esm/utils/crossProjectServerPolicy.mjs +50 -0
  43. package/dist/esm/utils/effectClientGenerator.mjs +88 -484
  44. package/dist/esm-node/runtime/data-platform/index.mjs +2 -13
  45. package/dist/esm-node/runtime/effect/adapter.mjs +78 -9
  46. package/dist/esm-node/runtime/effect/edge.mjs +2 -9
  47. package/dist/esm-node/runtime/effect/endpoint-contracts.mjs +69 -0
  48. package/dist/esm-node/runtime/effect/handler.mjs +41 -8
  49. package/dist/esm-node/runtime/effect/index.mjs +2 -0
  50. package/dist/esm-node/runtime/effect/module.mjs +63 -31
  51. package/dist/esm-node/runtime/effect/operation-context.mjs +1 -13
  52. package/dist/esm-node/runtime/effect-client/index.mjs +1 -1
  53. package/dist/esm-node/runtime/effect-client/runtime.mjs +229 -0
  54. package/dist/esm-node/runtime/hono/adapter.mjs +21 -9
  55. package/dist/esm-node/runtime/safe-failure.mjs +46 -0
  56. package/dist/esm-node/utils/clientGenerator.mjs +5 -5
  57. package/dist/esm-node/utils/crossProjectServerPolicy.mjs +52 -0
  58. package/dist/esm-node/utils/effectClientGenerator.mjs +88 -484
  59. package/dist/types/runtime/effect/adapter.d.ts +25 -0
  60. package/dist/types/runtime/effect/context.d.ts +1 -1
  61. package/dist/types/runtime/effect/endpoint-contracts.d.ts +62 -0
  62. package/dist/types/runtime/effect/handler.d.ts +37 -4
  63. package/dist/types/runtime/effect/index.d.ts +1 -0
  64. package/dist/types/runtime/effect/module.d.ts +22 -2
  65. package/dist/types/runtime/effect-client/runtime.d.ts +71 -0
  66. package/dist/types/runtime/hono/adapter.d.ts +3 -0
  67. package/dist/types/runtime/safe-failure.d.ts +1 -0
  68. package/dist/types/server.d.ts +1 -1
  69. package/dist/types/utils/createHonoRoutes.d.ts +3 -3
  70. package/dist/types/utils/crossProjectServerPolicy.d.ts +35 -0
  71. package/dist/types/utils/effectClientGenerator.d.ts +16 -2
  72. package/package.json +40 -27
@@ -0,0 +1,229 @@
1
+ import "node:module";
2
+ import { DEFAULT_DATA_BATCH_HEADER, DEFAULT_DATA_ENVELOPE_HEADER, createDataBatchTransport, createRequestEnvelope, encodeRequestEnvelopeHeader } from "../data-platform/index.mjs";
3
+ const METHODS_WITHOUT_BODY = new Set([
4
+ 'GET',
5
+ 'DELETE',
6
+ 'HEAD',
7
+ 'OPTIONS'
8
+ ]);
9
+ const DATA_REQUEST_MODES = new Set([
10
+ 'cache-first',
11
+ 'stale-while-revalidate',
12
+ 'network-only'
13
+ ]);
14
+ const DATA_MUTATION_MODES = new Set([
15
+ 'optimistic',
16
+ 'pessimistic',
17
+ 'fire-and-forget'
18
+ ]);
19
+ const isRecord = (value)=>'object' == typeof value && null !== value;
20
+ const stringOrUndefined = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
21
+ const isDataRequestMode = (value)=>'string' == typeof value && DATA_REQUEST_MODES.has(value);
22
+ const isDataMutationMode = (value)=>'string' == typeof value && DATA_MUTATION_MODES.has(value);
23
+ const normalizeOrigin = (value)=>{
24
+ if ('string' != typeof value || 0 === value.length) return;
25
+ try {
26
+ return new URL(value).origin;
27
+ } catch {
28
+ return;
29
+ }
30
+ };
31
+ const resolveRuntimeFetch = ()=>'function' == typeof fetch ? fetch.bind(globalThis) : void 0;
32
+ const resolveOrigin = (defaultOrigin)=>{
33
+ if ("u" > typeof window && window.location && 'string' == typeof window.location.origin && window.location.origin) return window.location.origin;
34
+ const globalLocation = globalThis?.location;
35
+ if (globalLocation && 'string' == typeof globalLocation.origin && globalLocation.origin) return globalLocation.origin;
36
+ return defaultOrigin;
37
+ };
38
+ const normalizeRequest = (method, request)=>{
39
+ if (!isRecord(request)) return {};
40
+ const payload = {
41
+ ...request
42
+ };
43
+ if (isRecord(request.path) && !isRecord(payload.params)) payload.params = request.path;
44
+ if (isRecord(request.urlParams) && !isRecord(payload.query)) payload.query = request.urlParams;
45
+ if (isRecord(request.headers) && !isRecord(payload.headers)) payload.headers = request.headers;
46
+ if ('payload' in request && void 0 !== request.payload) if ("u" > typeof FormData && request.payload instanceof FormData && !('formData' in payload)) payload.formData = request.payload;
47
+ else if (METHODS_WITHOUT_BODY.has(method)) {
48
+ if (isRecord(request.payload)) payload.query = isRecord(payload.query) ? {
49
+ ...payload.query,
50
+ ...request.payload
51
+ } : request.payload;
52
+ else if (!('body' in payload)) payload.body = request.payload;
53
+ } else if (!isRecord(request.payload) || 'data' in payload) {
54
+ if (!('body' in payload)) payload.body = request.payload;
55
+ } else payload.data = request.payload;
56
+ return payload;
57
+ };
58
+ const resolveTargetOrigin = (dataPlatform, defaultOrigin)=>{
59
+ const explicitTargetOrigin = stringOrUndefined(dataPlatform.targetOrigin) || stringOrUndefined(dataPlatform.endpointOrigin);
60
+ if (explicitTargetOrigin) return explicitTargetOrigin;
61
+ return defaultOrigin;
62
+ };
63
+ const shouldAttachEnvelopeHeader = (dataPlatform, defaultOrigin)=>{
64
+ if (true === dataPlatform.allowCrossOriginEnvelope) return true;
65
+ const currentOrigin = normalizeOrigin(resolveOrigin(defaultOrigin));
66
+ const targetOrigin = normalizeOrigin(resolveTargetOrigin(dataPlatform, defaultOrigin));
67
+ if (!currentOrigin || !targetOrigin) return true;
68
+ return currentOrigin === targetOrigin;
69
+ };
70
+ const toEnvelopeInput = (normalizedRequest)=>{
71
+ const payload = {};
72
+ if (isRecord(normalizedRequest.params)) payload.path = normalizedRequest.params;
73
+ if (isRecord(normalizedRequest.query)) payload.query = normalizedRequest.query;
74
+ if ('data' in normalizedRequest && void 0 !== normalizedRequest.data) payload.data = normalizedRequest.data;
75
+ if ('body' in normalizedRequest && void 0 !== normalizedRequest.body) payload.body = normalizedRequest.body;
76
+ if ("u" > typeof FormData && normalizedRequest.formData instanceof FormData) payload.formData = Array.from(normalizedRequest.formData.entries()).map(([key, value])=>[
77
+ key,
78
+ String(value)
79
+ ]);
80
+ if ("u" > typeof URLSearchParams && normalizedRequest.formUrlencoded instanceof URLSearchParams) payload.formUrlencoded = normalizedRequest.formUrlencoded.toString();
81
+ return payload;
82
+ };
83
+ const createGeneratedEffectClient = (manifest, config, requestRuntime)=>{
84
+ const createRequest = requestRuntime.createRequest;
85
+ const configureRequest = 'function' == typeof requestRuntime.configure ? requestRuntime.configure : void 0;
86
+ const createRequestContextHeaders = 'function' == typeof requestRuntime.createRequestContextHeaders ? requestRuntime.createRequestContextHeaders : void 0;
87
+ const defaultOrigin = config.defaultOrigin;
88
+ const httpMethodDecider = config.httpMethodDecider || 'functionName';
89
+ const port = config.useEnvPort && "u" > typeof process && process.env && process.env.PORT ? process.env.PORT : config.port;
90
+ if (config.requestId && configureRequest) {
91
+ const configurePayload = {
92
+ requestId: config.requestId,
93
+ requireEnvelope: true,
94
+ identityBinding: {
95
+ enabled: true,
96
+ strict: true
97
+ },
98
+ operationContract: {
99
+ enabled: true,
100
+ strict: true,
101
+ requireSchemaHash: true,
102
+ requireOperationVersion: true
103
+ },
104
+ setDomain: ()=>resolveOrigin(defaultOrigin)
105
+ };
106
+ const runtimeFetch = resolveRuntimeFetch();
107
+ if (false !== config.batch.enabled && runtimeFetch) configurePayload.request = createDataBatchTransport({
108
+ fetch: runtimeFetch,
109
+ endpoint: config.batch.endpoint,
110
+ flushIntervalMs: config.batch.flushIntervalMs,
111
+ maxBatchSize: config.batch.maxBatchSize,
112
+ maxBatchBytes: config.batch.maxBatchBytes,
113
+ requestTimeoutMs: config.batch.requestTimeoutMs,
114
+ allowedMethods: config.batch.allowedMethods
115
+ });
116
+ configureRequest(configurePayload);
117
+ }
118
+ const createEffectRequestContext = (requestContext)=>{
119
+ if (!isRecord(requestContext)) return {};
120
+ const headers = createRequestContextHeaders ? createRequestContextHeaders(requestContext) : {};
121
+ return {
122
+ ...requestContext,
123
+ headers
124
+ };
125
+ };
126
+ const applyRequestContext = (normalizedRequest, request)=>{
127
+ if (!isRecord(request) || !isRecord(request.requestContext)) return normalizedRequest;
128
+ const requestContext = createEffectRequestContext(request.requestContext);
129
+ const requestHeaders = isRecord(requestContext.headers) ? requestContext.headers : {};
130
+ if (0 === Object.keys(requestHeaders).length) return normalizedRequest;
131
+ return {
132
+ ...normalizedRequest,
133
+ headers: {
134
+ ...requestHeaders,
135
+ ...isRecord(normalizedRequest.headers) ? normalizedRequest.headers : {}
136
+ }
137
+ };
138
+ };
139
+ const prepareEffectRequest = (endpoint, operation, request)=>{
140
+ const normalizedRequest = applyRequestContext(normalizeRequest(endpoint.method, request), request);
141
+ const dataPlatform = isRecord(request) && isRecord(request.dataPlatform) ? request.dataPlatform : {};
142
+ const strictEnvelope = true === dataPlatform.requireEnvelope || true === dataPlatform.strict;
143
+ if (!strictEnvelope && !shouldAttachEnvelopeHeader(dataPlatform, defaultOrigin)) return normalizedRequest;
144
+ try {
145
+ const namespace = stringOrUndefined(dataPlatform.appNamespace) || config.appNamespace;
146
+ const origin = stringOrUndefined(dataPlatform.origin) || resolveOrigin(defaultOrigin);
147
+ const envelope = createRequestEnvelope({
148
+ operation: {
149
+ ...operation,
150
+ appNamespace: namespace
151
+ },
152
+ scope: {
153
+ appNamespace: namespace,
154
+ origin,
155
+ tenantId: stringOrUndefined(dataPlatform.tenantId),
156
+ userId: stringOrUndefined(dataPlatform.userId),
157
+ sessionId: stringOrUndefined(dataPlatform.sessionId)
158
+ },
159
+ requestInput: {
160
+ method: endpoint.method,
161
+ routePath: endpoint.routePath,
162
+ payload: toEnvelopeInput(normalizedRequest)
163
+ },
164
+ requestMode: isDataRequestMode(dataPlatform.requestMode) ? dataPlatform.requestMode : void 0,
165
+ mutationMode: isDataMutationMode(dataPlatform.mutationMode) ? dataPlatform.mutationMode : void 0,
166
+ selectionPlan: isRecord(dataPlatform.selectionPlan) ? dataPlatform.selectionPlan : void 0,
167
+ traceContext: isRecord(dataPlatform.traceContext) ? dataPlatform.traceContext : void 0,
168
+ requireTraceContext: true === dataPlatform.requireTraceContext
169
+ });
170
+ const headerName = stringOrUndefined(dataPlatform.envelopeHeader) || DEFAULT_DATA_ENVELOPE_HEADER;
171
+ const headers = isRecord(normalizedRequest.headers) ? {
172
+ ...normalizedRequest.headers
173
+ } : {};
174
+ if (false === dataPlatform.batch) headers[DEFAULT_DATA_BATCH_HEADER] = 'off';
175
+ headers[headerName] = encodeRequestEnvelopeHeader(envelope);
176
+ return {
177
+ ...normalizedRequest,
178
+ headers
179
+ };
180
+ } catch (error) {
181
+ if (strictEnvelope) throw error;
182
+ return normalizedRequest;
183
+ }
184
+ };
185
+ const client = {};
186
+ const operationManifest = {};
187
+ for (const endpoint of manifest.endpoints){
188
+ const operationId = `${endpoint.method}:${endpoint.routePath}`;
189
+ const operation = {
190
+ appNamespace: config.appNamespace,
191
+ apiId: endpoint.apiId,
192
+ group: endpoint.group,
193
+ endpoint: endpoint.endpoint,
194
+ operationId,
195
+ routePath: endpoint.routePath,
196
+ method: endpoint.method,
197
+ operationVersion: endpoint.operationVersion,
198
+ schemaHash: endpoint.schemaHash,
199
+ version: endpoint.operationVersion
200
+ };
201
+ const sender = createRequest({
202
+ path: endpoint.routePath,
203
+ method: endpoint.method,
204
+ port,
205
+ operationContext: {
206
+ operationId,
207
+ routePath: endpoint.routePath,
208
+ method: endpoint.method,
209
+ schemaHash: endpoint.schemaHash,
210
+ operationVersion: endpoint.operationVersion
211
+ },
212
+ httpMethodDecider,
213
+ ...config.requestId ? {
214
+ requestId: config.requestId
215
+ } : {}
216
+ });
217
+ const call = (request = {})=>sender(prepareEffectRequest(endpoint, operation, request));
218
+ client[endpoint.group] ??= {};
219
+ client[endpoint.group][endpoint.endpoint] = call;
220
+ operationManifest[endpoint.group] ??= {};
221
+ operationManifest[endpoint.group][endpoint.endpoint] = operation;
222
+ }
223
+ return {
224
+ client,
225
+ operationManifest,
226
+ createEffectRequestContext
227
+ };
228
+ };
229
+ export { createGeneratedEffectClient };
@@ -2,6 +2,8 @@ import "node:module";
2
2
  import { Hono, run } from "@modern-js/server-core";
3
3
  import { isProd, logger } from "@modern-js/utils";
4
4
  import createHonoRoutes from "../../utils/createHonoRoutes.mjs";
5
+ import { checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy } from "../../utils/crossProjectServerPolicy.mjs";
6
+ import { createSafeFailureResponse } from "../safe-failure.mjs";
5
7
  const before = [
6
8
  'custom-server-hook',
7
9
  'custom-server-middleware',
@@ -19,6 +21,7 @@ class HonoAdapter {
19
21
  this.apiMiddleware = [];
20
22
  this.apiServer = null;
21
23
  this.isHono = true;
24
+ this.prefix = '/api';
22
25
  this.setHandlers = async ()=>{
23
26
  if (!this.isHono) return;
24
27
  const { apiHandlerInfos } = this.api.getServerContext();
@@ -31,6 +34,22 @@ class HonoAdapter {
31
34
  order: 'post',
32
35
  before: before
33
36
  }));
37
+ this.crossProjectPolicy = resolveAdapterCrossProjectPolicy(this.api, apiHandlerInfos || []);
38
+ if (this.crossProjectPolicy) {
39
+ const policyMiddleware = async (c, next)=>{
40
+ const denial = checkCrossProjectPolicyResponse(c.req.header(), this.crossProjectPolicy);
41
+ if (denial) return denial;
42
+ await next();
43
+ };
44
+ this.apiMiddleware.unshift({
45
+ name: 'bff-cross-project-policy',
46
+ path: `${this.prefix}/*`,
47
+ method: 'all',
48
+ handler: policyMiddleware,
49
+ order: 'post',
50
+ before: before
51
+ });
52
+ }
34
53
  };
35
54
  this.registerApiRoutes = async ()=>{
36
55
  if (!this.isHono) return;
@@ -67,15 +86,7 @@ class HonoAdapter {
67
86
  } catch (configError) {
68
87
  logger.error(`Error in serverConfig.onError handler: ${configError}`);
69
88
  }
70
- const status = 'object' == typeof err && null !== err && 'status' in err && 'number' == typeof err.status ? err.status : 500;
71
- return new Response(JSON.stringify({
72
- message: err instanceof Error ? err.message : '[BFF] Internal Server Error'
73
- }), {
74
- status,
75
- headers: {
76
- 'content-type': 'application/json; charset=utf-8'
77
- }
78
- });
89
+ return createSafeFailureResponse(err);
79
90
  });
80
91
  };
81
92
  this.registerMiddleware = async (options)=>{
@@ -85,6 +96,7 @@ class HonoAdapter {
85
96
  this.isHono = false;
86
97
  return;
87
98
  }
99
+ this.prefix = prefix || this.prefix;
88
100
  const { middlewares: globalMiddlewares } = this.api.getServerContext();
89
101
  await this.setHandlers();
90
102
  if (isProd()) globalMiddlewares.push(...this.apiMiddleware);
@@ -0,0 +1,46 @@
1
+ import "node:module";
2
+ const SAFE_FAILURE_MESSAGES = {
3
+ 500: 'Internal Server Error',
4
+ 503: 'Service Unavailable'
5
+ };
6
+ const SAFE_FAILURE_CODES = {
7
+ 500: 'INTERNAL_SERVER_ERROR',
8
+ 503: 'SERVICE_UNAVAILABLE'
9
+ };
10
+ const readErrorProperty = (error, key)=>{
11
+ if ('object' != typeof error || null === error || !(key in error)) return;
12
+ return error[key];
13
+ };
14
+ const normalizeFailureStatus = (value)=>{
15
+ if ('number' != typeof value || !Number.isInteger(value)) return;
16
+ return value >= 400 && value <= 599 ? value : void 0;
17
+ };
18
+ const normalizeRetryAfter = (value)=>{
19
+ if ('number' == typeof value && Number.isFinite(value) && value >= 0) return String(Math.ceil(value));
20
+ if ('string' == typeof value) {
21
+ const trimmed = value.trim();
22
+ return trimmed.length > 0 ? trimmed : void 0;
23
+ }
24
+ if (value instanceof Date) return value.toUTCString();
25
+ };
26
+ const createSafeFailureResponse = (error)=>{
27
+ const status = normalizeFailureStatus(readErrorProperty(error, 'status')) ?? normalizeFailureStatus(readErrorProperty(error, 'statusCode')) ?? 500;
28
+ const retryAfter = 503 === status ? normalizeRetryAfter(readErrorProperty(error, 'retryAfter')) ?? normalizeRetryAfter(readErrorProperty(error, 'retryAfterSeconds')) ?? normalizeRetryAfter('number' == typeof readErrorProperty(error, 'retryAfterMs') ? readErrorProperty(error, 'retryAfterMs') / 1000 : void 0) : void 0;
29
+ const body = {
30
+ success: false,
31
+ error: {
32
+ code: SAFE_FAILURE_CODES[status] ?? 'REQUEST_FAILED',
33
+ message: SAFE_FAILURE_MESSAGES[status] ?? 'Request failed',
34
+ status
35
+ }
36
+ };
37
+ const headers = {
38
+ 'content-type': 'application/json; charset=utf-8'
39
+ };
40
+ if (void 0 !== retryAfter) headers['Retry-After'] = retryAfter;
41
+ return new Response(JSON.stringify(body), {
42
+ status,
43
+ headers
44
+ });
45
+ };
46
+ export { createSafeFailureResponse };
@@ -2,7 +2,7 @@ import "node:module";
2
2
  import { generateClient } from "@modern-js/bff-core";
3
3
  import { fs, logger } from "@modern-js/utils";
4
4
  import path from "path";
5
- import { generateEffectClientCode, renderEffectClientDeclaration, resolveEffectEntryFile } from "./effectClientGenerator.mjs";
5
+ import { generateEffectClient, resolveEffectEntryFile } from "./effectClientGenerator.mjs";
6
6
  const API_DIR = 'api';
7
7
  const PLUGIN_DIR = 'plugin';
8
8
  const RUNTIME_DIR = 'runtime';
@@ -263,7 +263,7 @@ async function clientGenerator(draftOptions) {
263
263
  source: effectSource,
264
264
  relativeDistPath: draftOptions.relativeDistPath
265
265
  });
266
- const effectClientCode = await generateEffectClientCode({
266
+ const effectClientArtifacts = await generateEffectClient({
267
267
  appDir: draftOptions.appDir,
268
268
  apiDir: draftOptions.apiDir,
269
269
  resourcePath: effectEntryFile,
@@ -274,10 +274,10 @@ async function clientGenerator(draftOptions) {
274
274
  httpMethodDecider: draftOptions.httpMethodDecider,
275
275
  dataPlatformBatch: draftOptions.effectDataPlatformBatch
276
276
  });
277
- if (effectClientCode) {
277
+ if (effectClientArtifacts) {
278
278
  const targetTypeFile = effectFileDetails.targetDir.replace(/\.js$/, '.d.ts');
279
- await writeTargetFile(effectFileDetails.absTargetDir, effectClientCode);
280
- await writeTargetFile(path.resolve(targetTypeFile), renderEffectClientDeclaration());
279
+ await writeTargetFile(effectFileDetails.absTargetDir, effectClientArtifacts.code);
280
+ await writeTargetFile(path.resolve(targetTypeFile), effectClientArtifacts.declaration);
281
281
  generatedSourceList.push(effectFileDetails);
282
282
  }
283
283
  }
@@ -0,0 +1,52 @@
1
+ import __rslib_shim_module__ from "node:module";
2
+ const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
+ import { checkCrossProjectPolicy, deriveOperationVersion, resolveCrossProjectPolicy } from "@modern-js/bff-core";
4
+ import path from "path";
5
+ const readNearestPackageVersion = (startDir)=>{
6
+ if (!startDir) return;
7
+ let current = path.resolve(startDir);
8
+ for(let depth = 0; depth < 10; depth += 1){
9
+ try {
10
+ const packageJson = require(path.join(current, 'package.json'));
11
+ if ('string' == typeof packageJson.version) return packageJson.version;
12
+ } catch {}
13
+ const parent = path.dirname(current);
14
+ if (parent === current) break;
15
+ current = parent;
16
+ }
17
+ };
18
+ const resolveAdapterCrossProjectPolicy = (api, handlers)=>{
19
+ const bff = api.getServerConfig()?.bff;
20
+ const { apiDirectory, appDirectory } = api.getServerContext();
21
+ return resolveCrossProjectPolicy({
22
+ crossProjectPolicy: bff?.crossProjectPolicy,
23
+ handlers,
24
+ requestId: bff?.requestId,
25
+ isCrossProjectServer: bff?.isCrossProjectServer,
26
+ operationVersion: deriveOperationVersion(readNearestPackageVersion(apiDirectory) ?? readNearestPackageVersion(appDirectory))
27
+ });
28
+ };
29
+ const DENIAL_HEADERS = {
30
+ 'content-type': 'application/json; charset=utf-8'
31
+ };
32
+ const checkCrossProjectPolicyResponse = (headers, policy)=>{
33
+ if (!policy?.enabled) return null;
34
+ const denial = checkCrossProjectPolicy(headers, policy);
35
+ if (!denial) return null;
36
+ return new Response(JSON.stringify(denial.body), {
37
+ status: denial.status,
38
+ headers: DENIAL_HEADERS
39
+ });
40
+ };
41
+ const toHeaderRecord = (headers)=>{
42
+ const record = {};
43
+ headers.forEach((value, key)=>{
44
+ record[key] = value;
45
+ });
46
+ return record;
47
+ };
48
+ const checkCrossProjectPolicyForRequest = (request, policy)=>{
49
+ if (!policy?.enabled) return null;
50
+ return checkCrossProjectPolicyResponse(toHeaderRecord(request.headers), policy);
51
+ };
52
+ export { checkCrossProjectPolicyForRequest, checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy };