@primitive.ai/prim 0.1.0-alpha.128 → 0.1.0-alpha.129
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/{chunk-6QAXVFRJ.js → chunk-BVGASV3B.js} +942 -123
- package/dist/{chunk-M6KIDB5R.js → chunk-DYXCJ6WZ.js} +10 -10
- package/dist/{chunk-B3ICJZXV.js → chunk-FHDTJP2C.js} +2 -2
- package/dist/{chunk-XINFTNA2.js → chunk-HDL3UXYJ.js} +5 -5
- package/dist/{chunk-C357P37M.js → chunk-IJ3W2YBM.js} +4 -4
- package/dist/{chunk-N6PZE7HX.js → chunk-IZAEAALI.js} +2 -2
- package/dist/{chunk-PTZ6BWSU.js → chunk-KZNOT2YH.js} +2 -2
- package/dist/{chunk-P4O7L7JZ.js → chunk-L7U3G4XR.js} +2 -2
- package/dist/{chunk-EQWUCFXK.js → chunk-LBZJD2I4.js} +3 -5
- package/dist/{chunk-PRJ7P6J5.js → chunk-MMMY57UA.js} +22 -837
- package/dist/{chunk-WEGZGF3K.js → chunk-NWWTFQP2.js} +1 -1
- package/dist/{chunk-NYDM4HSS.js → chunk-QSDWWEHF.js} +2 -2
- package/dist/{chunk-3KPTVK4N.js → chunk-RAEOSDQ6.js} +1 -1
- package/dist/{chunk-E57UVUJL.js → chunk-RS5ADTDF.js} +4 -4
- package/dist/{chunk-ANMNGGTN.js → chunk-RWJ7WHTJ.js} +2 -2
- package/dist/{chunk-RDT2IV2Z.js → chunk-SJE7OWVS.js} +3 -3
- package/dist/{chunk-VLT2KGPE.js → chunk-USJ4JGDC.js} +3 -3
- package/dist/{chunk-ZCT23NBS.js → chunk-YPHDV5AS.js} +2 -2
- package/dist/{chunk-QQ3G5SXX.js → chunk-YSV43WLK.js} +1 -1
- package/dist/{chunk-4DYEBYKM.js → chunk-ZKIBD5MO.js} +2 -2
- package/dist/daemon/server.js +624 -289
- package/dist/hook-runtime/daemon/server.js +624 -289
- package/dist/hook-runtime/hooks/post-commit.js +307 -186
- package/dist/hook-runtime/hooks/post-rewrite.js +302 -181
- package/dist/hook-runtime/hooks/post-tool-use.js +490 -276
- package/dist/hook-runtime/hooks/pre-commit.js +516 -181
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +221 -7
- package/dist/hook-runtime/hooks/prim-hook.js +511 -297
- package/dist/hook-runtime/hooks/session-end.js +229 -229
- package/dist/hook-runtime/hooks/session-start.js +170 -14
- package/dist/hook-runtime/index.js +1623 -1607
- package/dist/hook-runtime/statusline-main.js +245 -124
- package/dist/hooks/post-commit.js +7 -7
- package/dist/hooks/post-rewrite.js +6 -6
- package/dist/hooks/post-tool-use.js +15 -15
- package/dist/hooks/pre-commit.js +4 -4
- package/dist/hooks/pre-tool-use-worker.js +9 -9
- package/dist/hooks/prim-hook.js +16 -16
- package/dist/hooks/session-end.js +4 -4
- package/dist/hooks/session-start.js +12 -13
- package/dist/index.js +24 -24
- package/dist/statusline-main.js +245 -124
- package/package.json +1 -1
|
@@ -1046,14 +1046,14 @@ function throwIfAborted(signal) {
|
|
|
1046
1046
|
}
|
|
1047
1047
|
async function defaultSleep(ms, signal) {
|
|
1048
1048
|
if (!signal) {
|
|
1049
|
-
await new Promise((
|
|
1049
|
+
await new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
1050
1050
|
return;
|
|
1051
1051
|
}
|
|
1052
1052
|
throwIfAborted(signal);
|
|
1053
|
-
await new Promise((
|
|
1053
|
+
await new Promise((resolve3, reject) => {
|
|
1054
1054
|
const timer = setTimeout(() => {
|
|
1055
1055
|
signal.removeEventListener("abort", onAbort);
|
|
1056
|
-
|
|
1056
|
+
resolve3();
|
|
1057
1057
|
}, ms);
|
|
1058
1058
|
const onAbort = () => {
|
|
1059
1059
|
clearTimeout(timer);
|
|
@@ -1122,6 +1122,810 @@ async function withFileLock(lockDir, operation, options = {}) {
|
|
|
1122
1122
|
}
|
|
1123
1123
|
}
|
|
1124
1124
|
|
|
1125
|
+
// src/lib/git-hook-contract.ts
|
|
1126
|
+
var MANAGED_GIT_HOOK_NAMES = ["pre-commit", "post-commit", "post-rewrite"];
|
|
1127
|
+
var GIT_HOOK_ENTRYPOINT_NAME = "prim-git-hook-v1";
|
|
1128
|
+
var GIT_HOOK_CONTRACT_VERSION = 1;
|
|
1129
|
+
var CONTRACT_LINE_PREFIX = "# prim git hook v";
|
|
1130
|
+
var CONTRACT_LINE_RE = /^# prim git hook v([0-9]+)\b/mu;
|
|
1131
|
+
function blockMarkers(hook) {
|
|
1132
|
+
return { start: `# >>> prim ${hook} hook >>>`, end: `# <<< prim ${hook} hook <<<` };
|
|
1133
|
+
}
|
|
1134
|
+
function entrypointCommand(hook) {
|
|
1135
|
+
const script = [
|
|
1136
|
+
PRIM_CONFIG_SHELL_RESOLVER,
|
|
1137
|
+
`case "$prim_config" in /) prim_entry=/${GIT_HOOK_ENTRYPOINT_NAME} ;; `,
|
|
1138
|
+
`*) prim_entry="$prim_config/${GIT_HOOK_ENTRYPOINT_NAME}" ;; esac; `,
|
|
1139
|
+
'[ -x "$prim_entry" ] || exit 0; exec "$prim_entry" "$@"'
|
|
1140
|
+
].join("");
|
|
1141
|
+
return `/bin/sh -c ${shellQuote(script)} ${GIT_HOOK_ENTRYPOINT_NAME} ${hook} "$@"`;
|
|
1142
|
+
}
|
|
1143
|
+
function managedHookBlock(hook) {
|
|
1144
|
+
const { start, end } = blockMarkers(hook);
|
|
1145
|
+
const header = `${CONTRACT_LINE_PREFIX}${GIT_HOOK_CONTRACT_VERSION}: a no-op unless prim is installed and enabled here (see \`prim hooks snippet ${hook}\`).`;
|
|
1146
|
+
if (hook !== "post-rewrite") {
|
|
1147
|
+
return `${start}
|
|
1148
|
+
${header}
|
|
1149
|
+
# shellcheck disable=SC2016
|
|
1150
|
+
${entrypointCommand(hook)} || :
|
|
1151
|
+
${end}`;
|
|
1152
|
+
}
|
|
1153
|
+
return `${start}
|
|
1154
|
+
${header}
|
|
1155
|
+
prim_rewrite_stdin=$(mktemp 2>/dev/null) || prim_rewrite_stdin=
|
|
1156
|
+
if test -n "\${prim_rewrite_stdin}" && cat >"\${prim_rewrite_stdin}"; then
|
|
1157
|
+
# shellcheck disable=SC2016
|
|
1158
|
+
${entrypointCommand(hook)} <"\${prim_rewrite_stdin}" || :
|
|
1159
|
+
exec <"\${prim_rewrite_stdin}"
|
|
1160
|
+
fi
|
|
1161
|
+
if test -n "\${prim_rewrite_stdin}"; then rm -f "\${prim_rewrite_stdin}"; fi
|
|
1162
|
+
unset prim_rewrite_stdin
|
|
1163
|
+
${end}`;
|
|
1164
|
+
}
|
|
1165
|
+
var OPEN_WORDS = /* @__PURE__ */ new Set(["then", "do", "else", "{", "(", "&&", "||", "|"]);
|
|
1166
|
+
function canonicalHookBlock(text) {
|
|
1167
|
+
const tokens = [];
|
|
1168
|
+
let word = "";
|
|
1169
|
+
let quote;
|
|
1170
|
+
const separate = () => {
|
|
1171
|
+
const last = tokens.at(-1);
|
|
1172
|
+
if (last !== void 0 && last !== ";" && !OPEN_WORDS.has(last)) tokens.push(";");
|
|
1173
|
+
};
|
|
1174
|
+
const flush = () => {
|
|
1175
|
+
if (word === "") return;
|
|
1176
|
+
const last = tokens.at(-1);
|
|
1177
|
+
if (last !== void 0 && /^[0-9]*(?:<|>|>>|<&|>&)$/u.test(last))
|
|
1178
|
+
tokens[tokens.length - 1] += word;
|
|
1179
|
+
else tokens.push(word);
|
|
1180
|
+
word = "";
|
|
1181
|
+
};
|
|
1182
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
1183
|
+
const char = text[index];
|
|
1184
|
+
if (quote) {
|
|
1185
|
+
word += char;
|
|
1186
|
+
if (char === quote) quote = void 0;
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
if (char === "'" || char === '"') {
|
|
1190
|
+
quote = char;
|
|
1191
|
+
word += char;
|
|
1192
|
+
} else if (char === "#" && word === "") {
|
|
1193
|
+
const end = text.indexOf("\n", index);
|
|
1194
|
+
const comment = text.slice(index, end === -1 ? text.length : end);
|
|
1195
|
+
tokens.push(comment.replace(/\s+/gu, " ").trim());
|
|
1196
|
+
index = (end === -1 ? text.length : end) - 1;
|
|
1197
|
+
} else if (char === "\n" || char === ";") {
|
|
1198
|
+
flush();
|
|
1199
|
+
if (char === ";" && text[index + 1] === ";") {
|
|
1200
|
+
tokens.push(";;");
|
|
1201
|
+
index += 1;
|
|
1202
|
+
} else {
|
|
1203
|
+
separate();
|
|
1204
|
+
}
|
|
1205
|
+
} else if (/\s/u.test(char)) {
|
|
1206
|
+
flush();
|
|
1207
|
+
} else {
|
|
1208
|
+
word += char;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
flush();
|
|
1212
|
+
while (tokens.at(-1) === ";") tokens.pop();
|
|
1213
|
+
return tokens.join("\0");
|
|
1214
|
+
}
|
|
1215
|
+
function hookBlockContractVersion(block) {
|
|
1216
|
+
const match = CONTRACT_LINE_RE.exec(block);
|
|
1217
|
+
return match?.[1] ? Number(match[1]) : void 0;
|
|
1218
|
+
}
|
|
1219
|
+
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
1220
|
+
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
1221
|
+
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
1222
|
+
# where prim is active (git config prim.active true) and never fails the hook.
|
|
1223
|
+
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
1224
|
+
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
1225
|
+
[ -x "$prim_launcher" ] || exit 0
|
|
1226
|
+
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
1227
|
+
prim_hook=\${1-}
|
|
1228
|
+
if [ "$#" -gt 0 ]; then shift; fi
|
|
1229
|
+
case "$prim_hook" in
|
|
1230
|
+
pre-commit)
|
|
1231
|
+
"$prim_launcher" prim-pre-commit "$@" || :
|
|
1232
|
+
;;
|
|
1233
|
+
post-commit)
|
|
1234
|
+
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
1235
|
+
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
1236
|
+
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
1237
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
1238
|
+
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
1239
|
+
(
|
|
1240
|
+
trap '' HUP
|
|
1241
|
+
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
1242
|
+
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
1243
|
+
) </dev/null >/dev/null 2>&1 &
|
|
1244
|
+
;;
|
|
1245
|
+
post-rewrite)
|
|
1246
|
+
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
1247
|
+
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
1248
|
+
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
1249
|
+
rm -f "$prim_pairs"
|
|
1250
|
+
exit 0
|
|
1251
|
+
fi
|
|
1252
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
1253
|
+
(
|
|
1254
|
+
trap '' HUP
|
|
1255
|
+
trap 'rm -f "$prim_pairs"' 0
|
|
1256
|
+
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
1257
|
+
) </dev/null >/dev/null 2>&1 &
|
|
1258
|
+
;;
|
|
1259
|
+
esac
|
|
1260
|
+
exit 0
|
|
1261
|
+
`;
|
|
1262
|
+
|
|
1263
|
+
// src/lib/hook-runtime.ts
|
|
1264
|
+
import { createHash as createHash2, randomBytes as randomBytes2 } from "crypto";
|
|
1265
|
+
import {
|
|
1266
|
+
constants as constants2,
|
|
1267
|
+
accessSync,
|
|
1268
|
+
chmodSync,
|
|
1269
|
+
copyFileSync,
|
|
1270
|
+
existsSync as existsSync6,
|
|
1271
|
+
lstatSync as lstatSync2,
|
|
1272
|
+
mkdirSync as mkdirSync4,
|
|
1273
|
+
mkdtempSync,
|
|
1274
|
+
readFileSync as readFileSync6,
|
|
1275
|
+
readdirSync,
|
|
1276
|
+
realpathSync as realpathSync2,
|
|
1277
|
+
renameSync as renameSync2,
|
|
1278
|
+
rmSync as rmSync4,
|
|
1279
|
+
statSync as statSync2,
|
|
1280
|
+
utimesSync,
|
|
1281
|
+
writeFileSync as writeFileSync4
|
|
1282
|
+
} from "fs";
|
|
1283
|
+
import { delimiter, dirname as dirname6, isAbsolute as isAbsolute4, join as join8, resolve as resolve2 } from "path";
|
|
1284
|
+
|
|
1285
|
+
// src/lib/native-keyring.ts
|
|
1286
|
+
import { existsSync as existsSync5 } from "fs";
|
|
1287
|
+
import { createRequire } from "module";
|
|
1288
|
+
import { dirname as dirname5, join as join7 } from "path";
|
|
1289
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
1290
|
+
var require2 = createRequire(import.meta.url);
|
|
1291
|
+
var KEYRING_LOADER = 'module.exports = require("./keyring.node");\n';
|
|
1292
|
+
var KEYRING_NATIVE_FILES = ["native/keyring.cjs", "native/keyring.node"];
|
|
1293
|
+
function keyringNativeBinary() {
|
|
1294
|
+
let directory = dirname5(fileURLToPath2(import.meta.url));
|
|
1295
|
+
for (let depth = 0; depth < 5; depth++) {
|
|
1296
|
+
const binary2 = join7(directory, "native", "keyring.node");
|
|
1297
|
+
if (existsSync5(binary2)) return binary2;
|
|
1298
|
+
directory = dirname5(directory);
|
|
1299
|
+
}
|
|
1300
|
+
const packagePath = require2.resolve("@napi-rs/keyring");
|
|
1301
|
+
require2(packagePath);
|
|
1302
|
+
const entry = require2.cache[packagePath];
|
|
1303
|
+
const binary = entry?.children.find(
|
|
1304
|
+
(child) => child.filename.endsWith(".node") && child.filename.includes("keyring")
|
|
1305
|
+
);
|
|
1306
|
+
if (!binary || !existsSync5(binary.filename))
|
|
1307
|
+
throw new Error(
|
|
1308
|
+
"Cannot stage the native credential store; reinstall Prim with optional dependencies enabled."
|
|
1309
|
+
);
|
|
1310
|
+
return binary.filename;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// src/lib/hook-runtime.ts
|
|
1314
|
+
var HOOK_RUNTIME_SCHEMA_VERSION = 1;
|
|
1315
|
+
var DIRECTORY_MODE2 = 448;
|
|
1316
|
+
var DATA_FILE_MODE = 384;
|
|
1317
|
+
var LAUNCHER_MODE = 448;
|
|
1318
|
+
var RELEASE_PREFIX = "release-";
|
|
1319
|
+
var RELEASE_NAME_RE = /^release-[0-9a-f]{64}$/u;
|
|
1320
|
+
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
1321
|
+
var MAX_RELEASES = 8;
|
|
1322
|
+
var MAX_RELEASE_DELETIONS_PER_STAGE = 8;
|
|
1323
|
+
var RETAIN_PREDECESSORS = 2;
|
|
1324
|
+
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
1325
|
+
var HOOK_RUNTIME_ENTRIES = {
|
|
1326
|
+
// SessionStart self-healing resolves this exact CLI entry to run
|
|
1327
|
+
// `daemon ensure`; it must travel with the immutable hook release.
|
|
1328
|
+
prim: "dist/index.js",
|
|
1329
|
+
"prim-hook": "dist/hooks/prim-hook.js",
|
|
1330
|
+
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
1331
|
+
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
1332
|
+
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
1333
|
+
"prim-pre-tool-use-worker": "dist/hooks/pre-tool-use-worker.js",
|
|
1334
|
+
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
1335
|
+
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
1336
|
+
"prim-session-start": "dist/hooks/session-start.js",
|
|
1337
|
+
"prim-session-end": "dist/hooks/session-end.js",
|
|
1338
|
+
"prim-daemon-server": "dist/daemon/server.js",
|
|
1339
|
+
"prim-statusline": "dist/statusline-main.js"
|
|
1340
|
+
};
|
|
1341
|
+
var SOURCE_ENTRIES = Object.fromEntries(
|
|
1342
|
+
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
1343
|
+
bin,
|
|
1344
|
+
target.replace(/^dist\//u, "")
|
|
1345
|
+
])
|
|
1346
|
+
);
|
|
1347
|
+
function hookRuntimePaths(options = {}) {
|
|
1348
|
+
const env = options.env ?? process.env;
|
|
1349
|
+
const configDir = primConfigDirectory({
|
|
1350
|
+
...options,
|
|
1351
|
+
env,
|
|
1352
|
+
homeDir: options.homeDir ?? env.HOME ?? ""
|
|
1353
|
+
});
|
|
1354
|
+
const runtimeDir = join8(configDir, "hook-runtime");
|
|
1355
|
+
return {
|
|
1356
|
+
configDir,
|
|
1357
|
+
launcher: join8(configDir, STABLE_HOOK_LAUNCHER_NAME),
|
|
1358
|
+
gitHookEntrypoint: join8(configDir, GIT_HOOK_ENTRYPOINT_NAME),
|
|
1359
|
+
runtimeDir,
|
|
1360
|
+
releasesDir: join8(runtimeDir, "releases"),
|
|
1361
|
+
current: join8(runtimeDir, "current"),
|
|
1362
|
+
selectionLock: join8(runtimeDir, "selection.lock")
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
function sha256(path) {
|
|
1366
|
+
return createHash2("sha256").update(readFileSync6(path)).digest("hex");
|
|
1367
|
+
}
|
|
1368
|
+
function exactManifest(manifest) {
|
|
1369
|
+
return `${JSON.stringify(manifest, null, 2)}
|
|
1370
|
+
`;
|
|
1371
|
+
}
|
|
1372
|
+
function exactKeys(value, expected) {
|
|
1373
|
+
const actual = Object.keys(value).sort();
|
|
1374
|
+
const wanted = [...expected].sort();
|
|
1375
|
+
return actual.length === wanted.length && actual.every((key, index) => key === wanted[index]);
|
|
1376
|
+
}
|
|
1377
|
+
function releaseId(manifest) {
|
|
1378
|
+
return createHash2("sha256").update(exactManifest(manifest)).digest("hex");
|
|
1379
|
+
}
|
|
1380
|
+
function runtimePackageJson(manifest) {
|
|
1381
|
+
return `${JSON.stringify(
|
|
1382
|
+
{
|
|
1383
|
+
name: "@primitive.ai/prim",
|
|
1384
|
+
version: manifest.version,
|
|
1385
|
+
type: "module",
|
|
1386
|
+
bin: HOOK_RUNTIME_ENTRIES
|
|
1387
|
+
},
|
|
1388
|
+
null,
|
|
1389
|
+
2
|
|
1390
|
+
)}
|
|
1391
|
+
`;
|
|
1392
|
+
}
|
|
1393
|
+
function validateRelease(releaseDir, manifest) {
|
|
1394
|
+
try {
|
|
1395
|
+
if (!directReleaseLayout(releaseDir, manifest)) return false;
|
|
1396
|
+
if (readFileSync6(join8(releaseDir, "manifest.json"), "utf8") !== exactManifest(manifest)) {
|
|
1397
|
+
return false;
|
|
1398
|
+
}
|
|
1399
|
+
if (readFileSync6(join8(releaseDir, "package.json"), "utf8") !== runtimePackageJson(manifest)) {
|
|
1400
|
+
return false;
|
|
1401
|
+
}
|
|
1402
|
+
if (readFileSync6(join8(releaseDir, "node"), "utf8") !== `${manifest.nodePath}
|
|
1403
|
+
`) {
|
|
1404
|
+
return false;
|
|
1405
|
+
}
|
|
1406
|
+
for (const [bin, relativePath] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
1407
|
+
const target = join8(releaseDir, relativePath);
|
|
1408
|
+
if (sha256(target) !== manifest.files[bin]) return false;
|
|
1409
|
+
}
|
|
1410
|
+
for (const [path, digest] of Object.entries(manifest.native ?? {})) {
|
|
1411
|
+
if (sha256(join8(releaseDir, path)) !== digest) return false;
|
|
1412
|
+
}
|
|
1413
|
+
return true;
|
|
1414
|
+
} catch {
|
|
1415
|
+
return false;
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
function directReleaseLayout(releaseDir, manifest) {
|
|
1419
|
+
try {
|
|
1420
|
+
const directories = /* @__PURE__ */ new Set([releaseDir]);
|
|
1421
|
+
for (const relativePath of [
|
|
1422
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
1423
|
+
...Object.keys(manifest.native ?? {})
|
|
1424
|
+
]) {
|
|
1425
|
+
for (let directory = dirname6(relativePath); directory !== "."; directory = dirname6(directory)) {
|
|
1426
|
+
directories.add(join8(releaseDir, directory));
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
if (![...directories].every((directory) => lstatSync2(directory).isDirectory())) return false;
|
|
1430
|
+
const files = [
|
|
1431
|
+
"manifest.json",
|
|
1432
|
+
"package.json",
|
|
1433
|
+
"node",
|
|
1434
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
1435
|
+
...Object.keys(manifest.native ?? {})
|
|
1436
|
+
];
|
|
1437
|
+
const owned = /* @__PURE__ */ new Set([...directories, ...files.map((path) => join8(releaseDir, path))]);
|
|
1438
|
+
if (![...directories].every(
|
|
1439
|
+
(directory) => readdirSync(directory).every((name) => owned.has(join8(directory, name)))
|
|
1440
|
+
))
|
|
1441
|
+
return false;
|
|
1442
|
+
return files.every((relativePath) => lstatSync2(join8(releaseDir, relativePath)).isFile());
|
|
1443
|
+
} catch {
|
|
1444
|
+
return false;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
function readReleaseManifest(releaseDir, releaseName) {
|
|
1448
|
+
try {
|
|
1449
|
+
const manifestPath = join8(releaseDir, "manifest.json");
|
|
1450
|
+
const manifestStat = statSync2(manifestPath);
|
|
1451
|
+
if (!manifestStat.isFile() || manifestStat.size > MAX_MANIFEST_BYTES) return null;
|
|
1452
|
+
const raw = readFileSync6(manifestPath, "utf8");
|
|
1453
|
+
const parsed = JSON.parse(raw);
|
|
1454
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
1455
|
+
if (!exactKeys(parsed, [
|
|
1456
|
+
"schemaVersion",
|
|
1457
|
+
"version",
|
|
1458
|
+
"nodePath",
|
|
1459
|
+
"files",
|
|
1460
|
+
..."native" in parsed ? ["native"] : []
|
|
1461
|
+
]))
|
|
1462
|
+
return null;
|
|
1463
|
+
const candidate = parsed;
|
|
1464
|
+
if (candidate.schemaVersion !== HOOK_RUNTIME_SCHEMA_VERSION || typeof candidate.version !== "string" || compareSemver(candidate.version, candidate.version) !== 0 || typeof candidate.nodePath !== "string" || !isAbsolute4(candidate.nodePath) || /[\r\n]/u.test(candidate.nodePath) || !candidate.files || typeof candidate.files !== "object" || Array.isArray(candidate.files) || !exactKeys(candidate.files, Object.keys(HOOK_RUNTIME_ENTRIES)) || Object.values(candidate.files).some(
|
|
1465
|
+
(digest) => typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest)
|
|
1466
|
+
)) {
|
|
1467
|
+
return null;
|
|
1468
|
+
}
|
|
1469
|
+
if (candidate.native !== void 0 && (!candidate.native || typeof candidate.native !== "object" || Array.isArray(candidate.native) || !exactKeys(candidate.native, KEYRING_NATIVE_FILES) || Object.values(candidate.native).some(
|
|
1470
|
+
(digest) => typeof digest !== "string" || !/^[a-f0-9]{64}$/u.test(digest)
|
|
1471
|
+
)))
|
|
1472
|
+
return null;
|
|
1473
|
+
const manifest = candidate;
|
|
1474
|
+
if (raw !== exactManifest(manifest) || releaseName !== `${RELEASE_PREFIX}${releaseId(manifest)}` || !validateRelease(releaseDir, manifest)) {
|
|
1475
|
+
return null;
|
|
1476
|
+
}
|
|
1477
|
+
return manifest;
|
|
1478
|
+
} catch {
|
|
1479
|
+
return null;
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
function readSelectedRelease(paths) {
|
|
1483
|
+
let raw;
|
|
1484
|
+
try {
|
|
1485
|
+
raw = readFileSync6(paths.current, "utf8");
|
|
1486
|
+
} catch (error) {
|
|
1487
|
+
if (error.code === "ENOENT") return null;
|
|
1488
|
+
throw error;
|
|
1489
|
+
}
|
|
1490
|
+
const name = raw.endsWith("\n") ? raw.slice(0, -1) : "";
|
|
1491
|
+
if (`${name}
|
|
1492
|
+
` !== raw || !RELEASE_NAME_RE.test(name)) {
|
|
1493
|
+
throw new Error("cannot stage hook runtime: selected release is malformed");
|
|
1494
|
+
}
|
|
1495
|
+
const dir = join8(paths.releasesDir, name);
|
|
1496
|
+
const manifest = readReleaseManifest(dir, name);
|
|
1497
|
+
if (!manifest) {
|
|
1498
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
1499
|
+
}
|
|
1500
|
+
return { name, dir, manifest };
|
|
1501
|
+
}
|
|
1502
|
+
function selectedHookRuntimeKeyringLoader(options = {}) {
|
|
1503
|
+
try {
|
|
1504
|
+
const selected = readSelectedRelease(hookRuntimePaths(options));
|
|
1505
|
+
if (!selected?.manifest.native) return void 0;
|
|
1506
|
+
const loader = join8(selected.dir, "native", "keyring.cjs");
|
|
1507
|
+
return existsSync6(loader) ? loader : void 0;
|
|
1508
|
+
} catch {
|
|
1509
|
+
return void 0;
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
function releaseEntries(paths) {
|
|
1513
|
+
return readdirSync(paths.releasesDir, { withFileTypes: true }).flatMap((entry) => {
|
|
1514
|
+
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name)) return [];
|
|
1515
|
+
const dir = join8(paths.releasesDir, entry.name);
|
|
1516
|
+
try {
|
|
1517
|
+
return [{ name: entry.name, dir, mtimeMs: statSync2(dir).mtimeMs }];
|
|
1518
|
+
} catch {
|
|
1519
|
+
return [];
|
|
1520
|
+
}
|
|
1521
|
+
});
|
|
1522
|
+
}
|
|
1523
|
+
function pruneReleases(paths, protectedNames, reserveSlots, now) {
|
|
1524
|
+
const entries = releaseEntries(paths).sort(
|
|
1525
|
+
(left, right) => right.mtimeMs - left.mtimeMs || left.name.localeCompare(right.name)
|
|
1526
|
+
);
|
|
1527
|
+
const protectedWithPredecessors = new Set(protectedNames);
|
|
1528
|
+
for (const entry of entries) {
|
|
1529
|
+
if (protectedWithPredecessors.size >= protectedNames.size + RETAIN_PREDECESSORS) break;
|
|
1530
|
+
if (!protectedWithPredecessors.has(entry.name)) protectedWithPredecessors.add(entry.name);
|
|
1531
|
+
}
|
|
1532
|
+
const candidates = entries.filter(
|
|
1533
|
+
(entry) => !protectedWithPredecessors.has(entry.name) && now - entry.mtimeMs >= RELEASE_GRACE_MS
|
|
1534
|
+
).sort((left, right) => left.mtimeMs - right.mtimeMs || left.name.localeCompare(right.name));
|
|
1535
|
+
let remaining = entries.length;
|
|
1536
|
+
let deleted = 0;
|
|
1537
|
+
for (const entry of candidates) {
|
|
1538
|
+
if (remaining + reserveSlots <= MAX_RELEASES || deleted >= MAX_RELEASE_DELETIONS_PER_STAGE) {
|
|
1539
|
+
break;
|
|
1540
|
+
}
|
|
1541
|
+
rmSync4(entry.dir, { recursive: true, force: true });
|
|
1542
|
+
remaining -= 1;
|
|
1543
|
+
deleted += 1;
|
|
1544
|
+
}
|
|
1545
|
+
if (remaining + reserveSlots > MAX_RELEASES) {
|
|
1546
|
+
throw new Error(
|
|
1547
|
+
"cannot stage hook runtime: release retention is full while recent runtimes are protected"
|
|
1548
|
+
);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
function sourceManifest(sourceDir, version, nodePath) {
|
|
1552
|
+
const files = {};
|
|
1553
|
+
for (const [bin, relativePath] of Object.entries(SOURCE_ENTRIES)) {
|
|
1554
|
+
const source = join8(sourceDir, relativePath);
|
|
1555
|
+
if (!existsSync6(source) || !statSync2(source).isFile()) {
|
|
1556
|
+
throw new Error(`cannot stage hook runtime: missing self-contained ${bin} bundle`);
|
|
1557
|
+
}
|
|
1558
|
+
files[bin] = sha256(source);
|
|
1559
|
+
}
|
|
1560
|
+
return {
|
|
1561
|
+
schemaVersion: HOOK_RUNTIME_SCHEMA_VERSION,
|
|
1562
|
+
version,
|
|
1563
|
+
nodePath,
|
|
1564
|
+
files,
|
|
1565
|
+
native: {
|
|
1566
|
+
"native/keyring.cjs": createHash2("sha256").update(KEYRING_LOADER).digest("hex"),
|
|
1567
|
+
"native/keyring.node": sha256(keyringNativeBinary())
|
|
1568
|
+
}
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
1572
|
+
set -eu
|
|
1573
|
+
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
1574
|
+
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
1575
|
+
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
1576
|
+
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
1577
|
+
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
1578
|
+
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
1579
|
+
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
1580
|
+
case "\${1-}" in
|
|
1581
|
+
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
1582
|
+
*) exit 64 ;;
|
|
1583
|
+
esac
|
|
1584
|
+
shift
|
|
1585
|
+
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
1586
|
+
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
1587
|
+
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
1588
|
+
exec "$prim_node" "$prim_entry" "$@"
|
|
1589
|
+
`;
|
|
1590
|
+
function privateDirectory(path) {
|
|
1591
|
+
const stat = lstatIfPresent(path);
|
|
1592
|
+
if (!stat?.isDirectory()) return false;
|
|
1593
|
+
if (process.platform === "win32") return true;
|
|
1594
|
+
const uid = process.getuid?.();
|
|
1595
|
+
return (Number(stat.mode) & 511) === DIRECTORY_MODE2 && (uid === void 0 || stat.uid === uid);
|
|
1596
|
+
}
|
|
1597
|
+
function privateFile(path, mode) {
|
|
1598
|
+
const stat = lstatIfPresent(path);
|
|
1599
|
+
if (!stat?.isFile()) return false;
|
|
1600
|
+
if (process.platform === "win32") return true;
|
|
1601
|
+
const uid = process.getuid?.();
|
|
1602
|
+
return (Number(stat.mode) & 511) === mode && (uid === void 0 || stat.uid === uid);
|
|
1603
|
+
}
|
|
1604
|
+
function hasSafeRuntimeLayout(paths, selected) {
|
|
1605
|
+
const releaseDirectories = /* @__PURE__ */ new Set([selected.dir]);
|
|
1606
|
+
for (const relativePath of [
|
|
1607
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
1608
|
+
...Object.keys(selected.manifest.native ?? {})
|
|
1609
|
+
]) {
|
|
1610
|
+
for (let directory = dirname6(relativePath); directory !== "."; directory = dirname6(directory)) {
|
|
1611
|
+
releaseDirectories.add(join8(selected.dir, directory));
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
if (!privateDirectory(paths.configDir) || !privateDirectory(paths.runtimeDir) || !privateDirectory(paths.releasesDir) || ![...releaseDirectories].every(privateDirectory) || !privateFile(paths.launcher, LAUNCHER_MODE) || !privateFile(paths.current, DATA_FILE_MODE) || !privateFile(join8(selected.dir, "manifest.json"), DATA_FILE_MODE) || !privateFile(join8(selected.dir, "package.json"), DATA_FILE_MODE) || !privateFile(join8(selected.dir, "node"), DATA_FILE_MODE)) {
|
|
1615
|
+
return false;
|
|
1616
|
+
}
|
|
1617
|
+
return [
|
|
1618
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
1619
|
+
...Object.keys(selected.manifest.native ?? {})
|
|
1620
|
+
].every((relativePath) => privateFile(join8(selected.dir, relativePath), DATA_FILE_MODE));
|
|
1621
|
+
}
|
|
1622
|
+
function sameNodeExecutable(recorded, desired) {
|
|
1623
|
+
try {
|
|
1624
|
+
if (!statSync2(recorded).isFile()) return false;
|
|
1625
|
+
accessSync(recorded, constants2.X_OK);
|
|
1626
|
+
return realpathSync2(recorded) === realpathSync2(desired);
|
|
1627
|
+
} catch {
|
|
1628
|
+
return false;
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
function recordedNodeMatchesCurrentProcess(nodePath) {
|
|
1632
|
+
return sameNodeExecutable(nodePath, process.execPath);
|
|
1633
|
+
}
|
|
1634
|
+
function inspectHookRuntime(options = {}) {
|
|
1635
|
+
let paths;
|
|
1636
|
+
try {
|
|
1637
|
+
paths = hookRuntimePaths(options);
|
|
1638
|
+
} catch {
|
|
1639
|
+
return { state: "invalid" };
|
|
1640
|
+
}
|
|
1641
|
+
const launcher = lstatIfPresent(paths.launcher);
|
|
1642
|
+
const selector = lstatIfPresent(paths.current);
|
|
1643
|
+
if (!(launcher || selector)) return { state: "missing" };
|
|
1644
|
+
if (!(launcher && selector)) return { state: "invalid" };
|
|
1645
|
+
try {
|
|
1646
|
+
if (readFileSync6(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
1647
|
+
return { state: "invalid" };
|
|
1648
|
+
}
|
|
1649
|
+
const selected = readSelectedRelease(paths);
|
|
1650
|
+
if (!selected) return { state: "invalid" };
|
|
1651
|
+
assertOwnedHookRuntime(paths);
|
|
1652
|
+
if (!hasSafeRuntimeLayout(paths, selected)) return { state: "invalid" };
|
|
1653
|
+
return recordedNodeMatchesCurrentProcess(selected.manifest.nodePath) ? { state: "ready", version: selected.manifest.version } : { state: "invalid" };
|
|
1654
|
+
} catch {
|
|
1655
|
+
return { state: "invalid" };
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
function writeRelease(sourceDir, releaseDir, manifest, releasesDir) {
|
|
1659
|
+
const stagingDir = mkdtempSync(join8(releasesDir, ".stage-"));
|
|
1660
|
+
chmodSync(stagingDir, DIRECTORY_MODE2);
|
|
1661
|
+
try {
|
|
1662
|
+
const directories = /* @__PURE__ */ new Set([stagingDir]);
|
|
1663
|
+
const syncStagedFile = (path) => {
|
|
1664
|
+
syncFile(path);
|
|
1665
|
+
for (let directory = dirname6(path); ; directory = dirname6(directory)) {
|
|
1666
|
+
directories.add(directory);
|
|
1667
|
+
if (directory === stagingDir) break;
|
|
1668
|
+
}
|
|
1669
|
+
};
|
|
1670
|
+
for (const [bin, targetRelative] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
1671
|
+
const source = join8(sourceDir, SOURCE_ENTRIES[bin]);
|
|
1672
|
+
const target = join8(stagingDir, targetRelative);
|
|
1673
|
+
mkdirSync4(dirname6(target), { recursive: true, mode: DIRECTORY_MODE2 });
|
|
1674
|
+
chmodSync(dirname6(target), DIRECTORY_MODE2);
|
|
1675
|
+
copyFileSync(source, target);
|
|
1676
|
+
chmodSync(target, DATA_FILE_MODE);
|
|
1677
|
+
if (sha256(target) !== manifest.files[bin]) {
|
|
1678
|
+
throw new Error(`cannot stage hook runtime: ${bin} changed while it was copied`);
|
|
1679
|
+
}
|
|
1680
|
+
syncStagedFile(target);
|
|
1681
|
+
}
|
|
1682
|
+
if (manifest.native) {
|
|
1683
|
+
const nativeDir = join8(stagingDir, "native");
|
|
1684
|
+
mkdirSync4(nativeDir, { mode: DIRECTORY_MODE2 });
|
|
1685
|
+
writeFileSync4(join8(nativeDir, "keyring.cjs"), KEYRING_LOADER, { mode: DATA_FILE_MODE });
|
|
1686
|
+
copyFileSync(keyringNativeBinary(), join8(nativeDir, "keyring.node"));
|
|
1687
|
+
chmodSync(join8(nativeDir, "keyring.node"), DATA_FILE_MODE);
|
|
1688
|
+
for (const [path, digest] of Object.entries(manifest.native)) {
|
|
1689
|
+
const target = join8(stagingDir, path);
|
|
1690
|
+
if (sha256(target) !== digest)
|
|
1691
|
+
throw new Error("Native credential store changed while copying");
|
|
1692
|
+
syncStagedFile(target);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
const nodePath = join8(stagingDir, "node");
|
|
1696
|
+
writeFileSync4(nodePath, `${manifest.nodePath}
|
|
1697
|
+
`, {
|
|
1698
|
+
encoding: "utf8",
|
|
1699
|
+
flag: "wx",
|
|
1700
|
+
mode: DATA_FILE_MODE
|
|
1701
|
+
});
|
|
1702
|
+
syncStagedFile(nodePath);
|
|
1703
|
+
const packageJsonPath = join8(stagingDir, "package.json");
|
|
1704
|
+
writeFileSync4(packageJsonPath, runtimePackageJson(manifest), {
|
|
1705
|
+
encoding: "utf8",
|
|
1706
|
+
flag: "wx",
|
|
1707
|
+
mode: DATA_FILE_MODE
|
|
1708
|
+
});
|
|
1709
|
+
syncStagedFile(packageJsonPath);
|
|
1710
|
+
const manifestPath = join8(stagingDir, "manifest.json");
|
|
1711
|
+
writeFileSync4(manifestPath, exactManifest(manifest), {
|
|
1712
|
+
encoding: "utf8",
|
|
1713
|
+
flag: "wx",
|
|
1714
|
+
mode: DATA_FILE_MODE
|
|
1715
|
+
});
|
|
1716
|
+
syncStagedFile(manifestPath);
|
|
1717
|
+
for (const directory of [...directories].sort((left, right) => right.length - left.length)) {
|
|
1718
|
+
syncDirectory(directory);
|
|
1719
|
+
}
|
|
1720
|
+
try {
|
|
1721
|
+
renameSync2(stagingDir, releaseDir);
|
|
1722
|
+
} catch (error) {
|
|
1723
|
+
const code = error.code;
|
|
1724
|
+
if (code !== "EEXIST" && code !== "ENOTEMPTY" || !validateRelease(releaseDir, manifest)) {
|
|
1725
|
+
throw error;
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
} finally {
|
|
1729
|
+
rmSync4(stagingDir, { recursive: true, force: true });
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
function stableNodePath(execPath = process.execPath, searchPath = process.env.PATH ?? "") {
|
|
1733
|
+
let target;
|
|
1734
|
+
try {
|
|
1735
|
+
target = realpathSync2(execPath);
|
|
1736
|
+
} catch {
|
|
1737
|
+
return execPath;
|
|
1738
|
+
}
|
|
1739
|
+
for (const directory of searchPath.split(delimiter)) {
|
|
1740
|
+
if (!isAbsolute4(directory)) continue;
|
|
1741
|
+
const candidate = join8(directory, "node");
|
|
1742
|
+
try {
|
|
1743
|
+
if (realpathSync2(candidate) === target) return candidate;
|
|
1744
|
+
} catch {
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return execPath;
|
|
1748
|
+
}
|
|
1749
|
+
function stageHookRuntime(options = {}) {
|
|
1750
|
+
const root = packageRoot();
|
|
1751
|
+
const sourceDir = options.sourceDir ?? (root ? join8(root, "dist", "hook-runtime") : null);
|
|
1752
|
+
const version = options.version ?? packageVersion();
|
|
1753
|
+
const nodePath = resolve2(options.nodePath ?? stableNodePath());
|
|
1754
|
+
if (!sourceDir || !version || compareSemver(version, version) !== 0 || !isAbsolute4(nodePath) || /[\r\n]/u.test(nodePath)) {
|
|
1755
|
+
throw new Error("cannot stage hook runtime: package identity is unavailable");
|
|
1756
|
+
}
|
|
1757
|
+
const manifest = sourceManifest(sourceDir, version, nodePath);
|
|
1758
|
+
const paths = hookRuntimePaths(options);
|
|
1759
|
+
const releaseName = `${RELEASE_PREFIX}${releaseId(manifest)}`;
|
|
1760
|
+
const releaseDir = join8(paths.releasesDir, releaseName);
|
|
1761
|
+
mkdirSync4(paths.releasesDir, { recursive: true, mode: DIRECTORY_MODE2 });
|
|
1762
|
+
chmodSync(paths.configDir, DIRECTORY_MODE2);
|
|
1763
|
+
chmodSync(paths.runtimeDir, DIRECTORY_MODE2);
|
|
1764
|
+
chmodSync(paths.releasesDir, DIRECTORY_MODE2);
|
|
1765
|
+
return withFileLockSync(paths.selectionLock, () => {
|
|
1766
|
+
const now = Date.now();
|
|
1767
|
+
const selected = readSelectedRelease(paths);
|
|
1768
|
+
if (selected) utimesSync(selected.dir, now / 1e3, now / 1e3);
|
|
1769
|
+
const precedence = selected ? compareSemver(selected.manifest.version, manifest.version) : -1;
|
|
1770
|
+
if (precedence === void 0) {
|
|
1771
|
+
throw new Error("cannot stage hook runtime: selected release version is malformed");
|
|
1772
|
+
}
|
|
1773
|
+
const selectRequested = !selected || precedence < 0 || precedence === 0 && !sameNodeExecutable(selected.manifest.nodePath, nodePath);
|
|
1774
|
+
const requestedExists = existsSync6(releaseDir);
|
|
1775
|
+
const protectedBefore = new Set(selected ? [selected.name] : []);
|
|
1776
|
+
if (selectRequested && requestedExists) protectedBefore.add(releaseName);
|
|
1777
|
+
pruneReleases(paths, protectedBefore, selectRequested && !requestedExists ? 1 : 0, now);
|
|
1778
|
+
if (selectRequested && !validateRelease(releaseDir, manifest)) {
|
|
1779
|
+
if (requestedExists) {
|
|
1780
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
1781
|
+
}
|
|
1782
|
+
writeRelease(sourceDir, releaseDir, manifest, paths.releasesDir);
|
|
1783
|
+
}
|
|
1784
|
+
const finalRelease = selectRequested ? { name: releaseName, dir: releaseDir, manifest } : selected;
|
|
1785
|
+
const launcherCurrent = (() => {
|
|
1786
|
+
try {
|
|
1787
|
+
return readFileSync6(paths.launcher, "utf8") === STABLE_HOOK_LAUNCHER_CONTENT;
|
|
1788
|
+
} catch {
|
|
1789
|
+
return false;
|
|
1790
|
+
}
|
|
1791
|
+
})();
|
|
1792
|
+
const selectorCurrent = selected?.name === finalRelease.name;
|
|
1793
|
+
if (!launcherCurrent) {
|
|
1794
|
+
atomicWriteFile(paths.launcher, STABLE_HOOK_LAUNCHER_CONTENT, {
|
|
1795
|
+
ensureParent: true,
|
|
1796
|
+
mode: LAUNCHER_MODE
|
|
1797
|
+
});
|
|
1798
|
+
}
|
|
1799
|
+
const entrypointCurrent = gitHookEntrypointCurrent(paths);
|
|
1800
|
+
if (!entrypointCurrent) {
|
|
1801
|
+
atomicWriteFile(paths.gitHookEntrypoint, GIT_HOOK_ENTRYPOINT_CONTENT, {
|
|
1802
|
+
ensureParent: true,
|
|
1803
|
+
mode: LAUNCHER_MODE
|
|
1804
|
+
});
|
|
1805
|
+
}
|
|
1806
|
+
if (!selectorCurrent) {
|
|
1807
|
+
syncDirectory(paths.releasesDir);
|
|
1808
|
+
atomicWriteFile(paths.current, `${finalRelease.name}
|
|
1809
|
+
`, {
|
|
1810
|
+
ensureParent: true,
|
|
1811
|
+
mode: DATA_FILE_MODE
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
utimesSync(finalRelease.dir, now / 1e3, now / 1e3);
|
|
1815
|
+
pruneReleases(
|
|
1816
|
+
paths,
|
|
1817
|
+
/* @__PURE__ */ new Set([finalRelease.name, ...selected ? [selected.name] : []]),
|
|
1818
|
+
0,
|
|
1819
|
+
now
|
|
1820
|
+
);
|
|
1821
|
+
return {
|
|
1822
|
+
changed: !launcherCurrent || !entrypointCurrent || !selectorCurrent,
|
|
1823
|
+
releaseDir: finalRelease.dir,
|
|
1824
|
+
manifest: finalRelease.manifest,
|
|
1825
|
+
paths
|
|
1826
|
+
};
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1829
|
+
function gitHookEntrypointCurrent(paths) {
|
|
1830
|
+
try {
|
|
1831
|
+
return readFileSync6(paths.gitHookEntrypoint, "utf8") === GIT_HOOK_ENTRYPOINT_CONTENT;
|
|
1832
|
+
} catch {
|
|
1833
|
+
return false;
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
function inspectGitHookEntrypoint(options = {}) {
|
|
1837
|
+
let paths;
|
|
1838
|
+
try {
|
|
1839
|
+
paths = hookRuntimePaths(options);
|
|
1840
|
+
} catch {
|
|
1841
|
+
return "invalid";
|
|
1842
|
+
}
|
|
1843
|
+
try {
|
|
1844
|
+
const entrypoint = lstatIfPresent(paths.gitHookEntrypoint);
|
|
1845
|
+
if (!entrypoint) return "missing";
|
|
1846
|
+
if (!entrypoint.isFile() || (Number(entrypoint.mode) & 64) === 0 || !gitHookEntrypointCurrent(paths) || readFileSync6(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT || !lstatIfPresent(paths.current)?.isFile()) {
|
|
1847
|
+
return "invalid";
|
|
1848
|
+
}
|
|
1849
|
+
return "ready";
|
|
1850
|
+
} catch {
|
|
1851
|
+
return "invalid";
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
function lstatIfPresent(path) {
|
|
1855
|
+
try {
|
|
1856
|
+
return lstatSync2(path);
|
|
1857
|
+
} catch (error) {
|
|
1858
|
+
const code = error.code;
|
|
1859
|
+
if (code === "ENOENT" || code === "ENOTDIR") return void 0;
|
|
1860
|
+
throw error;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
function assertOwnedHookRuntime(paths) {
|
|
1864
|
+
const entrypoint = lstatIfPresent(paths.gitHookEntrypoint);
|
|
1865
|
+
if (entrypoint && (!entrypoint.isFile() || !gitHookEntrypointCurrent(paths))) {
|
|
1866
|
+
throw new Error(
|
|
1867
|
+
`refusing to remove unrecognized Git hook entrypoint at ${paths.gitHookEntrypoint}`
|
|
1868
|
+
);
|
|
1869
|
+
}
|
|
1870
|
+
const launcher = lstatIfPresent(paths.launcher);
|
|
1871
|
+
if (launcher) {
|
|
1872
|
+
if (!launcher.isFile() || readFileSync6(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
1873
|
+
throw new Error(`refusing to remove unrecognized hook launcher at ${paths.launcher}`);
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
const runtime = lstatIfPresent(paths.runtimeDir);
|
|
1877
|
+
if (!runtime) return;
|
|
1878
|
+
if (!runtime.isDirectory()) {
|
|
1879
|
+
throw new Error(`refusing to remove non-directory hook runtime at ${paths.runtimeDir}`);
|
|
1880
|
+
}
|
|
1881
|
+
const allowed = /* @__PURE__ */ new Set(["current", "releases", "selection.lock"]);
|
|
1882
|
+
const unexpected = readdirSync(paths.runtimeDir).filter((entry) => !allowed.has(entry));
|
|
1883
|
+
if (unexpected.length > 0) {
|
|
1884
|
+
throw new Error(
|
|
1885
|
+
`refusing to remove hook runtime with unrecognized entries: ${unexpected.sort().join(", ")}`
|
|
1886
|
+
);
|
|
1887
|
+
}
|
|
1888
|
+
const current = lstatIfPresent(paths.current);
|
|
1889
|
+
const releases = lstatIfPresent(paths.releasesDir);
|
|
1890
|
+
if (current) {
|
|
1891
|
+
if (!current.isFile() || !releases || !readSelectedRelease(paths)) {
|
|
1892
|
+
throw new Error(`refusing to remove malformed hook runtime selection at ${paths.current}`);
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
if (!releases) return;
|
|
1896
|
+
if (!releases.isDirectory()) {
|
|
1897
|
+
throw new Error(`refusing to remove non-directory hook releases at ${paths.releasesDir}`);
|
|
1898
|
+
}
|
|
1899
|
+
for (const entry of readdirSync(paths.releasesDir, { withFileTypes: true })) {
|
|
1900
|
+
const releaseDir = join8(paths.releasesDir, entry.name);
|
|
1901
|
+
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name) || !readReleaseManifest(releaseDir, entry.name)) {
|
|
1902
|
+
throw new Error(`refusing to remove unrecognized hook release ${releaseDir}`);
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
function removeHookRuntime(options = {}) {
|
|
1907
|
+
const paths = hookRuntimePaths(options);
|
|
1908
|
+
const hadLauncher = lstatIfPresent(paths.launcher) !== void 0;
|
|
1909
|
+
const hadEntrypoint = lstatIfPresent(paths.gitHookEntrypoint) !== void 0;
|
|
1910
|
+
const hadRuntime = lstatIfPresent(paths.runtimeDir) !== void 0;
|
|
1911
|
+
if (!hadLauncher && !hadEntrypoint && !hadRuntime) return { changed: false, paths };
|
|
1912
|
+
assertOwnedHookRuntime(paths);
|
|
1913
|
+
if (!hadRuntime) {
|
|
1914
|
+
rmSync4(paths.gitHookEntrypoint, { force: true });
|
|
1915
|
+
rmSync4(paths.launcher, { force: true });
|
|
1916
|
+
return { changed: true, paths };
|
|
1917
|
+
}
|
|
1918
|
+
withFileLockSync(paths.selectionLock, () => {
|
|
1919
|
+
assertOwnedHookRuntime(paths);
|
|
1920
|
+
if (lstatIfPresent(paths.gitHookEntrypoint)) rmSync4(paths.gitHookEntrypoint, { force: true });
|
|
1921
|
+
if (lstatIfPresent(paths.launcher)) rmSync4(paths.launcher, { force: true });
|
|
1922
|
+
const quarantined = `${paths.runtimeDir}.uninstall-${String(process.pid)}-${randomBytes2(8).toString("hex")}`;
|
|
1923
|
+
renameSync2(paths.runtimeDir, quarantined);
|
|
1924
|
+
rmSync4(quarantined, { recursive: true, force: true });
|
|
1925
|
+
});
|
|
1926
|
+
return { changed: true, paths };
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1125
1929
|
// src/lib/workos-connect.ts
|
|
1126
1930
|
var DISCOVERY_PATH = "/api/cli/auth/connect/config";
|
|
1127
1931
|
var DEVICE_AUTHORIZATION_PATH = "/oauth2/device_authorization";
|
|
@@ -1362,7 +2166,7 @@ function providerError(value) {
|
|
|
1362
2166
|
return exactString(error, "error", MAX_USER_CODE_LENGTH);
|
|
1363
2167
|
}
|
|
1364
2168
|
async function defaultSleep2(milliseconds, signal) {
|
|
1365
|
-
await new Promise((
|
|
2169
|
+
await new Promise((resolve3, reject) => {
|
|
1366
2170
|
if (signal.aborted) {
|
|
1367
2171
|
reject(signal.reason);
|
|
1368
2172
|
return;
|
|
@@ -1373,7 +2177,7 @@ async function defaultSleep2(milliseconds, signal) {
|
|
|
1373
2177
|
};
|
|
1374
2178
|
const timer = setTimeout(() => {
|
|
1375
2179
|
signal.removeEventListener("abort", abort);
|
|
1376
|
-
|
|
2180
|
+
resolve3();
|
|
1377
2181
|
}, milliseconds);
|
|
1378
2182
|
signal.addEventListener("abort", abort, { once: true });
|
|
1379
2183
|
});
|
|
@@ -1715,35 +2519,35 @@ var stripControlChars = terminalSafeText;
|
|
|
1715
2519
|
|
|
1716
2520
|
// src/lib/session-identity.ts
|
|
1717
2521
|
import { execFile } from "child_process";
|
|
1718
|
-
import { createHash as
|
|
1719
|
-
import { rmSync as
|
|
1720
|
-
import { join as
|
|
2522
|
+
import { createHash as createHash6 } from "crypto";
|
|
2523
|
+
import { rmSync as rmSync8 } from "fs";
|
|
2524
|
+
import { join as join12 } from "path";
|
|
1721
2525
|
import { promisify } from "util";
|
|
1722
2526
|
|
|
1723
2527
|
// src/lib/machine-credentials.ts
|
|
1724
2528
|
import { execFileSync as execFileSync4 } from "child_process";
|
|
1725
|
-
import { createHash as
|
|
2529
|
+
import { createHash as createHash5, randomBytes as randomBytes4, randomUUID as randomUUID2 } from "crypto";
|
|
1726
2530
|
import {
|
|
1727
|
-
chmodSync,
|
|
1728
|
-
existsSync as
|
|
2531
|
+
chmodSync as chmodSync2,
|
|
2532
|
+
existsSync as existsSync7,
|
|
1729
2533
|
linkSync,
|
|
1730
|
-
lstatSync as
|
|
1731
|
-
mkdirSync as
|
|
1732
|
-
readFileSync as
|
|
1733
|
-
rmSync as
|
|
1734
|
-
writeFileSync as
|
|
2534
|
+
lstatSync as lstatSync5,
|
|
2535
|
+
mkdirSync as mkdirSync5,
|
|
2536
|
+
readFileSync as readFileSync9,
|
|
2537
|
+
rmSync as rmSync7,
|
|
2538
|
+
writeFileSync as writeFileSync5
|
|
1735
2539
|
} from "fs";
|
|
1736
|
-
import { createRequire } from "module";
|
|
1737
|
-
import { dirname as
|
|
1738
|
-
import { fileURLToPath as
|
|
2540
|
+
import { createRequire as createRequire2 } from "module";
|
|
2541
|
+
import { dirname as dirname7, join as join11 } from "path";
|
|
2542
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
1739
2543
|
|
|
1740
2544
|
// src/lib/machine-broker.ts
|
|
1741
2545
|
import { spawn } from "child_process";
|
|
1742
|
-
import { createHash as
|
|
1743
|
-
import { lstatSync as
|
|
2546
|
+
import { createHash as createHash3, createHmac, randomBytes as randomBytes3, timingSafeEqual } from "crypto";
|
|
2547
|
+
import { lstatSync as lstatSync3, readFileSync as readFileSync7, rmSync as rmSync5 } from "fs";
|
|
1744
2548
|
import { createServer } from "http";
|
|
1745
|
-
import { basename as basename2, join as
|
|
1746
|
-
import { fileURLToPath as
|
|
2549
|
+
import { basename as basename2, join as join9 } from "path";
|
|
2550
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
1747
2551
|
|
|
1748
2552
|
// src/lib/loopback-fetch.ts
|
|
1749
2553
|
import { request } from "http";
|
|
@@ -1752,7 +2556,7 @@ function loopbackFetch(url, init = {}) {
|
|
|
1752
2556
|
const target = new URL(url);
|
|
1753
2557
|
if (target.protocol !== "http:" || !["127.0.0.1", "[::1]", "localhost"].includes(target.hostname))
|
|
1754
2558
|
return Promise.reject(new Error(`not a loopback URL: ${url}`));
|
|
1755
|
-
return new Promise((
|
|
2559
|
+
return new Promise((resolve3, reject) => {
|
|
1756
2560
|
const outgoing = request(
|
|
1757
2561
|
target,
|
|
1758
2562
|
{ method: init.method ?? "GET", headers: init.headers, agent: false, signal: init.signal },
|
|
@@ -1763,7 +2567,7 @@ function loopbackFetch(url, init = {}) {
|
|
|
1763
2567
|
if (typeof value === "string") headers.set(name, value);
|
|
1764
2568
|
const status = incoming.statusCode ?? 502;
|
|
1765
2569
|
const body = [101, 204, 205, 304].includes(status) ? null : Readable.toWeb(incoming);
|
|
1766
|
-
|
|
2570
|
+
resolve3(new Response(body, { status, headers }));
|
|
1767
2571
|
} catch (error) {
|
|
1768
2572
|
incoming.destroy();
|
|
1769
2573
|
reject(error);
|
|
@@ -1784,14 +2588,14 @@ var FORWARD_REQUEST_PROOF = "forward-request";
|
|
|
1784
2588
|
var FORWARD_ANSWER_PROOF = "forward-answer";
|
|
1785
2589
|
var IDLE_MS = 5 * 6e4;
|
|
1786
2590
|
function descriptorPath(directory, generation) {
|
|
1787
|
-
return
|
|
2591
|
+
return join9(directory, `machine_broker_${generation}.json`);
|
|
1788
2592
|
}
|
|
1789
2593
|
function readDescriptor(path) {
|
|
1790
2594
|
try {
|
|
1791
|
-
const stat =
|
|
2595
|
+
const stat = lstatSync3(path);
|
|
1792
2596
|
if (!stat.isFile() || stat.size > 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.()))
|
|
1793
2597
|
return void 0;
|
|
1794
|
-
const data = JSON.parse(
|
|
2598
|
+
const data = JSON.parse(readFileSync7(path, "utf8"));
|
|
1795
2599
|
return Number.isInteger(data.port) && data.port > 0 && data.port <= 65535 && /^[a-f0-9]{64}$/u.test(data.nonce) && Number.isInteger(data.pid) ? data : void 0;
|
|
1796
2600
|
} catch {
|
|
1797
2601
|
return void 0;
|
|
@@ -1804,7 +2608,7 @@ function matches(left, right) {
|
|
|
1804
2608
|
return typeof right === "string" && /^[a-f0-9]{64}$/u.test(right) && timingSafeEqual(Buffer.from(left, "hex"), Buffer.from(right, "hex"));
|
|
1805
2609
|
}
|
|
1806
2610
|
function tokenHash(token) {
|
|
1807
|
-
return token ?
|
|
2611
|
+
return token ? createHash3("sha256").update(token).digest("hex") : void 0;
|
|
1808
2612
|
}
|
|
1809
2613
|
async function brokerRequest(descriptor, environment, options) {
|
|
1810
2614
|
const body = JSON.stringify({
|
|
@@ -1881,7 +2685,7 @@ async function acquireBrokerCredential(options) {
|
|
|
1881
2685
|
async function ensureBroker(options) {
|
|
1882
2686
|
const path = descriptorPath(options.directory, options.environment.generation);
|
|
1883
2687
|
return withFileLock(
|
|
1884
|
-
|
|
2688
|
+
join9(options.directory, "machine-broker.lock"),
|
|
1885
2689
|
async () => {
|
|
1886
2690
|
const current = readDescriptor(path);
|
|
1887
2691
|
if (current) {
|
|
@@ -1894,14 +2698,14 @@ async function ensureBroker(options) {
|
|
|
1894
2698
|
options.signal.throwIfAborted();
|
|
1895
2699
|
}
|
|
1896
2700
|
}
|
|
1897
|
-
const bundled =
|
|
2701
|
+
const bundled = fileURLToPath3(import.meta.url);
|
|
1898
2702
|
const entry = basename2(bundled) === "prim-daemon-server" ? bundled : binFile("prim-daemon-server");
|
|
1899
2703
|
if (!entry)
|
|
1900
2704
|
throw new MachineAuthError(
|
|
1901
2705
|
"broker_unavailable",
|
|
1902
2706
|
"Reinstall Prim to restore its machine credential broker."
|
|
1903
2707
|
);
|
|
1904
|
-
await new Promise((
|
|
2708
|
+
await new Promise((resolve3, reject) => {
|
|
1905
2709
|
const child = spawn(process.execPath, [entry, "--machine-token-broker"], {
|
|
1906
2710
|
detached: true,
|
|
1907
2711
|
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
@@ -1916,7 +2720,7 @@ async function ensureBroker(options) {
|
|
|
1916
2720
|
if (error) child.kill();
|
|
1917
2721
|
if (child.connected) child.disconnect();
|
|
1918
2722
|
child.unref();
|
|
1919
|
-
error ? reject(error) :
|
|
2723
|
+
error ? reject(error) : resolve3();
|
|
1920
2724
|
};
|
|
1921
2725
|
const abort = () => finish(
|
|
1922
2726
|
new MachineAuthError(
|
|
@@ -2032,26 +2836,26 @@ async function forwardBrokerRequest(options) {
|
|
|
2032
2836
|
}
|
|
2033
2837
|
|
|
2034
2838
|
// src/lib/machine-retry.ts
|
|
2035
|
-
import { createHash as
|
|
2036
|
-
import { lstatSync as
|
|
2037
|
-
import { join as
|
|
2839
|
+
import { createHash as createHash4 } from "crypto";
|
|
2840
|
+
import { lstatSync as lstatSync4, readFileSync as readFileSync8, readdirSync as readdirSync2, rmSync as rmSync6 } from "fs";
|
|
2841
|
+
import { join as join10 } from "path";
|
|
2038
2842
|
function clearMachineCooldowns(directory) {
|
|
2039
2843
|
let names;
|
|
2040
2844
|
try {
|
|
2041
|
-
names =
|
|
2845
|
+
names = readdirSync2(directory);
|
|
2042
2846
|
} catch {
|
|
2043
2847
|
return;
|
|
2044
2848
|
}
|
|
2045
2849
|
for (const name of names) {
|
|
2046
2850
|
if (!/^machine_retry_[0-9a-f]{64}\.json$/u.test(name)) continue;
|
|
2047
|
-
const path =
|
|
2851
|
+
const path = join10(directory, name);
|
|
2048
2852
|
try {
|
|
2049
|
-
const state = JSON.parse(
|
|
2853
|
+
const state = JSON.parse(readFileSync8(path, "utf8"));
|
|
2050
2854
|
if (state.serverRetryAfter === true) continue;
|
|
2051
2855
|
} catch {
|
|
2052
2856
|
}
|
|
2053
2857
|
try {
|
|
2054
|
-
|
|
2858
|
+
rmSync6(path, { force: true });
|
|
2055
2859
|
} catch {
|
|
2056
2860
|
}
|
|
2057
2861
|
}
|
|
@@ -2060,16 +2864,16 @@ var SERVER_WAIT_PREFIX = "The sign-in service asked prim to wait";
|
|
|
2060
2864
|
var MAX_SERVER_WAIT_MS = 15 * 6e4;
|
|
2061
2865
|
async function withMachineCooldown(directory, apiUrl, clientId, signal, request2) {
|
|
2062
2866
|
signal?.throwIfAborted();
|
|
2063
|
-
const key =
|
|
2064
|
-
const path =
|
|
2867
|
+
const key = createHash4("sha256").update(`${apiUrl}\0${clientId}`).digest("hex");
|
|
2868
|
+
const path = join10(directory, `machine_retry_${key}.json`);
|
|
2065
2869
|
let state = {
|
|
2066
2870
|
failures: 0,
|
|
2067
2871
|
retryAt: 0
|
|
2068
2872
|
};
|
|
2069
2873
|
try {
|
|
2070
|
-
const stat =
|
|
2874
|
+
const stat = lstatSync4(path);
|
|
2071
2875
|
if (!stat.isFile() || stat.size > 1024) throw new Error("Invalid retry state");
|
|
2072
|
-
const data = JSON.parse(
|
|
2876
|
+
const data = JSON.parse(readFileSync8(path, "utf8"));
|
|
2073
2877
|
if (!Number.isSafeInteger(data.failures) || data.failures < 0 || data.failures > 16 || !Number.isSafeInteger(data.retryAt) || data.retryAt < 0)
|
|
2074
2878
|
throw new Error("Invalid retry state");
|
|
2075
2879
|
state = data;
|
|
@@ -2093,7 +2897,7 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request2
|
|
|
2093
2897
|
);
|
|
2094
2898
|
try {
|
|
2095
2899
|
const result = await request2();
|
|
2096
|
-
|
|
2900
|
+
rmSync6(path, { force: true });
|
|
2097
2901
|
return result;
|
|
2098
2902
|
} catch (error) {
|
|
2099
2903
|
if (!signal?.aborted && error instanceof MachineAuthError && [
|
|
@@ -2124,7 +2928,7 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request2
|
|
|
2124
2928
|
function sessionScoped(deps) {
|
|
2125
2929
|
return !deps.localEnvironment && !deps.environmentOnly && sessionIdentityMode(deps);
|
|
2126
2930
|
}
|
|
2127
|
-
var
|
|
2931
|
+
var require3 = createRequire2(import.meta.url);
|
|
2128
2932
|
var SERVICE = "ai.primitive.prim.machine";
|
|
2129
2933
|
var DEFAULT_API_URL = "https://api.getprimitive.ai";
|
|
2130
2934
|
var PROFILE_FILE = "machine_profile.json";
|
|
@@ -2137,16 +2941,16 @@ function apiUrlFor(deps) {
|
|
|
2137
2941
|
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL);
|
|
2138
2942
|
}
|
|
2139
2943
|
function hash(value) {
|
|
2140
|
-
return
|
|
2944
|
+
return createHash5("sha256").update(value).digest("hex");
|
|
2141
2945
|
}
|
|
2142
2946
|
function recordPath(directory, generation) {
|
|
2143
|
-
return
|
|
2947
|
+
return join11(directory, `machine_${generation}.json`);
|
|
2144
2948
|
}
|
|
2145
2949
|
function storageKey(directory, generation) {
|
|
2146
2950
|
return `${hash(directory)}:${generation}`;
|
|
2147
2951
|
}
|
|
2148
2952
|
function environmentContextPath(deps, clientId) {
|
|
2149
|
-
return
|
|
2953
|
+
return join11(
|
|
2150
2954
|
configurationDirectory(deps),
|
|
2151
2955
|
`machine_context_${hash(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
2152
2956
|
);
|
|
@@ -2206,22 +3010,28 @@ function readEnvironmentContext(environment, deps) {
|
|
|
2206
3010
|
return void 0;
|
|
2207
3011
|
}
|
|
2208
3012
|
}
|
|
2209
|
-
function
|
|
2210
|
-
let directory =
|
|
3013
|
+
function keyringLoaderPath(start, options = {}) {
|
|
3014
|
+
let directory = start;
|
|
2211
3015
|
for (let depth = 0; depth < 5; depth++) {
|
|
2212
|
-
const loader =
|
|
2213
|
-
if (
|
|
2214
|
-
|
|
2215
|
-
return new module2.AsyncEntry(SERVICE, key, { linux: { store: "secret-service" } });
|
|
2216
|
-
}
|
|
2217
|
-
directory = dirname5(directory);
|
|
3016
|
+
const loader = join11(directory, "native", "keyring.cjs");
|
|
3017
|
+
if (existsSync7(loader)) return loader;
|
|
3018
|
+
directory = dirname7(directory);
|
|
2218
3019
|
}
|
|
2219
|
-
|
|
3020
|
+
try {
|
|
3021
|
+
createRequire2(join11(start, "noop.js")).resolve("@napi-rs/keyring");
|
|
3022
|
+
return void 0;
|
|
3023
|
+
} catch {
|
|
3024
|
+
return selectedHookRuntimeKeyringLoader(options);
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
function keyringEntry(key) {
|
|
3028
|
+
const loader = keyringLoaderPath(dirname7(fileURLToPath4(import.meta.url)));
|
|
3029
|
+
const module = require3(loader ?? "@napi-rs/keyring");
|
|
2220
3030
|
return new module.AsyncEntry(SERVICE, key, { linux: { store: "secret-service" } });
|
|
2221
3031
|
}
|
|
2222
3032
|
function protectPath(path, directory) {
|
|
2223
3033
|
if (process.platform !== "win32") {
|
|
2224
|
-
|
|
3034
|
+
chmodSync2(path, directory ? 448 : 384);
|
|
2225
3035
|
return;
|
|
2226
3036
|
}
|
|
2227
3037
|
const encodedPath = Buffer.from(path).toString("base64");
|
|
@@ -2238,8 +3048,8 @@ function protectPath(path, directory) {
|
|
|
2238
3048
|
);
|
|
2239
3049
|
}
|
|
2240
3050
|
function ensureDirectory(directory) {
|
|
2241
|
-
|
|
2242
|
-
if (
|
|
3051
|
+
mkdirSync5(directory, { recursive: true, mode: 448 });
|
|
3052
|
+
if (lstatSync5(directory).isSymbolicLink())
|
|
2243
3053
|
throw new MachineAuthError(
|
|
2244
3054
|
"unsafe_storage",
|
|
2245
3055
|
"Credential directory must not be a symbolic link."
|
|
@@ -2257,14 +3067,14 @@ function readMachineMetadata(path) {
|
|
|
2257
3067
|
function readPrivate(path) {
|
|
2258
3068
|
let raw;
|
|
2259
3069
|
try {
|
|
2260
|
-
const stat =
|
|
3070
|
+
const stat = lstatSync5(path);
|
|
2261
3071
|
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
2262
3072
|
throw new MachineAuthError(
|
|
2263
3073
|
"unsafe_storage",
|
|
2264
3074
|
"Machine credential files must be owner-only regular files."
|
|
2265
3075
|
);
|
|
2266
3076
|
}
|
|
2267
|
-
raw =
|
|
3077
|
+
raw = readFileSync9(path, "utf8");
|
|
2268
3078
|
} catch (error) {
|
|
2269
3079
|
if (error.code === "ENOENT") return void 0;
|
|
2270
3080
|
throw error;
|
|
@@ -2309,7 +3119,7 @@ function parseProfile(value) {
|
|
|
2309
3119
|
};
|
|
2310
3120
|
}
|
|
2311
3121
|
function readMachineProfile(deps = {}) {
|
|
2312
|
-
const value = readPrivate(
|
|
3122
|
+
const value = readPrivate(join11(configurationDirectory(deps), PROFILE_FILE));
|
|
2313
3123
|
return value === void 0 ? void 0 : parseProfile(value);
|
|
2314
3124
|
}
|
|
2315
3125
|
var NETWORK_SECRET_AUTH = "network-secret";
|
|
@@ -2318,20 +3128,20 @@ var INJECTED_IDENTITY_TTL_MS = 60 * 6e4;
|
|
|
2318
3128
|
var brokerKeys = /* @__PURE__ */ new Map();
|
|
2319
3129
|
function networkSecretBrokerKey(deps, clientId) {
|
|
2320
3130
|
const directory = configurationDirectory(deps);
|
|
2321
|
-
const path =
|
|
3131
|
+
const path = join11(directory, `machine_network_${hash(`${apiUrlFor(deps)}\0${clientId}`)}.key`);
|
|
2322
3132
|
const known = brokerKeys.get(path);
|
|
2323
3133
|
if (known) return known;
|
|
2324
3134
|
ensureDirectory(directory);
|
|
2325
3135
|
const read = () => {
|
|
2326
3136
|
try {
|
|
2327
|
-
const stat =
|
|
3137
|
+
const stat = lstatSync5(path);
|
|
2328
3138
|
if (!stat.isFile() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()))
|
|
2329
3139
|
throw new MachineAuthError(
|
|
2330
3140
|
"unsafe_storage",
|
|
2331
3141
|
`The machine broker key at ${path} is not an owner-only file. Delete it and retry.`,
|
|
2332
3142
|
true
|
|
2333
3143
|
);
|
|
2334
|
-
const value =
|
|
3144
|
+
const value = readFileSync9(path, "utf8").trim();
|
|
2335
3145
|
return { key: /^[a-f0-9]{64}$/u.test(value) ? value : void 0 };
|
|
2336
3146
|
} catch (error) {
|
|
2337
3147
|
if (error.code === "ENOENT") return void 0;
|
|
@@ -2350,16 +3160,16 @@ function networkSecretBrokerKey(deps, clientId) {
|
|
|
2350
3160
|
`The machine broker key at ${path} is damaged. Delete it and retry.`,
|
|
2351
3161
|
true
|
|
2352
3162
|
);
|
|
2353
|
-
const staged = `${path}.${process.pid}.${
|
|
3163
|
+
const staged = `${path}.${process.pid}.${randomBytes4(6).toString("hex")}`;
|
|
2354
3164
|
try {
|
|
2355
|
-
|
|
3165
|
+
writeFileSync5(staged, `${randomBytes4(32).toString("hex")}
|
|
2356
3166
|
`, { mode: 384, flag: "wx" });
|
|
2357
3167
|
protectPath(staged, false);
|
|
2358
3168
|
linkSync(staged, path);
|
|
2359
3169
|
} catch (error) {
|
|
2360
3170
|
if (error.code !== "EEXIST") throw error;
|
|
2361
3171
|
} finally {
|
|
2362
|
-
|
|
3172
|
+
rmSync7(staged, { force: true });
|
|
2363
3173
|
}
|
|
2364
3174
|
}
|
|
2365
3175
|
const key = read()?.key;
|
|
@@ -2418,7 +3228,7 @@ function environmentCredentials(deps) {
|
|
|
2418
3228
|
}
|
|
2419
3229
|
function hasMachineConfiguration(deps = {}) {
|
|
2420
3230
|
const env = deps.env ?? process.env;
|
|
2421
|
-
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 ||
|
|
3231
|
+
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 || existsSync7(join11(configurationDirectory(deps), PROFILE_FILE));
|
|
2422
3232
|
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
2423
3233
|
throw new MachineAuthError(
|
|
2424
3234
|
"conflicting_credentials",
|
|
@@ -2454,7 +3264,7 @@ function machineSessionEnded(deps = {}) {
|
|
|
2454
3264
|
const generation = environmentCredentials(deps)?.generation ?? readMachineProfile(deps)?.generation;
|
|
2455
3265
|
if (!generation) return false;
|
|
2456
3266
|
if (environmentCredentials(deps)) return environmentTerminal === generation;
|
|
2457
|
-
return readPrivate(
|
|
3267
|
+
return readPrivate(join11(configurationDirectory(deps), "machine_terminal.json")) === generation;
|
|
2458
3268
|
}
|
|
2459
3269
|
var environmentTerminal;
|
|
2460
3270
|
var NETWORK_REJECTION_PAUSE_MS = 3e4;
|
|
@@ -2527,7 +3337,7 @@ async function configureMachine(clientId, clientSecret, deps = {}, signal) {
|
|
|
2527
3337
|
const directory = configurationDirectory(deps);
|
|
2528
3338
|
ensureDirectory(directory);
|
|
2529
3339
|
return withFileLock(
|
|
2530
|
-
|
|
3340
|
+
join11(directory, "credentials.lock"),
|
|
2531
3341
|
async () => {
|
|
2532
3342
|
const { configuration, tokens, identity } = await withMachineCooldown(
|
|
2533
3343
|
directory,
|
|
@@ -2583,11 +3393,11 @@ async function configureMachine(clientId, clientSecret, deps = {}, signal) {
|
|
|
2583
3393
|
profile = { ...profile, storage: "file" };
|
|
2584
3394
|
await persistRecord({ ...profile, ...tokens, clientSecret }, deps, signal);
|
|
2585
3395
|
}
|
|
2586
|
-
writePrivate(
|
|
2587
|
-
|
|
3396
|
+
writePrivate(join11(directory, PROFILE_FILE), profile);
|
|
3397
|
+
rmSync7(join11(directory, "machine_terminal.json"), { force: true });
|
|
2588
3398
|
cached = { ...profile, ...tokens };
|
|
2589
3399
|
if (previous) {
|
|
2590
|
-
writePrivate(
|
|
3400
|
+
writePrivate(join11(directory, "machine_retired.json"), [...retired, previous]);
|
|
2591
3401
|
}
|
|
2592
3402
|
await cleanupRetired(deps, signal);
|
|
2593
3403
|
return profile;
|
|
@@ -2626,7 +3436,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2626
3436
|
}
|
|
2627
3437
|
}
|
|
2628
3438
|
return withFileLock(
|
|
2629
|
-
|
|
3439
|
+
join11(directory2, "credentials.lock"),
|
|
2630
3440
|
async () => {
|
|
2631
3441
|
if (machineSessionEnded(deps))
|
|
2632
3442
|
throw new MachineAuthError(
|
|
@@ -2708,7 +3518,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2708
3518
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2709
3519
|
try {
|
|
2710
3520
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2711
|
-
|
|
3521
|
+
rmSync7(contextPath, { force: true });
|
|
2712
3522
|
} catch {
|
|
2713
3523
|
}
|
|
2714
3524
|
throw error;
|
|
@@ -2718,7 +3528,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2718
3528
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2719
3529
|
try {
|
|
2720
3530
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2721
|
-
|
|
3531
|
+
rmSync7(contextPath, { force: true });
|
|
2722
3532
|
} catch {
|
|
2723
3533
|
}
|
|
2724
3534
|
}
|
|
@@ -2732,7 +3542,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2732
3542
|
const expected = machineContext(deps);
|
|
2733
3543
|
const directory = configurationDirectory(deps);
|
|
2734
3544
|
return withFileLock(
|
|
2735
|
-
|
|
3545
|
+
join11(directory, "credentials.lock"),
|
|
2736
3546
|
async () => {
|
|
2737
3547
|
const profile = readMachineProfile(deps);
|
|
2738
3548
|
if (!profile || profile.generation !== expected?.generation || profile.apiUrl !== apiUrl)
|
|
@@ -2782,7 +3592,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2782
3592
|
await persistRecord(record, deps, signal);
|
|
2783
3593
|
} catch (error) {
|
|
2784
3594
|
if (error instanceof MachineAuthError && error.terminal)
|
|
2785
|
-
writePrivate(
|
|
3595
|
+
writePrivate(join11(directory, "machine_terminal.json"), profile.generation);
|
|
2786
3596
|
throw error;
|
|
2787
3597
|
}
|
|
2788
3598
|
}
|
|
@@ -2824,12 +3634,12 @@ async function verifyAcquiredMachineIdentity(machine, deps = {}) {
|
|
|
2824
3634
|
}
|
|
2825
3635
|
async function clearMachineCredentials(deps = {}) {
|
|
2826
3636
|
const directory = configurationDirectory(deps);
|
|
2827
|
-
if (!
|
|
2828
|
-
return withFileLock(
|
|
3637
|
+
if (!existsSync7(join11(directory, PROFILE_FILE))) return false;
|
|
3638
|
+
return withFileLock(join11(directory, "credentials.lock"), async () => {
|
|
2829
3639
|
const profile = readMachineProfile(deps);
|
|
2830
3640
|
if (!profile) return false;
|
|
2831
3641
|
await cleanupRetired(deps, AbortSignal.timeout(1e4));
|
|
2832
|
-
if (
|
|
3642
|
+
if (existsSync7(join11(directory, "machine_retired.json")))
|
|
2833
3643
|
throw new MachineAuthError(
|
|
2834
3644
|
"storage_unavailable",
|
|
2835
3645
|
"Unlock the credential store to remove retired machine credentials."
|
|
@@ -2845,9 +3655,9 @@ async function clearMachineCredentials(deps = {}) {
|
|
|
2845
3655
|
"Unlock the keychain to remove the saved machine credential."
|
|
2846
3656
|
);
|
|
2847
3657
|
}
|
|
2848
|
-
} else
|
|
2849
|
-
|
|
2850
|
-
|
|
3658
|
+
} else rmSync7(recordPath(directory, profile.generation), { force: true });
|
|
3659
|
+
rmSync7(join11(directory, PROFILE_FILE), { force: true });
|
|
3660
|
+
rmSync7(join11(directory, "machine_terminal.json"), { force: true });
|
|
2851
3661
|
cached = void 0;
|
|
2852
3662
|
return true;
|
|
2853
3663
|
});
|
|
@@ -2878,7 +3688,7 @@ function machinePendingBucket(identity) {
|
|
|
2878
3688
|
return `_machine_pending_${hash(`${identity.apiUrl}\0${identity.clientId}`)}`;
|
|
2879
3689
|
}
|
|
2880
3690
|
function readRetiredProfiles(directory) {
|
|
2881
|
-
const value = readPrivate(
|
|
3691
|
+
const value = readPrivate(join11(directory, "machine_retired.json"));
|
|
2882
3692
|
if (value === void 0) return [];
|
|
2883
3693
|
if (!Array.isArray(value))
|
|
2884
3694
|
throw new MachineAuthError(
|
|
@@ -2889,20 +3699,20 @@ function readRetiredProfiles(directory) {
|
|
|
2889
3699
|
}
|
|
2890
3700
|
async function cleanupRetired(deps, signal) {
|
|
2891
3701
|
const directory = configurationDirectory(deps);
|
|
2892
|
-
const path =
|
|
3702
|
+
const path = join11(directory, "machine_retired.json");
|
|
2893
3703
|
const pending = [];
|
|
2894
3704
|
for (const profile of readRetiredProfiles(directory)) {
|
|
2895
3705
|
try {
|
|
2896
3706
|
if (profile.storage === "keychain") {
|
|
2897
3707
|
const entry = (deps.entry ?? keyringEntry)(storageKey(directory, profile.generation));
|
|
2898
3708
|
if (await entry.getPassword(signal) !== null) await entry.deletePassword(signal);
|
|
2899
|
-
} else
|
|
3709
|
+
} else rmSync7(recordPath(directory, profile.generation), { force: true });
|
|
2900
3710
|
} catch {
|
|
2901
3711
|
pending.push(profile);
|
|
2902
3712
|
}
|
|
2903
3713
|
}
|
|
2904
3714
|
if (pending.length) writePrivate(path, pending);
|
|
2905
|
-
else
|
|
3715
|
+
else rmSync7(path, { force: true });
|
|
2906
3716
|
}
|
|
2907
3717
|
function prepareMachineMetadataDirectory(directory) {
|
|
2908
3718
|
ensureDirectory(directory);
|
|
@@ -3012,7 +3822,7 @@ function sessionIdentityMessage(outcome) {
|
|
|
3012
3822
|
return `Primitive could not identify who started this Claude Code cloud session: ${outcome.cause}. To fix it, ${outcome.remedy}.`;
|
|
3013
3823
|
}
|
|
3014
3824
|
function hash2(value) {
|
|
3015
|
-
return
|
|
3825
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
3016
3826
|
}
|
|
3017
3827
|
function sessionContext(deps) {
|
|
3018
3828
|
const env = deps.env ?? process.env;
|
|
@@ -3039,9 +3849,9 @@ function sessionContext(deps) {
|
|
|
3039
3849
|
clientId: environment.clientId,
|
|
3040
3850
|
generation: environment.generation,
|
|
3041
3851
|
sessionId,
|
|
3042
|
-
tokenPath:
|
|
3043
|
-
statePath:
|
|
3044
|
-
lockPath:
|
|
3852
|
+
tokenPath: join12(directory, `session_identity_${key}.json`),
|
|
3853
|
+
statePath: join12(directory, `session_identity_${key}.state.json`),
|
|
3854
|
+
lockPath: join12(directory, "session-identity.lock")
|
|
3045
3855
|
};
|
|
3046
3856
|
}
|
|
3047
3857
|
function readRecord(path) {
|
|
@@ -3073,7 +3883,7 @@ function readState(context) {
|
|
|
3073
3883
|
function writeToken(context, record) {
|
|
3074
3884
|
prepareMachineMetadataDirectory(context.directory);
|
|
3075
3885
|
writeMachineMetadata(context.tokenPath, record);
|
|
3076
|
-
|
|
3886
|
+
rmSync8(context.statePath, { force: true });
|
|
3077
3887
|
}
|
|
3078
3888
|
function writeState(context, state) {
|
|
3079
3889
|
try {
|
|
@@ -3251,8 +4061,8 @@ function gitDetail(stderr) {
|
|
|
3251
4061
|
return (lines.find((line) => /error|denied|refused|forbidden|403|fatal/iu.test(line)) ?? lines.at(-1) ?? "no detail").slice(0, 200);
|
|
3252
4062
|
}
|
|
3253
4063
|
async function defaultSleep3(milliseconds, signal) {
|
|
3254
|
-
await new Promise((
|
|
3255
|
-
const timer = setTimeout(
|
|
4064
|
+
await new Promise((resolve3, reject) => {
|
|
4065
|
+
const timer = setTimeout(resolve3, milliseconds);
|
|
3256
4066
|
signal?.addEventListener(
|
|
3257
4067
|
"abort",
|
|
3258
4068
|
() => {
|
|
@@ -3683,7 +4493,7 @@ async function ensureSessionIdentity(options = {}, deps = {}) {
|
|
|
3683
4493
|
return identified(refreshed.record, true);
|
|
3684
4494
|
}
|
|
3685
4495
|
if (refreshed.status === "refused") {
|
|
3686
|
-
|
|
4496
|
+
rmSync8(ctx.tokenPath, { force: true });
|
|
3687
4497
|
writeState(ctx, {
|
|
3688
4498
|
sessionId: ctx.sessionId,
|
|
3689
4499
|
status: "failed",
|
|
@@ -3919,24 +4729,24 @@ async function linkSessionIdentity(challenge, humanAccessToken, options = {}, de
|
|
|
3919
4729
|
}
|
|
3920
4730
|
|
|
3921
4731
|
// src/lib/credentials.ts
|
|
3922
|
-
import { createHash as
|
|
3923
|
-
import { readFileSync as
|
|
3924
|
-
import { join as
|
|
4732
|
+
import { createHash as createHash7 } from "crypto";
|
|
4733
|
+
import { readFileSync as readFileSync10 } from "fs";
|
|
4734
|
+
import { join as join13 } from "path";
|
|
3925
4735
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
3926
|
-
var TOKEN_FILE_PATH =
|
|
3927
|
-
var REFRESH_TOKEN_PATH =
|
|
3928
|
-
var TOKEN_EXPIRES_PATH =
|
|
3929
|
-
var TERMINAL_REFRESH_PATH =
|
|
3930
|
-
var CREDENTIAL_METADATA_PATH =
|
|
3931
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
3932
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
3933
|
-
var CREDENTIAL_LOCK_PATH =
|
|
4736
|
+
var TOKEN_FILE_PATH = join13(CONFIG_DIRECTORY, "token");
|
|
4737
|
+
var REFRESH_TOKEN_PATH = join13(CONFIG_DIRECTORY, "refresh_token");
|
|
4738
|
+
var TOKEN_EXPIRES_PATH = join13(CONFIG_DIRECTORY, "token_expires_at");
|
|
4739
|
+
var TERMINAL_REFRESH_PATH = join13(CONFIG_DIRECTORY, "refresh_terminal");
|
|
4740
|
+
var CREDENTIAL_METADATA_PATH = join13(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
4741
|
+
var CREDENTIAL_FAMILY_PATH = join13(CONFIG_DIRECTORY, "credential_family.json");
|
|
4742
|
+
var CREDENTIAL_MIGRATION_PATH = join13(CONFIG_DIRECTORY, "credential_migration.json");
|
|
4743
|
+
var CREDENTIAL_LOCK_PATH = join13(CONFIG_DIRECTORY, "credentials.lock");
|
|
3934
4744
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
3935
4745
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
3936
4746
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
3937
4747
|
function readTrimmed(path) {
|
|
3938
4748
|
try {
|
|
3939
|
-
const value =
|
|
4749
|
+
const value = readFileSync10(path, "utf8").trim();
|
|
3940
4750
|
return value || void 0;
|
|
3941
4751
|
} catch {
|
|
3942
4752
|
return void 0;
|
|
@@ -3976,15 +4786,15 @@ function resolveStoredCredential(env = process.env) {
|
|
|
3976
4786
|
const directory = primConfigDirectory({ env });
|
|
3977
4787
|
return resolveAuthCredential({
|
|
3978
4788
|
env,
|
|
3979
|
-
tokenFilePath:
|
|
3980
|
-
refreshTokenPath:
|
|
3981
|
-
metadataPath:
|
|
3982
|
-
familyPath:
|
|
3983
|
-
migrationPath:
|
|
4789
|
+
tokenFilePath: join13(directory, "token"),
|
|
4790
|
+
refreshTokenPath: join13(directory, "refresh_token"),
|
|
4791
|
+
metadataPath: join13(directory, "credential_metadata.json"),
|
|
4792
|
+
familyPath: join13(directory, "credential_family.json"),
|
|
4793
|
+
migrationPath: join13(directory, "credential_migration.json")
|
|
3984
4794
|
});
|
|
3985
4795
|
}
|
|
3986
4796
|
function credentialFingerprint(value) {
|
|
3987
|
-
return
|
|
4797
|
+
return createHash7("sha256").update(value).digest("hex");
|
|
3988
4798
|
}
|
|
3989
4799
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
3990
4800
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -4004,7 +4814,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
4004
4814
|
function readBoundedCredentialJson(path) {
|
|
4005
4815
|
let raw;
|
|
4006
4816
|
try {
|
|
4007
|
-
raw =
|
|
4817
|
+
raw = readFileSync10(path, "utf8");
|
|
4008
4818
|
} catch (error) {
|
|
4009
4819
|
return error.code === "ENOENT" ? void 0 : null;
|
|
4010
4820
|
}
|
|
@@ -4081,28 +4891,38 @@ function jwtOrganizationId(token) {
|
|
|
4081
4891
|
|
|
4082
4892
|
export {
|
|
4083
4893
|
syncDirectory,
|
|
4084
|
-
syncFile,
|
|
4085
4894
|
ensureDurableDirectory,
|
|
4086
4895
|
atomicWriteFile,
|
|
4087
4896
|
processIsAlive,
|
|
4088
4897
|
withFileLockSync,
|
|
4089
4898
|
withFileLock,
|
|
4090
4899
|
compareSemver,
|
|
4091
|
-
STABLE_HOOK_LAUNCHER_NAME,
|
|
4092
4900
|
binFile,
|
|
4093
4901
|
packageVersion,
|
|
4094
4902
|
packageRoot,
|
|
4095
|
-
shellQuote,
|
|
4096
4903
|
pinnedNpxArgs,
|
|
4097
4904
|
pinnedNpxCommand,
|
|
4098
4905
|
pinnedHookCommand,
|
|
4099
|
-
PRIM_CONFIG_SHELL_RESOLVER,
|
|
4100
4906
|
stableHookCommand,
|
|
4101
4907
|
quietWithoutLauncher,
|
|
4102
4908
|
detachedHookShimCommand,
|
|
4103
4909
|
commandMatchesBin,
|
|
4104
4910
|
hookCommandResolution,
|
|
4105
4911
|
hookCommandResolutions,
|
|
4912
|
+
MANAGED_GIT_HOOK_NAMES,
|
|
4913
|
+
GIT_HOOK_CONTRACT_VERSION,
|
|
4914
|
+
blockMarkers,
|
|
4915
|
+
managedHookBlock,
|
|
4916
|
+
canonicalHookBlock,
|
|
4917
|
+
hookBlockContractVersion,
|
|
4918
|
+
primConfigDirectory,
|
|
4919
|
+
hookRuntimePaths,
|
|
4920
|
+
inspectHookRuntime,
|
|
4921
|
+
stableNodePath,
|
|
4922
|
+
stageHookRuntime,
|
|
4923
|
+
inspectGitHookEntrypoint,
|
|
4924
|
+
assertOwnedHookRuntime,
|
|
4925
|
+
removeHookRuntime,
|
|
4106
4926
|
isClaudeCloudSession,
|
|
4107
4927
|
insideClaudeCode,
|
|
4108
4928
|
CLOUD_CREDENTIALS_REMEDY,
|
|
@@ -4127,7 +4947,6 @@ export {
|
|
|
4127
4947
|
exchangeMachineCredentials,
|
|
4128
4948
|
parseMachineIdentity,
|
|
4129
4949
|
verifyMachineIdentity,
|
|
4130
|
-
primConfigDirectory,
|
|
4131
4950
|
clearMachineCooldowns,
|
|
4132
4951
|
SERVER_WAIT_PREFIX,
|
|
4133
4952
|
gitToplevel,
|