miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

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

Open mrbbot opened this issue 3 years ago • 3 comments

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

This is because the undefineds would make it to the encodeCapnpStruct function: https://github.com/cloudflare/miniflare/blob/b0e1d06d32e1f6ff8f809d3b8180e5f17494732a/packages/tre/src/plugins/core/index.ts#L172-L173

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 avatar Sep 14 '22 14:09 mrbbot

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 avatar Sep 15 '22 10:09 mrbbot

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?

penalosa avatar Sep 15 '22 12:09 penalosa

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