- From: Andrea Giammarchi <notifications@github.com>
- Date: Tue, 14 May 2024 02:44:51 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1314@github.com>
### What problem are you trying to solve?
The complexity of cross-realms interactions is very high these days:
* *Workers* and `postMessage` implicitly use *structuredClone* to pass values around
* *Proxy* used in foreign worlds can't survive any `postMessage` dance without explicit user interaction (i.e. use ad-hoc API to transform that proxy into something consumable elsewhere)
* classes defined in a world cannot be cloned in any other world, including the very same realm they'd like to be cloned
### What solutions exist today?
None, but we historically have `toJSON` convention to automatically transform any class into its *JSON* representation. Times are different these days though, and while `structuredClone` works wonderfully for all its allowed types, it's impossible from a library author point of view to grant some data can cross boundaries or be cloned properly, as opposite of throwing errors.
### How would you solve it?
```js
const ref = {
// implicitly invoked when `structuredClone(ref)` happens
// or during the conversion / clone algorithm is applied
[Symbol.clone]() {
// returns any structured-clone compatible value
return new Map(Object.entries(this));
}
};
```
The algorithm should look for non primitive values to a `Symbol.clone` special property that should return the cloned representation of the underlying proxy, class, complex data, and so on.
For this proposal, it would be a developers concern to "*reconstruct*" or understand returned data, simply screening in a recursive way whatever was cloned, simplifying ad-hoc clones for API calls, or cross-proxy related use cases.
### Anything else?
As library author, I could dictate how any reference the library creates could be cloned or even throw if some reference should actually never be cloned (authentication / credentials / passwords / security related things / ... and so on).
This was (wrongly) posted also in *TC39* in case some extra thoughts are needed: https://es.discourse.group/t/symbol-clone-to-ease-out-structuredclone-implicit-conversion/2035
--
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1314
You are receiving this because you are subscribed to this thread.
Message ID: <whatwg/streams/issues/1314@github.com>
Received on Tuesday, 14 May 2024 09:44:55 UTC