|
| 1 | +/** |
| 2 | + * @module Cocoon |
| 3 | + * @description The main entry point for the Cocoon Node.js extension host. |
| 4 | + * |
| 5 | + * This file orchestrates the entire application lifecycle: |
| 6 | + * 1. Sets up the Node.js environment by patching module paths. |
| 7 | + * 2. Defines the main application workflow as a declarative `Effect`. |
| 8 | + * 3. Composes all service layers (`Core`, `IPC`, `Service`) into a single application layer. |
| 9 | + * 4. Executes the main Effect, which includes performing a handshake with Mountain, |
| 10 | + * initializing all services with data from the host, and activating extensions. |
| 11 | + */ |
| 12 | + |
| 13 | +import * as Path from "path"; |
| 14 | +import { Barrier, Context, Effect, Layer, Scope } from "effect"; |
| 15 | +import type { IExtensionHostInitData } from "vs/workbench/services/extensions/common/extensionHostProtocol.js"; |
| 16 | + |
| 17 | +import { CoreServiceLayer } from "./Core.js"; |
| 18 | +import { ExtensionHost } from "./Core/ExtensionHost.js"; |
| 19 | +import { RequireInterceptor } from "./Core/RequireInterceptor.js"; |
| 20 | +import { RunProcessPatch } from "./PatchProcess.js"; |
| 21 | +import { AllServiceLayer } from "./Service.js"; |
| 22 | +import { InitDataLayer } from "./Service/InitData.js"; |
| 23 | +import { IPCProvider, Live as LiveIPC } from "./Service/IPC.js"; |
| 24 | + |
| 25 | +// --- Pre-initialization Steps --- |
| 26 | +// Add the bundled VS Code module path to Node's search paths. This allows |
| 27 | +// imports like `vs/base/common/uri.js` to resolve correctly. |
| 28 | +const VSCodeOutputDirectory = |
| 29 | + process.env["VSCODE_OUT_DIR"] ?? |
| 30 | + Path.resolve(__dirname, "../../../Dependency/VSCode/out"); |
| 31 | +(module as any).paths.unshift(VSCodeOutputDirectory); |
| 32 | + |
| 33 | +// --- Application Logic --- |
| 34 | + |
| 35 | +/** |
| 36 | + * An Effect that represents the full initialization of all services *after* |
| 37 | + * the handshake with Mountain is complete and the init data has been received. |
| 38 | + */ |
| 39 | +const FullApplicationInitialization = Effect.gen(function* (_) { |
| 40 | + const Interceptor = yield* _(RequireInterceptor.Tag); |
| 41 | + yield* _(Interceptor.Install()); |
| 42 | + yield* _(Effect.logInfo("Node.js require() interceptor installed.")); |
| 43 | + |
| 44 | + // Now that the environment is fully set up, we can activate extensions. |
| 45 | + const Host = yield* _(ExtensionHost.Tag); |
| 46 | + // The '*' event signifies activating all extensions marked for startup. |
| 47 | + yield* _( |
| 48 | + Host.ActivateById("*" as any, { startup: true, activationEvent: "*" }), |
| 49 | + ); |
| 50 | + |
| 51 | + yield* _(Effect.logInfo("Startup extensions activated.")); |
| 52 | +}); |
| 53 | + |
| 54 | +/** |
| 55 | + * The main application workflow, described as a single declarative Effect. |
| 56 | + */ |
| 57 | +const Main = Effect.gen(function* (_) { |
| 58 | + const InitializationBarrier = yield* _(Barrier.make()); |
| 59 | + |
| 60 | + // 1. Apply all low-level process patches (e.g., console piping, termination hooks). |
| 61 | + yield* _(RunProcessPatch); |
| 62 | + |
| 63 | + // 2. Get the IPC provider. |
| 64 | + const IPC = yield* _(IPCProvider.Tag); |
| 65 | + |
| 66 | + // 3. Register the handler that will be called by Mountain to kick off initialization. |
| 67 | + IPC.RegisterInvokeHandler( |
| 68 | + "initExtensionHost", |
| 69 | + (initializationData: IExtensionHostInitData) => |
| 70 | + Effect.gen(function* (_) { |
| 71 | + yield* _( |
| 72 | + Effect.logInfo( |
| 73 | + "Received 'initExtensionHost' data from Mountain.", |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + // Compose the final application layer, providing the received init data. |
| 78 | + const ApplicationLayer = AllServiceLayer.pipe( |
| 79 | + Layer.provide(CoreServiceLayer), |
| 80 | + Layer.provide(InitDataLayer(initializationData)), |
| 81 | + ); |
| 82 | + |
| 83 | + // Provide the full layer to our initialization Effect and run it. |
| 84 | + yield* _( |
| 85 | + Effect.provide( |
| 86 | + FullApplicationInitialization, |
| 87 | + ApplicationLayer, |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + // Signal that initialization is complete. |
| 92 | + yield* _(Barrier.succeed(InitializationBarrier, undefined)); |
| 93 | + return "initialized"; // Acknowledge completion back to Mountain. |
| 94 | + }).pipe(Effect.runPromise), |
| 95 | + ); |
| 96 | + |
| 97 | + // 4. Send the 'Ready' signal to Mountain, indicating we are ready for init data. |
| 98 | + yield* _(IPC.SendNotification("$initialHandshake", [])); |
| 99 | + yield* _(Effect.logInfo("Cocoon is ready. Sent handshake to Mountain.")); |
| 100 | + |
| 101 | + // 5. Wait for the `initExtensionHost` handler to open the barrier. |
| 102 | + yield* _(Barrier.await(InitializationBarrier)); |
| 103 | + yield* _(Effect.logInfo("Cocoon is fully initialized and operational.")); |
| 104 | + |
| 105 | + // 6. Keep the process alive indefinitely. |
| 106 | + yield* _(Effect.never); |
| 107 | +}).pipe( |
| 108 | + // Global error handler for the entire application. |
| 109 | + Effect.catchAllCause((cause) => |
| 110 | + Effect.logFatal("Cocoon main process failed.", cause), |
| 111 | + ), |
| 112 | +); |
| 113 | + |
| 114 | +// --- Application Layer Composition --- |
| 115 | + |
| 116 | +/** |
| 117 | + * A configuration object for the IPC layer. |
| 118 | + */ |
| 119 | +const ApplicationConfiguration = { |
| 120 | + MountainAddress: process.env["MOUNTAIN_ADDR"] || "localhost:50051", |
| 121 | + CocoonAddress: process.env["COCOON_ADDR"] || "localhost:50052", |
| 122 | +}; |
| 123 | + |
| 124 | +/** |
| 125 | + * The base Layer for the application, providing the IPC connection. |
| 126 | + * Other layers will be built on top of this. |
| 127 | + */ |
| 128 | +const CocoonBaseLayer = LiveIPC(ApplicationConfiguration); |
| 129 | + |
| 130 | +// --- Run the Application --- |
| 131 | + |
| 132 | +// We create a master Scope for the application. When this scope is closed (e.g., on SIGTERM), |
| 133 | +// all finalizers from our `Layer.scoped` resources (like the gRPC client/server) |
| 134 | +// will be executed, ensuring a graceful shutdown. |
| 135 | +const ApplicationWithScope = Scope.make().pipe( |
| 136 | + Effect.flatMap((scope) => |
| 137 | + Effect.provide(Main, CocoonBaseLayer).pipe(Scope.extend(scope)), |
| 138 | + ), |
| 139 | +); |
| 140 | + |
| 141 | +// Fork the entire application into the background. |
| 142 | +Effect.runFork(ApplicationWithScope); |
0 commit comments