linkedin / linkedin/dustjs

Dust js does not stream (show html) when using helmet

Open
#795 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2.9k
Forks
461
PR merge metrics
No merged PRs in 30d

Description

I want to use dust stream (or adaro stream) for async html template, so I tried using stream.
What I found was that if I added helmet to my express app, then the html is not rendered and is shown as text insted of the html on screen.
I tried dust.stream, and hoffman, and adaro, all the same result:
https://stackoverflow.com/questions/56507447/dust-js-and-helmet-not-rendering-html
Dust.stream:

var fs = require('fs'),
    path = require('path'),
    express = require('express'),
    request = require('request'),
    helmet = require('helmet'),
    dust = require('dustjs-linkedin');

dust.config.whitespace = true;
dust.config.cache = false;

// Define a custom `onLoad` function to tell Dust how to load templates
dust.onLoad = function(tmpl, cb) {
  fs.readFile(path.join('./views', path.relative('/', path.resolve('/', tmpl + '.dust'))),
              { encoding: 'utf8' }, cb);
};

var app = express();
app.use(helmet());

app.get('/streaming', function(req, res) {
  dust.stream('hello', {
    "async": request('http://www.dustjs.com/')
  }).pipe(res)
    .on('end', function() {
      console.log('Done streaming!');
    });
});

app.get('/rendering', function(req, res) {
  dust.render('hello', {
    "async": request('http://www.dustjs.com/')
  }, function(err, out) {
    res.send(out);
    console.log('Done rendering!');
  });
});


const port = process.env.PORT | 3002;
app.listen(port, function () {
  console.log(`Visit http://localhost:${port} to see streaming!`);
});

Hoffman:

hoffman = require('hoffman'),
    express = require('express'),
    helmet = require('helmet'),
    request = require('request');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'dust');
app.engine('dust', hoffman.__express());

app.use(helmet());
// This is the important part-- it adds res.stream()
app.use(hoffman.stream);

app.get('/', function (req, res) {
  res.stream("hello", {
    "async": function(chunk, context, bodies, params) {
      return chunk.map(function(chunk) {
        // Introducting an artificial delay to make streaming more apparent
        setTimeout(function() {
          request('http://www.dustjs.com/')
          .on('data', chunk.write.bind(chunk))
          .on('end', chunk.end.bind(chunk));
        }, 3000);
      });
    }
  });
});
const port = process.env.PORT | 3007;

app.listen(port, function () {
  console.log(`Visit http://localhost:${port} to see streaming!`);
});

image

Can you help me find a solution

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The payload names no repository files or tests; begin with the supplied Dust.stream and Hoffman reproductions, focusing on app.use(helmet()) and the streaming response path. Compare behavior with and without helmet, then verify that the resulting response renders streamed HTML rather than showing it as text.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.