Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all private modules are exported correctly #2050

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/workbox-core/_private.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
*/

// We either expose defaults or we expose every named export.
import {DBWrapper} from './_private/DBWrapper.mjs';
import {deleteDatabase} from './_private/deleteDatabase.mjs';
import {WorkboxError} from './_private/WorkboxError.mjs';
import {assert} from './_private/assert.mjs';
import {cacheNames} from './_private/cacheNames.mjs';
import {cacheWrapper} from './_private/cacheWrapper.mjs';
import {DBWrapper} from './_private/DBWrapper.mjs';
import {Deferred} from './_private/Deferred.mjs';
import {deleteDatabase} from './_private/deleteDatabase.mjs';
import {executeQuotaErrorCallbacks} from './_private/executeQuotaErrorCallbacks.mjs';
import {fetchWrapper} from './_private/fetchWrapper.mjs';
import {getFriendlyURL} from './_private/getFriendlyURL.mjs';
import {logger} from './_private/logger.mjs';
import {WorkboxError} from './_private/WorkboxError.mjs';

import './_version.mjs';

export {
DBWrapper,
deleteDatabase,
WorkboxError,
assert,
cacheNames,
cacheWrapper,
DBWrapper,
Deferred,
deleteDatabase,
executeQuotaErrorCallbacks,
fetchWrapper,
getFriendlyURL,
logger,
WorkboxError,
};
6 changes: 3 additions & 3 deletions packages/workbox-core/_private/cacheWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
https://opensource.org/licenses/MIT.
*/

import {pluginEvents} from '../models/pluginEvents.mjs';
import {pluginUtils} from '../utils/pluginUtils.mjs';
import {WorkboxError} from './WorkboxError.mjs';
import {assert} from './assert.mjs';
import {executeQuotaErrorCallbacks} from './quota.mjs';
import {getFriendlyURL} from './getFriendlyURL.mjs';
import {logger} from './logger.mjs';
import {executeQuotaErrorCallbacks} from './executeQuotaErrorCallbacks.mjs';
import {pluginEvents} from '../models/pluginEvents.mjs';
import {pluginUtils} from '../utils/pluginUtils.mjs';
import '../_version.mjs';


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

This file was deleted.

39 changes: 39 additions & 0 deletions packages/workbox-core/_private/executeQuotaErrorCallbacks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2018 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

import {logger} from '../_private/logger.mjs';
import {quotaErrorCallbacks} from '../models/quotaErrorCallbacks.mjs';
import '../_version.mjs';


/**
* Runs all of the callback functions, one at a time sequentially, in the order
* in which they were registered.
*
* @memberof workbox.core
* @private
*/
async function executeQuotaErrorCallbacks() {
if (process.env.NODE_ENV !== 'production') {
logger.log(`About to run ${quotaErrorCallbacks.size} ` +
`callbacks to clean up caches.`);
}

for (const callback of quotaErrorCallbacks) {
await callback();
if (process.env.NODE_ENV !== 'production') {
logger.log(callback, 'is complete.');
}
}

if (process.env.NODE_ENV !== 'production') {
logger.log('Finished running callbacks.');
}
}

export {executeQuotaErrorCallbacks};
66 changes: 0 additions & 66 deletions packages/workbox-core/_private/quota.mjs

This file was deleted.

15 changes: 15 additions & 0 deletions packages/workbox-core/models/quotaErrorCallbacks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright 2018 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

import '../_version.mjs';


// Callbacks to be executed whenever there's a quota error.
const quotaErrorCallbacks = new Set();

export {quotaErrorCallbacks};
27 changes: 26 additions & 1 deletion packages/workbox-core/registerQuotaErrorCallback.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,33 @@
https://opensource.org/licenses/MIT.
*/

import {registerQuotaErrorCallback} from './_private/quota.mjs';
import {logger} from './_private/logger.mjs';
import {assert} from './_private/assert.mjs';
import {quotaErrorCallbacks} from './models/quotaErrorCallbacks.mjs';
import './_version.mjs';


/**
* Adds a function to the set of quotaErrorCallbacks that will be executed if
* there's a quota error.
*
* @param {Function} callback
* @memberof workbox.core
*/
function registerQuotaErrorCallback(callback) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(callback, 'function', {
moduleName: 'workbox-core',
funcName: 'register',
paramName: 'callback',
});
}

quotaErrorCallbacks.add(callback);

if (process.env.NODE_ENV !== 'production') {
logger.log('Registered a callback to respond to quota errors.', callback);
}
}

export {registerQuotaErrorCallback};
46 changes: 46 additions & 0 deletions test/all/node/test-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,50 @@ describe(`[all] Window and SW packages`, function() {
.to.deep.equal(topLevelFiles.concat(deprecatedExports).sort());
}
});

it(`should have top a level module for every export in _private.mjs (and vise-versa)`, async function() {
for (const pkg of windowAndBrowserPackages) {
const packagePath = path.join(__dirname, '..', '..', '..', 'packages', pkg.name);
const privateFile = path.join(packagePath, '_private.mjs');

// Only some packages have a `_private.mjs` module.
if (!fs.existsSync(privateFile)) {
continue;
}

const privateContents = await fs.readFile(privateFile, 'utf-8');

// Use the acorn parser to generate a list of named exports.
const namedExports = [];
const indexAST = acorn.parse(privateContents, {
ecmaVersion: 6,
sourceType: 'module',
});
for (const node of indexAST.body) {
if (node.type === 'ExportDefaultDeclaration') {
throw new Error(`'_private.mjs' files cannot contain default exports`);
}
if (node.type === 'ExportNamedDeclaration') {
if (node.specifiers.length === 0) {
throw new Error(`'_private.mjs' files may only contain a single, named-export block`);
}
for (const specifier of node.specifiers) {
namedExports.push(specifier.exported.name);
}
}
}

// Inspect the package directory to get a list of top-level, public
// module basenames.
const privateDirectoryPath = path.join(packagePath, '_private');
const topLevelFiles = glob.sync('*.mjs', {cwd: privateDirectoryPath})
.map((file) => path.basename(file, '.mjs'));

const deprecatedExports = deprecatedPackageExports[pkg.name] || [];

// Assert there's a 1-to-1 mapping between exports and top-level files.
expect(namedExports.sort())
.to.deep.equal(topLevelFiles.concat(deprecatedExports).sort());
}
});
});
2 changes: 1 addition & 1 deletion test/workbox-core/sw/_private/test-cacheWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {cacheWrapper} from 'workbox-core/_private/cacheWrapper.mjs';
import {registerQuotaErrorCallback} from 'workbox-core/_private/quota.mjs';
import {registerQuotaErrorCallback} from 'workbox-core/registerQuotaErrorCallback.mjs';


describe(`cacheWrapper`, function() {
Expand Down
Loading