npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jongleberry/pipe

v1.1.0

Published

Streaming server template. Define variables or sections as promises or streams or thunks that return either in your template, and they will be evaluated at render time.

Downloads

5

Readme

@jongleberry/pipe

Node.js CI codecov

Streaming and async template rendering for node.js using template strings. Define variables or sections as promises or streams or thunks that return either in your template, and they will be evaluated at render time.

This allows you to create fast, non-blocking server-side rendered applications while minimizing time-to-first-byte for the user.

Example

const pipe = require('@jongleberry/pipe')

const render = function ({
  currentUser
}) {
  return pipe`
    <!DOCTYPE html>
    <html>
      <head>
        <title>Example</title>
        <link rel='stylesheet' href='entrypoint.css'>
      </head>
      <body>
        ${async () => React.renderToNodeStream(React.createElement(App, {
          currentUser: await currentUser,
        }))}
        <script>window.__INITIAL_STATE__ = ${async () => JSON.stringify({
          currentUser: await currentUser,
        })}
        <script src='entrypoint.js></script>
      </body>
    </html>
  `
}

// express middleware
function (req, res) {
  res.setHeader('content-type', 'text/html; charset=utf-8')
  res.flushheaders()

  render({
    // pass a promise that resolves to the user
    getCurrentUser: services.getCurrentUser(req)
  }).pipe(res)
}

API

stream = pipe`` || pipe(strings, ...values)

render is a tagged template that returns a readable PassThrough stream.

Value Types

  • <String> - serialize anything that isn't a string yourself as this library will throw.
  • Promise<String> - a promise that returns a string.
  • Stream - a binary stream that will be pipe directly into the response. This library does not check its contents, but object streams will throw: pipe it into a stream such as streaming-json-stringify.
  • A function or async function that returns either of the above.

Error Handling

Any section of the template that errors will simply not be rendered. Streams may partially be rendered, so it's advised that you handle stream errors yourself or pass streams that cannot error (e.g. a passthrough stream). Listen to any errors on the resulting stream, e.g.:

const stream = pipe`<div id="root">${async () => React.renderToNodeStream(React.createElement(App, { 
  currentUser: await currentUser 
}))}</div>`
stream.on('error', err => console.error(err.stack))

Some frameworks such as Koa automatically handle this when you set the stream as ctx.body = stream

Note that because status codes (or should be) flushed to the client before rendering begins, errors in the template cannot affect the response status code.

Implementation Notes

Alternatives

  • A cleaner version of https://github.com/almost/stream-template
    • stream-template recreates a readable stream from scratch whereas this library relies on passthrough streams .pipe()
  • A simpler API of https://github.com/matthewp/flora
    • Does not require any other utilities
  • A simpler, reduced-scope version of https://github.com/popeindustries/lit-html-server
    • Does not support arrays - it's not difficult to call .join('')
    • Does not require any other utilities
    • Does not provide any helper functions for streams
    • Does not work on the browser

Please let me know if you are aware of any other ones.