You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importfsfrom'node:fs';importgotfrom'got';letwriteStream;constfn=retryStream=>{constoptions={headers: {foo: 'bar'},};conststream=retryStream??got.stream('https://example.com',options);if(writeStream){writeStream.destroy();}writeStream=fs.createWriteStream('example-com.html');stream.pipe(writeStream);// If you don't attach the listener, it will NOT make a retry.// It automatically checks the listener count so it knows whether to retry or not :)stream.once('retry',(retryCount,error,createRetryStream)=>{fn(createRetryStream());// or: fn(createRetryStream(optionsToMerge))});};fn();
I am implementing this for Twitter's search stream, which has a tendency to disconnect at times.
The problem comes when the retry is triggered - createRetryStream is undefined and causes a type error when it attempts to execute it.
My code:
functionopenStream(retryStream){conststream=retryStream??got.stream(`${apiUrl}tweets/search/stream?expansions=author_id,referenced_tweets.id.author_id&user.fields=id,name,username,profile_image_url`,{
headers,responseType: 'json',})stream.on('response',()=>{log.info('twitter stream open')})stream.on('data',async(buffer)=>{try{constdata=JSON.parse(buffer)notify.twitter(data)}catch(error){// Heartbeats}})stream.once('retry',(retryCount,error,createRetryStream)=>{// createRetryStream is undefinedlog.error({ error, retryCount },'twitter stream retry')openStream(createRetryStream())// this throws type error })stream.on('error',(error)=>{log.error({ error },'twitter stream error')})}openStream()
For further context, I tested this by disconnecting my wifi to simulate an outage and it throws an ECONNRESET error, which does trigger the retry hook.
I am not sure if this is working as designed or a bug (in my code or in got). Appreciate your attention and any help here, thanks in advance.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, looking for best practices on implementing a reconnect after a stream is disconnected, and running into a snag w/ the provided documentation.
Referenced documentation:
https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md#createretrystream
Referenced code:
I am implementing this for Twitter's search stream, which has a tendency to disconnect at times.
The problem comes when the
retry
is triggered -createRetryStream
isundefined
and causes a type error when it attempts to execute it.My code:
For further context, I tested this by disconnecting my wifi to simulate an outage and it throws an
ECONNRESET
error, which does trigger theretry
hook.I am not sure if this is working as designed or a bug (in my code or in
got
). Appreciate your attention and any help here, thanks in advance.Beta Was this translation helpful? Give feedback.
All reactions