-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add support for base64 encoded buffer properties #22
Conversation
@@ -1,18 +1,6 @@ | |||
import ejs from 'ejs' | |||
import { helpers, getContext, Property, Parameter } from "@dylibso/xtp-bindgen" | |||
|
|||
function needsCasting(p: Property | Parameter): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this ended up being redundant logic and i think i wrote it when i was experiencing a little brain fog
@@ -11,7 +11,9 @@ import { | |||
export function <%- ex.name %>(): number { | |||
<% if (ex.input) { -%> | |||
<% if (isJsonEncoded(ex.input)) { -%> | |||
<% if (isPrimitive(ex.input)) { -%> | |||
<% if (isBuffer(ex.input)) { -%> | |||
const input: <%- toTypeScriptType(ex.input) %> = Host.base64ToArrayBuffer(JSON.parse(Host.inputString())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on if you think it'd be confusing for folks to get the ArrayBuffer
handle instead of a typed array view, we may want to wrap the result of base64ToArrayBuffer
in a Uint8Array
.
const input: <%- toTypeScriptType(ex.input) %> = Host.base64ToArrayBuffer(JSON.parse(Host.inputString())) | |
const input: <%- toTypeScriptType(ex.input) %> = new Uint8Array(Host.base64ToArrayBuffer(JSON.parse(Host.inputString()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, i'm not sure. input
here needs to be ArrayBufferLike. wouldn't that violate it? it should be the same type as Host.inputBytes()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, both Uint8Array
and ArrayBuffer
are ArrayBufferLike
because they both match the {byteLength: number, slice(begin: number, end: number): ArrayBuffer}
type.
(A useful litmus test would be whether we think users expect input[0]
to return a byte or not)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's follow back up on this discussion. it feels like a broader change than just this PR. I'll file an issue.
@@ -38,7 +40,9 @@ export function <%- ex.name %>(): number { | |||
|
|||
<% if (ex.output) { -%> | |||
<% if (isJsonEncoded(ex.output)) { -%> | |||
<% if (isPrimitive(ex.output)) { -%> | |||
<% if (isBuffer(ex.output)) { -%> | |||
Host.outputString(JSON.stringify(Host.arrayBufferToBase64(output))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here – we can unwrap views:
Host.outputString(JSON.stringify(Host.arrayBufferToBase64(output))) | |
Host.outputString(JSON.stringify(Host.arrayBufferToBase64(ArrayBuffer.isView(output) ? output.buffer : output))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Actually, I take this back – the view has a .byteOffset
that we'd need to respect by slicing the buffer!)
Base64 encodes/decodes json properties that are buffers at the transport layer. Translates at both the property layer and the top level