Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: add Blob methods to FileHandle #40021

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/api/fs.md
Expand Up @@ -188,6 +188,17 @@ When operating on file handles, the mode cannot be changed from what it was set
to with [`fsPromises.open()`][]. Therefore, this is equivalent to
[`filehandle.writeFile()`][].

#### `filehandle.arrayBuffer()`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.

Reads the whole file as binary data.

#### `filehandle.chmod(mode)`
<!-- YAML
added: v10.0.0
Expand Down Expand Up @@ -415,6 +426,17 @@ Request that all data for the open file descriptor is flushed to the storage
device. The specific implementation is operating system and device specific.
Refer to the POSIX fsync(2) documentation for more detail.

#### `filehandle.text()`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* Returns: {Promise} Fulfills with a {string} upon success.

Reads the whole file as UTF-8 text.

#### `filehandle.truncate(len)`
<!-- YAML
added: v10.0.0
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/fs/promises.js
Expand Up @@ -252,6 +252,15 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
return readable;
}

async arrayBuffer() {
const buffer = await this.readFile();
return buffer.buffer;
}

text() {
return this.readFile({ encoding: 'utf8' });
Copy link
Member

@ronag ronag Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toUSVString?

}

[kTransfer]() {
if (this[kClosePromise] || this[kRefs] > 1) {
throw lazyDOMException('Cannot transfer FileHandle while in use',
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-fs-promises-file-handle-arrayBuffer.js
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');

// The following tests validate FileHandle.prototype.arrayBuffer method.

const fs = require('fs');
const { open } = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

tmpdir.refresh();

(async () => {
const data = Buffer.allocUnsafe(100);
const filePath = path.resolve(tmpDir, 'arrayBuffer');
fs.writeFileSync(filePath, data);

let fileHandle;
try {
fileHandle = await open(filePath);

const fileContent = await fileHandle.arrayBuffer();
assert.deepStrictEqual(Buffer.from(fileContent), data);
} finally {
await fileHandle?.close();
}
})().then(common.mustCall());
30 changes: 30 additions & 0 deletions test/parallel/test-fs-promises-file-handle-text.js
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');

// The following tests validate FileHandle.prototype.text method.

const fs = require('fs');
const { open } = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

tmpdir.refresh();

(async () => {
const data = 'Hello World!';
const filePath = path.resolve(tmpDir, 'text');
fs.writeFileSync(filePath, data);

let fileHandle;
try {
fileHandle = await open(filePath);

const fileContent = await fileHandle.text();
assert.strictEqual(fileContent, data);
} finally {
await fileHandle?.close();
}
})().then(common.mustCall());