mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-14 20:00:39 -07:00
feat: renderFileToNodeStream(filepath, scope)
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import { stringify } from '../util/underscore'
|
||||
import { Emitter } from './emitter'
|
||||
|
||||
export class StreamedEmitter {
|
||||
export class StreamedEmitter implements Emitter {
|
||||
public buffer = '';
|
||||
public stream = new (require('stream').PassThrough)()
|
||||
public stream: NodeJS.ReadWriteStream = new (require('stream').PassThrough)()
|
||||
public write (html: any) {
|
||||
this.stream.write(stringify(html))
|
||||
}
|
||||
public error (err: Error) {
|
||||
this.stream.emit('error', err)
|
||||
}
|
||||
public end () {
|
||||
this.stream.end()
|
||||
}
|
||||
|
||||
@@ -84,6 +84,10 @@ export class Liquid {
|
||||
const templates = this.parseFileSync(file)
|
||||
return this.renderSync(templates, ctx)
|
||||
}
|
||||
public async renderFileToNodeStream (file: string, scope?: object) {
|
||||
const templates = await this.parseFile(file)
|
||||
return this.renderToNodeStream(templates, scope)
|
||||
}
|
||||
|
||||
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
|
||||
const value = new Value(str, this)
|
||||
|
||||
@@ -10,7 +10,8 @@ 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)).then(() => emitter.end())
|
||||
toThenable(this.renderTemplates(templates, ctx, emitter))
|
||||
.then(() => emitter.end(), err => emitter.error(err))
|
||||
return emitter.stream
|
||||
}
|
||||
public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator<any> {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"mocha"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-var-requires": "off",
|
||||
"no-unused-expressions": "off",
|
||||
"no-new": "off"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { expect, use } from 'chai'
|
||||
import { resolve } from 'path'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('.renderToNodeStream()', function () {
|
||||
it('should render to stream in Node.js', done => {
|
||||
const cjs = require('../../dist/liquid.node.cjs')
|
||||
const engine = new cjs.Liquid()
|
||||
const tpl = engine.parseFileSync(resolve(__dirname, '../stub/root/foo.html'))
|
||||
const stream = engine.renderToNodeStream(tpl)
|
||||
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.renderToNodeStream('foo')
|
||||
return expect(render).to.throw('streaming not supported in browser')
|
||||
})
|
||||
})
|
||||
|
||||
describe('.renderFileToNodeStream()', function () {
|
||||
it('should render to stream in Node.js', async done => {
|
||||
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')
|
||||
})
|
||||
})
|
||||
@@ -136,7 +136,36 @@ describe('Liquid', function () {
|
||||
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
|
||||
let html = ''
|
||||
stream.on('data', data => { html += data })
|
||||
stream.on('end', () => { expect(html).to.equal('foo'); done() })
|
||||
stream.on('end', () => {
|
||||
try {
|
||||
expect(html).to.equal('foo')
|
||||
done()
|
||||
} catch (err) {
|
||||
done(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('#enderFileToNodeStream', function () {
|
||||
before(function () {
|
||||
mock({
|
||||
'/root/foo.html': 'foo'
|
||||
})
|
||||
})
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { expect } from 'chai'
|
||||
import { StreamedEmitter } from '../../../src/emitters/streamed-emitter-browser'
|
||||
|
||||
describe('emitters/streamed-emitter-browser', () => {
|
||||
it('should throw when try to constructing', () => {
|
||||
expect(() => new StreamedEmitter()).to.throw(/streaming not supported/)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user