moduleRunnerTransport.d.ts 3.0 KB

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