Skip to content

Commit

Permalink
feat: 🎸 add .truncate() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 15, 2023
1 parent 5119b8f commit 085335c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
this.fs.truncateSync(this.fd, newSize);
}
}

export interface FileSystemReadWriteOptions {
Expand Down
12 changes: 12 additions & 0 deletions src/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
2 changes: 1 addition & 1 deletion src/node-to-fsa/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IFs } from '..';
/**
* Required Node.js `fs` module functions for File System Access API.
*/
export type NodeFsaFs = Pick<IFs, 'promises' | 'openSync' | 'fsyncSync' | 'statSync' | 'closeSync' | 'readSync'>;
export type NodeFsaFs = Pick<IFs, 'promises' | 'openSync' | 'fsyncSync' | 'statSync' | 'closeSync' | 'readSync' | 'truncateSync'>;

export interface NodeFsaContext {
separator: '/' | '\\';
Expand Down

0 comments on commit 085335c

Please sign in to comment.