Proxy.revocable Enforces Temporary Access
Issue temporary object handles with Proxy.revocable and call revoke() when a task ends so subsequent property reads, writes, and in checks throw TypeError.
Code
function issueConfigLease(target) {
let reads = 0;
const { proxy, revoke } = Proxy.revocable(target, {
get(obj, prop, receiver) {
reads++;
return Reflect.get(obj, prop, receiver);
},
set() {
throw new TypeError('leased config is read-only');
},
});
return { proxy, revoke, getReads: () => reads };
}
const backing = { host: 'db.internal', port: 5432 };
const lease = issueConfigLease(backing);
console.log('active read:', lease.proxy.host, lease.proxy.port, 'reads:', lease.getReads());
lease.revoke();
// Once revoked, every trap (get, set, has) fails immediately with TypeError
for (const action of [
() => lease.proxy.host,
() => ('host' in lease.proxy),
]) {
try {
action();
} catch (err) {
console.log('after revoke:', err.name, err.message);
}
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
active read: db.internal 5432 reads: 2
after revoke: TypeError Cannot perform 'get' on a proxy that has been revoked
after revoke: TypeError Cannot perform 'has' on a proxy that has been revokedInside the implementation — Proxy.revocable Enforces Temporary Access
Proxy.revocable Enforces Temporary Access comes down to the function issueConfigLease.
A single loop drives the file — for (const action of [. A TypeError is thrown unconditionally at its throw site — no input check sits in front of it.
The result comes back as an object carrying proxy, revoke.
What to watch for
- Ready as-is on a bare install of the language.
Applying it
Signature: function issueConfigLease(target)
The run prints active read:, after revoke: — each prefix names the step that produced it.
The call returns proxy, revoke — destructure what you need at the call site.
Statements that matter
return { proxy, revoke, getReads: () => reads };
throw new TypeError('leased config is read-only');
return Reflect.get(obj, prop, receiver);
Interactive Tools for This Topic
FAQ
- Does `issueConfigLease` modify its input?
- No — a new value is constructed for the result and the arguments are never written to.
- Does the snippet validate its input?
- No. There is no input validation in the snippet — the `TypeError` is raised unconditionally at its site, with no check in front of it.
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `issueConfigLease` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.