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

marinate

v0.1.6

Published

A library to pipe a template string to a stream, "marinating" strings, functions, promises and streams together

Downloads

21

Readme

Marinate

WORK IN PROGRESS

A JavaScript library making use of Tagged Template Literals to chunk, transform and pipe a template to a given stream.

Installation

The library works on Node 8 and above. However, it's possible to transpile the used features (async await in for..of, template literal strings, tagged template literal) to support older versions of Node.

# use npm
npm install marinate
# or yarn
yarn add marinate
# or new-hip-package-manager-for-js
new-hip-package-manager-for-js marinate

Usage

For a working server, check out this example that uses react & express.

import marinate from 'marinate';

import express from 'express';
import fetch from 'node-fetch';

import { renderToString, renderToNodeStream }  from 'hip-new-framework-of-the-month';

import { Nav, App } from './spa';
import footerHTML from './templates/footer';

const app = express();

const template = marinate`
<!doctype html>
<html>
  <body>
    ${renderToString(Nav)}
    ${() => renderToNodeStream(App)}
    ${() => fetch('api-route').then(r => r.text())}
    ${() => footerHTML}
  </body>
</html>`

app
  .get('/', (req, res) => template(res))
  .listen(4000, () => 'Server listening on port 4000')

Motivation

marinate was created to solve a problem where a node based web server is designed to stream the output, yet use a templating library to split static and non-static parts.

Assume following template

const template = `<html>
  <head>
    ${fs.readFileSync(cssPath)}
  </head>
  <body>
    ${renderToString(App)}
  </body>
</html>`

If you want to stream fs.readFileSync or renderToString part of your application, you will have to split your template into chunks, i.e. template parts before and the said part, then write your first chunk to HTTP Response stream, pipe your App renderStream to it, and finally write latter chunk once your App stream ends. This gets complicated as more and more dynamic portions become part of your template.

Tagged Template Literals

Thankfully, ES2015's tagged template literals are sort of designed to do exactly this. They can create these chunks automatically, and act differently upon the dynamic parts. This allows you to now pass functions, promises, streams and what not !

Currently we support following parts:

  • [x] strings
  • [x] functions
    • [x] returning strings
    • [x] returning promises
    • [x] returning streams

Note that a function can return a function, or a promsie can resovle to a function/promise. Basically the definition of dynami parts is recursive, ultimately evaluating to a string or stream. Be careful!

License

MIT