@capacms/sdk 1.0.0-next.10 → 1.0.0-next.12
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/CHANGELOG.md +170 -0
- package/README.md +368 -5
- package/bin/capa-check-bundle.js +80 -0
- package/bin/capa.js +3 -3
- package/bin/graphql-project.js +1 -1
- package/dist/client.d.ts +84 -4
- package/dist/client.js +23 -5
- package/dist/esm/image/index.d.ts +69 -2
- package/dist/esm/image/index.js +108 -5
- package/dist/esm/image/shared-params.generated.d.ts +126 -2
- package/dist/esm/image/shared-params.generated.js +224 -5
- package/dist/esm/overlay/index.d.ts +4 -1
- package/dist/esm/overlay/index.js +382 -30
- package/dist/esm/overlay/live-fields.d.ts +133 -0
- package/dist/esm/overlay/live-fields.js +478 -0
- package/dist/esm/overlay/markdown.d.ts +38 -0
- package/dist/esm/overlay/markdown.js +536 -0
- package/dist/esm/overlay/protocol.d.ts +51 -5
- package/dist/esm/overlay/protocol.js +79 -9
- package/dist/image/index.d.ts +69 -2
- package/dist/image/index.js +109 -4
- package/dist/image/shared-params.generated.d.ts +126 -2
- package/dist/image/shared-params.generated.js +235 -6
- package/dist/index.d.ts +1 -1
- package/dist/next/attrs.d.ts +42 -1
- package/dist/next/attrs.js +42 -3
- package/dist/next/client.d.ts +48 -1
- package/dist/next/client.js +33 -2
- package/dist/next/entry-fields.d.ts +43 -2
- package/dist/next/graphql/request.d.ts +4 -0
- package/dist/next/graphql/request.js +4 -0
- package/dist/next/graphql/tree.js +9 -0
- package/dist/next/index.d.ts +4 -4
- package/dist/next/index.js +3 -1
- package/dist/nextjs/index.d.ts +91 -7
- package/dist/nextjs/index.js +230 -17
- package/dist/nextjs/write-key-guard.d.ts +117 -0
- package/dist/nextjs/write-key-guard.js +285 -0
- package/dist/overlay/index.d.ts +4 -1
- package/dist/overlay/index.js +399 -29
- package/dist/overlay/live-fields.d.ts +133 -0
- package/dist/overlay/live-fields.js +493 -0
- package/dist/overlay/markdown.d.ts +38 -0
- package/dist/overlay/markdown.js +539 -0
- package/dist/overlay/protocol.d.ts +51 -5
- package/dist/overlay/protocol.js +82 -9
- package/dist/server/browser.d.ts +1 -0
- package/dist/server/browser.js +20 -0
- package/dist/server/client.d.ts +268 -0
- package/dist/server/client.js +335 -0
- package/dist/server/errors.d.ts +60 -0
- package/dist/server/errors.js +120 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +32 -0
- package/dist/server/key-format.d.ts +41 -0
- package/dist/server/key-format.js +91 -0
- package/dist/server/transport.d.ts +47 -0
- package/dist/server/transport.js +114 -0
- package/dist/webhooks.d.ts +8 -0
- package/package.json +18 -4
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
export interface WriteKeyFinding {
|
|
2
|
+
/** The file, relative to the scanned directory, with `/` separators. */
|
|
3
|
+
file: string;
|
|
4
|
+
line: number;
|
|
5
|
+
/** The first 12 characters of the match, never the whole key. */
|
|
6
|
+
prefix: string;
|
|
7
|
+
/** Whether the match's checksum is right, so it is most likely a real key and not just shaped like one. */
|
|
8
|
+
checksumOk: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Every write-key-shaped string in `text`, with its 1-based line. */
|
|
11
|
+
export declare function findWriteKeys(text: string): Array<{
|
|
12
|
+
line: number;
|
|
13
|
+
prefix: string;
|
|
14
|
+
checksumOk: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
export interface ScanOptions {
|
|
17
|
+
/** Only files with one of these endings, such as `[".html", ".rsc"]`. Every file when left out. */
|
|
18
|
+
extensions?: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
/** Scan every file under `dir`, or every file with one of `extensions`. A directory that does not exist has no findings. */
|
|
21
|
+
export declare function scanForWriteKeys(dir: string, options?: ScanOptions): WriteKeyFinding[];
|
|
22
|
+
/**
|
|
23
|
+
* The parts of a finished Next build that a browser downloads. `static/` is
|
|
24
|
+
* served whole. Under `server/`, only what prerendering wrote is served: an
|
|
25
|
+
* App Router page's HTML and RSC payload (`.html`, `.rsc`, segment `.rsc`
|
|
26
|
+
* files) and a static route handler's body (`.body`), and a Pages Router
|
|
27
|
+
* page's HTML and `getStaticProps` data (`.html`, `.json`). The `.js` beside
|
|
28
|
+
* them is server code and is never sent, so it is not scanned.
|
|
29
|
+
*
|
|
30
|
+
* This is where a key passed from a server component to a client
|
|
31
|
+
* component's props ends up on a static page: in the page's HTML and RSC
|
|
32
|
+
* payload, never in `static/`.
|
|
33
|
+
*/
|
|
34
|
+
export declare const NEXT_BROWSER_FILES: ReadonlyArray<{
|
|
35
|
+
dir: string;
|
|
36
|
+
extensions?: readonly string[];
|
|
37
|
+
}>;
|
|
38
|
+
/** Whether `dir` is a Next build directory (`.next`, or the config's `distDir`). */
|
|
39
|
+
export declare function isNextBuildDir(dir: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Scan what a browser downloads from a finished Next build in `distDir`
|
|
42
|
+
* (`NEXT_BROWSER_FILES`). Each finding's `file` is relative to `distDir`,
|
|
43
|
+
* such as `server/app/book.html`. Run it after `next build` has finished:
|
|
44
|
+
* prerendered pages are written after the compile hook runs.
|
|
45
|
+
*/
|
|
46
|
+
export declare function scanNextBuildForWriteKeys(distDir: string): WriteKeyFinding[];
|
|
47
|
+
/**
|
|
48
|
+
* Findings split in two: `keys`, whose checksum is right, so almost surely a
|
|
49
|
+
* real key, which fail a check; and `shaped`, which only look like one (a
|
|
50
|
+
* placeholder in docs, a long identifier), which only warn.
|
|
51
|
+
*/
|
|
52
|
+
export declare function splitFindings(findings: readonly WriteKeyFinding[]): {
|
|
53
|
+
keys: WriteKeyFinding[];
|
|
54
|
+
shaped: WriteKeyFinding[];
|
|
55
|
+
};
|
|
56
|
+
/** The message a failed check prints: what was found, where, and what to do. Pass it the `keys` of `splitFindings`. */
|
|
57
|
+
export declare function writeKeyReport(findings: readonly WriteKeyFinding[], scanned: string): string;
|
|
58
|
+
/** The warning for values shaped like a write key whose checksum is wrong. Pass it the `shaped` of `splitFindings`. */
|
|
59
|
+
export declare function writeKeyShapeWarning(findings: readonly WriteKeyFinding[], scanned: string): string;
|
|
60
|
+
/** Thrown when a scan finds a key. Its message is `writeKeyReport`. */
|
|
61
|
+
export declare class CapaWriteKeyInBundleError extends Error {
|
|
62
|
+
readonly findings: readonly WriteKeyFinding[];
|
|
63
|
+
constructor(findings: readonly WriteKeyFinding[], scanned: string);
|
|
64
|
+
}
|
|
65
|
+
/** Where a check prints its warning. `console.warn` unless a caller passes its own. */
|
|
66
|
+
export type WarnFn = (message: string) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Scan `<distDir>/static` and throw `CapaWriteKeyInBundleError` on a key.
|
|
69
|
+
* A value only shaped like one is a warning. This is what the compile hook
|
|
70
|
+
* can see; prerendered pages come later (`assertNoWriteKeysInBuild`).
|
|
71
|
+
*/
|
|
72
|
+
export declare function assertNoWriteKeysInBundle(distDir: string, warn?: WarnFn): void;
|
|
73
|
+
/**
|
|
74
|
+
* Scan a finished Next build (`scanNextBuildForWriteKeys`), static files and
|
|
75
|
+
* prerendered pages, and throw `CapaWriteKeyInBundleError` on a key. What
|
|
76
|
+
* `capa-check-bundle` runs after `next build`.
|
|
77
|
+
*/
|
|
78
|
+
export declare function assertNoWriteKeysInBuild(distDir: string, warn?: WarnFn): void;
|
|
79
|
+
type AfterCompile = (metadata: {
|
|
80
|
+
projectDir: string;
|
|
81
|
+
distDir: string;
|
|
82
|
+
}) => Promise<void>;
|
|
83
|
+
/** The parts of a Next config this touches. Anything else passes through untouched. */
|
|
84
|
+
export interface GuardableNextConfig {
|
|
85
|
+
distDir?: string;
|
|
86
|
+
compiler?: {
|
|
87
|
+
runAfterProductionCompile?: AfterCompile;
|
|
88
|
+
[key: string]: unknown;
|
|
89
|
+
} & Record<string, unknown>;
|
|
90
|
+
[key: string]: unknown;
|
|
91
|
+
}
|
|
92
|
+
type ConfigFunction<C> = (phase: string, context: {
|
|
93
|
+
defaultConfig: unknown;
|
|
94
|
+
}) => C | Promise<C>;
|
|
95
|
+
export interface WriteKeyGuardOptions {
|
|
96
|
+
/** The Next version to check against. Read from the project's `next/package.json` when left out. */
|
|
97
|
+
nextVersion?: string | null;
|
|
98
|
+
/** Where warnings go. `console.warn` when left out. */
|
|
99
|
+
warn?: WarnFn;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The installed Next's version: resolved from the project (the directory
|
|
103
|
+
* `next build` runs in), else from this package. Null when neither resolves.
|
|
104
|
+
*/
|
|
105
|
+
export declare function installedNextVersion(): string | null;
|
|
106
|
+
/** Whether a Next version calls `compiler.runAfterProductionCompile`: 15.4 and later. Null when the version is unknown. */
|
|
107
|
+
export declare function nextRunsAfterProductionCompile(version: string | null | undefined): boolean | null;
|
|
108
|
+
/**
|
|
109
|
+
* Wrap a Next config so `next build` fails when a `capw_` key is in a browser
|
|
110
|
+
* bundle. Takes the config object or the function form, and returns the same
|
|
111
|
+
* form. An earlier check beside the required one, `capa-check-bundle` after
|
|
112
|
+
* the build: this runs before prerendering, on Next 15.4 and later. On an
|
|
113
|
+
* older Next it adds nothing and warns once.
|
|
114
|
+
*/
|
|
115
|
+
export declare function withCapaWriteKeyGuard<C extends GuardableNextConfig>(config: C, options?: WriteKeyGuardOptions): C;
|
|
116
|
+
export declare function withCapaWriteKeyGuard<C extends GuardableNextConfig>(config: ConfigFunction<C>, options?: WriteKeyGuardOptions): ConfigFunction<C>;
|
|
117
|
+
export {};
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CapaWriteKeyInBundleError = exports.NEXT_BROWSER_FILES = void 0;
|
|
4
|
+
exports.findWriteKeys = findWriteKeys;
|
|
5
|
+
exports.scanForWriteKeys = scanForWriteKeys;
|
|
6
|
+
exports.isNextBuildDir = isNextBuildDir;
|
|
7
|
+
exports.scanNextBuildForWriteKeys = scanNextBuildForWriteKeys;
|
|
8
|
+
exports.splitFindings = splitFindings;
|
|
9
|
+
exports.writeKeyReport = writeKeyReport;
|
|
10
|
+
exports.writeKeyShapeWarning = writeKeyShapeWarning;
|
|
11
|
+
exports.assertNoWriteKeysInBundle = assertNoWriteKeysInBundle;
|
|
12
|
+
exports.assertNoWriteKeysInBuild = assertNoWriteKeysInBuild;
|
|
13
|
+
exports.installedNextVersion = installedNextVersion;
|
|
14
|
+
exports.nextRunsAfterProductionCompile = nextRunsAfterProductionCompile;
|
|
15
|
+
exports.withCapaWriteKeyGuard = withCapaWriteKeyGuard;
|
|
16
|
+
/**
|
|
17
|
+
* write-key-guard.ts — fail a Next.js build that put a `capw_` write key in
|
|
18
|
+
* the files a browser downloads.
|
|
19
|
+
*
|
|
20
|
+
* // next.config.ts
|
|
21
|
+
* import { withCapaWriteKeyGuard } from "@capacms/sdk/nextjs";
|
|
22
|
+
* export default withCapaWriteKeyGuard({ ... });
|
|
23
|
+
*
|
|
24
|
+
* THE REQUIRED STEP IS THE BIN, after the build:
|
|
25
|
+
*
|
|
26
|
+
* "build": "next build && capa-check-bundle"
|
|
27
|
+
*
|
|
28
|
+
* It scans the finished build, prerendered pages included
|
|
29
|
+
* (`scanNextBuildForWriteKeys`). A key passed from a server component to a
|
|
30
|
+
* client component's props lands in a static page's HTML and RSC payload
|
|
31
|
+
* under `server/app`, which only exist once `next build` is done.
|
|
32
|
+
*
|
|
33
|
+
* HOW THE WRAPPER HOOKS IN. `compiler.runAfterProductionCompile`, which Next
|
|
34
|
+
* 15.4 and later run once the client and server bundles are written and
|
|
35
|
+
* before type checking and static generation, with webpack and with
|
|
36
|
+
* Turbopack alike. A throw there fails `next build` early, for a key inlined
|
|
37
|
+
* into client code (a `NEXT_PUBLIC_` variable). It cannot see prerendered
|
|
38
|
+
* pages. The wrapper keeps any hook the config already has and runs it first.
|
|
39
|
+
*
|
|
40
|
+
* WHAT IT SCANS. Every file under `<distDir>/static`, which is everything Next
|
|
41
|
+
* serves to browsers from the build, for `capw_` followed by 49 letters and
|
|
42
|
+
* digits. A match whose checksum is right fails the build and names the file
|
|
43
|
+
* and line; a match that is only shaped like a key is a warning. The key is
|
|
44
|
+
* printed as its first 12 characters only, so the build log does not leak it
|
|
45
|
+
* a second time.
|
|
46
|
+
*
|
|
47
|
+
* Node's `fs` and `path` are loaded when a scan runs, not when this module is
|
|
48
|
+
* imported, so importing `@capacms/sdk/nextjs` from middleware still builds
|
|
49
|
+
* for the edge runtime.
|
|
50
|
+
*/
|
|
51
|
+
const key_format_1 = require("../server/key-format");
|
|
52
|
+
/**
|
|
53
|
+
* A Node built-in, loaded when a scan runs. `process.getBuiltinModule` first
|
|
54
|
+
* (Node 20.16, 22.3 and later), else this module's own `module.require`.
|
|
55
|
+
* Neither is a `require("node:fs")` call a bundler would try to resolve, so
|
|
56
|
+
* `@capacms/sdk/nextjs` still builds for the edge runtime, where a scan
|
|
57
|
+
* never runs.
|
|
58
|
+
*/
|
|
59
|
+
function builtin(id) {
|
|
60
|
+
const proc = globalThis.process;
|
|
61
|
+
if (typeof proc?.getBuiltinModule === "function")
|
|
62
|
+
return proc.getBuiltinModule(id);
|
|
63
|
+
if (typeof module !== "undefined" && typeof module?.require === "function")
|
|
64
|
+
return module.require(id);
|
|
65
|
+
throw new Error(`@capacms/sdk/nextjs: the write key check needs Node's ${id}, which is not available here. Run capa-check-bundle after the build instead.`);
|
|
66
|
+
}
|
|
67
|
+
function loadFs() {
|
|
68
|
+
return builtin("node:fs");
|
|
69
|
+
}
|
|
70
|
+
function loadPath() {
|
|
71
|
+
return builtin("node:path");
|
|
72
|
+
}
|
|
73
|
+
/** Every write-key-shaped string in `text`, with its 1-based line. */
|
|
74
|
+
function findWriteKeys(text) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const match of text.matchAll(key_format_1.WRITE_KEY_IN_TEXT)) {
|
|
77
|
+
let line = 1;
|
|
78
|
+
for (let i = 0; i < (match.index ?? 0); i++)
|
|
79
|
+
if (text.charCodeAt(i) === 10)
|
|
80
|
+
line++;
|
|
81
|
+
out.push({ line, prefix: (0, key_format_1.writeKeyLabel)(match[0]), checksumOk: (0, key_format_1.writeKeyChecksumOk)(match[0]) });
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/** Scan every file under `dir`, or every file with one of `extensions`. A directory that does not exist has no findings. */
|
|
86
|
+
function scanForWriteKeys(dir, options = {}) {
|
|
87
|
+
const fs = loadFs();
|
|
88
|
+
const path = loadPath();
|
|
89
|
+
const root = path.resolve(dir);
|
|
90
|
+
if (!fs.existsSync(root))
|
|
91
|
+
return [];
|
|
92
|
+
const findings = [];
|
|
93
|
+
const wanted = options.extensions;
|
|
94
|
+
const walk = (current) => {
|
|
95
|
+
const entries = fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
96
|
+
for (const entry of entries) {
|
|
97
|
+
const full = path.join(current, entry.name);
|
|
98
|
+
if (entry.isDirectory())
|
|
99
|
+
walk(full);
|
|
100
|
+
else if (entry.isFile() && (!wanted || wanted.some((ending) => entry.name.endsWith(ending)))) {
|
|
101
|
+
// latin1 maps every byte to one character, so a binary file reads without a decode error
|
|
102
|
+
// and an ASCII key in it is still found.
|
|
103
|
+
const text = fs.readFileSync(full, "latin1");
|
|
104
|
+
if (!text.includes("capw_"))
|
|
105
|
+
continue;
|
|
106
|
+
const file = path.relative(root, full).split(path.sep).join("/");
|
|
107
|
+
for (const hit of findWriteKeys(text))
|
|
108
|
+
findings.push({ file, ...hit });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
walk(root);
|
|
113
|
+
return findings;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The parts of a finished Next build that a browser downloads. `static/` is
|
|
117
|
+
* served whole. Under `server/`, only what prerendering wrote is served: an
|
|
118
|
+
* App Router page's HTML and RSC payload (`.html`, `.rsc`, segment `.rsc`
|
|
119
|
+
* files) and a static route handler's body (`.body`), and a Pages Router
|
|
120
|
+
* page's HTML and `getStaticProps` data (`.html`, `.json`). The `.js` beside
|
|
121
|
+
* them is server code and is never sent, so it is not scanned.
|
|
122
|
+
*
|
|
123
|
+
* This is where a key passed from a server component to a client
|
|
124
|
+
* component's props ends up on a static page: in the page's HTML and RSC
|
|
125
|
+
* payload, never in `static/`.
|
|
126
|
+
*/
|
|
127
|
+
exports.NEXT_BROWSER_FILES = [
|
|
128
|
+
{ dir: "static" },
|
|
129
|
+
{ dir: "server/app", extensions: [".html", ".rsc", ".body"] },
|
|
130
|
+
{ dir: "server/pages", extensions: [".html", ".json"] },
|
|
131
|
+
];
|
|
132
|
+
/** Whether `dir` is a Next build directory (`.next`, or the config's `distDir`). */
|
|
133
|
+
function isNextBuildDir(dir) {
|
|
134
|
+
const fs = loadFs();
|
|
135
|
+
const path = loadPath();
|
|
136
|
+
if (fs.existsSync(path.join(dir, "BUILD_ID")))
|
|
137
|
+
return true;
|
|
138
|
+
return fs.existsSync(path.join(dir, "static")) && fs.existsSync(path.join(dir, "server"));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Scan what a browser downloads from a finished Next build in `distDir`
|
|
142
|
+
* (`NEXT_BROWSER_FILES`). Each finding's `file` is relative to `distDir`,
|
|
143
|
+
* such as `server/app/book.html`. Run it after `next build` has finished:
|
|
144
|
+
* prerendered pages are written after the compile hook runs.
|
|
145
|
+
*/
|
|
146
|
+
function scanNextBuildForWriteKeys(distDir) {
|
|
147
|
+
const path = loadPath();
|
|
148
|
+
return exports.NEXT_BROWSER_FILES.flatMap(({ dir, extensions }) => scanForWriteKeys(path.join(distDir, dir), { extensions }).map((finding) => ({ ...finding, file: `${dir}/${finding.file}` })));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Findings split in two: `keys`, whose checksum is right, so almost surely a
|
|
152
|
+
* real key, which fail a check; and `shaped`, which only look like one (a
|
|
153
|
+
* placeholder in docs, a long identifier), which only warn.
|
|
154
|
+
*/
|
|
155
|
+
function splitFindings(findings) {
|
|
156
|
+
return { keys: findings.filter((f) => f.checksumOk), shaped: findings.filter((f) => !f.checksumOk) };
|
|
157
|
+
}
|
|
158
|
+
function findingLines(findings, scanned) {
|
|
159
|
+
return findings.map((f) => ` ${scanned}/${f.file}:${f.line} ${f.prefix}`);
|
|
160
|
+
}
|
|
161
|
+
/** The message a failed check prints: what was found, where, and what to do. Pass it the `keys` of `splitFindings`. */
|
|
162
|
+
function writeKeyReport(findings, scanned) {
|
|
163
|
+
return [
|
|
164
|
+
`@capacms/sdk: a Capa server write key (capw_) is in the browser bundle, ${findings.length} ${findings.length === 1 ? "match" : "matches"}:`,
|
|
165
|
+
...findingLines(findings, scanned),
|
|
166
|
+
"A write key works from a server only. Read it from a server-only environment variable (never NEXT_PUBLIC_),",
|
|
167
|
+
"use it only in server code, and rotate this key in the Capa admin under Developers > Keys, since it may have shipped.",
|
|
168
|
+
].join("\n");
|
|
169
|
+
}
|
|
170
|
+
/** The warning for values shaped like a write key whose checksum is wrong. Pass it the `shaped` of `splitFindings`. */
|
|
171
|
+
function writeKeyShapeWarning(findings, scanned) {
|
|
172
|
+
return [
|
|
173
|
+
`@capacms/sdk: ${findings.length} ${findings.length === 1 ? "value is" : "values are"} shaped like a Capa write key (capw_) but the checksum does not match, so not failing:`,
|
|
174
|
+
...findingLines(findings, scanned),
|
|
175
|
+
"If one of these is a real key cut or changed in the build, move the code that uses it to the server and rotate the key.",
|
|
176
|
+
].join("\n");
|
|
177
|
+
}
|
|
178
|
+
/** Thrown when a scan finds a key. Its message is `writeKeyReport`. */
|
|
179
|
+
class CapaWriteKeyInBundleError extends Error {
|
|
180
|
+
findings;
|
|
181
|
+
constructor(findings, scanned) {
|
|
182
|
+
super(writeKeyReport(findings, scanned));
|
|
183
|
+
this.name = "CapaWriteKeyInBundleError";
|
|
184
|
+
this.findings = findings;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.CapaWriteKeyInBundleError = CapaWriteKeyInBundleError;
|
|
188
|
+
/**
|
|
189
|
+
* Throw `CapaWriteKeyInBundleError` for every finding whose checksum is right,
|
|
190
|
+
* and warn about the ones only shaped like a key.
|
|
191
|
+
*/
|
|
192
|
+
function settle(findings, scanned, warn) {
|
|
193
|
+
const { keys, shaped } = splitFindings(findings);
|
|
194
|
+
if (shaped.length > 0)
|
|
195
|
+
warn(writeKeyShapeWarning(shaped, scanned));
|
|
196
|
+
if (keys.length > 0)
|
|
197
|
+
throw new CapaWriteKeyInBundleError(keys, scanned);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Scan `<distDir>/static` and throw `CapaWriteKeyInBundleError` on a key.
|
|
201
|
+
* A value only shaped like one is a warning. This is what the compile hook
|
|
202
|
+
* can see; prerendered pages come later (`assertNoWriteKeysInBuild`).
|
|
203
|
+
*/
|
|
204
|
+
function assertNoWriteKeysInBundle(distDir, warn = (message) => console.warn(message)) {
|
|
205
|
+
const path = loadPath();
|
|
206
|
+
const staticDir = path.join(distDir, "static");
|
|
207
|
+
settle(scanForWriteKeys(staticDir), staticDir, warn);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Scan a finished Next build (`scanNextBuildForWriteKeys`), static files and
|
|
211
|
+
* prerendered pages, and throw `CapaWriteKeyInBundleError` on a key. What
|
|
212
|
+
* `capa-check-bundle` runs after `next build`.
|
|
213
|
+
*/
|
|
214
|
+
function assertNoWriteKeysInBuild(distDir, warn = (message) => console.warn(message)) {
|
|
215
|
+
settle(scanNextBuildForWriteKeys(distDir), distDir, warn);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* The installed Next's version: resolved from the project (the directory
|
|
219
|
+
* `next build` runs in), else from this package. Null when neither resolves.
|
|
220
|
+
*/
|
|
221
|
+
function installedNextVersion() {
|
|
222
|
+
const read = (load) => {
|
|
223
|
+
const version = load("next/package.json")?.version;
|
|
224
|
+
return typeof version === "string" ? version : null;
|
|
225
|
+
};
|
|
226
|
+
try {
|
|
227
|
+
const path = loadPath();
|
|
228
|
+
const { createRequire } = builtin("node:module");
|
|
229
|
+
const cwd = globalThis.process?.cwd?.();
|
|
230
|
+
if (cwd)
|
|
231
|
+
return read(createRequire(path.join(cwd, "package.json")));
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Not resolvable from the project; try from here.
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
const own = typeof module !== "undefined" ? module : undefined;
|
|
238
|
+
if (typeof own?.require === "function")
|
|
239
|
+
return read((id) => own.require(id));
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// Unknown.
|
|
243
|
+
}
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
/** Whether a Next version calls `compiler.runAfterProductionCompile`: 15.4 and later. Null when the version is unknown. */
|
|
247
|
+
function nextRunsAfterProductionCompile(version) {
|
|
248
|
+
const match = /^(\d+)\.(\d+)/.exec(version ?? "");
|
|
249
|
+
if (!match)
|
|
250
|
+
return null;
|
|
251
|
+
const major = Number(match[1]);
|
|
252
|
+
const minor = Number(match[2]);
|
|
253
|
+
return major > 15 || (major === 15 && minor >= 4);
|
|
254
|
+
}
|
|
255
|
+
let warnedOldNext = false;
|
|
256
|
+
function guard(config, options) {
|
|
257
|
+
const version = options.nextVersion === undefined ? installedNextVersion() : options.nextVersion;
|
|
258
|
+
if (nextRunsAfterProductionCompile(version) === false) {
|
|
259
|
+
// An older Next never calls the hook and warns that the key is unknown,
|
|
260
|
+
// so add nothing, and say the check is not running.
|
|
261
|
+
const message = `@capacms/sdk: withCapaWriteKeyGuard does nothing on Next ${version}: Next calls compiler.runAfterProductionCompile from 15.4. ` +
|
|
262
|
+
'Run capa-check-bundle after the build, which is the required check anyway: "build": "next build && capa-check-bundle".';
|
|
263
|
+
if (options.warn)
|
|
264
|
+
options.warn(message);
|
|
265
|
+
else if (!warnedOldNext) {
|
|
266
|
+
warnedOldNext = true;
|
|
267
|
+
console.warn(message);
|
|
268
|
+
}
|
|
269
|
+
return config;
|
|
270
|
+
}
|
|
271
|
+
const warn = options.warn ?? ((message) => console.warn(message));
|
|
272
|
+
const previous = config.compiler?.runAfterProductionCompile;
|
|
273
|
+
const runAfterProductionCompile = async (metadata) => {
|
|
274
|
+
if (previous)
|
|
275
|
+
await previous(metadata);
|
|
276
|
+
assertNoWriteKeysInBundle(metadata.distDir, warn);
|
|
277
|
+
};
|
|
278
|
+
return { ...config, compiler: { ...(config.compiler ?? {}), runAfterProductionCompile } };
|
|
279
|
+
}
|
|
280
|
+
function withCapaWriteKeyGuard(config, options = {}) {
|
|
281
|
+
if (typeof config === "function") {
|
|
282
|
+
return async (phase, context) => guard(await config(phase, context), options);
|
|
283
|
+
}
|
|
284
|
+
return guard(config ?? {}, options);
|
|
285
|
+
}
|
package/dist/overlay/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
export { acceptMessage, hoverMessage, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
1
|
+
export { acceptMessage, hoverMessage, isItemPath, itemLabel, itemMatches, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
2
2
|
export type { AdminMessage, MessageLike, PatchField, SiteMessage, TaggedBox } from "./protocol.js";
|
|
3
|
+
export { acceptLiveFields, canonicalHtml, findDateFormat, formatDateAs, parseHtml, preparedHtml, richTextHtml, safeHref, safeImageUrl, sanitizeHtml, swapDatetime, swapImageUrl, FIELDS_MAX_FIELDS, FIELDS_MAX_LENGTH, } from "./live-fields.js";
|
|
4
|
+
export { renderMarkdown } from "./markdown.js";
|
|
5
|
+
export type { HtmlElement, HtmlNode, LiveField, LiveImage, LiveKind } from "./live-fields.js";
|
|
3
6
|
export interface OverlayOptions {
|
|
4
7
|
/**
|
|
5
8
|
* The Capa admin origins allowed to drive this page, for example
|