-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't download non-HTML pages (#798)
* don't download non-HTML pages * fix timeouts and HTTP headers
- Loading branch information
Showing
4 changed files
with
64 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const got = require('got') | ||
|
||
module.exports = got.default.extend({ | ||
handlers: [ | ||
(options, next) => { | ||
const promiseOrStream = next(options) | ||
|
||
// A destroy function that supports both promises and streams. | ||
// For newer versions, we could use abortcontroller, but alas... | ||
const destroy = message => { | ||
if (options.isStream) { | ||
promiseOrStream.destroy(message) | ||
return | ||
} | ||
|
||
// Also note that got v11 is a fucking troll and won't actually pass on the cancellation reason. | ||
promiseOrStream.cancel(message) | ||
} | ||
|
||
promiseOrStream.on('response', response => { | ||
const contentType = response.headers['content-type'] | ||
|
||
// The goal is to not download *anything* if it's not HTML, | ||
// not only because we can't get metadata from non-HTML responses, | ||
// but also because non-HTML responses may cause us to download some gigantic payload. | ||
if (contentType && contentType.startsWith('text/html')) { | ||
options.context.requestLogger.info( | ||
`Received an HTML page. Returning response as-is.` | ||
) | ||
return | ||
} | ||
|
||
options.context.requestLogger.info( | ||
`Received a non-HTML response. Aborting early.` | ||
) | ||
destroy('Not an HTML response') | ||
}) | ||
|
||
return promiseOrStream | ||
} | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5258166
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blink – ./
blink-janejeon.vercel.app
blink-git-master-janejeon.vercel.app
docs.blink.rest