Sub topic 11
Revocable Proxies
This chapter assumes you can create a proxy from a target and a handler, write basic traps, and forward operations with Reflect. Here you will learn how to make a proxy that can be switched off for good.
Why Revocation Exists
When you pass an object to other code, that code keeps its reference for as long as it likes. You cannot take the reference back. A revocable proxy solves this. You hand out the proxy instead of the real object. Later, you call a function that cuts the link between the proxy and its target. From then on, any code holding the proxy can no longer reach the target through it.
This is useful when:
- A plugin or third-party script should only have access during a setup phase.
- A callback should only be able to touch an object while a single request is being handled.
- You want to make sure code that runs late fails loudly instead of quietly reading stale data.
Creating a Revocable Proxy
Proxy.revocable(target, handler) takes the same arguments as new Proxy. It returns an object with two properties:
| Property | What it is |
|---|---|
| `proxy` | A normal proxy that behaves exactly like one made with `new Proxy(target, handler)` |
| `revoke` | A function with no arguments that permanently disables `proxy` |
const { proxy, revoke } = Proxy.revocable({ a: 1 }, {});
console.log(proxy.a); // 1
revoke();
try {
proxy.a;
} catch (err) {
console.log(err instanceof TypeError); // true
console.log(err.message); // e.g. "Cannot perform 'get' on a proxy that has been revoked"
}The handler here is empty, so every operation falls through to the target until revoke() runs.
What Revocation Does Internally
A proxy holds two internal references: one to its target and one to its handler. Calling revoke sets both to null. Every internal method on a proxy, such as [[Get]], [[Set]], [[OwnPropertyKeys]], or [[Call]], first checks whether the handler is null. If it is, the method throws a TypeError before any trap runs.
The design has these consequences:
- Revocation is permanent. Nothing can restore the link.
- Calling
revoketwice is safe. Later calls do nothing. - The target can be garbage collected. The revoked proxy no longer references the target. If nothing else does, the engine can free it.
- The target itself is untouched. Code holding a direct reference to the target keeps working.
A Worked Example: Scoped Access
The pattern below gives a callback access to a config object only while the callback runs. If the callback saves the proxy and uses it later, that later use throws.
function withTemporaryAccess(target, callback) {
const { proxy, revoke } = Proxy.revocable(target, {
set(t, prop, value, receiver) {
console.log(`write ${String(prop)}`);
return Reflect.set(t, prop, value, receiver);
}
});
try {
return callback(proxy);
} finally {
revoke();
}
}
const config = { mode: 'dev' };
let leaked;
withTemporaryAccess(config, (cfg) => {
cfg.mode = 'prod'; // logs "write mode"
leaked = cfg; // the callback keeps a reference
});
console.log(config.mode); // 'prod'
try {
leaked.mode = 'test';
} catch (err) {
console.log('access withdrawn:', err instanceof TypeError); // access withdrawn: true
}The finally block makes sure the proxy is revoked even if the callback throws. Revocable proxies combine with any traps, so this one also logs writes.
Common Mistakes
- Handing out the target by accident. If a trap returns the target or a nested object directly, the caller now has an unrevocable reference. Revocation only protects what is reached through the proxy. Libraries that need deep protection wrap returned objects in their own revocable proxies, a design known as a membrane.
- Forgetting to call
revoke. If nothing callsrevoke, the revocable proxy behaves like an ordinary proxy and access is never withdrawn. Tie revocation to a clear event, such as afinallyblock, a timeout, or a teardown hook. - Expecting
typeofto detect revocation. It does not. The only check is to attempt an operation and catch theTypeError.
Exercise
You run const { proxy, revoke } = Proxy.revocable(function f() {}, {}); revoke();. What do typeof proxy and proxy() each produce?
Show answer
typeof proxy returns "function", because typeof does not call any internal method on the proxy. proxy() throws a TypeError, because [[Call]] checks the handler, finds it set to null by revoke, and fails before reaching the target.
Exercise
In withTemporaryAccess, suppose the config had a nested object config.db = { host: 'x' } and the callback stored cfg.db in a variable. Could it still modify db.host after revocation? Why?
Show answer
Yes. The get operation returned the real nested object, not a proxy, so the callback holds a direct reference that revocation does not affect. To prevent this, the handler's get trap would have to wrap object values in their own revocable proxies and revoke them together.