{"_id":"first-chunk-stream","_rev":"538414","name":"first-chunk-stream","description":"Buffer and transform the n first bytes of a stream","dist-tags":{"latest":"5.0.0"},"maintainers":[{"name":"sindresorhus","email":""}],"time":{"modified":"2021-08-15T21:45:46.000Z","created":"2014-05-25T15:47:09.711Z","5.0.0":"2021-08-15T21:43:54.847Z","4.0.0":"2019-06-13T07:23:24.939Z","3.0.0":"2019-05-12T14:15:06.570Z","2.0.0":"2015-09-20T16:31:11.197Z","1.0.0":"2014-08-14T09:06:30.958Z","0.1.0":"2014-05-25T15:47:09.711Z"},"users":{},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"repository":{"type":"git","url":"git+https://github.com/sindresorhus/first-chunk-stream.git"},"versions":{"5.0.0":{"name":"first-chunk-stream","version":"5.0.0","description":"Buffer and transform the n first bytes of a stream","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/first-chunk-stream.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":"./index.js","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"scripts":{"test":"xo && nyc ava && tsd"},"keywords":["buffer","stream","streams","transform","first","chunk","size","minimum","bytes"],"devDependencies":{"@types/node":"^16.6.1","ava":"^3.15.0","nyc":"^15.1.0","streamtest":"^2.0.0","tsd":"^0.17.0","xo":"^0.44.0"},"gitHead":"91cd5d0cd51c796768f43cc7d6e314eff325460c","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream#readme","_id":"first-chunk-stream@5.0.0","_nodeVersion":"12.22.1","_npmVersion":"7.10.0","dist":{"shasum":"6089fd0add05b058ec0c122a21c0c1801d06d5ff","size":3605,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-5.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-5.0.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/first-chunk-stream_5.0.0_1629063834702_0.9370822033374449"},"_hasShrinkwrap":false,"publish_time":1629063834847,"_cnpm_publish_time":1629063834847},"4.0.0":{"name":"first-chunk-stream","version":"4.0.0","description":"Buffer and transform the n first bytes of a stream","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/first-chunk-stream.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"sindresorhus.com"},"engines":{"node":">=8"},"scripts":{"test":"xo && nyc ava && tsd"},"keywords":["buffer","stream","streams","transform","first","chunk","size","min","minimum","bytes"],"devDependencies":{"@types/node":"^12.0.8","ava":"^2.1.0","nyc":"^14.0.0","streamtest":"^1.2.1","tsd":"^0.7.3","xo":"^0.24.0"},"gitHead":"0f40e3d18ac9527a5efeca2f5a9fe25297a243dc","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream#readme","_id":"first-chunk-stream@4.0.0","_nodeVersion":"8.16.0","_npmVersion":"6.9.0","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"dist":{"shasum":"798e0b91a019a9421246ec4e4d25885c5d97912c","size":3662,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-4.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-4.0.0.tgz"},"maintainers":[{"name":"sindresorhus","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/first-chunk-stream_4.0.0_1560410604833_0.02719108546276816"},"_hasShrinkwrap":false,"publish_time":1560410604939,"_cnpm_publish_time":1560410604939},"3.0.0":{"name":"first-chunk-stream","version":"3.0.0","description":"Transform the first chunk in a stream","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/first-chunk-stream.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"sindresorhus.com"},"engines":{"node":">=8"},"scripts":{"test":"xo && nyc ava && tsd"},"keywords":["buffer","stream","streams","transform","first","chunk","size","min","minimum"],"devDependencies":{"@types/node":"^12.0.0","ava":"^1.4.1","nyc":"^14.0.0","streamtest":"^1.2.1","tsd":"^0.7.3","xo":"^0.24.0"},"gitHead":"c34c62cbf812d3b7c034b4e82a6118c385be118a","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream#readme","_id":"first-chunk-stream@3.0.0","_nodeVersion":"8.16.0","_npmVersion":"6.9.0","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"dist":{"shasum":"06972a66263505ed82b2c4db93c1b5e078a6576a","size":3519,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-3.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-3.0.0.tgz"},"maintainers":[{"name":"sindresorhus","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/first-chunk-stream_3.0.0_1557670506283_0.4465839831807381"},"_hasShrinkwrap":false,"publish_time":1557670506570,"_cnpm_publish_time":1557670506570},"2.0.0":{"name":"first-chunk-stream","version":"2.0.0","description":"Transform the first chunk in a stream","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/first-chunk-stream.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"sindresorhus.com"},"engines":{"node":">=0.10.0"},"scripts":{"test":"xo && mocha","cover":"istanbul cover --report html _mocha -- test.js -R spec -t 5000"},"files":["index.js"],"keywords":["buffer","stream","streams","transform","first","chunk","size","min","minimum"],"dependencies":{"readable-stream":"^2.0.2"},"devDependencies":{"istanbul":"^0.3.19","mocha":"*","mocha-lcov-reporter":"0.0.2","streamtest":"^1.2.1","xo":"*"},"gitHead":"867c900661f90b4f50d1b2494f714f80933fc696","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream#readme","_id":"first-chunk-stream@2.0.0","_shasum":"1bdecdb8e083c0664b91945581577a43a9f31d70","_from":".","_npmVersion":"2.14.3","_nodeVersion":"4.1.0","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"dist":{"shasum":"1bdecdb8e083c0664b91945581577a43a9f31d70","size":3270,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-2.0.0.tgz"},"maintainers":[{"name":"sindresorhus","email":""}],"directories":{},"publish_time":1442766671197,"_cnpm_publish_time":1442766671197,"_hasShrinkwrap":false},"1.0.0":{"name":"first-chunk-stream","version":"1.0.0","description":"Transform the first chunk in a stream","license":"MIT","repository":{"type":"git","url":"https://github.com/sindresorhus/first-chunk-stream"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"http://sindresorhus.com"},"engines":{"node":">=0.10.0"},"scripts":{"test":"mocha"},"files":["index.js"],"keywords":["buffer","stream","streams","transform","first","chunk","size","min","minimum"],"devDependencies":{"concat-stream":"^1.4.5","mocha":"*"},"gitHead":"8b0b1750edcc30fa2b2071245198181e925be619","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream","_id":"first-chunk-stream@1.0.0","_shasum":"59bfb50cd905f60d7c394cd3d9acaab4e6ad934e","_from":".","_npmVersion":"1.4.21","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"maintainers":[{"name":"sindresorhus","email":""}],"dist":{"shasum":"59bfb50cd905f60d7c394cd3d9acaab4e6ad934e","size":1636,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-1.0.0.tgz"},"directories":{},"publish_time":1408007190958,"_cnpm_publish_time":1408007190958,"_hasShrinkwrap":false},"0.1.0":{"name":"first-chunk-stream","version":"0.1.0","description":"Transform the first chunk in a stream","license":"MIT","repository":{"type":"git","url":"git://github.com/sindresorhus/first-chunk-stream"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"http://sindresorhus.com"},"engines":{"node":">=0.10.0"},"scripts":{"test":"mocha"},"files":["index.js"],"keywords":["buffer","stream","streams","transform","first","chunk","size","min","minimum"],"devDependencies":{"concat-stream":"^1.4.5","mocha":"*"},"gitHead":"79ca900b4d31074448dc7767eae79f1e605dca20","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"homepage":"https://github.com/sindresorhus/first-chunk-stream","_id":"first-chunk-stream@0.1.0","_shasum":"755d3ec14d49a86e3d2fcc08beead5c0ca2b9c0a","_from":".","_npmVersion":"1.4.13","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"maintainers":[{"name":"sindresorhus","email":""}],"dist":{"shasum":"755d3ec14d49a86e3d2fcc08beead5c0ca2b9c0a","size":1632,"noattachment":false,"key":"/first-chunk-stream/-/first-chunk-stream-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/first-chunk-stream/download/first-chunk-stream-0.1.0.tgz"},"directories":{},"publish_time":1401032829711,"_cnpm_publish_time":1401032829711,"_hasShrinkwrap":false}},"readme":"# first-chunk-stream\n\n> Buffer and transform the n first bytes of a stream\n\n## Install\n\n```\n$ npm install first-chunk-stream\n```\n\n## Usage\n\n```js\nimport fs from 'node:fs';\nimport getStream from 'get-stream';\nimport FirstChunkStream from 'first-chunk-stream';\n\n// unicorn.txt => unicorn rainbow\nconst stream = fs.createReadStream('unicorn.txt')\n\t.pipe(new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\t\treturn chunk.toString(encoding).toUpperCase();\n\t}));\n\nconst data = await getStream(stream);\n\nif (data.length < 7) {\n\tthrow new Error('Couldn\\'t get the minimum required first chunk length');\n}\n\nconsole.log(data);\n//=> 'UNICORN rainbow'\n```\n\n## API\n\n### FirstChunkStream(options, transform)\n\n`FirstChunkStream` constructor.\n\n#### transform(chunk, encoding)\n\nType: `Function`\n\nAsync function that receives the required `options.chunkSize` bytes.\n\nExpected to return an buffer-like object or `string` or object of form {buffer: `Buffer`, encoding: `string`} to send to stream or `firstChunkStream.stop` to end stream right away.\n\nAn error thrown from this function will be emitted as stream errors.\n\nNote that the buffer can have a smaller length than the required one. In that case, it will be due to the fact that the complete stream contents has a length less than the `options.chunkSize` value. You should check for this yourself if you strictly depend on the length.\n\n```js\nimport FirstChunkStream from 'first-chunk-stream';\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\treturn chunk.toString(encoding).toUpperCase(); // Send string to stream\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\treturn chunk; // Send buffer to stream\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\treturn {\n\t\tbuffer: chunk,\n\t\tencoding: encoding,\n\t}; // Send buffer with encoding to stream\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\treturn FirstChunkStream.stop; // End the stream early\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => {\n\tthrow new Error('Unconditional error'); // Emit stream error\n});\n```\n\n#### options\n\nType: `object`\n\nThe options object is passed to the [`Duplex` stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) constructor allowing you to customize your stream behavior. In addition, you can specify the following option:\n\n###### chunkSize\n\nType: `number`\n\nThe number of bytes to buffer.\n","_attachments":{},"homepage":"https://github.com/sindresorhus/first-chunk-stream#readme","bugs":{"url":"https://github.com/sindresorhus/first-chunk-stream/issues"},"license":"MIT"}