ebml.js 4.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ebml.js CHANGED
@@ -6417,307 +6417,353 @@ module.exports = { debugLog }
6417
6417
  (module, __unused_webpack_exports, __webpack_require__) {
6418
6418
 
6419
6419
  /* provided dependency */ var Buffer = __webpack_require__(287)["Buffer"];
6420
- const { Transform } = __webpack_require__(310);
6421
- const tools = __webpack_require__(327);
6422
- const schema = __webpack_require__(261);
6423
- const { debugLog } = __webpack_require__(224);
6424
-
6425
- const debug = debugLog('ebml:decoder');
6426
-
6427
- const STATE_TAG = 1;
6428
- const STATE_SIZE = 2;
6429
- const STATE_CONTENT = 3;
6430
-
6431
- class EbmlDecoder extends Transform {
6432
- /**
6433
- * @property
6434
- * @private
6435
- * @type {Buffer}
6436
- */
6437
- mBuffer = null;
6438
-
6439
- /**
6440
- * @private
6441
- * @property
6442
- * @readonly
6443
- */
6444
- mTagStack = [];
6445
-
6446
- /**
6447
- * @property
6448
- * @private
6449
- * @type {Number}
6450
- */
6451
- mState = STATE_TAG;
6452
-
6453
- /**
6454
- * @property
6455
- * @private
6456
- * @type {Number}
6457
- */
6458
- mCursor = 0;
6459
-
6460
- /**
6461
- * @property
6462
- * @private
6463
- * @type {Number}
6464
- */
6465
- mTotal = 0;
6466
-
6467
- /**
6468
- * @constructor
6469
- * @param {Object} options The options to be passed along to the super class
6470
- */
6471
- constructor(options = {}) {
6472
- super({ ...options, readableObjectMode: true });
6473
- }
6474
-
6475
- get buffer() {
6476
- return this.mBuffer;
6477
- }
6478
-
6479
- get cursor() {
6480
- return this.mCursor;
6481
- }
6482
-
6483
- get state() {
6484
- return this.mState;
6485
- }
6486
-
6487
- get tagStack() {
6488
- return this.mTagStack;
6489
- }
6490
-
6491
- get total() {
6492
- return this.mTotal;
6493
- }
6494
-
6495
- set buffer(buffer) {
6496
- this.mBuffer = buffer;
6497
- }
6498
-
6499
- /**
6500
- * @param {number} cursor
6501
- */
6502
- set cursor(cursor) {
6503
- this.mCursor = cursor;
6504
- }
6505
-
6506
- set state(state) {
6507
- this.mState = state;
6508
- }
6509
-
6510
- set total(total) {
6511
- this.mTotal = total;
6512
- }
6513
-
6514
- _transform(chunk, enc, done) {
6515
- if (!this.buffer) {
6516
- this.buffer = Buffer.from(chunk);
6517
- } else {
6518
- this.buffer = tools.concatenate(this.buffer, Buffer.from(chunk));
6519
- }
6520
-
6521
- while (this.cursor < this.buffer.length) {
6522
- if (this.state === STATE_TAG && !this.readTag()) {
6523
- break;
6524
- }
6525
- if (this.state === STATE_SIZE && !this.readSize()) {
6526
- break;
6527
- }
6528
- if (this.state === STATE_CONTENT && !this.readContent()) {
6529
- break;
6530
- }
6531
- }
6532
-
6533
- done();
6534
- }
6535
-
6536
- static getSchemaInfo(tag) {
6537
- if (Number.isInteger(tag) && schema.has(tag)) {
6538
- return schema.get(tag);
6539
- }
6540
- const tagStr = `0x${tag.toString(16).toUpperCase()}`
6541
- const unknown = {
6542
- type: null,
6543
- name: `unknown-${tagStr}`,
6544
- description: `${tagStr}`,
6545
- level: -1,
6546
- minver: -1,
6547
- multiple: false,
6548
- webm: false,
6549
- };
6550
- schema.set(tag, unknown)
6551
- console.warn('[SCHEMA]', 'unknown tag:', tagStr)
6552
- return unknown
6553
- }
6554
-
6555
- readTag() {
6556
- /* istanbul ignore if */
6557
- if (debug.enabled) {
6558
- debug('parsing tag');
6559
- }
6560
-
6561
- if (this.cursor >= this.buffer.length) {
6562
- /* istanbul ignore if */
6563
- if (debug.enabled) {
6564
- debug('waiting for more data');
6565
- }
6566
- return false;
6567
- }
6568
-
6569
- const start = this.total;
6570
- const tag = tools.readVint(this.buffer, this.cursor);
6571
-
6572
- if (tag == null) {
6573
- /* istanbul ignore if */
6574
- if (debug.enabled) {
6575
- debug('waiting for more data');
6576
- }
6577
-
6578
- return false;
6579
- }
6580
-
6581
- const tagStr = tools.readHexString(
6582
- this.buffer,
6583
- this.cursor,
6584
- this.cursor + tag.length,
6585
- );
6586
- const tagNum = Number.parseInt(tagStr, 16);
6587
- this.cursor += tag.length;
6588
- this.total += tag.length;
6589
- this.state = STATE_SIZE;
6590
-
6591
- const tagObj = {
6592
- tag: tag.value,
6593
- tagStr,
6594
- type: EbmlDecoder.getSchemaInfo(tagNum).type,
6595
- name: EbmlDecoder.getSchemaInfo(tagNum).name,
6596
- start,
6597
- end: start + tag.length,
6598
- };
6599
-
6600
- this.tagStack.push(tagObj);
6601
- /* istanbul ignore if */
6602
- if (debug.enabled) {
6603
- debug(`read tag: ${tagStr}`);
6604
- }
6605
-
6606
- return true;
6607
- }
6608
-
6609
- readSize() {
6610
- const tagObj = this.tagStack[this.tagStack.length - 1];
6611
-
6612
- /* istanbul ignore if */
6613
- if (debug.enabled) {
6614
- debug(`parsing size for tag: ${tagObj.tagStr}`);
6615
- }
6616
-
6617
- if (this.cursor >= this.buffer.length) {
6618
- /* istanbul ignore if */
6619
- if (debug.enabled) {
6620
- debug('waiting for more data');
6621
- }
6622
-
6623
- return false;
6624
- }
6625
-
6626
- const size = tools.readVint(this.buffer, this.cursor);
6627
-
6628
- if (size == null) {
6629
- /* istanbul ignore if */
6630
- if (debug.enabled) {
6631
- debug('waiting for more data');
6632
- }
6633
-
6634
- return false;
6635
- }
6636
-
6637
- this.cursor += size.length;
6638
- this.total += size.length;
6639
- this.state = STATE_CONTENT;
6640
- tagObj.dataSize = size.value;
6641
-
6642
- // unknown size
6643
- if (size.value === -1) {
6644
- tagObj.end = -1;
6645
- } else {
6646
- tagObj.end += size.value + size.length;
6647
- }
6648
- /* istanbul ignore if */
6649
- if (debug.enabled) {
6650
- debug(`read size: ${size.value}`);
6651
- }
6652
-
6653
- return true;
6654
- }
6655
-
6656
- readContent() {
6657
- const { tagStr, type, dataSize, ...rest } = this.tagStack[this.tagStack.length - 1];
6658
-
6659
- /* istanbul ignore if */
6660
- if (debug.enabled) {
6661
- debug(`parsing content for tag: ${tagStr}`);
6662
- }
6663
-
6664
- if (type === 'm') {
6665
- /* istanbul ignore if */
6666
- if (debug.enabled) {
6667
- debug('content should be tags');
6668
- }
6669
- this.push(['start', { tagStr, type, dataSize, ...rest }]);
6670
- this.state = STATE_TAG;
6671
-
6672
- return true;
6673
- }
6674
-
6675
- if (this.buffer.length < this.cursor + dataSize) {
6676
- /* istanbul ignore if */
6677
- if (debug.enabled) {
6678
- debug(`got: ${this.buffer.length}`);
6679
- debug(`need: ${this.cursor + dataSize}`);
6680
- debug('waiting for more data');
6681
- }
6682
-
6683
- return false;
6684
- }
6685
-
6686
- const data = this.buffer.subarray(this.cursor, this.cursor + dataSize);
6687
- this.total += dataSize;
6688
- this.state = STATE_TAG;
6689
- this.buffer = this.buffer.subarray(this.cursor + dataSize);
6690
- this.cursor = 0;
6691
-
6692
- this.tagStack.pop(); // remove the object from the stack
6693
-
6694
- this.push([
6695
- 'tag',
6696
- tools.readDataFromTag(
6697
- { tagStr, type, dataSize, ...rest },
6698
- Buffer.from(data),
6699
- ),
6700
- ]);
6701
-
6702
- while (this.tagStack.length > 0) {
6703
- const topEle = this.tagStack[this.tagStack.length - 1];
6704
- if (this.total < topEle.end) {
6705
- break;
6706
- }
6707
- this.push(['end', topEle]);
6708
- this.tagStack.pop();
6709
- }
6710
-
6711
- /* istanbul ignore if */
6712
- if (debug.enabled) {
6713
- debug(`read data: ${data.toString('hex')}`);
6714
- }
6715
-
6716
- return true;
6717
- }
6718
- }
6719
-
6720
- module.exports = EbmlDecoder
6420
+ const { Transform } = __webpack_require__(310);
6421
+ const tools = __webpack_require__(327);
6422
+ const schema = __webpack_require__(261);
6423
+ const { debugLog } = __webpack_require__(224);
6424
+
6425
+ const debug = debugLog('ebml:decoder');
6426
+
6427
+ const STATE_TAG = 1;
6428
+ const STATE_SIZE = 2;
6429
+ const STATE_CONTENT = 3;
6430
+
6431
+ class EbmlDecoder extends Transform {
6432
+ /**
6433
+ * @property
6434
+ * @private
6435
+ * @type {Buffer}
6436
+ */
6437
+ mBuffer = null;
6438
+
6439
+ /**
6440
+ * @private
6441
+ * @property
6442
+ * @readonly
6443
+ */
6444
+ mTagStack = [];
6445
+
6446
+ /**
6447
+ * @property
6448
+ * @private
6449
+ * @type {Number}
6450
+ */
6451
+ mState = STATE_TAG;
6452
+
6453
+ /**
6454
+ * @property
6455
+ * @private
6456
+ * @type {Number}
6457
+ */
6458
+ mCursor = 0;
6459
+
6460
+ /**
6461
+ * @property
6462
+ * @private
6463
+ * @type {Number}
6464
+ */
6465
+ mTotal = 0;
6466
+
6467
+ /** @private */
6468
+ mIsLive = false
6469
+
6470
+ /**
6471
+ * @constructor
6472
+ * @param {{ isLive?: boolean }} options The options to be passed along to the super class
6473
+ */
6474
+ constructor(options = {}) {
6475
+ super({ ...options, readableObjectMode: true });
6476
+ this.mIsLive = options ? options.isLive : false
6477
+ }
6478
+
6479
+ get isLive() {
6480
+ return this.mIsLive;
6481
+ }
6482
+
6483
+ get buffer() {
6484
+ return this.mBuffer;
6485
+ }
6486
+
6487
+ get cursor() {
6488
+ return this.mCursor;
6489
+ }
6490
+
6491
+ get state() {
6492
+ return this.mState;
6493
+ }
6494
+
6495
+ get tagStack() {
6496
+ return this.mTagStack;
6497
+ }
6498
+
6499
+ get total() {
6500
+ return this.mTotal;
6501
+ }
6502
+
6503
+ set buffer(buffer) {
6504
+ this.mBuffer = buffer;
6505
+ }
6506
+
6507
+ /**
6508
+ * @param {number} cursor
6509
+ */
6510
+ set cursor(cursor) {
6511
+ this.mCursor = cursor;
6512
+ }
6513
+
6514
+ set state(state) {
6515
+ this.mState = state;
6516
+ }
6517
+
6518
+ set total(total) {
6519
+ this.mTotal = total;
6520
+ }
6521
+
6522
+ _transform(chunk, enc, done) {
6523
+ if (!this.buffer) {
6524
+ this.buffer = Buffer.from(chunk);
6525
+ } else {
6526
+ this.buffer = tools.concatenate(this.buffer, Buffer.from(chunk));
6527
+ }
6528
+
6529
+ while (this.cursor < this.buffer.length) {
6530
+ if (this.state === STATE_TAG && !this.readTag()) {
6531
+ break;
6532
+ }
6533
+ if (this.state === STATE_SIZE && !this.readSize()) {
6534
+ break;
6535
+ }
6536
+ if (this.state === STATE_CONTENT && !this.readContent()) {
6537
+ break;
6538
+ }
6539
+ }
6540
+
6541
+ done();
6542
+ }
6543
+
6544
+ static getSchemaInfo(tag) {
6545
+ if (Number.isInteger(tag) && schema.has(tag)) {
6546
+ return schema.get(tag);
6547
+ }
6548
+ const tagStr = `0x${tag.toString(16).toUpperCase()}`
6549
+ const unknown = {
6550
+ type: null,
6551
+ name: `unknown-${tagStr}`,
6552
+ description: `${tagStr}`,
6553
+ level: -1,
6554
+ minver: -1,
6555
+ multiple: false,
6556
+ webm: false,
6557
+ };
6558
+ schema.set(tag, unknown)
6559
+ console.warn('[SCHEMA]', 'unknown tag:', tagStr)
6560
+ return unknown
6561
+ }
6562
+
6563
+ readTag() {
6564
+ /* istanbul ignore if */
6565
+ if (debug.enabled) {
6566
+ debug('parsing tag');
6567
+ }
6568
+
6569
+ if (this.cursor >= this.buffer.length) {
6570
+ /* istanbul ignore if */
6571
+ if (debug.enabled) {
6572
+ debug('waiting for more data');
6573
+ }
6574
+ return false;
6575
+ }
6576
+
6577
+ if (this.isLive && !this.findTagStart()) {
6578
+ return false
6579
+ }
6580
+
6581
+ const start = this.total;
6582
+ const tag = tools.readVint(this.buffer, this.cursor);
6583
+
6584
+ if (tag == null) {
6585
+ /* istanbul ignore if */
6586
+ if (debug.enabled) {
6587
+ debug('waiting for more data');
6588
+ }
6589
+
6590
+ return false;
6591
+ }
6592
+
6593
+ const tagStr = tools.readHexString(
6594
+ this.buffer,
6595
+ this.cursor,
6596
+ this.cursor + tag.length,
6597
+ );
6598
+ const tagNum = Number.parseInt(tagStr, 16);
6599
+ this.cursor += tag.length;
6600
+ this.total += tag.length;
6601
+ this.state = STATE_SIZE;
6602
+
6603
+ const tagObj = {
6604
+ tag: tag.value,
6605
+ tagStr,
6606
+ type: EbmlDecoder.getSchemaInfo(tagNum).type,
6607
+ name: EbmlDecoder.getSchemaInfo(tagNum).name,
6608
+ start,
6609
+ end: start + tag.length,
6610
+ };
6611
+
6612
+ this.tagStack.push(tagObj);
6613
+ /* istanbul ignore if */
6614
+ if (debug.enabled) {
6615
+ debug(`read tag: ${tagStr}`);
6616
+ }
6617
+
6618
+ return true;
6619
+ }
6620
+
6621
+ /** @private */
6622
+ findTagStart() {
6623
+ while (this.cursor < this.buffer.length) {
6624
+ try {
6625
+ const tag = tools.readVint(this.buffer, this.cursor);
6626
+ if (tag == null) {
6627
+ return false
6628
+ }
6629
+ const tagStr = tools.readHexString(
6630
+ this.buffer,
6631
+ this.cursor,
6632
+ this.cursor + tag.length,
6633
+ );
6634
+ const tagNum = Number.parseInt(tagStr, 16);
6635
+ const tagName = EbmlDecoder.getSchemaInfo(tagNum).name
6636
+ if (tagName && tagName.startsWith('unknown')) {
6637
+ this.cursor += 1
6638
+ continue
6639
+ }
6640
+ if (this.cursor > 0 && !this.total) {
6641
+ this.buffer = this.buffer.subarray(this.cursor)
6642
+ this.cursor = 0
6643
+ }
6644
+ return true
6645
+ } catch (e) {
6646
+ if (!e.message.startsWith('Unrepresentable length')) {
6647
+ throw e
6648
+ }
6649
+ this.cursor += 1
6650
+ }
6651
+ }
6652
+ return false
6653
+ }
6654
+
6655
+ readSize() {
6656
+ const tagObj = this.tagStack[this.tagStack.length - 1];
6657
+
6658
+ /* istanbul ignore if */
6659
+ if (debug.enabled) {
6660
+ debug(`parsing size for tag: ${tagObj.tagStr}`);
6661
+ }
6662
+
6663
+ if (this.cursor >= this.buffer.length) {
6664
+ /* istanbul ignore if */
6665
+ if (debug.enabled) {
6666
+ debug('waiting for more data');
6667
+ }
6668
+
6669
+ return false;
6670
+ }
6671
+
6672
+ const size = tools.readVint(this.buffer, this.cursor);
6673
+
6674
+ if (size == null) {
6675
+ /* istanbul ignore if */
6676
+ if (debug.enabled) {
6677
+ debug('waiting for more data');
6678
+ }
6679
+
6680
+ return false;
6681
+ }
6682
+
6683
+ this.cursor += size.length;
6684
+ this.total += size.length;
6685
+ this.state = STATE_CONTENT;
6686
+ tagObj.dataSize = size.value;
6687
+
6688
+ // unknown size
6689
+ if (size.value === -1) {
6690
+ tagObj.end = -1;
6691
+ } else {
6692
+ tagObj.end += size.value + size.length;
6693
+ }
6694
+ /* istanbul ignore if */
6695
+ if (debug.enabled) {
6696
+ debug(`read size: ${size.value}`);
6697
+ }
6698
+
6699
+ return true;
6700
+ }
6701
+
6702
+ readContent() {
6703
+ const { tagStr, type, dataSize, ...rest } = this.tagStack[this.tagStack.length - 1];
6704
+
6705
+ /* istanbul ignore if */
6706
+ if (debug.enabled) {
6707
+ debug(`parsing content for tag: ${tagStr}`);
6708
+ }
6709
+
6710
+ if (type === 'm') {
6711
+ /* istanbul ignore if */
6712
+ if (debug.enabled) {
6713
+ debug('content should be tags');
6714
+ }
6715
+ this.push(['start', { tagStr, type, dataSize, ...rest }]);
6716
+ this.state = STATE_TAG;
6717
+
6718
+ return true;
6719
+ }
6720
+
6721
+ if (this.buffer.length < this.cursor + dataSize) {
6722
+ /* istanbul ignore if */
6723
+ if (debug.enabled) {
6724
+ debug(`got: ${this.buffer.length}`);
6725
+ debug(`need: ${this.cursor + dataSize}`);
6726
+ debug('waiting for more data');
6727
+ }
6728
+
6729
+ return false;
6730
+ }
6731
+
6732
+ const data = this.buffer.subarray(this.cursor, this.cursor + dataSize);
6733
+ this.total += dataSize;
6734
+ this.state = STATE_TAG;
6735
+ this.buffer = this.buffer.subarray(this.cursor + dataSize);
6736
+ this.cursor = 0;
6737
+
6738
+ this.tagStack.pop(); // remove the object from the stack
6739
+
6740
+ this.push([
6741
+ 'tag',
6742
+ tools.readDataFromTag(
6743
+ { tagStr, type, dataSize, ...rest },
6744
+ Buffer.from(data),
6745
+ ),
6746
+ ]);
6747
+
6748
+ while (this.tagStack.length > 0) {
6749
+ const topEle = this.tagStack[this.tagStack.length - 1];
6750
+ if (this.total < topEle.end) {
6751
+ break;
6752
+ }
6753
+ this.push(['end', topEle]);
6754
+ this.tagStack.pop();
6755
+ }
6756
+
6757
+ /* istanbul ignore if */
6758
+ if (debug.enabled) {
6759
+ debug(`read data: ${data.toString('hex')}`);
6760
+ }
6761
+
6762
+ return true;
6763
+ }
6764
+ }
6765
+
6766
+ module.exports = EbmlDecoder
6721
6767
 
6722
6768
 
6723
6769
  /***/ },