Skip to content

Commit

Permalink
Properly unpipe old pipes on request
Browse files Browse the repository at this point in the history
Otherwise it feels like the scope is going wild because you'll end up
piping to the previous transformer
  • Loading branch information
leppert committed Mar 24, 2023
1 parent f82bdcc commit 08c7762
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Scoop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ await test('Scoop - capture of a web page.', async (t) => {
await t.test('Scoop follows redirects', async (_t) => {
const statusCode = 301
const { exchanges: [redirect, html] } = await Scoop.capture(`${URL}/redirect?statusCode=${statusCode}&path=test.html`, options)
assert.equal(redirect.response.statusCode, statusCode)
assert.equal(redirect.response.startLine.split(' ')[1], statusCode.toString())
assert.equal(html.response.body.toString(), testHtmlFixture.toString())
})

Expand Down
3 changes: 1 addition & 2 deletions intercepters/ScoopProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ export class ScoopProxy extends ScoopIntercepter {
* @returns {Buffer}
*/
intercept (type, data, request) {
// Early exit if not recording exchanges or request is blocked
const exchange = this.exchanges.find(ex => ex.requestParsed === request)
if (!exchange) return data
if (!exchange) return data // Early exit if not recording exchanges or request is blocked

const prop = `${type}Raw` // `responseRaw` | `requestRaw`
exchange[prop] = Buffer.concat([exchange[prop], data], exchange[prop].length + data.length)
Expand Down
14 changes: 7 additions & 7 deletions utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as http from 'http'
import * as https from 'https'
import { TLSSocket } from 'tls'
import { URL } from 'url'
import { Transform, PassThrough } from 'node:stream'
import { PassThrough } from 'node:stream'

const defaults = {
requestTransformer: (_request) => new PassThrough(),
Expand Down Expand Up @@ -41,11 +41,7 @@ export function createProxy (options) {
const proxy = http.createServer()

proxy.on('connection', (socket) => {
socket.mirror = new Transform({
transform: (chunk, _encoding, callback) => {
process.nextTick(callback, null, chunk)
}
})
socket.mirror = new PassThrough()
socket.pipe(socket.mirror)
})

Expand All @@ -65,6 +61,10 @@ export function createProxy (options) {
})

proxy.on('request', (request) => {
// clear existing pipes from previous requests
// otherwise the previous request will be sent to the transformer
request.socket.mirror.unpipe()

const urlString = request.url.startsWith('/')
? `${UNKNOWN_PROTOCOL}//${request.headers.host}${request.url}`
: request.url
Expand All @@ -82,7 +82,7 @@ export function createProxy (options) {
httpModule
.request(options)
.on('socket', (socket) => {
request.socket.mirror.pipe(requestTransformer(request)).pipe(socket, { end: false })
request.socket.mirror.pipe(requestTransformer(request)).pipe(socket)
socket.mirror = new PassThrough()
socket.pipe(socket.mirror)
})
Expand Down

0 comments on commit 08c7762

Please sign in to comment.