FsaNodeWriteStream.d.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Writable } from 'stream';
  2. import { FsaNodeFsOpenFile } from './FsaNodeFsOpenFile';
  3. import type { IFileSystemWritableFileStream } from '../fsa/types';
  4. import type { IWriteStream } from '../node/types/misc';
  5. import type { IWriteStreamOptions } from '../node/types/options';
  6. /**
  7. * This WriteStream implementation does not build on top of the `fs` module,
  8. * but instead uses the lower-level `FileSystemFileHandle` interface. The reason
  9. * is the different semantics in `fs` and FSA (File System Access API) write streams.
  10. *
  11. * When data is written to an FSA file, a new FSA stream is created, it copies
  12. * the file to a temporary swap file. After each written chunk, that swap file
  13. * is closed and the original file is replaced with the swap file. This means,
  14. * if WriteStream was built on top of `fs`, each chunk write would result in
  15. * a file copy, write, close, rename operations, which is not what we want.
  16. *
  17. * Instead this implementation hooks into the lower-level and closes the swap
  18. * file only once the stream is closed. The downside is that the written data
  19. * is not immediately visible to other processes (because it is written to the
  20. * swap file), but that is the trade-off we have to make.
  21. *
  22. * @todo Could make this flush the data to the original file periodically, so that
  23. * the data is visible to other processes.
  24. * @todo This stream could work through `FileSystemSyncAccessHandle.write` in a
  25. * Worker thread instead.
  26. */
  27. export declare class FsaNodeWriteStream extends Writable implements IWriteStream {
  28. readonly path: string;
  29. protected readonly options: IWriteStreamOptions;
  30. protected __pending__: boolean;
  31. protected __closed__: boolean;
  32. protected __bytes__: number;
  33. protected readonly __stream__: Promise<IFileSystemWritableFileStream>;
  34. protected readonly __mutex__: <T = unknown>(code: import("../thingies/types").Code<T>) => Promise<T>;
  35. constructor(handle: Promise<FsaNodeFsOpenFile>, path: string, options: IWriteStreamOptions);
  36. private ___write___;
  37. private __close__;
  38. get bytesWritten(): number;
  39. get pending(): boolean;
  40. close(cb: any): void;
  41. _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  42. _writev(chunks: Array<{
  43. chunk: any;
  44. encoding: string;
  45. }>, callback: (error?: Error | null) => void): void;
  46. _final(callback: (error?: Error | null) => void): void;
  47. }