oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.09k stars 2.67k forks source link

Bun.file:Memory observation mode【文件加载到内存并监视变化】 #13293

Open vamsaya opened 1 month ago

vamsaya commented 1 month ago

What is the problem this feature would solve?

In Bun.serve, when a request is index.html, it has to be read from disk every time, and the response speed is not very fast. If you load index.html content into memory, the response speed is at least 3 times faster.

What is the feature you are proposing to solve the problem?

However, if the index.html content changes, the content that has been loaded into memory does not change with it. So is it possible to automatically update disk content changes to memory in Bun.file?

const file = Bun.file('index.html', { loadIntoMemory: true, onchange: true });

Bun.serve({
    fetch(request) {
        return new Response(file);
    }
});

What alternatives have you considered?

No response

KilianB commented 3 weeks ago

There isn't anything available out of the box but you can quickly create this functionality yourself.

import fs from "node:fs/promises";

const filename = "./index.html";

let file = Bun.file(filename);

const itterator = fs.watch(filename);

for await (const event of itterator) {
  // Care you should deduplicate updates. We might get 2 change events.
  file = Bun.file(filename);
}