Skip to content

Commit

Permalink
feat: Add randomUUID polyfill (shaka-project#3669)
Browse files Browse the repository at this point in the history
Related to: shaka-project#3662
  • Loading branch information
Álvaro Velad Galván committed Sep 29, 2021
1 parent 157bd77 commit a72adca
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/types/polyfill
Expand Up @@ -13,6 +13,7 @@
+../../lib/polyfill/patchedmediakeys_nop.js
+../../lib/polyfill/patchedmediakeys_webkit.js
+../../lib/polyfill/pip_webkit.js
+../../lib/polyfill/random_uuid.js
+../../lib/polyfill/storage_estimate.js
+../../lib/polyfill/video_play_promise.js
+../../lib/polyfill/videoplaybackquality.js
Expand Down
18 changes: 18 additions & 0 deletions externs/webcrypto.js
@@ -0,0 +1,18 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/


/**
* @fileoverview Externs for crypto properties not in the Closure compiler.
*
* @externs
*/


/**
* @return {string}
*/
webCrypto.Crypto.prototype.randomUUID = function() {};
52 changes: 52 additions & 0 deletions lib/polyfill/random_uuid.js
@@ -0,0 +1,52 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.polyfill.RandomUUID');

goog.require('shaka.log');
goog.require('shaka.polyfill');

/**
* @summary A polyfill to provide window.crypto.randomUUID in all browsers.
* @export
*/
shaka.polyfill.RandomUUID = class {
/**
* Install the polyfill if needed.
* @export
*/
static install() {
shaka.log.debug('randomUUID.install');

if (!window.crypto) {
// See: https://caniuse.com/cryptography
shaka.log.debug(
'window.crypto must be available to install randomUUID polyfill.');
return;
}

if ('randomUUID' in window.crypto) {
shaka.log.debug(
'RandomUUID: Native window.crypto.randomUUID() support found.');
return;
}

window.crypto.randomUUID = shaka.polyfill.RandomUUID.randomUUID_;
}

/**
* @return {string}
* @private
*/
static randomUUID_() {
const url = URL.createObjectURL(new Blob());
const uuid = url.toString();
URL.revokeObjectURL(url);
return uuid.substr(uuid.lastIndexOf('/') + 1);
}
};


shaka.polyfill.register(shaka.polyfill.RandomUUID.install);

0 comments on commit a72adca

Please sign in to comment.