123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const stream_1 = require("stream");
- const xmlfeedparser_1 = __importDefault(require("./xmlfeedparser"));
- const jsonfeedparser_1 = __importDefault(require("./jsonfeedparser"));
- class FeedMe extends stream_1.Writable {
- /**
- * Creates an instance of a parser. Parser can be JSON/XML.
- *
- * @param {boolean} buffer If true, feed will be buffered into memory
- * and can be retrieved using `parser.done()` on the `end` event.
- * @constructor
- */
- constructor(buffer = false) {
- super();
- this._buffer = buffer;
- }
- _proxyEvents() {
- const parserEmit = this._parser.parser.emit;
- this._parser.parser.emit = (event, ...args) => {
- parserEmit.apply(this._parser.parser, [event, ...args]);
- if (event !== 'error') {
- return this.emit(event, ...args);
- }
- return true;
- };
- this._parser.parser.on('error', this.emit.bind(this, 'error'));
- }
- /**
- * @param {Buffer} data
- */
- _write(data, encoding, callback) {
- const str = data.toString();
- // First find out what type of feed this is.
- if (!this._parser) {
- if (/^\s*</.test(str)) {
- this._parser = new xmlfeedparser_1.default(this._buffer);
- this._proxyEvents();
- }
- else if (/^\s*[{[]/.test(str)) {
- this._parser = new jsonfeedparser_1.default(this._buffer);
- this.emit('type', 'json');
- this._proxyEvents();
- }
- else {
- callback(new Error('Not a correctly formatted feed'));
- return;
- }
- }
- this._parser.write(data, encoding, callback);
- }
- _final(callback) {
- if (this._parser) {
- this._parser.end(callback);
- }
- else {
- callback(null);
- }
- }
- done() {
- return this._parser.done();
- }
- }
- exports.default = FeedMe;
- module.exports = FeedMe;
- //# sourceMappingURL=feedme.js.map
|