Skip to content

Commit

Permalink
fix: tunnel server injection
Browse files Browse the repository at this point in the history
- non-html docs were not streaming
- responses with no content-type header were throwing
  • Loading branch information
Roy Razon committed Sep 18, 2023
1 parent 42711d7 commit 6bd90b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 8 additions & 4 deletions tunnel-server/src/proxy/html-manipulation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ export const injectScripts = async (
req: Pick<IncomingMessage, 'headers'>,
res: stream.Writable & Pick<ServerResponse<IncomingMessage>, 'writeHead'>,
) => {
res.writeHead(proxyRes.statusCode as number, proxyRes.headers)

const injectsStr = req.headers[INJECT_SCRIPTS_HEADER] as string | undefined
if (!injectsStr) {
const contentTypeHeader = proxyRes.headers['content-type']

if (!injectsStr || !contentTypeHeader) {
proxyRes.pipe(res)
return undefined
}

const {
type: contentType,
parameters: { charset: reqCharset },
} = parseContentType(proxyRes)
} = parseContentType(contentTypeHeader)

if (contentType !== 'text/html') {
proxyRes.pipe(res)
return undefined
}

res.writeHead(proxyRes.statusCode as number, proxyRes.headers)

const compress = compressionsForContentEncoding(proxyRes.headers['content-encoding'] || 'identity')

const [input, output] = compress
Expand Down
5 changes: 2 additions & 3 deletions tunnel-server/src/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ export const proxy = ({
?.filter(({ pathRegex }) => !pathRegex || pathRegex.test(mutatedReq.url || ''))
?.map(({ src, defer, async }) => ({ src, defer, async }))

const shouldInject = Boolean(injects?.length)
if (shouldInject) {
if (injects?.length) {
mutatedReq.headers[INJECT_SCRIPTS_HEADER] = JSON.stringify(injects)
}

Expand All @@ -153,7 +152,7 @@ export const proxy = ({
target: {
socketPath: activeTunnel.target,
},
selfHandleResponse: shouldInject,
selfHandleResponse: true, // handled by the injectScripts onProxyRes hook
},
err => errorHandler(log, err, req, res)
)
Expand Down

0 comments on commit 6bd90b7

Please sign in to comment.