@homeflare/alchemy 0.27.0 → 0.27.2

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 (76) hide show
  1. package/README.md +3 -2
  2. package/dist/cloudflare/fake-r2-lock.d.ts +53 -0
  3. package/dist/cloudflare/fake-r2-lock.d.ts.map +1 -0
  4. package/dist/cloudflare/index.d.ts +4 -5
  5. package/dist/cloudflare/index.d.ts.map +1 -1
  6. package/dist/cloudflare/index.js +79 -103
  7. package/dist/cloudflare/index.js.map +6 -7
  8. package/dist/cloudflare/mesh-node-api.d.ts +3 -2
  9. package/dist/cloudflare/mesh-node-api.d.ts.map +1 -1
  10. package/dist/cloudflare/providers.d.ts +1 -1
  11. package/dist/cloudflare/providers.d.ts.map +1 -1
  12. package/dist/cloudflare/r2-bucket-lock.d.ts +28 -21
  13. package/dist/cloudflare/r2-bucket-lock.d.ts.map +1 -1
  14. package/dist/forgejo/branch-protection-form.d.ts +16 -5
  15. package/dist/forgejo/branch-protection-form.d.ts.map +1 -1
  16. package/dist/forgejo/branch-protection.d.ts +34 -3
  17. package/dist/forgejo/branch-protection.d.ts.map +1 -1
  18. package/dist/forgejo/fake-forgejo.d.ts +41 -0
  19. package/dist/forgejo/fake-forgejo.d.ts.map +1 -0
  20. package/dist/forgejo/index.js +220 -284
  21. package/dist/forgejo/index.js.map +14 -15
  22. package/dist/forgejo/org-actions-secrets.d.ts +42 -12
  23. package/dist/forgejo/org-actions-secrets.d.ts.map +1 -1
  24. package/dist/forgejo/org-label.d.ts +30 -3
  25. package/dist/forgejo/org-label.d.ts.map +1 -1
  26. package/dist/forgejo/org-team.d.ts +37 -4
  27. package/dist/forgejo/org-team.d.ts.map +1 -1
  28. package/dist/forgejo/repo-webhook-form.d.ts +9 -18
  29. package/dist/forgejo/repo-webhook-form.d.ts.map +1 -1
  30. package/dist/forgejo/repo-webhook.d.ts +35 -3
  31. package/dist/forgejo/repo-webhook.d.ts.map +1 -1
  32. package/dist/forgejo/repository.d.ts +47 -2
  33. package/dist/forgejo/repository.d.ts.map +1 -1
  34. package/dist/forgejo/resource.d.ts +37 -37
  35. package/dist/forgejo/resource.d.ts.map +1 -1
  36. package/dist/forgejo/team-member.d.ts +48 -4
  37. package/dist/forgejo/team-member.d.ts.map +1 -1
  38. package/dist/forgejo/values.d.ts +9 -5
  39. package/dist/forgejo/values.d.ts.map +1 -1
  40. package/dist/launchd/index.js.map +2 -2
  41. package/dist/launchd/port-claims.d.ts +2 -2
  42. package/dist/launchd/port-claims.d.ts.map +1 -1
  43. package/docs/credentials.md +6 -0
  44. package/docs/distilled-interim.md +151 -0
  45. package/docs/launchd-ports.md +14 -17
  46. package/docs/peers.md +15 -6
  47. package/docs/upstream-conformance.md +46 -22
  48. package/package.json +2 -2
  49. package/src/cloudflare/fake-r2-lock.ts +120 -0
  50. package/src/cloudflare/index.ts +4 -5
  51. package/src/cloudflare/mesh-node-api.ts +3 -2
  52. package/src/cloudflare/providers.ts +13 -16
  53. package/src/cloudflare/r2-bucket-lock.test.ts +139 -0
  54. package/src/cloudflare/r2-bucket-lock.ts +59 -51
  55. package/src/forgejo/branch-protection-form.ts +12 -5
  56. package/src/forgejo/branch-protection.ts +58 -26
  57. package/src/forgejo/fake-forgejo.ts +54 -0
  58. package/src/forgejo/org-actions-secrets.ts +44 -36
  59. package/src/forgejo/org-label.test.ts +100 -0
  60. package/src/forgejo/org-label.ts +46 -36
  61. package/src/forgejo/org-team.ts +40 -27
  62. package/src/forgejo/repo-webhook-form.ts +10 -27
  63. package/src/forgejo/repo-webhook.ts +66 -35
  64. package/src/forgejo/repository.test.ts +125 -0
  65. package/src/forgejo/repository.ts +72 -52
  66. package/src/forgejo/resource.ts +80 -79
  67. package/src/forgejo/team-member.test.ts +97 -0
  68. package/src/forgejo/team-member.ts +56 -38
  69. package/src/forgejo/values.ts +9 -17
  70. package/src/launchd/port-claims.ts +8 -9
  71. package/dist/cloudflare/client.d.ts +0 -45
  72. package/dist/cloudflare/client.d.ts.map +0 -1
  73. package/dist/forgejo/client.d.ts +0 -37
  74. package/dist/forgejo/client.d.ts.map +0 -1
  75. package/src/cloudflare/client.ts +0 -76
  76. package/src/forgejo/client.ts +0 -136
@@ -1,27 +1,19 @@
1
1
  /**
2
2
  * Wire coercions shared across Forgejo resources.
3
3
  *
4
- * ★ GITEA/ FORGEJO JSON USES snake_case ON THE WIRE and camelCase in props — each resource
4
+ * ★ GITEA/FORGEJO JSON USES snake_case ON THE WIRE and camelCase in props — each resource
5
5
  * translates at the form boundary; these helpers only normalise values for comparison.
6
+ *
7
+ * ⛔ `text`/`bool`/`int` FROM THE HAND-ROLLED CLIENT ERA ARE GONE. They coerced values out of an
8
+ * untyped `Record<string, unknown>` response. `@distilled.cloud/forgejo`'s operations decode
9
+ * into typed structs (`Repository`, `Label`, `BranchProtection`, …), so an optional field is
10
+ * already `string | undefined` / `boolean | undefined` — a plain `live.field ?? fallback` at
11
+ * the call site replaces every one of their call sites, and a malformed response now fails the
12
+ * operation's decode instead of silently coercing to a fallback.
6
13
  */
7
14
 
8
- export const text = (value: unknown, fallback = '') =>
9
- typeof value === 'string' ? value : fallback;
10
-
11
15
  /** Forgejo returns label colours without `#`; declarations may include it. */
12
- export const color = (value: unknown) => text(value).replace(/^#/, '').toLowerCase();
13
-
14
- export const bool = (value: unknown, fallback = false) =>
15
- value === undefined || value === null ? fallback : value === true || value === 1 || value === '1';
16
-
17
- export const int = (value: unknown, fallback = 0) => {
18
- if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
19
- if (typeof value === 'string' && value.trim() !== '') {
20
- const parsed = Number.parseInt(value, 10);
21
- if (Number.isFinite(parsed)) return parsed;
22
- }
23
- return fallback;
24
- };
16
+ export const color = (value: string) => value.replace(/^#/, '').toLowerCase();
25
17
 
26
18
  /** Wire arrays compared in sorted order — Gitea reshuffles nothing but callers should not depend on order. */
27
19
  export const stringArray = (value: unknown) =>
@@ -3,13 +3,12 @@
3
3
  * runtime-portable (no `Bun.*`, no `node:*`), so a future Linux consumer (systemd units, under
4
4
  * `/linux`) can import it unchanged.
5
5
  *
6
- * ★ MIRRORS house/nix's REGISTRY, NOT A NEW RULE. `lib-ports.nix` (house @ 92cff7f) collides on
7
- * `lib.attrValues ports` alone — `dupes = lib.subtractLists (lib.unique vals) vals` — keying on
8
- * the PORT NUMBER by itself; address and protocol are never read. `portClaimProblems` below keys
9
- * the same way on purpose: it replaces that check for the jobs a kit stack declares, and a looser
10
- * or stricter rule would silently diverge from the registry it takes over from. What that trades
11
- * away — a wildcard bind beside a specific one on the same port both succeed, silently splitting
12
- * traffic — is measured in docs/launchd-ports.md, not assumed.
6
+ * ★ KEYS ON THE PORT NUMBER ALONE, NOT A NEW RULE. `portClaimProblems` below matches the estate's
7
+ * existing port-collision strictness — address and protocol never read — deliberately: a looser
8
+ * or stricter rule here would be a second, inconsistent source of truth for jobs migrating off a
9
+ * host's own port registry (still enforcing that same strictness until a job leaves it). What
10
+ * this trades away — a wildcard bind beside a specific one on the same port both succeed,
11
+ * silently splitting traffic — is measured in docs/launchd-ports.md, not assumed.
13
12
  * ★ IN THE STACK PROGRAM, NOT A PROVIDER — same shape as `catalogBinary()` (../release/catalog.ts).
14
13
  * Two reasons, both structural to Alchemy 2.0.0-beta.79: Alchemy never diffs a first `create`
15
14
  * whose props still hold an unresolved `Output`, and every mini job's `programArguments` carries
@@ -38,8 +37,8 @@ const describeAddress = (address: string | undefined): string => address ?? 'uns
38
37
  const describeOwner = (owner: string): string => (owner === '' ? '(empty owner)' : owner);
39
38
 
40
39
  /**
41
- * Everything wrong with a set of claims; empty when none collide. ★ KEYS ON `port` ALONE, exactly
42
- * like `lib-ports.nix`'s `assertNoCollision` — see the file header for why.
40
+ * Everything wrong with a set of claims; empty when none collide. ★ KEYS ON `port` ALONE — see the
41
+ * file header for why.
43
42
  */
44
43
  export const portClaimProblems = (claims: readonly PortClaim[]): string[] => {
45
44
  const found: string[] = [];
@@ -1,45 +0,0 @@
1
- /**
2
- * The Cloudflare REST API as an Effect service, over `cloudflare-typescript`. Codex standard 5:
3
- * the vendor's SDK, never a hand-rolled client — and never a hand-rolled `fetch` either.
4
- *
5
- * ⛔ THE SDK WAS ALREADY IN THE STORE AND DECLARED BY NOBODY. Measured 2026-09-15:
6
- * `node_modules/.pnpm/cloudflare@4.5.0` exists as a transitive dependency of `alchemy`, and
7
- * `pnpm ls cloudflare --depth 0` at the root and in every workspace package returns nothing.
8
- * `<estate>/local/scripts/vendor-sdk-ratchet.ts` row R2 says exactly that — "not declared anywhere
9
- * in the workspace yet … the row adds the dependency once, then converts the TS callers" — so
10
- * this package is that row's first half, and the twenty ratcheted offences keep their rows.
11
- *
12
- * ⛔ AND IT IS PINNED EXACTLY, NOT `catalog:`. There is no catalog entry for `cloudflare`, and the
13
- * version that resolves today is the one alchemy pulls in. Pinning `4.5.0` means a future
14
- * alchemy bump moves alchemy's copy and not this package's contract — the alternative is a
15
- * provider whose request shape changes because an unrelated dependency was upgraded.
16
- *
17
- * ⚠️ `fetch` IS A SEAM, NOT A CONVENIENCE — the same argument `packages/litellm-client` makes. The
18
- * SDK hands its custom fetch the URL and init it built, so a test double conforming to
19
- * `Core.Fetch` exercises the real path assembly, the real body serialisation and the real header
20
- * set. A test that stubbed `client.r2.buckets.locks.update` would prove only that a stub was
21
- * called, which is precisely the class of check this estate keeps finding green over nothing.
22
- */
23
- import Cloudflare from 'cloudflare';
24
- import * as Context from 'effect/Context';
25
- import * as Layer from 'effect/Layer';
26
- /** What the SDK accepts for `fetch`, restated so tests can name it without importing SDK internals. */
27
- export type ClientFetch = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
28
- declare const CloudflareApi_base: Context.ServiceClass<CloudflareApi, "homeflare/CloudflareApi", Cloudflare>;
29
- export declare class CloudflareApi extends CloudflareApi_base {
30
- }
31
- /**
32
- * ⚠️ THE FAKE FETCH IS THE ONLY REASON THIS TAKES OPTIONS AT ALL. Everything else the SDK reads
33
- * from the environment the estate CLI already populates.
34
- */
35
- export declare const makeClient: (options: {
36
- apiToken: string;
37
- fetch?: ClientFetch;
38
- }) => Cloudflare;
39
- /**
40
- * ⛔ A LAYER'S ERROR CHANNEL MUST BE `never`, so a ConfigError becomes a defect carrying a sentence
41
- * rather than a type error pushed up into `Stack`. Same decision, same words, as `LiteLLMLive`.
42
- */
43
- export declare const CloudflareApiLive: Layer.Layer<CloudflareApi, never, never>;
44
- export {};
45
- //# sourceMappingURL=client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/cloudflare/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAE1C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAGtC,uGAAuG;AACvG,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;;AAEjG,qBAAa,aAAc,SAAQ,kBAElC;CAAG;AAEJ;;;GAGG;AACH,eAAO,MAAM,UAAU,YAAa;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,WAAW,CAAA;CAAE,KAAG,UAI5E,CAAC;AAiBL;;;GAGG;AACH,eAAO,MAAM,iBAAiB,0CAU7B,CAAC"}
@@ -1,37 +0,0 @@
1
- /**
2
- * One authenticated call to the Forgejo (Gitea) API at `/api/v1`.
3
- *
4
- * ★ PLAIN `fetch`, NO TLS ESCAPE HATCH — your Forgejo host serves a real certificate on :443,
5
- * and the local daemon on :3000 is loopback. There is no `rejectUnauthorized` switch to leave
6
- * on by accident elsewhere.
7
- *
8
- * ⚠️ GITEA DOES NOT WRAP ANSWERS IN `{"data": ...}` THE WAY PVE DOES. MEASURED 2026-09-13 on
9
- * the live daemon: `GET /api/v1/version` returns `{"version":"16.0.3"}`; `GET /api/v1/user`
10
- * returns the user object; `GET /api/v1/orgs/HomeFlare/repos` returns a JSON array. A caller
11
- * that unwraps a non-existent envelope sees `undefined` and calls a healthy object "absent".
12
- *
13
- * ⛔ THE TOKEN IS NEVER A PROP. Read `FORGEJO_TOKEN` from the environment at call time only.
14
- * Alchemy persists attributes unencrypted and this estate's state store is Postgres dumped
15
- * nightly — nothing stored may be a credential.
16
- */
17
- import * as Effect from 'effect/Effect';
18
- import * as HttpClient from 'effect/unstable/http/HttpClient';
19
- /**
20
- * The Forgejo instance, from `FORGEJO_URL`. Trailing slash stripped.
21
- *
22
- * ⛔ NO DEFAULT HOSTNAME. This used to fall back to the author's own instance, which is
23
- * fine inside one estate and wrong in a published package: a consumer who forgets the
24
- * variable would silently point at someone else's server rather than being told.
25
- * ⚠️ Read at CALL time, never at module scope — a value captured at import is pinned to
26
- * whatever the first isolate saw.
27
- */
28
- export declare const apiBase: () => string;
29
- export declare class ForgejoError extends Error {
30
- readonly status: number;
31
- readonly method: string;
32
- readonly path: string;
33
- constructor(status: number, method: string, path: string, detail: string);
34
- }
35
- /** One JSON call. Returns the parsed body, or `undefined` when the response is empty (DELETE). */
36
- export declare const forgejo: <T>(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', path: string, body?: Record<string, unknown>) => Effect.Effect<T, ForgejoError, HttpClient.HttpClient>;
37
- //# sourceMappingURL=client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/forgejo/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,UAAU,MAAM,iCAAiC,CAAC;AAK9D;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO,QAAO,MAQ1B,CAAC;AAkBF,qBAAa,YAAa,SAAQ,KAAK;IAEnC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IAHvB,YACW,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,EAIf;CACF;AAQD,kGAAkG;AAClG,eAAO,MAAM,OAAO,GAAI,CAAC,UACf,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,QAC7C,MAAM,SACL,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,0DAwD5B,CAAC"}
@@ -1,76 +0,0 @@
1
- /**
2
- * The Cloudflare REST API as an Effect service, over `cloudflare-typescript`. Codex standard 5:
3
- * the vendor's SDK, never a hand-rolled client — and never a hand-rolled `fetch` either.
4
- *
5
- * ⛔ THE SDK WAS ALREADY IN THE STORE AND DECLARED BY NOBODY. Measured 2026-09-15:
6
- * `node_modules/.pnpm/cloudflare@4.5.0` exists as a transitive dependency of `alchemy`, and
7
- * `pnpm ls cloudflare --depth 0` at the root and in every workspace package returns nothing.
8
- * `<estate>/local/scripts/vendor-sdk-ratchet.ts` row R2 says exactly that — "not declared anywhere
9
- * in the workspace yet … the row adds the dependency once, then converts the TS callers" — so
10
- * this package is that row's first half, and the twenty ratcheted offences keep their rows.
11
- *
12
- * ⛔ AND IT IS PINNED EXACTLY, NOT `catalog:`. There is no catalog entry for `cloudflare`, and the
13
- * version that resolves today is the one alchemy pulls in. Pinning `4.5.0` means a future
14
- * alchemy bump moves alchemy's copy and not this package's contract — the alternative is a
15
- * provider whose request shape changes because an unrelated dependency was upgraded.
16
- *
17
- * ⚠️ `fetch` IS A SEAM, NOT A CONVENIENCE — the same argument `packages/litellm-client` makes. The
18
- * SDK hands its custom fetch the URL and init it built, so a test double conforming to
19
- * `Core.Fetch` exercises the real path assembly, the real body serialisation and the real header
20
- * set. A test that stubbed `client.r2.buckets.locks.update` would prove only that a stub was
21
- * called, which is precisely the class of check this estate keeps finding green over nothing.
22
- */
23
- import Cloudflare from 'cloudflare';
24
- import * as Config from 'effect/Config';
25
- import * as Context from 'effect/Context';
26
- import * as Effect from 'effect/Effect';
27
- import * as Layer from 'effect/Layer';
28
- import * as Redacted from 'effect/Redacted';
29
-
30
- /** What the SDK accepts for `fetch`, restated so tests can name it without importing SDK internals. */
31
- export type ClientFetch = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
32
-
33
- export class CloudflareApi extends Context.Service<CloudflareApi, Cloudflare>()(
34
- 'homeflare/CloudflareApi',
35
- ) {}
36
-
37
- /**
38
- * ⚠️ THE FAKE FETCH IS THE ONLY REASON THIS TAKES OPTIONS AT ALL. Everything else the SDK reads
39
- * from the environment the estate CLI already populates.
40
- */
41
- export const makeClient = (options: { apiToken: string; fetch?: ClientFetch }): Cloudflare =>
42
- new Cloudflare({
43
- apiToken: options.apiToken,
44
- ...(options.fetch === undefined ? {} : { fetch: options.fetch }),
45
- });
46
-
47
- /**
48
- * ⛔ FAIL CLOSED ON THE VALUE, NEVER ON THE VARIABLE EXISTING. AGENTS.md "the four that cost the
49
- * most" §4: a denied OpenBao path renders the template EMPTY rather than erroring, and every
50
- * Cloudflare call then answers 403 exactly like a revoked token — which sends the operator to
51
- * Cloudflare to rotate a credential that was fine. Your deploy wrapper should write
52
- * CLOUDFLARE_API_TOKEN from the stack's declared role, short-lived and scoped.
53
- */
54
- const MISSING_TOKEN =
55
- 'CLOUDFLARE_API_TOKEN is not set. Mint a short-lived, scoped token and export it before ' +
56
- 'role named in estate.stack.ts; running `alchemy` directly skips that and there is no default.';
57
-
58
- const EMPTY_TOKEN =
59
- 'CLOUDFLARE_API_TOKEN is set but EMPTY. That is a denied or missing OpenBao grant on the ' +
60
- 'stack’s role, not a rotated token — re-mint it rather than rotating anything at Cloudflare.';
61
-
62
- /**
63
- * ⛔ A LAYER'S ERROR CHANNEL MUST BE `never`, so a ConfigError becomes a defect carrying a sentence
64
- * rather than a type error pushed up into `Stack`. Same decision, same words, as `LiteLLMLive`.
65
- */
66
- export const CloudflareApiLive = Layer.effect(
67
- CloudflareApi,
68
- Effect.gen(function* () {
69
- const token = yield* Config.Redacted('CLOUDFLARE_API_TOKEN').pipe(
70
- Effect.catchTag('ConfigError', () => Effect.die(new Error(MISSING_TOKEN))),
71
- );
72
- const value = Redacted.value(token);
73
- if (value.length === 0) return yield* Effect.die(new Error(EMPTY_TOKEN));
74
- return makeClient({ apiToken: value });
75
- }),
76
- );
@@ -1,136 +0,0 @@
1
- /**
2
- * One authenticated call to the Forgejo (Gitea) API at `/api/v1`.
3
- *
4
- * ★ PLAIN `fetch`, NO TLS ESCAPE HATCH — your Forgejo host serves a real certificate on :443,
5
- * and the local daemon on :3000 is loopback. There is no `rejectUnauthorized` switch to leave
6
- * on by accident elsewhere.
7
- *
8
- * ⚠️ GITEA DOES NOT WRAP ANSWERS IN `{"data": ...}` THE WAY PVE DOES. MEASURED 2026-09-13 on
9
- * the live daemon: `GET /api/v1/version` returns `{"version":"16.0.3"}`; `GET /api/v1/user`
10
- * returns the user object; `GET /api/v1/orgs/HomeFlare/repos` returns a JSON array. A caller
11
- * that unwraps a non-existent envelope sees `undefined` and calls a healthy object "absent".
12
- *
13
- * ⛔ THE TOKEN IS NEVER A PROP. Read `FORGEJO_TOKEN` from the environment at call time only.
14
- * Alchemy persists attributes unencrypted and this estate's state store is Postgres dumped
15
- * nightly — nothing stored may be a credential.
16
- */
17
- import * as Effect from 'effect/Effect';
18
- import * as HttpClient from 'effect/unstable/http/HttpClient';
19
- import * as HttpClientRequest from 'effect/unstable/http/HttpClientRequest';
20
-
21
- const PATH_PREFIX = '/api/v1';
22
-
23
- /**
24
- * The Forgejo instance, from `FORGEJO_URL`. Trailing slash stripped.
25
- *
26
- * ⛔ NO DEFAULT HOSTNAME. This used to fall back to the author's own instance, which is
27
- * fine inside one estate and wrong in a published package: a consumer who forgets the
28
- * variable would silently point at someone else's server rather than being told.
29
- * ⚠️ Read at CALL time, never at module scope — a value captured at import is pinned to
30
- * whatever the first isolate saw.
31
- */
32
- export const apiBase = (): string => {
33
- const raw = process.env['FORGEJO_URL'];
34
- if (raw === undefined || raw.trim() === '') {
35
- throw new Error(
36
- 'FORGEJO_URL is not set. Point it at your Forgejo instance, e.g. https://git.example.com',
37
- );
38
- }
39
- return raw.trim().replace(/\/$/, '');
40
- };
41
-
42
- const token = (): Effect.Effect<string, ForgejoError> =>
43
- Effect.gen(function* () {
44
- const raw = process.env['FORGEJO_TOKEN'];
45
- if (raw === undefined || raw.trim() === '') {
46
- return yield* Effect.fail(
47
- new ForgejoError(
48
- 0,
49
- 'GET',
50
- '',
51
- 'FORGEJO_TOKEN is unset. Mint a scoped token and export it — never store it in Alchemy props.',
52
- ),
53
- );
54
- }
55
- return raw.trim();
56
- });
57
-
58
- export class ForgejoError extends Error {
59
- constructor(
60
- readonly status: number,
61
- readonly method: string,
62
- readonly path: string,
63
- detail: string,
64
- ) {
65
- super(`Forgejo ${method} ${path} -> ${String(status)}: ${detail}`);
66
- this.name = 'ForgejoError';
67
- }
68
- }
69
-
70
- /**
71
- * ⚠️ `Authorization: token <value>`, NOT `Bearer`. Gitea docs and MEASURED behaviour on this
72
- * estate: Bearer is treated as OAuth2 and rejected; `token` is the documented API-key form.
73
- */
74
- const authorization = (value: string) => ({ Authorization: `token ${value}` });
75
-
76
- /** One JSON call. Returns the parsed body, or `undefined` when the response is empty (DELETE). */
77
- export const forgejo = <T>(
78
- method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
79
- path: string,
80
- body?: Record<string, unknown>,
81
- ) =>
82
- Effect.gen(function* () {
83
- const credential = yield* token();
84
- const url = `${apiBase()}${PATH_PREFIX}/${path.replace(/^\//, '')}`;
85
- /**
86
- * ★ THROUGH `HttpClient`, NOT THE GLOBAL `fetch` — the idiom Alchemy's own providers follow
87
- * (src/Hetzner/Providers.ts builds on `FetchHttpClient.layer`; there is no bare fetch in its
88
- * Docker, Kubernetes or GitHub providers). It buys a typed error channel, interruption when
89
- * a plan is cancelled, the runtime's tracing, and a client a test can replace without
90
- * monkey-patching a global. <estate>/proxmox/src/resource.ts carries the same note.
91
- */
92
- const client = yield* HttpClient.HttpClient;
93
- /**
94
- * ⛔ THE CONTENT TYPE GOES ON `bodyText`, NOT IN THE HEADER MAP, AND PUTTING IT IN BOTH IS HOW
95
- * THIS PACKAGE SHIPPED UNABLE TO WRITE AT ALL.
96
- *
97
- * 🔴 THE BUG, MEASURED THE FIRST TIME A WRITE WAS EVER ATTEMPTED. The header map DID carry
98
- * `Content-Type: application/json` — and `bodyText(s)` with no second argument sets
99
- * `text/plain` and OVERWRITES it, because it is piped AFTER `setHeaders`. Forgejo answered
100
- * 422 {"message":"[]: Unsupported Content-Type"}
101
- * on `PATCH orgs/homeflare/labels/1`. Every create and every update in every family here was
102
- * broken, identically, from the day the client was written.
103
- *
104
- * ⛔ NOTHING CAUGHT IT BECAUSE NOTHING HAD EVER WRITTEN. The only credential on the shelf was
105
- * `mcp-read`, which answers 403 before a request body is ever parsed — so read, adopt, diff
106
- * and plan all worked perfectly and the write path had literally never executed. A provider
107
- * that has never performed its own writes is not a tested provider, whatever the plan says.
108
- *
109
- * ⚠️ `bodyJsonUnsafe` WOULD ALSO WORK. `bodyText` with an explicit type is used instead
110
- * because it makes the content type visible at the call site, which is the thing that was
111
- * invisible before.
112
- */
113
- const request = HttpClientRequest.make(method)(url).pipe(
114
- HttpClientRequest.setHeaders({ ...authorization(credential), Accept: 'application/json' }),
115
- body === undefined
116
- ? (self) => self
117
- : HttpClientRequest.bodyText(JSON.stringify(body), 'application/json'),
118
- );
119
- const response = yield* client
120
- .execute(request)
121
- .pipe(Effect.mapError((cause) => new ForgejoError(0, method, path, String(cause))));
122
- // ⛔ Status first, body once — a Gitea error body is JSON but a proxy's 502 is not.
123
- if (response.status < 200 || response.status >= 300) {
124
- const detail = yield* response.text.pipe(Effect.orElseSucceed(() => ''));
125
- return yield* Effect.fail(
126
- new ForgejoError(response.status, method, path, detail.slice(0, 300)),
127
- );
128
- }
129
- // ⚠️ 204 AND AN EMPTY BODY ARE REAL: a label DELETE answers no content, and `json` on an empty
130
- // body fails. Read text once and parse only when there is something to parse.
131
- const text = yield* response.text.pipe(
132
- Effect.mapError((cause) => new ForgejoError(0, method, path, String(cause))),
133
- );
134
- // ⚠️ PUT/DELETE on actions secrets and hooks often answer 201/204 with no body — same trap as DELETE.
135
- return (text === '' ? undefined : JSON.parse(text)) as T;
136
- });