@httptoolkit/httpolyglot 2.1.0 → 2.2.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 +7 -6
- package/dist/index.d.ts +9 -0
- package/dist/index.js +5 -9
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +11 -9
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
# Httpolyglot [](https://github.com/httptoolkit/httpolyglot/actions) [](https://npmjs.com/package/@httptoolkit/httpolyglot)
|
|
2
2
|
|
|
3
|
-
> _Part of [HTTP Toolkit](https://httptoolkit.
|
|
3
|
+
> _Part of [HTTP Toolkit](https://httptoolkit.com): powerful tools for building, testing & debugging HTTP(S)_
|
|
4
4
|
|
|
5
|
-
A module for serving
|
|
5
|
+
A module for serving HTTP, HTTPS and HTTP/2 connections, all over the same port.
|
|
6
6
|
|
|
7
|
-
Forked from the original [`httpolyglot`](https://github.com/mscdex/httpolyglot) to fix various issues required for [HTTP Toolkit](https://httptoolkit.
|
|
7
|
+
Forked from the original [`httpolyglot`](https://github.com/mscdex/httpolyglot) to fix various issues required for [HTTP Toolkit](https://httptoolkit.com), including:
|
|
8
8
|
|
|
9
9
|
* Support for HTTP/2
|
|
10
|
-
* Fixing `tlsClientError`: https://github.com/mscdex/httpolyglot/pull/11
|
|
11
|
-
*
|
|
12
|
-
* Dropping support for old versions of Node (and thereby simplifying the code somewhat)
|
|
10
|
+
* Fixing `tlsClientError`: https://github.com/mscdex/httpolyglot/pull/11
|
|
11
|
+
* Include initially sniffed bytes aren't lost in subsequent `clientError` events (https://github.com/mscdex/httpolyglot/issues/13)
|
|
12
|
+
* Dropping support for very old versions of Node (and thereby simplifying the code somewhat)
|
|
13
13
|
* Converting to TypeScript
|
|
14
|
+
* Event subscription support (subscribe to `server.on(x, ...)` to hear about `x` from _all_ internal servers - HTTP/2, HTTP/1, TLS and net)
|
|
14
15
|
|
|
15
16
|
Requirements
|
|
16
17
|
============
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
/// <reference types="node" />
|
|
2
6
|
import * as net from 'net';
|
|
3
7
|
import * as tls from 'tls';
|
|
4
8
|
import * as http from 'http';
|
|
5
9
|
import * as https from 'https';
|
|
6
10
|
declare module 'net' {
|
|
7
11
|
interface Socket {
|
|
12
|
+
/**
|
|
13
|
+
* Only preserved for types backward compat - always undefined in new releases.
|
|
14
|
+
*
|
|
15
|
+
* @deprecated
|
|
16
|
+
*/
|
|
8
17
|
__httpPeekedData?: Buffer;
|
|
9
18
|
}
|
|
10
19
|
}
|
package/dist/index.js
CHANGED
|
@@ -68,7 +68,7 @@ class Server extends net.Server {
|
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
connectionListener(socket) {
|
|
71
|
-
const data = socket.read(
|
|
71
|
+
const data = socket.read();
|
|
72
72
|
if (data === null) {
|
|
73
73
|
socket.removeListener('error', onError);
|
|
74
74
|
socket.on('error', onError);
|
|
@@ -83,8 +83,6 @@ class Server extends net.Server {
|
|
|
83
83
|
socket.unshift(data);
|
|
84
84
|
// Pass the socket to the correct subserver:
|
|
85
85
|
if (firstByte === TLS_HANDSHAKE_BYTE) {
|
|
86
|
-
// TLS sockets don't allow half open
|
|
87
|
-
socket.allowHalfOpen = false;
|
|
88
86
|
this._tlsServer.emit('connection', socket);
|
|
89
87
|
}
|
|
90
88
|
else {
|
|
@@ -94,18 +92,16 @@ class Server extends net.Server {
|
|
|
94
92
|
this.http2Listener(socket);
|
|
95
93
|
}
|
|
96
94
|
else {
|
|
97
|
-
// The above unshift isn't always sufficient to invisibly replace the
|
|
98
|
-
// read data. The rawPacket property on errors in the clientError event
|
|
99
|
-
// for plain HTTP servers loses this data - this prop makes it available.
|
|
100
|
-
// Bit of a hacky fix, but sufficient to allow for manual workarounds.
|
|
101
|
-
socket.__httpPeekedData = data;
|
|
102
95
|
this._httpServer.emit('connection', socket);
|
|
103
96
|
}
|
|
104
97
|
}
|
|
105
98
|
}
|
|
106
99
|
}
|
|
107
100
|
tlsListener(tlsSocket) {
|
|
108
|
-
if (tlsSocket.alpnProtocol === false ||
|
|
101
|
+
if (tlsSocket.alpnProtocol === false || // Old non-ALPN client
|
|
102
|
+
tlsSocket.alpnProtocol === 'http/1.1' || // Modern HTTP/1.1 ALPN client
|
|
103
|
+
tlsSocket.alpnProtocol === 'http 1.1' // Broken ALPN client (e.g. https-proxy-agent)
|
|
104
|
+
) {
|
|
109
105
|
this._httpServer.emit('connection', tlsSocket);
|
|
110
106
|
}
|
|
111
107
|
else {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2BAA2B;AAC3B,2BAA2B;AAC3B,6BAA6B;AAE7B,+BAA+B;AAE/B,mCAAsC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2BAA2B;AAC3B,2BAA2B;AAC3B,6BAA6B;AAE7B,+BAA+B;AAE/B,mCAAsC;AAatC,SAAS,OAAO,CAAC,GAAQ,IAAG,CAAC;AAE7B,MAAM,kBAAkB,GAAG,IAAI,CAAC,CAAC,0BAA0B;AAC3D,MAAM,aAAa,GAAG,kCAAkC,CAAC;AACzD,MAAM,oBAAoB,GAAG,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAExD,MAAM,kBAAkB,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAIhF,MAAa,MAAO,SAAQ,GAAG,CAAC,MAAM;IAuBpC,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AM,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,EAAE;YAC3B,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlLD,wBAkLC;AAmBD,SAAgB,YAAY,CAAC,wBAGf,EACZ,QAA+B;IAE/B,OAAO,IAAI,MAAM,CAAC,wBAA+B,EAAE,QAAe,CAAC,CAAC;AACtE,CAAC;AAPD,oCAOC;AAAA,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@httptoolkit/httpolyglot",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"author": "Tim Perry <pimterry@gmail.com>",
|
|
5
5
|
"description": "Serve http and https connections over the same port with node.js",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"url": "http://github.com/httptoolkit/httpolyglot.git"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@types/node": "
|
|
40
|
+
"@types/node": "*"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/chai": "^4.2.21",
|
package/src/index.ts
CHANGED
|
@@ -8,6 +8,11 @@ import { EventEmitter } from 'events';
|
|
|
8
8
|
|
|
9
9
|
declare module 'net' {
|
|
10
10
|
interface Socket {
|
|
11
|
+
/**
|
|
12
|
+
* Only preserved for types backward compat - always undefined in new releases.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
11
16
|
__httpPeekedData?: Buffer;
|
|
12
17
|
}
|
|
13
18
|
}
|
|
@@ -112,7 +117,7 @@ export class Server extends net.Server {
|
|
|
112
117
|
}
|
|
113
118
|
|
|
114
119
|
private connectionListener(socket: net.Socket) {
|
|
115
|
-
const data = socket.read(
|
|
120
|
+
const data = socket.read();
|
|
116
121
|
|
|
117
122
|
if (data === null) {
|
|
118
123
|
socket.removeListener('error', onError);
|
|
@@ -130,8 +135,6 @@ export class Server extends net.Server {
|
|
|
130
135
|
|
|
131
136
|
// Pass the socket to the correct subserver:
|
|
132
137
|
if (firstByte === TLS_HANDSHAKE_BYTE) {
|
|
133
|
-
// TLS sockets don't allow half open
|
|
134
|
-
socket.allowHalfOpen = false;
|
|
135
138
|
this._tlsServer.emit('connection', socket);
|
|
136
139
|
} else {
|
|
137
140
|
if (firstByte === HTTP2_PREFACE_BUFFER[0]) {
|
|
@@ -139,11 +142,6 @@ export class Server extends net.Server {
|
|
|
139
142
|
// reading until we get the whole stream:
|
|
140
143
|
this.http2Listener(socket);
|
|
141
144
|
} else {
|
|
142
|
-
// The above unshift isn't always sufficient to invisibly replace the
|
|
143
|
-
// read data. The rawPacket property on errors in the clientError event
|
|
144
|
-
// for plain HTTP servers loses this data - this prop makes it available.
|
|
145
|
-
// Bit of a hacky fix, but sufficient to allow for manual workarounds.
|
|
146
|
-
socket.__httpPeekedData = data;
|
|
147
145
|
this._httpServer.emit('connection', socket);
|
|
148
146
|
}
|
|
149
147
|
}
|
|
@@ -151,7 +149,11 @@ export class Server extends net.Server {
|
|
|
151
149
|
}
|
|
152
150
|
|
|
153
151
|
private tlsListener(tlsSocket: tls.TLSSocket) {
|
|
154
|
-
if (
|
|
152
|
+
if (
|
|
153
|
+
tlsSocket.alpnProtocol === false || // Old non-ALPN client
|
|
154
|
+
tlsSocket.alpnProtocol === 'http/1.1' || // Modern HTTP/1.1 ALPN client
|
|
155
|
+
tlsSocket.alpnProtocol === 'http 1.1' // Broken ALPN client (e.g. https-proxy-agent)
|
|
156
|
+
) {
|
|
155
157
|
this._httpServer.emit('connection', tlsSocket);
|
|
156
158
|
} else {
|
|
157
159
|
this._http2Server.emit('connection', tlsSocket);
|