Skip to content

Commit 816699f

Browse files
philipwaltonjeffposnick
authored andcommitted
Ensure all private modules are exported correctly (#2050)
1 parent 23af8ac commit 816699f

14 files changed

+214
-353
lines changed

packages/workbox-core/_private.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@
77
*/
88

99
// We either expose defaults or we expose every named export.
10-
import {DBWrapper} from './_private/DBWrapper.mjs';
11-
import {deleteDatabase} from './_private/deleteDatabase.mjs';
12-
import {WorkboxError} from './_private/WorkboxError.mjs';
1310
import {assert} from './_private/assert.mjs';
1411
import {cacheNames} from './_private/cacheNames.mjs';
1512
import {cacheWrapper} from './_private/cacheWrapper.mjs';
13+
import {DBWrapper} from './_private/DBWrapper.mjs';
14+
import {Deferred} from './_private/Deferred.mjs';
15+
import {deleteDatabase} from './_private/deleteDatabase.mjs';
16+
import {executeQuotaErrorCallbacks} from './_private/executeQuotaErrorCallbacks.mjs';
1617
import {fetchWrapper} from './_private/fetchWrapper.mjs';
1718
import {getFriendlyURL} from './_private/getFriendlyURL.mjs';
1819
import {logger} from './_private/logger.mjs';
20+
import {WorkboxError} from './_private/WorkboxError.mjs';
1921

2022
import './_version.mjs';
2123

2224
export {
23-
DBWrapper,
24-
deleteDatabase,
25-
WorkboxError,
2625
assert,
2726
cacheNames,
2827
cacheWrapper,
28+
DBWrapper,
29+
Deferred,
30+
deleteDatabase,
31+
executeQuotaErrorCallbacks,
2932
fetchWrapper,
3033
getFriendlyURL,
3134
logger,
35+
WorkboxError,
3236
};

packages/workbox-core/_private/cacheWrapper.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
https://opensource.org/licenses/MIT.
77
*/
88

9-
import {pluginEvents} from '../models/pluginEvents.mjs';
10-
import {pluginUtils} from '../utils/pluginUtils.mjs';
119
import {WorkboxError} from './WorkboxError.mjs';
1210
import {assert} from './assert.mjs';
13-
import {executeQuotaErrorCallbacks} from './quota.mjs';
1411
import {getFriendlyURL} from './getFriendlyURL.mjs';
1512
import {logger} from './logger.mjs';
13+
import {executeQuotaErrorCallbacks} from './executeQuotaErrorCallbacks.mjs';
14+
import {pluginEvents} from '../models/pluginEvents.mjs';
15+
import {pluginUtils} from '../utils/pluginUtils.mjs';
1616
import '../_version.mjs';
1717

1818

packages/workbox-core/_private/checkSWFileCacheHeaders.mjs

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2018 Google LLC
3+
4+
Use of this source code is governed by an MIT-style
5+
license that can be found in the LICENSE file or at
6+
https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import {logger} from '../_private/logger.mjs';
10+
import {quotaErrorCallbacks} from '../models/quotaErrorCallbacks.mjs';
11+
import '../_version.mjs';
12+
13+
14+
/**
15+
* Runs all of the callback functions, one at a time sequentially, in the order
16+
* in which they were registered.
17+
*
18+
* @memberof workbox.core
19+
* @private
20+
*/
21+
async function executeQuotaErrorCallbacks() {
22+
if (process.env.NODE_ENV !== 'production') {
23+
logger.log(`About to run ${quotaErrorCallbacks.size} ` +
24+
`callbacks to clean up caches.`);
25+
}
26+
27+
for (const callback of quotaErrorCallbacks) {
28+
await callback();
29+
if (process.env.NODE_ENV !== 'production') {
30+
logger.log(callback, 'is complete.');
31+
}
32+
}
33+
34+
if (process.env.NODE_ENV !== 'production') {
35+
logger.log('Finished running callbacks.');
36+
}
37+
}
38+
39+
export {executeQuotaErrorCallbacks};

packages/workbox-core/_private/quota.mjs

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright 2018 Google LLC
3+
4+
Use of this source code is governed by an MIT-style
5+
license that can be found in the LICENSE file or at
6+
https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import '../_version.mjs';
10+
11+
12+
// Callbacks to be executed whenever there's a quota error.
13+
const quotaErrorCallbacks = new Set();
14+
15+
export {quotaErrorCallbacks};

packages/workbox-core/registerQuotaErrorCallback.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,33 @@
66
https://opensource.org/licenses/MIT.
77
*/
88

9-
import {registerQuotaErrorCallback} from './_private/quota.mjs';
9+
import {logger} from './_private/logger.mjs';
10+
import {assert} from './_private/assert.mjs';
11+
import {quotaErrorCallbacks} from './models/quotaErrorCallbacks.mjs';
1012
import './_version.mjs';
1113

1214

15+
/**
16+
* Adds a function to the set of quotaErrorCallbacks that will be executed if
17+
* there's a quota error.
18+
*
19+
* @param {Function} callback
20+
* @memberof workbox.core
21+
*/
22+
function registerQuotaErrorCallback(callback) {
23+
if (process.env.NODE_ENV !== 'production') {
24+
assert.isType(callback, 'function', {
25+
moduleName: 'workbox-core',
26+
funcName: 'register',
27+
paramName: 'callback',
28+
});
29+
}
30+
31+
quotaErrorCallbacks.add(callback);
32+
33+
if (process.env.NODE_ENV !== 'production') {
34+
logger.log('Registered a callback to respond to quota errors.', callback);
35+
}
36+
}
37+
1338
export {registerQuotaErrorCallback};

test/all/node/test-exports.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,50 @@ describe(`[all] Window and SW packages`, function() {
7373
.to.deep.equal(topLevelFiles.concat(deprecatedExports).sort());
7474
}
7575
});
76+
77+
it(`should have top a level module for every export in _private.mjs (and vise-versa)`, async function() {
78+
for (const pkg of windowAndBrowserPackages) {
79+
const packagePath = path.join(__dirname, '..', '..', '..', 'packages', pkg.name);
80+
const privateFile = path.join(packagePath, '_private.mjs');
81+
82+
// Only some packages have a `_private.mjs` module.
83+
if (!fs.existsSync(privateFile)) {
84+
continue;
85+
}
86+
87+
const privateContents = await fs.readFile(privateFile, 'utf-8');
88+
89+
// Use the acorn parser to generate a list of named exports.
90+
const namedExports = [];
91+
const indexAST = acorn.parse(privateContents, {
92+
ecmaVersion: 6,
93+
sourceType: 'module',
94+
});
95+
for (const node of indexAST.body) {
96+
if (node.type === 'ExportDefaultDeclaration') {
97+
throw new Error(`'_private.mjs' files cannot contain default exports`);
98+
}
99+
if (node.type === 'ExportNamedDeclaration') {
100+
if (node.specifiers.length === 0) {
101+
throw new Error(`'_private.mjs' files may only contain a single, named-export block`);
102+
}
103+
for (const specifier of node.specifiers) {
104+
namedExports.push(specifier.exported.name);
105+
}
106+
}
107+
}
108+
109+
// Inspect the package directory to get a list of top-level, public
110+
// module basenames.
111+
const privateDirectoryPath = path.join(packagePath, '_private');
112+
const topLevelFiles = glob.sync('*.mjs', {cwd: privateDirectoryPath})
113+
.map((file) => path.basename(file, '.mjs'));
114+
115+
const deprecatedExports = deprecatedPackageExports[pkg.name] || [];
116+
117+
// Assert there's a 1-to-1 mapping between exports and top-level files.
118+
expect(namedExports.sort())
119+
.to.deep.equal(topLevelFiles.concat(deprecatedExports).sort());
120+
}
121+
});
76122
});

test/workbox-core/sw/_private/test-cacheWrapper.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

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

1212

1313
describe(`cacheWrapper`, function() {

0 commit comments

Comments
 (0)