@theholocron/github-client 1.58.0-alpha.69 → 1.58.0-alpha.71

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.d.mts CHANGED
@@ -1003,15 +1003,28 @@ interface InstallationAccessToken {
1003
1003
  * to call the REST API directly.
1004
1004
  */
1005
1005
  export declare function createAppJWT(creds: GitHubAppCredentials, now?: number): Promise<string>;
1006
+ /** Options for minting and caching an installation token. */
1007
+ type InstallationTokenOptions = Pick<GitHubClientOptions, "baseUrl" | "fetch"> & {
1008
+ /** Clock in ms since the epoch; injectable for tests. */
1009
+ now?: () => number;
1010
+ };
1011
+ /** Drops every cached installation token (tests, or a rotated App key). */
1012
+ export declare function clearInstallationTokenCache(): void;
1013
+ /** Drops one cached token — call it when GitHub answers 401 for a token that was still cached. */
1014
+ export declare function forgetInstallationToken(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): void;
1006
1015
  /**
1007
1016
  * Exchanges an App JWT for an installation access token — the credential
1008
1017
  * that actually authenticates REST calls, scoped to exactly one
1009
1018
  * installation (D10: never a hardcoded org — the caller supplies which
1010
1019
  * installation, from the webhook payload that triggered this).
1020
+ *
1021
+ * The token is cached in memory (per app, API host and installation) until
1022
+ * five minutes before it expires; concurrent callers share one mint and a
1023
+ * failure is never cached.
1011
1024
  */
1012
- export declare function getInstallationAccessToken(creds: GitHubAppCredentials, installationId: number, opts?: Pick<GitHubClientOptions, "baseUrl" | "fetch">): Promise<InstallationAccessToken>;
1025
+ export declare function getInstallationAccessToken(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): Promise<InstallationAccessToken>;
1013
1026
  /** The whole flow in one call — an already-authenticated `GitHubClient` scoped to one installation. */
1014
- export declare function createInstallationClient(creds: GitHubAppCredentials, installationId: number, opts?: Pick<GitHubClientOptions, "baseUrl" | "fetch">): Promise<GitHubClient>;
1027
+ export declare function createInstallationClient(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): Promise<GitHubClient>;
1015
1028
  /** The account an App or one of its installations belongs to. */
1016
1029
  interface GitHubAppAccount {
1017
1030
  /** The user or organization login. */
@@ -1382,4 +1395,4 @@ export declare function createGitHubClient(opts: GitHubClientOptions): {
1382
1395
  };
1383
1396
  /** The client returned by `createGitHubClient()`: one group of methods per GitHub API area. */
1384
1397
  export type GitHubClient = ReturnType<typeof createGitHubClient>;
1385
- export type { CheckRunAnnotation, CheckRunAnnotationLevel, CheckRunConclusion, CheckRunOutput, CheckRunStatus, CodeScanningSetupResult, CommitStatusState, CreateCheckRunInput, CreateHookInput, CreatePagesPayload, CreatePullInput, CreatePullRequestInput, CreateReviewComment, CreateReviewInput, GitBlob, GitCommit, GitContents, GitDirectoryEntry, GitHubApp, GitHubAppAccount, GitHubAppCredentials, GitHubAppDelivery, GitHubAppInstallation, GitHubCheckRun, GitHubClientOptions, GitHubCommitStatus, GitHubContents, GitHubEnvironment, GitHubHook, GitHubInstallationWebhookPayload, GitHubIssue, GitHubIssueComment, GitHubIssueField, GitHubIssueFieldOption, GitHubIssueFieldValue, GitHubLabel, GitHubMilestone, GitHubPages, GitHubPublicKey, GitHubPullRequest, GitHubPullRequestCommit, GitHubPullRequestFile, GitHubPullRequestReview, GitHubPullRequestWebhookPayload, GitHubPushWebhookPayload, GitHubRepo, GitHubRuleset, GitHubRulesetDetail, GitHubUser, GitHubUserWithExpiry, GitHubVariable, GitHubWebhookHeaders, GitHubWorkflowRun, GitPull, GitRef, GitTree, GitTreeItem, GraphQLClient, HookScope, InstallationAccessToken, IssueFieldValueInput, IssueSearchParams, IssueTypeColor, IssueTypeInput, ListAppDeliveriesOptions, ListDirectoryOptions, OrgIssueTypeInfo, OrgProperty, OrgSecretInfo, OrgSecretVisibility, PagesBuildStatus, PagesBuildType, PutContentsInput, PutContentsResult, RepoPropertyValue, RepoTeam, ReviewThread, SecretScope, TeamMember, TeamPermission, UpdateCheckRunInput, UpdateHookInput, UpdatePagesPayload, UpdatePullRequestInput, VariableScope, VariableVisibility, WorkflowRunFilter };
1398
+ export type { CheckRunAnnotation, CheckRunAnnotationLevel, CheckRunConclusion, CheckRunOutput, CheckRunStatus, CodeScanningSetupResult, CommitStatusState, CreateCheckRunInput, CreateHookInput, CreatePagesPayload, CreatePullInput, CreatePullRequestInput, CreateReviewComment, CreateReviewInput, GitBlob, GitCommit, GitContents, GitDirectoryEntry, GitHubApp, GitHubAppAccount, GitHubAppCredentials, GitHubAppDelivery, GitHubAppInstallation, GitHubCheckRun, GitHubClientOptions, GitHubCommitStatus, GitHubContents, GitHubEnvironment, GitHubHook, GitHubInstallationWebhookPayload, GitHubIssue, GitHubIssueComment, GitHubIssueField, GitHubIssueFieldOption, GitHubIssueFieldValue, GitHubLabel, GitHubMilestone, GitHubPages, GitHubPublicKey, GitHubPullRequest, GitHubPullRequestCommit, GitHubPullRequestFile, GitHubPullRequestReview, GitHubPullRequestWebhookPayload, GitHubPushWebhookPayload, GitHubRepo, GitHubRuleset, GitHubRulesetDetail, GitHubUser, GitHubUserWithExpiry, GitHubVariable, GitHubWebhookHeaders, GitHubWorkflowRun, GitPull, GitRef, GitTree, GitTreeItem, GraphQLClient, HookScope, InstallationAccessToken, InstallationTokenOptions, IssueFieldValueInput, IssueSearchParams, IssueTypeColor, IssueTypeInput, ListAppDeliveriesOptions, ListDirectoryOptions, OrgIssueTypeInfo, OrgProperty, OrgSecretInfo, OrgSecretVisibility, PagesBuildStatus, PagesBuildType, PutContentsInput, PutContentsResult, RepoPropertyValue, RepoTeam, ReviewThread, SecretScope, TeamMember, TeamPermission, UpdateCheckRunInput, UpdateHookInput, UpdatePagesPayload, UpdatePullRequestInput, VariableScope, VariableVisibility, WorkflowRunFilter };
package/dist/index.mjs CHANGED
@@ -1208,14 +1208,27 @@ async function createAppJWT(creds, now = Date.now()) {
1208
1208
  const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", key, new TextEncoder().encode(signingInput));
1209
1209
  return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;
1210
1210
  }
1211
- /**
1212
- * Exchanges an App JWT for an installation access token — the credential
1213
- * that actually authenticates REST calls, scoped to exactly one
1214
- * installation (D10: never a hardcoded org — the caller supplies which
1215
- * installation, from the webhook payload that triggered this).
1216
- */
1217
- async function getInstallationAccessToken(creds, installationId, opts = {}) {
1218
- const jwt = await createAppJWT(creds);
1211
+ /** A cached token is dropped this long before GitHub says it expires. */
1212
+ const REFRESH_BEFORE_EXPIRY_MS = 3e5;
1213
+ const tokenCache = /* @__PURE__ */ new Map();
1214
+ const inFlight = /* @__PURE__ */ new Map();
1215
+ /** Key by app, API host and installation only — never the private key. */
1216
+ function cacheKey(creds, installationId, opts) {
1217
+ return `${creds.appId}|${opts.baseUrl ?? "https://api.github.com"}|${installationId}`;
1218
+ }
1219
+ /** Drops every cached installation token (tests, or a rotated App key). */
1220
+ function clearInstallationTokenCache() {
1221
+ tokenCache.clear();
1222
+ inFlight.clear();
1223
+ }
1224
+ /** Drops one cached token — call it when GitHub answers 401 for a token that was still cached. */
1225
+ function forgetInstallationToken(creds, installationId, opts = {}) {
1226
+ const key = cacheKey(creds, installationId, opts);
1227
+ tokenCache.delete(key);
1228
+ inFlight.delete(key);
1229
+ }
1230
+ async function mintInstallationAccessToken(creds, installationId, opts) {
1231
+ const jwt = await createAppJWT(creds, opts.now?.());
1219
1232
  const baseUrl = opts.baseUrl ?? "https://api.github.com";
1220
1233
  const res = await (opts.fetch ?? globalThis.fetch)(`${baseUrl}/app/installations/${installationId}/access_tokens`, {
1221
1234
  method: "POST",
@@ -1235,6 +1248,32 @@ async function getInstallationAccessToken(creds, installationId, opts = {}) {
1235
1248
  expiresAt: json.expires_at
1236
1249
  };
1237
1250
  }
1251
+ /**
1252
+ * Exchanges an App JWT for an installation access token — the credential
1253
+ * that actually authenticates REST calls, scoped to exactly one
1254
+ * installation (D10: never a hardcoded org — the caller supplies which
1255
+ * installation, from the webhook payload that triggered this).
1256
+ *
1257
+ * The token is cached in memory (per app, API host and installation) until
1258
+ * five minutes before it expires; concurrent callers share one mint and a
1259
+ * failure is never cached.
1260
+ */
1261
+ async function getInstallationAccessToken(creds, installationId, opts = {}) {
1262
+ const key = cacheKey(creds, installationId, opts);
1263
+ const now = opts.now?.() ?? Date.now();
1264
+ const cached = tokenCache.get(key);
1265
+ if (cached && Date.parse(cached.expiresAt) - REFRESH_BEFORE_EXPIRY_MS > now) return cached;
1266
+ const pending = inFlight.get(key);
1267
+ if (pending) return pending;
1268
+ const mint = mintInstallationAccessToken(creds, installationId, opts).then((token) => {
1269
+ if (inFlight.get(key) === mint) tokenCache.set(key, token);
1270
+ return token;
1271
+ }).finally(() => {
1272
+ if (inFlight.get(key) === mint) inFlight.delete(key);
1273
+ });
1274
+ inFlight.set(key, mint);
1275
+ return mint;
1276
+ }
1238
1277
  /** The whole flow in one call — an already-authenticated `GitHubClient` scoped to one installation. */
1239
1278
  async function createInstallationClient(creds, installationId, opts = {}) {
1240
1279
  const { token } = await getInstallationAccessToken(creds, installationId, opts);
@@ -1469,4 +1508,4 @@ function createGitHubClient(opts) {
1469
1508
  workflows: workflows(rest)
1470
1509
  };
1471
1510
  }
1472
- export { MAX_CHECK_RUN_ANNOTATIONS, createAppJWT, createGitHubClient, createInstallationClient, getApp, getInstallationAccessToken, getRepoInstallation, listAppDeliveries, listAppInstallations, parseGitHubWebhookHeaders, parseTokenExpiration, redeliverAppDelivery, suspendAppInstallation, unsuspendAppInstallation, verifyGitHubWebhookSignature };
1511
+ export { MAX_CHECK_RUN_ANNOTATIONS, clearInstallationTokenCache, createAppJWT, createGitHubClient, createInstallationClient, forgetInstallationToken, getApp, getInstallationAccessToken, getRepoInstallation, listAppDeliveries, listAppInstallations, parseGitHubWebhookHeaders, parseTokenExpiration, redeliverAppDelivery, suspendAppInstallation, unsuspendAppInstallation, verifyGitHubWebhookSignature };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/github-client",
3
- "version": "1.58.0-alpha.69",
3
+ "version": "1.58.0-alpha.71",
4
4
  "description": "A TypeScript client for the GitHub REST API",
5
5
  "keywords": [
6
6
  "github",
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@theholocron/http-client": "^1.58.0-alpha.69",
35
+ "@theholocron/http-client": "^1.58.0-alpha.71",
36
36
  "@theholocron/observability": "^0.7.0-alpha.9"
37
37
  },
38
38
  "devDependencies": {
@@ -45,8 +45,8 @@
45
45
  "tsdown": "^0.23.7",
46
46
  "typescript": "^6.0.3",
47
47
  "vitest": "^5.1.1",
48
- "@theholocron/eslint-config": "^8.9.0-alpha.24",
49
48
  "@theholocron/tsconfig": "^8.9.0-alpha.24",
49
+ "@theholocron/eslint-config": "^8.9.0-alpha.24",
50
50
  "@theholocron/tsdown-config": "^8.9.0-alpha.24",
51
51
  "@theholocron/vitest-config": "^8.9.0-alpha.24"
52
52
  },