Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added stream support

  • Loading branch information...
commit 6559f0c2ab8e67a6dc28fb2418711ebcf51c5f88 1 parent f1bc269
@sh1mmer authored
Showing with 55 additions and 25 deletions.
  1. +55 −25 lib/node-expat.js
View
80 lib/node-expat.js
@@ -1,42 +1,72 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
+var Stream = require('stream').Stream;
+
try {
- var expat = require('../build/Release/node-expat');
+ var expat = require('../build/Release/node-expat');
} catch(e) {
- var expat = require('../build/default/node-expat');
+ var expat = require('../build/default/node-expat');
}
-/**
- * Simple wrapper because EventEmitter has turned pure-JS as of node
- * 0.5.x.
- */
-exports.Parser = function(encoding) {
- this.parser = new expat.Parser(encoding);
+var Parser = function(encoding) {
+ var that = this;
+
+ this.parser = new expat.Parser(encoding);
+ this.parser.emit = function() {
+ that.emit.apply(that, arguments);
+ };
+
+ //stream API
+ this.writable = true;
+ this.readable = true;
+};
+util.inherits(Parser, Stream);
- var that = this;
- this.parser.emit = function() {
- that.emit.apply(that, arguments);
- };
+Parser.prototype.parse = function(buf, isFinal) {
+ this.emit('data', buf);
+ return this.parser.parse(buf, isFinal);
};
-util.inherits(exports.Parser, EventEmitter);
-exports.Parser.prototype.parse = function(buf, isFinal) {
- return this.parser.parse(buf, isFinal);
+Parser.prototype.setEncoding = function(encoding) {
+ return this.parser.setEncoding(encoding);
};
-exports.Parser.prototype.setEncoding = function(encoding) {
- return this.parser.setEncoding(encoding);
+Parser.prototype.getError = function() {
+ return this.parser.getError();
+};
+Parser.prototype.stop = function() {
+ return this.parser.stop();
+};
+Parser.prototype.pause = function() {
+ return this.stop();
+};
+Parser.prototype.resume = function() {
+ return this.parser.resume();
};
-exports.Parser.prototype.getError = function() {
- return this.parser.getError();
+Parser.prototype.destroy = function() {
+ this.parser.stop();
+ this.end();
};
-exports.Parser.prototype.stop = function() {
- return this.parser.stop();
+
+Parser.prototype.destroySoon = function() {
+ this.destroy();
};
-exports.Parser.prototype.pause = function() {
- return this.stop();
+
+Parser.prototype.write = Parser.prototype.parse;
+
+Parser.prototype.end = function() {
+ this.emit('close');
+ this.emit('end');
};
-exports.Parser.prototype.resume = function() {
- return this.parser.resume();
+
+
+//Exports
+
+exports.Parser = Parser;
+
+exports.createParser = function(cb) {
+ var parser = new Parser();
+ if(cb) { parser.on('startElement', cb); }
+ return parser;
};

2 comments on commit 6559f0c

@astro

Any reason this shouldn't get merged in?

@sh1mmer
Owner

Oh rad. Sorry I'd meant to do a pull. Let me tidy it a tiny bit first but we should definitely get streams support in there :)

Please sign in to comment.
Something went wrong with that request. Please try again.