Skip to content

Conversation

@dom96
Copy link
Contributor

@dom96 dom96 commented Nov 18, 2025

Deployed URL: https://python-image-gen.runtime-playground.workers.dev

Screenshot 2025-11-18 at 17 42 25

Mostly generated using GenAI.

CC @ryanking13

Comment on lines +333 to +331
image_bytes = buffer.getvalue()

# Create and return response with appropriate headers
return Response(
to_js(image_bytes).buffer,
headers={
"Content-Type": content_type,
"Cache-Control": "public, max-age=3600",
},
)
Copy link
Contributor

@hoodmane hoodmane Nov 18, 2025

Choose a reason for hiding this comment

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

One copy instead of two:

Suggested change
image_bytes = buffer.getvalue()
# Create and return response with appropriate headers
return Response(
to_js(image_bytes).buffer,
headers={
"Content-Type": content_type,
"Cache-Control": "public, max-age=3600",
},
)
image_bytes = create_proxy(buffer.getvalue())
jsbuffer = image_bytes.getBuffer()
try:
# Create and return response with appropriate headers
return Response(
jsbuffer.data,
headers={
"Content-Type": content_type,
"Cache-Control": "public, max-age=3600",
},
)
finally:
image_bytes.destroy()
jsbuffer.release()

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 tried this and it doesn't work: I get a corrupted image. This is what it's returning:

Screenshot 2025-11-20 at 12 11 24

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is presumably that the Response constructor is somehow messing up array buffer views. We should figure out where that is messed up and fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @ryanking13 this might be a good issue for you to investigate.

Choose a reason for hiding this comment

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

Yeah, it looks like it doesn't take Uint8Array type as a Response. The error log that I get in the console is

TypeError: Unsupported type in Response: Uint8Array

Raw ArrayBuffer seems to work fine, but it doesn't seem to handle ArrayBufferView.

I was able to make it work with the following workaround but it involves copying in slice anyways.

        image_bytes = create_proxy(buffer.getvalue())
        jsbuffer = image_bytes.getBuffer()

        # ...
        return Response(
          jsbuffer.data.slice(0, jsbuffer.data.byteLength).buffer,
        )

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we need a fix to JavaScript Response:

const a = new TextEncoder().encode("hello there!!")
const b = a.subarray(3)
const r = new Response(b)
console.log(await r.text()); // 'lo there!!'

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay nevermind that clearly works.

Return an HTML page showing available endpoints and examples.
"""
html = """
<!DOCTYPE html>
Copy link
Contributor

@hoodmane hoodmane Nov 18, 2025

Choose a reason for hiding this comment

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

What if we move this into it's own .html file and do

  endpoints = Path(__file__).parent / "endpoints.html"
  return Response(endpoints.read_text(), headers={"Content-Type": "text/html"})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, this is a great example too that we should put in our docs somewhere

@dom96 dom96 force-pushed the dominik/opengraph branch from 08c0160 to eb78296 Compare November 20, 2025 11:50
@dom96 dom96 changed the base branch from dominik/opengraph to main November 20, 2025 12:18
@dom96 dom96 force-pushed the dominik/pillow branch 2 times, most recently from d81e4e9 to 873157c Compare November 21, 2025 15:03
Copy link
Contributor

@hoodmane hoodmane left a comment

Choose a reason for hiding this comment

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

It'd be good to improve this when we figure out what's going on with the Reponse constructor but this is good for now.

Comment on lines +333 to +331
image_bytes = buffer.getvalue()

# Create and return response with appropriate headers
return Response(
to_js(image_bytes).buffer,
headers={
"Content-Type": content_type,
"Cache-Control": "public, max-age=3600",
},
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Okay nevermind that clearly works.

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.

3 participants