diff --git a/.eslintrc.json b/.eslintrc.json index 67e0f20..27df673 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,7 +17,10 @@ "extends": [ "amex/test", "plugin:prettier/recommended" - ] + ], + "rules": { + "jest/no-large-snapshots": "off" + } }, { "files": [ diff --git a/__tests__/__snapshots__/index.spec.js.snap b/__tests__/__snapshots__/index.spec.js.snap index 060d5eb..e9562ce 100644 --- a/__tests__/__snapshots__/index.spec.js.snap +++ b/__tests__/__snapshots__/index.spec.js.snap @@ -1,3 +1,108 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`library consistently exports 1`] = `"on :: function once :: function off :: function emit :: function emitter :: function eventListeners :: object calls :: object createMiddleware :: function createMiddlewareContext :: function createMiddlewareFactory :: function TYPES :: object ENUMS :: object expectedType :: function expectedArrayOfType :: function enumerableException :: function unknownKey :: function unknownEventName :: function validateInput :: function getCacheOptions :: function OneServiceWorkerError :: function errorFactory :: function notEnabled :: function notSupported :: function failedToInstall :: function failure :: function configure :: function getConfig :: function isDevelopment :: function isEventsEnabled :: function isNonStandardEnabled :: function isNavigationPreloadEnabled :: function isServiceWorker :: function isServiceWorkerSupported :: function isNotificationSupported :: function isPushSupported :: function isBackgroundSyncSupported :: function isCacheStorageSupported :: function isIndexedDBSupported :: function isPermissionsSupported :: function isOffline :: function getRegistration :: function getRegistrations :: function register :: function getWorker :: function postMessage :: function update :: function unregister :: function escapeHatch :: function showNotification :: function getNotifications :: function getSubscription :: function subscribe :: function unsubscribe :: function registerTag :: function getTags :: function urlBase64ToUint8Array :: function defaultCacheName :: string defaultCacheOptions :: object normalizeRequest :: function open :: function has :: function keys :: function match :: function matchAll :: function add :: function addAll :: function put :: function remove :: function removeAll :: function entries :: function clear :: function metaDataCacheName :: string createCacheName :: function createCacheEntryName :: function createMetaRequest :: function createMetaResponse :: function getMetaStore :: function getMetaData :: function setMetaData :: function deleteMetaData :: function onInstall :: function onActivate :: function onMessage :: function onPush :: function onSync :: function onFetch :: function cacheBusting :: function cacheRouter :: function cacheStrategy :: function clientsClaim :: function escapeHatchRoute :: function manifest :: function messageContext :: function messenger :: function navigationPreloadActivation :: function navigationPreloadResponse :: function appShell :: function precache :: function skipWaiting :: function expiration :: function"`; +exports[`library consistently exports 1`] = ` +"on :: function +once :: function +off :: function +emit :: function +emitter :: function +eventListeners :: object +calls :: object +createMiddleware :: function +createMiddlewareContext :: function +createMiddlewareFactory :: function +TYPES :: object +ENUMS :: object +expectedType :: function +expectedArrayOfType :: function +enumerableException :: function +unknownKey :: function +unknownEventName :: function +validateInput :: function +getCacheOptions :: function +OneServiceWorkerError :: function +errorFactory :: function +notEnabled :: function +notSupported :: function +failedToInstall :: function +failure :: function +configure :: function +getConfig :: function +isDevelopment :: function +isEventsEnabled :: function +isNonStandardEnabled :: function +isNavigationPreloadEnabled :: function +isServiceWorker :: function +isServiceWorkerSupported :: function +isNotificationSupported :: function +isPushSupported :: function +isBackgroundSyncSupported :: function +isCacheStorageSupported :: function +isIndexedDBSupported :: function +isPermissionsSupported :: function +isOffline :: function +getRegistration :: function +getRegistrations :: function +register :: function +getWorker :: function +postMessage :: function +update :: function +unregister :: function +escapeHatch :: function +showNotification :: function +getNotifications :: function +getSubscription :: function +subscribe :: function +unsubscribe :: function +registerTag :: function +getTags :: function +urlBase64ToUint8Array :: function +cachePrefix :: string +cacheDelimiter :: string +defaultCacheName :: string +defaultCacheOptions :: object +createCacheName :: function +normalizeRequest :: function +open :: function +has :: function +keys :: function +match :: function +matchAll :: function +add :: function +addAll :: function +put :: function +remove :: function +removeAll :: function +entries :: function +clear :: function +metaDataCacheName :: string +createMetaCacheName :: function +createMetaCacheEntryName :: function +createMetaRequest :: function +createMetaResponse :: function +getMetaStore :: function +getMetaData :: function +setMetaData :: function +deleteMetaData :: function +createCacheEntryName :: function +onInstall :: function +onActivate :: function +onMessage :: function +onPush :: function +onSync :: function +onFetch :: function +cacheBusting :: function +cacheRouter :: function +cacheStrategy :: function +clientsClaim :: function +escapeHatchRoute :: function +manifest :: function +messageContext :: function +messenger :: function +navigationPreloadActivation :: function +navigationPreloadResponse :: function +appShell :: function +precache :: function +skipWaiting :: function +expiration :: function" +`; diff --git a/__tests__/cache/__snapshots__/index.spec.js.snap b/__tests__/cache/__snapshots__/index.spec.js.snap index dc3555c..d69ef9a 100644 --- a/__tests__/cache/__snapshots__/index.spec.js.snap +++ b/__tests__/cache/__snapshots__/index.spec.js.snap @@ -1,8 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`cache consistently exports 1`] = ` -"defaultCacheName :: string +"cachePrefix :: string +cacheDelimiter :: string +defaultCacheName :: string defaultCacheOptions :: object +createCacheName :: function normalizeRequest :: function open :: function has :: function @@ -17,12 +20,13 @@ removeAll :: function entries :: function clear :: function metaDataCacheName :: string -createCacheName :: function -createCacheEntryName :: function +createMetaCacheName :: function +createMetaCacheEntryName :: function createMetaRequest :: function createMetaResponse :: function getMetaStore :: function getMetaData :: function setMetaData :: function -deleteMetaData :: function" +deleteMetaData :: function +createCacheEntryName :: function" `; diff --git a/__tests__/cache/cache.spec.js b/__tests__/cache/cache.spec.js index 56c8fab..8afdf48 100644 --- a/__tests__/cache/cache.spec.js +++ b/__tests__/cache/cache.spec.js @@ -28,7 +28,10 @@ import { removeAll, clear, normalizeRequest, - defaultCacheName, + createCacheName, + cacheDelimiter, + cachePrefix, + defaultCacheOptions, } from '../../src/cache/cache'; const cacheName = 'my-cache'; @@ -61,7 +64,7 @@ describe('playground', () => { // would match await expect(match(html)).resolves.toEqual(expect.any(Response)); // would not match different cache name - await expect(match(html, { cacheName: 'one-cache' })).resolves.toBe(null); + await expect(match(html, { cacheName: createCacheName() })).resolves.toBe(null); // matches the newly added response with cache specified await expect(match(html, { cacheName })).resolves.toEqual(expect.any(Response)); // returns only the single html we added @@ -113,6 +116,25 @@ describe('playground', () => { }); }); +describe('createCacheName', () => { + test('returns a string from a given cacheName', async () => { + expect.assertions(1); + + const myCacheName = 'my-cache'; + expect(createCacheName(myCacheName)).toEqual( + [cachePrefix, cacheDelimiter, myCacheName].join(''), + ); + }); +}); + +describe('normalizeRequest', () => { + test('returns a request with the string url passed in', async () => { + expect.assertions(1); + + expect(normalizeRequest('/index.js')).toEqual(new Request('/index.js')); + }); +}); + describe('open', () => { test('open defaults to new cache with the default name', async () => { expect.assertions(3); @@ -144,7 +166,7 @@ describe('has', () => { }); describe('keys', () => { - test('retturns all the keys in the cache', async () => { + test('returns all the keys in the cache', async () => { expect.assertions(3); await expect(keys()).resolves.toHaveLength(0); @@ -165,7 +187,9 @@ describe('entries', () => { await addAll(['/index.js', '/analytics.js']); - await expect(entries()).resolves.toEqual([[await keys(), await open(), defaultCacheName]]); + await expect(entries()).resolves.toEqual([ + [await keys(), await open(), defaultCacheOptions.cacheName], + ]); }); test('can select which caches is returned', async () => { @@ -184,7 +208,7 @@ describe('entries', () => { [[], await open(randomCacheName), randomCacheName], ]); await expect(entries()).resolves.toEqual([ - [await keys(), await open(), defaultCacheName], + [await keys(), await open(), defaultCacheOptions.cacheName], [[], await open(randomCacheName), randomCacheName], ]); }); diff --git a/__tests__/cache/meta-data.spec.js b/__tests__/cache/meta-data.spec.js index 08ec26b..53b3635 100644 --- a/__tests__/cache/meta-data.spec.js +++ b/__tests__/cache/meta-data.spec.js @@ -15,35 +15,49 @@ */ import { - createCacheName, - createCacheEntryName, + createMetaCacheName, + createMetaCacheEntryName, createMetaRequest, createMetaResponse, getMetaData, setMetaData, deleteMetaData, metaDataCacheName, + createCacheEntryName, } from '../../src/cache/meta-data'; -import { clear, defaultCacheName, cachePrefix } from '../../src/cache/cache'; +import { clear, defaultCacheName, cachePrefix, cacheDelimiter } from '../../src/cache/cache'; beforeEach(async () => { await clear(); }); -describe('createCacheName', () => { +describe('createCacheEntryName - deprecated', () => { + beforeAll(() => { + jest.spyOn(console, 'warn'); + console.warn.mockImplementation(); + }); + + test('warns about the deprecation', () => { + const cacheName = 'my-cache'; + expect(createCacheEntryName(cacheName)).toEqual(createMetaCacheEntryName(cacheName)); + expect(console.warn).toHaveBeenCalledTimes(1); + }); +}); + +describe('createMetaCacheName', () => { test('returns the cache name to use for meta data', () => { expect.assertions(1); - expect(createCacheName()).toEqual(`${cachePrefix}/${metaDataCacheName}`); + expect(createMetaCacheName()).toEqual(`${cachePrefix}${cacheDelimiter}${metaDataCacheName}`); }); }); -describe('createCacheEntryName', () => { +describe('createMetaCacheEntryName', () => { test('returns a prefixed url based on the cache name', () => { expect.assertions(1); - expect(createCacheEntryName()).toEqual( - `/${cachePrefix}/${metaDataCacheName}/${defaultCacheName}`, + expect(createMetaCacheEntryName()).toEqual( + `/${cachePrefix}${cacheDelimiter}${metaDataCacheName}/${defaultCacheName}`, ); }); }); @@ -54,8 +68,8 @@ describe('createMetaRequest', () => { const metaRequest = createMetaRequest(); - expect(metaRequest).toEqual(new Request(createCacheEntryName())); - expect(metaRequest.url).toMatch(createCacheEntryName()); + expect(metaRequest).toEqual(new Request(createMetaCacheEntryName())); + expect(metaRequest.url).toMatch(createMetaCacheEntryName()); }); }); diff --git a/__tests__/index.spec.js b/__tests__/index.spec.js index 3b71999..518bcde 100644 --- a/__tests__/index.spec.js +++ b/__tests__/index.spec.js @@ -20,6 +20,6 @@ import { printExports } from './helpers'; describe('library', () => { test('consistently exports', () => { expect.assertions(1); - expect(printExports(library, true)).toMatchSnapshot(); + expect(printExports(library)).toMatchSnapshot(); }); }); diff --git a/__tests__/middleware/expiration.spec.js b/__tests__/middleware/expiration.spec.js index 0368375..3046075 100644 --- a/__tests__/middleware/expiration.spec.js +++ b/__tests__/middleware/expiration.spec.js @@ -16,8 +16,8 @@ import createExpirationMiddleware from '../../src/middleware/expiration'; import { + createMetaCacheName, createMetaRequest, - createCacheName, getMetaData, setMetaData, deleteMetaData, @@ -92,7 +92,7 @@ describe('createExpirationMiddleware', () => { await waitFor(event.waitUntil); await expect( match(createMetaRequest(), { - cacheName: createCacheName(), + cacheName: createMetaCacheName(), }), ).resolves.toBeInstanceOf(Response); await expect( @@ -116,7 +116,7 @@ describe('createExpirationMiddleware', () => { await waitFor(event.waitUntil); await expect( match(createMetaRequest(), { - cacheName: createCacheName(), + cacheName: createMetaCacheName(), }), ).resolves.toBeInstanceOf(Response); await expect( @@ -162,7 +162,7 @@ describe('createExpirationMiddleware', () => { await waitFor(event.waitUntil); await expect( match(createMetaRequest(), { - cacheName: createCacheName(), + cacheName: createMetaCacheName(), }), ).resolves.toBeInstanceOf(Response); await expect( diff --git a/packages/one-service-worker-demo/src/client/sw.js b/packages/one-service-worker-demo/src/client/sw.js index 5be4f59..a8554c2 100644 --- a/packages/one-service-worker-demo/src/client/sw.js +++ b/packages/one-service-worker-demo/src/client/sw.js @@ -80,19 +80,19 @@ on('fetch', [ route: '/index.html', }), cacheRouter({ - matcher: event => /@americanexpress/.test(event.request.url), + match: event => /@americanexpress/.test(event.request.url), }), cacheRouter({ cacheName: 'unpkg-cache', - matcher: event => /unpkg\.com/.test(event.request.url), + match: event => /unpkg\.com/.test(event.request.url), }), cacheRouter({ cacheName: 'example-scripts-cache', - matcher: event => /(scripts.*\.js$)/.test(event.request.url), + match: event => /(scripts.*\.js$)/.test(event.request.url), }), cacheRouter({ cacheName: 'example-static-cache', - matcher: event => /(static|dls).*\.(js|css|svg|png|ttf|woff)$/.test(event.request.url), + match: event => /(static|dls).*\.(js|css|svg|png|ttf|woff)$/.test(event.request.url), }), expiration(), ]); diff --git a/src/cache/__mocks__/cache.js b/src/cache/__mocks__/cache.js index 8c2a61b..199a823 100644 --- a/src/cache/__mocks__/cache.js +++ b/src/cache/__mocks__/cache.js @@ -22,8 +22,10 @@ Object.keys(imported).forEach( export const { cachePrefix, + cacheDelimiter, defaultCacheName, defaultCacheOptions, + createCacheName, normalizeRequest, open, has, diff --git a/src/cache/__mocks__/meta-data.js b/src/cache/__mocks__/meta-data.js index 7c0f9a2..e2ab2c9 100644 --- a/src/cache/__mocks__/meta-data.js +++ b/src/cache/__mocks__/meta-data.js @@ -22,8 +22,8 @@ Object.keys(imported).forEach( export const { metaDataCacheName, - createCacheName, - createCacheEntryName, + createMetaCacheName, + createMetaCacheEntryName, createMetaRequest, createMetaResponse, getMetaStore, diff --git a/src/cache/cache.js b/src/cache/cache.js index 1a1fd7e..2892221 100644 --- a/src/cache/cache.js +++ b/src/cache/cache.js @@ -17,9 +17,15 @@ import { getCacheOptions } from '../utility/validation'; export const cachePrefix = '__sw'; +export const cacheDelimiter = '/'; export const defaultCacheName = 'one-cache'; + +export function createCacheName(cacheName = defaultCacheName) { + return [cachePrefix, cacheName].join(cacheDelimiter); +} + export const defaultCacheOptions = { - cacheName: defaultCacheName, + cacheName: createCacheName(defaultCacheName), }; export function normalizeRequest(request) { @@ -27,11 +33,11 @@ export function normalizeRequest(request) { return request instanceof Request ? request : new Request(request); } -export function open(cacheName = defaultCacheName) { +export function open(cacheName = defaultCacheOptions.cacheName) { return caches.open(cacheName); } -export function has(cacheName = defaultCacheName) { +export function has(cacheName = defaultCacheOptions.cacheName) { return caches.has(cacheName); } diff --git a/src/cache/index.js b/src/cache/index.js index bd7ae31..54c8acf 100644 --- a/src/cache/index.js +++ b/src/cache/index.js @@ -15,8 +15,11 @@ */ export { + cachePrefix, + cacheDelimiter, defaultCacheName, defaultCacheOptions, + createCacheName, normalizeRequest, open, has, @@ -33,12 +36,13 @@ export { } from './cache'; export { metaDataCacheName, - createCacheName, - createCacheEntryName, + createMetaCacheName, + createMetaCacheEntryName, createMetaRequest, createMetaResponse, getMetaStore, getMetaData, setMetaData, deleteMetaData, + createCacheEntryName, } from './meta-data'; diff --git a/src/cache/meta-data.js b/src/cache/meta-data.js index aedd9ae..4d49f84 100644 --- a/src/cache/meta-data.js +++ b/src/cache/meta-data.js @@ -14,20 +14,28 @@ * permissions and limitations under the License. */ -import { match, put, remove, defaultCacheName, cachePrefix } from './cache'; +import { match, put, remove, createCacheName, defaultCacheName } from './cache'; export const metaDataCacheName = '__meta'; -export function createCacheName() { - return `${cachePrefix}/${metaDataCacheName}`; +export function createMetaCacheName() { + return createCacheName(metaDataCacheName); } -export function createCacheEntryName(cacheName = defaultCacheName) { - return `/${createCacheName()}/${cacheName}`; +export function createMetaCacheEntryName(cacheName = defaultCacheName) { + return `/${createMetaCacheName()}/${cacheName}`; +} + +export function createCacheEntryName(cacheName) { + console.warn( + '[One Service Worker]: Deprecation Notice - %s is marked for deprecation and will not be accessible in the next major release.', + 'createCacheEntryName', + ); + return createMetaCacheEntryName(cacheName); } export function createMetaRequest(cacheName) { - return new Request(createCacheEntryName(cacheName)); + return new Request(createMetaCacheEntryName(cacheName)); } export function createMetaResponse({ url, data = {} } = {}) { @@ -42,11 +50,11 @@ export function createMetaResponse({ url, data = {} } = {}) { export function getMetaStore(cacheName) { return match(createMetaRequest(cacheName), { - cacheName: createCacheName(), + cacheName: createMetaCacheName(), }).then(exists => (exists ? exists.json() : Promise.resolve({}))); } -export function getMetaData({ url, cacheName = defaultCacheName } = {}) { +export function getMetaData({ url, cacheName } = {}) { return getMetaStore(cacheName).then(data => { if (url) { const request = new Request(url); @@ -79,7 +87,7 @@ export function setMetaData({ url, cacheName, metadata } = {}) { data: updates, }), { - cacheName: createCacheName(), + cacheName: createMetaCacheName(), }, ).then(() => (key ? updates[key] : updates)); }); @@ -99,7 +107,7 @@ export function deleteMetaData({ url, cacheName } = {}) { if (Object.keys(updates).length === 0) { return remove(metaRequest, { - cacheName: createCacheName(cacheName), + cacheName: createMetaCacheName(cacheName), }); } @@ -111,7 +119,7 @@ export function deleteMetaData({ url, cacheName } = {}) { if (cacheName) { return remove(metaRequest, { - cacheName: createCacheName(cacheName), + cacheName: createMetaCacheName(cacheName), }); } diff --git a/src/index.js b/src/index.js index 7ed3016..cc757d0 100644 --- a/src/index.js +++ b/src/index.js @@ -87,8 +87,11 @@ export { } from './core'; export { + cachePrefix, + cacheDelimiter, defaultCacheName, defaultCacheOptions, + createCacheName, normalizeRequest, open, has, @@ -103,14 +106,15 @@ export { entries, clear, metaDataCacheName, - createCacheName, - createCacheEntryName, + createMetaCacheName, + createMetaCacheEntryName, createMetaRequest, createMetaResponse, getMetaStore, getMetaData, setMetaData, deleteMetaData, + createCacheEntryName, } from './cache'; export {