ProgressTracker.d.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /// <reference types="node" />
  2. import { Socket } from "net";
  3. export type ProgressType = "upload" | "download" | "list";
  4. /**
  5. * Describes progress of file transfer.
  6. */
  7. export interface ProgressInfo {
  8. /** A name describing this info, e.g. the filename of the transfer. */
  9. readonly name: string;
  10. /** The type of transfer, typically "upload" or "download". */
  11. readonly type: ProgressType;
  12. /** Transferred bytes in current transfer. */
  13. readonly bytes: number;
  14. /** Transferred bytes since last counter reset. Useful for tracking multiple transfers. */
  15. readonly bytesOverall: number;
  16. }
  17. export type ProgressHandler = (info: ProgressInfo) => void;
  18. /**
  19. * Tracks progress of one socket data transfer at a time.
  20. */
  21. export declare class ProgressTracker {
  22. bytesOverall: number;
  23. protected readonly intervalMs = 500;
  24. protected onStop: (stopWithUpdate: boolean) => void;
  25. protected onHandle: ProgressHandler;
  26. /**
  27. * Register a new handler for progress info. Use `undefined` to disable reporting.
  28. */
  29. reportTo(onHandle?: ProgressHandler): void;
  30. /**
  31. * Start tracking transfer progress of a socket.
  32. *
  33. * @param socket The socket to observe.
  34. * @param name A name associated with this progress tracking, e.g. a filename.
  35. * @param type The type of the transfer, typically "upload" or "download".
  36. */
  37. start(socket: Socket, name: string, type: ProgressType): void;
  38. /**
  39. * Stop tracking transfer progress.
  40. */
  41. stop(): void;
  42. /**
  43. * Call the progress handler one more time, then stop tracking.
  44. */
  45. updateAndStop(): void;
  46. }