Skip to content

Commit

Permalink
Add getRandomValues()
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
kasperisager committed Jan 17, 2025
1 parent 7d9631a commit 0fecb4c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export function randomFillSync<B extends ArrayBuffer | ArrayBufferView>(
): B

export { CryptoError as errors }

export * as webcrypto from './web'
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ exports.randomFill = function randomFill(buffer, offset, size, cb) {
exports.randomFillSync = function randomFillSync(buffer, offset, size) {
return exports.randomFill(buffer, offset, size)
}

// For Node.js compatibility
exports.webcrypto = require('./web')
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
"default": "./index.js"
},
"./package": "./package.json",
"./web": {
"types": "./web.d.ts",
"default": "./web.js"
},
"./constants": "./lib/constants.js",
"./errors": "./lib/errors.js"
},
"files": [
"index.js",
"index.d.ts",
"web.js",
"web.d.ts",
"binding.c",
"binding.js",
"CMakeLists.txt",
Expand Down
3 changes: 3 additions & 0 deletions web.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getRandomValues<B extends ArrayBuffer | ArrayBufferView>(
array: B
): B
6 changes: 6 additions & 0 deletions web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const crypto = require('.')

// https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues
exports.getRandomValues = function getRandomValues(array) {
return crypto.randomFillSync(array)
}

0 comments on commit 0fecb4c

Please sign in to comment.