dsh-crate 0.5.7 → 0.5.8

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.
@@ -1,14 +1,13 @@
1
1
  /**
2
2
  * 每个 profile 的状态目录名。
3
- * 0.5 起由 `.dsh-shelfry` 改为 `.dsh-crate`,旧目录在首次访问时就地改名。
3
+ * 0.5 起统一为 `.dsh-crate`,0.4.x 的旧目录不再读取或迁移。
4
4
  */
5
5
  export declare const STATE_DIR_NAME = ".dsh-crate";
6
6
  /**
7
- * 返回可用的状态目录。
7
+ * 返回状态目录。
8
8
  *
9
9
  * 入参:profileDir —— profile 根目录的绝对路径。
10
- * 返回:新目录路径;旧目录存在且新目录不存在时,先把旧目录改名再返回。
11
- * 改名失败时回退到旧目录,保证数据仍可读写,不影响启动。
10
+ * 返回:状态目录路径;目录本身由调用方按需创建。
12
11
  */
13
12
  export declare function stateDir(profileDir: string): string;
14
13
  /**
@@ -0,0 +1,52 @@
1
+ import { marketFetch } from './net.ts';
2
+ /**
3
+ * The anonymous daily count.
4
+ *
5
+ * One POST per install per UTC day, from the Node side only, carrying five
6
+ * fields (see `Ping`). On a day the user opens the plugin page, a second POST
7
+ * carries the same five plus `opened: true` — the only thing it adds is that
8
+ * the page was opened, never what was done there. It is ON by default and off
9
+ * with one switch in the settings card or with `DSH_CRATE_TELEMETRY=0` /
10
+ * `DO_NOT_TRACK=1`. Everything about it is best effort: a failure changes
11
+ * nothing for the user and is never an error.
12
+ */
13
+ export declare const TELEMETRY_URL = "https://dshcrate.com/api/telemetry";
14
+ /** The whole payload. Adding a field here is a privacy change: update SECURITY.md. */
15
+ export interface Ping {
16
+ id: string;
17
+ v: string;
18
+ hv: string | null;
19
+ kind: 'desktop' | 'web';
20
+ region: string;
21
+ /** Present (and true) only on the day's first open of the plugin page. */
22
+ opened?: true;
23
+ }
24
+ interface TelemetryState {
25
+ id?: string;
26
+ lastDay?: string;
27
+ lastOpenDay?: string;
28
+ enabled: boolean;
29
+ }
30
+ export declare function telemetryFile(home?: string): string;
31
+ export declare function readTelemetryState(file?: string): TelemetryState;
32
+ /** Turn the count on or off. Keeps the rest of the file; deletes nothing else. */
33
+ export declare function setTelemetryEnabled(enabled: boolean, file?: string): void;
34
+ /** Why nothing may be sent right now, or null. Explicit opt-outs always win. */
35
+ export declare function suppressedBy(env: Record<string, string | undefined>): string | null;
36
+ export interface HeartbeatOptions {
37
+ version: string;
38
+ hostVersion: () => string | null;
39
+ kind: () => 'desktop' | 'web';
40
+ region: () => string;
41
+ file?: string;
42
+ env?: Record<string, string | undefined>;
43
+ now?: () => number;
44
+ fetch?: typeof marketFetch;
45
+ }
46
+ /** Send today's count if it is due. Never throws. */
47
+ export declare function sendHeartbeat(options: HeartbeatOptions): Promise<'sent' | 'skipped' | 'failed'>;
48
+ /** Note that the plugin page was opened today, if that has not been sent yet. Never throws. */
49
+ export declare function sendOpened(options: HeartbeatOptions): Promise<'sent' | 'skipped' | 'failed'>;
50
+ /** Start the schedule: first check after a short delay, then every ~6 hours. */
51
+ export declare function startHeartbeat(options: HeartbeatOptions, delayMs?: number, everyMs?: number): () => void;
52
+ export {};
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The remembered WebDAV password (#1058), opt-in and off by default.
3
+ *
4
+ * The DSH host exposes no secret store to plugins, so the one place it can
5
+ * live is a 0600 file in the market's own state directory under DSH_HOME —
6
+ * outside the profile directory, so exported backups (which walk the profile)
7
+ * never contain it. It is bound to the URL and username it was saved for: a
8
+ * password is never sent to a different server just because the form changed.
9
+ */
10
+ export interface RememberedWebdav {
11
+ url: string;
12
+ username: string;
13
+ password: string;
14
+ }
15
+ export declare function webdavSecretFile(home?: string): string;
16
+ export declare function readWebdavSecret(file?: string): RememberedWebdav | null;
17
+ /** The remembered password, only when it was saved for exactly this URL and username. */
18
+ export declare function webdavSecretFor(url: string, username: string, file?: string): string | null;
19
+ export declare function writeWebdavSecret(value: RememberedWebdav, file?: string): void;
20
+ export declare function clearWebdavSecret(file?: string): void;
@@ -0,0 +1,34 @@
1
+ import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { resolveDshHome } from './home-paths.js';
4
+ export function webdavSecretFile(home = resolveDshHome()) {
5
+ return join(home, 'dsh-crate', 'webdav.json');
6
+ }
7
+ export function readWebdavSecret(file = webdavSecretFile()) {
8
+ try {
9
+ const value = JSON.parse(readFileSync(file, 'utf8'));
10
+ if (typeof value.url === 'string' && typeof value.username === 'string' && typeof value.password === 'string' && value.password !== '') {
11
+ return { url: value.url, username: value.username, password: value.password };
12
+ }
13
+ }
14
+ catch { /* absent or unreadable: nothing remembered */ }
15
+ return null;
16
+ }
17
+ /** The remembered password, only when it was saved for exactly this URL and username. */
18
+ export function webdavSecretFor(url, username, file = webdavSecretFile()) {
19
+ const stored = readWebdavSecret(file);
20
+ return stored !== null && stored.url === url && stored.username === username ? stored.password : null;
21
+ }
22
+ export function writeWebdavSecret(value, file = webdavSecretFile()) {
23
+ mkdirSync(dirname(file), { recursive: true });
24
+ const temp = `${file}.${process.pid}.tmp`;
25
+ writeFileSync(temp, JSON.stringify(value), { mode: 0o600 });
26
+ try {
27
+ chmodSync(temp, 0o600);
28
+ }
29
+ catch { /* filesystems without POSIX modes */ }
30
+ renameSync(temp, file);
31
+ }
32
+ export function clearWebdavSecret(file = webdavSecretFile()) {
33
+ rmSync(file, { force: true });
34
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-crate",
3
3
  "description": "逛一逛,点一下,装好。浏览和搜索 DeepSeek Harness 社区插件,看详情、一键安装,收藏和更新都在一处。",
4
- "version": "0.5.7",
4
+ "version": "0.5.8",
5
5
  "icon": "assets/logo.svg",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",
@@ -1,179 +0,0 @@
1
- /**
2
- * shelfry 目录适配层。
3
- *
4
- * shelfry 的 dsh-shelfry.json 与市场内部使用的 Registry 是两份不同的契约:
5
- * 前者用单字母字段(i/n/d/s/t/vr/cmd)描述条目,后者要求 category、owner、
6
- * description 等结构化字段。本模块只做一次单向映射,并把外部输入的校验收在
7
- * 这里;抓取、重试与缓存仍留在 registry.ts。
8
- *
9
- * 字段对应关系:
10
- * - `i` owner/repo,同时决定展示页与兜底安装源
11
- * - `n` 展示名(市场内部作为 name 使用)
12
- * - `d` 描述(shelfry 只提供一份文本,中英两栏共用)
13
- * - `s` star 数
14
- * - `vr` 最新版本号
15
- * - `t` 条目类型,映射成市场分类 id
16
- * - `cmd` 源站给出的安装命令;其中若是普通 npm 包名,同时作为 npm 字段
17
- */
18
- import { NPM_NAME_RE } from './sources.js';
19
- /** shelfry 条目类型到市场分类 id 的映射;未知类型一律归入 other。 */
20
- const TYPE_TO_CATEGORY = {
21
- 'cordis-plugin': 'plugin',
22
- skill: 'skill',
23
- theme: 'theme',
24
- };
25
- /** 分类的本地化标签;主题分类让插件端的主题页有内容可筛。 */
26
- const CATEGORY_LABELS = {
27
- plugin: { zh: '插件', en: 'Plugins' },
28
- skill: { zh: '技能', en: 'Skills' },
29
- theme: { zh: '主题', en: 'Themes' },
30
- other: { zh: '其他', en: 'Other' },
31
- };
32
- /** 分类字典的输出顺序:插件、技能、主题、其他。 */
33
- const CATEGORY_ORDER = ['plugin', 'skill', 'theme', 'other'];
34
- /** `i` 字段的唯一合法形态:owner/repo。 */
35
- const REPO_PATH_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u;
36
- /** 安装命令的前缀,源站与本地补全都按它拼接。 */
37
- const INSTALL_PREFIX = 'dsh plugin --profile web add ';
38
- /** 安装命令里分隔「参数」与「安装源」的词。 */
39
- const ADD_TOKEN = ' add ';
40
- /**
41
- * 判断一份 JSON 是否为 shelfry 目录。
42
- *
43
- * 只用结构特征,不依赖某个版本号:带 `entries` 数组且没有 `plugins` 数组的
44
- * 就是 shelfry 形态,旧目录照常走 `asRegistry`,两套夹具互不干扰。
45
- */
46
- export function isShelfryCatalog(value) {
47
- if (typeof value !== 'object' || value === null)
48
- return false;
49
- const data = value;
50
- return Array.isArray(data.entries) && !Array.isArray(data.plugins);
51
- }
52
- /**
53
- * 把 shelfry 目录映射成市场内部的 Registry。
54
- *
55
- * @param value - 已经 `JSON.parse` 过的载荷。
56
- * @returns 条目已校验、分类已归一化的目录。
57
- * @throws 载荷不是对象、缺少 entries、或没有一条可用条目时抛出。
58
- */
59
- export function asShelfryRegistry(value) {
60
- if (typeof value !== 'object' || value === null)
61
- throw new Error('shelfry 目录不是对象');
62
- const data = value;
63
- if (!Array.isArray(data.entries))
64
- throw new Error('shelfry 目录缺少 entries 数组');
65
- const plugins = [];
66
- for (const entry of data.entries) {
67
- const plugin = asShelfryPlugin(entry);
68
- if (plugin !== null)
69
- plugins.push(plugin);
70
- }
71
- if (plugins.length === 0)
72
- throw new Error('shelfry 目录里没有一条可用条目');
73
- return {
74
- updated: typeof data.generatedAt === 'string' ? data.generatedAt : '',
75
- count: plugins.length,
76
- categories: categoriesOf(plugins),
77
- plugins,
78
- };
79
- }
80
- /**
81
- * 单条 shelfry 条目转成 RegistryPlugin。
82
- *
83
- * @param entry - entries 数组里的一项,来源不可信,逐字段校验。
84
- * @returns 映射结果;缺少仓库路径或展示名时返回 null,由调用方跳过。
85
- */
86
- export function asShelfryPlugin(entry) {
87
- if (typeof entry !== 'object' || entry === null)
88
- return null;
89
- const row = entry;
90
- const repo = typeof row.i === 'string' ? row.i.trim() : '';
91
- if (!REPO_PATH_RE.test(repo))
92
- return null;
93
- const name = typeof row.n === 'string' ? row.n.trim() : '';
94
- if (name === '')
95
- return null;
96
- const npm = npmFromCommand(row.cmd);
97
- const text = typeof row.d === 'string' ? row.d.trim() : '';
98
- return {
99
- name,
100
- owner: repo.slice(0, repo.indexOf('/')),
101
- url: `https://github.com/${repo}`,
102
- category: categoryOf(row.t),
103
- description: { zh: text, en: text },
104
- npm,
105
- stars: starCountOf(row.s),
106
- version: typeof row.vr === 'string' && row.vr.trim() !== '' ? row.vr.trim() : null,
107
- icon: null,
108
- install: installCommandOf(row.cmd, npm, repo),
109
- added: '',
110
- };
111
- }
112
- /**
113
- * 条目类型转分类 id。
114
- *
115
- * @param type - shelfry 的 `t` 字段,可能是任何类型。
116
- */
117
- export function categoryOf(type) {
118
- if (typeof type !== 'string')
119
- return 'other';
120
- return TYPE_TO_CATEGORY[type] ?? 'other';
121
- }
122
- /**
123
- * 从安装命令里取出 npm 包名。
124
- *
125
- * 命令形如 `dsh plugin --profile web add @scope/name@1.2.3`;版本号与 tag 都要
126
- * 去掉,带变量的地址(`github:...#${DSH_PLUGIN_REF}`)和本地绝对路径都不是
127
- * npm 包名,返回 null 让安装回落到 GitHub 源。
128
- *
129
- * @param command - shelfry 的 `cmd` 字段,可能是任何类型。
130
- * @returns 合法 npm 包名,或 null。
131
- */
132
- export function npmFromCommand(command) {
133
- if (typeof command !== 'string')
134
- return null;
135
- const index = command.indexOf(ADD_TOKEN);
136
- if (index < 0)
137
- return null;
138
- const spec = stripSpecVersion(command.slice(index + ADD_TOKEN.length).trim());
139
- return NPM_NAME_RE.test(spec) ? spec : null;
140
- }
141
- /**
142
- * 去掉安装源末尾的 `@版本` 或 `@tag`。
143
- *
144
- * 从下标 1 开始查找,避免把作用域包名开头的 `@` 当成版本分隔符。
145
- *
146
- * @param spec - 安装源,例如 `@scope/name@1.2.3`。
147
- */
148
- export function stripSpecVersion(spec) {
149
- const at = spec.indexOf('@', 1);
150
- return at === -1 ? spec : spec.slice(0, at);
151
- }
152
- /** 卡片上展示的安装命令:优先用源站给的,缺省时按 npm / GitHub 源补全。 */
153
- function installCommandOf(command, npm, repo) {
154
- if (typeof command === 'string' && command.trim() !== '')
155
- return command.trim();
156
- return `${INSTALL_PREFIX}${npm ?? `github:${repo}`}`;
157
- }
158
- /** star 数只接受有限数字,其余一律当作「未知」而不是 0。 */
159
- function starCountOf(value) {
160
- return typeof value === 'number' && Number.isFinite(value) ? value : null;
161
- }
162
- /** 按固定顺序收集实际出现过的分类,供筛选栏渲染。 */
163
- function categoriesOf(plugins) {
164
- const used = new Set();
165
- for (const plugin of plugins) {
166
- const category = plugin.category;
167
- if (typeof category === 'string')
168
- used.add(category);
169
- else
170
- for (const id of category)
171
- used.add(id);
172
- }
173
- const categories = {};
174
- for (const id of CATEGORY_ORDER) {
175
- if (used.has(id))
176
- categories[id] = CATEGORY_LABELS[id];
177
- }
178
- return categories;
179
- }
@@ -1,48 +0,0 @@
1
- import type { Registry, RegistryPlugin } from './registry.ts';
2
- /**
3
- * 判断一份 JSON 是否为 shelfry 目录。
4
- *
5
- * 只用结构特征,不依赖某个版本号:带 `entries` 数组且没有 `plugins` 数组的
6
- * 就是 shelfry 形态,旧目录照常走 `asRegistry`,两套夹具互不干扰。
7
- */
8
- export declare function isShelfryCatalog(value: unknown): boolean;
9
- /**
10
- * 把 shelfry 目录映射成市场内部的 Registry。
11
- *
12
- * @param value - 已经 `JSON.parse` 过的载荷。
13
- * @returns 条目已校验、分类已归一化的目录。
14
- * @throws 载荷不是对象、缺少 entries、或没有一条可用条目时抛出。
15
- */
16
- export declare function asShelfryRegistry(value: unknown): Registry;
17
- /**
18
- * 单条 shelfry 条目转成 RegistryPlugin。
19
- *
20
- * @param entry - entries 数组里的一项,来源不可信,逐字段校验。
21
- * @returns 映射结果;缺少仓库路径或展示名时返回 null,由调用方跳过。
22
- */
23
- export declare function asShelfryPlugin(entry: unknown): RegistryPlugin | null;
24
- /**
25
- * 条目类型转分类 id。
26
- *
27
- * @param type - shelfry 的 `t` 字段,可能是任何类型。
28
- */
29
- export declare function categoryOf(type: unknown): string;
30
- /**
31
- * 从安装命令里取出 npm 包名。
32
- *
33
- * 命令形如 `dsh plugin --profile web add @scope/name@1.2.3`;版本号与 tag 都要
34
- * 去掉,带变量的地址(`github:...#${DSH_PLUGIN_REF}`)和本地绝对路径都不是
35
- * npm 包名,返回 null 让安装回落到 GitHub 源。
36
- *
37
- * @param command - shelfry 的 `cmd` 字段,可能是任何类型。
38
- * @returns 合法 npm 包名,或 null。
39
- */
40
- export declare function npmFromCommand(command: unknown): string | null;
41
- /**
42
- * 去掉安装源末尾的 `@版本` 或 `@tag`。
43
- *
44
- * 从下标 1 开始查找,避免把作用域包名开头的 `@` 当成版本分隔符。
45
- *
46
- * @param spec - 安装源,例如 `@scope/name@1.2.3`。
47
- */
48
- export declare function stripSpecVersion(spec: string): string;