krillplayer-plugin 0.5.0 → 0.6.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/README.md CHANGED
@@ -104,6 +104,7 @@ npx krillplayer-plugin skill
104
104
 
105
105
  ## 更新记录
106
106
 
107
+ - **0.6.0**:`play.start` 多一个可选参数 `selection`(用户选的线路与档位,没选过时不给),`PlayableLine` 多一个可选的 `qualities`,列出没取地址的档位与线路;每一路要单独签名的平台可以只取选中的那一路。凭证扫描对请求的 `Cookie` 头逐对看,与 `set-cookie` 同一套:每一对的值都是占位值时放行,混进一对真值或写法认不出时照报,用例可以写上带 Cookie 的请求头;`set-cookie` 里认不出的属性也按 Cookie 看,用例文件里重名的键照报。契约修订号不变。完整记录见 `CHANGELOG.md`。
107
108
  - **0.5.0**:`krillplayer.crypto.digest` 收 `md5` 与 `sm3`,新增 `krillplayer.crypto.rc4(key, data)`;MD5 与 RC4 不提供安全保证,只为与已有协议互通。`subtle` 不变。契约修订号不变。完整记录见 `CHANGELOG.md`。
108
109
  - **0.4.0**:`danmaku.send` 可以返回发送会话(WebSocket),App 替插件连、发、等,收到的帧交给新的 `danmaku.sendDecode` 判;导出它的插件能力是 `danmakuSendSession`。用例多了假服务器 `socket`。契约修订号不变。完整记录见 `CHANGELOG.md`。
109
110
  - **0.1.2**:契约去掉资料库槽位 `library`(`list`、`read`、`changes`、`ids`)与能力 `library`、`libraryChanges`,App 不再整库扫描来源。还导出 `library` 的插件打包失败,删掉这个导出即可;已经装着的旧版本照常运行,App 不调它。契约修订号不变。
package/dist/cli.js CHANGED
@@ -396,7 +396,8 @@ var PlayableStream = object("PlayableStream", {
396
396
  });
397
397
  var PlayableLine = object("PlayableLine", {
398
398
  name: string,
399
- streams: array(PlayableStream)
399
+ streams: array(PlayableStream),
400
+ qualities: optional(array(string))
400
401
  });
401
402
  var Playable = object("Playable", {
402
403
  timing: enumeration("PlayableTiming", ["live", "onDemand"]),
@@ -3980,11 +3981,21 @@ var BadJSON = class extends Error {
3980
3981
  var JSON_DEPTH = 128;
3981
3982
  var NUMBER = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y;
3982
3983
  function parseJSONBody(text) {
3984
+ return readJSON(text, true, () => {
3985
+ throw new BadJSON("duplicate key");
3986
+ });
3987
+ }
3988
+ function duplicateKeys(text) {
3989
+ const keys = [];
3990
+ readJSON(text, false, (key) => keys.push(key));
3991
+ return keys;
3992
+ }
3993
+ function readJSON(text, strict, onDuplicate) {
3983
3994
  let index = 0;
3984
3995
  let depth = 0;
3985
3996
  const enter = () => {
3986
3997
  depth += 1;
3987
- if (depth > JSON_DEPTH) throw new BadJSON(`nesting deeper than ${JSON_DEPTH}`);
3998
+ if (strict && depth > JSON_DEPTH) throw new BadJSON(`nesting deeper than ${JSON_DEPTH}`);
3988
3999
  };
3989
4000
  const space = () => {
3990
4001
  while (index < text.length && " \n\r".includes(text[index])) index += 1;
@@ -4041,7 +4052,7 @@ function parseJSONBody(text) {
4041
4052
  space();
4042
4053
  expect(":");
4043
4054
  const entry = value();
4044
- if (object2.has(key)) throw new BadJSON("duplicate key");
4055
+ if (object2.has(key)) onDuplicate(key);
4045
4056
  object2.set(key, entry);
4046
4057
  space();
4047
4058
  if (text[index] === ",") {
@@ -4087,7 +4098,7 @@ function parseJSONBody(text) {
4087
4098
  if (match === null) throw new NotJSON();
4088
4099
  index += match[0].length;
4089
4100
  const number2 = Number(match[0]);
4090
- if (!Number.isFinite(number2)) throw new BadJSON("number is not a finite double");
4101
+ if (strict && !Number.isFinite(number2)) throw new BadJSON("number is not a finite double");
4091
4102
  return number2;
4092
4103
  };
4093
4104
  const result = value();
@@ -4099,7 +4110,7 @@ function parseJSONBody(text) {
4099
4110
  if (item instanceof Map) return [...item].some(([key, entry]) => !key.isWellFormed() || loneSurrogate(entry));
4100
4111
  return false;
4101
4112
  };
4102
- if (loneSurrogate(result)) throw new BadJSON("lone surrogate in a string");
4113
+ if (strict && loneSurrogate(result)) throw new BadJSON("lone surrogate in a string");
4103
4114
  return result;
4104
4115
  }
4105
4116
  function bodyKind(bytes2) {
@@ -4176,8 +4187,35 @@ var SECRET_VALUES = [
4176
4187
  /\beyJ[\w-]{8,}\.eyJ[\w-]{8,}\.[\w-]+/,
4177
4188
  /\b(?:access_key|access_token)=(?!(?:REDACTED-0{10}|REDACTED)(?![\w.-]))[\w.-]{16,}/
4178
4189
  ];
4179
- var redactSetCookie = (lines) => lines.split("\n").map((line) => line.replace(/^([^=;]*)=[^;]*/, `$1=${REDACTED}`)).join("\n");
4180
- var isRedactedSetCookie = (lines) => lines.split("\n").every((line) => new RegExp(`^[^=;]*=${REDACTED}(;|$)`).test(line));
4190
+ var COOKIE_ATTRIBUTES = /* @__PURE__ */ new Set(["path", "domain", "expires", "max-age", "samesite", "secure", "httponly", "partitioned", "priority"]);
4191
+ function setCookieValueRanges(line) {
4192
+ const ranges = [];
4193
+ let start = 0;
4194
+ for (const [index, segment] of line.split(";").entries()) {
4195
+ const end = start + segment.length;
4196
+ const equals = segment.indexOf("=");
4197
+ const name2 = (equals < 0 ? segment : segment.slice(0, equals)).trim().toLowerCase();
4198
+ if (index === 0) {
4199
+ if (equals < 0) return void 0;
4200
+ ranges.push([start + equals + 1, end]);
4201
+ } else if (segment.trim() !== "" && !COOKIE_ATTRIBUTES.has(name2)) {
4202
+ ranges.push(equals < 0 ? [start + segment.length - segment.trimStart().length, end] : [start + equals + 1, end]);
4203
+ }
4204
+ start = end + 1;
4205
+ }
4206
+ return ranges;
4207
+ }
4208
+ var redactSetCookie = (lines) => lines.split("\n").map((line) => {
4209
+ const ranges = setCookieValueRanges(line);
4210
+ return ranges === void 0 ? line : ranges.reduceRight((text, [from, to]) => `${text.slice(0, from)}${REDACTED}${text.slice(to)}`, line);
4211
+ }).join("\n");
4212
+ var isPlaceholder = (value) => isRedacted(value) || isFakeId(value);
4213
+ var COOKIE_PAIR = /^[^=;]*=([^;]*)$/;
4214
+ function cookieValues(header, value) {
4215
+ const values = header === "set-cookie" ? value.split("\n").flatMap((line) => setCookieValueRanges(line)?.map(([from, to]) => line.slice(from, to)) ?? [void 0]) : value.split(/;[ \t]*/).map((pair) => COOKIE_PAIR.exec(pair)?.[1]);
4216
+ return values.every((entry) => entry !== void 0) ? values : void 0;
4217
+ }
4218
+ var isRedactedCookie = (header, value) => (header === "cookie" || header === "set-cookie") && cookieValues(header, value)?.every((entry) => entry === "" || isPlaceholder(entry)) === true;
4181
4219
  function redactHeaders(headers2) {
4182
4220
  return Object.fromEntries(Object.entries(headers2).flatMap(([name2, value]) => {
4183
4221
  if (name2.toLowerCase() === "set-cookie") return [[name2, redactSetCookie(value)]];
@@ -4283,8 +4321,7 @@ function findingsIn(value, path, findings) {
4283
4321
  return;
4284
4322
  }
4285
4323
  for (const [key, entry] of Object.entries(value)) {
4286
- const redacted = isRedacted(entry) || typeof entry === "string" && isFakeId(entry) || key.toLowerCase() === "set-cookie" && typeof entry === "string" && isRedactedSetCookie(entry);
4287
- if (isSecretName(key) && typeof entry === "string" && !redacted && entry !== "") {
4324
+ if (isSecretName(key) && typeof entry === "string" && entry !== "" && !isPlaceholder(entry) && !isRedactedCookie(key.toLowerCase(), entry)) {
4288
4325
  findings.push(`${path}.${key}: ${key}`);
4289
4326
  }
4290
4327
  findingsIn(entry, `${path}.${key}`, findings);
@@ -4301,7 +4338,9 @@ async function scanDirectories(directories) {
4301
4338
  for (const directory of directories) {
4302
4339
  for (const file of await jsonFiles(directory)) {
4303
4340
  files2 += 1;
4304
- for (const finding of scanValue(JSON.parse(await readFile(file, "utf8")))) findings.push(`${file} ${finding}`);
4341
+ const text = await readFile(file, "utf8");
4342
+ for (const finding of scanValue(JSON.parse(text))) findings.push(`${file} ${finding}`);
4343
+ for (const key of duplicateKeys(text)) findings.push(`${file} duplicate key ${JSON.stringify(key)}`);
4305
4344
  }
4306
4345
  }
4307
4346
  return { files: files2, findings };
package/dist/crypto.js CHANGED
@@ -385,7 +385,8 @@ var PlayableStream = object("PlayableStream", {
385
385
  });
386
386
  var PlayableLine = object("PlayableLine", {
387
387
  name: string,
388
- streams: array(PlayableStream)
388
+ streams: array(PlayableStream),
389
+ qualities: optional(array(string))
389
390
  });
390
391
  var Playable = object("Playable", {
391
392
  timing: enumeration("PlayableTiming", ["live", "onDemand"]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "krillplayer-plugin",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Write, pack, record and test KrillPlayer source plugins on the App's own QuickJS-ng engine, with an AI skill.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://krillplayer.kami.asia",
@@ -41,6 +41,6 @@
41
41
  "eslint": "9.39.5"
42
42
  },
43
43
  "optionalDependencies": {
44
- "@krillplayer/plugin-engine-darwin-arm64": "0.5.0"
44
+ "@krillplayer/plugin-engine-darwin-arm64": "0.6.0"
45
45
  }
46
46
  }
@@ -0,0 +1,38 @@
1
+ {
2
+ "slot": "play",
3
+ "call": "start",
4
+ "args": [
5
+ "session-88231",
6
+ {
7
+ "line": "备用线路",
8
+ "quality": "高清"
9
+ }
10
+ ],
11
+ "exchanges": [],
12
+ "output": {
13
+ "timing": "live",
14
+ "lines": [
15
+ {
16
+ "name": "主线路",
17
+ "streams": [],
18
+ "qualities": [
19
+ "原画",
20
+ "高清"
21
+ ]
22
+ },
23
+ {
24
+ "name": "备用线路",
25
+ "streams": [
26
+ {
27
+ "quality": "高清",
28
+ "url": "https://cdn.example/live/session-88231/backup/720p.m3u8"
29
+ }
30
+ ],
31
+ "qualities": [
32
+ "原画",
33
+ "高清"
34
+ ]
35
+ }
36
+ ]
37
+ }
38
+ }
@@ -0,0 +1,38 @@
1
+ {
2
+ "slot": "play",
3
+ "call": "start",
4
+ "args": [
5
+ "session-88231",
6
+ {
7
+ "line": "线路9",
8
+ "quality": "高清"
9
+ }
10
+ ],
11
+ "exchanges": [],
12
+ "output": {
13
+ "timing": "live",
14
+ "lines": [
15
+ {
16
+ "name": "主线路",
17
+ "streams": [
18
+ {
19
+ "quality": "原画",
20
+ "url": "https://cdn.example/live/session-88231/main/source.m3u8"
21
+ }
22
+ ],
23
+ "qualities": [
24
+ "原画",
25
+ "高清"
26
+ ]
27
+ },
28
+ {
29
+ "name": "备用线路",
30
+ "streams": [],
31
+ "qualities": [
32
+ "原画",
33
+ "高清"
34
+ ]
35
+ }
36
+ ]
37
+ }
38
+ }
@@ -13,8 +13,20 @@
13
13
  "streams": [
14
14
  {
15
15
  "quality": "原画",
16
- "url": "https://cdn.example/live/session-88231.m3u8"
16
+ "url": "https://cdn.example/live/session-88231/main/source.m3u8"
17
17
  }
18
+ ],
19
+ "qualities": [
20
+ "原画",
21
+ "高清"
22
+ ]
23
+ },
24
+ {
25
+ "name": "备用线路",
26
+ "streams": [],
27
+ "qualities": [
28
+ "原画",
29
+ "高清"
18
30
  ]
19
31
  }
20
32
  ]
@@ -1,9 +1,10 @@
1
1
  // 样例:既直播又发视频的平台。数据写死在这里,不出网;主机名都是不存在的 .example。
2
2
  // 看点:创作者是频道,带可选的 live(正在播才有)与分栏;直播条目的 id 由插件给(这里是场次号,和频道 id 没有关系),
3
3
  // 状态槽位用 liveId 报同一个 id;预告开播后变成直播、结束后变成回放(点播);普通视频归进播放列表;顶层的 resolve 认链接。
4
+ // 直播间有两条线路、各两档,起播只取用户选的那一路(start 的 selection,#857),别的列在 qualities 里。
4
5
  // 直播间有弹幕:connect 给连接规格、decode 一次解一帧;登录后能 send 发一条(#704),平台的回答按 code 换成契约的错误种类。
5
6
  import type {
6
- BrowseSlot, ChannelRef, Creator, DanmakuSlot, Entry, Live, OnDemand, Paged, PluginError, Playlist, PlaySlot, ResolveFunction, StatusSlot, Upcoming,
7
+ BrowseSlot, ChannelRef, Creator, DanmakuSlot, Entry, Live, OnDemand, Paged, Playable, PluginError, Playlist, PlaySelection, PlaySlot, ResolveFunction, StatusSlot, Upcoming,
7
8
  } from "krillplayer-plugin";
8
9
 
9
10
  const CDN = "https://cdn.example";
@@ -112,9 +113,26 @@ export const status = {
112
113
  },
113
114
  } satisfies StatusSlot;
114
115
 
116
+ // 直播间有两条线路、各两档,平台每一路地址要单独签一次名(直播平台常见的样子;这里不出网,签名就是拼地址)。
117
+ // 起播只取一路:用户选过时取 selection 那一路,没选过或平台没有那一路时取主线路的第一档;别的线路与档位写进 qualities,
118
+ // 菜单照样列,用户选中时内核带着它再调 start(PlaySlot 的说明)。这样一次起播的请求数不随线路数、档位数增长。
119
+ const LIVE_LINES = [{ name: "主线路", path: "main" }, { name: "备用线路", path: "backup" }];
120
+ const LIVE_QUALITIES = [{ name: "原画", path: "source" }, { name: "高清", path: "720p" }];
121
+
122
+ function liveLines(itemId: string, selection: PlaySelection | undefined): Playable["lines"] {
123
+ const line = LIVE_LINES.find((candidate) => candidate.name === selection?.line);
124
+ const quality = LIVE_QUALITIES.find((candidate) => candidate.name === selection?.quality);
125
+ const [signedLine, signedQuality] = line !== undefined && quality !== undefined ? [line, quality] : [LIVE_LINES[0], LIVE_QUALITIES[0]];
126
+ return LIVE_LINES.map((candidate) => ({
127
+ name: candidate.name,
128
+ streams: candidate === signedLine ? [{ quality: signedQuality.name, url: `${CDN}/live/${itemId}/${candidate.path}/${signedQuality.path}.m3u8` }] : [],
129
+ qualities: LIVE_QUALITIES.map(({ name }) => name),
130
+ }));
131
+ }
132
+
115
133
  export const play = {
116
- async start(krillplayer, itemId) {
117
- if (itemId === LIVE.id) return { timing: "live", lines: [{ name: "主线路", streams: [{ quality: "原画", url: `${CDN}/live/${itemId}.m3u8` }] }] };
134
+ async start(krillplayer, itemId, selection) {
135
+ if (itemId === LIVE.id) return { timing: "live", lines: liveLines(itemId, selection) };
118
136
  if (itemId === RADIO_ROOM.id) throw krillplayer.error("notStarted", "还没开播");
119
137
  if (itemId === PREMIERE.id) throw krillplayer.error("notStarted", "首映还没开始");
120
138
  const video = [...VIDEOS, ...REPLAYS].find((candidate) => candidate.id === itemId);
@@ -47,7 +47,7 @@
47
47
  "response": { "status": 200, "headers": { "content-type": "application/json" }, "body": { "$bytes": "eyJpdGVtcyI6W119" } } }
48
48
  ```
49
49
 
50
- - 回放按「方法 + 地址 + 正文字节」匹配,不比请求头,不要求顺序。插件发出没录过的请求(`unrecorded request`)、或录过的请求没发出(`recorded request not sent`),都判失败。
50
+ - 回放按「方法 + 地址 + 正文字节」匹配,不要求顺序。录制不录请求头;要钉住某个请求带了什么头(例如只有搜索请求带 `referer`),在 `request` 里手工写上 `"headers": { … }`,回放时发出的请求头要与它完全相同(名字不分大小写),没写的不比。带 Cookie 的请求照样能写:`"cookie": "ttwid=REDACTED; uid=9999999"` 每一对的值都是占位值时扫描放行,一对是真值就报。插件发出没录过的请求(`unrecorded request`)、或录过的请求没发出(`recorded request not sent`),都判失败。
51
51
  - 重定向逐跳录:第一跳的应答是 `302` 带 `location` 头,下一条 exchange 是跳到的地址。
52
52
  - 正文是 UTF-8 文本时直接写字符串;字节写 `{"$bytes": "<base64>"}`;很长的重复文本写 `{"$repeat": ["<文本>", <次数>]}`。
53
53
  - 应答头名小写。`set-cookie` 一条一行。
@@ -62,7 +62,7 @@ npx krillplayer-plugin record --slot <槽位> [--call <函数>] --args '<JSON
62
62
 
63
63
  真实出网跑一次,脱敏后再用录下的应答回放一次,得到 `output` 或 `error`,扫描通过才写文件。
64
64
 
65
- - 脱敏:去掉 Cookie、`authorization` 等请求与应答头;地址、JSON 正文与表单正文里名字像凭证的参数(`token`、`access_key`、`SESSDATA` 等)换成 `REDACTED`;凭证文件的每个值、清单里 `secret: true` 的实例参数值也换成 `REDACTED`;Set-Cookie 只留名字与属性。
65
+ - 脱敏:去掉 Cookie、`authorization` 等请求与应答头;地址、JSON 正文与表单正文里名字像凭证的参数(`token`、`access_key`、`SESSDATA` 等)换成 `REDACTED`;凭证文件的每个值、清单里 `secret: true` 的实例参数值也换成 `REDACTED`;Set-Cookie 只留名字与认得的属性(Path、Domain、Expires、Max-Age、SameSite、Secure、HttpOnly、Partitioned、Priority),别的属性的值也换掉。扫描对 `cookie` 与 `set-cookie` 头逐对看,每一对「名=值」的值都得是 `REDACTED` 这类占位值,`set-cookie` 里认不出的属性也按一对看,写法认不出的也报。用例文件里同一个对象重名的键也报。
66
66
  - 插件经 `throw krillplayer.error(kind, message)` 失败时照样写文件,用例里是 `error` 而不是 `output`:真实来源能触发的失败(不存在的 id、下架的条目)就这样录。
67
67
  - 不写文件的情形:返回值不合契约(报错照 [errors-and-fixes.md](errors-and-fixes.md) 改);插件抛的不是 `krillplayer.error`(例如读了 `undefined` 的属性,报错带调用栈);脱敏后还像有凭证。
68
68
  - 凭证文件放在插件目录外,录完删掉。
@@ -605,9 +605,12 @@ export interface PlayableStream {
605
605
 
606
606
  export type Container = "mp4" | "mov" | "mkv" | "ts" | "flv" | "hls" | "dash" | "webm" | "mp3" | "aac" | "flac" | "ogg";
607
607
 
608
+ // qualities 是这条线路的全部档位,按菜单里的顺序;streams 只放这次取了地址的几档。没给时档位就是 streams 里的那几档。
609
+ // 每档都要单独取地址的平台用它列出没取的档位与线路(streams 可以为空):用户选中时内核重调 play.start 并带上那一路。
608
610
  export interface PlayableLine {
609
611
  name: string;
610
612
  streams: PlayableStream[];
613
+ qualities?: string[];
611
614
  }
612
615
 
613
616
  // 时间都以秒计;expiresAt 是 Unix 时间戳。resume 是来源记下的续播位置(点播,秒),
@@ -1406,9 +1409,15 @@ export interface ResourcesSlot {
1406
1409
  find(krillplayer: KrillPlayer, work: WorkQuery): Promise<ResourceVersion[]>;
1407
1410
  }
1408
1411
 
1412
+ // 用户在菜单里选过的线路与档位(PlayableLine.name 与 PlayableStream.quality 的名字),内核按来源记住。
1413
+ export interface PlaySelection { line: string; quality: string }
1414
+
1409
1415
  // 条目、版本都用自己的 id 起播;同一个来源实例里能起播的 id 不重复。
1416
+ // selection 是用户要的线路与档位:用户选过时内核才给,没选过时不给。每次换线路、换档位、失败重开都重新调 start,
1417
+ // 地址一路一个请求的平台可以只取 selection 那一路,别的线路与档位写进 PlayableLine.qualities 列出来、不取地址。
1418
+ // 给不出那一路(没有这条线路、这档,或取不到)时照没给 selection 时那样交,内核照常换一路,不要因为它报错。
1410
1419
  export interface PlaySlot {
1411
- start(krillplayer: KrillPlayer, itemId: string): Promise<Playable>;
1420
+ start(krillplayer: KrillPlayer, itemId: string, selection?: PlaySelection): Promise<Playable>;
1412
1421
  }
1413
1422
 
1414
1423
  // 认链接:顶层导出的一个函数,不属于哪个槽位。链接属于本来源时返回它指向的东西,不属于时返回 null。