{"_id":"@cjser/peek-readable__v5_4_2","name":"@cjser/peek-readable__v5_4_2","dist-tags":{"latest":"5.4.2-cjser.2"},"versions":{"5.4.2-cjser.2":{"name":"@cjser/peek-readable__v5_4_2","version":"5.4.2-cjser.2","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":"yarn run compile-src && yarn run compile-test","lint-ts":"biome check","lint-md":"remark -u preset-lint-recommended .","lint":"yarn run lint-md && yarn run lint-ts","test":"mocha","test-coverage":"c8 npm run test","start":"yarn run compile && yarn run lint && yarn run cover-test"},"engines":{"node":">=14.16"},"repository":{"type":"git","url":"https://code.moenext.com/3rdeye/cjser.git"},"license":"MIT","type":"module","exports":{"require":"./dist-cjser/index.cjs","default":"./lib/index.js"},"types":"lib/index.d.ts","bugs":{"url":"https://github.com/Borewit/peek-readable/issues"},"devDependencies":{"@biomejs/biome":"1.9.4","@types/chai":"^5.0.1","@types/chai-as-promised":"^8.0.1","@types/mocha":"^10.0.10","@types/node":"^22.10.10","c8":"^10.1.3","chai":"^5.1.2","chai-as-promised":"^8.0.1","del-cli":"^6.0.0","mocha":"^11.1.0","remark-cli":"^12.0.1","remark-preset-lint-recommended":"^7.0.0","source-map-support":"^0.5.21","ts-node":"^10.9.2","typescript":"^5.7.3"},"keywords":["readable","buffer","stream","read"],"packageManager":"yarn@4.6.0","main":"./dist-cjser/index.cjs","cjser":{"sourceVersion":"5.4.2","cjserVersion":2,"original":{"name":"peek-readable","version":"5.4.2","exports":"./lib/index.js","repository":{"type":"git","url":"git+https://github.com/Borewit/peek-readable"},"files":["lib/**/*.js","lib/**/*.d.ts"],"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":"yarn run compile-src && yarn run compile-test","lint-ts":"biome check","lint-md":"remark -u preset-lint-recommended .","lint":"yarn run lint-md && yarn run lint-ts","test":"mocha","test-coverage":"c8 npm run test","start":"yarn run compile && yarn run lint && yarn run cover-test"}}},"_id":"@cjser/peek-readable__v5_4_2@5.4.2-cjser.2","gitHead":"a4bef54d7d22d3d9a6b84528457938d84cd65fe7","_nodeVersion":"20.14.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-xo3Q0WptDOvd2Oej5n5rYt9wson7s4YP0xQ/1MORRrwsr72q1LEdrcD+VTQRpMaIYrZIYbDv4Cf/nC4EfwBmcA==","shasum":"c810418f836bc437d259d73bcfbd1be196a64a1b","tarball":"https://registry.npmjs.org/@cjser/peek-readable__v5_4_2/-/peek-readable__v5_4_2-5.4.2-cjser.2.tgz","fileCount":16,"unpackedSize":29366,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCJ7r2jfcFKEOYSq6DPTTcp3pSF0KNck1umYSA++diloAIhAIdp2Hcdtk92BEyJnNwNYUq+/WkVdUBHLBFShcKId84U"}]},"_npmUser":{"name":"nanahira","email":"nanahira@momobako.com"},"directories":{},"maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/peek-readable__v5_4_2_5.4.2-cjser.2_1778159598085_0.1904089545499481"},"_hasShrinkwrap":false}},"time":{"created":"2026-05-07T13:13:17.938Z","5.4.2-cjser.2":"2026-05-07T13:13:18.257Z","modified":"2026-05-07T13:13:18.515Z"},"maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"description":"Read and peek from a readable stream","keywords":["readable","buffer","stream","read"],"repository":{"type":"git","url":"https://code.moenext.com/3rdeye/cjser.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/actions/workflows/nodejs-ci.yml/badge.svg?branch=master)](https://github.com/Borewit/peek-readable/actions/workflows/nodejs-ci.yml)\n[![CodeQL](https://github.com/Borewit/peek-readable/actions/workflows/github-code-scanning/codeql/badge.svg?branch=master)](https://github.com/Borewit/peek-readable/actions/workflows/github-code-scanning/codeql)[![NPM version](https://badge.fury.io/js/peek-readable.svg)](https://npmjs.org/package/peek-readable)\n[![npm downloads](http://img.shields.io/npm/dm/peek-readable.svg)](https://npmcharts.com/compare/peek-readable?start=600&interval=30)\n[![Coverage Status](https://coveralls.io/repos/github/Borewit/peek-readable/badge.svg?branch=master)](https://coveralls.io/github/Borewit/peek-readable?branch=master)\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)\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)\n\n# peek-readable\n\nA promise based asynchronous stream reader, which makes reading from a stream easy.\n\nAllows to read and peek from a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams)\n\nThis module is used by [strtok3](https://github.com/Borewit/strtok3)\n\nThe `peek-readable` contains one class: `StreamReader`, which reads from a [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable).\n\n- Class `StreamReader` is used to read from Node.js [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable).\n- Class `WebStreamReader` is used to read from [ReadableStream<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)\n\n## Compatibility\n\nModule: version 5 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).\nJavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).\nRequires Node.js ≥ 14.16 engine.\n\n## Usage\n\n### Installation\n\n```shell script\nnpm install --save peek-readable\n```\n\n## API Documentation\n\nBoth `StreamReader` and `WebStreamReader` implement the [IStreamReader interface](#istreamreader-interface).\n\n### `IStreamReader` Interface\n\nThe `IStreamReader` interface defines the contract for a stream reader,\nwhich provides methods to read and peek data from a stream into a `Uint8Array` buffer.\nThe methods are asynchronous and return a promise that resolves with the number of bytes read.\n\n#### Methods\n\n##### `peek` function\nThis method allows you to inspect data from the stream without advancing the read pointer.\nIt reads data into the provided Uint8Array at a specified offset but does not modify the stream's internal position, \nallowing you to look ahead in the stream.\n\n```ts  \npeek(uint8Array: Uint8Array, offset: number, length: number): Promise<number>\n```\n\nParameters:\n- `uint8Array`: `Uint8Array`: The buffer into which the data will be peeked.\n  This is where the peeked data will be stored.\n- `offset`: `number`: The offset in the Uint8Array where the peeked data should start being written.\n- `length`: `number`: The number of bytes to peek from the stream.\n\nReturns `Promise<number>`: \nA promise that resolves with the number of bytes actually peeked into the buffer. \nThis number may be less than the requested length if the end of the stream is reached.\n\n##### `read` function\n```ts  \nread(buffer: Uint8Array, offset: number, length: number): Promise<number>\n```\n\nParameters:\n- `uint8Array`: `Uint8Array`: The buffer into which the data will be read.\n  This is where the read data will be stored.\n- `offset`: `number`: The offset in the Uint8Array where the read data should start being written.\n- `length`: `number`: The number of bytes to read from the stream.\n\nReturns `Promise<number>`:\nA promise that resolves with the number of bytes actually read into the buffer.\nThis number may be less than the requested length if the end of the stream is reached.\n\n##### `abort` function\n\nAbort active asynchronous operation (`read` or `peak`) before it has completed.\n\n```ts  \nabort(): Promise<void>\n```\n\n## Examples\n\nIn the following example we read the first 16 bytes from a stream and store them in our buffer.\nSource code of examples can be found [here](test/examples.ts).\n\n```js\nimport fs from 'node:fs';\nimport { StreamReader } from 'peek-readable';\n\n(async () => {\n  const readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\n  const streamReader = new StreamReader(readable);\n  const uint8Array = new Uint8Array(16);\n  const bytesRead = await streamReader.read(uint8Array, 0, 16);;\n  // buffer contains 16 bytes, if the end-of-stream has not been reached\n})();\n```\n\nEnd-of-stream detection:\n```js\n(async () => {\n\n  const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\n  const streamReader = new StreamReader(fileReadStream);\n  const buffer = Buffer.alloc(16); // or use: new Uint8Array(16);\n\n  try {\n    await streamReader.read(buffer, 0, 16);\n    // buffer contains 16 bytes, if the end-of-stream has not been reached\n  } catch(error) {\n    if (error instanceof EndOfStreamError) {\n      console.log('End-of-stream reached');\n    }\n  }\n})();\n```\n\nWith `peek` you can read ahead:\n```js\nimport fs from 'node:fs';\nimport { StreamReader } from 'peek-readable';\n\nfunction closeNodeStream(stream: ReadStream): Promise<void> {\n  return new Promise<void>((resolve, reject) => {\n    stream.close(err => {\n      if(err)\n        reject(err);\n      else\n        resolve();\n    });\n  })\n}\n\n(async () => {\n\n  const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');\n  try {\n    const streamReader = new StreamReader(fileReadStream);\n    try {\n      const buffer = Buffer.alloc(20);\n\n      let bytesRead = await streamReader.peek(buffer, 0, 3);\n      if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {\n        console.log('This is a JPEG file');\n      } else {\n        throw Error('Expected a JPEG file');\n      }\n\n      bytesRead = await streamReader.read(buffer, 0, 20); // Read JPEG header\n      if (bytesRead === 20) {\n        console.log('Got the JPEG header');\n      } else {\n        throw Error('Failed to read JPEG header');\n      }\n    } finally {\n      await streamReader.close(); // Release fileReadStream\n    }    \n  } finally {\n    await closeNodeStream(fileReadStream);\n  }\n\n\n})();\n```\n\n## cjser\n\nThis package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.\nOriginal repository: https://github.com/Borewit/peek-readable\n","readmeFilename":"README.md","_rev":"1-10003ab20a72007a0d4511056ef429ca"}