Files
node/test/parallel/test-vfs-fs-createReadStream.js
T
Matteo CollinaandJames M Snell 4d9cb71fa2 vfs: integrate with CJS and ESM module loaders
Co-authored-by: James M Snell <jasnell@gmail.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63653
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-08-31 10:31:16 +02:00

57 lines
1.7 KiB
JavaScript

// Flags: --experimental-vfs
'use strict';
// fs.createReadStream dispatches through VFS, including the emitted 'open'
// event with the bitmask-encoded virtual fd.
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vfs = require('node:vfs');
function mounted() {
const myVfs = vfs.create();
myVfs.mkdirSync('/src', { recursive: true });
myVfs.writeFileSync('/src/hello.txt', 'hello world');
const mountPoint = myVfs.mount();
return { myVfs, mountPoint };
}
// Whole-file read
{
const { myVfs, mountPoint } = mounted();
const chunks = [];
const stream = fs.createReadStream(path.join(mountPoint, 'src/hello.txt'));
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('end', common.mustCall(() => {
assert.strictEqual(Buffer.concat(chunks).toString(), 'hello world');
myVfs.unmount();
}));
}
// Slice with start + end (inclusive)
{
const { myVfs, mountPoint } = mounted();
const chunks = [];
const stream = fs.createReadStream(path.join(mountPoint, 'src/hello.txt'),
{ start: 0, end: 4 });
assert.strictEqual(stream.path, path.join(mountPoint, 'src/hello.txt'));
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('end', common.mustCall(() => {
assert.strictEqual(Buffer.concat(chunks).toString(), 'hello');
myVfs.unmount();
}));
}
// 'open' event fires with a VFS fd
{
const { myVfs, mountPoint } = mounted();
const stream = fs.createReadStream(path.join(mountPoint, 'src/hello.txt'));
stream.on('open', common.mustCall((fd) => {
assert.notStrictEqual(fd & 0x40000000, 0);
}));
stream.on('end', common.mustCall(() => myVfs.unmount()));
stream.resume();
}