runtime-caching-converter.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  4. /*
  5. Copyright 2018 Google LLC
  6. Use of this source code is governed by an MIT-style
  7. license that can be found in the LICENSE file or at
  8. https://opensource.org/licenses/MIT.
  9. */
  10. const ol = require('common-tags').oneLine;
  11. const errors = require('./errors');
  12. const stringifyWithoutComments = require('./stringify-without-comments');
  13. /**
  14. * Given a set of options that configures `sw-toolbox`'s behavior, convert it
  15. * into a string that would configure equivalent `workbox-sw` behavior.
  16. *
  17. * @param {Object} options See
  18. * https://googlechromelabs.github.io/sw-toolbox/api.html#options
  19. * @return {string} A JSON string representing the equivalent options.
  20. *
  21. * @private
  22. */
  23. function getOptionsString(options = {}) {
  24. let plugins = [];
  25. if (options.plugins) {
  26. // Using libs because JSON.stringify won't handle functions.
  27. plugins = options.plugins.map(stringifyWithoutComments);
  28. delete options.plugins;
  29. } // Pull handler-specific config from the options object, since they are
  30. // not directly used to construct a Plugin instance. If set, need to be
  31. // passed as options to the handler constructor instead.
  32. const handlerOptionKeys = ['cacheName', 'networkTimeoutSeconds', 'fetchOptions', 'matchOptions'];
  33. const handlerOptions = {};
  34. for (var _i = 0; _i < handlerOptionKeys.length; _i++) {
  35. const key = handlerOptionKeys[_i];
  36. if (key in options) {
  37. handlerOptions[key] = options[key];
  38. delete options[key];
  39. }
  40. }
  41. const pluginsMapping = {
  42. backgroundSync: 'workbox.backgroundSync.Plugin',
  43. broadcastUpdate: 'workbox.broadcastUpdate.Plugin',
  44. expiration: 'workbox.expiration.Plugin',
  45. cacheableResponse: 'workbox.cacheableResponse.Plugin'
  46. };
  47. var _arr = Object.entries(options);
  48. for (var _i2 = 0; _i2 < _arr.length; _i2++) {
  49. const _arr$_i = (0, _slicedToArray2.default)(_arr[_i2], 2),
  50. pluginName = _arr$_i[0],
  51. pluginConfig = _arr$_i[1];
  52. // Ensure that we have some valid configuration to pass to Plugin().
  53. if (Object.keys(pluginConfig).length === 0) {
  54. continue;
  55. }
  56. const pluginString = pluginsMapping[pluginName];
  57. if (!pluginString) {
  58. throw new Error(`${errors['bad-runtime-caching-config']} ${pluginName}`);
  59. }
  60. let pluginCode;
  61. switch (pluginName) {
  62. // Special case logic for plugins that have a required parameter, and then
  63. // an additional optional config parameter.
  64. case 'backgroundSync':
  65. {
  66. const name = pluginConfig.name;
  67. pluginCode = `new ${pluginString}(${JSON.stringify(name)}`;
  68. if ('options' in pluginConfig) {
  69. pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
  70. }
  71. pluginCode += `)`;
  72. break;
  73. }
  74. case 'broadcastUpdate':
  75. {
  76. const channelName = pluginConfig.channelName;
  77. const opts = Object.assign({
  78. channelName
  79. }, pluginConfig.options);
  80. pluginCode = `new ${pluginString}(${stringifyWithoutComments(opts)})`;
  81. break;
  82. }
  83. // For plugins that just pass in an Object to the constructor, like
  84. // expiration and cacheableResponse
  85. default:
  86. {
  87. pluginCode = `new ${pluginString}(${stringifyWithoutComments(pluginConfig)})`;
  88. }
  89. }
  90. plugins.push(pluginCode);
  91. }
  92. if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
  93. const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
  94. return ol`{
  95. ${optionsString ? optionsString + ',' : ''}
  96. plugins: [${plugins.join(', ')}]
  97. }`;
  98. } else {
  99. return '';
  100. }
  101. }
  102. module.exports = (runtimeCaching = []) => {
  103. return runtimeCaching.map(entry => {
  104. const method = entry.method || 'GET';
  105. if (!entry.urlPattern) {
  106. throw new Error(errors['urlPattern-is-required']);
  107. }
  108. if (!entry.handler) {
  109. throw new Error(errors['handler-is-required']);
  110. } // This validation logic is a bit too gnarly for joi, so it's manually
  111. // implemented here.
  112. if (entry.options && entry.options.networkTimeoutSeconds && entry.handler !== 'NetworkFirst') {
  113. throw new Error(errors['invalid-network-timeout-seconds']);
  114. } // urlPattern might be a string, a RegExp object, or a function.
  115. // If it's a string, it needs to be quoted.
  116. const matcher = typeof entry.urlPattern === 'string' ? JSON.stringify(entry.urlPattern) : entry.urlPattern;
  117. if (typeof entry.handler === 'string') {
  118. const optionsString = getOptionsString(entry.options || {});
  119. const strategyString = `new workbox.strategies.${entry.handler}(${optionsString})`;
  120. return `workbox.routing.registerRoute(` + `${matcher}, ${strategyString}, '${method}');\n`;
  121. } else if (typeof entry.handler === 'function') {
  122. return `workbox.routing.registerRoute(` + `${matcher}, ${entry.handler}, '${method}');\n`;
  123. }
  124. }).filter(entry => Boolean(entry)); // Remove undefined map() return values.
  125. };