@kubb/studio 5.2.7 → 5.2.8

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 CHANGED
@@ -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 (0, node_crypto.hash)("sha256", node_process.default.env.KUBB_AGENT_SECRET);
426
+ if (node_process.default.env.KUBB_AGENT_SECRET) return machineTokenFrom(node_process.default.env.KUBB_AGENT_SECRET);
420
427
  fallbackSecretPromise ??= loadOrCreateFallbackSecret();
421
- return (0, node_crypto.hash)("sha256", await fallbackSecretPromise);
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.2.7";
654
+ var version = "5.2.8";
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;