Comparing version 1.0.0 to 2.0.0
@@ -1,2 +0,2 @@ | ||
var Writable = require('stream').Writable, | ||
var Transform = require('stream').Transform, | ||
tools = require('./tools.js'), | ||
@@ -12,6 +12,5 @@ schema = require('./schema.js'), | ||
function EbmlDecoder(options) { | ||
Writable.call(this, options); | ||
options = options || {}; | ||
options.readableObjectMode = true; | ||
Transform.call(this, options); | ||
@@ -26,5 +25,5 @@ this._buffer = null; | ||
require('util').inherits(EbmlDecoder, Writable); | ||
require('util').inherits(EbmlDecoder, Transform); | ||
EbmlDecoder.prototype._write = function(chunk, enc, done) { | ||
EbmlDecoder.prototype._transform = function(chunk, enc, done) { | ||
@@ -141,3 +140,3 @@ if (this._buffer === null) { | ||
debug('content should be tags'); | ||
this.emit(tagObj.name, tagObj); | ||
this.push(['start', tagObj]); | ||
this._state = STATE_TAG; | ||
@@ -163,3 +162,3 @@ return true; | ||
tagObj.data = data; | ||
this.emit(tagObj.name, tagObj); | ||
this.push(['tag', tagObj]); | ||
@@ -171,3 +170,3 @@ while (this._tag_stack.length > 0) { | ||
} | ||
this.emit(topEle.name + ':end', topEle); | ||
this.push(['end', topEle]); | ||
this._tag_stack.pop(); | ||
@@ -174,0 +173,0 @@ } |
@@ -1,2 +0,2 @@ | ||
var Readable = require('stream').Readable, | ||
var Transform = require('stream').Transform, | ||
tools = require('./tools.js'), | ||
@@ -8,49 +8,54 @@ schema = require('./schema.js'), | ||
function EbmlEncoder(options) { | ||
Readable.call(this, options); | ||
options = options || {}; | ||
options.writableObjectMode = true; | ||
Transform.call(this, options); | ||
this._schema = schema; | ||
this._cursor = 0; | ||
this._pending = 0; | ||
this._buffer = null; | ||
this._corked = false; | ||
this._stack = [{ | ||
children: [] | ||
}]; | ||
this._stack = []; | ||
} | ||
require('util').inherits(EbmlEncoder, Readable); | ||
require('util').inherits(EbmlEncoder, Transform); | ||
EbmlEncoder.prototype._read = function(size) { | ||
debug('read ' + size); | ||
this._pending += size; | ||
this._flush(); | ||
}; | ||
EbmlEncoder.prototype._transform = function(chunk, enc, done) { | ||
debug('encode ' + chunk[0] + ' ' + chunk[1].name); | ||
EbmlEncoder.prototype._flush = function() { | ||
if(chunk[0] === 'start') { | ||
this.startTag(chunk[1].name); | ||
} else if(chunk[0] === 'tag') { | ||
this.writeTag(chunk[1].name, chunk[1].data); | ||
} else if(chunk[0] === 'end') { | ||
this.endTag(chunk[1].name); | ||
} | ||
var buffer = this._stack[0].children[0] && this._stack[0].children[0].data; | ||
done(); | ||
}; | ||
if (!buffer || this._pending === 0) { | ||
EbmlEncoder.prototype._flush = function(done) { | ||
done = done || function(){}; | ||
if (!this._buffer || this._corked) { | ||
debug('no buffer/nothing pending'); | ||
done(); | ||
return; | ||
} | ||
debug('writing ' + this._pending + ' bytes'); | ||
debug('writing ' + this._buffer.length + ' bytes'); | ||
var chunk = buffer.slice(this._cursor, this._cursor + this._pending); | ||
this._cursor += this._pending; | ||
this._pending = 0; | ||
var chunk = this._buffer.toBuffer(); | ||
this._buffer = null; | ||
this.push(chunk); | ||
done(); | ||
}; | ||
debug('remaining buffer ' + (buffer.length - this._cursor)); | ||
if (this._cursor >= buffer.length) { | ||
debug('buffer empty'); | ||
this.push(null); | ||
EbmlEncoder.prototype._bufferAndFlush = function(buffer) { | ||
if(this._buffer) { | ||
this._buffer.push(buffer); | ||
} else { | ||
this._buffer = Buffers([buffer]); | ||
} | ||
this._flush(); | ||
}; | ||
EbmlEncoder.prototype.getSchemaInfo = function(tagName) { | ||
@@ -67,2 +72,11 @@ var tagStrs = Object.keys(this._schema); | ||
EbmlEncoder.prototype.cork = function() { | ||
this._corked = true; | ||
}; | ||
EbmlEncoder.prototype.uncork = function() { | ||
this._corked = false; | ||
this._flush(); | ||
}; | ||
EbmlEncoder.prototype._encodeTag = function(tagId, tagData) { | ||
@@ -78,7 +92,10 @@ return Buffers([tagId, tools.writeVint(tagData.length), tagData]); | ||
this._stack[this._stack.length - 1].children.push({ | ||
data: this._encodeTag(tagId, tagData) | ||
}); | ||
this._flush(); | ||
var data = this._encodeTag(tagId, tagData); | ||
if(this._stack.length > 0) { | ||
this._stack[this._stack.length - 1].children.push({ | ||
data: data | ||
}); | ||
} else { | ||
this._bufferAndFlush(data.toBuffer()); | ||
} | ||
}; | ||
@@ -94,12 +111,15 @@ | ||
id: tagId, | ||
name: tagName, | ||
children: [] | ||
}; | ||
this._stack[this._stack.length - 1].children.push(tag); | ||
if(this._stack.length > 0) { | ||
this._stack[this._stack.length - 1].children.push(tag); | ||
} | ||
this._stack.push(tag); | ||
}; | ||
EbmlEncoder.prototype.endTag = function() { | ||
EbmlEncoder.prototype.endTag = function(tagName) { | ||
var tag = this._stack.pop(); | ||
var tag = this._stack[this._stack.length - 1]; | ||
var childTagDataBuffers = tag.children.map(function(child) { | ||
@@ -109,6 +129,5 @@ return child.data; | ||
tag.data = this._encodeTag(tag.id, Buffers(childTagDataBuffers)); | ||
this._stack.pop(); | ||
if (this._stack.length === 1) { | ||
this._flush(); | ||
if (this._stack.length < 1) { | ||
this._bufferAndFlush(tag.data.toBuffer()); | ||
} | ||
@@ -115,0 +134,0 @@ }; |
{ | ||
"name": "ebml", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "ebml parser", | ||
@@ -47,2 +47,5 @@ "main": "index.js", | ||
"email": "max@maxogden.com" | ||
}, { | ||
"name": "Oliver Walzer", | ||
"email": "walzer@incuray.com" | ||
}], | ||
@@ -49,0 +52,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
# EBML [![Build Status](https://travis-ci.org/themasch/node-ebml.png?branch=master)](https://travis-ci.org/themasch/node-ebml) | ||
# EBML [![Build Status](https://travis-ci.org/themasch/node-ebml.png?branch=master)](https://travis-ci.org/themasch/node-ebml) [![NPM](https://nodei.co/npm/ebml.png?compact=true)](https://www.npmjs.com/package/ebml) | ||
@@ -35,1 +35,2 @@ [EBML](http://ebml.sourceforge.net/) stands for Extensible Binary Meta-Language | ||
* [Max Ogden](https://github.com/maxogden) | ||
* [Oliver Walzer](https://github.com/owcd) |
@@ -69,3 +69,6 @@ var ebml = require('../lib/ebml/index.js'), | ||
var decoder = new ebml.Decoder(); | ||
decoder.on('EBMLVersion', function(data) { | ||
decoder.on('data', function(data) { | ||
var state = data[0]; | ||
data = data[1]; | ||
assert.equal(state, 'tag'); | ||
assert.equal(data.tag, 0x286); | ||
@@ -84,3 +87,6 @@ assert.equal(data.tagStr, "4286"); | ||
decoder.on('EBML', function(data) { | ||
decoder.on('data', function(data) { | ||
var state = data[0]; | ||
data = data[1]; | ||
assert.equal(state, 'start'); | ||
assert.equal(data.tag, 0x0a45dfa3); | ||
@@ -100,17 +106,17 @@ assert.equal(data.tagStr, "1a45dfa3"); | ||
var tags = 0; | ||
decoder.on('EBML', function(data) { | ||
tags++; | ||
decoder.on('data', function(data) { | ||
var state = data[0]; | ||
data = data[1]; | ||
if(state != 'end') { | ||
tags++; | ||
} else { | ||
assert.equal(tags, 2); // two tags | ||
assert.equal(data.tag, 0x0a45dfa3); | ||
assert.equal(data.tagStr, "1a45dfa3"); | ||
assert.equal(data.dataSize, 4); | ||
assert.equal(data.type, 'm'); | ||
assert.equal(data.data, undefined); | ||
done(); | ||
} | ||
}); | ||
decoder.on('EBMLVersion', function(data) { | ||
tags++; | ||
}); | ||
decoder.on('EBML:end', function(data) { | ||
assert.equal(tags, 2); // two tags | ||
assert.equal(data.tag, 0x0a45dfa3); | ||
assert.equal(data.tagStr, "1a45dfa3"); | ||
assert.equal(data.dataSize, 4); | ||
assert.equal(data.type, 'm'); | ||
assert.equal(data.data, undefined); | ||
done(); | ||
}); | ||
decoder.write(new Buffer([0x1a, 0x45, 0xdf, 0xa3])); | ||
@@ -117,0 +123,0 @@ decoder.write(new Buffer([0x84, 0x42, 0x86, 0x81, 0x00])); |
@@ -16,11 +16,21 @@ var ebml = require('../lib/ebml/index.js'), | ||
var encoder = createEncoder([0x42, 0x86, 0x81, 0x01], done); | ||
encoder.writeTag('EBMLVersion', new Buffer([0x01])); | ||
encoder.write(['tag', { | ||
name: 'EBMLVersion', | ||
data: new Buffer([0x01]) | ||
}]); | ||
}); | ||
it('should write a tag with a single child', function(done) { | ||
var encoder = createEncoder([0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00], done); | ||
encoder.startTag('EBML'); | ||
encoder.writeTag('EBMLVersion', new Buffer([0x00])); | ||
encoder.endTag(); | ||
encoder.write(['start', { | ||
name: 'EBML', | ||
}]); | ||
encoder.write(['tag', { | ||
name: 'EBMLVersion', | ||
data: new Buffer([0x00]) | ||
}]); | ||
encoder.write(['end', { | ||
name: 'EBML', | ||
}]); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
313665
20
2690
36