Skip to content

Commit

Permalink
feat: use platform-independent randomBytes (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 authored Nov 14, 2024
1 parent 891c7e9 commit 0993e0c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ["14", "latest"]
node-version: ["18.19.0", "latest"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
23 changes: 7 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"license": "MIT",
"author": "Exodus Movement, Inc.",
"main": "./lib/index.js",
"main": "./src/index.js",
"files": [
"src/"
],
Expand All @@ -17,24 +17,15 @@
"url": "git+https://github.com/ExodusMovement/buffer-noise.git"
},
"scripts": {
"build": "make-dir lib && flow-remove-types ./src/index.js > ./lib/index.js",
"lint": "standard",
"prepare": "npm run build",
"pretest": "npm run build",
"test": "npm run lint && flow check && npm run unit",
"unit": "tape -r flow-remove-types/register ./test/index.js | tf-spec"
"test": "npm run lint && npm run unit",
"unit": "exodus-test"
},
"devDependencies": {
"@tap-format/spec": "^0.2.0",
"babel-eslint": "^7.2.1",
"flow-bin": "^0.43.0",
"flow-remove-types": "^1.2.0",
"make-dir-cli": "^3.0.0",
"standard": "^10.0.0",
"tape": "^4.6.3"
"@exodus/test": "^1.0.0-rc.63",
"standard": "^10.0.0"
},
"standard": {
"parser": "babel-eslint",
"ignore": "lib"
"dependencies": {
"@exodus/crypto": "^1.0.0-rc.14"
}
}
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// @flow
const crypto = require('crypto')
const { randomBytes } = require('@exodus/crypto/randomBytes')

module.exports = function (size: number): Object {
/**
* @param {number} size
* @returns {{expand(Buffer): Buffer, shrink(Buffer): Buffer}}
*/
module.exports = function (size) {
return {
expand (data: Buffer): Buffer {
const buffer = crypto.randomBytes(data.length < size - 4 ? size : data.length + 4)
expand (data) {
const buffer = randomBytes(data.length < size - 4 ? size : data.length + 4)
buffer.writeUInt32BE(data.length, 0)
data.copy(buffer, 4, 0)
return buffer
},
shrink (buffer: Buffer): Buffer {
shrink (buffer) {
const dataLen = buffer.readUInt32BE(0)
return buffer.slice(4, dataLen + 4)
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js → test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('tape')
const test = require('@exodus/test/tape')
const crypto = require('crypto')
const createExpander = require('..')

Expand Down

0 comments on commit 0993e0c

Please sign in to comment.