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

Fix issue where toolbox origin regex is stripped during json stringify #228

Merged
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
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"});');
});
});