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

"Fix" capnproto serialisation of undefined values, closes #371 #377

Merged
merged 1 commit into from
Sep 23, 2022

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Sep 14, 2022

Previously passing compatibilityDate: undefined or compatibilityFlags: undefined would throw.

This is because the undefineds would make it to the encodeCapnpStruct function:

compatibilityDate: options.compatibilityDate,
compatibilityFlags: options.compatibilityFlags,

encodeCapnpStruct distinguishes between { compatibilityDate: undefined } and {}. Calling tre/src/runtime/config/sserve-conf.capnp.js/Worker#setCompatibilityDate(value) with an undefined value throws.

The problem is that undefined may be needed in some cases to signal that a function should be called, e.g. sserve-conf.capnp.js/Worker_Binding_Type#setR2Bucket().

This "fix" checks if the function actually accepts a value before calling it. Because we don't have runtime type information, this is done by looking at the function's source code dynamically. 🤮

Writing out this description has given me an idea for an alternative. We could replace the undefineds in sserve-conf.ts/Worker_Binding_Type with a special Symbol to signal that we wanted to call the function with no arguments. Then just ignore all undefined values in encodeCapnpStruct? What do you think?

@mrbbot
Copy link
Contributor Author

mrbbot commented Sep 15, 2022

Rethinking this today, I not sure I want to ship this fix at the moment. Moving this PR to draft until I've got a better solution. 👍

@mrbbot mrbbot marked this pull request as draft September 15, 2022 10:03
@penalosa
Copy link
Contributor

penalosa commented Sep 15, 2022

Rethinking this today, I not sure I want to ship this fix at the moment. Moving this PR to draft until I've got a better solution. 👍

There's definitely something about parsing JS strings that makes me uneasy... Is there any specific reason we don't have runtime type information? It looks like function encodeCapnpStruct(obj: any, struct: Struct) types obj as any, but it's passed as config: Config? Why can't that type information be used?

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Sep 20, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0b673f0
Status: ✅  Deploy successful!
Preview URL: https://621f6e76.cf-miniflare.pages.dev
Branch Preview URL: https://tre-capnp-undefined.cf-miniflare.pages.dev

View logs

@mrbbot mrbbot marked this pull request as ready for review September 20, 2022 09:27
Copy link
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! My one concern is the typing of obj: any—is there any reason that's not able to be fully typed?

@penalosa penalosa merged commit a7417a7 into tre Sep 23, 2022
@mrbbot mrbbot deleted the tre-capnp-undefined branch September 28, 2022 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants