kleisauke / wasm-vips

libvips for the browser and Node.js, compiled to WebAssembly with Emscripten.
https://kleisauke.github.io/wasm-vips/
MIT License
509 stars 26 forks source link

SourceCustom requires a pointer in the onRead hook #74

Closed WojciechSoczynskiTHEY closed 1 week ago

WojciechSoczynskiTHEY commented 2 months ago

LibVips offers since some time a way to provide a custom source. This is handy since it allows creating adapter for many different input methods e.g S3 byte ranges / streaming.

Unfortunately, the SourceCustom.onRead requires a pointer as the first argument to the handler.

class SourceCustom extends Source {
        onRead: (ptr: number, size: bigint) => bigint;
        onSeek: (offset: bigint, whence: number) => bigint;
    }

The example on https://www.libvips.org/2019/11/29/True-streaming-for-libvips.html that is in Python shows how ideally that should work. Please note that the file here is only for demonstrative purposes and in a real world scenario that would be something completely dufferent.

file = open(sys.argv[1], "rb")

def read_handler(size):
    return file.read(size)

def seek_handler(offset, whence):
    file.seek(offset, whence)
    return file.tell()

source = pyvips.Source()
source.on_read(read_handler)
source.on_seek(seek_handler)
kleisauke commented 2 months ago

The arguments align with the read(2) system call. I'm uncertain if we should deviate from this approach. Here's an example to help you get started: https://gist.github.com/kleisauke/2ee72c6788dfc12b463f10e5b8d6632f

Note that the ReadableStream and WritableStream overlay is still on the roadmap, see: https://github.com/kleisauke/wasm-vips/issues/59#issuecomment-1869496369.

kleisauke commented 1 month ago

Actually, you're right, this is a bit confusing to do in JavaScript. Commit 26aaa0c4b7801b1ee51c2f33048b8b41de95c1a0 avoids the need of pointer arguments in these callbacks. I've updated the previous linked gist to reflect this.

This will be in v0.0.11, thanks for reporting this!

kleisauke commented 1 week ago

v0.0.11 is now available.