mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -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> {
|
||||
|
||||
Reference in New Issue
Block a user