forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add randomUUID polyfill (shaka-project#3669)
Related to: shaka-project#3662
- Loading branch information
Álvaro Velad Galván
authored
Sep 29, 2021
1 parent
157bd77
commit a72adca
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
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); |