Skip to content

Commit ff4270f

Browse files
author
Miriad
committed
fix: convert Web ReadableStream to Node.js stream for YouTube upload
googleapis expects a Node.js Readable stream with .pipe(), but fetch().body returns a Web ReadableStream. Convert via Buffer + Readable.from() to fix 'b.body.pipe is not a function' error.
1 parent bcc2ccc commit ff4270f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/youtube-upload.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { google } from "googleapis";
2+
import { Readable } from "node:stream";
23

34
const oauth2Client = new google.auth.OAuth2(
45
process.env.YOUTUBE_CLIENT_ID,
@@ -27,6 +28,11 @@ export async function uploadVideo(opts: UploadOptions): Promise<{ videoId: strin
2728
const response = await fetch(opts.videoUrl);
2829
if (!response.ok) throw new Error(`Failed to fetch video: ${response.statusText}`);
2930

31+
// Convert Web ReadableStream to Node.js Readable stream
32+
// googleapis expects a Node.js stream with .pipe(), not a Web ReadableStream
33+
const buffer = Buffer.from(await response.arrayBuffer());
34+
const nodeStream = Readable.from(buffer);
35+
3036
const res = await youtube.videos.insert({
3137
part: ["snippet", "status"],
3238
requestBody: {
@@ -43,7 +49,7 @@ export async function uploadVideo(opts: UploadOptions): Promise<{ videoId: strin
4349
},
4450
},
4551
media: {
46-
body: response.body as unknown as NodeJS.ReadableStream,
52+
body: nodeStream,
4753
},
4854
});
4955

0 commit comments

Comments
 (0)