Skip to content

Commit 413acc3

Browse files
authored
Merge pull request #370 from aminya/performance-polyfill
fix: polyfill performance for crypto randomuuid
2 parents a2dc2bc + 7fee455 commit 413acc3

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

dist/legacy/assets/actions_python-DAJt9Mk_.js renamed to dist/legacy/assets/actions_python-BgFBDq6n.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/assets/actions_python-DAJt9Mk_.js.map renamed to dist/legacy/assets/actions_python-BgFBDq6n.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@
193193
"util.types",
194194
"web-streams-polyfill",
195195
"timers-browserify",
196-
"fs-extra"
196+
"fs-extra",
197+
"randomuuid-polyfill"
197198
],
198199
"engines": {
199200
"node": ">=12.x",

src/utils/compat/crypto/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from "crypto"
33
import * as crypto from "crypto"
44
export default crypto
55

6-
export { randomUUID } from "randomuuid-polyfill"
6+
export { randomUUID } from "./randomuuid.mjs"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// from https://www.npmjs.com/package/randomuuid-polyfill
2+
3+
import { performance } from "perf_hooks"
4+
5+
// Adapted from https://stackoverflow.com/a/8809472/2993077
6+
if (!global.crypto?.randomUUID) {
7+
if (!global.crypto) {
8+
global.crypto = {}
9+
}
10+
11+
global.crypto.randomUUID = () => {
12+
let date = new Date().getTime()
13+
let performanceNow = performance.now() * 1000
14+
15+
// cspell:disable-next-line
16+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, char => {
17+
let random = Math.random() * 16
18+
if (date > 0) {
19+
random = (date + random) % 16 | 0
20+
date = Math.floor(date / 16)
21+
} else {
22+
random = (performanceNow + random) % 16 | 0
23+
performanceNow = Math.floor(performanceNow / 16)
24+
}
25+
return (char === "x" ? random : (random & 0x3 | 0x8)).toString(16)
26+
})
27+
}
28+
}
29+
30+
const randomUUID = global.crypto.randomUUID.bind(global.crypto)
31+
export { randomUUID }

0 commit comments

Comments
 (0)