@rathnasgala2/adapter-github-pages 0.1.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.
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Secret redaction for the two credentials this adapter handles.
3
+ *
4
+ * DEC-097 section 7 is explicit and bidirectional: "The OIDC token never
5
+ * appears in a header, evidence, digest, output or log; the GitHub token
6
+ * never appears in the body. Redaction occurs before any structured
7
+ * error/log serialization, and retained evidence records only body profile,
8
+ * byte count and the non-secret artifact ID/build version."
9
+ *
10
+ * This module is the enforcement point rather than a convention. Every
11
+ * evidence object and every error message that leaves `deployment.js` or
12
+ * `recovery.js` passes through {@link assertFreeOfSecrets} or
13
+ * {@link scrubSecrets}, so a future edit that accidentally interpolates a
14
+ * credential fails a test instead of shipping.
15
+ *
16
+ * @module
17
+ */
18
+
19
+ /** The placeholder a redacted secret is replaced with. */
20
+ export const REDACTION_PLACEHOLDER = '[redacted]';
21
+
22
+ /**
23
+ * Replace every occurrence of every secret in one string.
24
+ *
25
+ * @param {string} text the text to scrub
26
+ * @param {readonly (string | undefined)[]} secrets the secret byte strings
27
+ * @returns {string} the scrubbed text
28
+ */
29
+ export function scrubSecrets(text, secrets) {
30
+ let scrubbed = text;
31
+ for (const secret of secrets) {
32
+ if (typeof secret !== 'string' || secret.length === 0) {
33
+ continue;
34
+ }
35
+ scrubbed = scrubbed.split(secret).join(REDACTION_PLACEHOLDER);
36
+ }
37
+ return scrubbed;
38
+ }
39
+
40
+ /**
41
+ * Collect every string that appears anywhere in a JSON-like value, including
42
+ * object keys, so a secret cannot hide in a nested evidence member.
43
+ *
44
+ * @param {unknown} value the value to walk
45
+ * @param {string[]} [into] the accumulator
46
+ * @returns {string[]} every string found
47
+ */
48
+ export function collectStrings(value, into = []) {
49
+ if (typeof value === 'string') {
50
+ into.push(value);
51
+ return into;
52
+ }
53
+ if (Array.isArray(value)) {
54
+ for (const entry of value) {
55
+ collectStrings(entry, into);
56
+ }
57
+ return into;
58
+ }
59
+ if (value !== null && typeof value === 'object') {
60
+ for (const [key, member] of Object.entries(value)) {
61
+ into.push(key);
62
+ collectStrings(member, into);
63
+ }
64
+ }
65
+ return into;
66
+ }
67
+
68
+ /**
69
+ * Refuse a value that carries any secret's bytes anywhere inside it.
70
+ *
71
+ * @param {unknown} value the value to check (evidence, headers, a body text)
72
+ * @param {readonly (string | undefined)[]} secrets the secret byte strings
73
+ * @param {string} label where the value is bound for, for the diagnostic
74
+ * @returns {void}
75
+ * @throws {Error} `PAGES_SECRET_LEAK_DETECTED` when a secret is present
76
+ */
77
+ export function assertFreeOfSecrets(value, secrets, label) {
78
+ const strings = collectStrings(value);
79
+ for (const secret of secrets) {
80
+ if (typeof secret !== 'string' || secret.length === 0) {
81
+ continue;
82
+ }
83
+ if (strings.some((candidate) => candidate.includes(secret))) {
84
+ throw new Error(
85
+ `PAGES_SECRET_LEAK_DETECTED: a credential's bytes reached ${label}; DEC-097 section 7 forbids it`,
86
+ );
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Run one function with every thrown message scrubbed of every secret.
93
+ *
94
+ * @template T
95
+ * @param {readonly (string | undefined)[]} secrets the secret byte strings
96
+ * @param {() => Promise<T>} run the function to run
97
+ * @returns {Promise<T>} the function's result
98
+ */
99
+ export async function withRedactedFailures(secrets, run) {
100
+ try {
101
+ return await run();
102
+ } catch (error) {
103
+ const original = /** @type {Error} */ (error);
104
+ const scrubbed = scrubSecrets(String(original.message), secrets);
105
+ if (scrubbed === original.message) {
106
+ throw original;
107
+ }
108
+ const replacement = new Error(scrubbed);
109
+ replacement.stack = scrubSecrets(String(original.stack ?? ''), secrets);
110
+ throw replacement;
111
+ }
112
+ }
@@ -0,0 +1,417 @@
1
+ /**
2
+ * The frozen `gala-github-pages-http-v2` provider request-template catalog.
3
+ *
4
+ * DEC-097 section 7 fixes exactly nine `(stage, callClass)` rows for this
5
+ * adapter release's complete call plan. Every provider call this package is
6
+ * ever permitted to make is one of those rows, declared here as data and
7
+ * digested into the adapter's capability declaration
8
+ * (`requestTemplateCatalogDigest`). A call this catalog does not describe
9
+ * cannot be issued: `src/rest.js` resolves its method, origin, request
10
+ * target and every header from this catalog and never from a caller-supplied
11
+ * URL, and a `status_url` parsed out of a create response is retained as
12
+ * evidence but never followed.
13
+ *
14
+ * The only template variables are the closed percent-encoding-free ASCII
15
+ * `owner`/`repository` and `pagesDeploymentId`. The call class — not a
16
+ * provider response and not a workflow value — statically selects which of
17
+ * the two admitted sources that ID comes from: the intent's already reserved
18
+ * 40-lowercase-hex `pagesBuildVersion` for the seven ordinary/current rows,
19
+ * or `pagesRecovery.priorPagesBuildVersion` for the two
20
+ * `pages-recovery-prior-*` rows, which are forbidden outside
21
+ * `pages-reconciliation-recovery` mode.
22
+ *
23
+ * @module
24
+ */
25
+
26
+ import { canonicalizeJson } from '@rathnasgala2/adapter-protocol';
27
+
28
+ /** The exact request-template profile this adapter declares. */
29
+ export const REQUEST_TEMPLATE_PROFILE = 'gala-github-pages-http-v2';
30
+
31
+ /**
32
+ * The single origin every cataloged row targets. DEC-097 section 7: "Every
33
+ * row's origin is exactly `https://api.github.com`". The injectable
34
+ * `apiOrigin` exists only so a fixture can bind the identical code path to a
35
+ * local fake provider; the declared default is these exact bytes.
36
+ */
37
+ export const GITHUB_API_ORIGIN = 'https://api.github.com';
38
+
39
+ /**
40
+ * The GitHub REST API version header every call pins. DEC-097 section 7
41
+ * fixes it as the exact bytes below; it is not a free choice.
42
+ */
43
+ export const GITHUB_API_VERSION = '2026-03-10';
44
+
45
+ /**
46
+ * The `pagesDeploymentId` source a row's call class statically selects.
47
+ * `none` means the row's request target has no `pagesDeploymentId` variable.
48
+ */
49
+ export const DEPLOYMENT_ID_SOURCES = Object.freeze([
50
+ 'none',
51
+ 'intent-pages-build-version',
52
+ 'recovery-prior-pages-build-version',
53
+ ]);
54
+
55
+ const ACCEPT_HEADER = Object.freeze({
56
+ name: 'accept',
57
+ value: 'application/vnd.github+json',
58
+ });
59
+ const ACCEPT_ENCODING_HEADER = Object.freeze({
60
+ name: 'accept-encoding',
61
+ value: 'identity',
62
+ });
63
+ const CONNECTION_HEADER = Object.freeze({ name: 'connection', value: 'close' });
64
+ const CONTENT_TYPE_HEADER = Object.freeze({
65
+ name: 'content-type',
66
+ value: 'application/json',
67
+ });
68
+ const API_VERSION_HEADER = Object.freeze({
69
+ name: 'x-github-api-version',
70
+ value: GITHUB_API_VERSION,
71
+ });
72
+ const HOST_HEADER = Object.freeze({ name: 'host', source: 'origin-authority' });
73
+ const CONTENT_LENGTH_HEADER = Object.freeze({
74
+ name: 'content-length',
75
+ source: 'request-body-byte-count',
76
+ });
77
+ const INSTALLATION_TOKEN_HEADER = Object.freeze({
78
+ name: 'authorization',
79
+ source: 'github-token',
80
+ prefix: 'Bearer ',
81
+ maximumSourceBytes: 4096,
82
+ maximumRenderedValueBytes: 4103,
83
+ });
84
+
85
+ /** The fixed header rows every bodyless cataloged call sends, name-sorted. */
86
+ const BODYLESS_FIXED_HEADERS = Object.freeze([
87
+ ACCEPT_HEADER,
88
+ ACCEPT_ENCODING_HEADER,
89
+ CONNECTION_HEADER,
90
+ API_VERSION_HEADER,
91
+ ]);
92
+
93
+ /** The fixed header rows a cataloged call with a body sends, name-sorted. */
94
+ const BODY_FIXED_HEADERS = Object.freeze([
95
+ ACCEPT_HEADER,
96
+ ACCEPT_ENCODING_HEADER,
97
+ CONNECTION_HEADER,
98
+ CONTENT_TYPE_HEADER,
99
+ API_VERSION_HEADER,
100
+ ]);
101
+
102
+ const SITE_TARGET = '/repos/{owner}/{repository}/pages';
103
+ const DEPLOYMENTS_TARGET = '/repos/{owner}/{repository}/pages/deployments';
104
+ const STATUS_TARGET =
105
+ '/repos/{owner}/{repository}/pages/deployments/{pagesDeploymentId}';
106
+ const CANCEL_TARGET = `${STATUS_TARGET}/cancel`;
107
+
108
+ /**
109
+ * The request-body profile of the only two rows that carry an entity.
110
+ * DEC-097 section 7: "Only `pages-create-deployment` carries
111
+ * `requestBodyProfile: gala-pages-create-deployment-jcs-v2`".
112
+ */
113
+ export const CREATE_BODY_PROFILE = 'gala-pages-create-deployment-jcs-v2';
114
+
115
+ /**
116
+ * Build one catalog row.
117
+ *
118
+ * @param {{
119
+ * stage: string,
120
+ * callClass: string,
121
+ * method: string,
122
+ * origin: string,
123
+ * requestTargetTemplate: string,
124
+ * maximumRequestTargetBytes: number,
125
+ * hasBody: boolean,
126
+ * responseProfile: string
127
+ * }} row the row inputs
128
+ * @returns {Readonly<Record<string, unknown>>} the frozen row
129
+ */
130
+ function template(row) {
131
+ return Object.freeze({
132
+ stage: row.stage,
133
+ callClass: row.callClass,
134
+ method: row.method,
135
+ origin: row.origin,
136
+ requestTargetTemplate: row.requestTargetTemplate,
137
+ canonicalQueryProfile: 'none',
138
+ maximumRequestTargetBytes: row.maximumRequestTargetBytes,
139
+ fixedHeaders: row.hasBody ? BODY_FIXED_HEADERS : BODYLESS_FIXED_HEADERS,
140
+ derivedHeaders: row.hasBody
141
+ ? Object.freeze([CONTENT_LENGTH_HEADER, HOST_HEADER])
142
+ : Object.freeze([HOST_HEADER]),
143
+ credentialHeaders: Object.freeze([INSTALLATION_TOKEN_HEADER]),
144
+ requestBodyProfile: row.hasBody ? CREATE_BODY_PROFILE : 'empty',
145
+ responseProfile: row.responseProfile,
146
+ });
147
+ }
148
+
149
+ /**
150
+ * Compare two rows by their member JCS bytes. DEC-097 section 7:
151
+ * `requestTemplates` is "sorted by member JCS bytes", and the
152
+ * `callClassBinding` rows follow the same order.
153
+ *
154
+ * @param {Readonly<Record<string, unknown>>} left the first row
155
+ * @param {Readonly<Record<string, unknown>>} right the second row
156
+ * @returns {number} the byte-order comparison
157
+ */
158
+ export function compareTemplatesByJcsBytes(left, right) {
159
+ return Buffer.compare(
160
+ Buffer.from(canonicalizeJson(left), 'utf8'),
161
+ Buffer.from(canonicalizeJson(right), 'utf8'),
162
+ );
163
+ }
164
+
165
+ /**
166
+ * Build one `callClassBinding` row.
167
+ *
168
+ * @param {string} stage the lifecycle stage
169
+ * @param {string} callClass the call class
170
+ * @param {string} pagesDeploymentIdSource the statically selected source
171
+ * @param {boolean} recoveryOnly whether the row is admitted only in
172
+ * `pages-reconciliation-recovery` mode
173
+ * @returns {Readonly<{stage: string, callClass: string, pagesDeploymentIdSource: string, recoveryOnly: boolean}>}
174
+ * the frozen row
175
+ */
176
+ function binding(stage, callClass, pagesDeploymentIdSource, recoveryOnly) {
177
+ return Object.freeze({
178
+ stage,
179
+ callClass,
180
+ pagesDeploymentIdSource,
181
+ recoveryOnly,
182
+ });
183
+ }
184
+
185
+ /**
186
+ * The declared `callClassBinding` rows (`adapter-capability` 2.8.0,
187
+ * LOCAL-52 (2)): for every `(stage, callClass)` pair of the nine-row
188
+ * catalog, the `pagesDeploymentId` source its call class statically selects
189
+ * and whether the row is admitted only in `pages-reconciliation-recovery`
190
+ * mode.
191
+ *
192
+ * These two facts are contract-normative but cannot be members of the
193
+ * template rows themselves: `providerRequestTemplate` is closed with
194
+ * `additionalProperties: false`. Until schema 2.8.0 they lived here as an
195
+ * undeclared side table; they are now declared in the capability document
196
+ * under `limits.callClassBinding`, bound by their own
197
+ * `callClassBindingDigest` under `GALA-PROVIDER-CALL-CLASS-BINDING-V2\0`,
198
+ * and enforced in `rest.js`. The template catalog and its
199
+ * `requestTemplateCatalogDigest` are byte-identical to before.
200
+ *
201
+ * Sorted by member JCS bytes, like the template rows.
202
+ *
203
+ * @type {readonly Readonly<{stage: string, callClass: string, pagesDeploymentIdSource: string, recoveryOnly: boolean}>[]}
204
+ */
205
+ export const CALL_CLASS_BINDING = Object.freeze(
206
+ [
207
+ binding('inspect', 'pages-site', 'none', false),
208
+ binding('activate', 'pages-create-deployment', 'none', false),
209
+ binding(
210
+ 'activate',
211
+ 'pages-deployment-status',
212
+ 'intent-pages-build-version',
213
+ false,
214
+ ),
215
+ binding(
216
+ 'observe',
217
+ 'pages-deployment-status',
218
+ 'intent-pages-build-version',
219
+ false,
220
+ ),
221
+ binding(
222
+ 'inspect',
223
+ 'pages-recovery-prior-status',
224
+ 'recovery-prior-pages-build-version',
225
+ true,
226
+ ),
227
+ binding(
228
+ 'cleanup-staged',
229
+ 'pages-cancel-deployment',
230
+ 'intent-pages-build-version',
231
+ false,
232
+ ),
233
+ binding(
234
+ 'cleanup-staged',
235
+ 'pages-recovery-prior-cancel',
236
+ 'recovery-prior-pages-build-version',
237
+ true,
238
+ ),
239
+ binding('rollback', 'pages-create-deployment', 'none', false),
240
+ binding(
241
+ 'rollback',
242
+ 'pages-deployment-status',
243
+ 'intent-pages-build-version',
244
+ false,
245
+ ),
246
+ ].sort(compareTemplatesByJcsBytes),
247
+ );
248
+
249
+ /**
250
+ * Resolve the declared binding row for one `(stage, callClass)` pair.
251
+ *
252
+ * @param {string} stage the lifecycle stage
253
+ * @param {string} callClass the call class
254
+ * @returns {Readonly<{stage: string, callClass: string, pagesDeploymentIdSource: string, recoveryOnly: boolean}>}
255
+ * the binding row
256
+ */
257
+ export function callClassIdBinding(stage, callClass) {
258
+ const row = CALL_CLASS_BINDING.find(
259
+ (entry) => entry.stage === stage && entry.callClass === callClass,
260
+ );
261
+ if (row === undefined) {
262
+ throw new Error(
263
+ `PAGES_CALL_NOT_IN_CATALOG: (${JSON.stringify(stage)}, ${JSON.stringify(callClass)}) has no declared callClassBinding row`,
264
+ );
265
+ }
266
+ return row;
267
+ }
268
+
269
+ /**
270
+ * Build the exact nine-template catalog bound to one API origin.
271
+ *
272
+ * @param {string} [apiOrigin] the canonical `https://` GitHub REST origin;
273
+ * defaults to the declared {@link GITHUB_API_ORIGIN}
274
+ * @returns {readonly Readonly<Record<string, unknown>>[]} the frozen catalog,
275
+ * sorted by member JCS bytes
276
+ */
277
+ export function buildRequestTemplates(apiOrigin = GITHUB_API_ORIGIN) {
278
+ const origin = apiOrigin;
279
+ const rows = [
280
+ template({
281
+ stage: 'inspect',
282
+ callClass: 'pages-site',
283
+ method: 'GET',
284
+ origin,
285
+ requestTargetTemplate: SITE_TARGET,
286
+ maximumRequestTargetBytes: 512,
287
+ hasBody: false,
288
+ responseProfile: 'pages-site-json-v2',
289
+ }),
290
+ template({
291
+ stage: 'activate',
292
+ callClass: 'pages-create-deployment',
293
+ method: 'POST',
294
+ origin,
295
+ requestTargetTemplate: DEPLOYMENTS_TARGET,
296
+ maximumRequestTargetBytes: 512,
297
+ hasBody: true,
298
+ responseProfile: 'pages-create-deployment-json-v2',
299
+ }),
300
+ template({
301
+ stage: 'activate',
302
+ callClass: 'pages-deployment-status',
303
+ method: 'GET',
304
+ origin,
305
+ requestTargetTemplate: STATUS_TARGET,
306
+ maximumRequestTargetBytes: 640,
307
+ hasBody: false,
308
+ responseProfile: 'pages-deployment-status-json-v2',
309
+ }),
310
+ template({
311
+ stage: 'observe',
312
+ callClass: 'pages-deployment-status',
313
+ method: 'GET',
314
+ origin,
315
+ requestTargetTemplate: STATUS_TARGET,
316
+ maximumRequestTargetBytes: 640,
317
+ hasBody: false,
318
+ responseProfile: 'pages-deployment-status-json-v2',
319
+ }),
320
+ template({
321
+ stage: 'inspect',
322
+ callClass: 'pages-recovery-prior-status',
323
+ method: 'GET',
324
+ origin,
325
+ requestTargetTemplate: STATUS_TARGET,
326
+ maximumRequestTargetBytes: 640,
327
+ hasBody: false,
328
+ responseProfile: 'pages-deployment-status-json-v2',
329
+ }),
330
+ template({
331
+ stage: 'cleanup-staged',
332
+ callClass: 'pages-cancel-deployment',
333
+ method: 'POST',
334
+ origin,
335
+ requestTargetTemplate: CANCEL_TARGET,
336
+ maximumRequestTargetBytes: 704,
337
+ hasBody: false,
338
+ responseProfile: 'pages-cancel-empty-v2',
339
+ }),
340
+ template({
341
+ stage: 'cleanup-staged',
342
+ callClass: 'pages-recovery-prior-cancel',
343
+ method: 'POST',
344
+ origin,
345
+ requestTargetTemplate: CANCEL_TARGET,
346
+ maximumRequestTargetBytes: 704,
347
+ hasBody: false,
348
+ responseProfile: 'pages-cancel-empty-v2',
349
+ }),
350
+ template({
351
+ stage: 'rollback',
352
+ callClass: 'pages-create-deployment',
353
+ method: 'POST',
354
+ origin,
355
+ requestTargetTemplate: DEPLOYMENTS_TARGET,
356
+ maximumRequestTargetBytes: 512,
357
+ hasBody: true,
358
+ responseProfile: 'pages-create-deployment-json-v2',
359
+ }),
360
+ template({
361
+ stage: 'rollback',
362
+ callClass: 'pages-deployment-status',
363
+ method: 'GET',
364
+ origin,
365
+ requestTargetTemplate: STATUS_TARGET,
366
+ maximumRequestTargetBytes: 640,
367
+ hasBody: false,
368
+ responseProfile: 'pages-deployment-status-json-v2',
369
+ }),
370
+ ];
371
+ rows.sort(compareTemplatesByJcsBytes);
372
+ return Object.freeze(rows);
373
+ }
374
+
375
+ /**
376
+ * The exact nine `(stage, callClass)` pairs DEC-097 section 7 admits.
377
+ *
378
+ * @type {readonly Readonly<{stage: string, callClass: string}>[]}
379
+ */
380
+ export const PAGES_CALL_PLAN = Object.freeze(
381
+ buildRequestTemplates().map((row) =>
382
+ Object.freeze({
383
+ stage: /** @type {string} */ (row.stage),
384
+ callClass: /** @type {string} */ (row.callClass),
385
+ }),
386
+ ),
387
+ );
388
+
389
+ /**
390
+ * Resolve one template by its `(stage, callClass)` pair. The same call class
391
+ * appears under several stages, so the pair — never the call class alone —
392
+ * is the catalog key.
393
+ *
394
+ * @param {readonly Readonly<Record<string, unknown>>[]} templates the catalog
395
+ * @param {string} stage the lifecycle stage issuing the call
396
+ * @param {string} callClass the call class to resolve
397
+ * @returns {Readonly<Record<string, unknown>>} the template
398
+ */
399
+ export function requireTemplate(templates, stage, callClass) {
400
+ const found = templates.find(
401
+ (entry) => entry.stage === stage && entry.callClass === callClass,
402
+ );
403
+ if (found === undefined) {
404
+ throw new Error(
405
+ `PAGES_CALL_NOT_IN_CATALOG: (${JSON.stringify(stage)}, ${JSON.stringify(callClass)}) is not one of the nine admitted github-pages REST calls`,
406
+ );
407
+ }
408
+ return found;
409
+ }
410
+
411
+ /** The exact closed response-profile catalog this adapter can parse. */
412
+ export const RESPONSE_PROFILES = Object.freeze([
413
+ 'pages-cancel-empty-v2',
414
+ 'pages-create-deployment-json-v2',
415
+ 'pages-deployment-status-json-v2',
416
+ 'pages-site-json-v2',
417
+ ]);