{"id":13454499,"url":"https://github.com/sindresorhus/first-chunk-stream","last_synced_at":"2025-04-10T02:23:31.603Z","repository":{"id":17386075,"uuid":"20158193","full_name":"sindresorhus/first-chunk-stream","owner":"sindresorhus","description":"Transform the first chunk in a stream","archived":false,"fork":false,"pushed_at":"2023-11-01T14:33:15.000Z","size":39,"stargazers_count":26,"open_issues_count":0,"forks_count":5,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-28T21:31:19.977Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2014-05-25T15:42:55.000Z","updated_at":"2025-01-07T20:10:57.000Z","dependencies_parsed_at":"2022-09-26T21:30:28.837Z","dependency_job_id":"ff8ee9ff-10db-4827-be86-7d73dced291b","html_url":"https://github.com/sindresorhus/first-chunk-stream","commit_stats":{"total_commits":31,"total_committers":8,"mean_commits":3.875,"dds":"0.29032258064516125","last_synced_commit":"ede69d11e9710c20b4ada5143742634080078075"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Ffirst-chunk-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Ffirst-chunk-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Ffirst-chunk-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Ffirst-chunk-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/first-chunk-stream/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247493918,"owners_count":20947799,"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":[],"created_at":"2024-07-31T08:00:54.728Z","updated_at":"2025-04-10T02:23:31.582Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","Repository","包","目录","Streams","Modules"],"sub_categories":["Streams","文件流","流处理","流"],"readme":"# first-chunk-stream\n\n\u003e Buffer and transform the n first bytes of a stream\n\n## Install\n\n```sh\nnpm 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';\nimport {uint8ArrayToString} from 'uint8array-extras';\n\n// unicorn.txt =\u003e unicorn rainbow\nconst stream = fs.createReadStream('unicorn.txt')\n\t.pipe(new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) =\u003e {\n\t\treturn uint8ArrayToString(chunk).toUpperCase();\n\t}));\n\nconst data = await getStream(stream);\n\nif (data.length \u003c 7) {\n\tthrow new Error('Couldn\\'t get the minimum required first chunk length');\n}\n\nconsole.log(data);\n//=\u003e '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: `Uint8Array`, 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) =\u003e {\n\treturn chunk; // Send buffer to stream\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) =\u003e {\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) =\u003e {\n\treturn FirstChunkStream.stop; // End the stream early\n});\n\nnew FirstChunkStream({chunkSize: 7}, async (chunk, encoding) =\u003e {\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Ffirst-chunk-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Ffirst-chunk-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Ffirst-chunk-stream/lists"}