Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
Serialize JavaScript
====================

Serialize JavaScript to a _superset_ of JSON that includes regular expressions, dates and functions.

[![npm Version][npm-badge]][npm]
[![Dependency Status][david-badge]][david]
![Test](https://github.com/yahoo/serialize-javascript/workflows/Test/badge.svg)
This library is a fork of the [serialize-javacript] library that addresses browser compatibility by using the native crypto module.

## Overview

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.

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.

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.
Expand Down Expand Up @@ -133,11 +126,7 @@ function deserialize(serializedJavascript){
This software is free to use under the Yahoo! Inc. BSD license.
See the [LICENSE file][LICENSE] for license text and copyright information.


[npm]: https://www.npmjs.org/package/serialize-javascript
[npm-badge]: https://img.shields.io/npm/v/serialize-javascript.svg?style=flat-square
[david]: https://david-dm.org/yahoo/serialize-javascript
[david-badge]: https://img.shields.io/david/yahoo/serialize-javascript.svg?style=flat-square
[express-state]: https://github.com/yahoo/express-state
[serialize-javacript]: https://github.com/yahoo/serialize-javascript
[npm]: https://www.npmjs.org/package/@namecheap/serialize-javascript
[JSON.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[LICENSE]: https://github.com/yahoo/serialize-javascript/blob/main/LICENSE
[LICENSE]: https://github.com/namecheap/serialize-javascript/blob/main/LICENSE
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ See the accompanying LICENSE file for terms.

'use strict';

var randomBytes = require('randombytes');
var crypto = require('crypto');

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

function generateUID() {
var bytes = randomBytes(UID_LENGTH);
var result = '';
for(var i=0; i<UID_LENGTH; ++i) {
if (crypto.randomUUID) {
const uuid = crypto.randomUUID();
return uuid.replace(/-/g, '');
}

const randomValues = new Uint8Array(UID_LENGTH);
const bytes = crypto.getRandomValues(randomValues);
let result = '';

for (let i = 0; i < UID_LENGTH; ++i) {
result += bytes[i].toString(16);
}

return result;
}

Expand Down
15 changes: 7 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serialize-javascript",
"version": "6.0.2",
"name": "@namecheap/serialize-javascript",
"version": "6.1.0",
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.",
"main": "index.js",
"scripts": {
Expand All @@ -9,7 +9,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/yahoo/serialize-javascript.git"
"url": "git+https://github.com/namecheap/serialize-javascript.git"
},
"keywords": [
"serialize",
Expand All @@ -18,19 +18,17 @@
"js",
"json"
],
"author": "Eric Ferraiuolo <edf@ericf.me>",
"author": "",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/yahoo/serialize-javascript/issues"
"url": "https://github.com/namecheap/serialize-javascript/issues"
},
"homepage": "https://github.com/yahoo/serialize-javascript",
"homepage": "https://github.com/namecheap/serialize-javascript",
"devDependencies": {
"benchmark": "^2.1.4",
"chai": "^4.1.0",
"mocha": "^10.0.0",
"nyc": "^17.0.0"
},
"dependencies": {
"randombytes": "^2.1.0"
}
"dependencies": {}
}
15 changes: 0 additions & 15 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
/* global describe, it, beforeEach */
'use strict';

// temporarily monkeypatch `crypto.randomBytes` so we'll have a
// predictable UID for our tests
var crypto = require('crypto');
var oldRandom = crypto.randomBytes;
crypto.randomBytes = function(len, cb) {
var buf = Buffer.alloc(len);
buf.fill(0x00);
if (cb)
cb(null, buf);
return buf;
};

var serialize = require('../../'),
expect = require('chai').expect;

crypto.randomBytes = oldRandom;

describe('serialize( obj )', function () {
it('should be a function', function () {
expect(serialize).to.be.a('function');
Expand Down Expand Up @@ -579,5 +565,4 @@ describe('serialize( obj )', function () {
expect(obj.foo).to.equal(fakePlaceholder);
});
});

});