Skip to content

Encapsulation doesn't work when property throws an error #29

Closed

Description

@regseb

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

Activity

  1. brettz9 commented on Apr 30, 2022

    @brettz9
    Collaborator

    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.

  2. brettz9 commented on Jul 31, 2026

    @brettz9
    Collaborator

    If you are still interested, please have a look at #57 to see if that meets your needs...

  3. added 2 commits that reference this issue on Aug 1, 2026
    37a210d
    11b6ee2
  4. brettz9 commented on Aug 6, 2026

    @brettz9
    Collaborator

    Released in v10.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions