nodejs / nodejs/node

stream: Add option to `Readable.take` operator to not close the stream

Open
#46,980 42 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request stream
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

What is the problem this feature will solve?

I will be able to do this:

const csvParsedStream = fs
	.createReadStream('file.csv')
	.compose(csvParse({ columns: false }));

const [columns] = await csvParsedStream
	.take(1)
	.toArray();

// This will now be empty and no data as take already consumed the stream
const parsed = await csvParsedStream
   .map((row) => parseRowByColumns(row, columns))
   .toArray();

Another example (I know I can use [first, ...rest] this is just an example):

const a = Readable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const [first] = await a.take(1).toArray();

console.log(first);
// [1]

const rest = await a.toArray();
console.log(rest)
// []
What is the feature you are proposing to solve the problem?

Adding closeStream option to the take operator that with default value true that I could disable closing the stream

const csvParsedStream = fs
	.createReadStream('file.csv')
	.compose(csvParse({ columns: false }));

const [columns] = await csvParsedStream
	.take(1, { closeStream: false })  // Right now this would close the stream, but we give it an option to not
	.toArray();

const parsed = await csvParsedStream.map((row) => parseRowByColumns(row, columns)).toArray();
What alternatives have you considered?

Get the first value from stream as async itarator and rest

let columns;
for await (const c of csvParsedStream.iterator<string[]>({ destroyOnReturn: false })) {
	columns = c;

	break;
}

const parsed = await csvParsedStream
    .map((row) => parseRowByColumns(row, columns))
    .toArray();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Readable.take operator and compare its current stream-closing behavior with the mentioned async iterator({ destroyOnReturn: false }) alternative. Review the surrounding stream API discussion and define completion as supporting the proposed closeStream option while preserving the existing default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.