socket-function 0.7.14 → 0.7.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.7.14",
3
+ "version": "0.7.15",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -73,20 +73,6 @@ export async function startSocketServer(
73
73
  res.end();
74
74
  });
75
75
 
76
- httpServer.listen(0, "127.0.0.1");
77
- httpsServer.listen(0, "127.0.0.1");
78
-
79
- // TODO: We should really add error handling here, but... we should always be able to listen
80
- // on ANY port on localhost, as why couldn't we?
81
- let httpServerReady = new Promise(resolve => httpServer.once("listening", resolve));
82
- let httpsServerReady = new Promise(resolve => httpsServer.once("listening", resolve));
83
- await httpServerReady;
84
- await httpsServerReady;
85
-
86
- let httpAddress = httpServer.address() as net.AddressInfo;
87
- let httpsAddress = httpsServer.address() as net.AddressInfo;
88
-
89
-
90
76
  let realServer = net.createServer(socket => {
91
77
  // NOTE: ONCE is used, so we only look at the first buffer, and then after that
92
78
  // we pipe. This should be very efficient, as pipe has insane throughput
@@ -94,21 +80,11 @@ export async function startSocketServer(
94
80
  socket.once("data", buffer => {
95
81
  // All HTTPS requests start with 22, and no HTTP requests start with 22,
96
82
  // so we just need to read the first byte.
97
- let byte = buffer[0];
98
- let isHTTPS = byte === 22;
99
- let address = httpAddress;
100
- if (isHTTPS) {
101
- address = httpsAddress;
102
- }
103
- let baseSocket = net.connect(address.port);
104
-
105
- baseSocket.write(buffer);
106
- socket.pipe(baseSocket);
107
- baseSocket.pipe(socket);
108
-
109
- baseSocket.on("error", (e) => {
110
- console.error(`Base socket error, ${e.stack}`);
111
- });
83
+ let server = buffer[0] === 22 ? httpsServer : httpServer;
84
+
85
+ // NOTE: Messages aren't dequeued until the current handler finishes, so we don't need to pause the socket or anything.
86
+ server.emit("connection", socket);
87
+ socket.unshift(buffer);
112
88
  });
113
89
  socket.on("error", (e) => {
114
90
  console.error(`Exposed socket error, ${e.stack}`);