@mistriwork/engine 1.0.0-rc.15 → 1.0.0-rc.17

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.
Files changed (58) hide show
  1. package/README.md +5 -0
  2. package/dist/host.d.ts +46 -0
  3. package/dist/host.js +51 -0
  4. package/dist/server/cli/startup.js +7 -2
  5. package/dist/server/cli/usage.d.ts +2 -0
  6. package/dist/server/cli/usage.js +12 -1
  7. package/dist/server/clientHttp.js +20 -3
  8. package/dist/server/config/profile.d.ts +10 -3
  9. package/dist/server/config/profile.js +28 -4
  10. package/dist/server/connections.js +9 -0
  11. package/dist/server/http/baselineHeaders.d.ts +13 -3
  12. package/dist/server/http/baselineHeaders.js +6 -0
  13. package/dist/server/http/exportRoute.d.ts +74 -0
  14. package/dist/server/http/exportRoute.js +206 -169
  15. package/dist/server/http/frontDoor.d.ts +41 -0
  16. package/dist/server/http/frontDoor.js +36 -0
  17. package/dist/server/http/recordCompose.d.ts +3 -0
  18. package/dist/server/http/recordCompose.js +1 -0
  19. package/dist/server/http/reply.js +6 -0
  20. package/dist/server/http/serverCore.d.ts +2 -2
  21. package/dist/server/http/serverCore.js +16 -32
  22. package/dist/server/journal.d.ts +35 -6
  23. package/dist/server/journal.js +79 -8
  24. package/dist/server/journalStore.d.ts +37 -0
  25. package/dist/server/journalStore.js +352 -0
  26. package/dist/server/migrate/cli.js +8 -1
  27. package/dist/server/migrate/target.d.ts +8 -0
  28. package/dist/server/migrate/target.js +15 -0
  29. package/dist/server/seed/seed.js +46 -11
  30. package/dist/shared/appCards.d.ts +4 -0
  31. package/dist/shared/appCards.js +5 -0
  32. package/dist/shared/appListColumns.d.ts +7 -0
  33. package/dist/shared/appListColumns.js +13 -0
  34. package/dist/shared/breakdown.js +8 -2
  35. package/dist/shared/computed.d.ts +16 -2
  36. package/dist/shared/computed.js +23 -4
  37. package/dist/shared/computedValue.js +23 -0
  38. package/dist/shared/listQuestion.js +2 -2
  39. package/dist/shared/mentionLookup.js +3 -1
  40. package/dist/shared/page.js +23 -5
  41. package/dist/shared/pageAnatomy.js +2 -0
  42. package/dist/shared/pageConfig.d.ts +1 -1
  43. package/dist/shared/pageConfig.js +68 -18
  44. package/dist/shared/pageModel.d.ts +42 -5
  45. package/dist/shared/pagePaths.js +4 -1
  46. package/dist/shared/pagePlans.d.ts +2 -2
  47. package/dist/shared/pagePlans.js +3 -3
  48. package/dist/shared/recordLabel.d.ts +12 -3
  49. package/dist/shared/recordLabel.js +39 -18
  50. package/dist/shared/recordReads.d.ts +6 -30
  51. package/dist/shared/recordReads.js +32 -2
  52. package/dist/shared/rollups.d.ts +4 -4
  53. package/dist/shared/rollups.js +1 -1
  54. package/dist/shared/schema.d.ts +7 -0
  55. package/dist/shared/schema.js +17 -0
  56. package/dist/shared/servedApp.d.ts +1 -1
  57. package/dist/shared/viewLabels.js +22 -17
  58. package/package.json +5 -2
package/README.md CHANGED
@@ -16,3 +16,8 @@ This package is internal plumbing for `mistri` and `tablewalk`, with no semver
16
16
  promise to an App's author: its `exports` name each module those two import,
17
17
  and any of them may change or go in any release. An App imports `mistri`'s
18
18
  names (or `tablewalk`'s), never these.
19
+
20
+ `@mistriwork/engine/host` gathers what the database browser opens a database
21
+ through — connect, the catalog, reads, the recorded writes and their Undo
22
+ journal, and what its agent tools need beside them — into one entry. It is
23
+ Preview like the rest, until the browser has moved onto it.
package/dist/host.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * `@mistriwork/engine/host`: what the database browser opens a database and
3
+ * works with it through, as one entry point instead of the engine's own files.
4
+ *
5
+ * It holds exactly the names the browser's server modules take from the
6
+ * engine for five things, and nothing it does not use:
7
+ *
8
+ * - connect: a connection string becomes an adapter (`connect`), or a set of
9
+ * configured connections a `Registry` opens on demand, with the config file
10
+ * and the secrets it names kept out of what is said;
11
+ * - schema: the catalog the adapter reads, and the helpers that walk it;
12
+ * - reads: a query-language read, a saved view, a count and a breakdown, with
13
+ * the parser and the explainer an agent is shown;
14
+ * - writes: the recorded insert, update and delete the browser's write mode
15
+ * and its agent tools make, what a delete would reach, why a write failed,
16
+ * the write journal behind Undo and revert, and its store's provisioning;
17
+ * - the agent tools' support: cancellation and the statement deadline, the
18
+ * schema in words, and the nearest name to a wrong one.
19
+ *
20
+ * Preview, as the rest of the engine is: it is promoted once the browser has
21
+ * moved onto it, and a name is added here when the browser needs it, not
22
+ * because it is exported below. The serving, the CLI and the HTTP plumbing
23
+ * stay on the engine's deeper paths. test/engine-host-contract.test.ts
24
+ * imports this entry alone and does with it what the browser does.
25
+ */
26
+ export { connect } from './adapters/connect.js';
27
+ export { UNTRUSTED_NETWORK } from './adapters/net/untrustedDatabase.js';
28
+ export { Registry, redact, saveConfig, scrubSecrets, storeSecret } from './server/connections.js';
29
+ export { resolveConnection } from './server/auth/credentials.js';
30
+ export { Refusal, TransactionOutcomeUnknown, clampLimit, type Adapter, type Filter, type InsertResult, type UpdateRequest, type WriteTransaction, } from './adapters/adapter.js';
31
+ export { findTable, labelPath, primaryKey, referencesFrom, referencesTo, renderDDL, tableNamed, type DatabaseSchema, type DeleteRule, type ForeignKey, type TableMeta, } from './shared/schema.js';
32
+ export { toMarkdown } from './shared/schematext.js';
33
+ export { describeSchema } from './shared/shape.js';
34
+ export { readBreakdown, readCount, readRecords, readView } from './server/resourceReadOperations.js';
35
+ export { explain, parseQuery, quoteValue } from './shared/query.js';
36
+ export { textToView } from './shared/viewtext.js';
37
+ export { looksLikeBreakdown, textToBreakdown } from './shared/breakdowntext.js';
38
+ export type { ViewDef } from './shared/view.js';
39
+ export { changingReferences, recordedDelete, recordedInsert, recordedUpdate } from './server/recordWriteOperations.js';
40
+ export { deleteImpact, inBatches } from './server/impact.js';
41
+ export { explainWriteFailure, writeFailureOutcome } from './server/constraints.js';
42
+ export { attributeAction, attributeWrites } from './adapters/writeAttribution.js';
43
+ export { history, lastRevertible, latestRevertible, recordWrite, revert, summarise } from './server/journal.js';
44
+ export { provisionPostgresWriteJournal, provisionSqliteWriteJournal } from './server/journalStore.js';
45
+ export { STATEMENT_TIMEOUT_MS, StatementStopped, stoppedReason, throwIfCancelled, withCancellation } from './adapters/statementControl.js';
46
+ export { distance } from './shared/suggest.js';
package/dist/host.js ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * `@mistriwork/engine/host`: what the database browser opens a database and
3
+ * works with it through, as one entry point instead of the engine's own files.
4
+ *
5
+ * It holds exactly the names the browser's server modules take from the
6
+ * engine for five things, and nothing it does not use:
7
+ *
8
+ * - connect: a connection string becomes an adapter (`connect`), or a set of
9
+ * configured connections a `Registry` opens on demand, with the config file
10
+ * and the secrets it names kept out of what is said;
11
+ * - schema: the catalog the adapter reads, and the helpers that walk it;
12
+ * - reads: a query-language read, a saved view, a count and a breakdown, with
13
+ * the parser and the explainer an agent is shown;
14
+ * - writes: the recorded insert, update and delete the browser's write mode
15
+ * and its agent tools make, what a delete would reach, why a write failed,
16
+ * the write journal behind Undo and revert, and its store's provisioning;
17
+ * - the agent tools' support: cancellation and the statement deadline, the
18
+ * schema in words, and the nearest name to a wrong one.
19
+ *
20
+ * Preview, as the rest of the engine is: it is promoted once the browser has
21
+ * moved onto it, and a name is added here when the browser needs it, not
22
+ * because it is exported below. The serving, the CLI and the HTTP plumbing
23
+ * stay on the engine's deeper paths. test/engine-host-contract.test.ts
24
+ * imports this entry alone and does with it what the browser does.
25
+ */
26
+ /* ---------- connect ---------- */
27
+ export { connect } from './adapters/connect.js';
28
+ export { UNTRUSTED_NETWORK } from './adapters/net/untrustedDatabase.js';
29
+ export { Registry, redact, saveConfig, scrubSecrets, storeSecret } from './server/connections.js';
30
+ export { resolveConnection } from './server/auth/credentials.js';
31
+ /* ---------- the adapter ---------- */
32
+ export { Refusal, TransactionOutcomeUnknown, clampLimit, } from './adapters/adapter.js';
33
+ /* ---------- schema ---------- */
34
+ export { findTable, labelPath, primaryKey, referencesFrom, referencesTo, renderDDL, tableNamed, } from './shared/schema.js';
35
+ export { toMarkdown } from './shared/schematext.js';
36
+ export { describeSchema } from './shared/shape.js';
37
+ /* ---------- reads ---------- */
38
+ export { readBreakdown, readCount, readRecords, readView } from './server/resourceReadOperations.js';
39
+ export { explain, parseQuery, quoteValue } from './shared/query.js';
40
+ export { textToView } from './shared/viewtext.js';
41
+ export { looksLikeBreakdown, textToBreakdown } from './shared/breakdowntext.js';
42
+ /* ---------- writes and the write journal ---------- */
43
+ export { changingReferences, recordedDelete, recordedInsert, recordedUpdate } from './server/recordWriteOperations.js';
44
+ export { deleteImpact, inBatches } from './server/impact.js';
45
+ export { explainWriteFailure, writeFailureOutcome } from './server/constraints.js';
46
+ export { attributeAction, attributeWrites } from './adapters/writeAttribution.js';
47
+ export { history, lastRevertible, latestRevertible, recordWrite, revert, summarise } from './server/journal.js';
48
+ export { provisionPostgresWriteJournal, provisionSqliteWriteJournal } from './server/journalStore.js';
49
+ /* ---------- the agent tools' support ---------- */
50
+ export { STATEMENT_TIMEOUT_MS, StatementStopped, stoppedReason, throwIfCancelled, withCancellation } from './adapters/statementControl.js';
51
+ export { distance } from './shared/suggest.js';
@@ -10,7 +10,7 @@ import { configureServerSettings } from '../../adapters/serverSettings.js';
10
10
  import { configurePostgresPools } from '../../adapters/postgresPool.js';
11
11
  import { configureLockTimeout, configureWriterPool } from '../../adapters/writeLanes.js';
12
12
  import { postgresPoolFlags, shutdownTimings } from './args.js';
13
- import { didYouMean, packageVersion } from './usage.js';
13
+ import { commandsTaking, didYouMean, packageVersion } from './usage.js';
14
14
  import { envName } from '../product.js';
15
15
  /**
16
16
  * `--help`, `--version`, a retired spelling, an unknown flag, a command one slip away: answered before anything
@@ -39,7 +39,12 @@ export async function answeredFirst(opts, product) {
39
39
  line that said what was wrong. */
40
40
  if (opts.unknown) {
41
41
  const flag = opts.unknown.split(/[ =]/)[0];
42
- console.error(`${bin}: unknown option "${opts.unknown}".${opts.unknown === flag ? await didYouMean(flag, product.flagWords) : ''} ${bin} --help lists the options.`);
42
+ /* A flag the usage does spell, typed where it does not apply: say where it does, not that it is unknown. A flag
43
+ given twice ("--jobs (may be supplied only once)") applies here; it is said as it is. */
44
+ const homes = product.flagWords.includes(flag) && !opts.unknown.includes('(') ? commandsTaking(product.usage, bin, flag) : [];
45
+ console.error(homes.length
46
+ ? `${bin}: ${flag} is an option of ${homes.map((home) => `${bin} ${home}`).join(' and ')}, not of a start: name the command first (${bin} ${homes[0]} … ${flag}). ${bin} --help lists the options.`
47
+ : `${bin}: unknown option "${opts.unknown}".${opts.unknown === flag ? await didYouMean(flag, product.flagWords) : ''} ${bin} --help lists the options.`);
43
48
  process.exit(1);
44
49
  }
45
50
  /* A word that is no file and is one slip from a command (`tablewalk chek`) was opened as a SQLite file of that
@@ -14,3 +14,5 @@ export declare function didYouMean(said: string, among: readonly string[]): Prom
14
14
  * else the package's. Every package of the workspace carries the one version.
15
15
  */
16
16
  export declare function packageVersion(): Promise<string>;
17
+ /** The commands whose usage spells `flag` (`mistri schema --brief`), for a flag typed where no command takes it. */
18
+ export declare function commandsTaking(usage: string, bin: string, flag: string): string[];
@@ -15,7 +15,8 @@ export function flagWordsOf(usage) {
15
15
  /** What was probably meant, said the way a person would type it, or nothing when nothing is near. */
16
16
  export async function didYouMean(said, among) {
17
17
  const { distance, nearest } = await import('../../shared/suggest.js');
18
- const near = nearest(said, among);
18
+ /* Never the spelling that was typed: "Did you mean --brief?" to --brief is no help. */
19
+ const near = nearest(said, among.filter((word) => word !== said));
19
20
  return near !== undefined && distance(said.toLowerCase(), near.toLowerCase()) <= Math.max(1, Math.floor(said.length / 3))
20
21
  ? ` Did you mean ${near}?`
21
22
  : '';
@@ -36,3 +37,13 @@ export async function packageVersion() {
36
37
  return '0.0.0';
37
38
  }
38
39
  }
40
+ /** The commands whose usage spells `flag` (`mistri schema --brief`), for a flag typed where no command takes it. */
41
+ export function commandsTaking(usage, bin, flag) {
42
+ const out = new Set();
43
+ for (const line of usage.split('\n')) {
44
+ const command = new RegExp(`^ {2}${bin} ([a-z][a-z-]*)(?=\\s|$)`).exec(line)?.[1];
45
+ if (command && new RegExp(`(?<![\\w-])${flag}(?![\\w-])`).test(line))
46
+ out.add(command);
47
+ }
48
+ return [...out];
49
+ }
@@ -9,6 +9,7 @@ import { headingWeight, inheritThemePreset, lookNavRole } from '../shared/themeP
9
9
  import { typeTokens } from '../shared/themeType.js';
10
10
  import { fieldEdge } from '../shared/themeColor.js';
11
11
  import { recipeAttributes } from '../shared/themeRecipes.js';
12
+ import { APP_PAGE_PERMISSIONS_POLICY } from './http/baselineHeaders.js';
12
13
  const MIME = {
13
14
  '.html': 'text/html; charset=utf-8',
14
15
  '.js': 'text/javascript; charset=utf-8',
@@ -112,6 +113,11 @@ function withFrameAncestors(policy, origins) {
112
113
  return policy;
113
114
  return policy.replace("frame-ancestors 'self'", ["frame-ancestors 'self'", ...origins].join(' '));
114
115
  }
116
+ /**
117
+ * An App's page may use the camera (the scan input); the database browser's
118
+ * has no such input and keeps the baseline's refusal (http/baselineHeaders.ts).
119
+ */
120
+ const pagePermissions = (shell) => shell?.workbench ? {} : { 'permissions-policy': APP_PAGE_PERMISSIONS_POLICY };
115
121
  /** A page's whole policy: its own, the served Apps' egress, and who may frame it. */
116
122
  const pagePolicy = (policy, shell) => shell?.unframed
117
123
  ? withEgress(policy, shell.egress).replace("frame-ancestors 'self'", "frame-ancestors 'none'")
@@ -451,6 +457,7 @@ export async function serveClient(files, req, res, shell) {
451
457
  ...(data || shell?.branding?.private ? { vary: 'Cookie' } : {}),
452
458
  'x-content-type-options': 'nosniff',
453
459
  'content-security-policy': pagePolicy(shellPolicy(html, { fromSource: true, socket: vite.socket }), shell),
460
+ ...pagePermissions(shell),
454
461
  });
455
462
  res.end(head ? undefined : html);
456
463
  }
@@ -621,7 +628,12 @@ export function builtClient(dir) {
621
628
  'x-content-type-options': 'nosniff',
622
629
  'content-type': asset.type,
623
630
  vary: asset.variants.length > 1 ? 'Accept-Encoding, Cookie' : 'Cookie',
624
- ...(asset.policy ? { 'content-security-policy': pagePolicy(shell.mount?.base ? shellPolicy(text) : asset.policy, shell) } : {}),
631
+ ...(asset.policy
632
+ ? {
633
+ 'content-security-policy': pagePolicy(shell.mount?.base ? shellPolicy(text) : asset.policy, shell),
634
+ ...pagePermissions(shell),
635
+ }
636
+ : {}),
625
637
  };
626
638
  if (head) {
627
639
  res.writeHead(200, headers).end();
@@ -645,7 +657,12 @@ export function builtClient(dir) {
645
657
  'cache-control': privately ? PERSONAL : 'no-cache',
646
658
  ...(!privately ? { etag: strongTag(body) } : {}),
647
659
  'x-content-type-options': 'nosniff',
648
- ...(asset.policy ? { 'content-security-policy': pagePolicy(shell?.mount?.base ? shellPolicy(text) : asset.policy, shell) } : {}),
660
+ ...(asset.policy
661
+ ? {
662
+ 'content-security-policy': pagePolicy(shell?.mount?.base ? shellPolicy(text) : asset.policy, shell),
663
+ ...pagePermissions(shell),
664
+ }
665
+ : {}),
649
666
  ...(asset.variants.length > 1 || privately
650
667
  ? { vary: [asset.variants.length > 1 ? 'Accept-Encoding' : '', privately ? 'Cookie' : ''].filter(Boolean).join(', ') }
651
668
  : {}),
@@ -669,7 +686,7 @@ export function builtClient(dir) {
669
686
  'cache-control': asset.cache,
670
687
  etag: variant.etag,
671
688
  'x-content-type-options': 'nosniff',
672
- ...(asset.policy ? { 'content-security-policy': pagePolicy(asset.policy, shell) } : {}),
689
+ ...(asset.policy ? { 'content-security-policy': pagePolicy(asset.policy, shell), ...pagePermissions(shell) } : {}),
673
690
  };
674
691
  if (asset.variants.length > 1)
675
692
  headers.vary = 'Accept-Encoding';
@@ -27,7 +27,7 @@ interface ProjectContext {
27
27
  };
28
28
  readonly app?: {
29
29
  readonly path: string;
30
- readonly from: 'flag' | 'profile';
30
+ readonly from: 'flag' | 'profile' | 'cwd';
31
31
  };
32
32
  readonly entries: Partial<Record<ProfileEntry, {
33
33
  readonly path: string;
@@ -42,13 +42,20 @@ export declare class ProfileRefusal extends Error {
42
42
  /** What an App command takes of the profile: the App, and which entries. */
43
43
  export interface ProfileTakes {
44
44
  readonly entries: readonly ProfileEntry[];
45
- /** `dev` also takes the App as its first word. */
46
- readonly positional?: boolean;
45
+ /** Without `--app` or a profile's `dir`, the working directory is the App when it holds an App entry. */
46
+ readonly cwdApp?: boolean;
47
47
  /** The command prints the context in its own output and `--json` (check, migrate); another says it on stderr. */
48
48
  readonly says?: boolean;
49
49
  /** False where the command reads no App (`migrate diff` reads schema.sql and the config's connection). */
50
50
  readonly app?: boolean;
51
51
  }
52
+ /** Whether a directory holds an App entry. */
53
+ export declare const holdsApp: (dir: string) => boolean;
54
+ /**
55
+ * A word typed where a database, a route or a target goes that is an App's directory: the App is named with
56
+ * `--app`, never by position, so it is refused rather than read as one. Undefined when the word is anything else.
57
+ */
58
+ export declare function appDirectoryWord(command: string, word: string | undefined): string | undefined;
52
59
  /**
53
60
  * The config file a run reads, by the one order: `--config`, the App's own, the working directory's, the person's
54
61
  * own. Undefined when there is none (a run on a database named by `--db`).
@@ -15,7 +15,7 @@
15
15
  * The block takes paths and nothing else: a `${NAME}` reference, a key it does not know, or a file that is not
16
16
  * there is refused by name. It is not a second App definition: it names modules and defines nothing.
17
17
  */
18
- import { existsSync, readFileSync } from 'node:fs';
18
+ import { existsSync, readFileSync, statSync } from 'node:fs';
19
19
  import { homedir } from 'node:os';
20
20
  import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
21
21
  import { configFileName, product } from '../product.js';
@@ -25,6 +25,26 @@ const PROFILE_KEYS = ['dir', ...Object.keys(PROFILE_ENTRIES)];
25
25
  /** A profile that cannot be used: refused before anything is read. */
26
26
  export class ProfileRefusal extends Error {
27
27
  }
28
+ /** The files an App's directory is known by: the entry `loadApp` reads. */
29
+ const APP_ENTRY_FILES = ['app.ts', 'app.mjs', 'app.json'];
30
+ /** Whether a directory holds an App entry. */
31
+ export const holdsApp = (dir) => APP_ENTRY_FILES.some((name) => existsSync(join(dir, name)));
32
+ /**
33
+ * A word typed where a database, a route or a target goes that is an App's directory: the App is named with
34
+ * `--app`, never by position, so it is refused rather than read as one. Undefined when the word is anything else.
35
+ */
36
+ export function appDirectoryWord(command, word) {
37
+ if (word === undefined || word.startsWith('-'))
38
+ return undefined;
39
+ try {
40
+ if (!statSync(word).isDirectory() || !holdsApp(word))
41
+ return undefined;
42
+ }
43
+ catch {
44
+ return undefined;
45
+ }
46
+ return `${word} is an App's directory, and an App is named with --app: ${command} --app ${word}`;
47
+ }
28
48
  const shown = (path) => {
29
49
  const near = relative(process.cwd(), path);
30
50
  return !near ? '.' : near.startsWith('..') || isAbsolute(near) ? path : near;
@@ -117,8 +137,7 @@ export function applyProfile(argv, takes, env = process.env) {
117
137
  /* A suite's Apps are the suite's to name (`migrate stores up --suite`): the profile's one App is not added to it. */
118
138
  const suite = out.includes('--suite');
119
139
  const apps = flagged(out, '--app');
120
- const positional = takes.positional && out[0] !== undefined && !out[0].startsWith('-') ? out[0] : undefined;
121
- const named = apps.length === 1 ? apps[0] : positional;
140
+ const named = apps.length === 1 ? apps[0] : undefined;
122
141
  const explicit = flagged(out, '--config').at(-1);
123
142
  const naming = out.includes('--db') || out.includes('-d');
124
143
  const found = naming && explicit === undefined
@@ -139,6 +158,11 @@ export function applyProfile(argv, takes, env = process.env) {
139
158
  app = { path: profile.dir, from: 'profile' };
140
159
  out.push('--app', shown(profile.dir));
141
160
  }
161
+ else if (apps.length === 0 && takes.cwdApp && !suite && takes.app !== false && holdsApp('.')) {
162
+ /* The one rule: no --app and no profile, the App is the directory the person is in, when it is one. */
163
+ app = { path: resolve('.'), from: 'cwd' };
164
+ out.push('--app', '.');
165
+ }
142
166
  const entries = {};
143
167
  if (apps.length <= 1 && !suite && takes.app !== false)
144
168
  for (const entry of takes.entries) {
@@ -202,7 +226,7 @@ export function contextLines(context, more = {}) {
202
226
  const lines = [`Context: ${where}`];
203
227
  const row = (key, path, from) => lines.push(` ${key.padEnd(9)} ${(path ?? '—').padEnd(13)} ${from}`);
204
228
  if (context.app)
205
- row('app', shown(context.app.path), context.app.from === 'profile' ? 'mistri.json' : over(context, 'dir', 'flag'));
229
+ row('app', shown(context.app.path), context.app.from === 'profile' ? 'mistri.json' : context.app.from === 'cwd' ? 'the working directory' : over(context, 'dir', 'flag'));
206
230
  for (const entry of ['commands', 'jobs', 'policy', 'entry']) {
207
231
  const own = context.entries[entry] ?? more[entry];
208
232
  if (!own) {
@@ -35,6 +35,8 @@ import { listenOf, poolOf, replicaEngine, replicasOf } from './poolConfig.js';
35
35
  import { slug } from '../shared/slug.js';
36
36
  import { sqliteTargetFrom } from './configPaths.js';
37
37
  import { product } from './product.js';
38
+ import { flushJournal } from './journal.js';
39
+ import { keepWriteJournal } from './journalStore.js';
38
40
  export { sqliteTargetFrom } from './configPaths.js';
39
41
  export class Registry {
40
42
  /** The environment this registry's references resolve in: an App's own in a suite, else the process's (undefined). */
@@ -484,6 +486,11 @@ export class Registry {
484
486
  const schema = entry.config.hidden ? withoutHiddenColumns(read, entry.config.hidden, entry.config.name) : read;
485
487
  for (const hook of this.dialedHooks)
486
488
  hook(entry.config.id, adapter);
489
+ /* A writable connection keeps its Undo history in its database where the
490
+ store is provisioned (journalStore.ts), read back before the first
491
+ write; elsewhere it stays in memory, as it always has. */
492
+ if (entry.config.writable)
493
+ await keepWriteJournal(entry.config.id, adapter, this.onWarning);
487
494
  /* Wrapped before it is stored, so nothing can reach the unwrapped one:
488
495
  the boundary is a property of the connection, not a rule each caller
489
496
  remembers. Introspection happens first because the catalog is the
@@ -625,6 +632,8 @@ export class Registry {
625
632
  const entries = [...this.entries.values()];
626
633
  const finish = entries.map((entry) => this.invalidateAdapter(entry));
627
634
  try {
635
+ // What the journal wrote behind lands before the handles it is written through close.
636
+ await Promise.all(entries.map((entry) => flushJournal(entry.config.id)));
628
637
  await Promise.allSettled(entries.map((entry) => entry.adapter?.close()));
629
638
  }
630
639
  finally {
@@ -6,9 +6,13 @@
6
6
  * - `Referrer-Policy: strict-origin-when-cross-origin`: an address under a
7
7
  * record (`/compass/leads/127?search=…`) leaves this origin as the origin
8
8
  * alone, and never over plain http.
9
- * - `Permissions-Policy`: no page here uses a camera, a microphone, a
10
- * location or a payment sheet, so none is granted — to a component, or to
11
- * whatever frames or is framed.
9
+ * - `Permissions-Policy`: no page here uses a microphone, a location or a
10
+ * payment sheet, and nothing but an App's page draws a camera, so none is
11
+ * granted — to a component, or to whatever frames or is framed. An App's
12
+ * own HTML (clientHttp.ts) replaces this with `APP_PAGE_PERMISSIONS_POLICY`,
13
+ * which lets its own origin ask for the camera: the scan input
14
+ * (`field.text({ as: 'scan' })`) reads a code through `getUserMedia`, and
15
+ * a form with one can be a signed-in page or a public one.
12
16
  * - `Strict-Transport-Security`, only on an answer that arrived over https
13
17
  * (trust.ts `arrivedOverHttps`): the socket's own TLS, an https public
14
18
  * address, or a proxy this server trusts saying it forwarded https. A
@@ -21,6 +25,12 @@ import { type ProxyRange } from './trust.js';
21
25
  export declare const REFERRER_POLICY = "strict-origin-when-cross-origin";
22
26
  /** @public the database browser's (scripts/boundary/entries.json, "host") */
23
27
  export declare const PERMISSIONS_POLICY: string;
28
+ /**
29
+ * An App's page: `PERMISSIONS_POLICY` with the camera allowed for this origin
30
+ * alone. Only the HTML says it — the API, the assets and every refusal keep
31
+ * the baseline — and a frame from another origin is still not granted it.
32
+ */
33
+ export declare const APP_PAGE_PERMISSIONS_POLICY: string;
24
34
  /** A year, and the names under this one: the time browsers ask of a name kept on https. */
25
35
  export declare const STRICT_TRANSPORT = "max-age=31536000; includeSubDomains";
26
36
  export declare function setBaselineHeaders(req: Pick<IncomingMessage, 'headers' | 'socket'>, res: ServerResponse, trustedProxies?: readonly ProxyRange[]): void;
@@ -4,6 +4,12 @@ export const REFERRER_POLICY = 'strict-origin-when-cross-origin';
4
4
  /** @public the database browser's (scripts/boundary/entries.json, "host") */
5
5
  export const PERMISSIONS_POLICY = 'camera=(), microphone=(), geolocation=(), payment=(), usb=(), serial=(), bluetooth=(), hid=(), midi=(), ' +
6
6
  'magnetometer=(), gyroscope=(), accelerometer=(), display-capture=(), browsing-topics=()';
7
+ /**
8
+ * An App's page: `PERMISSIONS_POLICY` with the camera allowed for this origin
9
+ * alone. Only the HTML says it — the API, the assets and every refusal keep
10
+ * the baseline — and a frame from another origin is still not granted it.
11
+ */
12
+ export const APP_PAGE_PERMISSIONS_POLICY = PERMISSIONS_POLICY.replace('camera=()', 'camera=(self)');
7
13
  /** A year, and the names under this one: the time browsers ask of a name kept on https. */
8
14
  export const STRICT_TRANSPORT = 'max-age=31536000; includeSubDomains';
9
15
  export function setBaselineHeaders(req, res, trustedProxies = []) {
@@ -1,5 +1,56 @@
1
+ /**
2
+ * An export, streamed: `POST /api/export { q, format, columns? }` answers
3
+ * with the file itself — CSV, a JSON array or NDJSON — read in batches and
4
+ * written as each arrives, compressed when the request accepts it.
5
+ *
6
+ * "All matching rows" used to be one `/api/run` of up to 100,000 rows: the
7
+ * whole result held as objects, serialized as one JSON string (about 19 MB
8
+ * and 175 ms of a blocked event loop at the ceiling), with a total counted
9
+ * beside it that the file never used. Here a batch is read, written, and let
10
+ * go, so memory stays flat whatever the size, the first byte leaves after
11
+ * the first batch, and nothing is counted. There is no row ceiling: a file
12
+ * holds every row the question matches, or as many as its own `limit` says.
13
+ *
14
+ * The next batch is read only once the last has been handed on: a write the
15
+ * connection cannot take yet waits for it to drain (backpressure), so a slow
16
+ * client holds the server to a batch in flight, not the whole file. A client
17
+ * that goes away stops the export — the statement in flight is cancelled
18
+ * (statementControl.ts) and nothing more is read.
19
+ *
20
+ * Each batch is the ordinary read through this request's own reader —
21
+ * the same grants, fields, row scope, narrowed resources and errors as
22
+ * `/api/run` — so an export can never hold a row the list could not show.
23
+ * Batches page the way the list's own pager does (keysetReadHttp.ts): in the
24
+ * list's order with the key as its tiebreak, each starting after the last row
25
+ * of the one before, as a continuation ANDed into the question's own filter,
26
+ * so a batch costs what the first one did however deep the file goes and no
27
+ * row is skipped or repeated. Where the list pages by offset — no primary
28
+ * key, a union, a sort through a reference or on a value that does not come
29
+ * back as it is stored — so does the file.
30
+ *
31
+ * Inside a row-policy App each batch is a read of the policy's own facade,
32
+ * its row scope ANDed into the batch as into a list's page, and the file
33
+ * holds only what the reader's list could show. A policy reply is held until
34
+ * its context settles (handler.ts): the file's rows go as they are read, each
35
+ * already the reader's, but its last write waits for the context, so a
36
+ * context that fails after the rows were read cuts the connection and the
37
+ * browser never saves a file that looks whole. A tenant's lease streams the
38
+ * same way through `exportFile` (mistri: tenant/tenantHttp.ts).
39
+ *
40
+ * What an App decides of it is `ExportHooks` (mistri: http/appExport.ts): who
41
+ * exports which table, who `me` is in the question, and what an amount kept
42
+ * in minor units says in a CSV. The database browser exports every table it
43
+ * reads, as stored.
44
+ */
45
+ import type { ServerResponse } from 'node:http';
46
+ import type { Adapter } from '../../adapters/adapter.js';
1
47
  import { parseQuery } from '../../shared/query.js';
48
+ import { type DatabaseSchema } from '../../shared/schema.js';
2
49
  import type { PostContext } from './context.js';
50
+ /** Rows read per statement, and so the most the server holds of a file at once:
51
+ * large enough that a million-row file is a thousand reads, small enough that
52
+ * one batch is never a memory event. */
53
+ export declare const EXPORT_BATCH = 1000;
3
54
  /** Who `me` is in a question, where its words name `me` (parseQuery's `me`). */
4
55
  type Person = NonNullable<Parameters<typeof parseQuery>[1]>['me'];
5
56
  /** What a product decides of an export. */
@@ -11,5 +62,28 @@ export interface ExportHooks {
11
62
  /** A row as a CSV says it: an amount kept in minor units as the figure it is, a value as its component's word. */
12
63
  amounts?(post: PostContext, tableId: string): ((row: Record<string, unknown>) => Record<string, unknown>) | undefined;
13
64
  }
65
+ /** One export, on what its host reads it through: the routes below, or a tenant's lease (mistri: tenant/tenantHttp.ts). */
66
+ interface ExportAsked {
67
+ res: ServerResponse;
68
+ body: Record<string, unknown>;
69
+ /** The reader every batch is read through: this request's own, with its grants, fields and row scope. */
70
+ adapter: Adapter;
71
+ schema: DatabaseSchema;
72
+ now: () => Date;
73
+ /** Whether a table may be exported (`''`: any table). */
74
+ may: (tableId: string) => boolean;
75
+ person?: (text: string) => Promise<Person | undefined>;
76
+ amounts?: (tableId: string) => ((row: Record<string, unknown>) => Record<string, unknown>) | undefined;
77
+ /** Headers the file carries beside its own: a tenant lease's owner, which its client checks before it keeps the file. */
78
+ headers?: Record<string, string>;
79
+ /** A reply that is not the file (a refusal, the question's errors), sent the host's way. */
80
+ answer?: (status: number, body: unknown) => void;
81
+ }
14
82
  export declare function exportRoute(post: PostContext, hooks: ExportHooks): Promise<boolean>;
83
+ /**
84
+ * The export, streamed. Resolves with the file's last write, for the caller
85
+ * to make once whatever the reads ran in has settled; undefined when the
86
+ * answer was not a file (it was sent already) or the file was cut.
87
+ */
88
+ export declare function exportFile(asked: ExportAsked): Promise<(() => void) | undefined>;
15
89
  export {};