@dbt-tools/web 0.3.9 → 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/README.md +112 -170
- package/dist-serve/server/cli.js +528 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,246 +1,188 @@
|
|
|
1
1
|
# @dbt-tools/web
|
|
2
2
|
|
|
3
|
-
React
|
|
3
|
+
React application for visual dbt artifact analysis: dependency graphs, execution timelines, inventory views, and optional remote runs from S3 or GCS.
|
|
4
|
+
|
|
5
|
+
**End users:** install from npm and run **`dbt-tools-web`** (see below). **Contributors:** clone the monorepo and use Vite — see [Developing from source](#developing-from-source) and [CONTRIBUTING.md](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/CONTRIBUTING.md).
|
|
6
|
+
|
|
7
|
+
Full operator topics (Docker, GHCR, remote sources, Vite-only options) live in the [user guide](../../../docs/user-guide-dbt-tools-web.md).
|
|
4
8
|
|
|
5
9
|
---
|
|
6
10
|
|
|
7
11
|
## Prerequisites
|
|
8
12
|
|
|
9
|
-
- **Node.js**
|
|
10
|
-
-
|
|
11
|
-
- A dbt project with a `./target/` directory containing `manifest.json` and/or `run_results.json`
|
|
13
|
+
- **Node.js** — use the version in [`.node-version`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/.node-version) when developing; **Node.js 20+** is required to run the published app (Node 18 is EOL — see [Node.js releases](https://nodejs.org/en/about/previous-releases)).
|
|
14
|
+
- A dbt **`target/`** directory (or object storage) with **`manifest.json`** and **`run_results.json`** when you want preloaded artifacts.
|
|
12
15
|
|
|
13
16
|
---
|
|
14
17
|
|
|
15
|
-
##
|
|
18
|
+
## Install and run (npm)
|
|
19
|
+
|
|
20
|
+
The package publishes a small static server plus the **`dbt-tools-web`** binary ([source](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-tools/web/src/server/cli.ts)).
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
```bash
|
|
23
|
+
npm install -g @dbt-tools/web
|
|
24
|
+
dbt-tools-web --target /path/to/your/dbt/target
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Or without a global install:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx @dbt-tools/web --target /path/to/your/dbt/target
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`npx` invokes the package’s binary (`dbt-tools-web`). Useful flags:
|
|
34
|
+
|
|
35
|
+
| Flag | Description |
|
|
36
|
+
| ----------------------- | ---------------------------------------------------- |
|
|
37
|
+
| `--target <dir>` / `-t` | dbt `target` directory (sets `DBT_TOOLS_TARGET_DIR`) |
|
|
38
|
+
| `--port <n>` / `-p` | Listen port (default **3000**) |
|
|
39
|
+
| `--no-open` | Do not open a browser |
|
|
40
|
+
| `--help` / `-h` | Usage |
|
|
41
|
+
|
|
42
|
+
The server listens on **127.0.0.1** and prints the URL (e.g. `http://127.0.0.1:3000`).
|
|
43
|
+
|
|
44
|
+
You can also set **`DBT_TOOLS_TARGET_DIR`** (or legacy `DBT_TARGET_DIR` / `DBT_TARGET`) in the environment instead of `--target`.
|
|
26
45
|
|
|
27
46
|
---
|
|
28
47
|
|
|
29
48
|
## Features
|
|
30
49
|
|
|
31
|
-
- **Dependency graph
|
|
32
|
-
- **Execution timeline** — Gantt-style
|
|
33
|
-
- **
|
|
34
|
-
- **Remote
|
|
35
|
-
- **Large
|
|
50
|
+
- **Dependency graph** — interactive lineage
|
|
51
|
+
- **Execution timeline** — Gantt-style `run_results` with critical path
|
|
52
|
+
- **Local artifacts** — read `manifest.json` / `run_results.json` from a target directory via server-side routes
|
|
53
|
+
- **Remote sources (S3 / GCS)** — optional `DBT_TOOLS_REMOTE_SOURCE`; server-side credentials; UI prompts before switching runs ([ADR-0029](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/docs/adr/0029-remote-object-storage-artifact-sources-and-auto-reload.md))
|
|
54
|
+
- **Large manifests** — web workers and virtualization for very large projects
|
|
36
55
|
|
|
37
56
|
---
|
|
38
57
|
|
|
39
|
-
## Architecture
|
|
58
|
+
## Architecture (runtime)
|
|
40
59
|
|
|
41
60
|
```mermaid
|
|
42
61
|
graph TD
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
62
|
+
subgraph npmServer [dbt-tools-web_published]
|
|
63
|
+
BIN[dbt-tools-web_CLI]
|
|
64
|
+
HTTP[Node_HTTP_server]
|
|
65
|
+
ART[ArtifactSourceService\nlocal_dir_or_remote]
|
|
66
|
+
BIN --> HTTP
|
|
67
|
+
HTTP --> ART
|
|
68
|
+
ART -->|GET_/api/...| API[manifest.json_run_results_routes]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
APP[React_app_static_dist]
|
|
72
|
+
WW[Web_Worker]
|
|
73
|
+
CORE["@dbt-tools/core/browser"]
|
|
74
|
+
|
|
75
|
+
HTTP -->|static_files| APP
|
|
51
76
|
APP -->|postMessage| WW
|
|
52
77
|
WW --> CORE
|
|
53
|
-
CORE -->|graph · analysis results| WW
|
|
54
|
-
WW -->|postMessage| APP
|
|
55
|
-
APP -->|renders| UI[Dependency Graph\nExecution Timeline\nSummary Stats]
|
|
56
78
|
```
|
|
57
79
|
|
|
58
|
-
Heavy analysis runs in a web worker
|
|
80
|
+
Heavy analysis runs in a **web worker** using `@dbt-tools/core/browser`. The same artifact HTTP surface is used in **Vite dev** (monorepo) with extra file-watching behavior — see the [user guide](../../../docs/user-guide-dbt-tools-web.md#vite-dev-server-monorepo).
|
|
59
81
|
|
|
60
82
|
---
|
|
61
83
|
|
|
62
|
-
##
|
|
63
|
-
|
|
64
|
-
The web app is not published to npm. Run it from the monorepo:
|
|
65
|
-
|
|
66
|
-
```bash
|
|
67
|
-
# From repo root
|
|
68
|
-
pnpm dev:web
|
|
69
|
-
|
|
70
|
-
# Or from the package directory
|
|
71
|
-
cd packages/dbt-tools/web
|
|
72
|
-
pnpm dev
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Preloading artifacts from a local dbt project
|
|
84
|
+
## Configuration (`dbt-tools-web` and production server)
|
|
76
85
|
|
|
77
|
-
Set
|
|
78
|
-
|
|
79
|
-
```bash
|
|
80
|
-
DBT_TOOLS_TARGET_DIR=./target pnpm dev
|
|
81
|
-
# or use the dev:target convenience script from the package directory:
|
|
82
|
-
pnpm dev:target # shorthand for DBT_TOOLS_TARGET_DIR=./target vite
|
|
83
|
-
```
|
|
86
|
+
Set these in the environment for the **Node process** that runs `dbt-tools-web` (not in the browser):
|
|
84
87
|
|
|
85
|
-
|
|
88
|
+
| Variable | Description |
|
|
89
|
+
| ------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
90
|
+
| `DBT_TOOLS_TARGET_DIR` | Directory containing `manifest.json` and `run_results.json` (unless using remote source) |
|
|
91
|
+
| `DBT_TOOLS_REMOTE_SOURCE` | JSON config for S3/GCS discovery (server-side only); see [user guide](../../../docs/user-guide-dbt-tools-web.md) |
|
|
92
|
+
| `DBT_TOOLS_DEBUG` | Set to `1` for server-side debug logs (legacy: `DBT_DEBUG`) |
|
|
86
93
|
|
|
87
|
-
|
|
94
|
+
**Deprecated (still read):** `DBT_TARGET`, `DBT_TARGET_DIR`, `DBT_DEBUG`.
|
|
88
95
|
|
|
89
|
-
|
|
96
|
+
**Client:** add **`?debug=1`** to the URL for browser console debug logging.
|
|
90
97
|
|
|
91
|
-
|
|
92
|
-
- **Client-side** (browser console): add `?debug=1` to the URL
|
|
98
|
+
**Vite-only (monorepo dev):** `DBT_TOOLS_WATCH`, `DBT_TOOLS_RELOAD_DEBOUNCE_MS` — file watch and auto-reload; **not** used by the published `dbt-tools-web` binary. See the [user guide](../../../docs/user-guide-dbt-tools-web.md#vite-dev-server-monorepo).
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
DBT_TOOLS_DEBUG=1 DBT_TOOLS_TARGET_DIR=~/path/to/target pnpm dev
|
|
96
|
-
# then open: http://localhost:5173/?debug=1
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### Auto-reload When Artifacts Change
|
|
100
|
-
|
|
101
|
-
When `DBT_TOOLS_TARGET_DIR` is set, the app automatically reloads and re-analyzes when `manifest.json` or `run_results.json` change on disk (e.g. after `dbt run`):
|
|
102
|
-
|
|
103
|
-
| Variable | Default | Description |
|
|
104
|
-
| ------------------------------ | ------- | --------------------------------------------------- |
|
|
105
|
-
| `DBT_TOOLS_WATCH` | on | Set to `0` to disable file watching and auto-reload |
|
|
106
|
-
| `DBT_TOOLS_RELOAD_DEBOUNCE_MS` | `300` | Debounce in ms for rapid file writes |
|
|
100
|
+
---
|
|
107
101
|
|
|
108
|
-
|
|
102
|
+
## Docker and container images
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
# Disable auto-reload
|
|
112
|
-
DBT_TOOLS_WATCH=0 DBT_TOOLS_TARGET_DIR=./target pnpm dev
|
|
113
|
-
```
|
|
104
|
+
For building the **nginx static image** from the monorepo, GHCR tags, and limitations (static `dist/` only), see [user guide — Docker & GHCR](../../../docs/user-guide-dbt-tools-web.md#docker).
|
|
114
105
|
|
|
115
106
|
---
|
|
116
107
|
|
|
117
|
-
##
|
|
118
|
-
|
|
119
|
-
All configuration is via environment variables passed to the Vite dev server or build:
|
|
120
|
-
|
|
121
|
-
| Variable | Default | Description |
|
|
122
|
-
| ------------------------------ | ------- | -------------------------------------------------------------------------------------------------- |
|
|
123
|
-
| `DBT_TOOLS_TARGET_DIR` | — | Directory containing `manifest.json` and `run_results.json`; enables the `/api/*` proxy middleware |
|
|
124
|
-
| `DBT_TOOLS_REMOTE_SOURCE` | — | JSON config for S3 or GCS bucket + prefix discovery (server-side access only); see ADR-0029 |
|
|
125
|
-
| `DBT_TOOLS_DEBUG` | unset | Set to `1` to enable server-side debug logging in the Vite middleware |
|
|
126
|
-
| `DBT_TOOLS_WATCH` | on | Set to `0` to disable file watching and auto-reload when artifacts change |
|
|
127
|
-
| `DBT_TOOLS_RELOAD_DEBOUNCE_MS` | `300` | Debounce delay in ms before triggering a reload on rapid file writes |
|
|
108
|
+
## Troubleshooting
|
|
128
109
|
|
|
129
|
-
|
|
110
|
+
| Symptom | What to check |
|
|
111
|
+
| ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
112
|
+
| Blank UI / no artifacts | Pass **`--target`** or set **`DBT_TOOLS_TARGET_DIR`** to a folder that contains **`manifest.json`** (and ideally `run_results.json`). For remote mode, set **`DBT_TOOLS_REMOTE_SOURCE`**. |
|
|
113
|
+
| `GET /api/...` 404 | Target dir missing files, wrong path, or remote config not returning a complete pair. |
|
|
114
|
+
| Expected “hot reload” after `dbt run` | The **npm** server re-reads files when the app fetches them; refresh the browser. **File watch + auto-reload** is a **Vite dev** feature — see the [user guide](../../../docs/user-guide-dbt-tools-web.md). |
|
|
115
|
+
| Slow UI on huge projects | Prefer the latest version; very large graphs still benefit from narrowing scope in the UI. |
|
|
130
116
|
|
|
131
|
-
|
|
117
|
+
More rows and fixes: [user guide — Troubleshooting](../../../docs/user-guide-dbt-tools-web.md#troubleshooting).
|
|
132
118
|
|
|
133
119
|
---
|
|
134
120
|
|
|
135
|
-
##
|
|
121
|
+
## Developing from source
|
|
136
122
|
|
|
137
|
-
|
|
138
|
-
pnpm build
|
|
139
|
-
# Output in dist/
|
|
140
|
-
pnpm preview # serve the production build locally
|
|
141
|
-
```
|
|
123
|
+
For **clone, pnpm install, build order, lint, and tests**, use [CONTRIBUTING.md](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/CONTRIBUTING.md).
|
|
142
124
|
|
|
143
|
-
###
|
|
125
|
+
### Tech stack
|
|
144
126
|
|
|
145
|
-
|
|
127
|
+
| Layer | Technology |
|
|
128
|
+
| -------------- | ------------------------------------------------------------- |
|
|
129
|
+
| UI | [React](https://react.dev/) |
|
|
130
|
+
| Build | [Vite](https://vitejs.dev/) |
|
|
131
|
+
| Charts | [Recharts](https://recharts.org/) |
|
|
132
|
+
| Virtualization | [@tanstack/react-virtual](https://tanstack.com/virtual) |
|
|
133
|
+
| Analysis | `@dbt-tools/core` / `@dbt-tools/core/browser` in a web worker |
|
|
134
|
+
| E2E | [Playwright](https://playwright.dev/) |
|
|
135
|
+
| Language | TypeScript |
|
|
146
136
|
|
|
147
|
-
|
|
137
|
+
### Monorepo commands
|
|
148
138
|
|
|
149
139
|
```bash
|
|
150
140
|
# From repository root
|
|
151
|
-
|
|
152
|
-
docker run --rm -p 8080:8080 dbt-tools-web:local
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
Then open `http://localhost:8080/`.
|
|
156
|
-
|
|
157
|
-
The production image is static files only: there is no Vite dev middleware, so `DBT_TOOLS_TARGET_DIR` and related server-side env vars from local dev do not apply unless you add a different deployment shape. Any future **Vite build-time** variables (`VITE_*`) must be passed at image build time, for example `docker build --build-arg VITE_EXAMPLE=...` (and your Dockerfile would need to forward them into the build step).
|
|
158
|
-
|
|
159
|
-
#### GitHub Container Registry (CI)
|
|
160
|
-
|
|
161
|
-
The workflow [`.github/workflows/docker-dbt-tools-web.yml`](../../../.github/workflows/docker-dbt-tools-web.yml) builds this Dockerfile on `push` to `main`, on `pull_request` (build only, no push), and via `workflow_dispatch`. Images are pushed to **GHCR**:
|
|
141
|
+
pnpm dev:web
|
|
162
142
|
|
|
163
|
-
|
|
143
|
+
# Or from this package
|
|
144
|
+
cd packages/dbt-tools/web
|
|
145
|
+
pnpm dev
|
|
146
|
+
```
|
|
164
147
|
|
|
165
|
-
|
|
148
|
+
Preload local artifacts (Vite):
|
|
166
149
|
|
|
167
150
|
```bash
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
docker pull ghcr.io/<github-owner-lowercase>/dbt-tools-web:latest
|
|
151
|
+
DBT_TOOLS_TARGET_DIR=./target pnpm dev
|
|
152
|
+
# or: pnpm dev:target
|
|
171
153
|
```
|
|
172
154
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
---
|
|
176
|
-
|
|
177
|
-
## Project Structure
|
|
155
|
+
### Project layout (abridged)
|
|
178
156
|
|
|
179
157
|
```text
|
|
180
158
|
packages/dbt-tools/web/
|
|
181
159
|
├── src/
|
|
182
|
-
│ ├── components/
|
|
183
|
-
│
|
|
184
|
-
│
|
|
185
|
-
│
|
|
186
|
-
│
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
│ ├── workers/ # Web worker for off-thread analysis
|
|
190
|
-
│ ├── constants/ # Theme colors and shared constants
|
|
191
|
-
│ ├── lib/ # Analysis workspace helpers
|
|
192
|
-
│ ├── styles/ # CSS tokens, base, and component styles
|
|
193
|
-
│ ├── App.tsx # Root application component
|
|
194
|
-
│ └── main.tsx # Entry point
|
|
195
|
-
├── e2e/ # Playwright end-to-end test specs
|
|
196
|
-
├── vite.config.ts # Vite + Vite middleware (DBT_TOOLS_TARGET_DIR proxy)
|
|
197
|
-
└── package.json
|
|
160
|
+
│ ├── components/ # React UI (AnalysisWorkspace, AppShell, ui)
|
|
161
|
+
│ ├── artifact-source/ # Local + remote artifact HTTP surface
|
|
162
|
+
│ ├── server/ # dbt-tools-web CLI + static server
|
|
163
|
+
│ ├── workers/ # Analysis web worker
|
|
164
|
+
│ └── ...
|
|
165
|
+
├── e2e/ # Playwright specs
|
|
166
|
+
└── vite.config.ts
|
|
198
167
|
```
|
|
199
168
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
## E2E Tests
|
|
203
|
-
|
|
204
|
-
The web app has Playwright end-to-end tests:
|
|
169
|
+
### E2E tests
|
|
205
170
|
|
|
206
171
|
```bash
|
|
207
172
|
pnpm test:e2e
|
|
208
173
|
```
|
|
209
174
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
## Troubleshooting
|
|
215
|
-
|
|
216
|
-
| Symptom | Fix |
|
|
217
|
-
| ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
218
|
-
| Blank page / "No artifacts found" | Ensure `DBT_TOOLS_TARGET_DIR` points to a directory that contains `manifest.json` |
|
|
219
|
-
| Auto-reload not triggering | Check `DBT_TOOLS_WATCH` is not set to `0`; verify the file watcher has read access to the target directory |
|
|
220
|
-
| Slow UI with large manifests | The web worker and virtualized lists should handle 100k+ nodes; if performance still degrades, open the browser profiler and check for main-thread analysis code paths |
|
|
221
|
-
| `GET /api/manifest.json` returns 404 | `DBT_TOOLS_TARGET_DIR` is not set or the Vite dev server was started without it |
|
|
222
|
-
| Debug logs not appearing | Server-side: restart dev with `DBT_TOOLS_DEBUG=1`; client-side: add `?debug=1` to the URL |
|
|
223
|
-
|
|
224
|
-
---
|
|
225
|
-
|
|
226
|
-
## Development
|
|
227
|
-
|
|
228
|
-
```bash
|
|
229
|
-
pnpm build # TypeScript + Vite build
|
|
230
|
-
pnpm dev # Vite dev server with HMR
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
See [CONTRIBUTING.md](../../../CONTRIBUTING.md) for the full developer guide, including how to set up the monorepo and run all tests.
|
|
175
|
+
(from repo root: `pnpm test:e2e` as documented in CONTRIBUTING)
|
|
234
176
|
|
|
235
177
|
---
|
|
236
178
|
|
|
237
|
-
## Related
|
|
179
|
+
## Related packages
|
|
238
180
|
|
|
239
|
-
| Package
|
|
240
|
-
|
|
|
241
|
-
| [`@dbt-tools/core`](
|
|
242
|
-
| [`@dbt-tools/cli`](
|
|
243
|
-
| [`dbt-artifacts-parser`](
|
|
181
|
+
| Package | Role |
|
|
182
|
+
| ------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
|
|
183
|
+
| [`@dbt-tools/core`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-tools/core/README.md) | Analysis engine |
|
|
184
|
+
| [`@dbt-tools/cli`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-tools/cli/README.md) | CLI (`dbt-tools`) |
|
|
185
|
+
| [`dbt-artifacts-parser`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-artifacts-parser/README.md) | Artifact parsing |
|
|
244
186
|
|
|
245
187
|
---
|
|
246
188
|
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { exec } from "node:child_process";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import http from "node:http";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import fs$1 from "node:fs/promises";
|
|
8
|
+
import { DBT_MANIFEST_JSON, DBT_RUN_RESULTS_JSON, getDbtToolsRemoteSourceConfigFromEnv, getDbtToolsTargetDirFromEnv, isDbtToolsDebugEnabled } from "@dbt-tools/core";
|
|
9
|
+
import { GetObjectCommand, ListObjectsV2Command, S3Client } from "@aws-sdk/client-s3";
|
|
10
|
+
import { Storage } from "@google-cloud/storage";
|
|
11
|
+
//#region src/artifact-source/prefix.ts
|
|
12
|
+
/** Trim leading/trailing slashes for object-store key prefix handling. */
|
|
13
|
+
function normalizeArtifactPrefix(prefix) {
|
|
14
|
+
return prefix.replace(/^\/+|\/+$/g, "");
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/artifact-source/discovery.ts
|
|
18
|
+
function toRelativeKey(key, prefix) {
|
|
19
|
+
const normalizedKey = key.replace(/^\/+/, "");
|
|
20
|
+
const normalizedPrefix = normalizeArtifactPrefix(prefix);
|
|
21
|
+
if (normalizedPrefix === "") return normalizedKey;
|
|
22
|
+
if (normalizedKey === normalizedPrefix) return "";
|
|
23
|
+
if (normalizedKey.startsWith(`${normalizedPrefix}/`)) return normalizedKey.slice(normalizedPrefix.length + 1);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
function runIdForKey(relativeKey) {
|
|
27
|
+
const dir = path.posix.dirname(relativeKey);
|
|
28
|
+
return dir === "." ? "current" : dir;
|
|
29
|
+
}
|
|
30
|
+
function versionTokenForKeys(parts) {
|
|
31
|
+
return parts.map((part) => [
|
|
32
|
+
part.key,
|
|
33
|
+
part.updatedAtMs,
|
|
34
|
+
part.etag ?? "",
|
|
35
|
+
part.generation ?? ""
|
|
36
|
+
].join(":")).join("|");
|
|
37
|
+
}
|
|
38
|
+
function discoverLatestArtifactRuns(objects, prefix) {
|
|
39
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
40
|
+
for (const object of objects) {
|
|
41
|
+
const relativeKey = toRelativeKey(object.key, prefix);
|
|
42
|
+
if (relativeKey == null) continue;
|
|
43
|
+
const fileName = path.posix.basename(relativeKey);
|
|
44
|
+
if (fileName !== DBT_MANIFEST_JSON && fileName !== DBT_RUN_RESULTS_JSON) continue;
|
|
45
|
+
const runId = runIdForKey(relativeKey);
|
|
46
|
+
const current = grouped.get(runId) ?? {};
|
|
47
|
+
if (fileName === DBT_MANIFEST_JSON) current.manifest = object;
|
|
48
|
+
if (fileName === DBT_RUN_RESULTS_JSON) current.runResults = object;
|
|
49
|
+
grouped.set(runId, current);
|
|
50
|
+
}
|
|
51
|
+
return [...grouped.entries()].flatMap(([runId, parts]) => {
|
|
52
|
+
if (parts.manifest == null || parts.runResults == null) return [];
|
|
53
|
+
return [{
|
|
54
|
+
runId,
|
|
55
|
+
manifestKey: parts.manifest.key,
|
|
56
|
+
runResultsKey: parts.runResults.key,
|
|
57
|
+
updatedAtMs: Math.max(parts.manifest.updatedAtMs, parts.runResults.updatedAtMs),
|
|
58
|
+
versionToken: versionTokenForKeys([parts.manifest, parts.runResults])
|
|
59
|
+
}];
|
|
60
|
+
}).sort((left, right) => {
|
|
61
|
+
if (right.updatedAtMs !== left.updatedAtMs) return right.updatedAtMs - left.updatedAtMs;
|
|
62
|
+
return right.runId.localeCompare(left.runId);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function toRemoteArtifactRun(provider, run) {
|
|
66
|
+
return {
|
|
67
|
+
runId: run.runId,
|
|
68
|
+
label: run.runId === "current" ? `${provider.toUpperCase()} current` : `${provider.toUpperCase()} ${run.runId}`,
|
|
69
|
+
updatedAtMs: run.updatedAtMs,
|
|
70
|
+
versionToken: run.versionToken
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/artifact-source/remoteObjectStore.ts
|
|
75
|
+
var S3RemoteObjectStoreClient = class {
|
|
76
|
+
client;
|
|
77
|
+
constructor(config) {
|
|
78
|
+
this.client = new S3Client({
|
|
79
|
+
region: config.region,
|
|
80
|
+
endpoint: config.endpoint,
|
|
81
|
+
forcePathStyle: config.forcePathStyle
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async listObjects(bucket, prefix) {
|
|
85
|
+
const results = [];
|
|
86
|
+
let continuationToken;
|
|
87
|
+
do {
|
|
88
|
+
const response = await this.client.send(new ListObjectsV2Command({
|
|
89
|
+
Bucket: bucket,
|
|
90
|
+
Prefix: prefix === "" ? void 0 : `${prefix}/`,
|
|
91
|
+
ContinuationToken: continuationToken
|
|
92
|
+
}));
|
|
93
|
+
for (const object of response.Contents ?? []) {
|
|
94
|
+
if (!object.Key || !object.LastModified) continue;
|
|
95
|
+
results.push({
|
|
96
|
+
key: object.Key,
|
|
97
|
+
updatedAtMs: object.LastModified.getTime(),
|
|
98
|
+
etag: object.ETag ?? void 0
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
continuationToken = response.IsTruncated ? response.NextContinuationToken : void 0;
|
|
102
|
+
} while (continuationToken);
|
|
103
|
+
return results;
|
|
104
|
+
}
|
|
105
|
+
async readObjectBytes(bucket, key) {
|
|
106
|
+
const bytes = await (await this.client.send(new GetObjectCommand({
|
|
107
|
+
Bucket: bucket,
|
|
108
|
+
Key: key
|
|
109
|
+
}))).Body?.transformToByteArray();
|
|
110
|
+
if (bytes == null) throw new Error(`Missing S3 object body for ${key}`);
|
|
111
|
+
return bytes;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var GcsRemoteObjectStoreClient = class {
|
|
115
|
+
storage;
|
|
116
|
+
constructor(config) {
|
|
117
|
+
this.storage = new Storage({ projectId: config.projectId });
|
|
118
|
+
}
|
|
119
|
+
async listObjects(bucket, prefix) {
|
|
120
|
+
const [files] = await this.storage.bucket(bucket).getFiles({
|
|
121
|
+
prefix: prefix === "" ? void 0 : `${prefix}/`,
|
|
122
|
+
autoPaginate: true
|
|
123
|
+
});
|
|
124
|
+
return files.flatMap((file) => {
|
|
125
|
+
const updated = file.metadata.updated;
|
|
126
|
+
if (!updated) return [];
|
|
127
|
+
return [{
|
|
128
|
+
key: file.name,
|
|
129
|
+
updatedAtMs: new Date(updated).getTime(),
|
|
130
|
+
etag: file.metadata.etag,
|
|
131
|
+
generation: file.metadata.generation == null ? void 0 : String(file.metadata.generation)
|
|
132
|
+
}];
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async readObjectBytes(bucket, key) {
|
|
136
|
+
const [bytes] = await this.storage.bucket(bucket).file(key).download();
|
|
137
|
+
return bytes;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
function createRemoteObjectStoreClient(config) {
|
|
141
|
+
return config.provider === "s3" ? new S3RemoteObjectStoreClient(config) : new GcsRemoteObjectStoreClient(config);
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/artifact-source/resolveLocalTargetDir.ts
|
|
145
|
+
function expandDbtTargetDirFromEnvValue(targetDir) {
|
|
146
|
+
return targetDir.replace(/^~($|\/)/, `${process.env.HOME ?? ""}$1`).trim();
|
|
147
|
+
}
|
|
148
|
+
function resolveLocalArtifactTargetDirFromEnv(cwd, rawTargetDir) {
|
|
149
|
+
return path.resolve(cwd, expandDbtTargetDirFromEnvValue(rawTargetDir));
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/artifact-source/sourceService.ts
|
|
153
|
+
/** Node (Vite dev) service: local target dir vs remote S3/GCS; drives middleware routes. */
|
|
154
|
+
function debugLog(...args) {
|
|
155
|
+
if (isDbtToolsDebugEnabled()) console.log("[artifact-source]", ...args);
|
|
156
|
+
}
|
|
157
|
+
function toLocationLabel(provider, bucket, prefix) {
|
|
158
|
+
return `${provider.toUpperCase()} ${bucket}/${prefix}`;
|
|
159
|
+
}
|
|
160
|
+
function toArtifactSourceStatus(status) {
|
|
161
|
+
return {
|
|
162
|
+
...status,
|
|
163
|
+
checkedAtMs: Date.now()
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
var LocalArtifactSourceAdapter = class {
|
|
167
|
+
constructor(targetDir) {
|
|
168
|
+
this.targetDir = targetDir;
|
|
169
|
+
}
|
|
170
|
+
async getStatus() {
|
|
171
|
+
return toArtifactSourceStatus({
|
|
172
|
+
mode: "preload",
|
|
173
|
+
currentSource: "preload",
|
|
174
|
+
label: "Live target",
|
|
175
|
+
remoteProvider: null,
|
|
176
|
+
remoteLocation: null,
|
|
177
|
+
pollIntervalMs: null,
|
|
178
|
+
currentRun: null,
|
|
179
|
+
pendingRun: null,
|
|
180
|
+
supportsSwitch: false
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async getCurrentArtifacts() {
|
|
184
|
+
try {
|
|
185
|
+
const [manifestBytes, runResultsBytes] = await Promise.all([fs$1.readFile(path.join(this.targetDir, DBT_MANIFEST_JSON)), fs$1.readFile(path.join(this.targetDir, DBT_RUN_RESULTS_JSON))]);
|
|
186
|
+
return {
|
|
187
|
+
source: "preload",
|
|
188
|
+
manifestBytes,
|
|
189
|
+
runResultsBytes
|
|
190
|
+
};
|
|
191
|
+
} catch {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async switchToRun() {
|
|
196
|
+
return this.getStatus();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
var RemoteArtifactSourceAdapter = class {
|
|
200
|
+
currentRunId = null;
|
|
201
|
+
constructor(config, client) {
|
|
202
|
+
this.config = config;
|
|
203
|
+
this.client = client;
|
|
204
|
+
}
|
|
205
|
+
async resolveRuns() {
|
|
206
|
+
return discoverLatestArtifactRuns(await this.client.listObjects(this.config.bucket, normalizeArtifactPrefix(this.config.prefix)), this.config.prefix);
|
|
207
|
+
}
|
|
208
|
+
chooseCurrentRun(runs) {
|
|
209
|
+
if (runs.length === 0) return null;
|
|
210
|
+
if (this.currentRunId == null) {
|
|
211
|
+
this.currentRunId = runs[0].runId;
|
|
212
|
+
return runs[0];
|
|
213
|
+
}
|
|
214
|
+
const current = runs.find((run) => run.runId === this.currentRunId);
|
|
215
|
+
if (current) return current;
|
|
216
|
+
this.currentRunId = runs[0].runId;
|
|
217
|
+
return runs[0];
|
|
218
|
+
}
|
|
219
|
+
async getStatus() {
|
|
220
|
+
const runs = await this.resolveRuns();
|
|
221
|
+
const currentRun = this.chooseCurrentRun(runs);
|
|
222
|
+
const latestRun = runs[0] ?? null;
|
|
223
|
+
const pendingRun = latestRun != null && currentRun != null && latestRun.runId !== currentRun.runId ? latestRun : null;
|
|
224
|
+
return toArtifactSourceStatus({
|
|
225
|
+
mode: "remote",
|
|
226
|
+
currentSource: currentRun == null ? null : "remote",
|
|
227
|
+
label: "Remote source",
|
|
228
|
+
remoteProvider: this.config.provider,
|
|
229
|
+
remoteLocation: toLocationLabel(this.config.provider, this.config.bucket, normalizeArtifactPrefix(this.config.prefix)),
|
|
230
|
+
pollIntervalMs: this.config.pollIntervalMs,
|
|
231
|
+
currentRun: currentRun == null ? null : toRemoteArtifactRun(this.config.provider, currentRun),
|
|
232
|
+
pendingRun: pendingRun == null ? null : toRemoteArtifactRun(this.config.provider, pendingRun),
|
|
233
|
+
supportsSwitch: pendingRun != null
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async getCurrentArtifacts() {
|
|
237
|
+
const runs = await this.resolveRuns();
|
|
238
|
+
const currentRun = this.chooseCurrentRun(runs);
|
|
239
|
+
if (currentRun == null) return null;
|
|
240
|
+
const [manifestBytes, runResultsBytes] = await Promise.all([this.client.readObjectBytes(this.config.bucket, currentRun.manifestKey), this.client.readObjectBytes(this.config.bucket, currentRun.runResultsKey)]);
|
|
241
|
+
return {
|
|
242
|
+
source: "remote",
|
|
243
|
+
manifestBytes,
|
|
244
|
+
runResultsBytes
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
async switchToRun(runId) {
|
|
248
|
+
const runs = await this.resolveRuns();
|
|
249
|
+
const target = runId == null ? runs[0] : runs.find((candidate) => candidate.runId === runId);
|
|
250
|
+
if (target != null) {
|
|
251
|
+
this.currentRunId = target.runId;
|
|
252
|
+
debugLog("Switched remote artifact run", this.currentRunId);
|
|
253
|
+
}
|
|
254
|
+
return this.getStatus();
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var ArtifactSourceService = class {
|
|
258
|
+
adapter;
|
|
259
|
+
constructor(options = {}) {
|
|
260
|
+
if (options.adapter !== void 0) {
|
|
261
|
+
this.adapter = options.adapter;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const remoteConfig = options.remoteConfig === void 0 ? getDbtToolsRemoteSourceConfigFromEnv() : options.remoteConfig;
|
|
265
|
+
if (remoteConfig != null) {
|
|
266
|
+
debugLog("Configured remote artifact source", remoteConfig.provider, remoteConfig.bucket, remoteConfig.prefix);
|
|
267
|
+
this.adapter = new RemoteArtifactSourceAdapter(remoteConfig, options.remoteClient ?? createRemoteObjectStoreClient(remoteConfig));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const targetDir = options.targetDir === void 0 ? getDbtToolsTargetDirFromEnv() : options.targetDir;
|
|
271
|
+
if (targetDir == null) {
|
|
272
|
+
this.adapter = null;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.adapter = new LocalArtifactSourceAdapter(resolveLocalArtifactTargetDirFromEnv(process.cwd(), targetDir));
|
|
276
|
+
}
|
|
277
|
+
async getStatus() {
|
|
278
|
+
if (this.adapter == null) return toArtifactSourceStatus({
|
|
279
|
+
mode: "none",
|
|
280
|
+
currentSource: null,
|
|
281
|
+
label: "Waiting for artifacts",
|
|
282
|
+
remoteProvider: null,
|
|
283
|
+
remoteLocation: null,
|
|
284
|
+
pollIntervalMs: null,
|
|
285
|
+
currentRun: null,
|
|
286
|
+
pendingRun: null,
|
|
287
|
+
supportsSwitch: false
|
|
288
|
+
});
|
|
289
|
+
return this.adapter.getStatus();
|
|
290
|
+
}
|
|
291
|
+
async getCurrentArtifacts() {
|
|
292
|
+
return this.adapter?.getCurrentArtifacts() ?? null;
|
|
293
|
+
}
|
|
294
|
+
async switchToRun(runId) {
|
|
295
|
+
if (this.adapter == null) return this.getStatus();
|
|
296
|
+
return this.adapter.switchToRun(runId);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/artifact-source/viteArtifactRoutes.ts
|
|
301
|
+
async function readJsonBody(request) {
|
|
302
|
+
const chunks = [];
|
|
303
|
+
for await (const chunk of request) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
304
|
+
if (chunks.length === 0) return {};
|
|
305
|
+
try {
|
|
306
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
307
|
+
} catch {
|
|
308
|
+
return {};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function sendJson(res, statusCode, payload) {
|
|
312
|
+
res.setHeader("Content-Type", "application/json");
|
|
313
|
+
res.statusCode = statusCode;
|
|
314
|
+
res.end(JSON.stringify(payload));
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Vite middleware handler for artifact-source HTTP routes. Returns `true` when
|
|
318
|
+
* the request was fully handled (response ended).
|
|
319
|
+
*/
|
|
320
|
+
async function tryHandleArtifactSourceViteRequest(req, res, service) {
|
|
321
|
+
const pathname = req.url?.split("?")[0];
|
|
322
|
+
if (!pathname) return false;
|
|
323
|
+
if (req.method === "GET" && pathname === "/api/artifact-source") {
|
|
324
|
+
sendJson(res, 200, await service.getStatus());
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (req.method === "POST" && pathname === "/api/artifact-source/switch") {
|
|
328
|
+
const body = await readJsonBody(req);
|
|
329
|
+
const runId = typeof body.runId === "string" && body.runId.trim() !== "" ? body.runId : void 0;
|
|
330
|
+
sendJson(res, 200, await service.switchToRun(runId));
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
if (req.method === "GET" && (pathname === `/api/${DBT_MANIFEST_JSON}` || pathname === `/api/artifacts/current/${DBT_MANIFEST_JSON}` || pathname === `/api/${DBT_RUN_RESULTS_JSON}` || pathname === `/api/artifacts/current/${DBT_RUN_RESULTS_JSON}`)) {
|
|
334
|
+
const current = await service.getCurrentArtifacts();
|
|
335
|
+
if (current == null) {
|
|
336
|
+
res.statusCode = 404;
|
|
337
|
+
res.end();
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
const bytes = pathname.endsWith(DBT_MANIFEST_JSON) ? current.manifestBytes : current.runResultsBytes;
|
|
341
|
+
res.setHeader("Content-Type", "application/json");
|
|
342
|
+
res.statusCode = 200;
|
|
343
|
+
res.end(Buffer.from(bytes));
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/server/serve.ts
|
|
350
|
+
var __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
351
|
+
var DIST_DIR = path.resolve(__dirname, "../../dist");
|
|
352
|
+
var DIST_ROOT_REAL;
|
|
353
|
+
try {
|
|
354
|
+
DIST_ROOT_REAL = fs.realpathSync(DIST_DIR);
|
|
355
|
+
} catch {
|
|
356
|
+
DIST_ROOT_REAL = path.resolve(DIST_DIR);
|
|
357
|
+
}
|
|
358
|
+
var INDEX_HTML = path.join(DIST_DIR, "index.html");
|
|
359
|
+
/** Loopback host used for listen and advertised URLs (IPv4; avoids localhost → ::1 mismatch). */
|
|
360
|
+
var LISTEN_HOST = "127.0.0.1";
|
|
361
|
+
var MIME = {
|
|
362
|
+
".html": "text/html; charset=utf-8",
|
|
363
|
+
".js": "application/javascript",
|
|
364
|
+
".mjs": "application/javascript",
|
|
365
|
+
".css": "text/css",
|
|
366
|
+
".json": "application/json",
|
|
367
|
+
".svg": "image/svg+xml",
|
|
368
|
+
".png": "image/png",
|
|
369
|
+
".ico": "image/x-icon",
|
|
370
|
+
".woff": "font/woff",
|
|
371
|
+
".woff2": "font/woff2",
|
|
372
|
+
".ttf": "font/ttf",
|
|
373
|
+
".txt": "text/plain"
|
|
374
|
+
};
|
|
375
|
+
function isContainedUnderRoot(root, candidateResolved) {
|
|
376
|
+
const rel = path.relative(root, candidateResolved);
|
|
377
|
+
return rel === "" || !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Path safe for `fs.*`: validate relative to logical `DIST_DIR`, then rebuild with
|
|
381
|
+
* `path.join(DIST_ROOT_REAL, rel)` + `realpathSync` so CodeQL `js/path-injection` sees a guarded join.
|
|
382
|
+
*/
|
|
383
|
+
function toSafeAbsoluteUnderDist(candidatePath) {
|
|
384
|
+
const resolvedInput = path.resolve(candidatePath);
|
|
385
|
+
const rel = path.relative(DIST_DIR, resolvedInput);
|
|
386
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) return INDEX_HTML;
|
|
387
|
+
const underReal = path.join(DIST_ROOT_REAL, rel);
|
|
388
|
+
try {
|
|
389
|
+
const canonical = fs.realpathSync(underReal);
|
|
390
|
+
return isContainedUnderRoot(DIST_ROOT_REAL, canonical) ? canonical : INDEX_HTML;
|
|
391
|
+
} catch {
|
|
392
|
+
return underReal;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* URL path → filesystem path under dist (traversal + symlink escape). Symlinked `DIST_DIR` uses logical containment first.
|
|
397
|
+
*/
|
|
398
|
+
function resolveStaticPath(urlPath) {
|
|
399
|
+
const pathOnly = (urlPath.startsWith("/") ? urlPath : `/${urlPath}`).split("?")[0] ?? "/";
|
|
400
|
+
let decoded;
|
|
401
|
+
try {
|
|
402
|
+
decoded = decodeURIComponent(pathOnly);
|
|
403
|
+
} catch {
|
|
404
|
+
return INDEX_HTML;
|
|
405
|
+
}
|
|
406
|
+
const relativeSegment = decoded.replace(/^\/+/, "") || ".";
|
|
407
|
+
const resolved = path.resolve(DIST_DIR, relativeSegment);
|
|
408
|
+
if (!isContainedUnderRoot(DIST_DIR, resolved)) return INDEX_HTML;
|
|
409
|
+
try {
|
|
410
|
+
const canonical = fs.realpathSync(resolved);
|
|
411
|
+
if (!isContainedUnderRoot(DIST_ROOT_REAL, canonical)) return INDEX_HTML;
|
|
412
|
+
return canonical;
|
|
413
|
+
} catch {
|
|
414
|
+
return resolved;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function requestPathname(req) {
|
|
418
|
+
return (req.url ?? "/").split("?")[0] ?? "/";
|
|
419
|
+
}
|
|
420
|
+
/** 500 body, or destroy if a response was already started (e.g. mid-stream read failure). */
|
|
421
|
+
function sendInternalError(res, destroyIfPartial) {
|
|
422
|
+
if (!res.headersSent) {
|
|
423
|
+
res.statusCode = 500;
|
|
424
|
+
res.end("Internal server error");
|
|
425
|
+
} else if (destroyIfPartial) res.destroy();
|
|
426
|
+
}
|
|
427
|
+
function sendFileWithOptionalSpaFallback(primaryPath, res, allowSpaFallback) {
|
|
428
|
+
const safePath = toSafeAbsoluteUnderDist(primaryPath);
|
|
429
|
+
const stream = fs.createReadStream(safePath);
|
|
430
|
+
stream.on("error", () => {
|
|
431
|
+
stream.destroy();
|
|
432
|
+
if (allowSpaFallback && !res.headersSent) {
|
|
433
|
+
sendFileWithOptionalSpaFallback(INDEX_HTML, res, false);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
sendInternalError(res, true);
|
|
437
|
+
});
|
|
438
|
+
const ext = path.extname(safePath).toLowerCase();
|
|
439
|
+
res.setHeader("Content-Type", MIME[ext] ?? "application/octet-stream");
|
|
440
|
+
res.statusCode = 200;
|
|
441
|
+
stream.pipe(res);
|
|
442
|
+
}
|
|
443
|
+
function serveStaticFile(filePath, res) {
|
|
444
|
+
const safeResolved = toSafeAbsoluteUnderDist(filePath);
|
|
445
|
+
const safeIndex = toSafeAbsoluteUnderDist(INDEX_HTML);
|
|
446
|
+
let target = safeResolved;
|
|
447
|
+
if (!fs.existsSync(target) || fs.statSync(target).isDirectory()) target = safeIndex;
|
|
448
|
+
sendFileWithOptionalSpaFallback(target, res, safeResolved !== safeIndex);
|
|
449
|
+
}
|
|
450
|
+
function sendApiNotFound(res) {
|
|
451
|
+
res.statusCode = 404;
|
|
452
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
453
|
+
res.end(JSON.stringify({ error: "not_found" }));
|
|
454
|
+
}
|
|
455
|
+
function startServer(port) {
|
|
456
|
+
const service = new ArtifactSourceService();
|
|
457
|
+
const server = http.createServer((req, res) => {
|
|
458
|
+
(async () => {
|
|
459
|
+
try {
|
|
460
|
+
if (await tryHandleArtifactSourceViteRequest(req, res, service)) return;
|
|
461
|
+
const pathname = requestPathname(req);
|
|
462
|
+
if (pathname === "/api" || pathname.startsWith("/api/")) {
|
|
463
|
+
sendApiNotFound(res);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
serveStaticFile(resolveStaticPath(req.url ?? "/"), res);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
sendInternalError(res, false);
|
|
469
|
+
console.error("[dbt-tools-web]", err);
|
|
470
|
+
}
|
|
471
|
+
})();
|
|
472
|
+
});
|
|
473
|
+
return new Promise((resolve, reject) => {
|
|
474
|
+
server.once("error", reject);
|
|
475
|
+
server.listen(port, LISTEN_HOST, () => {
|
|
476
|
+
resolve();
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/server/cli.ts
|
|
482
|
+
var USAGE = `
|
|
483
|
+
Usage: dbt-tools-web [options]
|
|
484
|
+
|
|
485
|
+
--target <dir> Path to dbt target directory (sets DBT_TOOLS_TARGET_DIR)
|
|
486
|
+
--port <n> Port to listen on (default: 3000)
|
|
487
|
+
--no-open Do not open the browser automatically
|
|
488
|
+
--help Show this help message
|
|
489
|
+
`.trimStart();
|
|
490
|
+
function parseArgs(argv) {
|
|
491
|
+
let targetDir;
|
|
492
|
+
let port = 3e3;
|
|
493
|
+
let open = true;
|
|
494
|
+
for (let i = 0; i < argv.length; i++) {
|
|
495
|
+
const arg = argv[i];
|
|
496
|
+
if (arg === "--help" || arg === "-h") {
|
|
497
|
+
process.stdout.write(USAGE);
|
|
498
|
+
process.exit(0);
|
|
499
|
+
} else if (arg === "--no-open") open = false;
|
|
500
|
+
else if ((arg === "--target" || arg === "-t") && argv[i + 1]) targetDir = argv[++i];
|
|
501
|
+
else if ((arg === "--port" || arg === "-p") && argv[i + 1]) {
|
|
502
|
+
const parsed = parseInt(argv[++i], 10);
|
|
503
|
+
if (isNaN(parsed) || parsed < 1 || parsed > 65535) {
|
|
504
|
+
process.stderr.write(`Invalid port: ${argv[i]}\n`);
|
|
505
|
+
process.exit(1);
|
|
506
|
+
}
|
|
507
|
+
port = parsed;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return {
|
|
511
|
+
targetDir,
|
|
512
|
+
port,
|
|
513
|
+
open
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
function openBrowser(url) {
|
|
517
|
+
exec(process.platform === "darwin" ? `open ${url}` : process.platform === "win32" ? `start "" "${url}"` : `xdg-open ${url}`, (err) => {
|
|
518
|
+
if (err) {}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
var { targetDir, port, open } = parseArgs(process.argv.slice(2));
|
|
522
|
+
if (targetDir !== void 0) process.env.DBT_TOOLS_TARGET_DIR = targetDir;
|
|
523
|
+
await startServer(port);
|
|
524
|
+
var url = `http://${LISTEN_HOST}:${port}`;
|
|
525
|
+
process.stdout.write(`dbt-tools-web ${url}\n`);
|
|
526
|
+
if (open) openBrowser(url);
|
|
527
|
+
//#endregion
|
|
528
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbt-tools/web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Web application for visual dbt artifact analysis",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dbt",
|
|
@@ -9,9 +9,16 @@
|
|
|
9
9
|
],
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"author": "yu-iskw",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=20"
|
|
14
|
+
},
|
|
12
15
|
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"dbt-tools-web": "./dist-serve/server/cli.js"
|
|
18
|
+
},
|
|
13
19
|
"files": [
|
|
14
20
|
"dist",
|
|
21
|
+
"dist-serve",
|
|
15
22
|
"README.md"
|
|
16
23
|
],
|
|
17
24
|
"dependencies": {
|
|
@@ -22,8 +29,8 @@
|
|
|
22
29
|
"react-dom": "^19.2.4",
|
|
23
30
|
"react-markdown": "^10.1.0",
|
|
24
31
|
"remark-gfm": "^4.0.1",
|
|
25
|
-
"dbt-
|
|
26
|
-
"
|
|
32
|
+
"@dbt-tools/core": "0.4.0",
|
|
33
|
+
"dbt-artifacts-parser": "0.4.0"
|
|
27
34
|
},
|
|
28
35
|
"devDependencies": {
|
|
29
36
|
"@playwright/test": "^1.58.2",
|
|
@@ -38,7 +45,8 @@
|
|
|
38
45
|
"access": "public"
|
|
39
46
|
},
|
|
40
47
|
"scripts": {
|
|
41
|
-
"build": "pnpm --filter @dbt-tools/core build && tsc && vite build",
|
|
48
|
+
"build": "pnpm --filter @dbt-tools/core build && tsc && vite build && pnpm run build:serve",
|
|
49
|
+
"build:serve": "vite build --config vite.server.config.ts",
|
|
42
50
|
"dev": "vite",
|
|
43
51
|
"dev:debug": "DBT_TOOLS_DEBUG=1 vite",
|
|
44
52
|
"dev:target": "DBT_TOOLS_TARGET_DIR=./target vite",
|