Featureadvanced
Revocable proxies
Also called Proxy.revocable, revoke, revoked proxy, membrane, membranes, object capability, revocable reference.
Proxy.revocable(target, handler) returns a proxy and a revoke function. After revoke(), each operation on the proxy throws a TypeError. A membrane wraps each object that comes out of it.
- Language
- JavaScript
Synopsis
const { proxy, revoke } = Proxy.revocable({ secret: 42 }, {});
console.log(proxy.secret);
revoke();
try {
proxy.secret;
} catch (e) {
console.log(e.constructor.name, e.message);
}
console.log(typeof proxy);
revoke();
42
TypeError Cannot perform 'get' on a proxy that has been revoked
object
The proxy works until revoke() runs. After that, the read throws. typeof still works, because it does not call a trap, and a second call of revoke does nothing.
const { proxy, revoke } = Proxy.revocable(target, handler);
Semantics
r1. "The Proxy.revocable() factory function is the same as the Proxy() constructor, except that in addition to creating a proxy object, it also creates a revoke function that can be called to disable the proxy. The proxy object and the revoke function are wrapped in a plain object."1 The specification makes this object with two properties, "proxy" and "revoke".2
r2. Revoking sets the two slots of the proxy to null. "When a proxy is revoked, its [[ProxyHandler]] and [[ProxyTarget]] internal slots are set to null causing subsequent invocations of internal methods on that Proxy object to throw a TypeError exception."3 Each internal method starts with ValidateNonRevokedProxy, which "throws a TypeError exception if proxy has been revoked."3 Example: revoke.
r3. "Once a proxy is revoked, it remains revoked, and calling revoke() again has no effect".1 The revoke function "does not take any parameters, nor does it rely on the this value."1 So the code can pass revoke as a callback, as in lease.
r4. Revoking lets the target go. "Letting users interact with an object through a revocable proxy allows you to control the lifetime of the object exposed to the user".1 MDN adds that the object can be garbage-collected while the user still holds its proxy.1 The revoked proxy no longer refers to the target (rule r2), so the garbage collector can take the target when no other code refers to it.
r5. Some operations do not use an internal method, so they work on a revoked proxy. MDN notes that typeof "doesn't trigger any trap".4 Example: revoke.
r6. Revoking cuts off only the proxy itself. An object that the code read through the proxy before the revoke is the real object, and it stays usable. A membrane fixes this: it wraps each object that leaves the proxy in a new revocable proxy, and it revokes all of them together. Examples: leak, membrane.
Examples
function lend(resource, ms) {
const { proxy, revoke } = Proxy.revocable(resource, {});
setTimeout(revoke, ms);
return proxy;
}
const db = { query: (q) => `rows for ${q}` };
const lease = lend(db, 10);
console.log(lease.query("a"));
setTimeout(() => {
try {
lease.query("b");
} catch (e) {
console.log("revoked:", e.constructor.name);
}
}, 30);
rows for a
revoked: TypeError
The code gives access to db for 10 milliseconds. The timer calls revoke with no this and no arguments (rule r3).
function membrane(root) {
const revokes = [];
const wrap = (value) => {
if (value === null || (typeof value !== "object" && typeof value !== "function")) {
return value;
}
const { proxy, revoke } = Proxy.revocable(value, {
get: (t, k) => wrap(Reflect.get(t, k)),
apply: (t, self, args) => wrap(Reflect.apply(t, self, args)),
});
revokes.push(revoke);
return proxy;
};
return { root: wrap(root), revokeAll: () => revokes.forEach((r) => r()) };
}
const app = { config: { port: 8080 }, getUser: () => ({ name: "Ana" }) };
const { root, revokeAll } = membrane(app);
const config = root.config;
const user = root.getUser();
console.log(config.port, user.name);
revokeAll();
for (const p of [root, config, user]) {
try {
p.x;
} catch (e) {
console.log("blocked", e.constructor.name);
}
}
8080 Ana
blocked TypeError
blocked TypeError
blocked TypeError
Each object that the code gets from root, by a read or by a call, is a revocable proxy too. One call of revokeAll cuts off root, config and user (rule r6). A full membrane also wraps the arguments that go in and keeps one proxy for each object, but the idea is the same.
Versions
ECMAScript 2015, the 6th edition, added the function as "26.2.2.1 Proxy.revocable ( target, handler )".5 The 17th edition (ECMAScript 2026) has it as 28.2.2.1.2 Node v24 runs all the examples on this page.
Pitfalls
Objects read before the revoke
This code runs but gives a wrong answer on purpose.
const app = { config: { port: 8080 } };
const { proxy, revoke } = Proxy.revocable(app, {});
const config = proxy.config;
revoke();
console.log(config.port, config === app.config);
8080 true
proxy.config gave the real object, so the revoke does not stop the code that holds it (rule r6). Use a membrane, as in membrane, when the code must lose access to all the objects.
Comparison
A revocable proxy is a reference that its owner can take back. In Java or Python, a program writes a wrapper class with a flag and tests the flag in each method. In JavaScript, the engine does the test for each of the 13 internal methods (rule r2). For proxies in general, see Proxies.
References
- MDN Web Docs, JavaScript Reference, Proxy.revocable(), the introduction; Return value; Description
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 28, 28.2.2.1 Proxy.revocable ( target, handler )
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 10, 10.5 Proxy Object Internal Methods and Internal Slots; 10.5.14 ValidateNonRevokedProxy
- MDN Web Docs, JavaScript Guide, Meta programming, Revocable Proxy
- ECMAScript 2015 Language Specification (ECMA-262, 6th edition), 26.2.2.1 Proxy.revocable ( target, handler )
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag QH7PC3 Edit this page History Report an error Markdown