Skip to content

Commit

Permalink
fix: renderToNodeStream() now emit 'error' event instead of throw
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 3, 2021
1 parent 3b5eb66 commit afeef1d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bin/perf-diff.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/usr/bin/env bash

VERSION_LATEST=$(cat package.json | grep '"version":' | awk -F'"' '{print $4}')
FILE_LOCAL=dist/liquid.node.cjs.js
Expand Down
2 changes: 1 addition & 1 deletion src/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { KeepingTypeEmitter } from '../emitters/keeping-type-emitter'
export class Render {
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
const emitter = new StreamedEmitter()
toThenable(this.renderTemplates(templates, ctx, emitter))
Promise.resolve().then(() => toThenable(this.renderTemplates(templates, ctx, emitter)))
.then(() => emitter.end(), err => emitter.error(err))
return emitter.stream
}
Expand Down
20 changes: 3 additions & 17 deletions test/e2e/render-to-node-stream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, use } from 'chai'
import { resolve } from 'path'
import * as chaiAsPromised from 'chai-as-promised'
import { drainStream } from '../stub/stream'

use(chaiAsPromised)

Expand Down Expand Up @@ -30,27 +31,12 @@ describe('.renderToNodeStream()', function () {
})

describe('.renderFileToNodeStream()', function () {
it('should render to stream in Node.js', async done => {
it('should render to stream in Node.js', async () => {
const cjs = require('../../dist/liquid.node.cjs')
const engine = new cjs.Liquid({
root: resolve(__dirname, '../stub/root/')
})
const stream = await engine.renderFileToNodeStream('foo.html')
let html = ''
stream.on('data', (data: string) => { html += data })
stream.on('end', () => {
try {
expect(html).to.equal('foo')
done()
} catch (err) {
done(err)
}
})
})
it('should throw in browser', async function () {
const cjs = require('../../dist/liquid.browser.umd')
const engine = new cjs.Liquid()
const render = () => engine.renderFileToNodeStream('foo')
return expect(render).to.throw('streaming not supported in browser')
expect(drainStream(stream)).to.eventually.equal('foo')
})
})
46 changes: 20 additions & 26 deletions test/integration/liquid/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Liquid, Context, isFalsy } from '../../../src/liquid'
import * as chai from 'chai'
import { mock, restore } from '../../stub/mockfs'
import * as chaiAsPromised from 'chai-as-promised'
import { drainStream } from '../../stub/stream'

const expect = chai.expect
chai.use(chaiAsPromised)
Expand Down Expand Up @@ -132,40 +133,33 @@ describe('Liquid', function () {
})
describe('#enderToNodeStream', function () {
const engine = new Liquid()
it('should render a simple value', function (done) {
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
let html = ''
stream.on('data', data => { html += data })
stream.on('end', () => {
try {
expect(html).to.equal('foo')
done()
} catch (err) {
done(err)
}
})
it('should render a simple value', async () => {
const stream = await engine.renderToNodeStream(engine.parse('{{"foo"}}'))
expect(drainStream(stream)).to.eventually.equal('foo')
})
})
describe('#enderFileToNodeStream', function () {
let engine: Liquid
before(function () {
mock({
'/root/foo.html': 'foo'
'/root/foo.html': 'foo',
'/root/error.html': 'A{%throwingTag%}B'
})
engine = new Liquid({ root: ['/root/'] })
engine.registerTag('throwingTag', {
render: function () {
throw new Error('intended render error')
}
})
})
after(restore)
it('should render a simple value', (done) => {
const engine = new Liquid({ root: ['/root/'] })
engine.renderFileToNodeStream('foo.html').then(stream => {
let html = ''
stream.on('data', data => { html += data })
stream.on('end', () => {
try {
expect(html).to.equal('foo'); done()
} catch (err) {
done(err)
}
})
})
it('should render a simple value', async () => {
const stream = await engine.renderFileToNodeStream('foo.html')
expect(drainStream(stream)).to.be.eventually.equal('foo')
})
it('should throw RenderError when tag throws', async () => {
const stream = engine.renderFileToNodeStream('error.html')
expect(drainStream(stream)).to.be.rejectedWith(/intended render error/)
})
})
})
8 changes: 8 additions & 0 deletions test/stub/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function drainStream (stream: NodeJS.ReadableStream) {
return new Promise((resolve, reject) => {
let html = ''
stream.on('data', data => { html += data })
stream.on('end', () => resolve(html))
stream.on('error', (err: Error) => reject(err))
})
}

0 comments on commit afeef1d

Please sign in to comment.