blogwright 0.5.0 → 0.7.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/dist/ports.d.ts CHANGED
@@ -124,6 +124,20 @@ export interface AwsLogin {
124
124
  run(args: readonly string[]): Promise<AwsLoginResult>;
125
125
  }
126
126
  /** The ports domain code reaches side effects through; adapters are wired in createContext. */
127
+ /**
128
+ * DNS questions the multi-domain bootstrap asks. The real adapter queries the
129
+ * system resolver and falls back to public ones; it throws only when no
130
+ * resolver could answer at all.
131
+ */
132
+ export interface DnsPort {
133
+ /**
134
+ * An opaque fingerprint of the SOA record `name` answers with, or
135
+ * `undefined` when it has none. A resolver follows a CNAME when asked for
136
+ * SOA, so `www` aliased to an apex answers with the apex's record; callers
137
+ * compare fingerprints rather than treating any answer as "this is a zone".
138
+ */
139
+ soa(name: string): Promise<string | undefined>;
140
+ }
127
141
  export interface Ports {
128
142
  fs: FileSystem;
129
143
  vcs: Vcs;
@@ -131,4 +145,5 @@ export interface Ports {
131
145
  ping: PingBuilder;
132
146
  loader: ModuleLoader;
133
147
  awsLogin: AwsLogin;
148
+ dns: DnsPort;
134
149
  }
@@ -0,0 +1,29 @@
1
+ import { type PathRedirect } from 'blogwright-core';
2
+ /** CloudFront Function source limit, in bytes. */
3
+ export declare const ROUTER_CODE_LIMIT = 10240;
4
+ /** True when the compiled router source fits the CloudFront Function limit. */
5
+ export declare function routerCodeFits(code: string): boolean;
6
+ /** Hosts whose requests redirect, and the host they redirect to. */
7
+ export interface AliasRedirect {
8
+ /** The canonical host (`OpsConfig.domain`); the redirect target. */
9
+ primary: string;
10
+ /** Hosts that answer with a `301` to `primary`. */
11
+ aliases: readonly string[];
12
+ }
13
+ /**
14
+ * Source of the production/staging CloudFront viewer-request function. With no
15
+ * `redirect` it is the directory-index rewrite alone, byte-for-byte what every
16
+ * existing site already runs. A distribution allows one function per event
17
+ * type, so the alias redirect is folded into this function rather than being a
18
+ * second one.
19
+ */
20
+ export declare function staticRouterCode(redirect?: AliasRedirect, paths?: readonly PathRedirect[]): string;
21
+ /**
22
+ * Source of the viewer-request function an SSR distribution runs when
23
+ * `redirectToPrimary` is set. The application owns its own routing, so unlike
24
+ * the static router there is no directory-index rewrite: alias hosts get the
25
+ * `301` and every other request passes through untouched.
26
+ */
27
+ export declare function ssrRouterCode(redirect: AliasRedirect | undefined, paths?: readonly PathRedirect[]): string;
28
+ /** Stable fingerprint of a function's source, recorded in state to detect a change. */
29
+ export declare function routerCodeHash(code: string): string;
@@ -0,0 +1,149 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { isPrefixRedirect, normalizeRedirectFrom } from 'blogwright-core';
3
+ /** CloudFront Function source limit, in bytes. */
4
+ export const ROUTER_CODE_LIMIT = 10_240;
5
+ /** True when the compiled router source fits the CloudFront Function limit. */
6
+ export function routerCodeFits(code) {
7
+ return Buffer.byteLength(code, 'utf8') <= ROUTER_CODE_LIMIT;
8
+ }
9
+ /**
10
+ * The directory-index rewrite every static-mode request goes through. The
11
+ * S3 origin is the private REST endpoint (via OAC), which - unlike an S3
12
+ * website endpoint - does no index-document resolution, so `/projects/`
13
+ * becomes `/projects/index.html` here. The `/site` origin path is applied by
14
+ * the distribution, so this must not add it.
15
+ */
16
+ const INDEX_REWRITE = ` var uri = request.uri;
17
+ if (uri.endsWith('/')) { uri += 'index.html'; }
18
+ else if (uri.lastIndexOf('.') < uri.lastIndexOf('/')) { uri += '/index.html'; }
19
+ request.uri = uri;
20
+ return request;`;
21
+ /**
22
+ * Host check that runs ahead of the rewrite when `redirectToPrimary` is set:
23
+ * a request for an alias answers `301` to the same path and query on the
24
+ * primary. It reads `request.uri` before the rewrite touches it, so the
25
+ * redirect target is the URL the viewer asked for, not the origin key.
26
+ */
27
+ function redirectBlock(redirect, declareHost = true) {
28
+ return `${declareHost ? HOST_LINE : ''} if (${JSON.stringify([...redirect.aliases])}.indexOf(host) !== -1) {
29
+ ${queryParts(' ')}
30
+ return {
31
+ statusCode: 301,
32
+ statusDescription: 'Moved Permanently',
33
+ headers: {
34
+ location: { value: 'https://' + ${JSON.stringify(redirect.primary)} + request.uri + (parts.length ? '?' + parts.join('&') : '') }
35
+ }
36
+ };
37
+ }
38
+ `;
39
+ }
40
+ const HOST_LINE = ` var host = request.headers.host ? request.headers.host.value.toLowerCase() : '';
41
+ `;
42
+ /** Rebuilds the request's query string into `parts`; shared by both redirect blocks. */
43
+ function queryParts(indent) {
44
+ return [
45
+ 'var parts = [];',
46
+ 'var query = request.querystring;',
47
+ 'for (var key in query) {',
48
+ ' var entries = query[key].multiValue || [query[key]];',
49
+ ' for (var i = 0; i < entries.length; i++) {',
50
+ " parts.push(entries[i].value === '' ? key : key + '=' + entries[i].value);",
51
+ ' }',
52
+ '}',
53
+ ]
54
+ .map((line) => indent + line)
55
+ .join('\n');
56
+ }
57
+ /**
58
+ * Path-redirect check that runs ahead of the alias host check and the rewrite.
59
+ * Exact matches are one object lookup; prefix matches are an array walked
60
+ * longest-first. For an alias request (when `redirectToPrimary` applies) a
61
+ * relative target is made absolute on the primary, so the request reaches the
62
+ * final URL in one hop.
63
+ */
64
+ function pathRedirectsBlock(paths, alias) {
65
+ const exact = {};
66
+ const prefixes = [];
67
+ for (const r of paths) {
68
+ const from = normalizeRedirectFrom(r.from);
69
+ const status = r.status ?? 301;
70
+ if (isPrefixRedirect(from))
71
+ prefixes.push([from.slice(0, -2), r.to, status]);
72
+ else
73
+ exact[from] = [r.to, status];
74
+ }
75
+ prefixes.sort((a, b) => b[0].length - a[0].length);
76
+ const aliasCheck = alias ? `${JSON.stringify([...alias.aliases])}.indexOf(host) !== -1` : 'false';
77
+ const primary = alias ? JSON.stringify(alias.primary) : '""';
78
+ return `${alias ? HOST_LINE : ''} var uri = request.uri;
79
+ var key = uri.length > 1 && uri.charAt(uri.length - 1) === '/' ? uri.slice(0, -1) : uri;
80
+ var exact = ${JSON.stringify(exact)};
81
+ var prefixes = ${JSON.stringify(prefixes)};
82
+ var to = null;
83
+ var status = 301;
84
+ if (exact.hasOwnProperty(key)) {
85
+ to = exact[key][0];
86
+ status = exact[key][1];
87
+ } else {
88
+ for (var p = 0; p < prefixes.length; p++) {
89
+ var base = prefixes[p][0];
90
+ if (key === base || key.indexOf(base + '/') === 0) {
91
+ to = prefixes[p][1].split(':splat').join(uri.length > base.length ? uri.slice(base.length + 1) : '');
92
+ status = prefixes[p][2];
93
+ // A splat that begins with slashes (\`/old//evil.com\`) must not turn a
94
+ // relative target into a protocol-relative one (\`//evil.com\`).
95
+ if (to.charAt(0) === '/') { to = '/' + to.replace(/^[\\/\\\\]+/, ''); }
96
+ break;
97
+ }
98
+ }
99
+ }
100
+ if (to !== null) {
101
+ ${queryParts(' ')}
102
+ if (to.charAt(0) === '/' && ${aliasCheck}) { to = 'https://' + ${primary} + to; }
103
+ if (parts.length) { to += (to.indexOf('?') === -1 ? '?' : '&') + parts.join('&'); }
104
+ return {
105
+ statusCode: status,
106
+ statusDescription: { 301: 'Moved Permanently', 302: 'Found', 307: 'Temporary Redirect', 308: 'Permanent Redirect' }[status],
107
+ headers: { location: { value: to } }
108
+ };
109
+ }
110
+ `;
111
+ }
112
+ /** The redirect checks that precede the rewrite: path redirects, then the alias host check. */
113
+ function checks(redirect, paths) {
114
+ const alias = redirect && redirect.aliases.length > 0 ? redirect : undefined;
115
+ if (paths.length > 0)
116
+ return pathRedirectsBlock(paths, alias) + (alias ? redirectBlock(alias, false) : '');
117
+ return alias ? redirectBlock(alias) : '';
118
+ }
119
+ /**
120
+ * Source of the production/staging CloudFront viewer-request function. With no
121
+ * `redirect` it is the directory-index rewrite alone, byte-for-byte what every
122
+ * existing site already runs. A distribution allows one function per event
123
+ * type, so the alias redirect is folded into this function rather than being a
124
+ * second one.
125
+ */
126
+ export function staticRouterCode(redirect, paths = []) {
127
+ const block = checks(redirect, paths);
128
+ return `function handler(event) {
129
+ var request = event.request;
130
+ ${block}${INDEX_REWRITE}
131
+ }`;
132
+ }
133
+ /**
134
+ * Source of the viewer-request function an SSR distribution runs when
135
+ * `redirectToPrimary` is set. The application owns its own routing, so unlike
136
+ * the static router there is no directory-index rewrite: alias hosts get the
137
+ * `301` and every other request passes through untouched.
138
+ */
139
+ export function ssrRouterCode(redirect, paths = []) {
140
+ const block = checks(redirect, paths);
141
+ return `function handler(event) {
142
+ var request = event.request;
143
+ ${block} return request;
144
+ }`;
145
+ }
146
+ /** Stable fingerprint of a function's source, recorded in state to detect a change. */
147
+ export function routerCodeHash(code) {
148
+ return createHash('sha256').update(code).digest('hex');
149
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blogwright",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "blogwright": "./dist/bin.js",
@@ -17,12 +17,12 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "blogwright-core": "0.5.0"
20
+ "blogwright-core": "0.7.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/node": "^26.6.5",
24
24
  "blogwright-build-agent": "0.1.0",
25
- "blogwright-pds": "0.5.0",
25
+ "blogwright-pds": "0.7.0",
26
26
  "fflate": "^0.8.3",
27
27
  "oxlint": "^1.90.0",
28
28
  "typescript": "^6.0.3",