harnesstap 1.2.0 → 1.3.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/README.md +28 -15
- package/dist/apply-2GRNKR5H.js +67 -0
- package/dist/apply-2GRNKR5H.js.map +1 -0
- package/dist/approve-KQNCNH2R.js +181 -0
- package/dist/approve-KQNCNH2R.js.map +1 -0
- package/dist/audit-SLYOZCMI.js +434 -0
- package/dist/audit-SLYOZCMI.js.map +1 -0
- package/dist/auth-3IEVICFT.js +220 -0
- package/dist/auth-3IEVICFT.js.map +1 -0
- package/dist/bin.js +166 -0
- package/dist/bin.js.map +1 -0
- package/dist/chunk-2EIJYDNI.js +10945 -0
- package/dist/chunk-2EIJYDNI.js.map +1 -0
- package/dist/chunk-2H7UOFLK.js +11 -0
- package/dist/chunk-2H7UOFLK.js.map +1 -0
- package/dist/chunk-37E5M4JX.js +34 -0
- package/dist/chunk-37E5M4JX.js.map +1 -0
- package/dist/chunk-3FIWOI3H.js +98 -0
- package/dist/chunk-3FIWOI3H.js.map +1 -0
- package/dist/chunk-3IWTJCPA.js +6448 -0
- package/dist/chunk-3IWTJCPA.js.map +1 -0
- package/dist/chunk-3IZ7SEGT.js +346 -0
- package/dist/chunk-3IZ7SEGT.js.map +1 -0
- package/dist/chunk-3RBYT7QN.js +540 -0
- package/dist/chunk-3RBYT7QN.js.map +1 -0
- package/dist/chunk-4KNQZDZC.js +115 -0
- package/dist/chunk-4KNQZDZC.js.map +1 -0
- package/dist/chunk-5F3LNDZG.js +9 -0
- package/dist/chunk-5F3LNDZG.js.map +1 -0
- package/dist/chunk-63U7QWNY.js +364 -0
- package/dist/chunk-63U7QWNY.js.map +1 -0
- package/dist/chunk-6BY4BV5G.js +998 -0
- package/dist/chunk-6BY4BV5G.js.map +1 -0
- package/dist/chunk-7L2K4N25.js +610 -0
- package/dist/chunk-7L2K4N25.js.map +1 -0
- package/dist/chunk-7LWH5SWH.js +18 -0
- package/dist/chunk-7LWH5SWH.js.map +1 -0
- package/dist/chunk-BIBUCC2E.js +696 -0
- package/dist/chunk-BIBUCC2E.js.map +1 -0
- package/dist/chunk-BSOBXLYI.js +172 -0
- package/dist/chunk-BSOBXLYI.js.map +1 -0
- package/dist/chunk-C4QBVVEH.js +32 -0
- package/dist/chunk-C4QBVVEH.js.map +1 -0
- package/dist/chunk-CHQGE2HF.js +283 -0
- package/dist/chunk-CHQGE2HF.js.map +1 -0
- package/dist/chunk-DV74UZJI.js +87 -0
- package/dist/chunk-DV74UZJI.js.map +1 -0
- package/dist/chunk-DWVAR4BP.js +656 -0
- package/dist/chunk-DWVAR4BP.js.map +1 -0
- package/dist/chunk-DXKAKKLP.js +302 -0
- package/dist/chunk-DXKAKKLP.js.map +1 -0
- package/dist/chunk-EIWXS4RM.js +235 -0
- package/dist/chunk-EIWXS4RM.js.map +1 -0
- package/dist/chunk-EWBCFAF3.js +681 -0
- package/dist/chunk-EWBCFAF3.js.map +1 -0
- package/dist/chunk-F2L4VNQK.js +1759 -0
- package/dist/chunk-F2L4VNQK.js.map +1 -0
- package/dist/chunk-G26HZZDJ.js +1359 -0
- package/dist/chunk-G26HZZDJ.js.map +1 -0
- package/dist/chunk-GQKKX7OE.js +818 -0
- package/dist/chunk-GQKKX7OE.js.map +1 -0
- package/dist/chunk-GRJY22U6.js +691 -0
- package/dist/chunk-GRJY22U6.js.map +1 -0
- package/dist/chunk-H6DVP2SI.js +682 -0
- package/dist/chunk-H6DVP2SI.js.map +1 -0
- package/dist/chunk-HVVPTCKR.js +1173 -0
- package/dist/chunk-HVVPTCKR.js.map +1 -0
- package/dist/chunk-I6XFZLTJ.js +9 -0
- package/dist/chunk-I6XFZLTJ.js.map +1 -0
- package/dist/chunk-IBOABSIA.js +17 -0
- package/dist/chunk-IBOABSIA.js.map +1 -0
- package/dist/chunk-IK524ZDW.js +691 -0
- package/dist/chunk-IK524ZDW.js.map +1 -0
- package/dist/chunk-IRU7NBOT.js +141 -0
- package/dist/chunk-IRU7NBOT.js.map +1 -0
- package/dist/chunk-J4CKQXQ6.js +806 -0
- package/dist/chunk-J4CKQXQ6.js.map +1 -0
- package/dist/chunk-JKGSJGCC.js +311 -0
- package/dist/chunk-JKGSJGCC.js.map +1 -0
- package/dist/chunk-JQOKQKQT.js +158 -0
- package/dist/chunk-JQOKQKQT.js.map +1 -0
- package/dist/chunk-JRFUTIEH.js +426 -0
- package/dist/chunk-JRFUTIEH.js.map +1 -0
- package/dist/chunk-K2BPYEH5.js +491 -0
- package/dist/chunk-K2BPYEH5.js.map +1 -0
- package/dist/chunk-KCQC7YBK.js +7795 -0
- package/dist/chunk-KCQC7YBK.js.map +1 -0
- package/dist/chunk-KDAV2GUT.js +1357 -0
- package/dist/chunk-KDAV2GUT.js.map +1 -0
- package/dist/chunk-KDDXZBMC.js +583 -0
- package/dist/chunk-KDDXZBMC.js.map +1 -0
- package/dist/chunk-KMWHZSEG.js +41 -0
- package/dist/chunk-KMWHZSEG.js.map +1 -0
- package/dist/chunk-KRI4R7EM.js +25 -0
- package/dist/chunk-KRI4R7EM.js.map +1 -0
- package/dist/chunk-LNJ3WZZX.js +186 -0
- package/dist/chunk-LNJ3WZZX.js.map +1 -0
- package/dist/chunk-MWXA4PFE.js +103 -0
- package/dist/chunk-MWXA4PFE.js.map +1 -0
- package/dist/chunk-MY62SSKC.js +183 -0
- package/dist/chunk-MY62SSKC.js.map +1 -0
- package/dist/chunk-NAGAJ5UM.js +145 -0
- package/dist/chunk-NAGAJ5UM.js.map +1 -0
- package/dist/chunk-NJ7WRKIQ.js +532 -0
- package/dist/chunk-NJ7WRKIQ.js.map +1 -0
- package/dist/chunk-OS6NUIBJ.js +38 -0
- package/dist/chunk-OS6NUIBJ.js.map +1 -0
- package/dist/chunk-PXU2OS26.js +238 -0
- package/dist/chunk-PXU2OS26.js.map +1 -0
- package/dist/chunk-QO4LBDVI.js +496 -0
- package/dist/chunk-QO4LBDVI.js.map +1 -0
- package/dist/chunk-QRZPQNLX.js +65 -0
- package/dist/chunk-QRZPQNLX.js.map +1 -0
- package/dist/chunk-SNVAMWB7.js +40 -0
- package/dist/chunk-SNVAMWB7.js.map +1 -0
- package/dist/chunk-SPRVUATR.js +430 -0
- package/dist/chunk-SPRVUATR.js.map +1 -0
- package/dist/chunk-SY7G5QEI.js +35 -0
- package/dist/chunk-SY7G5QEI.js.map +1 -0
- package/dist/chunk-SZNBUIDR.js +6438 -0
- package/dist/chunk-SZNBUIDR.js.map +1 -0
- package/dist/chunk-TQ2NA4Z4.js +24 -0
- package/dist/chunk-TQ2NA4Z4.js.map +1 -0
- package/dist/chunk-TYULB6ZY.js +2959 -0
- package/dist/chunk-TYULB6ZY.js.map +1 -0
- package/dist/chunk-UJYFTXHV.js +296 -0
- package/dist/chunk-UJYFTXHV.js.map +1 -0
- package/dist/chunk-VC6OKCJF.js +184 -0
- package/dist/chunk-VC6OKCJF.js.map +1 -0
- package/dist/chunk-VL2NWCZV.js +118 -0
- package/dist/chunk-VL2NWCZV.js.map +1 -0
- package/dist/chunk-VL7BGD47.js +514 -0
- package/dist/chunk-VL7BGD47.js.map +1 -0
- package/dist/chunk-VQVUJYYY.js +244 -0
- package/dist/chunk-VQVUJYYY.js.map +1 -0
- package/dist/chunk-VZQG2JDV.js +9 -0
- package/dist/chunk-VZQG2JDV.js.map +1 -0
- package/dist/chunk-XFZQVYIN.js +115 -0
- package/dist/chunk-XFZQVYIN.js.map +1 -0
- package/dist/chunk-XGTTOO4R.js +84 -0
- package/dist/chunk-XGTTOO4R.js.map +1 -0
- package/dist/chunk-XQFT4PEP.js +60 -0
- package/dist/chunk-XQFT4PEP.js.map +1 -0
- package/dist/chunk-YDD3ES2K.js +571 -0
- package/dist/chunk-YDD3ES2K.js.map +1 -0
- package/dist/chunk-ZY2MZ2KQ.js +1140 -0
- package/dist/chunk-ZY2MZ2KQ.js.map +1 -0
- package/dist/compile-57EEIUMB.js +161 -0
- package/dist/compile-57EEIUMB.js.map +1 -0
- package/dist/config-4EJ3D7QJ.js +438 -0
- package/dist/config-4EJ3D7QJ.js.map +1 -0
- package/dist/doctor-EEV6P5CH.js +111 -0
- package/dist/doctor-EEV6P5CH.js.map +1 -0
- package/dist/environment-WFEG4DAT.js +2745 -0
- package/dist/environment-WFEG4DAT.js.map +1 -0
- package/dist/github-BSMQD72A.js +114 -0
- package/dist/github-BSMQD72A.js.map +1 -0
- package/dist/harness-VX3PNVQW.js +746 -0
- package/dist/harness-VX3PNVQW.js.map +1 -0
- package/dist/help-AW2HKX63.js +2079 -0
- package/dist/help-AW2HKX63.js.map +1 -0
- package/dist/help-BVFOZD7E.js +1892 -0
- package/dist/help-BVFOZD7E.js.map +1 -0
- package/dist/index.d.ts +164 -1
- package/dist/index.js +50 -59246
- package/dist/index.js.map +1 -1
- package/dist/init-SPN2GEZ4.js +873 -0
- package/dist/init-SPN2GEZ4.js.map +1 -0
- package/dist/lock-K2LLUU7H.js +455 -0
- package/dist/lock-K2LLUU7H.js.map +1 -0
- package/dist/marketplace-VEKSWGX3.js +495 -0
- package/dist/marketplace-VEKSWGX3.js.map +1 -0
- package/dist/mcp-T6DKIKSF.js +202 -0
- package/dist/mcp-T6DKIKSF.js.map +1 -0
- package/dist/migrate-OO37WSB2.js +1043 -0
- package/dist/migrate-OO37WSB2.js.map +1 -0
- package/dist/open-3V3QK5RK.js +32 -0
- package/dist/open-3V3QK5RK.js.map +1 -0
- package/dist/pack-V32UPC7W.js +99 -0
- package/dist/pack-V32UPC7W.js.map +1 -0
- package/dist/plugin-KOBNQ7GT.js +64 -0
- package/dist/plugin-KOBNQ7GT.js.map +1 -0
- package/dist/plugin-from-project-X3VO3MB6.js +16 -0
- package/dist/plugin-from-project-X3VO3MB6.js.map +1 -0
- package/dist/policy-F5MKZNOC.js +105 -0
- package/dist/policy-F5MKZNOC.js.map +1 -0
- package/dist/profile-H4PMH4XT.js +1886 -0
- package/dist/profile-H4PMH4XT.js.map +1 -0
- package/dist/project-QVNEJJGH.js +1392 -0
- package/dist/project-QVNEJJGH.js.map +1 -0
- package/dist/resource-GL4RLSGI.js +555 -0
- package/dist/resource-GL4RLSGI.js.map +1 -0
- package/dist/version-PBHZK44K.js +17 -0
- package/dist/version-PBHZK44K.js.map +1 -0
- package/package.json +21 -14
- package/schemas/ap-package.v1.json +36 -0
- package/schemas/fixtures/catalog-missing-schema.ap.json +13 -0
- package/schemas/fixtures/confidence.ap.json +13 -0
- package/schemas/fixtures/context7.ap.json +13 -0
- package/schemas/fixtures/minimal-with-schema.ap.json +13 -0
- package/schemas/fixtures/superpowers.ap.json +13 -0
- package/schemas/fixtures/third-party-skills.ap.json +13 -0
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PACKAGE_VERSION,
|
|
3
|
+
fetchWithTimeout,
|
|
4
|
+
getHarnesstapDir
|
|
5
|
+
} from "./chunk-DWVAR4BP.js";
|
|
6
|
+
|
|
7
|
+
// src/config/github-session.ts
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
function getGithubSessionPath() {
|
|
11
|
+
return join(getHarnesstapDir(), "github-session.json");
|
|
12
|
+
}
|
|
13
|
+
function loadGithubSession() {
|
|
14
|
+
const path = getGithubSessionPath();
|
|
15
|
+
if (!fs.existsSync(path)) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const parsed = JSON.parse(fs.readFileSync(path, "utf8"));
|
|
20
|
+
if (!isSession(parsed)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return parsed;
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function saveGithubSession(session) {
|
|
29
|
+
const dir = getHarnesstapDir();
|
|
30
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
31
|
+
const path = getGithubSessionPath();
|
|
32
|
+
fs.writeFileSync(path, `${JSON.stringify(session, null, 2)}
|
|
33
|
+
`, {
|
|
34
|
+
encoding: "utf8"
|
|
35
|
+
});
|
|
36
|
+
try {
|
|
37
|
+
fs.chmodSync(path, 384);
|
|
38
|
+
} catch {
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function clearGithubSession() {
|
|
42
|
+
const path = getGithubSessionPath();
|
|
43
|
+
if (!fs.existsSync(path)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
fs.rmSync(path);
|
|
48
|
+
} catch {
|
|
49
|
+
saveGithubSession({
|
|
50
|
+
clientId: "",
|
|
51
|
+
accessToken: ""
|
|
52
|
+
});
|
|
53
|
+
fs.rmSync(path, { force: true });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function isSession(value) {
|
|
57
|
+
if (typeof value !== "object" || value === null) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const record = value;
|
|
61
|
+
return typeof record.clientId === "string" && typeof record.accessToken === "string" && record.accessToken.length > 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/config/github-app.ts
|
|
65
|
+
var DEFAULT_GITHUB_APP_CLIENT_ID = "Iv23liiaeCAUoGKe2uUx";
|
|
66
|
+
var GITHUB_APP_CLIENT_ID_ENV = "HARNESSTAP_GITHUB_APP_CLIENT_ID";
|
|
67
|
+
var GITHUB_APP_CLIENT_SECRET_ENV = "HARNESSTAP_GITHUB_APP_CLIENT_SECRET";
|
|
68
|
+
var GITHUB_DEVICE_CODE_URL = "https://github.com/login/device/code";
|
|
69
|
+
var GITHUB_OAUTH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
|
|
70
|
+
var GITHUB_API_URL = "https://api.github.com";
|
|
71
|
+
var GITHUB_DEVICE_VERIFICATION_URL = "https://github.com/login/device";
|
|
72
|
+
function resolveGithubAppClientId(env = process.env) {
|
|
73
|
+
const override = env[GITHUB_APP_CLIENT_ID_ENV]?.trim();
|
|
74
|
+
return override && override.length > 0 ? override : DEFAULT_GITHUB_APP_CLIENT_ID;
|
|
75
|
+
}
|
|
76
|
+
function resolveGithubAppClientSecret(env = process.env) {
|
|
77
|
+
const secret = env[GITHUB_APP_CLIENT_SECRET_ENV]?.trim();
|
|
78
|
+
return secret && secret.length > 0 ? secret : void 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/services/self-update.ts
|
|
82
|
+
import { spawn } from "child_process";
|
|
83
|
+
import { chmodSync, createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
84
|
+
import { join as join2 } from "path";
|
|
85
|
+
import { Readable } from "stream";
|
|
86
|
+
import { pipeline } from "stream/promises";
|
|
87
|
+
import semver from "semver";
|
|
88
|
+
var GITHUB_REPO = "harnesstap/harnesstap";
|
|
89
|
+
var GITHUB_LATEST_RELEASE_API = `https://api.github.com/repos/${GITHUB_REPO}/releases/latest`;
|
|
90
|
+
var NPM_LATEST_API = "https://registry.npmjs.org/harnesstap/latest";
|
|
91
|
+
var NPM_PACKAGE_NAME = "harnesstap";
|
|
92
|
+
var UPDATE_CHECK_MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
93
|
+
var UPDATE_LOOKUP_TIMEOUT_MS = 2500;
|
|
94
|
+
var EMPTY_CACHE = {
|
|
95
|
+
lastLookupAt: null,
|
|
96
|
+
latestVersion: null,
|
|
97
|
+
latestNotes: null,
|
|
98
|
+
latestHtmlUrl: null,
|
|
99
|
+
assets: [],
|
|
100
|
+
notifiedVersion: null
|
|
101
|
+
};
|
|
102
|
+
var UPDATE_CHECK_FILENAME = "update-check.json";
|
|
103
|
+
function updateCheckPath(harnesstapDir) {
|
|
104
|
+
return join2(harnesstapDir, UPDATE_CHECK_FILENAME);
|
|
105
|
+
}
|
|
106
|
+
function normalizePublishedVersion(version) {
|
|
107
|
+
const trimmed = version.trim();
|
|
108
|
+
return trimmed.startsWith("v") || trimmed.startsWith("V") ? trimmed.slice(1) : trimmed;
|
|
109
|
+
}
|
|
110
|
+
function isNewerPublishedVersion(latest, current) {
|
|
111
|
+
const latestVersion = normalizePublishedVersion(latest);
|
|
112
|
+
const currentVersion = normalizePublishedVersion(current);
|
|
113
|
+
const latestCore = semver.coerce(latestVersion)?.version;
|
|
114
|
+
const currentCore = semver.coerce(currentVersion)?.version;
|
|
115
|
+
if (!latestCore || !currentCore) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
return semver.gt(latestCore, currentCore);
|
|
119
|
+
}
|
|
120
|
+
function detectCliInstallChannel(scriptPath = process.argv[1] ?? "", env = process.env) {
|
|
121
|
+
if (env.HARNESSTAP_UPDATE_CHANNEL === "npm" || env.HARNESSTAP_UPDATE_CHANNEL === "github") {
|
|
122
|
+
return env.HARNESSTAP_UPDATE_CHANNEL;
|
|
123
|
+
}
|
|
124
|
+
if (env.npm_package_name === NPM_PACKAGE_NAME) {
|
|
125
|
+
return "npm";
|
|
126
|
+
}
|
|
127
|
+
const normalized = scriptPath.replaceAll("\\", "/");
|
|
128
|
+
if (normalized.includes("/node_modules/harnesstap/") || normalized.includes("/node_modules/.bin/") || normalized.includes("/_npx/") || normalized.includes("/.npm/_npx/")) {
|
|
129
|
+
return "npm";
|
|
130
|
+
}
|
|
131
|
+
return "github";
|
|
132
|
+
}
|
|
133
|
+
function cliInstallHint(channel, version) {
|
|
134
|
+
const normalized = normalizePublishedVersion(version);
|
|
135
|
+
switch (channel) {
|
|
136
|
+
case "npm":
|
|
137
|
+
return `npm install -g ${NPM_PACKAGE_NAME}@${normalized}`;
|
|
138
|
+
case "github":
|
|
139
|
+
return `https://github.com/${GITHUB_REPO}/releases/tag/v${normalized}`;
|
|
140
|
+
default: {
|
|
141
|
+
const exhaustive = channel;
|
|
142
|
+
return exhaustive;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function githubReleaseHtmlUrl(version) {
|
|
147
|
+
return `https://github.com/${GITHUB_REPO}/releases/tag/v${normalizePublishedVersion(version)}`;
|
|
148
|
+
}
|
|
149
|
+
function loadUpdateCheckCache(harnesstapDir) {
|
|
150
|
+
const path = updateCheckPath(harnesstapDir);
|
|
151
|
+
if (!existsSync(path)) {
|
|
152
|
+
return { ...EMPTY_CACHE };
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const raw = JSON.parse(readFileSync(path, "utf-8"));
|
|
156
|
+
const assets = Array.isArray(raw.assets) ? raw.assets.filter((asset) => typeof asset?.name === "string" && typeof asset?.browser_download_url === "string") : [];
|
|
157
|
+
return {
|
|
158
|
+
lastLookupAt: typeof raw.lastLookupAt === "string" ? raw.lastLookupAt : null,
|
|
159
|
+
latestVersion: typeof raw.latestVersion === "string" ? raw.latestVersion : null,
|
|
160
|
+
latestNotes: typeof raw.latestNotes === "string" ? raw.latestNotes : null,
|
|
161
|
+
latestHtmlUrl: typeof raw.latestHtmlUrl === "string" ? raw.latestHtmlUrl : null,
|
|
162
|
+
assets,
|
|
163
|
+
notifiedVersion: typeof raw.notifiedVersion === "string" ? raw.notifiedVersion : null
|
|
164
|
+
};
|
|
165
|
+
} catch {
|
|
166
|
+
return { ...EMPTY_CACHE };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function saveUpdateCheckCache(harnesstapDir, cache) {
|
|
170
|
+
mkdirSync(harnesstapDir, { recursive: true });
|
|
171
|
+
writeFileSync(updateCheckPath(harnesstapDir), `${JSON.stringify(cache, null, 2)}
|
|
172
|
+
`, "utf-8");
|
|
173
|
+
}
|
|
174
|
+
function isUpdateLookupFresh(cache, now = /* @__PURE__ */ new Date(), maxAgeMs = UPDATE_CHECK_MAX_AGE_MS) {
|
|
175
|
+
if (!cache.lastLookupAt) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const lookedUp = Date.parse(cache.lastLookupAt);
|
|
179
|
+
if (Number.isNaN(lookedUp)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return now.getTime() - lookedUp <= maxAgeMs;
|
|
183
|
+
}
|
|
184
|
+
function publishedReleaseUserAgent() {
|
|
185
|
+
return `HarnessTap/${PACKAGE_VERSION} (+https://github.com/${GITHUB_REPO})`;
|
|
186
|
+
}
|
|
187
|
+
function githubHeaders() {
|
|
188
|
+
return {
|
|
189
|
+
Accept: "application/vnd.github+json",
|
|
190
|
+
"User-Agent": publishedReleaseUserAgent(),
|
|
191
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
async function fetchNpmLatestVersion(fetchFn = fetchWithTimeout) {
|
|
195
|
+
const response = await fetchFn(NPM_LATEST_API, {
|
|
196
|
+
headers: { Accept: "application/json", "User-Agent": publishedReleaseUserAgent() },
|
|
197
|
+
timeoutMs: UPDATE_LOOKUP_TIMEOUT_MS,
|
|
198
|
+
retries: 0
|
|
199
|
+
});
|
|
200
|
+
if (!response.ok) {
|
|
201
|
+
throw new Error(`npm latest lookup failed (${response.status})`);
|
|
202
|
+
}
|
|
203
|
+
const body = await response.json();
|
|
204
|
+
if (typeof body.version !== "string" || body.version.trim().length === 0) {
|
|
205
|
+
throw new Error("npm latest lookup returned no version");
|
|
206
|
+
}
|
|
207
|
+
return normalizePublishedVersion(body.version);
|
|
208
|
+
}
|
|
209
|
+
async function fetchGithubLatestRelease(fetchFn = fetchWithTimeout) {
|
|
210
|
+
const response = await fetchFn(GITHUB_LATEST_RELEASE_API, {
|
|
211
|
+
headers: githubHeaders(),
|
|
212
|
+
timeoutMs: UPDATE_LOOKUP_TIMEOUT_MS,
|
|
213
|
+
retries: 0
|
|
214
|
+
});
|
|
215
|
+
if (!response.ok) {
|
|
216
|
+
throw new Error(`GitHub latest release lookup failed (${response.status})`);
|
|
217
|
+
}
|
|
218
|
+
const body = await response.json();
|
|
219
|
+
if (typeof body.tag_name !== "string" || body.tag_name.trim().length === 0) {
|
|
220
|
+
throw new Error("GitHub latest release lookup returned no tag");
|
|
221
|
+
}
|
|
222
|
+
const version = normalizePublishedVersion(body.tag_name);
|
|
223
|
+
const assets = Array.isArray(body.assets) ? body.assets.flatMap((asset) => {
|
|
224
|
+
if (!asset || typeof asset !== "object") {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
const record = asset;
|
|
228
|
+
if (typeof record.name !== "string" || typeof record.browser_download_url !== "string") {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
return [{ name: record.name, browser_download_url: record.browser_download_url }];
|
|
232
|
+
}) : [];
|
|
233
|
+
return {
|
|
234
|
+
version,
|
|
235
|
+
notes: typeof body.body === "string" ? body.body : "",
|
|
236
|
+
htmlUrl: typeof body.html_url === "string" && body.html_url.length > 0 ? body.html_url : githubReleaseHtmlUrl(version),
|
|
237
|
+
assets
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
async function lookupPublishedRelease(channel, fetchFn = fetchWithTimeout) {
|
|
241
|
+
switch (channel) {
|
|
242
|
+
case "github":
|
|
243
|
+
return fetchGithubLatestRelease(fetchFn);
|
|
244
|
+
case "npm": {
|
|
245
|
+
const version = await fetchNpmLatestVersion(fetchFn);
|
|
246
|
+
try {
|
|
247
|
+
const github = await fetchGithubLatestRelease(fetchFn);
|
|
248
|
+
if (github.version === version) {
|
|
249
|
+
return github;
|
|
250
|
+
}
|
|
251
|
+
} catch {
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
version,
|
|
255
|
+
notes: "",
|
|
256
|
+
htmlUrl: githubReleaseHtmlUrl(version),
|
|
257
|
+
assets: []
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
default: {
|
|
261
|
+
const exhaustive = channel;
|
|
262
|
+
return exhaustive;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function releaseFromCache(cache) {
|
|
267
|
+
if (!cache.latestVersion) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
version: cache.latestVersion,
|
|
272
|
+
notes: cache.latestNotes ?? "",
|
|
273
|
+
htmlUrl: cache.latestHtmlUrl ?? githubReleaseHtmlUrl(cache.latestVersion),
|
|
274
|
+
assets: cache.assets
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
async function resolvePublishedRelease(options) {
|
|
278
|
+
const harnesstapDir = options.harnesstapDir ?? getHarnesstapDir();
|
|
279
|
+
const now = options.now ?? /* @__PURE__ */ new Date();
|
|
280
|
+
const cache = loadUpdateCheckCache(harnesstapDir);
|
|
281
|
+
if (!options.forceRefresh && isUpdateLookupFresh(cache, now)) {
|
|
282
|
+
return releaseFromCache(cache);
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const release = await lookupPublishedRelease(
|
|
286
|
+
options.channel,
|
|
287
|
+
options.fetchFn ?? fetchWithTimeout
|
|
288
|
+
);
|
|
289
|
+
saveUpdateCheckCache(harnesstapDir, {
|
|
290
|
+
...cache,
|
|
291
|
+
lastLookupAt: now.toISOString(),
|
|
292
|
+
latestVersion: release.version,
|
|
293
|
+
latestNotes: release.notes,
|
|
294
|
+
latestHtmlUrl: release.htmlUrl,
|
|
295
|
+
assets: release.assets
|
|
296
|
+
});
|
|
297
|
+
return release;
|
|
298
|
+
} catch {
|
|
299
|
+
return releaseFromCache(cache);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function formatCliUpdateNotice(notice) {
|
|
303
|
+
return `A newer HarnessTap CLI is available: ${notice.currentVersion} \u2192 ${notice.latestVersion}. Install: ${notice.installHint}`;
|
|
304
|
+
}
|
|
305
|
+
function shouldSkipCliUpdateNotice(argv = process.argv, env = process.env) {
|
|
306
|
+
if (env.HARNESSTAP_SKIP_UPDATE_CHECK === "1" || env.HARNESSTAP_SKIP_UPDATE_CHECK === "true") {
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
if (env.HARNESSTAP_UPDATE_CHECK !== "1" && (Boolean(env.BUN_TEST) || env.NODE_ENV === "test")) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
const args = argv.slice(2);
|
|
313
|
+
if (args[0] === "__complete" || args[0] === "completion") {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
if (args.includes("--json")) {
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
320
|
+
const token = args[index];
|
|
321
|
+
if (token === "--format" && args[index + 1] === "json") {
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
if (token === "--format=json") {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
async function maybeNotifyCliUpdate(options = {}) {
|
|
331
|
+
const argv = options.argv ?? process.argv;
|
|
332
|
+
const env = options.env ?? process.env;
|
|
333
|
+
if (shouldSkipCliUpdateNotice(argv, env)) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
if (typeof process.exitCode === "number" && process.exitCode !== 0) {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
const harnesstapDir = options.harnesstapDir ?? getHarnesstapDir();
|
|
341
|
+
const currentVersion = options.currentVersion ?? PACKAGE_VERSION;
|
|
342
|
+
const channel = detectCliInstallChannel(argv[1], env);
|
|
343
|
+
const release = await resolvePublishedRelease({
|
|
344
|
+
channel,
|
|
345
|
+
harnesstapDir,
|
|
346
|
+
now: options.now,
|
|
347
|
+
fetchFn: options.fetchFn
|
|
348
|
+
});
|
|
349
|
+
if (!release || !isNewerPublishedVersion(release.version, currentVersion)) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const cache = loadUpdateCheckCache(harnesstapDir);
|
|
353
|
+
if (cache.notifiedVersion === release.version) {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
const notice = {
|
|
357
|
+
currentVersion: normalizePublishedVersion(currentVersion),
|
|
358
|
+
latestVersion: release.version,
|
|
359
|
+
channel,
|
|
360
|
+
installHint: cliInstallHint(channel, release.version),
|
|
361
|
+
htmlUrl: release.htmlUrl
|
|
362
|
+
};
|
|
363
|
+
const writeNotice = options.writeNotice ?? ((message) => {
|
|
364
|
+
console.error(message);
|
|
365
|
+
});
|
|
366
|
+
writeNotice(formatCliUpdateNotice(notice));
|
|
367
|
+
saveUpdateCheckCache(harnesstapDir, {
|
|
368
|
+
...loadUpdateCheckCache(harnesstapDir),
|
|
369
|
+
notifiedVersion: release.version
|
|
370
|
+
});
|
|
371
|
+
return notice;
|
|
372
|
+
} catch {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/services/github-device-flow.ts
|
|
378
|
+
function githubHeaders2(extra) {
|
|
379
|
+
return {
|
|
380
|
+
Accept: "application/json",
|
|
381
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
382
|
+
"User-Agent": publishedReleaseUserAgent(),
|
|
383
|
+
...extra
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function formBody(fields) {
|
|
387
|
+
const params = new URLSearchParams();
|
|
388
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
389
|
+
if (value !== void 0 && value.length > 0) {
|
|
390
|
+
params.set(key, value);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return params.toString();
|
|
394
|
+
}
|
|
395
|
+
function isRecord(value) {
|
|
396
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
397
|
+
}
|
|
398
|
+
function stringField(value) {
|
|
399
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
400
|
+
}
|
|
401
|
+
function numberField(value) {
|
|
402
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
403
|
+
}
|
|
404
|
+
async function readJson(fetchFn, input, init) {
|
|
405
|
+
const response = await fetchFn(input, init);
|
|
406
|
+
const text = await response.text();
|
|
407
|
+
if (!text.trim()) {
|
|
408
|
+
return { ok: response.ok, status: response.status, body: {}, text: "" };
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
return {
|
|
412
|
+
ok: response.ok,
|
|
413
|
+
status: response.status,
|
|
414
|
+
body: JSON.parse(text),
|
|
415
|
+
text
|
|
416
|
+
};
|
|
417
|
+
} catch {
|
|
418
|
+
return { ok: response.ok, status: response.status, body: {}, text };
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
function oauthErrorMessage(body, fallback) {
|
|
422
|
+
if (!isRecord(body)) {
|
|
423
|
+
return fallback;
|
|
424
|
+
}
|
|
425
|
+
const description = stringField(body.error_description) ?? stringField(body.message);
|
|
426
|
+
const code = stringField(body.error);
|
|
427
|
+
if (code === "incorrect_client_credentials") {
|
|
428
|
+
return "GitHub rejected the App credentials. Set HARNESSTAP_GITHUB_APP_CLIENT_SECRET if this GitHub App requires a client secret for device flow.";
|
|
429
|
+
}
|
|
430
|
+
if (description) {
|
|
431
|
+
return description;
|
|
432
|
+
}
|
|
433
|
+
if (code) {
|
|
434
|
+
return code;
|
|
435
|
+
}
|
|
436
|
+
return fallback;
|
|
437
|
+
}
|
|
438
|
+
function resolveGithubDeviceVerificationUris(device) {
|
|
439
|
+
const verificationUri = device.verification_uri?.trim() || GITHUB_DEVICE_VERIFICATION_URL;
|
|
440
|
+
const completeFromApi = device.verification_uri_complete?.trim();
|
|
441
|
+
return {
|
|
442
|
+
verification_uri: verificationUri,
|
|
443
|
+
verification_uri_complete: completeFromApi || `${verificationUri}?user_code=${encodeURIComponent(device.user_code)}`
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
async function requestGithubDeviceCode(opts = {}) {
|
|
447
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
448
|
+
const env = opts.env ?? process.env;
|
|
449
|
+
const clientId = resolveGithubAppClientId(env);
|
|
450
|
+
const { ok, status, body, text } = await readJson(
|
|
451
|
+
fetchFn,
|
|
452
|
+
GITHUB_DEVICE_CODE_URL,
|
|
453
|
+
{
|
|
454
|
+
method: "POST",
|
|
455
|
+
headers: githubHeaders2(),
|
|
456
|
+
body: formBody({
|
|
457
|
+
client_id: clientId
|
|
458
|
+
})
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
if (!ok) {
|
|
462
|
+
throw new Error(
|
|
463
|
+
oauthErrorMessage(body, `Failed to request GitHub device code (${status})`)
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
if (!isRecord(body)) {
|
|
467
|
+
throw new Error(`Failed to request GitHub device code: ${text || "empty response"}`);
|
|
468
|
+
}
|
|
469
|
+
const deviceCode = stringField(body.device_code);
|
|
470
|
+
const userCode = stringField(body.user_code);
|
|
471
|
+
if (!deviceCode || !userCode) {
|
|
472
|
+
throw new Error(
|
|
473
|
+
oauthErrorMessage(body, "GitHub device code response was missing codes")
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
const raw = {
|
|
477
|
+
device_code: deviceCode,
|
|
478
|
+
user_code: userCode,
|
|
479
|
+
verification_uri: stringField(body.verification_uri) ?? GITHUB_DEVICE_VERIFICATION_URL,
|
|
480
|
+
verification_uri_complete: stringField(body.verification_uri_complete),
|
|
481
|
+
expires_in: numberField(body.expires_in) ?? 900,
|
|
482
|
+
interval: numberField(body.interval) ?? 5
|
|
483
|
+
};
|
|
484
|
+
const uris = resolveGithubDeviceVerificationUris(raw);
|
|
485
|
+
return {
|
|
486
|
+
...raw,
|
|
487
|
+
verification_uri: uris.verification_uri,
|
|
488
|
+
verification_uri_complete: uris.verification_uri_complete
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
async function pollGithubDeviceTokenOnce(deviceCode, opts = {}) {
|
|
492
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
493
|
+
const env = opts.env ?? process.env;
|
|
494
|
+
const pollIntervalMs = opts.intervalMs ?? 5e3;
|
|
495
|
+
const { ok, status, body } = await readJson(
|
|
496
|
+
fetchFn,
|
|
497
|
+
GITHUB_OAUTH_ACCESS_TOKEN_URL,
|
|
498
|
+
{
|
|
499
|
+
method: "POST",
|
|
500
|
+
headers: githubHeaders2(),
|
|
501
|
+
body: formBody({
|
|
502
|
+
client_id: resolveGithubAppClientId(env),
|
|
503
|
+
device_code: deviceCode,
|
|
504
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
505
|
+
client_secret: resolveGithubAppClientSecret(env)
|
|
506
|
+
})
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
const errorCode = isRecord(body) ? stringField(body.error) : void 0;
|
|
510
|
+
if (errorCode === "authorization_pending") {
|
|
511
|
+
return { status: "pending", intervalMs: pollIntervalMs };
|
|
512
|
+
}
|
|
513
|
+
if (errorCode === "slow_down") {
|
|
514
|
+
return { status: "pending", intervalMs: pollIntervalMs + 5e3 };
|
|
515
|
+
}
|
|
516
|
+
if (errorCode === "expired_token") {
|
|
517
|
+
return { status: "error", message: "GitHub login code expired" };
|
|
518
|
+
}
|
|
519
|
+
if (errorCode === "access_denied") {
|
|
520
|
+
return { status: "error", message: "GitHub login was denied" };
|
|
521
|
+
}
|
|
522
|
+
if (errorCode) {
|
|
523
|
+
return {
|
|
524
|
+
status: "error",
|
|
525
|
+
message: oauthErrorMessage(body, errorCode)
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
if (ok && isRecord(body) && stringField(body.access_token)) {
|
|
529
|
+
return {
|
|
530
|
+
status: "authorized",
|
|
531
|
+
token: {
|
|
532
|
+
access_token: stringField(body.access_token),
|
|
533
|
+
token_type: stringField(body.token_type),
|
|
534
|
+
expires_in: numberField(body.expires_in),
|
|
535
|
+
refresh_token: stringField(body.refresh_token),
|
|
536
|
+
refresh_token_expires_in: numberField(body.refresh_token_expires_in)
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
if (status >= 500 || status === 408 || status === 429) {
|
|
541
|
+
return { status: "pending", intervalMs: pollIntervalMs };
|
|
542
|
+
}
|
|
543
|
+
return {
|
|
544
|
+
status: "error",
|
|
545
|
+
message: oauthErrorMessage(body, `Failed to poll GitHub device token (${status})`)
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
async function pollGithubDeviceToken(deviceCode, opts = {}) {
|
|
549
|
+
const sleep = opts.sleep ?? ((ms) => new Promise((resolve) => {
|
|
550
|
+
setTimeout(resolve, ms);
|
|
551
|
+
}));
|
|
552
|
+
let pollIntervalMs = (opts.interval ?? 5) * 1e3;
|
|
553
|
+
const maxPolls = opts.maxPolls ?? 180;
|
|
554
|
+
for (let attempt = 0; attempt < maxPolls; attempt += 1) {
|
|
555
|
+
const result = await pollGithubDeviceTokenOnce(deviceCode, {
|
|
556
|
+
...opts,
|
|
557
|
+
intervalMs: pollIntervalMs
|
|
558
|
+
});
|
|
559
|
+
if (result.status === "authorized") {
|
|
560
|
+
return result.token;
|
|
561
|
+
}
|
|
562
|
+
if (result.status === "error") {
|
|
563
|
+
throw new Error(result.message);
|
|
564
|
+
}
|
|
565
|
+
pollIntervalMs = result.intervalMs;
|
|
566
|
+
if (pollIntervalMs > 0) {
|
|
567
|
+
await sleep(pollIntervalMs);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
throw new Error("Timed out waiting for GitHub device login");
|
|
571
|
+
}
|
|
572
|
+
async function fetchGithubUser(accessToken, opts = {}) {
|
|
573
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
574
|
+
const response = await fetchFn(`${GITHUB_API_URL}/user`, {
|
|
575
|
+
headers: {
|
|
576
|
+
Accept: "application/vnd.github+json",
|
|
577
|
+
Authorization: `Bearer ${accessToken}`,
|
|
578
|
+
"User-Agent": publishedReleaseUserAgent(),
|
|
579
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
const text = await response.text();
|
|
583
|
+
if (!response.ok) {
|
|
584
|
+
throw new Error(`Failed to read GitHub user (${response.status})`);
|
|
585
|
+
}
|
|
586
|
+
let body = {};
|
|
587
|
+
try {
|
|
588
|
+
body = JSON.parse(text);
|
|
589
|
+
} catch {
|
|
590
|
+
throw new Error("GitHub user response was not JSON");
|
|
591
|
+
}
|
|
592
|
+
if (!isRecord(body) || typeof body.login !== "string") {
|
|
593
|
+
throw new Error("GitHub user response was missing login");
|
|
594
|
+
}
|
|
595
|
+
return {
|
|
596
|
+
login: body.login,
|
|
597
|
+
id: typeof body.id === "number" ? body.id : 0,
|
|
598
|
+
...typeof body.name === "string" && body.name.trim() ? { name: body.name.trim() } : {}
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
async function refreshGithubAccessToken(refreshToken, opts = {}) {
|
|
602
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
603
|
+
const env = opts.env ?? process.env;
|
|
604
|
+
const secret = resolveGithubAppClientSecret(env);
|
|
605
|
+
if (!secret) {
|
|
606
|
+
throw new Error(
|
|
607
|
+
"GitHub token refresh requires HARNESSTAP_GITHUB_APP_CLIENT_SECRET"
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
const { ok, status, body } = await readJson(
|
|
611
|
+
fetchFn,
|
|
612
|
+
GITHUB_OAUTH_ACCESS_TOKEN_URL,
|
|
613
|
+
{
|
|
614
|
+
method: "POST",
|
|
615
|
+
headers: githubHeaders2(),
|
|
616
|
+
body: formBody({
|
|
617
|
+
client_id: resolveGithubAppClientId(env),
|
|
618
|
+
client_secret: secret,
|
|
619
|
+
grant_type: "refresh_token",
|
|
620
|
+
refresh_token: refreshToken
|
|
621
|
+
})
|
|
622
|
+
}
|
|
623
|
+
);
|
|
624
|
+
if (!ok || !isRecord(body) || !stringField(body.access_token)) {
|
|
625
|
+
throw new Error(
|
|
626
|
+
oauthErrorMessage(body, `Failed to refresh GitHub token (${status})`)
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
access_token: stringField(body.access_token),
|
|
631
|
+
token_type: stringField(body.token_type),
|
|
632
|
+
expires_in: numberField(body.expires_in),
|
|
633
|
+
refresh_token: stringField(body.refresh_token),
|
|
634
|
+
refresh_token_expires_in: numberField(body.refresh_token_expires_in)
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
function sessionFromDeviceToken(token, identity, opts = {}) {
|
|
638
|
+
const env = opts.env ?? process.env;
|
|
639
|
+
const now = Math.floor((opts.now ?? Date.now)() / 1e3);
|
|
640
|
+
return {
|
|
641
|
+
clientId: resolveGithubAppClientId(env),
|
|
642
|
+
accessToken: token.access_token,
|
|
643
|
+
tokenType: token.token_type,
|
|
644
|
+
...token.expires_in ? { accessTokenExpiresAt: now + token.expires_in } : {},
|
|
645
|
+
...token.refresh_token ? { refreshToken: token.refresh_token } : {},
|
|
646
|
+
...token.refresh_token_expires_in ? { refreshTokenExpiresAt: now + token.refresh_token_expires_in } : {},
|
|
647
|
+
...identity ? {
|
|
648
|
+
login: identity.login,
|
|
649
|
+
userId: identity.id,
|
|
650
|
+
...identity.name ? { name: identity.name } : {}
|
|
651
|
+
} : {}
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/services/github-credentials.ts
|
|
656
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
657
|
+
import { spawnSync } from "child_process";
|
|
658
|
+
var ENV_SOURCES = [
|
|
659
|
+
{ key: "HARNESSTAP_GITHUB_TOKEN", source: "harnesstap_github_token" },
|
|
660
|
+
{ key: "GH_TOKEN", source: "gh_token" },
|
|
661
|
+
{ key: "GITHUB_TOKEN", source: "github_token" }
|
|
662
|
+
];
|
|
663
|
+
var ghCliTokenCache;
|
|
664
|
+
function envToken(env) {
|
|
665
|
+
for (const entry of ENV_SOURCES) {
|
|
666
|
+
const value = env[entry.key]?.trim();
|
|
667
|
+
if (value) {
|
|
668
|
+
return { token: value, source: entry.source };
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
function sessionTokenUsable(session, nowSeconds) {
|
|
674
|
+
if (!session?.accessToken) {
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
if (typeof session.accessTokenExpiresAt === "number" && session.accessTokenExpiresAt <= nowSeconds + 30) {
|
|
678
|
+
return null;
|
|
679
|
+
}
|
|
680
|
+
return session.accessToken;
|
|
681
|
+
}
|
|
682
|
+
function readGhCliToken(runCommand) {
|
|
683
|
+
if (ghCliTokenCache) {
|
|
684
|
+
return ghCliTokenCache.token;
|
|
685
|
+
}
|
|
686
|
+
try {
|
|
687
|
+
const result = runCommand ? runCommand("gh", ["auth", "token"], { timeoutMs: 3e3 }) : spawnSync("gh", ["auth", "token"], {
|
|
688
|
+
encoding: "utf-8",
|
|
689
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
690
|
+
timeout: 3e3
|
|
691
|
+
});
|
|
692
|
+
const exitCode = "exitCode" in result ? result.exitCode : result.status ?? 1;
|
|
693
|
+
const stdout = result.stdout?.toString() ?? "";
|
|
694
|
+
const token = exitCode === 0 ? stdout.trim() : "";
|
|
695
|
+
ghCliTokenCache = { token: token.length > 0 ? token : null };
|
|
696
|
+
return ghCliTokenCache.token;
|
|
697
|
+
} catch {
|
|
698
|
+
ghCliTokenCache = { token: null };
|
|
699
|
+
return null;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function resolveGithubAccessToken(opts = {}) {
|
|
703
|
+
const env = opts.env ?? process.env;
|
|
704
|
+
const nowSeconds = opts.nowSeconds ?? Math.floor(Date.now() / 1e3);
|
|
705
|
+
const fromEnv = envToken(env);
|
|
706
|
+
if (fromEnv) {
|
|
707
|
+
return fromEnv;
|
|
708
|
+
}
|
|
709
|
+
const session = opts.session === void 0 ? loadGithubSession() : opts.session;
|
|
710
|
+
const fromSession = sessionTokenUsable(session, nowSeconds);
|
|
711
|
+
if (fromSession) {
|
|
712
|
+
return { token: fromSession, source: "harnesstap_session" };
|
|
713
|
+
}
|
|
714
|
+
if (opts.lookupGhCli !== false) {
|
|
715
|
+
const fromGh = readGhCliToken(opts.runCommand);
|
|
716
|
+
if (fromGh) {
|
|
717
|
+
return { token: fromGh, source: "gh_cli" };
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
return { token: null, source: "none" };
|
|
721
|
+
}
|
|
722
|
+
async function ensureGithubSessionAccess(opts = {}) {
|
|
723
|
+
const env = opts.env ?? process.env;
|
|
724
|
+
const nowSeconds = opts.nowSeconds ?? Math.floor(Date.now() / 1e3);
|
|
725
|
+
const session = opts.session === void 0 ? loadGithubSession() : opts.session;
|
|
726
|
+
if (!session?.accessToken) {
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
if (sessionTokenUsable(session, nowSeconds)) {
|
|
730
|
+
return session;
|
|
731
|
+
}
|
|
732
|
+
if (!session.refreshToken || !resolveGithubAppClientSecret(env)) {
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
const token = await refreshGithubAccessToken(session.refreshToken, { env });
|
|
737
|
+
const next = sessionFromDeviceToken(token, {
|
|
738
|
+
login: session.login ?? "",
|
|
739
|
+
id: session.userId ?? 0,
|
|
740
|
+
...session.name ? { name: session.name } : {}
|
|
741
|
+
}, { env, now: () => nowSeconds * 1e3 });
|
|
742
|
+
const merged = {
|
|
743
|
+
...session,
|
|
744
|
+
...next,
|
|
745
|
+
login: next.login || session.login,
|
|
746
|
+
userId: next.userId || session.userId,
|
|
747
|
+
name: next.name ?? session.name
|
|
748
|
+
};
|
|
749
|
+
saveGithubSession(merged);
|
|
750
|
+
return merged;
|
|
751
|
+
} catch {
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
function githubGitHttpAuthorizationHeader(token) {
|
|
756
|
+
const basic = Buffer2.from(`x-access-token:${token}`, "utf8").toString("base64");
|
|
757
|
+
return `AUTHORIZATION: basic ${basic}`;
|
|
758
|
+
}
|
|
759
|
+
function githubGitConfigArgs(token) {
|
|
760
|
+
const args = ["-c", "protocol.file.allow=always"];
|
|
761
|
+
if (!token) {
|
|
762
|
+
return args;
|
|
763
|
+
}
|
|
764
|
+
const sanitized = token.replace(/[\r\n]/g, "");
|
|
765
|
+
if (!sanitized) {
|
|
766
|
+
return args;
|
|
767
|
+
}
|
|
768
|
+
const extraHeader = `http.https://github.com/.extraheader=${githubGitHttpAuthorizationHeader(sanitized)}`;
|
|
769
|
+
args.push("-c", "http.https://github.com/.extraheader=", "-c", extraHeader);
|
|
770
|
+
return args;
|
|
771
|
+
}
|
|
772
|
+
function ensureGithubGitConfigArgs(args) {
|
|
773
|
+
if (args[0] === "-c") {
|
|
774
|
+
return args;
|
|
775
|
+
}
|
|
776
|
+
return [...githubGitConfigArgs(resolveGithubAccessToken().token), ...args];
|
|
777
|
+
}
|
|
778
|
+
var GITHUB_SCP = /^(?:ssh:\/\/)?(?:[^@/\s]+@)?(github\.com|gist\.github\.com)[:/](.+)$/i;
|
|
779
|
+
function githubHttpsRemoteForAuth(url, token) {
|
|
780
|
+
if (!token) {
|
|
781
|
+
return url;
|
|
782
|
+
}
|
|
783
|
+
const trimmed = url.trim();
|
|
784
|
+
if (!trimmed) {
|
|
785
|
+
return url;
|
|
786
|
+
}
|
|
787
|
+
try {
|
|
788
|
+
const parsed = new URL(trimmed);
|
|
789
|
+
if ((parsed.protocol === "https:" || parsed.protocol === "http:") && (parsed.hostname === "github.com" || parsed.hostname === "www.github.com" || parsed.hostname === "gist.github.com")) {
|
|
790
|
+
return trimmed;
|
|
791
|
+
}
|
|
792
|
+
} catch {
|
|
793
|
+
}
|
|
794
|
+
const scp = trimmed.match(GITHUB_SCP);
|
|
795
|
+
if (scp?.[1] && scp[2]) {
|
|
796
|
+
const path = scp[2].replace(/^\/+/, "").replace(/\.git$/i, "");
|
|
797
|
+
return `https://${scp[1]}/${path}.git`;
|
|
798
|
+
}
|
|
799
|
+
return url;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
export {
|
|
803
|
+
loadGithubSession,
|
|
804
|
+
saveGithubSession,
|
|
805
|
+
clearGithubSession,
|
|
806
|
+
resolveGithubAppClientId,
|
|
807
|
+
maybeNotifyCliUpdate,
|
|
808
|
+
requestGithubDeviceCode,
|
|
809
|
+
pollGithubDeviceToken,
|
|
810
|
+
fetchGithubUser,
|
|
811
|
+
sessionFromDeviceToken,
|
|
812
|
+
resolveGithubAccessToken,
|
|
813
|
+
ensureGithubSessionAccess,
|
|
814
|
+
githubGitConfigArgs,
|
|
815
|
+
ensureGithubGitConfigArgs,
|
|
816
|
+
githubHttpsRemoteForAuth
|
|
817
|
+
};
|
|
818
|
+
//# sourceMappingURL=chunk-GQKKX7OE.js.map
|