feat: support automatic workspace detection in Dev (#2985)
Adds support for [Automatic Workspace Detection](https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md). - Avoids 404's or middleware logging when this feature is active (e.g. Chrome/Edge with DevTools open) - Generates the same UUID based on root folder path (without writing anything to disk) - The middleware is lazy and does nothing until the path is opened for `com.chrome.devtools.json`
This commit is contained in:
@@ -23,6 +23,7 @@ import { BUILD_ID } from "../runtime/build_id.ts";
|
||||
import { updateCheck } from "./update_check.ts";
|
||||
import { DAY } from "@std/datetime";
|
||||
import { devErrorOverlay } from "./middlewares/error_overlay/middleware.tsx";
|
||||
import { automaticWorkspaceFolders } from "./middlewares/automatic_workspace_folders.ts";
|
||||
|
||||
export interface BuildOptions {
|
||||
/**
|
||||
@@ -68,6 +69,7 @@ export class Builder implements FreshBuilder {
|
||||
const devApp = new App<T>(app.config)
|
||||
.use(liveReload())
|
||||
.use(devErrorOverlay())
|
||||
.use(automaticWorkspaceFolders(app.config.root))
|
||||
.mountApp("/*", app);
|
||||
|
||||
devApp.config.mode = "development";
|
||||
|
||||
@@ -209,3 +209,38 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Builder - workspace folder middleware on listen",
|
||||
fn: async () => {
|
||||
const builder = new Builder();
|
||||
const tmp = await Deno.makeTempDir();
|
||||
const app = new App({
|
||||
staticDir: tmp,
|
||||
build: {
|
||||
outDir: path.join(tmp, "dist"),
|
||||
},
|
||||
});
|
||||
const abort = new AbortController();
|
||||
const port = 8011;
|
||||
await builder.listen(app, { port, signal: abort.signal });
|
||||
|
||||
const res = await fetch(
|
||||
`http://localhost:${port}/.well-known/appspecific/com.chrome.devtools.json`,
|
||||
);
|
||||
const json = await res.json();
|
||||
await abort.abort();
|
||||
|
||||
expect(res.ok).toBe(true);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("etag")).toEqual(expect.any(String));
|
||||
expect(json).toEqual({
|
||||
workspace: {
|
||||
root: app.config.root,
|
||||
uuid: expect.any(String),
|
||||
},
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { eTag, ifNoneMatch } from "@std/http/etag";
|
||||
import { generate } from "@std/uuid/v5";
|
||||
import { NAMESPACE_URL } from "@std/uuid/constants";
|
||||
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
||||
|
||||
/**
|
||||
* Automatically detects workspace folders for DevTools in Chromium browsers.
|
||||
*
|
||||
* Without this middleware, if the feature is enabled and devTools is opened
|
||||
* there will be 404 errors in the console. This can be annoying for developers,
|
||||
* especially if there is logging for incoming requests.
|
||||
*
|
||||
* Enabling this feature can be helpful to make edits in DevTools change source
|
||||
* files directly, for a small amount of code.
|
||||
*
|
||||
* The UUID is generated with a UUIDv5, so that the same directory should
|
||||
* always generate the same UUID. This way, we can preserve the connected
|
||||
* workspace across reloads without writing anything to disk.
|
||||
*
|
||||
* @see https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md
|
||||
*/
|
||||
export function automaticWorkspaceFolders<T>(root: string): MiddlewareFn<T> {
|
||||
let uuid: string | undefined;
|
||||
let etag: string | undefined;
|
||||
let content: string | undefined;
|
||||
|
||||
return async (ctx) => {
|
||||
const { pathname } = ctx.url;
|
||||
|
||||
if (pathname !== "/.well-known/appspecific/com.chrome.devtools.json") {
|
||||
return ctx.next();
|
||||
}
|
||||
|
||||
uuid ??= await generate(
|
||||
NAMESPACE_URL,
|
||||
new TextEncoder().encode(
|
||||
`https://fresh.com?root=${encodeURIComponent(root)}&v=${0}`,
|
||||
),
|
||||
);
|
||||
content ??= JSON.stringify({ workspace: { root, uuid } }, undefined, 2);
|
||||
etag ??= await eTag(content);
|
||||
|
||||
const noneMatchValue = ctx.req.headers.get("if-none-match");
|
||||
if (!ifNoneMatch(noneMatchValue, etag)) {
|
||||
return new Response(undefined, { status: 304, headers: { etag } });
|
||||
}
|
||||
return new Response(content, { status: 200, headers: { etag } });
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user