npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lazy-node/readlines

v3.0.5

Published

逐行讀取檔案,無需將整個檔案載入記憶體 (Read file line by line without buffering the whole file in memory)

Downloads

282

Readme

@lazy-node/readlines

逐行讀取檔案,無需將整個檔案載入記憶體。 Read file line by line without buffering the whole file in memory.

在 Node.js 中逐行讀取檔案看似簡單,但沒有直接的方法可以做到。許多函式庫使用 Transform Streams 來實現,但這似乎過於複雜。因此這個函式庫只使用 Node.js 的 filesystem 模組來達成相同目的。請注意這是同步函式庫。

Reading file line by line may seem like a trivial problem, but in Node.js, there is no straightforward way to do it. Many libraries use Transform Streams to achieve it, but it seems like overkill. This library uses only the filesystem module of Node.js. Note that this is a synchronous library.

安裝 (Installation)

# 使用 yarn / Using yarn
yarn add @lazy-node/readlines

# 使用 yarn-tool / Using yarn-tool
yarn-tool add @lazy-node/readlines
# yt 是 yarn-tool 的別名 / yt is an alias for yarn-tool
yt add @lazy-node/readlines

# 使用 pnpm / Using pnpm
pnpm add @lazy-node/readlines

# 使用 npm / Using npm
npm install @lazy-node/readlines

文件 (Documentation)

new LineByLine(filename, [options])

new LineByLine(fd, [options])

建立逐行讀取器實例。 Create a line-by-line reader instance.

參數 (Arguments)

| 參數 | 類型 | 說明 | |------|------|------| | filename | string | 要讀取的檔案路徑 / Path to the file you want to read from | | fd | number | 檔案描述符 / File descriptor | | options | object | 選項物件 / Options object | | options.readChunk | number | 每次讀取的位元組數。預設:1024 / Number of bytes to read at once. Default: 1024 | | options.newLineCharacter | string | 換行字元,目前只支援單一位元組字元。預設:\n (0x0a) / New line character, only supports single byte characters for now. Default: \n which is 0x0a hex encoded |

@lazy-node/readlines 可以處理最後一行後面沒有換行字元的檔案。 @lazy-node/readlines can handle files without newLineCharacter after the last line.


readlines.next()

讀取下一行。 Read the next line.

回傳 (Returns)

  • Buffer - 行資料(不包含換行字元)/ Line data without the newLineCharacter
  • undefined - 已到達檔案結尾 / End of file is reached

readlines.reset()

重置讀取器,從檔案開頭重新讀取。只有在未到達結尾時才有效。 Resets the pointer and starts from the beginning of the file. This works only if the end is not reached.


readlines.close()

手動關閉開啟的檔案,後續的 next() 呼叫將回傳 false。只有在未到達結尾時才有效。 Manually close the open file, subsequent next() calls will return false. This works only if the end is not reached.


readlines.generator()

產生器函式,用於迭代讀取所有行。 Generator function for iterating through all lines.

回傳 (Returns)

  • Generator<Buffer> - 行資料產生器 / Line data generator

LineByLine.generator(file, [options])

靜態產生器函式,建立實例並迭代讀取所有行。 Static generator function to create instance and iterate through all lines.


屬性 (Properties)

| 屬性 | 類型 | 說明 | |------|------|------| | file | IPathLike | 檔案路徑或描述符 / File path or descriptor | | fd | number | 檔案描述符 / File descriptor | | options | IOptions | 讀取選項 / Read options | | fdPosition | number | 當前檔案讀取位置 / Current file read position | | newLineCharacter | Buffer | 換行字元 Buffer / New line character buffer | | eofReached | boolean | 是否已到達檔案結尾 / Whether end of file is reached | | lineNumber | number | 當前行號(從 0 開始)/ Current line number (0-based) |


使用範例 (Example)

const LineByLine = require('@lazy-node/readlines');
const liner = new LineByLine('./test/fixtures/normalFile.txt');

let line;
let lineNumber = 0;

// 使用 next() 逐行讀取 / Read line by line using next()
while (line = liner.next()) {
    console.log('Line ' + lineNumber + ': ' + line.toString());
    lineNumber++;
}

console.log('end of line reached');

使用產生器 (Using Generator)

const LineByLine = require('@lazy-node/readlines');

// 使用實例產生器 / Using instance generator
const liner = new LineByLine('./test/fixtures/normalFile.txt');
for (const line of liner.generator()) {
    console.log(line.toString());
}

// 使用靜態產生器 / Using static generator
for (const line of LineByLine.generator('./test/fixtures/normalFile.txt')) {
    console.log(line.toString());
}

相關連結 (Links)

授權 (License)

MIT