@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,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server extension for HttpApiSchema - adds register method.
|
|
3
|
+
* This file should only be imported in server (Node.js) context.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import http from "http";
|
|
7
|
+
import {GGLocator} from "@grest-ts/locator";
|
|
8
|
+
import {ClientHttpRouteToRpcTransformServerCodec, GGHttpCodec, GGHttpSchema} from "../schema/GGHttpSchema";
|
|
9
|
+
import {ERROR, GGContractApiDefinition, GGContractImplementation, GGContractMethod, OK, SERVER_ERROR} from "@grest-ts/schema";
|
|
10
|
+
import {HttpMethod} from "@grest-ts/common";
|
|
11
|
+
import {GG_DISCOVERY} from "@grest-ts/discovery";
|
|
12
|
+
import {GGContext, GGContextStore} from "@grest-ts/context";
|
|
13
|
+
import {GG_TRACE} from "@grest-ts/trace";
|
|
14
|
+
import {GG_HTTP_REQUEST} from "./GG_HTTP_REQUEST";
|
|
15
|
+
import {GG_METRICS} from "@grest-ts/metrics";
|
|
16
|
+
import {GGHttpMetrics} from "./GGHttpMetrics";
|
|
17
|
+
import {GG_HTTP_SERVER} from "./GG_HTTP_SERVER";
|
|
18
|
+
import {GGHttpServer} from "./GGHttpServer";
|
|
19
|
+
import {GGLog} from "@grest-ts/logger";
|
|
20
|
+
|
|
21
|
+
export interface GGHttpSchemaConfig {
|
|
22
|
+
/**
|
|
23
|
+
* The HTTP request handler to register with.
|
|
24
|
+
*/
|
|
25
|
+
http?: GGHttpServer;
|
|
26
|
+
/**
|
|
27
|
+
* Additional middlewares to apply to all routes.
|
|
28
|
+
*/
|
|
29
|
+
middlewares?: GGHttpServerMiddleware[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface GGHttpServerMiddleware {
|
|
33
|
+
process?(): Promise<void>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare module "../schema/GGHttpSchema" {
|
|
37
|
+
interface GGHttpSchema<TContract extends GGContractApiDefinition> {
|
|
38
|
+
/**
|
|
39
|
+
* Start server with direct implementation.
|
|
40
|
+
* Uses parseRequest from use classes without transform.
|
|
41
|
+
* For custom transforms, use createServer() instead.
|
|
42
|
+
*/
|
|
43
|
+
register(implementation: GGContractImplementation<TContract>, config?: GGHttpSchemaConfig): void
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
GGHttpSchema.prototype.register = function <TContract extends GGContractApiDefinition>(
|
|
48
|
+
this: GGHttpSchema<TContract>,
|
|
49
|
+
implementation: GGContractImplementation<TContract>,
|
|
50
|
+
config?: GGHttpSchemaConfig
|
|
51
|
+
) {
|
|
52
|
+
return setupRoutes(this, implementation, config)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function setupRoutes<TContract extends GGContractApiDefinition>(
|
|
56
|
+
httpSchema: GGHttpSchema<TContract>,
|
|
57
|
+
implementation: GGContractImplementation<TContract>,
|
|
58
|
+
config?: GGHttpSchemaConfig
|
|
59
|
+
) {
|
|
60
|
+
config ??= {};
|
|
61
|
+
config.middlewares ??= [];
|
|
62
|
+
|
|
63
|
+
if (!httpSchema.contract) throw new Error(`HttpApiSchema "${httpSchema.name}" has no contract.`);
|
|
64
|
+
|
|
65
|
+
const server = config.http ?? GGLocator.getScope().get(GG_HTTP_SERVER);
|
|
66
|
+
if (!server) throw new Error(`No HTTP server found. Make sure to register GGHttpServerAdapter in the scope or pass handler via config`)
|
|
67
|
+
|
|
68
|
+
const pathPrefix = "/" + httpSchema.pathPrefix + "/"
|
|
69
|
+
const apiMiddlewares = httpSchema.apiMiddlewares;
|
|
70
|
+
const scope = GGLocator.getScope();
|
|
71
|
+
const parentContext = GGContextStore.tryGetContext();
|
|
72
|
+
|
|
73
|
+
server.onStart(() => {
|
|
74
|
+
GG_DISCOVERY.tryGet()?.registerRoutes([{
|
|
75
|
+
runtime: scope.serviceName,
|
|
76
|
+
api: httpSchema.name,
|
|
77
|
+
pathPrefix: pathPrefix,
|
|
78
|
+
protocol: "http",
|
|
79
|
+
port: server.port
|
|
80
|
+
}]);
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// Info: Contract instance is auto-registered in GGLocator via patched implement()
|
|
84
|
+
// @TODO This is not really used and only for testkit so it would register implementation of the contract... Not cool
|
|
85
|
+
httpSchema.contract.implement(implementation);
|
|
86
|
+
|
|
87
|
+
for (const methodName in httpSchema.codec) {
|
|
88
|
+
// Wire format.
|
|
89
|
+
const codec: GGHttpCodec = httpSchema.codec[methodName]
|
|
90
|
+
if (!codec) throw new Error(`Contract for "${httpSchema.name}.${methodName}" is missing wire format!`)
|
|
91
|
+
|
|
92
|
+
// Implementation function
|
|
93
|
+
const implFn = implementation[methodName]?.bind(implementation)
|
|
94
|
+
if (!implFn) throw new Error(`Implementation for "${httpSchema.name}.${methodName}" is missing implementation method!`)
|
|
95
|
+
|
|
96
|
+
// Input schema
|
|
97
|
+
const contractFunctionSchema: GGContractMethod = httpSchema.contract.methods[methodName as keyof TContract]
|
|
98
|
+
if (!contractFunctionSchema) throw new Error(`Contract for "${httpSchema.name}.${methodName}" is missing contract function schema!`)
|
|
99
|
+
|
|
100
|
+
// build request mapping
|
|
101
|
+
const requestParser: ClientHttpRouteToRpcTransformServerCodec = codec.createForServer({
|
|
102
|
+
contract: contractFunctionSchema,
|
|
103
|
+
apiMiddlewares: apiMiddlewares,
|
|
104
|
+
serverMiddlewares: config.middlewares
|
|
105
|
+
})
|
|
106
|
+
server.registerRoute(codec.method, pathPrefix + codec.path, async (req: http.IncomingMessage, res: http.ServerResponse): Promise<void> => {
|
|
107
|
+
scope.ensureEntered();
|
|
108
|
+
return new GGContext("REQ", parentContext).run(async () => {
|
|
109
|
+
GG_TRACE.init();
|
|
110
|
+
GG_HTTP_REQUEST.set({port: server.port, method: req.method, path: req.url});
|
|
111
|
+
const startTime = performance.now()
|
|
112
|
+
let rpcResult: ERROR<string, unknown> | OK<unknown>
|
|
113
|
+
try {
|
|
114
|
+
const rpcInput = await requestParser.parseRequest(req)
|
|
115
|
+
try {
|
|
116
|
+
rpcResult = {success: true, type: "OK", data: await implFn(rpcInput)}
|
|
117
|
+
// GGLog.debug(httpSchema, "Response", rpcResult) // This is very slow to log this (like 3x performance loss)
|
|
118
|
+
} catch (error: unknown) {
|
|
119
|
+
rpcResult = ERROR.fromUnknown(error);
|
|
120
|
+
if (rpcResult instanceof SERVER_ERROR) {
|
|
121
|
+
GGLog.error(httpSchema, rpcResult)
|
|
122
|
+
} else {
|
|
123
|
+
// GGLog.debug(httpSchema, "Response", rpcResult) // This is very slow to log this (like 3x performance loss)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
await requestParser.sendResponse(res, rpcResult) // can throw
|
|
127
|
+
} catch (error: unknown) {
|
|
128
|
+
rpcResult = ERROR.fromUnknown(error)
|
|
129
|
+
await requestParser.sendResponse(res, rpcResult) // Does not throw
|
|
130
|
+
GGLog.error(httpSchema, rpcResult)
|
|
131
|
+
} finally {
|
|
132
|
+
metrics(rpcResult?.type, httpSchema.name, codec.method, pathPrefix, codec.path, startTime)
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function metrics(
|
|
140
|
+
resultType: string,
|
|
141
|
+
apiName: string,
|
|
142
|
+
method: HttpMethod,
|
|
143
|
+
pathPrefix: string,
|
|
144
|
+
pathSuffix: string,
|
|
145
|
+
startTime: number
|
|
146
|
+
) {
|
|
147
|
+
if (GG_METRICS.has()) {
|
|
148
|
+
const path = method + " " + pathPrefix + pathSuffix;
|
|
149
|
+
GGHttpMetrics.requests.inc(1, {
|
|
150
|
+
api: apiName,
|
|
151
|
+
method: pathSuffix,
|
|
152
|
+
path: path,
|
|
153
|
+
result: resultType
|
|
154
|
+
});
|
|
155
|
+
GGHttpMetrics.requestDuration.observe(performance.now() - startTime, {
|
|
156
|
+
api: apiName,
|
|
157
|
+
method: pathSuffix,
|
|
158
|
+
path: path
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import http from "http";
|
|
2
|
+
import {HttpMethod, HttpStatusCode} from "@grest-ts/common";
|
|
3
|
+
import {GGLocator, GGLocatorKey, GGLocatorScope, GGLocatorServiceType} from "@grest-ts/locator";
|
|
4
|
+
import {GG_HTTP_SERVER} from "./GG_HTTP_SERVER";
|
|
5
|
+
import {GGLog} from "@grest-ts/logger";
|
|
6
|
+
import findMyWay, {HTTPMethod} from "find-my-way";
|
|
7
|
+
|
|
8
|
+
export interface GGHttpServerAdapterConfig {
|
|
9
|
+
key?: GGLocatorKey<GGHttpServer>;
|
|
10
|
+
port?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class GGHttpServer {
|
|
14
|
+
|
|
15
|
+
protected _port: number | undefined;
|
|
16
|
+
protected teardownPromise: Promise<void> | undefined;
|
|
17
|
+
protected readonly scope: GGLocatorScope;
|
|
18
|
+
protected readonly runtimeName: string;
|
|
19
|
+
private readonly configuredPort: number;
|
|
20
|
+
|
|
21
|
+
private readonly _onStart: Array<() => void> = [];
|
|
22
|
+
private readonly _onTeardown: Array<() => void> = [];
|
|
23
|
+
|
|
24
|
+
public readonly httpServer: http.Server;
|
|
25
|
+
private activeRequests = 0;
|
|
26
|
+
private router = findMyWay<findMyWay.HTTPVersion.V1>();
|
|
27
|
+
|
|
28
|
+
constructor(config?: GGHttpServerAdapterConfig) {
|
|
29
|
+
|
|
30
|
+
this.runtimeName = GGLocator.getScope().serviceName;
|
|
31
|
+
this.scope = GGLocator.getScope();
|
|
32
|
+
this.configuredPort = config?.port ?? (process.env.PORT ? Number(process.env.PORT) : 0);
|
|
33
|
+
|
|
34
|
+
GGLocator.getScope().setWithLifecycle(config?.key ?? GG_HTTP_SERVER, this, {
|
|
35
|
+
type: GGLocatorServiceType.HTTP,
|
|
36
|
+
start: this.start.bind(this),
|
|
37
|
+
teardown: this.teardown.bind(this)
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this.httpServer = http.createServer(async (req, res) => {
|
|
41
|
+
if (this.teardownPromise) {
|
|
42
|
+
res.statusCode = HttpStatusCode.ServerTemporarilyNotAvailable503;
|
|
43
|
+
res.end();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.activeRequests++;
|
|
47
|
+
try {
|
|
48
|
+
if (req.headers.origin) { // For browsers
|
|
49
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
50
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
|
|
51
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-company-auth'); // @TODO Remove x-company-auth
|
|
52
|
+
res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition');
|
|
53
|
+
}
|
|
54
|
+
if (req.method === 'OPTIONS') {
|
|
55
|
+
res.writeHead(204);
|
|
56
|
+
res.end();
|
|
57
|
+
} else {
|
|
58
|
+
const resolved = this.router.find(req.method as HTTPMethod, req.url)
|
|
59
|
+
if (resolved) {
|
|
60
|
+
await (resolved.handler as unknown as GGHttpRequestCallback)(req, res);
|
|
61
|
+
} else {
|
|
62
|
+
res.writeHead(404);
|
|
63
|
+
res.end();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
GGLog.error(this, error);
|
|
68
|
+
res.writeHead(500);
|
|
69
|
+
res.end();
|
|
70
|
+
} finally {
|
|
71
|
+
this.activeRequests--;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Current port the server is listening to.
|
|
78
|
+
* May be undefined before start() is called.
|
|
79
|
+
*/
|
|
80
|
+
get port(): number | undefined {
|
|
81
|
+
return this._port;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// =========================================================================
|
|
85
|
+
// Common implementation
|
|
86
|
+
// =========================================================================
|
|
87
|
+
|
|
88
|
+
public registerRoute(method: HttpMethod, path: string, handler: GGHttpRequestCallback): void {
|
|
89
|
+
this.router.on(method as HTTPMethod, path, handler as unknown as findMyWay.Handler<findMyWay.HTTPVersion.V1>);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public async start(): Promise<void> {
|
|
93
|
+
this._port = await new Promise((resolve) => {
|
|
94
|
+
this.httpServer.listen(this.configuredPort, '0.0.0.0', () => {
|
|
95
|
+
const port = (this.httpServer.address() as any).port;
|
|
96
|
+
resolve(port);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
this._onStart.forEach(callback => callback());
|
|
100
|
+
GGLog.info(this, `HTTP server started`, {
|
|
101
|
+
port: this._port,
|
|
102
|
+
runtime: this.runtimeName
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public async teardown(): Promise<void> {
|
|
107
|
+
if (this.teardownPromise) {
|
|
108
|
+
return this.teardownPromise;
|
|
109
|
+
}
|
|
110
|
+
return this.teardownPromise = (async () => {
|
|
111
|
+
await Promise.all(this._onTeardown.map(callback => callback()));
|
|
112
|
+
this.httpServer.close();
|
|
113
|
+
const maxWaitTime = 3000;
|
|
114
|
+
const deadline = performance.now() + maxWaitTime;
|
|
115
|
+
while (this.activeRequests > 0 && performance.now() < deadline) {
|
|
116
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
117
|
+
}
|
|
118
|
+
GGLog.info(this, `HTTP server stopped`);
|
|
119
|
+
})();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public onStart(callback: () => Promise<void> | void): this {
|
|
123
|
+
this._onStart.push(callback);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public onTeardown(callback: () => Promise<void> | void): this {
|
|
128
|
+
this._onTeardown.push(callback);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type GGHttpRequestCallback = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {GGContextKey} from "@grest-ts/context";
|
|
2
|
+
import {IsNumber, IsObject, IsString} from "@grest-ts/schema";
|
|
3
|
+
|
|
4
|
+
const IsHttpRequestContext = IsObject({
|
|
5
|
+
port: IsNumber,
|
|
6
|
+
method: IsString,
|
|
7
|
+
path: IsString,
|
|
8
|
+
pathTemplate: IsString.orUndefined
|
|
9
|
+
});
|
|
10
|
+
export type HttpRequestContext = typeof IsHttpRequestContext.infer;
|
|
11
|
+
|
|
12
|
+
export const GG_HTTP_REQUEST = new GGContextKey<HttpRequestContext>('http-request', IsHttpRequestContext);
|