Comparing version 1.0.2 to 1.0.3
@@ -13,2 +13,3 @@ /// <reference types="node" /> | ||
private mirrorRejection; | ||
private sendErrorResponse; | ||
} |
@@ -30,24 +30,39 @@ "use strict"; | ||
handleUpgrade(req, socket, head) { | ||
let { protocol: requestedProtocol, hostname, port, path } = url.parse(req.url); | ||
if (this.debug) | ||
console.log(`Handling upgrade for ${req.url}`); | ||
const transparentProxy = !hostname; | ||
if (transparentProxy) { | ||
const hostHeader = req.headers.host; | ||
[hostname, port] = hostHeader.split(':'); | ||
// upstreamEncryption is set in http-combo-server, for requests that have explicitly | ||
// CONNECTed upstream (which may then up/downgrade from the current encryption). | ||
let protocol; | ||
if (socket.lastHopEncrypted !== undefined) { | ||
protocol = socket.lastHopEncrypted ? 'wss' : 'ws'; | ||
try { | ||
// Ensure WS errors don't crash anything. Once/if upstream connects, we'll also add | ||
// a listener that will proxy errors upstream - but for now don't crash either way. | ||
socket.on('error', (e) => { | ||
if (socket.listenerCount('error') === 1) { | ||
// If no other error handler exists, make sure we at least try to | ||
// clean up this socket so the client sees it die. | ||
socket.destroy(); | ||
} | ||
}); | ||
let { protocol: requestedProtocol, hostname, port, path } = url.parse(req.url); | ||
if (this.debug) | ||
console.log(`Handling upgrade for ${req.url}`); | ||
const transparentProxy = !hostname; | ||
if (transparentProxy) { | ||
const hostHeader = req.headers.host; | ||
[hostname, port] = hostHeader.split(':'); | ||
// upstreamEncryption is set in http-combo-server, for requests that have explicitly | ||
// CONNECTed upstream (which may then up/downgrade from the current encryption). | ||
let protocol; | ||
if (socket.lastHopEncrypted !== undefined) { | ||
protocol = socket.lastHopEncrypted ? 'wss' : 'ws'; | ||
} | ||
else { | ||
protocol = req.connection.encrypted ? 'wss' : 'ws'; | ||
} | ||
this.connectUpstream(`${protocol}://${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} | ||
else { | ||
protocol = req.connection.encrypted ? 'wss' : 'ws'; | ||
// Connect directly according to the specified URL | ||
const protocol = requestedProtocol.replace('http', 'ws'); | ||
this.connectUpstream(`${protocol}//${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} | ||
this.connectUpstream(`${protocol}://${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} | ||
else { | ||
// Connect directly according to the specified URL | ||
const protocol = requestedProtocol.replace('http', 'ws'); | ||
this.connectUpstream(`${protocol}//${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
catch (e) { | ||
console.error(e); | ||
this.sendErrorResponse(socket, e); | ||
} | ||
@@ -136,2 +151,12 @@ } | ||
} | ||
sendErrorResponse(socket, error) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (socket.writable) { | ||
socket.write('HTTP/1.1 500 Internal Server Error\r\n' + | ||
'\r\n' + | ||
error.message); | ||
} | ||
socket.destroy(error); | ||
}); | ||
} | ||
} | ||
@@ -138,0 +163,0 @@ exports.WebSocketHandler = WebSocketHandler; |
{ | ||
"name": "mockttp", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Mock HTTP server for testing HTTP clients and stubbing webservices", | ||
@@ -135,3 +135,3 @@ "main": "dist/main.js", | ||
"native-duplexpair": "^1.0.0", | ||
"node-forge": "^0.9.0", | ||
"node-forge": "^0.10.0", | ||
"normalize-url": "^1.9.1", | ||
@@ -138,0 +138,0 @@ "performance-now": "^2.1.0", |
@@ -30,26 +30,41 @@ import * as _ from 'lodash'; | ||
handleUpgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer) { | ||
let { protocol: requestedProtocol, hostname, port, path } = url.parse(req.url!); | ||
try { | ||
// Ensure WS errors don't crash anything. Once/if upstream connects, we'll also add | ||
// a listener that will proxy errors upstream - but for now don't crash either way. | ||
socket.on('error', (e) => { | ||
if (socket.listenerCount('error') === 1) { | ||
// If no other error handler exists, make sure we at least try to | ||
// clean up this socket so the client sees it die. | ||
socket.destroy(); | ||
} | ||
}); | ||
if (this.debug) console.log(`Handling upgrade for ${req.url}`); | ||
let { protocol: requestedProtocol, hostname, port, path } = url.parse(req.url!); | ||
const transparentProxy = !hostname; | ||
if (this.debug) console.log(`Handling upgrade for ${req.url}`); | ||
if (transparentProxy) { | ||
const hostHeader = req.headers.host; | ||
[ hostname, port ] = hostHeader!.split(':'); | ||
const transparentProxy = !hostname; | ||
// upstreamEncryption is set in http-combo-server, for requests that have explicitly | ||
// CONNECTed upstream (which may then up/downgrade from the current encryption). | ||
let protocol: string; | ||
if (socket.lastHopEncrypted !== undefined) { | ||
protocol = socket.lastHopEncrypted ? 'wss' : 'ws'; | ||
if (transparentProxy) { | ||
const hostHeader = req.headers.host; | ||
[ hostname, port ] = hostHeader!.split(':'); | ||
// upstreamEncryption is set in http-combo-server, for requests that have explicitly | ||
// CONNECTed upstream (which may then up/downgrade from the current encryption). | ||
let protocol: string; | ||
if (socket.lastHopEncrypted !== undefined) { | ||
protocol = socket.lastHopEncrypted ? 'wss' : 'ws'; | ||
} else { | ||
protocol = req.connection.encrypted ? 'wss' : 'ws'; | ||
} | ||
this.connectUpstream(`${protocol}://${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} else { | ||
protocol = req.connection.encrypted ? 'wss' : 'ws'; | ||
// Connect directly according to the specified URL | ||
const protocol = requestedProtocol!.replace('http', 'ws'); | ||
this.connectUpstream(`${protocol}//${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} | ||
this.connectUpstream(`${protocol}://${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} else { | ||
// Connect directly according to the specified URL | ||
const protocol = requestedProtocol!.replace('http', 'ws'); | ||
this.connectUpstream(`${protocol}//${hostname}${port ? ':' + port : ''}${path}`, req, socket, head); | ||
} catch (e) { | ||
console.error(e); | ||
this.sendErrorResponse(socket, e); | ||
} | ||
@@ -149,2 +164,14 @@ } | ||
} | ||
private async sendErrorResponse(socket: net.Socket, error: Error) { | ||
if (socket.writable) { | ||
socket.write( | ||
'HTTP/1.1 500 Internal Server Error\r\n' + | ||
'\r\n' + | ||
error.message | ||
); | ||
} | ||
socket.destroy(error); | ||
} | ||
} | ||
@@ -151,0 +178,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
733820
13305
+ Addednode-forge@0.10.0(transitive)
- Removednode-forge@0.9.2(transitive)
Updatednode-forge@^0.10.0