@ppp0/runtime 0.0.0-stage → 0.2.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.
@@ -0,0 +1,44 @@
1
+ import { type PppItem, type PppPage, type PppSection } from '@ppp0/core';
2
+ export interface ItemAddressOptions {
3
+ /** 分集 id(`#fragment` 形态) */
4
+ episodeId?: string;
5
+ /** 评级字母(12 个条目字母之一);缺省按 `sha256(peerId/itemId) % 2` 在未分级里确定性选 */
6
+ rate?: string;
7
+ }
8
+ /**
9
+ * 构造**条目**地址 `ppp://<条目字母>/<peerId>/<itemId>[#<episodeId>]`。
10
+ * 字母缺省按 `sha256(peerId + '/' + itemId) % 2` 在未分级条目字母(`a` / `b`)里确定性选取。
11
+ */
12
+ export declare function itemAddress(peerId: string, itemId: string, options?: ItemAddressOptions): string;
13
+ /**
14
+ * core 归一化产出的 urn 仍是裸形态(v1.0 已废除)→ 就地改写成带评级字母的条目地址。
15
+ * 幂等:同一 `peerId/itemId` 永远得到同一字母,重复改写不产生副本(§3.1 字母语义 5)。
16
+ */
17
+ export declare function letterUrn<T extends PppItem>(item: T): T;
18
+ /** 分页视图里的条目 urn 一律改写为 v1.0 形态。 */
19
+ export declare function letterPage<T extends PppItem>(page: PppPage<T>): PppPage<T>;
20
+ /** 分区视图(home)里的条目 urn 一律改写为 v1.0 形态。 */
21
+ export declare function letterSections(sections: PppSection[]): PppSection[];
22
+ /**
23
+ * 首段**评级字母 `rate`**(`a`–`x`);非 `ppp://` 或首段不是评级字母(含裸地址、`y` / `z`、
24
+ * 大写、多字母)→ `null`。评级类请用 `rateClass(rate)` 推导,`mature` 用 `matureFromRate(rate)`。
25
+ */
26
+ export declare function addressRate(url: string): string | null;
27
+ /**
28
+ * 以地址为输入的**身份键**(§3.3 步骤 5 / Kevin 2026-12-05 裁定):
29
+ * `ppp://` 地址先去评级字母再归一(`pppCacheKey` ≡ `addressIdentity` 的口径),
30
+ * 非 `ppp://` 输入(https 源地址、相对路径)原样返回。
31
+ *
32
+ * 非法地址不参与归一:原样返回,交给解析层报 `INVALID_URI` / `BAD_PARAM`。
33
+ */
34
+ export declare function addressCacheKey(url: string): string;
35
+ /**
36
+ * `ppp://` **导入深链** → 它指向的源 https 地址:
37
+ * query 形态 `ppp://<导入字母>?src=…` 取**首个** `src`(§3.8 允许 1–5 条,预览只看第一条),
38
+ * path 形态 `ppp://<导入字母>/<host>/<path>[?query]` 整体还原。
39
+ * 不是导入深链(https / 相对地址 / 条目地址 / 裸地址)→ `null`。
40
+ *
41
+ * @throws PppError `INVALID_URI` / `BAD_PARAM`(深链本身非法)
42
+ */
43
+ export declare function importTargetOf(url: string): string | null;
44
+ //# sourceMappingURL=address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,UAAU,EAChB,MAAM,YAAY,CAAC;AAepB,MAAM,WAAW,kBAAkB;IACjC,4BAA4B;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAOpG;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,CAKvD;AAED,kCAAkC;AAClC,wBAAgB,UAAU,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAG1E;AAED,wCAAwC;AACxC,wBAAgB,cAAc,CAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,CAOnE;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQtD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAOnD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKzD"}
@@ -0,0 +1,104 @@
1
+ import { buildPppUri, isPppBatchUri, isPppImportUri, parsePppBatchUri, parsePppImportUri, pppCacheKey, } from '@ppp0/core';
2
+ /**
3
+ * v1.0 地址构造与身份键(spec §3.1 / §3.3 步骤 5、Kevin 2026-12-05 裁定)。
4
+ *
5
+ * - **条目地址**:`ppp://<条目字母>/<peerId>/<itemId>[#<episodeId>][?<params>]`;
6
+ * 字母由 `pickLetter(peerId + '/' + itemId, 'item', 'unrated')` 确定性得出
7
+ * (未分级 → `a` / `b`),禁止直接拼裸地址 `ppp://<peer>/<item>`。
8
+ * - **评级字母完全不参与地址身份**:`ppp://a/<peer>/<item>` ≡ `ppp://u/<peer>/<item>`、
9
+ * `ppp://k?src=X` ≡ `ppp://l?src=X` ≡ `ppp://s?src=X` —— 内容相同就共享缓存,
10
+ * 评级只作为**元数据**随结果交给宿主(`rate` / `mature`),协议层不判门禁、不拦缓存。
11
+ */
12
+ const PPP_SCHEME_RE = /^ppp:\/\//i;
13
+ /**
14
+ * 构造**条目**地址 `ppp://<条目字母>/<peerId>/<itemId>[#<episodeId>]`。
15
+ * 字母缺省按 `sha256(peerId + '/' + itemId) % 2` 在未分级条目字母(`a` / `b`)里确定性选取。
16
+ */
17
+ export function itemAddress(peerId, itemId, options = {}) {
18
+ return buildPppUri({
19
+ peerId,
20
+ itemId,
21
+ ...(options.episodeId !== undefined ? { episodeId: options.episodeId } : {}),
22
+ ...(options.rate !== undefined ? { rate: options.rate } : {}),
23
+ });
24
+ }
25
+ /**
26
+ * core 归一化产出的 urn 仍是裸形态(v1.0 已废除)→ 就地改写成带评级字母的条目地址。
27
+ * 幂等:同一 `peerId/itemId` 永远得到同一字母,重复改写不产生副本(§3.1 字母语义 5)。
28
+ */
29
+ export function letterUrn(item) {
30
+ if (!item || typeof item.peerId !== 'string' || typeof item.id !== 'string')
31
+ return item;
32
+ const urn = itemAddress(item.peerId, item.id);
33
+ if (item.urn !== urn)
34
+ item.urn = urn;
35
+ return item;
36
+ }
37
+ /** 分页视图里的条目 urn 一律改写为 v1.0 形态。 */
38
+ export function letterPage(page) {
39
+ if (page && Array.isArray(page.items))
40
+ for (const item of page.items)
41
+ letterUrn(item);
42
+ return page;
43
+ }
44
+ /** 分区视图(home)里的条目 urn 一律改写为 v1.0 形态。 */
45
+ export function letterSections(sections) {
46
+ if (Array.isArray(sections)) {
47
+ for (const section of sections) {
48
+ if (section && Array.isArray(section.items))
49
+ for (const item of section.items)
50
+ letterUrn(item);
51
+ }
52
+ }
53
+ return sections;
54
+ }
55
+ /**
56
+ * 首段**评级字母 `rate`**(`a`–`x`);非 `ppp://` 或首段不是评级字母(含裸地址、`y` / `z`、
57
+ * 大写、多字母)→ `null`。评级类请用 `rateClass(rate)` 推导,`mature` 用 `matureFromRate(rate)`。
58
+ */
59
+ export function addressRate(url) {
60
+ const scheme = PPP_SCHEME_RE.exec(url);
61
+ if (!scheme)
62
+ return null;
63
+ const rest = url.slice(scheme[0].length);
64
+ const stop = rest.search(/[/?#]/);
65
+ const letter = stop < 0 ? rest : rest.slice(0, stop);
66
+ if (!/^[a-z]$/.test(letter))
67
+ return null;
68
+ return letter;
69
+ }
70
+ /**
71
+ * 以地址为输入的**身份键**(§3.3 步骤 5 / Kevin 2026-12-05 裁定):
72
+ * `ppp://` 地址先去评级字母再归一(`pppCacheKey` ≡ `addressIdentity` 的口径),
73
+ * 非 `ppp://` 输入(https 源地址、相对路径)原样返回。
74
+ *
75
+ * 非法地址不参与归一:原样返回,交给解析层报 `INVALID_URI` / `BAD_PARAM`。
76
+ */
77
+ export function addressCacheKey(url) {
78
+ if (!PPP_SCHEME_RE.test(url))
79
+ return url;
80
+ try {
81
+ return pppCacheKey(url);
82
+ }
83
+ catch {
84
+ return url;
85
+ }
86
+ }
87
+ /**
88
+ * `ppp://` **导入深链** → 它指向的源 https 地址:
89
+ * query 形态 `ppp://<导入字母>?src=…` 取**首个** `src`(§3.8 允许 1–5 条,预览只看第一条),
90
+ * path 形态 `ppp://<导入字母>/<host>/<path>[?query]` 整体还原。
91
+ * 不是导入深链(https / 相对地址 / 条目地址 / 裸地址)→ `null`。
92
+ *
93
+ * @throws PppError `INVALID_URI` / `BAD_PARAM`(深链本身非法)
94
+ */
95
+ export function importTargetOf(url) {
96
+ if (!PPP_SCHEME_RE.test(url))
97
+ return null;
98
+ if (isPppBatchUri(url))
99
+ return parsePppBatchUri(url).srcs[0];
100
+ if (isPppImportUri(url))
101
+ return parsePppImportUri(url).url;
102
+ return null;
103
+ }
104
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,GAIZ,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;GASG;AAEH,MAAM,aAAa,GAAG,YAAY,CAAC;AASnC;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,MAAc,EAAE,UAA8B,EAAE;IAC1F,OAAO,WAAW,CAAC;QACjB,MAAM;QACN,MAAM;QACN,GAAG,CAAC,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5E,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC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,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IACzC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IACrD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IACzC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IACzC,IAAI,CAAC;QACH,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1C,IAAI,aAAa,CAAC,GAAG,CAAC;QAAE,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7D,IAAI,cAAc,CAAC,GAAG,CAAC;QAAE,OAAO,iBAAiB,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;IAC3D,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,42 @@
1
+ export interface BreakerOptions {
2
+ /** consecutive failures that open the circuit (default 5) */
3
+ threshold?: number;
4
+ /** first cooldown (default 60s) */
5
+ cooldownMs?: number;
6
+ /** cooldown cap (default 10min) */
7
+ maxCooldownMs?: number;
8
+ now?: () => number;
9
+ }
10
+ export type BreakerState = 'closed' | 'open' | 'half-open';
11
+ /**
12
+ * 5 consecutive failures → open 60s. When the cooldown elapses one trial request is
13
+ * allowed; if it fails the cooldown doubles (max 10min), if it succeeds everything resets.
14
+ */
15
+ export declare class CircuitBreaker {
16
+ private readonly threshold;
17
+ private readonly baseCooldownMs;
18
+ private readonly maxCooldownMs;
19
+ private readonly now;
20
+ private failures;
21
+ private currentCooldownMs;
22
+ private openedAt;
23
+ private trialInFlight;
24
+ private stateValue;
25
+ constructor(options?: BreakerOptions);
26
+ get state(): BreakerState;
27
+ get consecutiveFailures(): number;
28
+ get cooldownMs(): number;
29
+ private refresh;
30
+ /** false ⇒ skip this peer without touching the network */
31
+ canRequest(): boolean;
32
+ onSuccess(): void;
33
+ onFailure(): void;
34
+ reset(): void;
35
+ snapshot(): {
36
+ state: BreakerState;
37
+ failures: number;
38
+ cooldownMs: number;
39
+ retryAt: number | null;
40
+ };
41
+ }
42
+ //# sourceMappingURL=breaker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"breaker.d.ts","sourceRoot":"","sources":["../src/breaker.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mCAAmC;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mCAAmC;IACnC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE3D;;;GAGG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,UAAU,CAA0B;gBAEhC,OAAO,GAAE,cAAmB;IAQxC,IAAI,KAAK,IAAI,YAAY,CAGxB;IAED,IAAI,mBAAmB,IAAI,MAAM,CAEhC;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,OAAO,CAAC,OAAO;IAOf,0DAA0D;IAC1D,UAAU,IAAI,OAAO;IASrB,SAAS,IAAI,IAAI;IAOjB,SAAS,IAAI,IAAI;IAiBjB,KAAK,IAAI,IAAI;IAOb,QAAQ,IAAI;QAAE,KAAK,EAAE,YAAY,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE;CASlG"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * 5 consecutive failures → open 60s. When the cooldown elapses one trial request is
3
+ * allowed; if it fails the cooldown doubles (max 10min), if it succeeds everything resets.
4
+ */
5
+ export class CircuitBreaker {
6
+ threshold;
7
+ baseCooldownMs;
8
+ maxCooldownMs;
9
+ now;
10
+ failures = 0;
11
+ currentCooldownMs;
12
+ openedAt = 0;
13
+ trialInFlight = false;
14
+ stateValue = 'closed';
15
+ constructor(options = {}) {
16
+ this.threshold = options.threshold ?? 5;
17
+ this.baseCooldownMs = options.cooldownMs ?? 60_000;
18
+ this.maxCooldownMs = options.maxCooldownMs ?? 600_000;
19
+ this.now = options.now ?? (() => Date.now());
20
+ this.currentCooldownMs = this.baseCooldownMs;
21
+ }
22
+ get state() {
23
+ this.refresh();
24
+ return this.stateValue;
25
+ }
26
+ get consecutiveFailures() {
27
+ return this.failures;
28
+ }
29
+ get cooldownMs() {
30
+ return this.currentCooldownMs;
31
+ }
32
+ refresh() {
33
+ if (this.stateValue === 'open' && this.now() >= this.openedAt + this.currentCooldownMs) {
34
+ this.stateValue = 'half-open';
35
+ this.trialInFlight = false;
36
+ }
37
+ }
38
+ /** false ⇒ skip this peer without touching the network */
39
+ canRequest() {
40
+ this.refresh();
41
+ if (this.stateValue === 'closed')
42
+ return true;
43
+ if (this.stateValue === 'open')
44
+ return false;
45
+ if (this.trialInFlight)
46
+ return false;
47
+ this.trialInFlight = true;
48
+ return true;
49
+ }
50
+ onSuccess() {
51
+ this.failures = 0;
52
+ this.stateValue = 'closed';
53
+ this.trialInFlight = false;
54
+ this.currentCooldownMs = this.baseCooldownMs;
55
+ }
56
+ onFailure() {
57
+ this.failures++;
58
+ if (this.stateValue === 'half-open') {
59
+ this.currentCooldownMs = Math.min(this.currentCooldownMs * 2, this.maxCooldownMs);
60
+ this.stateValue = 'open';
61
+ this.openedAt = this.now();
62
+ this.trialInFlight = false;
63
+ return;
64
+ }
65
+ if (this.stateValue === 'closed' && this.failures >= this.threshold) {
66
+ this.stateValue = 'open';
67
+ this.openedAt = this.now();
68
+ this.currentCooldownMs = this.baseCooldownMs;
69
+ this.trialInFlight = false;
70
+ }
71
+ }
72
+ reset() {
73
+ this.failures = 0;
74
+ this.stateValue = 'closed';
75
+ this.trialInFlight = false;
76
+ this.currentCooldownMs = this.baseCooldownMs;
77
+ }
78
+ snapshot() {
79
+ this.refresh();
80
+ return {
81
+ state: this.stateValue,
82
+ failures: this.failures,
83
+ cooldownMs: this.currentCooldownMs,
84
+ retryAt: this.stateValue === 'open' ? this.openedAt + this.currentCooldownMs : null,
85
+ };
86
+ }
87
+ }
88
+ //# sourceMappingURL=breaker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"breaker.js","sourceRoot":"","sources":["../src/breaker.ts"],"names":[],"mappings":"AAYA;;;GAGG;AACH,MAAM,OAAO,cAAc;IACR,SAAS,CAAS;IAClB,cAAc,CAAS;IACvB,aAAa,CAAS;IACtB,GAAG,CAAe;IAC3B,QAAQ,GAAG,CAAC,CAAC;IACb,iBAAiB,CAAS;IAC1B,QAAQ,GAA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Q;QACN,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,UAAU;YACtB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,UAAU,EAAE,IAAI,CAAC,iBAAiB;YAClC,OAAO,EAAE,IAAI,CAAC,UAAU,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,IAAI;SACpF,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,29 @@
1
+ export interface CacheEntry<T> {
2
+ value: T;
3
+ expiresAt: number;
4
+ }
5
+ /**
6
+ * Runtime only caches normalized views, and never longer than the source declared.
7
+ * Media/playback results are intentionally NOT stored here (URLs expire).
8
+ */
9
+ export declare class ViewCache {
10
+ private readonly now;
11
+ private readonly entries;
12
+ constructor(now?: () => number);
13
+ get<T>(key: string): T | undefined;
14
+ set<T>(key: string, value: T, ttlSec: number): void;
15
+ delete(key: string): void;
16
+ clear(): void;
17
+ get size(): number;
18
+ }
19
+ /**
20
+ * Runtime 默认的归一化视图 TTL(秒),按能力分档(PPP spec §6.4)。
21
+ * 实例级 `cacheTtlSec` 一旦显式传入,就覆盖这里所有档位。
22
+ */
23
+ export declare const DEFAULT_VIEW_TTL_SEC: Record<string, number>;
24
+ /**
25
+ * TTL = min(runtimeDefault[capability], peer 自声明 viewTtlSec)。
26
+ * 自声明值优先于默认值压短;声明为 0 表示该能力不缓存。
27
+ */
28
+ export declare function effectiveTtl(capability: string, runtimeTtlSec?: number, declaredTtlSec?: number): number;
29
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,KAAK,EAAE,CAAC,CAAC;IACT,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,qBAAa,SAAS;IAGR,OAAO,CAAC,QAAQ,CAAC,GAAG;IAFhC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0C;gBAErC,GAAG,GAAE,MAAM,MAAyB;IAEjE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAUlC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAKnD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAQvD,CAAC;AAEF;;;GAGG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,CAMxG"}
package/dist/cache.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Runtime only caches normalized views, and never longer than the source declared.
3
+ * Media/playback results are intentionally NOT stored here (URLs expire).
4
+ */
5
+ export class ViewCache {
6
+ now;
7
+ entries = new Map();
8
+ constructor(now = () => Date.now()) {
9
+ this.now = now;
10
+ }
11
+ get(key) {
12
+ const entry = this.entries.get(key);
13
+ if (!entry)
14
+ return undefined;
15
+ if (entry.expiresAt <= this.now()) {
16
+ this.entries.delete(key);
17
+ return undefined;
18
+ }
19
+ return entry.value;
20
+ }
21
+ set(key, value, ttlSec) {
22
+ if (!(ttlSec > 0))
23
+ return;
24
+ this.entries.set(key, { value, expiresAt: this.now() + ttlSec * 1000 });
25
+ }
26
+ delete(key) {
27
+ this.entries.delete(key);
28
+ }
29
+ clear() {
30
+ this.entries.clear();
31
+ }
32
+ get size() {
33
+ return this.entries.size;
34
+ }
35
+ }
36
+ /**
37
+ * Runtime 默认的归一化视图 TTL(秒),按能力分档(PPP spec §6.4)。
38
+ * 实例级 `cacheTtlSec` 一旦显式传入,就覆盖这里所有档位。
39
+ */
40
+ export const DEFAULT_VIEW_TTL_SEC = {
41
+ home: 60,
42
+ feed: 60,
43
+ catalog: 60,
44
+ categories: 600,
45
+ detail: 300,
46
+ comments: 60,
47
+ search: 60,
48
+ };
49
+ /**
50
+ * TTL = min(runtimeDefault[capability], peer 自声明 viewTtlSec)。
51
+ * 自声明值优先于默认值压短;声明为 0 表示该能力不缓存。
52
+ */
53
+ export function effectiveTtl(capability, runtimeTtlSec, declaredTtlSec) {
54
+ const runtime = runtimeTtlSec ?? DEFAULT_VIEW_TTL_SEC[capability] ?? 60;
55
+ if (typeof declaredTtlSec === 'number' && Number.isFinite(declaredTtlSec) && declaredTtlSec >= 0) {
56
+ return Math.min(runtime, declaredTtlSec);
57
+ }
58
+ return runtime;
59
+ }
60
+ //# sourceMappingURL=cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAAM,OAAO,SAAS;IAGS;IAFZ,OAAO,GAAG,IAAI,GAAG,EAA+B,CAAC;IAElE,YAA6B,MAAoB,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;QAApC,QAAG,GAAH,GAAG,CAAiC;IAAG,CAAC;IAErE,GAAG,CAAI,GAAW;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,CAAC,KAAK;YAAE,OAAO,SAAS,CAAC;QAC7B,IAAI,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;YAClC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,KAAK,CAAC,KAAU,CAAC;IAC1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'./breaker.js';
5
+ export * from './cache.js';
6
+ export * from './static.js';
7
+ export * from './runtime.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from '@ppp0/core';
2
+ export * from './address.js';
3
+ export * from './manifest.js';
4
+ export * from './breaker.js';
5
+ export * from './cache.js';
6
+ export * from './static.js';
7
+ export * from './runtime.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,59 @@
1
+ import { type Capability, type Protocol, type RiskLevel } from '@ppp0/core';
2
+ export interface ManifestPlugin {
3
+ id: string;
4
+ name: string;
5
+ version: string;
6
+ /** true when the manifest declared `version` (needed for the meta.version gate) */
7
+ versionExplicit: boolean;
8
+ /** shipped with the player (spec §7.1) — user sources default to disabled */
9
+ builtin: boolean;
10
+ description?: string;
11
+ /** absolute URL, resolved against the manifest's FINAL url */
12
+ url: string;
13
+ protocol: Protocol;
14
+ contentType?: string;
15
+ riskLevel: RiskLevel;
16
+ priority: number;
17
+ enabled: boolean;
18
+ timeout?: Partial<Record<Capability, number>>;
19
+ cacheTtlSec?: number;
20
+ }
21
+ export interface ParsedManifest {
22
+ name: string;
23
+ description?: string;
24
+ pppVersion?: string;
25
+ /** final URL after redirects — relative plugin URLs resolve against this */
26
+ url: string;
27
+ plugins: ManifestPlugin[];
28
+ /** non-fatal diagnostics (unknown protocol entries skipped, legacy fields, ...) */
29
+ warnings: string[];
30
+ }
31
+ export declare function resolveUrl(value: string, baseUrl: string): string;
32
+ /**
33
+ * PPP manifest = HPP manifest superset. Missing `protocol` means 'hpp' so existing
34
+ * HPP manifests import unchanged.
35
+ *
36
+ * Version gate: a present `pppVersion` must have major version 0 — a future protocol
37
+ * (1.x) must be refused instead of silently mis-parsed.
38
+ */
39
+ export declare function parseManifest(input: unknown, manifestUrl: string): ParsedManifest;
40
+ /** L0 static doc: `{ ppp, peer, items[], next }` (or HPP-shaped `items` + peer id). */
41
+ export interface StaticPeerInfo {
42
+ id: string;
43
+ name: string;
44
+ contentType?: string;
45
+ riskLevel?: RiskLevel;
46
+ mature?: boolean;
47
+ }
48
+ export interface StaticDocLike {
49
+ ppp?: string;
50
+ peer: StaticPeerInfo;
51
+ items: unknown[];
52
+ next?: string | null;
53
+ categories?: unknown[];
54
+ }
55
+ export declare function isStaticDoc(input: unknown): input is StaticDocLike;
56
+ export declare function parseStaticDoc(input: unknown, docUrl: string): StaticDocLike & {
57
+ url: string;
58
+ };
59
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,UAAU,EAAE,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAEpG,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,mFAAmF;IACnF,eAAe,EAAE,OAAO,CAAC;IACzB,6EAA6E;IAC7E,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,CAAC,EAA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ACxB;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAKlE;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAkB9F"}
@@ -0,0 +1,137 @@
1
+ import { PppError, riskLevelFor } from '@ppp0/core';
2
+ export function resolveUrl(value, baseUrl) {
3
+ try {
4
+ return new URL(value, baseUrl).toString();
5
+ }
6
+ catch {
7
+ if (!baseUrl.endsWith('/') && !value.startsWith('/'))
8
+ return `${baseUrl}/${value}`;
9
+ return baseUrl + value.replace(/^\/+/, '');
10
+ }
11
+ }
12
+ function asString(value) {
13
+ return typeof value === 'string' && value.trim() ? value.trim() : undefined;
14
+ }
15
+ /** Protocols a manifest entry may declare. Anything else is refused (never coerced). */
16
+ const SUPPORTED_PROTOCOLS = ['hpp', 'ppp', 'ppp-static'];
17
+ /**
18
+ * PPP manifest = HPP manifest superset. Missing `protocol` means 'hpp' so existing
19
+ * HPP manifests import unchanged.
20
+ *
21
+ * Version gate: a present `pppVersion` must have major version 0 — a future protocol
22
+ * (1.x) must be refused instead of silently mis-parsed.
23
+ */
24
+ export function parseManifest(input, manifestUrl) {
25
+ if (!input || typeof input !== 'object' || Array.isArray(input)) {
26
+ throw new PppError('INVALID_ARGUMENT', '清单不是一个 JSON 对象');
27
+ }
28
+ const raw = input;
29
+ const pluginsRaw = raw.plugins;
30
+ if (!Array.isArray(pluginsRaw) || pluginsRaw.length === 0) {
31
+ throw new PppError('INVALID_ARGUMENT', '清单缺少 plugins 数组');
32
+ }
33
+ const warnings = [];
34
+ const pppVersion = asString(raw.pppVersion);
35
+ if (pppVersion) {
36
+ const major = Number.parseInt(pppVersion.split('.')[0] ?? '', 10);
37
+ if (!Number.isFinite(major) || major !== 0) {
38
+ throw new PppError('UNSUPPORTED', `清单 pppVersion=${pppVersion} 的主版本不受支持(本版仅实现 0.x,拒绝整份清单)`);
39
+ }
40
+ }
41
+ const plugins = [];
42
+ const seen = new Set();
43
+ for (const [index, entry] of pluginsRaw.entries()) {
44
+ if (!entry || typeof entry !== 'object')
45
+ continue;
46
+ const plugin = entry;
47
+ const id = asString(plugin.id);
48
+ const url = asString(plugin.url);
49
+ if (!id || !url)
50
+ throw new PppError('INVALID_ARGUMENT', `plugins[${index}] 缺少 id 或 url`);
51
+ if (seen.has(id))
52
+ throw new PppError('INVALID_ARGUMENT', `清单中重复的插件 id:${id}`);
53
+ const protocolRaw = asString(plugin.protocol);
54
+ if (protocolRaw !== undefined && !SUPPORTED_PROTOCOLS.includes(protocolRaw)) {
55
+ // Refuse the entry — never coerce an unknown protocol into 'hpp'.
56
+ warnings.push(`plugins[${index}].protocol=${protocolRaw} 不受支持,已跳过该条`);
57
+ continue;
58
+ }
59
+ const protocol = protocolRaw ?? 'hpp';
60
+ seen.add(id);
61
+ const mature = plugin.mature === true;
62
+ const entryRisk = plugin.riskLevel;
63
+ const riskLevel = entryRisk === 'forbidden'
64
+ ? 'forbidden'
65
+ : entryRisk === 'gray' || mature
66
+ ? 'gray'
67
+ : 'safe';
68
+ const priorityRaw = Number(plugin.priority);
69
+ const versionDeclared = asString(plugin.version);
70
+ const builtin = plugin.builtin === true || raw.builtin === true;
71
+ const pluginEntry = {
72
+ id,
73
+ name: asString(plugin.name) ?? id,
74
+ version: versionDeclared ?? '0.0.0',
75
+ versionExplicit: versionDeclared !== undefined,
76
+ builtin,
77
+ url: resolveUrl(url, manifestUrl),
78
+ protocol,
79
+ riskLevel,
80
+ priority: Number.isFinite(priorityRaw) ? priorityRaw : 50,
81
+ enabled: plugin.enabled !== false,
82
+ };
83
+ const description = asString(plugin.description);
84
+ if (description)
85
+ pluginEntry.description = description;
86
+ const contentType = asString(plugin.contentType);
87
+ if (contentType)
88
+ pluginEntry.contentType = contentType;
89
+ if (plugin.timeout && typeof plugin.timeout === 'object') {
90
+ pluginEntry.timeout = plugin.timeout;
91
+ }
92
+ // PPP extension: `viewTtlSec` is the adopted name; `cacheTtlSec` stays as a legacy alias.
93
+ const ttl = Number(plugin.viewTtlSec ?? plugin.cacheTtlSec);
94
+ if (Number.isFinite(ttl) && ttl >= 0)
95
+ pluginEntry.cacheTtlSec = ttl;
96
+ plugins.push(pluginEntry);
97
+ }
98
+ if (!plugins.length)
99
+ throw new PppError('INVALID_ARGUMENT', '清单里没有可用的插件条目');
100
+ const manifest = { name: asString(raw.name) ?? '未命名源', url: manifestUrl, plugins, warnings };
101
+ const description = asString(raw.description);
102
+ if (description)
103
+ manifest.description = description;
104
+ if (pppVersion)
105
+ manifest.pppVersion = pppVersion;
106
+ return manifest;
107
+ }
108
+ export function isStaticDoc(input) {
109
+ if (!input || typeof input !== 'object' || Array.isArray(input))
110
+ return false;
111
+ const raw = input;
112
+ const peer = raw.peer;
113
+ return Array.isArray(raw.items) && !!peer && typeof peer.id === 'string' && !('plugins' in raw);
114
+ }
115
+ export function parseStaticDoc(input, docUrl) {
116
+ if (!isStaticDoc(input))
117
+ throw new PppError('INVALID_ARGUMENT', '不是 ppp-static JSON(缺少 items/peer.id)');
118
+ const peer = input.peer;
119
+ const doc = {
120
+ ...input,
121
+ peer: {
122
+ id: peer.id,
123
+ name: typeof peer.name === 'string' && peer.name.trim() ? peer.name : peer.id,
124
+ riskLevel: riskLevelFor({ mature: peer.mature === true, riskLevel: peer.riskLevel }),
125
+ },
126
+ url: docUrl,
127
+ };
128
+ if (typeof peer.contentType === 'string')
129
+ doc.peer.contentType = peer.contentType;
130
+ // v1.0.5 §4.2:`peer.mature` 是 L0 评级的**真相源**,`riskLevel` 只是派生值——两者并存。
131
+ // 三态保留:只回填真实的布尔值;文档没写 ⇒ 字段缺省(**不得**默认成 `false`,那会把
132
+ // 「未声明」伪造成「已声明非成人」)。
133
+ if (typeof peer.mature === 'boolean')
134
+ doc.peer.mature = peer.mature;
135
+ return doc;
136
+ }
137
+ //# sourceMappingURL=manifest.js.map
@@ -0,0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