@databricks/appkit 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +1179 -0
- package/CLAUDE.md +1178 -2
- package/NOTICE.md +2 -0
- package/bin/setup-claude.js +1 -1
- package/dist/_virtual/rolldown_runtime.js +2 -2
- package/dist/analytics/analytics.d.ts +33 -8
- package/dist/analytics/analytics.d.ts.map +1 -1
- package/dist/analytics/analytics.js +51 -24
- package/dist/analytics/analytics.js.map +1 -1
- package/dist/analytics/defaults.js.map +1 -1
- package/dist/analytics/query.js +4 -4
- package/dist/analytics/query.js.map +1 -1
- package/dist/appkit/package.js +1 -1
- package/dist/cache/defaults.js.map +1 -1
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +1 -2
- package/dist/cache/index.js.map +1 -1
- package/dist/cache/storage/memory.js.map +1 -1
- package/dist/connectors/lakebase/client.js +7 -8
- package/dist/connectors/lakebase/client.js.map +1 -1
- package/dist/connectors/lakebase/defaults.js.map +1 -1
- package/dist/connectors/sql-warehouse/client.js.map +1 -1
- package/dist/connectors/sql-warehouse/defaults.js.map +1 -1
- package/dist/context/execution-context.js +75 -0
- package/dist/context/execution-context.js.map +1 -0
- package/dist/context/index.js +27 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/service-context.js +149 -0
- package/dist/context/service-context.js.map +1 -0
- package/dist/context/user-context.js +15 -0
- package/dist/context/user-context.js.map +1 -0
- package/dist/core/appkit.d.ts +3 -0
- package/dist/core/appkit.d.ts.map +1 -1
- package/dist/core/appkit.js +7 -0
- package/dist/core/appkit.js.map +1 -1
- package/dist/index.d.ts +5 -6
- package/dist/index.js +3 -10
- package/dist/plugin/interceptors/cache.js.map +1 -1
- package/dist/plugin/interceptors/retry.js.map +1 -1
- package/dist/plugin/interceptors/telemetry.js.map +1 -1
- package/dist/plugin/interceptors/timeout.js.map +1 -1
- package/dist/plugin/plugin.d.ts +39 -5
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/plugin/plugin.js +82 -6
- package/dist/plugin/plugin.js.map +1 -1
- package/dist/plugin/to-plugin.d.ts +4 -0
- package/dist/plugin/to-plugin.d.ts.map +1 -1
- package/dist/plugin/to-plugin.js +3 -0
- package/dist/plugin/to-plugin.js.map +1 -1
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +3 -4
- package/dist/server/index.js.map +1 -1
- package/dist/server/remote-tunnel/remote-tunnel-manager.js.map +1 -1
- package/dist/server/utils.js.map +1 -1
- package/dist/server/vite-dev-server.js +0 -2
- package/dist/server/vite-dev-server.js.map +1 -1
- package/dist/shared/src/sql/helpers.js.map +1 -1
- package/dist/stream/arrow-stream-processor.js.map +1 -1
- package/dist/stream/buffers.js.map +1 -1
- package/dist/stream/sse-writer.js.map +1 -1
- package/dist/stream/stream-manager.js.map +1 -1
- package/dist/stream/stream-registry.js.map +1 -1
- package/dist/telemetry/instrumentations.js.map +1 -1
- package/dist/type-generator/index.js.map +1 -1
- package/dist/type-generator/query-registry.js.map +1 -1
- package/dist/type-generator/types.js.map +1 -1
- package/dist/type-generator/vite-plugin.js.map +1 -1
- package/dist/utils/env-validator.js +1 -5
- package/dist/utils/env-validator.js.map +1 -1
- package/dist/utils/merge.js +1 -5
- package/dist/utils/merge.js.map +1 -1
- package/dist/utils/vite-config-merge.js +1 -5
- package/dist/utils/vite-config-merge.js.map +1 -1
- package/llms.txt +160 -58
- package/package.json +5 -2
- package/dist/index.js.map +0 -1
- package/dist/utils/databricks-client-middleware.d.ts +0 -17
- package/dist/utils/databricks-client-middleware.d.ts.map +0 -1
- package/dist/utils/databricks-client-middleware.js +0 -117
- package/dist/utils/databricks-client-middleware.js.map +0 -1
- package/dist/utils/index.js +0 -26
- package/dist/utils/index.js.map +0 -1
package/dist/core/appkit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appkit.js","names":[
|
|
1
|
+
{"version":3,"file":"appkit.js","names":[],"sources":["../../src/core/appkit.ts"],"sourcesContent":["import type {\n BasePlugin,\n CacheConfig,\n InputPluginMap,\n OptionalConfigPluginDef,\n PluginConstructor,\n PluginData,\n PluginMap,\n} from \"shared\";\nimport { CacheManager } from \"../cache\";\nimport { ServiceContext } from \"../context\";\nimport type { TelemetryConfig } from \"../telemetry\";\nimport { TelemetryManager } from \"../telemetry\";\n\nexport class AppKit<TPlugins extends InputPluginMap> {\n private static _instance: AppKit<InputPluginMap> | null = null;\n private pluginInstances: Record<string, BasePlugin> = {};\n private setupPromises: Promise<void>[] = [];\n\n private constructor(config: { plugins: TPlugins }) {\n const { plugins, ...globalConfig } = config;\n\n const pluginEntries = Object.entries(plugins);\n\n const corePlugins = pluginEntries.filter(([_, p]) => {\n return (p?.plugin?.phase ?? \"normal\") === \"core\";\n });\n const normalPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"normal\",\n );\n const deferredPlugins = pluginEntries.filter(\n ([_, p]) => (p?.plugin?.phase ?? \"normal\") === \"deferred\",\n );\n\n for (const [name, pluginData] of corePlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of normalPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData);\n }\n }\n\n for (const [name, pluginData] of deferredPlugins) {\n if (pluginData) {\n this.createAndRegisterPlugin(globalConfig, name, pluginData, {\n plugins: this.pluginInstances,\n });\n }\n }\n }\n\n private createAndRegisterPlugin<T extends PluginConstructor>(\n config: Omit<{ plugins: TPlugins }, \"plugins\">,\n name: string,\n pluginData: OptionalConfigPluginDef<T>,\n extraData?: Record<string, unknown>,\n ) {\n const { plugin: Plugin, config: pluginConfig } = pluginData;\n const baseConfig = {\n ...config,\n ...Plugin.DEFAULT_CONFIG,\n ...pluginConfig,\n name,\n ...extraData,\n };\n const pluginInstance = new Plugin(baseConfig);\n\n this.pluginInstances[name] = pluginInstance;\n\n pluginInstance.validateEnv();\n\n this.setupPromises.push(pluginInstance.setup());\n\n Object.defineProperty(this, name, {\n get() {\n return this.pluginInstances[name];\n },\n enumerable: true,\n });\n }\n\n static async _createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n >(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;\n cache?: CacheConfig;\n } = {},\n ): Promise<PluginMap<T>> {\n // Initialize core services\n TelemetryManager.initialize(config?.telemetry);\n await CacheManager.getInstance(config?.cache);\n\n // Initialize ServiceContext for Databricks client management\n // This provides the service principal client and shared resources\n await ServiceContext.initialize();\n\n const rawPlugins = config.plugins as T;\n const preparedPlugins = AppKit.preparePlugins(rawPlugins);\n const mergedConfig = {\n plugins: preparedPlugins,\n };\n\n AppKit._instance = new AppKit(mergedConfig);\n\n await Promise.all(AppKit._instance.setupPromises);\n\n return AppKit._instance as unknown as PluginMap<T>;\n }\n\n private static preparePlugins(\n plugins: PluginData<PluginConstructor, unknown, string>[],\n ) {\n const result: InputPluginMap = {};\n for (const currentPlugin of plugins) {\n result[currentPlugin.name] = {\n plugin: currentPlugin.plugin,\n config: currentPlugin.config as Record<string, unknown>,\n };\n }\n return result;\n }\n}\n\n/**\n * Bootstraps AppKit with the provided configuration.\n */\nexport async function createApp<\n T extends PluginData<PluginConstructor, unknown, string>[],\n>(\n config: {\n plugins?: T;\n telemetry?: TelemetryConfig;\n cache?: CacheConfig;\n } = {},\n): Promise<PluginMap<T>> {\n return AppKit._createApp(config);\n}\n"],"mappings":";;;;;;;cAU4C;AAI5C,IAAa,SAAb,MAAa,OAAwC;;mBACO;;CAI1D,AAAQ,YAAY,QAA+B;yBAHG,EAAE;uBACf,EAAE;EAGzC,MAAM,EAAE,SAAS,GAAG,iBAAiB;EAErC,MAAM,gBAAgB,OAAO,QAAQ,QAAQ;EAE7C,MAAM,cAAc,cAAc,QAAQ,CAAC,GAAG,OAAO;AACnD,WAA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MAAM,iBAAiB,QAC1B,QAAO,cAAc,QAAQ;GAC3B,QAAQ,cAAc;GACtB,QAAQ,cAAc;GACvB;AAEH,SAAO;;;;;;AAOX,eAAsB,UAGpB,SAII,EAAE,EACiB;AACvB,QAAO,OAAO,WAAW,OAAO"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { BasePluginConfig, IAppRouter } from "./shared/src/plugin.js";
|
|
2
|
+
import { CacheConfig } from "./shared/src/cache.js";
|
|
2
3
|
import { StreamExecutionSettings } from "./shared/src/execute.js";
|
|
3
|
-
import { SQLTypeMarker } from "./shared/src/sql/types.js";
|
|
4
4
|
import { isSQLTypeMarker, sql } from "./shared/src/sql/helpers.js";
|
|
5
|
-
import {
|
|
6
|
-
import { ITelemetry } from "./telemetry/types.js";
|
|
5
|
+
import { ITelemetry, TelemetryConfig } from "./telemetry/types.js";
|
|
7
6
|
import { Counter, Histogram, SeverityNumber, Span, SpanStatusCode } from "./telemetry/index.js";
|
|
7
|
+
import { createApp } from "./core/appkit.js";
|
|
8
|
+
import { CacheManager } from "./cache/index.js";
|
|
8
9
|
import { Plugin } from "./plugin/plugin.js";
|
|
9
10
|
import { toPlugin } from "./plugin/to-plugin.js";
|
|
10
11
|
import { analytics } from "./analytics/analytics.js";
|
|
11
|
-
import { createApp } from "./core/appkit.js";
|
|
12
12
|
import { server } from "./server/index.js";
|
|
13
13
|
import { appKitTypesPlugin } from "./type-generator/vite-plugin.js";
|
|
14
|
-
|
|
15
|
-
export { type BasePluginConfig, CacheManager, type Counter, type Histogram, type IAppRouter, type ITelemetry, Plugin, type SQLTypeMarker, SeverityNumber, type Span, SpanStatusCode, type StreamExecutionSettings, analytics, appKitTypesPlugin, createApp, getRequestContext, isSQLTypeMarker, server, sql, toPlugin };
|
|
14
|
+
export { type BasePluginConfig, type CacheConfig, CacheManager, type Counter, type Histogram, type IAppRouter, type ITelemetry, Plugin, SeverityNumber, type Span, SpanStatusCode, type StreamExecutionSettings, type TelemetryConfig, analytics, appKitTypesPlugin, createApp, isSQLTypeMarker, server, sql, toPlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import { isSQLTypeMarker, sql } from "./shared/src/sql/helpers.js";
|
|
2
2
|
import { SeverityNumber, SpanStatusCode } from "./telemetry/index.js";
|
|
3
|
-
import { getRequestContext } from "./utils/databricks-client-middleware.js";
|
|
4
|
-
import { init_utils } from "./utils/index.js";
|
|
5
3
|
import { CacheManager } from "./cache/index.js";
|
|
4
|
+
import { createApp } from "./core/appkit.js";
|
|
5
|
+
import "./core/index.js";
|
|
6
6
|
import { Plugin } from "./plugin/plugin.js";
|
|
7
7
|
import { toPlugin } from "./plugin/to-plugin.js";
|
|
8
8
|
import "./plugin/index.js";
|
|
9
9
|
import { analytics } from "./analytics/analytics.js";
|
|
10
10
|
import "./analytics/index.js";
|
|
11
|
-
import { createApp } from "./core/appkit.js";
|
|
12
|
-
import "./core/index.js";
|
|
13
11
|
import { appKitTypesPlugin } from "./type-generator/vite-plugin.js";
|
|
14
12
|
import { server } from "./server/index.js";
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
init_utils();
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
|
-
export { CacheManager, Plugin, SeverityNumber, SpanStatusCode, analytics, appKitTypesPlugin, createApp, getRequestContext, isSQLTypeMarker, server, sql, toPlugin };
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
|
14
|
+
export { CacheManager, Plugin, SeverityNumber, SpanStatusCode, analytics, appKitTypesPlugin, createApp, isSQLTypeMarker, server, sql, toPlugin };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","names":[
|
|
1
|
+
{"version":3,"file":"cache.js","names":[],"sources":["../../../src/plugin/interceptors/cache.ts"],"sourcesContent":["import type { CacheManager } from \"../../cache\";\nimport type { CacheConfig } from \"shared\";\nimport type { InterceptorContext, ExecutionInterceptor } from \"./types\";\n\n// interceptor to handle caching logic\nexport class CacheInterceptor implements ExecutionInterceptor {\n constructor(\n private cacheManager: CacheManager,\n private config: CacheConfig,\n ) {}\n\n async intercept<T>(\n fn: () => Promise<T>,\n context: InterceptorContext,\n ): Promise<T> {\n // if cache disabled, ignore\n if (!this.config.enabled || !this.config.cacheKey?.length) {\n return fn();\n }\n\n return this.cacheManager.getOrExecute(\n this.config.cacheKey,\n fn,\n context.userKey,\n { ttl: this.config.ttl },\n );\n }\n}\n"],"mappings":";AAKA,IAAa,mBAAb,MAA8D;CAC5D,YACE,AAAQ,cACR,AAAQ,QACR;EAFQ;EACA;;CAGV,MAAM,UACJ,IACA,SACY;AAEZ,MAAI,CAAC,KAAK,OAAO,WAAW,CAAC,KAAK,OAAO,UAAU,OACjD,QAAO,IAAI;AAGb,SAAO,KAAK,aAAa,aACvB,KAAK,OAAO,UACZ,IACA,QAAQ,SACR,EAAE,KAAK,KAAK,OAAO,KAAK,CACzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","names":[
|
|
1
|
+
{"version":3,"file":"retry.js","names":[],"sources":["../../../src/plugin/interceptors/retry.ts"],"sourcesContent":["import type { RetryConfig } from \"shared\";\nimport type { InterceptorContext, ExecutionInterceptor } from \"./types\";\n\n// interceptor to handle retry logic\nexport class RetryInterceptor implements ExecutionInterceptor {\n private attempts: number;\n private initialDelay: number;\n private maxDelay: number;\n\n constructor(config: RetryConfig) {\n this.attempts = config.attempts ?? 3;\n this.initialDelay = config.initialDelay ?? 1000;\n this.maxDelay = config.maxDelay ?? 30000;\n }\n\n async intercept<T>(\n fn: () => Promise<T>,\n context: InterceptorContext,\n ): Promise<T> {\n let lastError: Error | unknown;\n\n for (let attempt = 1; attempt <= this.attempts; attempt++) {\n try {\n return await fn();\n } catch (error) {\n lastError = error;\n\n // last attempt, rethrow the error\n if (attempt === this.attempts) {\n throw error;\n }\n\n // don't retry if was already aborted\n if (context.signal?.aborted) {\n throw error;\n }\n\n const delay = this.calculateDelay(attempt);\n await this.sleep(delay);\n }\n }\n\n // type guard\n throw lastError;\n }\n\n private calculateDelay(attempt: number): number {\n // exponential backoff\n const delay = this.initialDelay * 2 ** (attempt - 1);\n\n // max delay cap\n return Math.min(delay, this.maxDelay);\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n"],"mappings":";AAIA,IAAa,mBAAb,MAA8D;CAK5D,YAAY,QAAqB;AAC/B,OAAK,WAAW,OAAO,YAAY;AACnC,OAAK,eAAe,OAAO,gBAAgB;AAC3C,OAAK,WAAW,OAAO,YAAY;;CAGrC,MAAM,UACJ,IACA,SACY;EACZ,IAAI;AAEJ,OAAK,IAAI,UAAU,GAAG,WAAW,KAAK,UAAU,UAC9C,KAAI;AACF,UAAO,MAAM,IAAI;WACV,OAAO;AACd,eAAY;AAGZ,OAAI,YAAY,KAAK,SACnB,OAAM;AAIR,OAAI,QAAQ,QAAQ,QAClB,OAAM;GAGR,MAAM,QAAQ,KAAK,eAAe,QAAQ;AAC1C,SAAM,KAAK,MAAM,MAAM;;AAK3B,QAAM;;CAGR,AAAQ,eAAe,SAAyB;EAE9C,MAAM,QAAQ,KAAK,eAAe,MAAM,UAAU;AAGlD,SAAO,KAAK,IAAI,OAAO,KAAK,SAAS;;CAGvC,AAAQ,MAAM,IAA2B;AACvC,SAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.js","names":[
|
|
1
|
+
{"version":3,"file":"telemetry.js","names":[],"sources":["../../../src/plugin/interceptors/telemetry.ts"],"sourcesContent":["import type { ITelemetry, Span } from \"../../telemetry\";\nimport { SpanStatusCode } from \"../../telemetry\";\nimport type { TelemetryConfig } from \"shared\";\nimport type { InterceptorContext, ExecutionInterceptor } from \"./types\";\n\n/**\n * Interceptor to automatically instrument plugin executions with telemetry spans.\n * Wraps the execution in a span and handles success/error status.\n */\nexport class TelemetryInterceptor implements ExecutionInterceptor {\n constructor(\n private telemetry: ITelemetry,\n private config?: TelemetryConfig,\n ) {}\n\n async intercept<T>(\n fn: () => Promise<T>,\n _context: InterceptorContext,\n ): Promise<T> {\n const spanName = this.config?.spanName || \"plugin.execute\";\n return this.telemetry.startActiveSpan(\n spanName,\n { attributes: this.config?.attributes },\n async (span: Span) => {\n try {\n const result = await fn();\n span.setStatus({ code: SpanStatusCode.OK });\n return result;\n } catch (error) {\n span.recordException(error as Error);\n span.setStatus({ code: SpanStatusCode.ERROR });\n throw error;\n } finally {\n span.end();\n }\n },\n );\n }\n}\n"],"mappings":";;;;;;;AASA,IAAa,uBAAb,MAAkE;CAChE,YACE,AAAQ,WACR,AAAQ,QACR;EAFQ;EACA;;CAGV,MAAM,UACJ,IACA,UACY;EACZ,MAAM,WAAW,KAAK,QAAQ,YAAY;AAC1C,SAAO,KAAK,UAAU,gBACpB,UACA,EAAE,YAAY,KAAK,QAAQ,YAAY,EACvC,OAAO,SAAe;AACpB,OAAI;IACF,MAAM,SAAS,MAAM,IAAI;AACzB,SAAK,UAAU,EAAE,MAAM,eAAe,IAAI,CAAC;AAC3C,WAAO;YACA,OAAO;AACd,SAAK,gBAAgB,MAAe;AACpC,SAAK,UAAU,EAAE,MAAM,eAAe,OAAO,CAAC;AAC9C,UAAM;aACE;AACR,SAAK,KAAK;;IAGf"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.js","names":[
|
|
1
|
+
{"version":3,"file":"timeout.js","names":[],"sources":["../../../src/plugin/interceptors/timeout.ts"],"sourcesContent":["import type { InterceptorContext, ExecutionInterceptor } from \"./types\";\n\n// interceptor to handle timeout logic\nexport class TimeoutInterceptor implements ExecutionInterceptor {\n constructor(private timeoutMs: number) {}\n\n async intercept<T>(\n fn: () => Promise<T>,\n context: InterceptorContext,\n ): Promise<T> {\n // create timeout signal\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(() => {\n timeoutController.abort(\n new Error(`Operation timed out after ${this.timeoutMs} ms`),\n );\n }, this.timeoutMs);\n\n try {\n // combine user signal (if exists) with timeout signal\n const combinedSignal = context.signal\n ? this._combineSignals([context.signal, timeoutController.signal])\n : timeoutController.signal;\n\n // execute function with combined signal\n context.signal = combinedSignal;\n return await fn();\n } finally {\n // cleanup timeout\n clearTimeout(timeoutId);\n }\n }\n\n private _combineSignals(signals: AbortSignal[]): AbortSignal {\n const controller = new AbortController();\n for (const signal of signals) {\n if (signal.aborted) {\n controller.abort(signal.reason);\n break;\n }\n signal.addEventListener(\n \"abort\",\n () => {\n controller.abort(signal.reason);\n },\n { once: true },\n );\n }\n return controller.signal;\n }\n}\n"],"mappings":";AAGA,IAAa,qBAAb,MAAgE;CAC9D,YAAY,AAAQ,WAAmB;EAAnB;;CAEpB,MAAM,UACJ,IACA,SACY;EAEZ,MAAM,oBAAoB,IAAI,iBAAiB;EAC/C,MAAM,YAAY,iBAAiB;AACjC,qBAAkB,sBAChB,IAAI,MAAM,6BAA6B,KAAK,UAAU,KAAK,CAC5D;KACA,KAAK,UAAU;AAElB,MAAI;AAOF,WAAQ,SALe,QAAQ,SAC3B,KAAK,gBAAgB,CAAC,QAAQ,QAAQ,kBAAkB,OAAO,CAAC,GAChE,kBAAkB;AAItB,UAAO,MAAM,IAAI;YACT;AAER,gBAAa,UAAU;;;CAI3B,AAAQ,gBAAgB,SAAqC;EAC3D,MAAM,aAAa,IAAI,iBAAiB;AACxC,OAAK,MAAM,UAAU,SAAS;AAC5B,OAAI,OAAO,SAAS;AAClB,eAAW,MAAM,OAAO,OAAO;AAC/B;;AAEF,UAAO,iBACL,eACM;AACJ,eAAW,MAAM,OAAO,OAAO;MAEjC,EAAE,MAAM,MAAM,CACf;;AAEH,SAAO,WAAW"}
|
package/dist/plugin/plugin.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BasePlugin, BasePluginConfig, IAppResponse, PluginEndpointMap, PluginPhase, RouteConfig } from "../shared/src/plugin.js";
|
|
2
2
|
import { PluginExecutionSettings, StreamExecuteHandler, StreamExecutionSettings } from "../shared/src/execute.js";
|
|
3
|
+
import { ITelemetry } from "../telemetry/types.js";
|
|
3
4
|
import { AppManager } from "../app/index.js";
|
|
4
5
|
import { CacheManager } from "../cache/index.js";
|
|
5
6
|
import { StreamManager } from "../stream/stream-manager.js";
|
|
6
|
-
import { ITelemetry } from "../telemetry/types.js";
|
|
7
7
|
import { DevFileReader } from "./dev-reader.js";
|
|
8
8
|
import express from "express";
|
|
9
9
|
|
|
@@ -17,8 +17,6 @@ declare abstract class Plugin<TConfig extends BasePluginConfig = BasePluginConfi
|
|
|
17
17
|
protected streamManager: StreamManager;
|
|
18
18
|
protected telemetry: ITelemetry;
|
|
19
19
|
protected abstract envVars: string[];
|
|
20
|
-
/** If the plugin requires the Databricks client to be set in the request context */
|
|
21
|
-
requiresDatabricksClient: boolean;
|
|
22
20
|
/** Registered endpoints for this plugin */
|
|
23
21
|
private registeredEndpoints;
|
|
24
22
|
static phase: PluginPhase;
|
|
@@ -29,8 +27,44 @@ declare abstract class Plugin<TConfig extends BasePluginConfig = BasePluginConfi
|
|
|
29
27
|
setup(): Promise<void>;
|
|
30
28
|
getEndpoints(): PluginEndpointMap;
|
|
31
29
|
abortActiveOperations(): void;
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Execute operations using the user's identity from the request.
|
|
32
|
+
*
|
|
33
|
+
* Returns a scoped instance of this plugin where all method calls
|
|
34
|
+
* will execute with the user's Databricks credentials instead of
|
|
35
|
+
* the service principal.
|
|
36
|
+
*
|
|
37
|
+
* @param req - The Express request containing the user token in headers
|
|
38
|
+
* @returns A scoped plugin instance that executes as the user
|
|
39
|
+
* @throws Error if user token is not available in request headers
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // In route handler - execute query as the requesting user
|
|
44
|
+
* router.post('/users/me/query/:key', async (req, res) => {
|
|
45
|
+
* const result = await this.asUser(req).query(req.params.key)
|
|
46
|
+
* res.json(result)
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
49
|
+
* // Mixed execution in same handler
|
|
50
|
+
* router.post('/dashboard', async (req, res) => {
|
|
51
|
+
* const [systemData, userData] = await Promise.all([
|
|
52
|
+
* this.getSystemStats(), // Service principal
|
|
53
|
+
* this.asUser(req).getUserPreferences(), // User context
|
|
54
|
+
* ])
|
|
55
|
+
* res.json({ systemData, userData })
|
|
56
|
+
* })
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
asUser(req: express.Request): this;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a proxy that wraps method calls in a user context.
|
|
62
|
+
* This allows all plugin methods to automatically use the user's
|
|
63
|
+
* Databricks credentials.
|
|
64
|
+
*/
|
|
65
|
+
private createUserContextProxy;
|
|
66
|
+
protected executeStream<T>(res: IAppResponse, fn: StreamExecuteHandler<T>, options: StreamExecutionSettings, userKey?: string): Promise<void>;
|
|
67
|
+
protected execute<T>(fn: (signal?: AbortSignal) => Promise<T>, options: PluginExecutionSettings, userKey?: string): Promise<T | undefined>;
|
|
34
68
|
protected registerEndpoint(name: string, path: string): void;
|
|
35
69
|
protected route<_TResponse>(router: express.Router, config: RouteConfig): void;
|
|
36
70
|
private _buildExecutionConfig;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../src/plugin/plugin.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../src/plugin/plugin.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;uBAyDsB,uBACJ,mBAAmB,6BACxB;oBAgBmB;;EAlBV,UAAM,KAAA,EAKT,YALS;EAAA,UAAA,GAAA,EAMX,UANW;YACV,aAAA,EAMS,aANT;YAAmB,aAAA,EAOV,aAPU;YAiBL,SAAA,EATT,UASS;qBAbb,OAAA,EAAA,MAAA,EAAA;;UAEQ,mBAAA;SACA,KAAA,EAOX,WAPW;MACJ,EAAA,MAAA;aAMP,CAAA,MAAA,EAGgB,OAHhB;aAGgB,CAAA,CAAA,EAAA,IAAA;cAeN,CAAA,CAAA,EAAR,OAAA,CAAQ,MAAA,CAAA,EAAA,IAAA;OAIb,CAAA,CAAA,EAAA,OAAA,CAAA,IAAA,CAAA;cAEK,CAAA,CAAA,EAAA,iBAAA;uBAqCI,CAAA,CAAA,EAAA,IAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAR,OAAA,CAAQ;;;;;;;kCAoEb,kBACD,qBAAqB,aAChB,4CACO;qCA2DF,gBAAgB,QAAQ,aAC7B,4CAER,QAAQ;;sCA0BD,OAAA,CAAQ,gBACR"}
|
package/dist/plugin/plugin.js
CHANGED
|
@@ -3,9 +3,11 @@ import { TelemetryManager } from "../telemetry/telemetry-manager.js";
|
|
|
3
3
|
import "../telemetry/index.js";
|
|
4
4
|
import { validateEnv } from "../utils/env-validator.js";
|
|
5
5
|
import { deepMerge } from "../utils/merge.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { ServiceContext } from "../context/service-context.js";
|
|
7
|
+
import { getCurrentUserId, runInUserContext } from "../context/execution-context.js";
|
|
8
|
+
import { init_context } from "../context/index.js";
|
|
8
9
|
import { CacheManager } from "../cache/index.js";
|
|
10
|
+
import { AppManager } from "../app/index.js";
|
|
9
11
|
import { StreamManager } from "../stream/stream-manager.js";
|
|
10
12
|
import "../stream/index.js";
|
|
11
13
|
import { DevFileReader } from "./dev-reader.js";
|
|
@@ -15,7 +17,22 @@ import { TelemetryInterceptor } from "./interceptors/telemetry.js";
|
|
|
15
17
|
import { TimeoutInterceptor } from "./interceptors/timeout.js";
|
|
16
18
|
|
|
17
19
|
//#region src/plugin/plugin.ts
|
|
18
|
-
|
|
20
|
+
init_context();
|
|
21
|
+
/**
|
|
22
|
+
* Methods that should not be proxied by asUser().
|
|
23
|
+
* These are lifecycle/internal methods that don't make sense
|
|
24
|
+
* to execute in a user context.
|
|
25
|
+
*/
|
|
26
|
+
const EXCLUDED_FROM_PROXY = new Set([
|
|
27
|
+
"setup",
|
|
28
|
+
"shutdown",
|
|
29
|
+
"validateEnv",
|
|
30
|
+
"injectRoutes",
|
|
31
|
+
"getEndpoints",
|
|
32
|
+
"abortActiveOperations",
|
|
33
|
+
"asUser",
|
|
34
|
+
"constructor"
|
|
35
|
+
]);
|
|
19
36
|
var Plugin = class {
|
|
20
37
|
static {
|
|
21
38
|
this.phase = "normal";
|
|
@@ -23,7 +40,6 @@ var Plugin = class {
|
|
|
23
40
|
constructor(config) {
|
|
24
41
|
this.config = config;
|
|
25
42
|
this.isReady = false;
|
|
26
|
-
this.requiresDatabricksClient = false;
|
|
27
43
|
this.registeredEndpoints = {};
|
|
28
44
|
this.name = config.name ?? "plugin";
|
|
29
45
|
this.telemetry = TelemetryManager.getProvider(this.name, config.telemetry);
|
|
@@ -44,18 +60,77 @@ var Plugin = class {
|
|
|
44
60
|
abortActiveOperations() {
|
|
45
61
|
this.streamManager.abortAll();
|
|
46
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Execute operations using the user's identity from the request.
|
|
65
|
+
*
|
|
66
|
+
* Returns a scoped instance of this plugin where all method calls
|
|
67
|
+
* will execute with the user's Databricks credentials instead of
|
|
68
|
+
* the service principal.
|
|
69
|
+
*
|
|
70
|
+
* @param req - The Express request containing the user token in headers
|
|
71
|
+
* @returns A scoped plugin instance that executes as the user
|
|
72
|
+
* @throws Error if user token is not available in request headers
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // In route handler - execute query as the requesting user
|
|
77
|
+
* router.post('/users/me/query/:key', async (req, res) => {
|
|
78
|
+
* const result = await this.asUser(req).query(req.params.key)
|
|
79
|
+
* res.json(result)
|
|
80
|
+
* })
|
|
81
|
+
*
|
|
82
|
+
* // Mixed execution in same handler
|
|
83
|
+
* router.post('/dashboard', async (req, res) => {
|
|
84
|
+
* const [systemData, userData] = await Promise.all([
|
|
85
|
+
* this.getSystemStats(), // Service principal
|
|
86
|
+
* this.asUser(req).getUserPreferences(), // User context
|
|
87
|
+
* ])
|
|
88
|
+
* res.json({ systemData, userData })
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
asUser(req) {
|
|
93
|
+
const token = req.headers["x-forwarded-access-token"];
|
|
94
|
+
const userId = req.headers["x-forwarded-user"];
|
|
95
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
96
|
+
if (!token && isDev) {
|
|
97
|
+
console.warn("[AppKit] asUser() called without user token in development mode. Using service principal.");
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
if (!token) throw new Error("User token not available in request headers. Ensure the request has the x-forwarded-access-token header.");
|
|
101
|
+
if (!userId && !isDev) throw new Error("User ID not available in request headers. Ensure the request has the x-forwarded-user header.");
|
|
102
|
+
const effectiveUserId = userId || "dev-user";
|
|
103
|
+
const userContext = ServiceContext.createUserContext(token, effectiveUserId);
|
|
104
|
+
return this.createUserContextProxy(userContext);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Creates a proxy that wraps method calls in a user context.
|
|
108
|
+
* This allows all plugin methods to automatically use the user's
|
|
109
|
+
* Databricks credentials.
|
|
110
|
+
*/
|
|
111
|
+
createUserContextProxy(userContext) {
|
|
112
|
+
return new Proxy(this, { get: (target, prop, receiver) => {
|
|
113
|
+
const value = Reflect.get(target, prop, receiver);
|
|
114
|
+
if (typeof value !== "function") return value;
|
|
115
|
+
if (typeof prop === "string" && EXCLUDED_FROM_PROXY.has(prop)) return value;
|
|
116
|
+
return (...args) => {
|
|
117
|
+
return runInUserContext(userContext, () => value.apply(target, args));
|
|
118
|
+
};
|
|
119
|
+
} });
|
|
120
|
+
}
|
|
47
121
|
async executeStream(res, fn, options, userKey) {
|
|
48
122
|
const { stream: streamConfig, default: defaultConfig, user: userConfig } = options;
|
|
49
123
|
const executeConfig = this._buildExecutionConfig({
|
|
50
124
|
default: defaultConfig,
|
|
51
125
|
user: userConfig
|
|
52
126
|
});
|
|
127
|
+
const effectiveUserKey = userKey ?? getCurrentUserId();
|
|
53
128
|
const self = this;
|
|
54
129
|
const asyncWrapperFn = async function* (streamSignal) {
|
|
55
130
|
const context = {
|
|
56
131
|
signal: streamSignal,
|
|
57
132
|
metadata: /* @__PURE__ */ new Map(),
|
|
58
|
-
userKey
|
|
133
|
+
userKey: effectiveUserKey
|
|
59
134
|
};
|
|
60
135
|
const interceptors = self._buildInterceptors(executeConfig);
|
|
61
136
|
const wrappedFn = async () => {
|
|
@@ -70,9 +145,10 @@ var Plugin = class {
|
|
|
70
145
|
async execute(fn, options, userKey) {
|
|
71
146
|
const executeConfig = this._buildExecutionConfig(options);
|
|
72
147
|
const interceptors = this._buildInterceptors(executeConfig);
|
|
148
|
+
const effectiveUserKey = userKey ?? getCurrentUserId();
|
|
73
149
|
const context = {
|
|
74
150
|
metadata: /* @__PURE__ */ new Map(),
|
|
75
|
-
userKey
|
|
151
|
+
userKey: effectiveUserKey
|
|
76
152
|
};
|
|
77
153
|
try {
|
|
78
154
|
return await this._executeWithInterceptors(fn, interceptors, context);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":["config: TConfig","context: ExecutionContext","interceptors: ExecutionInterceptor[]"],"sources":["../../src/plugin/plugin.ts"],"sourcesContent":["import type express from \"express\";\nimport type {\n BasePlugin,\n BasePluginConfig,\n IAppResponse,\n PluginEndpointMap,\n PluginExecuteConfig,\n PluginExecutionSettings,\n PluginPhase,\n RouteConfig,\n StreamExecuteHandler,\n StreamExecutionSettings,\n} from \"shared\";\nimport { AppManager } from \"../app\";\nimport { CacheManager } from \"../cache\";\nimport { StreamManager } from \"../stream\";\nimport {\n type ITelemetry,\n normalizeTelemetryOptions,\n TelemetryManager,\n} from \"../telemetry\";\nimport { deepMerge, validateEnv } from \"../utils\";\nimport { DevFileReader } from \"./dev-reader\";\nimport { CacheInterceptor } from \"./interceptors/cache\";\nimport { RetryInterceptor } from \"./interceptors/retry\";\nimport { TelemetryInterceptor } from \"./interceptors/telemetry\";\nimport { TimeoutInterceptor } from \"./interceptors/timeout\";\nimport type {\n ExecutionContext,\n ExecutionInterceptor,\n} from \"./interceptors/types\";\n\nexport abstract class Plugin<\n TConfig extends BasePluginConfig = BasePluginConfig,\n> implements BasePlugin\n{\n protected isReady = false;\n protected cache: CacheManager;\n protected app: AppManager;\n protected devFileReader: DevFileReader;\n protected streamManager: StreamManager;\n protected telemetry: ITelemetry;\n protected abstract envVars: string[];\n\n /** If the plugin requires the Databricks client to be set in the request context */\n requiresDatabricksClient = false;\n\n /** Registered endpoints for this plugin */\n private registeredEndpoints: PluginEndpointMap = {};\n\n static phase: PluginPhase = \"normal\";\n name: string;\n\n constructor(protected config: TConfig) {\n this.name = config.name ?? \"plugin\";\n this.telemetry = TelemetryManager.getProvider(this.name, config.telemetry);\n this.streamManager = new StreamManager();\n this.cache = CacheManager.getInstanceSync();\n this.app = new AppManager();\n this.devFileReader = DevFileReader.getInstance();\n\n this.isReady = true;\n }\n\n validateEnv() {\n validateEnv(this.envVars);\n }\n\n injectRoutes(_: express.Router) {\n return;\n }\n\n async setup() {}\n\n getEndpoints(): PluginEndpointMap {\n return this.registeredEndpoints;\n }\n\n abortActiveOperations(): void {\n this.streamManager.abortAll();\n }\n\n // streaming execution with interceptors\n protected async executeStream<T>(\n res: IAppResponse,\n fn: StreamExecuteHandler<T>,\n options: StreamExecutionSettings,\n userKey: string,\n ) {\n // destructure options\n const {\n stream: streamConfig,\n default: defaultConfig,\n user: userConfig,\n } = options;\n\n // build execution options\n const executeConfig = this._buildExecutionConfig({\n default: defaultConfig,\n user: userConfig,\n });\n\n const self = this;\n\n // wrapper function to ensure it returns a generator\n const asyncWrapperFn = async function* (streamSignal?: AbortSignal) {\n // build execution context\n const context: ExecutionContext = {\n signal: streamSignal,\n metadata: new Map(),\n userKey: userKey,\n };\n\n // build interceptors\n const interceptors = self._buildInterceptors(executeConfig);\n\n // wrap the function to ensure it returns a promise\n const wrappedFn = async () => {\n const result = await fn(context.signal);\n return result;\n };\n\n // execute the function with interceptors\n const result = await self._executeWithInterceptors(\n wrappedFn as (signal?: AbortSignal) => Promise<T>,\n interceptors,\n context,\n );\n\n // check if result is a generator\n if (self._checkIfGenerator(result)) {\n yield* result;\n } else {\n yield result;\n }\n };\n\n // stream the result to the client\n await this.streamManager.stream(res, asyncWrapperFn, streamConfig);\n }\n\n // single sync execution with interceptors\n protected async execute<T>(\n fn: (signal?: AbortSignal) => Promise<T>,\n options: PluginExecutionSettings,\n userKey: string,\n ): Promise<T | undefined> {\n const executeConfig = this._buildExecutionConfig(options);\n\n const interceptors = this._buildInterceptors(executeConfig);\n\n const context: ExecutionContext = {\n metadata: new Map(),\n userKey: userKey,\n };\n\n try {\n return await this._executeWithInterceptors(fn, interceptors, context);\n } catch (_error) {\n // production-safe, don't crash sdk\n return undefined;\n }\n }\n\n protected registerEndpoint(name: string, path: string): void {\n this.registeredEndpoints[name] = path;\n }\n\n protected route<_TResponse>(\n router: express.Router,\n config: RouteConfig,\n ): void {\n const { name, method, path, handler } = config;\n\n router[method](path, handler);\n\n this.registerEndpoint(name, `/api/${this.name}${path}`);\n }\n\n // build execution options by merging defaults, plugin config, and user overrides\n private _buildExecutionConfig(\n options: PluginExecutionSettings,\n ): PluginExecuteConfig {\n const { default: methodDefaults, user: userOverride } = options;\n\n // Merge: method defaults <- plugin config <- user override (highest priority)\n return deepMerge(\n deepMerge(methodDefaults, this.config),\n userOverride ?? {},\n ) as PluginExecuteConfig;\n }\n\n // build interceptors based on execute options\n private _buildInterceptors(\n options: PluginExecuteConfig,\n ): ExecutionInterceptor[] {\n const interceptors: ExecutionInterceptor[] = [];\n\n // order matters: telemetry → timeout → retry → cache (innermost to outermost)\n\n // Only add telemetry interceptor if traces are enabled\n const telemetryConfig = normalizeTelemetryOptions(this.config.telemetry);\n if (\n telemetryConfig.traces &&\n (options.telemetryInterceptor?.enabled ?? true)\n ) {\n interceptors.push(\n new TelemetryInterceptor(this.telemetry, options.telemetryInterceptor),\n );\n }\n\n if (options.timeout && options.timeout > 0) {\n interceptors.push(new TimeoutInterceptor(options.timeout));\n }\n\n if (\n options.retry?.enabled &&\n options.retry.attempts &&\n options.retry.attempts > 1\n ) {\n interceptors.push(new RetryInterceptor(options.retry));\n }\n\n if (options.cache?.enabled && options.cache.cacheKey?.length) {\n interceptors.push(new CacheInterceptor(this.cache, options.cache));\n }\n\n return interceptors;\n }\n\n // execute method wrapped with interceptors\n private async _executeWithInterceptors<T>(\n fn: (signal?: AbortSignal) => Promise<T>,\n interceptors: ExecutionInterceptor[],\n context: ExecutionContext,\n ): Promise<T> {\n // no interceptors, execute directly\n if (interceptors.length === 0) {\n return fn(context.signal);\n }\n // build nested execution chain from interceptors\n let wrappedFn = () => fn(context.signal);\n\n // wrap each interceptor around the previous function\n for (const interceptor of interceptors) {\n const previousFn = wrappedFn;\n wrappedFn = () => interceptor.intercept(previousFn, context);\n }\n\n return wrappedFn();\n }\n\n private _checkIfGenerator(\n result: any,\n ): result is AsyncGenerator<any, void, unknown> {\n return (\n result && typeof result === \"object\" && Symbol.asyncIterator in result\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;YAqBkD;AAWlD,IAAsB,SAAtB,MAGA;;eAe8B;;CAG5B,YAAY,AAAUA,QAAiB;EAAjB;iBAjBF;kCASO;6BAGsB,EAAE;AAMjD,OAAK,OAAO,OAAO,QAAQ;AAC3B,OAAK,YAAY,iBAAiB,YAAY,KAAK,MAAM,OAAO,UAAU;AAC1E,OAAK,gBAAgB,IAAI,eAAe;AACxC,OAAK,QAAQ,aAAa,iBAAiB;AAC3C,OAAK,MAAM,IAAI,YAAY;AAC3B,OAAK,gBAAgB,cAAc,aAAa;AAEhD,OAAK,UAAU;;CAGjB,cAAc;AACZ,cAAY,KAAK,QAAQ;;CAG3B,aAAa,GAAmB;CAIhC,MAAM,QAAQ;CA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AAE,CACnB;;CAIH,AAAQ,mBACN,SACwB;EACxB,MAAMC,eAAuC,EAAE;AAM/C,MADwB,0BAA0B,KAAK,OAAO,UAAU,CAEtD,WACf,QAAQ,sBAAsB,WAAW,MAE1C,cAAa,KACX,IAAI,qBAAqB,KAAK,WAAW,QAAQ,qBAAqB,CACvE;AAGH,MAAI,QAAQ,WAAW,QAAQ,UAAU,EACvC,cAAa,KAAK,IAAI,mBAAmB,QAAQ,QAAQ,CAAC;AAG5D,MACE,QAAQ,OAAO,WACf,QAAQ,MAAM,YACd,QAAQ,MAAM,WAAW,EAEzB,cAAa,KAAK,IAAI,iBAAiB,QAAQ,MAAM,CAAC;AAGxD,MAAI,QAAQ,OAAO,WAAW,QAAQ,MAAM,UAAU,OACpD,cAAa,KAAK,IAAI,iBAAiB,KAAK,OAAO,QAAQ,MAAM,CAAC;AAGpE,SAAO;;CAIT,MAAc,yBACZ,IACA,cACA,SACY;AAEZ,MAAI,aAAa,WAAW,EAC1B,QAAO,GAAG,QAAQ,OAAO;EAG3B,IAAI,kBAAkB,GAAG,QAAQ,OAAO;AAGxC,OAAK,MAAM,eAAe,cAAc;GACtC,MAAM,aAAa;AACnB,qBAAkB,YAAY,UAAU,YAAY,QAAQ;;AAG9D,SAAO,WAAW;;CAGpB,AAAQ,kBACN,QAC8C;AAC9C,SACE,UAAU,OAAO,WAAW,YAAY,OAAO,iBAAiB"}
|
|
1
|
+
{"version":3,"file":"plugin.js","names":[],"sources":["../../src/plugin/plugin.ts"],"sourcesContent":["import type express from \"express\";\nimport type {\n BasePlugin,\n BasePluginConfig,\n IAppResponse,\n PluginEndpointMap,\n PluginExecuteConfig,\n PluginExecutionSettings,\n PluginPhase,\n RouteConfig,\n StreamExecuteHandler,\n StreamExecutionSettings,\n} from \"shared\";\nimport { AppManager } from \"../app\";\nimport { CacheManager } from \"../cache\";\nimport {\n ServiceContext,\n getCurrentUserId,\n runInUserContext,\n type UserContext,\n} from \"../context\";\nimport { StreamManager } from \"../stream\";\nimport {\n type ITelemetry,\n normalizeTelemetryOptions,\n TelemetryManager,\n} from \"../telemetry\";\nimport { deepMerge, validateEnv } from \"../utils\";\nimport { DevFileReader } from \"./dev-reader\";\nimport { CacheInterceptor } from \"./interceptors/cache\";\nimport { RetryInterceptor } from \"./interceptors/retry\";\nimport { TelemetryInterceptor } from \"./interceptors/telemetry\";\nimport { TimeoutInterceptor } from \"./interceptors/timeout\";\nimport type {\n InterceptorContext,\n ExecutionInterceptor,\n} from \"./interceptors/types\";\n\n/**\n * Methods that should not be proxied by asUser().\n * These are lifecycle/internal methods that don't make sense\n * to execute in a user context.\n */\nconst EXCLUDED_FROM_PROXY = new Set([\n // Lifecycle methods\n \"setup\",\n \"shutdown\",\n \"validateEnv\",\n \"injectRoutes\",\n \"getEndpoints\",\n \"abortActiveOperations\",\n // asUser itself - prevent chaining like .asUser().asUser()\n \"asUser\",\n // Internal methods\n \"constructor\",\n]);\n\nexport abstract class Plugin<\n TConfig extends BasePluginConfig = BasePluginConfig,\n> implements BasePlugin\n{\n protected isReady = false;\n protected cache: CacheManager;\n protected app: AppManager;\n protected devFileReader: DevFileReader;\n protected streamManager: StreamManager;\n protected telemetry: ITelemetry;\n protected abstract envVars: string[];\n\n /** Registered endpoints for this plugin */\n private registeredEndpoints: PluginEndpointMap = {};\n\n static phase: PluginPhase = \"normal\";\n name: string;\n\n constructor(protected config: TConfig) {\n this.name = config.name ?? \"plugin\";\n this.telemetry = TelemetryManager.getProvider(this.name, config.telemetry);\n this.streamManager = new StreamManager();\n this.cache = CacheManager.getInstanceSync();\n this.app = new AppManager();\n this.devFileReader = DevFileReader.getInstance();\n\n this.isReady = true;\n }\n\n validateEnv() {\n validateEnv(this.envVars);\n }\n\n injectRoutes(_: express.Router) {\n return;\n }\n\n async setup() {}\n\n getEndpoints(): PluginEndpointMap {\n return this.registeredEndpoints;\n }\n\n abortActiveOperations(): void {\n this.streamManager.abortAll();\n }\n\n /**\n * Execute operations using the user's identity from the request.\n *\n * Returns a scoped instance of this plugin where all method calls\n * will execute with the user's Databricks credentials instead of\n * the service principal.\n *\n * @param req - The Express request containing the user token in headers\n * @returns A scoped plugin instance that executes as the user\n * @throws Error if user token is not available in request headers\n *\n * @example\n * ```typescript\n * // In route handler - execute query as the requesting user\n * router.post('/users/me/query/:key', async (req, res) => {\n * const result = await this.asUser(req).query(req.params.key)\n * res.json(result)\n * })\n *\n * // Mixed execution in same handler\n * router.post('/dashboard', async (req, res) => {\n * const [systemData, userData] = await Promise.all([\n * this.getSystemStats(), // Service principal\n * this.asUser(req).getUserPreferences(), // User context\n * ])\n * res.json({ systemData, userData })\n * })\n * ```\n */\n asUser(req: express.Request): this {\n const token = req.headers[\"x-forwarded-access-token\"] as string;\n const userId = req.headers[\"x-forwarded-user\"] as string;\n const isDev = process.env.NODE_ENV === \"development\";\n\n // In local development, fall back to service principal\n // since there's no user token available\n if (!token && isDev) {\n console.warn(\n \"[AppKit] asUser() called without user token in development mode. \" +\n \"Using service principal.\",\n );\n\n return this;\n }\n\n if (!token) {\n throw new Error(\n \"User token not available in request headers. \" +\n \"Ensure the request has the x-forwarded-access-token header.\",\n );\n }\n\n if (!userId && !isDev) {\n throw new Error(\n \"User ID not available in request headers. \" +\n \"Ensure the request has the x-forwarded-user header.\",\n );\n }\n\n const effectiveUserId = userId || \"dev-user\";\n\n const userContext = ServiceContext.createUserContext(\n token,\n effectiveUserId,\n );\n\n // Return a proxy that wraps method calls in user context\n return this.createUserContextProxy(userContext);\n }\n\n /**\n * Creates a proxy that wraps method calls in a user context.\n * This allows all plugin methods to automatically use the user's\n * Databricks credentials.\n */\n private createUserContextProxy(userContext: UserContext): this {\n return new Proxy(this, {\n get: (target, prop, receiver) => {\n const value = Reflect.get(target, prop, receiver);\n\n if (typeof value !== \"function\") {\n return value;\n }\n\n if (typeof prop === \"string\" && EXCLUDED_FROM_PROXY.has(prop)) {\n return value;\n }\n\n return (...args: unknown[]) => {\n return runInUserContext(userContext, () => value.apply(target, args));\n };\n },\n }) as this;\n }\n\n // streaming execution with interceptors\n protected async executeStream<T>(\n res: IAppResponse,\n fn: StreamExecuteHandler<T>,\n options: StreamExecutionSettings,\n userKey?: string,\n ) {\n // destructure options\n const {\n stream: streamConfig,\n default: defaultConfig,\n user: userConfig,\n } = options;\n\n // build execution options\n const executeConfig = this._buildExecutionConfig({\n default: defaultConfig,\n user: userConfig,\n });\n\n // Get user key from context if not provided\n const effectiveUserKey = userKey ?? getCurrentUserId();\n\n const self = this;\n\n // wrapper function to ensure it returns a generator\n const asyncWrapperFn = async function* (streamSignal?: AbortSignal) {\n // build execution context\n const context: InterceptorContext = {\n signal: streamSignal,\n metadata: new Map(),\n userKey: effectiveUserKey,\n };\n\n // build interceptors\n const interceptors = self._buildInterceptors(executeConfig);\n\n // wrap the function to ensure it returns a promise\n const wrappedFn = async () => {\n const result = await fn(context.signal);\n return result;\n };\n\n // execute the function with interceptors\n const result = await self._executeWithInterceptors(\n wrappedFn as (signal?: AbortSignal) => Promise<T>,\n interceptors,\n context,\n );\n\n // check if result is a generator\n if (self._checkIfGenerator(result)) {\n yield* result;\n } else {\n yield result;\n }\n };\n\n // stream the result to the client\n await this.streamManager.stream(res, asyncWrapperFn, streamConfig);\n }\n\n // single sync execution with interceptors\n protected async execute<T>(\n fn: (signal?: AbortSignal) => Promise<T>,\n options: PluginExecutionSettings,\n userKey?: string,\n ): Promise<T | undefined> {\n const executeConfig = this._buildExecutionConfig(options);\n\n const interceptors = this._buildInterceptors(executeConfig);\n\n // Get user key from context if not provided\n const effectiveUserKey = userKey ?? getCurrentUserId();\n\n const context: InterceptorContext = {\n metadata: new Map(),\n userKey: effectiveUserKey,\n };\n\n try {\n return await this._executeWithInterceptors(fn, interceptors, context);\n } catch (_error) {\n // production-safe, don't crash sdk\n return undefined;\n }\n }\n\n protected registerEndpoint(name: string, path: string): void {\n this.registeredEndpoints[name] = path;\n }\n\n protected route<_TResponse>(\n router: express.Router,\n config: RouteConfig,\n ): void {\n const { name, method, path, handler } = config;\n\n router[method](path, handler);\n\n this.registerEndpoint(name, `/api/${this.name}${path}`);\n }\n\n // build execution options by merging defaults, plugin config, and user overrides\n private _buildExecutionConfig(\n options: PluginExecutionSettings,\n ): PluginExecuteConfig {\n const { default: methodDefaults, user: userOverride } = options;\n\n // Merge: method defaults <- plugin config <- user override (highest priority)\n return deepMerge(\n deepMerge(methodDefaults, this.config),\n userOverride ?? {},\n ) as PluginExecuteConfig;\n }\n\n // build interceptors based on execute options\n private _buildInterceptors(\n options: PluginExecuteConfig,\n ): ExecutionInterceptor[] {\n const interceptors: ExecutionInterceptor[] = [];\n\n // order matters: telemetry → timeout → retry → cache (innermost to outermost)\n\n // Only add telemetry interceptor if traces are enabled\n const telemetryConfig = normalizeTelemetryOptions(this.config.telemetry);\n if (\n telemetryConfig.traces &&\n (options.telemetryInterceptor?.enabled ?? true)\n ) {\n interceptors.push(\n new TelemetryInterceptor(this.telemetry, options.telemetryInterceptor),\n );\n }\n\n if (options.timeout && options.timeout > 0) {\n interceptors.push(new TimeoutInterceptor(options.timeout));\n }\n\n if (\n options.retry?.enabled &&\n options.retry.attempts &&\n options.retry.attempts > 1\n ) {\n interceptors.push(new RetryInterceptor(options.retry));\n }\n\n if (options.cache?.enabled && options.cache.cacheKey?.length) {\n interceptors.push(new CacheInterceptor(this.cache, options.cache));\n }\n\n return interceptors;\n }\n\n // execute method wrapped with interceptors\n private async _executeWithInterceptors<T>(\n fn: (signal?: AbortSignal) => Promise<T>,\n interceptors: ExecutionInterceptor[],\n context: InterceptorContext,\n ): Promise<T> {\n // no interceptors, execute directly\n if (interceptors.length === 0) {\n return fn(context.signal);\n }\n // build nested execution chain from interceptors\n let wrappedFn = () => fn(context.signal);\n\n // wrap each interceptor around the previous function\n for (const interceptor of interceptors) {\n const previousFn = wrappedFn;\n wrappedFn = () => interceptor.intercept(previousFn, context);\n }\n\n return wrappedFn();\n }\n\n private _checkIfGenerator(\n result: any,\n ): result is AsyncGenerator<any, void, unknown> {\n return (\n result && typeof result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":"to-plugin.d.ts","names":[],"sources":["../../src/plugin/to-plugin.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAAwB,iBAAR,QAAQ,CAAA,CAAA,EAAA,CAAA,EAAA,UAAA,MAAA,CAAA,CAAA,MAAA,EACd,CADc,EAAA,IAAA,EAEhB,CAFgB,CAAA,EAGrB,QAHqB,CAGZ,CAHY,EAGT,CAHS,EAGN,CAHM,CAAA"}
|
package/dist/plugin/to-plugin.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-plugin.js","names":[],"sources":["../../src/plugin/to-plugin.ts"],"sourcesContent":["import type { PluginData, ToPlugin } from \"shared\";\n\nexport function toPlugin<T, U, N extends string>(\n plugin: T,\n name: N,\n): ToPlugin<T, U, N> {\n return (config: U = {} as U): PluginData<T, U, N> => ({\n plugin: plugin as T,\n config: config as U,\n name,\n });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-plugin.js","names":[],"sources":["../../src/plugin/to-plugin.ts"],"sourcesContent":["import type { PluginData, ToPlugin } from \"shared\";\n\n/**\n * @internal\n */\nexport function toPlugin<T, U, N extends string>(\n plugin: T,\n name: N,\n): ToPlugin<T, U, N> {\n return (config: U = {} as U): PluginData<T, U, N> => ({\n plugin: plugin as T,\n config: config as U,\n name,\n });\n}\n"],"mappings":";;;;AAKA,SAAgB,SACd,QACA,MACmB;AACnB,SAAQ,SAAY,EAAE,MAAgC;EAC5C;EACA;EACR;EACD"}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/server/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/server/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AA8BA;;;;;;;;;;;;AAAwC,cAA3B,YAAA,SAAqB,MAAA,CAAM;EA2S3B,OAAA,cAGZ,EAAA;IAAA,SAAA,EAAA,OAAA;IAHkB,IAAA,EAAA,MAAA;IAAA,IAAA,EAAA,MAAA;;EAAA,IAAA,EAAA,QAAA;;;;;;oBA9RS;;gBAEZ;sBAEM;;WAaT;;;;;;;;;gBAAA;;;;;;;;;;;;WA0BI,QAAQ,OAAA,CAAQ;;;;;;;;;eA8ClB;;;;;;;;mBAqBI,OAAA,CAAQ;;;;;;;;;;;;;;;;;;;;;;cAgLd,QAAM,gBAAA,cAAA"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { instrumentations } from "../telemetry/instrumentations.js";
|
|
2
2
|
import "../telemetry/index.js";
|
|
3
|
-
import { databricksClientMiddleware } from "../utils/databricks-client-middleware.js";
|
|
4
|
-
import { init_utils } from "../utils/index.js";
|
|
5
3
|
import { Plugin } from "../plugin/plugin.js";
|
|
6
4
|
import { toPlugin } from "../plugin/to-plugin.js";
|
|
7
5
|
import "../plugin/index.js";
|
|
@@ -15,7 +13,6 @@ import dotenv from "dotenv";
|
|
|
15
13
|
import express from "express";
|
|
16
14
|
|
|
17
15
|
//#region src/server/index.ts
|
|
18
|
-
init_utils();
|
|
19
16
|
dotenv.config({ path: path.resolve(process.cwd(), "./.env") });
|
|
20
17
|
/**
|
|
21
18
|
* Server plugin for the AppKit.
|
|
@@ -134,7 +131,6 @@ var ServerPlugin = class ServerPlugin extends Plugin {
|
|
|
134
131
|
if (EXCLUDED_PLUGINS.includes(plugin.name)) continue;
|
|
135
132
|
if (plugin?.injectRoutes && typeof plugin.injectRoutes === "function") {
|
|
136
133
|
const router = express.Router();
|
|
137
|
-
if (plugin.requiresDatabricksClient) router.use(await databricksClientMiddleware());
|
|
138
134
|
plugin.injectRoutes(router);
|
|
139
135
|
const basePath = `/api/${plugin.name}`;
|
|
140
136
|
this.serverApplication.use(basePath, router);
|
|
@@ -217,6 +213,9 @@ var ServerPlugin = class ServerPlugin extends Plugin {
|
|
|
217
213
|
}
|
|
218
214
|
};
|
|
219
215
|
const EXCLUDED_PLUGINS = [ServerPlugin.name];
|
|
216
|
+
/**
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
220
219
|
const server = toPlugin(ServerPlugin, "server");
|
|
221
220
|
|
|
222
221
|
//#endregion
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["server","endpoints: PluginEndpoints"],"sources":["../../src/server/index.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport type { Server as HTTPServer } from \"node:http\";\nimport path from \"node:path\";\nimport dotenv from \"dotenv\";\nimport express from \"express\";\nimport type { PluginPhase } from \"shared\";\nimport { Plugin, toPlugin } from \"../plugin\";\nimport { instrumentations } from \"../telemetry\";\nimport { databricksClientMiddleware } from \"../utils\";\nimport { RemoteTunnelController } from \"./remote-tunnel/remote-tunnel-controller\";\nimport { StaticServer } from \"./static-server\";\nimport type { ServerConfig } from \"./types\";\nimport { type PluginEndpoints, getRoutes } from \"./utils\";\nimport { ViteDevServer } from \"./vite-dev-server\";\n\ndotenv.config({ path: path.resolve(process.cwd(), \"./.env\") });\n\n/**\n * Server plugin for the AppKit.\n *\n * This plugin is responsible for starting the server and serving the static files.\n * It also handles the remote tunneling for development purposes.\n *\n * @example\n * ```ts\n * createApp({\n * plugins: [server(), telemetryExamples(), analytics({})],\n * });\n * ```\n *\n */\nexport class ServerPlugin extends Plugin {\n public static DEFAULT_CONFIG = {\n autoStart: true,\n host: process.env.FLASK_RUN_HOST || \"0.0.0.0\",\n port: Number(process.env.DATABRICKS_APP_PORT) || 8000,\n };\n\n public name = \"server\" as const;\n public envVars: string[] = [];\n private serverApplication: express.Application;\n private server: HTTPServer | null;\n private viteDevServer?: ViteDevServer;\n private remoteTunnelController?: RemoteTunnelController;\n protected declare config: ServerConfig;\n private serverExtensions: ((app: express.Application) => void)[] = [];\n static phase: PluginPhase = \"deferred\";\n\n constructor(config: ServerConfig) {\n super(config);\n this.config = config;\n this.serverApplication = express();\n this.server = null;\n this.serverExtensions = [];\n this.telemetry.registerInstrumentations([\n instrumentations.http,\n instrumentations.express,\n ]);\n }\n\n /** Setup the server plugin. */\n async setup() {\n if (this.shouldAutoStart()) {\n await this.start();\n }\n }\n\n /** Get the server configuration. */\n getConfig() {\n const { plugins: _plugins, ...config } = this.config;\n\n return config;\n }\n\n /** Check if the server should auto start. */\n shouldAutoStart() {\n return this.config.autoStart;\n }\n\n /**\n * Start the server.\n *\n * This method starts the server and sets up the frontend.\n * It also sets up the remote tunneling if enabled.\n *\n * @returns The express application.\n */\n async start(): Promise<express.Application> {\n this.serverApplication.use(express.json());\n\n const endpoints = await this.extendRoutes();\n\n for (const extension of this.serverExtensions) {\n extension(this.serverApplication);\n }\n\n // register remote tunnel controller (before static/vite)\n this.remoteTunnelController = new RemoteTunnelController(\n this.devFileReader,\n );\n this.serverApplication.use(this.remoteTunnelController.middleware);\n\n await this.setupFrontend(endpoints);\n\n const server = this.serverApplication.listen(\n this.config.port ?? ServerPlugin.DEFAULT_CONFIG.port,\n this.config.host ?? ServerPlugin.DEFAULT_CONFIG.host,\n () => this.logStartupInfo(),\n );\n\n this.server = server;\n\n // attach server to remote tunnel controller\n this.remoteTunnelController.setServer(server);\n\n process.on(\"SIGTERM\", () => this._gracefulShutdown());\n process.on(\"SIGINT\", () => this._gracefulShutdown());\n\n if (process.env.NODE_ENV === \"development\") {\n const allRoutes = getRoutes(this.serverApplication._router.stack);\n console.dir(allRoutes, { depth: null });\n }\n return this.serverApplication;\n }\n\n /**\n * Get the low level node.js http server instance.\n *\n * Only use this method if you need to access the server instance for advanced usage like a custom websocket server, etc.\n *\n * @throws {Error} If the server is not started or autoStart is true.\n * @returns {HTTPServer} The server instance.\n */\n getServer(): HTTPServer {\n if (this.shouldAutoStart()) {\n throw new Error(\"Cannot get server when autoStart is true.\");\n }\n\n if (!this.server) {\n throw new Error(\n \"Server not started. Please start the server first by calling the start() method.\",\n );\n }\n\n return this.server;\n }\n\n /**\n * Extend the server with custom routes or middleware.\n *\n * @param fn - A function that receives the express application.\n * @returns The server plugin instance for chaining.\n * @throws {Error} If autoStart is true.\n */\n extend(fn: (app: express.Application) => void) {\n if (this.shouldAutoStart()) {\n throw new Error(\"Cannot extend server when autoStart is true.\");\n }\n\n this.serverExtensions.push(fn);\n return this;\n }\n\n /**\n * Setup the routes with the plugins.\n *\n * This method goes through all the plugins and injects the routes into the server application.\n * Returns a map of plugin names to their registered named endpoints.\n */\n private async extendRoutes(): Promise<PluginEndpoints> {\n const endpoints: PluginEndpoints = {};\n\n if (!this.config.plugins) return endpoints;\n\n this.serverApplication.get(\"/health\", (_, res) => {\n res.status(200).json({ status: \"ok\" });\n });\n this.registerEndpoint(\"health\", \"/health\");\n\n for (const plugin of Object.values(this.config.plugins)) {\n if (EXCLUDED_PLUGINS.includes(plugin.name)) continue;\n\n if (plugin?.injectRoutes && typeof plugin.injectRoutes === \"function\") {\n const router = express.Router();\n\n // add databricks client middleware to the router if the plugin needs the request context\n if (plugin.requiresDatabricksClient)\n router.use(await databricksClientMiddleware());\n\n plugin.injectRoutes(router);\n\n const basePath = `/api/${plugin.name}`;\n this.serverApplication.use(basePath, router);\n\n // Collect named endpoints from the plugin\n endpoints[plugin.name] = plugin.getEndpoints();\n }\n }\n\n return endpoints;\n }\n\n /**\n * Setup frontend serving based on environment:\n * - If staticPath is explicitly provided: use static server\n * - Dev mode (no staticPath): Vite for HMR\n * - Production (no staticPath): Static files auto-detected\n */\n private async setupFrontend(endpoints: PluginEndpoints) {\n const isDev = process.env.NODE_ENV === \"development\";\n const hasExplicitStaticPath = this.config.staticPath !== undefined;\n\n // explict static path provided\n if (hasExplicitStaticPath) {\n const staticServer = new StaticServer(\n this.serverApplication,\n this.config.staticPath as string,\n endpoints,\n );\n staticServer.setup();\n return;\n }\n\n // auto-detection based on environment\n if (isDev) {\n this.viteDevServer = new ViteDevServer(this.serverApplication, endpoints);\n await this.viteDevServer.setup();\n return;\n }\n\n // auto-detection based on static path\n const staticPath = ServerPlugin.findStaticPath();\n if (staticPath) {\n const staticServer = new StaticServer(\n this.serverApplication,\n staticPath,\n endpoints,\n );\n\n staticServer.setup();\n }\n }\n\n private static findStaticPath() {\n const staticPaths = [\"dist\", \"client/dist\", \"build\", \"public\", \"out\"];\n const cwd = process.cwd();\n for (const p of staticPaths) {\n const fullPath = path.resolve(cwd, p);\n if (fs.existsSync(path.resolve(fullPath, \"index.html\"))) {\n console.log(`Static files: serving from ${fullPath}`);\n return fullPath;\n }\n }\n return undefined;\n }\n\n private logStartupInfo() {\n const isDev = process.env.NODE_ENV === \"development\";\n const hasExplicitStaticPath = this.config.staticPath !== undefined;\n const port = this.config.port ?? ServerPlugin.DEFAULT_CONFIG.port;\n const host = this.config.host ?? ServerPlugin.DEFAULT_CONFIG.host;\n\n console.log(`Server running on http://${host}:${port}`);\n\n if (hasExplicitStaticPath) {\n console.log(`Mode: static (${this.config.staticPath})`);\n } else if (isDev) {\n console.log(\"Mode: development (Vite HMR)\");\n } else {\n console.log(\"Mode: production (static)\");\n }\n\n const remoteServerController = this.remoteTunnelController;\n if (!remoteServerController) {\n console.log(\"Remote tunnel: disabled (controller not initialized)\");\n } else {\n console.log(\n `Remote tunnel: ${\n remoteServerController.isAllowedByEnv() ? \"allowed\" : \"blocked\"\n }; ${remoteServerController.isActive() ? \"active\" : \"inactive\"}`,\n );\n }\n }\n\n private async _gracefulShutdown() {\n console.log(\"Starting graceful shutdown...\");\n\n if (this.viteDevServer) {\n await this.viteDevServer.close();\n }\n\n if (this.remoteTunnelController) {\n this.remoteTunnelController.cleanup();\n }\n\n // 1. abort active operations from plugins\n if (this.config.plugins) {\n for (const plugin of Object.values(this.config.plugins)) {\n if (plugin.abortActiveOperations) {\n try {\n plugin.abortActiveOperations();\n } catch (err) {\n console.error(\n `Error aborting operations for plugin ${plugin.name}:`,\n err,\n );\n }\n }\n }\n }\n\n // 2. close the server\n if (this.server) {\n this.server.close(() => {\n console.log(\"Server closed gracefully\");\n process.exit(0);\n });\n\n // 3. timeout to force shutdown after 15 seconds\n setTimeout(() => {\n console.log(\"Force shutdown after timeout\");\n process.exit(1);\n }, 15000);\n } else {\n process.exit(0);\n }\n }\n}\n\nconst EXCLUDED_PLUGINS = [ServerPlugin.name];\n\nexport const server = toPlugin<typeof ServerPlugin, ServerConfig, \"server\">(\n ServerPlugin,\n \"server\",\n);\n"],"mappings":";;;;;;;;;;;;;;;;;YAQsD;AAOtD,OAAO,OAAO,EAAE,MAAM,KAAK,QAAQ,QAAQ,KAAK,EAAE,SAAS,EAAE,CAAC;;;;;;;;;;;;;;;AAgB9D,IAAa,eAAb,MAAa,qBAAqB,OAAO;;wBACR;GAC7B,WAAW;GACX,MAAM,QAAQ,IAAI,kBAAkB;GACpC,MAAM,OAAO,QAAQ,IAAI,oBAAoB,IAAI;GAClD;;;eAU2B;;CAE5B,YAAY,QAAsB;AAChC,QAAM,OAAO;cAXD;iBACa,EAAE;0BAMsC,EAAE;AAKnE,OAAK,SAAS;AACd,OAAK,oBAAoB,SAAS;AAClC,OAAK,SAAS;AACd,OAAK,mBAAmB,EAAE;AAC1B,OAAK,UAAU,yBAAyB,CACtC,iBAAiB,MACjB,iBAAiB,QAClB,CAAC;;;CAIJ,MAAM,QAAQ;AACZ,MAAI,KAAK,iBAAiB,CACxB,OAAM,KAAK,OAAO;;;CAKtB,YAAY;EACV,MAAM,EAAE,SAAS,UAAU,GAAG,WAAW,KAAK;AAE9C,SAAO;;;CAIT,kBAAkB;AAChB,SAAO,KAAK,OAAO;;;;;;;;;;CAWrB,MAAM,QAAsC;AAC1C,OAAK,kBAAkB,IAAI,QAAQ,MAAM,CAAC;EAE1C,MAAM,YAAY,MAAM,KAAK,cAAc;AAE3C,OAAK,MAAM,aAAa,KAAK,iBAC3B,WAAU,KAAK,kBAAkB;AAInC,OAAK,yBAAyB,IAAI,uBAChC,KAAK,cACN;AACD,OAAK,kBAAkB,IAAI,KAAK,uBAAuB,WAAW;AAElE,QAAM,KAAK,cAAc,UAAU;EAEnC,M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AK;AACpC,MAAI,CAAC,uBACH,SAAQ,IAAI,uDAAuD;MAEnE,SAAQ,IACN,kBACE,uBAAuB,gBAAgB,GAAG,YAAY,UACvD,IAAI,uBAAuB,UAAU,GAAG,WAAW,aACrD;;CAIL,MAAc,oBAAoB;AAChC,UAAQ,IAAI,gCAAgC;AAE5C,MAAI,KAAK,cACP,OAAM,KAAK,cAAc,OAAO;AAGlC,MAAI,KAAK,uBACP,MAAK,uBAAuB,SAAS;AAIvC,MAAI,KAAK,OAAO,SACd;QAAK,MAAM,UAAU,OAAO,OAAO,KAAK,OAAO,QAAQ,CACrD,KAAI,OAAO,sBACT,KAAI;AACF,WAAO,uBAAuB;YACvB,KAAK;AACZ,YAAQ,MACN,wCAAwC,OAAO,KAAK,IACpD,IACD;;;AAOT,MAAI,KAAK,QAAQ;AACf,QAAK,OAAO,YAAY;AACtB,YAAQ,IAAI,2BAA2B;AACvC,YAAQ,KAAK,EAAE;KACf;AAGF,oBAAiB;AACf,YAAQ,IAAI,+BAA+B;AAC3C,YAAQ,KAAK,EAAE;MACd,KAAM;QAET,SAAQ,KAAK,EAAE;;;AAKrB,MAAM,mBAAmB,CAAC,aAAa,KAAK;AAE5C,MAAa,SAAS,SACpB,cACA,SACD"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["server"],"sources":["../../src/server/index.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport type { Server as HTTPServer } from \"node:http\";\nimport path from \"node:path\";\nimport dotenv from \"dotenv\";\nimport express from \"express\";\nimport type { PluginPhase } from \"shared\";\nimport { Plugin, toPlugin } from \"../plugin\";\nimport { instrumentations } from \"../telemetry\";\nimport { RemoteTunnelController } from \"./remote-tunnel/remote-tunnel-controller\";\nimport { StaticServer } from \"./static-server\";\nimport type { ServerConfig } from \"./types\";\nimport { type PluginEndpoints, getRoutes } from \"./utils\";\nimport { ViteDevServer } from \"./vite-dev-server\";\n\ndotenv.config({ path: path.resolve(process.cwd(), \"./.env\") });\n\n/**\n * Server plugin for the AppKit.\n *\n * This plugin is responsible for starting the server and serving the static files.\n * It also handles the remote tunneling for development purposes.\n *\n * @example\n * ```ts\n * createApp({\n * plugins: [server(), telemetryExamples(), analytics({})],\n * });\n * ```\n *\n */\nexport class ServerPlugin extends Plugin {\n public static DEFAULT_CONFIG = {\n autoStart: true,\n host: process.env.FLASK_RUN_HOST || \"0.0.0.0\",\n port: Number(process.env.DATABRICKS_APP_PORT) || 8000,\n };\n\n public name = \"server\" as const;\n public envVars: string[] = [];\n private serverApplication: express.Application;\n private server: HTTPServer | null;\n private viteDevServer?: ViteDevServer;\n private remoteTunnelController?: RemoteTunnelController;\n protected declare config: ServerConfig;\n private serverExtensions: ((app: express.Application) => void)[] = [];\n static phase: PluginPhase = \"deferred\";\n\n constructor(config: ServerConfig) {\n super(config);\n this.config = config;\n this.serverApplication = express();\n this.server = null;\n this.serverExtensions = [];\n this.telemetry.registerInstrumentations([\n instrumentations.http,\n instrumentations.express,\n ]);\n }\n\n /** Setup the server plugin. */\n async setup() {\n if (this.shouldAutoStart()) {\n await this.start();\n }\n }\n\n /** Get the server configuration. */\n getConfig() {\n const { plugins: _plugins, ...config } = this.config;\n\n return config;\n }\n\n /** Check if the server should auto start. */\n shouldAutoStart() {\n return this.config.autoStart;\n }\n\n /**\n * Start the server.\n *\n * This method starts the server and sets up the frontend.\n * It also sets up the remote tunneling if enabled.\n *\n * @returns The express application.\n */\n async start(): Promise<express.Application> {\n this.serverApplication.use(express.json());\n\n const endpoints = await this.extendRoutes();\n\n for (const extension of this.serverExtensions) {\n extension(this.serverApplication);\n }\n\n // register remote tunnel controller (before static/vite)\n this.remoteTunnelController = new RemoteTunnelController(\n this.devFileReader,\n );\n this.serverApplication.use(this.remoteTunnelController.middleware);\n\n await this.setupFrontend(endpoints);\n\n const server = this.serverApplication.listen(\n this.config.port ?? ServerPlugin.DEFAULT_CONFIG.port,\n this.config.host ?? ServerPlugin.DEFAULT_CONFIG.host,\n () => this.logStartupInfo(),\n );\n\n this.server = server;\n\n // attach server to remote tunnel controller\n this.remoteTunnelController.setServer(server);\n\n process.on(\"SIGTERM\", () => this._gracefulShutdown());\n process.on(\"SIGINT\", () => this._gracefulShutdown());\n\n if (process.env.NODE_ENV === \"development\") {\n const allRoutes = getRoutes(this.serverApplication._router.stack);\n console.dir(allRoutes, { depth: null });\n }\n return this.serverApplication;\n }\n\n /**\n * Get the low level node.js http server instance.\n *\n * Only use this method if you need to access the server instance for advanced usage like a custom websocket server, etc.\n *\n * @throws {Error} If the server is not started or autoStart is true.\n * @returns {HTTPServer} The server instance.\n */\n getServer(): HTTPServer {\n if (this.shouldAutoStart()) {\n throw new Error(\"Cannot get server when autoStart is true.\");\n }\n\n if (!this.server) {\n throw new Error(\n \"Server not started. Please start the server first by calling the start() method.\",\n );\n }\n\n return this.server;\n }\n\n /**\n * Extend the server with custom routes or middleware.\n *\n * @param fn - A function that receives the express application.\n * @returns The server plugin instance for chaining.\n * @throws {Error} If autoStart is true.\n */\n extend(fn: (app: express.Application) => void) {\n if (this.shouldAutoStart()) {\n throw new Error(\"Cannot extend server when autoStart is true.\");\n }\n\n this.serverExtensions.push(fn);\n return this;\n }\n\n /**\n * Setup the routes with the plugins.\n *\n * This method goes through all the plugins and injects the routes into the server application.\n * Returns a map of plugin names to their registered named endpoints.\n */\n private async extendRoutes(): Promise<PluginEndpoints> {\n const endpoints: PluginEndpoints = {};\n\n if (!this.config.plugins) return endpoints;\n\n this.serverApplication.get(\"/health\", (_, res) => {\n res.status(200).json({ status: \"ok\" });\n });\n this.registerEndpoint(\"health\", \"/health\");\n\n for (const plugin of Object.values(this.config.plugins)) {\n if (EXCLUDED_PLUGINS.includes(plugin.name)) continue;\n\n if (plugin?.injectRoutes && typeof plugin.injectRoutes === \"function\") {\n const router = express.Router();\n\n plugin.injectRoutes(router);\n\n const basePath = `/api/${plugin.name}`;\n this.serverApplication.use(basePath, router);\n\n // Collect named endpoints from the plugin\n endpoints[plugin.name] = plugin.getEndpoints();\n }\n }\n\n return endpoints;\n }\n\n /**\n * Setup frontend serving based on environment:\n * - If staticPath is explicitly provided: use static server\n * - Dev mode (no staticPath): Vite for HMR\n * - Production (no staticPath): Static files auto-detected\n */\n private async setupFrontend(endpoints: PluginEndpoints) {\n const isDev = process.env.NODE_ENV === \"development\";\n const hasExplicitStaticPath = this.config.staticPath !== undefined;\n\n // explict static path provided\n if (hasExplicitStaticPath) {\n const staticServer = new StaticServer(\n this.serverApplication,\n this.config.staticPath as string,\n endpoints,\n );\n staticServer.setup();\n return;\n }\n\n // auto-detection based on environment\n if (isDev) {\n this.viteDevServer = new ViteDevServer(this.serverApplication, endpoints);\n await this.viteDevServer.setup();\n return;\n }\n\n // auto-detection based on static path\n const staticPath = ServerPlugin.findStaticPath();\n if (staticPath) {\n const staticServer = new StaticServer(\n this.serverApplication,\n staticPath,\n endpoints,\n );\n\n staticServer.setup();\n }\n }\n\n private static findStaticPath() {\n const staticPaths = [\"dist\", \"client/dist\", \"build\", \"public\", \"out\"];\n const cwd = process.cwd();\n for (const p of staticPaths) {\n const fullPath = path.resolve(cwd, p);\n if (fs.existsSync(path.resolve(fullPath, \"index.html\"))) {\n console.log(`Static files: serving from ${fullPath}`);\n return fullPath;\n }\n }\n return undefined;\n }\n\n private logStartupInfo() {\n const isDev = process.env.NODE_ENV === \"development\";\n const hasExplicitStaticPath = this.config.staticPath !== undefined;\n const port = this.config.port ?? ServerPlugin.DEFAULT_CONFIG.port;\n const host = this.config.host ?? ServerPlugin.DEFAULT_CONFIG.host;\n\n console.log(`Server running on http://${host}:${port}`);\n\n if (hasExplicitStaticPath) {\n console.log(`Mode: static (${this.config.staticPath})`);\n } else if (isDev) {\n console.log(\"Mode: development (Vite HMR)\");\n } else {\n console.log(\"Mode: production (static)\");\n }\n\n const remoteServerController = this.remoteTunnelController;\n if (!remoteServerController) {\n console.log(\"Remote tunnel: disabled (controller not initialized)\");\n } else {\n console.log(\n `Remote tunnel: ${\n remoteServerController.isAllowedByEnv() ? \"allowed\" : \"blocked\"\n }; ${remoteServerController.isActive() ? \"active\" : \"inactive\"}`,\n );\n }\n }\n\n private async _gracefulShutdown() {\n console.log(\"Starting graceful shutdown...\");\n\n if (this.viteDevServer) {\n await this.viteDevServer.close();\n }\n\n if (this.remoteTunnelController) {\n this.remoteTunnelController.cleanup();\n }\n\n // 1. abort active operations from plugins\n if (this.config.plugins) {\n for (const plugin of Object.values(this.config.plugins)) {\n if (plugin.abortActiveOperations) {\n try {\n plugin.abortActiveOperations();\n } catch (err) {\n console.error(\n `Error aborting operations for plugin ${plugin.name}:`,\n err,\n );\n }\n }\n }\n }\n\n // 2. close the server\n if (this.server) {\n this.server.close(() => {\n console.log(\"Server closed gracefully\");\n process.exit(0);\n });\n\n // 3. timeout to force shutdown after 15 seconds\n setTimeout(() => {\n console.log(\"Force shutdown after timeout\");\n process.exit(1);\n }, 15000);\n } else {\n process.exit(0);\n }\n }\n}\n\nconst EXCLUDED_PLUGINS = [ServerPlugin.name];\n\n/**\n * @internal\n */\nexport const server = toPlugin<typeof ServerPlugin, ServerConfig, \"server\">(\n ServerPlugin,\n \"server\",\n);\n"],"mappings":";;;;;;;;;;;;;;;AAcA,OAAO,OAAO,EAAE,MAAM,KAAK,QAAQ,QAAQ,KAAK,EAAE,SAAS,EAAE,CAAC;;;;;;;;;;;;;;;AAgB9D,IAAa,eAAb,MAAa,qBAAqB,OAAO;;wBACR;GAC7B,WAAW;GACX,MAAM,QAAQ,IAAI,kBAAkB;GACpC,MAAM,OAAO,QAAQ,IAAI,oBAAoB,IAAI;GAClD;;;eAU2B;;CAE5B,YAAY,QAAsB;AAChC,QAAM,OAAO;cAXD;iBACa,EAAE;0BAMsC,EAAE;AAKnE,OAAK,SAAS;AACd,OAAK,oBAAoB,SAAS;AAClC,OAAK,SAAS;AACd,OAAK,mBAAmB,EAAE;AAC1B,OAAK,UAAU,yBAAyB,CACtC,iBAAiB,MACjB,iBAAiB,QAClB,CAAC;;;CAIJ,MAAM,QAAQ;AACZ,MAAI,KAAK,i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