@comforge/github 0.2.0 → 0.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/README.md +55 -4
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +4 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/operations.d.ts +147 -0
- package/dist/operations.js +328 -0
- package/dist/request.d.ts +49 -0
- package/dist/request.js +88 -0
- package/dist/writer.d.ts +183 -0
- package/dist/writer.js +877 -0
- package/llms.txt +5 -3
- package/package.json +2 -2
package/dist/request.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { GitHubAdapterError } from "./errors.js";
|
|
2
|
+
import { API_VERSION, classifyStatus } from "./transport.js";
|
|
3
|
+
/**
|
|
4
|
+
* The package's only non-GET request, used by the writer alone and not exported from the
|
|
5
|
+
* package. Trust boundary:
|
|
6
|
+
* - the method comes from a fixed table keyed by operation kind, never from a caller;
|
|
7
|
+
* - the path is built by the writer from fixed templates under the bound repository and checked
|
|
8
|
+
* here again; the origin is fixed and redirects fail;
|
|
9
|
+
* - the token is attached here and never appears in a result, error or ledger record;
|
|
10
|
+
* - nothing retries. A request that may have reached GitHub and has no definite answer is
|
|
11
|
+
* `sent-unknown`, never a refusal: the caller must not treat it as "nothing happened".
|
|
12
|
+
*/
|
|
13
|
+
export const WRITE_METHODS = {
|
|
14
|
+
"issue.create": "POST",
|
|
15
|
+
"issue.comment": "POST",
|
|
16
|
+
"pull.review": "POST",
|
|
17
|
+
"pull.create": "POST",
|
|
18
|
+
"issue.update": "PATCH",
|
|
19
|
+
"comment.update": "PATCH",
|
|
20
|
+
};
|
|
21
|
+
const PATH = /^\/repos\/[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+\/(issues|pulls)(\/[0-9]+)?(\/(comments|reviews))?$|^\/repos\/[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+\/issues\/comments\/[0-9]+$/;
|
|
22
|
+
export async function sendWrite(options, kind, path, payload) {
|
|
23
|
+
const method = WRITE_METHODS[kind];
|
|
24
|
+
if (!PATH.test(path))
|
|
25
|
+
throw new GitHubAdapterError("invalid-input", "The write path is not a supported API path");
|
|
26
|
+
const url = new URL(`${options.base}${path}`);
|
|
27
|
+
if (url.origin !== options.origin)
|
|
28
|
+
throw new GitHubAdapterError("unapproved-origin", "Unapproved outbound host");
|
|
29
|
+
// Resolved before sending: a failing token provider is a refusal, never an uncertain write.
|
|
30
|
+
let token;
|
|
31
|
+
try {
|
|
32
|
+
token = await options.token();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new GitHubAdapterError("unauthorized", "The token provider failed; nothing was sent");
|
|
36
|
+
}
|
|
37
|
+
if (typeof token !== "string" || token.length === 0)
|
|
38
|
+
throw new GitHubAdapterError("unauthorized", "The token provider returned no token; nothing was sent");
|
|
39
|
+
let res;
|
|
40
|
+
try {
|
|
41
|
+
res = await options.fetchImpl(url, {
|
|
42
|
+
method,
|
|
43
|
+
redirect: "error",
|
|
44
|
+
signal: AbortSignal.timeout(options.timeoutMs),
|
|
45
|
+
headers: {
|
|
46
|
+
Accept: "application/vnd.github+json",
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
"X-GitHub-Api-Version": API_VERSION,
|
|
49
|
+
Authorization: `Bearer ${token}`,
|
|
50
|
+
...(options.userAgent ? { "User-Agent": options.userAgent } : {}),
|
|
51
|
+
},
|
|
52
|
+
body: JSON.stringify(payload),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return { kind: "sent-unknown", reason: "no answer (network or timeout)", accepted: false };
|
|
57
|
+
}
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
// A gateway error or timeout from GitHub's edge says nothing about whether the write applied.
|
|
60
|
+
if (res.status >= 500 || res.status === 408)
|
|
61
|
+
return {
|
|
62
|
+
kind: "sent-unknown",
|
|
63
|
+
reason: `HTTP ${res.status}`,
|
|
64
|
+
status: res.status,
|
|
65
|
+
accepted: false,
|
|
66
|
+
};
|
|
67
|
+
return { kind: "refused", error: classifyStatus(res.status, res.headers) };
|
|
68
|
+
}
|
|
69
|
+
const status = res.status;
|
|
70
|
+
const declared = Number(res.headers.get("content-length") ?? "0");
|
|
71
|
+
if (Number.isFinite(declared) && declared > options.maxBodyBytes)
|
|
72
|
+
return { kind: "sent-unknown", reason: "response too large", status, accepted: true };
|
|
73
|
+
let text;
|
|
74
|
+
try {
|
|
75
|
+
text = await res.text();
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return { kind: "sent-unknown", reason: "response body unreadable", status, accepted: true };
|
|
79
|
+
}
|
|
80
|
+
if (text.length > options.maxBodyBytes)
|
|
81
|
+
return { kind: "sent-unknown", reason: "response too large", status, accepted: true };
|
|
82
|
+
try {
|
|
83
|
+
return { kind: "answered", status, body: JSON.parse(text) };
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return { kind: "sent-unknown", reason: "response not JSON", status, accepted: true };
|
|
87
|
+
}
|
|
88
|
+
}
|
package/dist/writer.d.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { type GitHubErrorCode } from "./errors.ts";
|
|
2
|
+
import { type OperationDescriptor, type WriteCapability, type WriteKind, type WriteOperation } from "./operations.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Caller-authorized GitHub writes. Opt-in and separate from every read
|
|
5
|
+
* API: `createFetchTransport` and `createGitHubEvidence` stay GET-only, and nothing they return
|
|
6
|
+
* can reach this module.
|
|
7
|
+
*
|
|
8
|
+
* Authority stays with the consumer. The writer validates and binds; it decides nothing:
|
|
9
|
+
* - `authorize` is the consumer's policy, called with the frozen descriptor of exactly what will
|
|
10
|
+
* be sent. Its `allow` is a policy decision, never evidence that a human approved anything;
|
|
11
|
+
* - the token is the consumer's, and whatever it permits on GitHub, the writer performs only the
|
|
12
|
+
* capabilities it was constructed with;
|
|
13
|
+
* - the operation ledger is the consumer's durable storage. The writer's guarantees across
|
|
14
|
+
* processes are exactly those of the ledger's atomic `create` and `replace`.
|
|
15
|
+
*
|
|
16
|
+
* GitHub has no idempotency keys and no compare-and-swap for these endpoints, so nothing here is
|
|
17
|
+
* exactly-once. A write that may have reached GitHub without a definite answer is reported
|
|
18
|
+
* `indeterminate`, recorded, and never resent automatically; `reconcile` looks for it.
|
|
19
|
+
*/
|
|
20
|
+
export type OperationState = "reserved" | "sending" | "succeeded" | "failed" | "indeterminate" | "abandoned";
|
|
21
|
+
export type WrittenResource = {
|
|
22
|
+
type: "issue";
|
|
23
|
+
number: number;
|
|
24
|
+
url: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "comment";
|
|
27
|
+
id: number;
|
|
28
|
+
issueNumber: number;
|
|
29
|
+
url: string;
|
|
30
|
+
} | {
|
|
31
|
+
type: "review";
|
|
32
|
+
id: number;
|
|
33
|
+
pullNumber: number;
|
|
34
|
+
commitSha: string | null;
|
|
35
|
+
state: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "pull";
|
|
38
|
+
number: number;
|
|
39
|
+
url: string;
|
|
40
|
+
headSha: string;
|
|
41
|
+
};
|
|
42
|
+
export interface ReconciliationReport {
|
|
43
|
+
/** Every page that could hold the write was read. */
|
|
44
|
+
complete: boolean;
|
|
45
|
+
/** Bodies with this operation's marker, by the expected author when one is configured. */
|
|
46
|
+
matches: number;
|
|
47
|
+
/** Bodies with this operation's marker by another author: copies, never accepted as the write. */
|
|
48
|
+
foreignMarkers: number;
|
|
49
|
+
/** Extra matches beyond the first: duplicates GitHub holds for this one operation. */
|
|
50
|
+
duplicates: number[];
|
|
51
|
+
}
|
|
52
|
+
export interface WriteOutcome {
|
|
53
|
+
/**
|
|
54
|
+
* - `verified`: GitHub answered, and a fresh read shows exactly what was authorized.
|
|
55
|
+
* - `accepted`: the resource exists but the read-back differs or was unavailable (`mismatches`).
|
|
56
|
+
* - `indeterminate`: the write may or may not have happened. Never resend blindly: `reconcile`.
|
|
57
|
+
*/
|
|
58
|
+
status: "verified" | "accepted" | "indeterminate";
|
|
59
|
+
operationId: string;
|
|
60
|
+
kind: WriteKind;
|
|
61
|
+
repository: string;
|
|
62
|
+
descriptorDigest: string;
|
|
63
|
+
/** Answered from the ledger; nothing was sent by this call. */
|
|
64
|
+
replayed: boolean;
|
|
65
|
+
/** The resource was identified by reconciliation, not by GitHub's response to the write. */
|
|
66
|
+
recovered: boolean;
|
|
67
|
+
resource: WrittenResource | null;
|
|
68
|
+
verification: "matched" | "mismatch" | "unavailable" | "not-attempted";
|
|
69
|
+
mismatches: string[];
|
|
70
|
+
/** For a review: the pull request head moved between the precondition and the read-back. */
|
|
71
|
+
headMovedDuringPublication?: boolean;
|
|
72
|
+
reconciliation?: ReconciliationReport;
|
|
73
|
+
reason?: string;
|
|
74
|
+
/** False when the ledger could not record this outcome; the write's result above still stands. */
|
|
75
|
+
recorded: boolean;
|
|
76
|
+
}
|
|
77
|
+
export interface OperationRecord {
|
|
78
|
+
schema: "comforge-github-operation/v1";
|
|
79
|
+
operationId: string;
|
|
80
|
+
repository: string;
|
|
81
|
+
kind: WriteKind;
|
|
82
|
+
capability: WriteCapability;
|
|
83
|
+
/** The descriptor digest: a retry with other content under the same id is a `conflict`. */
|
|
84
|
+
fingerprint: string;
|
|
85
|
+
/** The authorized descriptor, kept so `reconcile` needs nothing else. Holds no secret. */
|
|
86
|
+
descriptor: OperationDescriptor;
|
|
87
|
+
/** Incremented by every replace; the ledger's compare-and-swap key. */
|
|
88
|
+
version: number;
|
|
89
|
+
state: OperationState;
|
|
90
|
+
/** While `reserved` or `sending`, another caller must wait until this clock time. */
|
|
91
|
+
leaseUntil: number | null;
|
|
92
|
+
attempts: number;
|
|
93
|
+
/** Clock time of the first request that may have reached GitHub. */
|
|
94
|
+
firstSentAt: number | null;
|
|
95
|
+
changedAt: number;
|
|
96
|
+
authorizationReference: string | null;
|
|
97
|
+
marker: string | null;
|
|
98
|
+
outcome: Omit<WriteOutcome, "replayed" | "recorded"> | null;
|
|
99
|
+
failure: {
|
|
100
|
+
code: GitHubErrorCode;
|
|
101
|
+
message: string;
|
|
102
|
+
status?: number;
|
|
103
|
+
retryable: boolean;
|
|
104
|
+
notBefore: number | null;
|
|
105
|
+
} | null;
|
|
106
|
+
note: string | null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Durable, consumer-owned storage. Preconditions the writer relies on (a process-local map meets
|
|
110
|
+
* them only within one process):
|
|
111
|
+
* - `create` inserts only when no record has that `operationId`, atomically, and says whether it did;
|
|
112
|
+
* - `replace` writes only when the stored `version` equals `expectedVersion`, atomically;
|
|
113
|
+
* - records are returned as stored (a copy), never shared mutable objects.
|
|
114
|
+
* A thrown error from any method is `ledger-failure`: before a send, nothing is sent.
|
|
115
|
+
*/
|
|
116
|
+
export interface OperationLedger {
|
|
117
|
+
get(operationId: string): Promise<OperationRecord | null>;
|
|
118
|
+
create(record: OperationRecord): Promise<boolean>;
|
|
119
|
+
replace(operationId: string, expectedVersion: number, next: OperationRecord): Promise<boolean>;
|
|
120
|
+
}
|
|
121
|
+
export type AuthorizationDecision = {
|
|
122
|
+
allow: true;
|
|
123
|
+
descriptorDigest: string;
|
|
124
|
+
expiresAt: number;
|
|
125
|
+
reference?: string;
|
|
126
|
+
} | {
|
|
127
|
+
allow: false;
|
|
128
|
+
reason?: string;
|
|
129
|
+
};
|
|
130
|
+
export interface WriterLimits {
|
|
131
|
+
/** Pages read when searching for an uncertain create (100 items each). */
|
|
132
|
+
reconcileMaxPages: number;
|
|
133
|
+
/** How long a `reserved`/`sending` record excludes other callers. */
|
|
134
|
+
leaseMs: number;
|
|
135
|
+
/** The longest authorization lifetime accepted; a longer `expiresAt` is refused. */
|
|
136
|
+
maxAuthorizationTtlMs: number;
|
|
137
|
+
/** How far before the first send a reconciliation search starts (clock skew allowance). */
|
|
138
|
+
searchSkewMs: number;
|
|
139
|
+
}
|
|
140
|
+
export declare const DEFAULT_WRITER_LIMITS: WriterLimits;
|
|
141
|
+
export interface GitHubWriterOptions {
|
|
142
|
+
repository: {
|
|
143
|
+
owner: string;
|
|
144
|
+
name: string;
|
|
145
|
+
};
|
|
146
|
+
/** A token, or a function returning one per request. Never stored, logged or returned. */
|
|
147
|
+
token: string | (() => string | Promise<string>);
|
|
148
|
+
/** The only writes this writer performs, whatever the token permits. */
|
|
149
|
+
capabilities: readonly WriteCapability[];
|
|
150
|
+
/** The consumer's policy decision for one exact descriptor. */
|
|
151
|
+
authorize: (descriptor: OperationDescriptor) => AuthorizationDecision | Promise<AuthorizationDecision>;
|
|
152
|
+
ledger: OperationLedger;
|
|
153
|
+
/** Milliseconds since the epoch. Injected: the package reads no ambient clock. */
|
|
154
|
+
clock: () => number;
|
|
155
|
+
/**
|
|
156
|
+
* Correlation markers appended to created bodies, so an uncertain create can be found.
|
|
157
|
+
* `false` disables them; an uncertain create then stays `indeterminate` until you resolve it.
|
|
158
|
+
*/
|
|
159
|
+
markers?: {
|
|
160
|
+
namespace?: string;
|
|
161
|
+
} | false;
|
|
162
|
+
/** The login the token writes as (an App writes as `<slug>[bot]`). Reconciliation trusts only its markers. */
|
|
163
|
+
expectedAuthor?: string;
|
|
164
|
+
fetchImpl?: typeof fetch;
|
|
165
|
+
apiOrigin?: string;
|
|
166
|
+
timeoutMs?: number;
|
|
167
|
+
maxBodyBytes?: number;
|
|
168
|
+
userAgent?: string;
|
|
169
|
+
limits?: Partial<WriterLimits>;
|
|
170
|
+
}
|
|
171
|
+
export interface GitHubWriter {
|
|
172
|
+
readonly repository: string;
|
|
173
|
+
readonly capabilities: readonly WriteCapability[];
|
|
174
|
+
/** The descriptor `execute` would authorize and send; sends and records nothing. */
|
|
175
|
+
prepare(operation: WriteOperation): Promise<OperationDescriptor>;
|
|
176
|
+
/** Authorize, record, check preconditions, send once, read back. Refusals throw; nothing was written. */
|
|
177
|
+
execute(operation: WriteOperation): Promise<WriteOutcome>;
|
|
178
|
+
/** Looks for an `indeterminate` operation's write on GitHub (reads only) and records what it finds. */
|
|
179
|
+
reconcile(operationId: string): Promise<WriteOutcome>;
|
|
180
|
+
/** Ends an unfinished operation by the caller's decision; it is never sent again under this id. */
|
|
181
|
+
abandon(operationId: string, reason: string): Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
export declare function createGitHubWriter(options: GitHubWriterOptions): GitHubWriter;
|