How to stream and consume 2 binary files with Hono and Bun?

I need to stream 2 binary files from the filesystem from a Hono app to another Hono app.

Doing just one file is very easy with Bun and Hono Streaming API.

// Streaming app
import { Hono } from "hono";
import { stream } from "hono/streaming";

const app = new Hono();

app.get("/", (c) => {
    return stream(c, async (stream) => {
        // Write a process to be executed when aborted.
        stream.onAbort(() => {
            console.log("Aborted!");
        });

        // Pipe the video stream.
        const videoFile = Bun.file("./data/worker/video.mp4");
        console.log("Streaming video file...");
        await stream.pipe(videoFile.stream());

        // Pipe the audio stream
        // const audioFile = Bun.file("./data/worker/audio.mp4");
        // console.log("Streaming audio file...");
        // await stream.pipe(audioFile.stream());

        console.log("Done!");
    });
});

console.log("Worker is running on port 3001");

export default {
    port: 3001,
    fetch: app.fetch,
};

And consuming it from another app

app.get("/", async (c) => {
    console.log("Streaming files from worker...");

    const response = await fetch("http://localhost:3001/");

    if (!response.ok) {
        return c.json({ error: "Failed to stream files from worker" }, 500);
    }

    await Bun.write("./data/output/video.mp4", response.body);

    return c.json({ message: "File streamed successfully" });
});

Now, how do I add a second file to the stream? The consumer will need to read both files and write them to the filesystem.

1

Streaming App

import { Hono } from "hono";
import { stream } from "hono/streaming";

const app = new Hono();

app.get("/", (c) => {
    return stream(c, async (stream) => {
        stream.onAbort(() => {
            console.log("Aborted!");
        });

        // Pipe the first video file
        const videoFile = Bun.file("./data/worker/video.mp4");
        console.log("Streaming video file...");
        await stream.pipe(videoFile.stream());

        // Send a delimiter or an identifier to signal the end of the first file
        await stream.write(new TextEncoder().encode("n--file-boundary--n"));

        // Pipe the second audio file
        const audioFile = Bun.file("./data/worker/audio.mp4");
        console.log("Streaming audio file...");
        await stream.pipe(audioFile.stream());

        console.log("Done streaming both files!");
    });
});

console.log("Worker is running on port 3001");

export default {
    port: 3001,
    fetch: app.fetch,
};

The other app

import { Hono } from "hono";
import { Bun } from "bun"; // Import Bun for filesystem handling if needed.

const app = new Hono();

app.get("/", async (c) => {
    console.log("Streaming files from worker...");

    const response = await fetch("http://localhost:3001/");
    if (!response.ok) {
        return c.json({ error: "Failed to stream files from worker" }, 500);
    }

    const reader = response.body.getReader();
    const decoder = new TextDecoder();
    let buffer = "";
    let isVideo = true; // Start with the assumption that the first file is the video

    while (true) {
        const { done, value } = await reader.read();
        if (done) break;

        // Append the chunk to the buffer
        buffer += decoder.decode(value, { stream: true });

        // Check for file boundary
        if (buffer.includes("--file-boundary--")) {
            const [firstFilePart, secondFilePart] = buffer.split("--file-boundary--");

            // Write the first file part to disk
            const firstFile = new TextEncoder().encode(firstFilePart.trim());
            await Bun.write(isVideo ? "./data/output/video.mp4" : "./data/output/audio.mp4", firstFile);

            // Set up for the next file
            buffer = secondFilePart;
            isVideo = !isVideo;
        }
    }

    // Write any remaining part to the second file
    if (buffer) {
        const finalFile = new TextEncoder().encode(buffer.trim());
        await Bun.write(isVideo ? "./data/output/video.mp4" : "./data/output/audio.mp4", finalFile);
    }

    return c.json({ message: "Both files streamed successfully" });
});

export default app;

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật