Skip to content

Commit

Permalink
fix: replace indexOf with typedArrayIndexOf for IE11 support (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinger committed Jul 7, 2022
1 parent 5454bdd commit 4e1b195
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 deletions.
59 changes: 37 additions & 22 deletions lib/m2ts/metadata-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var
Stream = require('../utils/stream'),
StreamTypes = require('./stream-types'),
typedArrayIndexOf = require('../utils/typed-array').typedArrayIndexOf,
// Frames that allow different types of text encoding contain a text
// encoding description byte [ID3v2.4.0 section 4.]
textEncodingDescriptionByte = {
Expand Down Expand Up @@ -59,7 +60,7 @@ var
}

// parsing fields [ID3v2.4.0 section 4.14.]
mimeTypeEndIndex = frame.data.indexOf(0, i);
mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
if (mimeTypeEndIndex < 0) {
// malformed frame
return;
Expand All @@ -73,7 +74,7 @@ var
frame.pictureType = frame.data[i];
i++

descriptionEndIndex = frame.data.indexOf(0, i);
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
if (descriptionEndIndex < 0) {
// malformed frame
return;
Expand Down Expand Up @@ -104,22 +105,29 @@ var
frame.values = frame.value.split('\0');
},
'TXXX': function(frame) {
var i;
var descriptionEndIndex;

if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
// ignore frames with unrecognized character encodings
return;
}

for (i = 1; i < frame.data.length; i++) {
if (frame.data[i] === 0) {
// parse the text fields
frame.description = parseUtf8(frame.data, 1, i);
// do not include the null terminator in the tag value
// frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.]
frame.value = parseUtf8(frame.data, i + 1, frame.data.length).replace(/\0*$/, '');
break;
}
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);

if (descriptionEndIndex === -1) {
return;
}

// parse the text fields
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
// do not include the null terminator in the tag value
// frames that allow different types of encoding contain terminated text
// [ID3v2.4.0 section 4.]
frame.value = parseUtf8(
frame.data,
descriptionEndIndex + 1,
frame.data.length
).replace(/\0*$/, '');
frame.data = frame.value;
},
'W*': function(frame) {
Expand All @@ -128,22 +136,29 @@ var
frame.url = parseIso88591(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
},
'WXXX': function(frame) {
var i;
var descriptionEndIndex;

if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
// ignore frames with unrecognized character encodings
return;
}

for (i = 1; i < frame.data.length; i++) {
if (frame.data[i] === 0) {
// parse the description and URL fields
frame.description = parseUtf8(frame.data, 1, i);
// URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
// if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3]
frame.url = parseIso88591(frame.data, i + 1, frame.data.length).replace(/\0.*$/, '');
break;
}
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);

if (descriptionEndIndex === -1) {
return;
}

// parse the description and URL fields
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
// URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
// if the value is followed by a string termination all the following information
// should be ignored [ID3v2.4.0 section 4.3]
frame.url = parseIso88591(
frame.data,
descriptionEndIndex + 1,
frame.data.length
).replace(/\0.*$/, '');
},
'PRIV': function(frame) {
var i;
Expand Down
19 changes: 19 additions & 0 deletions lib/utils/typed-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// IE11 doesn't support indexOf for TypedArrays.
// Once IE11 support is dropped, this function should be removed.
var typedArrayIndexOf = (typedArray, element, fromIndex) => {
if (!typedArray) {
return -1;
}

var currentIndex = fromIndex;

for (; currentIndex < typedArray.length; currentIndex++) {
if (typedArray[currentIndex] === element) {
return currentIndex;
}
}

return -1;
};

module.exports = { typedArrayIndexOf };
28 changes: 28 additions & 0 deletions test/utils.typed-array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

'use strict';

var
QUnit = require('qunit'),
typedArrayIndexOf = require('../lib/utils/typed-array').typedArrayIndexOf;

QUnit.module('typedArrayIndexOf');

QUnit.test('returns -1 when no typed array', function(assert) {
assert.equal(typedArrayIndexOf(null, 5, 0), -1, 'returned -1');
});

QUnit.test('returns -1 when element not found', function(assert) {
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3]), 5, 0), -1, 'returned -1');
});

QUnit.test('returns -1 when element not found starting from index', function(assert) {
assert.equal(typedArrayIndexOf(new Uint8Array([3, 5, 6, 7]), 5, 2), -1, 'returned -1');
});

QUnit.test('returns index when element found', function(assert) {
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 0), 2, 'returned 2');
});

QUnit.test('returns index when element found starting from index', function(assert) {
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 2), 2, 'returned 2');
});

0 comments on commit 4e1b195

Please sign in to comment.