dsh-shiori 0.2.0 → 0.3.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.
@@ -1,136 +1,146 @@
1
- // Shiori P0-CORE · 作用域可见性与 Topic 元数据校验。
2
- // 公共契约导出 selectVisibleTopics;validateTopic/validateScopeId/compareKeys
3
- // 为 render 与本模块共用的内部助手(不进公共入口)。
4
- //
5
- // 可见性规则(CONTRACTS §A / DESIGN §4.4、§10):
6
- // - scopeId === activeScopeId 或 scopeId === "global" 的专题可见;
7
- // - 同 key 时 active 覆盖 global;
8
- // - 其他 scope 一律不可见;
9
- // - 相同 key 可在不同 scope 并存,但同一 scope 内重复 key 是数据错误,拒绝;
10
- // - 输出按 key 的 UTF-16 码元序稳定排序,绝不依赖系统 locale。
11
- //
12
- // Topic 形状:{key,title,summary,scopeId,revision,updated,body?}。
13
- // body 之外的字段全部必填;summary ≤60 UTF-16 且不得含换行(会破坏索引行结构);
14
- // 允许未知字段以保持数据前向兼容(Topic 是数据记录,不是控制接口)。
15
-
16
- import { fail, describe, requireObject, requireExactKeys } from "./errors.mjs";
17
- import { validateKey } from "./keys.mjs";
18
-
19
- // DESIGN §10 目录布局 scopes/global/ 即共享作用域,因此字面量 "global"
20
- // 标记全局专题;B/C 必须使用同一字面量(见 REPORT.md 集成约定)。
21
- export const GLOBAL_SCOPE_ID = "global";
22
-
23
- export const SUMMARY_MAX_CHARS = 60;
24
- const MAX_SCOPE_ID_CHARS = 128;
25
-
26
- export function validateScopeId(value, name) {
27
- if (typeof value !== "string" || value.length === 0) {
28
- fail("INVALID_INPUT", `${name} must be a non-empty string, got ${describe(value)}`);
29
- }
30
- if (value.length > MAX_SCOPE_ID_CHARS) {
31
- fail("INVALID_INPUT", `${name} must be at most ${MAX_SCOPE_ID_CHARS} UTF-16 chars, got ${value.length}`);
32
- }
33
- if (value !== value.trim()) {
34
- fail("INVALID_INPUT", `${name} must not start or end with whitespace`);
35
- }
36
- for (const ch of value) {
37
- const cp = ch.codePointAt(0);
38
- if (cp >= 0xd800 && cp <= 0xdfff) {
39
- fail("INVALID_INPUT", `${name} must not contain lone surrogate code units`);
40
- }
41
- if (cp <= 0x1f || cp === 0x7f || (cp >= 0x80 && cp <= 0x9f)) {
42
- fail("INVALID_INPUT", `${name} must not contain control characters`);
43
- }
44
- }
45
- return value;
46
- }
47
-
48
- // locale 无关的 key 比较:直接按 UTF-16 码元比较,结果跨平台逐字一致。
49
- export function compareKeys(a, b) {
50
- return a < b ? -1 : a > b ? 1 : 0;
51
- }
52
-
53
- export function validateTopic(topic, where) {
54
- requireObject(topic, where);
55
- const required = ["key", "title", "summary", "scopeId", "revision", "updated"];
56
- // 必填字段必须是自有属性:仅存在于原型链上的字段不算数
57
- for (const field of required) {
58
- if (!Object.prototype.hasOwnProperty.call(topic, field)) {
59
- fail("INVALID_INPUT", `${where} is missing required property "${field}"`);
60
- }
61
- }
62
- try {
63
- validateKey(topic.key);
64
- } catch (error) {
65
- fail("INVALID_INPUT", `${where}: ${error.message}`);
66
- }
67
- if (typeof topic.title !== "string" || topic.title.length === 0) {
68
- fail("INVALID_INPUT", `${where}: title must be a non-empty string`);
69
- }
70
- if (typeof topic.summary !== "string") {
71
- fail("INVALID_INPUT", `${where}: summary must be a string`);
72
- }
73
- if (topic.summary.length > SUMMARY_MAX_CHARS) {
74
- fail(
75
- "INVALID_INPUT",
76
- `${where}: summary is ${topic.summary.length} UTF-16 chars, max is ${SUMMARY_MAX_CHARS}`,
77
- );
78
- }
79
- if (topic.summary.includes("\n") || topic.summary.includes("\r")) {
80
- fail("INVALID_INPUT", `${where}: summary must not contain line breaks`);
81
- }
82
- validateScopeId(topic.scopeId, `${where}: scopeId`);
83
- if (typeof topic.revision !== "string" || topic.revision.length === 0) {
84
- fail("INVALID_INPUT", `${where}: revision must be a non-empty string`);
85
- }
86
- const updated = topic.updated;
87
- const updatedOk =
88
- (typeof updated === "string" && updated.length > 0) ||
89
- (typeof updated === "number" && Number.isFinite(updated)) ||
90
- (updated instanceof Date && Number.isFinite(updated.getTime()));
91
- if (!updatedOk) {
92
- fail("INVALID_INPUT", `${where}: updated must be a non-empty string, a finite number, or a valid Date`);
93
- }
94
- if (topic.body !== undefined && typeof topic.body !== "string") {
95
- fail("INVALID_INPUT", `${where}: body must be a string when present`);
96
- }
97
- return topic;
98
- }
99
-
100
- export function selectVisibleTopics(input) {
101
- requireObject(input, "selectVisibleTopics input");
102
- requireExactKeys(input, "selectVisibleTopics input", ["activeScopeId", "topics"]);
103
- validateScopeId(input.activeScopeId, "activeScopeId");
104
- if (!Array.isArray(input.topics)) {
105
- fail("INVALID_INPUT", `topics must be an array, got ${describe(input.topics)}`);
106
- }
107
-
108
- // 全部输入(含不可见 scope)都做完整校验:坏数据即刻显式失败,
109
- // 不静默跳过。同一 scope 内重复 key 是存储层错误。
110
- // 用索引循环而非 forEach:稀疏数组的孔(undefined)必须进入校验,
111
- // 不允许被跳孔逻辑静默当成无 topic。
112
- const seenScopeKeys = new Set();
113
- const activeByKey = new Map();
114
- const globalByKey = new Map();
115
- for (let index = 0; index < input.topics.length; index++) {
116
- const topic = validateTopic(input.topics[index], `topics[${index}]`);
117
- const scopeKey = `${topic.scopeId}\u0000${topic.key}`;
118
- if (seenScopeKeys.has(scopeKey)) {
119
- fail(
120
- "INVALID_INPUT",
121
- `duplicate topic key "${topic.key}" within scope "${topic.scopeId}" (topics[${index}])`,
122
- );
123
- }
124
- seenScopeKeys.add(scopeKey);
125
- if (topic.scopeId === input.activeScopeId) {
126
- activeByKey.set(topic.key, topic);
127
- } else if (topic.scopeId === GLOBAL_SCOPE_ID) {
128
- globalByKey.set(topic.key, topic);
129
- }
130
- }
131
-
132
- // 同 key:active 覆盖 global(activeScopeId 为 "global" 时自然退化为仅全局)。
133
- const visibleByKey = new Map(globalByKey);
134
- for (const [key, topic] of activeByKey) visibleByKey.set(key, topic);
135
- return [...visibleByKey.values()].sort((a, b) => compareKeys(a.key, b.key));
136
- }
1
+ // Shiori P0-CORE · 作用域可见性与 Topic 元数据校验。
2
+ // 公共契约导出 selectVisibleTopics;validateTopic/validateScopeId/compareKeys
3
+ // 为 render 与本模块共用的内部助手(不进公共入口)。
4
+ //
5
+ // 可见性规则(CONTRACTS §A / DESIGN §4.4、§10):
6
+ // - scopeId === activeScopeId 或 scopeId === "global" 的专题可见;
7
+ // - 同 key 时 active 覆盖 global;
8
+ // - 其他 scope 一律不可见;
9
+ // - 相同 key 可在不同 scope 并存,但同一 scope 内重复 key 是数据错误,拒绝;
10
+ // - 输出按 key 的 UTF-16 码元序稳定排序,绝不依赖系统 locale。
11
+ //
12
+ // Topic 形状:{key,title,summary,scopeId,revision,updated,body?}。
13
+ // body 之外的字段全部必填;summary ≤60 UTF-16 且不得含换行(会破坏索引行结构);
14
+ // 允许未知字段以保持数据前向兼容(Topic 是数据记录,不是控制接口)。
15
+
16
+ import { fail, describe, requireObject, requireExactKeys } from "./errors.mjs";
17
+ import { validateKey } from "./keys.mjs";
18
+
19
+ // DESIGN §10 目录布局 scopes/global/ 即共享作用域,因此字面量 "global"
20
+ // 标记全局专题;B/C 必须使用同一字面量(见 REPORT.md 集成约定)。
21
+ export const GLOBAL_SCOPE_ID = "global";
22
+
23
+ export const SUMMARY_MAX_CHARS = 60;
24
+ const MAX_SCOPE_ID_CHARS = 128;
25
+
26
+ export function validateScopeId(value, name) {
27
+ if (typeof value !== "string" || value.length === 0) {
28
+ fail("INVALID_INPUT", `${name} must be a non-empty string, got ${describe(value)}`);
29
+ }
30
+ if (value.length > MAX_SCOPE_ID_CHARS) {
31
+ fail("INVALID_INPUT", `${name} must be at most ${MAX_SCOPE_ID_CHARS} UTF-16 chars, got ${value.length}`);
32
+ }
33
+ if (value !== value.trim()) {
34
+ fail("INVALID_INPUT", `${name} must not start or end with whitespace`);
35
+ }
36
+ for (const ch of value) {
37
+ const cp = ch.codePointAt(0);
38
+ if (cp >= 0xd800 && cp <= 0xdfff) {
39
+ fail("INVALID_INPUT", `${name} must not contain lone surrogate code units`);
40
+ }
41
+ if (cp <= 0x1f || cp === 0x7f || (cp >= 0x80 && cp <= 0x9f)) {
42
+ fail("INVALID_INPUT", `${name} must not contain control characters`);
43
+ }
44
+ }
45
+ return value;
46
+ }
47
+
48
+ // locale 无关的 key 比较:直接按 UTF-16 码元比较,结果跨平台逐字一致。
49
+ export function compareKeys(a, b) {
50
+ return a < b ? -1 : a > b ? 1 : 0;
51
+ }
52
+
53
+ export function validateTopic(topic, where) {
54
+ requireObject(topic, where);
55
+ const required = ["key", "title", "summary", "scopeId", "revision", "updated"];
56
+ // 必填字段必须是自有属性:仅存在于原型链上的字段不算数
57
+ for (const field of required) {
58
+ if (!Object.prototype.hasOwnProperty.call(topic, field)) {
59
+ fail("INVALID_INPUT", `${where} is missing required property "${field}"`);
60
+ }
61
+ }
62
+ try {
63
+ validateKey(topic.key);
64
+ } catch (error) {
65
+ fail("INVALID_INPUT", `${where}: ${error.message}`);
66
+ }
67
+ if (typeof topic.title !== "string" || topic.title.length === 0) {
68
+ fail("INVALID_INPUT", `${where}: title must be a non-empty string`);
69
+ }
70
+ if (typeof topic.summary !== "string") {
71
+ fail("INVALID_INPUT", `${where}: summary must be a string`);
72
+ }
73
+ if (topic.summary.length > SUMMARY_MAX_CHARS) {
74
+ fail(
75
+ "INVALID_INPUT",
76
+ `${where}: summary is ${topic.summary.length} UTF-16 chars, max is ${SUMMARY_MAX_CHARS}`,
77
+ );
78
+ }
79
+ if (topic.summary.includes("\n") || topic.summary.includes("\r")) {
80
+ fail("INVALID_INPUT", `${where}: summary must not contain line breaks`);
81
+ }
82
+ validateScopeId(topic.scopeId, `${where}: scopeId`);
83
+ if (typeof topic.revision !== "string" || topic.revision.length === 0) {
84
+ fail("INVALID_INPUT", `${where}: revision must be a non-empty string`);
85
+ }
86
+ const updated = topic.updated;
87
+ const updatedOk =
88
+ (typeof updated === "string" && updated.length > 0) ||
89
+ (typeof updated === "number" && Number.isFinite(updated)) ||
90
+ (updated instanceof Date && Number.isFinite(updated.getTime()));
91
+ if (!updatedOk) {
92
+ fail("INVALID_INPUT", `${where}: updated must be a non-empty string, a finite number, or a valid Date`);
93
+ }
94
+ if (topic.body !== undefined && typeof topic.body !== "string") {
95
+ fail("INVALID_INPUT", `${where}: body must be a string when present`);
96
+ }
97
+ // 归档标记(可选):有值即归档(ISO 时间串),缺省/空 = 未归档;
98
+ // 老文件没有该字段,行为不变(DESIGN §2.1 已定方案)。
99
+ if (topic.archivedAt !== undefined) {
100
+ if (typeof topic.archivedAt !== "string" || topic.archivedAt.length === 0) {
101
+ fail("INVALID_INPUT", `${where}: archivedAt must be a non-empty string when present`);
102
+ }
103
+ if (topic.archivedAt.includes("\n") || topic.archivedAt.includes("\r")) {
104
+ fail("INVALID_INPUT", `${where}: archivedAt must not contain line breaks`);
105
+ }
106
+ }
107
+ return topic;
108
+ }
109
+
110
+ export function selectVisibleTopics(input) {
111
+ requireObject(input, "selectVisibleTopics input");
112
+ requireExactKeys(input, "selectVisibleTopics input", ["activeScopeId", "topics"]);
113
+ validateScopeId(input.activeScopeId, "activeScopeId");
114
+ if (!Array.isArray(input.topics)) {
115
+ fail("INVALID_INPUT", `topics must be an array, got ${describe(input.topics)}`);
116
+ }
117
+
118
+ // 全部输入(含不可见 scope)都做完整校验:坏数据即刻显式失败,
119
+ // 不静默跳过。同一 scope 内重复 key 是存储层错误。
120
+ // 用索引循环而非 forEach:稀疏数组的孔(undefined)必须进入校验,
121
+ // 不允许被跳孔逻辑静默当成无 topic。
122
+ const seenScopeKeys = new Set();
123
+ const activeByKey = new Map();
124
+ const globalByKey = new Map();
125
+ for (let index = 0; index < input.topics.length; index++) {
126
+ const topic = validateTopic(input.topics[index], `topics[${index}]`);
127
+ const scopeKey = `${topic.scopeId}\u0000${topic.key}`;
128
+ if (seenScopeKeys.has(scopeKey)) {
129
+ fail(
130
+ "INVALID_INPUT",
131
+ `duplicate topic key "${topic.key}" within scope "${topic.scopeId}" (topics[${index}])`,
132
+ );
133
+ }
134
+ seenScopeKeys.add(scopeKey);
135
+ if (topic.scopeId === input.activeScopeId) {
136
+ activeByKey.set(topic.key, topic);
137
+ } else if (topic.scopeId === GLOBAL_SCOPE_ID) {
138
+ globalByKey.set(topic.key, topic);
139
+ }
140
+ }
141
+
142
+ // 同 key:active 覆盖 global(activeScopeId 为 "global" 时自然退化为仅全局)。
143
+ const visibleByKey = new Map(globalByKey);
144
+ for (const [key, topic] of activeByKey) visibleByKey.set(key, topic);
145
+ return [...visibleByKey.values()].sort((a, b) => compareKeys(a.key, b.key));
146
+ }