Skip to content

Commit cc76975

Browse files
rrdelaneyhimanshiLt
authored andcommitted
Add support for URL's (yahoo#123)
1 parent a47fab5 commit cc76975

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
1111
// Generate an internal UID to make the regexp pattern harder to guess.
1212
var UID_LENGTH = 16;
1313
var UID = generateUID();
14-
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B)-' + UID + '-(\\d+)__@"', 'g');
14+
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L)-' + UID + '-(\\d+)__@"', 'g');
1515

1616
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1717
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -72,6 +72,7 @@ module.exports = function serialize(obj, options) {
7272
var undefs = [];
7373
var infinities= [];
7474
var bigInts = [];
75+
var urls = [];
7576

7677
// Returns placeholders for functions and regexps (identified by index)
7778
// which are later replaced by their string representation.
@@ -114,6 +115,10 @@ module.exports = function serialize(obj, options) {
114115
return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@';
115116
}
116117
}
118+
119+
if(origValue instanceof URL) {
120+
return '@__L-' + UID + '-' + (urls.push(origValue) - 1) + '__@';
121+
}
117122
}
118123

119124
if (type === 'function') {
@@ -205,7 +210,7 @@ module.exports = function serialize(obj, options) {
205210
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
206211
}
207212

208-
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0) {
213+
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0 && urls.length === 0) {
209214
return str;
210215
}
211216

@@ -252,6 +257,10 @@ module.exports = function serialize(obj, options) {
252257
return "BigInt(\"" + bigInts[valueIndex] + "\")";
253258
}
254259

260+
if (type === 'L') {
261+
return "new URL(\"" + urls[valueIndex].toString() + "\")";
262+
}
263+
255264
var fn = functions[valueIndex];
256265

257266
return serializeFunc(fn);

test/unit/serialize.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,20 @@ describe('serialize( obj )', function () {
452452
});
453453
});
454454

455+
describe('URL', function () {
456+
it('should serialize URL', function () {
457+
var u = new URL('https://x.com/')
458+
expect(serialize(u)).to.equal('new URL("https://x.com/")');
459+
expect(serialize({t: [u]})).to.be.a('string').equal('{"t":[new URL("https://x.com/")]}');
460+
});
461+
462+
it('should deserialize URL', function () {
463+
var d = eval(serialize(new URL('https://x.com/')));
464+
expect(d).to.be.a('URL');
465+
expect(d.toString()).to.equal('https://x.com/');
466+
});
467+
});
468+
455469
describe('XSS', function () {
456470
it('should encode unsafe HTML chars to Unicode', function () {
457471
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');

0 commit comments

Comments
 (0)