@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.
Files changed (3) hide show
  1. package/README.md +8 -8
  2. package/package.json +4 -4
  3. 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): (req: HttpRequest, res: HttpResponse) => Promise<void>;
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(req: HttpRequest): WebRequest;
308
+ static toWebRequest(request: HttpRequest): WebRequest;
309
309
  /**
310
310
  * Send WebResponse to outbound http response
311
311
  */
312
- static async respondToServerResponse(webRes: WebResponse, res: HttpResponse): Promise<void>;
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): (req: HttpRequest, res: HttpResponse) => Promise<void> {
321
- return async (req: HttpRequest, res: HttpResponse): Promise<void> => {
322
- const request = this.toWebRequest(req);
323
- const response = await dispatcher.dispatch({ request });
324
- this.respondToServerResponse(response, res);
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.2",
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.1"
29
+ "@travetto/web": "^7.0.0-rc.3"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/cli": "^7.0.0-rc.0",
33
- "@travetto/test": "^7.0.0-rc.0"
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): (req: HttpRequest, res: HttpResponse) => Promise<void> {
34
- return async (req: HttpRequest, res: HttpResponse): Promise<void> => {
35
- const request = this.toWebRequest(req);
36
- const response = await dispatcher.dispatch({ request });
37
- this.respondToServerResponse(response, res);
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>(r => target.on('close', r));
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(req: HttpRequest): WebRequest {
107
- const secure = req.socket instanceof TLSSocket;
108
- const [path, query] = (req.url ?? '/').split('?') ?? [];
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: req.socket.remoteAddress!,
113
- host: req.headers.host,
112
+ ip: request.socket.remoteAddress!,
113
+ host: request.headers.host,
114
114
  httpProtocol: secure ? 'https' : 'http',
115
- port: req.socket.localPort
115
+ port: request.socket.localPort
116
116
  },
117
- httpMethod: castTo(req.method?.toUpperCase()),
117
+ httpMethod: castTo(request.method?.toUpperCase()),
118
118
  path,
119
119
  httpQuery: Object.fromEntries(new URLSearchParams(query)),
120
120
  },
121
- headers: req.headers,
122
- body: WebBodyUtil.markRaw(req)
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(webRes: WebResponse, res: HttpResponse): Promise<void> {
130
- const binaryResponse = new WebResponse({ context: webRes.context, ...WebBodyUtil.toBinaryMessage(webRes) });
131
- binaryResponse.headers.forEach((v, k) => res.setHeader(k, v));
132
- res.statusCode = WebCommonUtil.getStatusCode(binaryResponse);
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, res);
136
+ await pipeline(body, response);
137
137
  } else {
138
138
  if (body) {
139
139
  // Weird type union that http2 uses
140
- 'stream' in res ? res.write(body) : res.write(body);
140
+ 'stream' in response ? response.write(body) : response.write(body);
141
141
  }
142
- res.end();
142
+ response.end();
143
143
  }
144
144
  }
145
145
  }