@travetto/web-http 7.1.4 → 8.0.0-alpha.0
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 +4 -4
- package/package.json +4 -4
- package/src/config.ts +5 -5
- package/src/http.ts +7 -12
- package/support/test/dispatcher.ts +5 -8
package/README.md
CHANGED
|
@@ -126,20 +126,20 @@ export class WebHttpConfig {
|
|
|
126
126
|
async postConstruct(): Promise<void> {
|
|
127
127
|
this.tls ??= (this.httpVersion === '2' || !!this.tlsKeys);
|
|
128
128
|
this.port = (this.port < 0 ? await NetUtil.getFreePort() : this.port);
|
|
129
|
-
this.bindAddress ||=
|
|
129
|
+
this.bindAddress ||= NetUtil.getLocalAddress();
|
|
130
130
|
|
|
131
131
|
if (!this.tls) {
|
|
132
132
|
// Clear out keys if tls is not set
|
|
133
133
|
this.tlsKeys = undefined;
|
|
134
134
|
} else if (!this.tlsKeys) {
|
|
135
135
|
if (Runtime.production) {
|
|
136
|
-
throw new
|
|
136
|
+
throw new RuntimeError('Default tls keys are only valid for development use, please specify a config value at web.tls.keys');
|
|
137
137
|
}
|
|
138
138
|
this.tlsKeys = await WebTlsUtil.generateKeyPair();
|
|
139
139
|
} else {
|
|
140
140
|
if (this.tlsKeys.key.length < 100) { // We have files or resources
|
|
141
|
-
this.tlsKeys.key =
|
|
142
|
-
this.tlsKeys.cert =
|
|
141
|
+
this.tlsKeys.key = await RuntimeResources.readText(this.tlsKeys.key);
|
|
142
|
+
this.tlsKeys.cert = await RuntimeResources.readText(this.tlsKeys.cert);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/web-http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Web HTTP Server Support",
|
|
6
6
|
"keywords": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"directory": "module/web-http"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/web": "^
|
|
30
|
+
"@travetto/web": "^8.0.0-alpha.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/cli": "^
|
|
34
|
-
"@travetto/test": "^
|
|
33
|
+
"@travetto/cli": "^8.0.0-alpha.0",
|
|
34
|
+
"@travetto/test": "^8.0.0-alpha.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@travetto/test": {
|
package/src/config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Config, EnvVar } from '@travetto/config';
|
|
2
2
|
import { Ignore, Secret } from '@travetto/schema';
|
|
3
|
-
import {
|
|
3
|
+
import { RuntimeError, Runtime, RuntimeResources } from '@travetto/runtime';
|
|
4
4
|
import { NetUtil } from '@travetto/web';
|
|
5
5
|
|
|
6
6
|
import type { WebSecureKeyPair } from './types.ts';
|
|
@@ -48,20 +48,20 @@ export class WebHttpConfig {
|
|
|
48
48
|
async postConstruct(): Promise<void> {
|
|
49
49
|
this.tls ??= (this.httpVersion === '2' || !!this.tlsKeys);
|
|
50
50
|
this.port = (this.port < 0 ? await NetUtil.getFreePort() : this.port);
|
|
51
|
-
this.bindAddress ||=
|
|
51
|
+
this.bindAddress ||= NetUtil.getLocalAddress();
|
|
52
52
|
|
|
53
53
|
if (!this.tls) {
|
|
54
54
|
// Clear out keys if tls is not set
|
|
55
55
|
this.tlsKeys = undefined;
|
|
56
56
|
} else if (!this.tlsKeys) {
|
|
57
57
|
if (Runtime.production) {
|
|
58
|
-
throw new
|
|
58
|
+
throw new RuntimeError('Default tls keys are only valid for development use, please specify a config value at web.tls.keys');
|
|
59
59
|
}
|
|
60
60
|
this.tlsKeys = await WebTlsUtil.generateKeyPair();
|
|
61
61
|
} else {
|
|
62
62
|
if (this.tlsKeys.key.length < 100) { // We have files or resources
|
|
63
|
-
this.tlsKeys.key =
|
|
64
|
-
this.tlsKeys.cert =
|
|
63
|
+
this.tlsKeys.key = await RuntimeResources.readText(this.tlsKeys.key);
|
|
64
|
+
this.tlsKeys.cert = await RuntimeResources.readText(this.tlsKeys.cert);
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
package/src/http.ts
CHANGED
|
@@ -2,11 +2,10 @@ import type net from 'node:net';
|
|
|
2
2
|
import http from 'node:http';
|
|
3
3
|
import http2 from 'node:http2';
|
|
4
4
|
import https from 'node:https';
|
|
5
|
-
import { pipeline } from 'node:stream/promises';
|
|
6
5
|
import { TLSSocket } from 'node:tls';
|
|
7
6
|
|
|
8
7
|
import { WebBodyUtil, WebCommonUtil, type WebDispatcher, WebRequest, WebResponse } from '@travetto/web';
|
|
9
|
-
import { BinaryUtil, castTo, ShutdownManager } from '@travetto/runtime';
|
|
8
|
+
import { type BinaryType, BinaryUtil, castTo, ShutdownManager } from '@travetto/runtime';
|
|
10
9
|
|
|
11
10
|
import type { WebSecureKeyPair, WebServerHandle } from './types.ts';
|
|
12
11
|
|
|
@@ -119,7 +118,7 @@ export class WebHttpUtil {
|
|
|
119
118
|
httpQuery: Object.fromEntries(new URLSearchParams(query)),
|
|
120
119
|
},
|
|
121
120
|
headers: request.headers,
|
|
122
|
-
body: WebBodyUtil.
|
|
121
|
+
body: WebBodyUtil.markRawBinary(request)
|
|
123
122
|
});
|
|
124
123
|
}
|
|
125
124
|
|
|
@@ -127,18 +126,14 @@ export class WebHttpUtil {
|
|
|
127
126
|
* Send WebResponse to outbound http response
|
|
128
127
|
*/
|
|
129
128
|
static async respondToServerResponse(webResponse: WebResponse, response: HttpResponse): Promise<void> {
|
|
130
|
-
const binaryResponse = new WebResponse({ context: webResponse.context, ...WebBodyUtil.toBinaryMessage(webResponse) });
|
|
129
|
+
const binaryResponse = new WebResponse<BinaryType>({ context: webResponse.context, ...WebBodyUtil.toBinaryMessage(webResponse) });
|
|
131
130
|
binaryResponse.headers.forEach((value, key) => response.setHeader(key, value));
|
|
132
131
|
response.statusCode = WebCommonUtil.getStatusCode(binaryResponse);
|
|
133
|
-
const body = binaryResponse.body;
|
|
134
132
|
|
|
135
|
-
if (
|
|
136
|
-
await pipeline(body, response);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Weird type union that http2 uses
|
|
140
|
-
'stream' in response ? response.write(body) : response.write(body);
|
|
141
|
-
}
|
|
133
|
+
if (binaryResponse.body) {
|
|
134
|
+
await BinaryUtil.pipeline(binaryResponse.body, response);
|
|
135
|
+
}
|
|
136
|
+
if (!response.closed) {
|
|
142
137
|
response.end();
|
|
143
138
|
}
|
|
144
139
|
}
|
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream';
|
|
2
|
-
import { buffer } from 'node:stream/consumers';
|
|
3
|
-
|
|
4
1
|
import { Inject, Injectable } from '@travetto/di';
|
|
5
2
|
import { type WebFilterContext, WebResponse, type WebDispatcher, WebBodyUtil } from '@travetto/web';
|
|
6
|
-
import { castTo } from '@travetto/runtime';
|
|
3
|
+
import { BinaryUtil, castTo } from '@travetto/runtime';
|
|
7
4
|
|
|
8
5
|
import { WebTestDispatchUtil } from '@travetto/web/support/test/dispatch-util.ts';
|
|
9
6
|
|
|
10
7
|
import type { WebHttpConfig } from '../../src/config.ts';
|
|
11
8
|
|
|
12
|
-
const toBuffer = (src: Buffer | Readable) => Buffer.isBuffer(src) ? src : buffer(src);
|
|
13
|
-
|
|
14
9
|
/**
|
|
15
10
|
* Support for invoking http requests against the server
|
|
16
11
|
*/
|
|
@@ -23,7 +18,9 @@ export class FetchWebDispatcher implements WebDispatcher {
|
|
|
23
18
|
async dispatch({ request }: WebFilterContext): Promise<WebResponse> {
|
|
24
19
|
const baseRequest = await WebTestDispatchUtil.applyRequestBody(request);
|
|
25
20
|
const finalPath = WebTestDispatchUtil.buildPath(baseRequest);
|
|
26
|
-
const body: RequestInit['body'] = WebBodyUtil.
|
|
21
|
+
const body: RequestInit['body'] = WebBodyUtil.isRawBinary(request.body) ?
|
|
22
|
+
await BinaryUtil.toBinaryArray(request.body) :
|
|
23
|
+
castTo(request.body);
|
|
27
24
|
const { context: { httpMethod: method }, headers } = request;
|
|
28
25
|
|
|
29
26
|
const response = await fetch(
|
|
@@ -33,7 +30,7 @@ export class FetchWebDispatcher implements WebDispatcher {
|
|
|
33
30
|
|
|
34
31
|
return await WebTestDispatchUtil.finalizeResponseBody(
|
|
35
32
|
new WebResponse({
|
|
36
|
-
body:
|
|
33
|
+
body: await response.arrayBuffer(),
|
|
37
34
|
context: { httpStatusCode: response.status },
|
|
38
35
|
headers: response.headers
|
|
39
36
|
})
|