@wordpress/private-apis 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/LICENSE.md +788 -0
- package/README.md +98 -0
- package/build/implementation.js +208 -0
- package/build/implementation.js.map +1 -0
- package/build/index.js +14 -0
- package/build/index.js.map +1 -0
- package/build-module/implementation.js +193 -0
- package/build-module/implementation.js.map +1 -0
- package/build-module/index.js +2 -0
- package/build-module/index.js.map +1 -0
- package/build-types/implementation.d.ts +72 -0
- package/build-types/implementation.d.ts.map +1 -0
- package/build-types/index.d.ts +2 -0
- package/build-types/index.d.ts.map +1 -0
- package/package.json +37 -0
- package/src/implementation.js +220 -0
- package/src/index.js +1 -0
- package/src/test/index.js +308 -0
- package/tsconfig.json +12 -0
- package/tsconfig.tsbuildinfo +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Private APIs
|
|
2
|
+
|
|
3
|
+
`@wordpress/private-apis` enables sharing private `__experimental` APIs across `@wordpress` packages without
|
|
4
|
+
[publicly exposing them to WordPress extenders](https://make.wordpress.org/core/2022/08/10/proposal-stop-merging-experimental-apis-from-gutenberg-to-wordpress-core/#respond).
|
|
5
|
+
|
|
6
|
+
## Getting started
|
|
7
|
+
|
|
8
|
+
Every `@wordpress` package wanting to privately access or expose experimental APIs must opt-in to `@wordpress/private-apis`:
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
// In packages/block-editor/private-apis.js:
|
|
12
|
+
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
|
|
13
|
+
export const { lock, unlock } =
|
|
14
|
+
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
|
|
15
|
+
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
|
|
16
|
+
'@wordpress/block-editor' // Name of the package calling __dangerousOptInToUnstableAPIsOnlyForCoreModules,
|
|
17
|
+
// (not the name of the package whose APIs you want to access)
|
|
18
|
+
);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Each package may only opt in once. The function name communicates that plugins are not supposed to use it.
|
|
22
|
+
|
|
23
|
+
The function will throw an error if the following conditions are not met:
|
|
24
|
+
|
|
25
|
+
1. The first argument must exactly match the required consent string: `'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.'`.
|
|
26
|
+
2. The second argument must be a known `@wordpress` package that hasn't yet opted into `@wordpress/private-apis`
|
|
27
|
+
|
|
28
|
+
Once the opt-in is complete, the obtained `lock()` and `unlock()` utilities enable hiding `__experimental` APIs from the naked eye:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
// Say this object is exported from a package:
|
|
32
|
+
export const publicObject = {};
|
|
33
|
+
|
|
34
|
+
// However, this string is internal and should not be publicly available:
|
|
35
|
+
const __experimentalString = '__experimental information';
|
|
36
|
+
|
|
37
|
+
// Solution: lock the string "inside" of the object:
|
|
38
|
+
lock( publicObject, __experimentalString );
|
|
39
|
+
|
|
40
|
+
// The string is not nested in the object and cannot be extracted from it:
|
|
41
|
+
console.log( publicObject );
|
|
42
|
+
// {}
|
|
43
|
+
|
|
44
|
+
// The only way to access the string is by "unlocking" the object:
|
|
45
|
+
console.log( unlock( publicObject ) );
|
|
46
|
+
// "__experimental information"
|
|
47
|
+
|
|
48
|
+
// lock() accepts all data types, not just strings:
|
|
49
|
+
export const anotherObject = {};
|
|
50
|
+
lock( anotherObject, function __experimentalFn() {} );
|
|
51
|
+
console.log( unlock( anotherObject ) );
|
|
52
|
+
// function __experimentalFn() {}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Use `lock()` and `unlock()` to privately distribute the `__experimental` APIs across `@wordpress` packages:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
// In packages/package1/index.js:
|
|
59
|
+
import { lock } from './private-apis';
|
|
60
|
+
|
|
61
|
+
export const privateApis = {};
|
|
62
|
+
/* Attach private data to the exported object */
|
|
63
|
+
lock( privateApis, {
|
|
64
|
+
__experimentalFunction: function () {},
|
|
65
|
+
} );
|
|
66
|
+
|
|
67
|
+
// In packages/package2/index.js:
|
|
68
|
+
import { privateApis } from '@wordpress/package1';
|
|
69
|
+
import { unlock } from './private-apis';
|
|
70
|
+
|
|
71
|
+
const { __experimentalFunction } = unlock( privateApis );
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Shipping experimental APIs
|
|
75
|
+
|
|
76
|
+
See the [Experimental and Unstable APIs chapter of Coding Guidelines](/docs/contributors/code/coding-guidelines.md) to learn how `lock()` and `unlock()` can help
|
|
77
|
+
you ship private experimental functions, arguments, components, properties, actions, selectors.
|
|
78
|
+
|
|
79
|
+
## Technical limitations
|
|
80
|
+
|
|
81
|
+
A determined developer who would want to use the private experimental APIs at all costs would have to:
|
|
82
|
+
|
|
83
|
+
- Realize a private importing system exists
|
|
84
|
+
- Read the code where the risks would be spelled out in capital letters
|
|
85
|
+
- Explicitly type out he or she is aware of the consequences
|
|
86
|
+
- Pretend to register a `@wordpress` package (and trigger an error as soon as the real package is loaded)
|
|
87
|
+
|
|
88
|
+
Dangerously opting in to using these APIs by theme and plugin developers is not recommended. Furthermore, the WordPress Core philosophy to strive to maintain backward compatibility for third-party developers **does not apply** to experimental APIs registered via this package.
|
|
89
|
+
|
|
90
|
+
The consent string for opting in to these APIs may change at any time and without notice. This change will break existing third-party code. Such a change may occur in either a major or minor release.
|
|
91
|
+
|
|
92
|
+
## Contributing to this package
|
|
93
|
+
|
|
94
|
+
This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
|
|
95
|
+
|
|
96
|
+
To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
|
|
97
|
+
|
|
98
|
+
<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.__dangerousOptInToUnstableAPIsOnlyForCoreModules = void 0;
|
|
7
|
+
exports.allowCoreModule = allowCoreModule;
|
|
8
|
+
exports.resetAllowedCoreModules = resetAllowedCoreModules;
|
|
9
|
+
exports.resetRegisteredPrivateApis = resetRegisteredPrivateApis;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* wordpress/private-apis – the utilities to enable private cross-package
|
|
13
|
+
* exports of private APIs.
|
|
14
|
+
*
|
|
15
|
+
* This "implementation.js" file is needed for the sake of the unit tests. It
|
|
16
|
+
* exports more than the public API of the package to aid in testing.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The list of core modules allowed to opt-in to the private APIs.
|
|
21
|
+
*/
|
|
22
|
+
const CORE_MODULES_USING_PRIVATE_APIS = ['@wordpress/block-editor', '@wordpress/block-library', '@wordpress/blocks', '@wordpress/components', '@wordpress/customize-widgets', '@wordpress/data', '@wordpress/edit-post', '@wordpress/edit-site', '@wordpress/edit-widgets', '@wordpress/editor'];
|
|
23
|
+
/**
|
|
24
|
+
* A list of core modules that already opted-in to
|
|
25
|
+
* the privateApis package.
|
|
26
|
+
*
|
|
27
|
+
* @type {string[]}
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const registeredPrivateApis = [];
|
|
31
|
+
/*
|
|
32
|
+
* Warning for theme and plugin developers.
|
|
33
|
+
*
|
|
34
|
+
* The use of private developer APIs is intended for use by WordPress Core
|
|
35
|
+
* and the Gutenberg plugin exclusively.
|
|
36
|
+
*
|
|
37
|
+
* Dangerously opting in to using these APIs is NOT RECOMMENDED. Furthermore,
|
|
38
|
+
* the WordPress Core philosophy to strive to maintain backward compatibility
|
|
39
|
+
* for third-party developers DOES NOT APPLY to private APIs.
|
|
40
|
+
*
|
|
41
|
+
* THE CONSENT STRING FOR OPTING IN TO THESE APIS MAY CHANGE AT ANY TIME AND
|
|
42
|
+
* WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A
|
|
43
|
+
* CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
const requiredConsent = 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.';
|
|
47
|
+
/** @type {boolean} */
|
|
48
|
+
|
|
49
|
+
let allowReRegistration; // Use try/catch to force "false" if the environment variable is not explicitly
|
|
50
|
+
// set to true (e.g. when building WordPress core).
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
var _process$env$ALLOW_EX;
|
|
54
|
+
|
|
55
|
+
allowReRegistration = (_process$env$ALLOW_EX = process.env.ALLOW_EXPERIMENT_REREGISTRATION) !== null && _process$env$ALLOW_EX !== void 0 ? _process$env$ALLOW_EX : false;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
allowReRegistration = false;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Called by a @wordpress package wishing to opt-in to accessing or exposing
|
|
61
|
+
* private private APIs.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} consent The consent string.
|
|
64
|
+
* @param {string} moduleName The name of the module that is opting in.
|
|
65
|
+
* @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (consent, moduleName) => {
|
|
70
|
+
if (!CORE_MODULES_USING_PRIVATE_APIS.includes(moduleName)) {
|
|
71
|
+
throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}". ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!allowReRegistration && registeredPrivateApis.includes(moduleName)) {
|
|
75
|
+
// This check doesn't play well with Story Books / Hot Module Reloading
|
|
76
|
+
// and isn't included in the Gutenberg plugin. It only matters in the
|
|
77
|
+
// WordPress core release.
|
|
78
|
+
throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}" which is already registered. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (consent !== requiredConsent) {
|
|
82
|
+
throw new Error(`You tried to opt-in to unstable APIs without confirming you know the consequences. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on the next WordPress release.');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
registeredPrivateApis.push(moduleName);
|
|
86
|
+
return {
|
|
87
|
+
lock,
|
|
88
|
+
unlock
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Binds private data to an object.
|
|
93
|
+
* It does not alter the passed object in any way, only
|
|
94
|
+
* registers it in an internal map of private data.
|
|
95
|
+
*
|
|
96
|
+
* The private data can't be accessed by any other means
|
|
97
|
+
* than the `unlock` function.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```js
|
|
101
|
+
* const object = {};
|
|
102
|
+
* const privateData = { a: 1 };
|
|
103
|
+
* lock( object, privateData );
|
|
104
|
+
*
|
|
105
|
+
* object
|
|
106
|
+
* // {}
|
|
107
|
+
*
|
|
108
|
+
* unlock( object );
|
|
109
|
+
* // { a: 1 }
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @param {any} object The object to bind the private data to.
|
|
113
|
+
* @param {any} privateData The private data to bind to the object.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
exports.__dangerousOptInToUnstableAPIsOnlyForCoreModules = __dangerousOptInToUnstableAPIsOnlyForCoreModules;
|
|
118
|
+
|
|
119
|
+
function lock(object, privateData) {
|
|
120
|
+
if (!object) {
|
|
121
|
+
throw new Error('Cannot lock an undefined object.');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!(__private in object)) {
|
|
125
|
+
object[__private] = {};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
lockedData.set(object[__private], privateData);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Unlocks the private data bound to an object.
|
|
132
|
+
*
|
|
133
|
+
* It does not alter the passed object in any way, only
|
|
134
|
+
* returns the private data paired with it using the `lock()`
|
|
135
|
+
* function.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```js
|
|
139
|
+
* const object = {};
|
|
140
|
+
* const privateData = { a: 1 };
|
|
141
|
+
* lock( object, privateData );
|
|
142
|
+
*
|
|
143
|
+
* object
|
|
144
|
+
* // {}
|
|
145
|
+
*
|
|
146
|
+
* unlock( object );
|
|
147
|
+
* // { a: 1 }
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @param {any} object The object to unlock the private data from.
|
|
151
|
+
* @return {any} The private data bound to the object.
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
function unlock(object) {
|
|
156
|
+
if (!object) {
|
|
157
|
+
throw new Error('Cannot unlock an undefined object.');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!(__private in object)) {
|
|
161
|
+
throw new Error('Cannot unlock an object that was not locked before. ');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return lockedData.get(object[__private]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const lockedData = new WeakMap();
|
|
168
|
+
/**
|
|
169
|
+
* Used by lock() and unlock() to uniquely identify the private data
|
|
170
|
+
* related to a containing object.
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
const __private = Symbol('Private API ID'); // Unit tests utilities:
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Private function to allow the unit tests to allow
|
|
177
|
+
* a mock module to access the private APIs.
|
|
178
|
+
*
|
|
179
|
+
* @param {string} name The name of the module.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
function allowCoreModule(name) {
|
|
184
|
+
CORE_MODULES_USING_PRIVATE_APIS.push(name);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Private function to allow the unit tests to set
|
|
188
|
+
* a custom list of allowed modules.
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
function resetAllowedCoreModules() {
|
|
193
|
+
while (CORE_MODULES_USING_PRIVATE_APIS.length) {
|
|
194
|
+
CORE_MODULES_USING_PRIVATE_APIS.pop();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Private function to allow the unit tests to reset
|
|
199
|
+
* the list of registered private apis.
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
function resetRegisteredPrivateApis() {
|
|
204
|
+
while (registeredPrivateApis.length) {
|
|
205
|
+
registeredPrivateApis.pop();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=implementation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/private-apis/src/implementation.js"],"names":["CORE_MODULES_USING_PRIVATE_APIS","registeredPrivateApis","requiredConsent","allowReRegistration","process","env","ALLOW_EXPERIMENT_REREGISTRATION","error","__dangerousOptInToUnstableAPIsOnlyForCoreModules","consent","moduleName","includes","Error","push","lock","unlock","object","privateData","__private","lockedData","set","get","WeakMap","Symbol","allowCoreModule","name","resetAllowedCoreModules","length","pop","resetRegisteredPrivateApis"],"mappings":"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","sourcesContent":["/**\n * wordpress/private-apis – the utilities to enable private cross-package\n * exports of private APIs.\n *\n * This \"implementation.js\" file is needed for the sake of the unit tests. It\n * exports more than the public API of the package to aid in testing.\n */\n\n/**\n * The list of core modules allowed to opt-in to the private APIs.\n */\nconst CORE_MODULES_USING_PRIVATE_APIS = [\n\t'@wordpress/block-editor',\n\t'@wordpress/block-library',\n\t'@wordpress/blocks',\n\t'@wordpress/components',\n\t'@wordpress/customize-widgets',\n\t'@wordpress/data',\n\t'@wordpress/edit-post',\n\t'@wordpress/edit-site',\n\t'@wordpress/edit-widgets',\n\t'@wordpress/editor',\n];\n\n/**\n * A list of core modules that already opted-in to\n * the privateApis package.\n *\n * @type {string[]}\n */\nconst registeredPrivateApis = [];\n\n/*\n * Warning for theme and plugin developers.\n *\n * The use of private developer APIs is intended for use by WordPress Core\n * and the Gutenberg plugin exclusively.\n *\n * Dangerously opting in to using these APIs is NOT RECOMMENDED. Furthermore,\n * the WordPress Core philosophy to strive to maintain backward compatibility\n * for third-party developers DOES NOT APPLY to private APIs.\n *\n * THE CONSENT STRING FOR OPTING IN TO THESE APIS MAY CHANGE AT ANY TIME AND\n * WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A\n * CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.\n */\nconst requiredConsent =\n\t'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.';\n\n/** @type {boolean} */\nlet allowReRegistration;\n// Use try/catch to force \"false\" if the environment variable is not explicitly\n// set to true (e.g. when building WordPress core).\ntry {\n\tallowReRegistration = process.env.ALLOW_EXPERIMENT_REREGISTRATION ?? false;\n} catch ( error ) {\n\tallowReRegistration = false;\n}\n\n/**\n * Called by a @wordpress package wishing to opt-in to accessing or exposing\n * private private APIs.\n *\n * @param {string} consent The consent string.\n * @param {string} moduleName The name of the module that is opting in.\n * @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.\n */\nexport const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (\n\tconsent,\n\tmoduleName\n) => {\n\tif ( ! CORE_MODULES_USING_PRIVATE_APIS.includes( moduleName ) ) {\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs as module \"${ moduleName }\". ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will be removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on one of the next WordPress releases.'\n\t\t);\n\t}\n\tif (\n\t\t! allowReRegistration &&\n\t\tregisteredPrivateApis.includes( moduleName )\n\t) {\n\t\t// This check doesn't play well with Story Books / Hot Module Reloading\n\t\t// and isn't included in the Gutenberg plugin. It only matters in the\n\t\t// WordPress core release.\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs as module \"${ moduleName }\" which is already registered. ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will be removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on one of the next WordPress releases.'\n\t\t);\n\t}\n\tif ( consent !== requiredConsent ) {\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs without confirming you know the consequences. ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on the next WordPress release.'\n\t\t);\n\t}\n\tregisteredPrivateApis.push( moduleName );\n\n\treturn {\n\t\tlock,\n\t\tunlock,\n\t};\n};\n\n/**\n * Binds private data to an object.\n * It does not alter the passed object in any way, only\n * registers it in an internal map of private data.\n *\n * The private data can't be accessed by any other means\n * than the `unlock` function.\n *\n * @example\n * ```js\n * const object = {};\n * const privateData = { a: 1 };\n * lock( object, privateData );\n *\n * object\n * // {}\n *\n * unlock( object );\n * // { a: 1 }\n * ```\n *\n * @param {any} object The object to bind the private data to.\n * @param {any} privateData The private data to bind to the object.\n */\nfunction lock( object, privateData ) {\n\tif ( ! object ) {\n\t\tthrow new Error( 'Cannot lock an undefined object.' );\n\t}\n\tif ( ! ( __private in object ) ) {\n\t\tobject[ __private ] = {};\n\t}\n\tlockedData.set( object[ __private ], privateData );\n}\n\n/**\n * Unlocks the private data bound to an object.\n *\n * It does not alter the passed object in any way, only\n * returns the private data paired with it using the `lock()`\n * function.\n *\n * @example\n * ```js\n * const object = {};\n * const privateData = { a: 1 };\n * lock( object, privateData );\n *\n * object\n * // {}\n *\n * unlock( object );\n * // { a: 1 }\n * ```\n *\n * @param {any} object The object to unlock the private data from.\n * @return {any} The private data bound to the object.\n */\nfunction unlock( object ) {\n\tif ( ! object ) {\n\t\tthrow new Error( 'Cannot unlock an undefined object.' );\n\t}\n\tif ( ! ( __private in object ) ) {\n\t\tthrow new Error(\n\t\t\t'Cannot unlock an object that was not locked before. '\n\t\t);\n\t}\n\n\treturn lockedData.get( object[ __private ] );\n}\n\nconst lockedData = new WeakMap();\n\n/**\n * Used by lock() and unlock() to uniquely identify the private data\n * related to a containing object.\n */\nconst __private = Symbol( 'Private API ID' );\n\n// Unit tests utilities:\n\n/**\n * Private function to allow the unit tests to allow\n * a mock module to access the private APIs.\n *\n * @param {string} name The name of the module.\n */\nexport function allowCoreModule( name ) {\n\tCORE_MODULES_USING_PRIVATE_APIS.push( name );\n}\n\n/**\n * Private function to allow the unit tests to set\n * a custom list of allowed modules.\n */\nexport function resetAllowedCoreModules() {\n\twhile ( CORE_MODULES_USING_PRIVATE_APIS.length ) {\n\t\tCORE_MODULES_USING_PRIVATE_APIS.pop();\n\t}\n}\n/**\n * Private function to allow the unit tests to reset\n * the list of registered private apis.\n */\nexport function resetRegisteredPrivateApis() {\n\twhile ( registeredPrivateApis.length ) {\n\t\tregisteredPrivateApis.pop();\n\t}\n}\n"]}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "__dangerousOptInToUnstableAPIsOnlyForCoreModules", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _implementation.__dangerousOptInToUnstableAPIsOnlyForCoreModules;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
var _implementation = require("./implementation");
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/private-apis/src/index.js"],"names":[],"mappings":";;;;;;;;;;;;AAAA","sourcesContent":["export { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from './implementation';\n"]}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wordpress/private-apis – the utilities to enable private cross-package
|
|
3
|
+
* exports of private APIs.
|
|
4
|
+
*
|
|
5
|
+
* This "implementation.js" file is needed for the sake of the unit tests. It
|
|
6
|
+
* exports more than the public API of the package to aid in testing.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The list of core modules allowed to opt-in to the private APIs.
|
|
11
|
+
*/
|
|
12
|
+
const CORE_MODULES_USING_PRIVATE_APIS = ['@wordpress/block-editor', '@wordpress/block-library', '@wordpress/blocks', '@wordpress/components', '@wordpress/customize-widgets', '@wordpress/data', '@wordpress/edit-post', '@wordpress/edit-site', '@wordpress/edit-widgets', '@wordpress/editor'];
|
|
13
|
+
/**
|
|
14
|
+
* A list of core modules that already opted-in to
|
|
15
|
+
* the privateApis package.
|
|
16
|
+
*
|
|
17
|
+
* @type {string[]}
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const registeredPrivateApis = [];
|
|
21
|
+
/*
|
|
22
|
+
* Warning for theme and plugin developers.
|
|
23
|
+
*
|
|
24
|
+
* The use of private developer APIs is intended for use by WordPress Core
|
|
25
|
+
* and the Gutenberg plugin exclusively.
|
|
26
|
+
*
|
|
27
|
+
* Dangerously opting in to using these APIs is NOT RECOMMENDED. Furthermore,
|
|
28
|
+
* the WordPress Core philosophy to strive to maintain backward compatibility
|
|
29
|
+
* for third-party developers DOES NOT APPLY to private APIs.
|
|
30
|
+
*
|
|
31
|
+
* THE CONSENT STRING FOR OPTING IN TO THESE APIS MAY CHANGE AT ANY TIME AND
|
|
32
|
+
* WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A
|
|
33
|
+
* CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const requiredConsent = 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.';
|
|
37
|
+
/** @type {boolean} */
|
|
38
|
+
|
|
39
|
+
let allowReRegistration; // Use try/catch to force "false" if the environment variable is not explicitly
|
|
40
|
+
// set to true (e.g. when building WordPress core).
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
var _process$env$ALLOW_EX;
|
|
44
|
+
|
|
45
|
+
allowReRegistration = (_process$env$ALLOW_EX = process.env.ALLOW_EXPERIMENT_REREGISTRATION) !== null && _process$env$ALLOW_EX !== void 0 ? _process$env$ALLOW_EX : false;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
allowReRegistration = false;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Called by a @wordpress package wishing to opt-in to accessing or exposing
|
|
51
|
+
* private private APIs.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} consent The consent string.
|
|
54
|
+
* @param {string} moduleName The name of the module that is opting in.
|
|
55
|
+
* @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (consent, moduleName) => {
|
|
60
|
+
if (!CORE_MODULES_USING_PRIVATE_APIS.includes(moduleName)) {
|
|
61
|
+
throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}". ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!allowReRegistration && registeredPrivateApis.includes(moduleName)) {
|
|
65
|
+
// This check doesn't play well with Story Books / Hot Module Reloading
|
|
66
|
+
// and isn't included in the Gutenberg plugin. It only matters in the
|
|
67
|
+
// WordPress core release.
|
|
68
|
+
throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}" which is already registered. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (consent !== requiredConsent) {
|
|
72
|
+
throw new Error(`You tried to opt-in to unstable APIs without confirming you know the consequences. ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on the next WordPress release.');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
registeredPrivateApis.push(moduleName);
|
|
76
|
+
return {
|
|
77
|
+
lock,
|
|
78
|
+
unlock
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Binds private data to an object.
|
|
83
|
+
* It does not alter the passed object in any way, only
|
|
84
|
+
* registers it in an internal map of private data.
|
|
85
|
+
*
|
|
86
|
+
* The private data can't be accessed by any other means
|
|
87
|
+
* than the `unlock` function.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```js
|
|
91
|
+
* const object = {};
|
|
92
|
+
* const privateData = { a: 1 };
|
|
93
|
+
* lock( object, privateData );
|
|
94
|
+
*
|
|
95
|
+
* object
|
|
96
|
+
* // {}
|
|
97
|
+
*
|
|
98
|
+
* unlock( object );
|
|
99
|
+
* // { a: 1 }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @param {any} object The object to bind the private data to.
|
|
103
|
+
* @param {any} privateData The private data to bind to the object.
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
function lock(object, privateData) {
|
|
107
|
+
if (!object) {
|
|
108
|
+
throw new Error('Cannot lock an undefined object.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!(__private in object)) {
|
|
112
|
+
object[__private] = {};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
lockedData.set(object[__private], privateData);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Unlocks the private data bound to an object.
|
|
119
|
+
*
|
|
120
|
+
* It does not alter the passed object in any way, only
|
|
121
|
+
* returns the private data paired with it using the `lock()`
|
|
122
|
+
* function.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```js
|
|
126
|
+
* const object = {};
|
|
127
|
+
* const privateData = { a: 1 };
|
|
128
|
+
* lock( object, privateData );
|
|
129
|
+
*
|
|
130
|
+
* object
|
|
131
|
+
* // {}
|
|
132
|
+
*
|
|
133
|
+
* unlock( object );
|
|
134
|
+
* // { a: 1 }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @param {any} object The object to unlock the private data from.
|
|
138
|
+
* @return {any} The private data bound to the object.
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
function unlock(object) {
|
|
143
|
+
if (!object) {
|
|
144
|
+
throw new Error('Cannot unlock an undefined object.');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!(__private in object)) {
|
|
148
|
+
throw new Error('Cannot unlock an object that was not locked before. ');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return lockedData.get(object[__private]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const lockedData = new WeakMap();
|
|
155
|
+
/**
|
|
156
|
+
* Used by lock() and unlock() to uniquely identify the private data
|
|
157
|
+
* related to a containing object.
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
const __private = Symbol('Private API ID'); // Unit tests utilities:
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Private function to allow the unit tests to allow
|
|
164
|
+
* a mock module to access the private APIs.
|
|
165
|
+
*
|
|
166
|
+
* @param {string} name The name of the module.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
export function allowCoreModule(name) {
|
|
171
|
+
CORE_MODULES_USING_PRIVATE_APIS.push(name);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Private function to allow the unit tests to set
|
|
175
|
+
* a custom list of allowed modules.
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
export function resetAllowedCoreModules() {
|
|
179
|
+
while (CORE_MODULES_USING_PRIVATE_APIS.length) {
|
|
180
|
+
CORE_MODULES_USING_PRIVATE_APIS.pop();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Private function to allow the unit tests to reset
|
|
185
|
+
* the list of registered private apis.
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
export function resetRegisteredPrivateApis() {
|
|
189
|
+
while (registeredPrivateApis.length) {
|
|
190
|
+
registeredPrivateApis.pop();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=implementation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/private-apis/src/implementation.js"],"names":["CORE_MODULES_USING_PRIVATE_APIS","registeredPrivateApis","requiredConsent","allowReRegistration","process","env","ALLOW_EXPERIMENT_REREGISTRATION","error","__dangerousOptInToUnstableAPIsOnlyForCoreModules","consent","moduleName","includes","Error","push","lock","unlock","object","privateData","__private","lockedData","set","get","WeakMap","Symbol","allowCoreModule","name","resetAllowedCoreModules","length","pop","resetRegisteredPrivateApis"],"mappings":"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","sourcesContent":["/**\n * wordpress/private-apis – the utilities to enable private cross-package\n * exports of private APIs.\n *\n * This \"implementation.js\" file is needed for the sake of the unit tests. It\n * exports more than the public API of the package to aid in testing.\n */\n\n/**\n * The list of core modules allowed to opt-in to the private APIs.\n */\nconst CORE_MODULES_USING_PRIVATE_APIS = [\n\t'@wordpress/block-editor',\n\t'@wordpress/block-library',\n\t'@wordpress/blocks',\n\t'@wordpress/components',\n\t'@wordpress/customize-widgets',\n\t'@wordpress/data',\n\t'@wordpress/edit-post',\n\t'@wordpress/edit-site',\n\t'@wordpress/edit-widgets',\n\t'@wordpress/editor',\n];\n\n/**\n * A list of core modules that already opted-in to\n * the privateApis package.\n *\n * @type {string[]}\n */\nconst registeredPrivateApis = [];\n\n/*\n * Warning for theme and plugin developers.\n *\n * The use of private developer APIs is intended for use by WordPress Core\n * and the Gutenberg plugin exclusively.\n *\n * Dangerously opting in to using these APIs is NOT RECOMMENDED. Furthermore,\n * the WordPress Core philosophy to strive to maintain backward compatibility\n * for third-party developers DOES NOT APPLY to private APIs.\n *\n * THE CONSENT STRING FOR OPTING IN TO THESE APIS MAY CHANGE AT ANY TIME AND\n * WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A\n * CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.\n */\nconst requiredConsent =\n\t'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.';\n\n/** @type {boolean} */\nlet allowReRegistration;\n// Use try/catch to force \"false\" if the environment variable is not explicitly\n// set to true (e.g. when building WordPress core).\ntry {\n\tallowReRegistration = process.env.ALLOW_EXPERIMENT_REREGISTRATION ?? false;\n} catch ( error ) {\n\tallowReRegistration = false;\n}\n\n/**\n * Called by a @wordpress package wishing to opt-in to accessing or exposing\n * private private APIs.\n *\n * @param {string} consent The consent string.\n * @param {string} moduleName The name of the module that is opting in.\n * @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.\n */\nexport const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (\n\tconsent,\n\tmoduleName\n) => {\n\tif ( ! CORE_MODULES_USING_PRIVATE_APIS.includes( moduleName ) ) {\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs as module \"${ moduleName }\". ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will be removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on one of the next WordPress releases.'\n\t\t);\n\t}\n\tif (\n\t\t! allowReRegistration &&\n\t\tregisteredPrivateApis.includes( moduleName )\n\t) {\n\t\t// This check doesn't play well with Story Books / Hot Module Reloading\n\t\t// and isn't included in the Gutenberg plugin. It only matters in the\n\t\t// WordPress core release.\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs as module \"${ moduleName }\" which is already registered. ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will be removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on one of the next WordPress releases.'\n\t\t);\n\t}\n\tif ( consent !== requiredConsent ) {\n\t\tthrow new Error(\n\t\t\t`You tried to opt-in to unstable APIs without confirming you know the consequences. ` +\n\t\t\t\t'This feature is only for JavaScript modules shipped with WordPress core. ' +\n\t\t\t\t'Please do not use it in plugins and themes as the unstable APIs will removed ' +\n\t\t\t\t'without a warning. If you ignore this error and depend on unstable features, ' +\n\t\t\t\t'your product will inevitably break on the next WordPress release.'\n\t\t);\n\t}\n\tregisteredPrivateApis.push( moduleName );\n\n\treturn {\n\t\tlock,\n\t\tunlock,\n\t};\n};\n\n/**\n * Binds private data to an object.\n * It does not alter the passed object in any way, only\n * registers it in an internal map of private data.\n *\n * The private data can't be accessed by any other means\n * than the `unlock` function.\n *\n * @example\n * ```js\n * const object = {};\n * const privateData = { a: 1 };\n * lock( object, privateData );\n *\n * object\n * // {}\n *\n * unlock( object );\n * // { a: 1 }\n * ```\n *\n * @param {any} object The object to bind the private data to.\n * @param {any} privateData The private data to bind to the object.\n */\nfunction lock( object, privateData ) {\n\tif ( ! object ) {\n\t\tthrow new Error( 'Cannot lock an undefined object.' );\n\t}\n\tif ( ! ( __private in object ) ) {\n\t\tobject[ __private ] = {};\n\t}\n\tlockedData.set( object[ __private ], privateData );\n}\n\n/**\n * Unlocks the private data bound to an object.\n *\n * It does not alter the passed object in any way, only\n * returns the private data paired with it using the `lock()`\n * function.\n *\n * @example\n * ```js\n * const object = {};\n * const privateData = { a: 1 };\n * lock( object, privateData );\n *\n * object\n * // {}\n *\n * unlock( object );\n * // { a: 1 }\n * ```\n *\n * @param {any} object The object to unlock the private data from.\n * @return {any} The private data bound to the object.\n */\nfunction unlock( object ) {\n\tif ( ! object ) {\n\t\tthrow new Error( 'Cannot unlock an undefined object.' );\n\t}\n\tif ( ! ( __private in object ) ) {\n\t\tthrow new Error(\n\t\t\t'Cannot unlock an object that was not locked before. '\n\t\t);\n\t}\n\n\treturn lockedData.get( object[ __private ] );\n}\n\nconst lockedData = new WeakMap();\n\n/**\n * Used by lock() and unlock() to uniquely identify the private data\n * related to a containing object.\n */\nconst __private = Symbol( 'Private API ID' );\n\n// Unit tests utilities:\n\n/**\n * Private function to allow the unit tests to allow\n * a mock module to access the private APIs.\n *\n * @param {string} name The name of the module.\n */\nexport function allowCoreModule( name ) {\n\tCORE_MODULES_USING_PRIVATE_APIS.push( name );\n}\n\n/**\n * Private function to allow the unit tests to set\n * a custom list of allowed modules.\n */\nexport function resetAllowedCoreModules() {\n\twhile ( CORE_MODULES_USING_PRIVATE_APIS.length ) {\n\t\tCORE_MODULES_USING_PRIVATE_APIS.pop();\n\t}\n}\n/**\n * Private function to allow the unit tests to reset\n * the list of registered private apis.\n */\nexport function resetRegisteredPrivateApis() {\n\twhile ( registeredPrivateApis.length ) {\n\t\tregisteredPrivateApis.pop();\n\t}\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/private-apis/src/index.js"],"names":["__dangerousOptInToUnstableAPIsOnlyForCoreModules"],"mappings":"AAAA,SAASA,gDAAT,QAAiE,kBAAjE","sourcesContent":["export { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from './implementation';\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private function to allow the unit tests to allow
|
|
3
|
+
* a mock module to access the private APIs.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} name The name of the module.
|
|
6
|
+
*/
|
|
7
|
+
export function allowCoreModule(name: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Private function to allow the unit tests to set
|
|
10
|
+
* a custom list of allowed modules.
|
|
11
|
+
*/
|
|
12
|
+
export function resetAllowedCoreModules(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Private function to allow the unit tests to reset
|
|
15
|
+
* the list of registered private apis.
|
|
16
|
+
*/
|
|
17
|
+
export function resetRegisteredPrivateApis(): void;
|
|
18
|
+
export function __dangerousOptInToUnstableAPIsOnlyForCoreModules(consent: string, moduleName: string): {
|
|
19
|
+
lock: typeof lock;
|
|
20
|
+
unlock: typeof unlock;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Binds private data to an object.
|
|
24
|
+
* It does not alter the passed object in any way, only
|
|
25
|
+
* registers it in an internal map of private data.
|
|
26
|
+
*
|
|
27
|
+
* The private data can't be accessed by any other means
|
|
28
|
+
* than the `unlock` function.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```js
|
|
32
|
+
* const object = {};
|
|
33
|
+
* const privateData = { a: 1 };
|
|
34
|
+
* lock( object, privateData );
|
|
35
|
+
*
|
|
36
|
+
* object
|
|
37
|
+
* // {}
|
|
38
|
+
*
|
|
39
|
+
* unlock( object );
|
|
40
|
+
* // { a: 1 }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param {any} object The object to bind the private data to.
|
|
44
|
+
* @param {any} privateData The private data to bind to the object.
|
|
45
|
+
*/
|
|
46
|
+
declare function lock(object: any, privateData: any): void;
|
|
47
|
+
/**
|
|
48
|
+
* Unlocks the private data bound to an object.
|
|
49
|
+
*
|
|
50
|
+
* It does not alter the passed object in any way, only
|
|
51
|
+
* returns the private data paired with it using the `lock()`
|
|
52
|
+
* function.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```js
|
|
56
|
+
* const object = {};
|
|
57
|
+
* const privateData = { a: 1 };
|
|
58
|
+
* lock( object, privateData );
|
|
59
|
+
*
|
|
60
|
+
* object
|
|
61
|
+
* // {}
|
|
62
|
+
*
|
|
63
|
+
* unlock( object );
|
|
64
|
+
* // { a: 1 }
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @param {any} object The object to unlock the private data from.
|
|
68
|
+
* @return {any} The private data bound to the object.
|
|
69
|
+
*/
|
|
70
|
+
declare function unlock(object: any): any;
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=implementation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"implementation.d.ts","sourceRoot":"","sources":["../src/implementation.js"],"names":[],"mappings":"AAgMA;;;;;GAKG;AACH,sCAFW,MAAM,QAIhB;AAED;;;GAGG;AACH,gDAIC;AACD;;;GAGG;AACH,mDAIC;AAxJM,0EAJI,MAAM,cACN,MAAM,GACL;IAAC,MAAM,WAAW,CAAC;IAAC,QAAQ,aAAa,CAAA;CAAC,CA6CrD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,8BAHW,GAAG,eACH,GAAG,QAUb;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,gCAHW,GAAG,GACF,GAAG,CAad"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
|