{"_id":"@alexssmusica/peek-readable","name":"@alexssmusica/peek-readable","dist-tags":{"latest":"5.0.0"},"versions":{"5.0.0":{"name":"@alexssmusica/peek-readable","version":"5.0.0","description":"Read and peek from a readable stream","author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":{"type":"github","url":"https://github.com/sponsors/Borewit"},"scripts":{"clean":"del-cli lib/**/*.js lib/**/*.js.map lib/**/*.d.ts test/**/*.js test/**/*.js.map coverage .nyc_output","build":"npm run clean && npm run compile","compile-src":"tsc -p lib","compile-test":"tsc -p test","compile":"npm run compile-src && yarn run compile-test","eslint":"eslint lib test --ext .ts --ignore-pattern *.d.ts","lint-md":"remark -u preset-lint-recommended .","lint":"npm run lint-md && npm run eslint","test":"mocha","test-coverage":"c8 npm run test","start":"npm run compile && npm run lint && npm run cover-test"},"engines":{"node":">=14.16"},"repository":{"type":"git","url":"git+https://github.com/Borewit/peek-readable.git"},"license":"MIT","exports":"./lib/index.js","types":"lib/index.d.ts","bugs":{"url":"https://github.com/Borewit/peek-readable/issues"},"devDependencies":{"@types/chai":"^4.3.4","@types/mocha":"^10.0.1","@types/node":"^18.11.13","@typescript-eslint/eslint-plugin":"^4.33.0","@typescript-eslint/parser":"^4.33.0","c8":"^7.12.0","chai":"^4.3.7","del-cli":"^5.0.0","eslint":"^7.32.0","eslint-config-prettier":"^8.5.0","eslint-import-resolver-typescript":"^3.5.2","eslint-plugin-import":"^2.26.0","eslint-plugin-jsdoc":"^39.6.2","eslint-plugin-node":"^11.1.0","eslint-plugin-unicorn":"^40.1.0","mocha":"^10.1.0","remark-cli":"^11.0.0","remark-preset-lint-recommended":"^6.1.2","ts-node":"^10.9.1","typescript":"^4.9.4"},"keywords":["readable","buffer","stream","read"],"gitHead":"174a035a513f47ebdc19464cde9a1d28c2bb6581","homepage":"https://github.com/Borewit/peek-readable#readme","_id":"@alexssmusica/peek-readable@5.0.0","_nodeVersion":"14.19.3","_npmVersion":"6.14.17","dist":{"integrity":"sha512-N0lZchN2QoAhsg9lNVWMidbJTE7UP2Xt/YdVTya8QtZy+i8SvNinN/IUE7kCLPEddj7GubJsvxgWoWZeXeLXqA==","shasum":"d0cd779ac258150611e6f67471de1dac85550f20","tarball":"https://registry.npmjs.org/@alexssmusica/peek-readable/-/peek-readable-5.0.0.tgz","fileCount":11,"unpackedSize":16237,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCZ0RC7n/xmGQa4xiLx++1kKWpnVsZu9Kl2wLhZfRBh2gIgWeDCouYyIvA/eNGY+i+RMLjH6831+zG+pkhiRoCS294="}]},"_npmUser":{"name":"alexssmusica","email":"alexssmusica@gmail.com"},"directories":{},"maintainers":[{"name":"alexssmusica","email":"alexssmusica@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/peek-readable_5.0.0_1690209894430_0.3523281779322964"},"_hasShrinkwrap":false}},"time":{"created":"2023-07-24T14:44:54.342Z","5.0.0":"2023-07-24T14:44:54.591Z","modified":"2023-07-24T14:44:54.935Z"},"maintainers":[{"name":"alexssmusica","email":"alexssmusica@gmail.com"}],"description":"Read and peek from a readable stream","homepage":"https://github.com/Borewit/peek-readable#readme","keywords":["readable","buffer","stream","read"],"repository":{"type":"git","url":"git+https://github.com/Borewit/peek-readable.git"},"author":{"name":"Borewit","url":"https://github.com/Borewit"},"bugs":{"url":"https://github.com/Borewit/peek-readable/issues"},"license":"MIT","readme":"![Node.js CI](https://github.com/Borewit/peek-readable/workflows/Node.js%20CI/badge.svg)\r\n[![NPM version](https://badge.fury.io/js/peek-readable.svg)](https://npmjs.org/package/peek-readable)\r\n[![npm downloads](http://img.shields.io/npm/dm/peek-readable.svg)](https://npmcharts.com/compare/peek-readable?start=600&interval=30)\r\n[![Coverage Status](https://coveralls.io/repos/github/Borewit/peek-readable/badge.svg?branch=master)](https://coveralls.io/github/Borewit/peek-readable?branch=master)\r\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d4b511481b3a4634b6ca5c0724407eb9)](https://www.codacy.com/gh/Borewit/peek-readable/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=Borewit/peek-readable&amp;utm_campaign=Badge_Grade)\r\n[![Total alerts](https://img.shields.io/lgtm/alerts/g/Borewit/peek-readable.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/peek-readable/alerts/)\r\n[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Borewit/peek-readable.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/peek-readable/context:javascript)\r\n[![Known Vulnerabilities](https://snyk.io/test/github/Borewit/peek-readable/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/peek-readable?targetFile=package.json)\r\n[![CodeQL](https://github.com/Borewit/peek-readable/actions/workflows/codeql.yml/badge.svg)](https://github.com/Borewit/peek-readable/actions/workflows/codeql.yml)\r\n\r\n# peek-readable\r\n\r\nA promise based asynchronous stream reader, which makes reading from a stream easy.\r\n\r\nAllows to read and peek from a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams) \r\n\r\nNote that [peek-readable](https://github.com/Borewit/peek-readable) was formally released as [then-read-stream](https://github.com/Borewit/peek-readable).\r\n\r\n## Usage\r\n\r\n### Installation\r\n\r\n```shell script\r\nnpm install --save peek-readable\r\n```\r\n\r\nThe `peek-readable` contains one class: `StreamReader`, which reads from a [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable).\r\n\r\n### Compatibility\r\n\r\nModule: version 5 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).\r\nJavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).\r\nRequires Node.js ≥ 14.16 engine.\r\n\r\n## Examples\r\n\r\nIn the following example we read the first 16 bytes from a stream and store them in our buffer.\r\nSource code of examples can be found [here](test/examples.ts).\r\n\r\n```js\r\nimport fs from 'node:fs';\r\nimport { StreamReader } from 'peek-readable';\r\n\r\n(async () => {\r\n  const readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\r\n  const streamReader = new StreamReader(readable);\r\n  const uint8Array = new Uint8Array(16);\r\n  const bytesRead = await streamReader.read(uint8Array, 0, 16);;\r\n  // buffer contains 16 bytes, if the end-of-stream has not been reached\r\n})();\r\n```\r\n\r\nEnd-of-stream detection:\r\n```js\r\n(async () => {\r\n\r\n  const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\r\n  const streamReader = new StreamReader(fileReadStream);\r\n  const buffer = Buffer.alloc(16); // or use: new Uint8Array(16);\r\n\r\n  try {\r\n    await streamReader.read(buffer, 0, 16);\r\n    // buffer contains 16 bytes, if the end-of-stream has not been reached\r\n  } catch(error) {\r\n    if (error instanceof EndOfStreamError) {\r\n      console.log('End-of-stream reached');\r\n    }\r\n  }\r\n})();\r\n```\r\n\r\nWith peek you can read ahead:\r\n```js\r\nimport fs from 'node:fs';\r\nimport { StreamReader } from 'peek-readable';\r\n\r\nconst fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\r\nconst streamReader = new StreamReader(fileReadStream);\r\nconst buffer = Buffer.alloc(20);\r\n\r\n(async () => {\r\n  let bytesRead = await streamReader.peek(buffer, 0, 3);\r\n  if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {\r\n    console.log('This is a JPEG file');\r\n  } else {\r\n    throw Error('Expected a JPEG file');\r\n  }\r\n\r\n  bytesRead = await streamReader.read(buffer, 0, 20); // Read JPEG header\r\n  if (bytesRead === 20) {\r\n    console.log('Got the JPEG header');\r\n  } else {\r\n    throw Error('Failed to read JPEG header');\r\n  }\r\n})();\r\n```\r\n","readmeFilename":"README.md"}