@webiny/api-kernel-core 0.0.0-stage → 0.0.0-unstable.e1999482d0
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/LICENSE +21 -0
- package/README.md +10 -2
- package/exports/api.d.ts +2 -0
- package/exports/api.js +1 -0
- package/features/backgroundTask/BackgroundTaskInputHandler.d.ts +9 -0
- package/features/backgroundTask/BackgroundTaskInputHandler.js +5 -0
- package/features/backgroundTask/BackgroundTaskInputHandler.js.map +1 -0
- package/features/backgroundTask/BackgroundTaskInputType.d.ts +18 -0
- package/features/backgroundTask/BackgroundTaskInputType.js +18 -0
- package/features/backgroundTask/BackgroundTaskInputType.js.map +1 -0
- package/features/backgroundTask/index.d.ts +3 -0
- package/features/backgroundTask/index.js +2 -0
- package/features/dispatch/InputDispatcher.d.ts +17 -0
- package/features/dispatch/InputDispatcher.js +5 -0
- package/features/dispatch/InputDispatcher.js.map +1 -0
- package/features/dispatch/index.d.ts +1 -0
- package/features/dispatch/index.js +1 -0
- package/features/http/HttpResponseBuilder.d.ts +36 -0
- package/features/http/HttpResponseBuilder.js +132 -0
- package/features/http/HttpResponseBuilder.js.map +1 -0
- package/features/http/HttpRouter.d.ts +26 -0
- package/features/http/HttpRouter.js +58 -0
- package/features/http/HttpRouter.js.map +1 -0
- package/features/http/HttpStreamBody.d.ts +34 -0
- package/features/http/HttpStreamBody.js +44 -0
- package/features/http/HttpStreamBody.js.map +1 -0
- package/features/http/abstractions.d.ts +202 -0
- package/features/http/abstractions.js +13 -0
- package/features/http/abstractions.js.map +1 -0
- package/features/http/codedErrorBody.d.ts +10 -0
- package/features/http/codedErrorBody.js +10 -0
- package/features/http/codedErrorBody.js.map +1 -0
- package/features/http/corsHeaders.d.ts +8 -0
- package/features/http/corsHeaders.js +10 -0
- package/features/http/corsHeaders.js.map +1 -0
- package/features/http/createHttpRouteDefinition.d.ts +17 -0
- package/features/http/createHttpRouteDefinition.js +18 -0
- package/features/http/createHttpRouteDefinition.js.map +1 -0
- package/features/http/decorators/CompressionDecorator.d.ts +28 -0
- package/features/http/decorators/CompressionDecorator.js +66 -0
- package/features/http/decorators/CompressionDecorator.js.map +1 -0
- package/features/http/decorators/SecureHeadersDecorator.d.ts +13 -0
- package/features/http/decorators/SecureHeadersDecorator.js +71 -0
- package/features/http/decorators/SecureHeadersDecorator.js.map +1 -0
- package/features/http/decorators/index.d.ts +2 -0
- package/features/http/decorators/index.js +2 -0
- package/features/http/feature.d.ts +4 -0
- package/features/http/feature.js +15 -0
- package/features/http/feature.js.map +1 -0
- package/features/http/index.d.ts +12 -0
- package/features/http/index.js +12 -0
- package/features/http/invokeHttpRoute.d.ts +17 -0
- package/features/http/invokeHttpRoute.js +32 -0
- package/features/http/invokeHttpRoute.js.map +1 -0
- package/features/http/parseJsonBody.d.ts +7 -0
- package/features/http/parseJsonBody.js +8 -0
- package/features/http/parseJsonBody.js.map +1 -0
- package/features/http/toSseFrame.d.ts +10 -0
- package/features/http/toSseFrame.js +6 -0
- package/features/http/toSseFrame.js.map +1 -0
- package/features/kernel/ChildContainerFactory.d.ts +15 -0
- package/features/kernel/ChildContainerFactory.js +35 -0
- package/features/kernel/ChildContainerFactory.js.map +1 -0
- package/features/kernel/DefaultKernelExecution.d.ts +14 -0
- package/features/kernel/DefaultKernelExecution.js +15 -0
- package/features/kernel/DefaultKernelExecution.js.map +1 -0
- package/features/kernel/DefaultKernelExecutionId.d.ts +11 -0
- package/features/kernel/DefaultKernelExecutionId.js +11 -0
- package/features/kernel/DefaultKernelExecutionId.js.map +1 -0
- package/features/kernel/DefaultKernelExecutionScope.d.ts +11 -0
- package/features/kernel/DefaultKernelExecutionScope.js +29 -0
- package/features/kernel/DefaultKernelExecutionScope.js.map +1 -0
- package/features/kernel/InputHandler.d.ts +13 -0
- package/features/kernel/InputHandler.js +5 -0
- package/features/kernel/InputHandler.js.map +1 -0
- package/features/kernel/InputShape.d.ts +11 -0
- package/features/kernel/InputShape.js +29 -0
- package/features/kernel/InputShape.js.map +1 -0
- package/features/kernel/InputType.d.ts +15 -0
- package/features/kernel/InputType.js +5 -0
- package/features/kernel/InputType.js.map +1 -0
- package/features/kernel/Kernel.d.ts +28 -0
- package/features/kernel/Kernel.js +34 -0
- package/features/kernel/Kernel.js.map +1 -0
- package/features/kernel/KernelExecution.d.ts +18 -0
- package/features/kernel/KernelExecution.js +5 -0
- package/features/kernel/KernelExecution.js.map +1 -0
- package/features/kernel/KernelExecutionId.d.ts +22 -0
- package/features/kernel/KernelExecutionId.js +5 -0
- package/features/kernel/KernelExecutionId.js.map +1 -0
- package/features/kernel/KernelExecutionRunner.d.ts +17 -0
- package/features/kernel/KernelExecutionRunner.js +41 -0
- package/features/kernel/KernelExecutionRunner.js.map +1 -0
- package/features/kernel/KernelExecutionScope.d.ts +36 -0
- package/features/kernel/KernelExecutionScope.js +5 -0
- package/features/kernel/KernelExecutionScope.js.map +1 -0
- package/features/kernel/KernelPlatform.d.ts +32 -0
- package/features/kernel/KernelPlatform.js +5 -0
- package/features/kernel/KernelPlatform.js.map +1 -0
- package/features/kernel/NoMatchingInputTypeError.d.ts +8 -0
- package/features/kernel/NoMatchingInputTypeError.js +24 -0
- package/features/kernel/NoMatchingInputTypeError.js.map +1 -0
- package/features/kernel/NoopKernelPlatform.d.ts +15 -0
- package/features/kernel/NoopKernelPlatform.js +12 -0
- package/features/kernel/NoopKernelPlatform.js.map +1 -0
- package/features/kernel/RequestContainer.d.ts +8 -0
- package/features/kernel/RequestContainer.js +5 -0
- package/features/kernel/RequestContainer.js.map +1 -0
- package/features/kernel/RootContainerFactory.d.ts +14 -0
- package/features/kernel/RootContainerFactory.js +31 -0
- package/features/kernel/RootContainerFactory.js.map +1 -0
- package/features/kernel/StaleKernelExecutionError.d.ts +4 -0
- package/features/kernel/StaleKernelExecutionError.js +20 -0
- package/features/kernel/StaleKernelExecutionError.js.map +1 -0
- package/features/kernel/abstractions.d.ts +77 -0
- package/features/kernel/abstractions.js +8 -0
- package/features/kernel/abstractions.js.map +1 -0
- package/features/kernel/chain.d.ts +2 -0
- package/features/kernel/chain.js +22 -0
- package/features/kernel/chain.js.map +1 -0
- package/features/kernel/index.d.ts +16 -0
- package/features/kernel/index.js +15 -0
- package/features/kernel/types.d.ts +4 -0
- package/features/kernel/types.js +0 -0
- package/features/scheduledAction/ScheduledActionInputHandler.d.ts +12 -0
- package/features/scheduledAction/ScheduledActionInputHandler.js +5 -0
- package/features/scheduledAction/ScheduledActionInputHandler.js.map +1 -0
- package/features/scheduledAction/ScheduledActionInputType.d.ts +19 -0
- package/features/scheduledAction/ScheduledActionInputType.js +19 -0
- package/features/scheduledAction/ScheduledActionInputType.js.map +1 -0
- package/features/scheduledAction/index.d.ts +4 -0
- package/features/scheduledAction/index.js +2 -0
- package/features/testing/HttpRouterHandler.d.ts +13 -0
- package/features/testing/HttpRouterHandler.js +42 -0
- package/features/testing/HttpRouterHandler.js.map +1 -0
- package/features/testing/TestHttpInputHandler.d.ts +6 -0
- package/features/testing/TestHttpInputHandler.js +5 -0
- package/features/testing/TestHttpInputHandler.js.map +1 -0
- package/features/testing/TestHttpInputType.d.ts +15 -0
- package/features/testing/TestHttpInputType.js +20 -0
- package/features/testing/TestHttpInputType.js.map +1 -0
- package/features/testing/createTestHttpHandler.d.ts +14 -0
- package/features/testing/createTestHttpHandler.js +26 -0
- package/features/testing/createTestHttpHandler.js.map +1 -0
- package/features/testing/index.d.ts +5 -0
- package/features/testing/index.js +5 -0
- package/features/testing/registerHttpRouteInstance.d.ts +16 -0
- package/features/testing/registerHttpRouteInstance.js +27 -0
- package/features/testing/registerHttpRouteInstance.js.map +1 -0
- package/index.d.ts +8 -0
- package/index.js +8 -0
- package/nodeHttp/abstractions/NodeHttpInputHandler.d.ts +14 -0
- package/nodeHttp/abstractions/NodeHttpInputHandler.js +5 -0
- package/nodeHttp/abstractions/NodeHttpInputHandler.js.map +1 -0
- package/nodeHttp/createServerHandler.d.ts +41 -0
- package/nodeHttp/createServerHandler.js +43 -0
- package/nodeHttp/createServerHandler.js.map +1 -0
- package/nodeHttp/features/NodeHttpFeature.d.ts +15 -0
- package/nodeHttp/features/NodeHttpFeature.js +15 -0
- package/nodeHttp/features/NodeHttpFeature.js.map +1 -0
- package/nodeHttp/handlers/NodeHttpRouterHandler.d.ts +18 -0
- package/nodeHttp/handlers/NodeHttpRouterHandler.js +55 -0
- package/nodeHttp/handlers/NodeHttpRouterHandler.js.map +1 -0
- package/nodeHttp/index.d.ts +7 -0
- package/nodeHttp/index.js +6 -0
- package/nodeHttp/inputTypes/NodeHttpInputType.d.ts +10 -0
- package/nodeHttp/inputTypes/NodeHttpInputType.js +18 -0
- package/nodeHttp/inputTypes/NodeHttpInputType.js.map +1 -0
- package/nodeHttp/server/InFlightRequests.d.ts +21 -0
- package/nodeHttp/server/InFlightRequests.js +37 -0
- package/nodeHttp/server/InFlightRequests.js.map +1 -0
- package/nodeHttp/server/KernelInputDispatcher.d.ts +15 -0
- package/nodeHttp/server/KernelInputDispatcher.js +12 -0
- package/nodeHttp/server/KernelInputDispatcher.js.map +1 -0
- package/nodeHttp/server/createNodeHttpServer.d.ts +12 -0
- package/nodeHttp/server/createNodeHttpServer.js +20 -0
- package/nodeHttp/server/createNodeHttpServer.js.map +1 -0
- package/nodeHttp/server/handleShutdownSignals.d.ts +16 -0
- package/nodeHttp/server/handleShutdownSignals.js +40 -0
- package/nodeHttp/server/handleShutdownSignals.js.map +1 -0
- package/nodeHttp/server/writeBufferedBody.d.ts +5 -0
- package/nodeHttp/server/writeBufferedBody.js +9 -0
- package/nodeHttp/server/writeBufferedBody.js.map +1 -0
- package/nodeHttp/server/writeErrorResponse.d.ts +10 -0
- package/nodeHttp/server/writeErrorResponse.js +9 -0
- package/nodeHttp/server/writeErrorResponse.js.map +1 -0
- package/nodeHttp/server/writeHttpResponse.d.ts +7 -0
- package/nodeHttp/server/writeHttpResponse.js +23 -0
- package/nodeHttp/server/writeHttpResponse.js.map +1 -0
- package/nodeHttp/server/writeStreamBody.d.ts +6 -0
- package/nodeHttp/server/writeStreamBody.js +24 -0
- package/nodeHttp/server/writeStreamBody.js.map +1 -0
- package/nodeHttp/translators/NodeHttpTranslator.d.ts +7 -0
- package/nodeHttp/translators/NodeHttpTranslator.js +79 -0
- package/nodeHttp/translators/NodeHttpTranslator.js.map +1 -0
- package/nodeHttp/translators/RequestBodyTooLargeError.d.ts +8 -0
- package/nodeHttp/translators/RequestBodyTooLargeError.js +9 -0
- package/nodeHttp/translators/RequestBodyTooLargeError.js.map +1 -0
- package/package.json +38 -5
- package/process/ProcessOwner.d.ts +7 -0
- package/process/ProcessOwner.js +5 -0
- package/process/ProcessOwner.js.map +1 -0
- package/process/RandomProcessOwner.d.ts +9 -0
- package/process/RandomProcessOwner.js +16 -0
- package/process/RandomProcessOwner.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webiny/api-kernel-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
6
|
+
|
|
7
|
+
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
package/exports/api.d.ts
ADDED
package/exports/api.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { HttpAllowedRequestHeaders, HttpRouteDefinition, HttpRouteHandler } from "../features/http/abstractions.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Abstraction } from "@webiny/di";
|
|
2
|
+
import type { InputHandler } from "../../features/kernel/InputHandler.js";
|
|
3
|
+
import type { IBackgroundTaskEvent } from "./BackgroundTaskInputType.js";
|
|
4
|
+
export interface IBackgroundTaskInputHandler extends InputHandler.Interface<IBackgroundTaskEvent, unknown> {
|
|
5
|
+
}
|
|
6
|
+
export declare const BackgroundTaskInputHandler: Abstraction<IBackgroundTaskInputHandler>;
|
|
7
|
+
export declare namespace BackgroundTaskInputHandler {
|
|
8
|
+
type Interface = IBackgroundTaskInputHandler;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/backgroundTask/BackgroundTaskInputHandler.js","sources":["../../../src/features/backgroundTask/BackgroundTaskInputHandler.ts"],"sourcesContent":["import { Abstraction } from \"@webiny/di\";\nimport type { InputHandler } from \"~/features/kernel/InputHandler.js\";\nimport type { IBackgroundTaskEvent } from \"./BackgroundTaskInputType.js\";\n\n// Result is the task response (status: continue/done/error) — the Step Functions state machine\n// reads `$.status` from the Lambda's return value to decide the next state, so the handler MUST\n// return it (returning void makes the SFN see null → UnknownError → execution FAILED).\nexport interface IBackgroundTaskInputHandler extends InputHandler.Interface<\n IBackgroundTaskEvent,\n unknown\n> {}\n\nexport const BackgroundTaskInputHandler = new Abstraction<IBackgroundTaskInputHandler>(\n \"BackgroundTaskInputHandler\"\n);\n\nexport namespace BackgroundTaskInputHandler {\n export type Interface = IBackgroundTaskInputHandler;\n}\n"],"names":["BackgroundTaskInputHandler","Abstraction"],"mappings":";AAYO,MAAMA,6BAA6B,IAAIC,YAC1C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InputType } from "../../features/kernel/InputType.js";
|
|
2
|
+
export interface IBackgroundTaskEvent {
|
|
3
|
+
webinyTaskId: string;
|
|
4
|
+
webinyTaskDefinitionId: string;
|
|
5
|
+
tenant: string;
|
|
6
|
+
endpoint: string;
|
|
7
|
+
executionName: string;
|
|
8
|
+
stateMachineId: string;
|
|
9
|
+
delay?: number;
|
|
10
|
+
}
|
|
11
|
+
declare class BackgroundTaskInputTypeImpl implements InputType.Interface<IBackgroundTaskEvent> {
|
|
12
|
+
matches(input: any): input is IBackgroundTaskEvent;
|
|
13
|
+
getHandlerAbstraction(): import("@webiny/di").Abstraction<import("./BackgroundTaskInputHandler.js").IBackgroundTaskInputHandler>;
|
|
14
|
+
}
|
|
15
|
+
export declare const BackgroundTaskInputType: typeof BackgroundTaskInputTypeImpl & {
|
|
16
|
+
__abstraction: import("@webiny/di").Abstraction<import("~/features/kernel/InputType.js").IInputType<any>>;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InputType } from "../kernel/InputType.js";
|
|
2
|
+
import { BackgroundTaskInputHandler } from "./BackgroundTaskInputHandler.js";
|
|
3
|
+
class BackgroundTaskInputTypeImpl {
|
|
4
|
+
matches(input) {
|
|
5
|
+
const e = input?.payload ?? input;
|
|
6
|
+
return !!(e?.webinyTaskId && e?.webinyTaskDefinitionId && e?.tenant);
|
|
7
|
+
}
|
|
8
|
+
getHandlerAbstraction() {
|
|
9
|
+
return BackgroundTaskInputHandler;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const BackgroundTaskInputType = InputType.createImplementation({
|
|
13
|
+
implementation: BackgroundTaskInputTypeImpl,
|
|
14
|
+
dependencies: []
|
|
15
|
+
});
|
|
16
|
+
export { BackgroundTaskInputType };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=BackgroundTaskInputType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/backgroundTask/BackgroundTaskInputType.js","sources":["../../../src/features/backgroundTask/BackgroundTaskInputType.ts"],"sourcesContent":["import { InputType } from \"~/features/kernel/InputType.js\";\n\nimport { BackgroundTaskInputHandler } from \"./BackgroundTaskInputHandler.js\";\n\nexport interface IBackgroundTaskEvent {\n webinyTaskId: string;\n webinyTaskDefinitionId: string;\n tenant: string;\n endpoint: string;\n executionName: string;\n stateMachineId: string;\n delay?: number;\n}\n\nclass BackgroundTaskInputTypeImpl implements InputType.Interface<IBackgroundTaskEvent> {\n matches(input: any): input is IBackgroundTaskEvent {\n // The Step Functions / EventBridge transport delivers the task wrapped as\n // `{ name: \"background-task\", payload: { webinyTaskId, ... } }` (see the bg-task SFN ASL\n // definition in project-aws). Unwrap `payload` if present; also accept an already-flat input\n // defensively so direct invokes still work.\n const e = input?.payload ?? input;\n return !!(e?.webinyTaskId && e?.webinyTaskDefinitionId && e?.tenant);\n }\n\n getHandlerAbstraction() {\n return BackgroundTaskInputHandler;\n }\n}\n\nexport const BackgroundTaskInputType = InputType.createImplementation({\n implementation: BackgroundTaskInputTypeImpl,\n dependencies: []\n});\n"],"names":["BackgroundTaskInputTypeImpl","input","e","BackgroundTaskInputHandler","BackgroundTaskInputType","InputType"],"mappings":";;AAcA,MAAMA;IACF,QAAQC,KAAU,EAAiC;QAK/C,MAAMC,IAAID,OAAO,WAAWA;QAC5B,OAAO,CAAC,CAAEC,CAAAA,GAAG,gBAAgBA,GAAG,0BAA0BA,GAAG,MAAK;IACtE;IAEA,wBAAwB;QACpB,OAAOC;IACX;AACJ;AAEO,MAAMC,0BAA0BC,UAAU,oBAAoB,CAAC;IAClE,gBAAgBL;IAChB,cAAc,EAAE;AACpB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Abstraction } from "@webiny/di";
|
|
2
|
+
/**
|
|
3
|
+
* Hands an event to the handler app from inside the running process, as if a platform had invoked
|
|
4
|
+
* it: the app builds a fresh request container, matches the event to its event type, and runs that
|
|
5
|
+
* type's handler chain. Resolves with what the handler returns.
|
|
6
|
+
*
|
|
7
|
+
* This is how a long-lived process does work that needs the per-request stack (tenant, CMS models,
|
|
8
|
+
* use cases) outside any incoming request: a background task iteration, a scheduled action firing.
|
|
9
|
+
* Lambda gets the same from a fresh invocation, so only transports without one register this.
|
|
10
|
+
*/
|
|
11
|
+
export interface IInputDispatcher {
|
|
12
|
+
dispatch<TResult = unknown>(event: unknown): Promise<TResult>;
|
|
13
|
+
}
|
|
14
|
+
export declare const InputDispatcher: Abstraction<IInputDispatcher>;
|
|
15
|
+
export declare namespace InputDispatcher {
|
|
16
|
+
type Interface = IInputDispatcher;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/dispatch/InputDispatcher.js","sources":["../../../src/features/dispatch/InputDispatcher.ts"],"sourcesContent":["import { Abstraction } from \"@webiny/di\";\n\n/**\n * Hands an event to the handler app from inside the running process, as if a platform had invoked\n * it: the app builds a fresh request container, matches the event to its event type, and runs that\n * type's handler chain. Resolves with what the handler returns.\n *\n * This is how a long-lived process does work that needs the per-request stack (tenant, CMS models,\n * use cases) outside any incoming request: a background task iteration, a scheduled action firing.\n * Lambda gets the same from a fresh invocation, so only transports without one register this.\n */\nexport interface IInputDispatcher {\n dispatch<TResult = unknown>(event: unknown): Promise<TResult>;\n}\n\nexport const InputDispatcher = new Abstraction<IInputDispatcher>(\"InputDispatcher\");\n\nexport namespace InputDispatcher {\n export type Interface = IInputDispatcher;\n}\n"],"names":["InputDispatcher","Abstraction"],"mappings":";AAeO,MAAMA,kBAAkB,IAAIC,YAA8B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { InputDispatcher } from "./InputDispatcher.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { InputDispatcher } from "./InputDispatcher.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CookieOptions, IHttpResponse, IHttpResponseBuilder } from "./abstractions.js";
|
|
2
|
+
import type { HttpStreamSource } from "./HttpStreamBody.js";
|
|
3
|
+
/**
|
|
4
|
+
* Serialize a single `Set-Cookie` value. Kept dependency-free (no `cookie` package) — the attribute
|
|
5
|
+
* set below is everything the framework needs.
|
|
6
|
+
*/
|
|
7
|
+
export declare function serializeCookie(name: string, value: string, options?: CookieOptions): string;
|
|
8
|
+
export declare class HttpResponseBuilder implements IHttpResponseBuilder {
|
|
9
|
+
private statusCodeValue;
|
|
10
|
+
private headerValues;
|
|
11
|
+
private cookieValues;
|
|
12
|
+
private bodyValue;
|
|
13
|
+
private modified;
|
|
14
|
+
status(statusCode: number): this;
|
|
15
|
+
header(name: string, value: string): this;
|
|
16
|
+
setHeaders(headers: Record<string, string>): this;
|
|
17
|
+
getHeader(name: string): string | undefined;
|
|
18
|
+
removeHeader(name: string): this;
|
|
19
|
+
type(contentType: string): this;
|
|
20
|
+
cookie(name: string, value: string, options?: CookieOptions): this;
|
|
21
|
+
clearCookie(name: string, options?: Omit<CookieOptions, "expires" | "maxAge">): this;
|
|
22
|
+
json(body?: any): this;
|
|
23
|
+
send(body?: any): this;
|
|
24
|
+
text(body: string): this;
|
|
25
|
+
html(body: string): this;
|
|
26
|
+
redirect(url: string, statusCode?: number): this;
|
|
27
|
+
end(body?: any): this;
|
|
28
|
+
sse(source: HttpStreamSource): this;
|
|
29
|
+
/**
|
|
30
|
+
* Whether anything was set on this builder. The router uses it to decide if builder state needs
|
|
31
|
+
* merging into a plain IHttpResponse a route returned instead.
|
|
32
|
+
*/
|
|
33
|
+
isModified(): boolean;
|
|
34
|
+
toResponse(): IHttpResponse;
|
|
35
|
+
}
|
|
36
|
+
export declare function createHttpResponseBuilder(): HttpResponseBuilder;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { HttpStreamBody } from "./HttpStreamBody.js";
|
|
2
|
+
const CONTENT_TYPE = "content-type";
|
|
3
|
+
const TYPE_SHORTHANDS = {
|
|
4
|
+
json: "application/json",
|
|
5
|
+
text: "text/plain; charset=utf-8",
|
|
6
|
+
html: "text/html; charset=utf-8",
|
|
7
|
+
xml: "application/xml",
|
|
8
|
+
bin: "application/octet-stream"
|
|
9
|
+
};
|
|
10
|
+
function serializeCookie(name, value, options = {}) {
|
|
11
|
+
const parts = [
|
|
12
|
+
`${name}=${encodeURIComponent(value)}`
|
|
13
|
+
];
|
|
14
|
+
if (void 0 !== options.maxAge) parts.push(`Max-Age=${Math.floor(options.maxAge)}`);
|
|
15
|
+
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
16
|
+
parts.push(`Path=${options.path ?? "/"}`);
|
|
17
|
+
if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);
|
|
18
|
+
if (options.httpOnly) parts.push("HttpOnly");
|
|
19
|
+
if (options.secure) parts.push("Secure");
|
|
20
|
+
if (options.partitioned) parts.push("Partitioned");
|
|
21
|
+
if (options.sameSite) {
|
|
22
|
+
const sameSite = options.sameSite;
|
|
23
|
+
parts.push(`SameSite=${sameSite.charAt(0).toUpperCase()}${sameSite.slice(1)}`);
|
|
24
|
+
}
|
|
25
|
+
return parts.join("; ");
|
|
26
|
+
}
|
|
27
|
+
class HttpResponseBuilder {
|
|
28
|
+
status(statusCode) {
|
|
29
|
+
this.statusCodeValue = statusCode;
|
|
30
|
+
this.modified = true;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
header(name, value) {
|
|
34
|
+
this.headerValues[name.toLowerCase()] = value;
|
|
35
|
+
this.modified = true;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
setHeaders(headers) {
|
|
39
|
+
for (const [name, value] of Object.entries(headers))this.header(name, value);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
getHeader(name) {
|
|
43
|
+
return this.headerValues[name.toLowerCase()];
|
|
44
|
+
}
|
|
45
|
+
removeHeader(name) {
|
|
46
|
+
delete this.headerValues[name.toLowerCase()];
|
|
47
|
+
this.modified = true;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
type(contentType) {
|
|
51
|
+
return this.header(CONTENT_TYPE, TYPE_SHORTHANDS[contentType] ?? contentType);
|
|
52
|
+
}
|
|
53
|
+
cookie(name, value, options) {
|
|
54
|
+
this.cookieValues.push(serializeCookie(name, value, options));
|
|
55
|
+
this.modified = true;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
clearCookie(name, options) {
|
|
59
|
+
return this.cookie(name, "", {
|
|
60
|
+
...options,
|
|
61
|
+
expires: new Date(0),
|
|
62
|
+
maxAge: 0
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
json(body) {
|
|
66
|
+
if (void 0 === this.getHeader(CONTENT_TYPE)) this.type("json");
|
|
67
|
+
this.bodyValue = void 0 === body ? void 0 : JSON.stringify(body);
|
|
68
|
+
this.modified = true;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
send(body) {
|
|
72
|
+
if (null == body) return this.end(body);
|
|
73
|
+
if ("string" == typeof body) {
|
|
74
|
+
if (void 0 === this.getHeader(CONTENT_TYPE)) this.type("html");
|
|
75
|
+
return this.end(body);
|
|
76
|
+
}
|
|
77
|
+
if (Buffer.isBuffer(body) || body instanceof Uint8Array) {
|
|
78
|
+
if (void 0 === this.getHeader(CONTENT_TYPE)) this.type("bin");
|
|
79
|
+
return this.end(body);
|
|
80
|
+
}
|
|
81
|
+
return this.json(body);
|
|
82
|
+
}
|
|
83
|
+
text(body) {
|
|
84
|
+
return this.type("text").end(body);
|
|
85
|
+
}
|
|
86
|
+
html(body) {
|
|
87
|
+
return this.type("html").end(body);
|
|
88
|
+
}
|
|
89
|
+
redirect(url, statusCode = 302) {
|
|
90
|
+
return this.status(statusCode).header("location", url).end();
|
|
91
|
+
}
|
|
92
|
+
end(body) {
|
|
93
|
+
this.bodyValue = body;
|
|
94
|
+
this.modified = true;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
sse(source) {
|
|
98
|
+
return this.setHeaders({
|
|
99
|
+
"content-type": "text/event-stream",
|
|
100
|
+
"cache-control": "no-store, no-cache, no-transform",
|
|
101
|
+
"x-accel-buffering": "no"
|
|
102
|
+
}).end(new HttpStreamBody(source));
|
|
103
|
+
}
|
|
104
|
+
isModified() {
|
|
105
|
+
return this.modified;
|
|
106
|
+
}
|
|
107
|
+
toResponse() {
|
|
108
|
+
const response = {
|
|
109
|
+
statusCode: this.statusCodeValue
|
|
110
|
+
};
|
|
111
|
+
if (Object.keys(this.headerValues).length > 0) response.headers = {
|
|
112
|
+
...this.headerValues
|
|
113
|
+
};
|
|
114
|
+
if (this.cookieValues.length > 0) response.cookies = [
|
|
115
|
+
...this.cookieValues
|
|
116
|
+
];
|
|
117
|
+
if (void 0 !== this.bodyValue) response.body = this.bodyValue;
|
|
118
|
+
return response;
|
|
119
|
+
}
|
|
120
|
+
constructor(){
|
|
121
|
+
this.statusCodeValue = 200;
|
|
122
|
+
this.headerValues = {};
|
|
123
|
+
this.cookieValues = [];
|
|
124
|
+
this.modified = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function createHttpResponseBuilder() {
|
|
128
|
+
return new HttpResponseBuilder();
|
|
129
|
+
}
|
|
130
|
+
export { HttpResponseBuilder, createHttpResponseBuilder, serializeCookie };
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=HttpResponseBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/http/HttpResponseBuilder.js","sources":["../../../src/features/http/HttpResponseBuilder.ts"],"sourcesContent":["import type { CookieOptions, IHttpResponse, IHttpResponseBuilder } from \"./abstractions.js\";\nimport { HttpStreamBody } from \"./HttpStreamBody.js\";\nimport type { HttpStreamSource } from \"./HttpStreamBody.js\";\n\nconst CONTENT_TYPE = \"content-type\";\n\nconst TYPE_SHORTHANDS: Record<string, string> = {\n json: \"application/json\",\n text: \"text/plain; charset=utf-8\",\n html: \"text/html; charset=utf-8\",\n xml: \"application/xml\",\n bin: \"application/octet-stream\"\n};\n\n/**\n * Serialize a single `Set-Cookie` value. Kept dependency-free (no `cookie` package) — the attribute\n * set below is everything the framework needs.\n */\nexport function serializeCookie(name: string, value: string, options: CookieOptions = {}): string {\n const parts = [`${name}=${encodeURIComponent(value)}`];\n\n if (options.maxAge !== undefined) {\n parts.push(`Max-Age=${Math.floor(options.maxAge)}`);\n }\n if (options.domain) {\n parts.push(`Domain=${options.domain}`);\n }\n // Default to \"/\" so a cookie set on e.g. POST /graphql isn't scoped to that one path.\n parts.push(`Path=${options.path ?? \"/\"}`);\n if (options.expires) {\n parts.push(`Expires=${options.expires.toUTCString()}`);\n }\n if (options.httpOnly) {\n parts.push(\"HttpOnly\");\n }\n if (options.secure) {\n parts.push(\"Secure\");\n }\n if (options.partitioned) {\n parts.push(\"Partitioned\");\n }\n if (options.sameSite) {\n const sameSite = options.sameSite;\n parts.push(`SameSite=${sameSite.charAt(0).toUpperCase()}${sameSite.slice(1)}`);\n }\n\n return parts.join(\"; \");\n}\n\nexport class HttpResponseBuilder implements IHttpResponseBuilder {\n private statusCodeValue = 200;\n private headerValues: Record<string, string> = {};\n private cookieValues: string[] = [];\n private bodyValue: any;\n private modified = false;\n\n status(statusCode: number): this {\n this.statusCodeValue = statusCode;\n this.modified = true;\n return this;\n }\n\n header(name: string, value: string): this {\n this.headerValues[name.toLowerCase()] = value;\n this.modified = true;\n return this;\n }\n\n setHeaders(headers: Record<string, string>): this {\n for (const [name, value] of Object.entries(headers)) {\n this.header(name, value);\n }\n return this;\n }\n\n getHeader(name: string): string | undefined {\n return this.headerValues[name.toLowerCase()];\n }\n\n removeHeader(name: string): this {\n delete this.headerValues[name.toLowerCase()];\n this.modified = true;\n return this;\n }\n\n type(contentType: string): this {\n return this.header(CONTENT_TYPE, TYPE_SHORTHANDS[contentType] ?? contentType);\n }\n\n cookie(name: string, value: string, options?: CookieOptions): this {\n this.cookieValues.push(serializeCookie(name, value, options));\n this.modified = true;\n return this;\n }\n\n clearCookie(name: string, options?: Omit<CookieOptions, \"expires\" | \"maxAge\">): this {\n return this.cookie(name, \"\", {\n ...options,\n expires: new Date(0),\n maxAge: 0\n });\n }\n\n json(body?: any): this {\n if (this.getHeader(CONTENT_TYPE) === undefined) {\n this.type(\"json\");\n }\n // Serialize here rather than leaving an object for the transport, so the body a route\n // produces is the exact bytes that go out regardless of transport.\n this.bodyValue = body === undefined ? undefined : JSON.stringify(body);\n this.modified = true;\n return this;\n }\n\n send(body?: any): this {\n if (body === undefined || body === null) {\n return this.end(body);\n }\n if (typeof body === \"string\") {\n if (this.getHeader(CONTENT_TYPE) === undefined) {\n this.type(\"html\");\n }\n return this.end(body);\n }\n if (Buffer.isBuffer(body) || body instanceof Uint8Array) {\n if (this.getHeader(CONTENT_TYPE) === undefined) {\n this.type(\"bin\");\n }\n return this.end(body);\n }\n return this.json(body);\n }\n\n text(body: string): this {\n return this.type(\"text\").end(body);\n }\n\n html(body: string): this {\n return this.type(\"html\").end(body);\n }\n\n redirect(url: string, statusCode = 302): this {\n return this.status(statusCode).header(\"location\", url).end();\n }\n\n end(body?: any): this {\n this.bodyValue = body;\n this.modified = true;\n return this;\n }\n\n sse(source: HttpStreamSource): this {\n // `no-transform` and `x-accel-buffering: no` are the two that silently matter: without them a\n // proxy compresses or buffers the body and the response stops being incremental while still\n // looking correct. `no-store` is about something else entirely — see the interface docs.\n // No `connection` header: the transport decides it, and a server shutting down answers `close`.\n return this.setHeaders({\n \"content-type\": \"text/event-stream\",\n \"cache-control\": \"no-store, no-cache, no-transform\",\n \"x-accel-buffering\": \"no\"\n }).end(new HttpStreamBody(source));\n }\n\n /**\n * Whether anything was set on this builder. The router uses it to decide if builder state needs\n * merging into a plain IHttpResponse a route returned instead.\n */\n isModified(): boolean {\n return this.modified;\n }\n\n toResponse(): IHttpResponse {\n const response: IHttpResponse = { statusCode: this.statusCodeValue };\n if (Object.keys(this.headerValues).length > 0) {\n response.headers = { ...this.headerValues };\n }\n if (this.cookieValues.length > 0) {\n response.cookies = [...this.cookieValues];\n }\n if (this.bodyValue !== undefined) {\n response.body = this.bodyValue;\n }\n return response;\n }\n}\n\nexport function createHttpResponseBuilder(): HttpResponseBuilder {\n return new HttpResponseBuilder();\n}\n"],"names":["CONTENT_TYPE","TYPE_SHORTHANDS","serializeCookie","name","value","options","parts","encodeURIComponent","undefined","Math","sameSite","HttpResponseBuilder","statusCode","headers","Object","contentType","Date","body","JSON","Buffer","Uint8Array","url","source","HttpStreamBody","response","createHttpResponseBuilder"],"mappings":"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"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Container } from "@webiny/di";
|
|
2
|
+
import { HttpRouter } from "../../features/http/abstractions.js";
|
|
3
|
+
import type { IHttpRequest, IHttpResponse } from "../../features/http/abstractions.js";
|
|
4
|
+
declare class HttpRouterImplClass implements HttpRouter.Interface {
|
|
5
|
+
private container;
|
|
6
|
+
/**
|
|
7
|
+
* Takes the container so it can build the matched route — and ONLY the matched route.
|
|
8
|
+
*
|
|
9
|
+
* A definition declares no dependencies, so resolving all of them to match a path is a handful
|
|
10
|
+
* of field assignments. Routes used to be resolved as instances just to read their `path`,
|
|
11
|
+
* which built every one of their dependency graphs on every request: a static-asset request
|
|
12
|
+
* constructed the whole GraphQL engine, every contextual schema and the AI provider before
|
|
13
|
+
* discovering it wanted none of them.
|
|
14
|
+
*
|
|
15
|
+
* `resolveImplementation` asks for exactly the matched route's class: it reads that route's
|
|
16
|
+
* dependencies from its own metadata, resolves them here, and applies decorators registered
|
|
17
|
+
* for `HttpRouteHandler`, so a route stays decoratable.
|
|
18
|
+
*/
|
|
19
|
+
constructor(container: Container);
|
|
20
|
+
route(request: IHttpRequest): Promise<IHttpResponse>;
|
|
21
|
+
private match;
|
|
22
|
+
}
|
|
23
|
+
export declare const HttpRouterImpl: typeof HttpRouterImplClass & {
|
|
24
|
+
__abstraction: import("@webiny/di").Abstraction<import("~/features/http/abstractions.js").IHttpRouter>;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { HttpRouteDefinition, HttpRouter, RouteNotFoundError } from "./abstractions.js";
|
|
2
|
+
import { HttpResponseBuilder } from "./HttpResponseBuilder.js";
|
|
3
|
+
import { toHttpResponse } from "./invokeHttpRoute.js";
|
|
4
|
+
import { RequestContainer } from "../kernel/RequestContainer.js";
|
|
5
|
+
function matchPath(pattern, path) {
|
|
6
|
+
if (pattern.endsWith("/*")) {
|
|
7
|
+
const prefix = pattern.slice(0, -2);
|
|
8
|
+
return path.startsWith(prefix) ? {} : null;
|
|
9
|
+
}
|
|
10
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
11
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
12
|
+
if (patternParts.length !== pathParts.length) return null;
|
|
13
|
+
const params = {};
|
|
14
|
+
for(let i = 0; i < patternParts.length; i++){
|
|
15
|
+
const pp = patternParts[i];
|
|
16
|
+
const pathPart = pathParts[i];
|
|
17
|
+
if (pp.startsWith(":")) params[pp.slice(1)] = decodeURIComponent(pathPart);
|
|
18
|
+
else if (pp !== pathPart) return null;
|
|
19
|
+
}
|
|
20
|
+
return params;
|
|
21
|
+
}
|
|
22
|
+
class HttpRouterImplClass {
|
|
23
|
+
constructor(container){
|
|
24
|
+
this.container = container;
|
|
25
|
+
}
|
|
26
|
+
async route(request) {
|
|
27
|
+
for (const definition of this.container.resolveAll(HttpRouteDefinition)){
|
|
28
|
+
const params = this.match(definition, request);
|
|
29
|
+
if (null === params) continue;
|
|
30
|
+
const route = this.container.resolveImplementation(definition.handler);
|
|
31
|
+
const response = new HttpResponseBuilder();
|
|
32
|
+
const result = await route.handle({
|
|
33
|
+
...request,
|
|
34
|
+
pathParameters: params,
|
|
35
|
+
route: {
|
|
36
|
+
name: definition.name,
|
|
37
|
+
method: definition.method,
|
|
38
|
+
path: definition.path
|
|
39
|
+
}
|
|
40
|
+
}, response);
|
|
41
|
+
return toHttpResponse(result, response);
|
|
42
|
+
}
|
|
43
|
+
throw new RouteNotFoundError(request.method, request.path);
|
|
44
|
+
}
|
|
45
|
+
match(definition, request) {
|
|
46
|
+
if (definition.method !== request.method) return null;
|
|
47
|
+
return matchPath(definition.path, request.path);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const HttpRouterImpl = HttpRouter.createImplementation({
|
|
51
|
+
implementation: HttpRouterImplClass,
|
|
52
|
+
dependencies: [
|
|
53
|
+
RequestContainer
|
|
54
|
+
]
|
|
55
|
+
});
|
|
56
|
+
export { HttpRouterImpl };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=HttpRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/http/HttpRouter.js","sources":["../../../src/features/http/HttpRouter.ts"],"sourcesContent":["import type { Container } from \"@webiny/di\";\nimport {\n HttpRouter,\n HttpRouteDefinition,\n RouteNotFoundError\n} from \"~/features/http/abstractions.js\";\nimport { HttpResponseBuilder } from \"~/features/http/HttpResponseBuilder.js\";\nimport { toHttpResponse } from \"~/features/http/invokeHttpRoute.js\";\nimport { RequestContainer } from \"~/features/kernel/RequestContainer.js\";\nimport type {\n IHttpRouteDefinition,\n IHttpRequest,\n IHttpResponse\n} from \"~/features/http/abstractions.js\";\n\nfunction matchPath(pattern: string, path: string): Record<string, string> | null {\n if (pattern.endsWith(\"/*\")) {\n const prefix = pattern.slice(0, -2);\n return path.startsWith(prefix) ? {} : null;\n }\n\n const patternParts = pattern.split(\"/\").filter(Boolean);\n const pathParts = path.split(\"/\").filter(Boolean);\n\n if (patternParts.length !== pathParts.length) {\n return null;\n }\n\n const params: Record<string, string> = {};\n\n for (let i = 0; i < patternParts.length; i++) {\n const pp = patternParts[i];\n const pathPart = pathParts[i];\n\n if (pp.startsWith(\":\")) {\n params[pp.slice(1)] = decodeURIComponent(pathPart);\n } else if (pp !== pathPart) {\n return null;\n }\n }\n\n return params;\n}\n\nclass HttpRouterImplClass implements HttpRouter.Interface {\n /**\n * Takes the container so it can build the matched route — and ONLY the matched route.\n *\n * A definition declares no dependencies, so resolving all of them to match a path is a handful\n * of field assignments. Routes used to be resolved as instances just to read their `path`,\n * which built every one of their dependency graphs on every request: a static-asset request\n * constructed the whole GraphQL engine, every contextual schema and the AI provider before\n * discovering it wanted none of them.\n *\n * `resolveImplementation` asks for exactly the matched route's class: it reads that route's\n * dependencies from its own metadata, resolves them here, and applies decorators registered\n * for `HttpRouteHandler`, so a route stays decoratable.\n */\n constructor(private container: Container) {}\n\n async route(request: IHttpRequest): Promise<IHttpResponse> {\n for (const definition of this.container.resolveAll(HttpRouteDefinition)) {\n const params = this.match(definition, request);\n if (params === null) {\n continue;\n }\n\n const route = this.container.resolveImplementation(definition.handler);\n const response = new HttpResponseBuilder();\n const result = await route.handle(\n {\n ...request,\n pathParameters: params,\n // Lets a route — or anything wrapping one — know which route is running.\n route: {\n name: definition.name,\n method: definition.method,\n path: definition.path\n }\n },\n response\n );\n return toHttpResponse(result, response);\n }\n throw new RouteNotFoundError(request.method, request.path);\n }\n\n private match(\n definition: IHttpRouteDefinition,\n request: IHttpRequest\n ): Record<string, string> | null {\n if (definition.method !== request.method) {\n return null;\n }\n return matchPath(definition.path, request.path);\n }\n}\n\nexport const HttpRouterImpl = HttpRouter.createImplementation({\n implementation: HttpRouterImplClass,\n dependencies: [RequestContainer]\n});\n"],"names":["matchPath","pattern","path","prefix","patternParts","Boolean","pathParts","params","i","pp","pathPart","decodeURIComponent","HttpRouterImplClass","container","request","definition","HttpRouteDefinition","route","response","HttpResponseBuilder","result","toHttpResponse","RouteNotFoundError","HttpRouterImpl","HttpRouter","RequestContainer"],"mappings":";;;;AAeA,SAASA,UAAUC,OAAe,EAAEC,IAAY;IAC5C,IAAID,QAAQ,QAAQ,CAAC,OAAO;QACxB,MAAME,SAASF,QAAQ,KAAK,CAAC,GAAG;QAChC,OAAOC,KAAK,UAAU,CAACC,UAAU,CAAC,IAAI;IAC1C;IAEA,MAAMC,eAAeH,QAAQ,KAAK,CAAC,KAAK,MAAM,CAACI;IAC/C,MAAMC,YAAYJ,KAAK,KAAK,CAAC,KAAK,MAAM,CAACG;IAEzC,IAAID,aAAa,MAAM,KAAKE,UAAU,MAAM,EACxC,OAAO;IAGX,MAAMC,SAAiC,CAAC;IAExC,IAAK,IAAIC,IAAI,GAAGA,IAAIJ,aAAa,MAAM,EAAEI,IAAK;QAC1C,MAAMC,KAAKL,YAAY,CAACI,EAAE;QAC1B,MAAME,WAAWJ,SAAS,CAACE,EAAE;QAE7B,IAAIC,GAAG,UAAU,CAAC,MACdF,MAAM,CAACE,GAAG,KAAK,CAAC,GAAG,GAAGE,mBAAmBD;aACtC,IAAID,OAAOC,UACd,OAAO;IAEf;IAEA,OAAOH;AACX;AAEA,MAAMK;IAcF,YAAoBC,SAAoB,CAAE;aAAtBA,SAAS,GAATA;IAAuB;IAE3C,MAAM,MAAMC,OAAqB,EAA0B;QACvD,KAAK,MAAMC,cAAc,IAAI,CAAC,SAAS,CAAC,UAAU,CAACC,qBAAsB;YACrE,MAAMT,SAAS,IAAI,CAAC,KAAK,CAACQ,YAAYD;YACtC,IAAIP,AAAW,SAAXA,QACA;YAGJ,MAAMU,QAAQ,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAACF,WAAW,OAAO;YACrE,MAAMG,WAAW,IAAIC;YACrB,MAAMC,SAAS,MAAMH,MAAM,MAAM,CAC7B;gBACI,GAAGH,OAAO;gBACV,gBAAgBP;gBAEhB,OAAO;oBACH,MAAMQ,WAAW,IAAI;oBACrB,QAAQA,WAAW,MAAM;oBACzB,MAAMA,WAAW,IAAI;gBACzB;YACJ,GACAG;YAEJ,OAAOG,eAAeD,QAAQF;QAClC;QACA,MAAM,IAAII,mBAAmBR,QAAQ,MAAM,EAAEA,QAAQ,IAAI;IAC7D;IAEQ,MACJC,UAAgC,EAChCD,OAAqB,EACQ;QAC7B,IAAIC,WAAW,MAAM,KAAKD,QAAQ,MAAM,EACpC,OAAO;QAEX,OAAOd,UAAUe,WAAW,IAAI,EAAED,QAAQ,IAAI;IAClD;AACJ;AAEO,MAAMS,iBAAiBC,WAAW,oBAAoB,CAAC;IAC1D,gBAAgBZ;IAChB,cAAc;QAACa;KAAiB;AACpC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type HttpStreamChunk = Uint8Array | string;
|
|
2
|
+
export type HttpStreamSource = AsyncIterable<HttpStreamChunk>;
|
|
3
|
+
/**
|
|
4
|
+
* Marker for a streaming HTTP response body.
|
|
5
|
+
*
|
|
6
|
+
* `IHttpResponse.body` is untyped, and buffered bodies already carry meaning by their runtime type
|
|
7
|
+
* (string, Buffer/Uint8Array, plain object). Streaming has to be distinguishable from those WITHOUT
|
|
8
|
+
* duck-typing `Symbol.asyncIterator`: a plain object body could accidentally satisfy it, and
|
|
9
|
+
* `ReadableStream`'s async-iterator support (present in Node at runtime) isn't declared in the DOM
|
|
10
|
+
* types. So a route opts into streaming explicitly, by wrapping its source in this class.
|
|
11
|
+
*
|
|
12
|
+
* Transports that can stream (the Node HTTP server; AWS Lambda response streaming via a Function
|
|
13
|
+
* URL) write chunks as they are produced. Transports that cannot (API Gateway buffers the entire
|
|
14
|
+
* Lambda response no matter how it was produced) call {@link collect} and send one buffered body —
|
|
15
|
+
* so the same route still works there, just without incremental delivery.
|
|
16
|
+
*/
|
|
17
|
+
export declare class HttpStreamBody {
|
|
18
|
+
readonly source: HttpStreamSource;
|
|
19
|
+
constructor(source: HttpStreamSource);
|
|
20
|
+
static is(value: unknown): value is HttpStreamBody;
|
|
21
|
+
/**
|
|
22
|
+
* Wrap a web `ReadableStream` — what `fetch` and the AI SDK's `toUIMessageStreamResponse()`
|
|
23
|
+
* hand back. The reader is driven explicitly rather than relying on async iteration, because
|
|
24
|
+
* the DOM types don't declare it even though Node implements it.
|
|
25
|
+
*/
|
|
26
|
+
static fromWebStream(stream: ReadableStream<Uint8Array>): HttpStreamBody;
|
|
27
|
+
/**
|
|
28
|
+
* Drain the whole stream into a single byte array, for transports that can't stream.
|
|
29
|
+
*
|
|
30
|
+
* Consumes the source — a stream can only be read once, so this must not be combined with
|
|
31
|
+
* writing the same body incrementally.
|
|
32
|
+
*/
|
|
33
|
+
collect(): Promise<Uint8Array>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
class HttpStreamBody {
|
|
2
|
+
constructor(source){
|
|
3
|
+
this.source = source;
|
|
4
|
+
}
|
|
5
|
+
static is(value) {
|
|
6
|
+
return value instanceof HttpStreamBody;
|
|
7
|
+
}
|
|
8
|
+
static fromWebStream(stream) {
|
|
9
|
+
return new HttpStreamBody({
|
|
10
|
+
async *[Symbol.asyncIterator] () {
|
|
11
|
+
const reader = stream.getReader();
|
|
12
|
+
try {
|
|
13
|
+
while(true){
|
|
14
|
+
const { done, value } = await reader.read();
|
|
15
|
+
if (done) break;
|
|
16
|
+
if (void 0 !== value) yield value;
|
|
17
|
+
}
|
|
18
|
+
} finally{
|
|
19
|
+
reader.releaseLock();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async collect() {
|
|
25
|
+
const encoder = new TextEncoder();
|
|
26
|
+
const chunks = [];
|
|
27
|
+
let total = 0;
|
|
28
|
+
for await (const chunk of this.source){
|
|
29
|
+
const bytes = "string" == typeof chunk ? encoder.encode(chunk) : chunk;
|
|
30
|
+
chunks.push(bytes);
|
|
31
|
+
total += bytes.byteLength;
|
|
32
|
+
}
|
|
33
|
+
const result = new Uint8Array(total);
|
|
34
|
+
let offset = 0;
|
|
35
|
+
for (const chunk of chunks){
|
|
36
|
+
result.set(chunk, offset);
|
|
37
|
+
offset += chunk.byteLength;
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export { HttpStreamBody };
|
|
43
|
+
|
|
44
|
+
//# sourceMappingURL=HttpStreamBody.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/http/HttpStreamBody.js","sources":["../../../src/features/http/HttpStreamBody.ts"],"sourcesContent":["export type HttpStreamChunk = Uint8Array | string;\n\nexport type HttpStreamSource = AsyncIterable<HttpStreamChunk>;\n\n/**\n * Marker for a streaming HTTP response body.\n *\n * `IHttpResponse.body` is untyped, and buffered bodies already carry meaning by their runtime type\n * (string, Buffer/Uint8Array, plain object). Streaming has to be distinguishable from those WITHOUT\n * duck-typing `Symbol.asyncIterator`: a plain object body could accidentally satisfy it, and\n * `ReadableStream`'s async-iterator support (present in Node at runtime) isn't declared in the DOM\n * types. So a route opts into streaming explicitly, by wrapping its source in this class.\n *\n * Transports that can stream (the Node HTTP server; AWS Lambda response streaming via a Function\n * URL) write chunks as they are produced. Transports that cannot (API Gateway buffers the entire\n * Lambda response no matter how it was produced) call {@link collect} and send one buffered body —\n * so the same route still works there, just without incremental delivery.\n */\nexport class HttpStreamBody {\n constructor(readonly source: HttpStreamSource) {}\n\n static is(value: unknown): value is HttpStreamBody {\n return value instanceof HttpStreamBody;\n }\n\n /**\n * Wrap a web `ReadableStream` — what `fetch` and the AI SDK's `toUIMessageStreamResponse()`\n * hand back. The reader is driven explicitly rather than relying on async iteration, because\n * the DOM types don't declare it even though Node implements it.\n */\n static fromWebStream(stream: ReadableStream<Uint8Array>): HttpStreamBody {\n return new HttpStreamBody({\n async *[Symbol.asyncIterator]() {\n const reader = stream.getReader();\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n if (value !== undefined) {\n yield value;\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n });\n }\n\n /**\n * Drain the whole stream into a single byte array, for transports that can't stream.\n *\n * Consumes the source — a stream can only be read once, so this must not be combined with\n * writing the same body incrementally.\n */\n async collect(): Promise<Uint8Array> {\n const encoder = new TextEncoder();\n const chunks: Uint8Array[] = [];\n let total = 0;\n\n for await (const chunk of this.source) {\n const bytes = typeof chunk === \"string\" ? encoder.encode(chunk) : chunk;\n chunks.push(bytes);\n total += bytes.byteLength;\n }\n\n const result = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.byteLength;\n }\n\n return result;\n }\n}\n"],"names":["HttpStreamBody","source","value","stream","Symbol","reader","done","undefined","encoder","TextEncoder","chunks","total","chunk","bytes","result","Uint8Array","offset"],"mappings":"AAkBO,MAAMA;IACT,YAAqBC,MAAwB,CAAE;aAA1BA,MAAM,GAANA;IAA2B;IAEhD,OAAO,GAAGC,KAAc,EAA2B;QAC/C,OAAOA,iBAAiBF;IAC5B;IAOA,OAAO,cAAcG,MAAkC,EAAkB;QACrE,OAAO,IAAIH,eAAe;YACtB,OAAO,CAACI,OAAO,aAAa,CAAC;gBACzB,MAAMC,SAASF,OAAO,SAAS;gBAC/B,IAAI;oBACA,MAAO,KAAM;wBACT,MAAM,EAAEG,IAAI,EAAEJ,KAAK,EAAE,GAAG,MAAMG,OAAO,IAAI;wBACzC,IAAIC,MACA;wBAEJ,IAAIJ,AAAUK,WAAVL,OACA,MAAMA;oBAEd;gBACJ,SAAU;oBACNG,OAAO,WAAW;gBACtB;YACJ;QACJ;IACJ;IAQA,MAAM,UAA+B;QACjC,MAAMG,UAAU,IAAIC;QACpB,MAAMC,SAAuB,EAAE;QAC/B,IAAIC,QAAQ;QAEZ,WAAW,MAAMC,SAAS,IAAI,CAAC,MAAM,CAAE;YACnC,MAAMC,QAAQ,AAAiB,YAAjB,OAAOD,QAAqBJ,QAAQ,MAAM,CAACI,SAASA;YAClEF,OAAO,IAAI,CAACG;YACZF,SAASE,MAAM,UAAU;QAC7B;QAEA,MAAMC,SAAS,IAAIC,WAAWJ;QAC9B,IAAIK,SAAS;QACb,KAAK,MAAMJ,SAASF,OAAQ;YACxBI,OAAO,GAAG,CAACF,OAAOI;YAClBA,UAAUJ,MAAM,UAAU;QAC9B;QAEA,OAAOE;IACX;AACJ"}
|