The short answer to read_all_async()
is that GJS/JavaScript does not support the mutable buffer type necessary for that function. Ideally it would be excluded from our documentation, but some other language bindings do support this.
However, it’s pretty likely that you don’t want to use a “read all” function at all, since that means “keep reading data until the remote end stops providing it or closes”. More likely what you want is a read loop:
function readLoop(inputStream, cancellable = null) {
// If you're reading from a daemon, it seems likely you're outputting
// line-by-line but either way GDataInputStream is a useful wrapper
if (!(inputStream instanceof Gio.DataInputStream))
inputStream = Gio.DataInputStream.new(inputStream);
inputStream.read_line_async(
GLib.PRIORITY_DEFAULT,
cancellable,
(stream, res) => {
try {
const data = stream.read_line_finish_utf8(res)[0];
if (data === null) {
throw new Gio.IOErrorEnum({
message: 'End of stream',
code: Gio.IOErrorEnum.CONNECTION_CLOSED,
});
}
// Do something with your data here, then recurse
readLoop(stream, cancellable);
} catch (e) {
logError(e);
}
}
);
}
You could also skip the recursion and wrap that in Promise
, looping inside an async
function or something, but generally this the pattern I’d use. Passing a GCancellable
will allow you to halt the operation at anytime (eg. when your extension is disabled).