Giter VIP home page Giter VIP logo

stream-template's Introduction

stream-template

An ES6/ES2015 Tagged String Literal tag that can interpolate Node.JS streams, strings and Promises that return either (or arrays of any of those) and produces a Node.JS stream. This allows you to join several Streams together with bits in between without having to buffer anything.

Written by Thomas Parslow (almostobsolete.net and tomparslow.co.uk) for IORad (iorad.com) and released with their kind permission.

NPM

Build Status

Warning: will only run on newer versions of Node.JS (tested on 4.0.0 and above)

Install

npm install --save stream-template

Examples

const ST = require("stream-template");

const data1 = fs.createReadStream("data1.txt");
const data2 = fs.createReadStream("data2.txt");
const output = ST`<html>
  1: <pre>${data1}</pre>
  2: <pre>${data2}</pre>
</html>`;
output.pipe(process.stdout);

Can also accept arrays (items are concatenated, array items can be any of the supported types):

const ST = require("stream-template");

const data = [
  fs.createReadStream("part1.txt"),
  fs.createReadStream("part2.txt")
];
const output = ST`Data follows: ${data}`;
output.pipe(process.stdout);

And also Promises:

const ST = require("./stream-template");
const fetch = require("node-fetch");

const email = fetch("https://api.github.com/users/almost")
  .then(r => r.json())
  .then(profile => {
    return profile.email;
  });

const output = ST`<a href="mailto:${email}">Email</a>`;
output.pipe(process.stdout);

And of course regular strings work:

const output = ST`Hello by name is ${name}`;
output.pipe(process.stdout);

I've shown each used seperated but you can do it all mixed together as well.

Encoding

By default strings are encoded as utf-8, you can change the encoding like this:

const ST = require("stream-template").encoding("utf16le");

Note that buffers and other streams are passed through as-is, the encoding only effects the template strings and interpolated strings.

Contributing

Fixed or improved stuff? Great! Send me a pull request through GitHub or get in touch on Twitter @almostobsolete or email at [email protected]

stream-template's People

Contributors

almost avatar mafintosh avatar popeindustries avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stream-template's Issues

Perf optimisation: prefer strings over streams?

I've had a chance to run some stress tests with a "production" template tree to measure the overhead associated with treating all templates as Streams as default instead of Strings. Rendering a template tree with 6-8 levels of nested templates (mostly Strings, some Promises and Arrays) resulted in the following when all templates were processed with stream-template:

> autocannon -c 100 -d 5 -p 10 http://localhost:3000/1.13377671

Running 5s test @ http://localhost:3000/1.13377671
100 connections with 10 pipelining factor

Stat         Avg     Stdev  Max     
Latency (ms) 167.44  614.22 4070.71 
Req/Sec      472     13.27  490     
Bytes/Sec    8.36 MB 254 kB 8.58 MB 

2k requests in 5s, 41.3 MB read

Defaulting to Strings and deopting when hitting an object interpolation with the following code:

function fastTemplate(strings, ...interpolations) {
  let str = strings[0];

  for (let i = 0, n = interpolations.length; i < n; i++) {
    const interpolation = interpolations[i];
    // Deopt if not primitive type
    if (typeof interpolation === 'object') {
      return streamTemplate([str, ...strings.slice(i + 1)], ...interpolations.slice(i));
    }

    str += interpolations[i];
    str += strings[i + 1];
  }

  return str;
}

...resulted in a ~4x throughput increase:

> autocannon -c 100 -d 5 -p 10 http://localhost:3000/1.13377671

Running 5s test @ http://localhost:3000/1.13377671
100 connections with 10 pipelining factor

Stat         Avg     Stdev   Max     
Latency (ms) 56.55   180.16  1346.15 
Req/Sec      1678    369.8   1970    
Bytes/Sec    29.4 MB 6.56 MB 34.5 MB 

8k requests in 5s, 147 MB read

The only potential weakness I can see with this approach is that, depending on the tree, you would end up with either a String or a Stream as output. This is not a problem for us since we are using a server framework that accepts either type in it's res.send() method, but it's not ideal. I think it would be best to be able to mark the entry template as "root" to enforce a Stream return type, but I'm not entirely sure how this could work. In any case, it's an interesting opportunity!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.