marko
Version:
UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.
57 lines (50 loc) • 1.48 kB
JavaScript
"use strict";var stream = require("stream");
var OutgoingMessageProto = require("http").OutgoingMessage.prototype;
if (String(OutgoingMessageProto.flush).indexOf("deprecated") !== -1) {
// Yes, we are monkey-patching http. This method should never have been added and it was introduced on
// the iojs fork. It was quickly deprecated and I'm 99% sure no one is actually using it.
// See:
// - https://github.com/marko-js/async-writer/issues/3
// - https://github.com/nodejs/node/issues/2920
//
// This method causes problems since marko looks for the flush method and calls it found.
// The `res.flush()` method is introduced by the [compression](https://www.npmjs.com/package/compression)
// middleware, but, otherwise, it should typically not exist.
delete OutgoingMessageProto.flush;
}
class Readable extends stream.Readable {
constructor(template, data) {
super();
this._Y_ = template;
this._Z_ = data;
this.a__ = false;
}
write(data) {
if (data != null) {
this.push(data);
}
}
end() {
this.push(null);
}
_read() {
if (this.a__) {
return;
}
this.a__ = true;
var template = this._Y_;
var data = this._Z_;
var globalData = data && data.$global;
var out = this._Y_.createOut(
globalData,
this,
undefined,
template._X_
);
template.render(data, out);
out.end();
}
}
module.exports = function (data) {
return new Readable(this, data);
};