Skip to content

Commit

Permalink
Merge branch 'v6' into workbox-webpack-plugin-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tropicadri committed Feb 17, 2022
2 parents 3917f2f + 10859de commit 3b2f9d0
Show file tree
Hide file tree
Showing 10 changed files with 592 additions and 29 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/scorecards-analysis.yml
@@ -0,0 +1,55 @@
name: Scorecards supply-chain security
on:
# Only the default branch is supported.
branch_protection_rule:
schedule:
- cron: '33 8 * * 4'
push:
branches: [ v6 ]

# Declare default permissions as read only.
permissions: read-all

jobs:
analysis:
name: Scorecards analysis
runs-on: ubuntu-latest
permissions:
# Needed to upload the results to code-scanning dashboard.
security-events: write
actions: read
contents: read

steps:
- name: "Checkout code"
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2.4.0
with:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@c8416b0b2bf627c349ca92fc8e3de51a64b005cf # v1.0.2
with:
results_file: results.sarif
results_format: sarif
# Read-only PAT token. To create it,
# follow the steps in https://github.com/ossf/scorecard-action#pat-token-creation.
repo_token: ${{ secrets.SCORECARD_READ_TOKEN }}
# Publish the results to enable scorecard badges. For more details, see
# https://github.com/ossf/scorecard-action#publishing-results.
# For private repositories, `publish_results` will automatically be set to `false`,
# regardless of the value entered here.
publish_results: true

# Upload the results as artifacts (optional).
- name: "Upload artifact"
uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2 # v2.3.1
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@5f532563584d71fdef14ee64d17bafb34f751ce5 # v1.0.26
with:
sarif_file: results.sarif
52 changes: 41 additions & 11 deletions packages/workbox-background-sync/src/Queue.ts
Expand Up @@ -11,7 +11,7 @@ import {logger} from 'workbox-core/_private/logger.js';
import {assert} from 'workbox-core/_private/assert.js';
import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';
import {QueueStore} from './lib/QueueStore.js';
import {UnidentifiedQueueStoreEntry} from './lib/QueueDb.js';
import {QueueStoreEntry, UnidentifiedQueueStoreEntry} from './lib/QueueDb.js';
import {StorableRequest} from './lib/StorableRequest.js';
import './_version.js';

Expand All @@ -27,8 +27,9 @@ interface OnSyncCallback {
}

export interface QueueOptions {
onSync?: OnSyncCallback;
forceSyncFallback?: boolean;
maxRetentionTime?: number;
onSync?: OnSyncCallback;
}

interface QueueEntry {
Expand Down Expand Up @@ -79,6 +80,7 @@ class Queue {
private readonly _onSync: OnSyncCallback;
private readonly _maxRetentionTime: number;
private readonly _queueStore: QueueStore;
private readonly _forceSyncFallback: boolean;
private _syncInProgress = false;
private _requestsAddedDuringSync = false;

Expand All @@ -100,8 +102,17 @@ class Queue {
* @param {number} [options.maxRetentionTime=7 days] The amount of time (in
* minutes) a request may be retried. After this amount of time has
* passed, the request will be deleted from the queue.
* @param {boolean} [options.forceSyncFallback=false] If `true`, instead
* of attempting to use background sync events, always attempt to replay
* queued request at service worker startup. Most folks will not need
* this, unless you explicitly target a runtime like Electron that
* exposes the interfaces for background sync, but does not have a working
* implementation.
*/
constructor(name: string, {onSync, maxRetentionTime}: QueueOptions = {}) {
constructor(
name: string,
{forceSyncFallback, onSync, maxRetentionTime}: QueueOptions = {},
) {
// Ensure the store name is not already being used
if (queueNames.has(name)) {
throw new WorkboxError('duplicate-queue-name', {name});
Expand All @@ -112,6 +123,7 @@ class Queue {
this._name = name;
this._onSync = onSync || this.replayRequests;
this._maxRetentionTime = maxRetentionTime || MAX_RETENTION_TIME;
this._forceSyncFallback = Boolean(forceSyncFallback);
this._queueStore = new QueueStore(this._name);

this._addSyncListener();
Expand Down Expand Up @@ -276,7 +288,14 @@ class Queue {
entry.metadata = metadata;
}

await this._queueStore[`${operation}Entry`](entry);
switch (operation) {
case 'push':
await this._queueStore.pushEntry(entry);
break;
case 'unshift':
await this._queueStore.unshiftEntry(entry);
break;
}

if (process.env.NODE_ENV !== 'production') {
logger.log(
Expand Down Expand Up @@ -307,7 +326,15 @@ class Queue {
operation: 'pop' | 'shift',
): Promise<QueueEntry | undefined> {
const now = Date.now();
const entry = await this._queueStore[`${operation}Entry`]();
let entry: QueueStoreEntry | undefined;
switch (operation) {
case 'pop':
entry = await this._queueStore.popEntry();
break;
case 'shift':
entry = await this._queueStore.shiftEntry();
break;
}

if (entry) {
// Ignore requests older than maxRetentionTime. Call this function
Expand Down Expand Up @@ -364,7 +391,8 @@ class Queue {
* Registers a sync event with a tag unique to this instance.
*/
async registerSync(): Promise<void> {
if ('sync' in self.registration) {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
try {
await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
} catch (err) {
Expand All @@ -382,13 +410,14 @@ class Queue {

/**
* In sync-supporting browsers, this adds a listener for the sync event.
* In non-sync-supporting browsers, this will retry the queue on service
* worker startup.
* In non-sync-supporting browsers, or if _forceSyncFallback is true, this
* will retry the queue on service worker startup.
*
* @private
*/
private _addSyncListener() {
if ('sync' in self.registration) {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
self.addEventListener('sync', (event: SyncEvent) => {
if (event.tag === `${TAG_PREFIX}:${this._name}`) {
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -435,8 +464,9 @@ class Queue {
if (process.env.NODE_ENV !== 'production') {
logger.log(`Background sync replaying without background sync event`);
}
// If the browser doesn't support background sync, retry
// every time the service worker starts up as a fallback.
// If the browser doesn't support background sync, or the developer has
// opted-in to not using it, retry every time the service worker starts up
// as a fallback.
void this._onSync({queue: this});
}
}
Expand Down

0 comments on commit 3b2f9d0

Please sign in to comment.