NodeFileSystemWritableFileStream.d.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type { Data, FileSystemWritableFileStreamParams, IFileSystemWritableFileStream } from '@jsonjoy.com/fs-fsa';
  2. import type { IFileHandle } from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  3. import type { NodeFsaFs, NodeFsaContext } from './types';
  4. /**
  5. * When Chrome writes to the file, it creates a copy of the file with extension
  6. * `.crswap` and then replaces the original file with the copy only when the
  7. * `close()` method is called. If the `abort()` method is called, the `.crswap`
  8. * file is deleted.
  9. *
  10. * If a file name with with extension `.crswap` is already taken, it
  11. * creates a new swap file with extension `.1.crswap` and so on.
  12. */
  13. export declare const createSwapFile: (fs: NodeFsaFs, path: string, keepExistingData: boolean) => Promise<[handle: IFileHandle, path: string]>;
  14. interface SwapFile {
  15. /** Swap file full path name. */
  16. path: string;
  17. /** Seek offset in the file. */
  18. offset: number;
  19. /** Node.js open FileHandle. */
  20. handle?: IFileHandle;
  21. /** Resolves when swap file is ready for operations. */
  22. ready?: Promise<void>;
  23. }
  24. declare const WS: typeof WritableStream;
  25. /**
  26. * Is a WritableStream object with additional convenience methods, which
  27. * operates on a single file on disk. The interface is accessed through the
  28. * `FileSystemFileHandle.createWritable()` method.
  29. *
  30. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream
  31. */
  32. export declare class NodeFileSystemWritableFileStream extends WS implements IFileSystemWritableFileStream {
  33. protected readonly fs: NodeFsaFs;
  34. protected readonly path: string;
  35. protected readonly ctx: NodeFsaContext;
  36. protected readonly swap: SwapFile;
  37. constructor(fs: NodeFsaFs, path: string, keepExistingData: boolean, ctx: NodeFsaContext);
  38. /**
  39. * @sse https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/seek
  40. * @param position An `unsigned long` describing the byte position from the top
  41. * (beginning) of the file.
  42. */
  43. seek(position: number): Promise<void>;
  44. /**
  45. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/truncate
  46. * @param size An `unsigned long` of the amount of bytes to resize the stream to.
  47. */
  48. truncate(size: number): Promise<void>;
  49. protected writeBase(chunk: Data): Promise<void>;
  50. /**
  51. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write
  52. */
  53. write(chunk: Data): Promise<void>;
  54. write(params: FileSystemWritableFileStreamParams): Promise<void>;
  55. }
  56. export {};