1919import { Form , SecurityScheme } from "@node-wot/td-tools" ;
2020import { ProtocolClient , Content , createLoggers , ContentSerdes } from "@node-wot/core" ;
2121import { Subscription } from "rxjs/Subscription" ;
22- import fs = require ( "fs" ) ;
22+ import { promises as asyncFs } from "fs" ;
2323import { fileURLToPath } from "node:url" ;
2424
2525const { debug } = createLoggers ( "binding-file" , "file-client" ) ;
@@ -33,8 +33,9 @@ export default class FileClient implements ProtocolClient {
3333 const filePath = fileURLToPath ( uri ) ;
3434 debug ( `Reading file of Content-Type ${ contentType } from path ${ filePath } .` ) ;
3535
36- const resource = fs . createReadStream ( filePath ) ;
37- return new Content ( contentType , resource ) ;
36+ const fileHandle = await asyncFs . open ( filePath ) ;
37+ const body = fileHandle . createReadStream ( ) ;
38+ return new Content ( contentType , body ) ;
3839 }
3940
4041 public async readResource ( form : Form ) : Promise < Content > {
@@ -50,10 +51,7 @@ export default class FileClient implements ProtocolClient {
5051 public async writeResource ( form : Form , content : Content ) : Promise < void > {
5152 const filePath = fileURLToPath ( form . href ) ;
5253
53- const writeStream = fs . createWriteStream ( filePath ) ;
54- const buffer = await content . toBuffer ( ) ;
55-
56- writeStream . end ( buffer ) ;
54+ await asyncFs . writeFile ( filePath , content . body ) ;
5755 }
5856
5957 public async invokeResource ( form : Form , content : Content ) : Promise < Content > {
0 commit comments