Skip to content

Commit e7709f2

Browse files
authored
feat!: generate UID without randombytes dependency (#196)
1 parent 79ac5da commit e7709f2

File tree

5 files changed

+7
-36
lines changed

5 files changed

+7
-36
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
node-version: [18.x, 20.x, 22.x]
14+
node-version: [20.x, 22.x, 24.x]
1515
steps:
1616
- uses: actions/checkout@v3
1717
- name: Use Node.js ${{ matrix.node-version }}

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ See the accompanying LICENSE file for terms.
66

77
'use strict';
88

9-
var randomBytes = require('randombytes');
10-
119
// Generate an internal UID to make the regexp pattern harder to guess.
1210
var UID_LENGTH = 16;
1311
var UID = generateUID();
@@ -35,7 +33,7 @@ function escapeUnsafeChars(unsafeChar) {
3533
}
3634

3735
function generateUID() {
38-
var bytes = randomBytes(UID_LENGTH);
36+
var bytes = crypto.getRandomValues(new Uint8Array(UID_LENGTH));
3937
var result = '';
4038
for(var i=0; i<UID_LENGTH; ++i) {
4139
result += bytes[i].toString(16);

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"benchmark": "^2.1.4"
2929
},
30-
"dependencies": {
31-
"randombytes": "^2.1.0"
30+
"engines": {
31+
"node": ">=20.0.0"
3232
}
3333
}

test/unit/serialize.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@ const { deepStrictEqual, strictEqual, throws } = require('node:assert');
33

44
var serialize = require('../../');
55

6-
// temporarily monkeypatch `crypto.randomBytes` so we'll have a
7-
// predictable UID for our tests
8-
var crypto = require('crypto');
9-
var oldRandom = crypto.randomBytes;
10-
crypto.randomBytes = function(len, cb) {
11-
var buf = Buffer.alloc(len);
12-
buf.fill(0x00);
13-
if (cb)
14-
cb(null, buf);
15-
return buf;
16-
};
17-
18-
crypto.randomBytes = oldRandom;
19-
206
describe('serialize( obj )', function () {
217
it('should be a function', function () {
228
strictEqual(typeof serialize, 'function');

0 commit comments

Comments
 (0)