Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"finalhandler",
"fullhash",
"hono",
"jsonjoy",
"leadinghash",
"memfs",
"middie",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@hapi/hapi": "^21.4.10",
"@hono/node-server": "^2.1.1",
"@jsonjoy.com/fs-core": "4.79.0",
"@rspack/core": "2.2.2",
"@types/express": "^5.0.6",
"@types/node": "^24.13.3",
Expand All @@ -47,7 +48,7 @@
"finalhandler": "^2.1.1",
"hono": "^4.13.5",
"koa": "^3.2.1",
"memfs": "^4.68.2",
"memfs": "4.79.0",
"mrmime": "^2.0.1",
"on-finished": "^2.4.1",
"range-parser": "^1.3.0",
Expand Down
121 changes: 62 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/utils/memoryFileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Node, Superblock } from "@jsonjoy.com/fs-core";
import { Volume, createFsFromVolume } from "memfs";

/**
* Rspack emits complete buffers and truncates files before replacing them.
* Retain those buffers to avoid copying and unused growth capacity. Callers
* must not mutate the input buffer or its aliases after a complete write.
*
* @this {Node}
* @param {Buffer} buffer input buffer
* @param {number} [offset] source offset
* @param {number} [length] number of bytes
* @param {number} [position] file offset
* @returns {number} written bytes
*/
function write(buffer, offset = 0, length = buffer.length, position = 0) {
if (
this.getSize() === 0 &&
position === 0 &&
offset === 0 &&
length === buffer.length &&
length > 0
) {
// The pinned fs-core's private setter preserves metadata and change events.
/** @type {{ _setBuf(buffer: Buffer): void }} */ (
/** @type {unknown} */ (this)
)._setBuf(buffer);
Comment on lines +25 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve write-buffer snapshot semantics

When a plugin or other caller reuses or mutates a Buffer after a synchronous write returns (or after an asynchronous write's callback), _setBuf(buffer) leaves the file backed by that same mutable storage. A later buffer.fill() therefore silently changes the bytes served by the middleware without another filesystem write, metadata update, or watcher event; the previous Node.write path copied the bytes. Because this hook applies to every complete write rather than only buffers whose ownership Rspack can guarantee, the filesystem must preserve the normal post-write snapshot semantics.

Useful? React with 👍 / 👎.

return length;
}

return Node.prototype.write.call(this, buffer, offset, length, position);
}

class OutputSuperblock extends Superblock {
/**
* @param {number} mode file mode
* @returns {Node} file node
*/
createNode(mode) {
const node = super.createNode(mode);
node.write = write;
return node;
}
}

/**
* @returns {import("memfs").IFs} memory file system
*/
function createMemoryFileSystem() {
return createFsFromVolume(new Volume(new OutputSuperblock()));
}

export default createMemoryFileSystem;
18 changes: 2 additions & 16 deletions src/utils/setupOutputFileSystem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memfs, { Volume, createFsFromVolume } from "memfs";
import createMemoryFileSystem from "./memoryFileSystem.js";

/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */
/** @typedef {import("../index.js").DevServerOption} DevServerOption */
Expand All @@ -20,21 +20,7 @@ function setupOutputFileSystem(context) {
}
// Don't use `memfs` when developer wants to write everything to a disk, because it doesn't make sense.
else if (context.options.writeToDisk !== true) {
const candidateOutputFileSystem = memfs.createFsFromVolume(new Volume());

if (
candidateOutputFileSystem &&
typeof candidateOutputFileSystem.readFileSync === "function" &&
typeof candidateOutputFileSystem.statSync === "function"
) {
outputFileSystem = candidateOutputFileSystem;
} else {
outputFileSystem = createFsFromVolume(new Volume());

if (candidateOutputFileSystem) {
Object.assign(outputFileSystem, candidateOutputFileSystem);
}
}
outputFileSystem = createMemoryFileSystem();
} else {
const isMultiCompiler =
/** @type {MultiCompiler} */
Expand Down
23 changes: 23 additions & 0 deletions test/utils/memoryFileSystem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import createMemoryFileSystem from "../../src/utils/memoryFileSystem.js";

it("should write whole files, including overwrites", () => {
const fs = createMemoryFileSystem();
for (const content of ["hello world", "hello", ""]) {
const buffer = Buffer.from(content);
fs.writeFileSync("/asset.js", buffer);

expect(fs.statSync("/asset.js").size).toBe(buffer.length);
expect(fs.readFileSync("/asset.js", "utf8")).toBe(content);
}
});

it("should preserve partial writes, existing contents and appends", () => {
const fs = createMemoryFileSystem();
const fd = fs.openSync("/asset.js", "w+");
fs.writeSync(fd, Buffer.from("abcd"), 1, 2, 2);
fs.writeSync(fd, Buffer.from("X"), 0, 1, 0);
fs.closeSync(fd);
fs.appendFileSync("/asset.js", "!");

expect(fs.readFileSync("/asset.js", "utf8")).toBe("X\0bc!");
});
5 changes: 5 additions & 0 deletions types/utils/memoryFileSystem.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @returns {import("memfs").IFs} memory file system
*/
declare function createMemoryFileSystem(): import("memfs").IFs;
export default createMemoryFileSystem;
Loading