Socket
Socket
Sign inDemoInstall

trough

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    trough

Middleware: a channel used to convey a liquid


Version published
Weekly downloads
7.6M
decreased by-7.65%
Maintainers
1
Install size
12.3 kB
Created
Weekly downloads
 

Package description

What is trough?

The trough npm package is a utility for creating middleware-style function pipelines. It allows you to compose and execute functions in a series, where each function can asynchronously handle data and pass it to the next function in the pipeline. This is particularly useful for processing data, handling requests in web servers, or any scenario where you need a series of operations to be performed in order.

What are trough's main functionalities?

Pipeline Creation

This code sample demonstrates how to create a new pipeline using trough. It first requires the trough package and then creates a new pipeline instance.

const trough = require('trough');
const pipeline = trough();

Adding Middleware

This code sample shows how to add a middleware function to the pipeline. The middleware function takes data and a callback function (`next`) as arguments. It modifies the data and passes it to the next middleware in the pipeline by calling `next`.

pipeline.use(function (data, next) {
  // Modify data
  next(null, modifiedData);
});

Executing the Pipeline

This code sample illustrates how to execute the pipeline with some initial data. It runs the pipeline and provides a callback function to handle the final result or any errors that might occur during the execution.

pipeline.run(initialData, function (err, result) {
  if (err) throw err;
  // Handle result
});

Other packages similar to trough

Readme

Source

trough Build Status Coverage Status

trough /trôf/ — a channel used to convey a liquid.

trough is like ware with less sugar, and middleware functions can change the input of the next.

Installation

npm:

npm install trough

Usage

var fs = require('fs');
var path = require('path');
var trough = require('trough');

var pipeline = trough()
  .use(function (fileName) {
    console.log('Checking... ' + fileName);
  })
  .use(function (fileName) {
    return path.join(process.cwd(), fileName);
  })
  .use(function (filePath, next) {
    fs.stat(filePath, function (err, stats) {
      next(err, {filePath: filePath, stats: stats});
    });
  })
  .use(function (ctx, next) {
    if (ctx.stats.isFile()) {
      fs.readFile(ctx.filePath, next);
    } else {
      next(new Error('Expected file'));
    }
  });

pipeline.run('readme.md', console.log);
pipeline.run('node_modules', console.log);

Yields:

Checking... readme.md
Checking... node_modules
Error: Expected file
    at ~/example.js:21:12
    at wrapped (~/node_modules/trough/index.js:93:19)
    at next (~/node_modules/trough/index.js:56:24)
    at done (~/node_modules/trough/index.js:124:12)
    at ~/node_modules/example.js:14:7
    at FSReqWrap.oncomplete (fs.js:153:5)
null <Buffer 23 20 74 72 6f 75 67 68 20 5b 21 5b 42 75 69 6c 64 20 53 74 61 74 75 73 5d 5b 74 72 61 76 69 73 2d 62 61 64 67 65 5d 5d 5b 74 72 61 76 69 73 5d 20 5b ... >

API

trough()

Create a new Trough.

Trough

A pipeline.

Trough#run([input..., ]done)

Run the pipeline (all use()d middleware). Invokes done on completion with either an error or the output of the last middleware

Note! as the length of input defines whether async function get a next function, it’s recommended to keep input at one value normally.

function done(err?, [output...])

The final handler passed to run(), invoked with an error if a middleware function rejected, passed, or threw one, or the output of the last middleware function.

Trough#use(fn)

Add fn, a middleware function, to the pipeline.

function fn([input..., ][next])

A middleware function invoked with the output of its predecessor.

Synchronous

If fn returns or throws an error, the pipeline fails and done is invoked with that error.

If fn returns a value (neither null nor undefined), the first input of the next function is set to that value (all other input is passed through).

Example

The following example shows how returning an error stops the pipeline:

var trough = require('trough');

trough()
  .use(function (val) {
    return new Error('Got: ' + val);
  })
  .run('some value', console.log);

Yields:

Error: Got: some value
    at ~/example.js:5:12
    ...

The following example shows how throwing an error stops the pipeline:

var trough = require('trough');

trough()
  .use(function (val) {
    throw new Error('Got: ' + val);
  })
  .run('more value', console.log);

Yields:

Error: Got: more value
    at ~/example.js:5:11
    ...

The following example shows how the first output can be modified:

var trough = require('trough');

trough()
  .use(function (val) {
    return 'even ' + val;
  })
  .run('more value', 'untouched', console.log);

Yields:

null 'even more value' 'untouched'
Promise

If fn returns a promise, and that promise rejects, the pipeline fails and done is invoked with the rejected value.

If fn returns a promise, and that promise resolves with a value (neither null nor undefined), the first input of the next function is set to that value (all other input is passed through).

Example

The following example shows how rejecting a promise stops the pipeline:

var trough = require('trough');

trough()
  .use(function (val) {
    return new Promise(function (resolve, reject) {
      reject('Got: ' + val);
    });
  })
  .run('val', console.log);

Yields:

Got: val

The following example shows how the input isn’t touched by resolving to null.

var trough = require('trough');

trough()
  .use(function () {
    return new Promise(function (resolve) {
      setTimeout(function () {
        resolve(null);
      }, 100);
    });
  })
  .run('Input', console.log);

Yields:

null 'Input'
Asynchronous

If fn accepts one more argument than the given input, a next function is given (after the input). next must be called, but doesn’t have to be called async.

If next is given a value (neither null nor undefined) as its first argument, the pipeline fails and done is invoked with that value.

If next is given no value (either null or undefined) as the first argument, all following non-nully values change the input of the following function, and all nully values default to the input.

Example

The following example shows how passing a first argument stops the pipeline:

var trough = require('trough');

trough()
  .use(function (val, next) {
    next(new Error('Got: ' + val));
  })
  .run('val', console.log);

Yields:

Error: Got: val
    at ~/example.js:5:10

The following example shows how more values than the input are passed.

var trough = require('trough');

trough()
  .use(function (val, next) {
    setTimeout(function () {
      next(null, null, 'values');
    }, 100);
  })
  .run('some', console.log);

Yields:

null 'some' 'values'

License

MIT © Titus Wormer

Keywords

FAQs

Last updated on 09 Jul 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc