@bleedingdev/modern-js-plugin-bff 3.2.0-ultramodern.120 → 3.2.0-ultramodern.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/cjs/runtime/data-platform/index.js +2 -13
  2. package/dist/cjs/runtime/effect/adapter.js +78 -9
  3. package/dist/cjs/runtime/effect/edge.js +13 -17
  4. package/dist/cjs/runtime/effect/endpoint-contracts.js +130 -0
  5. package/dist/cjs/runtime/effect/handler.js +50 -5
  6. package/dist/cjs/runtime/effect/module.js +16 -7
  7. package/dist/cjs/runtime/effect/operation-context.js +1 -13
  8. package/dist/cjs/runtime/effect-client/runtime.js +266 -0
  9. package/dist/cjs/runtime/hono/adapter.js +21 -9
  10. package/dist/cjs/runtime/safe-failure.js +83 -0
  11. package/dist/cjs/utils/clientGenerator.js +4 -4
  12. package/dist/cjs/utils/crossProjectServerPolicy.js +104 -0
  13. package/dist/cjs/utils/effectClientGenerator.js +90 -483
  14. package/dist/esm/runtime/data-platform/index.mjs +2 -13
  15. package/dist/esm/runtime/effect/adapter.mjs +78 -9
  16. package/dist/esm/runtime/effect/edge.mjs +2 -9
  17. package/dist/esm/runtime/effect/endpoint-contracts.mjs +68 -0
  18. package/dist/esm/runtime/effect/handler.mjs +36 -4
  19. package/dist/esm/runtime/effect/module.mjs +17 -8
  20. package/dist/esm/runtime/effect/operation-context.mjs +1 -13
  21. package/dist/esm/runtime/effect-client/runtime.mjs +228 -0
  22. package/dist/esm/runtime/hono/adapter.mjs +21 -9
  23. package/dist/esm/runtime/safe-failure.mjs +45 -0
  24. package/dist/esm/utils/clientGenerator.mjs +5 -5
  25. package/dist/esm/utils/crossProjectServerPolicy.mjs +50 -0
  26. package/dist/esm/utils/effectClientGenerator.mjs +88 -484
  27. package/dist/esm-node/runtime/data-platform/index.mjs +2 -13
  28. package/dist/esm-node/runtime/effect/adapter.mjs +78 -9
  29. package/dist/esm-node/runtime/effect/edge.mjs +2 -9
  30. package/dist/esm-node/runtime/effect/endpoint-contracts.mjs +69 -0
  31. package/dist/esm-node/runtime/effect/handler.mjs +36 -4
  32. package/dist/esm-node/runtime/effect/module.mjs +17 -8
  33. package/dist/esm-node/runtime/effect/operation-context.mjs +1 -13
  34. package/dist/esm-node/runtime/effect-client/runtime.mjs +229 -0
  35. package/dist/esm-node/runtime/hono/adapter.mjs +21 -9
  36. package/dist/esm-node/runtime/safe-failure.mjs +46 -0
  37. package/dist/esm-node/utils/clientGenerator.mjs +5 -5
  38. package/dist/esm-node/utils/crossProjectServerPolicy.mjs +52 -0
  39. package/dist/esm-node/utils/effectClientGenerator.mjs +88 -484
  40. package/dist/types/runtime/effect/adapter.d.ts +25 -0
  41. package/dist/types/runtime/effect/endpoint-contracts.d.ts +62 -0
  42. package/dist/types/runtime/effect/handler.d.ts +30 -0
  43. package/dist/types/runtime/effect/module.d.ts +21 -1
  44. package/dist/types/runtime/effect-client/runtime.d.ts +71 -0
  45. package/dist/types/runtime/hono/adapter.d.ts +3 -0
  46. package/dist/types/runtime/safe-failure.d.ts +1 -0
  47. package/dist/types/utils/crossProjectServerPolicy.d.ts +35 -0
  48. package/dist/types/utils/effectClientGenerator.d.ts +15 -1
  49. package/package.json +24 -12
@@ -1,6 +1,8 @@
1
1
  import { Hono, run } from "@modern-js/server-core";
2
2
  import { isProd, logger } from "@modern-js/utils";
3
3
  import createHonoRoutes from "../../utils/createHonoRoutes.mjs";
4
+ import { checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy } from "../../utils/crossProjectServerPolicy.mjs";
5
+ import { createSafeFailureResponse } from "../safe-failure.mjs";
4
6
  const before = [
5
7
  'custom-server-hook',
6
8
  'custom-server-middleware',
@@ -18,6 +20,7 @@ class HonoAdapter {
18
20
  this.apiMiddleware = [];
19
21
  this.apiServer = null;
20
22
  this.isHono = true;
23
+ this.prefix = '/api';
21
24
  this.setHandlers = async ()=>{
22
25
  if (!this.isHono) return;
23
26
  const { apiHandlerInfos } = this.api.getServerContext();
@@ -30,6 +33,22 @@ class HonoAdapter {
30
33
  order: 'post',
31
34
  before: before
32
35
  }));
36
+ this.crossProjectPolicy = resolveAdapterCrossProjectPolicy(this.api, apiHandlerInfos || []);
37
+ if (this.crossProjectPolicy) {
38
+ const policyMiddleware = async (c, next)=>{
39
+ const denial = checkCrossProjectPolicyResponse(c.req.header(), this.crossProjectPolicy);
40
+ if (denial) return denial;
41
+ await next();
42
+ };
43
+ this.apiMiddleware.unshift({
44
+ name: 'bff-cross-project-policy',
45
+ path: `${this.prefix}/*`,
46
+ method: 'all',
47
+ handler: policyMiddleware,
48
+ order: 'post',
49
+ before: before
50
+ });
51
+ }
33
52
  };
34
53
  this.registerApiRoutes = async ()=>{
35
54
  if (!this.isHono) return;
@@ -66,15 +85,7 @@ class HonoAdapter {
66
85
  } catch (configError) {
67
86
  logger.error(`Error in serverConfig.onError handler: ${configError}`);
68
87
  }
69
- const status = 'object' == typeof err && null !== err && 'status' in err && 'number' == typeof err.status ? err.status : 500;
70
- return new Response(JSON.stringify({
71
- message: err instanceof Error ? err.message : '[BFF] Internal Server Error'
72
- }), {
73
- status,
74
- headers: {
75
- 'content-type': 'application/json; charset=utf-8'
76
- }
77
- });
88
+ return createSafeFailureResponse(err);
78
89
  });
79
90
  };
80
91
  this.registerMiddleware = async (options)=>{
@@ -84,6 +95,7 @@ class HonoAdapter {
84
95
  this.isHono = false;
85
96
  return;
86
97
  }
98
+ this.prefix = prefix || this.prefix;
87
99
  const { middlewares: globalMiddlewares } = this.api.getServerContext();
88
100
  await this.setHandlers();
89
101
  if (isProd()) globalMiddlewares.push(...this.apiMiddleware);
@@ -0,0 +1,45 @@
1
+ const SAFE_FAILURE_MESSAGES = {
2
+ 500: 'Internal Server Error',
3
+ 503: 'Service Unavailable'
4
+ };
5
+ const SAFE_FAILURE_CODES = {
6
+ 500: 'INTERNAL_SERVER_ERROR',
7
+ 503: 'SERVICE_UNAVAILABLE'
8
+ };
9
+ const readErrorProperty = (error, key)=>{
10
+ if ('object' != typeof error || null === error || !(key in error)) return;
11
+ return error[key];
12
+ };
13
+ const normalizeFailureStatus = (value)=>{
14
+ if ('number' != typeof value || !Number.isInteger(value)) return;
15
+ return value >= 400 && value <= 599 ? value : void 0;
16
+ };
17
+ const normalizeRetryAfter = (value)=>{
18
+ if ('number' == typeof value && Number.isFinite(value) && value >= 0) return String(Math.ceil(value));
19
+ if ('string' == typeof value) {
20
+ const trimmed = value.trim();
21
+ return trimmed.length > 0 ? trimmed : void 0;
22
+ }
23
+ if (value instanceof Date) return value.toUTCString();
24
+ };
25
+ const createSafeFailureResponse = (error)=>{
26
+ const status = normalizeFailureStatus(readErrorProperty(error, 'status')) ?? normalizeFailureStatus(readErrorProperty(error, 'statusCode')) ?? 500;
27
+ 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;
28
+ const body = {
29
+ success: false,
30
+ error: {
31
+ code: SAFE_FAILURE_CODES[status] ?? 'REQUEST_FAILED',
32
+ message: SAFE_FAILURE_MESSAGES[status] ?? 'Request failed',
33
+ status
34
+ }
35
+ };
36
+ const headers = {
37
+ 'content-type': 'application/json; charset=utf-8'
38
+ };
39
+ if (void 0 !== retryAfter) headers['Retry-After'] = retryAfter;
40
+ return new Response(JSON.stringify(body), {
41
+ status,
42
+ headers
43
+ });
44
+ };
45
+ export { createSafeFailureResponse };
@@ -1,7 +1,7 @@
1
1
  import { generateClient } from "@modern-js/bff-core";
2
2
  import { fs, logger } from "@modern-js/utils";
3
3
  import path from "path";
4
- import { generateEffectClientCode, renderEffectClientDeclaration, resolveEffectEntryFile } from "./effectClientGenerator.mjs";
4
+ import { generateEffectClient, resolveEffectEntryFile } from "./effectClientGenerator.mjs";
5
5
  const API_DIR = 'api';
6
6
  const PLUGIN_DIR = 'plugin';
7
7
  const RUNTIME_DIR = 'runtime';
@@ -262,7 +262,7 @@ async function clientGenerator(draftOptions) {
262
262
  source: effectSource,
263
263
  relativeDistPath: draftOptions.relativeDistPath
264
264
  });
265
- const effectClientCode = await generateEffectClientCode({
265
+ const effectClientArtifacts = await generateEffectClient({
266
266
  appDir: draftOptions.appDir,
267
267
  apiDir: draftOptions.apiDir,
268
268
  resourcePath: effectEntryFile,
@@ -273,10 +273,10 @@ async function clientGenerator(draftOptions) {
273
273
  httpMethodDecider: draftOptions.httpMethodDecider,
274
274
  dataPlatformBatch: draftOptions.effectDataPlatformBatch
275
275
  });
276
- if (effectClientCode) {
276
+ if (effectClientArtifacts) {
277
277
  const targetTypeFile = effectFileDetails.targetDir.replace(/\.js$/, '.d.ts');
278
- await writeTargetFile(effectFileDetails.absTargetDir, effectClientCode);
279
- await writeTargetFile(path.resolve(targetTypeFile), renderEffectClientDeclaration());
278
+ await writeTargetFile(effectFileDetails.absTargetDir, effectClientArtifacts.code);
279
+ await writeTargetFile(path.resolve(targetTypeFile), effectClientArtifacts.declaration);
280
280
  generatedSourceList.push(effectFileDetails);
281
281
  }
282
282
  }
@@ -0,0 +1,50 @@
1
+ import { checkCrossProjectPolicy, deriveOperationVersion, resolveCrossProjectPolicy } from "@modern-js/bff-core";
2
+ import path from "path";
3
+ const readNearestPackageVersion = (startDir)=>{
4
+ if (!startDir) return;
5
+ let current = path.resolve(startDir);
6
+ for(let depth = 0; depth < 10; depth += 1){
7
+ try {
8
+ const packageJson = require(path.join(current, 'package.json'));
9
+ if ('string' == typeof packageJson.version) return packageJson.version;
10
+ } catch {}
11
+ const parent = path.dirname(current);
12
+ if (parent === current) break;
13
+ current = parent;
14
+ }
15
+ };
16
+ const resolveAdapterCrossProjectPolicy = (api, handlers)=>{
17
+ const bff = api.getServerConfig()?.bff;
18
+ const { apiDirectory, appDirectory } = api.getServerContext();
19
+ return resolveCrossProjectPolicy({
20
+ crossProjectPolicy: bff?.crossProjectPolicy,
21
+ handlers,
22
+ requestId: bff?.requestId,
23
+ isCrossProjectServer: bff?.isCrossProjectServer,
24
+ operationVersion: deriveOperationVersion(readNearestPackageVersion(apiDirectory) ?? readNearestPackageVersion(appDirectory))
25
+ });
26
+ };
27
+ const DENIAL_HEADERS = {
28
+ 'content-type': 'application/json; charset=utf-8'
29
+ };
30
+ const checkCrossProjectPolicyResponse = (headers, policy)=>{
31
+ if (!policy?.enabled) return null;
32
+ const denial = checkCrossProjectPolicy(headers, policy);
33
+ if (!denial) return null;
34
+ return new Response(JSON.stringify(denial.body), {
35
+ status: denial.status,
36
+ headers: DENIAL_HEADERS
37
+ });
38
+ };
39
+ const toHeaderRecord = (headers)=>{
40
+ const record = {};
41
+ headers.forEach((value, key)=>{
42
+ record[key] = value;
43
+ });
44
+ return record;
45
+ };
46
+ const checkCrossProjectPolicyForRequest = (request, policy)=>{
47
+ if (!policy?.enabled) return null;
48
+ return checkCrossProjectPolicyResponse(toHeaderRecord(request.headers), policy);
49
+ };
50
+ export { checkCrossProjectPolicyForRequest, checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy };