@nsnanocat/preference-panes 0.1.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/LICENSE +201 -0
- package/README.md +109 -0
- package/browser/client.mjs +112 -0
- package/browser/index.mjs +2 -0
- package/browser/panel.css +103 -0
- package/browser/panel.mjs +233 -0
- package/dist/preference-panes.mjs +720 -0
- package/dist/preference-panes.request.js +2129 -0
- package/index.mjs +3 -0
- package/lib/boxjs.mjs +90 -0
- package/lib/settings-handler.mjs +113 -0
- package/lib/settings-path.mjs +20 -0
- package/package.json +69 -0
- package/proxy/request.mjs +36 -0
- package/types/browser.d.ts +32 -0
- package/types/index.d.ts +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# @nsnanocat/preference-panes
|
|
2
|
+
|
|
3
|
+
尚未发布的通用设置面板和代理存储 API,基于 `@nsnanocat/util`。字段直接来自运行时加载的 BoxJS JSON,页面、缓存与读写逻辑不包含具体项目的选项。
|
|
4
|
+
|
|
5
|
+
## 工作方式
|
|
6
|
+
|
|
7
|
+
| 操作 | 网络请求 | 页面缓存 |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| 进入主菜单 | 并发 HEAD `/configs/<模块>` | 不读取持久化设置 |
|
|
10
|
+
| 打开、再次进入或刷新模块页 | GET 模块 BoxJS,再 GET 声明字段的公共子树,各一次 | 建立新的内存会话,替换旧值 |
|
|
11
|
+
| 修改单值 | POST 对应键路径,正文为 JSON 值本身 | 仅 HTTP 200 后更新该键 |
|
|
12
|
+
| 删除单值 | DELETE 对应键路径 | 仅 HTTP 200 后移除覆盖值,显示 BoxJS 默认值 |
|
|
13
|
+
| 保存、删除后 | 不追加 GET | 成功或失败显示临时通知 |
|
|
14
|
+
|
|
15
|
+
只有 `/api/` 是固定前缀。`@BiliBili.Enhanced.Settings.Home.Top_left` 映射为 `/api/Enhanced/Settings/Home/Top_left`;`BiliBili` 从 BoxJS ID 解析,不经浏览器 header 传递。
|
|
16
|
+
|
|
17
|
+
配置资源与数据接口使用不同前缀:`/configs/Enhanced` 是 BoxJS Mock,`/api/Enhanced/…` 是持久化 API。配置 Mock 地址不部署同名线上文件,关闭模块后 HEAD 非 200 或失败即禁用入口;它的下载源可以是另一个在线资源地址。公共 HTML、JS、CSS 可以在线托管后由代理 Mock 提供,选项由浏览器实时生成。
|
|
18
|
+
|
|
19
|
+
## 浏览器组件
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { mountPreferencePanes } from "@nsnanocat/preference-panes/browser";
|
|
23
|
+
import "@nsnanocat/preference-panes/browser/panel.css";
|
|
24
|
+
|
|
25
|
+
const panel = mountPreferencePanes({
|
|
26
|
+
element: document.querySelector("#preferences"),
|
|
27
|
+
title: "Preferences"
|
|
28
|
+
});
|
|
29
|
+
// 卸载时 panel.destroy()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
同一份 HTML 从 `/settings/{module}` 路径读取模块标识,按固定约定读取配置:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
/settings/Enhanced → GET /configs/Enhanced
|
|
36
|
+
/settings/Global → GET /configs/Global
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
不需要查询参数。路径中的 module 既决定配置 Mock 路径,也选择 BoxJS ID 中对应模块的字段,例如 `@BiliBili.Enhanced.Settings.…` 中的 Enhanced。配置文件的实际下载地址由模块模板中的 Mock 规则指定,不写入 HTML 或页面 URL。HTML 和通用 JS 不包含业务模块目录。
|
|
40
|
+
|
|
41
|
+
| 地址 | 谁响应 | 内容 |
|
|
42
|
+
| --- | --- | --- |
|
|
43
|
+
| `/settings/Enhanced` | 公共 HTML Mock | 通用设置页面 |
|
|
44
|
+
| `/configs/Enhanced` | Enhanced 的配置 Mock | argument config 经原有生成器生成的 BoxJS JSON |
|
|
45
|
+
| `/api/Enhanced/` 或 `/api/Enhanced/Settings/` | 通用读写脚本 | 已声明字段的持久化子树 |
|
|
46
|
+
| `/api/Enhanced/Settings/Home/Top_left` | 同一个通用读写脚本 | 单键 GET/POST/DELETE |
|
|
47
|
+
|
|
48
|
+
模块模板的配置规则只匹配 `/configs/…`,脚本规则只匹配 `/api/…`,两者没有交集,不依赖命中先后顺序。代理脚本自己的 configURL 参数由模块模板提供,指向与 Mock 相同版本的 BoxJS 下载源,用于写入校验;该 configURL 是代理脚本的模块模板参数,不是页面参数。
|
|
49
|
+
|
|
50
|
+
业务主菜单由调用项目维护(Biliverse 由 Enhanced 负责),每次进入通过 `client.probe(module)` 并发 HEAD `/configs/{module}`,再打开 `/settings/模块标识`。HEAD 只检测配置 Mock 可用性,不读取存储。
|
|
51
|
+
|
|
52
|
+
通用设置页每次进入、刷新或浏览器缓存恢复时,用 `client.open(module)` GET `/configs/{module}` 一次,再 GET 持久化子树一次。用配置实时生成表单,值放在内存,不使用 localStorage/sessionStorage。保存/删除仅 HTTP 200 后更新缓存和通知,不追加 GET。现有 UI 可直接使用同一客户端的 snapshot/set/remove/leave;snapshot 返回副本。
|
|
53
|
+
|
|
54
|
+
默认 CSS 是独立的通用样式,可由调用方替换;本包不依赖 Bilibili 样式或框架。字段定义由 BoxJS 决定,通用 JS 只定义每类控件如何绘制,不编入各项目的具体选项。
|
|
55
|
+
|
|
56
|
+
## 代理读写组件
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
import { createSettingsHandler } from "@nsnanocat/preference-panes";
|
|
60
|
+
import { fetch } from "@nsnanocat/util/polyfill/fetch";
|
|
61
|
+
|
|
62
|
+
const handle = createSettingsHandler({
|
|
63
|
+
origin: "https://example.org",
|
|
64
|
+
loadConfig: async module => {
|
|
65
|
+
const response = await fetch(`https://assets.example.org/${module}.boxjs.json`);
|
|
66
|
+
if (response.status !== 200) throw new Error(`BoxJS HTTP ${response.status}`);
|
|
67
|
+
return JSON.parse(response.body);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const response = await handle($request);
|
|
71
|
+
// 接入现有平台的 done 适配;或直接使用下述打包入口。
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
每个支持面板的模块都携带自己的配置 Mock,并引用同一个通用读写脚本。脚本正则只匹配各自 `/api/<模块>` 的数据路径,配置 Mock 则只匹配 `/configs/`。处理器每次键值请求运行时加载配置,仅允许操作已声明的字段。
|
|
75
|
+
|
|
76
|
+
持久化 GET 调用一次 util `Storage.getItem`;POST/DELETE 在写入前重新读取最新根对象,再用 util `Lodash.set/unset` 修改单键并 `Storage.setItem` 写回,保留其它模块、隐藏字段和缓存。这是代理端必要的读改写,浏览器不会因此重新 GET 整个模块。多个独立脚本上下文同时写同一根键仍受代理存储无事务能力的限制。
|
|
77
|
+
|
|
78
|
+
`resolveSettings(stored, definition)` 是可选的 GET 解析器,用来按模块既有规则合并 database、argument、持久化值。默认只返回持久化覆盖值,控件缺值时使用 BoxJS `val`。本包不修改插件原有的配置优先级;删除后不重新计算 resolver,而是在下次进入/刷新时重新读取。
|
|
79
|
+
|
|
80
|
+
## BoxJS 范围
|
|
81
|
+
|
|
82
|
+
支持 settings 数组、单个 app 的 `settings`、订阅的 `apps[].settings`;控件类型支持 boolean、selects、checkboxes、text、textarea、number。不执行 BoxJS 脚本或 HTML。
|
|
83
|
+
|
|
84
|
+
设置 ID 必须为 `@存储根.模块.子路径.键`。同一模块使用一个存储根,字段路径不得重复或父子重叠。为了用一次 GET 获取设置,字段必须具有模块根以下的公共父路径,例如 `Enhanced.Settings` 或 `Weather.Preferences`;公共路径自动计算,不固定为 Settings。不满足条件或遇到不支持的控件会报错。
|
|
85
|
+
|
|
86
|
+
## 打包与示例
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
npm ci --registry=https://registry.npmjs.org/ --@nsnanocat:registry=https://registry.npmjs.org/
|
|
90
|
+
npm run build
|
|
91
|
+
npm run check
|
|
92
|
+
npm run apifox:generate
|
|
93
|
+
node scripts/generate-apifox.mjs --check
|
|
94
|
+
npm pack --dry-run
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
构建生成可直接加载的 `dist/preference-panes.mjs` 和代理 IIFE `dist/preference-panes.request.js`,公共样式位于 `browser/panel.css`。代理包包括 util 的平台适配和 `@nsnanocat/url`,不依赖 Node 内置模块。
|
|
98
|
+
|
|
99
|
+
[Surge 模板](examples/surge.sgmodule)使用原生 Map Local 提供静态资源,http-request 提供持久化 API。模板中的域名均为占位,尚未部署;需要把源码资源和 dist 产物发布到自己的资源地址。其 `argument` 只配置 `origin` 和 `configURL`,不会固化字段。Map Local 下载缓存的更新时机由代理管理;浏览器 no-store 不会强制 Surge 更新资源缓存。配置 Mock 与脚本 configURL 应引用同一版本的 BoxJS。
|
|
100
|
+
|
|
101
|
+
Quantumult X 等不能通过模板传递 `$argument` 的平台,需要在构建入口注入这两个地址参数,再打包同一通用执行端;本仓库没有声称该 Surge 模板可直接安装到其它代理。隔离测试覆盖 Surge/QX 宿主 API,尚未在用户设备上安装验收。
|
|
102
|
+
|
|
103
|
+
## 接口文档与发布
|
|
104
|
+
|
|
105
|
+
- [完整请求、返回与缓存时序说明](apifox/guide.md)
|
|
106
|
+
- [Apifox 原生 JSON](apifox/preference-panes.apifox.json)
|
|
107
|
+
- [Apifox 项目](https://app.apifox.com/project/8803052)、[Git 数据源绑定记录](apifox/sync.md)
|
|
108
|
+
|
|
109
|
+
main/dev 普通推送与手动 CI 只验证并生成候选 tgz;两套 v* tag workflow 分别发布 npm 与 GitHub Packages。首版拟定为 `0.1.0`,发布步骤、首次 npm 注册与 Trusted Publisher 配置见 [RELEASING.md](RELEASING.md)。Enhanced 可先用候选 tgz 验证,正式依赖及 lockfile 必须在 registry 首发成功后生成。
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Lodash as _ } from "@nsnanocat/util/polyfill/Lodash.mjs";
|
|
2
|
+
import { normalizeBoxJs, normalizeStoredValue, validValue } from "../lib/boxjs.mjs";
|
|
3
|
+
import { parseSettingsPath } from "../lib/settings-path.mjs";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 创建页面会话缓存;打开时重读,选项操作仅在 HTTP 200 后更新缓存。
|
|
7
|
+
* Create a page-session cache; reload on open and mutate cache only after HTTP 200.
|
|
8
|
+
* @param {import("../types/browser.js").PreferencesClientOptions} options 请求与通知 / Requests and notifications.
|
|
9
|
+
* @returns {import("../types/browser.js").PreferencesClient} 通用客户端 / Generic client.
|
|
10
|
+
*/
|
|
11
|
+
export function createPreferencesClient({ fetch: request = globalThis.fetch.bind(globalThis), notify = () => {}, timeout = 10000 } = {}) {
|
|
12
|
+
const sessions = new Map();
|
|
13
|
+
async function send(path, method, body, signal, resource = false) {
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
const abort = () => controller.abort();
|
|
16
|
+
if (signal?.aborted) abort();
|
|
17
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
18
|
+
const timer = setTimeout(abort, timeout);
|
|
19
|
+
try {
|
|
20
|
+
const response = await request(path, {
|
|
21
|
+
method,
|
|
22
|
+
credentials: "omit",
|
|
23
|
+
cache: "no-store",
|
|
24
|
+
signal: controller.signal,
|
|
25
|
+
headers: resource ? {} : { "X-Settings-Client": "1", ...(method === "POST" ? { "Content-Type": "application/json" } : {}) },
|
|
26
|
+
...(method === "POST" ? { body: JSON.stringify(body) } : {}),
|
|
27
|
+
});
|
|
28
|
+
if (response.status !== 200) throw new Error(`HTTP ${response.status}`);
|
|
29
|
+
return response;
|
|
30
|
+
} finally {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
signal?.removeEventListener("abort", abort);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const configPath = (module) => {
|
|
36
|
+
if (typeof module !== "string" || !module) throw new TypeError("module is required");
|
|
37
|
+
const parts = parseSettingsPath(`https://example.invalid/api/${encodeURIComponent(module)}/`);
|
|
38
|
+
if (parts.length !== 1) throw new TypeError("Expected a module name");
|
|
39
|
+
return `/configs/${encodeURIComponent(module)}`;
|
|
40
|
+
};
|
|
41
|
+
const snapshot = (module) => {
|
|
42
|
+
const state = sessions.get(module);
|
|
43
|
+
if (!state?.definition) throw new Error("Open the module first");
|
|
44
|
+
return structuredClone({ definition: state.definition, values: state.values });
|
|
45
|
+
};
|
|
46
|
+
async function change(module, key, method, value) {
|
|
47
|
+
const state = sessions.get(module);
|
|
48
|
+
if (!state?.definition) throw new Error("Open the module first");
|
|
49
|
+
if (state.saving) throw new Error("A settings write is already in progress");
|
|
50
|
+
const field = state.definition.fields.find((field) => field.key === key);
|
|
51
|
+
state.saving = true;
|
|
52
|
+
try {
|
|
53
|
+
if (!field || (method === "POST" && !validValue(field, value))) throw new TypeError("Invalid setting value");
|
|
54
|
+
await send(`/api/${key.split(".").map(encodeURIComponent).join("/")}`, method, value);
|
|
55
|
+
if (sessions.get(module) === state) {
|
|
56
|
+
if (method === "DELETE") {
|
|
57
|
+
delete state.values[key];
|
|
58
|
+
if (Object.hasOwn(field, "defaultValue")) state.values[key] = structuredClone(field.defaultValue);
|
|
59
|
+
} else state.values[key] = structuredClone(value);
|
|
60
|
+
}
|
|
61
|
+
notify({ kind: "success", operation: method === "DELETE" ? "delete" : "write", module, key });
|
|
62
|
+
} catch (error) {
|
|
63
|
+
notify({ kind: "error", operation: method === "DELETE" ? "delete" : "write", module, key, message: error.message });
|
|
64
|
+
throw error;
|
|
65
|
+
} finally {
|
|
66
|
+
state.saving = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
async probe(module) {
|
|
71
|
+
try {
|
|
72
|
+
await send(configPath(module), "HEAD", undefined, undefined, true);
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
async open(module) {
|
|
79
|
+
const previous = sessions.get(module);
|
|
80
|
+
if (previous?.saving) throw new Error("Cannot refresh while saving");
|
|
81
|
+
previous?.controller.abort();
|
|
82
|
+
const state = { controller: new AbortController(), definition: null, values: {}, saving: false };
|
|
83
|
+
sessions.set(module, state);
|
|
84
|
+
try {
|
|
85
|
+
const resource = configPath(module);
|
|
86
|
+
const definition = normalizeBoxJs(await (await send(resource, "GET", undefined, state.controller.signal, true)).json(), module);
|
|
87
|
+
if (definition.settingsPath.length < 2) throw new TypeError("BoxJS fields must share a settings subtree below the module root");
|
|
88
|
+
const subtree = await (
|
|
89
|
+
await send(`/api/${definition.settingsPath.map(encodeURIComponent).join("/")}/`, "GET", undefined, state.controller.signal)
|
|
90
|
+
).json();
|
|
91
|
+
if (!subtree || typeof subtree !== "object" || Array.isArray(subtree)) throw new TypeError("Expected a settings subtree object");
|
|
92
|
+
if (sessions.get(module) !== state) throw new Error("Module session was replaced");
|
|
93
|
+
state.definition = definition;
|
|
94
|
+
for (const field of definition.fields) {
|
|
95
|
+
const value = _.get(subtree, field.key.split(".").slice(definition.settingsPath.length), field.defaultValue);
|
|
96
|
+
if (value !== undefined) state.values[field.key] = normalizeStoredValue(field, value);
|
|
97
|
+
}
|
|
98
|
+
return snapshot(module);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (sessions.get(module) === state) sessions.delete(module);
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
snapshot,
|
|
105
|
+
leave(module) {
|
|
106
|
+
sessions.get(module)?.controller.abort();
|
|
107
|
+
sessions.delete(module);
|
|
108
|
+
},
|
|
109
|
+
set: (module, key, value) => change(module, key, "POST", value),
|
|
110
|
+
remove: (module, key) => change(module, key, "DELETE"),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
.pp-panel {
|
|
2
|
+
font:
|
|
3
|
+
15px / 1.5 system-ui,
|
|
4
|
+
sans-serif;
|
|
5
|
+
color: var(--pp-text, #18191c);
|
|
6
|
+
background: var(--pp-background, #f6f7f8);
|
|
7
|
+
max-width: 760px;
|
|
8
|
+
margin: auto;
|
|
9
|
+
position: relative;
|
|
10
|
+
}
|
|
11
|
+
.pp-panel * {
|
|
12
|
+
box-sizing: border-box;
|
|
13
|
+
}
|
|
14
|
+
.pp-header {
|
|
15
|
+
display: flex;
|
|
16
|
+
align-items: center;
|
|
17
|
+
gap: 12px;
|
|
18
|
+
padding: 12px 16px;
|
|
19
|
+
background: var(--pp-surface, #fff);
|
|
20
|
+
}
|
|
21
|
+
.pp-title {
|
|
22
|
+
font-size: 18px;
|
|
23
|
+
margin: 0;
|
|
24
|
+
}
|
|
25
|
+
.pp-viewport {
|
|
26
|
+
max-height: 80vh;
|
|
27
|
+
overflow: auto;
|
|
28
|
+
padding: 16px;
|
|
29
|
+
}
|
|
30
|
+
.pp-panel button {
|
|
31
|
+
font: inherit;
|
|
32
|
+
cursor: pointer;
|
|
33
|
+
border: 1px solid var(--pp-border, #d8dce0);
|
|
34
|
+
border-radius: 8px;
|
|
35
|
+
padding: 8px 12px;
|
|
36
|
+
color: inherit;
|
|
37
|
+
background: var(--pp-surface, #fff);
|
|
38
|
+
}
|
|
39
|
+
.pp-panel button:disabled {
|
|
40
|
+
opacity: 0.45;
|
|
41
|
+
cursor: default;
|
|
42
|
+
}
|
|
43
|
+
.pp-field {
|
|
44
|
+
margin: 0 0 16px;
|
|
45
|
+
padding: 16px;
|
|
46
|
+
border: 1px solid var(--pp-border, #d8dce0);
|
|
47
|
+
border-radius: 10px;
|
|
48
|
+
background: var(--pp-surface, #fff);
|
|
49
|
+
min-width: 0;
|
|
50
|
+
}
|
|
51
|
+
.pp-description {
|
|
52
|
+
font-size: 13px;
|
|
53
|
+
opacity: 0.7;
|
|
54
|
+
margin: 0 0 8px;
|
|
55
|
+
}
|
|
56
|
+
.pp-input:not([type="checkbox"]) {
|
|
57
|
+
width: 100%;
|
|
58
|
+
font: inherit;
|
|
59
|
+
padding: 8px;
|
|
60
|
+
border: 1px solid var(--pp-border, #d8dce0);
|
|
61
|
+
border-radius: 6px;
|
|
62
|
+
background: var(--pp-surface, #fff);
|
|
63
|
+
color: inherit;
|
|
64
|
+
}
|
|
65
|
+
.pp-choice {
|
|
66
|
+
display: block;
|
|
67
|
+
margin: 6px 0;
|
|
68
|
+
}
|
|
69
|
+
.pp-choice input {
|
|
70
|
+
margin-right: 8px;
|
|
71
|
+
}
|
|
72
|
+
.pp-actions {
|
|
73
|
+
display: flex;
|
|
74
|
+
gap: 8px;
|
|
75
|
+
margin-top: 12px;
|
|
76
|
+
}
|
|
77
|
+
.pp-toast {
|
|
78
|
+
position: fixed;
|
|
79
|
+
bottom: 32px;
|
|
80
|
+
left: 50%;
|
|
81
|
+
transform: translateX(-50%);
|
|
82
|
+
padding: 10px 18px;
|
|
83
|
+
border-radius: 10px;
|
|
84
|
+
background: #252525;
|
|
85
|
+
color: #fff;
|
|
86
|
+
max-width: 90vw;
|
|
87
|
+
z-index: 1000;
|
|
88
|
+
}
|
|
89
|
+
.pp-toast[data-kind="error"] {
|
|
90
|
+
background: #8d2424;
|
|
91
|
+
}
|
|
92
|
+
.pp-panel :focus-visible {
|
|
93
|
+
outline: 2px solid #008ac5;
|
|
94
|
+
outline-offset: 2px;
|
|
95
|
+
}
|
|
96
|
+
@media (prefers-color-scheme: dark) {
|
|
97
|
+
.pp-panel {
|
|
98
|
+
--pp-text: #eee;
|
|
99
|
+
--pp-background: #18191c;
|
|
100
|
+
--pp-surface: #25262a;
|
|
101
|
+
--pp-border: #47494e;
|
|
102
|
+
}
|
|
103
|
+
}
|