Skip to content

Commit

Permalink
Merge pull request #65 from GoogleChrome/misc
Browse files Browse the repository at this point in the history
misc fixes
  • Loading branch information
samthor authored Feb 21, 2020
2 parents 5808aa0 + 9aa05b3 commit 8ad3f28
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
48 changes: 17 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is a polyfill for the `Proxy` object, part of ES6.
See the [MDN docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) or [Introducing ES2015 Proxies](https://developers.google.com/web/updates/2016/02/es2015-proxies) for more information on `Proxy` itself.
Unlike other polyfills, this does not require `Object.observe`, [which is deprecated](https://www.google.com/search?q=object.observe+deprecated).
Unlike other polyfills, this does not require `Object.observe`, [which is no longer supported anywhere](https://www.google.com/search?q=object.observe+deprecated).

The polyfill supports just a limited number of proxy 'traps'.
It also works by calling [seal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal) on the object passed to `Proxy`.
Expand Down Expand Up @@ -69,10 +69,10 @@ function observe(o, callback) {
return buildProxy('', o);
}

const x = {'model': {name: 'Falcon'}};
const x = {'model': {name: 'LEAF'}};
const p = observe(x, (property, value) => console.info(property, value));
p.model.name = 'Commodore';
// model.name Commodore
p.model.name = 'Tesla';
// model.name Tesla
```

## Adding new properties
Expand All @@ -94,48 +94,34 @@ For a similar reason, this polyfill can't proxy `Array` objects very well - but

# Usage

## To assign Proxy to the global object
Install via your favourite package manager as `proxy-polyfill`.

Include the JavaScript at the start of your page, as an ES6 module (although browsers that support ES6 modules support `Proxy` natively) or include it as a dependency to your build steps.
The source is in ES6, but the included, minified version is ES5.
## To polyfill Proxy everywhere

## To consume Proxy as a function
You should include `proxy-polyfill` into your build system (just require it directly, it doesn't export anything), or import the `proxy.min.js` file directly.
This is the recommended approach and works on the web, in Node, or React Native.

Require from your app the file `./src/proxy.js`, which exports a proxy polyfill functionvian commonJS.
## To consume the polyfill as a function

Requires `./src/proxy.js`, which exports a proxy polyfill _builder_ function in commonJS.

```js
// commonJS require
const ProxyPolyfill = require('proxy-polyfill/src/proxy');
const proxyPolyfill = require('proxy-polyfill/src/proxy')();

// Your environment may also support transparent rewriting of commonJS to ES6:
import ProxyPolyfill from 'proxy-polyfill/src/proxy';
import ProxyPolyfillBuilder from 'proxy-polyfill/src/proxy';
const proxyPolyfill = ProxyPolyfillBuilder();

// Then use...
const myProxy = new ProxyPolyfill(...);
```

## Installation

Available via NPM or Bower-

```bash
$ npm install proxy-polyfill
$ bower install proxy-polyfill
const myProxy = new proxyPolyfill(...);
```

If this is imported as a Node module, it will polyfill the global namespace rather than returning the `Proxy` object.
If you'd like to just get the polyfill'ed version, use the require statement as above.

## Supports
# Support

The polyfill supports browsers that implement the full [ES5 spec](http://kangax.github.io/compat-table/es5/), such as IE9+ and Safari 6+.
It may work in other non-browser environments too.

Note that Firefox, Chrome, Safari 10+ and Edge support `Proxy` natively.
You don't need this if you're only targeting these modern browsers.

# Release

Run
```bash
$ npm run build
```
3 changes: 1 addition & 2 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ const closureCompiler = new ClosureCompiler({
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5',
compilation_level: 'ADVANCED_OPTIMIZATIONS',
// dependency_mode: 'STRICT',
warning_level: 'VERBOSE',
process_common_js_modules: true,
output_wrapper: '(function(){%output%})()', // this prevents closure compiler from polluting the global scope
output_wrapper: '(function(){%output%})();', // don't pollute global scope
});

const compilerProcess = closureCompiler.run((code, stdout, stderr) => {
Expand Down
8 changes: 4 additions & 4 deletions proxy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ module.exports = function proxyPolyfill() {
// to call itself, but that seems unlikely especially when using the polyfill.
let throwRevoked = function() {};
lastRevokeFn = function() {
/** @suppress {checkTypes} */
target = null; // clear ref
throwRevoked = function(trap) {
throw new TypeError(`Cannot perform '${trap}' on a proxy that has been revoked`);
};
};
setTimeout(function() {
lastRevokeFn = null;
}, 0);

// Fail on unsupported traps: Chrome doesn't do this, but ensure that users of the polyfill
// are a bit more careful. Copy the internal parts of handler to prevent user changes.
Expand Down

0 comments on commit 8ad3f28

Please sign in to comment.