@travetto/web-http 7.0.0-rc.2 → 7.0.0-rc.4
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/README.md +8 -8
- package/package.json +4 -4
- package/src/http.ts +22 -22
package/README.md
CHANGED
|
@@ -297,7 +297,7 @@ export class WebHttpUtil {
|
|
|
297
297
|
* Build a simple request handler
|
|
298
298
|
* @param dispatcher
|
|
299
299
|
*/
|
|
300
|
-
static buildHandler(dispatcher: WebDispatcher): (
|
|
300
|
+
static buildHandler(dispatcher: WebDispatcher): (request: HttpRequest, response: HttpResponse) => Promise<void>;
|
|
301
301
|
/**
|
|
302
302
|
* Start an http server
|
|
303
303
|
*/
|
|
@@ -305,11 +305,11 @@ export class WebHttpUtil {
|
|
|
305
305
|
/**
|
|
306
306
|
* Create a WebRequest given an incoming http request
|
|
307
307
|
*/
|
|
308
|
-
static toWebRequest(
|
|
308
|
+
static toWebRequest(request: HttpRequest): WebRequest;
|
|
309
309
|
/**
|
|
310
310
|
* Send WebResponse to outbound http response
|
|
311
311
|
*/
|
|
312
|
-
static async respondToServerResponse(
|
|
312
|
+
static async respondToServerResponse(webResponse: WebResponse, response: HttpResponse): Promise<void>;
|
|
313
313
|
}
|
|
314
314
|
```
|
|
315
315
|
|
|
@@ -317,11 +317,11 @@ Specifically, looking at `buildHandler`,
|
|
|
317
317
|
|
|
318
318
|
**Code: Web Http Utilities**
|
|
319
319
|
```typescript
|
|
320
|
-
static buildHandler(dispatcher: WebDispatcher): (
|
|
321
|
-
return async (
|
|
322
|
-
const
|
|
323
|
-
const
|
|
324
|
-
this.respondToServerResponse(
|
|
320
|
+
static buildHandler(dispatcher: WebDispatcher): (request: HttpRequest, response: HttpResponse) => Promise<void> {
|
|
321
|
+
return async (request: HttpRequest, response: HttpResponse): Promise<void> => {
|
|
322
|
+
const webRequest = this.toWebRequest(request);
|
|
323
|
+
const webResponse = await dispatcher.dispatch({ request: webRequest });
|
|
324
|
+
this.respondToServerResponse(webResponse, response);
|
|
325
325
|
};
|
|
326
326
|
}
|
|
327
327
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/web-http",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.4",
|
|
4
4
|
"description": "Web HTTP Server Support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"directory": "module/web-http"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/web": "^7.0.0-rc.
|
|
29
|
+
"@travetto/web": "^7.0.0-rc.3"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/cli": "^7.0.0-rc.
|
|
33
|
-
"@travetto/test": "^7.0.0-rc.
|
|
32
|
+
"@travetto/cli": "^7.0.0-rc.2",
|
|
33
|
+
"@travetto/test": "^7.0.0-rc.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/test": {
|
package/src/http.ts
CHANGED
|
@@ -30,11 +30,11 @@ export class WebHttpUtil {
|
|
|
30
30
|
* Build a simple request handler
|
|
31
31
|
* @param dispatcher
|
|
32
32
|
*/
|
|
33
|
-
static buildHandler(dispatcher: WebDispatcher): (
|
|
34
|
-
return async (
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
this.respondToServerResponse(
|
|
33
|
+
static buildHandler(dispatcher: WebDispatcher): (request: HttpRequest, response: HttpResponse) => Promise<void> {
|
|
34
|
+
return async (request: HttpRequest, response: HttpResponse): Promise<void> => {
|
|
35
|
+
const webRequest = this.toWebRequest(request);
|
|
36
|
+
const webResponse = await dispatcher.dispatch({ request: webRequest });
|
|
37
|
+
this.respondToServerResponse(webResponse, response);
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -61,7 +61,7 @@ export class WebHttpUtil {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const complete = new Promise<void>(
|
|
64
|
+
const complete = new Promise<void>(onClose => target.on('close', onClose));
|
|
65
65
|
|
|
66
66
|
// Track connections for shutdown
|
|
67
67
|
const activeConnections = new Set<HttpSocket>();
|
|
@@ -103,43 +103,43 @@ export class WebHttpUtil {
|
|
|
103
103
|
/**
|
|
104
104
|
* Create a WebRequest given an incoming http request
|
|
105
105
|
*/
|
|
106
|
-
static toWebRequest(
|
|
107
|
-
const secure =
|
|
108
|
-
const [path, query] = (
|
|
106
|
+
static toWebRequest(request: HttpRequest): WebRequest {
|
|
107
|
+
const secure = request.socket instanceof TLSSocket;
|
|
108
|
+
const [path, query] = (request.url ?? '/').split('?') ?? [];
|
|
109
109
|
return new WebRequest({
|
|
110
110
|
context: {
|
|
111
111
|
connection: {
|
|
112
|
-
ip:
|
|
113
|
-
host:
|
|
112
|
+
ip: request.socket.remoteAddress!,
|
|
113
|
+
host: request.headers.host,
|
|
114
114
|
httpProtocol: secure ? 'https' : 'http',
|
|
115
|
-
port:
|
|
115
|
+
port: request.socket.localPort
|
|
116
116
|
},
|
|
117
|
-
httpMethod: castTo(
|
|
117
|
+
httpMethod: castTo(request.method?.toUpperCase()),
|
|
118
118
|
path,
|
|
119
119
|
httpQuery: Object.fromEntries(new URLSearchParams(query)),
|
|
120
120
|
},
|
|
121
|
-
headers:
|
|
122
|
-
body: WebBodyUtil.markRaw(
|
|
121
|
+
headers: request.headers,
|
|
122
|
+
body: WebBodyUtil.markRaw(request)
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
/**
|
|
127
127
|
* Send WebResponse to outbound http response
|
|
128
128
|
*/
|
|
129
|
-
static async respondToServerResponse(
|
|
130
|
-
const binaryResponse = new WebResponse({ context:
|
|
131
|
-
binaryResponse.headers.forEach((
|
|
132
|
-
|
|
129
|
+
static async respondToServerResponse(webResponse: WebResponse, response: HttpResponse): Promise<void> {
|
|
130
|
+
const binaryResponse = new WebResponse({ context: webResponse.context, ...WebBodyUtil.toBinaryMessage(webResponse) });
|
|
131
|
+
binaryResponse.headers.forEach((value, key) => response.setHeader(key, value));
|
|
132
|
+
response.statusCode = WebCommonUtil.getStatusCode(binaryResponse);
|
|
133
133
|
const body = binaryResponse.body;
|
|
134
134
|
|
|
135
135
|
if (BinaryUtil.isReadable(body)) {
|
|
136
|
-
await pipeline(body,
|
|
136
|
+
await pipeline(body, response);
|
|
137
137
|
} else {
|
|
138
138
|
if (body) {
|
|
139
139
|
// Weird type union that http2 uses
|
|
140
|
-
'stream' in
|
|
140
|
+
'stream' in response ? response.write(body) : response.write(body);
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
response.end();
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|