Repository navigation
Encapsulation doesn't work when property throws an error #29
Copy link
Copy link
Closed
Labels
Description
Hi,
Encapsulation doesn't work when the object has properties that throw errors. These properties should be ignored.
To reproduce, open this HTML file in browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="module">
import { Typeson } from "https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js";
new Typeson().encapsulate(Object.getPrototypeOf(navigator));
</script>
<script type="module">
import { Typeson } from "https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js";
const foo = {};
Object.defineProperty(foo, "bar", {
enumerable: true,
get() { throw Error("baz"); },
});
console.log(new Typeson().encapsulate(foo));
</script>
</head>
</html>This two errors are logged in console of Firefox:
Uncaught TypeError: 'get permissions' called on an object that does not implement interface Navigator.
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1142
_adaptBuiltinStateObjectProperties https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:957
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1141
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1135
encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:894
<anonymous> file:///home/regseb/testcase/index.html:7
typeson.esm.js:1142:25
Uncaught Error: baz
get file:///home/regseb/testcase/index.html:14
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1142
_adaptBuiltinStateObjectProperties https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:957
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1141
_encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:1135
encapsulate https://cdn.jsdelivr.net/npm/typeson@7.0.1/dist/typeson.esm.js:894
<anonymous> file:///home/regseb/testcase/index.html:16
index.html:14:27
Or these errors in Chromium:
Uncaught TypeError: Illegal invocation
at typeson.esm.js:1142:47
at _adaptBuiltinStateObjectProperties (typeson.esm.js:957:9)
at typeson.esm.js:1141:13
at Array.forEach (<anonymous>)
at _encapsulate (typeson.esm.js:1135:23)
at Typeson.encapsulate (typeson.esm.js:894:17)
at index.html:7:23
Uncaught Error: baz
at Object.get [as bar] (index.html:14:27)
at typeson.esm.js:1142:47
at _adaptBuiltinStateObjectProperties (typeson.esm.js:957:9)
at typeson.esm.js:1141:13
at Array.forEach (<anonymous>)
at _encapsulate (typeson.esm.js:1135:23)
at Typeson.encapsulate (typeson.esm.js:894:17)
at index.html:16:35
Reactions are currently unavailable
Activity
brettz9 commentedon Apr 30, 2022
One key use case of typeson is for structured cloning (as available in
typeson-registry).In structured clonining, Chrome and Firefox will both throw if you call
structuredClone(Object.getPrototypeOf(navigator)).But perhaps we could accept a PR to add configuration to allow ignoring such errors.
brettz9 commentedon Jul 31, 2026
If you are still interested, please have a look at #57 to see if that meets your needs...
feat: add `encapsulateError` option for control of throwing properties;
feat: add `encapsulateError` option for control of throwing properties;
brettz9 commentedon Aug 6, 2026
Released in v10.1.0