Skip to content

Commit

Permalink
fix(binding-file): improve file-client implementation (#1213)
Browse files Browse the repository at this point in the history
* fix(binding-file): improve file-client implementation

* chore(binding-file): improve .gitignore for test files
  • Loading branch information
JKRhb authored Jan 11, 2024
1 parent e8e0cd2 commit 3ccd242
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
12 changes: 5 additions & 7 deletions packages/binding-file/src/file-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { Form, SecurityScheme } from "@node-wot/td-tools";
import { ProtocolClient, Content, createLoggers, ContentSerdes } from "@node-wot/core";
import { Subscription } from "rxjs/Subscription";
import fs = require("fs");
import { promises as asyncFs } from "fs";
import { fileURLToPath } from "node:url";

const { debug } = createLoggers("binding-file", "file-client");
Expand All @@ -33,8 +33,9 @@ export default class FileClient implements ProtocolClient {
const filePath = fileURLToPath(uri);
debug(`Reading file of Content-Type ${contentType} from path ${filePath}.`);

const resource = fs.createReadStream(filePath);
return new Content(contentType, resource);
const fileHandle = await asyncFs.open(filePath);
const body = fileHandle.createReadStream();
return new Content(contentType, body);
}

public async readResource(form: Form): Promise<Content> {
Expand All @@ -50,10 +51,7 @@ export default class FileClient implements ProtocolClient {
public async writeResource(form: Form, content: Content): Promise<void> {
const filePath = fileURLToPath(form.href);

const writeStream = fs.createWriteStream(filePath);
const buffer = await content.toBuffer();

writeStream.end(buffer);
await asyncFs.writeFile(filePath, content.body);
}

public async invokeResource(form: Form, content: Content): Promise<Content> {
Expand Down
3 changes: 2 additions & 1 deletion packages/binding-file/test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Ignore auxiliary files created by the FileClient implementation tests
test.*
test*.json
test*.txt

0 comments on commit 3ccd242

Please sign in to comment.