Skip to content

Commit

Permalink
feat: 🎸 add truncateSync() and ftruncateSync() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 18, 2023
1 parent 8d243a0 commit 2b77619
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
await fs.promises.mkdir('/tmp');
const tmpDirName = fs.mkdtempSync('/tmp/temporary-');
strictEqual(fs.statSync(tmpDirName).isDirectory(), true);

console.log('truncateSync() - can truncate a file');
await fs.promises.writeFile('/truncated.txt', 'truncate here: abcdefghijklmnopqrstuvwxyz');
fs.truncateSync('/truncated.txt', 14);
strictEqual(fs.readFileSync('/truncated.txt', 'utf8'), 'truncate here:');
};

const main = async () => {
Expand Down
13 changes: 11 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,21 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
return bufferToEncoding(buffer, opts.encoding);
};

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported;
public readonly truncateSync: FsSynchronousApi['truncateSync'] = (id: misc.TFileId, len?: number): void => {
if (isFd(id)) return this.ftruncateSync(id as number, len);
const filename = pathToFilename(id as misc.PathLike);
this.getSyncAdapter().call('trunc', [filename, Number(len) || 0]);
};

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = (fd: number, len?: number): void => {
const filename = this.getFileName(fd);
this.truncateSync(filename, len);
};

public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported;
public readonly readSync: FsSynchronousApi['readSync'] = notSupported;
public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported;
public readonly truncateSync: FsSynchronousApi['truncateSync'] = notSupported;
public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = notSupported;
public readonly writeSync: FsSynchronousApi['writeSync'] = notSupported;

Expand Down
1 change: 1 addition & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface FsaNodeSyncAdapterApi {
rm(req: [filename: string, opts?: opts.IRmOptions]): void;
mkdir(req: [filename: string, opts?: misc.TMode | opts.IMkdirOptions]): string | undefined;
mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string;
trunc(req: [filename: string, len: number]): void;
}

export interface FsaNodeSyncAdapter {
Expand Down
3 changes: 3 additions & 0 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,8 @@ export class FsaNodeSyncWorker {
mkdtemp: async ([filename]): Promise<string> => {
return await this.fs.promises.mkdtemp(filename, {encoding: 'utf8'}) as string;
},
trunc: async ([filename, len]): Promise<void> => {
await this.fs.promises.truncate(filename, len);
},
};
}

0 comments on commit 2b77619

Please sign in to comment.