Skip to content

Commit

Permalink
Update code and tests to work cross-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Mar 1, 2019
1 parent 60a784e commit 8b9dc50
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 279 deletions.
15 changes: 9 additions & 6 deletions .eslintrc.js
Expand Up @@ -21,15 +21,15 @@ module.exports = {
WorkboxSW: false,
},
rules: {
'jsdoc/check-types': 2,
'jsdoc/newline-after-description': 2,
'max-len': [2, {
"jsdoc/check-types": 2,
"jsdoc/newline-after-description": 2,
"max-len": [2, {
code: 80,
tabWidth: 2,
ignoreComments: true,
ignoreUrls: true,
ignorePattern: '^\\s*import',
}],
"ignorePattern": "^\\s*import",
"ignoreUrls": true
}]
},
plugins: [
'jsdoc',
Expand All @@ -40,6 +40,9 @@ module.exports = {
env: {
mocha: true,
},
globals: {
expectError: false
},
rules: {
'max-len': 0,
'require-jsdoc': 0,
Expand Down
5 changes: 4 additions & 1 deletion packages/workbox-background-sync/Queue.mjs
Expand Up @@ -12,9 +12,12 @@ import {assert} from 'workbox-core/_private/assert.mjs';
import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.mjs';
import {QueueStore} from './lib/QueueStore.mjs';
import {StorableRequest} from './lib/StorableRequest.mjs';
import {TAG_PREFIX, MAX_RETENTION_TIME} from './lib/constants.mjs';
import './_version.mjs';


const TAG_PREFIX = 'workbox-background-sync';
const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes

const queueNames = new Set();

/**
Expand Down
64 changes: 14 additions & 50 deletions packages/workbox-background-sync/lib/QueueStore.mjs
Expand Up @@ -8,12 +8,14 @@

import {assert} from 'workbox-core/_private/assert.mjs';
import {DBWrapper} from 'workbox-core/_private/DBWrapper.mjs';
import {migrateDb} from 'workbox-core/_private/migrateDb.mjs';
import {DB_NAME, DB_VERSION, OBJECT_STORE_NAME, INDEXED_PROP} from '../lib/constants.mjs';

import '../_version.mjs';


const DB_VERSION = 3;
const DB_NAME = 'workbox-background-sync';
const OBJECT_STORE_NAME = 'requests';
const INDEXED_PROP = 'queueName';

/**
* A class to manage storing requests from a Queue in IndexedbDB,
* indexed by their queue name for easier access.
Expand Down Expand Up @@ -154,53 +156,15 @@ export class QueueStore {
*/
_upgradeDb(event) {
const db = event.target.result;
const txn = event.target.transaction;
let oldEntries = [];

migrateDb(event, {
v1: (next) => {
// When migrating from version 0, this will not exist.
if (db.objectStoreNames.contains(OBJECT_STORE_NAME)) {
// Get any existing entries in the v1 requests store
// and then delete it.
const objStore = txn.objectStore(OBJECT_STORE_NAME);
objStore.openCursor().onsuccess = ({target}) => {
const cursor = target.result;
if (cursor) {
oldEntries.push(cursor.value);
cursor.continue();
} else {
db.deleteObjectStore(OBJECT_STORE_NAME);
next();
}
};
} else {
next();
}
},
v2: (next) => {
// Creates v2 of the requests store and adds back any existing
// entries in the new format.
const objStore = db.createObjectStore(OBJECT_STORE_NAME, {
autoIncrement: true,
keyPath: 'id',
});
objStore.createIndex(INDEXED_PROP, INDEXED_PROP, {unique: false});

if (oldEntries.length) {
for (const {queueName, storableRequest} of oldEntries) {
// Move the timestamp from `storableRequest` to the top level.
const timestamp = storableRequest.timestamp;

// Reformat the storable request data
const requestData = Object.assign(
storableRequest.requestInit, {url: storableRequest.url});

objStore.add({queueName, timestamp, requestData});
}
}
next();
},

if (event.oldVersion > 0 && event.oldVersion < DB_VERSION) {
db.deleteObjectStore(OBJECT_STORE_NAME);
}

const objStore = db.createObjectStore(OBJECT_STORE_NAME, {
autoIncrement: true,
keyPath: 'id',
});
objStore.createIndex(INDEXED_PROP, INDEXED_PROP, {unique: false});
}
}
10 changes: 6 additions & 4 deletions packages/workbox-background-sync/lib/StorableRequest.mjs
Expand Up @@ -47,9 +47,11 @@ class StorableRequest {

// Set the body if present.
if (request.method !== 'GET') {
// Use blob to support non-text request bodies,
// and clone first in case the caller still needs the request.
requestData.body = await request.clone().blob();
// Use ArrayBuffer to support non-text request bodies.
// NOTE: we can't use Blobs becuse Safari doesn't support storing
// Blobs in IndexedDB in some cases:
// https://github.com/dfahlander/Dexie.js/issues/618#issuecomment-398348457
requestData.body = await request.clone().arrayBuffer();
}

// Convert the headers from an iterable to an object.
Expand Down Expand Up @@ -106,7 +108,7 @@ class StorableRequest {
const requestData = Object.assign({}, this._requestData);
requestData.headers = Object.assign({}, this._requestData.headers);
if (requestData.body) {
requestData.body = requestData.body.slice();
requestData.body = requestData.body.slice(0);
}

return requestData;
Expand Down
2 changes: 0 additions & 2 deletions packages/workbox-core/_private.mjs
Expand Up @@ -9,7 +9,6 @@
// We either expose defaults or we expose every named export.
import {DBWrapper} from './_private/DBWrapper.mjs';
import {deleteDatabase} from './_private/deleteDatabase.mjs';
import {migrateDb} from './_private/migrateDb.mjs';
import {WorkboxError} from './_private/WorkboxError.mjs';
import {assert} from './_private/assert.mjs';
import {cacheNames} from './_private/cacheNames.mjs';
Expand All @@ -23,7 +22,6 @@ import './_version.mjs';
export {
DBWrapper,
deleteDatabase,
migrateDb,
WorkboxError,
assert,
cacheNames,
Expand Down
7 changes: 5 additions & 2 deletions packages/workbox-core/_private/DBWrapper.mjs
Expand Up @@ -81,7 +81,7 @@ export class DBWrapper {
if (openRequestTimedOut) {
db.close();
} else {
db.onversionchange = this._onversionchange;
db.onversionchange = this._onversionchange.bind(this);
resolve(db);
}
};
Expand Down Expand Up @@ -241,7 +241,10 @@ export class DBWrapper {
* blocked by the current, open connection.
*/
close() {
if (this._db) this._db.close();
if (this._db) {
this._db.close();
this._db = null;
}
}
}

Expand Down
39 changes: 0 additions & 39 deletions packages/workbox-core/_private/migrateDb.mjs

This file was deleted.

0 comments on commit 8b9dc50

Please sign in to comment.