|
|
|
@ -243,6 +243,25 @@ function parseFileInfo(r: UnparsedFileInfo): FileInfo {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://mstn.github.io/2018/06/08/fixed-size-arrays-in-typescript/
|
|
|
|
|
type FixedSizeArray<T, N extends number> = ReadonlyArray<T> & {
|
|
|
|
|
length: N
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://gist.github.com/zapthedingbat/38ebfbedd98396624e5b5f2ff462611d
|
|
|
|
|
/** Converts a big-endian eight byte array to number */
|
|
|
|
|
function fromBytes(buffer: FixedSizeArray<number, 8>): number {
|
|
|
|
|
const bytes = new Uint8ClampedArray(buffer)
|
|
|
|
|
const size = bytes.byteLength
|
|
|
|
|
let x = 0
|
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
|
const byte = bytes[i]
|
|
|
|
|
x *= 0x100
|
|
|
|
|
x += byte
|
|
|
|
|
}
|
|
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Tauri abstraction for reading and writing files.
|
|
|
|
|
*
|
|
|
|
@ -285,12 +304,20 @@ class FileHandle extends Resource {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [data, nread] = await invoke<[number[], number]>('plugin:fs|read', {
|
|
|
|
|
const data = await invoke<ArrayBuffer | number[]>('plugin:fs|read', {
|
|
|
|
|
rid: this.rid,
|
|
|
|
|
len: buffer.byteLength
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
buffer.set(data)
|
|
|
|
|
// Rust side will never return an empty array for this command and
|
|
|
|
|
// ensure there is at least 8 elements there.
|
|
|
|
|
//
|
|
|
|
|
// This is an optimization to include the number of read bytes (as bigendian bytes)
|
|
|
|
|
// at the end of returned array to avoid serialization overhead of separate values.
|
|
|
|
|
const nread = fromBytes(data.slice(-8) as FixedSizeArray<number, 8>)
|
|
|
|
|
|
|
|
|
|
const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data
|
|
|
|
|
buffer.set(bytes.slice(0, bytes.length - 8))
|
|
|
|
|
|
|
|
|
|
return nread === 0 ? null : nread
|
|
|
|
|
}
|
|
|
|
|