From 085335ca8736b67db05c02bbd11cccf5b9bf000b Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Thu, 15 Jun 2023 02:18:30 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20.truncate()=20meth?= =?UTF-8?q?od?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts | 8 ++++++++ .../__tests__/NodeFileSystemSyncAccessHandle.test.ts | 12 ++++++++++++ src/node-to-fsa/types.ts | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts b/src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts index 28a90e81e..8dae957a2 100644 --- a/src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts +++ b/src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts @@ -55,6 +55,14 @@ export class NodeFileSystemSyncAccessHandle { throw error; } } + + /** + * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate + * @param newSize The number of bytes to resize the file to. + */ + public async truncate(newSize: number): Promise { + this.fs.truncateSync(this.fd, newSize); + } } export interface FileSystemReadWriteOptions { diff --git a/src/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.ts b/src/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.ts index 9db6c161d..a6192a2d0 100644 --- a/src/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.ts +++ b/src/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.ts @@ -112,4 +112,16 @@ maybe('NodeFileSystemSyncAccessHandle', () => { } }); }); + + describe('.truncate()', () => { + test('can read from beginning', async () => { + const { dir } = setup({ + 'file.txt': '0123456789', + }); + const entry = await dir.getFileHandle('file.txt'); + const sync = await entry.createSyncAccessHandle!(); + const res = await sync.truncate(5); + expect(res).toBe(undefined); + }); + }); }); diff --git a/src/node-to-fsa/types.ts b/src/node-to-fsa/types.ts index 950ab9ff7..0d0a058e9 100644 --- a/src/node-to-fsa/types.ts +++ b/src/node-to-fsa/types.ts @@ -3,7 +3,7 @@ import type { IFs } from '..'; /** * Required Node.js `fs` module functions for File System Access API. */ -export type NodeFsaFs = Pick; +export type NodeFsaFs = Pick; export interface NodeFsaContext { separator: '/' | '\\';