@vitejs/devtools-vitest 0.7.5 → 0.7.6

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.
Files changed (2) hide show
  1. package/dist/index.mjs +52 -13
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -8,6 +8,7 @@ import { resolveCommand } from "package-manager-detector/commands";
8
8
  import { detect } from "package-manager-detector/detect";
9
9
  import { glob } from "tinyglobby";
10
10
  import { createConsoleReporter, defineDiagnostics } from "nostics";
11
+ import { stripVTControlCharacters } from "node:util";
11
12
  //#region src/node/diagnostics.ts
12
13
  const diagnostics = /* #__PURE__ */ defineDiagnostics({
13
14
  docsBase: "https://devtools.vite.dev/errors",
@@ -24,6 +25,53 @@ const diagnostics = /* #__PURE__ */ defineDiagnostics({
24
25
  }
25
26
  });
26
27
  //#endregion
28
+ //#region src/node/ui-url.ts
29
+ /**
30
+ * Carry the Vitest UI API token into `url`.
31
+ *
32
+ * Since Vitest 5 the UI answers `403` unless the request carries
33
+ * `?token=<api token>` (which sets an auth cookie and redirects to the clean
34
+ * URL) or already holds that cookie. Vitest prints the authenticated URL on
35
+ * startup as `UI started at http://localhost:<port>/__vitest__/?token=…`.
36
+ *
37
+ * Returns `url` with that token, `url` unchanged when the printed URL has no
38
+ * token (Vitest < 5), or `undefined` while the line hasn't been printed yet.
39
+ */
40
+ function resolveVitestUiUrl(url, output) {
41
+ const printed = stripVTControlCharacters(output).match(/UI started at (\S+)/)?.[1];
42
+ if (!printed) return void 0;
43
+ let token;
44
+ try {
45
+ token = new URL(printed).searchParams.get("token");
46
+ } catch {
47
+ return;
48
+ }
49
+ if (!token) return url;
50
+ const authenticated = new URL(url);
51
+ authenticated.searchParams.set("token", token);
52
+ return authenticated.href;
53
+ }
54
+ /**
55
+ * Poll the Vitest UI until it serves the app, returning the URL to embed
56
+ * (tokenized when Vitest printed a token), or `undefined` on timeout.
57
+ *
58
+ * Only a `2xx` or a `3xx` (Vitest redirects a valid `?token=` to the clean
59
+ * URL) counts as ready. A `403` is Vitest's auth-required page, which is not
60
+ * the UI, so polling continues until the token shows up in `getOutput()`.
61
+ */
62
+ async function waitForVitestUi(url, timeout, getOutput, interval = 300) {
63
+ const start = Date.now();
64
+ while (Date.now() - start < timeout) {
65
+ const target = resolveVitestUiUrl(url, getOutput()) ?? url;
66
+ try {
67
+ const res = await fetch(target, { redirect: "manual" });
68
+ await res.body?.cancel();
69
+ if (res.status >= 200 && res.status < 400) return target;
70
+ } catch {}
71
+ await new Promise((resolve) => setTimeout(resolve, interval));
72
+ }
73
+ }
74
+ //#endregion
27
75
  //#region src/node/plugin.ts
28
76
  const VITEST_DEVTOOLS_BASE = "/__devtools-vitest/";
29
77
  /** Path Vitest's own UI server serves its app under. */
@@ -119,12 +167,13 @@ function DevToolsVitestUI() {
119
167
  cwd: root ?? cwd
120
168
  };
121
169
  },
122
- serve: { onReady: async () => {
123
- if (!await waitForServer(url, READY_TIMEOUT)) throw diagnostics.VTDT0002({
170
+ serve: { onReady: async (session) => {
171
+ const ready = await waitForVitestUi(url, READY_TIMEOUT, () => session.buffer?.join("") ?? "");
172
+ if (!ready) throw diagnostics.VTDT0002({
124
173
  url,
125
174
  timeout: READY_TIMEOUT
126
175
  });
127
- return url;
176
+ return ready;
128
177
  } }
129
178
  }).devtools.setup(ctx);
130
179
  } }
@@ -167,15 +216,5 @@ async function discoverRoots(cwd, workspaceRoot) {
167
216
  }
168
217
  return [...roots.values()];
169
218
  }
170
- async function waitForServer(url, timeout) {
171
- const start = Date.now();
172
- while (Date.now() - start < timeout) {
173
- try {
174
- if ((await fetch(url)).status < 500) return true;
175
- } catch {}
176
- await new Promise((resolve) => setTimeout(resolve, 300));
177
- }
178
- return false;
179
- }
180
219
  //#endregion
181
220
  export { DevToolsVitestUI };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitejs/devtools-vitest",
3
3
  "type": "module",
4
- "version": "0.7.5",
4
+ "version": "0.7.6",
5
5
  "description": "DevTools for Vitest",
6
6
  "author": "VoidZero Inc.",
7
7
  "license": "MIT",
@@ -36,10 +36,10 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@vitejs/devtools-kit": "0.7.5",
39
+ "@vitejs/devtools-kit": "0.7.6",
40
40
  "devframe": "^1.0.0",
41
41
  "local-pkg": "^1.2.1",
42
- "nostics": "^1.2.0",
42
+ "nostics": "^1.3.0",
43
43
  "package-manager-detector": "^1.8.0",
44
44
  "tinyglobby": "^0.2.17"
45
45
  },