Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
regseb opened this issue Apr 30, 2022 · 1 comment
Open

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

regseb opened this issue Apr 30, 2022 · 1 comment

Comments

@regseb
Copy link

regseb commented Apr 30, 2022

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/[email protected]/dist/typeson.esm.js";
        new Typeson().encapsulate(Object.getPrototypeOf(navigator));
    </script>
    <script type="module">
        import { Typeson } from "https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/typeson.esm.js:1142
    _adaptBuiltinStateObjectProperties https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:957
    _encapsulate                       https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:1141
    _encapsulate                       https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:1135
    encapsulate                        https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/typeson.esm.js:1142
    _adaptBuiltinStateObjectProperties https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:957
    _encapsulate                       https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:1141
    _encapsulate                       https://cdn.jsdelivr.net/npm/[email protected]/dist/typeson.esm.js:1135
    encapsulate                        https://cdn.jsdelivr.net/npm/[email protected]/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
@brettz9
Copy link
Collaborator

brettz9 commented 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.

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

No branches or pull requests

2 participants