|
| 1 | +import { NextApiRequest, NextApiResponse } from 'next' |
| 2 | +import fetch from 'isomorphic-unfetch' |
| 3 | +import stream, { Stream } from 'stream' |
| 4 | +import merge from 'lodash.merge' |
| 5 | +import UserAgent from 'user-agents' |
| 6 | +import { DeepPartial, Options } from './types' |
| 7 | + |
| 8 | +export function withImageProxy(passedOptions?: DeepPartial<Options>) { |
| 9 | + const defaultOptions: Options = { |
| 10 | + whitelistedPatterns: [], |
| 11 | + fallbackUrl: '', |
| 12 | + messages: { |
| 13 | + wrongFormat: 'Image url not provided or has wrong format', |
| 14 | + notWhitelisted: 'Provided image url is not whitelisted', |
| 15 | + imageFetchError: "Couldn't fetch the image", |
| 16 | + }, |
| 17 | + } |
| 18 | + |
| 19 | + const options: Options = merge(defaultOptions, passedOptions) |
| 20 | + |
| 21 | + return async function (req: NextApiRequest, res: NextApiResponse) { |
| 22 | + const imageUrl = req.query.imageUrl |
| 23 | + |
| 24 | + if (!imageUrl || (imageUrl && Array.isArray(imageUrl))) { |
| 25 | + res.status(400).send({ message: options.messages.wrongFormat }) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + const isAllowed = isUrlWhitelisted(imageUrl) |
| 30 | + |
| 31 | + if (!isAllowed) { |
| 32 | + res.status(422).send({ message: options.messages.notWhitelisted }) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const imageBlob = await fetchImageBlob(imageUrl) |
| 37 | + |
| 38 | + if (!imageBlob) { |
| 39 | + handleFallback() |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + pipeImage(imageBlob) |
| 44 | + |
| 45 | + function pipeImage(imageBlob: ReadableStream<Uint8Array>) { |
| 46 | + const passThrough = new Stream.PassThrough() |
| 47 | + |
| 48 | + stream.pipeline(imageBlob as unknown as NodeJS.ReadableStream, passThrough, (err) => { |
| 49 | + if (err) { |
| 50 | + console.log(err) |
| 51 | + handleFallback() |
| 52 | + return |
| 53 | + } |
| 54 | + }) |
| 55 | + passThrough.pipe(res) |
| 56 | + } |
| 57 | + |
| 58 | + function handleFallback() { |
| 59 | + if (options.fallbackUrl.trim()) { |
| 60 | + res.redirect(options.fallbackUrl) |
| 61 | + } else { |
| 62 | + res.status(422).send({ message: options.messages.imageFetchError }) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + async function fetchImageBlob(url: string) { |
| 67 | + return await fetch(url, { |
| 68 | + headers: { 'user-agent': new UserAgent().toString() }, |
| 69 | + }).then((data) => data.body) |
| 70 | + } |
| 71 | + |
| 72 | + function isUrlWhitelisted(url: string) { |
| 73 | + return options.whitelistedPatterns.some((singleHost) => { |
| 74 | + return url.match(singleHost) |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments