@dbt-tools/web 0.3.9 → 0.4.1
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 +135 -157
- package/dist-serve/server/cli.js +622 -0
- package/package.json +14 -5
package/README.md
CHANGED
|
@@ -1,246 +1,224 @@
|
|
|
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)).
|
|
21
|
+
|
|
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 |
|
|
16
41
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
| Build tool | [Vite 6](https://vitejs.dev/) |
|
|
21
|
-
| Charts | [Recharts](https://recharts.org/) |
|
|
22
|
-
| Virtualization | [@tanstack/react-virtual](https://tanstack.com/virtual) |
|
|
23
|
-
| Analysis engine | `@dbt-tools/core` (web worker, `@dbt-tools/core/browser` export) |
|
|
24
|
-
| E2E tests | [Playwright](https://playwright.dev/) |
|
|
25
|
-
| Language | TypeScript 5 |
|
|
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
|
-
##
|
|
84
|
+
## Configuration (`dbt-tools-web` and production server)
|
|
63
85
|
|
|
64
|
-
|
|
86
|
+
Set these in the environment for the **Node process** that runs `dbt-tools-web` (not in the browser):
|
|
65
87
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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`) |
|
|
69
93
|
|
|
70
|
-
|
|
71
|
-
cd packages/dbt-tools/web
|
|
72
|
-
pnpm dev
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Preloading artifacts from a local dbt project
|
|
76
|
-
|
|
77
|
-
Set `DBT_TOOLS_TARGET_DIR` to serve `manifest.json` and `run_results.json` from that directory:
|
|
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
|
-
```
|
|
94
|
+
**Deprecated (still read):** `DBT_TARGET`, `DBT_TARGET_DIR`, `DBT_DEBUG`.
|
|
84
95
|
|
|
85
|
-
|
|
96
|
+
**Client:** add **`?debug=1`** to the URL for browser console debug logging.
|
|
86
97
|
|
|
87
|
-
|
|
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).
|
|
88
99
|
|
|
89
|
-
|
|
100
|
+
---
|
|
90
101
|
|
|
91
|
-
|
|
92
|
-
- **Client-side** (browser console): add `?debug=1` to the URL
|
|
102
|
+
## Docker and container images
|
|
93
103
|
|
|
94
|
-
|
|
95
|
-
DBT_TOOLS_DEBUG=1 DBT_TOOLS_TARGET_DIR=~/path/to/target pnpm dev
|
|
96
|
-
# then open: http://localhost:5173/?debug=1
|
|
97
|
-
```
|
|
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).
|
|
98
105
|
|
|
99
|
-
|
|
106
|
+
---
|
|
100
107
|
|
|
101
|
-
|
|
108
|
+
## Troubleshooting
|
|
102
109
|
|
|
103
|
-
|
|
|
104
|
-
|
|
|
105
|
-
|
|
|
106
|
-
| `
|
|
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. |
|
|
107
116
|
|
|
108
|
-
|
|
117
|
+
If **`npx`** against a local **`.tgz`** fails with **`Permission denied`**, use `npx -y ./name.tgz -- --help` from the directory that contains the file, or `npx -y --package=/abs/path/name.tgz -- dbt-tools-web …`. See **Verify publish locally** under [Developing from source](#developing-from-source).
|
|
109
118
|
|
|
110
|
-
|
|
111
|
-
# Disable auto-reload
|
|
112
|
-
DBT_TOOLS_WATCH=0 DBT_TOOLS_TARGET_DIR=./target pnpm dev
|
|
113
|
-
```
|
|
119
|
+
More rows and fixes: [user guide — Troubleshooting](../../../docs/user-guide-dbt-tools-web.md#troubleshooting).
|
|
114
120
|
|
|
115
121
|
---
|
|
116
122
|
|
|
117
|
-
##
|
|
123
|
+
## Developing from source
|
|
118
124
|
|
|
119
|
-
|
|
125
|
+
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).
|
|
120
126
|
|
|
121
|
-
|
|
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 |
|
|
127
|
+
### Tech stack
|
|
128
128
|
|
|
129
|
-
|
|
129
|
+
| Layer | Technology |
|
|
130
|
+
| -------------- | ------------------------------------------------------------- |
|
|
131
|
+
| UI | [React](https://react.dev/) |
|
|
132
|
+
| Build | [Vite](https://vitejs.dev/) |
|
|
133
|
+
| Charts | [Recharts](https://recharts.org/) |
|
|
134
|
+
| Virtualization | [@tanstack/react-virtual](https://tanstack.com/virtual) |
|
|
135
|
+
| Analysis | `@dbt-tools/core` / `@dbt-tools/core/browser` in a web worker |
|
|
136
|
+
| E2E | [Playwright](https://playwright.dev/) |
|
|
137
|
+
| Language | TypeScript |
|
|
130
138
|
|
|
131
|
-
|
|
139
|
+
### Monorepo commands
|
|
132
140
|
|
|
133
|
-
|
|
141
|
+
```bash
|
|
142
|
+
# From repository root
|
|
143
|
+
pnpm dev:web
|
|
144
|
+
|
|
145
|
+
# Or from this package
|
|
146
|
+
cd packages/dbt-tools/web
|
|
147
|
+
pnpm dev
|
|
148
|
+
```
|
|
134
149
|
|
|
135
|
-
|
|
150
|
+
Preload local artifacts (Vite):
|
|
136
151
|
|
|
137
152
|
```bash
|
|
138
|
-
pnpm
|
|
139
|
-
#
|
|
140
|
-
pnpm preview # serve the production build locally
|
|
153
|
+
DBT_TOOLS_TARGET_DIR=./target pnpm dev
|
|
154
|
+
# or: pnpm dev:target
|
|
141
155
|
```
|
|
142
156
|
|
|
143
|
-
###
|
|
157
|
+
### Verify publish locally (tarball + `npx`)
|
|
144
158
|
|
|
145
|
-
|
|
159
|
+
To smoke-test the published **`dbt-tools-web`** entrypoint and tarball layout **without publishing to the public npm registry**:
|
|
146
160
|
|
|
147
|
-
**
|
|
161
|
+
- **Recommended (matches CI):** from the **monorepo root** after `pnpm install`, run Verdaccio, publish `dbt-artifacts-parser` → `@dbt-tools/core` → `@dbt-tools/web`, pack, then `npx` with `NPM_CONFIG_REGISTRY` pointing at that registry:
|
|
148
162
|
|
|
149
163
|
```bash
|
|
150
|
-
|
|
151
|
-
docker build -f packages/dbt-tools/web/Dockerfile -t dbt-tools-web:local .
|
|
152
|
-
docker run --rm -p 8080:8080 dbt-tools-web:local
|
|
164
|
+
bash scripts/smoke-npx-with-verdaccio.sh
|
|
153
165
|
```
|
|
154
166
|
|
|
155
|
-
|
|
167
|
+
This avoids **`No matching version found for @dbt-tools/core@…`** when those versions are not yet on npm (packed tarballs list concrete semver peers).
|
|
156
168
|
|
|
157
|
-
|
|
169
|
+
- **Manual pack only** (only reliable if peer versions already exist on the registry `npx` uses, or you set `NPM_CONFIG_REGISTRY` accordingly):
|
|
158
170
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
171
|
+
```bash
|
|
172
|
+
pnpm --filter @dbt-tools/web pack
|
|
173
|
+
```
|
|
162
174
|
|
|
163
|
-
`
|
|
175
|
+
`prepack` runs the full web build and writes **`dbt-tools-web-<version>.tgz` at the repo root** (the file is gitignored).
|
|
164
176
|
|
|
165
|
-
|
|
177
|
+
- **Empty directory** so `npx` does not see the monorepo’s `node_modules`:
|
|
166
178
|
|
|
167
179
|
```bash
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
180
|
+
cd "$(mktemp -d)"
|
|
181
|
+
# After copying the .tgz into this directory (adjust version to match package.json):
|
|
182
|
+
npx -y ./dbt-tools-web-0.4.1.tgz -- --help
|
|
171
183
|
```
|
|
172
184
|
|
|
173
|
-
|
|
185
|
+
If you pass a **bare absolute path** to the tarball, some `npx` versions try to execute it as a shell script and fail (`Permission denied`). Prefer either a **relative** `./…tgz` path or an explicit package spec:
|
|
174
186
|
|
|
175
|
-
|
|
187
|
+
```bash
|
|
188
|
+
npx -y --package=/absolute/path/to/dbt-tools-web-0.4.1.tgz -- dbt-tools-web --help
|
|
189
|
+
```
|
|
176
190
|
|
|
177
|
-
|
|
191
|
+
### Project layout (abridged)
|
|
178
192
|
|
|
179
193
|
```text
|
|
180
194
|
packages/dbt-tools/web/
|
|
181
195
|
├── 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
|
|
196
|
+
│ ├── components/ # React UI (AnalysisWorkspace, AppShell, ui)
|
|
197
|
+
│ ├── artifact-source/ # Local + remote artifact HTTP surface
|
|
198
|
+
│ ├── server/ # dbt-tools-web CLI + static server
|
|
199
|
+
│ ├── workers/ # Analysis web worker
|
|
200
|
+
│ └── ...
|
|
201
|
+
├── e2e/ # Playwright specs
|
|
202
|
+
└── vite.config.ts
|
|
198
203
|
```
|
|
199
204
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
## E2E Tests
|
|
203
|
-
|
|
204
|
-
The web app has Playwright end-to-end tests:
|
|
205
|
+
### E2E tests
|
|
205
206
|
|
|
206
207
|
```bash
|
|
207
208
|
pnpm test:e2e
|
|
208
209
|
```
|
|
209
210
|
|
|
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.
|
|
211
|
+
(from repo root: `pnpm test:e2e` as documented in CONTRIBUTING)
|
|
234
212
|
|
|
235
213
|
---
|
|
236
214
|
|
|
237
|
-
## Related
|
|
215
|
+
## Related packages
|
|
238
216
|
|
|
239
|
-
| Package
|
|
240
|
-
|
|
|
241
|
-
| [`@dbt-tools/core`](
|
|
242
|
-
| [`@dbt-tools/cli`](
|
|
243
|
-
| [`dbt-artifacts-parser`](
|
|
217
|
+
| Package | Role |
|
|
218
|
+
| ------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
|
|
219
|
+
| [`@dbt-tools/core`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-tools/core/README.md) | Analysis engine |
|
|
220
|
+
| [`@dbt-tools/cli`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-tools/cli/README.md) | CLI (`dbt-tools`) |
|
|
221
|
+
| [`dbt-artifacts-parser`](https://github.com/yu-iskw/dbt-artifacts-parser-ts/blob/main/packages/dbt-artifacts-parser/README.md) | Artifact parsing |
|
|
244
222
|
|
|
245
223
|
---
|
|
246
224
|
|
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFile } 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-args.ts
|
|
482
|
+
/** Pure CLI argument parsing for `dbt-tools-web` (testable without starting the server). */
|
|
483
|
+
var USAGE = `
|
|
484
|
+
Usage: dbt-tools-web [options]
|
|
485
|
+
|
|
486
|
+
--target <dir> Path to dbt target directory (sets DBT_TOOLS_TARGET_DIR)
|
|
487
|
+
--port <n> Port to listen on (default: 3000)
|
|
488
|
+
--no-open Do not open the browser automatically
|
|
489
|
+
--help Show this help message
|
|
490
|
+
`.trimStart();
|
|
491
|
+
function readRequiredValue(argv, i, flagDesc) {
|
|
492
|
+
const next = argv[i + 1];
|
|
493
|
+
if (!next || next.startsWith("-")) return {
|
|
494
|
+
ok: false,
|
|
495
|
+
message: `Missing value for ${flagDesc}`
|
|
496
|
+
};
|
|
497
|
+
return {
|
|
498
|
+
ok: true,
|
|
499
|
+
value: next,
|
|
500
|
+
nextIndex: i + 1
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function parsePortString(s) {
|
|
504
|
+
const parsed = Number.parseInt(s, 10);
|
|
505
|
+
if (Number.isNaN(parsed) || parsed < 1 || parsed > 65535) return null;
|
|
506
|
+
return parsed;
|
|
507
|
+
}
|
|
508
|
+
function consumeOne(argv, i, state) {
|
|
509
|
+
const arg = argv[i];
|
|
510
|
+
if (arg === "--help" || arg === "-h") return {
|
|
511
|
+
status: "done",
|
|
512
|
+
result: { kind: "help" }
|
|
513
|
+
};
|
|
514
|
+
if (arg === "--no-open") {
|
|
515
|
+
state.open = false;
|
|
516
|
+
return {
|
|
517
|
+
status: "advance",
|
|
518
|
+
nextIndex: i + 1
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
if (arg === "--target" || arg === "-t") {
|
|
522
|
+
const r = readRequiredValue(argv, i, "--target (or -t)");
|
|
523
|
+
if (!r.ok) return {
|
|
524
|
+
status: "done",
|
|
525
|
+
result: {
|
|
526
|
+
kind: "error",
|
|
527
|
+
message: r.message
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
state.targetDir = r.value;
|
|
531
|
+
return {
|
|
532
|
+
status: "advance",
|
|
533
|
+
nextIndex: r.nextIndex + 1
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
if (arg === "--port" || arg === "-p") {
|
|
537
|
+
const r = readRequiredValue(argv, i, "--port (or -p)");
|
|
538
|
+
if (!r.ok) return {
|
|
539
|
+
status: "done",
|
|
540
|
+
result: {
|
|
541
|
+
kind: "error",
|
|
542
|
+
message: r.message
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
const p = parsePortString(r.value);
|
|
546
|
+
if (p === null) return {
|
|
547
|
+
status: "done",
|
|
548
|
+
result: {
|
|
549
|
+
kind: "error",
|
|
550
|
+
message: `Invalid port: ${r.value}`
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
state.port = p;
|
|
554
|
+
return {
|
|
555
|
+
status: "advance",
|
|
556
|
+
nextIndex: r.nextIndex + 1
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
if (arg.startsWith("-")) return {
|
|
560
|
+
status: "done",
|
|
561
|
+
result: {
|
|
562
|
+
kind: "error",
|
|
563
|
+
message: `Unknown option: ${arg}`
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
return {
|
|
567
|
+
status: "done",
|
|
568
|
+
result: {
|
|
569
|
+
kind: "error",
|
|
570
|
+
message: `Unexpected argument: ${arg}`
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function parseCliArgs(argv) {
|
|
575
|
+
const state = {
|
|
576
|
+
targetDir: void 0,
|
|
577
|
+
port: 3e3,
|
|
578
|
+
open: true
|
|
579
|
+
};
|
|
580
|
+
let i = 0;
|
|
581
|
+
while (i < argv.length) {
|
|
582
|
+
const step = consumeOne(argv, i, state);
|
|
583
|
+
if (step.status === "done") return step.result;
|
|
584
|
+
i = step.nextIndex;
|
|
585
|
+
}
|
|
586
|
+
return {
|
|
587
|
+
kind: "ok",
|
|
588
|
+
targetDir: state.targetDir,
|
|
589
|
+
port: state.port,
|
|
590
|
+
open: state.open
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
//#endregion
|
|
594
|
+
//#region src/server/cli.ts
|
|
595
|
+
var parsed = parseCliArgs(process.argv.slice(2));
|
|
596
|
+
if (parsed.kind === "help") {
|
|
597
|
+
process.stdout.write(USAGE);
|
|
598
|
+
process.exit(0);
|
|
599
|
+
}
|
|
600
|
+
if (parsed.kind === "error") {
|
|
601
|
+
process.stderr.write(`${parsed.message}\n`);
|
|
602
|
+
process.exit(1);
|
|
603
|
+
}
|
|
604
|
+
var { targetDir, port, open } = parsed;
|
|
605
|
+
if (targetDir !== void 0) process.env.DBT_TOOLS_TARGET_DIR = targetDir;
|
|
606
|
+
await startServer(port);
|
|
607
|
+
var url = `http://${LISTEN_HOST}:${port}`;
|
|
608
|
+
process.stdout.write(`dbt-tools-web ${url}\n`);
|
|
609
|
+
function openBrowser(safeUrl) {
|
|
610
|
+
const cb = () => {};
|
|
611
|
+
if (process.platform === "darwin") execFile("open", [safeUrl], cb);
|
|
612
|
+
else if (process.platform === "win32") execFile("cmd.exe", [
|
|
613
|
+
"/c",
|
|
614
|
+
"start",
|
|
615
|
+
"",
|
|
616
|
+
safeUrl
|
|
617
|
+
], { windowsHide: true }, cb);
|
|
618
|
+
else execFile("xdg-open", [safeUrl], cb);
|
|
619
|
+
}
|
|
620
|
+
if (open) openBrowser(url);
|
|
621
|
+
//#endregion
|
|
622
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbt-tools/web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
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.1",
|
|
33
|
+
"dbt-artifacts-parser": "0.4.1"
|
|
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",
|
|
@@ -47,6 +55,7 @@
|
|
|
47
55
|
"lint": "pnpm lint:eslint && pnpm lint:stylelint",
|
|
48
56
|
"lint:eslint": "eslint ../../../eslint.config.mjs",
|
|
49
57
|
"lint:stylelint": "stylelint -c ../../../stylelint.config.mjs **/*.css",
|
|
50
|
-
"format:stylelint": "stylelint -c ../../../stylelint.config.mjs **/*.css --fix"
|
|
58
|
+
"format:stylelint": "stylelint -c ../../../stylelint.config.mjs **/*.css --fix",
|
|
59
|
+
"smoke:npx-tgz": "bash scripts/smoke-npx-packed-tarball.sh"
|
|
51
60
|
}
|
|
52
61
|
}
|