Your worker created multiple branches of a single stream (for instance, by calling response.clone()

I am getting this when trying to use a cloudflare worker together with the uploadthing library. I have debugged in VS-code and the error is thrown here when uploadthing library calls the safeParseJSON function: https://github.com/pingdotgg/uploadthing/blob/ea0f3c96f5db2fa945592d5efc06bf84811c5525/packages/shared/src/utils.ts#L233 Here is link to my cloudflare worker: https://github.com/kristiankauffeld/uploadthing-cf-worker
4 Replies
kian
kian13mo ago
Seems like a valid warning - it clones but only tries to read the second branch in the event of an error.
kristiankauffeld
kristiankauffeldOP13mo ago
@kian it throws the warning even though I commented the cloning part out:
export async function safeParseJSON<T>(
input: string | ResponseEsque | RequestLike,
): Promise<T | Error> {
if (typeof input === "string") {
try {
return JSON.parse(input) as T;
} catch (err) {
console.error(`Error parsing JSON, got '${input}'`);
return new Error(`Error parsing JSON, got '${input}'`);
}
}

//const clonedRes = input.clone?.();
try {
return (await input.json()) as T;
} catch (err) {
//const text = (await input?.text()) ?? "unknown";
//const text = await input?.text()) ?? "unknown";
//console.error(`Error parsing JSON, got '${text}'`);
return new Error(`Error parsing JSON, got '${input}'`);
}
}
export async function safeParseJSON<T>(
input: string | ResponseEsque | RequestLike,
): Promise<T | Error> {
if (typeof input === "string") {
try {
return JSON.parse(input) as T;
} catch (err) {
console.error(`Error parsing JSON, got '${input}'`);
return new Error(`Error parsing JSON, got '${input}'`);
}
}

//const clonedRes = input.clone?.();
try {
return (await input.json()) as T;
} catch (err) {
//const text = (await input?.text()) ?? "unknown";
//const text = await input?.text()) ?? "unknown";
//console.error(`Error parsing JSON, got '${text}'`);
return new Error(`Error parsing JSON, got '${input}'`);
}
}
kian
kian13mo ago
Look for other usages of clone
kristiankauffeld
kristiankauffeldOP13mo ago
the only other place I can find a usage of clone is here, but the warning is thrown before this is reached: https://github.com/pingdotgg/uploadthing/blob/ea0f3c96f5db2fa945592d5efc06bf84811c5525/packages/uploadthing/src/sdk/utils.ts#L85
Want results from more Discord servers?
Add your server