browserpaw 5.0.0 → 5.0.1
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/dist/v5/cli.cjs +1207 -335
- package/dist/v5/extension/background.js +8649 -6972
- package/dist/v5/extension/browserpaw-build.json +52 -45
- package/dist/v5/extension/chunks/{popup-DjxqduQH.js → popup-BSqTH70w.js} +39 -6
- package/dist/v5/extension/content-scripts/agent-cursor.js +30 -14
- package/dist/v5/extension/inject-scripts/accessibility-tree-helper.js +377 -229
- package/dist/v5/extension/inject-scripts/keyboard-helper.js +12 -2
- package/dist/v5/extension/inject-scripts/network-helper.js +166 -1
- package/dist/v5/extension/inject-scripts/screenshot-helper.js +397 -356
- package/dist/v5/extension/inpage-engine.js +7070 -6933
- package/dist/v5/extension/manifest.json +1 -1
- package/dist/v5/extension/popup.html +1 -1
- package/dist/v5/host.cjs +445 -73
- package/dist/v5/recipes/bilibili-popular-videos.json +364 -0
- package/dist/v5/recipes/bilibili-video-detail.json +395 -0
- package/dist/v5/recipes/github-issue-thread.json +313 -0
- package/dist/v5/recipes/github-releases.json +86 -45
- package/dist/v5/recipes/github-repo-readme.json +306 -0
- package/dist/v5/recipes/github-repo-search.json +313 -0
- package/dist/v5/recipes/mdn-search.json +93 -48
- package/dist/v5/recipes/npm-compare.json +6 -4
- package/dist/v5/recipes/reddit-post-read.json +289 -0
- package/dist/v5/recipes/reddit-subreddit-posts.json +331 -0
- package/dist/v5/recipes/v2ex-node-topics.json +334 -0
- package/dist/v5/recipes/v2ex-topic-read.json +325 -0
- package/dist/v5/recipes/x-profile-posts.json +332 -0
- package/dist/v5/recipes/x-search-thread.json +13 -41
- package/dist/v5/recipes/x-visible-thread.json +40 -83
- package/dist/v5/recipes/youtube-search-videos.json +279 -0
- package/dist/v5/recipes/youtube-video-read.json +349 -0
- package/dist/v5/skill/browserpaw/SKILL.md +4 -4
- package/dist/v5/skill/browserpaw/assets/recipes/bilibili-popular-videos.json +364 -0
- package/dist/v5/skill/browserpaw/assets/recipes/bilibili-video-detail.json +395 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-issue-thread.json +313 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-releases.json +86 -45
- package/dist/v5/skill/browserpaw/assets/recipes/github-repo-readme.json +306 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-repo-search.json +313 -0
- package/dist/v5/skill/browserpaw/assets/recipes/mdn-search.json +93 -48
- package/dist/v5/skill/browserpaw/assets/recipes/npm-compare.json +6 -4
- package/dist/v5/skill/browserpaw/assets/recipes/reddit-post-read.json +289 -0
- package/dist/v5/skill/browserpaw/assets/recipes/reddit-subreddit-posts.json +331 -0
- package/dist/v5/skill/browserpaw/assets/recipes/v2ex-node-topics.json +334 -0
- package/dist/v5/skill/browserpaw/assets/recipes/v2ex-topic-read.json +325 -0
- package/dist/v5/skill/browserpaw/assets/recipes/x-profile-posts.json +332 -0
- package/dist/v5/skill/browserpaw/assets/recipes/x-search-thread.json +13 -41
- package/dist/v5/skill/browserpaw/assets/recipes/x-visible-thread.json +40 -83
- package/dist/v5/skill/browserpaw/assets/recipes/youtube-search-videos.json +279 -0
- package/dist/v5/skill/browserpaw/assets/recipes/youtube-video-read.json +349 -0
- package/dist/v5/skill/browserpaw/references/recipe-contract.md +4 -3
- package/package.json +1 -1
package/dist/v5/cli.cjs
CHANGED
|
@@ -110,6 +110,14 @@ function isNewerVersion(remoteVersion, currentVersion) {
|
|
|
110
110
|
if (!remote || !current) return false;
|
|
111
111
|
return compareSemver(remote.raw, current.raw) > 0;
|
|
112
112
|
}
|
|
113
|
+
function isCompatibleExtensionVersion(extensionVersion, cliVersion = CURRENT_VERSION, minVersion = MIN_COMPATIBLE_EXTENSION_VERSION) {
|
|
114
|
+
const extension = parseSemver(extensionVersion);
|
|
115
|
+
const cli = parseSemver(cliVersion);
|
|
116
|
+
const floor = parseSemver(minVersion);
|
|
117
|
+
if (!extension || !cli || !floor) return false;
|
|
118
|
+
if (extension.major !== cli.major) return false;
|
|
119
|
+
return compareSemver(extension.raw, floor.raw) >= 0;
|
|
120
|
+
}
|
|
113
121
|
function isProfileCommand(name) {
|
|
114
122
|
return /^(history|bookmark|downloads|cookies|window|group) /.test(name) || name === "tab move";
|
|
115
123
|
}
|
|
@@ -246,7 +254,7 @@ function commandHelp(topic) {
|
|
|
246
254
|
)
|
|
247
255
|
].join("\n");
|
|
248
256
|
}
|
|
249
|
-
var DEFAULT_SERVER_PORT, HOST_NAME, TASK_ID, EXPLICIT_TARGET_SCHEMA, CAPTURE_NETWORK_SCHEMA, BROWSER_DATA_SCHEMA, TOOL_NAMES, schemas, TOOL_SCHEMAS, GITHUB_REPO_OWNER, GITHUB_REPO_NAME, GITHUB_REPO_URL, GITHUB_API_LATEST_RELEASE_URL, CURRENT_VERSION, DEFAULT_CACHE_TTL_MS, DEFAULT_MAX_SLIDING_WINDOW_MS, MemoryVersionCacheStorage, defaultMemoryCacheStorage, dataCommands, debugCommands, pageCommands, COMMANDS, globalOptions, repeated, isCommand, TARGET_HELP, examples, commandTopics, exitHelp;
|
|
257
|
+
var DEFAULT_SERVER_PORT, HOST_NAME, TASK_ID, EXPLICIT_TARGET_SCHEMA, CAPTURE_NETWORK_SCHEMA, BROWSER_DATA_SCHEMA, TOOL_NAMES, schemas, TOOL_SCHEMAS, GITHUB_REPO_OWNER, GITHUB_REPO_NAME, GITHUB_REPO_URL, GITHUB_API_LATEST_RELEASE_URL, CURRENT_VERSION, MIN_COMPATIBLE_EXTENSION_VERSION, DEFAULT_CACHE_TTL_MS, DEFAULT_MAX_SLIDING_WINDOW_MS, MemoryVersionCacheStorage, defaultMemoryCacheStorage, dataCommands, debugCommands, pageCommands, COMMANDS, globalOptions, repeated, isCommand, TARGET_HELP, examples, commandTopics, exitHelp;
|
|
250
258
|
var init_dist = __esm({
|
|
251
259
|
"../../packages/shared/dist/index.mjs"() {
|
|
252
260
|
"use strict";
|
|
@@ -901,6 +909,8 @@ var init_dist = __esm({
|
|
|
901
909
|
},
|
|
902
910
|
x: { type: "number" },
|
|
903
911
|
y: { type: "number" },
|
|
912
|
+
coordinateSpace: { type: "string", enum: ["viewport", "screenshot"] },
|
|
913
|
+
captureId: { type: "string" },
|
|
904
914
|
at: { type: "number" },
|
|
905
915
|
direction: { type: "string", enum: ["up", "down", "left", "right"] },
|
|
906
916
|
amount: { type: "number" },
|
|
@@ -948,6 +958,8 @@ var init_dist = __esm({
|
|
|
948
958
|
quiescenceTimeoutMs: { type: "number" },
|
|
949
959
|
includeDelta: { type: "boolean" },
|
|
950
960
|
sessionId: { type: "string" },
|
|
961
|
+
coordinateSpace: { type: "string", enum: ["viewport", "screenshot"] },
|
|
962
|
+
captureId: { type: "string" },
|
|
951
963
|
captureNetwork: CAPTURE_NETWORK_SCHEMA
|
|
952
964
|
},
|
|
953
965
|
required: ["actions"]
|
|
@@ -1075,7 +1087,8 @@ var init_dist = __esm({
|
|
|
1075
1087
|
GITHUB_REPO_NAME = "browserpaw";
|
|
1076
1088
|
GITHUB_REPO_URL = `https://github.com/${GITHUB_REPO_OWNER}/${GITHUB_REPO_NAME}`;
|
|
1077
1089
|
GITHUB_API_LATEST_RELEASE_URL = `https://api.github.com/repos/${GITHUB_REPO_OWNER}/${GITHUB_REPO_NAME}/releases/latest`;
|
|
1078
|
-
CURRENT_VERSION = "5.0.
|
|
1090
|
+
CURRENT_VERSION = "5.0.1";
|
|
1091
|
+
MIN_COMPATIBLE_EXTENSION_VERSION = "5.0.0";
|
|
1079
1092
|
DEFAULT_CACHE_TTL_MS = 60 * 60 * 1e3;
|
|
1080
1093
|
DEFAULT_MAX_SLIDING_WINDOW_MS = 24 * 60 * 60 * 1e3;
|
|
1081
1094
|
MemoryVersionCacheStorage = class {
|
|
@@ -1582,31 +1595,178 @@ var init_dist = __esm({
|
|
|
1582
1595
|
}
|
|
1583
1596
|
});
|
|
1584
1597
|
|
|
1585
|
-
// src/
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
throw new CommandError(`Command ${name} is unavailable inside a batch.`);
|
|
1589
|
-
}
|
|
1590
|
-
var CommandError, STANDALONE_COMMANDS;
|
|
1591
|
-
var init_command_utils = __esm({
|
|
1592
|
-
"src/command-utils.ts"() {
|
|
1598
|
+
// src/scripts/constant.ts
|
|
1599
|
+
var init_constant = __esm({
|
|
1600
|
+
"src/scripts/constant.ts"() {
|
|
1593
1601
|
"use strict";
|
|
1594
1602
|
init_dist();
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
"
|
|
1605
|
-
|
|
1606
|
-
"
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
|
|
1606
|
+
// src/scripts/browser-config.ts
|
|
1607
|
+
function getUserManifestPathForBrowser(browser) {
|
|
1608
|
+
const platform2 = os.platform();
|
|
1609
|
+
if (platform2 === "win32") {
|
|
1610
|
+
const appData = process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming");
|
|
1611
|
+
switch (browser) {
|
|
1612
|
+
case "chrome" /* CHROME */:
|
|
1613
|
+
return path.join(appData, "Google", "Chrome", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
1614
|
+
case "chromium" /* CHROMIUM */:
|
|
1615
|
+
return path.join(appData, "Chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
1616
|
+
default:
|
|
1617
|
+
return path.join(appData, "Google", "Chrome", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
1618
|
+
}
|
|
1619
|
+
} else if (platform2 === "darwin") {
|
|
1620
|
+
const home = os.homedir();
|
|
1621
|
+
switch (browser) {
|
|
1622
|
+
case "chrome" /* CHROME */:
|
|
1623
|
+
return path.join(
|
|
1624
|
+
home,
|
|
1625
|
+
"Library",
|
|
1626
|
+
"Application Support",
|
|
1627
|
+
"Google",
|
|
1628
|
+
"Chrome",
|
|
1629
|
+
"NativeMessagingHosts",
|
|
1630
|
+
`${HOST_NAME}.json`
|
|
1631
|
+
);
|
|
1632
|
+
case "chromium" /* CHROMIUM */:
|
|
1633
|
+
return path.join(
|
|
1634
|
+
home,
|
|
1635
|
+
"Library",
|
|
1636
|
+
"Application Support",
|
|
1637
|
+
"Chromium",
|
|
1638
|
+
"NativeMessagingHosts",
|
|
1639
|
+
`${HOST_NAME}.json`
|
|
1640
|
+
);
|
|
1641
|
+
default:
|
|
1642
|
+
return path.join(
|
|
1643
|
+
home,
|
|
1644
|
+
"Library",
|
|
1645
|
+
"Application Support",
|
|
1646
|
+
"Google",
|
|
1647
|
+
"Chrome",
|
|
1648
|
+
"NativeMessagingHosts",
|
|
1649
|
+
`${HOST_NAME}.json`
|
|
1650
|
+
);
|
|
1651
|
+
}
|
|
1652
|
+
} else {
|
|
1653
|
+
const home = os.homedir();
|
|
1654
|
+
switch (browser) {
|
|
1655
|
+
case "chrome" /* CHROME */:
|
|
1656
|
+
return path.join(
|
|
1657
|
+
home,
|
|
1658
|
+
".config",
|
|
1659
|
+
"google-chrome",
|
|
1660
|
+
"NativeMessagingHosts",
|
|
1661
|
+
`${HOST_NAME}.json`
|
|
1662
|
+
);
|
|
1663
|
+
case "chromium" /* CHROMIUM */:
|
|
1664
|
+
return path.join(home, ".config", "chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
1665
|
+
default:
|
|
1666
|
+
return path.join(
|
|
1667
|
+
home,
|
|
1668
|
+
".config",
|
|
1669
|
+
"google-chrome",
|
|
1670
|
+
"NativeMessagingHosts",
|
|
1671
|
+
`${HOST_NAME}.json`
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
function getSystemManifestPathForBrowser(browser) {
|
|
1677
|
+
const platform2 = os.platform();
|
|
1678
|
+
if (platform2 === "win32") {
|
|
1679
|
+
const programFiles = process.env.ProgramFiles || "C:\\Program Files";
|
|
1680
|
+
switch (browser) {
|
|
1681
|
+
case "chrome" /* CHROME */:
|
|
1682
|
+
return path.join(
|
|
1683
|
+
programFiles,
|
|
1684
|
+
"Google",
|
|
1685
|
+
"Chrome",
|
|
1686
|
+
"NativeMessagingHosts",
|
|
1687
|
+
`${HOST_NAME}.json`
|
|
1688
|
+
);
|
|
1689
|
+
case "chromium" /* CHROMIUM */:
|
|
1690
|
+
return path.join(programFiles, "Chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
1691
|
+
default:
|
|
1692
|
+
return path.join(
|
|
1693
|
+
programFiles,
|
|
1694
|
+
"Google",
|
|
1695
|
+
"Chrome",
|
|
1696
|
+
"NativeMessagingHosts",
|
|
1697
|
+
`${HOST_NAME}.json`
|
|
1698
|
+
);
|
|
1699
|
+
}
|
|
1700
|
+
} else if (platform2 === "darwin") {
|
|
1701
|
+
switch (browser) {
|
|
1702
|
+
case "chrome" /* CHROME */:
|
|
1703
|
+
return path.join(
|
|
1704
|
+
"/Library",
|
|
1705
|
+
"Google",
|
|
1706
|
+
"Chrome",
|
|
1707
|
+
"NativeMessagingHosts",
|
|
1708
|
+
`${HOST_NAME}.json`
|
|
1709
|
+
);
|
|
1710
|
+
case "chromium" /* CHROMIUM */:
|
|
1711
|
+
return path.join(
|
|
1712
|
+
"/Library",
|
|
1713
|
+
"Application Support",
|
|
1714
|
+
"Chromium",
|
|
1715
|
+
"NativeMessagingHosts",
|
|
1716
|
+
`${HOST_NAME}.json`
|
|
1717
|
+
);
|
|
1718
|
+
default:
|
|
1719
|
+
return path.join(
|
|
1720
|
+
"/Library",
|
|
1721
|
+
"Google",
|
|
1722
|
+
"Chrome",
|
|
1723
|
+
"NativeMessagingHosts",
|
|
1724
|
+
`${HOST_NAME}.json`
|
|
1725
|
+
);
|
|
1726
|
+
}
|
|
1727
|
+
} else {
|
|
1728
|
+
switch (browser) {
|
|
1729
|
+
case "chrome" /* CHROME */:
|
|
1730
|
+
return path.join("/etc", "opt", "chrome", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
1731
|
+
case "chromium" /* CHROMIUM */:
|
|
1732
|
+
return path.join("/etc", "chromium", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
1733
|
+
default:
|
|
1734
|
+
return path.join("/etc", "opt", "chrome", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
function getRegistryKeys(browser) {
|
|
1739
|
+
if (os.platform() !== "win32") return void 0;
|
|
1740
|
+
const browserPaths = {
|
|
1741
|
+
["chrome" /* CHROME */]: {
|
|
1742
|
+
user: `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`,
|
|
1743
|
+
system: `HKLM\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`
|
|
1744
|
+
},
|
|
1745
|
+
["chromium" /* CHROMIUM */]: {
|
|
1746
|
+
user: `HKCU\\Software\\Chromium\\NativeMessagingHosts\\${HOST_NAME}`,
|
|
1747
|
+
system: `HKLM\\Software\\Chromium\\NativeMessagingHosts\\${HOST_NAME}`
|
|
1748
|
+
}
|
|
1749
|
+
};
|
|
1750
|
+
return browserPaths[browser];
|
|
1751
|
+
}
|
|
1752
|
+
function getBrowserConfig(browser) {
|
|
1753
|
+
const registryKeys = getRegistryKeys(browser);
|
|
1754
|
+
return {
|
|
1755
|
+
type: browser,
|
|
1756
|
+
displayName: browser.charAt(0).toUpperCase() + browser.slice(1),
|
|
1757
|
+
userManifestPath: getUserManifestPathForBrowser(browser),
|
|
1758
|
+
systemManifestPath: getSystemManifestPathForBrowser(browser),
|
|
1759
|
+
registryKey: registryKeys?.user,
|
|
1760
|
+
systemRegistryKey: registryKeys?.system
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
var os, path;
|
|
1764
|
+
var init_browser_config = __esm({
|
|
1765
|
+
"src/scripts/browser-config.ts"() {
|
|
1766
|
+
"use strict";
|
|
1767
|
+
os = __toESM(require("os"));
|
|
1768
|
+
path = __toESM(require("path"));
|
|
1769
|
+
init_constant();
|
|
1610
1770
|
}
|
|
1611
1771
|
});
|
|
1612
1772
|
|
|
@@ -1615,7 +1775,7 @@ var require_package = __commonJS({
|
|
|
1615
1775
|
"package.json"(exports2, module2) {
|
|
1616
1776
|
module2.exports = {
|
|
1617
1777
|
name: "browserpaw",
|
|
1618
|
-
version: "5.0.
|
|
1778
|
+
version: "5.0.1",
|
|
1619
1779
|
description: "Command-line browser automation through the BrowserPaw Chrome extension",
|
|
1620
1780
|
author: "hangye",
|
|
1621
1781
|
repository: {
|
|
@@ -1767,7 +1927,7 @@ function formatFrontmatterDescription(base, entries) {
|
|
|
1767
1927
|
}
|
|
1768
1928
|
function renderLearnedSection(entries) {
|
|
1769
1929
|
if (!entries.length) return "";
|
|
1770
|
-
const visible = entries.slice(0,
|
|
1930
|
+
const visible = entries.slice(0, 30);
|
|
1771
1931
|
const rows = visible.map((entry) => {
|
|
1772
1932
|
const sites = entry.domains.length ? entry.domains.slice(0, 3).map((d) => `\`${d}\``).join(", ") : "-";
|
|
1773
1933
|
const summary = escapeMarkdownCell(entry.description || entry.name).slice(0, 110);
|
|
@@ -1992,13 +2152,179 @@ var init_skill_install = __esm({
|
|
|
1992
2152
|
}
|
|
1993
2153
|
});
|
|
1994
2154
|
|
|
2155
|
+
// src/command-utils.ts
|
|
2156
|
+
function assertBatchStep({ name, options }) {
|
|
2157
|
+
if (STANDALONE_COMMANDS.has(name) || isProfileCommand(name) || name.startsWith("recipe ") || options.file || options.input || name !== "download" && options.out !== void 0)
|
|
2158
|
+
throw new CommandError(`Command ${name} is unavailable inside a batch.`);
|
|
2159
|
+
}
|
|
2160
|
+
var CommandError, STANDALONE_COMMANDS;
|
|
2161
|
+
var init_command_utils = __esm({
|
|
2162
|
+
"src/command-utils.ts"() {
|
|
2163
|
+
"use strict";
|
|
2164
|
+
init_dist();
|
|
2165
|
+
CommandError = class extends Error {
|
|
2166
|
+
constructor(message, code = "invalid_arguments") {
|
|
2167
|
+
super(message);
|
|
2168
|
+
this.code = code;
|
|
2169
|
+
}
|
|
2170
|
+
};
|
|
2171
|
+
STANDALONE_COMMANDS = /* @__PURE__ */ new Set([
|
|
2172
|
+
"setup",
|
|
2173
|
+
"status",
|
|
2174
|
+
"uninstall",
|
|
2175
|
+
"help",
|
|
2176
|
+
"search",
|
|
2177
|
+
"batch",
|
|
2178
|
+
"task resume"
|
|
2179
|
+
]);
|
|
2180
|
+
}
|
|
2181
|
+
});
|
|
2182
|
+
|
|
2183
|
+
// src/skill-read-program.ts
|
|
2184
|
+
function programHash(code) {
|
|
2185
|
+
return (0, import_node_crypto2.createHash)("sha256").update(code).digest("hex");
|
|
2186
|
+
}
|
|
2187
|
+
var import_node_crypto2, DOM_EXTRACTION_PROGRAM_ALIAS, DOM_EXTRACTION_MAX_MATCHES, DOM_EXTRACTION_MAX_VALUE_CHARS, DOM_EXTRACTION_PROGRAM, DOM_EXTRACTION_PROGRAM_HASH, PUBLISHED_READ_ONLY_PROGRAMS, PREVIOUS_PUBLISHED_READ_ONLY_PROGRAMS;
|
|
2188
|
+
var init_skill_read_program = __esm({
|
|
2189
|
+
"src/skill-read-program.ts"() {
|
|
2190
|
+
"use strict";
|
|
2191
|
+
import_node_crypto2 = require("crypto");
|
|
2192
|
+
DOM_EXTRACTION_PROGRAM_ALIAS = "dom-extract";
|
|
2193
|
+
DOM_EXTRACTION_MAX_MATCHES = 250;
|
|
2194
|
+
DOM_EXTRACTION_MAX_VALUE_CHARS = 1e4;
|
|
2195
|
+
DOM_EXTRACTION_PROGRAM = `const config = input && input.config;
|
|
2196
|
+
if (!config || !Array.isArray(config.fields) || config.fields.length < 1 || config.fields.length > 32) {
|
|
2197
|
+
return { success: false, status: 'invalid_config', message: 'DOM extraction requires 1-32 fields' };
|
|
2198
|
+
}
|
|
2199
|
+
if (!document || typeof document.querySelectorAll !== 'function') {
|
|
2200
|
+
return { success: false, status: 'invalid_page', message: 'document.querySelectorAll is unavailable' };
|
|
2201
|
+
}
|
|
2202
|
+
const fields = {};
|
|
2203
|
+
for (let index = 0; index < config.fields.length; index += 1) {
|
|
2204
|
+
const field = config.fields[index];
|
|
2205
|
+
if (!field || typeof field.name !== 'string' || typeof field.selector !== 'string' || !('value' in field)) {
|
|
2206
|
+
return { success: false, status: 'invalid_config', message: 'DOM extraction field shape is invalid', field: String(field && field.name || index) };
|
|
2207
|
+
}
|
|
2208
|
+
if (field.value !== 'text' && field.value !== 'attribute') {
|
|
2209
|
+
return { success: false, status: 'invalid_config', message: 'DOM extraction value must be text or attribute', field: field.name };
|
|
2210
|
+
}
|
|
2211
|
+
if (Object.prototype.hasOwnProperty.call(fields, field.name)) {
|
|
2212
|
+
return { success: false, status: 'invalid_config', message: 'DOM extraction field names must be unique', field: field.name };
|
|
2213
|
+
}
|
|
2214
|
+
let matches;
|
|
2215
|
+
try {
|
|
2216
|
+
matches = document.querySelectorAll(field.selector);
|
|
2217
|
+
} catch (error) {
|
|
2218
|
+
return { success: false, status: 'invalid_selector', message: String(error && error.message || error), field: field.name };
|
|
2219
|
+
}
|
|
2220
|
+
const count = typeof matches.length === 'number' ? matches.length : Array.from(matches).length;
|
|
2221
|
+
if (count > ${DOM_EXTRACTION_MAX_MATCHES}) {
|
|
2222
|
+
return { success: false, status: 'limit_exceeded', field: field.name, count, limit: ${DOM_EXTRACTION_MAX_MATCHES} };
|
|
2223
|
+
}
|
|
2224
|
+
const nodes = Array.from(matches);
|
|
2225
|
+
const values = [];
|
|
2226
|
+
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex += 1) {
|
|
2227
|
+
const node = nodes[nodeIndex];
|
|
2228
|
+
let value;
|
|
2229
|
+
if (field.value === 'text') value = String(node && (node.innerText ?? node.textContent) || '').trim();
|
|
2230
|
+
else if (field.value === 'attribute') {
|
|
2231
|
+
const attribute = typeof node.getAttribute === 'function' ? node.getAttribute(field.attribute) : null;
|
|
2232
|
+
value = attribute === null || attribute === undefined ? null : String(attribute);
|
|
2233
|
+
}
|
|
2234
|
+
if (typeof value === 'string' && value.length > ${DOM_EXTRACTION_MAX_VALUE_CHARS}) {
|
|
2235
|
+
return { success: false, status: 'value_limit_exceeded', field: field.name, index: nodeIndex, length: value.length, limit: ${DOM_EXTRACTION_MAX_VALUE_CHARS} };
|
|
2236
|
+
}
|
|
2237
|
+
values.push(value);
|
|
2238
|
+
}
|
|
2239
|
+
if (field.multiple === true) fields[field.name] = values;
|
|
2240
|
+
else fields[field.name] = values.length ? values[0] : null;
|
|
2241
|
+
}
|
|
2242
|
+
return { success: true, fields, sources: { page: location.href } };`;
|
|
2243
|
+
DOM_EXTRACTION_PROGRAM_HASH = programHash(DOM_EXTRACTION_PROGRAM);
|
|
2244
|
+
PUBLISHED_READ_ONLY_PROGRAMS = Object.freeze({
|
|
2245
|
+
"bilibili-popular-videos": "9365eeb13a6dd90bb5114282b92381b5cf5e9865a2778c1cce276924cf2d35f1",
|
|
2246
|
+
"bilibili-video-detail": "940f9aaeb008fbf3f1d3180cb60c6b0efd6588d74dfbca4cd1384b0a225f94b7",
|
|
2247
|
+
"github-issue-thread": "4e2e21ce0247a08544fd7a8ca0bce3ee84cf2e06c0086f02d9e2488b81cb05ef",
|
|
2248
|
+
"github-releases": "34bee85d1ab7184f5a065dc61a8faf57e680681f0dd6fb23917bdeb8a6e5b8cd",
|
|
2249
|
+
"github-repo-readme": "55d7de67ff423593ce21c586f0650043a50b10b60abe86d2d4835982c91c872f",
|
|
2250
|
+
"github-repo-search": "dcd573dbcf66e4071186c4a69c226af984f095cac13ce5f2aca974016fe871f2",
|
|
2251
|
+
"mdn-search": "1ff226134f13fd1b923623887628b96256d51c9535a08f5c3df3a19a5ebb02ad",
|
|
2252
|
+
"npm-compare": "5785a9129d5c88e4df8a8b25745fa095e163b91eb1ee0477f58081e8a9329401",
|
|
2253
|
+
"reddit-post-read": "a8db61c52d2246b417374436cab2867cf51082c73d45102040ceaec2d8264cbf",
|
|
2254
|
+
"reddit-subreddit-posts": "cc9dc138b60209908aab7cb49205321ce59f480f6d744e8938e444dc230f2d0b",
|
|
2255
|
+
"v2ex-node-topics": "ab6b30af79f295bae137b928a6284574f58858c63dcd3e84f597d9dda7ae11b3",
|
|
2256
|
+
"v2ex-topic-read": "ee9ae86e6881e38a540d9efc51fd7282dd069195a33e700d3ae786122140b5c2",
|
|
2257
|
+
"x-profile-posts": "fb0869522e1aff847a4a70c124bd42712107bb3ba2b1d061073e2faa55e41064",
|
|
2258
|
+
"x-search-thread": "59fb2cd25479f332563c217bad8722c388967864bc056dd6f98e970ce11d6c11",
|
|
2259
|
+
"x-visible-thread": "9db24745e0451e22390001e825e9fd6a229d461d52383b8caa19bea6ebc0f710",
|
|
2260
|
+
"youtube-search-videos": "4a4e2a8b5316920c1229815a926921527b85dd9bc0388b9dcb9708fca0d5a9c0",
|
|
2261
|
+
"youtube-video-read": "b0c33f1c76499c2913461e553466a8ff648094990b8677631239b6fe5b11ace5"
|
|
2262
|
+
});
|
|
2263
|
+
PREVIOUS_PUBLISHED_READ_ONLY_PROGRAMS = Object.freeze({
|
|
2264
|
+
"bilibili-popular-videos": Object.freeze([
|
|
2265
|
+
"450dc01c9a427ccd7ec17c1422983b1fa8b00e87f33118c30f4763bd737943d3"
|
|
2266
|
+
]),
|
|
2267
|
+
"bilibili-video-detail": Object.freeze([
|
|
2268
|
+
"d2da238b495b8980d072cdc380f9f637d20487cc847e543d66780e0bb8ec88b2"
|
|
2269
|
+
]),
|
|
2270
|
+
"github-issue-thread": Object.freeze([
|
|
2271
|
+
"384e5adeab48a56892583d00d96db6f6af6a53c70aa3e13f9b0ed4e4423cde70"
|
|
2272
|
+
]),
|
|
2273
|
+
"github-releases": Object.freeze([
|
|
2274
|
+
"f629eb72cf3c97505029d424eb60b65e8236b331f9a5f30a4556fd64cec34ad4"
|
|
2275
|
+
]),
|
|
2276
|
+
"github-repo-readme": Object.freeze([
|
|
2277
|
+
"f9fc48e7f46d395e9eadc5ec6ac0f89f97a232ed0d4c5e47ecda16d78d09df67"
|
|
2278
|
+
]),
|
|
2279
|
+
"github-repo-search": Object.freeze([
|
|
2280
|
+
"12e93f0ca4606fcc825b983bbdc00e68e1a873a36649277d838ec15abe3f48ec"
|
|
2281
|
+
]),
|
|
2282
|
+
"mdn-search": Object.freeze([
|
|
2283
|
+
"533aa263b275356f18068c86d6cb1f240412f4a4dc2866fb9c5a58065cb6f17f"
|
|
2284
|
+
]),
|
|
2285
|
+
"npm-compare": Object.freeze([
|
|
2286
|
+
"ead55b7b52e965c16e9902c916306f955a259ee860a78fd66981909f18e9436e"
|
|
2287
|
+
]),
|
|
2288
|
+
"reddit-post-read": Object.freeze([
|
|
2289
|
+
"e40f0b8e5f99607b7ef691cfd98985500d31b82edee0d3c942ed87d0a63de8e3",
|
|
2290
|
+
"ef4f788eaeee0fe5ca33db705d6331726ad4cf6d739e4ab60972d949e35544b2"
|
|
2291
|
+
]),
|
|
2292
|
+
"reddit-subreddit-posts": Object.freeze([
|
|
2293
|
+
"789f71122d0772072cda01d718ed29f97dfbeb49f84643b1935972e9013e75b3"
|
|
2294
|
+
]),
|
|
2295
|
+
"v2ex-node-topics": Object.freeze([
|
|
2296
|
+
"a5dfb4036c8107c02a58e61d29af7f1d86c3a59f5969d3b0d45221e6c913f95b"
|
|
2297
|
+
]),
|
|
2298
|
+
"v2ex-topic-read": Object.freeze([
|
|
2299
|
+
"9477b4a08c9debbdf09c02d2a546bf4d958c24ddfd2bd65e941569b5dc4954fb"
|
|
2300
|
+
]),
|
|
2301
|
+
"x-profile-posts": Object.freeze([
|
|
2302
|
+
"f4fed984860f39f7c28f798df0a878c1224a8a0b3551834f72f02eff9d244b14",
|
|
2303
|
+
"0df6ed3703aec5713946014de767236c67bddce4092ea1c9426d647c21a7bdb2"
|
|
2304
|
+
]),
|
|
2305
|
+
"x-search-thread": Object.freeze([
|
|
2306
|
+
"f92986d0c7d618296abcb4d9234dd0fcda17ab10674c7d2c1f9b1411331e8dc6"
|
|
2307
|
+
]),
|
|
2308
|
+
"x-visible-thread": Object.freeze([
|
|
2309
|
+
"0ca61ddfb91bc18c7edd2ad209a485923ac2fc4c4c9a326977c06b64e014b145"
|
|
2310
|
+
]),
|
|
2311
|
+
"youtube-search-videos": Object.freeze([
|
|
2312
|
+
"f5aa24d95aac9610b541590106bef037ec7833cd43f1f69ec4f1974977bf0a01"
|
|
2313
|
+
]),
|
|
2314
|
+
"youtube-video-read": Object.freeze([
|
|
2315
|
+
"86ed74b0ee1a335be453bb71250cf8bccb6b1c0f8c504f8316d21ef220c696fe"
|
|
2316
|
+
])
|
|
2317
|
+
});
|
|
2318
|
+
}
|
|
2319
|
+
});
|
|
2320
|
+
|
|
1995
2321
|
// src/cli-files.cjs
|
|
1996
2322
|
var require_cli_files = __commonJS({
|
|
1997
2323
|
"src/cli-files.cjs"(exports2, module2) {
|
|
1998
2324
|
"use strict";
|
|
1999
2325
|
var fs4 = require("fs");
|
|
2000
2326
|
var path5 = require("path");
|
|
2001
|
-
var { randomUUID:
|
|
2327
|
+
var { randomUUID: randomUUID5 } = require("crypto");
|
|
2002
2328
|
var STALE_LOCK_AGE_MS = 10 * 60 * 1e3;
|
|
2003
2329
|
function assertNoLinks2(target) {
|
|
2004
2330
|
let current = path5.resolve(target);
|
|
@@ -2029,7 +2355,7 @@ var require_cli_files = __commonJS({
|
|
|
2029
2355
|
assertNoLinks2(target);
|
|
2030
2356
|
const temporary = path5.join(
|
|
2031
2357
|
path5.dirname(target),
|
|
2032
|
-
`.${path5.basename(target)}.${
|
|
2358
|
+
`.${path5.basename(target)}.${randomUUID5()}.tmp`
|
|
2033
2359
|
);
|
|
2034
2360
|
try {
|
|
2035
2361
|
fs4.writeFileSync(temporary, data, { flag: "wx", mode: 384 });
|
|
@@ -2044,7 +2370,7 @@ var require_cli_files = __commonJS({
|
|
|
2044
2370
|
"Session busy or stale lock; verify the previous CLI has stopped before removing its .lock file. Unknown actions must not be replayed."
|
|
2045
2371
|
);
|
|
2046
2372
|
const reapTarget = `${target}.reap`;
|
|
2047
|
-
const token =
|
|
2373
|
+
const token = randomUUID5();
|
|
2048
2374
|
const payload = { pid: process.pid, token, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
2049
2375
|
const readLock = () => {
|
|
2050
2376
|
try {
|
|
@@ -2152,6 +2478,31 @@ __export(learned_recipes_exports, {
|
|
|
2152
2478
|
syncLearnedRouting: () => syncLearnedRouting,
|
|
2153
2479
|
validateSavedRecipe: () => validateSavedRecipe
|
|
2154
2480
|
});
|
|
2481
|
+
function isTrustedBundledReadOnlySkill(raw) {
|
|
2482
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return false;
|
|
2483
|
+
const def = raw;
|
|
2484
|
+
if (typeof def.id !== "string" || def.source?.type !== "builtin") return false;
|
|
2485
|
+
if (!Array.isArray(def.steps) || def.steps.length < 1 || def.steps.length > 20) return false;
|
|
2486
|
+
for (const step of def.steps) {
|
|
2487
|
+
if (!step || typeof step !== "object" || typeof step.tool !== "string") return false;
|
|
2488
|
+
if (!READ_ONLY_SKILL_TOOLS.has(step.tool)) return false;
|
|
2489
|
+
if (step.tool === "chrome_javascript") {
|
|
2490
|
+
const args = step.args;
|
|
2491
|
+
if (!args || typeof args !== "object") return false;
|
|
2492
|
+
const hasProgram = args.program !== void 0;
|
|
2493
|
+
const hasCode = typeof args.code === "string";
|
|
2494
|
+
if (hasProgram && hasCode) return false;
|
|
2495
|
+
if (hasProgram) {
|
|
2496
|
+
if (args.program !== DOM_EXTRACTION_PROGRAM_ALIAS) return false;
|
|
2497
|
+
continue;
|
|
2498
|
+
}
|
|
2499
|
+
if (!hasCode) return false;
|
|
2500
|
+
const expectedHash = PUBLISHED_READ_ONLY_PROGRAMS[def.id];
|
|
2501
|
+
if (!expectedHash || programHash(args.code) !== expectedHash) return false;
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
return true;
|
|
2505
|
+
}
|
|
2155
2506
|
function browserPawHome(override) {
|
|
2156
2507
|
return import_node_path2.default.resolve(
|
|
2157
2508
|
override || process.env.BROWSERPAW_HOME || import_node_path2.default.join(import_node_os2.default.homedir(), ".browserpaw")
|
|
@@ -2400,9 +2751,9 @@ function resolveBundledRecipesDirectory(homeDir = browserPawHome()) {
|
|
|
2400
2751
|
const candidates = [
|
|
2401
2752
|
import_node_path2.default.join(__dirname, "recipes"),
|
|
2402
2753
|
import_node_path2.default.join(homeDir, "host", "recipes"),
|
|
2754
|
+
import_node_path2.default.resolve(__dirname, "..", "..", "..", "skills", "browserpaw", "assets", "recipes"),
|
|
2403
2755
|
import_node_path2.default.join(__dirname, "..", "dist", "v5", "recipes"),
|
|
2404
|
-
import_node_path2.default.join(__dirname, "..", "dist", "recipes")
|
|
2405
|
-
import_node_path2.default.resolve(__dirname, "..", "..", "..", "skills", "browserpaw", "assets", "recipes")
|
|
2756
|
+
import_node_path2.default.join(__dirname, "..", "dist", "recipes")
|
|
2406
2757
|
];
|
|
2407
2758
|
return candidates.find((candidate) => import_node_fs2.default.existsSync(candidate));
|
|
2408
2759
|
}
|
|
@@ -2416,7 +2767,9 @@ function readJsonSafe(filePath, maxBytes = 4 * 1024 * 1024) {
|
|
|
2416
2767
|
}
|
|
2417
2768
|
}
|
|
2418
2769
|
function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
2770
|
+
const includeBundled = options.includeBundled ?? true;
|
|
2419
2771
|
const byId = /* @__PURE__ */ new Map();
|
|
2772
|
+
const suppressedBundledIds = /* @__PURE__ */ new Set();
|
|
2420
2773
|
const recipesDir = recipesDirectory(homeDir);
|
|
2421
2774
|
if (import_node_fs2.default.existsSync(recipesDir) && !import_node_fs2.default.lstatSync(recipesDir).isSymbolicLink()) {
|
|
2422
2775
|
for (const file of import_node_fs2.default.readdirSync(recipesDir).filter((name) => /^[a-z][a-z0-9-]{0,63}(?:\.candidate)?\.json$/.test(name)).sort()) {
|
|
@@ -2457,7 +2810,16 @@ function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
|
2457
2810
|
const active = record.versions.find(
|
|
2458
2811
|
(item) => item.version === record.activeVersion && item.status === "active"
|
|
2459
2812
|
);
|
|
2460
|
-
const
|
|
2813
|
+
const hasUserOwnedVersion = record.versions.some(
|
|
2814
|
+
(item) => item.definition?.source?.type && item.definition.source.type !== "builtin"
|
|
2815
|
+
);
|
|
2816
|
+
const isDisabledByUser = record.autoUpdatePolicy === "disabled" || !active && record.versions.some((item) => item.status === "disabled");
|
|
2817
|
+
if (hasUserOwnedVersion || isDisabledByUser) {
|
|
2818
|
+
suppressedBundledIds.add(record.id);
|
|
2819
|
+
}
|
|
2820
|
+
const latestCandidate = [...record.versions].reverse().find((item) => item.status === "candidate");
|
|
2821
|
+
const autoDiscoverableBundled = !active && includeBundled && !suppressedBundledIds.has(record.id) && latestCandidate?.definition && isTrustedBundledReadOnlySkill(latestCandidate.definition) ? latestCandidate : void 0;
|
|
2822
|
+
const chosen = active ?? autoDiscoverableBundled ?? (options.includeCandidates ? latestCandidate : void 0);
|
|
2461
2823
|
if (!chosen?.definition) continue;
|
|
2462
2824
|
if (byId.has(record.id) && chosen.status !== "active") continue;
|
|
2463
2825
|
const def = chosen.definition;
|
|
@@ -2465,7 +2827,7 @@ function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
|
2465
2827
|
byId.set(record.id, {
|
|
2466
2828
|
id: record.id,
|
|
2467
2829
|
kind: "skill",
|
|
2468
|
-
status: chosen.status === "active" ? "active" : "candidate",
|
|
2830
|
+
status: chosen.status === "active" ? "active" : autoDiscoverableBundled ? "bundled" : "candidate",
|
|
2469
2831
|
name: def.name || record.id,
|
|
2470
2832
|
description: def.description || "",
|
|
2471
2833
|
domains: Array.isArray(def.domains) ? def.domains : [],
|
|
@@ -2476,7 +2838,7 @@ function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
|
2476
2838
|
});
|
|
2477
2839
|
}
|
|
2478
2840
|
}
|
|
2479
|
-
if (options.includeCandidates) {
|
|
2841
|
+
if (includeBundled || options.includeCandidates) {
|
|
2480
2842
|
const bundledDir = options.bundledDir ?? resolveBundledRecipesDirectory(homeDir);
|
|
2481
2843
|
if (bundledDir && import_node_fs2.default.existsSync(bundledDir) && !import_node_fs2.default.lstatSync(bundledDir).isSymbolicLink()) {
|
|
2482
2844
|
for (const file of import_node_fs2.default.readdirSync(bundledDir).filter((name) => /^[a-z][a-z0-9-]{0,63}\.json$/.test(name)).sort()) {
|
|
@@ -2485,11 +2847,15 @@ function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
|
2485
2847
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
|
|
2486
2848
|
const def = parsed;
|
|
2487
2849
|
if (typeof def.id !== "string" || byId.has(def.id)) continue;
|
|
2850
|
+
if (suppressedBundledIds.has(def.id)) continue;
|
|
2851
|
+
const trustedBundled = isTrustedBundledReadOnlySkill(parsed);
|
|
2852
|
+
if (!trustedBundled && !options.includeCandidates) continue;
|
|
2853
|
+
if (trustedBundled && !includeBundled && !options.includeCandidates) continue;
|
|
2488
2854
|
const inputs = schemaToInputSpecs(def.inputSchema);
|
|
2489
2855
|
byId.set(def.id, {
|
|
2490
2856
|
id: def.id,
|
|
2491
2857
|
kind: "skill",
|
|
2492
|
-
status: "candidate",
|
|
2858
|
+
status: trustedBundled ? "bundled" : "candidate",
|
|
2493
2859
|
name: def.name || def.id,
|
|
2494
2860
|
description: def.description || "",
|
|
2495
2861
|
domains: Array.isArray(def.domains) ? def.domains : [],
|
|
@@ -2501,8 +2867,9 @@ function loadLearnedWorkflows(homeDir = browserPawHome(), options = {}) {
|
|
|
2501
2867
|
}
|
|
2502
2868
|
}
|
|
2503
2869
|
}
|
|
2870
|
+
const statusRank = (status) => status === "active" ? 0 : status === "bundled" ? 1 : 2;
|
|
2504
2871
|
return [...byId.values()].sort((left, right) => {
|
|
2505
|
-
if (left.status !== right.status) return left.status
|
|
2872
|
+
if (left.status !== right.status) return statusRank(left.status) - statusRank(right.status);
|
|
2506
2873
|
return left.id.localeCompare(right.id);
|
|
2507
2874
|
});
|
|
2508
2875
|
}
|
|
@@ -2666,9 +3033,11 @@ function inspectLearnedWorkflow(id, homeDir = browserPawHome(), bundledDir, pref
|
|
|
2666
3033
|
"Recipe ID must begin with a lowercase letter and contain only letters, digits, or hyphens."
|
|
2667
3034
|
);
|
|
2668
3035
|
}
|
|
2669
|
-
const active = preferActive ? loadLearnedWorkflows(homeDir, {
|
|
2670
|
-
|
|
2671
|
-
|
|
3036
|
+
const active = preferActive ? loadLearnedWorkflows(homeDir, {
|
|
3037
|
+
includeCandidates: false,
|
|
3038
|
+
includeBundled: false,
|
|
3039
|
+
bundledDir
|
|
3040
|
+
}).find((item) => item.id === id) : void 0;
|
|
2672
3041
|
const candidatePath = candidateRecipeFilePath(id, homeDir);
|
|
2673
3042
|
const candidate = active ? void 0 : readJsonSafe(candidatePath, 256 * 1024);
|
|
2674
3043
|
if (candidate && typeof candidate === "object" && !Array.isArray(candidate)) {
|
|
@@ -2786,7 +3155,7 @@ function syncLearnedRouting(homeDir = browserPawHome(), extraSkillPaths = []) {
|
|
|
2786
3155
|
}
|
|
2787
3156
|
return { indexPath, workflowCount: activeWorkflows.length, syncedSkillPaths };
|
|
2788
3157
|
}
|
|
2789
|
-
var import_node_fs2, import_node_os2, import_node_path2, RECIPE_ID_PATTERN, DOMAIN_PATTERN, PLACEHOLDER_PATTERN, INPUT_TYPES, TARGET_ARGUMENTS, QUERY_ALIASES;
|
|
3158
|
+
var import_node_fs2, import_node_os2, import_node_path2, RECIPE_ID_PATTERN, DOMAIN_PATTERN, PLACEHOLDER_PATTERN, INPUT_TYPES, READ_ONLY_SKILL_TOOLS, TARGET_ARGUMENTS, QUERY_ALIASES;
|
|
2790
3159
|
var init_learned_recipes = __esm({
|
|
2791
3160
|
"src/learned-recipes.ts"() {
|
|
2792
3161
|
"use strict";
|
|
@@ -2795,6 +3164,7 @@ var init_learned_recipes = __esm({
|
|
|
2795
3164
|
import_node_path2 = __toESM(require("path"));
|
|
2796
3165
|
init_dist();
|
|
2797
3166
|
init_command_utils();
|
|
3167
|
+
init_skill_read_program();
|
|
2798
3168
|
init_skill_install();
|
|
2799
3169
|
RECIPE_ID_PATTERN = /^[a-z][a-z0-9-]{0,63}$/;
|
|
2800
3170
|
DOMAIN_PATTERN = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*$/;
|
|
@@ -2807,6 +3177,14 @@ var init_learned_recipes = __esm({
|
|
|
2807
3177
|
"object",
|
|
2808
3178
|
"array"
|
|
2809
3179
|
]);
|
|
3180
|
+
READ_ONLY_SKILL_TOOLS = /* @__PURE__ */ new Set([
|
|
3181
|
+
"chrome_javascript",
|
|
3182
|
+
"chrome_network_request",
|
|
3183
|
+
"chrome_read_dom",
|
|
3184
|
+
"chrome_get_markdown",
|
|
3185
|
+
"chrome_grep",
|
|
3186
|
+
"chrome_screenshot"
|
|
3187
|
+
]);
|
|
2810
3188
|
TARGET_ARGUMENTS = /* @__PURE__ */ new Map([
|
|
2811
3189
|
["text", [0]],
|
|
2812
3190
|
["screenshot", [0]],
|
|
@@ -2824,214 +3202,53 @@ var init_learned_recipes = __esm({
|
|
|
2824
3202
|
QUERY_ALIASES = [
|
|
2825
3203
|
[/推特|推文|twitter/i, ["twitter", "x", "x.com", "twitter.com"]],
|
|
2826
3204
|
[/搜索|查找|搜一下|搜一搜|检索/i, ["search", "find", "query"]],
|
|
2827
|
-
[/帖子|动态|话题|讨论|串|thread/i, ["posts", "thread", "status"]],
|
|
3205
|
+
[/帖子|动态|话题|讨论|串|thread/i, ["posts", "thread", "status", "topic"]],
|
|
2828
3206
|
[/版本|发布|发行版|更新日志/i, ["releases", "release", "version"]],
|
|
2829
3207
|
[/文档|手册|参考/i, ["docs", "documentation", "reference", "mdn"]],
|
|
2830
3208
|
[/对比|比较/i, ["compare", "comparison"]],
|
|
2831
3209
|
[/包|依赖|npm/i, ["npm", "package"]],
|
|
2832
|
-
[/下载/i, ["download"]]
|
|
3210
|
+
[/下载/i, ["download"]],
|
|
3211
|
+
[/仓库|代码库|开源|项目/i, ["github", "repo", "repository", "repositories"]],
|
|
3212
|
+
[/readme|项目说明|简介/i, ["readme", "overview", "repo"]],
|
|
3213
|
+
[/议题|工单|issue|pr/i, ["issue", "issues", "pr", "comments"]],
|
|
3214
|
+
[/v2ex|v站/i, ["v2ex", "www.v2ex.com", "topics"]],
|
|
3215
|
+
[/b站|哔哩哔哩|bilibili|bv号/i, ["bilibili", "www.bilibili.com", "video", "bvid"]],
|
|
3216
|
+
[/油管|youtube|ytb/i, ["youtube", "www.youtube.com", "video"]],
|
|
3217
|
+
[/字幕|台词|文稿/i, ["subtitles", "captions", "transcript", "subtitle"]],
|
|
3218
|
+
[/视频/i, ["video", "videos"]],
|
|
3219
|
+
[/热门|热榜|趋势|流行/i, ["popular", "hot", "trending"]],
|
|
3220
|
+
[/红迪|reddit|板块/i, ["reddit", "www.reddit.com", "subreddit"]],
|
|
3221
|
+
[/评论|回复/i, ["comments", "replies"]],
|
|
3222
|
+
[/主页|资料|用户/i, ["profile", "user", "tweets"]],
|
|
3223
|
+
[/节点/i, ["node", "topics"]]
|
|
2833
3224
|
];
|
|
2834
3225
|
}
|
|
2835
3226
|
});
|
|
2836
3227
|
|
|
2837
|
-
// src/
|
|
2838
|
-
var
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
}
|
|
2843
|
-
});
|
|
2844
|
-
|
|
2845
|
-
// src/scripts/browser-config.ts
|
|
2846
|
-
function getUserManifestPathForBrowser(browser) {
|
|
2847
|
-
const platform2 = os4.platform();
|
|
2848
|
-
if (platform2 === "win32") {
|
|
2849
|
-
const appData = process.env.APPDATA || path4.join(os4.homedir(), "AppData", "Roaming");
|
|
2850
|
-
switch (browser) {
|
|
2851
|
-
case "chrome" /* CHROME */:
|
|
2852
|
-
return path4.join(appData, "Google", "Chrome", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
2853
|
-
case "chromium" /* CHROMIUM */:
|
|
2854
|
-
return path4.join(appData, "Chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
2855
|
-
default:
|
|
2856
|
-
return path4.join(appData, "Google", "Chrome", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
2857
|
-
}
|
|
2858
|
-
} else if (platform2 === "darwin") {
|
|
2859
|
-
const home = os4.homedir();
|
|
2860
|
-
switch (browser) {
|
|
2861
|
-
case "chrome" /* CHROME */:
|
|
2862
|
-
return path4.join(
|
|
2863
|
-
home,
|
|
2864
|
-
"Library",
|
|
2865
|
-
"Application Support",
|
|
2866
|
-
"Google",
|
|
2867
|
-
"Chrome",
|
|
2868
|
-
"NativeMessagingHosts",
|
|
2869
|
-
`${HOST_NAME}.json`
|
|
2870
|
-
);
|
|
2871
|
-
case "chromium" /* CHROMIUM */:
|
|
2872
|
-
return path4.join(
|
|
2873
|
-
home,
|
|
2874
|
-
"Library",
|
|
2875
|
-
"Application Support",
|
|
2876
|
-
"Chromium",
|
|
2877
|
-
"NativeMessagingHosts",
|
|
2878
|
-
`${HOST_NAME}.json`
|
|
2879
|
-
);
|
|
2880
|
-
default:
|
|
2881
|
-
return path4.join(
|
|
2882
|
-
home,
|
|
2883
|
-
"Library",
|
|
2884
|
-
"Application Support",
|
|
2885
|
-
"Google",
|
|
2886
|
-
"Chrome",
|
|
2887
|
-
"NativeMessagingHosts",
|
|
2888
|
-
`${HOST_NAME}.json`
|
|
2889
|
-
);
|
|
2890
|
-
}
|
|
2891
|
-
} else {
|
|
2892
|
-
const home = os4.homedir();
|
|
2893
|
-
switch (browser) {
|
|
2894
|
-
case "chrome" /* CHROME */:
|
|
2895
|
-
return path4.join(
|
|
2896
|
-
home,
|
|
2897
|
-
".config",
|
|
2898
|
-
"google-chrome",
|
|
2899
|
-
"NativeMessagingHosts",
|
|
2900
|
-
`${HOST_NAME}.json`
|
|
2901
|
-
);
|
|
2902
|
-
case "chromium" /* CHROMIUM */:
|
|
2903
|
-
return path4.join(home, ".config", "chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
2904
|
-
default:
|
|
2905
|
-
return path4.join(
|
|
2906
|
-
home,
|
|
2907
|
-
".config",
|
|
2908
|
-
"google-chrome",
|
|
2909
|
-
"NativeMessagingHosts",
|
|
2910
|
-
`${HOST_NAME}.json`
|
|
2911
|
-
);
|
|
2912
|
-
}
|
|
2913
|
-
}
|
|
2914
|
-
}
|
|
2915
|
-
function getSystemManifestPathForBrowser(browser) {
|
|
2916
|
-
const platform2 = os4.platform();
|
|
2917
|
-
if (platform2 === "win32") {
|
|
2918
|
-
const programFiles = process.env.ProgramFiles || "C:\\Program Files";
|
|
2919
|
-
switch (browser) {
|
|
2920
|
-
case "chrome" /* CHROME */:
|
|
2921
|
-
return path4.join(
|
|
2922
|
-
programFiles,
|
|
2923
|
-
"Google",
|
|
2924
|
-
"Chrome",
|
|
2925
|
-
"NativeMessagingHosts",
|
|
2926
|
-
`${HOST_NAME}.json`
|
|
2927
|
-
);
|
|
2928
|
-
case "chromium" /* CHROMIUM */:
|
|
2929
|
-
return path4.join(programFiles, "Chromium", "NativeMessagingHosts", `${HOST_NAME}.json`);
|
|
2930
|
-
default:
|
|
2931
|
-
return path4.join(
|
|
2932
|
-
programFiles,
|
|
2933
|
-
"Google",
|
|
2934
|
-
"Chrome",
|
|
2935
|
-
"NativeMessagingHosts",
|
|
2936
|
-
`${HOST_NAME}.json`
|
|
2937
|
-
);
|
|
2938
|
-
}
|
|
2939
|
-
} else if (platform2 === "darwin") {
|
|
2940
|
-
switch (browser) {
|
|
2941
|
-
case "chrome" /* CHROME */:
|
|
2942
|
-
return path4.join(
|
|
2943
|
-
"/Library",
|
|
2944
|
-
"Google",
|
|
2945
|
-
"Chrome",
|
|
2946
|
-
"NativeMessagingHosts",
|
|
2947
|
-
`${HOST_NAME}.json`
|
|
2948
|
-
);
|
|
2949
|
-
case "chromium" /* CHROMIUM */:
|
|
2950
|
-
return path4.join(
|
|
2951
|
-
"/Library",
|
|
2952
|
-
"Application Support",
|
|
2953
|
-
"Chromium",
|
|
2954
|
-
"NativeMessagingHosts",
|
|
2955
|
-
`${HOST_NAME}.json`
|
|
2956
|
-
);
|
|
2957
|
-
default:
|
|
2958
|
-
return path4.join(
|
|
2959
|
-
"/Library",
|
|
2960
|
-
"Google",
|
|
2961
|
-
"Chrome",
|
|
2962
|
-
"NativeMessagingHosts",
|
|
2963
|
-
`${HOST_NAME}.json`
|
|
2964
|
-
);
|
|
2965
|
-
}
|
|
2966
|
-
} else {
|
|
2967
|
-
switch (browser) {
|
|
2968
|
-
case "chrome" /* CHROME */:
|
|
2969
|
-
return path4.join("/etc", "opt", "chrome", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
2970
|
-
case "chromium" /* CHROMIUM */:
|
|
2971
|
-
return path4.join("/etc", "chromium", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
2972
|
-
default:
|
|
2973
|
-
return path4.join("/etc", "opt", "chrome", "native-messaging-hosts", `${HOST_NAME}.json`);
|
|
2974
|
-
}
|
|
2975
|
-
}
|
|
2976
|
-
}
|
|
2977
|
-
function getRegistryKeys(browser) {
|
|
2978
|
-
if (os4.platform() !== "win32") return void 0;
|
|
2979
|
-
const browserPaths = {
|
|
2980
|
-
["chrome" /* CHROME */]: {
|
|
2981
|
-
user: `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`,
|
|
2982
|
-
system: `HKLM\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`
|
|
2983
|
-
},
|
|
2984
|
-
["chromium" /* CHROMIUM */]: {
|
|
2985
|
-
user: `HKCU\\Software\\Chromium\\NativeMessagingHosts\\${HOST_NAME}`,
|
|
2986
|
-
system: `HKLM\\Software\\Chromium\\NativeMessagingHosts\\${HOST_NAME}`
|
|
2987
|
-
}
|
|
2988
|
-
};
|
|
2989
|
-
return browserPaths[browser];
|
|
2990
|
-
}
|
|
2991
|
-
function getBrowserConfig(browser) {
|
|
2992
|
-
const registryKeys = getRegistryKeys(browser);
|
|
2993
|
-
return {
|
|
2994
|
-
type: browser,
|
|
2995
|
-
displayName: browser.charAt(0).toUpperCase() + browser.slice(1),
|
|
2996
|
-
userManifestPath: getUserManifestPathForBrowser(browser),
|
|
2997
|
-
systemManifestPath: getSystemManifestPathForBrowser(browser),
|
|
2998
|
-
registryKey: registryKeys?.user,
|
|
2999
|
-
systemRegistryKey: registryKeys?.system
|
|
3000
|
-
};
|
|
3001
|
-
}
|
|
3002
|
-
var os4, path4;
|
|
3003
|
-
var init_browser_config = __esm({
|
|
3004
|
-
"src/scripts/browser-config.ts"() {
|
|
3005
|
-
"use strict";
|
|
3006
|
-
os4 = __toESM(require("os"));
|
|
3007
|
-
path4 = __toESM(require("path"));
|
|
3008
|
-
init_constant();
|
|
3009
|
-
}
|
|
3010
|
-
});
|
|
3011
|
-
|
|
3012
|
-
// src/cli-setup.ts
|
|
3013
|
-
var cli_setup_exports = {};
|
|
3014
|
-
__export(cli_setup_exports, {
|
|
3015
|
-
manageInstallation: () => manageInstallation
|
|
3228
|
+
// src/cli-setup.ts
|
|
3229
|
+
var cli_setup_exports = {};
|
|
3230
|
+
__export(cli_setup_exports, {
|
|
3231
|
+
manageInstallation: () => manageInstallation,
|
|
3232
|
+
syncInstalledHostFromDist: () => syncInstalledHostFromDist
|
|
3016
3233
|
});
|
|
3017
3234
|
function readJson(path5) {
|
|
3018
|
-
if (!(0,
|
|
3019
|
-
return JSON.parse((0,
|
|
3235
|
+
if (!(0, import_node_fs3.existsSync)(path5)) return void 0;
|
|
3236
|
+
return JSON.parse((0, import_node_fs3.readFileSync)(path5, "utf8"));
|
|
3020
3237
|
}
|
|
3021
3238
|
function writeJson(path5, value) {
|
|
3022
|
-
const temporary = path5 + "." + (0,
|
|
3239
|
+
const temporary = path5 + "." + (0, import_node_crypto3.randomUUID)() + ".tmp";
|
|
3023
3240
|
try {
|
|
3024
|
-
(0,
|
|
3025
|
-
(0,
|
|
3241
|
+
(0, import_node_fs3.writeFileSync)(temporary, JSON.stringify(value, null, 2) + "\n", { flag: "wx", mode: 384 });
|
|
3242
|
+
(0, import_node_fs3.renameSync)(temporary, path5);
|
|
3026
3243
|
} finally {
|
|
3027
|
-
(0,
|
|
3244
|
+
(0, import_node_fs3.rmSync)(temporary, { force: true });
|
|
3028
3245
|
}
|
|
3029
3246
|
}
|
|
3030
3247
|
function manifestFor(root, extensionId) {
|
|
3031
3248
|
return {
|
|
3032
3249
|
name: HOST_NAME,
|
|
3033
3250
|
description: "BrowserPaw Native Messaging host",
|
|
3034
|
-
path: (0,
|
|
3251
|
+
path: (0, import_node_path3.join)(root, "host", process.platform === "win32" ? "run_host.bat" : "run_host.sh"),
|
|
3035
3252
|
type: "stdio",
|
|
3036
3253
|
allowed_origins: [`chrome-extension://${extensionId}/`]
|
|
3037
3254
|
};
|
|
@@ -3044,7 +3261,7 @@ function register(root, browser, extensionId) {
|
|
|
3044
3261
|
throw new Error(
|
|
3045
3262
|
`BrowserPaw is already registered from another installation: ${config.userManifestPath}`
|
|
3046
3263
|
);
|
|
3047
|
-
(0,
|
|
3264
|
+
(0, import_node_fs3.mkdirSync)((0, import_node_path3.dirname)(config.userManifestPath), { recursive: true });
|
|
3048
3265
|
writeJson(config.userManifestPath, manifest);
|
|
3049
3266
|
if (config.registryKey)
|
|
3050
3267
|
(0, import_node_child_process.execFileSync)("reg.exe", [
|
|
@@ -3076,54 +3293,96 @@ function unregister(root, browser, extensionId) {
|
|
|
3076
3293
|
if (registeredPath === config.userManifestPath)
|
|
3077
3294
|
(0, import_node_child_process.execFileSync)("reg.exe", ["delete", config.registryKey, "/ve", "/f"]);
|
|
3078
3295
|
}
|
|
3079
|
-
(0,
|
|
3296
|
+
(0, import_node_fs3.rmSync)(config.userManifestPath);
|
|
3080
3297
|
}
|
|
3081
3298
|
function replaceDirectory(destination, prepare) {
|
|
3082
|
-
const staging = destination + ".staging-" + (0,
|
|
3083
|
-
const backup = destination + ".backup-" + (0,
|
|
3084
|
-
(0,
|
|
3299
|
+
const staging = destination + ".staging-" + (0, import_node_crypto3.randomUUID)();
|
|
3300
|
+
const backup = destination + ".backup-" + (0, import_node_crypto3.randomUUID)();
|
|
3301
|
+
(0, import_node_fs3.mkdirSync)(staging);
|
|
3085
3302
|
try {
|
|
3086
3303
|
prepare(staging);
|
|
3087
|
-
if ((0,
|
|
3304
|
+
if ((0, import_node_fs3.existsSync)(destination)) (0, import_node_fs3.renameSync)(destination, backup);
|
|
3088
3305
|
try {
|
|
3089
|
-
(0,
|
|
3306
|
+
(0, import_node_fs3.renameSync)(staging, destination);
|
|
3090
3307
|
} catch (error) {
|
|
3091
|
-
if ((0,
|
|
3308
|
+
if ((0, import_node_fs3.existsSync)(backup)) (0, import_node_fs3.renameSync)(backup, destination);
|
|
3092
3309
|
throw error;
|
|
3093
3310
|
}
|
|
3094
|
-
(0,
|
|
3311
|
+
(0, import_node_fs3.rmSync)(backup, { recursive: true, force: true });
|
|
3095
3312
|
} finally {
|
|
3096
|
-
(0,
|
|
3313
|
+
(0, import_node_fs3.rmSync)(staging, { recursive: true, force: true });
|
|
3097
3314
|
}
|
|
3098
3315
|
}
|
|
3099
|
-
function installHost(destination) {
|
|
3100
|
-
(0,
|
|
3316
|
+
function installHost(destination, sourceDir = __dirname, version = CURRENT_VERSION) {
|
|
3317
|
+
(0, import_node_fs3.mkdirSync)(destination, { recursive: true, mode: 448 });
|
|
3101
3318
|
const replace = (file, bytes, mode) => {
|
|
3102
|
-
const target = (0,
|
|
3103
|
-
if ((0,
|
|
3104
|
-
const staging = target + "." + (0,
|
|
3319
|
+
const target = (0, import_node_path3.join)(destination, file);
|
|
3320
|
+
if ((0, import_node_fs3.existsSync)(target) && (0, import_node_fs3.readFileSync)(target).equals(bytes)) return;
|
|
3321
|
+
const staging = target + "." + (0, import_node_crypto3.randomUUID)() + ".tmp";
|
|
3105
3322
|
try {
|
|
3106
|
-
(0,
|
|
3107
|
-
(0,
|
|
3323
|
+
(0, import_node_fs3.writeFileSync)(staging, bytes, { flag: "wx", mode });
|
|
3324
|
+
(0, import_node_fs3.renameSync)(staging, target);
|
|
3108
3325
|
} finally {
|
|
3109
|
-
(0,
|
|
3326
|
+
(0, import_node_fs3.rmSync)(staging, { force: true });
|
|
3110
3327
|
}
|
|
3111
3328
|
};
|
|
3112
3329
|
for (const file of files) {
|
|
3113
|
-
const source = (0,
|
|
3114
|
-
replace(file, (0,
|
|
3330
|
+
const source = (0, import_node_path3.join)(sourceDir, file);
|
|
3331
|
+
replace(file, (0, import_node_fs3.readFileSync)(source), (0, import_node_fs3.statSync)(source).mode & 511);
|
|
3115
3332
|
if (file === "run_host.sh" && process.platform !== "win32")
|
|
3116
|
-
(0,
|
|
3333
|
+
(0, import_node_fs3.chmodSync)((0, import_node_path3.join)(destination, file), 493);
|
|
3117
3334
|
}
|
|
3118
3335
|
replace("node_path.txt", Buffer.from(process.execPath + "\n"), 384);
|
|
3336
|
+
replace("host-version.txt", Buffer.from(version + "\n"), 384);
|
|
3119
3337
|
replaceDirectory(
|
|
3120
|
-
(0,
|
|
3121
|
-
(staging) => (0,
|
|
3338
|
+
(0, import_node_path3.join)(destination, "recipes"),
|
|
3339
|
+
(staging) => (0, import_node_fs3.cpSync)((0, import_node_path3.join)(sourceDir, "recipes"), staging, { recursive: true })
|
|
3122
3340
|
);
|
|
3123
3341
|
}
|
|
3342
|
+
function syncInstalledHostFromDist(root, sourceDistDir, version) {
|
|
3343
|
+
const recordPath = (0, import_node_path3.join)(root, "installation.json");
|
|
3344
|
+
const previous = readJson(recordPath);
|
|
3345
|
+
if (!previous) return false;
|
|
3346
|
+
for (const file of files) {
|
|
3347
|
+
if (!(0, import_node_fs3.existsSync)((0, import_node_path3.join)(sourceDistDir, file))) return false;
|
|
3348
|
+
}
|
|
3349
|
+
if (!(0, import_node_fs3.existsSync)((0, import_node_path3.join)(sourceDistDir, "recipes"))) return false;
|
|
3350
|
+
const { acquireLock, assertNoLinks: assertNoLinks2 } = require_cli_files();
|
|
3351
|
+
assertNoLinks2(root);
|
|
3352
|
+
const unlock = acquireLock((0, import_node_path3.join)(root, ".installation.lock"));
|
|
3353
|
+
try {
|
|
3354
|
+
const current = readJson(recordPath);
|
|
3355
|
+
if (!current) return false;
|
|
3356
|
+
installHost((0, import_node_path3.join)(root, "host"), sourceDistDir, version);
|
|
3357
|
+
const skillSourceDir = (0, import_node_path3.join)(sourceDistDir, "skill", "browserpaw");
|
|
3358
|
+
const skillPaths = /* @__PURE__ */ new Set([
|
|
3359
|
+
...Array.isArray(current.skillPaths) ? current.skillPaths : [],
|
|
3360
|
+
...current.skillPath ? [current.skillPath] : []
|
|
3361
|
+
]);
|
|
3362
|
+
if (current.skillMode === "managed" && (0, import_node_fs3.existsSync)((0, import_node_path3.join)(skillSourceDir, "SKILL.md"))) {
|
|
3363
|
+
for (const candidate of skillPaths) {
|
|
3364
|
+
try {
|
|
3365
|
+
upgradeInstalledSkill(candidate, { sourceDir: skillSourceDir, version });
|
|
3366
|
+
} catch {
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
try {
|
|
3371
|
+
syncLearnedRouting(root, [...skillPaths]);
|
|
3372
|
+
} catch {
|
|
3373
|
+
}
|
|
3374
|
+
writeJson(recordPath, {
|
|
3375
|
+
...current,
|
|
3376
|
+
version
|
|
3377
|
+
});
|
|
3378
|
+
return true;
|
|
3379
|
+
} finally {
|
|
3380
|
+
unlock();
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3124
3383
|
async function manageInstallation(command, requestHost2) {
|
|
3125
3384
|
const root = installationRoot();
|
|
3126
|
-
const recordPath = (0,
|
|
3385
|
+
const recordPath = (0, import_node_path3.join)(root, "installation.json");
|
|
3127
3386
|
const previous = readJson(recordPath);
|
|
3128
3387
|
if (command.name === "uninstall") {
|
|
3129
3388
|
if (!previous) return { installed: false };
|
|
@@ -3133,12 +3392,12 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3133
3392
|
...previous.skillPath ? [previous.skillPath] : []
|
|
3134
3393
|
]);
|
|
3135
3394
|
for (const candidate of managedSkills) {
|
|
3136
|
-
const marker = readJson((0,
|
|
3137
|
-
if (marker?.name === "browserpaw") (0,
|
|
3395
|
+
const marker = readJson((0, import_node_path3.join)(candidate, ".browserpaw-skill.json"));
|
|
3396
|
+
if (marker?.name === "browserpaw") (0, import_node_fs3.rmSync)(candidate, { recursive: true, force: true });
|
|
3138
3397
|
}
|
|
3139
3398
|
for (const target of ["host", "extension"])
|
|
3140
|
-
(0,
|
|
3141
|
-
(0,
|
|
3399
|
+
(0, import_node_fs3.rmSync)((0, import_node_path3.join)(root, target), { recursive: true, force: true });
|
|
3400
|
+
(0, import_node_fs3.rmSync)(recordPath, { force: true });
|
|
3142
3401
|
if (command.options.purge) {
|
|
3143
3402
|
for (const target of [
|
|
3144
3403
|
"tasks.json",
|
|
@@ -3148,9 +3407,11 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3148
3407
|
"out",
|
|
3149
3408
|
"logs",
|
|
3150
3409
|
"token",
|
|
3151
|
-
"host.json"
|
|
3410
|
+
"host.json",
|
|
3411
|
+
"cli-versions",
|
|
3412
|
+
"cli-update.json"
|
|
3152
3413
|
])
|
|
3153
|
-
(0,
|
|
3414
|
+
(0, import_node_fs3.rmSync)((0, import_node_path3.join)(root, target), { recursive: true, force: true });
|
|
3154
3415
|
}
|
|
3155
3416
|
return { installed: false, dataPreserved: !command.options.purge };
|
|
3156
3417
|
}
|
|
@@ -3160,19 +3421,19 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3160
3421
|
for (const browser2 of previous.browsers) register(root, browser2, previous.extensionId);
|
|
3161
3422
|
syncLearnedRouting(root);
|
|
3162
3423
|
}
|
|
3163
|
-
const installedHost = (0,
|
|
3164
|
-
const extension2 = (0,
|
|
3424
|
+
const installedHost = (0, import_node_fs3.existsSync)((0, import_node_path3.join)(root, "host", "host.cjs"));
|
|
3425
|
+
const extension2 = (0, import_node_path3.join)(root, "extension");
|
|
3165
3426
|
const host = await requestHost2("status", void 0, 4e3).then(async (response) => response.ok ? await response.json() : void 0).catch(() => void 0);
|
|
3166
3427
|
const learnedCount = loadLearnedWorkflows(root, { includeCandidates: false }).length;
|
|
3167
3428
|
return {
|
|
3168
3429
|
installed: previous?.version,
|
|
3169
3430
|
hostPresent: installedHost,
|
|
3170
3431
|
extension: extension2,
|
|
3171
|
-
extensionPresent: (0,
|
|
3432
|
+
extensionPresent: (0, import_node_fs3.existsSync)((0, import_node_path3.join)(extension2, "manifest.json")),
|
|
3172
3433
|
...previous?.skillPath ? {
|
|
3173
3434
|
skillPath: previous.skillPath,
|
|
3174
3435
|
skillScope: previous.skillScope ?? "user",
|
|
3175
|
-
skillPresent: (0,
|
|
3436
|
+
skillPresent: (0, import_node_fs3.existsSync)((0, import_node_path3.join)(previous.skillPath, "SKILL.md"))
|
|
3176
3437
|
} : {},
|
|
3177
3438
|
learnedHome: root,
|
|
3178
3439
|
learnedCount,
|
|
@@ -3192,36 +3453,36 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3192
3453
|
if (browser !== void 0 && browser !== "chrome" && browser !== "chromium" && browser !== "all")
|
|
3193
3454
|
throw new Error("Use --browser chrome, chromium, or all.");
|
|
3194
3455
|
const browsers = browser === "all" ? ["chrome" /* CHROME */, "chromium" /* CHROMIUM */] : browser ? [browser] : previous?.browsers ?? ["chrome" /* CHROME */];
|
|
3195
|
-
const extensionSource = (0,
|
|
3196
|
-
const extension = readJson((0,
|
|
3456
|
+
const extensionSource = (0, import_node_path3.join)(__dirname, "extension");
|
|
3457
|
+
const extension = readJson((0, import_node_path3.join)(extensionSource, "manifest.json"));
|
|
3197
3458
|
if (typeof extension?.key !== "string")
|
|
3198
3459
|
throw new Error("Packaged extension is missing its public key.");
|
|
3199
|
-
const extensionId = (0,
|
|
3460
|
+
const extensionId = (0, import_node_crypto3.createHash)("sha256").update(Buffer.from(extension.key, "base64")).digest("hex").slice(0, 32).replace(/[\da-f]/g, (char) => String.fromCharCode(97 + Number.parseInt(char, 16)));
|
|
3200
3461
|
for (const browser2 of browsers) {
|
|
3201
3462
|
const config = getBrowserConfig(browser2);
|
|
3202
3463
|
const oldManifest = readJson(config.userManifestPath);
|
|
3203
3464
|
if (oldManifest && oldManifest.path !== manifestFor(root, extensionId).path)
|
|
3204
3465
|
throw new Error(`A different installation owns ${config.userManifestPath}.`);
|
|
3205
3466
|
}
|
|
3206
|
-
(0,
|
|
3467
|
+
(0, import_node_fs3.mkdirSync)(root, { recursive: true, mode: 448 });
|
|
3207
3468
|
const { acquireLock, assertNoLinks: assertNoLinks2 } = require_cli_files();
|
|
3208
3469
|
assertNoLinks2(root);
|
|
3209
|
-
const unlock = acquireLock((0,
|
|
3470
|
+
const unlock = acquireLock((0, import_node_path3.join)(root, ".installation.lock"));
|
|
3210
3471
|
try {
|
|
3211
3472
|
const managedArtifactDirectory = (name) => {
|
|
3212
|
-
const directory = (0,
|
|
3213
|
-
if (name === "host") return files.some((file) => (0,
|
|
3473
|
+
const directory = (0, import_node_path3.join)(root, name);
|
|
3474
|
+
if (name === "host") return files.some((file) => (0, import_node_fs3.existsSync)((0, import_node_path3.join)(directory, file)));
|
|
3214
3475
|
try {
|
|
3215
|
-
return readJson((0,
|
|
3476
|
+
return readJson((0, import_node_path3.join)(directory, "manifest.json"))?.name === "browserpaw";
|
|
3216
3477
|
} catch {
|
|
3217
3478
|
return false;
|
|
3218
3479
|
}
|
|
3219
3480
|
};
|
|
3220
3481
|
for (const name of ["host", "extension"]) {
|
|
3221
|
-
const entry = (0,
|
|
3482
|
+
const entry = (0, import_node_fs3.lstatSync)((0, import_node_path3.join)(root, name), { throwIfNoEntry: false });
|
|
3222
3483
|
if (entry && (!entry.isDirectory() || entry.isSymbolicLink() || !previous && !managedArtifactDirectory(name)))
|
|
3223
3484
|
throw new Error(
|
|
3224
|
-
`Existing directory is outside the managed installation: ${(0,
|
|
3485
|
+
`Existing directory is outside the managed installation: ${(0, import_node_path3.join)(root, name)}`
|
|
3225
3486
|
);
|
|
3226
3487
|
}
|
|
3227
3488
|
let skillPath = previous?.skillPath;
|
|
@@ -3243,7 +3504,7 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3243
3504
|
force: true,
|
|
3244
3505
|
version: CURRENT_VERSION,
|
|
3245
3506
|
...requestedScope ? { scope: requestedScope } : {},
|
|
3246
|
-
...requestedDir ? { targetRoot: requestedDir } : !requestedScope && skillPath ? { targetRoot: (0,
|
|
3507
|
+
...requestedDir ? { targetRoot: requestedDir } : !requestedScope && skillPath ? { targetRoot: (0, import_node_path3.dirname)(skillPath) } : {}
|
|
3247
3508
|
});
|
|
3248
3509
|
skillPath = installedSkill.installedPath;
|
|
3249
3510
|
skillScope = requestedScope || requestedDir ? installedSkill.scope : previous?.skillScope ?? installedSkill.scope;
|
|
@@ -3256,12 +3517,12 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3256
3517
|
}
|
|
3257
3518
|
}
|
|
3258
3519
|
}
|
|
3259
|
-
const extensionTarget = (0,
|
|
3260
|
-
const manifestPath = (0,
|
|
3520
|
+
const extensionTarget = (0, import_node_path3.join)(root, "extension");
|
|
3521
|
+
const manifestPath = (0, import_node_path3.join)(extensionTarget, "manifest.json");
|
|
3261
3522
|
let hasValidManifest = false;
|
|
3262
|
-
if ((0,
|
|
3523
|
+
if ((0, import_node_fs3.existsSync)(manifestPath)) {
|
|
3263
3524
|
try {
|
|
3264
|
-
const parsed = JSON.parse((0,
|
|
3525
|
+
const parsed = JSON.parse((0, import_node_fs3.readFileSync)(manifestPath, "utf8"));
|
|
3265
3526
|
hasValidManifest = parsed !== null && typeof parsed === "object" && typeof parsed.name === "string" && parsed.name.trim().length > 0;
|
|
3266
3527
|
} catch {
|
|
3267
3528
|
hasValidManifest = false;
|
|
@@ -3271,10 +3532,10 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3271
3532
|
if (extensionPrepared) {
|
|
3272
3533
|
replaceDirectory(
|
|
3273
3534
|
extensionTarget,
|
|
3274
|
-
(target) => (0,
|
|
3535
|
+
(target) => (0, import_node_fs3.cpSync)(extensionSource, target, { recursive: true })
|
|
3275
3536
|
);
|
|
3276
3537
|
}
|
|
3277
|
-
installHost((0,
|
|
3538
|
+
installHost((0, import_node_path3.join)(root, "host"));
|
|
3278
3539
|
for (const browser2 of browsers) register(root, browser2, extensionId);
|
|
3279
3540
|
for (const browser2 of previous?.browsers ?? []) {
|
|
3280
3541
|
if (!browsers.includes(browser2)) unregister(root, browser2, previous.extensionId);
|
|
@@ -3292,7 +3553,7 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3292
3553
|
const releaseAsset = `${GITHUB_REPO_URL}/releases/download/v${CURRENT_VERSION}/browserpaw-extension.zip`;
|
|
3293
3554
|
return {
|
|
3294
3555
|
installed: true,
|
|
3295
|
-
extension: (0,
|
|
3556
|
+
extension: (0, import_node_path3.join)(root, "extension"),
|
|
3296
3557
|
extensionId,
|
|
3297
3558
|
skillPath,
|
|
3298
3559
|
updatedSkillPaths,
|
|
@@ -3307,26 +3568,26 @@ async function manageInstallation(command, requestHost2) {
|
|
|
3307
3568
|
unlock();
|
|
3308
3569
|
}
|
|
3309
3570
|
}
|
|
3310
|
-
var
|
|
3571
|
+
var import_node_crypto3, import_node_child_process, import_node_fs3, import_node_os3, import_node_path3, installationRoot, files;
|
|
3311
3572
|
var init_cli_setup = __esm({
|
|
3312
3573
|
"src/cli-setup.ts"() {
|
|
3313
3574
|
"use strict";
|
|
3314
|
-
|
|
3575
|
+
import_node_crypto3 = require("crypto");
|
|
3315
3576
|
import_node_child_process = require("child_process");
|
|
3316
|
-
|
|
3577
|
+
import_node_fs3 = require("fs");
|
|
3317
3578
|
import_node_os3 = require("os");
|
|
3318
|
-
|
|
3579
|
+
import_node_path3 = require("path");
|
|
3319
3580
|
init_dist();
|
|
3320
3581
|
init_browser_config();
|
|
3321
3582
|
init_skill_install();
|
|
3322
3583
|
init_learned_recipes();
|
|
3323
|
-
installationRoot = () => (0,
|
|
3584
|
+
installationRoot = () => (0, import_node_path3.resolve)(process.env.BROWSERPAW_HOME || (0, import_node_path3.join)((0, import_node_os3.homedir)(), ".browserpaw"));
|
|
3324
3585
|
files = ["host.cjs", "run_host.bat", "run_host.sh"];
|
|
3325
3586
|
}
|
|
3326
3587
|
});
|
|
3327
3588
|
|
|
3328
3589
|
// src/command-client.ts
|
|
3329
|
-
var
|
|
3590
|
+
var import_node_crypto5 = require("crypto");
|
|
3330
3591
|
var import_promises2 = require("fs/promises");
|
|
3331
3592
|
var import_node_fs5 = require("fs");
|
|
3332
3593
|
var import_node_os4 = require("os");
|
|
@@ -3334,57 +3595,553 @@ var import_node_path6 = require("path");
|
|
|
3334
3595
|
init_dist();
|
|
3335
3596
|
|
|
3336
3597
|
// src/cli-update.ts
|
|
3337
|
-
var
|
|
3338
|
-
var
|
|
3598
|
+
var import_node_child_process2 = require("child_process");
|
|
3599
|
+
var import_node_crypto4 = require("crypto");
|
|
3600
|
+
var import_node_fs4 = require("fs");
|
|
3601
|
+
var import_node_path4 = require("path");
|
|
3602
|
+
var import_node_zlib = require("zlib");
|
|
3339
3603
|
init_dist();
|
|
3604
|
+
init_cli_setup();
|
|
3340
3605
|
init_learned_recipes();
|
|
3341
|
-
var
|
|
3342
|
-
var
|
|
3343
|
-
var
|
|
3606
|
+
var DEFAULT_REGISTRY_URL = "https://registry.npmjs.org/browserpaw/latest";
|
|
3607
|
+
var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
3608
|
+
var FAILED_CHECK_RETRY_MS = 10 * 60 * 1e3;
|
|
3609
|
+
var METADATA_TIMEOUT_MS = 2500;
|
|
3610
|
+
var TARBALL_TIMEOUT_MS = 15e3;
|
|
3611
|
+
var MAX_TARBALL_BYTES = 50 * 1024 * 1024;
|
|
3612
|
+
var MAX_EXTRACTED_BYTES = 100 * 1024 * 1024;
|
|
3613
|
+
var MAX_TAR_ENTRIES = 4e3;
|
|
3614
|
+
var STABLE_VERSION = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/;
|
|
3615
|
+
var SHA512_INTEGRITY = /^sha512-([A-Za-z0-9+/]+=*)$/;
|
|
3616
|
+
var REQUIRED_DIST_FILES = [
|
|
3617
|
+
"cli.cjs",
|
|
3618
|
+
"host.cjs",
|
|
3619
|
+
"cli-files.cjs",
|
|
3620
|
+
"run_host.bat",
|
|
3621
|
+
"run_host.sh",
|
|
3622
|
+
(0, import_node_path4.join)("skill", "browserpaw", "SKILL.md")
|
|
3623
|
+
];
|
|
3624
|
+
function normalizeRepoUrl(url) {
|
|
3625
|
+
if (typeof url !== "string") return void 0;
|
|
3626
|
+
return url.replace(/^git\+/, "").replace(/\.git$/, "");
|
|
3627
|
+
}
|
|
3628
|
+
function resolveRegistryUrl() {
|
|
3629
|
+
const override = process.env.BROWSERPAW_UPDATE_REGISTRY_URL?.trim();
|
|
3630
|
+
if (override) {
|
|
3631
|
+
try {
|
|
3632
|
+
const parsed = new URL(override);
|
|
3633
|
+
if (parsed.protocol === "http:" && !parsed.username && !parsed.password && (parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost" || parsed.hostname === "::1")) {
|
|
3634
|
+
return { url: parsed.href, isLoopbackOverride: true };
|
|
3635
|
+
}
|
|
3636
|
+
} catch {
|
|
3637
|
+
}
|
|
3638
|
+
}
|
|
3639
|
+
return { url: DEFAULT_REGISTRY_URL, isLoopbackOverride: false };
|
|
3640
|
+
}
|
|
3641
|
+
function isAllowedTarballUrl(tarballUrl, registry) {
|
|
3642
|
+
let parsed;
|
|
3643
|
+
try {
|
|
3644
|
+
parsed = new URL(tarballUrl);
|
|
3645
|
+
} catch {
|
|
3646
|
+
return false;
|
|
3647
|
+
}
|
|
3648
|
+
if (parsed.username || parsed.password) return false;
|
|
3649
|
+
if (!parsed.pathname.endsWith(".tgz")) return false;
|
|
3650
|
+
if (registry.isLoopbackOverride) {
|
|
3651
|
+
return parsed.origin === new URL(registry.url).origin;
|
|
3652
|
+
}
|
|
3653
|
+
return parsed.protocol === "https:" && parsed.hostname === "registry.npmjs.org" && parsed.pathname.startsWith("/browserpaw/-/");
|
|
3654
|
+
}
|
|
3655
|
+
function parseOctalOrBase256(buf, offset, length) {
|
|
3656
|
+
const slice = buf.subarray(offset, offset + length);
|
|
3657
|
+
if (slice.length > 0 && (slice[0] & 128) !== 0) {
|
|
3658
|
+
let value = 0;
|
|
3659
|
+
for (let i = 1; i < slice.length; i += 1) {
|
|
3660
|
+
value = value * 256 + slice[i];
|
|
3661
|
+
}
|
|
3662
|
+
return value;
|
|
3663
|
+
}
|
|
3664
|
+
const text = slice.toString("ascii").replace(/\0.*$/, "").trim();
|
|
3665
|
+
if (!text) return 0;
|
|
3666
|
+
if (!/^[0-7]+$/.test(text)) throw new Error(`Invalid tar octal field: ${text}`);
|
|
3667
|
+
return Number.parseInt(text, 8);
|
|
3668
|
+
}
|
|
3669
|
+
function readTarString(buf, offset, length) {
|
|
3670
|
+
const end = buf.indexOf(0, offset);
|
|
3671
|
+
const stop = end === -1 || end >= offset + length ? offset + length : end;
|
|
3672
|
+
return buf.subarray(offset, stop).toString("utf8");
|
|
3673
|
+
}
|
|
3674
|
+
function parsePaxRecords(data) {
|
|
3675
|
+
const records = {};
|
|
3676
|
+
let cursor = 0;
|
|
3677
|
+
while (cursor < data.length) {
|
|
3678
|
+
const space = data.indexOf(32, cursor);
|
|
3679
|
+
if (space === -1) break;
|
|
3680
|
+
const lenText = data.subarray(cursor, space).toString("ascii");
|
|
3681
|
+
const recordLen = Number.parseInt(lenText, 10);
|
|
3682
|
+
if (!Number.isInteger(recordLen) || recordLen <= 0 || cursor + recordLen > data.length) {
|
|
3683
|
+
throw new Error("Malformed PAX tar header");
|
|
3684
|
+
}
|
|
3685
|
+
const line = data.subarray(space + 1, cursor + recordLen).toString("utf8");
|
|
3686
|
+
const trimmed = line.endsWith("\n") ? line.slice(0, -1) : line;
|
|
3687
|
+
const eq = trimmed.indexOf("=");
|
|
3688
|
+
if (eq > 0) {
|
|
3689
|
+
records[trimmed.slice(0, eq)] = trimmed.slice(eq + 1);
|
|
3690
|
+
}
|
|
3691
|
+
cursor += recordLen;
|
|
3692
|
+
}
|
|
3693
|
+
return records;
|
|
3694
|
+
}
|
|
3695
|
+
function hasControlChars(value) {
|
|
3696
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
3697
|
+
const code = value.charCodeAt(i);
|
|
3698
|
+
if (code <= 31 || code === 127) return true;
|
|
3699
|
+
}
|
|
3700
|
+
return false;
|
|
3701
|
+
}
|
|
3702
|
+
function sanitizeTarEntryPath(rawPath) {
|
|
3703
|
+
let normalized = rawPath.replace(/^\.\//, "");
|
|
3704
|
+
while (normalized.startsWith("./")) normalized = normalized.slice(2);
|
|
3705
|
+
normalized = normalized.replace(/\/+$/, "");
|
|
3706
|
+
if (!normalized) throw new Error("Empty tar entry path");
|
|
3707
|
+
if (normalized.includes("\0") || normalized.includes("\\") || normalized.includes(":") || normalized.startsWith("/") || hasControlChars(normalized)) {
|
|
3708
|
+
throw new Error(`Unsafe tar entry path: ${rawPath}`);
|
|
3709
|
+
}
|
|
3710
|
+
const segments = normalized.split("/");
|
|
3711
|
+
if (segments[0] !== "package") {
|
|
3712
|
+
throw new Error(`Tar entry escapes package root: ${rawPath}`);
|
|
3713
|
+
}
|
|
3714
|
+
for (const segment of segments) {
|
|
3715
|
+
if (!segment || segment === "." || segment === "..") {
|
|
3716
|
+
throw new Error(`Unsafe tar path segment: ${rawPath}`);
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
return segments.join("/");
|
|
3720
|
+
}
|
|
3721
|
+
function extractVerifiedPackageTgz(tgzBytes, integrity, destinationDir) {
|
|
3722
|
+
const match = SHA512_INTEGRITY.exec(integrity);
|
|
3723
|
+
if (!match) throw new Error("Tarball integrity must be a valid sha512 SRI string");
|
|
3724
|
+
const expectedDigest = Buffer.from(match[1], "base64");
|
|
3725
|
+
const actualDigest = (0, import_node_crypto4.createHash)("sha512").update(tgzBytes).digest();
|
|
3726
|
+
if (expectedDigest.length !== actualDigest.length || !(0, import_node_crypto4.timingSafeEqual)(expectedDigest, actualDigest)) {
|
|
3727
|
+
throw new Error("Tarball SHA-512 integrity verification failed");
|
|
3728
|
+
}
|
|
3729
|
+
const tar = (0, import_node_zlib.gunzipSync)(tgzBytes, { maxOutputLength: MAX_EXTRACTED_BYTES });
|
|
3730
|
+
const { assertNoLinks: assertNoLinks2 } = require_cli_files();
|
|
3731
|
+
assertNoLinks2(destinationDir);
|
|
3732
|
+
const rootAbs = (0, import_node_path4.resolve)(destinationDir);
|
|
3733
|
+
let offset = 0;
|
|
3734
|
+
let entries = 0;
|
|
3735
|
+
let totalBytes = 0;
|
|
3736
|
+
let nextPax;
|
|
3737
|
+
let nextGnuLongName;
|
|
3738
|
+
while (offset + 512 <= tar.length) {
|
|
3739
|
+
const header = tar.subarray(offset, offset + 512);
|
|
3740
|
+
offset += 512;
|
|
3741
|
+
if (header.every((byte) => byte === 0)) break;
|
|
3742
|
+
const typeByte = header[156];
|
|
3743
|
+
const typeFlag = typeByte === 0 ? "0" : String.fromCharCode(typeByte);
|
|
3744
|
+
const rawSize = parseOctalOrBase256(header, 124, 12);
|
|
3745
|
+
const size = nextPax?.size && /^\d+$/.test(nextPax.size) ? Number.parseInt(nextPax.size, 10) : rawSize;
|
|
3746
|
+
if (!Number.isSafeInteger(size) || size < 0 || offset + size > tar.length) {
|
|
3747
|
+
throw new Error("Truncated or invalid tar entry size");
|
|
3748
|
+
}
|
|
3749
|
+
const content = tar.subarray(offset, offset + size);
|
|
3750
|
+
const paddedSize = Math.ceil(size / 512) * 512;
|
|
3751
|
+
offset += paddedSize;
|
|
3752
|
+
if (typeFlag === "x") {
|
|
3753
|
+
nextPax = parsePaxRecords(content);
|
|
3754
|
+
continue;
|
|
3755
|
+
}
|
|
3756
|
+
if (typeFlag === "g") {
|
|
3757
|
+
continue;
|
|
3758
|
+
}
|
|
3759
|
+
if (typeFlag === "L") {
|
|
3760
|
+
nextGnuLongName = content.toString("utf8").replace(/\0.*$/, "");
|
|
3761
|
+
continue;
|
|
3762
|
+
}
|
|
3763
|
+
const namePart = readTarString(header, 0, 100);
|
|
3764
|
+
const prefixPart = readTarString(header, 345, 155);
|
|
3765
|
+
const combinedName = nextPax?.path ?? nextGnuLongName ?? (prefixPart ? `${prefixPart}/${namePart}` : namePart);
|
|
3766
|
+
nextPax = void 0;
|
|
3767
|
+
nextGnuLongName = void 0;
|
|
3768
|
+
if (typeFlag !== "0" && typeFlag !== "5") {
|
|
3769
|
+
throw new Error(`Unsupported tar entry type '${typeFlag}' for ${combinedName}`);
|
|
3770
|
+
}
|
|
3771
|
+
entries += 1;
|
|
3772
|
+
if (entries > MAX_TAR_ENTRIES) {
|
|
3773
|
+
throw new Error("Tarball exceeds maximum allowed file count");
|
|
3774
|
+
}
|
|
3775
|
+
const safeRel = sanitizeTarEntryPath(combinedName);
|
|
3776
|
+
const targetPath = (0, import_node_path4.resolve)(rootAbs, safeRel);
|
|
3777
|
+
if (targetPath !== rootAbs && !targetPath.startsWith(rootAbs + import_node_path4.sep)) {
|
|
3778
|
+
throw new Error(`Tar entry escapes destination: ${safeRel}`);
|
|
3779
|
+
}
|
|
3780
|
+
if (typeFlag === "5") {
|
|
3781
|
+
(0, import_node_fs4.mkdirSync)(targetPath, { recursive: true, mode: 448 });
|
|
3782
|
+
continue;
|
|
3783
|
+
}
|
|
3784
|
+
totalBytes += size;
|
|
3785
|
+
if (totalBytes > MAX_EXTRACTED_BYTES) {
|
|
3786
|
+
throw new Error("Tarball exceeds maximum allowed extracted size");
|
|
3787
|
+
}
|
|
3788
|
+
(0, import_node_fs4.mkdirSync)((0, import_node_path4.dirname)(targetPath), { recursive: true, mode: 448 });
|
|
3789
|
+
(0, import_node_fs4.writeFileSync)(targetPath, content, { flag: "wx", mode: 384 });
|
|
3790
|
+
}
|
|
3791
|
+
}
|
|
3792
|
+
function assertNoSymlinksInTree(rootDir) {
|
|
3793
|
+
const stat = (0, import_node_fs4.lstatSync)(rootDir, { throwIfNoEntry: false });
|
|
3794
|
+
if (!stat || stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
3795
|
+
throw new Error(`Invalid directory tree: ${rootDir}`);
|
|
3796
|
+
}
|
|
3797
|
+
const stack = [rootDir];
|
|
3798
|
+
while (stack.length) {
|
|
3799
|
+
const current = stack.pop();
|
|
3800
|
+
for (const entry of (0, import_node_fs4.readdirSync)(current, { withFileTypes: true })) {
|
|
3801
|
+
const full = (0, import_node_path4.join)(current, entry.name);
|
|
3802
|
+
const entryStat = (0, import_node_fs4.lstatSync)(full);
|
|
3803
|
+
if (entryStat.isSymbolicLink()) {
|
|
3804
|
+
throw new Error(`Symlink forbidden in managed CLI package: ${full}`);
|
|
3805
|
+
}
|
|
3806
|
+
if (entryStat.isDirectory()) {
|
|
3807
|
+
stack.push(full);
|
|
3808
|
+
} else if (!entryStat.isFile()) {
|
|
3809
|
+
throw new Error(`Unsupported file type in managed CLI package: ${full}`);
|
|
3810
|
+
}
|
|
3811
|
+
}
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
function verifyExtractedRelease(packageDir, expectedVersion) {
|
|
3815
|
+
const { assertNoLinks: assertNoLinks2 } = require_cli_files();
|
|
3816
|
+
assertNoLinks2(packageDir);
|
|
3817
|
+
assertNoSymlinksInTree(packageDir);
|
|
3818
|
+
const pkgJsonPath = (0, import_node_path4.join)(packageDir, "package.json");
|
|
3819
|
+
if (!(0, import_node_fs4.existsSync)(pkgJsonPath)) throw new Error("Extracted package is missing package.json");
|
|
3820
|
+
const pkg = JSON.parse((0, import_node_fs4.readFileSync)(pkgJsonPath, "utf8"));
|
|
3821
|
+
if (pkg.name !== "browserpaw") throw new Error("Extracted package name mismatch");
|
|
3822
|
+
if (pkg.version !== expectedVersion) throw new Error("Extracted package version mismatch");
|
|
3823
|
+
if (normalizeRepoUrl(pkg.repository?.url) !== GITHUB_REPO_URL) {
|
|
3824
|
+
throw new Error("Extracted package repository mismatch");
|
|
3825
|
+
}
|
|
3826
|
+
if (pkg.bin?.browserpaw !== "./dist/v5/cli.cjs") {
|
|
3827
|
+
throw new Error("Extracted package bin entry mismatch");
|
|
3828
|
+
}
|
|
3829
|
+
const distDir = (0, import_node_path4.join)(packageDir, "dist", "v5");
|
|
3830
|
+
for (const rel of REQUIRED_DIST_FILES) {
|
|
3831
|
+
const full = (0, import_node_path4.join)(distDir, rel);
|
|
3832
|
+
const stat = (0, import_node_fs4.lstatSync)(full, { throwIfNoEntry: false });
|
|
3833
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile() || stat.size === 0) {
|
|
3834
|
+
throw new Error(`Extracted package is missing required file: ${rel}`);
|
|
3835
|
+
}
|
|
3836
|
+
}
|
|
3837
|
+
const recipesDir = (0, import_node_path4.join)(distDir, "recipes");
|
|
3838
|
+
const recipesStat = (0, import_node_fs4.lstatSync)(recipesDir, { throwIfNoEntry: false });
|
|
3839
|
+
if (!recipesStat || recipesStat.isSymbolicLink() || !recipesStat.isDirectory() || !(0, import_node_fs4.readdirSync)(recipesDir).some((name) => /^[a-z][a-z0-9-]*\.json$/.test(name))) {
|
|
3840
|
+
throw new Error("Extracted package is missing bundled recipes");
|
|
3841
|
+
}
|
|
3842
|
+
return {
|
|
3843
|
+
version: expectedVersion,
|
|
3844
|
+
packageDir,
|
|
3845
|
+
distDir,
|
|
3846
|
+
cliPath: (0, import_node_path4.join)(distDir, "cli.cjs")
|
|
3847
|
+
};
|
|
3848
|
+
}
|
|
3849
|
+
function versionsRoot(homeDir) {
|
|
3850
|
+
return (0, import_node_path4.join)(homeDir, "cli-versions");
|
|
3851
|
+
}
|
|
3852
|
+
function validateManagedInstallDir(homeDir, version) {
|
|
3853
|
+
if (!STABLE_VERSION.test(version)) return void 0;
|
|
3854
|
+
const packageDir = (0, import_node_path4.join)(versionsRoot(homeDir), version);
|
|
3855
|
+
try {
|
|
3856
|
+
const { assertNoLinks: assertNoLinks2 } = require_cli_files();
|
|
3857
|
+
assertNoLinks2(packageDir);
|
|
3858
|
+
const pkgStat = (0, import_node_fs4.lstatSync)(packageDir, { throwIfNoEntry: false });
|
|
3859
|
+
if (!pkgStat || pkgStat.isSymbolicLink() || !pkgStat.isDirectory()) return void 0;
|
|
3860
|
+
const distDir = (0, import_node_path4.join)(packageDir, "dist", "v5");
|
|
3861
|
+
const cliPath = (0, import_node_path4.join)(distDir, "cli.cjs");
|
|
3862
|
+
const hostPath = (0, import_node_path4.join)(distDir, "host.cjs");
|
|
3863
|
+
const pkgPath = (0, import_node_path4.join)(packageDir, "package.json");
|
|
3864
|
+
for (const file of [cliPath, hostPath, pkgPath]) {
|
|
3865
|
+
const stat = (0, import_node_fs4.lstatSync)(file, { throwIfNoEntry: false });
|
|
3866
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile() || stat.size === 0) return void 0;
|
|
3867
|
+
}
|
|
3868
|
+
const pkg = JSON.parse((0, import_node_fs4.readFileSync)(pkgPath, "utf8"));
|
|
3869
|
+
if (pkg.name !== "browserpaw" || pkg.version !== version) return void 0;
|
|
3870
|
+
return { version, packageDir, distDir, cliPath };
|
|
3871
|
+
} catch {
|
|
3872
|
+
return void 0;
|
|
3873
|
+
}
|
|
3874
|
+
}
|
|
3875
|
+
function resolveActiveManagedCli(homeDir = browserPawHome(), currentVersion = CURRENT_VERSION) {
|
|
3876
|
+
try {
|
|
3877
|
+
const { readJson: readJson2 } = require_cli_files();
|
|
3878
|
+
const state = readJson2((0, import_node_path4.join)(homeDir, "cli-update.json"));
|
|
3879
|
+
const activeVersion = state?.activeVersion;
|
|
3880
|
+
if (typeof activeVersion !== "string" || !STABLE_VERSION.test(activeVersion)) return void 0;
|
|
3881
|
+
if (!isNewerVersion(activeVersion, currentVersion)) return void 0;
|
|
3882
|
+
return validateManagedInstallDir(homeDir, activeVersion);
|
|
3883
|
+
} catch {
|
|
3884
|
+
return void 0;
|
|
3885
|
+
}
|
|
3886
|
+
}
|
|
3887
|
+
function readStateWithCorruptRecovery(statePath) {
|
|
3888
|
+
const { readJson: readJson2 } = require_cli_files();
|
|
3889
|
+
try {
|
|
3890
|
+
return readJson2(statePath);
|
|
3891
|
+
} catch (error) {
|
|
3892
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
3893
|
+
const backup = `${statePath}.corrupt.${Date.now()}-${process.pid}`;
|
|
3894
|
+
(0, import_node_fs4.renameSync)(statePath, backup);
|
|
3895
|
+
console.error(
|
|
3896
|
+
`BrowserPaw update state was corrupt; moved it to ${backup} and started from empty state.`
|
|
3897
|
+
);
|
|
3898
|
+
return void 0;
|
|
3899
|
+
}
|
|
3900
|
+
}
|
|
3901
|
+
async function fetchRegistryManifest() {
|
|
3902
|
+
const registry = resolveRegistryUrl();
|
|
3903
|
+
try {
|
|
3904
|
+
const response = await fetch(registry.url, {
|
|
3905
|
+
redirect: "error",
|
|
3906
|
+
signal: AbortSignal.timeout(METADATA_TIMEOUT_MS)
|
|
3907
|
+
});
|
|
3908
|
+
if (!response.ok) return void 0;
|
|
3909
|
+
const manifest = await response.json();
|
|
3910
|
+
if (manifest?.name !== "browserpaw" || normalizeRepoUrl(manifest?.repository?.url) !== GITHUB_REPO_URL) {
|
|
3911
|
+
return void 0;
|
|
3912
|
+
}
|
|
3913
|
+
const version = manifest.version;
|
|
3914
|
+
if (typeof version !== "string" || !STABLE_VERSION.test(version)) return void 0;
|
|
3915
|
+
const rawTarball = manifest.dist?.tarball;
|
|
3916
|
+
const rawIntegrity = manifest.dist?.integrity;
|
|
3917
|
+
const tarball = typeof rawTarball === "string" && isAllowedTarballUrl(rawTarball, registry) ? rawTarball : void 0;
|
|
3918
|
+
const integrity = typeof rawIntegrity === "string" && SHA512_INTEGRITY.test(rawIntegrity) ? rawIntegrity : void 0;
|
|
3919
|
+
return {
|
|
3920
|
+
version,
|
|
3921
|
+
...tarball && integrity ? { tarball, integrity } : {}
|
|
3922
|
+
};
|
|
3923
|
+
} catch {
|
|
3924
|
+
return void 0;
|
|
3925
|
+
}
|
|
3926
|
+
}
|
|
3927
|
+
async function downloadTarballBytes(url, maxBytes = MAX_TARBALL_BYTES) {
|
|
3928
|
+
const controller = new AbortController();
|
|
3929
|
+
const timer = setTimeout(() => controller.abort(), TARBALL_TIMEOUT_MS);
|
|
3930
|
+
timer.unref?.();
|
|
3931
|
+
try {
|
|
3932
|
+
const response = await fetch(url, {
|
|
3933
|
+
redirect: "error",
|
|
3934
|
+
signal: controller.signal
|
|
3935
|
+
});
|
|
3936
|
+
if (!response.ok) throw new Error(`Tarball download failed with HTTP ${response.status}`);
|
|
3937
|
+
const contentLength = Number(response.headers.get("content-length"));
|
|
3938
|
+
if (Number.isFinite(contentLength) && contentLength > maxBytes) {
|
|
3939
|
+
controller.abort();
|
|
3940
|
+
await response.body?.cancel().catch(() => void 0);
|
|
3941
|
+
throw new Error("Tarball exceeds maximum allowed download size");
|
|
3942
|
+
}
|
|
3943
|
+
if (!response.body) {
|
|
3944
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
3945
|
+
if (bytes.length === 0 || bytes.length > maxBytes) {
|
|
3946
|
+
throw new Error("Tarball size is outside allowed bounds");
|
|
3947
|
+
}
|
|
3948
|
+
return bytes;
|
|
3949
|
+
}
|
|
3950
|
+
const reader = response.body.getReader();
|
|
3951
|
+
const chunks = [];
|
|
3952
|
+
let total = 0;
|
|
3953
|
+
try {
|
|
3954
|
+
for (; ; ) {
|
|
3955
|
+
const { done, value } = await reader.read();
|
|
3956
|
+
if (done) break;
|
|
3957
|
+
if (!value || value.byteLength === 0) continue;
|
|
3958
|
+
total += value.byteLength;
|
|
3959
|
+
if (total > maxBytes) {
|
|
3960
|
+
controller.abort();
|
|
3961
|
+
await reader.cancel().catch(() => void 0);
|
|
3962
|
+
throw new Error("Tarball exceeds maximum allowed download size");
|
|
3963
|
+
}
|
|
3964
|
+
chunks.push(Buffer.from(value.buffer, value.byteOffset, value.byteLength));
|
|
3965
|
+
}
|
|
3966
|
+
} finally {
|
|
3967
|
+
reader.releaseLock();
|
|
3968
|
+
}
|
|
3969
|
+
if (total === 0) {
|
|
3970
|
+
throw new Error("Tarball size is outside allowed bounds");
|
|
3971
|
+
}
|
|
3972
|
+
return Buffer.concat(chunks, total);
|
|
3973
|
+
} finally {
|
|
3974
|
+
clearTimeout(timer);
|
|
3975
|
+
}
|
|
3976
|
+
}
|
|
3977
|
+
function pruneOldManagedVersions(root, keepVersion) {
|
|
3978
|
+
const dir = versionsRoot(root);
|
|
3979
|
+
if (!(0, import_node_fs4.existsSync)(dir)) return;
|
|
3980
|
+
const installed = (0, import_node_fs4.readdirSync)(dir).filter((name) => STABLE_VERSION.test(name)).sort((left, right) => {
|
|
3981
|
+
if (left === right) return 0;
|
|
3982
|
+
return isNewerVersion(left, right) ? -1 : 1;
|
|
3983
|
+
});
|
|
3984
|
+
const keep = /* @__PURE__ */ new Set([keepVersion]);
|
|
3985
|
+
for (const version of installed) {
|
|
3986
|
+
if (keep.size >= 2) break;
|
|
3987
|
+
keep.add(version);
|
|
3988
|
+
}
|
|
3989
|
+
for (const version of installed) {
|
|
3990
|
+
if (keep.has(version)) continue;
|
|
3991
|
+
try {
|
|
3992
|
+
(0, import_node_fs4.rmSync)((0, import_node_path4.join)(dir, version), { recursive: true, force: true });
|
|
3993
|
+
} catch {
|
|
3994
|
+
}
|
|
3995
|
+
}
|
|
3996
|
+
}
|
|
3997
|
+
async function installManagedRelease(homeDir, manifest) {
|
|
3998
|
+
const { ensureDirectory } = require_cli_files();
|
|
3999
|
+
const root = versionsRoot(homeDir);
|
|
4000
|
+
ensureDirectory(root);
|
|
4001
|
+
const staging = (0, import_node_path4.join)(root, `.staging-${(0, import_node_crypto4.randomUUID)()}`);
|
|
4002
|
+
const backup = (0, import_node_path4.join)(root, `.backup-${(0, import_node_crypto4.randomUUID)()}`);
|
|
4003
|
+
const target = (0, import_node_path4.join)(root, manifest.version);
|
|
4004
|
+
(0, import_node_fs4.mkdirSync)(staging, { recursive: false, mode: 448 });
|
|
4005
|
+
let removeBackup = false;
|
|
4006
|
+
try {
|
|
4007
|
+
const tgzBytes = await downloadTarballBytes(manifest.tarball);
|
|
4008
|
+
extractVerifiedPackageTgz(tgzBytes, manifest.integrity, staging);
|
|
4009
|
+
const extractedPkgDir = (0, import_node_path4.join)(staging, "package");
|
|
4010
|
+
verifyExtractedRelease(extractedPkgDir, manifest.version);
|
|
4011
|
+
if (process.platform !== "win32") {
|
|
4012
|
+
for (const executable of ["cli.cjs", "host.cjs", "run_host.sh"]) {
|
|
4013
|
+
(0, import_node_fs4.chmodSync)((0, import_node_path4.join)(extractedPkgDir, "dist", "v5", executable), 493);
|
|
4014
|
+
}
|
|
4015
|
+
}
|
|
4016
|
+
const existed = (0, import_node_fs4.existsSync)(target);
|
|
4017
|
+
if (existed) (0, import_node_fs4.renameSync)(target, backup);
|
|
4018
|
+
try {
|
|
4019
|
+
(0, import_node_fs4.renameSync)(extractedPkgDir, target);
|
|
4020
|
+
removeBackup = true;
|
|
4021
|
+
} catch (error) {
|
|
4022
|
+
if (existed && (0, import_node_fs4.existsSync)(backup)) {
|
|
4023
|
+
try {
|
|
4024
|
+
(0, import_node_fs4.renameSync)(backup, target);
|
|
4025
|
+
removeBackup = true;
|
|
4026
|
+
} catch {
|
|
4027
|
+
}
|
|
4028
|
+
}
|
|
4029
|
+
throw error;
|
|
4030
|
+
}
|
|
4031
|
+
const installed = verifyExtractedRelease(target, manifest.version);
|
|
4032
|
+
pruneOldManagedVersions(homeDir, manifest.version);
|
|
4033
|
+
try {
|
|
4034
|
+
syncInstalledHostFromDist(homeDir, installed.distDir, installed.version);
|
|
4035
|
+
} catch {
|
|
4036
|
+
}
|
|
4037
|
+
return installed;
|
|
4038
|
+
} finally {
|
|
4039
|
+
(0, import_node_fs4.rmSync)(staging, { recursive: true, force: true });
|
|
4040
|
+
if (removeBackup) {
|
|
4041
|
+
(0, import_node_fs4.rmSync)(backup, { recursive: true, force: true });
|
|
4042
|
+
}
|
|
4043
|
+
}
|
|
4044
|
+
}
|
|
4045
|
+
async function ensureOnDemandCliUpdate(options = {}) {
|
|
4046
|
+
const currentVersion = options.currentVersion ?? CURRENT_VERSION;
|
|
4047
|
+
const root = browserPawHome();
|
|
4048
|
+
const existingActive = resolveActiveManagedCli(root, currentVersion)?.version;
|
|
4049
|
+
if (process.env.BROWSERPAW_NO_UPDATE_CHECK === "1") {
|
|
4050
|
+
return { activeVersion: existingActive, updated: false };
|
|
4051
|
+
}
|
|
4052
|
+
const statePath = (0, import_node_path4.join)(root, "cli-update.json");
|
|
4053
|
+
const { ensureDirectory, acquireLock, atomicWrite } = require_cli_files();
|
|
4054
|
+
try {
|
|
4055
|
+
ensureDirectory(root);
|
|
4056
|
+
const unlock = acquireLock((0, import_node_path4.join)(root, ".cli-update.lock"));
|
|
4057
|
+
try {
|
|
4058
|
+
const state = readStateWithCorruptRecovery(statePath);
|
|
4059
|
+
const notified = Array.isArray(state?.notified) ? state.notified : [];
|
|
4060
|
+
const currentActive = typeof state?.activeVersion === "string" && isNewerVersion(state.activeVersion, currentVersion) && validateManagedInstallDir(root, state.activeVersion) ? state.activeVersion : void 0;
|
|
4061
|
+
const effectiveVersion = currentActive && isNewerVersion(currentActive, currentVersion) ? currentActive : currentVersion;
|
|
4062
|
+
const now = Date.now();
|
|
4063
|
+
const lastAutoCheck = typeof state?.autoUpdateCheckedAt === "number" ? state.autoUpdateCheckedAt : state?.notifyOnly ? NaN : state?.checkedAt ?? NaN;
|
|
4064
|
+
const age = now - lastAutoCheck;
|
|
4065
|
+
const retryAfter = typeof state?.autoUpdateRetryAfter === "number" ? state.autoUpdateRetryAfter : void 0;
|
|
4066
|
+
const due = retryAfter !== void 0 ? now >= retryAfter : !(age >= 0 && age < CHECK_INTERVAL_MS);
|
|
4067
|
+
if (!due) {
|
|
4068
|
+
return {
|
|
4069
|
+
activeVersion: currentActive,
|
|
4070
|
+
updated: false,
|
|
4071
|
+
latest: state?.latest
|
|
4072
|
+
};
|
|
4073
|
+
}
|
|
4074
|
+
const manifest = await fetchRegistryManifest();
|
|
4075
|
+
let activeVersion = currentActive;
|
|
4076
|
+
let updated = false;
|
|
4077
|
+
let installFailed = false;
|
|
4078
|
+
const latest = manifest?.version ?? state?.latest;
|
|
4079
|
+
if (manifest && isNewerVersion(manifest.version, effectiveVersion) && manifest.tarball && manifest.integrity) {
|
|
4080
|
+
try {
|
|
4081
|
+
const installed = await installManagedRelease(root, {
|
|
4082
|
+
version: manifest.version,
|
|
4083
|
+
tarball: manifest.tarball,
|
|
4084
|
+
integrity: manifest.integrity
|
|
4085
|
+
});
|
|
4086
|
+
activeVersion = installed.version;
|
|
4087
|
+
updated = true;
|
|
4088
|
+
} catch {
|
|
4089
|
+
installFailed = true;
|
|
4090
|
+
}
|
|
4091
|
+
}
|
|
4092
|
+
const nextNotified = updated && activeVersion && !notified.includes(activeVersion) ? [...notified, activeVersion] : notified;
|
|
4093
|
+
const transientFailure = !manifest || installFailed;
|
|
4094
|
+
atomicWrite(
|
|
4095
|
+
statePath,
|
|
4096
|
+
JSON.stringify({
|
|
4097
|
+
checkedAt: transientFailure ? state?.checkedAt ?? now : now,
|
|
4098
|
+
...transientFailure ? {
|
|
4099
|
+
...typeof state?.autoUpdateCheckedAt === "number" ? { autoUpdateCheckedAt: state.autoUpdateCheckedAt } : {},
|
|
4100
|
+
autoUpdateRetryAfter: now + FAILED_CHECK_RETRY_MS
|
|
4101
|
+
} : { autoUpdateCheckedAt: now },
|
|
4102
|
+
latest,
|
|
4103
|
+
...activeVersion ? { activeVersion } : {},
|
|
4104
|
+
notified: nextNotified
|
|
4105
|
+
}) + "\n"
|
|
4106
|
+
);
|
|
4107
|
+
return { activeVersion, updated, latest };
|
|
4108
|
+
} finally {
|
|
4109
|
+
unlock();
|
|
4110
|
+
}
|
|
4111
|
+
} catch {
|
|
4112
|
+
return { activeVersion: existingActive, updated: false };
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
3344
4115
|
async function checkForCliUpdate() {
|
|
3345
4116
|
if (process.env.BROWSERPAW_NO_UPDATE_CHECK === "1") return;
|
|
3346
4117
|
const root = browserPawHome();
|
|
3347
|
-
const statePath = (0,
|
|
3348
|
-
const { ensureDirectory, acquireLock,
|
|
4118
|
+
const statePath = (0, import_node_path4.join)(root, "cli-update.json");
|
|
4119
|
+
const { ensureDirectory, acquireLock, atomicWrite } = require_cli_files();
|
|
3349
4120
|
try {
|
|
3350
4121
|
ensureDirectory(root);
|
|
3351
|
-
const unlock = acquireLock((0,
|
|
4122
|
+
const unlock = acquireLock((0, import_node_path4.join)(root, ".cli-update.lock"));
|
|
3352
4123
|
try {
|
|
3353
|
-
|
|
3354
|
-
try {
|
|
3355
|
-
state = readJson2(statePath);
|
|
3356
|
-
} catch (error) {
|
|
3357
|
-
if (!(error instanceof SyntaxError)) throw error;
|
|
3358
|
-
const backup = `${statePath}.corrupt.${Date.now()}-${process.pid}`;
|
|
3359
|
-
(0, import_node_fs3.renameSync)(statePath, backup);
|
|
3360
|
-
console.error(
|
|
3361
|
-
`BrowserPaw update state was corrupt; moved it to ${backup} and started from empty state.`
|
|
3362
|
-
);
|
|
3363
|
-
state = void 0;
|
|
3364
|
-
}
|
|
4124
|
+
const state = readStateWithCorruptRecovery(statePath);
|
|
3365
4125
|
const notified = Array.isArray(state?.notified) ? state.notified : [];
|
|
4126
|
+
const activeVersion = typeof state?.activeVersion === "string" && STABLE_VERSION.test(state.activeVersion) ? state.activeVersion : void 0;
|
|
3366
4127
|
const now = Date.now();
|
|
3367
4128
|
const age = now - (state?.checkedAt ?? NaN);
|
|
3368
|
-
const check = !(age >= 0 && age <
|
|
4129
|
+
const check = !(age >= 0 && age < CHECK_INTERVAL_MS);
|
|
3369
4130
|
let latest = state?.latest;
|
|
3370
4131
|
if (check) {
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
const manifest = await response.json();
|
|
3374
|
-
const repository = manifest?.repository?.url;
|
|
3375
|
-
if (manifest?.name !== "browserpaw" || typeof repository !== "string" || repository.replace(/^git\+/, "").replace(/\.git$/, "") !== GITHUB_REPO_URL)
|
|
3376
|
-
return;
|
|
3377
|
-
const version = manifest.version;
|
|
3378
|
-
if (typeof version === "string" && stableVersion.test(version)) return version;
|
|
3379
|
-
}).catch(() => void 0);
|
|
4132
|
+
const manifest = await fetchRegistryManifest();
|
|
4133
|
+
latest = manifest?.version;
|
|
3380
4134
|
}
|
|
3381
|
-
const shouldNotify = typeof latest === "string" &&
|
|
4135
|
+
const shouldNotify = typeof latest === "string" && STABLE_VERSION.test(latest) && isNewerVersion(latest, CURRENT_VERSION) && activeVersion !== latest && !notified.includes(latest);
|
|
3382
4136
|
if (check || shouldNotify) {
|
|
3383
4137
|
atomicWrite(
|
|
3384
4138
|
statePath,
|
|
3385
4139
|
JSON.stringify({
|
|
3386
4140
|
checkedAt: check ? now : state?.checkedAt,
|
|
4141
|
+
...typeof state?.autoUpdateCheckedAt === "number" ? { autoUpdateCheckedAt: state.autoUpdateCheckedAt } : check || state?.notifyOnly ? { notifyOnly: true } : {},
|
|
4142
|
+
...typeof state?.autoUpdateRetryAfter === "number" ? { autoUpdateRetryAfter: state.autoUpdateRetryAfter } : {},
|
|
3387
4143
|
latest,
|
|
4144
|
+
...activeVersion ? { activeVersion } : {},
|
|
3388
4145
|
notified: shouldNotify ? [...notified, latest] : notified
|
|
3389
4146
|
}) + "\n"
|
|
3390
4147
|
);
|
|
@@ -3397,16 +4154,88 @@ async function checkForCliUpdate() {
|
|
|
3397
4154
|
return void 0;
|
|
3398
4155
|
}
|
|
3399
4156
|
}
|
|
4157
|
+
function firstCommandToken(argv) {
|
|
4158
|
+
for (const arg of argv) {
|
|
4159
|
+
if (arg === "--") return void 0;
|
|
4160
|
+
if (!arg.startsWith("-")) return arg;
|
|
4161
|
+
}
|
|
4162
|
+
return void 0;
|
|
4163
|
+
}
|
|
4164
|
+
function skipsAutoUpdateCheck(argv) {
|
|
4165
|
+
if (!argv.length) return true;
|
|
4166
|
+
if (argv[0] === "--version" || argv[0] === "-V") return true;
|
|
4167
|
+
const terminator = argv.indexOf("--");
|
|
4168
|
+
const flags = argv.slice(0, terminator < 0 ? void 0 : terminator);
|
|
4169
|
+
return flags.includes("--help") || flags.includes("-h") || argv[0] === "help";
|
|
4170
|
+
}
|
|
4171
|
+
async function maybeDelegateToManagedCli(argv) {
|
|
4172
|
+
if (process.env.BROWSERPAW_NO_AUTO_UPDATE === "1" || process.env.BROWSERPAW_CLI_DELEGATED === "1") {
|
|
4173
|
+
return void 0;
|
|
4174
|
+
}
|
|
4175
|
+
const commandToken = firstCommandToken(argv);
|
|
4176
|
+
if (commandToken === "uninstall") return void 0;
|
|
4177
|
+
const root = browserPawHome();
|
|
4178
|
+
if (process.env.BROWSERPAW_NO_UPDATE_CHECK !== "1" && !skipsAutoUpdateCheck(argv)) {
|
|
4179
|
+
await ensureOnDemandCliUpdate({ currentVersion: CURRENT_VERSION });
|
|
4180
|
+
}
|
|
4181
|
+
const managed = resolveActiveManagedCli(root, CURRENT_VERSION);
|
|
4182
|
+
if (!managed) return void 0;
|
|
4183
|
+
try {
|
|
4184
|
+
const currentScript = process.argv[1] ? (0, import_node_path4.resolve)(process.argv[1]) : void 0;
|
|
4185
|
+
if (currentScript && (0, import_node_path4.resolve)(managed.cliPath) === currentScript) return void 0;
|
|
4186
|
+
if (!(0, import_node_fs4.statSync)(managed.cliPath).isFile()) return void 0;
|
|
4187
|
+
} catch {
|
|
4188
|
+
return void 0;
|
|
4189
|
+
}
|
|
4190
|
+
return await new Promise((resolveExit) => {
|
|
4191
|
+
const child = (0, import_node_child_process2.spawn)(process.execPath, [managed.cliPath, ...argv], {
|
|
4192
|
+
stdio: "inherit",
|
|
4193
|
+
env: {
|
|
4194
|
+
...process.env,
|
|
4195
|
+
BROWSERPAW_CLI_DELEGATED: "1",
|
|
4196
|
+
BROWSERPAW_NO_UPDATE_CHECK: "1"
|
|
4197
|
+
}
|
|
4198
|
+
});
|
|
4199
|
+
const forwardSigint = () => {
|
|
4200
|
+
if (process.platform === "win32") return;
|
|
4201
|
+
try {
|
|
4202
|
+
child.kill("SIGINT");
|
|
4203
|
+
} catch {
|
|
4204
|
+
}
|
|
4205
|
+
};
|
|
4206
|
+
const forwardSigterm = () => {
|
|
4207
|
+
if (process.platform === "win32") return;
|
|
4208
|
+
try {
|
|
4209
|
+
child.kill("SIGTERM");
|
|
4210
|
+
} catch {
|
|
4211
|
+
}
|
|
4212
|
+
};
|
|
4213
|
+
process.on("SIGINT", forwardSigint);
|
|
4214
|
+
process.on("SIGTERM", forwardSigterm);
|
|
4215
|
+
const cleanup = () => {
|
|
4216
|
+
process.removeListener("SIGINT", forwardSigint);
|
|
4217
|
+
process.removeListener("SIGTERM", forwardSigterm);
|
|
4218
|
+
};
|
|
4219
|
+
child.once("error", () => {
|
|
4220
|
+
cleanup();
|
|
4221
|
+
resolveExit(void 0);
|
|
4222
|
+
});
|
|
4223
|
+
child.once("close", (code, signal) => {
|
|
4224
|
+
cleanup();
|
|
4225
|
+
resolveExit(typeof code === "number" ? code : signal ? 1 : 0);
|
|
4226
|
+
});
|
|
4227
|
+
});
|
|
4228
|
+
}
|
|
3400
4229
|
|
|
3401
4230
|
// src/server/token.ts
|
|
3402
|
-
var
|
|
4231
|
+
var os4 = __toESM(require("os"));
|
|
3403
4232
|
var fs3 = __toESM(require("fs"));
|
|
3404
|
-
var
|
|
4233
|
+
var path4 = __toESM(require("path"));
|
|
3405
4234
|
var crypto = __toESM(require("crypto"));
|
|
3406
|
-
var TOKEN_DIR =
|
|
3407
|
-
process.env.BROWSERPAW_HOME ||
|
|
4235
|
+
var TOKEN_DIR = path4.resolve(
|
|
4236
|
+
process.env.BROWSERPAW_HOME || path4.join(os4.homedir(), ".browserpaw")
|
|
3408
4237
|
);
|
|
3409
|
-
var TOKEN_FILE =
|
|
4238
|
+
var TOKEN_FILE = path4.join(TOKEN_DIR, "token");
|
|
3410
4239
|
var TOKEN_PATTERN = /^[0-9a-fA-F]{48}$|^[0-9a-fA-F]{64}$/;
|
|
3411
4240
|
function isValidTokenFormat(token) {
|
|
3412
4241
|
return typeof token === "string" && TOKEN_PATTERN.test(token.trim());
|
|
@@ -3427,26 +4256,26 @@ function readBridgeToken() {
|
|
|
3427
4256
|
|
|
3428
4257
|
// src/output-retention.ts
|
|
3429
4258
|
var import_promises = require("fs/promises");
|
|
3430
|
-
var
|
|
4259
|
+
var import_node_path5 = require("path");
|
|
3431
4260
|
var uuid = /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/i;
|
|
3432
4261
|
var managedFile = /^(?:result\.json|bookmarks|item-[1-9]\d*\.(?:bin|jpg|png|txt|webp))$/;
|
|
3433
4262
|
var day = 24 * 60 * 60 * 1e3;
|
|
3434
4263
|
async function pruneOutput(root, current, now = Date.now()) {
|
|
3435
4264
|
const { assertNoLinks: assertNoLinks2, readJson: readJson2 } = require_cli_files();
|
|
3436
4265
|
assertNoLinks2(root);
|
|
3437
|
-
const ledger = readJson2((0,
|
|
4266
|
+
const ledger = readJson2((0, import_node_path5.join)((0, import_node_path5.dirname)(root), "tasks.json"));
|
|
3438
4267
|
const active = ledger?.tasks ?? {};
|
|
3439
4268
|
const requests = [];
|
|
3440
4269
|
for (const bucket of await (0, import_promises.readdir)(root, { withFileTypes: true })) {
|
|
3441
4270
|
if (!bucket.isDirectory() || bucket.name !== "misc" && !uuid.test(bucket.name)) continue;
|
|
3442
|
-
const directory = (0,
|
|
3443
|
-
if (bucket.name !== "misc" && (directory === (0,
|
|
4271
|
+
const directory = (0, import_node_path5.join)(root, bucket.name);
|
|
4272
|
+
if (bucket.name !== "misc" && (directory === (0, import_node_path5.dirname)(current) || active[bucket.name]?.status === "active" || active[bucket.name]?.status === "paused"))
|
|
3444
4273
|
continue;
|
|
3445
4274
|
assertNoLinks2(directory);
|
|
3446
4275
|
const children = await (0, import_promises.readdir)(directory, { withFileTypes: true }).catch(() => []);
|
|
3447
4276
|
for (const request of children) {
|
|
3448
4277
|
if (!request.isDirectory() || !uuid.test(request.name)) continue;
|
|
3449
|
-
const path5 = (0,
|
|
4278
|
+
const path5 = (0, import_node_path5.join)(directory, request.name);
|
|
3450
4279
|
if (path5 === current) continue;
|
|
3451
4280
|
const stat = await (0, import_promises.lstat)(path5).catch(() => void 0);
|
|
3452
4281
|
if (stat?.isDirectory()) requests.push({ bucket: directory, path: path5, mtimeMs: stat.mtimeMs });
|
|
@@ -3459,7 +4288,7 @@ async function pruneOutput(root, current, now = Date.now()) {
|
|
|
3459
4288
|
assertNoLinks2(request.path);
|
|
3460
4289
|
const files2 = await (0, import_promises.readdir)(request.path, { withFileTypes: true }).catch(() => void 0);
|
|
3461
4290
|
if (!files2?.every((file) => file.isFile() && managedFile.test(file.name))) continue;
|
|
3462
|
-
for (const file of files2) await (0, import_promises.unlink)((0,
|
|
4291
|
+
for (const file of files2) await (0, import_promises.unlink)((0, import_node_path5.join)(request.path, file.name)).catch(() => {
|
|
3463
4292
|
});
|
|
3464
4293
|
await (0, import_promises.rmdir)(request.path).catch(() => {
|
|
3465
4294
|
});
|
|
@@ -3543,7 +4372,7 @@ async function saveOutput(result, command, requestId) {
|
|
|
3543
4372
|
path: path5,
|
|
3544
4373
|
mimeType: block.mimeType,
|
|
3545
4374
|
bytes: bytes.length,
|
|
3546
|
-
sha256: (0,
|
|
4375
|
+
sha256: (0, import_node_crypto5.createHash)("sha256").update(bytes).digest("hex")
|
|
3547
4376
|
};
|
|
3548
4377
|
references.set(stamp, reference);
|
|
3549
4378
|
return reference;
|
|
@@ -3670,7 +4499,39 @@ async function readStdin() {
|
|
|
3670
4499
|
}
|
|
3671
4500
|
return text;
|
|
3672
4501
|
}
|
|
4502
|
+
async function ensureHostMatchesCliVersion() {
|
|
4503
|
+
const root = (0, import_node_path6.resolve)(outputRoot(), "..");
|
|
4504
|
+
const hostInfo = await (0, import_promises2.readFile)((0, import_node_path6.join)(root, "host.json"), "utf8").then((value) => JSON.parse(value)).catch(() => void 0);
|
|
4505
|
+
if (typeof hostInfo?.version !== "string" || !isNewerVersion(CURRENT_VERSION, hostInfo.version)) {
|
|
4506
|
+
return;
|
|
4507
|
+
}
|
|
4508
|
+
const stagedMatches = async () => (0, import_promises2.readFile)((0, import_node_path6.join)(root, "host", "host-version.txt"), "utf8").then((value) => value.trim() === CURRENT_VERSION).catch(() => false);
|
|
4509
|
+
let readyToRestart = await stagedMatches();
|
|
4510
|
+
if (!readyToRestart) {
|
|
4511
|
+
try {
|
|
4512
|
+
const { syncInstalledHostFromDist: syncInstalledHostFromDist2 } = await Promise.resolve().then(() => (init_cli_setup(), cli_setup_exports));
|
|
4513
|
+
syncInstalledHostFromDist2(root, __dirname, CURRENT_VERSION);
|
|
4514
|
+
} catch {
|
|
4515
|
+
return;
|
|
4516
|
+
}
|
|
4517
|
+
readyToRestart = await stagedMatches();
|
|
4518
|
+
}
|
|
4519
|
+
if (!readyToRestart) return;
|
|
4520
|
+
const restarted = await requestHost("restart", {}, 1500).then((res) => res.status === 202).catch(() => false);
|
|
4521
|
+
if (!restarted) return;
|
|
4522
|
+
const deadline = Date.now() + 4e3;
|
|
4523
|
+
while (Date.now() < deadline) {
|
|
4524
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
4525
|
+
const refreshed = await (0, import_promises2.readFile)((0, import_node_path6.join)(root, "host.json"), "utf8").then((value) => JSON.parse(value)).catch(() => void 0);
|
|
4526
|
+
if (refreshed?.version === CURRENT_VERSION) {
|
|
4527
|
+
const ready = await requestHost("status", void 0, 1e3).then((res) => res.ok).catch(() => false);
|
|
4528
|
+
if (ready) return;
|
|
4529
|
+
}
|
|
4530
|
+
}
|
|
4531
|
+
}
|
|
3673
4532
|
async function runCommands(inputArgs) {
|
|
4533
|
+
const delegatedCode = await maybeDelegateToManagedCli(inputArgs);
|
|
4534
|
+
if (delegatedCode !== void 0) return delegatedCode;
|
|
3674
4535
|
const argv = inputArgs.slice();
|
|
3675
4536
|
if (argv[0] === "--json") {
|
|
3676
4537
|
argv.shift();
|
|
@@ -3683,7 +4544,7 @@ async function runCommands(inputArgs) {
|
|
|
3683
4544
|
let knownResult;
|
|
3684
4545
|
let update;
|
|
3685
4546
|
const controller = new AbortController();
|
|
3686
|
-
const requestId = (0,
|
|
4547
|
+
const requestId = (0, import_node_crypto5.randomUUID)();
|
|
3687
4548
|
const interrupt = () => {
|
|
3688
4549
|
if (!accepted) return;
|
|
3689
4550
|
void requestHost("cancel", { requestId }, 1500).catch(() => {
|
|
@@ -3868,6 +4729,7 @@ Showing ${items.length} of ${matches.length} matches. Refine the query or increa
|
|
|
3868
4729
|
if (command.name === "status") {
|
|
3869
4730
|
if (command.operands.length)
|
|
3870
4731
|
return failure(2, "invalid_arguments", "Usage: browserpaw status.");
|
|
4732
|
+
await ensureHostMatchesCliVersion();
|
|
3871
4733
|
const response2 = await requestHost("status", void 0, 4e3);
|
|
3872
4734
|
if (!response2.ok) return failure(3, "host_unavailable", "Cannot read the local host status.");
|
|
3873
4735
|
const data2 = await response2.json();
|
|
@@ -3881,11 +4743,12 @@ Showing ${items.length} of ${matches.length} matches. Refine the query or increa
|
|
|
3881
4743
|
"Could not read extension diagnostics.",
|
|
3882
4744
|
data2
|
|
3883
4745
|
);
|
|
3884
|
-
|
|
4746
|
+
const extensionVersion = typeof extension?.manifestVersion === "string" ? extension.manifestVersion : void 0;
|
|
4747
|
+
if (data2.version !== CURRENT_VERSION || !isCompatibleExtensionVersion(extensionVersion, CURRENT_VERSION)) {
|
|
3885
4748
|
return failure(
|
|
3886
4749
|
3,
|
|
3887
4750
|
"version_mismatch",
|
|
3888
|
-
`BrowserPaw requires
|
|
4751
|
+
`BrowserPaw ${CURRENT_VERSION} requires host ${CURRENT_VERSION} and a compatible extension (>= ${MIN_COMPATIBLE_EXTENSION_VERSION}, same major). Run setup with the matching CLI to update the host and Skill. For an incompatible extension, manually install ${GITHUB_REPO_URL}/releases/download/v${CURRENT_VERSION}/browserpaw-extension.zip and reload it.`,
|
|
3889
4752
|
data2
|
|
3890
4753
|
);
|
|
3891
4754
|
}
|
|
@@ -3896,7 +4759,7 @@ Showing ${items.length} of ${matches.length} matches. Refine the query or increa
|
|
|
3896
4759
|
return void 0;
|
|
3897
4760
|
});
|
|
3898
4761
|
const localManifest = await readExtensionJson("manifest.json");
|
|
3899
|
-
const localBuild = localManifest?.version ===
|
|
4762
|
+
const localBuild = localManifest?.version === extensionVersion ? await readExtensionJson("browserpaw-build.json") : void 0;
|
|
3900
4763
|
if (localBuild?.buildId && localBuild.buildId !== extension?.buildId)
|
|
3901
4764
|
return failure(
|
|
3902
4765
|
3,
|
|
@@ -3904,7 +4767,13 @@ Showing ${items.length} of ${matches.length} matches. Refine the query or increa
|
|
|
3904
4767
|
"Reload the installed BrowserPaw extension in chrome://extensions.",
|
|
3905
4768
|
data2
|
|
3906
4769
|
);
|
|
3907
|
-
|
|
4770
|
+
const statusData = extensionVersion && isNewerVersion(CURRENT_VERSION, extensionVersion) ? {
|
|
4771
|
+
...data2,
|
|
4772
|
+
extensionUpdateAvailable: true,
|
|
4773
|
+
extensionLatestVersion: CURRENT_VERSION,
|
|
4774
|
+
extensionReleaseAsset: `${GITHUB_REPO_URL}/releases/download/v${CURRENT_VERSION}/browserpaw-extension.zip`
|
|
4775
|
+
} : data2;
|
|
4776
|
+
writeReceipt({ ok: true, command: command.name, outcome: "done", data: statusData }, json);
|
|
3908
4777
|
return 0;
|
|
3909
4778
|
}
|
|
3910
4779
|
if (command.name === "upload") {
|
|
@@ -3921,6 +4790,9 @@ Showing ${items.length} of ${matches.length} matches. Refine the query or increa
|
|
|
3921
4790
|
else argv[index] = "--out=" + out;
|
|
3922
4791
|
}
|
|
3923
4792
|
const input = await inputFor(command);
|
|
4793
|
+
if (!command.options.task && !process.env.BROWSERPAW_TASK) {
|
|
4794
|
+
await ensureHostMatchesCliVersion();
|
|
4795
|
+
}
|
|
3924
4796
|
process.once("SIGINT", interrupt);
|
|
3925
4797
|
accepted = true;
|
|
3926
4798
|
const response = await requestHost(
|