@bleedingdev/modern-js-plugin-bff 3.5.0-ultramodern.4 → 3.5.0-ultramodern.40
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/cli/compress.js +52 -0
- package/dist/cjs/cli/generator.js +149 -0
- package/dist/cjs/cli/prefix.js +50 -0
- package/dist/cjs/cli/watch.js +51 -0
- package/dist/cjs/cli.js +11 -130
- package/dist/cjs/runtime/data-platform/batch/queue.js +284 -0
- package/dist/cjs/runtime/data-platform/batch/request.js +98 -0
- package/dist/cjs/runtime/data-platform/batch/response.js +64 -0
- package/dist/cjs/runtime/data-platform/batch/telemetry.js +67 -0
- package/dist/cjs/runtime/data-platform/batch/url.js +69 -0
- package/dist/cjs/runtime/data-platform/batch-transport.js +59 -0
- package/dist/cjs/runtime/data-platform/codec.js +155 -0
- package/dist/cjs/runtime/data-platform/index.js +30 -633
- package/dist/cjs/runtime/data-platform/invalidation.js +61 -0
- package/dist/cjs/runtime/data-platform/trace.js +73 -0
- package/dist/cjs/runtime/data-platform/types.js +48 -0
- package/dist/cjs/runtime/data-platform/validation.js +178 -0
- package/dist/cjs/runtime/effect/adapter/constants.js +44 -0
- package/dist/cjs/runtime/effect/adapter/cross-project-policy.js +61 -0
- package/dist/cjs/runtime/effect/adapter/entry.js +77 -0
- package/dist/cjs/runtime/effect/adapter/error-response.js +79 -0
- package/dist/cjs/runtime/effect/adapter/handler.js +53 -0
- package/dist/cjs/runtime/effect/adapter.js +20 -154
- package/dist/cjs/runtime/effect/backend-federation/entry.js +99 -0
- package/dist/cjs/runtime/effect/backend-federation/load.js +57 -0
- package/dist/cjs/runtime/effect/backend-federation/plugin.js +53 -0
- package/dist/cjs/runtime/effect/backend-federation/remotes.js +50 -0
- package/dist/cjs/runtime/effect/backend-federation/runtime.js +64 -0
- package/dist/cjs/runtime/effect/backend-federation/types.js +18 -0
- package/dist/cjs/runtime/effect/backend-federation/utils.js +48 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/errors.js +83 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/index.js +55 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/load.js +74 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/metadata.js +101 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/reference.js +117 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/remote.js +68 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/types.js +18 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest/validation.js +108 -0
- package/dist/cjs/runtime/effect/backend-federation-manifest.js +62 -0
- package/dist/cjs/runtime/effect/backend-federation.js +61 -0
- package/dist/cjs/runtime/effect/dispatch.js +101 -0
- package/dist/cjs/runtime/effect/edge.js +58 -69
- package/dist/cjs/runtime/effect/endpoint-contracts.js +7 -21
- package/dist/cjs/runtime/effect/entry-shape.js +84 -0
- package/dist/cjs/runtime/effect/handler/batch-handler.js +161 -0
- package/dist/cjs/runtime/effect/handler/batch.js +195 -0
- package/dist/cjs/runtime/effect/handler/definition.js +101 -0
- package/dist/cjs/runtime/effect/handler/envelope.js +104 -0
- package/dist/cjs/runtime/effect/handler/http.js +96 -0
- package/dist/cjs/runtime/effect/handler/openapi.js +57 -0
- package/dist/cjs/runtime/effect/handler/routing.js +100 -0
- package/dist/cjs/runtime/effect/handler/rpc.js +63 -0
- package/dist/cjs/runtime/effect/handler/types.js +43 -0
- package/dist/cjs/runtime/effect/handler.js +37 -529
- package/dist/cjs/runtime/effect/index.js +91 -15
- package/dist/cjs/runtime/effect/module.js +23 -80
- package/dist/cjs/runtime/effect/rpcSerialization.js +54 -0
- package/dist/cjs/runtime/effect-client/batch.js +73 -0
- package/dist/cjs/runtime/effect-client/envelope.js +145 -0
- package/dist/cjs/runtime/effect-client/guards.js +44 -0
- package/dist/cjs/runtime/effect-client/index.js +6 -16
- package/dist/cjs/runtime/effect-client/request-context.js +64 -0
- package/dist/cjs/runtime/effect-client/request.js +66 -0
- package/dist/cjs/runtime/effect-client/runtime.js +12 -178
- package/dist/cjs/runtime/effect-client/types.js +18 -0
- package/dist/cjs/runtime/safe-failure.js +2 -1
- package/dist/cjs/utils/client-generator/files.js +152 -0
- package/dist/cjs/utils/client-generator/generate.js +138 -0
- package/dist/cjs/utils/client-generator/index.js +83 -0
- package/dist/cjs/utils/client-generator/package-json.js +132 -0
- package/dist/cjs/utils/client-generator/write-package.js +125 -0
- package/dist/cjs/utils/clientGenerator.js +26 -303
- package/dist/cjs/utils/effect-client-generator/generator.js +64 -0
- package/dist/cjs/utils/effect-client-generator/index.js +51 -0
- package/dist/cjs/utils/effect-client-generator/paths.js +73 -0
- package/dist/cjs/utils/effect-client-generator/rendering.js +231 -0
- package/dist/cjs/utils/effect-client-generator/runtime.js +84 -0
- package/dist/cjs/utils/effect-client-generator/types.js +18 -0
- package/dist/cjs/utils/effectClientGenerator.js +5 -262
- package/dist/esm/cli/compress.mjs +14 -0
- package/dist/esm/cli/generator.mjs +98 -0
- package/dist/esm/cli/prefix.mjs +9 -0
- package/dist/esm/cli/watch.mjs +13 -0
- package/dist/esm/cli.mjs +7 -121
- package/dist/esm/runtime/data-platform/batch/queue.mjs +246 -0
- package/dist/esm/runtime/data-platform/batch/request.mjs +45 -0
- package/dist/esm/runtime/data-platform/batch/response.mjs +23 -0
- package/dist/esm/runtime/data-platform/batch/telemetry.mjs +23 -0
- package/dist/esm/runtime/data-platform/batch/url.mjs +25 -0
- package/dist/esm/runtime/data-platform/batch-transport.mjs +12 -0
- package/dist/esm/runtime/data-platform/codec.mjs +90 -0
- package/dist/esm/runtime/data-platform/index.mjs +6 -610
- package/dist/esm/runtime/data-platform/invalidation.mjs +20 -0
- package/dist/esm/runtime/data-platform/trace.mjs +29 -0
- package/dist/esm/runtime/data-platform/types.mjs +4 -0
- package/dist/esm/runtime/data-platform/validation.mjs +128 -0
- package/dist/esm/runtime/effect/adapter/constants.mjs +6 -0
- package/dist/esm/runtime/effect/adapter/cross-project-policy.mjs +23 -0
- package/dist/esm/runtime/effect/adapter/entry.mjs +29 -0
- package/dist/esm/runtime/effect/adapter/error-response.mjs +41 -0
- package/dist/esm/runtime/effect/adapter/handler.mjs +15 -0
- package/dist/esm/runtime/effect/adapter.mjs +21 -145
- package/dist/esm/runtime/effect/backend-federation/entry.mjs +61 -0
- package/dist/esm/runtime/effect/backend-federation/load.mjs +19 -0
- package/dist/esm/runtime/effect/backend-federation/plugin.mjs +15 -0
- package/dist/esm/runtime/effect/backend-federation/remotes.mjs +12 -0
- package/dist/esm/runtime/effect/backend-federation/runtime.mjs +26 -0
- package/dist/esm/runtime/effect/backend-federation/types.mjs +0 -0
- package/dist/esm/runtime/effect/backend-federation/utils.mjs +7 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/errors.mjs +32 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/index.mjs +4 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/load.mjs +36 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/metadata.mjs +42 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/reference.mjs +76 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/remote.mjs +30 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/types.mjs +0 -0
- package/dist/esm/runtime/effect/backend-federation-manifest/validation.mjs +64 -0
- package/dist/esm/runtime/effect/backend-federation-manifest.mjs +1 -0
- package/dist/esm/runtime/effect/backend-federation.mjs +4 -0
- package/dist/esm/runtime/effect/dispatch.mjs +63 -0
- package/dist/esm/runtime/effect/edge.mjs +17 -63
- package/dist/esm/runtime/effect/endpoint-contracts.mjs +8 -13
- package/dist/esm/runtime/effect/entry-shape.mjs +36 -0
- package/dist/esm/runtime/effect/handler/batch-handler.mjs +123 -0
- package/dist/esm/runtime/effect/handler/batch.mjs +124 -0
- package/dist/esm/runtime/effect/handler/definition.mjs +60 -0
- package/dist/esm/runtime/effect/handler/envelope.mjs +63 -0
- package/dist/esm/runtime/effect/handler/http.mjs +58 -0
- package/dist/esm/runtime/effect/handler/openapi.mjs +19 -0
- package/dist/esm/runtime/effect/handler/routing.mjs +50 -0
- package/dist/esm/runtime/effect/handler/rpc.mjs +22 -0
- package/dist/esm/runtime/effect/handler/types.mjs +5 -0
- package/dist/esm/runtime/effect/handler.mjs +11 -506
- package/dist/esm/runtime/effect/index.mjs +5 -2
- package/dist/esm/runtime/effect/module.mjs +25 -82
- package/dist/esm/runtime/effect/rpcSerialization.mjs +16 -0
- package/dist/esm/runtime/effect-client/batch.mjs +35 -0
- package/dist/esm/runtime/effect-client/envelope.mjs +104 -0
- package/dist/esm/runtime/effect-client/guards.mjs +3 -0
- package/dist/esm/runtime/effect-client/index.mjs +27 -37
- package/dist/esm/runtime/effect-client/request-context.mjs +23 -0
- package/dist/esm/runtime/effect-client/request.mjs +28 -0
- package/dist/esm/runtime/effect-client/runtime.mjs +12 -178
- package/dist/esm/runtime/effect-client/types.mjs +0 -0
- package/dist/esm/runtime/safe-failure.mjs +2 -1
- package/dist/esm/utils/client-generator/files.mjs +68 -0
- package/dist/esm/utils/client-generator/generate.mjs +88 -0
- package/dist/esm/utils/client-generator/index.mjs +4 -0
- package/dist/esm/utils/client-generator/package-json.mjs +78 -0
- package/dist/esm/utils/client-generator/write-package.mjs +74 -0
- package/dist/esm/utils/clientGenerator.mjs +2 -293
- package/dist/esm/utils/effect-client-generator/generator.mjs +23 -0
- package/dist/esm/utils/effect-client-generator/index.mjs +3 -0
- package/dist/esm/utils/effect-client-generator/paths.mjs +25 -0
- package/dist/esm/utils/effect-client-generator/rendering.mjs +180 -0
- package/dist/esm/utils/effect-client-generator/runtime.mjs +33 -0
- package/dist/esm/utils/effect-client-generator/types.mjs +0 -0
- package/dist/esm/utils/effectClientGenerator.mjs +1 -249
- package/dist/esm-node/cli/compress.mjs +15 -0
- package/dist/esm-node/cli/generator.mjs +99 -0
- package/dist/esm-node/cli/prefix.mjs +10 -0
- package/dist/esm-node/cli/watch.mjs +14 -0
- package/dist/esm-node/cli.mjs +7 -121
- package/dist/esm-node/runtime/data-platform/batch/queue.mjs +247 -0
- package/dist/esm-node/runtime/data-platform/batch/request.mjs +46 -0
- package/dist/esm-node/runtime/data-platform/batch/response.mjs +24 -0
- package/dist/esm-node/runtime/data-platform/batch/telemetry.mjs +24 -0
- package/dist/esm-node/runtime/data-platform/batch/url.mjs +26 -0
- package/dist/esm-node/runtime/data-platform/batch-transport.mjs +13 -0
- package/dist/esm-node/runtime/data-platform/codec.mjs +91 -0
- package/dist/esm-node/runtime/data-platform/index.mjs +6 -610
- package/dist/esm-node/runtime/data-platform/invalidation.mjs +21 -0
- package/dist/esm-node/runtime/data-platform/trace.mjs +30 -0
- package/dist/esm-node/runtime/data-platform/types.mjs +5 -0
- package/dist/esm-node/runtime/data-platform/validation.mjs +129 -0
- package/dist/esm-node/runtime/effect/adapter/constants.mjs +7 -0
- package/dist/esm-node/runtime/effect/adapter/cross-project-policy.mjs +24 -0
- package/dist/esm-node/runtime/effect/adapter/entry.mjs +30 -0
- package/dist/esm-node/runtime/effect/adapter/error-response.mjs +42 -0
- package/dist/esm-node/runtime/effect/adapter/handler.mjs +16 -0
- package/dist/esm-node/runtime/effect/adapter.mjs +21 -145
- package/dist/esm-node/runtime/effect/backend-federation/entry.mjs +62 -0
- package/dist/esm-node/runtime/effect/backend-federation/load.mjs +20 -0
- package/dist/esm-node/runtime/effect/backend-federation/plugin.mjs +16 -0
- package/dist/esm-node/runtime/effect/backend-federation/remotes.mjs +13 -0
- package/dist/esm-node/runtime/effect/backend-federation/runtime.mjs +27 -0
- package/dist/esm-node/runtime/effect/backend-federation/types.mjs +1 -0
- package/dist/esm-node/runtime/effect/backend-federation/utils.mjs +8 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/errors.mjs +33 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/index.mjs +5 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/load.mjs +37 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/metadata.mjs +43 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/reference.mjs +77 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/remote.mjs +31 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/types.mjs +1 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest/validation.mjs +65 -0
- package/dist/esm-node/runtime/effect/backend-federation-manifest.mjs +2 -0
- package/dist/esm-node/runtime/effect/backend-federation.mjs +5 -0
- package/dist/esm-node/runtime/effect/dispatch.mjs +64 -0
- package/dist/esm-node/runtime/effect/edge.mjs +17 -63
- package/dist/esm-node/runtime/effect/endpoint-contracts.mjs +8 -13
- package/dist/esm-node/runtime/effect/entry-shape.mjs +37 -0
- package/dist/esm-node/runtime/effect/handler/batch-handler.mjs +124 -0
- package/dist/esm-node/runtime/effect/handler/batch.mjs +125 -0
- package/dist/esm-node/runtime/effect/handler/definition.mjs +61 -0
- package/dist/esm-node/runtime/effect/handler/envelope.mjs +64 -0
- package/dist/esm-node/runtime/effect/handler/http.mjs +59 -0
- package/dist/esm-node/runtime/effect/handler/openapi.mjs +20 -0
- package/dist/esm-node/runtime/effect/handler/routing.mjs +51 -0
- package/dist/esm-node/runtime/effect/handler/rpc.mjs +23 -0
- package/dist/esm-node/runtime/effect/handler/types.mjs +6 -0
- package/dist/esm-node/runtime/effect/handler.mjs +11 -506
- package/dist/esm-node/runtime/effect/index.mjs +5 -2
- package/dist/esm-node/runtime/effect/module.mjs +25 -82
- package/dist/esm-node/runtime/effect/rpcSerialization.mjs +17 -0
- package/dist/esm-node/runtime/effect-client/batch.mjs +36 -0
- package/dist/esm-node/runtime/effect-client/envelope.mjs +105 -0
- package/dist/esm-node/runtime/effect-client/guards.mjs +4 -0
- package/dist/esm-node/runtime/effect-client/index.mjs +27 -37
- package/dist/esm-node/runtime/effect-client/request-context.mjs +24 -0
- package/dist/esm-node/runtime/effect-client/request.mjs +29 -0
- package/dist/esm-node/runtime/effect-client/runtime.mjs +12 -178
- package/dist/esm-node/runtime/effect-client/types.mjs +1 -0
- package/dist/esm-node/runtime/safe-failure.mjs +2 -1
- package/dist/esm-node/utils/client-generator/files.mjs +69 -0
- package/dist/esm-node/utils/client-generator/generate.mjs +89 -0
- package/dist/esm-node/utils/client-generator/index.mjs +5 -0
- package/dist/esm-node/utils/client-generator/package-json.mjs +79 -0
- package/dist/esm-node/utils/client-generator/write-package.mjs +75 -0
- package/dist/esm-node/utils/clientGenerator.mjs +2 -293
- package/dist/esm-node/utils/effect-client-generator/generator.mjs +24 -0
- package/dist/esm-node/utils/effect-client-generator/index.mjs +4 -0
- package/dist/esm-node/utils/effect-client-generator/paths.mjs +26 -0
- package/dist/esm-node/utils/effect-client-generator/rendering.mjs +181 -0
- package/dist/esm-node/utils/effect-client-generator/runtime.mjs +35 -0
- package/dist/esm-node/utils/effect-client-generator/types.mjs +1 -0
- package/dist/esm-node/utils/effectClientGenerator.mjs +2 -251
- package/dist/types/cli/compress.d.ts +2 -0
- package/dist/types/cli/generator.d.ts +7 -0
- package/dist/types/cli/prefix.d.ts +2 -0
- package/dist/types/cli/watch.d.ts +1 -0
- package/dist/types/cli.d.ts +0 -1
- package/dist/types/runtime/data-platform/batch/queue.d.ts +8 -0
- package/dist/types/runtime/data-platform/batch/request.d.ts +13 -0
- package/dist/types/runtime/data-platform/batch/response.d.ts +3 -0
- package/dist/types/runtime/data-platform/batch/telemetry.d.ts +4 -0
- package/dist/types/runtime/data-platform/batch/url.d.ts +4 -0
- package/dist/types/runtime/data-platform/batch-transport.d.ts +3 -0
- package/dist/types/runtime/data-platform/codec.d.ts +17 -0
- package/dist/types/runtime/data-platform/index.d.ts +7 -191
- package/dist/types/runtime/data-platform/invalidation.d.ts +8 -0
- package/dist/types/runtime/data-platform/trace.d.ts +4 -0
- package/dist/types/runtime/data-platform/types.d.ts +146 -0
- package/dist/types/runtime/data-platform/validation.d.ts +29 -0
- package/dist/types/runtime/effect/adapter/constants.d.ts +1 -0
- package/dist/types/runtime/effect/adapter/cross-project-policy.d.ts +4 -0
- package/dist/types/runtime/effect/adapter/entry.d.ts +2 -0
- package/dist/types/runtime/effect/adapter/error-response.d.ts +2 -0
- package/dist/types/runtime/effect/adapter/handler.d.ts +7 -0
- package/dist/types/runtime/effect/adapter.d.ts +4 -13
- package/dist/types/runtime/effect/backend-federation/entry.d.ts +2 -0
- package/dist/types/runtime/effect/backend-federation/load.d.ts +2 -0
- package/dist/types/runtime/effect/backend-federation/plugin.d.ts +3 -0
- package/dist/types/runtime/effect/backend-federation/remotes.d.ts +2 -0
- package/dist/types/runtime/effect/backend-federation/runtime.d.ts +3 -0
- package/dist/types/runtime/effect/backend-federation/types.d.ts +58 -0
- package/dist/types/runtime/effect/backend-federation/utils.d.ts +2 -0
- package/dist/types/runtime/effect/backend-federation-manifest/errors.d.ts +11 -0
- package/dist/types/runtime/effect/backend-federation-manifest/index.d.ts +6 -0
- package/dist/types/runtime/effect/backend-federation-manifest/load.d.ts +3 -0
- package/dist/types/runtime/effect/backend-federation-manifest/metadata.d.ts +18 -0
- package/dist/types/runtime/effect/backend-federation-manifest/reference.d.ts +3 -0
- package/dist/types/runtime/effect/backend-federation-manifest/remote.d.ts +3 -0
- package/dist/types/runtime/effect/backend-federation-manifest/types.d.ts +51 -0
- package/dist/types/runtime/effect/backend-federation-manifest/validation.d.ts +5 -0
- package/dist/types/runtime/effect/backend-federation-manifest.d.ts +1 -0
- package/dist/types/runtime/effect/backend-federation.d.ts +6 -0
- package/dist/types/runtime/effect/dispatch.d.ts +13 -0
- package/dist/types/runtime/effect/edge.d.ts +8 -8
- package/dist/types/runtime/effect/endpoint-contracts.d.ts +5 -7
- package/dist/types/runtime/effect/entry-shape.d.ts +31 -0
- package/dist/types/runtime/effect/handler/batch-handler.d.ts +11 -0
- package/dist/types/runtime/effect/handler/batch.d.ts +17 -0
- package/dist/types/runtime/effect/handler/definition.d.ts +17 -0
- package/dist/types/runtime/effect/handler/envelope.d.ts +13 -0
- package/dist/types/runtime/effect/handler/http.d.ts +14 -0
- package/dist/types/runtime/effect/handler/openapi.d.ts +4 -0
- package/dist/types/runtime/effect/handler/routing.d.ts +5 -0
- package/dist/types/runtime/effect/handler/rpc.d.ts +7 -0
- package/dist/types/runtime/effect/handler/types.d.ts +148 -0
- package/dist/types/runtime/effect/handler.d.ts +4 -190
- package/dist/types/runtime/effect/index.d.ts +3 -0
- package/dist/types/runtime/effect/module.d.ts +3 -2
- package/dist/types/runtime/effect/rpcSerialization.d.ts +3 -0
- package/dist/types/runtime/effect-client/batch.d.ts +2 -0
- package/dist/types/runtime/effect-client/envelope.d.ts +15 -0
- package/dist/types/runtime/effect-client/guards.d.ts +2 -0
- package/dist/types/runtime/effect-client/index.d.ts +1 -1
- package/dist/types/runtime/effect-client/request-context.d.ts +3 -0
- package/dist/types/runtime/effect-client/request.d.ts +1 -0
- package/dist/types/runtime/effect-client/runtime.d.ts +2 -68
- package/dist/types/runtime/effect-client/types.d.ts +68 -0
- package/dist/types/utils/client-generator/files.d.ts +28 -0
- package/dist/types/utils/client-generator/generate.d.ts +26 -0
- package/dist/types/utils/client-generator/index.d.ts +7 -0
- package/dist/types/utils/client-generator/package-json.d.ts +16 -0
- package/dist/types/utils/client-generator/write-package.d.ts +6 -0
- package/dist/types/utils/clientGenerator.d.ts +2 -37
- package/dist/types/utils/crossProjectServerPolicy.d.ts +3 -2
- package/dist/types/utils/effect-client-generator/generator.d.ts +10 -0
- package/dist/types/utils/effect-client-generator/index.d.ts +4 -0
- package/dist/types/utils/effect-client-generator/paths.d.ts +5 -0
- package/dist/types/utils/effect-client-generator/rendering.d.ts +4 -0
- package/dist/types/utils/effect-client-generator/runtime.d.ts +8 -0
- package/dist/types/utils/effect-client-generator/types.d.ts +26 -0
- package/dist/types/utils/effectClientGenerator.d.ts +2 -41
- package/package.json +34 -28
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { ApiRouter } from "@modern-js/bff-core";
|
|
2
|
+
import { compile } from "@modern-js/server-utils";
|
|
3
|
+
import { API_DIR, SHARED_DIR, fs, resolveServerTsconfig } from "@modern-js/utils";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import clientGenerator from "../utils/clientGenerator.mjs";
|
|
6
|
+
import pluginGenerator from "../utils/pluginGenerator.mjs";
|
|
7
|
+
import runtimeGenerator from "../utils/runtimeGenerator.mjs";
|
|
8
|
+
import { getPrimaryPrefix } from "./prefix.mjs";
|
|
9
|
+
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/client';
|
|
10
|
+
const createBffGenerator = (api)=>{
|
|
11
|
+
const compileApi = async ()=>{
|
|
12
|
+
const { appDirectory, distDirectory, apiDirectory, sharedDirectory, moduleType } = api.getAppContext();
|
|
13
|
+
const modernConfig = api.getNormalizedConfig();
|
|
14
|
+
const distDir = path.resolve(distDirectory);
|
|
15
|
+
const apiDir = apiDirectory || path.resolve(appDirectory, API_DIR);
|
|
16
|
+
const sharedDir = sharedDirectory || path.resolve(appDirectory, SHARED_DIR);
|
|
17
|
+
const tsconfigPath = resolveServerTsconfig(appDirectory, modernConfig?.server?.tsconfigPath);
|
|
18
|
+
const sourceDirs = [];
|
|
19
|
+
if (await fs.pathExists(apiDir)) sourceDirs.push(apiDir);
|
|
20
|
+
if (await fs.pathExists(sharedDir)) sourceDirs.push(sharedDir);
|
|
21
|
+
const { alias } = modernConfig.source;
|
|
22
|
+
const { alias: resolveAlias } = modernConfig.resolve;
|
|
23
|
+
if (sourceDirs.length > 0) {
|
|
24
|
+
const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
|
|
25
|
+
await compile(appDirectory, {
|
|
26
|
+
alias: combinedAlias
|
|
27
|
+
}, {
|
|
28
|
+
sourceDirs,
|
|
29
|
+
distDir,
|
|
30
|
+
tsconfigPath,
|
|
31
|
+
moduleType,
|
|
32
|
+
throwErrorInsteadOfExit: true
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const generate = async ()=>{
|
|
37
|
+
const { appDirectory, apiDirectory, lambdaDirectory, port, bffRuntimeFramework } = api.getAppContext();
|
|
38
|
+
const modernConfig = api.getNormalizedConfig();
|
|
39
|
+
const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
|
|
40
|
+
const { bff } = modernConfig || {};
|
|
41
|
+
const prefix = getPrimaryPrefix(bff?.prefix);
|
|
42
|
+
const httpMethodDecider = bff?.httpMethodDecider;
|
|
43
|
+
const apiRouter = new ApiRouter({
|
|
44
|
+
apiDir: apiDirectory,
|
|
45
|
+
appDir: appDirectory,
|
|
46
|
+
lambdaDir: lambdaDirectory,
|
|
47
|
+
prefix,
|
|
48
|
+
httpMethodDecider,
|
|
49
|
+
isBuild: true
|
|
50
|
+
});
|
|
51
|
+
const lambdaDir = apiRouter.getLambdaDir();
|
|
52
|
+
const existLambda = apiRouter.isExistLambda();
|
|
53
|
+
const runtime = bff?.runtimeCreateRequest || RUNTIME_CREATE_REQUEST;
|
|
54
|
+
const relativeApiPath = path.relative(appDirectory, apiDirectory);
|
|
55
|
+
const relativeLambdaPath = path.relative(appDirectory, lambdaDir);
|
|
56
|
+
await pluginGenerator({
|
|
57
|
+
prefix,
|
|
58
|
+
appDirectory,
|
|
59
|
+
relativeDistPath,
|
|
60
|
+
relativeApiPath,
|
|
61
|
+
relativeLambdaPath,
|
|
62
|
+
runtimeFramework: 'hono' === bffRuntimeFramework ? 'hono' : 'effect'
|
|
63
|
+
});
|
|
64
|
+
await clientGenerator({
|
|
65
|
+
prefix,
|
|
66
|
+
appDir: appDirectory,
|
|
67
|
+
apiDir: apiDirectory,
|
|
68
|
+
lambdaDir,
|
|
69
|
+
existLambda,
|
|
70
|
+
port,
|
|
71
|
+
requestCreator: bff?.requestCreator,
|
|
72
|
+
httpMethodDecider,
|
|
73
|
+
relativeDistPath,
|
|
74
|
+
relativeApiPath,
|
|
75
|
+
bffRuntimeFramework,
|
|
76
|
+
effectEntry: bff?.effect?.entry,
|
|
77
|
+
effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch
|
|
78
|
+
});
|
|
79
|
+
await runtimeGenerator({
|
|
80
|
+
runtime,
|
|
81
|
+
appDirectory,
|
|
82
|
+
relativeDistPath
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
const handleCrossProjectInvocation = async (isBuild = false)=>{
|
|
86
|
+
const { bff } = api.getNormalizedConfig();
|
|
87
|
+
if (bff?.crossProject) {
|
|
88
|
+
if (!isBuild) await compileApi();
|
|
89
|
+
await generate();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
compileApi,
|
|
94
|
+
generate,
|
|
95
|
+
handleCrossProjectInvocation
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
export { createBffGenerator };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DEFAULT_API_PREFIX } from "@modern-js/utils";
|
|
2
|
+
const normalizePrefixList = (prefix)=>{
|
|
3
|
+
if (Array.isArray(prefix)) return prefix.filter(Boolean);
|
|
4
|
+
return [
|
|
5
|
+
prefix || DEFAULT_API_PREFIX
|
|
6
|
+
];
|
|
7
|
+
};
|
|
8
|
+
const getPrimaryPrefix = (prefix)=>normalizePrefixList(prefix)[0] || DEFAULT_API_PREFIX;
|
|
9
|
+
export { getPrimaryPrefix, normalizePrefixList };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const WATCHABLE_EXTENSIONS = [
|
|
2
|
+
'.ts',
|
|
3
|
+
'.tsx',
|
|
4
|
+
'.js',
|
|
5
|
+
'.jsx',
|
|
6
|
+
'.mts',
|
|
7
|
+
'.cts',
|
|
8
|
+
'.mjs',
|
|
9
|
+
'.cjs',
|
|
10
|
+
'.json'
|
|
11
|
+
];
|
|
12
|
+
const isWatchableBffFile = (filename)=>WATCHABLE_EXTENSIONS.some((ext)=>filename.endsWith(ext));
|
|
13
|
+
export { isWatchableBffFile };
|
package/dist/esm/cli.mjs
CHANGED
|
@@ -1,30 +1,10 @@
|
|
|
1
1
|
import { ApiRouter } from "@modern-js/bff-core";
|
|
2
|
-
import {
|
|
3
|
-
import { API_DIR, DEFAULT_API_PREFIX, SHARED_DIR, fs, normalizeOutputPath, resolveServerTsconfig } from "@modern-js/utils";
|
|
2
|
+
import { normalizeOutputPath } from "@modern-js/utils";
|
|
4
3
|
import path from "path";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
const WATCHABLE_EXTENSIONS = [
|
|
10
|
-
'.ts',
|
|
11
|
-
'.tsx',
|
|
12
|
-
'.js',
|
|
13
|
-
'.jsx',
|
|
14
|
-
'.mts',
|
|
15
|
-
'.cts',
|
|
16
|
-
'.mjs',
|
|
17
|
-
'.cjs',
|
|
18
|
-
'.json'
|
|
19
|
-
];
|
|
20
|
-
const isWatchableBffFile = (filename)=>WATCHABLE_EXTENSIONS.some((ext)=>filename.endsWith(ext));
|
|
21
|
-
const normalizePrefixList = (prefix)=>{
|
|
22
|
-
if (Array.isArray(prefix)) return prefix.filter(Boolean);
|
|
23
|
-
return [
|
|
24
|
-
prefix || DEFAULT_API_PREFIX
|
|
25
|
-
];
|
|
26
|
-
};
|
|
27
|
-
const getPrimaryPrefix = (prefix)=>normalizePrefixList(prefix)[0] || DEFAULT_API_PREFIX;
|
|
4
|
+
import { createCompressConfig } from "./cli/compress.mjs";
|
|
5
|
+
import { createBffGenerator } from "./cli/generator.mjs";
|
|
6
|
+
import { getPrimaryPrefix } from "./cli/prefix.mjs";
|
|
7
|
+
import { isWatchableBffFile } from "./cli/watch.mjs";
|
|
28
8
|
const bffPlugin = ()=>({
|
|
29
9
|
name: '@modern-js/plugin-bff',
|
|
30
10
|
setup: (api)=>{
|
|
@@ -36,106 +16,14 @@ const bffPlugin = ()=>({
|
|
|
36
16
|
bffRuntimeFramework: 'hono' === userRuntimeFramework ? 'hono' : 'effect'
|
|
37
17
|
});
|
|
38
18
|
}
|
|
39
|
-
const compileApi =
|
|
40
|
-
const { appDirectory, distDirectory, apiDirectory, sharedDirectory, moduleType } = api.getAppContext();
|
|
41
|
-
const modernConfig = api.getNormalizedConfig();
|
|
42
|
-
const distDir = path.resolve(distDirectory);
|
|
43
|
-
const apiDir = apiDirectory || path.resolve(appDirectory, API_DIR);
|
|
44
|
-
const sharedDir = sharedDirectory || path.resolve(appDirectory, SHARED_DIR);
|
|
45
|
-
const tsconfigPath = resolveServerTsconfig(appDirectory, modernConfig?.server?.tsconfigPath);
|
|
46
|
-
const sourceDirs = [];
|
|
47
|
-
if (await fs.pathExists(apiDir)) sourceDirs.push(apiDir);
|
|
48
|
-
if (await fs.pathExists(sharedDir)) sourceDirs.push(sharedDir);
|
|
49
|
-
const { alias } = modernConfig.source;
|
|
50
|
-
const { alias: resolveAlias } = modernConfig.resolve;
|
|
51
|
-
if (sourceDirs.length > 0) {
|
|
52
|
-
const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
|
|
53
|
-
await compile(appDirectory, {
|
|
54
|
-
alias: combinedAlias
|
|
55
|
-
}, {
|
|
56
|
-
sourceDirs,
|
|
57
|
-
distDir,
|
|
58
|
-
tsconfigPath,
|
|
59
|
-
moduleType,
|
|
60
|
-
throwErrorInsteadOfExit: true
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const generator = async ()=>{
|
|
65
|
-
const { appDirectory, apiDirectory, lambdaDirectory, port, bffRuntimeFramework } = api.getAppContext();
|
|
66
|
-
const modernConfig = api.getNormalizedConfig();
|
|
67
|
-
const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
|
|
68
|
-
const { bff } = modernConfig || {};
|
|
69
|
-
const prefix = getPrimaryPrefix(bff?.prefix);
|
|
70
|
-
const httpMethodDecider = bff?.httpMethodDecider;
|
|
71
|
-
const apiRouter = new ApiRouter({
|
|
72
|
-
apiDir: apiDirectory,
|
|
73
|
-
appDir: appDirectory,
|
|
74
|
-
lambdaDir: lambdaDirectory,
|
|
75
|
-
prefix,
|
|
76
|
-
httpMethodDecider,
|
|
77
|
-
isBuild: true
|
|
78
|
-
});
|
|
79
|
-
const lambdaDir = apiRouter.getLambdaDir();
|
|
80
|
-
const existLambda = apiRouter.isExistLambda();
|
|
81
|
-
const runtime = bff?.runtimeCreateRequest || RUNTIME_CREATE_REQUEST;
|
|
82
|
-
const relativeApiPath = path.relative(appDirectory, apiDirectory);
|
|
83
|
-
const relativeLambdaPath = path.relative(appDirectory, lambdaDir);
|
|
84
|
-
await pluginGenerator({
|
|
85
|
-
prefix,
|
|
86
|
-
appDirectory,
|
|
87
|
-
relativeDistPath,
|
|
88
|
-
relativeApiPath,
|
|
89
|
-
relativeLambdaPath,
|
|
90
|
-
runtimeFramework: 'hono' === bffRuntimeFramework ? 'hono' : 'effect'
|
|
91
|
-
});
|
|
92
|
-
await clientGenerator({
|
|
93
|
-
prefix,
|
|
94
|
-
appDir: appDirectory,
|
|
95
|
-
apiDir: apiDirectory,
|
|
96
|
-
lambdaDir,
|
|
97
|
-
existLambda,
|
|
98
|
-
port,
|
|
99
|
-
requestCreator: bff?.requestCreator,
|
|
100
|
-
httpMethodDecider,
|
|
101
|
-
relativeDistPath,
|
|
102
|
-
relativeApiPath,
|
|
103
|
-
bffRuntimeFramework,
|
|
104
|
-
effectEntry: bff?.effect?.entry,
|
|
105
|
-
effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch
|
|
106
|
-
});
|
|
107
|
-
await runtimeGenerator({
|
|
108
|
-
runtime,
|
|
109
|
-
appDirectory,
|
|
110
|
-
relativeDistPath
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
const handleCrossProjectInvocation = async (isBuild = false)=>{
|
|
114
|
-
const { bff } = api.getNormalizedConfig();
|
|
115
|
-
if (bff?.crossProject) {
|
|
116
|
-
if (!isBuild) await compileApi();
|
|
117
|
-
await generator();
|
|
118
|
-
}
|
|
119
|
-
};
|
|
19
|
+
const { compileApi, handleCrossProjectInvocation } = createBffGenerator(api);
|
|
120
20
|
const isHono = ()=>{
|
|
121
21
|
const { bffRuntimeFramework } = api.getAppContext();
|
|
122
22
|
return 'hono' === bffRuntimeFramework;
|
|
123
23
|
};
|
|
124
|
-
const createCompressConfig = (devServer, prefix)=>{
|
|
125
|
-
if (!devServer || 'object' != typeof devServer || Array.isArray(devServer)) return;
|
|
126
|
-
const { compress } = devServer;
|
|
127
|
-
if (void 0 === compress || true === compress) {
|
|
128
|
-
const prefixes = normalizePrefixList(prefix);
|
|
129
|
-
return {
|
|
130
|
-
filter: (req)=>!prefixes.some((item)=>req.url?.includes(item))
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
if (false === compress) return false;
|
|
134
|
-
return compress;
|
|
135
|
-
};
|
|
136
24
|
api.config(async ()=>{
|
|
137
25
|
const devServer = api.getConfig()?.tools?.devServer;
|
|
138
|
-
const prefix = api.getConfig()?.bff?.prefix
|
|
26
|
+
const prefix = api.getConfig()?.bff?.prefix;
|
|
139
27
|
const compress = createCompressConfig(devServer, prefix);
|
|
140
28
|
return {
|
|
141
29
|
tools: {
|
|
@@ -240,6 +128,4 @@ const bffPlugin = ()=>({
|
|
|
240
128
|
});
|
|
241
129
|
}
|
|
242
130
|
});
|
|
243
|
-
const cli = bffPlugin;
|
|
244
|
-
export default cli;
|
|
245
131
|
export { bffPlugin };
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { stableStringify } from "../codec.mjs";
|
|
2
|
+
import { DEFAULT_DATA_BATCH_HEADER } from "../types.mjs";
|
|
3
|
+
import { createBatchId, measureTextBytes, normalizeMethod, shouldBatchRequest, toHeaderRecord, toRequestBody } from "./request.mjs";
|
|
4
|
+
import { isBatchResponsePayload, parseResponseLikeCreateRequest } from "./response.mjs";
|
|
5
|
+
import { emitDataBatchTransportEvent } from "./telemetry.mjs";
|
|
6
|
+
import { normalizeBatchEndpoint, toAbsoluteUrl } from "./url.mjs";
|
|
7
|
+
function ensureBucket(buckets, endpoint) {
|
|
8
|
+
const existing = buckets.get(endpoint);
|
|
9
|
+
if (existing) return existing;
|
|
10
|
+
const next = {
|
|
11
|
+
items: [],
|
|
12
|
+
bytes: 0,
|
|
13
|
+
timer: null,
|
|
14
|
+
flushing: false
|
|
15
|
+
};
|
|
16
|
+
buckets.set(endpoint, next);
|
|
17
|
+
return next;
|
|
18
|
+
}
|
|
19
|
+
function createBatchTransportQueue({ options, baseFetch }) {
|
|
20
|
+
const flushIntervalMs = Math.max(0, options.flushIntervalMs ?? 8);
|
|
21
|
+
const maxBatchSize = Math.max(1, options.maxBatchSize ?? 16);
|
|
22
|
+
const maxBatchBytes = Math.max(1024, options.maxBatchBytes ?? 65536);
|
|
23
|
+
const requestTimeoutMs = options.requestTimeoutMs;
|
|
24
|
+
const allowedMethods = new Set((options.allowedMethods && options.allowedMethods.length > 0 ? options.allowedMethods : [
|
|
25
|
+
'GET'
|
|
26
|
+
]).map((method)=>method.toUpperCase()));
|
|
27
|
+
const onEvent = options.onEvent;
|
|
28
|
+
const buckets = new Map();
|
|
29
|
+
const pendingByKey = new Map();
|
|
30
|
+
const disabledEndpoints = new Set();
|
|
31
|
+
let nextItemId = 0;
|
|
32
|
+
const runSingle = async (request)=>{
|
|
33
|
+
const response = await baseFetch(request.requestUrl, request.requestInit);
|
|
34
|
+
return parseResponseLikeCreateRequest(response);
|
|
35
|
+
};
|
|
36
|
+
const settleRequests = async (items, runner)=>{
|
|
37
|
+
await Promise.all(items.map(async (item)=>{
|
|
38
|
+
try {
|
|
39
|
+
const value = await runner(item);
|
|
40
|
+
item.resolve(value);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
item.reject(error);
|
|
43
|
+
} finally{
|
|
44
|
+
pendingByKey.delete(item.key);
|
|
45
|
+
}
|
|
46
|
+
}));
|
|
47
|
+
};
|
|
48
|
+
const flushBucket = async (endpoint)=>{
|
|
49
|
+
const bucket = buckets.get(endpoint);
|
|
50
|
+
if (!bucket || bucket.flushing) return;
|
|
51
|
+
if (bucket.timer) {
|
|
52
|
+
clearTimeout(bucket.timer);
|
|
53
|
+
bucket.timer = null;
|
|
54
|
+
}
|
|
55
|
+
if (0 === bucket.items.length) return;
|
|
56
|
+
bucket.flushing = true;
|
|
57
|
+
const items = bucket.items;
|
|
58
|
+
bucket.items = [];
|
|
59
|
+
bucket.bytes = 0;
|
|
60
|
+
if (1 === items.length || disabledEndpoints.has(endpoint)) {
|
|
61
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
62
|
+
type: disabledEndpoints.has(endpoint) ? 'fallback' : 'flush',
|
|
63
|
+
endpoint,
|
|
64
|
+
size: items.length,
|
|
65
|
+
reason: disabledEndpoints.has(endpoint) ? 'batch-disabled' : void 0
|
|
66
|
+
});
|
|
67
|
+
await settleRequests(items, runSingle);
|
|
68
|
+
bucket.flushing = false;
|
|
69
|
+
if (bucket.items.length > 0 && !bucket.timer) flushBucket(endpoint);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const batchId = createBatchId();
|
|
73
|
+
const payload = {
|
|
74
|
+
protocolVersion: 1,
|
|
75
|
+
batchId,
|
|
76
|
+
sentAt: Date.now(),
|
|
77
|
+
items: items.map((item)=>item.item)
|
|
78
|
+
};
|
|
79
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
80
|
+
type: 'flush',
|
|
81
|
+
endpoint,
|
|
82
|
+
batchId,
|
|
83
|
+
size: items.length
|
|
84
|
+
});
|
|
85
|
+
const payloadJson = JSON.stringify(payload);
|
|
86
|
+
const traceparent = items.find((item)=>'string' == typeof item.item.headers?.traceparent)?.item.headers?.traceparent || void 0;
|
|
87
|
+
const requestInit = {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
headers: {
|
|
90
|
+
accept: 'application/json, */*;q=0.8',
|
|
91
|
+
'content-type': 'application/json; charset=utf-8',
|
|
92
|
+
[DEFAULT_DATA_BATCH_HEADER]: '1',
|
|
93
|
+
...traceparent ? {
|
|
94
|
+
traceparent
|
|
95
|
+
} : {}
|
|
96
|
+
},
|
|
97
|
+
body: payloadJson
|
|
98
|
+
};
|
|
99
|
+
let timeoutHandle = null;
|
|
100
|
+
try {
|
|
101
|
+
const controller = requestTimeoutMs && requestTimeoutMs > 0 ? new AbortController() : void 0;
|
|
102
|
+
if (controller) {
|
|
103
|
+
requestInit.signal = controller.signal;
|
|
104
|
+
timeoutHandle = setTimeout(()=>{
|
|
105
|
+
controller.abort();
|
|
106
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
107
|
+
type: 'fallback',
|
|
108
|
+
endpoint,
|
|
109
|
+
batchId,
|
|
110
|
+
size: items.length,
|
|
111
|
+
reason: 'batch-timeout'
|
|
112
|
+
});
|
|
113
|
+
}, requestTimeoutMs);
|
|
114
|
+
}
|
|
115
|
+
const response = await baseFetch(endpoint, requestInit);
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
if (404 === response.status || 405 === response.status) {
|
|
118
|
+
disabledEndpoints.add(endpoint);
|
|
119
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
120
|
+
type: 'disable',
|
|
121
|
+
endpoint,
|
|
122
|
+
batchId,
|
|
123
|
+
reason: `batch-endpoint-unavailable-${String(response.status)}`
|
|
124
|
+
});
|
|
125
|
+
} else emitDataBatchTransportEvent(onEvent, {
|
|
126
|
+
type: 'fallback',
|
|
127
|
+
endpoint,
|
|
128
|
+
batchId,
|
|
129
|
+
size: items.length,
|
|
130
|
+
reason: `batch-response-${String(response.status)}`
|
|
131
|
+
});
|
|
132
|
+
await settleRequests(items, runSingle);
|
|
133
|
+
bucket.flushing = false;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const result = await response.json();
|
|
137
|
+
if (!isBatchResponsePayload(result)) {
|
|
138
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
139
|
+
type: 'fallback',
|
|
140
|
+
endpoint,
|
|
141
|
+
batchId,
|
|
142
|
+
size: items.length,
|
|
143
|
+
reason: 'invalid-batch-response'
|
|
144
|
+
});
|
|
145
|
+
await settleRequests(items, runSingle);
|
|
146
|
+
bucket.flushing = false;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const itemMap = new Map();
|
|
150
|
+
for (const item of result.items)itemMap.set(item.id, item);
|
|
151
|
+
await settleRequests(items, async (request)=>{
|
|
152
|
+
const resultItem = itemMap.get(request.item.id);
|
|
153
|
+
if (!resultItem) return runSingle(request);
|
|
154
|
+
const reconstructedResponse = new Response(resultItem.body ?? '', {
|
|
155
|
+
status: resultItem.status,
|
|
156
|
+
headers: resultItem.headers
|
|
157
|
+
});
|
|
158
|
+
return parseResponseLikeCreateRequest(reconstructedResponse);
|
|
159
|
+
});
|
|
160
|
+
} catch (error) {
|
|
161
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
162
|
+
type: 'fallback',
|
|
163
|
+
endpoint,
|
|
164
|
+
batchId,
|
|
165
|
+
size: items.length,
|
|
166
|
+
reason: 'batch-transport-error'
|
|
167
|
+
});
|
|
168
|
+
await settleRequests(items, runSingle);
|
|
169
|
+
} finally{
|
|
170
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
171
|
+
bucket.flushing = false;
|
|
172
|
+
if (bucket.items.length > 0 && !bucket.timer) flushBucket(endpoint);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
return (input, init)=>{
|
|
176
|
+
const requestUrl = toAbsoluteUrl(input);
|
|
177
|
+
const batchEndpointUrl = normalizeBatchEndpoint(requestUrl, options.endpoint);
|
|
178
|
+
const endpoint = batchEndpointUrl.toString();
|
|
179
|
+
const method = normalizeMethod(init?.method);
|
|
180
|
+
const body = toRequestBody(init?.body ?? null);
|
|
181
|
+
const headers = toHeaderRecord(init?.headers);
|
|
182
|
+
const normalizedInit = {
|
|
183
|
+
...init,
|
|
184
|
+
method,
|
|
185
|
+
headers,
|
|
186
|
+
body
|
|
187
|
+
};
|
|
188
|
+
if (disabledEndpoints.has(endpoint) || !shouldBatchRequest({
|
|
189
|
+
method,
|
|
190
|
+
body,
|
|
191
|
+
headers,
|
|
192
|
+
allowedMethods,
|
|
193
|
+
batchEndpoint: endpoint,
|
|
194
|
+
requestUrl
|
|
195
|
+
})) return baseFetch(requestUrl.toString(), normalizedInit).then(parseResponseLikeCreateRequest);
|
|
196
|
+
const itemId = `${Date.now().toString(36)}_${nextItemId.toString(36)}_${Math.random().toString(16).slice(2, 8)}`;
|
|
197
|
+
nextItemId += 1;
|
|
198
|
+
const item = {
|
|
199
|
+
id: itemId,
|
|
200
|
+
path: `${requestUrl.pathname}${requestUrl.search}`,
|
|
201
|
+
method,
|
|
202
|
+
headers,
|
|
203
|
+
...body ? {
|
|
204
|
+
body
|
|
205
|
+
} : {}
|
|
206
|
+
};
|
|
207
|
+
const key = stableStringify({
|
|
208
|
+
endpoint,
|
|
209
|
+
path: item.path,
|
|
210
|
+
method: item.method,
|
|
211
|
+
headers: item.headers,
|
|
212
|
+
body: item.body ?? null
|
|
213
|
+
});
|
|
214
|
+
const existing = pendingByKey.get(key);
|
|
215
|
+
if (existing) return existing;
|
|
216
|
+
const size = measureTextBytes(stableStringify(item));
|
|
217
|
+
const promise = new Promise((resolve, reject)=>{
|
|
218
|
+
const bucket = ensureBucket(buckets, endpoint);
|
|
219
|
+
const queued = {
|
|
220
|
+
key,
|
|
221
|
+
endpoint,
|
|
222
|
+
requestUrl: requestUrl.toString(),
|
|
223
|
+
requestInit: normalizedInit,
|
|
224
|
+
item,
|
|
225
|
+
size,
|
|
226
|
+
resolve,
|
|
227
|
+
reject
|
|
228
|
+
};
|
|
229
|
+
bucket.items.push(queued);
|
|
230
|
+
bucket.bytes += size;
|
|
231
|
+
emitDataBatchTransportEvent(onEvent, {
|
|
232
|
+
type: 'enqueue',
|
|
233
|
+
endpoint,
|
|
234
|
+
size: bucket.items.length
|
|
235
|
+
});
|
|
236
|
+
if (bucket.items.length >= maxBatchSize || bucket.bytes >= maxBatchBytes) return void flushBucket(endpoint);
|
|
237
|
+
if (!bucket.timer) bucket.timer = setTimeout(()=>{
|
|
238
|
+
bucket.timer = null;
|
|
239
|
+
flushBucket(endpoint);
|
|
240
|
+
}, flushIntervalMs);
|
|
241
|
+
});
|
|
242
|
+
pendingByKey.set(key, promise);
|
|
243
|
+
return promise;
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
export { createBatchTransportQueue };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { DEFAULT_DATA_BATCH_HEADER } from "../types.mjs";
|
|
2
|
+
function toHeaderRecord(headers) {
|
|
3
|
+
if (!headers) return {};
|
|
4
|
+
if (headers instanceof Headers) {
|
|
5
|
+
const next = {};
|
|
6
|
+
headers.forEach((value, key)=>{
|
|
7
|
+
next[key.toLowerCase()] = value;
|
|
8
|
+
});
|
|
9
|
+
return next;
|
|
10
|
+
}
|
|
11
|
+
if (Array.isArray(headers)) return headers.reduce((acc, [key, value])=>{
|
|
12
|
+
acc[String(key).toLowerCase()] = String(value);
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
return Object.entries(headers).reduce((acc, [key, value])=>{
|
|
16
|
+
if (void 0 === value) return acc;
|
|
17
|
+
acc[String(key).toLowerCase()] = Array.isArray(value) ? value.join(', ') : String(value);
|
|
18
|
+
return acc;
|
|
19
|
+
}, {});
|
|
20
|
+
}
|
|
21
|
+
function measureTextBytes(value) {
|
|
22
|
+
if ("u" > typeof TextEncoder) return new TextEncoder().encode(value).length;
|
|
23
|
+
if ("u" > typeof Buffer) return Buffer.byteLength(value);
|
|
24
|
+
return value.length;
|
|
25
|
+
}
|
|
26
|
+
function createBatchId() {
|
|
27
|
+
const now = Date.now().toString(36);
|
|
28
|
+
const random = Math.random().toString(16).slice(2, 10);
|
|
29
|
+
return `batch_${now}_${random}`;
|
|
30
|
+
}
|
|
31
|
+
function normalizeMethod(method) {
|
|
32
|
+
return (method || 'GET').toUpperCase();
|
|
33
|
+
}
|
|
34
|
+
function toRequestBody(initBody) {
|
|
35
|
+
if ('string' == typeof initBody) return initBody;
|
|
36
|
+
if ("u" > typeof URLSearchParams && initBody instanceof URLSearchParams) return initBody.toString();
|
|
37
|
+
}
|
|
38
|
+
function shouldBatchRequest(input) {
|
|
39
|
+
if (input.requestUrl.href === input.batchEndpoint) return false;
|
|
40
|
+
if ('off' === input.headers[DEFAULT_DATA_BATCH_HEADER]) return false;
|
|
41
|
+
if (!input.allowedMethods.has(input.method)) return false;
|
|
42
|
+
if (void 0 !== input.body) return false;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
export { createBatchId, measureTextBytes, normalizeMethod, shouldBatchRequest, toHeaderRecord, toRequestBody };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isPlainObject } from "../codec.mjs";
|
|
2
|
+
function isBatchResponseItem(value) {
|
|
3
|
+
return isPlainObject(value) && 'string' == typeof value.id && 'number' == typeof value.status;
|
|
4
|
+
}
|
|
5
|
+
function isBatchResponsePayload(value) {
|
|
6
|
+
return isPlainObject(value) && 1 === value.protocolVersion && 'string' == typeof value.batchId && 'number' == typeof value.receivedAt && Array.isArray(value.items) && value.items.every((item)=>isBatchResponseItem(item));
|
|
7
|
+
}
|
|
8
|
+
async function parseResponseLikeCreateRequest(response) {
|
|
9
|
+
const contentType = (response.headers.get('content-type') || '').toLowerCase();
|
|
10
|
+
if (!response.ok) {
|
|
11
|
+
let data = null;
|
|
12
|
+
data = contentType.includes('application/json') ? await response.json() : await response.text();
|
|
13
|
+
response.data = data;
|
|
14
|
+
throw response;
|
|
15
|
+
}
|
|
16
|
+
if (contentType.includes('application/json') || contentType.includes('text/json')) return response.json();
|
|
17
|
+
if (contentType.includes('text/html') || contentType.includes('text/plain')) return response.text();
|
|
18
|
+
if (contentType.includes('application/x-www-form-urlencoded') || contentType.includes('multipart/form-data')) return response.formData();
|
|
19
|
+
if (contentType.includes('application/octet-stream')) return response.arrayBuffer();
|
|
20
|
+
if (contentType.includes('image/png')) return response;
|
|
21
|
+
return response.text();
|
|
22
|
+
}
|
|
23
|
+
export { isBatchResponsePayload, parseResponseLikeCreateRequest };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { trace } from "@opentelemetry/api";
|
|
2
|
+
const DATA_BATCH_TRANSPORT_OTEL_EVENT = 'modernjs.data.batch';
|
|
3
|
+
function createDataBatchTransportTelemetryAttributes(event) {
|
|
4
|
+
return {
|
|
5
|
+
'modernjs.data.batch.type': event.type,
|
|
6
|
+
'modernjs.data.batch.endpoint': event.endpoint,
|
|
7
|
+
'modernjs.data.batch.degraded': 'fallback' === event.type || 'disable' === event.type,
|
|
8
|
+
...event.batchId ? {
|
|
9
|
+
'modernjs.data.batch.id': event.batchId
|
|
10
|
+
} : {},
|
|
11
|
+
...'number' == typeof event.size ? {
|
|
12
|
+
'modernjs.data.batch.size': event.size
|
|
13
|
+
} : {},
|
|
14
|
+
...event.reason ? {
|
|
15
|
+
'modernjs.data.batch.reason': event.reason
|
|
16
|
+
} : {}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function emitDataBatchTransportEvent(onEvent, event) {
|
|
20
|
+
onEvent?.(event);
|
|
21
|
+
trace.getActiveSpan()?.addEvent(DATA_BATCH_TRANSPORT_OTEL_EVENT, createDataBatchTransportTelemetryAttributes(event));
|
|
22
|
+
}
|
|
23
|
+
export { DATA_BATCH_TRANSPORT_OTEL_EVENT, createDataBatchTransportTelemetryAttributes, emitDataBatchTransportEvent };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DEFAULT_DATA_BATCH_ENDPOINT } from "../types.mjs";
|
|
2
|
+
function resolveRuntimeOrigin() {
|
|
3
|
+
if ("u" > typeof window && window.location && 'string' == typeof window.location.origin && window.location.origin) return window.location.origin;
|
|
4
|
+
if ("u" > typeof globalThis && globalThis.location && 'string' == typeof globalThis.location?.origin) return globalThis.location.origin;
|
|
5
|
+
return 'http://localhost';
|
|
6
|
+
}
|
|
7
|
+
function toAbsoluteUrl(input) {
|
|
8
|
+
if (input instanceof URL) return input;
|
|
9
|
+
if ("u" > typeof Request && input instanceof Request) return new URL(input.url);
|
|
10
|
+
const value = String(input);
|
|
11
|
+
try {
|
|
12
|
+
return new URL(value);
|
|
13
|
+
} catch {
|
|
14
|
+
return new URL(value, resolveRuntimeOrigin());
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function normalizeBatchEndpoint(requestUrl, endpoint) {
|
|
18
|
+
const value = endpoint || DEFAULT_DATA_BATCH_ENDPOINT;
|
|
19
|
+
try {
|
|
20
|
+
return new URL(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return new URL(value, requestUrl.origin);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export { normalizeBatchEndpoint, resolveRuntimeOrigin, toAbsoluteUrl };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createBatchTransportQueue } from "./batch/queue.mjs";
|
|
2
|
+
function createDataBatchTransport(options = {}) {
|
|
3
|
+
const fallbackFetch = 'function' == typeof fetch ? fetch.bind(globalThis) : void 0;
|
|
4
|
+
const baseFetch = options.fetch || fallbackFetch;
|
|
5
|
+
if (!baseFetch) throw new Error('createDataBatchTransport requires a fetch implementation');
|
|
6
|
+
return createBatchTransportQueue({
|
|
7
|
+
options,
|
|
8
|
+
baseFetch
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export { DATA_BATCH_TRANSPORT_OTEL_EVENT, createDataBatchTransportTelemetryAttributes, emitDataBatchTransportEvent } from "./batch/telemetry.mjs";
|
|
12
|
+
export { createDataBatchTransport };
|