Skip to content

Commit 4e1b195

Browse files
authored
fix: replace indexOf with typedArrayIndexOf for IE11 support (#417)
1 parent 5454bdd commit 4e1b195

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed

lib/m2ts/metadata-stream.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var
1313
Stream = require('../utils/stream'),
1414
StreamTypes = require('./stream-types'),
15+
typedArrayIndexOf = require('../utils/typed-array').typedArrayIndexOf,
1516
// Frames that allow different types of text encoding contain a text
1617
// encoding description byte [ID3v2.4.0 section 4.]
1718
textEncodingDescriptionByte = {
@@ -59,7 +60,7 @@ var
5960
}
6061

6162
// parsing fields [ID3v2.4.0 section 4.14.]
62-
mimeTypeEndIndex = frame.data.indexOf(0, i);
63+
mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
6364
if (mimeTypeEndIndex < 0) {
6465
// malformed frame
6566
return;
@@ -73,7 +74,7 @@ var
7374
frame.pictureType = frame.data[i];
7475
i++
7576

76-
descriptionEndIndex = frame.data.indexOf(0, i);
77+
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
7778
if (descriptionEndIndex < 0) {
7879
// malformed frame
7980
return;
@@ -104,22 +105,29 @@ var
104105
frame.values = frame.value.split('\0');
105106
},
106107
'TXXX': function(frame) {
107-
var i;
108+
var descriptionEndIndex;
109+
108110
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
109111
// ignore frames with unrecognized character encodings
110112
return;
111113
}
112114

113-
for (i = 1; i < frame.data.length; i++) {
114-
if (frame.data[i] === 0) {
115-
// parse the text fields
116-
frame.description = parseUtf8(frame.data, 1, i);
117-
// do not include the null terminator in the tag value
118-
// frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.]
119-
frame.value = parseUtf8(frame.data, i + 1, frame.data.length).replace(/\0*$/, '');
120-
break;
121-
}
115+
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
116+
117+
if (descriptionEndIndex === -1) {
118+
return;
122119
}
120+
121+
// parse the text fields
122+
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
123+
// do not include the null terminator in the tag value
124+
// frames that allow different types of encoding contain terminated text
125+
// [ID3v2.4.0 section 4.]
126+
frame.value = parseUtf8(
127+
frame.data,
128+
descriptionEndIndex + 1,
129+
frame.data.length
130+
).replace(/\0*$/, '');
123131
frame.data = frame.value;
124132
},
125133
'W*': function(frame) {
@@ -128,22 +136,29 @@ var
128136
frame.url = parseIso88591(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
129137
},
130138
'WXXX': function(frame) {
131-
var i;
139+
var descriptionEndIndex;
140+
132141
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
133142
// ignore frames with unrecognized character encodings
134143
return;
135144
}
136145

137-
for (i = 1; i < frame.data.length; i++) {
138-
if (frame.data[i] === 0) {
139-
// parse the description and URL fields
140-
frame.description = parseUtf8(frame.data, 1, i);
141-
// URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
142-
// if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3]
143-
frame.url = parseIso88591(frame.data, i + 1, frame.data.length).replace(/\0.*$/, '');
144-
break;
145-
}
146+
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
147+
148+
if (descriptionEndIndex === -1) {
149+
return;
146150
}
151+
152+
// parse the description and URL fields
153+
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
154+
// URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
155+
// if the value is followed by a string termination all the following information
156+
// should be ignored [ID3v2.4.0 section 4.3]
157+
frame.url = parseIso88591(
158+
frame.data,
159+
descriptionEndIndex + 1,
160+
frame.data.length
161+
).replace(/\0.*$/, '');
147162
},
148163
'PRIV': function(frame) {
149164
var i;

lib/utils/typed-array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// IE11 doesn't support indexOf for TypedArrays.
2+
// Once IE11 support is dropped, this function should be removed.
3+
var typedArrayIndexOf = (typedArray, element, fromIndex) => {
4+
if (!typedArray) {
5+
return -1;
6+
}
7+
8+
var currentIndex = fromIndex;
9+
10+
for (; currentIndex < typedArray.length; currentIndex++) {
11+
if (typedArray[currentIndex] === element) {
12+
return currentIndex;
13+
}
14+
}
15+
16+
return -1;
17+
};
18+
19+
module.exports = { typedArrayIndexOf };

test/utils.typed-array.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
'use strict';
3+
4+
var
5+
QUnit = require('qunit'),
6+
typedArrayIndexOf = require('../lib/utils/typed-array').typedArrayIndexOf;
7+
8+
QUnit.module('typedArrayIndexOf');
9+
10+
QUnit.test('returns -1 when no typed array', function(assert) {
11+
assert.equal(typedArrayIndexOf(null, 5, 0), -1, 'returned -1');
12+
});
13+
14+
QUnit.test('returns -1 when element not found', function(assert) {
15+
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3]), 5, 0), -1, 'returned -1');
16+
});
17+
18+
QUnit.test('returns -1 when element not found starting from index', function(assert) {
19+
assert.equal(typedArrayIndexOf(new Uint8Array([3, 5, 6, 7]), 5, 2), -1, 'returned -1');
20+
});
21+
22+
QUnit.test('returns index when element found', function(assert) {
23+
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 0), 2, 'returned 2');
24+
});
25+
26+
QUnit.test('returns index when element found starting from index', function(assert) {
27+
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 2), 2, 'returned 2');
28+
});

0 commit comments

Comments
 (0)