filehandle.pull([...transforms][, options])
稳定性: 1 - 实验性
...transforms<Function> | <Object> 可通过stream/iter pull()应用的可选转换。options<Object>signal<AbortSignal>autoClose<boolean> 当流结束时关闭文件句柄。 默认值:false。start<number> 开始读取的字节偏移量。当指定时,读取使用显式定位(pread语义)。**默认值:**当前文件位置。limit<number> 在结束迭代器之前要读取的最大字节数。读取将在送达limit字节或达到 EOF 时停止,以先到者为准。**默认值:**读取直到 EOF。chunkSize<number> 为每次读取操作分配的缓冲区大小(字节)。默认值:131072(128 KB)。
- 返回:AsyncIterable
使用node:stream/iter拉取模型将文件内容作为异步可迭代对象返回。读取操作以chunkSize字节为单位进行(默认128 KB)。如果提供了转换,它们将通过stream/iter pull()应用。
🌐 Return the file contents as an async iterable using the
node:stream/iter pull model. Reads are performed in chunkSize-byte
chunks (default 128 KB). If transforms are provided, they are applied
via stream/iter pull().
在迭代器被使用时,文件句柄会被锁定,当迭代完成、发生错误或使用者中止时,锁定会解除。
🌐 The file handle is locked while the iterable is being consumed and unlocked when iteration completes, an error occurs, or the consumer breaks.
此功能仅在启用 --experimental-stream-iter 标志时可用。
🌐 This function is only available when the --experimental-stream-iter flag is
enabled.
import { open } from 'node:fs/promises';
import { text } from 'node:stream/iter';
import { compressGzip } from 'node:zlib/iter';
const fh = await open('input.txt', 'r');
// Read as text
console.log(await text(fh.pull({ autoClose: true })));
// Read 1 KB starting at byte 100
const fh2 = await open('input.txt', 'r');
console.log(await text(fh2.pull({ start: 100, limit: 1024, autoClose: true })));
// Read with compression
const fh3 = await open('input.txt', 'r');
const compressed = fh3.pull(compressGzip(), { autoClose: true });const { open } = require('node:fs/promises');
const { text } = require('node:stream/iter');
const { compressGzip } = require('node:zlib/iter');
async function run() {
const fh = await open('input.txt', 'r');
// Read as text
console.log(await text(fh.pull({ autoClose: true })));
// Read 1 KB starting at byte 100
const fh2 = await open('input.txt', 'r');
console.log(await text(fh2.pull({ start: 100, limit: 1024, autoClose: true })));
// Read with compression
const fh3 = await open('input.txt', 'r');
const compressed = fh3.pull(compressGzip(), { autoClose: true });
}
run().catch(console.error);