@nsnanocat/preference-panes 0.7.2 → 0.8.0

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/src/build.mjs CHANGED
@@ -2,8 +2,8 @@ import { readFile } from "node:fs/promises";
2
2
  import { BoxJS } from "./BoxJS.mjs";
3
3
 
4
4
  /**
5
- * 从两个配置输入生成一个模块页及其读写与配置 Mock,不生成项目主页。
6
- * Build one module page, its storage script and config Mock without a project landing page.
5
+ * 仅生成模块前端文件,不复制配置或生成绑定业务的读写脚本。
6
+ * Build module frontend files without copying configuration or generating bound storage scripts.
7
7
  * @param {unknown} boxjs 仅包含一个模块的 BoxJS JSON / BoxJS JSON containing exactly one module.
8
8
  * @param {string} [css] 可选自定义 CSS 正文 / Optional custom CSS text.
9
9
  * @returns {Promise<Record<string, string>>} 相对路径到文件正文的映射 / Relative file paths mapped to file contents.
@@ -12,22 +12,12 @@ export async function build(boxjs, css = "") {
12
12
  if (typeof css !== "string") throw new TypeError("CSS must be a string");
13
13
  const catalog = new BoxJS(boxjs);
14
14
  const module = catalog.module.module;
15
- const [html, app, navigation, proxy, mock] = await Promise.all([
16
- readFile(new URL("../dist/module/index.html", import.meta.url), "utf8"),
17
- readFile(new URL("../dist/module/app.mjs", import.meta.url), "utf8"),
18
- readFile(new URL("../dist/module/navigation.mjs", import.meta.url), "utf8"),
19
- readFile(new URL("../dist/preference-panes.proxy.js", import.meta.url), "utf8"),
20
- readFile(new URL("../dist/preference-panes.config.js", import.meta.url), "utf8"),
21
- ]);
22
- const config = JSON.stringify(catalog.select(module));
15
+ const [html, app, navigation] = await Promise.all([readFile(new URL("../dist/module/index.html", import.meta.url), "utf8"), readFile(new URL("../dist/module/app.mjs", import.meta.url), "utf8"), readFile(new URL("../dist/module/navigation.mjs", import.meta.url), "utf8")]);
23
16
  return {
24
17
  [`settings/${module}/index.html`]: html,
25
18
  [`settings/assets/${module}.html`]: html,
26
19
  "settings/assets/app.mjs": app,
27
20
  "settings/assets/navigation.mjs": navigation,
28
- [`settings/assets/${module}.boxjs.json`]: config,
29
21
  [`settings/assets/${module}.css`]: css,
30
- [`settings/assets/${module}.request.js`]: `${proxy}\nPreferencePanes.run(${config},${JSON.stringify(css)});\n`,
31
- [`settings/assets/${module}.config.js`]: `${mock}\nPreferencePanes.mock(${config});\n`,
32
22
  };
33
23
  }
package/src/index.d.ts CHANGED
@@ -92,8 +92,8 @@ export interface SettingsRequest {
92
92
  */
93
93
  headers?: Record<string, string | undefined>;
94
94
  /**
95
- * POST 的正文为 JSON 值本身;DELETE 无正文
96
- * POST contains the JSON value itself; DELETE has no body.
95
+ * POST 的正文为一个 form 字段,字段名是完整 @root.path
96
+ * POST contains one form field whose name is the complete @root.path.
97
97
  */
98
98
  body?: string;
99
99
  }
@@ -113,8 +113,8 @@ export interface SettingsResponse {
113
113
  */
114
114
  headers: Record<string, string>;
115
115
  /**
116
- * JSON 文本;HEAD 始终为空字符串
117
- * JSON text; always an empty string for HEAD.
116
+ * JSON 或页面资源正文
117
+ * JSON or page resource body.
118
118
  */
119
119
  body: string;
120
120
  }
@@ -269,8 +269,8 @@ export interface BoxJSSubscription extends BoxJSMetadata {
269
269
  */
270
270
  export type BoxJSInput = BoxJSSetting[] | BoxJSApp | BoxJSSubscription;
271
271
  /**
272
- * 生成具体模块的页面、代理和配置 Mock,不生成项目入口页。
273
- * Build a concrete module's page, proxy and config Mock without a project landing page.
272
+ * 生成模块前端文件,不复制配置、不生成绑定模块的代理脚本。
273
+ * Build module frontend files without copying configuration or producing bound proxy scripts.
274
274
  * @param boxjs 恰好包含一个模块的 BoxJS JSON / BoxJS JSON describing exactly one module.
275
275
  * @param css 可选 CSS 正文 / Optional CSS text.
276
276
  * @returns 相对路径到文件内容的映射 / Relative paths mapped to file contents.
@@ -11,7 +11,7 @@ export function pageInputs(url, headers = {}) {
11
11
  const module = match[1];
12
12
  const values = Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value]));
13
13
  const json = values["x-preferencepanes-json"] ?? url.searchParams.get("json") ?? `/configs/${module}`;
14
- const css = values["x-preferencepanes-css"] ?? url.searchParams.get("css") ?? `/settings/assets/${module}.css`;
14
+ const css = values["x-preferencepanes-css"] ?? url.searchParams.get("css") ?? "";
15
15
  if (!json.trim()) throw new TypeError("JSON resource URL is required");
16
16
  return { url: url.href, module, json, css };
17
17
  }
@@ -1,21 +1,3 @@
1
- /**
2
- * 解析已经取得的 pathname,避免重复构造 URL。
3
- * Parse an existing pathname without constructing another URL.
4
- * @param {string} pathname 以 / 开头的 URL pathname / URL pathname beginning with /.
5
- * @returns {string[] | undefined} 解码后的路径,非 API 路径不处理 / Decoded path, or undefined outside /api/.
6
- * @throws {TypeError} 转义编码或路径片段非法 / Invalid percent encoding or path segments.
7
- */
8
- export function parseSettingsPathname(pathname) {
9
- if (!pathname.startsWith("/api/")) return;
10
- let parts;
11
- try {
12
- parts = pathname.slice(5).replace(/\/$/, "").split("/").map(decodeURIComponent);
13
- } catch {
14
- throw new TypeError("Invalid encoded key path");
15
- }
16
- return validatePathParts(parts);
17
- }
18
-
19
1
  /**
20
2
  * 校验原始路径片段,不进行 URL 编码转换。
21
3
  * Validate raw path segments without URL encoding conversion.
@@ -1,43 +1,29 @@
1
1
  import { URL } from "@nsnanocat/url";
2
2
  import assets from "#assets";
3
- import { BoxJS } from "../BoxJS.mjs";
4
3
  import { pageInputs } from "../lib/page-inputs.mjs";
5
4
  import { response } from "../lib/response.mjs";
6
5
  import { Store } from "../Store.mjs";
7
6
  import { complete } from "./response.mjs";
8
7
 
9
8
  /**
10
- * 仅为导入的模块提供页面与存储服务,项目主页由调用方自行托管。
11
- * Serve only the imported module's page and persistence; callers host their own project landing pages.
12
- * @param {unknown} boxjs BoxJS JSON / BoxJS JSON.
13
- * @param {string} [css] 自定义 CSS 正文 / Custom CSS text.
14
- * @returns {Promise<void>} 已提交宿主响应 / Delivered host response.
9
+ * 通用代理入口:提供无鉴权 form 存储 API 及模块页面,不含业务配置。
10
+ * Generic proxy entry serving unauthenticated form storage and module pages without business configuration.
11
+ * @returns {Promise<void>} 已交给代理宿主的响应 / Response delivered to the proxy host.
15
12
  */
16
- export async function run(boxjs, css = "") {
13
+ async function run() {
17
14
  const request = globalThis.$request;
18
15
  let result;
19
16
  try {
20
- if (typeof css !== "string") throw new TypeError("CSS must be a string");
21
- const catalog = new BoxJS(boxjs);
22
- const module = catalog.module.module;
23
17
  const url = new URL(request.url);
24
18
  switch (true) {
25
19
  case url.pathname.startsWith("/api/"):
26
- result = await new Store(catalog).handle(request, url);
20
+ result = await new Store().handle(request, url);
27
21
  break;
28
- case url.pathname.startsWith("/configs/"):
29
- break;
30
- case url.pathname === `/settings/${module}` || url.pathname === `/settings/${module}/`: {
31
- // Header 由代理传入文档,浏览器再下载 JSON/CSS;代理不获取外部资源。
32
- // Carry headers into the document; only the browser downloads external JSON/CSS.
22
+ case /^\/settings\/[a-zA-Z0-9_-]+\/?$/.test(url.pathname): {
33
23
  const inputs = encodeURIComponent(JSON.stringify(pageInputs(url, request.headers)));
34
- const html = assets.page.body.replace("</head>", `<meta name="preference-panes-inputs" content="${inputs}"></head>`);
35
- result = response(request, 200, html, "text/html");
24
+ result = response(request, 200, assets.page.body.replace("</head>", `<meta name="preference-panes-inputs" content="${inputs}"></head>`), "text/html");
36
25
  break;
37
26
  }
38
- case url.pathname === `/settings/assets/${module}.css`:
39
- result = response(request, 200, css, "text/css");
40
- break;
41
27
  default: {
42
28
  const asset = assets[url.pathname];
43
29
  if (asset) result = response(request, 200, asset.body, asset.type);
@@ -46,7 +32,8 @@ export async function run(boxjs, css = "") {
46
32
  if (result && !url.pathname.startsWith("/api/") && !["GET", "HEAD"].includes(request.method)) result = response(request, 405, { error: "Method not allowed" });
47
33
  } catch (error) {
48
34
  console.error(`PreferencePanes: ${error.message}`);
49
- result = response(request, 500, { error: "Settings execution failed" });
35
+ result = response(request, 500, { error: error.message });
50
36
  }
51
37
  complete(result);
52
38
  }
39
+ run();