Skip to content

Commit

Permalink
fix: use native uuid generator when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite authored Oct 28, 2024
1 parent 9d24854 commit a279e55
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions chatgpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -1872,14 +1872,19 @@ const chatgpt = { // eslint-disable-line no-redeclare
unminify() { chatgpt.code.unminify(); },

uuidv4() {
let d = new Date().getTime(); // get current timestamp in ms (to ensure UUID uniqueness)
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = ( // generate random nibble
( d + (window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1))*16)%16 | 0 );
d = Math.floor(d/16); // correspond each UUID digit to unique 4-bit chunks of timestamp
return ( c == 'x' ? r : (r&0x3|0x8) ).toString(16); // generate random hexadecimal digit
});
return uuid;
try {
// use native secure uuid generator when available
return crypto.randomUUID();
} catch(_e) {
let d = new Date().getTime(); // get current timestamp in ms (to ensure UUID uniqueness)
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = ( // generate random nibble
( d + (window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1))*16)%16 | 0 );
d = Math.floor(d/16); // correspond each UUID digit to unique 4-bit chunks of timestamp
return ( c == 'x' ? r : (r&0x3|0x8) ).toString(16); // generate random hexadecimal digit
});
return uuid;
}
},

writeCode() { chatgpt.code.write(); }
Expand Down

0 comments on commit a279e55

Please sign in to comment.