This PR intends to address an error logged when opening a DevTools in a
Chromium based browser while using Fresh in dev mode.
<details>
<summary><strong>See error</strong></summary>
```
Error: Processed file resolved outside of static dir C:\Users\<name>\fresh-site\src\static\.well-known\appspecific\com.chrome.devtools.json
at MemoryBuildCache.readFile (https://jsr.io/@fresh/core/2.0.0-alpha.29/src/dev/dev_build_cache.ts:81:13)
at eventLoopTick (ext:core/01_core.js:178:7)
at async freshStaticFiles (https://jsr.io/@fresh/core/2.0.0-alpha.29/src/middlewares/static_files.ts:30:18)
at async FreshReqContext.fn (https://jsr.io/@fresh/core/2.0.0-alpha.29/src/middlewares/mod.ts:99:18)
at async https://jsr.io/@fresh/core/2.0.0-alpha.29/src/dev/middlewares/error_overlay/middleware.tsx:15:14
at async FreshReqContext.fn (https://jsr.io/@fresh/core/2.0.0-alpha.29/src/middlewares/mod.ts:99:18)
at async fn (https://jsr.io/@fresh/core/2.0.0-alpha.29/src/middlewares/mod.ts:99:18)
at async https://jsr.io/@fresh/core/2.0.0-alpha.29/src/app.ts:232:16
at async mapped (ext:deno_http/00_serve.ts:407:18)
```
</details>
DevTools tries to load a
`<root>/static/.well-known/appspecific/com.chrome.devtools.json` file,
which incorrectly is flagged as "outside" the static directory, and thus
generates errors instead of 404.
Maybe hidden folders (starting with `.`) in `/static` should be opt-in
though? I'm not sure if anyone puts folders with `.` prefix inside
`static/` without intending them to be accessible?
- `..` throws errors (outside staticDir)
- `.` returns "missing file" (unless `config.allowHiddenFolders` is
true)
## Automatic workspace folders
I think a follow-up PR could be for the Dev `Builder` to also implement
support for [Automatic workspace
folders](https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md),
by returning JSON content automatically at
`.well-known/appspecific/com.chrome.devtools.json`.
I think a good first step is to allow `.<folder-name>`, especially since
`.well-known/` has [many valid
use-cases](https://en.wikipedia.org/wiki/Well-known_URI), and as such is
not unreasonable to want to add static files in.
135 lines
3.8 KiB
TypeScript
135 lines
3.8 KiB
TypeScript
import * as path from "@std/path";
|
|
import { getSnapshotPath, type ResolvedFreshConfig } from "./config.ts";
|
|
import { DENO_DEPLOYMENT_ID, setBuildId } from "./runtime/build_id.ts";
|
|
import * as colors from "@std/fmt/colors";
|
|
|
|
export interface FileSnapshot {
|
|
generated: boolean;
|
|
hash: string | null;
|
|
}
|
|
|
|
export interface BuildSnapshot {
|
|
version: number;
|
|
buildId: string;
|
|
staticFiles: Record<string, FileSnapshot>;
|
|
islands: Record<string, string>;
|
|
}
|
|
|
|
export interface StaticFile {
|
|
hash: string | null;
|
|
size: number;
|
|
readable: ReadableStream<Uint8Array> | Uint8Array;
|
|
close(): void;
|
|
}
|
|
|
|
export interface BuildCache {
|
|
hasSnapshot: boolean;
|
|
readFile(pathname: string): Promise<StaticFile | null>;
|
|
getIslandChunkName(islandName: string): string | null;
|
|
}
|
|
|
|
export class ProdBuildCache implements BuildCache {
|
|
static fromSnapshot(config: ResolvedFreshConfig, islandCount: number) {
|
|
const snapshotPath = getSnapshotPath(config);
|
|
|
|
const staticFiles = new Map<string, FileSnapshot>();
|
|
const islandToChunk = new Map<string, string>();
|
|
|
|
let hasSnapshot = false;
|
|
try {
|
|
const content = Deno.readTextFileSync(snapshotPath);
|
|
const snapshot = JSON.parse(content) as BuildSnapshot;
|
|
hasSnapshot = true;
|
|
setBuildId(snapshot.buildId);
|
|
|
|
const files = Object.keys(snapshot.staticFiles);
|
|
for (let i = 0; i < files.length; i++) {
|
|
const pathname = files[i];
|
|
const info = snapshot.staticFiles[pathname];
|
|
staticFiles.set(pathname, info);
|
|
}
|
|
|
|
const islands = Object.keys(snapshot.islands);
|
|
for (let i = 0; i < islands.length; i++) {
|
|
const pathname = islands[i];
|
|
islandToChunk.set(pathname, snapshot.islands[pathname]);
|
|
}
|
|
|
|
if (!DENO_DEPLOYMENT_ID) {
|
|
// deno-lint-ignore no-console
|
|
console.log(
|
|
`Found snapshot at ${colors.cyan(snapshotPath)}`,
|
|
);
|
|
}
|
|
} catch (err) {
|
|
if ((err instanceof Deno.errors.NotFound)) {
|
|
if (islandCount > 0) {
|
|
throw new Error(
|
|
`Found ${
|
|
colors.green(`${islandCount} islands`)
|
|
}, but did not find build snapshot at:\n${
|
|
colors.red(snapshotPath)
|
|
}.\n\nMaybe your forgot to run ${
|
|
colors.cyan("deno task build")
|
|
} before starting the production server\nor maybe you wanted to run ${
|
|
colors.cyan("deno task dev")
|
|
} to spin up a development server instead?\n`,
|
|
);
|
|
}
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
return new ProdBuildCache(config, islandToChunk, staticFiles, hasSnapshot);
|
|
}
|
|
|
|
#islands: Map<string, string>;
|
|
#fileInfo: Map<string, FileSnapshot>;
|
|
#config: ResolvedFreshConfig;
|
|
|
|
constructor(
|
|
config: ResolvedFreshConfig,
|
|
islands: Map<string, string>,
|
|
files: Map<string, FileSnapshot>,
|
|
public hasSnapshot: boolean,
|
|
) {
|
|
this.#islands = islands;
|
|
this.#fileInfo = files;
|
|
this.#config = config;
|
|
}
|
|
|
|
async readFile(pathname: string): Promise<StaticFile | null> {
|
|
const info = this.#fileInfo.get(pathname);
|
|
if (info === undefined) return null;
|
|
|
|
const base = info.generated
|
|
? this.#config.build.outDir
|
|
: this.#config.staticDir;
|
|
const filePath = info.generated
|
|
? path.join(base, "static", pathname)
|
|
: path.join(base, pathname);
|
|
|
|
// Check if path resolves outside of intended directory.
|
|
if (path.relative(base, filePath).startsWith("..")) {
|
|
return null;
|
|
}
|
|
|
|
const [stat, file] = await Promise.all([
|
|
Deno.stat(filePath),
|
|
Deno.open(filePath),
|
|
]);
|
|
|
|
return {
|
|
hash: info.hash,
|
|
size: stat.size,
|
|
readable: file.readable,
|
|
close: () => file.close(),
|
|
};
|
|
}
|
|
|
|
getIslandChunkName(islandName: string): string | null {
|
|
return this.#islands.get(islandName) ?? null;
|
|
}
|
|
}
|