# parallel-transform-stream

> stream.Transform with parallel transforms

Latest version **1.0.1** (published 2016-08-09) · MIT license · 31 weekly downloads

## Install

```sh
npm install parallel-transform-stream
pnpm add parallel-transform-stream
yarn add parallel-transform-stream
bun add parallel-transform-stream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-08-09 |
| First published | 2016-08-09 |
| Weekly downloads | 31 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Keno Schwalb |
| Maintainers | aemkei, caerostris, rich_pb, roka |
| Keywords | order, preserve, parallel, stream, transform |

## Links

- npm: https://www.npmjs.com/package/parallel-transform-stream
- Repository: https://github.com/ubilabs/node-parallel-transform-stream
- Homepage: https://github.com/ubilabs/node-parallel-transform-stream#readme
- Issues: https://github.com/ubilabs/node-parallel-transform-stream/issues
- npm.io page: https://npm.io/package/parallel-transform-stream

## Dependencies (1)

- [cyclist](https://npm.io/package/cyclist.md) ^1.0.1

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-08-09
- 1.0.0 — 2016-08-09

## README

# Parallel Transform Stream [![Build Status](https://travis-ci.org/ubilabs/node-parallel-transform-stream.svg?branch=master)](https://travis-ci.org/ubilabs/node-parallel-transform-stream)

A NodeJS transform stream which runs transformations in parallel and preserves input order.

```sh
npm install parallel-transform-stream --save
```

This module's core is based on [parallel-transform](https://www.npmjs.com/package/parallel-transform).  
It was fully rewritten in ES6, and provides a more flexible, inheritance-based interface.

## Usage

This module is (almost) a drop-in replacement for standard NodeJS transform streams.

```js
import ParallelTransform from 'parallel-transform-stream';

class MyParallelTransformStream extends ParallelTransform {
	constructor() {
		super({maxParallel: 50, objectMode: true}); // process up to 50 transforms in parallel
	}

	_parallelTransform(data, encoding, done) {
		// long-running, asynchronous operation
		done(null, data);
	}

	// optional, like in stream.Transform
	_parallelFlush(done) {
		// finish stuff, if required
		done();
	}
}
```

Alternatively, you can use the following shortcut function:

```js
import ParallelTransform from 'parallel-transform-stream';

const MyParallelTransformStream = ParallelTransform.create((data, encoding, done) => {
	// long-running, asynchronous operation
	done(null, data);
});
```

## Documentation

All classes extending the `ParallelTransform` class must implement the method `_parallelTransform`.  
They may implement `_parallelFlush`, although this is not required.

### API

**ParallelTransform.create(transform, flush = function(done) { done(); }, defaultOptions = {})**

* `transform` `<Function>` The \_transform function of the stream. [See below](#api-for-extending-paralleltransform) for more details
* `flush` `<Function>` The \_flush function of the stream. [See below](#api-for-extending-paralleltransform) for more details
* `defaultOptions` `<Object>` Default options for the class constructor

### API for extending ParallelTransform

The constructor of the `ParallelTransform` class accepts all options accepted by `stream.Transform`. In addition, it accepts the `maxParallel` property, which set the maximum number of parallel transformations.

All classes extending `ParallelTransform` must implement the `_parallelTransform` method, and may implement the `_parallelFlush` method.

**ParallelTransform._parallelTransform(chunk, encoding, callback)**

* `chunk` `<Buffer>` | `<String>` The chunk to be transformed.
* `encoding` `<String>` If the chunk is a string, then this is the encoding type. If chunk is a buffer, then this is the special value - 'buffer', ignore it in this case.
* `callback` `<Function>` A callback function to be called after the supplied chunk has been processed. The first argument passed to the callback must be an error which has occurred during transformation, or `null`. The second argument is the result. The stream will stop processing transforms and emit an `error` event instantly if the error passed to the callback function was not `null`.

*Please note* that, as opposed to traditional NodeJS transform streams, you **MUST NOT** call `this.push` directly. Emit values through the callback function instead.  
You **must not** call the callback more than once.

**ParallelTransform._parallelFlush(callback)**

* `callback` `<Function>` A callback function to be called when the stream has finished flushing.

`ParallelTransform` implementations may implement the transform._flush() method. This will be called when there is no more written data to be consumed, but before the 'end' event is emitted signaling the end of the Readable stream.

### Migrating from stream.Transform

1. Change `extends stream.Transform` to `extends ParallelTransform`
2. Rename `_transform(data, encoding, done)` to `_parallelTransform(data, encoding, done)`
3. If using `_flush`: Rename `_flush(done)` to `_parallelFlush(done)`
4. Replace `this.push(data); done();` with `done(null, data);`

#### Example

```js
import stream from 'stream';

class MyTransformStream extends stream.Transform {
	constructor() {
		super({objectMode: true});
	}

	_transform(data, encoding, done) {
		// do something with `data`
		this.push(data);
		done();
	}
}
```

becomes

```js
import ParallelTransform from 'parallel-transform-stream';

class MyTransformStream extends ParallelTransform {
	constructor() {
		super({objectMode: true});
	}

	_parallelTransform(data, encoding, done) {
		// do something with `data`
		done(null, data);
	}
}
```

### Gotchas and caveats

* Calling `this.push()` will result in unexpected behaviour. Push results by calling `done(null, result)`.
* Calling `done()` more than once will result in unexpected behaviour
* By design, you cannot push multiple results from a single transform

---
_Source: https://npm.io/package/parallel-transform-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
