@crombolo/server 0.48.0 → 0.49.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/server",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The crombolo HTTP server: routing, configuration, media stores and background jobs.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -31,18 +31,18 @@
31
31
  "access": "public"
32
32
  },
33
33
  "dependencies": {
34
- "@crombolo/api-delivery": "0.48.0",
35
- "@crombolo/api-management": "0.48.0",
36
- "@crombolo/auth": "0.48.0",
37
- "@crombolo/backoffice-host": "0.48.0",
38
- "@crombolo/common": "0.48.0",
39
- "@crombolo/content": "0.48.0",
40
- "@crombolo/contracts": "0.48.0",
41
- "@crombolo/core": "0.48.0",
42
- "@crombolo/data": "0.48.0",
43
- "@crombolo/render": "0.48.0",
44
- "@crombolo/schema": "0.48.0",
45
- "@crombolo/transfer": "0.48.0",
34
+ "@crombolo/api-delivery": "0.49.0",
35
+ "@crombolo/api-management": "0.49.0",
36
+ "@crombolo/auth": "0.49.0",
37
+ "@crombolo/backoffice-host": "0.49.0",
38
+ "@crombolo/common": "0.49.0",
39
+ "@crombolo/content": "0.49.0",
40
+ "@crombolo/contracts": "0.49.0",
41
+ "@crombolo/core": "0.49.0",
42
+ "@crombolo/data": "0.49.0",
43
+ "@crombolo/render": "0.49.0",
44
+ "@crombolo/schema": "0.49.0",
45
+ "@crombolo/transfer": "0.49.0",
46
46
  "@logtape/logtape": "^2.3.8",
47
47
  "@opentelemetry/api": "1.10.0",
48
48
  "@opentelemetry/context-async-hooks": "3.0.0",
package/src/components.ts CHANGED
@@ -83,6 +83,8 @@ export interface ComponentCheckReport {
83
83
  problems: ComponentProblem[]
84
84
  /** Whether types were checked, and what the compiler said. */
85
85
  types: { checked: boolean; problems: string[]; note?: string }
86
+ /** The gaps marked in the templates: work left, never a problem. */
87
+ gaps: ComponentGap[]
86
88
  }
87
89
 
88
90
  /**
@@ -182,9 +184,108 @@ export async function checkComponents(options: {
182
184
  declared: declared.filter((alias) => present.has(alias)),
183
185
  problems,
184
186
  types: await checkTypes(options.siteDir),
187
+ gaps: findGaps(options.componentsDir, options.siteDir),
185
188
  }
186
189
  }
187
190
 
191
+ /** A `<Gap>` in a file. A reason or `was` worked out in code cannot be read, so is null. */
192
+ export interface GapMark {
193
+ line: number
194
+ reason: string | null
195
+ was: string | null
196
+ }
197
+
198
+ export interface ComponentGap extends GapMark {
199
+ /** Relative to the site, as a problem's is. */
200
+ file: string
201
+ }
202
+
203
+ const PACKAGE_IMPORT = /import\s*\{([^}]*)\}\s*from\s*['"]@crombolo\/crombolo['"]/g
204
+ const COMMENT = /\/\*[\s\S]*?\*\/|^[ \t]*\/\/.*$/gm
205
+ const ENTITIES: Record<string, string> = { quot: '"', amp: '&', lt: '<', gt: '>', apos: "'" }
206
+
207
+ const decodeEntities = (text: string) =>
208
+ text.replace(/&(?:#(\d+)|#x([\da-f]+)|(\w+));/gi, (entity, decimal, hex, name) => {
209
+ if (decimal) return String.fromCodePoint(Number(decimal))
210
+ if (hex) return String.fromCodePoint(Number.parseInt(hex, 16))
211
+ return ENTITIES[name] ?? entity
212
+ })
213
+
214
+ /** What a site calls `Gap` here: undefined when it comes from anywhere but crombolo. */
215
+ function gapName(source: string): string | undefined {
216
+ for (const match of source.matchAll(PACKAGE_IMPORT))
217
+ for (const part of (match[1] as string).split(',')) {
218
+ const name = /^\s*Gap(?:\s+as\s+([\w$]+))?\s*$/.exec(part)
219
+ if (name) return name[1] ?? 'Gap'
220
+ }
221
+ return undefined
222
+ }
223
+
224
+ /** The index just past the brace that closes the one at `from`, strings skipped. */
225
+ function closingBrace(source: string, from: number): number {
226
+ let depth = 0
227
+ for (let at = from; at < source.length; at++) {
228
+ const c = source[at] as string
229
+ if (c === '"' || c === "'" || c === '`') {
230
+ for (at++; at < source.length && source[at] !== c; at++) if (source[at] === '\\') at++
231
+ } else if (c === '{') depth += 1
232
+ else if (c === '}' && --depth === 0) return at + 1
233
+ }
234
+ return source.length
235
+ }
236
+
237
+ /** The string in an attribute's `{…}`, when it is only a string. */
238
+ function literal(expression: string): string | null {
239
+ const string = /^\s*(["'`])((?:\\[\s\S]|(?!\1)[^\\])*)\1\s*$/.exec(expression)
240
+ if (!string || (string[1] === '`' && (string[2] as string).includes('${'))) return null
241
+ return (string[2] as string).replace(/\\([\s\S])/g, (_, c) => (c === 'n' ? '\n' : c))
242
+ }
243
+
244
+ /** `reason` and `was` from the tag whose attributes start at `from`. */
245
+ function gapAttributes(source: string, from: number): Pick<GapMark, 'reason' | 'was'> {
246
+ const found: Record<string, string | null> = {}
247
+ let at = from
248
+ while (at < source.length) {
249
+ const rest = /^\s*(?:([\w-]+)\s*(=\s*)?)?/.exec(source.slice(at)) as RegExpExecArray
250
+ at += rest[0].length
251
+ const quote = source[at]
252
+ if (rest[2] && (quote === '"' || quote === "'")) {
253
+ const end = source.indexOf(quote, at + 1)
254
+ if (end < 0) break
255
+ found[rest[1] as string] = decodeEntities(source.slice(at + 1, end))
256
+ at = end + 1
257
+ } else if (quote === '{') {
258
+ const end = closingBrace(source, at)
259
+ if (rest[2]) found[rest[1] as string] = literal(source.slice(at + 1, end - 1))
260
+ at = end
261
+ } else if (!rest[1]) break
262
+ }
263
+ return { reason: found.reason ?? null, was: found.was ?? null }
264
+ }
265
+
266
+ /** Every `<Gap>` in a file's source, read rather than compiled. Pure. */
267
+ export function gapsIn(source: string): GapMark[] {
268
+ const name = gapName(source)
269
+ if (!name) return []
270
+ // Blanked rather than removed, so what is left keeps its lines.
271
+ const code = source.replace(COMMENT, (comment) => comment.replace(/[^\n]/g, ' '))
272
+ const tag = new RegExp(`<${name.replace(/\$/g, '\\$')}(?=[\\s/>])`, 'g')
273
+ return [...code.matchAll(tag)].map((match) => ({
274
+ line: code.slice(0, match.index).split('\n').length,
275
+ ...gapAttributes(code, match.index + match[0].length),
276
+ }))
277
+ }
278
+
279
+ /** The gaps marked across `components/`, without compiling anything. */
280
+ export function findGaps(componentsDir: string, siteDir: string): ComponentGap[] {
281
+ return listComponents(componentsDir).flatMap((component) =>
282
+ gapsIn(readFileSync(component.path, 'utf8')).map((gap) => ({
283
+ file: relative(siteDir, component.path),
284
+ ...gap,
285
+ })),
286
+ )
287
+ }
288
+
188
289
  const MEDIA_CALL = /(\bawait\s+)?((?:[A-Za-z_$][\w$]*)(?:\??\.[A-Za-z_$][\w$]*)*)\.media\(/g
189
290
 
190
291
  /** The index just past the parenthesis that closes the one opened before `from`. */
package/src/config.ts CHANGED
@@ -1086,7 +1086,7 @@ export function describeDatabase(
1086
1086
  }
1087
1087
  }
1088
1088
 
1089
- export const VERSION = '0.48.0'
1089
+ export const VERSION = '0.49.0'
1090
1090
 
1091
1091
  /** A whole number of seconds from the environment; undefined when unset or not one. */
1092
1092
  /** A positive number from a setting, else the default. */
package/src/remote.ts CHANGED
@@ -83,7 +83,8 @@ export interface RemoteOptions {
83
83
  * Told once a push has landed, so this node's published cache is rebuilt: the
84
84
  * import's cache instruction carries this node's id, and a node skips its own.
85
85
  */
86
- onApplied?(): void
86
+ /** After a push, an apply or a revert: awaited, so the answer comes after what it changed renders. */
87
+ onApplied?(): void | Promise<void>
87
88
  /** A new server from the supervising `crombolo start`, once it is healthy (story 241). */
88
89
  restart?: () => Promise<RestartOutcome>
89
90
  /** Installed bundles (story 241); absent where this node does not manage them. */
@@ -523,7 +524,7 @@ export function createRemote(options: RemoteOptions): Remote {
523
524
  })
524
525
  return
525
526
  }
526
- options.onApplied?.()
527
+ await options.onApplied?.()
527
528
  await jobs.update(job.id, {
528
529
  state: 'done',
529
530
  stage:
@@ -790,7 +791,7 @@ export function createRemote(options: RemoteOptions): Remote {
790
791
  'RemoteBundleApplied',
791
792
  `${result.bundle.id} as run ${result.runId}${structural ? ', with structure' : ''}; ${describeProvenance(provenance)}`,
792
793
  )
793
- options.onApplied?.()
794
+ await options.onApplied?.()
794
795
  }
795
796
  return Response.json(
796
797
  {
@@ -996,7 +997,7 @@ export function createRemote(options: RemoteOptions): Remote {
996
997
  })
997
998
  const files = await revertSections(db, config, runId)
998
999
  await audit(principal, request, 'RemoteBundleReverted', `${runId} as run ${result.runId}`)
999
- options.onApplied?.()
1000
+ await options.onApplied?.()
1000
1001
  return Response.json({
1001
1002
  runId: result.runId,
1002
1003
  reverted: runId,
package/src/server.ts CHANGED
@@ -819,9 +819,19 @@ export async function createServer(
819
819
  authenticate,
820
820
  bundles: bundleAdmin,
821
821
  restart: serverOptions.restart,
822
- onApplied: () => {
822
+ onApplied: async () => {
823
823
  cache.invalidate()
824
824
  mediaCache.clear()
825
+ // A push or an apply may have replaced views: a new snapshot here, as an
826
+ // edit in the back office takes one, and every other node told.
827
+ const hash = await snapshots.refresh()
828
+ await appendCacheInstruction(db, {
829
+ kind: 'components',
830
+ payload: hash ? { hash } : {},
831
+ by: schema.nodeId,
832
+ }).catch((error: unknown) => {
833
+ renderLog.error('Could not announce the views a push changed: {error}', { error })
834
+ })
825
835
  },
826
836
  })
827
837
  const members = createMemberAuth(db, config, {
@@ -10,7 +10,8 @@
10
10
  * Checked at boot because the alternative is every render failing with a message
11
11
  * that names the runtime rather than the configuration that moved it.
12
12
  */
13
- import { mkdirSync } from 'node:fs'
13
+ import { existsSync, mkdirSync } from 'node:fs'
14
+ import { dirname, join, resolve } from 'node:path'
14
15
 
15
16
  export const JSX_RUNTIME = '@crombolo/crombolo/jsx-runtime'
16
17
 
@@ -28,9 +29,20 @@ export class ViewRuntimeUnreachableError extends Error {
28
29
  }
29
30
  }
30
31
 
32
+ /** Whether `dir` or a folder above it holds a node_modules. */
33
+ function insideNodeModules(dir: string): boolean {
34
+ for (let at = resolve(dir); ; at = dirname(at)) {
35
+ if (existsSync(join(at, 'node_modules'))) return true
36
+ if (dirname(at) === at) return false
37
+ }
38
+ }
39
+
31
40
  /** Throws unless a `.tsx` placed in `dir` could resolve the JSX runtime. */
32
41
  export function assertViewRuntime(dir: string): void {
33
42
  mkdirSync(dir, { recursive: true })
43
+ // With no node_modules at or above it, Bun installs a package it cannot find,
44
+ // and the resolve blocks on the registry: `crombolo status` is read-only.
45
+ if (!insideNodeModules(dir)) throw new ViewRuntimeUnreachableError(dir, 'no node_modules there')
34
46
  try {
35
47
  Bun.resolveSync(JSX_RUNTIME, dir)
36
48
  } catch (error) {