Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Fix issue where regex is stripped during json stringify (#228)
Browse files Browse the repository at this point in the history
JSON.stringify stringifys RegExp as an empty object literal. This fix post process the toolbox origin regex if one exists so we have it when code generating.
  • Loading branch information
mikestead authored and jeffposnick committed Dec 22, 2016
1 parent 28136db commit 3b816c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/sw-precache.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,22 @@ function generateRuntimeCaching(runtimeCaching) {
// include its body inline.
(typeof curr.handler === 'string' ? 'toolbox.' : '') + curr.handler,
// Default to no options.
JSON.stringify(curr.options || {}));
stringifyToolboxOptions(curr.options));
}

return prev + line;
}, '');
}

function stringifyToolboxOptions(options) {
options = options || {};
var str = JSON.stringify(options);
if (options.origin instanceof RegExp) {
str = str.replace(/("origin":)\{\}/, '$1' + options.origin);
}
return str;
}

function generate(params, callback) {
return new Promise(function(resolve, reject) {
params = defaults(params || {}, {
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,26 @@ describe('generateRuntimeCaching', function() {
}]);
assert.equal(code, '\ntoolbox.router.get(/test/, toolbox.testHandler, {});');
});

it('should handle origin regex', function() {
var code = generateRuntimeCaching([{
urlPattern: '/*',
handler: 'testHandler',
options: {
origin: /http:\/\/www.example\.com/
}
}]);
assert.equal(code, '\ntoolbox.router.get("/*", toolbox.testHandler, {"origin":/http:\\/\\/www.example\\.com/});');
});

it('should handle origin string', function() {
var code = generateRuntimeCaching([{
urlPattern: '/*',
handler: 'testHandler',
options: {
origin: 'http://www.example.com'
}
}]);
assert.equal(code, '\ntoolbox.router.get("/*", toolbox.testHandler, {"origin":"http://www.example.com"});');
});
});

0 comments on commit 3b816c0

Please sign in to comment.