diff options
author | Liam Murphy <43807659+Liamolucko@users.noreply.github.com> | 2021-01-26 23:34:40 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-26 13:34:40 +0100 |
commit | 06bd692e5c4a8f66960d3919e7087530b60c20dd (patch) | |
tree | 54da22655c71341c0e1e750d5197623e4fa0e286 /std/node/_crypto/randomBytes.ts | |
parent | f9949a31707dcaa5a8786bfe4f84ed202be91607 (diff) |
fix(std/node): Stop callbacks being called twice when callback throws error (#8867)
Diffstat (limited to 'std/node/_crypto/randomBytes.ts')
-rw-r--r-- | std/node/_crypto/randomBytes.ts | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/std/node/_crypto/randomBytes.ts b/std/node/_crypto/randomBytes.ts index 28c7e454e..335c92d68 100644 --- a/std/node/_crypto/randomBytes.ts +++ b/std/node/_crypto/randomBytes.ts @@ -39,8 +39,9 @@ export default function randomBytes( cb?: (err: Error | null, buf?: Buffer) => void, ): Buffer | void { if (typeof cb === "function") { + let err: Error | null = null, bytes: Buffer; try { - cb(null, generateRandomBytes(size)); + bytes = generateRandomBytes(size); } catch (e) { //NodeJS nonsense //If the size is out of range it will throw sync, otherwise throw async @@ -50,9 +51,16 @@ export default function randomBytes( ) { throw e; } else { - cb(e); + err = e; } } + setTimeout(() => { + if (err) { + cb(err); + } else { + cb(null, bytes); + } + }, 0); } else { return generateRandomBytes(size); } |