From ea4d941f39fade35487baf4fca09d46e28f1e494 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Wed, 15 May 2019 12:30:40 -0700 Subject: [PATCH] Clean up EBML size constants Instead of using an array of Uint8Arrays, which could trigger a load-time failure on very old browsers, store an array of number arrays. This is equivalent for the purpose of comparison, and it cannot fail at load-time. This cleanup seems to fix minor test flake in the EBML parser tests that was observed with upgraded node modules. Issue #1694 Change-Id: I87b2307bceddb3a21dfc81d4fe9828afd9508617 --- lib/util/ebml_parser.js | 37 +++++++++++++++++------------------- lib/util/uint8array_utils.js | 7 +++++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/util/ebml_parser.js b/lib/util/ebml_parser.js index 36e874958..0f3f39033 100644 --- a/lib/util/ebml_parser.js +++ b/lib/util/ebml_parser.js @@ -37,26 +37,23 @@ shaka.util.EbmlParser = function(dataView) { this.reader_ = new shaka.util.DataViewReader( dataView, shaka.util.DataViewReader.Endianness.BIG_ENDIAN); - - // If not already constructed, build a list of EBML dynamic size constants. - // This is not done at load-time to avoid exceptions on unsupported browsers. - if (!shaka.util.EbmlParser.DYNAMIC_SIZES) { - shaka.util.EbmlParser.DYNAMIC_SIZES = [ - new Uint8Array([0xff]), - new Uint8Array([0x7f, 0xff]), - new Uint8Array([0x3f, 0xff, 0xff]), - new Uint8Array([0x1f, 0xff, 0xff, 0xff]), - new Uint8Array([0x0f, 0xff, 0xff, 0xff, 0xff]), - new Uint8Array([0x07, 0xff, 0xff, 0xff, 0xff, 0xff]), - new Uint8Array([0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), - new Uint8Array([0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), - ]; - } }; -/** @const {!Array.} */ -shaka.util.EbmlParser.DYNAMIC_SIZES; +/** + * A list of EBML dynamic size constants. + * @const {!Array.>} + */ +shaka.util.EbmlParser.DYNAMIC_SIZES = [ + [0xff], + [0x7f, 0xff], + [0x3f, 0xff, 0xff], + [0x1f, 0xff, 0xff, 0xff], + [0x0f, 0xff, 0xff, 0xff, 0xff], + [0x07, 0xff, 0xff, 0xff, 0xff, 0xff], + [0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], + [0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], +]; /** @@ -225,10 +222,10 @@ shaka.util.EbmlParser.getVintValue_ = function(vint) { */ shaka.util.EbmlParser.isDynamicSizeValue_ = function(vint) { const EbmlParser = shaka.util.EbmlParser; - const uint8ArrayEqual = shaka.util.Uint8ArrayUtils.equal; + const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils; - for (let i = 0; i < EbmlParser.DYNAMIC_SIZES.length; i++) { - if (uint8ArrayEqual(vint, EbmlParser.DYNAMIC_SIZES[i])) { + for (const dynamicSizeConst of EbmlParser.DYNAMIC_SIZES) { + if (Uint8ArrayUtils.equal(vint, dynamicSizeConst)) { return true; } } diff --git a/lib/util/uint8array_utils.js b/lib/util/uint8array_utils.js index 899f9c147..713ca9679 100644 --- a/lib/util/uint8array_utils.js +++ b/lib/util/uint8array_utils.js @@ -109,8 +109,11 @@ shaka.util.Uint8ArrayUtils.toHex = function(arr) { /** * Compare two Uint8Arrays for equality. - * @param {Uint8Array} arr1 - * @param {Uint8Array} arr2 + * For convenience, this also accepts Arrays, so that one can trivially compare + * a Uint8Array to an Array of numbers. + * + * @param {(Uint8Array|Array.)} arr1 + * @param {(Uint8Array|Array.)} arr2 * @return {boolean} * @export */