{"id":18083354,"url":"https://github.com/leizongmin/node-lei-stream","last_synced_at":"2025-06-23T00:02:18.296Z","repository":{"id":32414911,"uuid":"35991886","full_name":"leizongmin/node-lei-stream","owner":"leizongmin","description":"Read/Write stream line by line 按行读写流","archived":false,"fork":false,"pushed_at":"2020-01-14T15:54:48.000Z","size":3772,"stargazers_count":100,"open_issues_count":5,"forks_count":18,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-10T09:05:21.427Z","etag":null,"topics":["nodejs","nodejs-modules","stream"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leizongmin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-21T04:59:59.000Z","updated_at":"2024-05-14T15:59:50.000Z","dependencies_parsed_at":"2022-07-08T12:18:33.467Z","dependency_job_id":null,"html_url":"https://github.com/leizongmin/node-lei-stream","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/leizongmin/node-lei-stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fnode-lei-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fnode-lei-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fnode-lei-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fnode-lei-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leizongmin","download_url":"https://codeload.github.com/leizongmin/node-lei-stream/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fnode-lei-stream/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261093056,"owners_count":23108580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","nodejs-modules","stream"],"created_at":"2024-10-31T14:07:38.598Z","updated_at":"2025-06-23T00:02:13.276Z","avatar_url":"https://github.com/leizongmin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![David deps][david-image]][david-url]\n[![node version][node-image]][node-url]\n[![npm download][download-image]][download-url]\n[![npm license][license-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/lei-stream.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/lei-stream\n[travis-image]: https://img.shields.io/travis/leizongmin/node-lei-stream.svg?style=flat-square\n[travis-url]: https://travis-ci.org/leizongmin/node-lei-stream\n[coveralls-image]: https://img.shields.io/coveralls/leizongmin/node-lei-stream.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/leizongmin/node-lei-stream?branch=master\n[david-image]: https://img.shields.io/david/leizongmin/node-lei-stream.svg?style=flat-square\n[david-url]: https://david-dm.org/leizongmin/node-lei-stream\n[node-image]: https://img.shields.io/badge/node.js-%3E=_4.0-green.svg?style=flat-square\n[node-url]: http://nodejs.org/download/\n[download-image]: https://img.shields.io/npm/dm/lei-stream.svg?style=flat-square\n[download-url]: https://npmjs.org/package/lei-stream\n[license-image]: https://img.shields.io/npm/l/lei-stream.svg\n\n# lei-stream\nRead/Write Stream line by line 按行读写流\n## 安装\n\n```bash\n$ npm install lei-stream --save\n```\n\n**需要 Node.js v4.0.0 或更高版本**\n\n\n## readLine 按行读取流\n\n使用方法1：\n\n```javascript\n'use strict';\n\nconst readLine = require('lei-stream').readLine;\n\nreadLine('./myfile.txt').go((data, next) =\u003e {\n  console.log(data);\n  next();\n}, function () {\n  console.log('end');\n});\n```\n\n使用方法2：\n\n```javascript\n'use strict';\n\nconst fs = require('fs');\nconst readLine = require('lei-stream').readLine;\n\n// readLineStream第一个参数为ReadStream实例，也可以为文件名\nconst s = readLine(fs.createReadStream('./myfile.txt'), {\n  // 换行符，默认\\n\n  newline: '\\n',\n  // 是否自动读取下一行，默认false\n  autoNext: false,\n  // 编码器，可以为函数或字符串（内置编码器：json，base64），默认null\n  encoding: function (data) {\n    return JSON.parse(data);\n  }\n});\n\n// 读取到一行数据时触发data事件\ns.on('data', (data) =\u003e {\n  console.log(data);\n  s.next();\n});\n\n// 流结束时触发end事件\ns.on('end', () =\u003e {\n  console.log('end');\n});\n\n// 读取时出错\ns.on('error', (err) =\u003e {\n  console.error(err);\n});\n```\n\n## writeLine\n\n按行写流\n\n```javascript\n'use strict';\n\nconst fs = require('fs');\nconst writeLineStream = require('lei-stream').writeLine;\n\n// writeLineStream第一个参数为WriteStream实例，也可以为文件名\nconst s = writeLineStream(fs.createWriteStream('./myfile.txt'), {\n  // 换行符，默认\\n\n  newline: '\\n',\n  // 编码器，可以为函数或字符串（内置编码器：json，base64），默认null\n  encoding: function (data) {\n    return JSON.stringify(data);\n  },\n  // 缓存的行数，默认为0（表示不缓存），此选项主要用于优化写文件性能，写入的内容会先存储到缓存中，当内容超过指定数量时再一次性写入到流中，可以提高写速度\n  cacheLines: 0\n});\n\n// 写一行\ns.write(data, () =\u003e {\n  // 回调函数可选\n  console.log('wrote');\n});\n\n// 结束\ns.end(() =\u003e {\n  // 回调函数可选\n  console.log('end');\n});\n\n// 写时出错\ns.on('error', (err) =\u003e {\n  console.error(err);\n});\n```\n\n\n## TailStream\n\n监听文件新增内容变化的流（返回的是一个`Readable Stream`,具体可参考 Node.js API 文档）\n\n```javascript\n'use strict';\n\nconst tailStream = require('lei-stream').tailStream;\n\n// 创建流\nconst s = tailStream('./myfile.txt', {\n  position: 'end', // end表示监听之前先定位到文件末尾,否则会先读取出文件之前的所有内容再开始监听\n});\n\n// 有新内容会触发data事件\ns.on('data', data =\u003e {\n  console.log(data);\n});\n````\n\n\n\n## License\n\n```\nThe MIT License (MIT)\n\nCopyright (c) 2015-2017 Zongmin Lei \u003cleizongmin@gmail.com\u003e\nhttp://ucdok.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fnode-lei-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleizongmin%2Fnode-lei-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fnode-lei-stream/lists"}