How to produce a composited PNG with an alpha layer but "flattened"
What are you trying to achieve?
I'm wondering if there's a way to manipulate pixels with Sharp so that every pixel with an alpha 0 is replaced with another pixel (keeping the alpha layer).
aka going from
to

But with the same display as the pixels that changed have a 0 alpha.
Have you searched for similar questions?
Yes, but noticed that flatten / remove_alpha completely removes the alpha layer is my understanding is correct.
Did you see the blend option of the composite operation? I think dest-in (or in, depending on the image order) would be appropriate for this scenario.
https://sharp.pixelplumbing.com/api-composite#composite
I hope this information helped. Please feel free to re-open with more details if further assistance is required.