Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add randomUUID polyfill #3669

Merged
merged 3 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/types/polyfill
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to look up support for WebCrypto to realize that it goes back to Chrome 11 / Firefox 26 / IE 11. Can you please add a comment so that some well-meaning person doesn't assume that this is a problem when reading this code in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A link to https://caniuse.com/cryptography is enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

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);