From 70b4579530b556514c4ffcd97becd67b0b26be1a Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Mon, 5 Mar 2018 14:57:09 -0800 Subject: [PATCH] Using cache name as idb name --- .../models/PrecachedDetailsModel.mjs | 7 ++++-- .../integration/precache-and-update.js | 6 +++-- .../models/test-PrecachedDetailsModel.mjs | 22 +++++++++---------- .../utils/getPrecachedIDBData.js | 8 +++---- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/workbox-precaching/models/PrecachedDetailsModel.mjs b/packages/workbox-precaching/models/PrecachedDetailsModel.mjs index 30cd7c8bf..55f1420d1 100644 --- a/packages/workbox-precaching/models/PrecachedDetailsModel.mjs +++ b/packages/workbox-precaching/models/PrecachedDetailsModel.mjs @@ -31,10 +31,13 @@ class PrecachedDetailsModel { /** * Construct a new model for a specific cache. * + * @param {string} dbName * @private */ - constructor() { - this._db = new DBWrapper(`workbox-precaching`, 2, { + constructor(dbName) { + // This ensures the db name contains only letters, numbers, '-', '_' and '$' + const filteredDBName = dbName.replace(/[^\w-]/g, '_'); + this._db = new DBWrapper(filteredDBName, 2, { onupgradeneeded: this._handleUpgrade, }); } diff --git a/test/workbox-precaching/integration/precache-and-update.js b/test/workbox-precaching/integration/precache-and-update.js index 62be8d249..71816c4ab 100644 --- a/test/workbox-precaching/integration/precache-and-update.js +++ b/test/workbox-precaching/integration/precache-and-update.js @@ -4,6 +4,8 @@ const activateAndControlSW = require('../../../infra/testing/activate-and-contro const cleanSWEnv = require('../../../infra/testing/clean-sw'); describe(`[workbox-precaching] Precache and Update`, function() { + const DB_NAME = 'workbox-precache-http___localhost_3004_test_workbox-precaching_static_precache-and-update_'; + let testServerAddress = global.__workbox.server.getAddress(); const testingUrl = `${testServerAddress}/test/workbox-precaching/static/precache-and-update/`; @@ -62,7 +64,7 @@ describe(`[workbox-precaching] Precache and Update`, function() { 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/styles/index.css', ]); - let savedIDBData = await getIdbData(); + let savedIDBData = await getIdbData(DB_NAME); expect(savedIDBData).to.deep.equal([ { revision: '1', @@ -126,7 +128,7 @@ describe(`[workbox-precaching] Precache and Update`, function() { 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/new-request.txt', ]); - savedIDBData = await getIdbData(); + savedIDBData = await getIdbData(DB_NAME); expect(savedIDBData).to.deep.equal([ { revision: '2', diff --git a/test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs b/test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs index 919ea0e03..79286ad83 100644 --- a/test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs +++ b/test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs @@ -19,7 +19,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { describe('constructor', function() { it(`should construct with no input`, async function() { - new PrecachedDetailsModel(); + new PrecachedDetailsModel(`test-idb-name`); }); }); @@ -34,7 +34,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { createObjectStore: sandbox.spy(), }; - const precacheDetailsModel = new PrecachedDetailsModel(); + const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`); precacheDetailsModel._handleUpgrade({ target: { result: fakeDB, @@ -58,7 +58,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { createObjectStore: sandbox.spy(), }; - const precacheDetailsModel = new PrecachedDetailsModel(); + const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`); precacheDetailsModel._handleUpgrade({ oldVersion: 1, target: { @@ -84,7 +84,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { createObjectStore: sandbox.spy(), }; - const precacheDetailsModel = new PrecachedDetailsModel(); + const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`); precacheDetailsModel._handleUpgrade({ oldVersion: 1, target: { @@ -103,13 +103,13 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { describe('_getAllEntries', function() { it(`should return an empty array`, async function() { - const model = new PrecachedDetailsModel(`test-cache-name`); + const model = new PrecachedDetailsModel(`test-idb-name`); const allEntries = await model._getAllEntries(); expect(allEntries).to.deep.equal([]); }); it(`should return entry with ID`, async function() { - const model = new PrecachedDetailsModel(`test-cache-name`); + const model = new PrecachedDetailsModel(`test-idb-name`); await model._addEntry(new PrecacheEntry( {}, '/', '1234', true )); @@ -130,7 +130,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { // TODO Test bad inputs it(`should return false for non-existant entry`, async function() { - const model = new PrecachedDetailsModel(); + const model = new PrecachedDetailsModel(`test-idb-name`); const isCached = await model._isEntryCached( 'test-cache', new PrecacheEntry( @@ -143,7 +143,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { it(`should return false for entry with different revision`, async function() { const cacheName = 'test-cache'; - const model = new PrecachedDetailsModel(); + const model = new PrecachedDetailsModel(`test-idb-name`); await model._addEntry( new PrecacheEntry( @@ -163,7 +163,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { it(`should return false for entry with revision but not in cache`, async function() { const cacheName = 'test-cache'; - const model = new PrecachedDetailsModel(); + const model = new PrecachedDetailsModel(`test-idb-name`); const entry = new PrecacheEntry( {}, '/', '1234', true ); @@ -177,7 +177,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { it(`should return true if entry with revision and in cache`, async function() { const cacheName = 'test-cache'; - const model = new PrecachedDetailsModel(); + const model = new PrecachedDetailsModel(`test-idb-name`); const entry = new PrecacheEntry( {}, '/', '1234', true ); @@ -196,7 +196,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() { // TODO add bad input tests it(`should be able to delete an entry`, async function() { - const model = new PrecachedDetailsModel(); + const model = new PrecachedDetailsModel(`test-idb-name`); await model._addEntry( new PrecacheEntry( diff --git a/test/workbox-precaching/utils/getPrecachedIDBData.js b/test/workbox-precaching/utils/getPrecachedIDBData.js index 06d18623c..baa707e3f 100644 --- a/test/workbox-precaching/utils/getPrecachedIDBData.js +++ b/test/workbox-precaching/utils/getPrecachedIDBData.js @@ -1,7 +1,7 @@ // Get the data for precached details from IDB via the window. -module.exports = () => { - return global.__workbox.webdriver.executeAsyncScript((cb) => { - const request = indexedDB.open('workbox-precaching'); +module.exports = (dbName) => { + return global.__workbox.webdriver.executeAsyncScript((dbName, cb) => { + const request = indexedDB.open(dbName); request.onerror = function(event) { cb('Error opening indexedDB'); }; @@ -18,5 +18,5 @@ module.exports = () => { cb(event.target.result); }; }; - }); + }, dbName); };