mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
fix: renderToNodeStream() now emit 'error' event instead of throw
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
VERSION_LATEST=$(cat package.json | grep '"version":' | awk -F'"' '{print $4}')
|
VERSION_LATEST=$(cat package.json | grep '"version":' | awk -F'"' '{print $4}')
|
||||||
FILE_LOCAL=dist/liquid.node.cjs.js
|
FILE_LOCAL=dist/liquid.node.cjs.js
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { KeepingTypeEmitter } from '../emitters/keeping-type-emitter'
|
|||||||
export class Render {
|
export class Render {
|
||||||
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
|
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
|
||||||
const emitter = new StreamedEmitter()
|
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))
|
.then(() => emitter.end(), err => emitter.error(err))
|
||||||
return emitter.stream
|
return emitter.stream
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { expect, use } from 'chai'
|
import { expect, use } from 'chai'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import * as chaiAsPromised from 'chai-as-promised'
|
import * as chaiAsPromised from 'chai-as-promised'
|
||||||
|
import { drainStream } from '../stub/stream'
|
||||||
|
|
||||||
use(chaiAsPromised)
|
use(chaiAsPromised)
|
||||||
|
|
||||||
@@ -30,27 +31,12 @@ describe('.renderToNodeStream()', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('.renderFileToNodeStream()', 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 cjs = require('../../dist/liquid.node.cjs')
|
||||||
const engine = new cjs.Liquid({
|
const engine = new cjs.Liquid({
|
||||||
root: resolve(__dirname, '../stub/root/')
|
root: resolve(__dirname, '../stub/root/')
|
||||||
})
|
})
|
||||||
const stream = await engine.renderFileToNodeStream('foo.html')
|
const stream = await engine.renderFileToNodeStream('foo.html')
|
||||||
let html = ''
|
expect(drainStream(stream)).to.eventually.equal('foo')
|
||||||
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')
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Liquid, Context, isFalsy } from '../../../src/liquid'
|
|||||||
import * as chai from 'chai'
|
import * as chai from 'chai'
|
||||||
import { mock, restore } from '../../stub/mockfs'
|
import { mock, restore } from '../../stub/mockfs'
|
||||||
import * as chaiAsPromised from 'chai-as-promised'
|
import * as chaiAsPromised from 'chai-as-promised'
|
||||||
|
import { drainStream } from '../../stub/stream'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
chai.use(chaiAsPromised)
|
chai.use(chaiAsPromised)
|
||||||
@@ -132,40 +133,33 @@ describe('Liquid', function () {
|
|||||||
})
|
})
|
||||||
describe('#enderToNodeStream', function () {
|
describe('#enderToNodeStream', function () {
|
||||||
const engine = new Liquid()
|
const engine = new Liquid()
|
||||||
it('should render a simple value', function (done) {
|
it('should render a simple value', async () => {
|
||||||
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
|
const stream = await engine.renderToNodeStream(engine.parse('{{"foo"}}'))
|
||||||
let html = ''
|
expect(drainStream(stream)).to.eventually.equal('foo')
|
||||||
stream.on('data', data => { html += data })
|
|
||||||
stream.on('end', () => {
|
|
||||||
try {
|
|
||||||
expect(html).to.equal('foo')
|
|
||||||
done()
|
|
||||||
} catch (err) {
|
|
||||||
done(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('#enderFileToNodeStream', function () {
|
describe('#enderFileToNodeStream', function () {
|
||||||
|
let engine: Liquid
|
||||||
before(function () {
|
before(function () {
|
||||||
mock({
|
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)
|
after(restore)
|
||||||
it('should render a simple value', (done) => {
|
it('should render a simple value', async () => {
|
||||||
const engine = new Liquid({ root: ['/root/'] })
|
const stream = await engine.renderFileToNodeStream('foo.html')
|
||||||
engine.renderFileToNodeStream('foo.html').then(stream => {
|
expect(drainStream(stream)).to.be.eventually.equal('foo')
|
||||||
let html = ''
|
})
|
||||||
stream.on('data', data => { html += data })
|
it('should throw RenderError when tag throws', async () => {
|
||||||
stream.on('end', () => {
|
const stream = engine.renderFileToNodeStream('error.html')
|
||||||
try {
|
expect(drainStream(stream)).to.be.rejectedWith(/intended render error/)
|
||||||
expect(html).to.equal('foo'); done()
|
|
||||||
} catch (err) {
|
|
||||||
done(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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))
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user