Skip to content

Add Source/Sink wrappers for kotlinx-browser's ArrayBuffer#498

Open
cedrickcooke wants to merge 2 commits intoKotlin:developfrom
cedrickcooke:integation-kotlinx-browser
Open

Add Source/Sink wrappers for kotlinx-browser's ArrayBuffer#498
cedrickcooke wants to merge 2 commits intoKotlin:developfrom
cedrickcooke:integation-kotlinx-browser

Conversation

@cedrickcooke
Copy link
Copy Markdown

Placed this in an integration folder like Okio has, to avoid adding a dependency to core.

@JakeWharton
Copy link
Copy Markdown
Contributor

If these are going to be supported as sink and source targets, I think they should be in the core. The kotlinx-browser dependency isn't required, we can define our own externals to avoid having a dependency.

*
* @param arrayBuffer the buffer to write to.
*/
public class ArrayBufferSink(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think either of these types should be public. If we're following the library and Kotlin conventions they should be in this shape:

fun ArrayBuffer.asSink(): RawSink
fun ArrayBuffer.asSource(): RawSource

It's probably also worth defining this for UInt8Array directly. Or possibly even only for UInt8Array...

The corresponding Okio issue for this feature is square/okio#689

@cedrickcooke
Copy link
Copy Markdown
Author

Done. PR/branch title are a bit out of date now, though 😆

- Moves implementation to kotlinx-io-core
- Adds our own `external class`es instead of using kotlinx-browser types
- Wrap Uint8Array instead of ArrayBuffer
- Use extension functions for asSink/asSource instead of public classes
@cedrickcooke cedrickcooke force-pushed the integation-kotlinx-browser branch from c8a6825 to f4fba13 Compare February 8, 2026 06:36
import kotlin.js.JsAny

/** Interop type for JavaScript `ArrayBuffer`. */
public external class ArrayBuffer : JsAny {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm I may have misled you by saying that this library could just copy the externals. Because normally that's what I do in libraries to avoid kotlinx-browser, but here these need to be public because the extensions hang off of them. That's... not great. I really don't like the whole kotlinx-browser approach. For Int8Array you can use Kotlin's normal ByteArray, but I'm not sure if UInt8Array is mapped to UByteArray or not. And there's no solution in the stdlib for ArrayBuffer. I will defer to the JetBrains people for what they think is the best approach.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

FWIW I do think having versions provided by the library is probably the "safest" option. Both JS/WasmJS have access to unsafeCast, which is already occasionally necessary for types that exist in both kotlinx-browser and kotlin-wrappers (like this one).

@fzhinkin
Copy link
Copy Markdown
Collaborator

fzhinkin commented Feb 9, 2026

Related PR: #386

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