@kubb/studio 5.2.7 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +40 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +38 -1
- package/dist/index.js +38 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -2,14 +2,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_rolldown_runtime = require("./rolldown-runtime-qbf5tadS.cjs");
|
|
3
3
|
const require_protocol = require("./protocol.cjs");
|
|
4
4
|
let node_util = require("node:util");
|
|
5
|
+
let node_process = require("node:process");
|
|
6
|
+
node_process = require_rolldown_runtime.__toESM(node_process, 1);
|
|
7
|
+
let node_child_process = require("node:child_process");
|
|
5
8
|
let node_fs_promises = require("node:fs/promises");
|
|
6
9
|
let node_path = require("node:path");
|
|
7
10
|
node_path = require_rolldown_runtime.__toESM(node_path, 1);
|
|
8
|
-
let node_child_process = require("node:child_process");
|
|
9
11
|
let ofetch = require("ofetch");
|
|
10
12
|
let node_crypto = require("node:crypto");
|
|
11
|
-
let node_process = require("node:process");
|
|
12
|
-
node_process = require_rolldown_runtime.__toESM(node_process, 1);
|
|
13
13
|
let unstorage = require("unstorage");
|
|
14
14
|
let unstorage_drivers_fs = require("unstorage/drivers/fs");
|
|
15
15
|
unstorage_drivers_fs = require_rolldown_runtime.__toESM(unstorage_drivers_fs, 1);
|
|
@@ -411,14 +411,21 @@ async function loadOrCreateFallbackSecret() {
|
|
|
411
411
|
return secret;
|
|
412
412
|
}
|
|
413
413
|
/**
|
|
414
|
+
* Hashes a stable secret into the machine token Studio expects, so a host can derive one from its
|
|
415
|
+
* own identity without duplicating `getMachineToken`'s SHA-256 step.
|
|
416
|
+
*/
|
|
417
|
+
function machineTokenFrom(secret) {
|
|
418
|
+
return (0, node_crypto.hash)("sha256", secret);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
414
421
|
* Returns the machine token derived from the `KUBB_AGENT_SECRET` environment variable.
|
|
415
422
|
* Falls back to a generated secret persisted in the runtime storage if the env var is not set.
|
|
416
423
|
* The token is hashed with SHA-256.
|
|
417
424
|
*/
|
|
418
425
|
async function getMachineToken() {
|
|
419
|
-
if (node_process.default.env.KUBB_AGENT_SECRET) return (
|
|
426
|
+
if (node_process.default.env.KUBB_AGENT_SECRET) return machineTokenFrom(node_process.default.env.KUBB_AGENT_SECRET);
|
|
420
427
|
fallbackSecretPromise ??= loadOrCreateFallbackSecret();
|
|
421
|
-
return (
|
|
428
|
+
return machineTokenFrom(await fallbackSecretPromise);
|
|
422
429
|
}
|
|
423
430
|
//#endregion
|
|
424
431
|
//#region src/api.ts
|
|
@@ -617,9 +624,34 @@ async function waitForJob({ studioUrl, token, id, timeoutMs = 6e4 }) {
|
|
|
617
624
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
618
625
|
}
|
|
619
626
|
}
|
|
627
|
+
/**
|
|
628
|
+
* Creates or reuses a CI agent (`POST /api/agents`), keyed by `(organization, machineToken)`.
|
|
629
|
+
* Authenticates via `x-api-key`. Reusing the same `machineToken` reuses the same agent instead of
|
|
630
|
+
* consuming a new one from the organization's agent limit.
|
|
631
|
+
*/
|
|
632
|
+
async function createAgent({ studioUrl, token, name, machineToken }) {
|
|
633
|
+
try {
|
|
634
|
+
return await (0, ofetch.ofetch)(`${studioUrl}/api/agents`, {
|
|
635
|
+
method: "POST",
|
|
636
|
+
headers: { "x-api-key": token },
|
|
637
|
+
body: {
|
|
638
|
+
name,
|
|
639
|
+
machineToken
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
} catch (error) {
|
|
643
|
+
if (error instanceof ofetch.FetchError) {
|
|
644
|
+
const upgradeUrl = error.data?.data?.upgradeUrl;
|
|
645
|
+
const detail = responseMessage(error.data) ?? getErrorMessage(error);
|
|
646
|
+
const hint = upgradeUrl ? ` Agent limit reached; upgrade at ${upgradeUrl}.` : "";
|
|
647
|
+
throw new Error(`Failed to create a Kubb Studio agent: ${detail}${hint}`, { cause: error });
|
|
648
|
+
}
|
|
649
|
+
throw error;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
620
652
|
//#endregion
|
|
621
653
|
//#region package.json
|
|
622
|
-
var version = "5.
|
|
654
|
+
var version = "5.3.0";
|
|
623
655
|
//#endregion
|
|
624
656
|
//#region src/hooks.ts
|
|
625
657
|
/**
|
|
@@ -2497,11 +2529,13 @@ async function pollForPairingToken({ studioUrl = agentDefaults.studioUrl, sessio
|
|
|
2497
2529
|
//#endregion
|
|
2498
2530
|
exports.InvalidAgentTokenError = InvalidAgentTokenError;
|
|
2499
2531
|
exports.PairingCanceledError = PairingCanceledError;
|
|
2532
|
+
exports.createAgent = createAgent;
|
|
2500
2533
|
exports.createClient = createClient;
|
|
2501
2534
|
exports.createFileStorage = createFileStorage;
|
|
2502
2535
|
exports.createJob = createJob;
|
|
2503
2536
|
exports.createJobId = require_protocol.createJobId;
|
|
2504
2537
|
exports.defaultStudioUrl = defaultStudioUrl;
|
|
2538
|
+
exports.machineTokenFrom = machineTokenFrom;
|
|
2505
2539
|
exports.pollForPairingToken = pollForPairingToken;
|
|
2506
2540
|
exports.runConnection = runConnection;
|
|
2507
2541
|
exports.setStorage = setStorage;
|