blogwright 0.4.0 → 0.6.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 +4 -1
- package/agent/agent-manifest.json +1 -1
- package/agent/server.js +2610 -616
- package/dist/adapters/node-dns.d.ts +20 -0
- package/dist/adapters/node-dns.js +49 -0
- package/dist/adapters/process-aws-login.d.ts +19 -0
- package/dist/adapters/process-aws-login.js +28 -0
- package/dist/agent-package.js +2 -3
- package/dist/artifacts.d.ts +73 -0
- package/dist/artifacts.js +218 -0
- package/dist/aws-session.d.ts +118 -0
- package/dist/aws-session.js +360 -0
- package/dist/bin.js +6 -2
- package/dist/cache-control.d.ts +66 -0
- package/dist/cache-control.js +176 -0
- package/dist/cli.d.ts +22 -3
- package/dist/cli.js +128 -31
- package/dist/commands.d.ts +41 -9
- package/dist/commands.js +135 -126
- package/dist/context.d.ts +42 -5
- package/dist/context.js +81 -19
- package/dist/deploy.d.ts +78 -6
- package/dist/deploy.js +136 -52
- package/dist/domains.d.ts +46 -0
- package/dist/domains.js +125 -0
- package/dist/init.d.ts +14 -2
- package/dist/init.js +145 -4
- package/dist/known-commands.d.ts +11 -9
- package/dist/known-commands.js +15 -9
- package/dist/live-release.d.ts +78 -0
- package/dist/live-release.js +159 -0
- package/dist/lock.d.ts +50 -0
- package/dist/lock.js +120 -0
- package/dist/nodes.d.ts +18 -0
- package/dist/nodes.js +721 -76
- package/dist/plugin-commands.d.ts +21 -0
- package/dist/plugin-commands.js +54 -3
- package/dist/plugins.d.ts +11 -9
- package/dist/plugins.js +29 -16
- package/dist/ports.d.ts +37 -1
- package/dist/releases.d.ts +54 -0
- package/dist/releases.js +527 -0
- package/dist/render.d.ts +18 -1
- package/dist/render.js +38 -6
- package/dist/repo.js +2 -3
- package/dist/rkey.d.ts +4 -3
- package/dist/rkey.js +4 -3
- package/dist/router-function.d.ts +24 -0
- package/dist/router-function.js +71 -0
- package/dist/serving.d.ts +28 -0
- package/dist/serving.js +47 -0
- package/dist/verify.d.ts +45 -0
- package/dist/verify.js +183 -0
- package/dist/whoami.d.ts +23 -0
- package/dist/whoami.js +88 -0
- package/package.json +8 -8
package/dist/rkey.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The `blogwright/rkey` subpath export, kept stable for consuming sites: the
|
|
3
|
-
* implementation lives in blogwright-
|
|
4
|
-
* this re-export preserves the CLI's published contract
|
|
3
|
+
* implementation lives in blogwright-core (see packages/core/src/rkey.ts), and
|
|
4
|
+
* this re-export preserves the CLI's published contract without installing the
|
|
5
|
+
* pds plugin.
|
|
5
6
|
*/
|
|
6
|
-
export * from 'blogwright-
|
|
7
|
+
export * from 'blogwright-core/rkey';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Hosts whose requests redirect, and the host they redirect to. */
|
|
2
|
+
export interface AliasRedirect {
|
|
3
|
+
/** The canonical host (`OpsConfig.domain`); the redirect target. */
|
|
4
|
+
primary: string;
|
|
5
|
+
/** Hosts that answer with a `301` to `primary`. */
|
|
6
|
+
aliases: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Source of the production/staging CloudFront viewer-request function. With no
|
|
10
|
+
* `redirect` it is the directory-index rewrite alone, byte-for-byte what every
|
|
11
|
+
* existing site already runs. A distribution allows one function per event
|
|
12
|
+
* type, so the alias redirect is folded into this function rather than being a
|
|
13
|
+
* second one.
|
|
14
|
+
*/
|
|
15
|
+
export declare function staticRouterCode(redirect?: AliasRedirect): string;
|
|
16
|
+
/**
|
|
17
|
+
* Source of the viewer-request function an SSR distribution runs when
|
|
18
|
+
* `redirectToPrimary` is set. The application owns its own routing, so unlike
|
|
19
|
+
* the static router there is no directory-index rewrite: alias hosts get the
|
|
20
|
+
* `301` and every other request passes through untouched.
|
|
21
|
+
*/
|
|
22
|
+
export declare function ssrRouterCode(redirect: AliasRedirect): string;
|
|
23
|
+
/** Stable fingerprint of a function's source, recorded in state to detect a change. */
|
|
24
|
+
export declare function routerCodeHash(code: string): string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* The directory-index rewrite every static-mode request goes through. The
|
|
4
|
+
* S3 origin is the private REST endpoint (via OAC), which - unlike an S3
|
|
5
|
+
* website endpoint - does no index-document resolution, so `/projects/`
|
|
6
|
+
* becomes `/projects/index.html` here. The `/site` origin path is applied by
|
|
7
|
+
* the distribution, so this must not add it.
|
|
8
|
+
*/
|
|
9
|
+
const INDEX_REWRITE = ` var uri = request.uri;
|
|
10
|
+
if (uri.endsWith('/')) { uri += 'index.html'; }
|
|
11
|
+
else if (uri.lastIndexOf('.') < uri.lastIndexOf('/')) { uri += '/index.html'; }
|
|
12
|
+
request.uri = uri;
|
|
13
|
+
return request;`;
|
|
14
|
+
/**
|
|
15
|
+
* Host check that runs ahead of the rewrite when `redirectToPrimary` is set:
|
|
16
|
+
* a request for an alias answers `301` to the same path and query on the
|
|
17
|
+
* primary. It reads `request.uri` before the rewrite touches it, so the
|
|
18
|
+
* redirect target is the URL the viewer asked for, not the origin key.
|
|
19
|
+
*/
|
|
20
|
+
function redirectBlock(redirect) {
|
|
21
|
+
return ` var host = request.headers.host ? request.headers.host.value.toLowerCase() : '';
|
|
22
|
+
if (${JSON.stringify([...redirect.aliases])}.indexOf(host) !== -1) {
|
|
23
|
+
var parts = [];
|
|
24
|
+
var query = request.querystring;
|
|
25
|
+
for (var key in query) {
|
|
26
|
+
var entries = query[key].multiValue || [query[key]];
|
|
27
|
+
for (var i = 0; i < entries.length; i++) {
|
|
28
|
+
parts.push(entries[i].value === '' ? key : key + '=' + entries[i].value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
statusCode: 301,
|
|
33
|
+
statusDescription: 'Moved Permanently',
|
|
34
|
+
headers: {
|
|
35
|
+
location: { value: 'https://' + ${JSON.stringify(redirect.primary)} + request.uri + (parts.length ? '?' + parts.join('&') : '') }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Source of the production/staging CloudFront viewer-request function. With no
|
|
43
|
+
* `redirect` it is the directory-index rewrite alone, byte-for-byte what every
|
|
44
|
+
* existing site already runs. A distribution allows one function per event
|
|
45
|
+
* type, so the alias redirect is folded into this function rather than being a
|
|
46
|
+
* second one.
|
|
47
|
+
*/
|
|
48
|
+
export function staticRouterCode(redirect) {
|
|
49
|
+
const block = redirect && redirect.aliases.length > 0 ? redirectBlock(redirect) : '';
|
|
50
|
+
return `function handler(event) {
|
|
51
|
+
var request = event.request;
|
|
52
|
+
${block}${INDEX_REWRITE}
|
|
53
|
+
}`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Source of the viewer-request function an SSR distribution runs when
|
|
57
|
+
* `redirectToPrimary` is set. The application owns its own routing, so unlike
|
|
58
|
+
* the static router there is no directory-index rewrite: alias hosts get the
|
|
59
|
+
* `301` and every other request passes through untouched.
|
|
60
|
+
*/
|
|
61
|
+
export function ssrRouterCode(redirect) {
|
|
62
|
+
const block = redirect.aliases.length > 0 ? redirectBlock(redirect) : '';
|
|
63
|
+
return `function handler(event) {
|
|
64
|
+
var request = event.request;
|
|
65
|
+
${block} return request;
|
|
66
|
+
}`;
|
|
67
|
+
}
|
|
68
|
+
/** Stable fingerprint of a function's source, recorded in state to detect a change. */
|
|
69
|
+
export function routerCodeHash(code) {
|
|
70
|
+
return createHash('sha256').update(code).digest('hex');
|
|
71
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the environment serves and from where: the configured and recorded
|
|
3
|
+
* serving modes (static or SSR), the guard that refuses to publish in a mode
|
|
4
|
+
* the distribution is not serving, and the site's canonical origin. Shared by
|
|
5
|
+
* the build-and-publish commands and the release commands.
|
|
6
|
+
*/
|
|
7
|
+
import type { OpsContext } from './context.js';
|
|
8
|
+
/**
|
|
9
|
+
* Canonical origin the live site is served from: the custom domain if configured,
|
|
10
|
+
* else the distribution's CloudFront domain. Used for robots.txt / sitemap.xml URLs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function siteBaseUrl(ctx: OpsContext): string | undefined;
|
|
13
|
+
/** The serving mode the config asks for. */
|
|
14
|
+
export declare function configuredMode(ctx: OpsContext): 'static' | 'ssr';
|
|
15
|
+
/**
|
|
16
|
+
* The serving mode the live distribution was left in, or undefined before the
|
|
17
|
+
* first bootstrap. A distribution recorded without a mode was created before
|
|
18
|
+
* SSR existed, so it is static - reading that as "unknown" would let the very
|
|
19
|
+
* stacks the switch guard exists for through it.
|
|
20
|
+
*/
|
|
21
|
+
export declare function recordedMode(ctx: OpsContext): 'static' | 'ssr' | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Refuse to publish in a mode the distribution is not serving. The config and
|
|
24
|
+
* the stack disagree only between editing `ssr` and re-bootstrapping, and a
|
|
25
|
+
* deploy in that gap publishes where nothing serves from - or, static over
|
|
26
|
+
* SSR, deletes the `site/` assets the live revision still references.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assertServingModeMatches(ctx: OpsContext): void;
|
package/dist/serving.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the environment serves and from where: the configured and recorded
|
|
3
|
+
* serving modes (static or SSR), the guard that refuses to publish in a mode
|
|
4
|
+
* the distribution is not serving, and the site's canonical origin. Shared by
|
|
5
|
+
* the build-and-publish commands and the release commands.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Canonical origin the live site is served from: the custom domain if configured,
|
|
9
|
+
* else the distribution's CloudFront domain. Used for robots.txt / sitemap.xml URLs.
|
|
10
|
+
*/
|
|
11
|
+
export function siteBaseUrl(ctx) {
|
|
12
|
+
if (ctx.domain)
|
|
13
|
+
return `https://${ctx.domain}`;
|
|
14
|
+
const cf = ctx.state.resources['cloudfront-distribution']?.domainName;
|
|
15
|
+
return typeof cf === 'string' ? `https://${cf}` : undefined;
|
|
16
|
+
}
|
|
17
|
+
/** The serving mode the config asks for. */
|
|
18
|
+
export function configuredMode(ctx) {
|
|
19
|
+
return ctx.config.ssr ? 'ssr' : 'static';
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The serving mode the live distribution was left in, or undefined before the
|
|
23
|
+
* first bootstrap. A distribution recorded without a mode was created before
|
|
24
|
+
* SSR existed, so it is static - reading that as "unknown" would let the very
|
|
25
|
+
* stacks the switch guard exists for through it.
|
|
26
|
+
*/
|
|
27
|
+
export function recordedMode(ctx) {
|
|
28
|
+
const distribution = ctx.state.resources['cloudfront-distribution'];
|
|
29
|
+
if (typeof distribution?.id !== 'string')
|
|
30
|
+
return undefined;
|
|
31
|
+
return distribution.mode === 'ssr' ? 'ssr' : 'static';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Refuse to publish in a mode the distribution is not serving. The config and
|
|
35
|
+
* the stack disagree only between editing `ssr` and re-bootstrapping, and a
|
|
36
|
+
* deploy in that gap publishes where nothing serves from - or, static over
|
|
37
|
+
* SSR, deletes the `site/` assets the live revision still references.
|
|
38
|
+
*/
|
|
39
|
+
export function assertServingModeMatches(ctx) {
|
|
40
|
+
const recorded = recordedMode(ctx);
|
|
41
|
+
const configured = configuredMode(ctx);
|
|
42
|
+
if (recorded === undefined || recorded === configured)
|
|
43
|
+
return;
|
|
44
|
+
throw new Error(`"${ctx.env}" is configured for ${configured} serving but its distribution serves ` +
|
|
45
|
+
`${recorded}. Run \`blogwright bootstrap ${ctx.env} --switch-mode\` first; nothing was ` +
|
|
46
|
+
'built or published.');
|
|
47
|
+
}
|
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `blogwright verify`: compare the objects stored under a prefix - the live
|
|
3
|
+
* `site/`, or a release staged under `releases/<hash>/` - with the artifact
|
|
4
|
+
* manifest recorded for a hash, byte for byte - every object the manifest
|
|
5
|
+
* lists with its recorded size is read and hashed, so a pass means the bytes
|
|
6
|
+
* are exactly the recorded ones.
|
|
7
|
+
*/
|
|
8
|
+
import { type ArtifactComparison, type ArtifactManifest, type ObservedFile } from 'blogwright-core';
|
|
9
|
+
import type { OpsContext } from './context.js';
|
|
10
|
+
/**
|
|
11
|
+
* The most bytes one read holds in memory. Larger objects are hashed in
|
|
12
|
+
* ranged reads of this size, so verification's memory stays near
|
|
13
|
+
* `VERIFY_CONCURRENCY` x this, however large the site's objects are.
|
|
14
|
+
*/
|
|
15
|
+
export declare const VERIFY_READ_CHUNK_BYTES: number;
|
|
16
|
+
/** The prefix CloudFront serves the live site from. */
|
|
17
|
+
export declare const LIVE_SITE_PREFIX = "site/";
|
|
18
|
+
/**
|
|
19
|
+
* What is stored under `prefix`, keyed by site-root-relative path. Objects the
|
|
20
|
+
* manifest lists with the recorded size are read and hashed; any other object
|
|
21
|
+
* is described by its size alone, which already decides the comparison. Empty
|
|
22
|
+
* folder markers are logged and left out, and an object deleted since the
|
|
23
|
+
* listing is left out too, so the comparison reports it as missing.
|
|
24
|
+
*/
|
|
25
|
+
export declare function observeObjects(ctx: OpsContext, prefix: string, manifest: ArtifactManifest): Promise<Map<string, ObservedFile>>;
|
|
26
|
+
/**
|
|
27
|
+
* A stored copy that does not hold exactly the release it was checked
|
|
28
|
+
* against. The message names the location, the release and the counts; the
|
|
29
|
+
* comparison is kept so a caller can say how to recover.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ReleaseMismatchError extends Error {
|
|
32
|
+
readonly location: string;
|
|
33
|
+
readonly release: string;
|
|
34
|
+
readonly comparison: ArtifactComparison;
|
|
35
|
+
constructor(location: string, release: string, comparison: ArtifactComparison);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check that `prefix` holds exactly the release `manifest` records: logs
|
|
39
|
+
* every difference and throws a {@link ReleaseMismatchError} when there is any.
|
|
40
|
+
*/
|
|
41
|
+
export declare function verifyStoredRelease(ctx: OpsContext, manifest: ArtifactManifest, prefix: string): Promise<ArtifactManifest>;
|
|
42
|
+
/** Which copy of a release `verify` checks: what is served, or what `stage` stored. */
|
|
43
|
+
export type VerifyTarget = 'live' | 'staged';
|
|
44
|
+
/** `blogwright verify <hash>`: the live site (or the staged copy) against a hash's recorded release. */
|
|
45
|
+
export declare function verify(ctx: OpsContext, hash: string, target?: VerifyTarget): Promise<void>;
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `blogwright verify`: compare the objects stored under a prefix - the live
|
|
3
|
+
* `site/`, or a release staged under `releases/<hash>/` - with the artifact
|
|
4
|
+
* manifest recorded for a hash, byte for byte - every object the manifest
|
|
5
|
+
* lists with its recorded size is read and hashed, so a pass means the bytes
|
|
6
|
+
* are exactly the recorded ones.
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
import { AwsError, colors, compareArtifact, isExactMatch, mapConcurrently, stagedReleasePrefix, } from 'blogwright-core';
|
|
10
|
+
import { requireArtifactManifest, requireStagedReleaseManifest } from './artifacts.js';
|
|
11
|
+
/**
|
|
12
|
+
* How many objects verification reads at once. A tuning value for the
|
|
13
|
+
* client's own requests, not a limit on anything provisioned.
|
|
14
|
+
*/
|
|
15
|
+
const VERIFY_CONCURRENCY = 8;
|
|
16
|
+
/**
|
|
17
|
+
* The most bytes one read holds in memory. Larger objects are hashed in
|
|
18
|
+
* ranged reads of this size, so verification's memory stays near
|
|
19
|
+
* `VERIFY_CONCURRENCY` x this, however large the site's objects are.
|
|
20
|
+
*/
|
|
21
|
+
export const VERIFY_READ_CHUNK_BYTES = 8 * 1024 * 1024;
|
|
22
|
+
/** The prefix CloudFront serves the live site from. */
|
|
23
|
+
export const LIVE_SITE_PREFIX = 'site/';
|
|
24
|
+
/** The environment name `blogwright preview` commands run in (`cli.ts`'s `runPreview`). */
|
|
25
|
+
const PREVIEW_ENV = 'preview';
|
|
26
|
+
/**
|
|
27
|
+
* An S3 console "folder": an empty object whose key ends in `/`. CloudFront
|
|
28
|
+
* never requests one - the router serves a URL ending in `/` its
|
|
29
|
+
* `index.html` - so it is not part of what the site serves.
|
|
30
|
+
*/
|
|
31
|
+
function isFolderMarker(object) {
|
|
32
|
+
return object.size === 0 && object.key.endsWith('/');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Read one object in ranged reads and hash it. Every read is conditional on
|
|
36
|
+
* the ETag the listing reported, so the bytes hashed are one version's.
|
|
37
|
+
* Undefined when the object was deleted after it was listed, which the
|
|
38
|
+
* comparison then reports as missing.
|
|
39
|
+
*/
|
|
40
|
+
async function hashStoredObject(ctx, object, release) {
|
|
41
|
+
const location = `s3://${ctx.names.bucket}/${object.key}`;
|
|
42
|
+
const hash = createHash('sha256');
|
|
43
|
+
let read = 0;
|
|
44
|
+
try {
|
|
45
|
+
// An empty object takes one unranged read: a range on it is unsatisfiable.
|
|
46
|
+
do {
|
|
47
|
+
const end = Math.min(read + VERIFY_READ_CHUNK_BYTES, object.size) - 1;
|
|
48
|
+
const bytes = await ctx.clients.s3.getObject(ctx.names.bucket, object.key, {
|
|
49
|
+
...(object.size > 0 ? { range: { start: read, end } } : {}),
|
|
50
|
+
...(object.etag !== undefined ? { ifMatch: object.etag } : {}),
|
|
51
|
+
});
|
|
52
|
+
hash.update(bytes);
|
|
53
|
+
read += bytes.byteLength;
|
|
54
|
+
if (bytes.byteLength === 0)
|
|
55
|
+
break;
|
|
56
|
+
} while (read < object.size);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (err instanceof AwsError && err.code === 'NoSuchKey')
|
|
60
|
+
return undefined;
|
|
61
|
+
if (err instanceof AwsError && err.statusCode === 412) {
|
|
62
|
+
throw new Error(`${location} changed while verifying release ${release} - run verify again once ` +
|
|
63
|
+
'nothing is deploying to this environment', { cause: err });
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`could not read ${location} while verifying release ${release}: ${err.message}`, { cause: err });
|
|
66
|
+
}
|
|
67
|
+
return { bytes: read, sha256: hash.digest('hex') };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* What is stored under `prefix`, keyed by site-root-relative path. Objects the
|
|
71
|
+
* manifest lists with the recorded size are read and hashed; any other object
|
|
72
|
+
* is described by its size alone, which already decides the comparison. Empty
|
|
73
|
+
* folder markers are logged and left out, and an object deleted since the
|
|
74
|
+
* listing is left out too, so the comparison reports it as missing.
|
|
75
|
+
*/
|
|
76
|
+
export async function observeObjects(ctx, prefix, manifest) {
|
|
77
|
+
const recordedSizes = new Map(manifest.files.map((file) => [file.path, file.bytes]));
|
|
78
|
+
const listed = await ctx.clients.s3.listObjects(ctx.names.bucket, prefix);
|
|
79
|
+
const markers = listed.filter(isFolderMarker);
|
|
80
|
+
for (const marker of markers) {
|
|
81
|
+
ctx.logger.info(` ignored ${marker.key}: an empty folder marker, never served`);
|
|
82
|
+
}
|
|
83
|
+
const stored = listed.filter((object) => !isFolderMarker(object));
|
|
84
|
+
const { terminal } = ctx.ports;
|
|
85
|
+
let done = 0;
|
|
86
|
+
try {
|
|
87
|
+
const observed = await mapConcurrently(stored, VERIFY_CONCURRENCY, async (object) => {
|
|
88
|
+
const path = object.key.slice(prefix.length);
|
|
89
|
+
const file = recordedSizes.get(path) === object.size
|
|
90
|
+
? await hashStoredObject(ctx, object, manifest.release)
|
|
91
|
+
: { bytes: object.size };
|
|
92
|
+
done += 1;
|
|
93
|
+
terminal.status(`verifying ${done}/${stored.length} objects under ${prefix}`);
|
|
94
|
+
return [path, file];
|
|
95
|
+
});
|
|
96
|
+
return new Map(observed.filter((entry) => entry[1] !== undefined));
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
terminal.status('');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Compare the objects under `prefix` with a manifest. */
|
|
103
|
+
async function compareStoredRelease(ctx, manifest, prefix) {
|
|
104
|
+
return compareArtifact(manifest, await observeObjects(ctx, prefix, manifest));
|
|
105
|
+
}
|
|
106
|
+
function describeObserved(file) {
|
|
107
|
+
return `${file.bytes} bytes sha256 ${file.sha256 ?? '(not read: size differs)'}`;
|
|
108
|
+
}
|
|
109
|
+
/** One stable line per difference, for the terminal and for CI logs. */
|
|
110
|
+
function comparisonLines(comparison) {
|
|
111
|
+
return [
|
|
112
|
+
...comparison.missing.map((path) => ` missing ${path}`),
|
|
113
|
+
...comparison.changed.map((change) => ` changed ${change.path}: expected ${describeObserved(change.expected)}, ` +
|
|
114
|
+
`found ${describeObserved(change.actual)}`),
|
|
115
|
+
...comparison.unexpected.map((path) => ` unexpected ${path}`),
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
/** The failure summary for a comparison that did not match. */
|
|
119
|
+
function mismatchSummary(comparison) {
|
|
120
|
+
return (`${comparison.missing.length} missing, ${comparison.changed.length} changed, ` +
|
|
121
|
+
`${comparison.unexpected.length} unexpected`);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A stored copy that does not hold exactly the release it was checked
|
|
125
|
+
* against. The message names the location, the release and the counts; the
|
|
126
|
+
* comparison is kept so a caller can say how to recover.
|
|
127
|
+
*/
|
|
128
|
+
export class ReleaseMismatchError extends Error {
|
|
129
|
+
location;
|
|
130
|
+
release;
|
|
131
|
+
comparison;
|
|
132
|
+
constructor(location, release, comparison) {
|
|
133
|
+
super(`${location} does not match release ${release}: ${mismatchSummary(comparison)}`);
|
|
134
|
+
this.location = location;
|
|
135
|
+
this.release = release;
|
|
136
|
+
this.comparison = comparison;
|
|
137
|
+
this.name = 'ReleaseMismatchError';
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Check that `prefix` holds exactly the original release recorded for
|
|
142
|
+
* `hash`: logs every difference and throws when there is any, so CI fails the step.
|
|
143
|
+
*/
|
|
144
|
+
async function verifyRelease(ctx, hash, prefix) {
|
|
145
|
+
return verifyStoredRelease(ctx, await requireArtifactManifest(ctx, hash), prefix);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Check that `prefix` holds exactly the release `manifest` records: logs
|
|
149
|
+
* every difference and throws a {@link ReleaseMismatchError} when there is any.
|
|
150
|
+
*/
|
|
151
|
+
export async function verifyStoredRelease(ctx, manifest, prefix) {
|
|
152
|
+
const location = `s3://${ctx.names.bucket}/${prefix}`;
|
|
153
|
+
ctx.logger.step(`verifying ${manifest.release} against ${location} (${manifest.fileCount} files)`);
|
|
154
|
+
const comparison = await compareStoredRelease(ctx, manifest, prefix);
|
|
155
|
+
for (const line of comparisonLines(comparison))
|
|
156
|
+
ctx.logger.info(line);
|
|
157
|
+
if (!isExactMatch(comparison)) {
|
|
158
|
+
throw new ReleaseMismatchError(location, manifest.release, comparison);
|
|
159
|
+
}
|
|
160
|
+
ctx.logger.ok(`${location} matches release ${manifest.release}: ${manifest.fileCount} files, ` +
|
|
161
|
+
`sha256 ${manifest.releaseHash}`);
|
|
162
|
+
return manifest;
|
|
163
|
+
}
|
|
164
|
+
/** `blogwright verify <hash>`: the live site (or the staged copy) against a hash's recorded release. */
|
|
165
|
+
export async function verify(ctx, hash, target = 'live') {
|
|
166
|
+
if (ctx.preview || ctx.env === PREVIEW_ENV) {
|
|
167
|
+
throw new Error("verify checks an environment's live site/ against its artifact manifest, and PR previews " +
|
|
168
|
+
'record none - deploy the revision to an environment and verify it there');
|
|
169
|
+
}
|
|
170
|
+
if (ctx.config.ssr) {
|
|
171
|
+
throw new Error(`verify compares a static site with its artifact manifest, and "${ctx.env}" is an SSR ` +
|
|
172
|
+
'environment: an SSR deploy pins the exact artifact object version its revision serves ' +
|
|
173
|
+
'instead (see `blogwright history`)');
|
|
174
|
+
}
|
|
175
|
+
ctx.logger.info(colors.bold(`Verifying "${ctx.env}" against ${hash}`));
|
|
176
|
+
if (target === 'live') {
|
|
177
|
+
await verifyRelease(ctx, hash, LIVE_SITE_PREFIX);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
// A staged copy is checked against the manifest it was staged with, which
|
|
181
|
+
// is what `promote` will check it against too.
|
|
182
|
+
await verifyStoredRelease(ctx, await requireStagedReleaseManifest(ctx, hash), stagedReleasePrefix(hash));
|
|
183
|
+
}
|
package/dist/whoami.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type FileSystem, type Terminal } from 'blogwright-core';
|
|
2
|
+
import { type AwsHost } from './aws-session.js';
|
|
3
|
+
import type { Logger } from './logger.js';
|
|
4
|
+
import type { AwsLogin } from './ports.js';
|
|
5
|
+
export interface WhoamiOptions {
|
|
6
|
+
env: string;
|
|
7
|
+
configPath?: string | undefined;
|
|
8
|
+
profile?: string | undefined;
|
|
9
|
+
endpointOverride?: string | undefined;
|
|
10
|
+
}
|
|
11
|
+
export interface WhoamiDeps {
|
|
12
|
+
fs: FileSystem;
|
|
13
|
+
terminal: Terminal;
|
|
14
|
+
logger: Logger;
|
|
15
|
+
awsLogin: AwsLogin;
|
|
16
|
+
host: AwsHost;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Print the resolved identity as fixed `key value` lines on stdout. Exits 0
|
|
20
|
+
* when the identity resolves and the pin matches or is absent; a missing
|
|
21
|
+
* profile, unusable credentials or a pin mismatch is raised (exit 1).
|
|
22
|
+
*/
|
|
23
|
+
export declare function whoami(opts: WhoamiOptions, deps: WhoamiDeps): Promise<number>;
|
package/dist/whoami.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* `blogwright whoami [env]` - which AWS credentials this repo's commands
|
|
3
|
+
* would use, whose they are, and whether the account pin accepts them. Runs
|
|
4
|
+
* the same session resolution as every context-building command but loads no
|
|
5
|
+
* state, and works without a config (signing STS in us-east-1, no pin) or
|
|
6
|
+
* outside a repository, because it is most useful before a repo is set up.
|
|
7
|
+
*/
|
|
8
|
+
import { relative } from 'node:path';
|
|
9
|
+
import { findRepoRoot, RepoRootNotFoundError, shortIdentity, } from 'blogwright-core';
|
|
10
|
+
import { isNamedProfile, profileSourceLabel, resolveAwsSession, } from './aws-session.js';
|
|
11
|
+
import { ConfigNotFoundError, loadConfig } from './context.js';
|
|
12
|
+
/** Region STS signs in when there is no config to name one. */
|
|
13
|
+
const NO_CONFIG_REGION = 'us-east-1';
|
|
14
|
+
async function repoRootOrUndefined(fs, cwd) {
|
|
15
|
+
try {
|
|
16
|
+
return await findRepoRoot(fs, cwd);
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
if (err instanceof RepoRootNotFoundError)
|
|
20
|
+
return undefined;
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Load the environment's config. An explicit `--config` must exist; without
|
|
26
|
+
* one, a missing candidate or a missing repository is reported, not raised.
|
|
27
|
+
*/
|
|
28
|
+
async function locateConfig(fs, cwd, opts) {
|
|
29
|
+
const root = await repoRootOrUndefined(fs, cwd);
|
|
30
|
+
if (root === undefined && !opts.configPath)
|
|
31
|
+
return { found: false, reason: 'outside a repository' };
|
|
32
|
+
const base = root ?? cwd;
|
|
33
|
+
try {
|
|
34
|
+
const { config, path } = await loadConfig(fs, {
|
|
35
|
+
env: opts.env,
|
|
36
|
+
root: base,
|
|
37
|
+
configPath: opts.configPath,
|
|
38
|
+
});
|
|
39
|
+
return { found: true, config, label: relative(base, path) || path };
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
if (err instanceof ConfigNotFoundError && !opts.configPath) {
|
|
43
|
+
return { found: false, reason: 'no config found' };
|
|
44
|
+
}
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** `mysite-prod (config)`, or `none (default credentials)` when no profile is named. */
|
|
49
|
+
function describeProfileValue(profile) {
|
|
50
|
+
const name = isNamedProfile(profile) ? profile.name : 'none';
|
|
51
|
+
return `${name} (${profileSourceLabel(profile.source)})`;
|
|
52
|
+
}
|
|
53
|
+
const PIN_VALUES = {
|
|
54
|
+
matched: 'matches aws.accountId',
|
|
55
|
+
unset: 'not set',
|
|
56
|
+
skipped: 'skipped (endpoint override)',
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Print the resolved identity as fixed `key value` lines on stdout. Exits 0
|
|
60
|
+
* when the identity resolves and the pin matches or is absent; a missing
|
|
61
|
+
* profile, unusable credentials or a pin mismatch is raised (exit 1).
|
|
62
|
+
*/
|
|
63
|
+
export async function whoami(opts, deps) {
|
|
64
|
+
const located = await locateConfig(deps.fs, deps.host.cwd, opts);
|
|
65
|
+
const config = located.found ? located.config : undefined;
|
|
66
|
+
const session = await resolveAwsSession({
|
|
67
|
+
region: config?.region ?? NO_CONFIG_REGION,
|
|
68
|
+
endpointOverride: opts.endpointOverride,
|
|
69
|
+
flag: opts.profile,
|
|
70
|
+
aws: config?.aws,
|
|
71
|
+
configLabel: located.found ? located.label : 'config',
|
|
72
|
+
host: deps.host,
|
|
73
|
+
ports: { fs: deps.fs, terminal: deps.terminal, awsLogin: deps.awsLogin },
|
|
74
|
+
logger: deps.logger,
|
|
75
|
+
});
|
|
76
|
+
const rows = [
|
|
77
|
+
['account', session.identity.account],
|
|
78
|
+
['identity', shortIdentity(session.identity.arn)],
|
|
79
|
+
['arn', session.identity.arn],
|
|
80
|
+
['profile', describeProfileValue(session.profile)],
|
|
81
|
+
['env', `${opts.env} (${located.found ? located.label : located.reason})`],
|
|
82
|
+
['region', session.clients.region],
|
|
83
|
+
['pin', PIN_VALUES[session.pin]],
|
|
84
|
+
];
|
|
85
|
+
for (const [key, value] of rows)
|
|
86
|
+
deps.logger.info(`${key.padEnd(10)}${value}`);
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blogwright",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"blogwright": "./dist/bin.js",
|
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"blogwright-core": "0.
|
|
21
|
-
"blogwright-pds": "0.4.0",
|
|
22
|
-
"fflate": "^0.8.3"
|
|
20
|
+
"blogwright-core": "0.6.0"
|
|
23
21
|
},
|
|
24
22
|
"devDependencies": {
|
|
23
|
+
"@types/node": "^26.6.5",
|
|
25
24
|
"blogwright-build-agent": "0.1.0",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
25
|
+
"blogwright-pds": "0.6.0",
|
|
26
|
+
"fflate": "^0.8.3",
|
|
27
|
+
"oxlint": "^1.90.0",
|
|
28
28
|
"typescript": "^6.0.3",
|
|
29
|
-
"vitest": "^
|
|
29
|
+
"vitest": "^5.0.5"
|
|
30
30
|
},
|
|
31
31
|
"description": "Deploy static sites, single-page apps and server-rendered apps to AWS: S3 + CloudFront hosting, builds in a Lambda MicroVM, Lambda Web Functions for SSR, PR previews, GitHub-OIDC CI deploys, and plugins for analytics and standard.site publishing",
|
|
32
32
|
"keywords": [
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"url": "git+https://github.com/antstanley/blogwright.git"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
|
-
"node": ">=22"
|
|
48
|
+
"node": ">=22.2"
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/antstanley/blogwright#readme",
|
|
51
51
|
"bugs": "https://github.com/antstanley/blogwright/issues",
|