@vite-hub/forge 0.6.1 → 0.8.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.
Files changed (25) hide show
  1. package/README.md +13 -2
  2. package/dist/crypto-DydCoK4e-BCG61qh5.js +123 -0
  3. package/dist/{webhooks-Bc6QhmI8-eK71_mJE.js → define-C9xZZNVR-C-oojQRP.js} +3 -1118
  4. package/dist/{definition-BQFdu9ti-WxMjcAEl.js → definition-BQFdu9ti-BZgR-gkB.js} +5 -4
  5. package/dist/{github-CHzhqYOr.js → definition-s83gxg2p-JwDgFji9.js} +27 -28
  6. package/dist/{events-BL0zcgMf-9TwyYtCt.js → events-BL0zcgMf-Exs7JLX1.js} +5 -2
  7. package/dist/{events-DDdJnmD2-BSMoOqjV.js → events-DDdJnmD2-CdPFePvp.js} +5 -3
  8. package/dist/{events-DHqSJ5KI-z2-5_1vo.js → events-DHqSJ5KI-mRtn-qQ0.js} +5 -3
  9. package/dist/{events-DXE-Ul2S-BzTkugIK.js → events-DXE-Ul2S-BEUw4sQF.js} +5 -3
  10. package/dist/{feed-B1OiHFg0-DevZZe0y.js → feed-B1OiHFg0-ppQHqkxG.js} +4 -2
  11. package/dist/{feed-DROLF1tb-CtRfA_7N.js → feed-DROLF1tb-CMAanXw8.js} +4 -2
  12. package/dist/{feed-iWegqepj-ConxMpg4.js → feed-iWegqepj-DKBLqhvD.js} +4 -2
  13. package/dist/{forgejo-4sbZukng.js → forgejo-Deckk49p.js} +2 -2
  14. package/dist/{gitea-Cj7qy2X3.js → gitea-ChQKeRrf.js} +2 -2
  15. package/dist/{gitee-CKt4pEC0-BuLWggck.js → gitee-CKt4pEC0-BuzC1PrM.js} +1 -1
  16. package/dist/{gitlab-CS0shW2u.js → gitlab-CzCikZzN.js} +5 -4
  17. package/dist/{graphql-DxRA9BGO-BBbmTmWi.js → graphql-DxRA9BGO-Cy511t8-.js} +2 -1
  18. package/dist/index.d.ts +91 -3
  19. package/dist/index.js +400 -65
  20. package/dist/kit-CrquHPqr.js +4 -0
  21. package/dist/{templates-0m5QBVIh-D1fZeoC-.js → templates-0m5QBVIh-C1DcQ_yo.js} +1 -2
  22. package/dist/utils-DEwhctg5-BM3AMnSs.js +1213 -0
  23. package/dist/webhooks-Bc6QhmI8-CgoCnwj1.js +71 -0
  24. package/package.json +2 -2
  25. package/dist/model-Biiu1CfI-CVLamGai.js +0 -288
package/README.md CHANGED
@@ -29,12 +29,17 @@ await github.request("POST", "/repos/acme/web/issues", { body: { title: "Nightly
29
29
  | Call | Returns |
30
30
  | --- | --- |
31
31
  | `useForge({ forge, baseUrl, env, fetch, userAgent, conditionalRequests })` | The Forge of this deployment. `conditionalRequests: false` stops GitHub providers from revalidating repeated GETs with ETags. `forge` defaults to `"github"`. `env` defaults to the active Cloudflare bindings, then `process.env`. Throws `FORGE_MISSING_ENV` or `FORGE_INVALID_ENV` when the env cannot work. |
32
+ | `checkForge(options)` | Checks the same env with no request, for a Setup page. Returns `[undefined, { kind, auth, user }]`, where `user` says whether `forge.user` is set, or `[error, undefined]` with `FORGE_MISSING_ENV` or `FORGE_INVALID_ENV`. It checks the base URL and verifies that the inline or file-based GitHub App private key can sign. It does not test whether the forge accepts the credentials. |
32
33
  | `forge.kind`, `forge.auth`, `forge.baseUrl` | The Forge, `"app"` or `"token"`, and the API base. |
33
34
  | `forge.token({ repository, installation, refresh, minLifetime, signal })` | `[error, { token, expiresAt }]`. With a GitHub App, an installation token for `installation`, such as `installation.id` of a webhook delivery, else for the owner of `repository` (see [Installations](https://vitehub.dev/docs/forge/configure#installations)), or for `VITEHUB_GITHUB_INSTALLATION_ID` without `repository`. Cached until `minLifetime` milliseconds (default one minute) before it expires. |
34
35
  | `forge.provider({ repository, installation, refresh, minLifetime, signal })` | `[error, provider]`, a `forges` provider that sends `token()`. A GitHub provider has `graphql()`. |
35
36
  | `forge.app({ signal })` | `[error, { id, slug, login, email }]` of the GitHub App. Throws `FORGE_NOT_APP` with a token. |
37
+ | `forge.user` | GitHub App user-to-server tokens, to act as a person who authorized the App. `undefined` unless the Forge is GitHub and `VITEHUB_GITHUB_CLIENT_ID` and `VITEHUB_GITHUB_CLIENT_SECRET` are set. See [Act as a person](https://vitehub.dev/docs/forge/server-api#act-as-a-person). |
38
+ | `forge.user.authorizeUrl({ redirectUri, state })` | The GitHub page that asks the person to authorize the App. On GitHub Enterprise Server, the web host of the API base. |
39
+ | `forge.user.exchange({ code, redirectUri, signal })`, `forge.user.refresh({ refreshToken, signal })` | `[error, { token, expiresAt, refreshToken, refreshTokenExpiresAt, login, id }]`. `login` and `id` come from `GET /user` with the new token. The expiries are `undefined` when the App has user token expiry off. Errors are `FORGE_USER_AUTH_FAILED` and `FORGE_REQUEST_FAILED`. |
40
+ | `forge.user.provider({ token })` | A `forges` GitHub provider that sends the user token. |
36
41
 
37
- GitHub uses the App when `VITEHUB_GITHUB_APP_ID` is set, and `VITEHUB_GITHUB_TOKEN` otherwise. Runtime errors are `FORGE_APP_NOT_INSTALLED` and `FORGE_REQUEST_FAILED`. A cancelled call rejects with the abort reason. The codes are documented at [Forge limits and errors](https://vitehub.dev/docs/forge/limits-and-errors).
42
+ GitHub uses the App when `VITEHUB_GITHUB_APP_ID` is set, and `VITEHUB_GITHUB_TOKEN` otherwise. Runtime errors are `FORGE_APP_NOT_INSTALLED`, `FORGE_REQUEST_FAILED` and, from `forge.user`, `FORGE_USER_AUTH_FAILED`. A cancelled call rejects with the abort reason. The codes are documented at [Forge limits and errors](https://vitehub.dev/docs/forge/limits-and-errors).
38
43
 
39
44
  ## Server Env
40
45
 
@@ -46,6 +51,8 @@ Each field reads its `VITEHUB_` name first, then the `FORGES_` name that the `fo
46
51
  | `github.appId` | `VITEHUB_GITHUB_APP_ID`, `FORGES_GITHUB_APP_ID` |
47
52
  | `github.privateKey` | `VITEHUB_GITHUB_PRIVATE_KEY`, `FORGES_GITHUB_PRIVATE_KEY` |
48
53
  | `github.privateKeyPath` | `VITEHUB_GITHUB_PRIVATE_KEY_PATH` |
54
+ | `github.clientId` | `VITEHUB_GITHUB_CLIENT_ID`, `FORGES_GITHUB_CLIENT_ID` |
55
+ | `github.clientSecret` | `VITEHUB_GITHUB_CLIENT_SECRET`, `FORGES_GITHUB_CLIENT_SECRET` |
49
56
  | `github.installationId` | `VITEHUB_GITHUB_INSTALLATION_ID`, `FORGES_GITHUB_INSTALLATION_ID` |
50
57
  | `github.appOwner` | `VITEHUB_GITHUB_APP_OWNER` |
51
58
  | `github.appInstallations` | `VITEHUB_GITHUB_APP_INSTALLATIONS` |
@@ -64,11 +71,12 @@ For Gitea, `useForge()` uses `VITEHUB_GITEA_WEBHOOK_SECRET` (or `FORGES_GITEA_WE
64
71
 
65
72
  ## Public imports
66
73
 
67
- `useForge()` is the app API. The other exports are the building blocks that ViteHub packages use.
74
+ `useForge()` and `checkForge()` are the app API. The other exports are the building blocks that ViteHub packages use.
68
75
 
69
76
  | Import | Use |
70
77
  | --- | --- |
71
78
  | `useForge(options)` | The Forge identity of the app, as above. |
79
+ | `checkForge(options)` | Checks the Forge env without a request, as above. |
72
80
  | `forgeEnv` | The Server Env table above. |
73
81
  | `readForgeEnv(field, env)` | The first value set for one `forgeEnv` field, from an env record or a lookup function. |
74
82
  | `forgeProvider(connection)` | A `forges` provider for one Forge. Return it from a public API only where that API owns the Forge credentials. A GitHub provider has `graphql()`. GitHub providers revalidate repeated GETs with `If-None-Match`, so unchanged responses come back as `304`s that GitHub doesn't charge; `conditionalRequests: false` turns it off, and `{ identity, cache }` sets who shares entries and where they live. |
@@ -82,6 +90,8 @@ For Gitea, `useForge()` uses `VITEHUB_GITEA_WEBHOOK_SECRET` (or `FORGES_GITEA_WE
82
90
  | `readGitHubAppPrivateKey(inline, path)` | Read a PEM key from a value or a file. |
83
91
  | `githubInstallationRouting({ installationId, appOwner, appInstallations })` | Read the [installation routing](https://vitehub.dev/docs/forge/configure#installations) from Server Env values. Throws `FORGE_INVALID_ENV`. |
84
92
  | `githubInstallationFor(routing, repository)` | The configured installation for `repository`, or `undefined` when GitHub must find it. |
93
+ | `provider.contents.resolveRef(repo, ref)` | The full commit sha of a branch, tag or sha. Part of `forges`; use it to turn a branch into a commit. |
94
+ | `readForgeArchive(provider, repo, { ref, maxBytes, signal })` | `{ commit, bytes }`: the `tar.gz` of the repository tree at `ref` and the commit it came from. A branch or tag resolves to its commit first. Pass a non-negative safe integer for `maxBytes` (default 64 MiB). A larger archive rejects with `FORGE_ARCHIVE_TOO_LARGE`. GitHub only. |
85
95
  | `forgeArchive(provider, repo, { ref, signal })` | The repository tree at `ref` as a `tar.gz` stream. GitHub only. Anonymous github.com reads go to `codeload.github.com` and do not use the API limit. |
86
96
  | `forgeGitCredentials({ forge, baseUrl, token, repository })` | The Git smart HTTP remote and the basic auth pair for one repository. |
87
97
  | `forgeGitAuthHeader(credentials)` | The `http.<remote>.extraheader` entry for `GIT_CONFIG_KEY_<n>` and `GIT_CONFIG_VALUE_<n>`. It keeps the token out of process arguments. |
@@ -103,6 +113,7 @@ For Gitea, `useForge()` uses `VITEHUB_GITEA_WEBHOOK_SECRET` (or `FORGES_GITEA_WE
103
113
  | `FORGE_R0006` | A GitHub-only helper got a provider for another Forge. |
104
114
  | `FORGE_R0007` | `githubBranchRuleDetails()` got branch rules that are not complete GitHub rulesets or classic protection. |
105
115
  | `FORGE_R0009` | A GitHub-only helper got a reference that is not a numbered pull request, or an empty input. |
116
+ | `FORGE_ARCHIVE_TOO_LARGE` | `readForgeArchive()` read more than `maxBytes`. It stops the download at the limit. |
106
117
  | `FORGE_RESPONSE_INVALID` | A Forge request returned missing or invalid data. Read the error message for the specific problem. |
107
118
  | `FORGE_UNSUPPORTED` | The `forge` value of a Forge connection is not `github`, `gitlab`, `forgejo`, or `gitea`. |
108
119
 
@@ -0,0 +1,123 @@
1
+ // ../../node_modules/.pnpm/forges@0.4.1/node_modules/forges/dist/crypto-DydCoK4e.mjs
2
+ const encoder = new TextEncoder();
3
+ function toBytes(value) {
4
+ return typeof value === "string" ? encoder.encode(value) : value;
5
+ }
6
+ function toHex(buffer) {
7
+ return [...new Uint8Array(buffer)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
8
+ }
9
+ /** Base64 for arbitrary bytes, a byte at a time so a large buffer cannot overflow the call stack. */
10
+ function toBase64(bytes) {
11
+ let binary = "";
12
+ for (const byte of bytes) binary += String.fromCharCode(byte);
13
+ return btoa(binary);
14
+ }
15
+ function toBase64Url(input) {
16
+ return (typeof input === "string" ? btoa(input) : toBase64(input)).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
17
+ }
18
+ async function hmacSha256(secret, body) {
19
+ const key = await crypto.subtle.importKey("raw", toBytes(secret), {
20
+ name: "HMAC",
21
+ hash: "SHA-256"
22
+ }, false, ["sign"]);
23
+ return crypto.subtle.sign("HMAC", key, toBytes(body));
24
+ }
25
+ /** Lowercase hex HMAC-SHA256 of `body` under `secret`. */
26
+ async function hmacSha256Hex(secret, body) {
27
+ return toHex(await hmacSha256(secret, body));
28
+ }
29
+ /**
30
+ * Constant-time comparison of two strings. The time taken depends only on the
31
+ * length of `a`, so pass the untrusted value as `a`.
32
+ */
33
+ function timingSafeEqual(a, b) {
34
+ let mismatch = a.length ^ b.length;
35
+ for (let index = 0; index < a.length; index++) mismatch |= a.charCodeAt(index) ^ b.charCodeAt(index % b.length);
36
+ return mismatch === 0;
37
+ }
38
+ function decodePem(pem) {
39
+ const pkcs1 = pem.includes("BEGIN RSA PRIVATE KEY");
40
+ const body = pem.replace(/-----(?:BEGIN|END)[^-]+-----/g, "").replace(/\s+/g, "");
41
+ const binary = atob(body);
42
+ const der = new Uint8Array(binary.length);
43
+ for (let index = 0; index < binary.length; index++) der[index] = binary.charCodeAt(index);
44
+ return {
45
+ der,
46
+ pkcs1
47
+ };
48
+ }
49
+ function derLength(length) {
50
+ if (length < 128) return [length];
51
+ const bytes = [];
52
+ let remaining = length;
53
+ while (remaining > 0) {
54
+ bytes.unshift(remaining & 255);
55
+ remaining >>= 8;
56
+ }
57
+ return [128 | bytes.length, ...bytes];
58
+ }
59
+ /**
60
+ * Wraps a PKCS#1 RSA key in a PKCS#8 `PrivateKeyInfo`. GitHub App keys are
61
+ * distributed as PKCS#1, which Web Crypto cannot import directly.
62
+ */
63
+ function pkcs1ToPkcs8(der) {
64
+ const algorithm = [
65
+ 48,
66
+ 13,
67
+ 6,
68
+ 9,
69
+ 42,
70
+ 134,
71
+ 72,
72
+ 134,
73
+ 247,
74
+ 13,
75
+ 1,
76
+ 1,
77
+ 1,
78
+ 5,
79
+ 0
80
+ ];
81
+ const octetString = [
82
+ 4,
83
+ ...derLength(der.length),
84
+ ...der
85
+ ];
86
+ const contents = [
87
+ 2,
88
+ 1,
89
+ 0,
90
+ ...algorithm,
91
+ ...octetString
92
+ ];
93
+ return new Uint8Array([
94
+ 48,
95
+ ...derLength(contents.length),
96
+ ...contents
97
+ ]);
98
+ }
99
+ /** Signs an RS256 JWT with a PEM-encoded RSA private key. */
100
+ async function signRs256Jwt(privateKeyPem, claims) {
101
+ const { der, pkcs1 } = decodePem(privateKeyPem);
102
+ const key = await crypto.subtle.importKey("pkcs8", pkcs1 ? pkcs1ToPkcs8(der) : der, {
103
+ name: "RSASSA-PKCS1-v1_5",
104
+ hash: "SHA-256"
105
+ }, false, ["sign"]);
106
+ const payload = `${toBase64Url(JSON.stringify({
107
+ alg: "RS256",
108
+ typ: "JWT"
109
+ }))}.${toBase64Url(JSON.stringify(claims))}`;
110
+ const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", key, encoder.encode(payload));
111
+ return `${payload}.${toBase64Url(new Uint8Array(signature))}`;
112
+ }
113
+ /** The value of a header, whatever the casing of `name`, from a `Headers` object or a plain record. */
114
+ function headerValue(headers, name) {
115
+ if (headers instanceof Headers) return headers.get(name) ?? void 0;
116
+ const lower = name.toLowerCase();
117
+ for (const [key, value] of Object.entries(headers)) if (key.toLowerCase() === lower) return value;
118
+ }
119
+ /** A request body as text. */
120
+ function bodyText(body) {
121
+ return typeof body === "string" ? body : new TextDecoder().decode(body);
122
+ }
123
+ export { timingSafeEqual as a, signRs256Jwt as i, headerValue as n, toBase64 as o, hmacSha256Hex as r, bodyText as t };