@foliwake/next 0.10.0 → 0.10.2

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 CHANGED
@@ -7,6 +7,36 @@ minor release may break; breaking changes are marked **BREAKING**.
7
7
 
8
8
  Entries are written from the changesets of each pull request (`.changeset/`, CONTRIBUTING.md), newest first.
9
9
 
10
+ ## 0.10.2
11
+
12
+ ### Patch Changes
13
+
14
+ - 964b5e5: Links point to the public documentation on foliwake.org (`docs.foliwake.org`, `sdk.foliwake.org`); the config
15
+ schemas' `$id` is under `https://foliwake.org/schemas/`.
16
+ - Updated dependencies [964b5e5]
17
+ - @foliwake/blocks-contract@0.10.2
18
+ - @foliwake/react@0.10.2
19
+ - @foliwake/server@0.10.2
20
+
21
+ ## 0.10.1
22
+
23
+ ### Patch Changes
24
+
25
+ - 44822ad: The hook trims leading and trailing whitespace and newlines from the secret read from `secretEnv` and
26
+ `previousSecretEnv` (a secret pasted with a trailing newline used to fail every signature), warns once naming the
27
+ variable (never the value), and treats an all-whitespace value as unset. New: `hookSecretsFrom` in
28
+ `@foliwake/server/hook-endpoint`. The docs make the secret with `openssl rand -hex 32 | tr -d '\n'`.
29
+ - e81a756: `npx @foliwake/next init` turns on `cacheComponents: true` in `next.config.(ts|mjs|js)` for the same forms it
30
+ wraps in `withFoliwake` (`export default { … }` and `export default <name>` of a `const` object literal, wrapped or
31
+ not), including the file `create-next-app` writes. A second run changes nothing; `--dry-run` and `--json` report it
32
+ as its own step. Any other form (a config built elsewhere, `cacheComponents: false`) is left alone and the line to
33
+ add, `cacheComponents: true,`, is printed.
34
+ - Updated dependencies [55ffb45]
35
+ - Updated dependencies [44822ad]
36
+ - @foliwake/server@0.10.1
37
+ - @foliwake/blocks-contract@0.10.1
38
+ - @foliwake/react@0.10.1
39
+
10
40
  ## 0.10.0
11
41
 
12
42
  ### Minor Changes
package/README.md CHANGED
@@ -175,6 +175,9 @@ import { foliwakeRevalidateHandler } from '@foliwake/next/revalidate';
175
175
  export const { POST } = foliwakeRevalidateHandler({ secretEnv: 'FOLIWAKE_HOOK_SECRET' });
176
176
  ```
177
177
 
178
+ Make the secret with `openssl rand -hex 32 | tr -d '\n'`, and give the Foliwake server the same value. Surrounding
179
+ whitespace and newlines are trimmed from it (with one warning naming the variable); an all-whitespace value is unset.
180
+
178
181
  It checks the signature (within 300 s; `previousSecretEnv` names a second accepted secret while you rotate it)
179
182
  and expires `foliwake:<site>:live:page:<path>` of every changed path, or `foliwake:<site>:live` when every page
180
183
  changed (a code release, a large publication). Paths that are not pages, such as an index, get the same tag:
@@ -2,7 +2,7 @@
2
2
  // env names that are missing; edits next.config once.
3
3
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
4
4
  import path from 'node:path';
5
- import { editNextConfig, hasCacheComponents, MANUAL_STEPS } from './next-config.js';
5
+ import { addCacheComponents, CACHE_COMPONENTS_LINE, editNextConfig, MANUAL_STEPS, } from './next-config.js';
6
6
  import { GENERATED } from './templates.js';
7
7
  const ENV_NAME = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/gm;
8
8
  function read(file) {
@@ -45,26 +45,33 @@ function applyEnv(step, opts) {
45
45
  return { ...base, outcome: current === null ? 'created' : 'updated', note: missing.map(nameOf).join(', ') };
46
46
  }
47
47
  const nameOf = (line) => line.split('=')[0];
48
- function cacheNote(text) {
49
- if (hasCacheComponents(text))
50
- return undefined;
51
- return 'cacheComponents: true not found; the page route needs it (init does not turn it on for you)';
48
+ const CACHE_WHAT = 'cacheComponents: true (the page route needs it)';
49
+ function cacheStep(file, cache) {
50
+ const base = { path: file, what: CACHE_WHAT };
51
+ if (cache.kind === 'manual') {
52
+ return { ...base, outcome: 'manual', note: `${cache.reason}; add \`${CACHE_COMPONENTS_LINE}\` to the config` };
53
+ }
54
+ return { ...base, outcome: cache.kind === 'added' ? 'updated' : 'exists' };
52
55
  }
56
+ // Two results: the withFoliwake() wrap, then cacheComponents; the file is written once, when either changed it.
53
57
  function applyConfig(step, opts) {
54
58
  const file = path.join(opts.cwd, step.path);
55
59
  const base = { path: step.path, what: step.what };
56
60
  if (step.create !== null) {
57
61
  write(file, step.create, opts);
58
- return { ...base, outcome: 'created' };
62
+ return [{ ...base, outcome: 'created' }];
63
+ }
64
+ const original = readFileSync(file, 'utf8');
65
+ const wrap = editNextConfig(original, step.path);
66
+ if (wrap.kind === 'manual') {
67
+ return [{ ...base, outcome: 'manual', note: `${wrap.reason}. ${MANUAL_STEPS.join(' ')}` }];
59
68
  }
60
- const text = readFileSync(file, 'utf8');
61
- const edit = editNextConfig(text, step.path);
62
- if (edit.kind === 'present')
63
- return { ...base, outcome: 'exists', note: cacheNote(text) };
64
- if (edit.kind === 'manual')
65
- return { ...base, outcome: 'manual', note: `${edit.reason}. ${MANUAL_STEPS.join(' ')}` };
66
- write(file, edit.text, opts);
67
- return { ...base, outcome: 'updated', note: cacheNote(text) };
69
+ const wrapped = wrap.kind === 'wrapped' ? wrap.text : original;
70
+ const cache = addCacheComponents(wrapped);
71
+ const final = cache.kind === 'added' ? cache.text : wrapped;
72
+ if (final !== original)
73
+ write(file, final, opts);
74
+ return [{ ...base, outcome: wrap.kind === 'present' ? 'exists' : 'updated' }, cacheStep(step.path, cache)];
68
75
  }
69
76
  /** Applies every step in order. */
70
77
  export function applySteps(steps, opts) {
@@ -73,7 +80,7 @@ export function applySteps(steps, opts) {
73
80
  if (step.kind === 'file')
74
81
  results.push(applyFile(step, opts));
75
82
  else if (step.kind === 'next-config')
76
- results.push(applyConfig(step, opts));
83
+ results.push(...applyConfig(step, opts));
77
84
  else {
78
85
  const result = applyEnv(step, opts);
79
86
  if (result)
@@ -12,5 +12,19 @@ export type ConfigEdit = {
12
12
  export declare const MANUAL_STEPS: string[];
13
13
  /** The edit of a next.config text (an ES module). */
14
14
  export declare function editNextConfig(text: string, file: string): ConfigEdit;
15
- /** Whether the config text turns on cacheComponents (a hint only: init never turns it on silently). */
15
+ /** Whether the config text turns on cacheComponents. */
16
16
  export declare function hasCacheComponents(text: string): boolean;
17
+ /** The line init adds to the config object, and prints when it cannot. */
18
+ export declare const CACHE_COMPONENTS_LINE = "cacheComponents: true,";
19
+ /** What init does with cacheComponents. */
20
+ export type CacheEdit = {
21
+ kind: 'added';
22
+ text: string;
23
+ } | {
24
+ kind: 'present';
25
+ } | {
26
+ kind: 'manual';
27
+ reason: string;
28
+ };
29
+ /** The cacheComponents edit of a next.config text (an ES module). */
30
+ export declare function addCacheComponents(text: string): CacheEdit;
@@ -1,6 +1,7 @@
1
- // The conservative next.config edit: wrap the default export in withFoliwake() for two ESM forms only,
2
- // `export default <name>;` and `export default { … };`. Anything else (CommonJS, a function, an already wrapped
3
- // call) is left alone and init prints the three lines to add by hand.
1
+ // The conservative next.config edits, for two ESM forms only, `export default <name>;` and `export default { … };`
2
+ // (each also once wrapped in withFoliwake): wrap the default export in withFoliwake(), and turn on
3
+ // cacheComponents in the config object literal. Anything else (CommonJS, a function, a config built elsewhere) is
4
+ // left alone and init prints the lines to add by hand.
4
5
  const IMPORT = "import { withFoliwake } from '@foliwake/next/config';";
5
6
  const BY_NAME = /^export default ([A-Za-z_$][\w$]*);?[ \t]*$/m;
6
7
  const OBJECT_START = /^export default \{/m;
@@ -8,7 +9,7 @@ const OBJECT_START = /^export default \{/m;
8
9
  export const MANUAL_STEPS = [
9
10
  IMPORT,
10
11
  'Wrap the exported config: export default withFoliwake(yourConfig);',
11
- 'Keep cacheComponents: true in it (the page route needs it).',
12
+ 'Turn on cacheComponents in it (the page route needs it): cacheComponents: true,',
12
13
  ];
13
14
  function withImport(text) {
14
15
  const imports = [...text.matchAll(/^import .*;[ \t]*$/gm)];
@@ -45,7 +46,54 @@ export function editNextConfig(text, file) {
45
46
  return { kind: 'wrapped', text: withImport(wrapped) };
46
47
  return { kind: 'manual', reason: 'the default export is not a name or an object literal' };
47
48
  }
48
- /** Whether the config text turns on cacheComponents (a hint only: init never turns it on silently). */
49
+ /** Whether the config text turns on cacheComponents. */
49
50
  export function hasCacheComponents(text) {
50
51
  return /cacheComponents\s*:\s*true/.test(text);
51
52
  }
53
+ /** The line init adds to the config object, and prints when it cannot. */
54
+ export const CACHE_COMPONENTS_LINE = 'cacheComponents: true,';
55
+ const DEFAULT_OBJECT = /^export default (?:withFoliwake\()?\{/m;
56
+ const DEFAULT_NAME = /^export default (?:withFoliwake\()?([A-Za-z_$][\w$]*)\)?;?[ \t]*$/m;
57
+ function escaped(name) {
58
+ return name.replace(/\$/g, '\\$');
59
+ }
60
+ /** Where the `{` of `const <name>[: Type] = {` is, or null. */
61
+ function declaredObjectBrace(text, name) {
62
+ const declaration = new RegExp(`^(?:const|let|var) ${escaped(name)}(?:\\s*:\\s*[\\w.]+)?\\s*=\\s*\\{`, 'm');
63
+ const match = declaration.exec(text);
64
+ return match ? match.index + match[0].length - 1 : null;
65
+ }
66
+ /** Where the `{` of the exported config object literal is, or null when the export is another form. */
67
+ function configObjectBrace(text) {
68
+ const object = DEFAULT_OBJECT.exec(text);
69
+ if (object)
70
+ return object.index + object[0].length - 1;
71
+ const byName = DEFAULT_NAME.exec(text);
72
+ return byName ? declaredObjectBrace(text, byName[1]) : null;
73
+ }
74
+ /** The indentation of the first non-blank line after `at`, or two spaces. */
75
+ function indentAfter(text, at) {
76
+ const next = /\n([ \t]*)\S/.exec(text.slice(at));
77
+ return next && next[1] ? next[1] : ' ';
78
+ }
79
+ // `{\n a: 1,` gets a line of its own; `{}` becomes `{ cacheComponents: true }`; `{ a: 1 }` gets it first.
80
+ function insertAfterBrace(text, brace) {
81
+ const head = text.slice(0, brace + 1);
82
+ const rest = text.slice(brace + 1);
83
+ if (rest.startsWith('\n'))
84
+ return `${head}\n${indentAfter(text, brace)}${CACHE_COMPONENTS_LINE}${rest}`;
85
+ if (/^\s*\}/.test(rest))
86
+ return `${head} cacheComponents: true ${rest.trimStart()}`;
87
+ return `${head} ${CACHE_COMPONENTS_LINE} ${rest.trimStart()}`;
88
+ }
89
+ /** The cacheComponents edit of a next.config text (an ES module). */
90
+ export function addCacheComponents(text) {
91
+ if (hasCacheComponents(text))
92
+ return { kind: 'present' };
93
+ if (/\bcacheComponents\b/.test(text))
94
+ return { kind: 'manual', reason: 'cacheComponents is set to another value' };
95
+ const brace = configObjectBrace(text);
96
+ if (brace === null)
97
+ return { kind: 'manual', reason: 'the exported config is not an object literal init can find' };
98
+ return { kind: 'added', text: insertAfterBrace(text, brace) };
99
+ }
@@ -6,7 +6,7 @@ import { revalidateTag } from 'next/cache.js';
6
6
  import { after } from 'next/server.js';
7
7
  import { checkEnvName } from '@foliwake/server/config';
8
8
  import { readEnvelopeCachedEntry } from './cache.js';
9
- import { createHookEndpoint } from '@foliwake/server/hook-endpoint';
9
+ import { createHookEndpoint, hookSecretsFrom } from '@foliwake/server/hook-endpoint';
10
10
  import { foliwakeConfig, foliwakeTags } from './server.js';
11
11
  import { SDK } from './version.js';
12
12
  function checkOptions(opts) {
@@ -45,7 +45,7 @@ export function foliwakeRevalidateHandler(opts) {
45
45
  handler ??= createHookEndpoint({
46
46
  site: foliwakeConfig().site,
47
47
  sdk: SDK,
48
- secrets: () => [process.env[opts.secretEnv], opts.previousSecretEnv && process.env[opts.previousSecretEnv]],
48
+ secrets: hookSecretsFrom({ names: [opts.secretEnv, opts.previousSecretEnv], log: console }),
49
49
  liveTag,
50
50
  expire: expireWith(opts.mode ?? 'expire'),
51
51
  warm: (path) => readEnvelopeCachedEntry(path),
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // What the hook endpoint answers a ping with: this package and its version, stamped in by tools/build.mjs.
2
- export const SDK = '@foliwake/next/0.10.0';
2
+ export const SDK = '@foliwake/next/0.10.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foliwake/next",
3
- "version": "0.10.0",
3
+ "version": "0.10.2",
4
4
  "description": "Next.js App Router adapter for Foliwake pages: withFoliwake, cached envelope read, <FoliwakeServerPage>, preview pages, code and media route handlers, CSP header.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -10,7 +10,7 @@
10
10
  "url": "git+https://github.com/foliwake/foliwake-js.git",
11
11
  "directory": "packages/next"
12
12
  },
13
- "homepage": "https://sdk.foliwake.dev/reference/packages/next",
13
+ "homepage": "https://sdk.foliwake.org/reference/packages/next",
14
14
  "bugs": {
15
15
  "url": "https://github.com/foliwake/foliwake-js/issues"
16
16
  },
@@ -79,9 +79,9 @@
79
79
  },
80
80
  "dependencies": {
81
81
  "server-only": "0.0.1",
82
- "@foliwake/server": "0.10.0",
83
- "@foliwake/react": "0.10.0",
84
- "@foliwake/blocks-contract": "0.10.0"
82
+ "@foliwake/blocks-contract": "0.10.2",
83
+ "@foliwake/react": "0.10.2",
84
+ "@foliwake/server": "0.10.2"
85
85
  },
86
86
  "peerDependencies": {
87
87
  "next": ">=16.2.0 <17",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://foliwake.dev/schemas/remote-2.schema.json",
3
+ "$id": "https://foliwake.org/schemas/remote-2.schema.json",
4
4
  "title": "foliwake.remote.json version 2",
5
5
  "description": "Which Foliwake site an app (or `foliwake dev --remote`) reads, the NAMES of the variables holding its credentials, and the app's settings. No secrets: values are read from the environment on the server. The TypeScript parser (@foliwake/server/config) is the authority; test/packages/server-config-schema.test.js keeps both in step.",
6
6
  "type": "object",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://foliwake.dev/schemas/remote-3.schema.json",
3
+ "$id": "https://foliwake.org/schemas/remote-3.schema.json",
4
4
  "title": "foliwake.remote.json version 3",
5
5
  "description": "Optional overrides of an app configured by FOLIWAKE_URL: the site's discovery document (/.well-known/foliwake.json) gives its origins and media CDN; this file holds the app's own settings and the NAMES of the variables holding credentials. No secrets. The TypeScript parser (@foliwake/server/resolve) is the authority; test/server-config-v3.test.js keeps both in step.",
6
6
  "type": "object",