@farm.js/devtools 0.1.1-beta.1 → 0.1.1-beta.2
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/dist/client.js +17 -19
- package/dist/index.js +62 -62
- package/dist/modules.js +102 -84
- package/dist/server.js +117 -103
- package/dist/types.js +1 -4
- package/package.json +2 -2
package/dist/client.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import "@fontsource-variable/geist-mono/wght.css";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
/** A small launcher only. The inspector and highlighter load when the iframe opens. */
|
|
4
|
+
export function startDevtoolsLauncher(runtimeWindow = window) {
|
|
5
|
+
const document = runtimeWindow.document;
|
|
6
|
+
const existing = document.querySelector("farm-devtools-launcher");
|
|
7
|
+
existing?.remove();
|
|
8
|
+
const host = document.createElement("farm-devtools-launcher");
|
|
9
|
+
const shadow = host.attachShadow({ mode: "open", delegatesFocus: true });
|
|
10
|
+
shadow.innerHTML = `<style>
|
|
10
11
|
:host{--bg:#ffffff;--surface:#fafafa;--line:#d9d9d3;--text:#1b1b19;position:fixed;left:18px;bottom:18px;z-index:2147483645}
|
|
11
12
|
button{box-sizing:border-box;display:flex;align-items:center;gap:9px;min-height:42px;margin:0;padding:0 12px;border:1px solid var(--line);border-radius:999px;background:var(--bg);color:var(--text);box-shadow:0 10px 32px rgb(0 0 0 / 16%);font:11px/1.45 "Geist Mono Variable","Geist Mono",ui-monospace,SFMono-Regular,Menlo,monospace;font-variation-settings:"wght" 620;letter-spacing:0;text-transform:uppercase;text-rendering:geometricPrecision;white-space:nowrap;cursor:pointer;-webkit-tap-highlight-color:transparent}
|
|
12
13
|
@media(hover:hover){button:hover{background:var(--surface)}}
|
|
@@ -17,16 +18,13 @@ function startDevtoolsLauncher(runtimeWindow = window) {
|
|
|
17
18
|
@media(max-width:520px){:host{left:8px;bottom:8px}}
|
|
18
19
|
@media(pointer:coarse){button{min-height:44px}}
|
|
19
20
|
</style><button type="button" aria-label="Open Farm DevTools"><img src="/__farm/devtools/assets/logo.svg" alt=""><span>DevTools</span></button>`;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
const button = shadow.querySelector("button");
|
|
22
|
+
button.onclick = () => runtimeWindow.__FARM_DEVTOOLS__?.open();
|
|
23
|
+
document.body.append(host);
|
|
24
|
+
return {
|
|
25
|
+
dispose() {
|
|
26
|
+
button.onclick = null;
|
|
27
|
+
host.remove();
|
|
28
|
+
},
|
|
29
|
+
};
|
|
29
30
|
}
|
|
30
|
-
export {
|
|
31
|
-
startDevtoolsLauncher
|
|
32
|
-
};
|
package/dist/index.js
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
1
|
import { definePlugin } from "@farm.js/core/plugin";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
/** Add the development-only workspace inspector. No production runtime is registered. */
|
|
3
|
+
export function devtools(options = {}) {
|
|
4
|
+
for (const name of ["launcher", "inspect"]) {
|
|
5
|
+
if (options[name] !== undefined && typeof options[name] !== "boolean") {
|
|
6
|
+
throw new TypeError(`[farm:devtools] ${name} must be a boolean`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
if (options.shortcut !== undefined &&
|
|
10
|
+
options.shortcut !== false &&
|
|
11
|
+
(typeof options.shortcut !== "string" ||
|
|
12
|
+
!options.shortcut.trim() ||
|
|
13
|
+
!/^[a-z0-9+.,/ -]+$/i.test(options.shortcut))) {
|
|
14
|
+
throw new TypeError("[farm:devtools] shortcut must be a keyboard shortcut such as mod+shift+d, or false");
|
|
6
15
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
const inspect = options.inspect ?? true;
|
|
14
|
-
const launcher = options.launcher ?? true;
|
|
15
|
-
return definePlugin({
|
|
16
|
-
name: "farm:devtools",
|
|
17
|
-
enforce: "pre",
|
|
18
|
-
configure(config, context) {
|
|
19
|
-
if (context.isProd) {
|
|
20
|
-
return {
|
|
21
|
-
...config,
|
|
22
|
-
plugins: config.plugins?.filter((candidate) => candidate.name !== "farm:devtools")
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
if ((config.plugins ?? []).filter((candidate) => candidate.name === "farm:devtools").length > 1) {
|
|
26
|
-
throw new Error("[farm:devtools] Configure one devtools() plugin instance");
|
|
27
|
-
}
|
|
28
|
-
if (config.devtools === false || typeof config.devtools === "object" && config.devtools.enabled === false) {
|
|
29
|
-
throw new Error(
|
|
30
|
-
"[farm:devtools] Remove devtools: false (or enabled: false) when using the devtools() plugin"
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
const vitePlugin = {
|
|
34
|
-
name: "farm:devtools-ui",
|
|
16
|
+
const inspect = options.inspect ?? true;
|
|
17
|
+
const launcher = options.launcher ?? true;
|
|
18
|
+
return definePlugin({
|
|
19
|
+
name: "farm:devtools",
|
|
35
20
|
enforce: "pre",
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
21
|
+
configure(config, context) {
|
|
22
|
+
if (context.isProd) {
|
|
23
|
+
return {
|
|
24
|
+
...config,
|
|
25
|
+
plugins: config.plugins?.filter((candidate) => candidate.name !== "farm:devtools"),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if ((config.plugins ?? []).filter((candidate) => candidate.name === "farm:devtools").length > 1) {
|
|
29
|
+
throw new Error("[farm:devtools] Configure one devtools() plugin instance");
|
|
30
|
+
}
|
|
31
|
+
if (config.devtools === false ||
|
|
32
|
+
(typeof config.devtools === "object" && config.devtools.enabled === false)) {
|
|
33
|
+
throw new Error("[farm:devtools] Remove devtools: false (or enabled: false) when using the devtools() plugin");
|
|
34
|
+
}
|
|
35
|
+
const vitePlugin = {
|
|
36
|
+
name: "farm:devtools-ui",
|
|
37
|
+
enforce: "pre",
|
|
38
|
+
apply: "serve",
|
|
39
|
+
async configureServer(server) {
|
|
40
|
+
const { installDevtoolsServer } = await import("./server.js");
|
|
41
|
+
installDevtoolsServer(server, { inspect });
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
...config,
|
|
46
|
+
devtools: {
|
|
47
|
+
...(typeof config.devtools === "object" ? config.devtools : {}),
|
|
48
|
+
enabled: true,
|
|
49
|
+
...(options.shortcut === undefined ? {} : { shortcut: options.shortcut }),
|
|
50
|
+
},
|
|
51
|
+
vite: { ...config.vite, plugins: [vitePlugin, ...(config.vite?.plugins ?? [])] },
|
|
52
|
+
};
|
|
48
53
|
},
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
});
|
|
54
|
+
client: {
|
|
55
|
+
public: { launcher },
|
|
56
|
+
async setup({ isDev, public: config }) {
|
|
57
|
+
if (!isDev || !config.launcher)
|
|
58
|
+
return;
|
|
59
|
+
const { startDevtoolsLauncher } = await import("@farm.js/devtools/client");
|
|
60
|
+
return startDevtoolsLauncher();
|
|
61
|
+
},
|
|
62
|
+
close({ state }) {
|
|
63
|
+
state?.dispose();
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
64
67
|
}
|
|
65
|
-
export {
|
|
66
|
-
devtools
|
|
67
|
-
};
|
package/dist/modules.js
CHANGED
|
@@ -4,95 +4,113 @@ import { open, realpath } from "node:fs/promises";
|
|
|
4
4
|
const SOURCE_LIMIT = 1024 * 1024;
|
|
5
5
|
const MODULE_LIMIT = 500;
|
|
6
6
|
const SOURCE_EXTENSION = /\.(?:[cm]?[jt]sx?|css|vue|svelte)$/i;
|
|
7
|
-
class ModuleInspectionError extends Error {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
export class ModuleInspectionError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
constructor(message, status) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.status = status;
|
|
12
|
+
}
|
|
13
13
|
}
|
|
14
14
|
function contained(root, file) {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const relative = path.relative(root, file);
|
|
16
|
+
return (relative !== "" &&
|
|
17
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
18
|
+
relative !== ".." &&
|
|
19
|
+
!path.isAbsolute(relative));
|
|
17
20
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
const canonical = await realpath(module.file);
|
|
36
|
-
const realRoot = await rootReal;
|
|
37
|
-
return contained(realRoot, canonical) && SOURCE_EXTENSION.test(canonical) && !path.relative(realRoot, canonical).split(path.sep).some((part) => part.startsWith(".") || part === "node_modules") ? canonical : void 0;
|
|
38
|
-
} catch {
|
|
39
|
-
return void 0;
|
|
21
|
+
/** Only already-transformed browser modules are eligible. Never transform or execute on request. */
|
|
22
|
+
export function createModuleInspector(server) {
|
|
23
|
+
const root = path.resolve(server.config.root);
|
|
24
|
+
const rootReal = realpath(root);
|
|
25
|
+
function entries() {
|
|
26
|
+
return [...server.moduleGraph.idToModuleMap.values()]
|
|
27
|
+
.filter((module) => module.id &&
|
|
28
|
+
module.file &&
|
|
29
|
+
module.transformResult &&
|
|
30
|
+
contained(root, module.file) &&
|
|
31
|
+
SOURCE_EXTENSION.test(module.file) &&
|
|
32
|
+
!path
|
|
33
|
+
.relative(root, module.file)
|
|
34
|
+
.split(path.sep)
|
|
35
|
+
.some((part) => part.startsWith(".") || part === "node_modules"))
|
|
36
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
40
37
|
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
async list() {
|
|
44
|
-
const candidates = entries();
|
|
45
|
-
const modules = [];
|
|
46
|
-
for (const module of candidates) {
|
|
47
|
-
if (await safe(module)) modules.push(summary(module));
|
|
48
|
-
if (modules.length === MODULE_LIMIT) break;
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
modules,
|
|
52
|
-
limited: candidates.length > modules.length && modules.length === MODULE_LIMIT
|
|
53
|
-
};
|
|
54
|
-
},
|
|
55
|
-
async read(id) {
|
|
56
|
-
if (!/^[a-f0-9]{64}$/.test(id))
|
|
57
|
-
throw new ModuleInspectionError("Unknown browser module", 404);
|
|
58
|
-
const module = entries().find((entry) => summary(entry).id === id);
|
|
59
|
-
const canonical = module && await safe(module);
|
|
60
|
-
const transformed = module?.transformResult?.code;
|
|
61
|
-
if (!module || !canonical || transformed === void 0)
|
|
62
|
-
throw new ModuleInspectionError(
|
|
63
|
-
"Module is no longer available. Reload the app and refresh Inspect.",
|
|
64
|
-
404
|
|
65
|
-
);
|
|
66
|
-
if (Buffer.byteLength(transformed) > SOURCE_LIMIT)
|
|
67
|
-
throw new ModuleInspectionError("Module exceeds the 1 MiB inspection limit", 413);
|
|
68
|
-
const file = await open(canonical, "r");
|
|
69
|
-
try {
|
|
70
|
-
const stat = await file.stat();
|
|
71
|
-
if (!stat.isFile() || stat.size > SOURCE_LIMIT)
|
|
72
|
-
throw new ModuleInspectionError("Source exceeds the 1 MiB inspection limit", 413);
|
|
73
|
-
const buffer = Buffer.alloc(SOURCE_LIMIT + 1);
|
|
74
|
-
let bytesRead = 0;
|
|
75
|
-
while (bytesRead < buffer.length) {
|
|
76
|
-
const chunk = await file.read(buffer, bytesRead, buffer.length - bytesRead, bytesRead);
|
|
77
|
-
if (!chunk.bytesRead) break;
|
|
78
|
-
bytesRead += chunk.bytesRead;
|
|
79
|
-
}
|
|
80
|
-
if (bytesRead > SOURCE_LIMIT)
|
|
81
|
-
throw new ModuleInspectionError("Source exceeds the 1 MiB inspection limit", 413);
|
|
38
|
+
function summary(module) {
|
|
82
39
|
return {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
imports: [...module.importedModules].map((entry) => entry.url).sort(),
|
|
87
|
-
importers: [...module.importers].map((entry) => entry.url).sort()
|
|
40
|
+
id: createHash("sha256").update(module.id).digest("hex"),
|
|
41
|
+
path: path.relative(root, module.file).split(path.sep).join("/"),
|
|
42
|
+
url: module.url,
|
|
88
43
|
};
|
|
89
|
-
} finally {
|
|
90
|
-
await file.close();
|
|
91
|
-
}
|
|
92
44
|
}
|
|
93
|
-
|
|
45
|
+
async function safe(module) {
|
|
46
|
+
try {
|
|
47
|
+
const canonical = await realpath(module.file);
|
|
48
|
+
const realRoot = await rootReal;
|
|
49
|
+
return contained(realRoot, canonical) &&
|
|
50
|
+
SOURCE_EXTENSION.test(canonical) &&
|
|
51
|
+
!path
|
|
52
|
+
.relative(realRoot, canonical)
|
|
53
|
+
.split(path.sep)
|
|
54
|
+
.some((part) => part.startsWith(".") || part === "node_modules")
|
|
55
|
+
? canonical
|
|
56
|
+
: undefined;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
async list() {
|
|
64
|
+
const candidates = entries();
|
|
65
|
+
const modules = [];
|
|
66
|
+
for (const module of candidates) {
|
|
67
|
+
if (await safe(module))
|
|
68
|
+
modules.push(summary(module));
|
|
69
|
+
if (modules.length === MODULE_LIMIT)
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
modules,
|
|
74
|
+
limited: candidates.length > modules.length && modules.length === MODULE_LIMIT,
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
async read(id) {
|
|
78
|
+
if (!/^[a-f0-9]{64}$/.test(id))
|
|
79
|
+
throw new ModuleInspectionError("Unknown browser module", 404);
|
|
80
|
+
const module = entries().find((entry) => summary(entry).id === id);
|
|
81
|
+
const canonical = module && (await safe(module));
|
|
82
|
+
// Vite can invalidate the transform while the filesystem check is pending.
|
|
83
|
+
const transformed = module?.transformResult?.code;
|
|
84
|
+
if (!module || !canonical || transformed === undefined)
|
|
85
|
+
throw new ModuleInspectionError("Module is no longer available. Reload the app and refresh Inspect.", 404);
|
|
86
|
+
if (Buffer.byteLength(transformed) > SOURCE_LIMIT)
|
|
87
|
+
throw new ModuleInspectionError("Module exceeds the 1 MiB inspection limit", 413);
|
|
88
|
+
const file = await open(canonical, "r");
|
|
89
|
+
try {
|
|
90
|
+
const stat = await file.stat();
|
|
91
|
+
if (!stat.isFile() || stat.size > SOURCE_LIMIT)
|
|
92
|
+
throw new ModuleInspectionError("Source exceeds the 1 MiB inspection limit", 413);
|
|
93
|
+
const buffer = Buffer.alloc(SOURCE_LIMIT + 1);
|
|
94
|
+
let bytesRead = 0;
|
|
95
|
+
while (bytesRead < buffer.length) {
|
|
96
|
+
const chunk = await file.read(buffer, bytesRead, buffer.length - bytesRead, bytesRead);
|
|
97
|
+
if (!chunk.bytesRead)
|
|
98
|
+
break;
|
|
99
|
+
bytesRead += chunk.bytesRead;
|
|
100
|
+
}
|
|
101
|
+
if (bytesRead > SOURCE_LIMIT)
|
|
102
|
+
throw new ModuleInspectionError("Source exceeds the 1 MiB inspection limit", 413);
|
|
103
|
+
return {
|
|
104
|
+
...summary(module),
|
|
105
|
+
source: buffer.subarray(0, bytesRead).toString("utf8"),
|
|
106
|
+
transformed,
|
|
107
|
+
imports: [...module.importedModules].map((entry) => entry.url).sort(),
|
|
108
|
+
importers: [...module.importers].map((entry) => entry.url).sort(),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
await file.close();
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
94
116
|
}
|
|
95
|
-
export {
|
|
96
|
-
ModuleInspectionError,
|
|
97
|
-
createModuleInspector
|
|
98
|
-
};
|
package/dist/server.js
CHANGED
|
@@ -2,111 +2,125 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import { isIP } from "node:net";
|
|
3
3
|
import { createModuleInspector, ModuleInspectionError } from "./modules.js";
|
|
4
4
|
import { DEVTOOLS_PATH } from "./types.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const authority = req.headers.host ?? "";
|
|
8
|
-
if (!authority || /[\s/@\\?#]/.test(authority)) return false;
|
|
9
|
-
const protocol = req.socket?.encrypted ? "https:" : "http:";
|
|
10
|
-
const host = new URL(`${protocol}//${authority}`);
|
|
11
|
-
const hostname = host.hostname.replace(/^\[|\]$/g, "");
|
|
12
|
-
const allowed = server.config.server.allowedHosts;
|
|
13
|
-
const explicitHost = server.config.server.host;
|
|
14
|
-
const trusted = hostname === "localhost" || hostname.endsWith(".localhost") || isIP(hostname) !== 0 || typeof explicitHost === "string" && hostname === explicitHost || Array.isArray(allowed) && allowed.some(
|
|
15
|
-
(value) => value.startsWith(".") ? hostname === value.slice(1) || hostname.endsWith(value) : hostname === value
|
|
16
|
-
);
|
|
17
|
-
if (!trusted || req.headers["sec-fetch-site"] === "cross-site") return false;
|
|
18
|
-
if (req.headers.origin && new URL(req.headers.origin).origin !== host.origin) return false;
|
|
19
|
-
return true;
|
|
20
|
-
} catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function installDevtoolsServer(server, options) {
|
|
25
|
-
const inspector = createModuleInspector(server);
|
|
26
|
-
const assetRoot = new URL("./ui/", import.meta.url);
|
|
27
|
-
const assets = /* @__PURE__ */ new Map();
|
|
28
|
-
let closed = false;
|
|
29
|
-
server.httpServer?.once("close", () => {
|
|
30
|
-
closed = true;
|
|
31
|
-
assets.clear();
|
|
32
|
-
});
|
|
33
|
-
server.middlewares.use(async (req, res, next) => {
|
|
34
|
-
let url;
|
|
5
|
+
/** Guard our early middleware, before Vite's own host/CORS middleware runs. */
|
|
6
|
+
export function isTrustedRequest(req, server) {
|
|
35
7
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
8
|
+
const authority = req.headers.host ?? "";
|
|
9
|
+
if (!authority || /[\s/@\\?#]/.test(authority))
|
|
10
|
+
return false;
|
|
11
|
+
const protocol = req.socket?.encrypted
|
|
12
|
+
? "https:"
|
|
13
|
+
: "http:";
|
|
14
|
+
const host = new URL(`${protocol}//${authority}`);
|
|
15
|
+
const hostname = host.hostname.replace(/^\[|\]$/g, "");
|
|
16
|
+
const allowed = server.config.server.allowedHosts;
|
|
17
|
+
const explicitHost = server.config.server.host;
|
|
18
|
+
const trusted = hostname === "localhost" ||
|
|
19
|
+
hostname.endsWith(".localhost") ||
|
|
20
|
+
isIP(hostname) !== 0 ||
|
|
21
|
+
(typeof explicitHost === "string" && hostname === explicitHost) ||
|
|
22
|
+
(Array.isArray(allowed) &&
|
|
23
|
+
allowed.some((value) => value.startsWith(".")
|
|
24
|
+
? hostname === value.slice(1) || hostname.endsWith(value)
|
|
25
|
+
: hostname === value));
|
|
26
|
+
if (!trusted || req.headers["sec-fetch-site"] === "cross-site")
|
|
27
|
+
return false;
|
|
28
|
+
if (req.headers.origin && new URL(req.headers.origin).origin !== host.origin)
|
|
29
|
+
return false;
|
|
30
|
+
return true;
|
|
51
31
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
res.setHeader("Allow", "GET, HEAD");
|
|
55
|
-
res.end();
|
|
56
|
-
return;
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
57
34
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"text/html; charset=utf-8"
|
|
73
|
-
);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (url.pathname === `${DEVTOOLS_PATH}/modules.json` && options.inspect) {
|
|
77
|
-
send(JSON.stringify(await inspector.list()), "application/json; charset=utf-8");
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (url.pathname === `${DEVTOOLS_PATH}/module.json` && options.inspect) {
|
|
81
|
-
send(
|
|
82
|
-
JSON.stringify(await inspector.read(url.searchParams.get("id") ?? "")),
|
|
83
|
-
"application/json; charset=utf-8"
|
|
84
|
-
);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (url.pathname.startsWith(`${DEVTOOLS_PATH}/assets/`)) {
|
|
88
|
-
const name = url.pathname.slice(`${DEVTOOLS_PATH}/assets/`.length);
|
|
89
|
-
const type = name === "panel.js" ? "text/javascript; charset=utf-8" : name === "panel.css" ? "text/css; charset=utf-8" : name === "logo.svg" ? "image/svg+xml" : /^[a-zA-Z0-9_-]+\.woff2$/.test(name) ? "font/woff2" : void 0;
|
|
90
|
-
if (type) {
|
|
91
|
-
if (!assets.has(name)) assets.set(name, readFile(new URL(name, assetRoot)));
|
|
92
|
-
send(await assets.get(name), type);
|
|
93
|
-
return;
|
|
35
|
+
}
|
|
36
|
+
export function installDevtoolsServer(server, options) {
|
|
37
|
+
const inspector = createModuleInspector(server);
|
|
38
|
+
const assetRoot = new URL("./ui/", import.meta.url);
|
|
39
|
+
const assets = new Map();
|
|
40
|
+
let closed = false;
|
|
41
|
+
server.httpServer?.once("close", () => {
|
|
42
|
+
closed = true;
|
|
43
|
+
assets.clear();
|
|
44
|
+
});
|
|
45
|
+
server.middlewares.use(async (req, res, next) => {
|
|
46
|
+
let url;
|
|
47
|
+
try {
|
|
48
|
+
url = new URL(req.url || "/", "http://localhost");
|
|
94
49
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
"
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
50
|
+
catch {
|
|
51
|
+
return next();
|
|
52
|
+
}
|
|
53
|
+
if (url.pathname !== DEVTOOLS_PATH &&
|
|
54
|
+
url.pathname !== `${DEVTOOLS_PATH}.json` &&
|
|
55
|
+
!url.pathname.startsWith(`${DEVTOOLS_PATH}/`))
|
|
56
|
+
return next();
|
|
57
|
+
if (closed)
|
|
58
|
+
return next();
|
|
59
|
+
res.setHeader("Cache-Control", "no-store");
|
|
60
|
+
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
61
|
+
res.setHeader("X-Frame-Options", "SAMEORIGIN");
|
|
62
|
+
res.setHeader("Referrer-Policy", "no-referrer");
|
|
63
|
+
if (!isTrustedRequest(req, server)) {
|
|
64
|
+
res.statusCode = 403;
|
|
65
|
+
res.end("DevTools requires a trusted, same-origin development request.");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
69
|
+
res.statusCode = 405;
|
|
70
|
+
res.setHeader("Allow", "GET, HEAD");
|
|
71
|
+
res.end();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const send = (body, type) => {
|
|
75
|
+
res.setHeader("Content-Type", type);
|
|
76
|
+
res.end(req.method === "HEAD" ? undefined : body);
|
|
77
|
+
};
|
|
78
|
+
try {
|
|
79
|
+
if (url.pathname === `${DEVTOOLS_PATH}.json`)
|
|
80
|
+
return next();
|
|
81
|
+
if (url.pathname === DEVTOOLS_PATH) {
|
|
82
|
+
if (url.searchParams.get("embedded") !== "1")
|
|
83
|
+
return next();
|
|
84
|
+
res.setHeader("Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self'; connect-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'none'");
|
|
85
|
+
send(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="color-scheme" content="dark light"><title>Farm.js DevTools</title><link rel="stylesheet" href="${DEVTOOLS_PATH}/assets/panel.css"></head><body><div id="farm-devtools" data-inspect="${options.inspect}"></div><script type="module" src="${DEVTOOLS_PATH}/assets/panel.js"></script></body></html>`, "text/html; charset=utf-8");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (url.pathname === `${DEVTOOLS_PATH}/modules.json` && options.inspect) {
|
|
89
|
+
send(JSON.stringify(await inspector.list()), "application/json; charset=utf-8");
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (url.pathname === `${DEVTOOLS_PATH}/module.json` && options.inspect) {
|
|
93
|
+
send(JSON.stringify(await inspector.read(url.searchParams.get("id") ?? "")), "application/json; charset=utf-8");
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (url.pathname.startsWith(`${DEVTOOLS_PATH}/assets/`)) {
|
|
97
|
+
const name = url.pathname.slice(`${DEVTOOLS_PATH}/assets/`.length);
|
|
98
|
+
const type = name === "panel.js"
|
|
99
|
+
? "text/javascript; charset=utf-8"
|
|
100
|
+
: name === "panel.css"
|
|
101
|
+
? "text/css; charset=utf-8"
|
|
102
|
+
: name === "logo.svg"
|
|
103
|
+
? "image/svg+xml"
|
|
104
|
+
: /^[a-zA-Z0-9_-]+\.woff2$/.test(name)
|
|
105
|
+
? "font/woff2"
|
|
106
|
+
: undefined;
|
|
107
|
+
if (type) {
|
|
108
|
+
if (!assets.has(name))
|
|
109
|
+
assets.set(name, readFile(new URL(name, assetRoot)));
|
|
110
|
+
send(await assets.get(name), type);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
res.statusCode = 404;
|
|
115
|
+
send("Not found", "text/plain; charset=utf-8");
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
res.statusCode = error instanceof ModuleInspectionError ? error.status : 500;
|
|
119
|
+
send(JSON.stringify({
|
|
120
|
+
error: error instanceof ModuleInspectionError
|
|
121
|
+
? error.message
|
|
122
|
+
: "DevTools could not read this resource. Refresh the app and try again.",
|
|
123
|
+
}), "application/json; charset=utf-8");
|
|
124
|
+
}
|
|
125
|
+
});
|
|
108
126
|
}
|
|
109
|
-
export {
|
|
110
|
-
installDevtoolsServer,
|
|
111
|
-
isTrustedRequest
|
|
112
|
-
};
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/devtools",
|
|
3
|
-
"version": "0.1.1-beta.
|
|
3
|
+
"version": "0.1.1-beta.2",
|
|
4
4
|
"description": "Development workspace inspector for Farm.js routes, runtime, and browser modules",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"devtools",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"typescript": "^5.3.3",
|
|
53
53
|
"vite": "^5.0.10",
|
|
54
54
|
"vitest": "^3.2.7",
|
|
55
|
-
"@farm.js/core": "0.1.1-beta.
|
|
55
|
+
"@farm.js/core": "0.1.1-beta.2"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"@farm.js/core": ">=0.1.0-beta.96 <0.2.0"
|