如何使用 Hono 和 Bun 传输和使用 2 个二进制文件?

问题描述 投票:0回答:1

我需要将文件系统中的 2 个二进制文件从一个 Hono 应用程序传输到另一个 Hono 应用程序。

使用 Bun 和 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,
};

并从另一个应用程序使用它

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" });
});

现在,如何将第二个文件添加到流中?消费者需要读取这两个文件并将它们写入文件系统。

streaming binaryfiles bun hono
1个回答
0
投票

流媒体应用程序

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,
};

另一个应用程序

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;
© www.soinside.com 2019 - 2024. All rights reserved.