@grest-ts/http 0.0.5
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 +613 -0
- package/dist/src/client/GGHttpSchema.createClient.d.ts +14 -0
- package/dist/src/client/GGHttpSchema.createClient.d.ts.map +1 -0
- package/dist/src/client/GGHttpSchema.createClient.js +80 -0
- package/dist/src/client/GGHttpSchema.createClient.js.map +1 -0
- package/dist/src/index-browser.d.ts +8 -0
- package/dist/src/index-browser.d.ts.map +1 -0
- package/dist/src/index-browser.js +11 -0
- package/dist/src/index-browser.js.map +1 -0
- package/dist/src/index-node.d.ts +18 -0
- package/dist/src/index-node.d.ts.map +1 -0
- package/dist/src/index-node.js +32 -0
- package/dist/src/index-node.js.map +1 -0
- package/dist/src/rpc/GGHttpRouteRPC.d.ts +19 -0
- package/dist/src/rpc/GGHttpRouteRPC.d.ts.map +1 -0
- package/dist/src/rpc/GGHttpRouteRPC.js +32 -0
- package/dist/src/rpc/GGHttpRouteRPC.js.map +1 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestBuilder.d.ts +18 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestBuilder.d.ts.map +1 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestBuilder.js +80 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestBuilder.js.map +1 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestParser.d.ts +18 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestParser.d.ts.map +1 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestParser.js +90 -0
- package/dist/src/rpc/RpcRequest/GGRpcRequestParser.js.map +1 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseBuilder.d.ts +12 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseBuilder.d.ts.map +1 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseBuilder.js +77 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseBuilder.js.map +1 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseParser.d.ts +7 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseParser.d.ts.map +1 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseParser.js +21 -0
- package/dist/src/rpc/RpcResponse/GGRpcResponseParser.js.map +1 -0
- package/dist/src/schema/GGHttpSchema.d.ts +68 -0
- package/dist/src/schema/GGHttpSchema.d.ts.map +1 -0
- package/dist/src/schema/GGHttpSchema.js +18 -0
- package/dist/src/schema/GGHttpSchema.js.map +1 -0
- package/dist/src/schema/httpSchema.d.ts +43 -0
- package/dist/src/schema/httpSchema.d.ts.map +1 -0
- package/dist/src/schema/httpSchema.js +85 -0
- package/dist/src/schema/httpSchema.js.map +1 -0
- package/dist/src/server/GGHttp.d.ts +12 -0
- package/dist/src/server/GGHttp.d.ts.map +1 -0
- package/dist/src/server/GGHttp.js +16 -0
- package/dist/src/server/GGHttp.js.map +1 -0
- package/dist/src/server/GGHttpMetrics.d.ts +22 -0
- package/dist/src/server/GGHttpMetrics.d.ts.map +1 -0
- package/dist/src/server/GGHttpMetrics.js +15 -0
- package/dist/src/server/GGHttpMetrics.js.map +1 -0
- package/dist/src/server/GGHttpSchema.startServer.d.ts +30 -0
- package/dist/src/server/GGHttpSchema.startServer.d.ts.map +1 -0
- package/dist/src/server/GGHttpSchema.startServer.js +114 -0
- package/dist/src/server/GGHttpSchema.startServer.js.map +1 -0
- package/dist/src/server/GGHttpServer.d.ts +32 -0
- package/dist/src/server/GGHttpServer.d.ts.map +1 -0
- package/dist/src/server/GGHttpServer.js +116 -0
- package/dist/src/server/GGHttpServer.js.map +1 -0
- package/dist/src/server/GG_HTTP_REQUEST.d.ts +16 -0
- package/dist/src/server/GG_HTTP_REQUEST.d.ts.map +1 -0
- package/dist/src/server/GG_HTTP_REQUEST.js +10 -0
- package/dist/src/server/GG_HTTP_REQUEST.js.map +1 -0
- package/dist/src/server/GG_HTTP_SERVER.d.ts +4 -0
- package/dist/src/server/GG_HTTP_SERVER.d.ts.map +1 -0
- package/dist/src/server/GG_HTTP_SERVER.js +3 -0
- package/dist/src/server/GG_HTTP_SERVER.js.map +1 -0
- package/dist/src/tsconfig.json +17 -0
- package/dist/testkit/clientHttp/GGHttpCall.d.ts +35 -0
- package/dist/testkit/clientHttp/GGHttpCall.d.ts.map +1 -0
- package/dist/testkit/clientHttp/GGHttpCall.js +37 -0
- package/dist/testkit/clientHttp/GGHttpCall.js.map +1 -0
- package/dist/testkit/clientHttp/GGHttpSchema.callOn.d.ts +37 -0
- package/dist/testkit/clientHttp/GGHttpSchema.callOn.d.ts.map +1 -0
- package/dist/testkit/clientHttp/GGHttpSchema.callOn.js +29 -0
- package/dist/testkit/clientHttp/GGHttpSchema.callOn.js.map +1 -0
- package/dist/testkit/index-testkit.d.ts +8 -0
- package/dist/testkit/index-testkit.d.ts.map +1 -0
- package/dist/testkit/index-testkit.js +8 -0
- package/dist/testkit/index-testkit.js.map +1 -0
- package/dist/testkit/mock/GGHttpInterceptorsServer.d.ts +13 -0
- package/dist/testkit/mock/GGHttpInterceptorsServer.d.ts.map +1 -0
- package/dist/testkit/mock/GGHttpInterceptorsServer.js +100 -0
- package/dist/testkit/mock/GGHttpInterceptorsServer.js.map +1 -0
- package/dist/testkit/mock/GGHttpSchema.mock.d.ts +36 -0
- package/dist/testkit/mock/GGHttpSchema.mock.d.ts.map +1 -0
- package/dist/testkit/mock/GGHttpSchema.mock.js +78 -0
- package/dist/testkit/mock/GGHttpSchema.mock.js.map +1 -0
- package/dist/testkit/routing/GGApiRoutingSelector.d.ts +8 -0
- package/dist/testkit/routing/GGApiRoutingSelector.d.ts.map +1 -0
- package/dist/testkit/routing/GGApiRoutingSelector.js +4 -0
- package/dist/testkit/routing/GGApiRoutingSelector.js.map +1 -0
- package/dist/testkit/routing/GGHttpSchema.routing.d.ts +14 -0
- package/dist/testkit/routing/GGHttpSchema.routing.d.ts.map +1 -0
- package/dist/testkit/routing/GGHttpSchema.routing.js +21 -0
- package/dist/testkit/routing/GGHttpSchema.routing.js.map +1 -0
- package/dist/testkit/utils/validateContractResponse.d.ts +8 -0
- package/dist/testkit/utils/validateContractResponse.d.ts.map +1 -0
- package/dist/testkit/utils/validateContractResponse.js +68 -0
- package/dist/testkit/utils/validateContractResponse.js.map +1 -0
- package/dist/tsconfig.publish.tsbuildinfo +1 -0
- package/package.json +74 -0
- package/src/client/GGHttpSchema.createClient.ts +107 -0
- package/src/index-browser.ts +12 -0
- package/src/index-node.ts +38 -0
- package/src/rpc/GGHttpRouteRPC.ts +42 -0
- package/src/rpc/RpcRequest/GGRpcRequestBuilder.ts +91 -0
- package/src/rpc/RpcRequest/GGRpcRequestParser.ts +100 -0
- package/src/rpc/RpcResponse/GGRpcResponseBuilder.ts +84 -0
- package/src/rpc/RpcResponse/GGRpcResponseParser.ts +23 -0
- package/src/schema/GGHttpSchema.ts +115 -0
- package/src/schema/httpSchema.ts +99 -0
- package/src/server/GGHttp.ts +27 -0
- package/src/server/GGHttpMetrics.ts +31 -0
- package/src/server/GGHttpSchema.startServer.ts +161 -0
- package/src/server/GGHttpServer.ts +133 -0
- package/src/server/GG_HTTP_REQUEST.ts +12 -0
- package/src/server/GG_HTTP_SERVER.ts +4 -0
- package/src/tsconfig.json +17 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ERROR, ERROR_JSON, GGContractApiDefinition, GGContractClass, GGContractMethod, OK } from "@grest-ts/schema";
|
|
2
|
+
import type { HttpMethod } from "@grest-ts/common";
|
|
3
|
+
import type http from "http";
|
|
4
|
+
import type { GGHttpServerMiddleware } from "../server/GGHttpSchema.startServer";
|
|
5
|
+
export declare class GGHttpSchema<TContract extends GGContractApiDefinition, TContext> {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly pathPrefix: string;
|
|
8
|
+
readonly apiMiddlewares: readonly GGHttpTransportMiddleware[];
|
|
9
|
+
readonly codec: Record<keyof TContract, GGHttpCodec>;
|
|
10
|
+
readonly contract: GGContractClass<TContract> | null;
|
|
11
|
+
constructor(pathPrefix: string, contract: GGContractClass<TContract>, wireCodec: Record<keyof TContract, GGHttpCodec>, middlewares?: readonly GGHttpTransportMiddleware[]);
|
|
12
|
+
}
|
|
13
|
+
export interface ClientHttpRouteToRpcTransformClientConfig {
|
|
14
|
+
pathPrefix: string;
|
|
15
|
+
contract: GGContractMethod;
|
|
16
|
+
middlewares: readonly GGHttpTransportMiddleware[];
|
|
17
|
+
}
|
|
18
|
+
export interface ClientHttpRouteToRpcTransformClientCodec {
|
|
19
|
+
createRequest: (data: unknown) => GGHttpFetchRequest | Promise<GGHttpFetchRequest>;
|
|
20
|
+
parseResponse: (response: Response) => Promise<OK<unknown> | ERROR_JSON<string, unknown>>;
|
|
21
|
+
}
|
|
22
|
+
export interface GGHttpFetchRequest {
|
|
23
|
+
url: string;
|
|
24
|
+
method: HttpMethod;
|
|
25
|
+
headers: Record<string, string>;
|
|
26
|
+
body: string | FormData | undefined;
|
|
27
|
+
}
|
|
28
|
+
export interface ClientHttpRouteToRpcTransformServerConfig {
|
|
29
|
+
contract: GGContractMethod;
|
|
30
|
+
apiMiddlewares: readonly GGHttpTransportMiddleware[];
|
|
31
|
+
serverMiddlewares: readonly GGHttpServerMiddleware[];
|
|
32
|
+
}
|
|
33
|
+
export interface ClientHttpRouteToRpcTransformServerCodec {
|
|
34
|
+
parseRequest: (req: http.IncomingMessage) => Promise<unknown>;
|
|
35
|
+
sendResponse: (res: http.ServerResponse, rpcResult: ERROR<string, unknown> | OK<unknown>) => Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
export interface GGHttpCodec {
|
|
38
|
+
readonly method: HttpMethod;
|
|
39
|
+
readonly path: string;
|
|
40
|
+
createForClient(config: ClientHttpRouteToRpcTransformClientConfig): ClientHttpRouteToRpcTransformClientCodec;
|
|
41
|
+
createForServer(config: ClientHttpRouteToRpcTransformServerConfig): ClientHttpRouteToRpcTransformServerCodec;
|
|
42
|
+
}
|
|
43
|
+
export interface GGHttpTransportMiddleware {
|
|
44
|
+
/**
|
|
45
|
+
* Client-side: modify outgoing request (add headers, etc.)
|
|
46
|
+
*/
|
|
47
|
+
updateRequest?(req: GGHttpRequest): void;
|
|
48
|
+
/**
|
|
49
|
+
* Server-side: parse incoming request (extract context from headers, etc.)
|
|
50
|
+
*/
|
|
51
|
+
parseRequest?(req: GGHttpRequest): void;
|
|
52
|
+
/**
|
|
53
|
+
* Server-side: modify outgoing response
|
|
54
|
+
*/
|
|
55
|
+
updateResponse?(res: GGHttpResponse): void;
|
|
56
|
+
/**
|
|
57
|
+
* Client-side: parse incoming response
|
|
58
|
+
*/
|
|
59
|
+
parseResponse?(res: GGHttpResponse): void;
|
|
60
|
+
}
|
|
61
|
+
export interface GGHttpRequest {
|
|
62
|
+
headers?: Record<string, string | string[]>;
|
|
63
|
+
queryArgs?: Record<string, string | string[]>;
|
|
64
|
+
}
|
|
65
|
+
export interface GGHttpResponse {
|
|
66
|
+
headers: Record<string, string | string[]>;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=GGHttpSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpSchema.d.ts","sourceRoot":"","sources":["../../../src/schema/GGHttpSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,UAAU,EAAE,uBAAuB,EAAE,eAAe,EAAE,gBAAgB,EAAE,EAAE,EAAC,MAAM,kBAAkB,CAAC;AACnH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAC,sBAAsB,EAAC,MAAM,oCAAoC,CAAC;AAE/E,qBAAa,YAAY,CAAC,SAAS,SAAS,uBAAuB,EAAE,QAAQ;IAEzE,SAAgB,IAAI,EAAE,MAAM,CAAA;IAC5B,SAAgB,UAAU,EAAE,MAAM,CAAA;IAClC,SAAgB,cAAc,EAAE,SA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athPrefix, contract, wireCodec, middlewares = []) {
|
|
8
|
+
this.name = contract.name;
|
|
9
|
+
this.pathPrefix = pathPrefix;
|
|
10
|
+
this.apiMiddlewares = middlewares;
|
|
11
|
+
this.codec = wireCodec;
|
|
12
|
+
this.contract = contract;
|
|
13
|
+
Object.freeze(this.apiMiddlewares);
|
|
14
|
+
Object.freeze(this.codec);
|
|
15
|
+
Object.freeze(this);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=GGHttpSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpSchema.js","sourceRoot":"","sources":["../../../src/schema/GGHttpSchema.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,YAAY;IAEL,IAAI,CAAQ;IACZ,UAAU,CAAQ;IAClB,cAAc,CAAsC;IACpD,KAAK,CAAsC;IAC3C,QAAQ,GAAsC,IAAI,CAAA;IAElE,YACI,UAAkB,EAClB,QAAoC,EACpC,SAA+C,EAC/C,cAAoD,EAAE;QAEtD,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAA;QACzB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,cAAc,GAAG,WAAW,CAAA;QACjC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAA;QACtB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QAClC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACzB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACvB,CAAC;CACJ"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { GGHttpCodec, GGHttpSchema } from "./GGHttpSchema";
|
|
2
|
+
import { GGHttpTransportMiddleware } from "./GGHttpSchema";
|
|
3
|
+
import { GGContractApiDefinition, GGContractClass } from "@grest-ts/schema";
|
|
4
|
+
import { GGContextKey } from "@grest-ts/context";
|
|
5
|
+
/**
|
|
6
|
+
* Create an HTTP API schema builder from a contract.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* export const MyApiContract = defineApi("MyApi", () => ({
|
|
10
|
+
* list: {
|
|
11
|
+
* success: IsArray(IsItem),
|
|
12
|
+
* errors: [NOT_AUTHORIZED, SERVER_ERROR]
|
|
13
|
+
* },
|
|
14
|
+
* create: {
|
|
15
|
+
* input: IsCreateRequest,
|
|
16
|
+
* success: IsItem,
|
|
17
|
+
* errors: [NOT_AUTHORIZED, VALIDATION_ERROR, SERVER_ERROR]
|
|
18
|
+
* }
|
|
19
|
+
* }))
|
|
20
|
+
*
|
|
21
|
+
* export const MyApi = httpApi(MyApiContract)
|
|
22
|
+
* .pathPrefix("api/items")
|
|
23
|
+
* .use(AuthMiddleware)
|
|
24
|
+
* .routes({
|
|
25
|
+
* list: GGRpc.GET("list"),
|
|
26
|
+
* create: GGRpc.POST("create")
|
|
27
|
+
* })
|
|
28
|
+
*/
|
|
29
|
+
export declare function httpSchema<TContract extends GGContractApiDefinition>(contract: GGContractClass<TContract>): GGHttpSchemaBuilder<TContract>;
|
|
30
|
+
declare class GGHttpSchemaBuilder<TContract extends GGContractApiDefinition, TContext = undefined> {
|
|
31
|
+
private readonly _contract;
|
|
32
|
+
private _pathPrefix;
|
|
33
|
+
private _middlewares;
|
|
34
|
+
constructor(contract: GGContractClass<TContract>);
|
|
35
|
+
pathPrefix(prefix: string): this;
|
|
36
|
+
use<M extends GGHttpTransportMiddleware>(middleware: M): GGHttpSchemaBuilder<TContract, TContext | M>;
|
|
37
|
+
useHeader<Input>(contextKey: GGContextKey<Input>): GGHttpSchemaBuilder<TContract, TContext | Input>;
|
|
38
|
+
routes(mapping: {
|
|
39
|
+
[K in keyof TContract]: GGHttpCodec;
|
|
40
|
+
}): GGHttpSchema<TContract, TContext>;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
43
|
+
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
import { GGHttpSchema } from "./GGHttpSchema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create an HTTP API schema builder from a contract.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* export const MyApiContract = defineApi("MyApi", () => ({
|
|
7
|
+
* list: {
|
|
8
|
+
* success: IsArray(IsItem),
|
|
9
|
+
* errors: [NOT_AUTHORIZED, SERVER_ERROR]
|
|
10
|
+
* },
|
|
11
|
+
* create: {
|
|
12
|
+
* input: IsCreateRequest,
|
|
13
|
+
* success: IsItem,
|
|
14
|
+
* errors: [NOT_AUTHORIZED, VALIDATION_ERROR, SERVER_ERROR]
|
|
15
|
+
* }
|
|
16
|
+
* }))
|
|
17
|
+
*
|
|
18
|
+
* export const MyApi = httpApi(MyApiContract)
|
|
19
|
+
* .pathPrefix("api/items")
|
|
20
|
+
* .use(AuthMiddleware)
|
|
21
|
+
* .routes({
|
|
22
|
+
* list: GGRpc.GET("list"),
|
|
23
|
+
* create: GGRpc.POST("create")
|
|
24
|
+
* })
|
|
25
|
+
*/
|
|
26
|
+
export function httpSchema(contract) {
|
|
27
|
+
return new GGHttpSchemaBuilder(contract);
|
|
28
|
+
}
|
|
29
|
+
class GGHttpSchemaBuilder {
|
|
30
|
+
_contract;
|
|
31
|
+
_pathPrefix = "";
|
|
32
|
+
_middlewares = [];
|
|
33
|
+
constructor(contract) {
|
|
34
|
+
this._contract = contract;
|
|
35
|
+
}
|
|
36
|
+
pathPrefix(prefix) {
|
|
37
|
+
this._pathPrefix = prefix;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
use(middleware) {
|
|
41
|
+
this._middlewares.push(middleware);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
useHeader(contextKey) {
|
|
45
|
+
const codec = contextKey.getCodec("http");
|
|
46
|
+
if (!codec) {
|
|
47
|
+
throw new Error(`Context key '${contextKey.name}' does not have an 'http-header' codec registered.`);
|
|
48
|
+
}
|
|
49
|
+
const middleware = {
|
|
50
|
+
updateRequest(req) {
|
|
51
|
+
// Client-side: context -> headers
|
|
52
|
+
const contextValue = contextKey.get();
|
|
53
|
+
if (contextValue !== undefined) {
|
|
54
|
+
const result = codec.decode(contextValue);
|
|
55
|
+
if (result.success) {
|
|
56
|
+
Object.assign(req.headers, result.value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Clear any default headers by decoding an empty context
|
|
61
|
+
const emptyResult = codec.decode({});
|
|
62
|
+
if (emptyResult.success) {
|
|
63
|
+
for (const key of Object.keys(emptyResult.value)) {
|
|
64
|
+
delete req.headers[key];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
parseRequest(req) {
|
|
70
|
+
// Server-side: headers -> context
|
|
71
|
+
const headers = req.headers;
|
|
72
|
+
const result = codec.encode(headers);
|
|
73
|
+
if (result.success) {
|
|
74
|
+
contextKey.set(result.value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
this._middlewares.push(middleware);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
routes(mapping) {
|
|
82
|
+
return new GGHttpSchema(this._pathPrefix, this._contract, mapping, this._middlewares);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=httpSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"httpSchema.js","sourceRoot":"","sources":["../../../src/schema/httpSchema.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,UAAU,CACtB,QAAoC;IAEpC,OAAO,IAAI,mBAAmB,CAAC,QAAQ,CAAC,CAAA;AAC5C,CAAC;AAED,MAAM,mBAAmB;IAEJ,SAAS,CAA4B;IAC9C,WAAW,GAAW,EAAE,CAAA;IACxB,YAAY,GAAgC,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AAC;gBACL,CAAC;YACL,CAAC;YACD,YAAY,CAAC,GAAyB;gBAClC,kCAAkC;gBAClC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAiC,CAAC;gBACtD,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBACrC,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;oBACjB,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACjC,CAAC;YACL,CAAC;SACJ,CAAC;QAEF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACnC,OAAO,IAAmE,CAAC;IAC/E,CAAC;IAED,MAAM,CAAC,OAAgD;QACnD,OAAO,IAAI,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;IACzF,CAAC;CACJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GGHttpSchema } from "../schema/GGHttpSchema";
|
|
2
|
+
import { GGContractApiDefinition, GGContractImplementation } from "@grest-ts/schema";
|
|
3
|
+
import { GGHttpServerMiddleware } from "./GGHttpSchema.startServer";
|
|
4
|
+
import { GGHttpServer } from "./GGHttpServer";
|
|
5
|
+
export declare class GGHttp<TContext = undefined> {
|
|
6
|
+
private readonly httpServer;
|
|
7
|
+
private readonly middlewares;
|
|
8
|
+
constructor(httpServer: GGHttpServer);
|
|
9
|
+
use<M extends GGHttpServerMiddleware>(middleware: M): GGHttp<TContext | M>;
|
|
10
|
+
http<TContract extends GGContractApiDefinition, TSchemaContext>(schema: GGHttpSchema<TContract, TSchemaContext>, implementation: GGContractImplementation<TContract>): this;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=GGHttp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttp.d.ts","sourceRoot":"","sources":["../../../src/server/GGHttp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAC,uBAAuB,EAAE,wBAAwB,EAAC,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAC,sBAAsB,EAAC,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,qBAAa,MAAM,CAAC,QAAQ,GAAG,SAAS;IAEpC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAc;IACzC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAgC;gBAEhD,UAAU,EAAE,YAAY;IAI7B,GAAG,CAAC,CAAC,SAAS,sBAAsB,EAAE,UAAU,EAAE,CAAC,GAAG,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC;IAK1E,IAAI,CAAC,SAAS,SAAS,uBAAuB,EAAE,cAAc,EACjE,MAAM,EAAE,YAAY,CAAC,SAAS,EAAE,cAAc,CAAC,EAC/C,cAAc,EAAE,wBAAwB,CAAC,SAAS,CAAC,GACpD,IAAI;CAIV"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class GGHttp {
|
|
2
|
+
httpServer;
|
|
3
|
+
middlewares = [];
|
|
4
|
+
constructor(httpServer) {
|
|
5
|
+
this.httpServer = httpServer;
|
|
6
|
+
}
|
|
7
|
+
use(middleware) {
|
|
8
|
+
this.middlewares.push(middleware);
|
|
9
|
+
return this;
|
|
10
|
+
}
|
|
11
|
+
http(schema, implementation) {
|
|
12
|
+
schema.register(implementation, { http: this.httpServer, middlewares: this.middlewares });
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=GGHttp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttp.js","sourceRoot":"","sources":["../../../src/server/GGHttp.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,MAAM;IAEE,UAAU,CAAc;IACxB,WAAW,GAA6B,EAAE,CAAC;IAE5D,YAAY,UAAwB;QAChC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;IAEM,GAAG,CAAmC,UAAa;QACtD,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAClC,OAAO,IAAW,CAAC;IACvB,CAAC;IAEM,IAAI,CACP,MAA+C,EAC/C,cAAmD;QAEnD,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAC,IAAI,EAAE,IAAI,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAC,CAAC,CAAC;QACxF,OAAO,IAAW,CAAC;IACvB,CAAC;CACJ"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { GGCounterKey, GGHistogramKey } from "@grest-ts/metrics";
|
|
2
|
+
import { EXISTS, FORBIDDEN, NOT_AUTHORIZED, NOT_FOUND, OK, ROUTE_NOT_FOUND, SERVER_ERROR, VALIDATION_ERROR } from "@grest-ts/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Result type for HTTP operations.
|
|
5
|
+
* Known types listed for documentation, but accepts any string for custom error types.
|
|
6
|
+
*/
|
|
7
|
+
type ResultType = typeof OK.TYPE | typeof VALIDATION_ERROR.TYPE | typeof NOT_AUTHORIZED.TYPE | typeof FORBIDDEN.TYPE | typeof NOT_FOUND.TYPE | typeof ROUTE_NOT_FOUND.TYPE | typeof EXISTS.TYPE | typeof SERVER_ERROR.TYPE | string;
|
|
8
|
+
export declare const GGHttpMetrics: import("@grest-ts/metrics").GGMetricsDefinition<{
|
|
9
|
+
requests: GGCounterKey<{
|
|
10
|
+
api: string;
|
|
11
|
+
method: string;
|
|
12
|
+
path: string;
|
|
13
|
+
result: ResultType;
|
|
14
|
+
}>;
|
|
15
|
+
requestDuration: GGHistogramKey<{
|
|
16
|
+
api: string;
|
|
17
|
+
method: string;
|
|
18
|
+
path: string;
|
|
19
|
+
}>;
|
|
20
|
+
}>;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=GGHttpMetrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpMetrics.d.ts","sourceRoot":"","sources":["../../../src/server/GGHttpMetrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,cAAc,EAAY,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAC,MAAM,EAAE,SAAS,EAAE,cAAc,EAAE,SAAS,EAAE,EAAE,EAAE,eAAe,EAAE,YAAY,EAAE,gBAAgB,EAAC,MAAM,kBAAkB,CAAC;AAEnI;;;GAGG;AACH,KAAK,UAAU,GACT,OAAO,EAAE,CAAC,IAAI,GACd,OAAO,gBAAgB,CAAC,IAAI,GAC5B,OAAO,cAAc,CAAC,IAAI,GAC1B,OAAO,SAAS,CAAC,IAAI,GACrB,OAAO,SAAS,CAAC,IAAI,GACrB,OAAO,eAAe,CAAC,IAAI,GAC3B,OAAO,MAAM,CAAC,IAAI,GAClB,OAAO,YAAY,CAAC,IAAI,GACxB,MAAM,CAAC;AAEb,eAAO,MAAM,aAAa;;aACY,MAAM;gBAAU,MAAM;cAAQ,MAAM;gBAAU,UAAU;;;aAK/C,MAAM;gBAAU,MAAM;cAAQ,MAAM;;EAMhF,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GGCounterKey, GGHistogramKey, GGMetrics } from "@grest-ts/metrics";
|
|
2
|
+
export const GGHttpMetrics = GGMetrics.define('/http/', () => ({
|
|
3
|
+
requests: new GGCounterKey('requests_total', {
|
|
4
|
+
help: 'Total HTTP requests after contract validation',
|
|
5
|
+
labelNames: ['api', 'method', 'path', 'result'],
|
|
6
|
+
groupBy: { labels: ["api", "method"], template: "{api}.{method}" }
|
|
7
|
+
}),
|
|
8
|
+
requestDuration: new GGHistogramKey('request_duration_ms', {
|
|
9
|
+
help: 'HTTP request duration in milliseconds',
|
|
10
|
+
labelNames: ['api', 'method', 'path'],
|
|
11
|
+
buckets: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000],
|
|
12
|
+
groupBy: { labels: ["api", "method"], template: "{api}.{method}" }
|
|
13
|
+
}),
|
|
14
|
+
}));
|
|
15
|
+
//# sourceMappingURL=GGHttpMetrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpMetrics.js","sourceRoot":"","sources":["../../../src/server/GGHttpMetrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAkB1E,MAAM,CAAC,MAAM,aAAa,GAAG,SAAS,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC;IAC3D,QAAQ,EAAE,IAAI,YAAY,CAAoE,gBAAgB,EAAE;QAC5G,IAAI,EAAE,+CAA+C;QACrD,UAAU,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC;QAC/C,OAAO,EAAE,EAAC,MAAM,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAC;KACnE,CAAC;IACF,eAAe,EAAE,IAAI,cAAc,CAAgD,qBAAqB,EAAE;QACtG,IAAI,EAAE,uCAAuC;QAC7C,UAAU,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC;QACrC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;QACzD,OAAO,EAAE,EAAC,MAAM,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAC;KACnE,CAAC;CACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server extension for HttpApiSchema - adds register method.
|
|
3
|
+
* This file should only be imported in server (Node.js) context.
|
|
4
|
+
*/
|
|
5
|
+
import { GGContractApiDefinition, GGContractImplementation } from "@grest-ts/schema";
|
|
6
|
+
import { GGHttpServer } from "./GGHttpServer";
|
|
7
|
+
export interface GGHttpSchemaConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The HTTP request handler to register with.
|
|
10
|
+
*/
|
|
11
|
+
http?: GGHttpServer;
|
|
12
|
+
/**
|
|
13
|
+
* Additional middlewares to apply to all routes.
|
|
14
|
+
*/
|
|
15
|
+
middlewares?: GGHttpServerMiddleware[];
|
|
16
|
+
}
|
|
17
|
+
export interface GGHttpServerMiddleware {
|
|
18
|
+
process?(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
declare module "../schema/GGHttpSchema" {
|
|
21
|
+
interface GGHttpSchema<TContract extends GGContractApiDefinition> {
|
|
22
|
+
/**
|
|
23
|
+
* Start server with direct implementation.
|
|
24
|
+
* Uses parseRequest from use classes without transform.
|
|
25
|
+
* For custom transforms, use createServer() instead.
|
|
26
|
+
*/
|
|
27
|
+
register(implementation: GGContractImplementation<TContract>, config?: GGHttpSchemaConfig): void;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=GGHttpSchema.startServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpSchema.startServer.d.ts","sourceRoot":"","sources":["../../../src/server/GGHttpSchema.startServer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAQ,uBAAuB,EAAE,wBAAwB,EAAqC,MAAM,kBAAkB,CAAC;AAS9H,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAG5C,MAAM,WAAW,kBAAkB;IAC/B;;OAEG;IACH,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB;;OAEG;IACH,WAAW,CAAC,EAAE,sBAAsB,EAAE,CAAC;CAC1C;AAED,MAAM,WAAW,sBAAsB;IACnC,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5B;AAED,OAAO,QAAQ,wBAAwB,CAAC;IACpC,UAAU,YAAY,CAAC,SAAS,SAAS,uBAAuB;QAC5D;;;;WAIG;QACH,QAAQ,CAAC,cAAc,EAAE,wBAAwB,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAA;KACnG;CACJ"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server extension for HttpApiSchema - adds register method.
|
|
3
|
+
* This file should only be imported in server (Node.js) context.
|
|
4
|
+
*/
|
|
5
|
+
import { GGLocator } from "@grest-ts/locator";
|
|
6
|
+
import { GGHttpSchema } from "../schema/GGHttpSchema.js";
|
|
7
|
+
import { ERROR, SERVER_ERROR } from "@grest-ts/schema";
|
|
8
|
+
import { GG_DISCOVERY } from "@grest-ts/discovery";
|
|
9
|
+
import { GGContext, GGContextStore } from "@grest-ts/context";
|
|
10
|
+
import { GG_TRACE } from "@grest-ts/trace";
|
|
11
|
+
import { GG_HTTP_REQUEST } from "./GG_HTTP_REQUEST.js";
|
|
12
|
+
import { GG_METRICS } from "@grest-ts/metrics";
|
|
13
|
+
import { GGHttpMetrics } from "./GGHttpMetrics.js";
|
|
14
|
+
import { GG_HTTP_SERVER } from "./GG_HTTP_SERVER.js";
|
|
15
|
+
import { GGLog } from "@grest-ts/logger";
|
|
16
|
+
GGHttpSchema.prototype.register = function (implementation, config) {
|
|
17
|
+
return setupRoutes(this, implementation, config);
|
|
18
|
+
};
|
|
19
|
+
function setupRoutes(httpSchema, implementation, config) {
|
|
20
|
+
config ??= {};
|
|
21
|
+
config.middlewares ??= [];
|
|
22
|
+
if (!httpSchema.contract)
|
|
23
|
+
throw new Error(`HttpApiSchema "${httpSchema.name}" has no contract.`);
|
|
24
|
+
const server = config.http ?? GGLocator.getScope().get(GG_HTTP_SERVER);
|
|
25
|
+
if (!server)
|
|
26
|
+
throw new Error(`No HTTP server found. Make sure to register GGHttpServerAdapter in the scope or pass handler via config`);
|
|
27
|
+
const pathPrefix = "/" + httpSchema.pathPrefix + "/";
|
|
28
|
+
const apiMiddlewares = httpSchema.apiMiddlewares;
|
|
29
|
+
const scope = GGLocator.getScope();
|
|
30
|
+
const parentContext = GGContextStore.tryGetContext();
|
|
31
|
+
server.onStart(() => {
|
|
32
|
+
GG_DISCOVERY.tryGet()?.registerRoutes([{
|
|
33
|
+
runtime: scope.serviceName,
|
|
34
|
+
api: httpSchema.name,
|
|
35
|
+
pathPrefix: pathPrefix,
|
|
36
|
+
protocol: "http",
|
|
37
|
+
port: server.port
|
|
38
|
+
}]);
|
|
39
|
+
});
|
|
40
|
+
// Info: Contract instance is auto-registered in GGLocator via patched implement()
|
|
41
|
+
// @TODO This is not really used and only for testkit so it would register implementation of the contract... Not cool
|
|
42
|
+
httpSchema.contract.implement(implementation);
|
|
43
|
+
for (const methodName in httpSchema.codec) {
|
|
44
|
+
// Wire format.
|
|
45
|
+
const codec = httpSchema.codec[methodName];
|
|
46
|
+
if (!codec)
|
|
47
|
+
throw new Error(`Contract for "${httpSchema.name}.${methodName}" is missing wire format!`);
|
|
48
|
+
// Implementation function
|
|
49
|
+
const implFn = implementation[methodName]?.bind(implementation);
|
|
50
|
+
if (!implFn)
|
|
51
|
+
throw new Error(`Implementation for "${httpSchema.name}.${methodName}" is missing implementation method!`);
|
|
52
|
+
// Input schema
|
|
53
|
+
const contractFunctionSchema = httpSchema.contract.methods[methodName];
|
|
54
|
+
if (!contractFunctionSchema)
|
|
55
|
+
throw new Error(`Contract for "${httpSchema.name}.${methodName}" is missing contract function schema!`);
|
|
56
|
+
// build request mapping
|
|
57
|
+
const requestParser = codec.createForServer({
|
|
58
|
+
contract: contractFunctionSchema,
|
|
59
|
+
apiMiddlewares: apiMiddlewares,
|
|
60
|
+
serverMiddlewares: config.middlewares
|
|
61
|
+
});
|
|
62
|
+
server.registerRoute(codec.method, pathPrefix + codec.path, async (req, res) => {
|
|
63
|
+
scope.ensureEntered();
|
|
64
|
+
return new GGContext("REQ", parentContext).run(async () => {
|
|
65
|
+
GG_TRACE.init();
|
|
66
|
+
GG_HTTP_REQUEST.set({ port: server.port, method: req.method, path: req.url });
|
|
67
|
+
const startTime = performance.now();
|
|
68
|
+
let rpcResult;
|
|
69
|
+
try {
|
|
70
|
+
const rpcInput = await requestParser.parseRequest(req);
|
|
71
|
+
try {
|
|
72
|
+
rpcResult = { success: true, type: "OK", data: await implFn(rpcInput) };
|
|
73
|
+
// GGLog.debug(httpSchema, "Response", rpcResult) // This is very slow to log this (like 3x performance loss)
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
rpcResult = ERROR.fromUnknown(error);
|
|
77
|
+
if (rpcResult instanceof SERVER_ERROR) {
|
|
78
|
+
GGLog.error(httpSchema, rpcResult);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// GGLog.debug(httpSchema, "Response", rpcResult) // This is very slow to log this (like 3x performance loss)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
await requestParser.sendResponse(res, rpcResult); // can throw
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
rpcResult = ERROR.fromUnknown(error);
|
|
88
|
+
await requestParser.sendResponse(res, rpcResult); // Does not throw
|
|
89
|
+
GGLog.error(httpSchema, rpcResult);
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
metrics(rpcResult?.type, httpSchema.name, codec.method, pathPrefix, codec.path, startTime);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function metrics(resultType, apiName, method, pathPrefix, pathSuffix, startTime) {
|
|
99
|
+
if (GG_METRICS.has()) {
|
|
100
|
+
const path = method + " " + pathPrefix + pathSuffix;
|
|
101
|
+
GGHttpMetrics.requests.inc(1, {
|
|
102
|
+
api: apiName,
|
|
103
|
+
method: pathSuffix,
|
|
104
|
+
path: path,
|
|
105
|
+
result: resultType
|
|
106
|
+
});
|
|
107
|
+
GGHttpMetrics.requestDuration.observe(performance.now() - startTime, {
|
|
108
|
+
api: apiName,
|
|
109
|
+
method: pathSuffix,
|
|
110
|
+
path: path
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=GGHttpSchema.startServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpSchema.startServer.js","sourceRoot":"","sources":["../../../src/server/GGHttpSchema.startServer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAC,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAwD,YAAY,EAAC,MAAM,wBAAwB,CAAC;AAC3G,OAAO,EAAC,KAAK,EAA2E,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAE9H,OAAO,EAAC,YAAY,EAAC,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAC,SAAS,EAAE,cAAc,EAAC,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAC,QAAQ,EAAC,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAC,UAAU,EAAC,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAEhD,OAAO,EAAC,KAAK,EAAC,MAAM,kBAAkB,CAAC;AA4BvC,YAAY,CAAC,SAAS,CAAC,QAAQ,GAAG,UAE9B,cAAmD,EACnD,MAA2B;IAE3B,OAAO,WAAW,CAAC,IAAI,EAAE,cAAc,EAAE,MAAM,CAAC,CAAA;AACpD,CAAC,CAAA;AAED,SAAS,WAAW,CAChB,UAAmC,EACnC,cAAmD,EACnD,MAA2B;IAE3B,MAAM,KAAK,EAAE,CAA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;QACpD,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE;YAC1B,GAAG,EAAE,OAAO;YACZ,MAAM,EAAE,UAAU;YAClB,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,UAAU;SACrB,CAAC,CAAC;QACH,aAAa,CAAC,eAAe,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE;YACjE,GAAG,EAAE,OAAO;YACZ,MAAM,EAAE,UAAU;YAClB,IAAI,EAAE,IAAI;SACb,CAAC,CAAC;IACP,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import http from "http";
|
|
2
|
+
import { HttpMethod } from "@grest-ts/common";
|
|
3
|
+
import { GGLocatorKey, GGLocatorScope } from "@grest-ts/locator";
|
|
4
|
+
export interface GGHttpServerAdapterConfig {
|
|
5
|
+
key?: GGLocatorKey<GGHttpServer>;
|
|
6
|
+
port?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class GGHttpServer {
|
|
9
|
+
protected _port: number | undefined;
|
|
10
|
+
protected teardownPromise: Promise<void> | undefined;
|
|
11
|
+
protected readonly scope: GGLocatorScope;
|
|
12
|
+
protected readonly runtimeName: string;
|
|
13
|
+
private readonly configuredPort;
|
|
14
|
+
private readonly _onStart;
|
|
15
|
+
private readonly _onTeardown;
|
|
16
|
+
readonly httpServer: http.Server;
|
|
17
|
+
private activeRequests;
|
|
18
|
+
private router;
|
|
19
|
+
constructor(config?: GGHttpServerAdapterConfig);
|
|
20
|
+
/**
|
|
21
|
+
* Current port the server is listening to.
|
|
22
|
+
* May be undefined before start() is called.
|
|
23
|
+
*/
|
|
24
|
+
get port(): number | undefined;
|
|
25
|
+
registerRoute(method: HttpMethod, path: string, handler: GGHttpRequestCallback): void;
|
|
26
|
+
start(): Promise<void>;
|
|
27
|
+
teardown(): Promise<void>;
|
|
28
|
+
onStart(callback: () => Promise<void> | void): this;
|
|
29
|
+
onTeardown(callback: () => Promise<void> | void): this;
|
|
30
|
+
}
|
|
31
|
+
export type GGHttpRequestCallback = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
|
|
32
|
+
//# sourceMappingURL=GGHttpServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GGHttpServer.d.ts","sourceRoot":"","sources":["../../../src/server/GGHttpServer.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAC,UAAU,EAAiB,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAY,YAAY,EAAE,cAAc,EAAuB,MAAM,mBAAmB,CAAC;AAKhG,MAAM,WAAW,yBAAyB;IACtC,GAAG,CAAC,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,YAAY;IAErB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IACrD,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IACzC,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IACvC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IAExC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyB;IAClD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAyB;IAErD,SAAgB,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;IACxC,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,MAAM,CAAyC;gBAE3C,MAAM,CAAC,EAAE,yBAAyB;IAgD9C;;;OAGG;IACH,IAAI,IAAI,IAAI,MAAM,GAAG,SAAS,CAE7B;IAMM,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB,GAAG,IAAI;IAI/E,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IActB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAgB/B,OAAO,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,IAAI;IAKnD,UAAU,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,IAAI;CAIhE;AAED,MAAM,MAAM,qBAAqB,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import http from "http";
|
|
2
|
+
import { HttpStatusCode } from "@grest-ts/common";
|
|
3
|
+
import { GGLocator, GGLocatorServiceType } from "@grest-ts/locator";
|
|
4
|
+
import { GG_HTTP_SERVER } from "./GG_HTTP_SERVER.js";
|
|
5
|
+
import { GGLog } from "@grest-ts/logger";
|
|
6
|
+
import findMyWay from "find-my-way";
|
|
7
|
+
export class GGHttpServer {
|
|
8
|
+
_port;
|
|
9
|
+
teardownPromise;
|
|
10
|
+
scope;
|
|
11
|
+
runtimeName;
|
|
12
|
+
configuredPort;
|
|
13
|
+
_onStart = [];
|
|
14
|
+
_onTeardown = [];
|
|
15
|
+
httpServer;
|
|
16
|
+
activeRequests = 0;
|
|
17
|
+
router = findMyWay();
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.runtimeName = GGLocator.getScope().serviceName;
|
|
20
|
+
this.scope = GGLocator.getScope();
|
|
21
|
+
this.configuredPort = config?.port ?? (process.env.PORT ? Number(process.env.PORT) : 0);
|
|
22
|
+
GGLocator.getScope().setWithLifecycle(config?.key ?? GG_HTTP_SERVER, this, {
|
|
23
|
+
type: GGLocatorServiceType.HTTP,
|
|
24
|
+
start: this.start.bind(this),
|
|
25
|
+
teardown: this.teardown.bind(this)
|
|
26
|
+
});
|
|
27
|
+
this.httpServer = http.createServer(async (req, res) => {
|
|
28
|
+
if (this.teardownPromise) {
|
|
29
|
+
res.statusCode = HttpStatusCode.ServerTemporarilyNotAvailable503;
|
|
30
|
+
res.end();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this.activeRequests++;
|
|
34
|
+
try {
|
|
35
|
+
if (req.headers.origin) { // For browsers
|
|
36
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
37
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
|
|
38
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-company-auth'); // @TODO Remove x-company-auth
|
|
39
|
+
res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition');
|
|
40
|
+
}
|
|
41
|
+
if (req.method === 'OPTIONS') {
|
|
42
|
+
res.writeHead(204);
|
|
43
|
+
res.end();
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const resolved = this.router.find(req.method, req.url);
|
|
47
|
+
if (resolved) {
|
|
48
|
+
await resolved.handler(req, res);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
res.writeHead(404);
|
|
52
|
+
res.end();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
GGLog.error(this, error);
|
|
58
|
+
res.writeHead(500);
|
|
59
|
+
res.end();
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
this.activeRequests--;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Current port the server is listening to.
|
|
68
|
+
* May be undefined before start() is called.
|
|
69
|
+
*/
|
|
70
|
+
get port() {
|
|
71
|
+
return this._port;
|
|
72
|
+
}
|
|
73
|
+
// =========================================================================
|
|
74
|
+
// Common implementation
|
|
75
|
+
// =========================================================================
|
|
76
|
+
registerRoute(method, path, handler) {
|
|
77
|
+
this.router.on(method, path, handler);
|
|
78
|
+
}
|
|
79
|
+
async start() {
|
|
80
|
+
this._port = await new Promise((resolve) => {
|
|
81
|
+
this.httpServer.listen(this.configuredPort, '0.0.0.0', () => {
|
|
82
|
+
const port = this.httpServer.address().port;
|
|
83
|
+
resolve(port);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
this._onStart.forEach(callback => callback());
|
|
87
|
+
GGLog.info(this, `HTTP server started`, {
|
|
88
|
+
port: this._port,
|
|
89
|
+
runtime: this.runtimeName
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
async teardown() {
|
|
93
|
+
if (this.teardownPromise) {
|
|
94
|
+
return this.teardownPromise;
|
|
95
|
+
}
|
|
96
|
+
return this.teardownPromise = (async () => {
|
|
97
|
+
await Promise.all(this._onTeardown.map(callback => callback()));
|
|
98
|
+
this.httpServer.close();
|
|
99
|
+
const maxWaitTime = 3000;
|
|
100
|
+
const deadline = performance.now() + maxWaitTime;
|
|
101
|
+
while (this.activeRequests > 0 && performance.now() < deadline) {
|
|
102
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
103
|
+
}
|
|
104
|
+
GGLog.info(this, `HTTP server stopped`);
|
|
105
|
+
})();
|
|
106
|
+
}
|
|
107
|
+
onStart(callback) {
|
|
108
|
+
this._onStart.push(callback);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
onTeardown(callback) {
|
|
112
|
+
this._onTeardown.push(callback);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=GGHttpServer.js.map
|