-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrandom_uuid.js
52 lines (44 loc) · 1.17 KB
/
random_uuid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);