@@ -770,10 +770,14 @@ async function readTextFile(
770
770
throw new TypeError ( 'Must be a file URL.' )
771
771
}
772
772
773
- return await invoke < string > ( 'plugin:fs|read_text_file' , {
773
+ const arr = await invoke < ArrayBuffer | number [ ] > ( 'plugin:fs|read_text_file' , {
774
774
path : path instanceof URL ? path . toString ( ) : path ,
775
775
options
776
776
} )
777
+
778
+ const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array . from ( arr )
779
+
780
+ return new TextDecoder ( ) . decode ( bytes )
777
781
}
778
782
779
783
/**
@@ -804,6 +808,7 @@ async function readTextFileLines(
804
808
return await Promise . resolve ( {
805
809
path : pathStr ,
806
810
rid : null as number | null ,
811
+
807
812
async next ( ) : Promise < IteratorResult < string > > {
808
813
if ( this . rid === null ) {
809
814
this . rid = await invoke < number > ( 'plugin:fs|read_text_file_lines' , {
@@ -812,19 +817,35 @@ async function readTextFileLines(
812
817
} )
813
818
}
814
819
815
- const [ line , done ] = await invoke < [ string | null , boolean ] > (
820
+ const arr = await invoke < ArrayBuffer | number [ ] > (
816
821
'plugin:fs|read_text_file_lines_next' ,
817
822
{ rid : this . rid }
818
823
)
819
824
820
- // an iteration is over, reset rid for next iteration
821
- if ( done ) this . rid = null
825
+ const bytes =
826
+ arr instanceof ArrayBuffer ? new Uint8Array ( arr ) : Uint8Array . from ( arr )
827
+
828
+ // Rust side will never return an empty array for this command and
829
+ // ensure there is at least one elements there.
830
+ //
831
+ // This is an optimization to include whether we finished iteration or not (1 or 0)
832
+ // at the end of returned array to avoid serialization overhead of separate values.
833
+ const done = bytes [ bytes . byteLength - 1 ] === 1
834
+
835
+ if ( done ) {
836
+ // a full iteration is over, reset rid for next iteration
837
+ this . rid = null
838
+ return { value : null , done }
839
+ }
840
+
841
+ const line = new TextDecoder ( ) . decode ( bytes . slice ( 0 , bytes . byteLength ) )
822
842
823
843
return {
824
- value : done ? '' : line ! ,
844
+ value : line ,
825
845
done
826
846
}
827
847
} ,
848
+
828
849
[ Symbol . asyncIterator ] ( ) : AsyncIterableIterator < string > {
829
850
return this
830
851
}
0 commit comments