streaming
Version:
Transforms and other streaming helpers
49 lines (44 loc) • 2.16 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventSource = void 0;
var stream_1 = require("stream");
/**
EventSource is a subclass of stream.Transform for converting a stream of objects
into `text/event-stream` style output.
You should call `res.setHeader('Content-Type', 'text/event-stream');` on your
HTTP response `res` before piping this EventSource stream into the response.
And for this use-case, you'll want to listen for onerror on the client-side,
and close() the EventSource object on the first error, otherwise it will try
to reconnect immediately when the response stream closes. As far as I know,
there is no special event you can send to trigger a complete stop.
Documentation: http://www.w3.org/TR/eventsource/
MDN is usually great, but their article on EventSource sucks:
https://developer.mozilla.org/en-US/docs/Web/API/EventSource
*/
var EventSource = /** @class */ (function (_super) {
__extends(EventSource, _super);
function EventSource(options) {
return _super.call(this, options) || this;
}
EventSource.prototype._transform = function (chunk, encoding, callback) {
// encoding may be something weird like "buffer" if chunk is just a string
callback(null, "data: ".concat(chunk.toString(), "\n\n"));
};
return EventSource;
}(stream_1.Transform));
exports.EventSource = EventSource;