@databricks/appkit 0.2.0 → 0.4.0
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/CLAUDE.md +121 -1228
- package/NOTICE.md +1 -1
- package/bin/appkit.js +3 -0
- package/dist/analytics/analytics.d.ts.map +1 -1
- package/dist/analytics/analytics.js +17 -30
- package/dist/analytics/analytics.js.map +1 -1
- package/dist/app/index.d.ts +5 -1
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +38 -9
- package/dist/app/index.js.map +1 -1
- package/dist/appkit/package.js +1 -1
- package/dist/cache/index.js +3 -3
- package/dist/cache/index.js.map +1 -1
- package/dist/cli/commands/docs.js +47 -0
- package/dist/cli/commands/docs.js.map +1 -0
- package/dist/cli/commands/generate-types.js +38 -0
- package/dist/cli/commands/generate-types.js.map +1 -0
- package/dist/cli/commands/lint.js +104 -0
- package/dist/cli/commands/lint.js.map +1 -0
- package/dist/cli/commands/setup.js +121 -0
- package/dist/cli/commands/setup.js.map +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +24 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.js.map +1 -1
- package/dist/plugin/plugin.d.ts +1 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/plugin/plugin.js +1 -0
- package/dist/plugin/plugin.js.map +1 -1
- package/dist/server/remote-tunnel/remote-tunnel-manager.js +9 -9
- package/dist/server/remote-tunnel/remote-tunnel-manager.js.map +1 -1
- package/dist/server/utils.js +6 -6
- package/dist/server/utils.js.map +1 -1
- package/dist/shared/src/execute.d.ts +1 -0
- package/dist/shared/src/execute.d.ts.map +1 -1
- package/dist/shared/src/plugin.d.ts +3 -0
- package/dist/shared/src/plugin.d.ts.map +1 -1
- package/dist/telemetry/types.d.ts +1 -0
- package/dist/telemetry/types.d.ts.map +1 -1
- package/dist/type-generator/index.js +1 -1
- package/dist/type-generator/index.js.map +1 -1
- package/dist/type-generator/query-registry.js +9 -1
- package/dist/type-generator/query-registry.js.map +1 -1
- package/docs/docs/api/appkit/Class.AppKitError/index.html +77 -0
- package/docs/docs/api/appkit/Class.AppKitError.md +154 -0
- package/docs/docs/api/appkit/Class.AuthenticationError/index.html +110 -0
- package/docs/docs/api/appkit/Class.AuthenticationError.md +236 -0
- package/docs/docs/api/appkit/Class.ConfigurationError/index.html +112 -0
- package/docs/docs/api/appkit/Class.ConfigurationError.md +243 -0
- package/docs/docs/api/appkit/Class.ConnectionError/index.html +120 -0
- package/docs/docs/api/appkit/Class.ConnectionError.md +265 -0
- package/docs/docs/api/appkit/Class.ExecutionError/index.html +116 -0
- package/docs/docs/api/appkit/Class.ExecutionError.md +250 -0
- package/docs/docs/api/appkit/Class.InitializationError/index.html +104 -0
- package/docs/docs/api/appkit/Class.InitializationError.md +222 -0
- package/docs/docs/api/appkit/Class.Plugin/index.html +149 -0
- package/docs/docs/api/appkit/Class.Plugin.md +392 -0
- package/docs/docs/api/appkit/Class.ServerError/index.html +108 -0
- package/docs/docs/api/appkit/Class.ServerError.md +229 -0
- package/docs/docs/api/appkit/Class.TunnelError/index.html +108 -0
- package/docs/docs/api/appkit/Class.TunnelError.md +231 -0
- package/docs/docs/api/appkit/Class.ValidationError/index.html +106 -0
- package/docs/docs/api/appkit/Class.ValidationError.md +225 -0
- package/docs/docs/api/appkit/Function.appKitTypesPlugin/index.html +24 -0
- package/docs/docs/api/appkit/Function.appKitTypesPlugin.md +20 -0
- package/docs/docs/api/appkit/Function.createApp/index.html +24 -0
- package/docs/docs/api/appkit/Function.createApp.md +31 -0
- package/docs/docs/api/appkit/Function.isSQLTypeMarker/index.html +25 -0
- package/docs/docs/api/appkit/Function.isSQLTypeMarker.md +32 -0
- package/docs/docs/api/appkit/Interface.BasePluginConfig/index.html +28 -0
- package/docs/docs/api/appkit/Interface.BasePluginConfig.md +37 -0
- package/docs/docs/api/appkit/Interface.CacheConfig/index.html +63 -0
- package/docs/docs/api/appkit/Interface.CacheConfig.md +131 -0
- package/docs/docs/api/appkit/Interface.ITelemetry/index.html +73 -0
- package/docs/docs/api/appkit/Interface.ITelemetry.md +144 -0
- package/docs/docs/api/appkit/Interface.StreamExecutionSettings/index.html +26 -0
- package/docs/docs/api/appkit/Interface.StreamExecutionSettings.md +30 -0
- package/docs/docs/api/appkit/Interface.TelemetryConfig/index.html +32 -0
- package/docs/docs/api/appkit/Interface.TelemetryConfig.md +48 -0
- package/docs/docs/api/appkit/TypeAlias.IAppRouter/index.html +18 -0
- package/docs/docs/api/appkit/TypeAlias.IAppRouter.md +8 -0
- package/docs/docs/api/appkit/Variable.sql/index.html +98 -0
- package/docs/docs/api/appkit/Variable.sql.md +260 -0
- package/docs/docs/api/appkit/index.html +28 -0
- package/docs/docs/api/appkit-ui/data/AreaChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/AreaChart.md +79 -0
- package/docs/docs/api/appkit-ui/data/BarChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/BarChart.md +74 -0
- package/docs/docs/api/appkit-ui/data/DataTable/index.html +36 -0
- package/docs/docs/api/appkit-ui/data/DataTable.md +69 -0
- package/docs/docs/api/appkit-ui/data/DonutChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/DonutChart.md +72 -0
- package/docs/docs/api/appkit-ui/data/HeatmapChart/index.html +35 -0
- package/docs/docs/api/appkit-ui/data/HeatmapChart.md +91 -0
- package/docs/docs/api/appkit-ui/data/LineChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/LineChart.md +77 -0
- package/docs/docs/api/appkit-ui/data/PieChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/PieChart.md +72 -0
- package/docs/docs/api/appkit-ui/data/RadarChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/RadarChart.md +74 -0
- package/docs/docs/api/appkit-ui/data/ScatterChart/index.html +29 -0
- package/docs/docs/api/appkit-ui/data/ScatterChart.md +76 -0
- package/docs/docs/api/appkit-ui/index.html +23 -0
- package/docs/docs/api/appkit-ui/styling/index.html +74 -0
- package/docs/docs/api/appkit-ui/styling.md +81 -0
- package/docs/docs/api/appkit-ui/ui/Accordion/index.html +48 -0
- package/docs/docs/api/appkit-ui/ui/Accordion.md +139 -0
- package/docs/docs/api/appkit-ui/ui/Alert/index.html +41 -0
- package/docs/docs/api/appkit-ui/ui/Alert.md +89 -0
- package/docs/docs/api/appkit-ui/ui/AlertDialog/index.html +97 -0
- package/docs/docs/api/appkit-ui/ui/AlertDialog.md +282 -0
- package/docs/docs/api/appkit-ui/ui/AspectRatio/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/AspectRatio.md +46 -0
- package/docs/docs/api/appkit-ui/ui/Avatar/index.html +41 -0
- package/docs/docs/api/appkit-ui/ui/Avatar.md +90 -0
- package/docs/docs/api/appkit-ui/ui/Badge/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Badge.md +38 -0
- package/docs/docs/api/appkit-ui/ui/Breadcrumb/index.html +69 -0
- package/docs/docs/api/appkit-ui/ui/Breadcrumb.md +193 -0
- package/docs/docs/api/appkit-ui/ui/Button/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Button.md +39 -0
- package/docs/docs/api/appkit-ui/ui/ButtonGroup/index.html +38 -0
- package/docs/docs/api/appkit-ui/ui/ButtonGroup.md +68 -0
- package/docs/docs/api/appkit-ui/ui/Calendar/index.html +34 -0
- package/docs/docs/api/appkit-ui/ui/Calendar.md +154 -0
- package/docs/docs/api/appkit-ui/ui/Card/index.html +69 -0
- package/docs/docs/api/appkit-ui/ui/Card.md +222 -0
- package/docs/docs/api/appkit-ui/ui/Carousel/index.html +55 -0
- package/docs/docs/api/appkit-ui/ui/Carousel.md +152 -0
- package/docs/docs/api/appkit-ui/ui/ChartContainer/index.html +58 -0
- package/docs/docs/api/appkit-ui/ui/ChartContainer.md +343 -0
- package/docs/docs/api/appkit-ui/ui/Checkbox/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Checkbox.md +53 -0
- package/docs/docs/api/appkit-ui/ui/Collapsible/index.html +41 -0
- package/docs/docs/api/appkit-ui/ui/Collapsible.md +125 -0
- package/docs/docs/api/appkit-ui/ui/Command/index.html +83 -0
- package/docs/docs/api/appkit-ui/ui/Command.md +287 -0
- package/docs/docs/api/appkit-ui/ui/ContextMenu/index.html +111 -0
- package/docs/docs/api/appkit-ui/ui/ContextMenu.md +419 -0
- package/docs/docs/api/appkit-ui/ui/Dialog/index.html +90 -0
- package/docs/docs/api/appkit-ui/ui/Dialog.md +285 -0
- package/docs/docs/api/appkit-ui/ui/Drawer/index.html +90 -0
- package/docs/docs/api/appkit-ui/ui/Drawer.md +387 -0
- package/docs/docs/api/appkit-ui/ui/DropdownMenu/index.html +111 -0
- package/docs/docs/api/appkit-ui/ui/DropdownMenu.md +478 -0
- package/docs/docs/api/appkit-ui/ui/Empty/index.html +54 -0
- package/docs/docs/api/appkit-ui/ui/Empty.md +109 -0
- package/docs/docs/api/appkit-ui/ui/Field/index.html +87 -0
- package/docs/docs/api/appkit-ui/ui/Field.md +201 -0
- package/docs/docs/api/appkit-ui/ui/FormControl/index.html +59 -0
- package/docs/docs/api/appkit-ui/ui/FormControl.md +128 -0
- package/docs/docs/api/appkit-ui/ui/HoverCard/index.html +39 -0
- package/docs/docs/api/appkit-ui/ui/HoverCard.md +131 -0
- package/docs/docs/api/appkit-ui/ui/Input/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Input.md +35 -0
- package/docs/docs/api/appkit-ui/ui/InputGroup/index.html +59 -0
- package/docs/docs/api/appkit-ui/ui/InputGroup.md +123 -0
- package/docs/docs/api/appkit-ui/ui/InputOTP/index.html +48 -0
- package/docs/docs/api/appkit-ui/ui/InputOTP.md +124 -0
- package/docs/docs/api/appkit-ui/ui/Item/index.html +78 -0
- package/docs/docs/api/appkit-ui/ui/Item.md +185 -0
- package/docs/docs/api/appkit-ui/ui/Kbd/index.html +30 -0
- package/docs/docs/api/appkit-ui/ui/Kbd.md +39 -0
- package/docs/docs/api/appkit-ui/ui/Label/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Label.md +44 -0
- package/docs/docs/api/appkit-ui/ui/Menubar/index.html +117 -0
- package/docs/docs/api/appkit-ui/ui/Menubar.md +484 -0
- package/docs/docs/api/appkit-ui/ui/NavigationMenu/index.html +76 -0
- package/docs/docs/api/appkit-ui/ui/NavigationMenu.md +338 -0
- package/docs/docs/api/appkit-ui/ui/Pagination/index.html +69 -0
- package/docs/docs/api/appkit-ui/ui/Pagination.md +191 -0
- package/docs/docs/api/appkit-ui/ui/Popover/index.html +45 -0
- package/docs/docs/api/appkit-ui/ui/Popover.md +173 -0
- package/docs/docs/api/appkit-ui/ui/Progress/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Progress.md +51 -0
- package/docs/docs/api/appkit-ui/ui/RadioGroup/index.html +33 -0
- package/docs/docs/api/appkit-ui/ui/RadioGroup.md +83 -0
- package/docs/docs/api/appkit-ui/ui/ResizableHandle/index.html +41 -0
- package/docs/docs/api/appkit-ui/ui/ResizableHandle.md +136 -0
- package/docs/docs/api/appkit-ui/ui/ScrollArea/index.html +34 -0
- package/docs/docs/api/appkit-ui/ui/ScrollArea.md +83 -0
- package/docs/docs/api/appkit-ui/ui/Select/index.html +82 -0
- package/docs/docs/api/appkit-ui/ui/Select.md +267 -0
- package/docs/docs/api/appkit-ui/ui/Separator/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Separator.md +56 -0
- package/docs/docs/api/appkit-ui/ui/Sheet/index.html +76 -0
- package/docs/docs/api/appkit-ui/ui/Sheet.md +236 -0
- package/docs/docs/api/appkit-ui/ui/Sidebar/index.html +183 -0
- package/docs/docs/api/appkit-ui/ui/Sidebar.md +490 -0
- package/docs/docs/api/appkit-ui/ui/Skeleton/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Skeleton.md +43 -0
- package/docs/docs/api/appkit-ui/ui/Slider/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Slider.md +61 -0
- package/docs/docs/api/appkit-ui/ui/Spinner/index.html +24 -0
- package/docs/docs/api/appkit-ui/ui/Spinner.md +22 -0
- package/docs/docs/api/appkit-ui/ui/Switch/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Switch.md +46 -0
- package/docs/docs/api/appkit-ui/ui/Table/index.html +69 -0
- package/docs/docs/api/appkit-ui/ui/Table.md +236 -0
- package/docs/docs/api/appkit-ui/ui/Tabs/index.html +48 -0
- package/docs/docs/api/appkit-ui/ui/Tabs.md +177 -0
- package/docs/docs/api/appkit-ui/ui/Textarea/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Textarea.md +35 -0
- package/docs/docs/api/appkit-ui/ui/Toaster/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Toaster.md +75 -0
- package/docs/docs/api/appkit-ui/ui/Toggle/index.html +27 -0
- package/docs/docs/api/appkit-ui/ui/Toggle.md +48 -0
- package/docs/docs/api/appkit-ui/ui/ToggleGroup/index.html +33 -0
- package/docs/docs/api/appkit-ui/ui/ToggleGroup.md +88 -0
- package/docs/docs/api/appkit-ui/ui/Tooltip/index.html +46 -0
- package/docs/docs/api/appkit-ui/ui/Tooltip.md +134 -0
- package/docs/docs/api/appkit-ui.md +15 -0
- package/docs/docs/api/appkit.md +48 -0
- package/docs/docs/api/index.html +28 -0
- package/docs/docs/api.md +24 -0
- package/docs/docs/app-management/index.html +106 -0
- package/docs/docs/app-management.md +171 -0
- package/docs/docs/architecture/index.html +71 -0
- package/docs/docs/architecture.md +69 -0
- package/docs/docs/category/development/index.html +16 -0
- package/docs/docs/category/development.md +3 -0
- package/docs/docs/configuration/index.html +66 -0
- package/docs/docs/configuration.md +150 -0
- package/docs/docs/core-principles/index.html +38 -0
- package/docs/docs/core-principles.md +31 -0
- package/docs/docs/development/index.html +34 -0
- package/docs/docs/development/llm-guide/index.html +74 -0
- package/docs/docs/development/llm-guide.md +74 -0
- package/docs/docs/development/local-development/index.html +27 -0
- package/docs/docs/development/local-development.md +20 -0
- package/docs/docs/development/project-setup/index.html +69 -0
- package/docs/docs/development/project-setup.md +246 -0
- package/docs/docs/development/remote-bridge/index.html +76 -0
- package/docs/docs/development/remote-bridge.md +80 -0
- package/docs/docs/development/type-generation/index.html +65 -0
- package/docs/docs/development/type-generation.md +110 -0
- package/docs/docs/development.md +21 -0
- package/docs/docs/index.html +58 -0
- package/docs/docs/plugins/index.html +151 -0
- package/docs/docs/plugins.md +313 -0
- package/docs/docs.md +64 -0
- package/llms.txt +121 -1228
- package/package.json +11 -11
- package/scripts/postinstall.js +1 -1
- package/AGENTS.md +0 -1231
- package/bin/appkit-lint.js +0 -129
- package/bin/generate-types.js +0 -27
- package/bin/setup-claude.js +0 -190
package/NOTICE.md
CHANGED
|
@@ -55,12 +55,12 @@ This Software contains code from the following open source projects:
|
|
|
55
55
|
| [class-variance-authority](https://www.npmjs.com/package/class-variance-authority) | 0.7.1 | Apache-2.0 | https://github.com/joe-bell/cva#readme |
|
|
56
56
|
| [clsx](https://www.npmjs.com/package/clsx) | 2.1.1 | MIT | https://github.com/lukeed/clsx#readme |
|
|
57
57
|
| [cmdk](https://www.npmjs.com/package/cmdk) | 1.1.1 | MIT | https://github.com/pacocoursey/cmdk#readme |
|
|
58
|
+
| [commander](https://www.npmjs.com/package/commander) | 2.20.3, 5.1.0, 7.2.0, 8.3.0, 10.0.1, 12.1.0 | MIT | https://github.com/tj/commander.js#readme |
|
|
58
59
|
| [date-fns](https://www.npmjs.com/package/date-fns) | 4.1.0 | MIT | https://github.com/date-fns/date-fns#readme |
|
|
59
60
|
| [dotenv](https://www.npmjs.com/package/dotenv) | 16.6.1 | BSD-2-Clause | https://github.com/motdotla/dotenv#readme |
|
|
60
61
|
| [echarts-for-react](https://www.npmjs.com/package/echarts-for-react) | 3.0.5 | MIT | https://github.com/hustcc/echarts-for-react |
|
|
61
62
|
| [embla-carousel-react](https://www.npmjs.com/package/embla-carousel-react) | 8.6.0 | MIT | https://www.embla-carousel.com |
|
|
62
63
|
| [express](https://www.npmjs.com/package/express) | 4.22.0 | MIT | http://expressjs.com/ |
|
|
63
|
-
| [fast-glob](https://www.npmjs.com/package/fast-glob) | 3.3.3 | MIT | https://github.com/mrmlnc/fast-glob#readme |
|
|
64
64
|
| [input-otp](https://www.npmjs.com/package/input-otp) | 1.4.2 | MIT | https://input-otp.rodz.dev/ |
|
|
65
65
|
| [lucide-react](https://www.npmjs.com/package/lucide-react) | 0.554.0 | ISC | https://lucide.dev |
|
|
66
66
|
| [next-themes](https://www.npmjs.com/package/next-themes) | 0.4.6 | MIT | https://github.com/pacocoursey/next-themes#readme |
|
package/bin/appkit.js
ADDED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics.d.ts","names":[],"sources":["../../src/analytics/analytics.ts"],"sourcesContent":[],"mappings":";;;;;;;;cA0Ba,eAAA,SAAwB,MAAA;;;EAAxB,iBAAA,WAAgB,EAAA,MAAA;EAAA,UAAA,MAAA,EAKD,gBALC;UAKD,SAAA;UAMN,cAAA;aAWC,CAAA,MAAA,EAXD,gBAWC;
|
|
1
|
+
{"version":3,"file":"analytics.d.ts","names":[],"sources":["../../src/analytics/analytics.ts"],"sourcesContent":[],"mappings":";;;;;;;;cA0Ba,eAAA,SAAwB,MAAA;;;EAAxB,iBAAA,WAAgB,EAAA,MAAA;EAAA,UAAA,MAAA,EAKD,gBALC;UAKD,SAAA;UAMN,cAAA;aAWC,CAAA,MAAA,EAXD,gBAWC;cA0BN,CAAA,MAAA,EA1BM,UA0BN,CAAA,EAAA,IAAA;;;;;mBA2CZ,CAAA,GAAA,EA3CI,OAAA,CAAQ,OA2CZ,EAAA,GAAA,EA1CI,OAAA,CAAQ,QA0CZ,CAAA,EAzCA,OAyCA,CAAA,IAAA,CAAA;;;;;mBAkHA,CAAA,GAAA,EApHI,OAAA,CAAQ,OAoHZ,EAAA,GAAA,EAnHI,OAAA,CAAQ,QAmHZ,CAAA,EAlHA,OAkHA,CAAA,IAAA,CAAA;;;;;;;;AAwCL;;;;;;;;oCA3CiB,eAAe,sDACT,8BACV,cACR;;;;0CAyBgB,yCAER,cACR,QAAQ;cAIO;;;;;cAQP,WAAS,gBAAA,iBAAA"}
|
|
@@ -44,22 +44,6 @@ var AnalyticsPlugin = class extends Plugin {
|
|
|
44
44
|
await this._handleQueryRoute(req, res);
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
|
-
this.route(router, {
|
|
48
|
-
name: "arrowAsUser",
|
|
49
|
-
method: "get",
|
|
50
|
-
path: "/users/me/arrow-result/:jobId",
|
|
51
|
-
handler: async (req, res) => {
|
|
52
|
-
await this.asUser(req)._handleArrowRoute(req, res);
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
this.route(router, {
|
|
56
|
-
name: "queryAsUser",
|
|
57
|
-
method: "post",
|
|
58
|
-
path: "/users/me/query/:query_key",
|
|
59
|
-
handler: async (req, res) => {
|
|
60
|
-
await this.asUser(req)._handleQueryRoute(req, res);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
47
|
}
|
|
64
48
|
/**
|
|
65
49
|
* Handle Arrow data download requests.
|
|
@@ -102,23 +86,26 @@ var AnalyticsPlugin = class extends Plugin {
|
|
|
102
86
|
parameter_count: parameters ? Object.keys(parameters).length : 0,
|
|
103
87
|
plugin: this.name
|
|
104
88
|
});
|
|
105
|
-
const queryParameters = format === "ARROW" ? {
|
|
106
|
-
formatParameters: {
|
|
107
|
-
disposition: "EXTERNAL_LINKS",
|
|
108
|
-
format: "ARROW_STREAM"
|
|
109
|
-
},
|
|
110
|
-
type: "arrow"
|
|
111
|
-
} : { type: "result" };
|
|
112
|
-
const userKey = getCurrentUserId();
|
|
113
89
|
if (!query_key) {
|
|
114
90
|
res.status(400).json({ error: "query_key is required" });
|
|
115
91
|
return;
|
|
116
92
|
}
|
|
117
|
-
const
|
|
118
|
-
if (!
|
|
93
|
+
const queryResult = await this.app.getAppQuery(query_key, req, this.devFileReader);
|
|
94
|
+
if (!queryResult) {
|
|
119
95
|
res.status(404).json({ error: "Query not found" });
|
|
120
96
|
return;
|
|
121
97
|
}
|
|
98
|
+
const { query, isAsUser } = queryResult;
|
|
99
|
+
const executor = isAsUser ? this.asUser(req) : this;
|
|
100
|
+
const userKey = getCurrentUserId();
|
|
101
|
+
const executorKey = isAsUser ? userKey : "global";
|
|
102
|
+
const queryParameters = format === "ARROW" ? {
|
|
103
|
+
formatParameters: {
|
|
104
|
+
disposition: "EXTERNAL_LINKS",
|
|
105
|
+
format: "ARROW_STREAM"
|
|
106
|
+
},
|
|
107
|
+
type: "arrow"
|
|
108
|
+
} : { type: "result" };
|
|
122
109
|
const hashedQuery = this.queryProcessor.hashQuery(query);
|
|
123
110
|
const streamExecutionSettings = { default: {
|
|
124
111
|
...queryDefaults,
|
|
@@ -130,18 +117,18 @@ var AnalyticsPlugin = class extends Plugin {
|
|
|
130
117
|
JSON.stringify(parameters),
|
|
131
118
|
JSON.stringify(format),
|
|
132
119
|
hashedQuery,
|
|
133
|
-
|
|
120
|
+
executorKey
|
|
134
121
|
]
|
|
135
122
|
}
|
|
136
123
|
} };
|
|
137
|
-
await
|
|
124
|
+
await executor.executeStream(res, async (signal) => {
|
|
138
125
|
const processedParams = await this.queryProcessor.processQueryParams(query, parameters);
|
|
139
|
-
const result = await
|
|
126
|
+
const result = await executor.query(query, processedParams, queryParameters.formatParameters, signal);
|
|
140
127
|
return {
|
|
141
128
|
type: queryParameters.type,
|
|
142
129
|
...result
|
|
143
130
|
};
|
|
144
|
-
}, streamExecutionSettings,
|
|
131
|
+
}, streamExecutionSettings, executorKey);
|
|
145
132
|
}
|
|
146
133
|
/**
|
|
147
134
|
* Execute a SQL query using the current execution context.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics.js","names":[],"sources":["../../src/analytics/analytics.ts"],"sourcesContent":["import type { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type express from \"express\";\nimport type {\n IAppRouter,\n PluginExecuteConfig,\n SQLTypeMarker,\n StreamExecutionSettings,\n} from \"shared\";\nimport { SQLWarehouseConnector } from \"../connectors\";\nimport {\n getCurrentUserId,\n getWarehouseId,\n getWorkspaceClient,\n} from \"../context\";\nimport { createLogger } from \"../logging/logger\";\nimport { Plugin, toPlugin } from \"../plugin\";\nimport { queryDefaults } from \"./defaults\";\nimport { QueryProcessor } from \"./query\";\nimport type {\n AnalyticsQueryResponse,\n IAnalyticsConfig,\n IAnalyticsQueryRequest,\n} from \"./types\";\n\nconst logger = createLogger(\"analytics\");\n\nexport class AnalyticsPlugin extends Plugin {\n name = \"analytics\";\n envVars = [];\n\n protected static description = \"Analytics plugin for data analysis\";\n protected declare config: IAnalyticsConfig;\n\n // analytics services\n private SQLClient: SQLWarehouseConnector;\n private queryProcessor: QueryProcessor;\n\n constructor(config: IAnalyticsConfig) {\n super(config);\n this.config = config;\n this.queryProcessor = new QueryProcessor();\n\n this.SQLClient = new SQLWarehouseConnector({\n timeout: config.timeout,\n telemetry: config.telemetry,\n });\n }\n\n injectRoutes(router: IAppRouter) {\n // Service principal endpoints\n this.route(router, {\n name: \"arrow\",\n method: \"get\",\n path: \"/arrow-result/:jobId\",\n handler: async (req: express.Request, res: express.Response) => {\n await this._handleArrowRoute(req, res);\n },\n });\n\n this.route<AnalyticsQueryResponse>(router, {\n name: \"query\",\n method: \"post\",\n path: \"/query/:query_key\",\n handler: async (req: express.Request, res: express.Response) => {\n await this._handleQueryRoute(req, res);\n },\n });\n\n // User context endpoints - use asUser(req) to execute with user's identity\n this.route(router, {\n name: \"arrowAsUser\",\n method: \"get\",\n path: \"/users/me/arrow-result/:jobId\",\n handler: async (req: express.Request, res: express.Response) => {\n await this.asUser(req)._handleArrowRoute(req, res);\n },\n });\n\n this.route<AnalyticsQueryResponse>(router, {\n name: \"queryAsUser\",\n method: \"post\",\n path: \"/users/me/query/:query_key\",\n handler: async (req: express.Request, res: express.Response) => {\n await this.asUser(req)._handleQueryRoute(req, res);\n },\n });\n }\n\n /**\n * Handle Arrow data download requests.\n * When called via asUser(req), uses the user's Databricks credentials.\n */\n async _handleArrowRoute(\n req: express.Request,\n res: express.Response,\n ): Promise<void> {\n try {\n const { jobId } = req.params;\n const workspaceClient = getWorkspaceClient();\n\n logger.debug(\"Processing Arrow job request for jobId=%s\", jobId);\n\n const event = logger.event(req);\n event?.setComponent(\"analytics\", \"getArrowData\").setContext(\"analytics\", {\n job_id: jobId,\n plugin: this.name,\n });\n\n const result = await this.getArrowData(workspaceClient, jobId);\n\n res.setHeader(\"Content-Type\", \"application/octet-stream\");\n res.setHeader(\"Content-Length\", result.data.length.toString());\n res.setHeader(\"Cache-Control\", \"public, max-age=3600\");\n\n logger.debug(\n \"Sending Arrow buffer: %d bytes for job %s\",\n result.data.length,\n jobId,\n );\n res.send(Buffer.from(result.data));\n } catch (error) {\n logger.error(\"Arrow job error: %O\", error);\n res.status(404).json({\n error: error instanceof Error ? error.message : \"Arrow job not found\",\n plugin: this.name,\n });\n }\n }\n\n /**\n * Handle SQL query execution requests.\n * When called via asUser(req), uses the user's Databricks credentials.\n */\n async _handleQueryRoute(\n req: express.Request,\n res: express.Response,\n ): Promise<void> {\n const { query_key } = req.params;\n const { parameters, format = \"JSON\" } = req.body as IAnalyticsQueryRequest;\n\n // Request-scoped logging with WideEvent tracking\n logger.debug(req, \"Executing query: %s (format=%s)\", query_key, format);\n\n const event = logger.event(req);\n event?.setComponent(\"analytics\", \"executeQuery\").setContext(\"analytics\", {\n query_key,\n format,\n parameter_count: parameters ? Object.keys(parameters).length : 0,\n plugin: this.name,\n });\n\n const queryParameters =\n format === \"ARROW\"\n ? {\n formatParameters: {\n disposition: \"EXTERNAL_LINKS\",\n format: \"ARROW_STREAM\",\n },\n type: \"arrow\",\n }\n : {\n type: \"result\",\n };\n\n // Get user key from current context (automatically includes user ID when in user context)\n const userKey = getCurrentUserId();\n\n if (!query_key) {\n res.status(400).json({ error: \"query_key is required\" });\n return;\n }\n\n const query = await this.app.getAppQuery(\n query_key,\n req,\n this.devFileReader,\n );\n\n if (!query) {\n res.status(404).json({ error: \"Query not found\" });\n return;\n }\n\n const hashedQuery = this.queryProcessor.hashQuery(query);\n\n const defaultConfig: PluginExecuteConfig = {\n ...queryDefaults,\n cache: {\n ...queryDefaults.cache,\n cacheKey: [\n \"analytics:query\",\n query_key,\n JSON.stringify(parameters),\n JSON.stringify(format),\n hashedQuery,\n userKey,\n ],\n },\n };\n\n const streamExecutionSettings: StreamExecutionSettings = {\n default: defaultConfig,\n };\n\n await this.executeStream(\n res,\n async (signal) => {\n const processedParams = await this.queryProcessor.processQueryParams(\n query,\n parameters,\n );\n\n const result = await this.query(\n query,\n processedParams,\n queryParameters.formatParameters,\n signal,\n );\n\n return { type: queryParameters.type, ...result };\n },\n streamExecutionSettings,\n userKey,\n );\n }\n\n /**\n * Execute a SQL query using the current execution context.\n *\n * When called directly: uses service principal credentials.\n * When called via asUser(req).query(...): uses user's credentials.\n *\n * @example\n * ```typescript\n * // Service principal execution\n * const result = await analytics.query(\"SELECT * FROM table\")\n *\n * // User context execution (in route handler)\n * const result = await this.asUser(req).query(\"SELECT * FROM table\")\n * ```\n */\n async query(\n query: string,\n parameters?: Record<string, SQLTypeMarker | null | undefined>,\n formatParameters?: Record<string, any>,\n signal?: AbortSignal,\n ): Promise<any> {\n const workspaceClient = getWorkspaceClient();\n const warehouseId = await getWarehouseId();\n\n const { statement, parameters: sqlParameters } =\n this.queryProcessor.convertToSQLParameters(query, parameters);\n\n const response = await this.SQLClient.executeStatement(\n workspaceClient,\n {\n statement,\n warehouse_id: warehouseId,\n parameters: sqlParameters,\n ...formatParameters,\n },\n signal,\n );\n\n return response.result;\n }\n\n /**\n * Get Arrow-formatted data for a completed query job.\n */\n protected async getArrowData(\n workspaceClient: WorkspaceClient,\n jobId: string,\n signal?: AbortSignal,\n ): Promise<ReturnType<typeof this.SQLClient.getArrowData>> {\n return await this.SQLClient.getArrowData(workspaceClient, jobId, signal);\n }\n\n async shutdown(): Promise<void> {\n this.streamManager.abortAll();\n }\n}\n\n/**\n * @internal\n */\n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AmD,EACvD,SAhByC;GACzC,GAAG;GACH,OAAO;IACL,GAAG,cAAc;IACjB,UAAU;KACR;KACA;KACA,KAAK,UAAU,WAAW;KAC1B,KAAK,UAAU,OAAO;KACtB;KACA;KACD;IACF;GACF,EAIA;AAED,QAAM,KAAK,cACT,KACA,OAAO,WAAW;GAChB,MAAM,kBAAkB,MAAM,KAAK,eAAe,mBAChD,OACA,WACD;GAED,MAAM,SAAS,MAAM,KAAK,MACxB,OACA,iBACA,gBAAgB,kBAChB,OACD;AAED,UAAO;IAAE,MAAM,gBAAgB;IAAM,GAAG;IAAQ;KAElD,yBACA,QACD;;;;;;;;;;;;;;;;;CAkBH,MAAM,MACJ,OACA,YACA,kBACA,QACc;EACd,MAAM,kBAAkB,oBAAoB;EAC5C,MAAM,cAAc,MAAM,gBAAgB;EAE1C,MAAM,EAAE,WAAW,YAAY,kBAC7B,KAAK,eAAe,uBAAuB,OAAO,WAAW;AAa/D,UAXiB,MAAM,KAAK,UAAU,iBACpC,iBACA;GACE;GACA,cAAc;GACd,YAAY;GACZ,GAAG;GACJ,EACD,OACD,EAEe;;;;;CAMlB,MAAgB,aACd,iBACA,OACA,QACyD;AACzD,SAAO,MAAM,KAAK,UAAU,aAAa,iBAAiB,OAAO,OAAO;;CAG1E,MAAM,WAA0B;AAC9B,OAAK,cAAc,UAAU;;;;;;AAOjC,MAAa,YAAY,SAIvB,iBAAiB,YAAY"}
|
|
1
|
+
{"version":3,"file":"analytics.js","names":[],"sources":["../../src/analytics/analytics.ts"],"sourcesContent":["import type { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type express from \"express\";\nimport type {\n IAppRouter,\n PluginExecuteConfig,\n SQLTypeMarker,\n StreamExecutionSettings,\n} from \"shared\";\nimport { SQLWarehouseConnector } from \"../connectors\";\nimport {\n getCurrentUserId,\n getWarehouseId,\n getWorkspaceClient,\n} from \"../context\";\nimport { createLogger } from \"../logging/logger\";\nimport { Plugin, toPlugin } from \"../plugin\";\nimport { queryDefaults } from \"./defaults\";\nimport { QueryProcessor } from \"./query\";\nimport type {\n AnalyticsQueryResponse,\n IAnalyticsConfig,\n IAnalyticsQueryRequest,\n} from \"./types\";\n\nconst logger = createLogger(\"analytics\");\n\nexport class AnalyticsPlugin extends Plugin {\n name = \"analytics\";\n envVars = [];\n\n protected static description = \"Analytics plugin for data analysis\";\n protected declare config: IAnalyticsConfig;\n\n // analytics services\n private SQLClient: SQLWarehouseConnector;\n private queryProcessor: QueryProcessor;\n\n constructor(config: IAnalyticsConfig) {\n super(config);\n this.config = config;\n this.queryProcessor = new QueryProcessor();\n\n this.SQLClient = new SQLWarehouseConnector({\n timeout: config.timeout,\n telemetry: config.telemetry,\n });\n }\n\n injectRoutes(router: IAppRouter) {\n // Service principal endpoints\n this.route(router, {\n name: \"arrow\",\n method: \"get\",\n path: \"/arrow-result/:jobId\",\n handler: async (req: express.Request, res: express.Response) => {\n await this._handleArrowRoute(req, res);\n },\n });\n\n this.route<AnalyticsQueryResponse>(router, {\n name: \"query\",\n method: \"post\",\n path: \"/query/:query_key\",\n handler: async (req: express.Request, res: express.Response) => {\n await this._handleQueryRoute(req, res);\n },\n });\n }\n\n /**\n * Handle Arrow data download requests.\n * When called via asUser(req), uses the user's Databricks credentials.\n */\n async _handleArrowRoute(\n req: express.Request,\n res: express.Response,\n ): Promise<void> {\n try {\n const { jobId } = req.params;\n const workspaceClient = getWorkspaceClient();\n\n logger.debug(\"Processing Arrow job request for jobId=%s\", jobId);\n\n const event = logger.event(req);\n event?.setComponent(\"analytics\", \"getArrowData\").setContext(\"analytics\", {\n job_id: jobId,\n plugin: this.name,\n });\n\n const result = await this.getArrowData(workspaceClient, jobId);\n\n res.setHeader(\"Content-Type\", \"application/octet-stream\");\n res.setHeader(\"Content-Length\", result.data.length.toString());\n res.setHeader(\"Cache-Control\", \"public, max-age=3600\");\n\n logger.debug(\n \"Sending Arrow buffer: %d bytes for job %s\",\n result.data.length,\n jobId,\n );\n res.send(Buffer.from(result.data));\n } catch (error) {\n logger.error(\"Arrow job error: %O\", error);\n res.status(404).json({\n error: error instanceof Error ? error.message : \"Arrow job not found\",\n plugin: this.name,\n });\n }\n }\n\n /**\n * Handle SQL query execution requests.\n * When called via asUser(req), uses the user's Databricks credentials.\n */\n async _handleQueryRoute(\n req: express.Request,\n res: express.Response,\n ): Promise<void> {\n const { query_key } = req.params;\n const { parameters, format = \"JSON\" } = req.body as IAnalyticsQueryRequest;\n\n // Request-scoped logging with WideEvent tracking\n logger.debug(req, \"Executing query: %s (format=%s)\", query_key, format);\n\n const event = logger.event(req);\n event?.setComponent(\"analytics\", \"executeQuery\").setContext(\"analytics\", {\n query_key,\n format,\n parameter_count: parameters ? Object.keys(parameters).length : 0,\n plugin: this.name,\n });\n\n if (!query_key) {\n res.status(400).json({ error: \"query_key is required\" });\n return;\n }\n\n const queryResult = await this.app.getAppQuery(\n query_key,\n req,\n this.devFileReader,\n );\n\n if (!queryResult) {\n res.status(404).json({ error: \"Query not found\" });\n return;\n }\n\n const { query, isAsUser } = queryResult;\n\n // get execution context - user-scoped if .obo.sql, otherwise service principal\n const executor = isAsUser ? this.asUser(req) : this;\n const userKey = getCurrentUserId();\n const executorKey = isAsUser ? userKey : \"global\";\n\n const queryParameters =\n format === \"ARROW\"\n ? {\n formatParameters: {\n disposition: \"EXTERNAL_LINKS\",\n format: \"ARROW_STREAM\",\n },\n type: \"arrow\",\n }\n : {\n type: \"result\",\n };\n\n const hashedQuery = this.queryProcessor.hashQuery(query);\n\n const defaultConfig: PluginExecuteConfig = {\n ...queryDefaults,\n cache: {\n ...queryDefaults.cache,\n cacheKey: [\n \"analytics:query\",\n query_key,\n JSON.stringify(parameters),\n JSON.stringify(format),\n hashedQuery,\n executorKey,\n ],\n },\n };\n\n const streamExecutionSettings: StreamExecutionSettings = {\n default: defaultConfig,\n };\n\n await executor.executeStream(\n res,\n async (signal) => {\n const processedParams = await this.queryProcessor.processQueryParams(\n query,\n parameters,\n );\n\n const result = await executor.query(\n query,\n processedParams,\n queryParameters.formatParameters,\n signal,\n );\n\n return { type: queryParameters.type, ...result };\n },\n streamExecutionSettings,\n executorKey,\n );\n }\n\n /**\n * Execute a SQL query using the current execution context.\n *\n * When called directly: uses service principal credentials.\n * When called via asUser(req).query(...): uses user's credentials.\n *\n * @example\n * ```typescript\n * // Service principal execution\n * const result = await analytics.query(\"SELECT * FROM table\")\n *\n * // User context execution (in route handler)\n * const result = await this.asUser(req).query(\"SELECT * FROM table\")\n * ```\n */\n async query(\n query: string,\n parameters?: Record<string, SQLTypeMarker | null | undefined>,\n formatParameters?: Record<string, any>,\n signal?: AbortSignal,\n ): Promise<any> {\n const workspaceClient = getWorkspaceClient();\n const warehouseId = await getWarehouseId();\n\n const { statement, parameters: sqlParameters } =\n this.queryProcessor.convertToSQLParameters(query, parameters);\n\n const response = await this.SQLClient.executeStatement(\n workspaceClient,\n {\n statement,\n warehouse_id: warehouseId,\n parameters: sqlParameters,\n ...formatParameters,\n },\n signal,\n );\n\n return response.result;\n }\n\n /**\n * Get Arrow-formatted data for a completed query job.\n */\n protected async getArrowData(\n workspaceClient: WorkspaceClient,\n jobId: string,\n signal?: AbortSignal,\n ): Promise<ReturnType<typeof this.SQLClient.getArrowData>> {\n return await this.SQLClient.getArrowData(workspaceClient, jobId, signal);\n }\n\n async shutdown(): Promise<void> {\n this.streamManager.abortAll();\n }\n}\n\n/**\n * @internal\n */\nexport const analytics = toPlugin<\n typeof AnalyticsPlugin,\n IAnalyticsConfig,\n \"analytics\"\n>(AnalyticsPlugin, \"analytics\");\n"],"mappings":";;;;;;;;;;;;cAaoB;AAWpB,MAAM,SAAS,aAAa,YAAY;AAExC,IAAa,kBAAb,cAAqC,OAAO;;qBAIX;;CAO/B,YAAY,QAA0B;AACpC,QAAM,OAAO;cAXR;iBACG,EAAE;AAWV,OAAK,SAAS;AACd,OAAK,iBAAiB,IAAI,gBAAgB;AAE1C,OAAK,YAAY,IAAI,sBAAsB;GACzC,SAAS,OAAO;GAChB,WAAW,OAAO;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UAXiB,MAAM,KAAK,UAAU,iBACpC,iBACA;GACE;GACA,cAAc;GACd,YAAY;GACZ,GAAG;GACJ,EACD,OACD,EAEe;;;;;CAMlB,MAAgB,aACd,iBACA,OACA,QACyD;AACzD,SAAO,MAAM,KAAK,UAAU,aAAa,iBAAiB,OAAO,OAAO;;CAG1E,MAAM,WAA0B;AAC9B,OAAK,cAAc,UAAU;;;;;;AAOjC,MAAa,YAAY,SAIvB,iBAAiB,YAAY"}
|
package/dist/app/index.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ interface RequestLike {
|
|
|
6
6
|
interface DevFileReader {
|
|
7
7
|
readFile(filePath: string, req: RequestLike): Promise<string>;
|
|
8
8
|
}
|
|
9
|
+
interface QueryResult {
|
|
10
|
+
query: string;
|
|
11
|
+
isAsUser: boolean;
|
|
12
|
+
}
|
|
9
13
|
declare class AppManager {
|
|
10
14
|
/**
|
|
11
15
|
* Retrieves a query file by key from the queries directory
|
|
@@ -16,7 +20,7 @@ declare class AppManager {
|
|
|
16
20
|
* @returns The query content as a string
|
|
17
21
|
* @throws Error if query key is invalid or file not found
|
|
18
22
|
*/
|
|
19
|
-
getAppQuery(queryKey: string, req?: RequestLike, devFileReader?: DevFileReader): Promise<
|
|
23
|
+
getAppQuery(queryKey: string, req?: RequestLike, devFileReader?: DevFileReader): Promise<QueryResult | null>;
|
|
20
24
|
}
|
|
21
25
|
//#endregion
|
|
22
26
|
export { AppManager };
|
package/dist/app/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/app/index.ts"],"sourcesContent":[],"mappings":";UAMU,WAAA;EAAA,KAAA,CAAA,EACA,MADW,CAAA,MAAA,EAAA,GAAA,CAAA;EAAA,OAAA,EAEV,MAFU,CAAA,MAAA,EAAA,MAAA,GAAA,MAAA,EAAA,GAAA,SAAA,CAAA;;UAKX,aAAA,CAHC;EAAM,QAAA,CAAA,QAAA,EAAA,MAAA,EAAA,GAAA,EAIiB,WAJjB,CAAA,EAI+B,OAJ/B,CAAA,MAAA,CAAA;AAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/app/index.ts"],"sourcesContent":[],"mappings":";UAMU,WAAA;EAAA,KAAA,CAAA,EACA,MADW,CAAA,MAAA,EAAA,GAAA,CAAA;EAAA,OAAA,EAEV,MAFU,CAAA,MAAA,EAAA,MAAA,GAAA,MAAA,EAAA,GAAA,SAAA,CAAA;;UAKX,aAAA,CAHC;EAAM,QAAA,CAAA,QAAA,EAAA,MAAA,EAAA,GAAA,EAIiB,WAJjB,CAAA,EAI+B,OAJ/B,CAAA,MAAA,CAAA;AAAA;UAOP,WAAA,CAJa;OACW,EAAA,MAAA;UAAc,EAAA,OAAA;;AAGtC,cAKG,UAAA,CALQ;EAKR;;;;;;;;;sCAYH,6BACU,gBACf,QAAQ"}
|
package/dist/app/index.js
CHANGED
|
@@ -19,28 +19,57 @@ var AppManager = class {
|
|
|
19
19
|
logger.error("Invalid query key format: %s. Only alphanumeric characters, underscores, and hyphens are allowed.", queryKey);
|
|
20
20
|
return null;
|
|
21
21
|
}
|
|
22
|
-
const queryFilePath = path.join(process.cwd(), "config/queries", `${queryKey}.sql`);
|
|
23
|
-
const resolvedPath = path.resolve(queryFilePath);
|
|
24
22
|
const queriesDir = path.resolve(process.cwd(), "config/queries");
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
const oboFileName = `${queryKey}.obo.sql`;
|
|
24
|
+
const defaultFileName = `${queryKey}.sql`;
|
|
25
|
+
let queryFileName = null;
|
|
26
|
+
let isAsUser = false;
|
|
27
|
+
try {
|
|
28
|
+
const files = await fs.readdir(queriesDir);
|
|
29
|
+
if (files.includes(oboFileName)) {
|
|
30
|
+
queryFileName = oboFileName;
|
|
31
|
+
isAsUser = true;
|
|
32
|
+
if (files.includes(defaultFileName)) logger.warn(`Both ${oboFileName} and ${defaultFileName} found for query ${queryKey}. Using ${oboFileName}.`);
|
|
33
|
+
} else if (files.includes(defaultFileName)) {
|
|
34
|
+
queryFileName = defaultFileName;
|
|
35
|
+
isAsUser = false;
|
|
36
|
+
}
|
|
37
|
+
} catch (error) {
|
|
38
|
+
logger.error(`Failed to read queries directory: ${error.message}`);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
if (!queryFileName) {
|
|
42
|
+
logger.error(`Query file not found: ${queryKey}`);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const queryFilePath = path.join(queriesDir, queryFileName);
|
|
46
|
+
const resolvedPath = path.resolve(queryFilePath);
|
|
47
|
+
const resolvedQueriesDir = path.resolve(queriesDir);
|
|
48
|
+
if (!resolvedPath.startsWith(resolvedQueriesDir)) {
|
|
49
|
+
logger.error(`Invalid query path: path traversal detected`);
|
|
27
50
|
return null;
|
|
28
51
|
}
|
|
29
52
|
if (req?.query?.dev !== void 0 && devFileReader && req) try {
|
|
30
53
|
const relativePath = path.relative(process.cwd(), resolvedPath);
|
|
31
|
-
return
|
|
54
|
+
return {
|
|
55
|
+
query: await devFileReader.readFile(relativePath, req),
|
|
56
|
+
isAsUser
|
|
57
|
+
};
|
|
32
58
|
} catch (error) {
|
|
33
|
-
logger.error(
|
|
59
|
+
logger.error(`Failed to read query from dev tunnel: ${error.message}`);
|
|
34
60
|
return null;
|
|
35
61
|
}
|
|
36
62
|
try {
|
|
37
|
-
return
|
|
63
|
+
return {
|
|
64
|
+
query: await fs.readFile(resolvedPath, "utf8"),
|
|
65
|
+
isAsUser
|
|
66
|
+
};
|
|
38
67
|
} catch (error) {
|
|
39
68
|
if (error.code === "ENOENT") {
|
|
40
|
-
logger.
|
|
69
|
+
logger.error(`Failed to read query from server filesystem: ${error.message}`);
|
|
41
70
|
return null;
|
|
42
71
|
}
|
|
43
|
-
logger.error(
|
|
72
|
+
logger.error(`Failed to read query from server filesystem: ${error.message}`);
|
|
44
73
|
return null;
|
|
45
74
|
}
|
|
46
75
|
}
|
package/dist/app/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/app/index.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { createLogger } from \"../logging/logger\";\n\nconst logger = createLogger(\"app\");\n\ninterface RequestLike {\n query?: Record<string, any>;\n headers: Record<string, string | string[] | undefined>;\n}\n\ninterface DevFileReader {\n readFile(filePath: string, req: RequestLike): Promise<string>;\n}\n\nexport class AppManager {\n /**\n * Retrieves a query file by key from the queries directory\n * In dev mode with a request context, reads from local filesystem via WebSocket\n * @param queryKey - The query file name (without extension)\n * @param req - Optional request object to detect dev mode\n * @param devFileReader - Optional DevFileReader instance to read files from local filesystem\n * @returns The query content as a string\n * @throws Error if query key is invalid or file not found\n */\n async getAppQuery(\n queryKey: string,\n req?: RequestLike,\n devFileReader?: DevFileReader,\n ): Promise<
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/app/index.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { createLogger } from \"../logging/logger\";\n\nconst logger = createLogger(\"app\");\n\ninterface RequestLike {\n query?: Record<string, any>;\n headers: Record<string, string | string[] | undefined>;\n}\n\ninterface DevFileReader {\n readFile(filePath: string, req: RequestLike): Promise<string>;\n}\n\ninterface QueryResult {\n query: string;\n isAsUser: boolean;\n}\n\nexport class AppManager {\n /**\n * Retrieves a query file by key from the queries directory\n * In dev mode with a request context, reads from local filesystem via WebSocket\n * @param queryKey - The query file name (without extension)\n * @param req - Optional request object to detect dev mode\n * @param devFileReader - Optional DevFileReader instance to read files from local filesystem\n * @returns The query content as a string\n * @throws Error if query key is invalid or file not found\n */\n async getAppQuery(\n queryKey: string,\n req?: RequestLike,\n devFileReader?: DevFileReader,\n ): Promise<QueryResult | null> {\n // Security: Sanitize query key to prevent path traversal\n if (!queryKey || !/^[a-zA-Z0-9_-]+$/.test(queryKey)) {\n logger.error(\n \"Invalid query key format: %s. Only alphanumeric characters, underscores, and hyphens are allowed.\",\n queryKey,\n );\n return null;\n }\n\n const queriesDir = path.resolve(process.cwd(), \"config/queries\");\n\n // priority order: .obo.sql first (asUser), then .sql (default)\n const oboFileName = `${queryKey}.obo.sql`;\n const defaultFileName = `${queryKey}.sql`;\n\n let queryFileName: string | null = null;\n let isAsUser: boolean = false;\n\n try {\n const files = await fs.readdir(queriesDir);\n\n // check for OBO query first\n if (files.includes(oboFileName)) {\n queryFileName = oboFileName;\n isAsUser = true;\n\n // check for both files and warn if both are present\n if (files.includes(defaultFileName)) {\n logger.warn(\n `Both ${oboFileName} and ${defaultFileName} found for query ${queryKey}. Using ${oboFileName}.`,\n );\n }\n // check for default query if OBO query is not present\n } else if (files.includes(defaultFileName)) {\n queryFileName = defaultFileName;\n isAsUser = false;\n }\n } catch (error) {\n logger.error(\n `Failed to read queries directory: ${(error as Error).message}`,\n );\n return null;\n }\n\n if (!queryFileName) {\n logger.error(`Query file not found: ${queryKey}`);\n return null;\n }\n\n const queryFilePath = path.join(queriesDir, queryFileName);\n\n // security: validate resolved path is within queries directory\n const resolvedPath = path.resolve(queryFilePath);\n const resolvedQueriesDir = path.resolve(queriesDir);\n\n if (!resolvedPath.startsWith(resolvedQueriesDir)) {\n logger.error(`Invalid query path: path traversal detected`);\n return null;\n }\n\n // check if we're in dev mode and should use WebSocket\n const isDevMode = req?.query?.dev !== undefined;\n if (isDevMode && devFileReader && req) {\n try {\n const relativePath = path.relative(process.cwd(), resolvedPath);\n return {\n query: await devFileReader.readFile(relativePath, req),\n isAsUser,\n };\n } catch (error) {\n logger.error(\n `Failed to read query from dev tunnel: ${(error as Error).message}`,\n );\n return null;\n }\n }\n\n // production mode: read from server filesystem\n try {\n const query = await fs.readFile(resolvedPath, \"utf8\");\n return { query, isAsUser };\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n logger.error(\n `Failed to read query from server filesystem: ${(error as Error).message}`,\n );\n return null;\n }\n\n logger.error(\n `Failed to read query from server filesyst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,SAAS,cAAc,IAAI;IACtD;IACD;WACM,OAAO;AACd,UAAO,MACL,yCAA0C,MAAgB,UAC3D;AACD,UAAO;;AAKX,MAAI;AAEF,UAAO;IAAE,OADK,MAAM,GAAG,SAAS,cAAc,OAAO;IACrC;IAAU;WACnB,OAAO;AACd,OAAK,MAAgC,SAAS,UAAU;AACtD,WAAO,MACL,gDAAiD,MAAgB,UAClE;AACD,WAAO;;AAGT,UAAO,MACL,gDAAiD,MAAgB,UAClE;AACD,UAAO"}
|
package/dist/appkit/package.js
CHANGED
package/dist/cache/index.js
CHANGED
|
@@ -181,13 +181,13 @@ var CacheManager = class CacheManager {
|
|
|
181
181
|
cache_hit: false,
|
|
182
182
|
cache_key: cacheKey
|
|
183
183
|
});
|
|
184
|
-
const promise = fn().then(async (result
|
|
185
|
-
await this.set(cacheKey, result
|
|
184
|
+
const promise = fn().then(async (result) => {
|
|
185
|
+
await this.set(cacheKey, result, options);
|
|
186
186
|
span.addEvent("cache.value_stored", {
|
|
187
187
|
"cache.key": cacheKey,
|
|
188
188
|
"cache.ttl": options?.ttl ?? this.config.ttl ?? 3600
|
|
189
189
|
});
|
|
190
|
-
return result
|
|
190
|
+
return result;
|
|
191
191
|
}).catch((error) => {
|
|
192
192
|
span.recordException(error);
|
|
193
193
|
span.setStatus({ code: SpanStatusCode.ERROR });
|
package/dist/cache/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["result"],"sources":["../../src/cache/index.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type { CacheConfig, CacheStorage } from \"shared\";\nimport { LakebaseConnector } from \"@/connectors\";\nimport { AppKitError, ExecutionError, InitializationError } from \"../errors\";\nimport { createLogger } from \"../logging/logger\";\nimport type { Counter, TelemetryProvider } from \"../telemetry\";\nimport { SpanStatusCode, TelemetryManager } from \"../telemetry\";\nimport { deepMerge } from \"../utils\";\nimport { cacheDefaults } from \"./defaults\";\nimport { InMemoryStorage, PersistentStorage } from \"./storage\";\n\nconst logger = createLogger(\"cache\");\n\n/**\n * Cache manager class to handle cache operations.\n * Can be used with in-memory storage or persistent storage (Lakebase).\n *\n * The cache is automatically initialized by AppKit. Use `getInstanceSync()` to access\n * the singleton instance after initialization.\n *\n * @internal\n * @example\n * ```typescript\n * const cache = CacheManager.getInstanceSync();\n * const result = await cache.getOrExecute([\"users\", userId], () => fetchUser(userId), userKey);\n * ```\n */\nexport class CacheManager {\n private static readonly MIN_CLEANUP_INTERVAL_MS = 60_000;\n private readonly name: string = \"cache-manager\";\n private static instance: CacheManager | null = null;\n private static initPromise: Promise<CacheManager> | null = null;\n\n private storage: CacheStorage;\n private config: CacheConfig;\n private inFlightRequests: Map<string, Promise<unknown>>;\n private cleanupInProgress: boolean;\n private lastCleanupAttempt: number;\n\n private telemetry: TelemetryProvider;\n private telemetryMetrics: {\n cacheHitCount: Counter;\n cacheMissCount: Counter;\n };\n\n private constructor(storage: CacheStorage, config: CacheConfig) {\n this.storage = storage;\n this.config = config;\n this.inFlightRequests = new Map();\n this.cleanupInProgress = false;\n this.lastCleanupAttempt = 0;\n\n this.telemetry = TelemetryManager.getProvider(\n this.name,\n this.config.telemetry,\n );\n this.telemetryMetrics = {\n cacheHitCount: this.telemetry.getMeter().createCounter(\"cache.hit\", {\n description: \"Total number of cache hits\",\n unit: \"1\",\n }),\n cacheMissCount: this.telemetry.getMeter().createCounter(\"cache.miss\", {\n description: \"Total number of cache misses\",\n unit: \"1\",\n }),\n };\n }\n\n /**\n * Get the singleton instance of the cache manager (sync version).\n *\n * Throws if not initialized - ensure AppKit.create() has completed first.\n * @returns CacheManager instance\n */\n static getInstanceSync(): CacheManager {\n if (!CacheManager.instance) {\n throw InitializationError.notInitialized(\n \"CacheManager\",\n \"Ensure AppKit.create() has completed before accessing the cache\",\n );\n }\n\n return CacheManager.instance;\n }\n\n /**\n * Initialize and get the singleton instance of the cache manager.\n * Called internally by AppKit - prefer `getInstanceSync()` for plugin access.\n * @param userConfig - User configuration for the cache manager\n * @returns CacheManager instance\n * @internal\n */\n static async getInstance(\n userConfig?: Partial<CacheConfig>,\n ): Promise<CacheManager> {\n if (CacheManager.instance) {\n return CacheManager.instance;\n }\n\n if (!CacheManager.initPromise) {\n CacheManager.initPromise = CacheManager.create(userConfig).then(\n (instance) => {\n CacheManager.instance = instance;\n return instance;\n },\n );\n }\n\n return CacheManager.initPromise;\n }\n\n /**\n * Create a new cache manager instance\n *\n * Storage selection logic:\n * 1. If `storage` provided and healthy → use provided storage\n * 2. If `storage` provided but unhealthy → fallback to InMemory (or disable if strictPersistence)\n * 3. If no `storage` provided and Lakebase available → use Lakebase\n * 4. If no `storage` provided and Lakebase unavailable → fallback to InMemory (or disable if strictPersistence)\n *\n * @param userConfig - User configuration for the cache manager\n * @returns CacheManager instance\n */\n private static async create(\n userConfig?: Partial<CacheConfig>,\n ): Promise<CacheManager> {\n const config = deepMerge(cacheDefaults, userConfig);\n\n if (config.storage) {\n const isHealthy = await config.storage.healthCheck();\n if (isHealthy) {\n return new CacheManager(config.storage, config);\n }\n\n if (config.strictPersistence) {\n const disabledConfig = { ...config, enabled: false };\n return new CacheManager(\n new InMemoryStorage(disabledConfig),\n disabledConfig,\n );\n }\n\n return new CacheManager(new InMemoryStorage(config), config);\n }\n\n // try to use lakebase storage\n try {\n const workspaceClient = new WorkspaceClient({});\n const connector = new LakebaseConnector({ workspaceClient });\n const isHealthy = await connector.healthCheck();\n\n if (isHealthy) {\n const persistentStorage = new PersistentStorage(config, connector);\n await persistentStorage.initialize();\n return new CacheManager(persistentStorage, config);\n }\n } catch {\n // lakebase unavailable, continue with in-memory storage\n }\n\n if (config.strictPersistence) {\n const disabledConfig = { ...config, enabled: false };\n return new CacheManager(\n new InMemoryStorage(disabledConfig),\n disabledConfig,\n );\n }\n\n return new CacheManager(new InMemoryStorage(config), config);\n }\n\n /**\n * Get or execute a function and cache the result\n * @param key - Cache key\n * @param fn - Function to execute\n * @param userKey - User key\n * @param options - Options for the cache\n * @returns Promise of the result\n */\n async getOrExecute<T>(\n key: (string | number | object)[],\n fn: () => Promise<T>,\n userKey: string,\n options?: { ttl?: number },\n ): Promise<T> {\n if (!this.config.enabled) return fn();\n\n const cacheKey = this.generateKey(key, userKey);\n\n return this.telemetry.startActiveSpan(\n \"cache.getOrExecute\",\n {\n attributes: {\n \"cache.key\": cacheKey,\n \"cache.enabled\": this.config.enabled,\n \"cache.persistent\": this.storage.isPersistent(),\n },\n },\n async (span) => {\n try {\n // check if the value is in the cache\n const cached = await this.storage.get<T>(cacheKey);\n if (cached !== null) {\n span.setAttribute(\"cache.hit\", true);\n span.setStatus({ code: SpanStatusCode.OK });\n this.telemetryMetrics.cacheHitCount.add(1, {\n \"cache.key\": cacheKey,\n });\n\n logger.event()?.setExecution({\n cache_hit: true,\n cache_key: cacheKey,\n });\n\n return cached.value as T;\n }\n\n // check if the value is being processed by another request\n const inFlight = this.inFlightRequests.get(cacheKey);\n if (inFlight) {\n span.setAttribute(\"cache.hit\", true);\n span.setAttribute(\"cache.deduplication\", true);\n span.addEvent(\"cache.deduplication_used\", {\n \"cache.key\": cacheKey,\n });\n span.setStatus({ code: SpanStatusCode.OK });\n this.telemetryMetrics.cacheHitCount.add(1, {\n \"cache.key\": cacheKey,\n \"cache.deduplication\": \"true\",\n });\n\n logger.event()?.setExecution({\n cache_hit: true,\n cache_key: cacheKey,\n cache_deduplication: true,\n });\n\n span.end();\n return inFlight as Promise<T>;\n }\n\n // cache miss - execute function\n span.setAttribute(\"cache.hit\", false);\n span.addEvent(\"cache.miss\", { \"cache.key\": cacheKey });\n this.telemetryMetrics.cacheMissCount.add(1, {\n \"cache.key\": cacheKey,\n });\n\n logger.event()?.setExecution({\n cache_hit: false,\n cache_key: cacheKey,\n });\n\n const promise = fn()\n .then(async (result) => {\n await this.set(cacheKey, result, options);\n span.addEvent(\"cache.value_stored\", {\n \"cache.key\": cacheKey,\n \"cache.ttl\": options?.ttl ?? this.config.ttl ?? 3600,\n });\n return result;\n })\n .catch((error) => {\n span.recordException(error);\n span.setStatus({ code: SpanStatusCode.ERROR });\n if (error instanceof AppKitError) {\n throw error;\n }\n throw ExecutionError.statementFailed(\n error instanceof Error ? error.message : String(error),\n );\n })\n .finally(() => {\n this.inFlightRequests.delete(cacheKey);\n });\n\n this.inFlightRequests.set(cacheKey, promise);\n\n const result = await promise;\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 { name: this.name, includePrefix: true },\n );\n }\n\n /**\n * Get a cached value\n * @param key - Cache key\n * @returns Promise of the value or null if not found or expired\n */\n async get<T>(key: string): Promise<T | null> {\n if (!this.config.enabled) return null;\n\n // probabilistic cleanup trigger\n this.maybeCleanup();\n\n const entry = await this.storage.get<T>(key);\n if (!entry) return null;\n\n if (Date.now() > entry.expiry) {\n await this.storage.delete(key);\n return null;\n }\n return entry.value as T;\n }\n\n /** Probabilistically trigger cleanup of expired entries (fire-and-forget) */\n private maybeCleanup(): void {\n if (this.cleanupInProgress) return;\n if (!this.storage.isPersistent()) return;\n const now = Date.now();\n if (now - this.lastCleanupAttempt < CacheManager.MIN_CLEANUP_INTERVAL_MS)\n return;\n\n const probability = this.config.cleanupProbability ?? 0.01;\n\n if (Math.random() > probability) return;\n\n this.lastCleanupAttempt = now;\n\n this.cleanupInProgress = true;\n (this.storage as PersistentStorage)\n .cleanupExpired()\n .catch((error) => {\n logger.debug(\"Error cleaning up expired entries: %O\", error);\n })\n .finally(() => {\n this.cleanupInProgress = false;\n });\n }\n\n /**\n * Set a value in the cache\n * @param key - Cache key\n * @param value - Value to set\n * @param options - Options for the cache\n * @returns Promise of the result\n */\n async set<T>(\n key: string,\n value: T,\n options?: { ttl?: number },\n ): Promise<void> {\n if (!this.config.enabled) return;\n\n const ttl = options?.ttl ?? this.config.ttl ?? 3600;\n const expiryTime = Date.now() + ttl * 1000;\n await this.storage.set(key, { value, expiry: expiryTime });\n }\n\n /**\n * Delete a value from the cache\n * @param key - Cache key\n * @returns Promise of the result\n */\n async delete(key: string): Promise<void> {\n if (!this.config.enabled) return;\n await this.storage.delete(key);\n }\n\n /** Clear the cache */\n async clear(): Promise<void> {\n await this.storage.clear();\n this.inFlightRequests.clear();\n }\n\n /**\n * Check if a value exists in the cache\n * @param key - Cache key\n * @returns Promise of true if the value exists, false otherwise\n */\n async has(key: string): Promise<boolean> {\n if (!this.config.enabled) return false;\n\n const entry = await this.storage.get(key);\n if (!entry) return false;\n\n if (Date.now() > entry.expiry) {\n await this.storage.delete(key);\n return false;\n }\n return true;\n }\n\n /**\n * Generate a cache key\n * @param parts - Parts of the key\n * @param userKey - User key\n * @returns Cache key\n */\n generateKey(parts: (string | number | object)[], userKey: string): string {\n const allParts = [userKey, ...parts];\n const serialized = JSON.stringify(allParts);\n return createHash(\"sha256\").update(serialized).digest(\"hex\");\n }\n\n /** Close the cache */\n async close(): Promise<void> {\n await this.storage.close();\n }\n\n /**\n * Check if the storage is healthy\n * @returns Promise of true if the storage is healthy, false otherwise\n */\n async isStorageHealthy(): Promise<boolean> {\n return this.storage.healthCheck();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;aAI6E;AAQ7E,MAAM,SAAS,aAAa,QAAQ;;;;;;;;;;;;;;;AAgBpC,IAAa,eAAb,MAAa,aAAa;;iCAC0B;;;kBAEH;;;qBACY;;CAc3D,AAAQ,YAAY,SAAuB,QAAqB;cAhBhC;AAiB9B,OAAK,UAAU;AACf,OAAK,SAAS;AACd,OAAK,mCAAmB,IAAI,KAAK;AACjC,OAAK,oBAAoB;AACzB,OAAK,qBAAqB;AAE1B,OAAK,YAAY,iBAAiB,YAChC,KAAK,MACL,KAAK,OAAO,UACb;AACD,OAAK,mBAAmB;GACtB,eAAe,KAAK,UAAU,UAAU,CAAC,cAAc,aAAa;IAClE,aAAa;IACb,MAAM;IACP,CAAC;GACF,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.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { WorkspaceClient } from \"@databricks/sdk-experimental\";\nimport type { CacheConfig, CacheStorage } from \"shared\";\nimport { LakebaseConnector } from \"@/connectors\";\nimport { AppKitError, ExecutionError, InitializationError } from \"../errors\";\nimport { createLogger } from \"../logging/logger\";\nimport type { Counter, TelemetryProvider } from \"../telemetry\";\nimport { SpanStatusCode, TelemetryManager } from \"../telemetry\";\nimport { deepMerge } from \"../utils\";\nimport { cacheDefaults } from \"./defaults\";\nimport { InMemoryStorage, PersistentStorage } from \"./storage\";\n\nconst logger = createLogger(\"cache\");\n\n/**\n * Cache manager class to handle cache operations.\n * Can be used with in-memory storage or persistent storage (Lakebase).\n *\n * The cache is automatically initialized by AppKit. Use `getInstanceSync()` to access\n * the singleton instance after initialization.\n *\n * @internal\n * @example\n * ```typescript\n * const cache = CacheManager.getInstanceSync();\n * const result = await cache.getOrExecute([\"users\", userId], () => fetchUser(userId), userKey);\n * ```\n */\nexport class CacheManager {\n private static readonly MIN_CLEANUP_INTERVAL_MS = 60_000;\n private readonly name: string = \"cache-manager\";\n private static instance: CacheManager | null = null;\n private static initPromise: Promise<CacheManager> | null = null;\n\n private storage: CacheStorage;\n private config: CacheConfig;\n private inFlightRequests: Map<string, Promise<unknown>>;\n private cleanupInProgress: boolean;\n private lastCleanupAttempt: number;\n\n private telemetry: TelemetryProvider;\n private telemetryMetrics: {\n cacheHitCount: Counter;\n cacheMissCount: Counter;\n };\n\n private constructor(storage: CacheStorage, config: CacheConfig) {\n this.storage = storage;\n this.config = config;\n this.inFlightRequests = new Map();\n this.cleanupInProgress = false;\n this.lastCleanupAttempt = 0;\n\n this.telemetry = TelemetryManager.getProvider(\n this.name,\n this.config.telemetry,\n );\n this.telemetryMetrics = {\n cacheHitCount: this.telemetry.getMeter().createCounter(\"cache.hit\", {\n description: \"Total number of cache hits\",\n unit: \"1\",\n }),\n cacheMissCount: this.telemetry.getMeter().createCounter(\"cache.miss\", {\n description: \"Total number of cache misses\",\n unit: \"1\",\n }),\n };\n }\n\n /**\n * Get the singleton instance of the cache manager (sync version).\n *\n * Throws if not initialized - ensure AppKit.create() has completed first.\n * @returns CacheManager instance\n */\n static getInstanceSync(): CacheManager {\n if (!CacheManager.instance) {\n throw InitializationError.notInitialized(\n \"CacheManager\",\n \"Ensure AppKit.create() has completed before accessing the cache\",\n );\n }\n\n return CacheManager.instance;\n }\n\n /**\n * Initialize and get the singleton instance of the cache manager.\n * Called internally by AppKit - prefer `getInstanceSync()` for plugin access.\n * @param userConfig - User configuration for the cache manager\n * @returns CacheManager instance\n * @internal\n */\n static async getInstance(\n userConfig?: Partial<CacheConfig>,\n ): Promise<CacheManager> {\n if (CacheManager.instance) {\n return CacheManager.instance;\n }\n\n if (!CacheManager.initPromise) {\n CacheManager.initPromise = CacheManager.create(userConfig).then(\n (instance) => {\n CacheManager.instance = instance;\n return instance;\n },\n );\n }\n\n return CacheManager.initPromise;\n }\n\n /**\n * Create a new cache manager instance\n *\n * Storage selection logic:\n * 1. If `storage` provided and healthy → use provided storage\n * 2. If `storage` provided but unhealthy → fallback to InMemory (or disable if strictPersistence)\n * 3. If no `storage` provided and Lakebase available → use Lakebase\n * 4. If no `storage` provided and Lakebase unavailable → fallback to InMemory (or disable if strictPersistence)\n *\n * @param userConfig - User configuration for the cache manager\n * @returns CacheManager instance\n */\n private static async create(\n userConfig?: Partial<CacheConfig>,\n ): Promise<CacheManager> {\n const config = deepMerge(cacheDefaults, userConfig);\n\n if (config.storage) {\n const isHealthy = await config.storage.healthCheck();\n if (isHealthy) {\n return new CacheManager(config.storage, config);\n }\n\n if (config.strictPersistence) {\n const disabledConfig = { ...config, enabled: false };\n return new CacheManager(\n new InMemoryStorage(disabledConfig),\n disabledConfig,\n );\n }\n\n return new CacheManager(new InMemoryStorage(config), config);\n }\n\n // try to use lakebase storage\n try {\n const workspaceClient = new WorkspaceClient({});\n const connector = new LakebaseConnector({ workspaceClient });\n const isHealthy = await connector.healthCheck();\n\n if (isHealthy) {\n const persistentStorage = new PersistentStorage(config, connector);\n await persistentStorage.initialize();\n return new CacheManager(persistentStorage, config);\n }\n } catch {\n // lakebase unavailable, continue with in-memory storage\n }\n\n if (config.strictPersistence) {\n const disabledConfig = { ...config, enabled: false };\n return new CacheManager(\n new InMemoryStorage(disabledConfig),\n disabledConfig,\n );\n }\n\n return new CacheManager(new InMemoryStorage(config), config);\n }\n\n /**\n * Get or execute a function and cache the result\n * @param key - Cache key\n * @param fn - Function to execute\n * @param userKey - User key\n * @param options - Options for the cache\n * @returns Promise of the result\n */\n async getOrExecute<T>(\n key: (string | number | object)[],\n fn: () => Promise<T>,\n userKey: string,\n options?: { ttl?: number },\n ): Promise<T> {\n if (!this.config.enabled) return fn();\n\n const cacheKey = this.generateKey(key, userKey);\n\n return this.telemetry.startActiveSpan(\n \"cache.getOrExecute\",\n {\n attributes: {\n \"cache.key\": cacheKey,\n \"cache.enabled\": this.config.enabled,\n \"cache.persistent\": this.storage.isPersistent(),\n },\n },\n async (span) => {\n try {\n // check if the value is in the cache\n const cached = await this.storage.get<T>(cacheKey);\n if (cached !== null) {\n span.setAttribute(\"cache.hit\", true);\n span.setStatus({ code: SpanStatusCode.OK });\n this.telemetryMetrics.cacheHitCount.add(1, {\n \"cache.key\": cacheKey,\n });\n\n logger.event()?.setExecution({\n cache_hit: true,\n cache_key: cacheKey,\n });\n\n return cached.value as T;\n }\n\n // check if the value is being processed by another request\n const inFlight = this.inFlightRequests.get(cacheKey);\n if (inFlight) {\n span.setAttribute(\"cache.hit\", true);\n span.setAttribute(\"cache.deduplication\", true);\n span.addEvent(\"cache.deduplication_used\", {\n \"cache.key\": cacheKey,\n });\n span.setStatus({ code: SpanStatusCode.OK });\n this.telemetryMetrics.cacheHitCount.add(1, {\n \"cache.key\": cacheKey,\n \"cache.deduplication\": \"true\",\n });\n\n logger.event()?.setExecution({\n cache_hit: true,\n cache_key: cacheKey,\n cache_deduplication: true,\n });\n\n span.end();\n return inFlight as Promise<T>;\n }\n\n // cache miss - execute function\n span.setAttribute(\"cache.hit\", false);\n span.addEvent(\"cache.miss\", { \"cache.key\": cacheKey });\n this.telemetryMetrics.cacheMissCount.add(1, {\n \"cache.key\": cacheKey,\n });\n\n logger.event()?.setExecution({\n cache_hit: false,\n cache_key: cacheKey,\n });\n\n const promise = fn()\n .then(async (result) => {\n await this.set(cacheKey, result, options);\n span.addEvent(\"cache.value_stored\", {\n \"cache.key\": cacheKey,\n \"cache.ttl\": options?.ttl ?? this.config.ttl ?? 3600,\n });\n return result;\n })\n .catch((error) => {\n span.recordException(error);\n span.setStatus({ code: SpanStatusCode.ERROR });\n if (error instanceof AppKitError) {\n throw error;\n }\n throw ExecutionError.statementFailed(\n error instanceof Error ? error.message : String(error),\n );\n })\n .finally(() => {\n this.inFlightRequests.delete(cacheKey);\n });\n\n this.inFlightRequests.set(cacheKey, promise);\n\n const result = await promise;\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 { name: this.name, includePrefix: true },\n );\n }\n\n /**\n * Get a cached value\n * @param key - Cache key\n * @returns Promise of the value or null if not found or expired\n */\n async get<T>(key: string): Promise<T | null> {\n if (!this.config.enabled) return null;\n\n // probabilistic cleanup trigger\n this.maybeCleanup();\n\n const entry = await this.storage.get<T>(key);\n if (!entry) return null;\n\n if (Date.now() > entry.expiry) {\n await this.storage.delete(key);\n return null;\n }\n return entry.value as T;\n }\n\n /** Probabilistically trigger cleanup of expired entries (fire-and-forget) */\n private maybeCleanup(): void {\n if (this.cleanupInProgress) return;\n if (!this.storage.isPersistent()) return;\n const now = Date.now();\n if (now - this.lastCleanupAttempt < CacheManager.MIN_CLEANUP_INTERVAL_MS)\n return;\n\n const probability = this.config.cleanupProbability ?? 0.01;\n\n if (Math.random() > probability) return;\n\n this.lastCleanupAttempt = now;\n\n this.cleanupInProgress = true;\n (this.storage as PersistentStorage)\n .cleanupExpired()\n .catch((error) => {\n logger.debug(\"Error cleaning up expired entries: %O\", error);\n })\n .finally(() => {\n this.cleanupInProgress = false;\n });\n }\n\n /**\n * Set a value in the cache\n * @param key - Cache key\n * @param value - Value to set\n * @param options - Options for the cache\n * @returns Promise of the result\n */\n async set<T>(\n key: string,\n value: T,\n options?: { ttl?: number },\n ): Promise<void> {\n if (!this.config.enabled) return;\n\n const ttl = options?.ttl ?? this.config.ttl ?? 3600;\n const expiryTime = Date.now() + ttl * 1000;\n await this.storage.set(key, { value, expiry: expiryTime });\n }\n\n /**\n * Delete a value from the cache\n * @param key - Cache key\n * @returns Promise of the result\n */\n async delete(key: string): Promise<void> {\n if (!this.config.enabled) return;\n await this.storage.delete(key);\n }\n\n /** Clear the cache */\n async clear(): Promise<void> {\n await this.storage.clear();\n this.inFlightRequests.clear();\n }\n\n /**\n * Check if a value exists in the cache\n * @param key - Cache key\n * @returns Promise of true if the value exists, false otherwise\n */\n async has(key: string): Promise<boolean> {\n if (!this.config.enabled) return false;\n\n const entry = await this.storage.get(key);\n if (!entry) return false;\n\n if (Date.now() > entry.expiry) {\n await this.storage.delete(key);\n return false;\n }\n return true;\n }\n\n /**\n * Generate a cache key\n * @param parts - Parts of the key\n * @param userKey - User key\n * @returns Cache key\n */\n generateKey(parts: (string | number | object)[], userKey: string): string {\n const allParts = [userKey, ...parts];\n const serialized = JSON.stringify(allParts);\n return createHash(\"sha256\").update(serialized).digest(\"hex\");\n }\n\n /** Close the cache */\n async close(): Promise<void> {\n await this.storage.close();\n }\n\n /**\n * Check if the storage is healthy\n * @returns Promise of true if the storage is healthy, false otherwise\n */\n async isStorageHealthy(): Promise<boolean> {\n return this.storage.healthCheck();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;aAI6E;AAQ7E,MAAM,SAAS,aAAa,QAAQ;;;;;;;;;;;;;;;AAgBpC,IAAa,eAAb,MAAa,aAAa;;iCAC0B;;;kBAEH;;;qBACY;;CAc3D,AAAQ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ACxE,MAAM,WAAW,CAAC,SAAS,GAAG,MAAM;EACpC,MAAM,aAAa,KAAK,UAAU,SAAS;AAC3C,SAAO,WAAW,SAAS,CAAC,OAAO,WAAW,CAAC,OAAO,MAAM;;;CAI9D,MAAM,QAAuB;AAC3B,QAAM,KAAK,QAAQ,OAAO;;;;;;CAO5B,MAAM,mBAAqC;AACzC,SAAO,KAAK,QAAQ,aAAa"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
|
|
6
|
+
//#region src/cli/commands/docs.ts
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
function findPackageRoot() {
|
|
10
|
+
let dir = __dirname;
|
|
11
|
+
while (dir !== path.parse(dir).root) {
|
|
12
|
+
if (fs.existsSync(path.join(dir, "package.json"))) return dir;
|
|
13
|
+
dir = path.dirname(dir);
|
|
14
|
+
}
|
|
15
|
+
throw new Error("Could not find package root");
|
|
16
|
+
}
|
|
17
|
+
function runDocs(docPath) {
|
|
18
|
+
const packageRoot = findPackageRoot();
|
|
19
|
+
if (!docPath) {
|
|
20
|
+
const llmsPath = path.join(packageRoot, "llms.txt");
|
|
21
|
+
if (!fs.existsSync(llmsPath)) {
|
|
22
|
+
console.error("Error: llms.txt not found in package");
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
const content = fs.readFileSync(llmsPath, "utf-8");
|
|
26
|
+
console.log(content);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let normalizedPath = docPath;
|
|
30
|
+
normalizedPath = normalizedPath.replace(/^\.\//, "");
|
|
31
|
+
normalizedPath = normalizedPath.replace(/^\//, "");
|
|
32
|
+
normalizedPath = normalizedPath.replace(/^appkit\/docs\//, "");
|
|
33
|
+
normalizedPath = normalizedPath.replace(/^docs\//, "");
|
|
34
|
+
const fullPath = path.join(packageRoot, "docs", normalizedPath);
|
|
35
|
+
if (!fs.existsSync(fullPath)) {
|
|
36
|
+
console.error(`Error: Documentation file not found: ${docPath}`);
|
|
37
|
+
console.error(`Tried: ${fullPath}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
41
|
+
console.log(content);
|
|
42
|
+
}
|
|
43
|
+
const docsCommand = new Command("docs").description("Display embedded documentation").argument("[path]", "Path to specific documentation file (e.g., /appkit/docs/api/appkit-ui/components/Sidebar.md)").action(runDocs);
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { docsCommand };
|
|
47
|
+
//# sourceMappingURL=docs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docs.js","names":[],"sources":["../../../src/cli/commands/docs.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nfunction findPackageRoot(): string {\n let dir = __dirname;\n while (dir !== path.parse(dir).root) {\n if (fs.existsSync(path.join(dir, \"package.json\"))) {\n return dir;\n }\n dir = path.dirname(dir);\n }\n throw new Error(\"Could not find package root\");\n}\n\nfunction runDocs(docPath?: string) {\n const packageRoot = findPackageRoot();\n\n if (!docPath) {\n // Display llms.txt by default\n const llmsPath = path.join(packageRoot, \"llms.txt\");\n\n if (!fs.existsSync(llmsPath)) {\n console.error(\"Error: llms.txt not found in package\");\n process.exit(1);\n }\n\n const content = fs.readFileSync(llmsPath, \"utf-8\");\n console.log(content);\n return;\n }\n\n // Handle path - remove leading ./ and / first, then strip prefixes\n let normalizedPath = docPath;\n\n // Strip leading ./ or /\n normalizedPath = normalizedPath.replace(/^\\.\\//, \"\");\n normalizedPath = normalizedPath.replace(/^\\//, \"\");\n\n // Remove /appkit/docs/ or docs/ prefix since files are in packageRoot/docs/\n normalizedPath = normalizedPath.replace(/^appkit\\/docs\\//, \"\");\n normalizedPath = normalizedPath.replace(/^docs\\//, \"\");\n\n const fullPath = path.join(packageRoot, \"docs\", normalizedPath);\n\n if (!fs.existsSync(fullPath)) {\n console.error(`Error: Documentation file not found: ${docPath}`);\n console.error(`Tried: ${fullPath}`);\n process.exit(1);\n }\n\n const content = fs.readFileSync(fullPath, \"utf-8\");\n console.log(content);\n}\n\nexport const docsCommand = new Command(\"docs\")\n .description(\"Display embedded documentation\")\n .argument(\n \"[path]\",\n \"Path to specific documentation file (e.g., /appkit/docs/api/appkit-ui/components/Sidebar.md)\",\n )\n .action(runDocs);\n"],"mappings":";;;;;;AAKA,MAAM,aAAa,cAAc,OAAO,KAAK,IAAI;AACjD,MAAM,YAAY,KAAK,QAAQ,WAAW;AAE1C,SAAS,kBAA0B;CACjC,IAAI,MAAM;AACV,QAAO,QAAQ,KAAK,MAAM,IAAI,CAAC,MAAM;AACnC,MAAI,GAAG,WAAW,KAAK,KAAK,KAAK,eAAe,CAAC,CAC/C,QAAO;AAET,QAAM,KAAK,QAAQ,IAAI;;AAEzB,OAAM,IAAI,MAAM,8BAA8B;;AAGhD,SAAS,QAAQ,SAAkB;CACjC,MAAM,cAAc,iBAAiB;AAErC,KAAI,CAAC,SAAS;EAEZ,MAAM,WAAW,KAAK,KAAK,aAAa,WAAW;AAEnD,MAAI,CAAC,GAAG,WAAW,SAAS,EAAE;AAC5B,WAAQ,MAAM,uCAAuC;AACrD,WAAQ,KAAK,EAAE;;EAGjB,MAAM,UAAU,GAAG,aAAa,UAAU,QAAQ;AAClD,UAAQ,IAAI,QAAQ;AACpB;;CAIF,IAAI,iBAAiB;AAGrB,kBAAiB,eAAe,QAAQ,SAAS,GAAG;AACpD,kBAAiB,eAAe,QAAQ,OAAO,GAAG;AAGlD,kBAAiB,eAAe,QAAQ,mBAAmB,GAAG;AAC9D,kBAAiB,eAAe,QAAQ,WAAW,GAAG;CAEtD,MAAM,WAAW,KAAK,KAAK,aAAa,QAAQ,eAAe;AAE/D,KAAI,CAAC,GAAG,WAAW,SAAS,EAAE;AAC5B,UAAQ,MAAM,wCAAwC,UAAU;AAChE,UAAQ,MAAM,UAAU,WAAW;AACnC,UAAQ,KAAK,EAAE;;CAGjB,MAAM,UAAU,GAAG,aAAa,UAAU,QAAQ;AAClD,SAAQ,IAAI,QAAQ;;AAGtB,MAAa,cAAc,IAAI,QAAQ,OAAO,CAC3C,YAAY,iCAAiC,CAC7C,SACC,UACA,+FACD,CACA,OAAO,QAAQ"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
|
|
4
|
+
//#region src/cli/commands/generate-types.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generate types command implementation
|
|
7
|
+
*/
|
|
8
|
+
async function runGenerateTypes(rootDir, outFile, warehouseId, options) {
|
|
9
|
+
try {
|
|
10
|
+
const { generateFromEntryPoint } = await import("@databricks/appkit/type-generator");
|
|
11
|
+
const resolvedRootDir = rootDir || process.cwd();
|
|
12
|
+
const resolvedOutFile = outFile || path.join(process.cwd(), "client/src/appKitTypes.d.ts");
|
|
13
|
+
const queryFolder = path.join(resolvedRootDir, "config/queries");
|
|
14
|
+
const resolvedWarehouseId = warehouseId || process.env.DATABRICKS_WAREHOUSE_ID;
|
|
15
|
+
if (!resolvedWarehouseId) {
|
|
16
|
+
console.error("Error: DATABRICKS_WAREHOUSE_ID is not set. Please provide it as an argument or environment variable.");
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
await generateFromEntryPoint({
|
|
20
|
+
queryFolder,
|
|
21
|
+
outFile: resolvedOutFile,
|
|
22
|
+
warehouseId: resolvedWarehouseId,
|
|
23
|
+
noCache: options?.noCache || false
|
|
24
|
+
});
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
27
|
+
console.error("Error: The 'generate-types' command is only available in @databricks/appkit.");
|
|
28
|
+
console.error("Please install @databricks/appkit to use this command.");
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const generateTypesCommand = new Command("generate-types").description("Generate TypeScript types from SQL queries").argument("[rootDir]", "Root directory of the project", process.cwd()).argument("[outFile]", "Output file path", path.join(process.cwd(), "client/src/appKitTypes.d.ts")).argument("[warehouseId]", "Databricks warehouse ID").option("--no-cache", "Disable caching for type generation").action(runGenerateTypes);
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
export { generateTypesCommand };
|
|
38
|
+
//# sourceMappingURL=generate-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-types.js","names":[],"sources":["../../../src/cli/commands/generate-types.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport path from \"node:path\";\n\n/**\n * Generate types command implementation\n */\nasync function runGenerateTypes(\n rootDir?: string,\n outFile?: string,\n warehouseId?: string,\n options?: { noCache?: boolean },\n) {\n try {\n // Try to import the type generator from @databricks/appkit\n const { generateFromEntryPoint } = await import(\n \"@databricks/appkit/type-generator\"\n );\n\n const resolvedRootDir = rootDir || process.cwd();\n const resolvedOutFile =\n outFile || path.join(process.cwd(), \"client/src/appKitTypes.d.ts\");\n\n const queryFolder = path.join(resolvedRootDir, \"config/queries\");\n\n const resolvedWarehouseId =\n warehouseId || process.env.DATABRICKS_WAREHOUSE_ID;\n if (!resolvedWarehouseId) {\n console.error(\n \"Error: DATABRICKS_WAREHOUSE_ID is not set. Please provide it as an argument or environment variable.\",\n );\n process.exit(1);\n }\n\n await generateFromEntryPoint({\n queryFolder,\n outFile: resolvedOutFile,\n warehouseId: resolvedWarehouseId,\n noCache: options?.noCache || false,\n });\n } catch (error) {\n if (\n error instanceof Error &&\n error.message.includes(\"Cannot find module\")\n ) {\n console.error(\n \"Error: The 'generate-types' command is only available in @databricks/appkit.\",\n );\n console.error(\"Please install @databricks/appkit to use this command.\");\n process.exit(1);\n }\n throw error;\n }\n}\n\nexport const generateTypesCommand = new Command(\"generate-types\")\n .description(\"Generate TypeScript types from SQL queries\")\n .argument(\"[rootDir]\", \"Root directory of the project\", process.cwd())\n .argument(\n \"[outFile]\",\n \"Output file path\",\n path.join(process.cwd(), \"client/src/appKitTypes.d.ts\"),\n )\n .argument(\"[warehouseId]\", \"Databricks warehouse ID\")\n .option(\"--no-cache\", \"Disable caching for type generation\")\n .action(runGenerateTypes);\n"],"mappings":";;;;;;;AAMA,eAAe,iBACb,SACA,SACA,aACA,SACA;AACA,KAAI;EAEF,MAAM,EAAE,2BAA2B,MAAM,OACvC;EAGF,MAAM,kBAAkB,WAAW,QAAQ,KAAK;EAChD,MAAM,kBACJ,WAAW,KAAK,KAAK,QAAQ,KAAK,EAAE,8BAA8B;EAEpE,MAAM,cAAc,KAAK,KAAK,iBAAiB,iBAAiB;EAEhE,MAAM,sBACJ,eAAe,QAAQ,IAAI;AAC7B,MAAI,CAAC,qBAAqB;AACxB,WAAQ,MACN,uGACD;AACD,WAAQ,KAAK,EAAE;;AAGjB,QAAM,uBAAuB;GAC3B;GACA,SAAS;GACT,aAAa;GACb,SAAS,SAAS,WAAW;GAC9B,CAAC;UACK,OAAO;AACd,MACE,iBAAiB,SACjB,MAAM,QAAQ,SAAS,qBAAqB,EAC5C;AACA,WAAQ,MACN,+EACD;AACD,WAAQ,MAAM,yDAAyD;AACvE,WAAQ,KAAK,EAAE;;AAEjB,QAAM;;;AAIV,MAAa,uBAAuB,IAAI,QAAQ,iBAAiB,CAC9D,YAAY,6CAA6C,CACzD,SAAS,aAAa,iCAAiC,QAAQ,KAAK,CAAC,CACrE,SACC,aACA,oBACA,KAAK,KAAK,QAAQ,KAAK,EAAE,8BAA8B,CACxD,CACA,SAAS,iBAAiB,0BAA0B,CACpD,OAAO,cAAc,sCAAsC,CAC3D,OAAO,iBAAiB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { Lang, parse } from "@ast-grep/napi";
|
|
5
|
+
|
|
6
|
+
//#region src/cli/commands/lint.ts
|
|
7
|
+
const rules = [
|
|
8
|
+
{
|
|
9
|
+
id: "no-double-type-assertion",
|
|
10
|
+
pattern: "$X as unknown as $Y",
|
|
11
|
+
message: "Avoid double type assertion (as unknown as). Use proper type guards or fix the source type."
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: "no-as-any",
|
|
15
|
+
pattern: "$X as any",
|
|
16
|
+
message: "Avoid \"as any\" type assertion. Use proper typing or unknown with type guards.",
|
|
17
|
+
includeTests: false
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "no-array-index-key",
|
|
21
|
+
pattern: "key={$IDX}",
|
|
22
|
+
message: "Avoid using array index as React key. Use a stable unique identifier.",
|
|
23
|
+
filter: (code) => /key=\{(idx|index|i)\}/.test(code)
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "no-parse-float-without-validation",
|
|
27
|
+
pattern: "parseFloat($X).toFixed($Y)",
|
|
28
|
+
message: "parseFloat can return NaN. Validate input or use toNumber() helper from shared/types.ts."
|
|
29
|
+
}
|
|
30
|
+
];
|
|
31
|
+
function isTestFile(filePath) {
|
|
32
|
+
return /\.(test|spec)\.(ts|tsx)$/.test(filePath) || filePath.includes("/tests/");
|
|
33
|
+
}
|
|
34
|
+
function findTsFiles(dir, files = []) {
|
|
35
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
const fullPath = path.join(dir, entry.name);
|
|
38
|
+
if (entry.isDirectory()) {
|
|
39
|
+
if ([
|
|
40
|
+
"node_modules",
|
|
41
|
+
"dist",
|
|
42
|
+
"build",
|
|
43
|
+
".git"
|
|
44
|
+
].includes(entry.name)) continue;
|
|
45
|
+
findTsFiles(fullPath, files);
|
|
46
|
+
} else if (entry.isFile() && /\.(ts|tsx)$/.test(entry.name)) files.push(fullPath);
|
|
47
|
+
}
|
|
48
|
+
return files;
|
|
49
|
+
}
|
|
50
|
+
function lintFile(filePath, rules) {
|
|
51
|
+
const violations = [];
|
|
52
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
53
|
+
const lang = filePath.endsWith(".tsx") ? Lang.Tsx : Lang.TypeScript;
|
|
54
|
+
const testFile = isTestFile(filePath);
|
|
55
|
+
const root = parse(lang, content).root();
|
|
56
|
+
for (const rule of rules) {
|
|
57
|
+
if (testFile && rule.includeTests === false) continue;
|
|
58
|
+
const matches = root.findAll(rule.pattern);
|
|
59
|
+
for (const match of matches) {
|
|
60
|
+
const code = match.text();
|
|
61
|
+
if (rule.filter && !rule.filter(code)) continue;
|
|
62
|
+
const range = match.range();
|
|
63
|
+
violations.push({
|
|
64
|
+
file: filePath,
|
|
65
|
+
line: range.start.line + 1,
|
|
66
|
+
column: range.start.column + 1,
|
|
67
|
+
rule: rule.id,
|
|
68
|
+
message: rule.message,
|
|
69
|
+
code: code.length > 80 ? `${code.slice(0, 77)}...` : code
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return violations;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Lint command implementation
|
|
77
|
+
*/
|
|
78
|
+
function runLint() {
|
|
79
|
+
const rootDir = process.cwd();
|
|
80
|
+
const files = findTsFiles(rootDir);
|
|
81
|
+
console.log(`Scanning ${files.length} TypeScript files...\n`);
|
|
82
|
+
const allViolations = [];
|
|
83
|
+
for (const file of files) {
|
|
84
|
+
const violations = lintFile(file, rules);
|
|
85
|
+
allViolations.push(...violations);
|
|
86
|
+
}
|
|
87
|
+
if (allViolations.length === 0) {
|
|
88
|
+
console.log("No ast-grep lint violations found.");
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}
|
|
91
|
+
console.log(`Found ${allViolations.length} violation(s):\n`);
|
|
92
|
+
for (const v of allViolations) {
|
|
93
|
+
const relPath = path.relative(rootDir, v.file);
|
|
94
|
+
console.log(`${relPath}:${v.line}:${v.column}`);
|
|
95
|
+
console.log(` ${v.rule}: ${v.message}`);
|
|
96
|
+
console.log(` > ${v.code}\n`);
|
|
97
|
+
}
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
const lintCommand = new Command("lint").description("Run AST-based linting on TypeScript files").action(runLint);
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { lintCommand };
|
|
104
|
+
//# sourceMappingURL=lint.js.map
|