@bleedingdev/modern-js-plugin-bff 3.2.0-ultramodern.120 → 3.2.0-ultramodern.122
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/runtime/data-platform/index.js +2 -13
- package/dist/cjs/runtime/effect/adapter.js +78 -9
- package/dist/cjs/runtime/effect/edge.js +13 -17
- package/dist/cjs/runtime/effect/endpoint-contracts.js +130 -0
- package/dist/cjs/runtime/effect/handler.js +50 -5
- package/dist/cjs/runtime/effect/module.js +16 -7
- package/dist/cjs/runtime/effect/operation-context.js +1 -13
- package/dist/cjs/runtime/effect-client/runtime.js +266 -0
- package/dist/cjs/runtime/hono/adapter.js +21 -9
- package/dist/cjs/runtime/safe-failure.js +83 -0
- package/dist/cjs/utils/clientGenerator.js +4 -4
- package/dist/cjs/utils/crossProjectServerPolicy.js +104 -0
- package/dist/cjs/utils/effectClientGenerator.js +90 -483
- package/dist/esm/runtime/data-platform/index.mjs +2 -13
- package/dist/esm/runtime/effect/adapter.mjs +78 -9
- package/dist/esm/runtime/effect/edge.mjs +2 -9
- package/dist/esm/runtime/effect/endpoint-contracts.mjs +68 -0
- package/dist/esm/runtime/effect/handler.mjs +36 -4
- package/dist/esm/runtime/effect/module.mjs +17 -8
- package/dist/esm/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm/runtime/effect-client/runtime.mjs +228 -0
- package/dist/esm/runtime/hono/adapter.mjs +21 -9
- package/dist/esm/runtime/safe-failure.mjs +45 -0
- package/dist/esm/utils/clientGenerator.mjs +5 -5
- package/dist/esm/utils/crossProjectServerPolicy.mjs +50 -0
- package/dist/esm/utils/effectClientGenerator.mjs +88 -484
- package/dist/esm-node/runtime/data-platform/index.mjs +2 -13
- package/dist/esm-node/runtime/effect/adapter.mjs +78 -9
- package/dist/esm-node/runtime/effect/edge.mjs +2 -9
- package/dist/esm-node/runtime/effect/endpoint-contracts.mjs +69 -0
- package/dist/esm-node/runtime/effect/handler.mjs +36 -4
- package/dist/esm-node/runtime/effect/module.mjs +17 -8
- package/dist/esm-node/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm-node/runtime/effect-client/runtime.mjs +229 -0
- package/dist/esm-node/runtime/hono/adapter.mjs +21 -9
- package/dist/esm-node/runtime/safe-failure.mjs +46 -0
- package/dist/esm-node/utils/clientGenerator.mjs +5 -5
- package/dist/esm-node/utils/crossProjectServerPolicy.mjs +52 -0
- package/dist/esm-node/utils/effectClientGenerator.mjs +88 -484
- package/dist/types/runtime/effect/adapter.d.ts +25 -0
- package/dist/types/runtime/effect/endpoint-contracts.d.ts +62 -0
- package/dist/types/runtime/effect/handler.d.ts +30 -0
- package/dist/types/runtime/effect/module.d.ts +21 -1
- package/dist/types/runtime/effect-client/runtime.d.ts +71 -0
- package/dist/types/runtime/hono/adapter.d.ts +3 -0
- package/dist/types/runtime/safe-failure.d.ts +1 -0
- package/dist/types/utils/crossProjectServerPolicy.d.ts +35 -0
- package/dist/types/utils/effectClientGenerator.d.ts +15 -1
- package/package.json +24 -12
|
@@ -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
|
-
|
|
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 {
|
|
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
|
|
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 (
|
|
277
|
+
if (effectClientArtifacts) {
|
|
278
278
|
const targetTypeFile = effectFileDetails.targetDir.replace(/\.js$/, '.d.ts');
|
|
279
|
-
await writeTargetFile(effectFileDetails.absTargetDir,
|
|
280
|
-
await writeTargetFile(path.resolve(targetTypeFile),
|
|
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 };
|