Skip to content

Commit e185c6d

Browse files
author
Egor Manzhula
committed
feat: Rewrite generateUID function to use native crypto module.
1 parent 5838b91 commit e185c6d

5 files changed

Lines changed: 30 additions & 51 deletions

File tree

README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
Serialize JavaScript
22
====================
3-
43
Serialize JavaScript to a _superset_ of JSON that includes regular expressions, dates and functions.
5-
6-
[![npm Version][npm-badge]][npm]
7-
[![Dependency Status][david-badge]][david]
8-
![Test](https://github.com/yahoo/serialize-javascript/workflows/Test/badge.svg)
4+
This library is a fork of the [serialize-javacript] library that addresses browser compatibility by using the native crypto module.
95

106
## Overview
11-
12-
The code in this package began its life as an internal module to [express-state][]. To expand its usefulness, it now lives as `serialize-javascript` — an independent package on npm.
13-
147
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps**, **dates**, **sets** or **maps**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. But this module is also great for communicating between node processes.
158

169
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element.
@@ -133,11 +126,7 @@ function deserialize(serializedJavascript){
133126
This software is free to use under the Yahoo! Inc. BSD license.
134127
See the [LICENSE file][LICENSE] for license text and copyright information.
135128

136-
137-
[npm]: https://www.npmjs.org/package/serialize-javascript
138-
[npm-badge]: https://img.shields.io/npm/v/serialize-javascript.svg?style=flat-square
139-
[david]: https://david-dm.org/yahoo/serialize-javascript
140-
[david-badge]: https://img.shields.io/david/yahoo/serialize-javascript.svg?style=flat-square
141-
[express-state]: https://github.com/yahoo/express-state
129+
[serialize-javacript]: https://github.com/yahoo/serialize-javascript
130+
[npm]: https://www.npmjs.org/package/@namecheap/serialize-javascript
142131
[JSON.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
143-
[LICENSE]: https://github.com/yahoo/serialize-javascript/blob/main/LICENSE
132+
[LICENSE]: https://github.com/namecheap/serialize-javascript/blob/main/LICENSE

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ See the accompanying LICENSE file for terms.
66

77
'use strict';
88

9-
var randomBytes = require('randombytes');
9+
var crypto = require('crypto');
1010

1111
// Generate an internal UID to make the regexp pattern harder to guess.
1212
var UID_LENGTH = 16;
@@ -35,11 +35,19 @@ function escapeUnsafeChars(unsafeChar) {
3535
}
3636

3737
function generateUID() {
38-
var bytes = randomBytes(UID_LENGTH);
39-
var result = '';
40-
for(var i=0; i<UID_LENGTH; ++i) {
38+
if (crypto.randomUUID) {
39+
const uuid = crypto.randomUUID();
40+
return uuid.replace(/-/g, '');
41+
}
42+
43+
const randomValues = new Uint8Array(UID_LENGTH);
44+
const bytes = crypto.getRandomValues(randomValues);
45+
let result = '';
46+
47+
for (let i = 0; i < UID_LENGTH; ++i) {
4148
result += bytes[i].toString(16);
4249
}
50+
4351
return result;
4452
}
4553

package-lock.json

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

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "serialize-javascript",
3-
"version": "6.0.2",
2+
"name": "@namecheap/serialize-javascript",
3+
"version": "6.1.0",
44
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.",
55
"main": "index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
},
1010
"repository": {
1111
"type": "git",
12-
"url": "git+https://github.com/yahoo/serialize-javascript.git"
12+
"url": "git+https://github.com/namecheap/serialize-javascript.git"
1313
},
1414
"keywords": [
1515
"serialize",
@@ -18,19 +18,17 @@
1818
"js",
1919
"json"
2020
],
21-
"author": "Eric Ferraiuolo <edf@ericf.me>",
21+
"author": "",
2222
"license": "BSD-3-Clause",
2323
"bugs": {
24-
"url": "https://github.com/yahoo/serialize-javascript/issues"
24+
"url": "https://github.com/namecheap/serialize-javascript/issues"
2525
},
26-
"homepage": "https://github.com/yahoo/serialize-javascript",
26+
"homepage": "https://github.com/namecheap/serialize-javascript",
2727
"devDependencies": {
2828
"benchmark": "^2.1.4",
2929
"chai": "^4.1.0",
3030
"mocha": "^10.0.0",
3131
"nyc": "^17.0.0"
3232
},
33-
"dependencies": {
34-
"randombytes": "^2.1.0"
35-
}
33+
"dependencies": {}
3634
}

test/unit/serialize.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
/* global describe, it, beforeEach */
22
'use strict';
33

4-
// temporarily monkeypatch `crypto.randomBytes` so we'll have a
5-
// predictable UID for our tests
6-
var crypto = require('crypto');
7-
var oldRandom = crypto.randomBytes;
8-
crypto.randomBytes = function(len, cb) {
9-
var buf = Buffer.alloc(len);
10-
buf.fill(0x00);
11-
if (cb)
12-
cb(null, buf);
13-
return buf;
14-
};
15-
164
var serialize = require('../../'),
175
expect = require('chai').expect;
186

19-
crypto.randomBytes = oldRandom;
20-
217
describe('serialize( obj )', function () {
228
it('should be a function', function () {
239
expect(serialize).to.be.a('function');
@@ -579,5 +565,4 @@ describe('serialize( obj )', function () {
579565
expect(obj.foo).to.equal(fakePlaceholder);
580566
});
581567
});
582-
583568
});

0 commit comments

Comments
 (0)