You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.9 KiB

  1. import { HotPayload } from '../../types/hmrPayload.js';
  2. interface FetchFunctionOptions {
  3. cached?: boolean;
  4. startOffset?: number;
  5. }
  6. type FetchResult = CachedFetchResult | ExternalFetchResult | ViteFetchResult;
  7. interface CachedFetchResult {
  8. /**
  9. * If module cached in the runner, we can just confirm
  10. * it wasn't invalidated on the server side.
  11. */
  12. cache: true;
  13. }
  14. interface ExternalFetchResult {
  15. /**
  16. * The path to the externalized module starting with file://,
  17. * by default this will be imported via a dynamic "import"
  18. * instead of being transformed by vite and loaded with vite runner
  19. */
  20. externalize: string;
  21. /**
  22. * Type of the module. Will be used to determine if import statement is correct.
  23. * For example, if Vite needs to throw an error if variable is not actually exported
  24. */
  25. type: 'module' | 'commonjs' | 'builtin' | 'network';
  26. }
  27. interface ViteFetchResult {
  28. /**
  29. * Code that will be evaluated by vite runner
  30. * by default this will be wrapped in an async function
  31. */
  32. code: string;
  33. /**
  34. * File path of the module on disk.
  35. * This will be resolved as import.meta.url/filename
  36. * Will be equal to `null` for virtual modules
  37. */
  38. file: string | null;
  39. /**
  40. * Module ID in the server module graph.
  41. */
  42. id: string;
  43. /**
  44. * Module URL used in the import.
  45. */
  46. url: string;
  47. /**
  48. * Invalidate module on the client side.
  49. */
  50. invalidate: boolean;
  51. }
  52. type InvokeMethods = {
  53. fetchModule: (id: string, importer?: string, options?: FetchFunctionOptions) => Promise<FetchResult>;
  54. };
  55. type ModuleRunnerTransportHandlers = {
  56. onMessage: (data: HotPayload) => void;
  57. onDisconnection: () => void;
  58. };
  59. /**
  60. * "send and connect" or "invoke" must be implemented
  61. */
  62. interface ModuleRunnerTransport {
  63. connect?(handlers: ModuleRunnerTransportHandlers): Promise<void> | void;
  64. disconnect?(): Promise<void> | void;
  65. send?(data: HotPayload): Promise<void> | void;
  66. invoke?(data: HotPayload): Promise<{
  67. result: any;
  68. } | {
  69. error: any;
  70. }>;
  71. timeout?: number;
  72. }
  73. interface NormalizedModuleRunnerTransport {
  74. connect?(onMessage?: (data: HotPayload) => void): Promise<void> | void;
  75. disconnect?(): Promise<void> | void;
  76. send(data: HotPayload): Promise<void>;
  77. invoke<T extends keyof InvokeMethods>(name: T, data: Parameters<InvokeMethods[T]>): Promise<ReturnType<Awaited<InvokeMethods[T]>>>;
  78. }
  79. declare const createWebSocketModuleRunnerTransport: (options: {
  80. createConnection: () => WebSocket;
  81. pingInterval?: number;
  82. }) => Required<Pick<ModuleRunnerTransport, "connect" | "disconnect" | "send">>;
  83. export { type ExternalFetchResult as E, type FetchFunctionOptions as F, type ModuleRunnerTransport as M, type NormalizedModuleRunnerTransport as N, type ViteFetchResult as V, type FetchResult as a, type ModuleRunnerTransportHandlers as b, createWebSocketModuleRunnerTransport as c };