mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
join/array_to_sentence_string/json/inspect charge memoryLimit by the
string they materialize (not element count), so discarded results like
{% assign out = a | join %}{{ out | size }} are still bounded.
Remove the emitter-level limiter added in 2f343f063; it cannot catch
materialized-but-not-emitted values.
Co-authored-by: Cursor <[email protected]>
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { getPerformance } from '../util/performance'
|
|
import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'
|
|
import { Context } from '../context'
|
|
import { Template } from '../template'
|
|
import { Emitter, KeepingTypeEmitter, StreamedEmitter, SimpleEmitter } from '../emitters'
|
|
|
|
export class Render {
|
|
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
|
|
const emitter = new StreamedEmitter()
|
|
Promise.resolve().then(() => toPromise(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> {
|
|
if (!emitter) {
|
|
emitter = ctx.opts.keepOutputType ? new KeepingTypeEmitter() : new SimpleEmitter()
|
|
}
|
|
ctx.renderLimit.check(getPerformance().now())
|
|
const errors = []
|
|
for (const tpl of templates) {
|
|
ctx.renderLimit.check(getPerformance().now())
|
|
try {
|
|
// if tpl.render supports emitter, it'll return empty `html`
|
|
const html = yield tpl.render(ctx, emitter)
|
|
// if not, it'll return an `html`, write to the emitter for it
|
|
html && emitter.write(html)
|
|
if (ctx.breakCalled || ctx.continueCalled) break
|
|
} catch (e) {
|
|
const err = LiquidError.is(e) ? e : new RenderError(e as Error, tpl)
|
|
if (ctx.opts.catchAllErrors) errors.push(err)
|
|
else throw err
|
|
}
|
|
}
|
|
if (errors.length) {
|
|
throw new LiquidErrors(errors)
|
|
}
|
|
return emitter.buffer
|
|
}
|
|
}
|