krillplayer-plugin 0.3.0 → 0.5.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 +2 -0
- package/dist/cli.js +577 -84
- package/dist/crypto.js +155 -5
- package/kernel/Sources/Kernel/Network/PublicSuffixList.dat +16978 -0
- package/kernel/Sources/Kernel/PluginPrelude.js +1 -0
- package/package.json +2 -2
- package/skill/krillplayer-plugin/SKILL.md +1 -0
- package/skill/krillplayer-plugin/references/cases.md +21 -0
- package/skill/krillplayer-plugin/references/contract.d.ts +51 -5
package/dist/cli.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// package/cli.ts
|
|
2
2
|
import { execFile as execFile2 } from "node:child_process";
|
|
3
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { existsSync, readFileSync as readFileSync2 } from "node:fs";
|
|
4
4
|
import { cp, mkdir as mkdir3, mkdtemp as mkdtemp2, readdir as readdir5, readFile as readFile6, rm as rm3, writeFile as writeFile3 } from "node:fs/promises";
|
|
5
5
|
import { createRequire as createRequire2 } from "node:module";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
|
-
import { basename as basename2, dirname as dirname2, join as
|
|
8
|
-
import { fileURLToPath as
|
|
7
|
+
import { basename as basename2, dirname as dirname2, join as join6, relative as relative3, resolve as resolve3 } from "node:path";
|
|
8
|
+
import { fileURLToPath as fileURLToPath4 } from "node:url";
|
|
9
9
|
import { parseArgs, promisify as promisify2 } from "node:util";
|
|
10
10
|
|
|
11
11
|
// contract/schema.ts
|
|
@@ -80,7 +80,8 @@ var Capability = enumeration("Capability", [
|
|
|
80
80
|
"danmakuSend",
|
|
81
81
|
"browseLists",
|
|
82
82
|
"playerPanels",
|
|
83
|
-
"cardActions"
|
|
83
|
+
"cardActions",
|
|
84
|
+
"danmakuSendSession"
|
|
84
85
|
]);
|
|
85
86
|
var Media = enumeration("Media", ["video", "audio"]);
|
|
86
87
|
var ChannelKind = enumeration("ChannelKind", ["creator", "station", "podcast"]);
|
|
@@ -484,6 +485,24 @@ var LiveDanmakuConnection = object("LiveDanmakuConnection", {
|
|
|
484
485
|
heartbeat: optional(object("DanmakuHeartbeat", { frame: DanmakuFrame, interval: number })),
|
|
485
486
|
recent: optional(array(DanmakuComment))
|
|
486
487
|
});
|
|
488
|
+
var DANMAKU_SEND_LIMITS = { waitSeconds: 10, sessionSeconds: 20 };
|
|
489
|
+
var DanmakuSendWait = object("DanmakuSendWait", {
|
|
490
|
+
seconds: number,
|
|
491
|
+
outcome: enumeration("DanmakuSendOutcome", ["sent", "failed"], "failed")
|
|
492
|
+
});
|
|
493
|
+
var DanmakuSendSession = object("DanmakuSendSession", {
|
|
494
|
+
url: string,
|
|
495
|
+
headers: optional(map(string)),
|
|
496
|
+
handshake: array(DanmakuFrame),
|
|
497
|
+
wait: DanmakuSendWait,
|
|
498
|
+
state: optional(string)
|
|
499
|
+
});
|
|
500
|
+
var DanmakuSendStep = object("DanmakuSendStep", {
|
|
501
|
+
replies: optional(array(DanmakuFrame)),
|
|
502
|
+
sent: optional(boolean),
|
|
503
|
+
wait: optional(DanmakuSendWait),
|
|
504
|
+
state: optional(string)
|
|
505
|
+
});
|
|
487
506
|
var DanmakuBatch = object("DanmakuBatch", {
|
|
488
507
|
messages: array(DanmakuComment),
|
|
489
508
|
replies: optional(array(DanmakuFrame)),
|
|
@@ -779,11 +798,15 @@ var SLOT_RULES = {
|
|
|
779
798
|
capabilities: { subtitles: ["list"], subtitleSearch: ["search"] }
|
|
780
799
|
},
|
|
781
800
|
// send 是在直播间里发一条弹幕(contract.md 2.7),单独一项能力:只认 danmaku 的旧内核不认识 danmakuSend,跳过,不会去调它。
|
|
801
|
+
// 导出 sendDecode 的插件的 send 可以返回发送会话,能力是 danmakuSendSession 而不是 danmakuSend:只认 danmakuSend 的旧内核
|
|
802
|
+
// 不会去调它、把会话当成发出去了。
|
|
782
803
|
danmaku: {
|
|
783
|
-
functions: ["connect", "decode", "segment", "match", "comments", "send"],
|
|
804
|
+
functions: ["connect", "decode", "segment", "match", "comments", "send", "sendDecode"],
|
|
784
805
|
required: [],
|
|
785
806
|
pairs: [["connect", "decode"], ["match", "comments"]],
|
|
786
|
-
|
|
807
|
+
needs: { sendDecode: "send" },
|
|
808
|
+
capabilities: { danmaku: ["connect", "segment"], danmakuMatch: ["match"], danmakuSend: ["send"], danmakuSendSession: ["sendDecode"] },
|
|
809
|
+
supersedes: { danmakuSendSession: "danmakuSend" }
|
|
787
810
|
},
|
|
788
811
|
status: { functions: ["check", "images"], required: ["check"], pairs: [], capabilities: { status: ["check"] } },
|
|
789
812
|
login: {
|
|
@@ -829,7 +852,9 @@ var SLOT_RETURNS = {
|
|
|
829
852
|
decode: { schema: DanmakuBatch },
|
|
830
853
|
segment: { schema: DanmakuSegment },
|
|
831
854
|
match: { schema: array(DanmakuMatch) },
|
|
832
|
-
comments: { schema: DanmakuSegment }
|
|
855
|
+
comments: { schema: DanmakuSegment },
|
|
856
|
+
send: { schema: DanmakuSendSession, nullable: true },
|
|
857
|
+
sendDecode: { schema: DanmakuSendStep }
|
|
833
858
|
},
|
|
834
859
|
status: { check: { schema: array(ChannelStatus) }, images: { schema: array(ChannelImage) } },
|
|
835
860
|
login: {
|
|
@@ -852,8 +877,8 @@ function slotMember(slot, name2) {
|
|
|
852
877
|
// packer/pack.ts
|
|
853
878
|
import { build as build2 } from "esbuild";
|
|
854
879
|
import { mkdir as mkdir2, readdir as readdir4, readFile as readFile5, rm as rm2, writeFile as writeFile2 } from "node:fs/promises";
|
|
855
|
-
import { join as
|
|
856
|
-
import { fileURLToPath as
|
|
880
|
+
import { join as join5 } from "node:path";
|
|
881
|
+
import { fileURLToPath as fileURLToPath3, pathToFileURL } from "node:url";
|
|
857
882
|
|
|
858
883
|
// contract/not-yet-used.ts
|
|
859
884
|
var NOT_YET_USED = [
|
|
@@ -1062,7 +1087,12 @@ function translate({ locale, language, dictionaries: dictionaries2 }, text, valu
|
|
|
1062
1087
|
|
|
1063
1088
|
// trial-runner/trial.ts
|
|
1064
1089
|
import { readdir as readdir3, readFile as readFile4, writeFile } from "node:fs/promises";
|
|
1065
|
-
import { basename, join as
|
|
1090
|
+
import { basename, join as join4, resolve } from "node:path";
|
|
1091
|
+
|
|
1092
|
+
// host/network.ts
|
|
1093
|
+
import { readFileSync } from "node:fs";
|
|
1094
|
+
import { join } from "node:path";
|
|
1095
|
+
import { domainToASCII, fileURLToPath } from "node:url";
|
|
1066
1096
|
|
|
1067
1097
|
// host/files.ts
|
|
1068
1098
|
var FileServiceError = class extends Error {
|
|
@@ -1141,6 +1171,52 @@ function createFiles(host) {
|
|
|
1141
1171
|
}
|
|
1142
1172
|
|
|
1143
1173
|
// host/network.ts
|
|
1174
|
+
function sameSite(host, other) {
|
|
1175
|
+
if (host === other) return true;
|
|
1176
|
+
const domain = registrableDomain(host);
|
|
1177
|
+
return domain !== null && domain === registrableDomain(other);
|
|
1178
|
+
}
|
|
1179
|
+
function vouchedByUserHost(host, use, userHosts, downgrade) {
|
|
1180
|
+
if (use !== "playback" && use !== "playback-follow" && use !== "playback-probe" || downgrade) return false;
|
|
1181
|
+
return [...userHosts].some((user) => sameSite(host, user));
|
|
1182
|
+
}
|
|
1183
|
+
function registrableDomain(host) {
|
|
1184
|
+
const labels = host.split(".");
|
|
1185
|
+
if (labels.length < 2 || labels.includes("") || host.includes(":") || /^[0-9]+$/.test(labels[labels.length - 1])) return null;
|
|
1186
|
+
const rules = publicSuffixRules();
|
|
1187
|
+
const suffix = (start) => labels.slice(start).join(".");
|
|
1188
|
+
let suffixLength = 1;
|
|
1189
|
+
const exception = labels.findIndex((_, start) => rules.exceptions.has(suffix(start)));
|
|
1190
|
+
if (exception >= 0) {
|
|
1191
|
+
suffixLength = labels.length - exception - 1;
|
|
1192
|
+
} else {
|
|
1193
|
+
const start = labels.findIndex((_, index) => rules.exact.has(suffix(index)) || index + 1 < labels.length && rules.wildcards.has(suffix(index + 1)));
|
|
1194
|
+
if (start >= 0) suffixLength = labels.length - start;
|
|
1195
|
+
}
|
|
1196
|
+
return labels.length > suffixLength ? labels.slice(labels.length - suffixLength - 1).join(".") : null;
|
|
1197
|
+
}
|
|
1198
|
+
var suffixRules;
|
|
1199
|
+
var suffixListKernel;
|
|
1200
|
+
function usePublicSuffixListFrom(kernelDirectory2) {
|
|
1201
|
+
suffixListKernel = kernelDirectory2;
|
|
1202
|
+
suffixRules = void 0;
|
|
1203
|
+
}
|
|
1204
|
+
function publicSuffixRules() {
|
|
1205
|
+
if (suffixRules !== void 0) return suffixRules;
|
|
1206
|
+
const kernel = process.env.KRILLPLAYER_KERNEL_DIR ?? suffixListKernel ?? fileURLToPath(new URL("../../Packages/Kernel", import.meta.url));
|
|
1207
|
+
const text = readFileSync(join(kernel, "Sources/Kernel/Network/PublicSuffixList.dat"), "utf8");
|
|
1208
|
+
const rules = { exact: /* @__PURE__ */ new Set(), wildcards: /* @__PURE__ */ new Set(), exceptions: /* @__PURE__ */ new Set() };
|
|
1209
|
+
const domain = (text2) => text2.includes("*") ? "" : bareHost(domainToASCII(text2));
|
|
1210
|
+
for (const line of text.split(/\r?\n/)) {
|
|
1211
|
+
const rule = line.trim().split(/\s/)[0];
|
|
1212
|
+
if (rule === "" || rule.startsWith("//")) continue;
|
|
1213
|
+
const [set, name2] = rule.startsWith("!") ? [rules.exceptions, rule.slice(1)] : rule.startsWith("*.") ? [rules.wildcards, rule.slice(2)] : [rules.exact, rule];
|
|
1214
|
+
const ascii = domain(name2);
|
|
1215
|
+
if (ascii !== "") set.add(ascii);
|
|
1216
|
+
}
|
|
1217
|
+
suffixRules = rules;
|
|
1218
|
+
return rules;
|
|
1219
|
+
}
|
|
1144
1220
|
var MAX_REDIRECTS = 20;
|
|
1145
1221
|
var REMEMBERED_HOSTS = 8;
|
|
1146
1222
|
var REDIRECT_STATUSES = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
@@ -1394,7 +1470,7 @@ var Chain = class {
|
|
|
1394
1470
|
this.leftOrigin = true;
|
|
1395
1471
|
headers2 = hostClass === "derived" ? only(headers2, SAFELIST) : this.withoutCredentials(headers2);
|
|
1396
1472
|
}
|
|
1397
|
-
return this.settle({ address, hostClass, method, headers: headers2, body });
|
|
1473
|
+
return this.settle({ address, hostClass, method, headers: headers2, body, downgrade });
|
|
1398
1474
|
}
|
|
1399
1475
|
// 主机解析出的地址。解析不出(空)算连不上,不是这里的事。
|
|
1400
1476
|
resolved(addresses) {
|
|
@@ -1477,7 +1553,7 @@ var Chain = class {
|
|
|
1477
1553
|
return "only network stream addresses are allowed";
|
|
1478
1554
|
}
|
|
1479
1555
|
}
|
|
1480
|
-
settle({ address, hostClass, method, headers: headers2, body, frames }) {
|
|
1556
|
+
settle({ address, hostClass, method, headers: headers2, body, frames, downgrade = false }) {
|
|
1481
1557
|
if (hostClass === "derived") {
|
|
1482
1558
|
if (method !== "GET" && method !== "HEAD" || body) {
|
|
1483
1559
|
throw this.refuse(
|
|
@@ -1487,7 +1563,7 @@ var Chain = class {
|
|
|
1487
1563
|
);
|
|
1488
1564
|
}
|
|
1489
1565
|
const carried = [address.href, ...Object.keys(headers2), ...Object.values(headers2), ...frames ?? []];
|
|
1490
|
-
if (this.tokens.some((secret) => carried.some((text) => carries(text, secret)))) {
|
|
1566
|
+
if (this.tokens.some((secret) => carried.some((text) => carries(text, secret))) && !vouchedByUserHost(address.host, this.use, this.user, downgrade)) {
|
|
1491
1567
|
throw this.refuse("derived-no-secrets", address, `credentials may not be sent to ${address.host}, a host that is neither declared nor given by the user`);
|
|
1492
1568
|
}
|
|
1493
1569
|
}
|
|
@@ -1806,11 +1882,85 @@ function readAttributes(text) {
|
|
|
1806
1882
|
}
|
|
1807
1883
|
return { attributes, rest: void 0 };
|
|
1808
1884
|
}
|
|
1809
|
-
var
|
|
1885
|
+
var PAIRS = /* @__PURE__ */ Symbol("pairs");
|
|
1886
|
+
var HEADER_FORMS = [
|
|
1887
|
+
{ directive: "#extinf", key: "http-header", carrier: PAIRS },
|
|
1888
|
+
{ directive: "#extinf", key: "http-user-agent", carrier: "User-Agent" },
|
|
1889
|
+
{ directive: "#extinf", key: "http-referrer", carrier: "Referer" },
|
|
1890
|
+
{ directive: "#extinf", key: "http-referer", carrier: "Referer" },
|
|
1891
|
+
{ directive: "#extvlcopt", key: "http-user-agent", carrier: "User-Agent" },
|
|
1892
|
+
{ directive: "#extvlcopt", key: "http-referrer", carrier: "Referer" },
|
|
1893
|
+
{ directive: "#extvlcopt", key: "http-referer", carrier: "Referer" },
|
|
1894
|
+
{ directive: "#kodiprop", key: "inputstream.adaptive.common_headers", carrier: PAIRS },
|
|
1895
|
+
{ directive: "#kodiprop", key: "inputstream.adaptive.manifest_headers", carrier: PAIRS },
|
|
1896
|
+
{ directive: "#kodiprop", key: "inputstream.adaptive.stream_headers", carrier: PAIRS }
|
|
1897
|
+
];
|
|
1898
|
+
var HEADER_NAMES = /* @__PURE__ */ new Map([["user-agent", "User-Agent"], ["referer", "Referer"], ["referrer", "Referer"]]);
|
|
1899
|
+
var NON_HEADER_OPTIONS = /* @__PURE__ */ new Set([
|
|
1900
|
+
"seekable",
|
|
1901
|
+
"reconnect_at_eof",
|
|
1902
|
+
"reconnect_streamed",
|
|
1903
|
+
"reconnect_delay_max",
|
|
1904
|
+
"icy",
|
|
1905
|
+
"icy_metadata_headers",
|
|
1906
|
+
"icy_metadata_packet"
|
|
1907
|
+
]);
|
|
1908
|
+
var RESERVED_HEADERS = /* @__PURE__ */ new Set(["host", "content-length", "connection", "proxy-authenticate", "proxy-authorization", "www-authenticate"]);
|
|
1909
|
+
var HEADER_TOKEN = /^[!#$%&'*+\-.^_\x60|~0-9A-Za-z]+$/;
|
|
1910
|
+
var HEADER_CONTROL = /[\x00-\x08\x0A-\x1F\x7F]/;
|
|
1911
|
+
var M3UHeaders = class {
|
|
1912
|
+
fields = /* @__PURE__ */ new Map();
|
|
1913
|
+
record() {
|
|
1914
|
+
return this.fields.size === 0 ? void 0 : Object.fromEntries(this.fields.values());
|
|
1915
|
+
}
|
|
1916
|
+
set(name2, raw) {
|
|
1917
|
+
const value = trimSpace(raw);
|
|
1918
|
+
if (!HEADER_TOKEN.test(name2) || value === "" || HEADER_CONTROL.test(value)) return;
|
|
1919
|
+
const lower2 = asciiLower(name2);
|
|
1920
|
+
if (RESERVED_HEADERS.has(lower2)) return;
|
|
1921
|
+
this.fields.set(lower2, [name2, value]);
|
|
1922
|
+
}
|
|
1923
|
+
apply(carrier, value) {
|
|
1924
|
+
if (carrier === PAIRS) this.setPairs(value);
|
|
1925
|
+
else this.set(carrier, value);
|
|
1926
|
+
}
|
|
1927
|
+
// `名=值&名=值`:名字前的 `!` 是 Kodi 的自定义头记号;值能按 URL 解码就解码,解不了按原样。
|
|
1928
|
+
setPairs(text) {
|
|
1929
|
+
for (const pair of headerPairs(text)) {
|
|
1930
|
+
if (pair === void 0) continue;
|
|
1931
|
+
const [name2, raw] = pair;
|
|
1932
|
+
const lower2 = asciiLower(name2);
|
|
1933
|
+
if (NON_HEADER_OPTIONS.has(lower2)) continue;
|
|
1934
|
+
this.set(HEADER_NAMES.get(lower2) ?? name2, decodedOr(raw));
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
function headerPairs(text) {
|
|
1939
|
+
return text.split("&").filter((pair) => pair !== "").map((pair) => {
|
|
1940
|
+
const equals = pair.indexOf("=");
|
|
1941
|
+
if (equals === -1) return void 0;
|
|
1942
|
+
const name2 = trimSpace(pair.slice(0, equals));
|
|
1943
|
+
return [name2.startsWith("!") ? name2.slice(1) : name2, pair.slice(equals + 1)];
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
function headerSuffix(line) {
|
|
1947
|
+
const bar = line.indexOf("|");
|
|
1948
|
+
if (bar <= 0) return void 0;
|
|
1949
|
+
const suffix = line.slice(bar + 1);
|
|
1950
|
+
const isHeaders = headerPairs(suffix).every((pair) => pair !== void 0 && HEADER_TOKEN.test(pair[0]));
|
|
1951
|
+
return isHeaders ? { url: trimSpace(line.slice(0, bar)), suffix } : void 0;
|
|
1952
|
+
}
|
|
1953
|
+
function decodedOr(raw) {
|
|
1954
|
+
try {
|
|
1955
|
+
return decodeURIComponent(raw);
|
|
1956
|
+
} catch {
|
|
1957
|
+
return raw;
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1810
1960
|
function parseM3U(data) {
|
|
1811
1961
|
const playlist = { guideURLs: [], attributes: {}, entries: [] };
|
|
1812
1962
|
let info;
|
|
1813
|
-
let
|
|
1963
|
+
let directiveHeaders = [];
|
|
1814
1964
|
let group;
|
|
1815
1965
|
for (const raw of splitLines(textOf(data))) {
|
|
1816
1966
|
const line = trimSpace(raw);
|
|
@@ -1818,19 +1968,28 @@ function parseM3U(data) {
|
|
|
1818
1968
|
if (line[0] !== "#") {
|
|
1819
1969
|
const attributes = info?.attributes ?? /* @__PURE__ */ new Map();
|
|
1820
1970
|
const tvgName = nonEmpty(attributes.get("tvg-name"));
|
|
1971
|
+
const headers2 = new M3UHeaders();
|
|
1972
|
+
for (const form of HEADER_FORMS) {
|
|
1973
|
+
const value2 = form.directive === "#extinf" ? attributes.get(form.key) : void 0;
|
|
1974
|
+
if (value2 !== void 0) headers2.apply(form.carrier, value2);
|
|
1975
|
+
}
|
|
1976
|
+
for (const [carrier, value2] of directiveHeaders) headers2.apply(carrier, value2);
|
|
1977
|
+
const split = headerSuffix(line);
|
|
1978
|
+
const url = split?.url ?? line;
|
|
1979
|
+
if (split !== void 0) headers2.setPairs(split.suffix);
|
|
1821
1980
|
playlist.entries.push(compact({
|
|
1822
|
-
url
|
|
1823
|
-
title: nonEmpty(info?.title) ?? tvgName ??
|
|
1981
|
+
url,
|
|
1982
|
+
title: nonEmpty(info?.title) ?? tvgName ?? url,
|
|
1824
1983
|
duration: info?.duration ?? -1,
|
|
1825
1984
|
tvgId: nonEmpty(attributes.get("tvg-id")),
|
|
1826
1985
|
tvgName,
|
|
1827
1986
|
logo: nonEmpty(attributes.get("tvg-logo")),
|
|
1828
1987
|
group: nonEmpty(attributes.get("group-title")) ?? group,
|
|
1829
|
-
headers: headers2.
|
|
1988
|
+
headers: headers2.record(),
|
|
1830
1989
|
attributes: Object.fromEntries(attributes)
|
|
1831
1990
|
}));
|
|
1832
1991
|
info = void 0;
|
|
1833
|
-
|
|
1992
|
+
directiveHeaders = [];
|
|
1834
1993
|
group = void 0;
|
|
1835
1994
|
continue;
|
|
1836
1995
|
}
|
|
@@ -1851,13 +2010,13 @@ function parseM3U(data) {
|
|
|
1851
2010
|
const { attributes, rest } = readAttributes(duration === null ? value : value.slice(duration[0].length));
|
|
1852
2011
|
const seconds = duration === null ? -1 : Number(duration[1]);
|
|
1853
2012
|
info = { duration: Number.isFinite(seconds) ? seconds : -1, attributes, title: trimSpace(rest ?? "") };
|
|
1854
|
-
} else if (directive === "#extvlcopt") {
|
|
1855
|
-
const equals = value.indexOf("=");
|
|
1856
|
-
const name3 = VLC_HEADERS.get(asciiLower(trimSpace(equals === -1 ? value : value.slice(0, equals))));
|
|
1857
|
-
const headerValue = equals === -1 ? "" : trimSpace(value.slice(equals + 1));
|
|
1858
|
-
if (name3 !== void 0 && headerValue !== "") headers2.set(name3, headerValue);
|
|
1859
2013
|
} else if (directive === "#extgrp") {
|
|
1860
2014
|
group = nonEmpty(value);
|
|
2015
|
+
} else {
|
|
2016
|
+
const equals = value.indexOf("=");
|
|
2017
|
+
const key = asciiLower(trimSpace(equals === -1 ? value : value.slice(0, equals)));
|
|
2018
|
+
const form = HEADER_FORMS.find((row) => row.directive === directive && row.key === key);
|
|
2019
|
+
if (equals !== -1 && form !== void 0) directiveHeaders.push([form.carrier, value.slice(equals + 1)]);
|
|
1861
2020
|
}
|
|
1862
2021
|
}
|
|
1863
2022
|
return playlist;
|
|
@@ -2109,6 +2268,121 @@ function createMetadata(service) {
|
|
|
2109
2268
|
};
|
|
2110
2269
|
}
|
|
2111
2270
|
|
|
2271
|
+
// host/interop-crypto.ts
|
|
2272
|
+
var rotl = (x, n) => x << n | x >>> 32 - n;
|
|
2273
|
+
function padded(data, littleEndian) {
|
|
2274
|
+
const length = (data.length + 8 >> 6) + 1 << 6;
|
|
2275
|
+
const block = new Uint8Array(length);
|
|
2276
|
+
block.set(data);
|
|
2277
|
+
block[data.length] = 128;
|
|
2278
|
+
const view = new DataView(block.buffer);
|
|
2279
|
+
const bits = data.length * 8;
|
|
2280
|
+
const high = Math.floor(bits / 4294967296);
|
|
2281
|
+
const low = bits >>> 0;
|
|
2282
|
+
if (littleEndian) {
|
|
2283
|
+
view.setUint32(length - 8, low, true);
|
|
2284
|
+
view.setUint32(length - 4, high, true);
|
|
2285
|
+
} else {
|
|
2286
|
+
view.setUint32(length - 8, high);
|
|
2287
|
+
view.setUint32(length - 4, low);
|
|
2288
|
+
}
|
|
2289
|
+
return view;
|
|
2290
|
+
}
|
|
2291
|
+
var MD5_K = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) >>> 0);
|
|
2292
|
+
var MD5_S = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
|
|
2293
|
+
function md5(data) {
|
|
2294
|
+
const view = padded(data, true);
|
|
2295
|
+
let a0 = 1732584193, b0 = 4023233417, c0 = 2562383102, d0 = 271733878;
|
|
2296
|
+
const m = new Array(16);
|
|
2297
|
+
for (let offset = 0; offset < view.byteLength; offset += 64) {
|
|
2298
|
+
for (let i = 0; i < 16; i++) m[i] = view.getUint32(offset + i * 4, true);
|
|
2299
|
+
let a = a0, b = b0, c = c0, d = d0;
|
|
2300
|
+
for (let i = 0; i < 64; i++) {
|
|
2301
|
+
const round = i >> 4;
|
|
2302
|
+
let f, g;
|
|
2303
|
+
if (round === 0) {
|
|
2304
|
+
f = b & c | ~b & d;
|
|
2305
|
+
g = i;
|
|
2306
|
+
} else if (round === 1) {
|
|
2307
|
+
f = d & b | ~d & c;
|
|
2308
|
+
g = 5 * i + 1 & 15;
|
|
2309
|
+
} else if (round === 2) {
|
|
2310
|
+
f = b ^ c ^ d;
|
|
2311
|
+
g = 3 * i + 5 & 15;
|
|
2312
|
+
} else {
|
|
2313
|
+
f = c ^ (b | ~d);
|
|
2314
|
+
g = 7 * i & 15;
|
|
2315
|
+
}
|
|
2316
|
+
const next = d;
|
|
2317
|
+
d = c;
|
|
2318
|
+
c = b;
|
|
2319
|
+
b = b + rotl(a + f + MD5_K[i] + m[g] | 0, MD5_S[round << 2 | i & 3]) | 0;
|
|
2320
|
+
a = next;
|
|
2321
|
+
}
|
|
2322
|
+
a0 = a0 + a | 0;
|
|
2323
|
+
b0 = b0 + b | 0;
|
|
2324
|
+
c0 = c0 + c | 0;
|
|
2325
|
+
d0 = d0 + d | 0;
|
|
2326
|
+
}
|
|
2327
|
+
const out = new DataView(new ArrayBuffer(16));
|
|
2328
|
+
[a0, b0, c0, d0].forEach((word, i) => out.setUint32(i * 4, word >>> 0, true));
|
|
2329
|
+
return new Uint8Array(out.buffer);
|
|
2330
|
+
}
|
|
2331
|
+
var SM3_IV = [1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214];
|
|
2332
|
+
var p0 = (x) => x ^ rotl(x, 9) ^ rotl(x, 17);
|
|
2333
|
+
var p1 = (x) => x ^ rotl(x, 15) ^ rotl(x, 23);
|
|
2334
|
+
function sm3(data) {
|
|
2335
|
+
const view = padded(data, false);
|
|
2336
|
+
const v = [...SM3_IV];
|
|
2337
|
+
const w = new Array(68);
|
|
2338
|
+
const w1 = new Array(64);
|
|
2339
|
+
for (let offset = 0; offset < view.byteLength; offset += 64) {
|
|
2340
|
+
for (let j = 0; j < 16; j++) w[j] = view.getUint32(offset + j * 4) | 0;
|
|
2341
|
+
for (let j = 16; j < 68; j++) w[j] = p1(w[j - 16] ^ w[j - 9] ^ rotl(w[j - 3], 15)) ^ rotl(w[j - 13], 7) ^ w[j - 6];
|
|
2342
|
+
for (let j = 0; j < 64; j++) w1[j] = w[j] ^ w[j + 4];
|
|
2343
|
+
let [a, b, c, d, e, f, g, h] = v;
|
|
2344
|
+
for (let j = 0; j < 64; j++) {
|
|
2345
|
+
const t = j < 16 ? 2043430169 : 2055708042;
|
|
2346
|
+
const ss1 = rotl(rotl(a, 12) + e + rotl(t, j % 32) | 0, 7);
|
|
2347
|
+
const ss2 = ss1 ^ rotl(a, 12);
|
|
2348
|
+
const ff = j < 16 ? a ^ b ^ c : a & b | a & c | b & c;
|
|
2349
|
+
const gg = j < 16 ? e ^ f ^ g : e & f | ~e & g;
|
|
2350
|
+
const tt1 = ff + d + ss2 + w1[j] | 0;
|
|
2351
|
+
const tt2 = gg + h + ss1 + w[j] | 0;
|
|
2352
|
+
d = c;
|
|
2353
|
+
c = rotl(b, 9);
|
|
2354
|
+
b = a;
|
|
2355
|
+
a = tt1;
|
|
2356
|
+
h = g;
|
|
2357
|
+
g = rotl(f, 19);
|
|
2358
|
+
f = e;
|
|
2359
|
+
e = p0(tt2);
|
|
2360
|
+
}
|
|
2361
|
+
[a, b, c, d, e, f, g, h].forEach((word, i) => {
|
|
2362
|
+
v[i] ^= word;
|
|
2363
|
+
});
|
|
2364
|
+
}
|
|
2365
|
+
const out = new DataView(new ArrayBuffer(32));
|
|
2366
|
+
v.forEach((word, i) => out.setUint32(i * 4, word >>> 0));
|
|
2367
|
+
return new Uint8Array(out.buffer);
|
|
2368
|
+
}
|
|
2369
|
+
function rc4(key, data) {
|
|
2370
|
+
const s = new Uint8Array(256);
|
|
2371
|
+
for (let i = 0; i < 256; i++) s[i] = i;
|
|
2372
|
+
for (let i = 0, j = 0; i < 256; i++) {
|
|
2373
|
+
j = j + s[i] + key[i % key.length] & 255;
|
|
2374
|
+
[s[i], s[j]] = [s[j], s[i]];
|
|
2375
|
+
}
|
|
2376
|
+
const out = new Uint8Array(data.length);
|
|
2377
|
+
for (let n = 0, i = 0, j = 0; n < data.length; n++) {
|
|
2378
|
+
i = i + 1 & 255;
|
|
2379
|
+
j = j + s[i] & 255;
|
|
2380
|
+
[s[i], s[j]] = [s[j], s[i]];
|
|
2381
|
+
out[n] = data[n] ^ s[s[i] + s[j] & 255];
|
|
2382
|
+
}
|
|
2383
|
+
return out;
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2112
2386
|
// host/json-depth.ts
|
|
2113
2387
|
function pluginJSONText(bytes2) {
|
|
2114
2388
|
if (bytes2[0] === 239 && bytes2[1] === 187 && bytes2[2] === 191) return void 0;
|
|
@@ -2897,15 +3171,30 @@ function createSubtle() {
|
|
|
2897
3171
|
};
|
|
2898
3172
|
}
|
|
2899
3173
|
var DIGESTS = /* @__PURE__ */ new Map([["sha-1", "SHA-1"], ["sha-256", "SHA-256"], ["sha-384", "SHA-384"], ["sha-512", "SHA-512"]]);
|
|
3174
|
+
var INTEROP_DIGESTS = /* @__PURE__ */ new Map([["md5", md5], ["sm3", sm3]]);
|
|
3175
|
+
function plainBytes(value) {
|
|
3176
|
+
if (typeof value === "string") return new TextEncoder().encode(value);
|
|
3177
|
+
if (ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === "[object Uint8Array]") return new Uint8Array(value);
|
|
3178
|
+
throw new TypeError("expected a Uint8Array or a string");
|
|
3179
|
+
}
|
|
2900
3180
|
async function plainDigest(algorithm, data) {
|
|
2901
|
-
const
|
|
2902
|
-
|
|
2903
|
-
|
|
3181
|
+
const lowered = String(algorithm).toLowerCase();
|
|
3182
|
+
const interop = INTEROP_DIGESTS.get(lowered);
|
|
3183
|
+
const name2 = DIGESTS.get(lowered);
|
|
3184
|
+
if (interop === void 0 && name2 === void 0) throw new TypeError(`unsupported digest ${algorithm}`);
|
|
3185
|
+
const bytes2 = plainBytes(data);
|
|
3186
|
+
return interop !== void 0 ? interop(bytes2) : new Uint8Array(await subtle().digest(name2, bytes2));
|
|
3187
|
+
}
|
|
3188
|
+
async function plainRC4(key, data) {
|
|
3189
|
+
const keyBytes = plainBytes(key);
|
|
3190
|
+
if (keyBytes.length < 1 || keyBytes.length > 256) throw new TypeError("krillplayer.crypto.rc4 needs a key of 1 to 256 bytes");
|
|
3191
|
+
return rc4(keyBytes, plainBytes(data));
|
|
2904
3192
|
}
|
|
2905
3193
|
function createCrypto() {
|
|
2906
3194
|
return {
|
|
2907
3195
|
randomBytes: (length) => globalThis.crypto.getRandomValues(new Uint8Array(length)),
|
|
2908
3196
|
digest: plainDigest,
|
|
3197
|
+
rc4: plainRC4,
|
|
2909
3198
|
subtle: createSubtle()
|
|
2910
3199
|
};
|
|
2911
3200
|
}
|
|
@@ -3337,6 +3626,91 @@ async function decompress(format2, data, limit = FORMAT_LIMITS.decompressedBytes
|
|
|
3337
3626
|
return output;
|
|
3338
3627
|
}
|
|
3339
3628
|
|
|
3629
|
+
// host/danmaku-send.ts
|
|
3630
|
+
var DanmakuSendFailure = class extends Error {
|
|
3631
|
+
};
|
|
3632
|
+
async function runSendSession({
|
|
3633
|
+
session,
|
|
3634
|
+
grants,
|
|
3635
|
+
open,
|
|
3636
|
+
decode: decode2,
|
|
3637
|
+
lookup: lookup2
|
|
3638
|
+
}) {
|
|
3639
|
+
const secrets = (grants.secrets ?? []).filter((secret) => secret !== "");
|
|
3640
|
+
const texts = (frames) => frames.map(frameText);
|
|
3641
|
+
const headers2 = session.headers ?? {};
|
|
3642
|
+
let pass;
|
|
3643
|
+
try {
|
|
3644
|
+
const chain = new Chain("danmaku", grants);
|
|
3645
|
+
pass = chain.first({ url: session.url, headers: headers2, frames: texts(session.handshake) });
|
|
3646
|
+
const looked = pass.publicOnly === null ? void 0 : lookup2?.(pass.address.host);
|
|
3647
|
+
if (looked !== void 0) chain.resolved(looked.addresses);
|
|
3648
|
+
} catch (error) {
|
|
3649
|
+
throw new DanmakuSendFailure(`danmaku send: ${error.message}`);
|
|
3650
|
+
}
|
|
3651
|
+
const inFrame = (frame2, secret) => frame2.data === void 0 ? (frame2.text ?? "").includes(secret) : Buffer.from(frame2.data).includes(Buffer.from(secret, "utf8"));
|
|
3652
|
+
const carrying = (strings, frames) => secrets.some((secret) => strings.some((text) => text.includes(secret)) || frames.some((frame2) => inFrame(frame2, secret)));
|
|
3653
|
+
const mayCarry = carrying([session.url, ...Object.keys(headers2), ...Object.values(headers2)], session.handshake);
|
|
3654
|
+
const socket = open(pass.address.href, pass.headers);
|
|
3655
|
+
let expired = null;
|
|
3656
|
+
let shookHands = false;
|
|
3657
|
+
let timer;
|
|
3658
|
+
const end = (outcome) => {
|
|
3659
|
+
expired ??= outcome;
|
|
3660
|
+
socket.close();
|
|
3661
|
+
};
|
|
3662
|
+
const arm = (wait) => {
|
|
3663
|
+
clearTimeout(timer);
|
|
3664
|
+
const seconds = Math.min(Math.max(wait.seconds, 0), DANMAKU_SEND_LIMITS.waitSeconds);
|
|
3665
|
+
timer = setTimeout(() => end(shookHands ? wait.outcome : "failed"), seconds * 1e3);
|
|
3666
|
+
};
|
|
3667
|
+
const cap = setTimeout(() => end("failed"), DANMAKU_SEND_LIMITS.sessionSeconds * 1e3);
|
|
3668
|
+
const closed = () => {
|
|
3669
|
+
if (expired === "sent") return;
|
|
3670
|
+
throw new DanmakuSendFailure(expired === null ? "danmaku send: the connection closed before the plugin decided" : "danmaku send: no decision before the deadline");
|
|
3671
|
+
};
|
|
3672
|
+
try {
|
|
3673
|
+
arm(session.wait);
|
|
3674
|
+
try {
|
|
3675
|
+
for (const frame2 of session.handshake) await socket.send(frame2);
|
|
3676
|
+
} catch {
|
|
3677
|
+
return closed();
|
|
3678
|
+
}
|
|
3679
|
+
shookHands = true;
|
|
3680
|
+
arm(session.wait);
|
|
3681
|
+
let state = session.state ?? null;
|
|
3682
|
+
for (; ; ) {
|
|
3683
|
+
let frame2;
|
|
3684
|
+
try {
|
|
3685
|
+
frame2 = await socket.receive();
|
|
3686
|
+
} catch {
|
|
3687
|
+
return closed();
|
|
3688
|
+
}
|
|
3689
|
+
const step = await decode2(frame2, state);
|
|
3690
|
+
if (step.state !== void 0) state = step.state;
|
|
3691
|
+
for (const reply of step.replies ?? []) {
|
|
3692
|
+
if (!mayCarry && carrying([], [reply])) {
|
|
3693
|
+
throw new DanmakuSendFailure("danmaku send: a reply carries credentials the handshake did not");
|
|
3694
|
+
}
|
|
3695
|
+
try {
|
|
3696
|
+
await socket.send(reply);
|
|
3697
|
+
} catch {
|
|
3698
|
+
return closed();
|
|
3699
|
+
}
|
|
3700
|
+
}
|
|
3701
|
+
if (step.sent === true) return;
|
|
3702
|
+
if (step.wait !== void 0) arm(step.wait);
|
|
3703
|
+
}
|
|
3704
|
+
} finally {
|
|
3705
|
+
clearTimeout(timer);
|
|
3706
|
+
clearTimeout(cap);
|
|
3707
|
+
socket.close();
|
|
3708
|
+
}
|
|
3709
|
+
}
|
|
3710
|
+
function frameText(frame2) {
|
|
3711
|
+
return frame2.data === void 0 ? frame2.text ?? "" : Buffer.from(frame2.data).toString("latin1");
|
|
3712
|
+
}
|
|
3713
|
+
|
|
3340
3714
|
// contract/validate.ts
|
|
3341
3715
|
var UNKNOWN_KIND_HINTS = [
|
|
3342
3716
|
[/^(video|vod|clip|short)s?$/i, "\u5E73\u53F0\u4E0A\u4E00\u4E2A\u666E\u901A\u7684\u89C6\u9891\u5199 onDemand\uFF0C\u4E0D\u8981\u5199\u6210 movie \u6216 file"],
|
|
@@ -3593,7 +3967,7 @@ function readZip(zip) {
|
|
|
3593
3967
|
|
|
3594
3968
|
// trial-runner/cases.ts
|
|
3595
3969
|
import { readdir, readFile as readFile2 } from "node:fs/promises";
|
|
3596
|
-
import { join, relative } from "node:path";
|
|
3970
|
+
import { join as join2, relative } from "node:path";
|
|
3597
3971
|
|
|
3598
3972
|
// trial-runner/credentials.ts
|
|
3599
3973
|
import { readFile } from "node:fs/promises";
|
|
@@ -4036,7 +4410,7 @@ function recordingFetch(fetch) {
|
|
|
4036
4410
|
}
|
|
4037
4411
|
async function jsonFiles(directory) {
|
|
4038
4412
|
const entries = await readdir(directory, { recursive: true, withFileTypes: true });
|
|
4039
|
-
return entries.filter((entry) => entry.isFile() && entry.name.endsWith(".json") && !entry.parentPath.includes("node_modules")).map((entry) =>
|
|
4413
|
+
return entries.filter((entry) => entry.isFile() && entry.name.endsWith(".json") && !entry.parentPath.includes("node_modules")).map((entry) => join2(entry.parentPath, entry.name)).sort();
|
|
4040
4414
|
}
|
|
4041
4415
|
var BYTE_ORDER_MARK_IN_CASE = "case file holds U+FEFF, which the Swift kernel's JSON reader drops; keep such text as $bytes";
|
|
4042
4416
|
var holdsByteOrderMark = (raw) => raw.includes("\uFEFF") || /\\u[fF][eE][fF][fF]/.test(raw);
|
|
@@ -4047,6 +4421,76 @@ async function readCases(directory) {
|
|
|
4047
4421
|
return { name: relative(directory, file), data: JSON.parse(raw), byteOrderMark: holdsByteOrderMark(raw) };
|
|
4048
4422
|
}));
|
|
4049
4423
|
}
|
|
4424
|
+
var frameJSON = (frame2) => frame2.data === void 0 ? { text: frame2.text ?? "" } : { data: bytesToJSON(frame2.data) };
|
|
4425
|
+
var frameOf = (json) => "data" in json ? { data: new Uint8Array(Buffer.from(json.data.$bytes, "base64")) } : { text: json.text };
|
|
4426
|
+
function scriptedSockets(script) {
|
|
4427
|
+
const problems = [];
|
|
4428
|
+
let opened = false;
|
|
4429
|
+
let step = 0;
|
|
4430
|
+
const open = (url, headers2) => {
|
|
4431
|
+
if (opened) problems.push(`a second socket to ${url}`);
|
|
4432
|
+
opened = true;
|
|
4433
|
+
if (script === void 0) problems.push(`unscripted socket to ${url}`);
|
|
4434
|
+
else if (url !== script.url) problems.push(`socket to ${url}, scripted ${script.url}`);
|
|
4435
|
+
else if (script.headers !== void 0 && canonicalHeaders(script.headers) !== canonicalHeaders(headers2)) {
|
|
4436
|
+
problems.push(`socket headers ${canonicalHeaders(headers2)}, scripted ${canonicalHeaders(script.headers)}`);
|
|
4437
|
+
}
|
|
4438
|
+
const steps = problems.length === 0 && script !== void 0 ? script.script : [];
|
|
4439
|
+
const inbox = [];
|
|
4440
|
+
const waiting = [];
|
|
4441
|
+
let closed = problems.length > 0;
|
|
4442
|
+
const shut = () => {
|
|
4443
|
+
closed = true;
|
|
4444
|
+
for (const waiter of waiting.splice(0)) waiter.reject(new Error("socket closed"));
|
|
4445
|
+
};
|
|
4446
|
+
const advance = () => {
|
|
4447
|
+
while (!closed && step < steps.length) {
|
|
4448
|
+
const current = steps[step];
|
|
4449
|
+
if ("client" in current) return;
|
|
4450
|
+
step += 1;
|
|
4451
|
+
if ("close" in current) return shut();
|
|
4452
|
+
const frame2 = frameOf(current.server);
|
|
4453
|
+
const waiter = waiting.shift();
|
|
4454
|
+
if (waiter === void 0) inbox.push(frame2);
|
|
4455
|
+
else waiter.resolve(frame2);
|
|
4456
|
+
}
|
|
4457
|
+
};
|
|
4458
|
+
advance();
|
|
4459
|
+
return {
|
|
4460
|
+
async send(frame2) {
|
|
4461
|
+
if (closed) throw new Error("socket closed");
|
|
4462
|
+
const current = steps[step];
|
|
4463
|
+
if (current === void 0 || !("client" in current)) {
|
|
4464
|
+
problems.push(`unscripted frame ${canonical(frameJSON(frame2))}`);
|
|
4465
|
+
return shut();
|
|
4466
|
+
}
|
|
4467
|
+
if (canonical(frameJSON(frame2)) !== canonical(current.client)) {
|
|
4468
|
+
problems.push(`frame differs at socket step ${step + 1}
|
|
4469
|
+
expected ${canonical(current.client)}
|
|
4470
|
+
actual ${canonical(frameJSON(frame2))}`);
|
|
4471
|
+
return shut();
|
|
4472
|
+
}
|
|
4473
|
+
step += 1;
|
|
4474
|
+
advance();
|
|
4475
|
+
},
|
|
4476
|
+
receive() {
|
|
4477
|
+
const frame2 = inbox.shift();
|
|
4478
|
+
if (frame2 !== void 0) return Promise.resolve(frame2);
|
|
4479
|
+
if (closed) return Promise.reject(new Error("socket closed"));
|
|
4480
|
+
return new Promise((resolve4, reject) => waiting.push({ resolve: resolve4, reject }));
|
|
4481
|
+
},
|
|
4482
|
+
close: shut
|
|
4483
|
+
};
|
|
4484
|
+
};
|
|
4485
|
+
return {
|
|
4486
|
+
open,
|
|
4487
|
+
problems: () => [
|
|
4488
|
+
...problems,
|
|
4489
|
+
...script !== void 0 && !opened ? [`scripted socket to ${script.url} was not opened`] : [],
|
|
4490
|
+
...opened && script !== void 0 && problems.length === 0 && step < script.script.length ? [`socket script stopped at step ${step + 1} of ${script.script.length}`] : []
|
|
4491
|
+
]
|
|
4492
|
+
};
|
|
4493
|
+
}
|
|
4050
4494
|
|
|
4051
4495
|
// trial-runner/engine.ts
|
|
4052
4496
|
import { execFile, spawn } from "node:child_process";
|
|
@@ -4055,8 +4499,8 @@ import { constants } from "node:fs";
|
|
|
4055
4499
|
import { access, mkdir, mkdtemp, readdir as readdir2, readFile as readFile3, rename, rm, stat } from "node:fs/promises";
|
|
4056
4500
|
import { createRequire } from "node:module";
|
|
4057
4501
|
import { homedir } from "node:os";
|
|
4058
|
-
import { dirname, join as
|
|
4059
|
-
import { fileURLToPath } from "node:url";
|
|
4502
|
+
import { dirname, join as join3, relative as relative2 } from "node:path";
|
|
4503
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
4060
4504
|
import { promisify } from "node:util";
|
|
4061
4505
|
|
|
4062
4506
|
// host/globals.ts
|
|
@@ -4168,7 +4612,7 @@ function decode(json, blob) {
|
|
|
4168
4612
|
}
|
|
4169
4613
|
|
|
4170
4614
|
// trial-runner/engine.ts
|
|
4171
|
-
var here = dirname(
|
|
4615
|
+
var here = dirname(fileURLToPath2(import.meta.url));
|
|
4172
4616
|
var run3 = promisify(execFile);
|
|
4173
4617
|
var KERNEL_LIMITS = {
|
|
4174
4618
|
memoryBytes: RUNTIME_LIMITS.memoryBytes,
|
|
@@ -4178,17 +4622,18 @@ var KERNEL_LIMITS = {
|
|
|
4178
4622
|
var packagedKernel;
|
|
4179
4623
|
function useKernelDirectory(directory) {
|
|
4180
4624
|
packagedKernel = directory;
|
|
4625
|
+
usePublicSuffixListFrom(directory);
|
|
4181
4626
|
}
|
|
4182
4627
|
function kernelDirectory() {
|
|
4183
|
-
return process.env.KRILLPLAYER_KERNEL_DIR ?? packagedKernel ??
|
|
4628
|
+
return process.env.KRILLPLAYER_KERNEL_DIR ?? packagedKernel ?? join3(here, "../../Packages/Kernel");
|
|
4184
4629
|
}
|
|
4185
4630
|
var FLAGS = ["-O2", "-D_GNU_SOURCE", "-DQUICKJS_NG_BUILD", "-DNDEBUG", "-w"];
|
|
4186
4631
|
async function files(directory) {
|
|
4187
4632
|
const entries = await readdir2(directory, { withFileTypes: true, recursive: true });
|
|
4188
|
-
return entries.filter((entry) => entry.isFile()).map((entry) =>
|
|
4633
|
+
return entries.filter((entry) => entry.isFile()).map((entry) => join3(entry.parentPath, entry.name)).sort();
|
|
4189
4634
|
}
|
|
4190
|
-
var DRIVER =
|
|
4191
|
-
async function engineSourceHash(quickjs =
|
|
4635
|
+
var DRIVER = join3(here, "engine/engine.c");
|
|
4636
|
+
async function engineSourceHash(quickjs = join3(kernelDirectory(), "Sources/CQuickJS")) {
|
|
4192
4637
|
const hash = createHash("sha256").update(JSON.stringify(FLAGS)).update("engine.c").update(await readFile3(DRIVER));
|
|
4193
4638
|
for (const file of await files(quickjs)) hash.update(relative2(quickjs, file)).update(await readFile3(file));
|
|
4194
4639
|
return hash.digest("hex");
|
|
@@ -4198,7 +4643,7 @@ async function prebuilt(source) {
|
|
|
4198
4643
|
try {
|
|
4199
4644
|
const manifest = createRequire(import.meta.url).resolve(`${ENGINE_PACKAGE()}/engine.json`);
|
|
4200
4645
|
if (JSON.parse(await readFile3(manifest, "utf8"))?.source !== source) return void 0;
|
|
4201
|
-
const binary2 =
|
|
4646
|
+
const binary2 = join3(dirname(manifest), "engine");
|
|
4202
4647
|
return await executable(binary2) ? binary2 : void 0;
|
|
4203
4648
|
} catch {
|
|
4204
4649
|
return void 0;
|
|
@@ -4214,7 +4659,7 @@ async function executable(file) {
|
|
|
4214
4659
|
}
|
|
4215
4660
|
var environment = (name2) => process.env[name2] || void 0;
|
|
4216
4661
|
async function build() {
|
|
4217
|
-
const quickjs =
|
|
4662
|
+
const quickjs = join3(kernelDirectory(), "Sources/CQuickJS");
|
|
4218
4663
|
const compiler = environment("CC") ?? "cc";
|
|
4219
4664
|
const source = await engineSourceHash(quickjs).catch((error) => {
|
|
4220
4665
|
throw new EngineFailure(`could not read the QuickJS-ng engine sources in ${quickjs}: ${error.message}`);
|
|
@@ -4228,15 +4673,15 @@ async function build() {
|
|
|
4228
4673
|
}
|
|
4229
4674
|
}
|
|
4230
4675
|
async function compileEngine(output, { compiler = environment("CC") ?? "cc", arch } = {}) {
|
|
4231
|
-
const quickjs =
|
|
4676
|
+
const quickjs = join3(kernelDirectory(), "Sources/CQuickJS");
|
|
4232
4677
|
await run3(compiler, [
|
|
4233
4678
|
...FLAGS,
|
|
4234
4679
|
...arch === void 0 ? [] : ["-arch", arch],
|
|
4235
|
-
`-I${
|
|
4680
|
+
`-I${join3(quickjs, "include")}`,
|
|
4236
4681
|
`-I${quickjs}`,
|
|
4237
4682
|
DRIVER,
|
|
4238
|
-
|
|
4239
|
-
|
|
4683
|
+
join3(quickjs, "kernel_qjs.c"),
|
|
4684
|
+
join3(quickjs, "quickjs_vendor.c"),
|
|
4240
4685
|
"-lm",
|
|
4241
4686
|
"-lpthread",
|
|
4242
4687
|
"-o",
|
|
@@ -4246,14 +4691,14 @@ async function compileEngine(output, { compiler = environment("CC") ?? "cc", arc
|
|
|
4246
4691
|
async function compile(quickjs, compiler, source) {
|
|
4247
4692
|
const version = (await run3(compiler, ["--version"])).stdout;
|
|
4248
4693
|
const key = createHash("sha256").update(source).update(version).digest("hex");
|
|
4249
|
-
const cache = environment("KRILLPLAYER_ENGINE_CACHE") ??
|
|
4250
|
-
const target =
|
|
4251
|
-
const binary2 =
|
|
4694
|
+
const cache = environment("KRILLPLAYER_ENGINE_CACHE") ?? join3(environment("XDG_CACHE_HOME") ?? join3(homedir(), ".cache"), "krillplayer-plugin/engine");
|
|
4695
|
+
const target = join3(cache, key.slice(0, 32));
|
|
4696
|
+
const binary2 = join3(target, "engine");
|
|
4252
4697
|
if (await executable(binary2)) return binary2;
|
|
4253
4698
|
await mkdir(cache, { recursive: true });
|
|
4254
|
-
const staging = await mkdtemp(
|
|
4699
|
+
const staging = await mkdtemp(join3(cache, "build-"));
|
|
4255
4700
|
try {
|
|
4256
|
-
await compileEngine(
|
|
4701
|
+
await compileEngine(join3(staging, "engine"), { compiler });
|
|
4257
4702
|
await rename(staging, target).catch(async (error) => {
|
|
4258
4703
|
if (error.code !== "ENOTEMPTY" && error.code !== "EEXIST") throw error;
|
|
4259
4704
|
await rm(staging, { recursive: true, force: true });
|
|
@@ -4271,7 +4716,7 @@ function engineBinary() {
|
|
|
4271
4716
|
}
|
|
4272
4717
|
var prelude;
|
|
4273
4718
|
function preludeSource() {
|
|
4274
|
-
const path =
|
|
4719
|
+
const path = join3(kernelDirectory(), "Sources/Kernel/PluginPrelude.js");
|
|
4275
4720
|
prelude ??= readFile3(path, "utf8").then(
|
|
4276
4721
|
(template) => template.replaceAll("__GLOBALS__", `[${[...PLUGIN_GLOBALS].sort().map((name2) => JSON.stringify(name2)).join(", ")}]`),
|
|
4277
4722
|
(error) => {
|
|
@@ -4404,6 +4849,7 @@ var CALLS = {
|
|
|
4404
4849
|
"formats.xml": (k, [data]) => k.formats.xml(data),
|
|
4405
4850
|
"decompress": (k, [format2, data]) => k.decompress(format2, data),
|
|
4406
4851
|
"crypto.digest": (k, [algorithm, data]) => k.crypto.digest(algorithm, data),
|
|
4852
|
+
"crypto.rc4": (k, [key, data]) => k.crypto.rc4(key, data),
|
|
4407
4853
|
"crypto.randomBytes": (k, [length]) => k.crypto.randomBytes(length),
|
|
4408
4854
|
"crypto.subtle.generateKey": (k, [algorithm, extractable, usages]) => k.crypto.subtle.generateKey(algorithm, extractable, usages),
|
|
4409
4855
|
"crypto.subtle.importKey": (k, [format2, keyData, algorithm, extractable, usages]) => k.crypto.subtle.importKey(format2, keyData, algorithm, extractable, usages),
|
|
@@ -4784,6 +5230,9 @@ function capabilitiesOf(namespace) {
|
|
|
4784
5230
|
problems.push(`${name2}.${present} needs ${name2}.${missing}`);
|
|
4785
5231
|
}
|
|
4786
5232
|
}
|
|
5233
|
+
for (const [member, needed] of Object.entries(rule.needs ?? {})) {
|
|
5234
|
+
if (has(member) && !has(needed)) problems.push(`${name2}.${member} needs ${name2}.${needed}`);
|
|
5235
|
+
}
|
|
4787
5236
|
const matched = Object.entries(rule.capabilities).filter(([, any]) => any.some(has)).map(([capability]) => capability);
|
|
4788
5237
|
const superseded = new Set(matched.flatMap((capability) => rule.supersedes?.[capability] ?? []));
|
|
4789
5238
|
const generated = matched.filter((capability) => !superseded.has(capability));
|
|
@@ -4904,9 +5353,9 @@ function checkManifest(manifest) {
|
|
|
4904
5353
|
// trial-runner/trial.ts
|
|
4905
5354
|
async function readFiles(path) {
|
|
4906
5355
|
const entries = path.endsWith(".zip") ? [...readZip(await readFile4(path))] : await (async () => {
|
|
4907
|
-
const locales = (await readdir3(
|
|
5356
|
+
const locales = (await readdir3(join4(path, LOCALES)).catch(() => [])).filter((name2) => name2.endsWith(".json")).map((name2) => `${LOCALES}/${name2}`);
|
|
4908
5357
|
const names = [...(await readdir3(path)).filter((name2) => /\.m?js$/.test(name2) || name2 === "manifest.json"), ...locales];
|
|
4909
|
-
return Promise.all(names.map(async (name2) => [name2, await readFile4(
|
|
5358
|
+
return Promise.all(names.map(async (name2) => [name2, await readFile4(join4(path, name2))]));
|
|
4910
5359
|
})();
|
|
4911
5360
|
return new Map(entries.map(([name2, data]) => {
|
|
4912
5361
|
if (!name2.endsWith(".json")) return [name2, Buffer.from(data).toString("utf8")];
|
|
@@ -4931,10 +5380,10 @@ async function readPlugin(path) {
|
|
|
4931
5380
|
const manifest = JSON.parse(files2.get("manifest.json") ?? "{}");
|
|
4932
5381
|
const source = files2.get(scripts[0]);
|
|
4933
5382
|
const checked = checkPlugin(source);
|
|
4934
|
-
const capabilities = Array.isArray(manifest.capabilities) || checked.violations.length > 0 ? manifest.capabilities : await loadPlugin(source, { identifier:
|
|
5383
|
+
const capabilities = Array.isArray(manifest.capabilities) || checked.violations.length > 0 ? manifest.capabilities : await loadPlugin(source, { identifier: join4(path, scripts[0]), allowCodeGeneration: !checked.exports.includes("status") }).then((namespace) => capabilitiesOf(namespace).capabilities, () => void 0);
|
|
4935
5384
|
return {
|
|
4936
5385
|
id: manifest.id ?? basename(resolve(path)),
|
|
4937
|
-
file:
|
|
5386
|
+
file: join4(path, scripts[0]),
|
|
4938
5387
|
name: scripts[0],
|
|
4939
5388
|
source,
|
|
4940
5389
|
network: { hosts: manifest.hosts ?? [], userProvidedURLs: manifest.userProvidedURLs ?? false },
|
|
@@ -4944,6 +5393,7 @@ async function readPlugin(path) {
|
|
|
4944
5393
|
language: manifest.language,
|
|
4945
5394
|
dictionaries: dictionariesIn(files2),
|
|
4946
5395
|
exports: checked.exports,
|
|
5396
|
+
capabilities: capabilities ?? [],
|
|
4947
5397
|
// 清单用了内核留用的参数名(contract.md 6.1)和静态检查拦下的一样判红。
|
|
4948
5398
|
violations: [...checked.violations, ...checkManifest({ ...manifest, capabilities }).map((problem) => ` manifest.json: ${problem}`)]
|
|
4949
5399
|
};
|
|
@@ -5008,15 +5458,38 @@ async function runSlotCase(plugin2, testCase) {
|
|
|
5008
5458
|
let outcome;
|
|
5009
5459
|
let written;
|
|
5010
5460
|
const problems = [];
|
|
5461
|
+
const sockets = scriptedSockets(testCase.socket);
|
|
5011
5462
|
const context = await openSlot(plugin2, testCase, replay.fetch, testCase.credentials ?? null, (write) => {
|
|
5012
5463
|
written = write;
|
|
5013
5464
|
}, metadata, files2);
|
|
5014
5465
|
try {
|
|
5015
5466
|
const value = await context.call(testCase.slot, testCase.call ?? "", testCase.args.map(fromJSON));
|
|
5016
|
-
|
|
5017
|
-
|
|
5467
|
+
const contract = contractProblems(testCase, value);
|
|
5468
|
+
problems.push(...contract);
|
|
5469
|
+
if (isSendSession(testCase, value) && contract.length === 0) {
|
|
5470
|
+
if (!plugin2.capabilities.includes("danmakuSendSession")) {
|
|
5471
|
+
throw new DanmakuSendFailure("danmaku send: the plugin returned a send session but does not export danmaku.sendDecode");
|
|
5472
|
+
}
|
|
5473
|
+
await runSendSession({
|
|
5474
|
+
session: value,
|
|
5475
|
+
grants: sendGrants(plugin2, testCase),
|
|
5476
|
+
open: sockets.open,
|
|
5477
|
+
decode: async (frame2, state) => {
|
|
5478
|
+
const step = await context.call("danmaku", "sendDecode", [frame2, state]);
|
|
5479
|
+
const stepProblems = contractProblems({ slot: "danmaku", call: "sendDecode" }, step);
|
|
5480
|
+
if (stepProblems.length > 0) {
|
|
5481
|
+
problems.push(...stepProblems);
|
|
5482
|
+
throw new DanmakuSendFailure("danmaku send: sendDecode returned a step outside the contract");
|
|
5483
|
+
}
|
|
5484
|
+
return step;
|
|
5485
|
+
}
|
|
5486
|
+
});
|
|
5487
|
+
outcome = { output: null };
|
|
5488
|
+
} else {
|
|
5489
|
+
outcome = { output: toJSON(value) };
|
|
5490
|
+
}
|
|
5018
5491
|
} catch (error) {
|
|
5019
|
-
if (!isCallFailure(error)) throw error;
|
|
5492
|
+
if (!isCallFailure(error) && !(error instanceof DanmakuSendFailure)) throw error;
|
|
5020
5493
|
outcome = { error: structuredError(error), message: describeError(error) };
|
|
5021
5494
|
} finally {
|
|
5022
5495
|
context.close();
|
|
@@ -5025,7 +5498,8 @@ async function runSlotCase(plugin2, testCase) {
|
|
|
5025
5498
|
...replay.unrecorded.map((request) => `unrecorded request ${request}`),
|
|
5026
5499
|
...replay.pending.map(({ request }) => `recorded request not sent ${request.method} ${request.url}`),
|
|
5027
5500
|
...metadata.problems(),
|
|
5028
|
-
...files2.problems()
|
|
5501
|
+
...files2.problems(),
|
|
5502
|
+
...sockets.problems()
|
|
5029
5503
|
);
|
|
5030
5504
|
if ("output" in outcome) {
|
|
5031
5505
|
if (testCase.error !== void 0) problems.push(`should throw ${canonical(testCase.error)}, returned ${canonical(outcome.output)}`);
|
|
@@ -5048,6 +5522,22 @@ async function runSlotCase(plugin2, testCase) {
|
|
|
5048
5522
|
}
|
|
5049
5523
|
return problems.length === 0 ? { status: "passed" } : { status: "failed", reason: problems.join("\n ") };
|
|
5050
5524
|
}
|
|
5525
|
+
function isSendSession(testCase, value) {
|
|
5526
|
+
return testCase.slot === "danmaku" && testCase.call === "send" && value !== null && value !== void 0;
|
|
5527
|
+
}
|
|
5528
|
+
function sendGrants(plugin2, testCase) {
|
|
5529
|
+
const parameters = testCase.parameters ?? null;
|
|
5530
|
+
const user = plugin2.network.userProvidedURLs === true && parameters !== null;
|
|
5531
|
+
return {
|
|
5532
|
+
declared: plugin2.network.hosts,
|
|
5533
|
+
user: user ? userHostsOf(plugin2, parameters) : [],
|
|
5534
|
+
userAddresses: user ? userAddressesOf(plugin2, parameters) : [],
|
|
5535
|
+
secrets: [
|
|
5536
|
+
...credentialValues(testCase.credentials ?? null),
|
|
5537
|
+
...plugin2.parameters.filter((parameter) => parameter.secret).flatMap((parameter) => parameters !== null && Object.hasOwn(parameters, parameter.name) && parameters[parameter.name] !== "" ? [parameters[parameter.name]] : [])
|
|
5538
|
+
]
|
|
5539
|
+
};
|
|
5540
|
+
}
|
|
5051
5541
|
function report(results, log) {
|
|
5052
5542
|
const counts = { passed: 0, skipped: 0, failed: 0 };
|
|
5053
5543
|
for (const { name: name2, outcome } of results) {
|
|
@@ -5086,6 +5576,9 @@ async function recordCommand(pluginDirectory, { slot, call: callName, args, cred
|
|
|
5086
5576
|
const problems = contractProblems({ slot, call: callName }, value);
|
|
5087
5577
|
if (problems.length > 0) throw new RecordRefused(`the plugin's output does not match the contract, nothing was recorded:
|
|
5088
5578
|
${problems.join("\n ")}`);
|
|
5579
|
+
if (isSendSession({ slot, call: callName }, value)) {
|
|
5580
|
+
throw new RecordRefused("danmaku.send returned a send session; record does not open sockets, write the case's socket script by hand (see the skill's references/cases.md, section socket)");
|
|
5581
|
+
}
|
|
5089
5582
|
return { output: toJSON(value) };
|
|
5090
5583
|
};
|
|
5091
5584
|
await settle(invokeSlot(plugin2, { slot, call: callName, args, parameters, appKeys, output: null }, recording.fetch, credentials));
|
|
@@ -5238,14 +5731,14 @@ if (false) {
|
|
|
5238
5731
|
}
|
|
5239
5732
|
|
|
5240
5733
|
// packer/pack.ts
|
|
5241
|
-
var TEMPLATES =
|
|
5242
|
-
var SAMPLES =
|
|
5243
|
-
var SAMPLE_PACKAGES =
|
|
5734
|
+
var TEMPLATES = fileURLToPath3(new URL("../templates/", import.meta.url));
|
|
5735
|
+
var SAMPLES = fileURLToPath3(new URL("../samples/src/", import.meta.url));
|
|
5736
|
+
var SAMPLE_PACKAGES = fileURLToPath3(new URL("../samples/dist/", import.meta.url));
|
|
5244
5737
|
var ICON = "icon.png";
|
|
5245
5738
|
var PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
5246
5739
|
var LOCALES2 = "locales";
|
|
5247
5740
|
async function dictionaries(directory) {
|
|
5248
|
-
const files2 = (await readdir4(
|
|
5741
|
+
const files2 = (await readdir4(join5(directory, LOCALES2)).catch(() => [])).sort();
|
|
5249
5742
|
const problems = [];
|
|
5250
5743
|
const tags = /* @__PURE__ */ new Map();
|
|
5251
5744
|
const result = [];
|
|
@@ -5261,7 +5754,7 @@ async function dictionaries(directory) {
|
|
|
5261
5754
|
continue;
|
|
5262
5755
|
}
|
|
5263
5756
|
tags.set(tag, file);
|
|
5264
|
-
const data = await readFile5(
|
|
5757
|
+
const data = await readFile5(join5(directory, LOCALES2, file));
|
|
5265
5758
|
bytes2 += data.length;
|
|
5266
5759
|
let table;
|
|
5267
5760
|
try {
|
|
@@ -5309,12 +5802,12 @@ async function pack(directory, warn = () => {
|
|
|
5309
5802
|
const source = result.outputFiles[0].text;
|
|
5310
5803
|
const { exports, violations } = checkPlugin(source);
|
|
5311
5804
|
if (violations.length > 0) throw new Error(`${directory}: ${violations.join("; ")}`);
|
|
5312
|
-
const namespace = await loadPlugin(source, { identifier:
|
|
5805
|
+
const namespace = await loadPlugin(source, { identifier: join5(directory, "plugin.js"), allowCodeGeneration: !exports.includes("status") });
|
|
5313
5806
|
const { capabilities, problems } = capabilitiesOf(namespace);
|
|
5314
5807
|
if (capabilities.length === 0 && problems.length === 0) problems.push("exports no capability; a plugin exports at least one slot or resolve");
|
|
5315
5808
|
if (problems.length > 0) throw new Error(`${directory}: ${problems.join("; ")}`);
|
|
5316
5809
|
for (const name2 of notYetUsedExports(namespace)) warn(`WARN ${directory}: exports ${name2}, which this version of the App does not call yet (App \u5C1A\u672A\u4F7F\u7528)`);
|
|
5317
|
-
const { default: metadata } = await import(pathToFileURL(
|
|
5810
|
+
const { default: metadata } = await import(pathToFileURL(join5(directory, "manifest.ts")).href);
|
|
5318
5811
|
if (metadata.interfaceLevel > CONTRACT.interfaceLevel || metadata.contractRevision !== CONTRACT.revision) {
|
|
5319
5812
|
throw new Error(`${directory}/manifest.ts targets interface level ${metadata.interfaceLevel} revision ${metadata.contractRevision}; the contract is level ${CONTRACT.interfaceLevel} revision ${CONTRACT.revision}`);
|
|
5320
5813
|
}
|
|
@@ -5329,7 +5822,7 @@ async function pack(directory, warn = () => {
|
|
|
5329
5822
|
throw new Error(`${directory}/manifest.ts: language ${metadata.language} is not a language tag (for example zh-Hans, en, ja)`);
|
|
5330
5823
|
}
|
|
5331
5824
|
const manifest = { ...metadata, ...language === void 0 ? {} : { language }, capabilities };
|
|
5332
|
-
const icon = await readFile5(
|
|
5825
|
+
const icon = await readFile5(join5(directory, ICON)).catch(() => void 0);
|
|
5333
5826
|
if (icon !== void 0 && !icon.subarray(0, 8).equals(PNG_SIGNATURE)) throw new Error(`${directory}/${ICON} is not a PNG`);
|
|
5334
5827
|
return writeZip([
|
|
5335
5828
|
{ name: "manifest.json", data: Buffer.from(`${JSON.stringify(manifest, null, 2)}
|
|
@@ -5351,20 +5844,20 @@ if (false) {
|
|
|
5351
5844
|
}
|
|
5352
5845
|
|
|
5353
5846
|
// package/cli.ts
|
|
5354
|
-
var here2 = dirname2(
|
|
5847
|
+
var here2 = dirname2(fileURLToPath4(import.meta.url));
|
|
5355
5848
|
var ROOT = basename2(here2) === "dist" ? dirname2(here2) : here2;
|
|
5356
5849
|
var PACKAGED = basename2(here2) === "dist";
|
|
5357
|
-
if (PACKAGED) useKernelDirectory(
|
|
5358
|
-
var TEMPLATES2 = PACKAGED ?
|
|
5359
|
-
var SKILL =
|
|
5850
|
+
if (PACKAGED) useKernelDirectory(join6(ROOT, "kernel"));
|
|
5851
|
+
var TEMPLATES2 = PACKAGED ? join6(ROOT, "templates") : join6(ROOT, "../templates");
|
|
5852
|
+
var SKILL = join6(ROOT, "skill/krillplayer-plugin");
|
|
5360
5853
|
var PACKAGE_NAME = "krillplayer-plugin";
|
|
5361
5854
|
function plugin(directory) {
|
|
5362
5855
|
const root = resolve3(directory ?? ".");
|
|
5363
|
-
return { root, cases:
|
|
5856
|
+
return { root, cases: join6(root, "cases"), zip: join6(root, "dist", `${basename2(root)}.zip`) };
|
|
5364
5857
|
}
|
|
5365
5858
|
function requirePlugin(directory) {
|
|
5366
5859
|
for (const file of ["plugin.ts", "manifest.ts"]) {
|
|
5367
|
-
if (!existsSync(
|
|
5860
|
+
if (!existsSync(join6(directory, file))) throw new UsageError(`${directory} has no ${file}; run this in a plugin directory, or npx ${PACKAGE_NAME} new <dir>`);
|
|
5368
5861
|
}
|
|
5369
5862
|
}
|
|
5370
5863
|
async function bundle(directory, out, log) {
|
|
@@ -5403,38 +5896,38 @@ var COMPILER_OPTIONS = {
|
|
|
5403
5896
|
};
|
|
5404
5897
|
var SCAFFOLD_TSCONFIG = { compilerOptions: COMPILER_OPTIONS, include: ["plugin.ts", "manifest.ts", "src/**/*.ts"] };
|
|
5405
5898
|
async function fallbackConfig(directory) {
|
|
5406
|
-
const config =
|
|
5407
|
-
await writeFile3(config, JSON.stringify({ compilerOptions: COMPILER_OPTIONS, files: ["plugin.ts", "manifest.ts"].map((file) =>
|
|
5899
|
+
const config = join6(await mkdtemp2(join6(tmpdir(), "krillplayer-plugin-tsconfig-")), "tsconfig.json");
|
|
5900
|
+
await writeFile3(config, JSON.stringify({ compilerOptions: COMPILER_OPTIONS, files: ["plugin.ts", "manifest.ts"].map((file) => join6(directory, file)) }));
|
|
5408
5901
|
return config;
|
|
5409
5902
|
}
|
|
5410
5903
|
async function newCommand(directory, { id, name: name2, template = "apple-hls" }, log) {
|
|
5411
5904
|
const target = resolve3(directory);
|
|
5412
5905
|
if (existsSync(target) && (await readdir5(target)).length > 0) throw new UsageError(`${target} already exists and is not empty`);
|
|
5413
|
-
const source =
|
|
5906
|
+
const source = join6(TEMPLATES2, template);
|
|
5414
5907
|
if (!existsSync(source)) throw new UsageError(`no template ${template}; templates: ${(await readdir5(TEMPLATES2)).join(", ")}`);
|
|
5415
5908
|
const slug = basename2(target).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "plugin";
|
|
5416
5909
|
const pluginId = id ?? `com.example.${slug}`;
|
|
5417
5910
|
await cp(source, target, { recursive: true });
|
|
5418
5911
|
for (const file of ["plugin.ts", "manifest.ts"]) {
|
|
5419
|
-
await writeFile3(
|
|
5912
|
+
await writeFile3(join6(target, file), rewriteImports(await readFile6(join6(target, file), "utf8")));
|
|
5420
5913
|
}
|
|
5421
|
-
const manifest = (await readFile6(
|
|
5422
|
-
await writeFile3(
|
|
5423
|
-
const { version } = JSON.parse(await readFile6(
|
|
5424
|
-
await writeFile3(
|
|
5914
|
+
const manifest = (await readFile6(join6(target, "manifest.ts"), "utf8")).replace(/id: "[^"]*"/, () => `id: ${JSON.stringify(pluginId)}`).replace(/sourceId: "[^"]*"/, () => `sourceId: ${JSON.stringify(slug)}`).replace(/name: "[^"]*"/, () => `name: ${JSON.stringify(name2 ?? basename2(target))}`).replace(/author: "[^"]*"/, () => `author: "<your name>"`).replace(/version: "[^"]*"/, () => `version: "0.1.0"`);
|
|
5915
|
+
await writeFile3(join6(target, "manifest.ts"), manifest);
|
|
5916
|
+
const { version } = JSON.parse(await readFile6(join6(ROOT, "package.json"), "utf8"));
|
|
5917
|
+
await writeFile3(join6(target, "package.json"), `${JSON.stringify(SCAFFOLD_PACKAGE(slug, version), null, 2)}
|
|
5425
5918
|
`);
|
|
5426
|
-
await writeFile3(
|
|
5919
|
+
await writeFile3(join6(target, "tsconfig.json"), `${JSON.stringify(SCAFFOLD_TSCONFIG, null, 2)}
|
|
5427
5920
|
`);
|
|
5428
|
-
await writeFile3(
|
|
5921
|
+
await writeFile3(join6(target, ".gitignore"), "node_modules/\ndist/\n");
|
|
5429
5922
|
log(`created ${target} from the ${template} template (contract revision ${CONTRACT.revision})`);
|
|
5430
5923
|
log(`next: cd ${relative3(process.cwd(), target) || "."} && npm install && npx ${PACKAGE_NAME} check`);
|
|
5431
5924
|
}
|
|
5432
5925
|
function tsc(directory) {
|
|
5433
|
-
for (const from of [
|
|
5926
|
+
for (const from of [join6(directory, "package.json"), join6(ROOT, "package.json")]) {
|
|
5434
5927
|
try {
|
|
5435
5928
|
const manifest = createRequire2(from).resolve("typescript/package.json");
|
|
5436
|
-
const bin = JSON.parse(
|
|
5437
|
-
return
|
|
5929
|
+
const bin = JSON.parse(readFileSync2(manifest, "utf8")).bin?.tsc ?? "bin/tsc";
|
|
5930
|
+
return join6(dirname2(manifest), bin);
|
|
5438
5931
|
} catch {
|
|
5439
5932
|
}
|
|
5440
5933
|
}
|
|
@@ -5446,7 +5939,7 @@ async function typecheck(directory, log) {
|
|
|
5446
5939
|
log(`FAIL typecheck: typescript is not installed; run npm install --save-dev typescript in ${directory}`);
|
|
5447
5940
|
return false;
|
|
5448
5941
|
}
|
|
5449
|
-
const own = existsSync(
|
|
5942
|
+
const own = existsSync(join6(directory, "tsconfig.json"));
|
|
5450
5943
|
const config = own ? directory : await fallbackConfig(directory);
|
|
5451
5944
|
try {
|
|
5452
5945
|
await promisify2(execFile2)(process.execPath, [compiler, "--noEmit", "-p", config], { cwd: directory, maxBuffer: 16 << 20 });
|