link-preview-js icon indicating copy to clipboard operation
link-preview-js copied to clipboard

Timeout not respected

Open wouterlemcke opened this issue 1 year ago • 6 comments

We noticed the configured timeout is not respected.

await getLinkPreview(url, {
    imagesPropertyType: 'og',
    headers: {
       'user-agent': 'googlebot'
    },
    timeout: 1000,
}))

When trying to generate a link preview for for example https://video013.kerkomroep.nl/pages/afspelen.php?mp=11115&file=2022-07-03-08-41-15.mp4 it never times out.

The weird thing is that when I set timeout to 10 it works, but as soon as I put it on 100 or above it just freezes.

wouterlemcke avatar Jul 07 '22 11:07 wouterlemcke

That's weird, the timeout code is very simple... maybe you are running it in an environment where the AbortController is not supported?

In any case, it would be great if you can post a reproducible example.

Screenshot 000728

ospfranco avatar Jul 07 '22 14:07 ospfranco

I can see the tests fail on the github runner, but on my local machine they work fine, so it probably has something to do with the node version. ping @wouterlemcke, can you provide more info where are you running this? node? which version? etc.

ospfranco avatar Jul 11 '22 05:07 ospfranco

Sorry for the slow response, I was on holiday 😄 We are running Node 14.19.3 on AWS EC2s, but also locally on my Macbook (running Node 14.19.0) I can reproduce it

wouterlemcke avatar Aug 02 '22 09:08 wouterlemcke

Welp 🤷‍♂️

Screenshot 000817

Kinda torn on this though... no idea if there is any other mechanism to timeout a fetch API request

ospfranco avatar Aug 02 '22 09:08 ospfranco

Haha, that's annoying. I can share my temp. solution:

const linkPreview = (await Promise.race([
                getLinkPreview(url, {
                    imagesPropertyType: 'og',
                    headers,
                    timeout,
                    followRedirects: 'manual',
                    handleRedirects: (baseURL: string, forwardedURL: string) => {
                        return LinkPreviewRedirect.isWhiteListed(baseURL, forwardedURL)
                    },
                    resolveDNSHost: async (url: string) => {
                        const hostname = new URL(url).hostname
                        const result = await dns.promises.lookup(hostname)
                        return result.address
                    },
                }),
                new Promise((resolve, reject) => {
                    setTimeout(() => {
                        reject(new GeneratingLinkPreviewFailedException('Timeout'))
                    }, timeout)
                }),
            ]))

wouterlemcke avatar Aug 02 '22 12:08 wouterlemcke

ospfranco avatar Aug 02 '22 12:08 ospfranco

Going to close this, node 14 has already reached eol on active support (despite security patches). And there is the existing workaround you posted.

ospfranco avatar Sep 10 '22 12:09 ospfranco