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

Add support for base64 encoded buffer properties #22

Merged
merged 4 commits into from
Sep 13, 2024
Merged

Conversation

bhelx
Copy link
Contributor

@bhelx bhelx commented Sep 12, 2024

Base64 encodes/decodes json properties that are buffers at the transport layer. Translates at both the property layer and the top level

@bhelx bhelx marked this pull request as ready for review September 12, 2024 18:02
@@ -1,18 +1,6 @@
import ejs from 'ejs'
import { helpers, getContext, Property, Parameter } from "@dylibso/xtp-bindgen"

function needsCasting(p: Property | Parameter): boolean {
Copy link
Contributor Author

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()))

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.

Suggested change
const input: <%- toTypeScriptType(ex.input) %> = Host.base64ToArrayBuffer(JSON.parse(Host.inputString()))
const input: <%- toTypeScriptType(ex.input) %> = new Uint8Array(Host.base64ToArrayBuffer(JSON.parse(Host.inputString())))

Copy link
Contributor Author

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()

Copy link

@chrisdickinson chrisdickinson Sep 12, 2024

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)

Copy link
Contributor Author

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)))

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:

Suggested change
Host.outputString(JSON.stringify(Host.arrayBufferToBase64(output)))
Host.outputString(JSON.stringify(Host.arrayBufferToBase64(ArrayBuffer.isView(output) ? output.buffer : output)))

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!)

@bhelx bhelx merged commit eb81879 into main Sep 13, 2024
3 of 4 checks passed
@bhelx bhelx deleted the base64-buffers branch September 13, 2024 15:08
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