市场夺宝奇兵
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.

87 lines
2.8 KiB

  1. import { HotPayload } from "../../types/hmrPayload.js";
  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 module cached in the runner, we can just confirm
  11. * it wasn't 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 vite runner
  20. */
  21. externalize: string;
  22. /**
  23. * Type of the module. Will be used to determine if import statement is correct.
  24. * For example, if Vite needs to throw an error if variable is not actually exported
  25. */
  26. type: 'module' | 'commonjs' | 'builtin' | 'network';
  27. }
  28. interface ViteFetchResult {
  29. /**
  30. * Code that will be evaluated by 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 equal to `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. };
  56. //#endregion
  57. //#region src/shared/moduleRunnerTransport.d.ts
  58. type ModuleRunnerTransportHandlers = {
  59. onMessage: (data: HotPayload) => void;
  60. onDisconnection: () => void;
  61. };
  62. /**
  63. * "send and connect" or "invoke" must be implemented
  64. */
  65. interface ModuleRunnerTransport {
  66. connect?(handlers: ModuleRunnerTransportHandlers): Promise<void> | void;
  67. disconnect?(): Promise<void> | void;
  68. send?(data: HotPayload): Promise<void> | void;
  69. invoke?(data: HotPayload): Promise<{
  70. result: any;
  71. } | {
  72. error: any;
  73. }>;
  74. timeout?: number;
  75. }
  76. interface NormalizedModuleRunnerTransport {
  77. connect?(onMessage?: (data: HotPayload) => void): Promise<void> | void;
  78. disconnect?(): Promise<void> | void;
  79. send(data: HotPayload): Promise<void>;
  80. invoke<T extends keyof InvokeMethods>(name: T, data: Parameters<InvokeMethods[T]>): Promise<ReturnType<Awaited<InvokeMethods[T]>>>;
  81. }
  82. declare const createWebSocketModuleRunnerTransport: (options: {
  83. createConnection: () => WebSocket;
  84. pingInterval?: number;
  85. }) => Required<Pick<ModuleRunnerTransport, "connect" | "disconnect" | "send">>;
  86. //#endregion
  87. export { ExternalFetchResult, FetchFunctionOptions, FetchResult, ModuleRunnerTransport, ModuleRunnerTransportHandlers, NormalizedModuleRunnerTransport, ViteFetchResult, createWebSocketModuleRunnerTransport };