GithubHelp home page GithubHelp logo

bogas04 / marinate Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 0.0 995 KB

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

License: MIT License

JavaScript 1.79% CSS 98.21%
nodejs nodejs-library templating stream

marinate's Introduction

Marinate

WORK IN PROGRESS

Marinate logo

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:

  • strings
  • functions
    • returning strings
    • returning promises
    • 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

marinate's People

Contributors

bogas04 avatar

Stargazers

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

Watchers

 avatar

marinate's Issues

Error Handling

Lets specify or create a guideline on how to handle errors.

  • Add in Readme about Promises should always resolve
    <meta http-equiv="refresh" content="0; url=/500"></head><body></body></html>
  • Still add catch blocks to avoid unhandledrejection
  • Research how React's renderToStream works in case of an error in a deep component
  • Research how React's componentDidCatch works in case of an error in deep component in both renderToString and renderToStream

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.