@kurvball/rehearse 1.13.3 → 1.15.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/cli.js +5 -0
- package/dist/commands/announce.js +2 -2
- package/dist/commands/clean.js +6 -3
- package/dist/commands/compare.js +6 -1
- package/dist/commands/drive.js +12 -3
- package/dist/commands/index.js +2 -0
- package/dist/commands/init.js +1 -1
- package/dist/commands/keys.d.ts +2 -0
- package/dist/commands/keys.js +67 -0
- package/dist/commands/run.js +8 -4
- package/dist/commands/services.d.ts +3 -0
- package/dist/commands/services.js +10 -1
- package/dist/commands/stage.js +18 -2
- package/dist/commands/up.js +11 -3
- package/dist/compare/compare.d.ts +2 -0
- package/dist/compare/compare.js +19 -10
- package/dist/detect/detect.js +4 -0
- package/dist/detect/index.d.ts +1 -0
- package/dist/detect/index.js +1 -0
- package/dist/detect/keys.d.ts +16 -0
- package/dist/detect/keys.js +141 -0
- package/dist/detect/write.d.ts +2 -0
- package/dist/detect/write.js +20 -1
- package/dist/drive/daemon.js +2 -1
- package/dist/drive/open.js +6 -1
- package/dist/errors.js +2 -1
- package/dist/git/checkout.d.ts +6 -0
- package/dist/git/checkout.js +28 -19
- package/dist/git/index.d.ts +3 -2
- package/dist/git/index.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/keys/index.d.ts +3 -0
- package/dist/keys/index.js +3 -0
- package/dist/keys/keys.d.ts +16 -0
- package/dist/keys/keys.js +139 -0
- package/dist/keys/report.d.ts +23 -0
- package/dist/keys/report.js +31 -0
- package/dist/keys/store.d.ts +7 -0
- package/dist/keys/store.js +133 -0
- package/dist/keys/tool.d.ts +11 -0
- package/dist/keys/tool.js +27 -0
- package/dist/kinds/flow/kind.js +2 -2
- package/dist/manifest/index.d.ts +1 -1
- package/dist/manifest/load.js +33 -0
- package/dist/manifest/types.d.ts +28 -0
- package/dist/repositories/copies.d.ts +16 -0
- package/dist/repositories/copies.js +132 -0
- package/dist/repositories/index.d.ts +1 -0
- package/dist/repositories/index.js +1 -0
- package/dist/run/cache.d.ts +2 -1
- package/dist/run/cache.js +3 -1
- package/dist/run/index.d.ts +1 -1
- package/dist/run/index.js +1 -0
- package/dist/run/observation.d.ts +4 -0
- package/dist/run/observation.js +5 -1
- package/dist/run/session.d.ts +3 -1
- package/dist/run/session.js +27 -13
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +1 -0
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +2 -2
- package/dist/services/registry.d.ts +1 -0
- package/dist/services/service.d.ts +4 -0
- package/dist/services/service.js +46 -14
- package/dist/stage/api.d.ts +2 -0
- package/dist/stage/api.js +4 -0
- package/dist/stage/http.js +2 -1
- package/dist/stage/sending.d.ts +2 -1
- package/dist/stage/sending.js +8 -2
- package/dist/stage/server.d.ts +2 -0
- package/dist/stage/server.js +14 -1
- package/llms.txt +7 -6
- package/package.json +2 -2
- package/schema/v1/common.schema.json +22 -0
- package/schema/v1/init.schema.json +2 -1
- package/schema/v1/keys.schema.json +51 -0
- package/schema/v1/manifest.schema.json +66 -0
- package/schema/v1/samples/keys.json +22 -0
- package/schema/v1/samples/manifest.yaml +12 -0
- package/schema/v1/services.schema.json +13 -0
- package/skill/rehearse/SKILL.md +8 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { readText, within } from './repository.js';
|
|
3
|
+
const ENV_NAME = '[A-Z][A-Z0-9_]*';
|
|
4
|
+
const WITHOUT_A_FALLBACK = '(?!\\s*(?:\\?\\?|\\|\\||\\?(?!\\.)|=(?!=)))';
|
|
5
|
+
const READS = [
|
|
6
|
+
new RegExp(`process\\.env\\.(${ENV_NAME})\\b${WITHOUT_A_FALLBACK}`, 'g'),
|
|
7
|
+
new RegExp(`process\\.env\\[\\s*['"\`](${ENV_NAME})['"\`]\\s*\\]${WITHOUT_A_FALLBACK}`, 'g'),
|
|
8
|
+
new RegExp(`(?:import\\.meta|Bun)\\.env\\.(${ENV_NAME})\\b${WITHOUT_A_FALLBACK}`, 'g'),
|
|
9
|
+
new RegExp(`Deno\\.env\\.get\\(\\s*['"\`](${ENV_NAME})['"\`]\\s*\\)${WITHOUT_A_FALLBACK}`, 'g'),
|
|
10
|
+
];
|
|
11
|
+
const TAKEN_APART = /\{([^{}]*)\}\s*=\s*process\.env\b/g;
|
|
12
|
+
const TAKEN_WITHOUT_A_DEFAULT = new RegExp(`^\\s*(${ENV_NAME})\\s*(?::\\s*[A-Za-z_$][\\w$]*\\s*)?$`);
|
|
13
|
+
const SET_IN_AN_EXAMPLE = new RegExp(`^\\s*(?:export\\s+)?(${ENV_NAME})\\s*=`, 'gm');
|
|
14
|
+
const AN_EXAMPLE_ENV_FILE = /^\.env(?:\.[\w-]+)?\.(?:example|sample|template)$/;
|
|
15
|
+
const NOT_THE_APP = /^(?:scripts|tools|docs|bench|benchmarks|playwright|cypress|\.[^/]+)\/|^[^/]+\.config\.[^/]+$|(?:^|\/)(?:test|tests|__tests__|__mocks__|fixtures|e2e|examples)\/|\.(?:test|spec|stories)\.[^/]+$/;
|
|
16
|
+
const READ_AS_CODE = /\.(?:[cm]?[jt]s|tsx|jsx|astro|vue|svelte)$/;
|
|
17
|
+
const BLOCK_COMMENT = /(^|[\s;{}(),])\/\*[\s\S]*?\*\//gm;
|
|
18
|
+
const LINE_COMMENT = /(^|[\s;{}(),])\/\/.*$/gm;
|
|
19
|
+
const SOURCES_READ = 400;
|
|
20
|
+
const LAST_WORD_OF_A_SECRET = new Set([
|
|
21
|
+
'KEY',
|
|
22
|
+
'SECRET',
|
|
23
|
+
'TOKEN',
|
|
24
|
+
'PASSWORD',
|
|
25
|
+
'PASSWD',
|
|
26
|
+
'PASS',
|
|
27
|
+
'CREDENTIAL',
|
|
28
|
+
'CREDENTIALS',
|
|
29
|
+
]);
|
|
30
|
+
const MEANT_TO_BE_SEEN = new Set(['PUBLIC', 'PUBLISHABLE']);
|
|
31
|
+
const A_KEY_THAT_NAMES_DATA = new Set([
|
|
32
|
+
'SORT',
|
|
33
|
+
'PARTITION',
|
|
34
|
+
'PRIMARY',
|
|
35
|
+
'FOREIGN',
|
|
36
|
+
'IDEMPOTENCY',
|
|
37
|
+
'DEDUP',
|
|
38
|
+
'DEDUPE',
|
|
39
|
+
'CACHE',
|
|
40
|
+
'HASH',
|
|
41
|
+
'ROUTING',
|
|
42
|
+
'LOCK',
|
|
43
|
+
'QUEUE',
|
|
44
|
+
'OBJECT',
|
|
45
|
+
'BUCKET',
|
|
46
|
+
'STORAGE',
|
|
47
|
+
'FILE',
|
|
48
|
+
'INDEX',
|
|
49
|
+
'LOOKUP',
|
|
50
|
+
'SHARD',
|
|
51
|
+
'TABLE',
|
|
52
|
+
'ROW',
|
|
53
|
+
'COLUMN',
|
|
54
|
+
'NAMESPACE',
|
|
55
|
+
]);
|
|
56
|
+
const and = (items) => items.length <= 1 ? (items[0] ?? '') : `${items.slice(0, -1).join(', ')} and ${items.at(-1)}`;
|
|
57
|
+
export function looksSecret(env) {
|
|
58
|
+
const words = env.split('_').filter(Boolean);
|
|
59
|
+
const last = words.at(-1) ?? '';
|
|
60
|
+
if (!LAST_WORD_OF_A_SECRET.has(last) || words.some((w) => MEANT_TO_BE_SEEN.has(w)))
|
|
61
|
+
return false;
|
|
62
|
+
return !(last === 'KEY' && A_KEY_THAT_NAMES_DATA.has(words.at(-2) ?? ''));
|
|
63
|
+
}
|
|
64
|
+
export const keyNameOf = (env) => env
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.replace(/_+/g, '-')
|
|
67
|
+
.replace(/^-+|-+$/g, '');
|
|
68
|
+
function namesRead(code) {
|
|
69
|
+
const text = code.replace(BLOCK_COMMENT, '$1').replace(LINE_COMMENT, '$1');
|
|
70
|
+
const names = READS.flatMap((re) => [...text.matchAll(re)].map((m) => m[1]));
|
|
71
|
+
for (const m of text.matchAll(TAKEN_APART))
|
|
72
|
+
for (const part of m[1].split(',')) {
|
|
73
|
+
const name = TAKEN_WITHOUT_A_DEFAULT.exec(part)?.[1];
|
|
74
|
+
if (name)
|
|
75
|
+
names.push(name);
|
|
76
|
+
}
|
|
77
|
+
return names;
|
|
78
|
+
}
|
|
79
|
+
const namesSetIn = (text) => [...text.matchAll(SET_IN_AN_EXAMPLE)].map((m) => m[1]);
|
|
80
|
+
function filesOf(repo, s) {
|
|
81
|
+
const dir = s.pkg.dir;
|
|
82
|
+
const others = repo.packages.map((p) => p.dir).filter((d) => d !== dir && within(dir, d));
|
|
83
|
+
return repo.files
|
|
84
|
+
.filter((f) => within(dir, f) && !others.some((d) => within(d, f)))
|
|
85
|
+
.map((f) => (dir ? f.slice(dir.length + 1) : f))
|
|
86
|
+
.filter((f) => !NOT_THE_APP.test(f));
|
|
87
|
+
}
|
|
88
|
+
function seenBy(repo, s) {
|
|
89
|
+
const inRepo = (f) => (s.pkg.dir ? `${s.pkg.dir}/${f}` : f);
|
|
90
|
+
const files = filesOf(repo, s);
|
|
91
|
+
const out = [];
|
|
92
|
+
for (const file of files.filter((f) => READ_AS_CODE.test(f)).slice(0, SOURCES_READ)) {
|
|
93
|
+
const text = readText(path.join(repo.root, inRepo(file)));
|
|
94
|
+
if (text !== null)
|
|
95
|
+
for (const env of namesRead(text))
|
|
96
|
+
out.push({ env, from: inRepo(file), onlyNamed: false, service: s.name });
|
|
97
|
+
}
|
|
98
|
+
for (const file of files.filter((f) => AN_EXAMPLE_ENV_FILE.test(f))) {
|
|
99
|
+
const text = readText(path.join(repo.root, inRepo(file)));
|
|
100
|
+
if (text !== null)
|
|
101
|
+
for (const env of namesSetIn(text))
|
|
102
|
+
out.push({ env, from: inRepo(file), onlyNamed: true, service: s.name });
|
|
103
|
+
}
|
|
104
|
+
return out.filter((x) => looksSecret(x.env) && !(x.env in s.env));
|
|
105
|
+
}
|
|
106
|
+
export function keysOf(repo, services) {
|
|
107
|
+
const byEnv = new Map();
|
|
108
|
+
for (const seen of services.flatMap((s) => seenBy(repo, s))) {
|
|
109
|
+
const known = byEnv.get(seen.env);
|
|
110
|
+
if (!known)
|
|
111
|
+
byEnv.set(seen.env, {
|
|
112
|
+
name: keyNameOf(seen.env),
|
|
113
|
+
env: seen.env,
|
|
114
|
+
from: seen.from,
|
|
115
|
+
onlyNamed: seen.onlyNamed,
|
|
116
|
+
services: [seen.service],
|
|
117
|
+
});
|
|
118
|
+
else if (!known.services.includes(seen.service))
|
|
119
|
+
known.services.push(seen.service);
|
|
120
|
+
}
|
|
121
|
+
const keys = [...byEnv.values()].sort((a, b) => (a.env < b.env ? -1 : a.env > b.env ? 1 : 0));
|
|
122
|
+
const taken = new Set();
|
|
123
|
+
for (const k of keys) {
|
|
124
|
+
let name = k.name;
|
|
125
|
+
for (let n = 2; taken.has(name); n += 1)
|
|
126
|
+
name = `${k.name}-${n}`;
|
|
127
|
+
k.name = name;
|
|
128
|
+
taken.add(name);
|
|
129
|
+
}
|
|
130
|
+
const findings = keys.map((k) => ({
|
|
131
|
+
kind: 'key',
|
|
132
|
+
name: k.name,
|
|
133
|
+
from: k.from,
|
|
134
|
+
written: true,
|
|
135
|
+
why: k.onlyNamed
|
|
136
|
+
? `${k.env}, which ${k.from} names for ${and(k.services)}`
|
|
137
|
+
: `${k.env}, which ${and(k.services)} ${k.services.length === 1 ? 'reads' : 'read'}`,
|
|
138
|
+
next: `Keep it on this computer with rehearse keys add ${k.name}, or say where it is with from: op://…`,
|
|
139
|
+
}));
|
|
140
|
+
return { keys, findings };
|
|
141
|
+
}
|
package/dist/detect/write.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DetectedKey } from './keys.ts';
|
|
1
2
|
import type { Blocks } from './scenarios.ts';
|
|
2
3
|
import type { Service } from './services.ts';
|
|
3
4
|
export interface Plan {
|
|
@@ -10,6 +11,7 @@ export interface Plan {
|
|
|
10
11
|
from: string[];
|
|
11
12
|
} | null;
|
|
12
13
|
services: readonly Service[];
|
|
14
|
+
keys: readonly DetectedKey[];
|
|
13
15
|
blocks: Blocks;
|
|
14
16
|
prefix: string;
|
|
15
17
|
}
|
package/dist/detect/write.js
CHANGED
|
@@ -55,10 +55,28 @@ function services(plan) {
|
|
|
55
55
|
for (const s of plan.services) {
|
|
56
56
|
const cwd = joined(plan.prefix, s.pkg.dir);
|
|
57
57
|
const env = Object.entries(s.env);
|
|
58
|
-
lines.push(` # Detected from ${printable(s.pkg.file)}: ${WHAT[s.server]}${s.built ? ', built first' : ''}.`, ` ${s.name}:`, ` up: ${scalar(s.up)}`, ...(cwd ? [` cwd: ${scalar(cwd)}`] : []), ' port: auto', ` ready: ${scalar(s.ready)}`, ...(env.length ? [' env:', ...env.map(([k, v]) => ` ${k}: ${scalar(v)}`)] : []));
|
|
58
|
+
lines.push(` # Detected from ${printable(s.pkg.file)}: ${WHAT[s.server]}${s.built ? ', built first' : ''}.`, ` ${s.name}:`, ` up: ${scalar(s.up)}`, ...(cwd ? [` cwd: ${scalar(cwd)}`] : []), ' port: auto', ` ready: ${scalar(s.ready)}`, ...(env.length ? [' env:', ...env.map(([k, v]) => ` ${k}: ${scalar(v)}`)] : []), ...keysGivenTo(plan, s));
|
|
59
59
|
}
|
|
60
60
|
return `${lines.join('\n')}\n`;
|
|
61
61
|
}
|
|
62
|
+
function keysGivenTo(plan, s) {
|
|
63
|
+
const given = plan.keys.filter((k) => k.services.includes(s.name));
|
|
64
|
+
return given.length ? [' keys:', ...given.map((k) => ` ${k.env}: ${scalar(k.name)}`)] : [];
|
|
65
|
+
}
|
|
66
|
+
function keys(plan) {
|
|
67
|
+
if (plan.keys.length === 0)
|
|
68
|
+
return null;
|
|
69
|
+
return [
|
|
70
|
+
'# The keys services get as they start, by name: where each comes from, never its value.',
|
|
71
|
+
'# None says where it is yet: keep each with rehearse keys add <key>, or give it from: op://vault/item/field.',
|
|
72
|
+
'keys:',
|
|
73
|
+
...plan.keys.flatMap((k) => [
|
|
74
|
+
` # ${k.onlyNamed ? 'Named' : 'Read'} in ${printable(k.from)}.`,
|
|
75
|
+
` ${scalar(k.name)}:`,
|
|
76
|
+
]),
|
|
77
|
+
'',
|
|
78
|
+
].join('\n');
|
|
79
|
+
}
|
|
62
80
|
function scenarios(plan) {
|
|
63
81
|
const b = plan.blocks;
|
|
64
82
|
if (!b.pieces && !b.flows && !b.calls && !b.files)
|
|
@@ -107,6 +125,7 @@ export function manifestText(plan) {
|
|
|
107
125
|
base(plan),
|
|
108
126
|
prepare(plan),
|
|
109
127
|
services(plan),
|
|
128
|
+
...[keys(plan)].filter((block) => block !== null),
|
|
110
129
|
commented(STARTER_BLOCKS.people),
|
|
111
130
|
commented(STARTER_BLOCKS.worlds),
|
|
112
131
|
commented(STARTER_BLOCKS.standins),
|
package/dist/drive/daemon.js
CHANGED
|
@@ -2,6 +2,7 @@ import { randomBytes } from 'node:crypto';
|
|
|
2
2
|
import { createServer } from 'node:http';
|
|
3
3
|
import { parseArgs } from 'node:util';
|
|
4
4
|
import { errorText } from '../errors.js';
|
|
5
|
+
import { withoutKeysDeep } from '../keys/index.js';
|
|
5
6
|
import { loadManifest } from '../manifest/index.js';
|
|
6
7
|
import { scrubDeep } from '../people/index.js';
|
|
7
8
|
import { freePort } from '../services/index.js';
|
|
@@ -149,7 +150,7 @@ async function main() {
|
|
|
149
150
|
const fresh = now.errors.slice(seen);
|
|
150
151
|
const answer = scrubDeep(result.ok && fresh.length ? { ...result, errors: fresh } : result, now.tokens);
|
|
151
152
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
152
|
-
res.end(JSON.stringify(answer), () => {
|
|
153
|
+
res.end(JSON.stringify(withoutKeysDeep(answer)), () => {
|
|
153
154
|
if (command.command === 'stop')
|
|
154
155
|
void stop().finally(() => process.exit(0));
|
|
155
156
|
});
|
package/dist/drive/open.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
+
import { knowKeys } from '../keys/index.js';
|
|
2
3
|
import { fill } from '../manifest/index.js';
|
|
3
4
|
import { scrub, signIn } from '../people/index.js';
|
|
4
5
|
import { startPieceServer } from '../kinds/index.js';
|
|
5
6
|
import { applyWorld, baseVars } from '../run/index.js';
|
|
6
7
|
import { bringDown, bringUp, runningServices, stateDir } from '../services/index.js';
|
|
8
|
+
import { copiesFor, rootsOf } from '../repositories/index.js';
|
|
7
9
|
import { readScenarios } from '../scenarios/index.js';
|
|
8
10
|
import { calm, contextFor, errorsOf, FIXED_TIME, launch, signInBySteps, signInOf, } from './browser.js';
|
|
9
11
|
import { noteSecretsOn, SECRET_AUTOCOMPLETE } from './secrets.js';
|
|
@@ -47,13 +49,16 @@ export async function openPage(m, checkout, o, showing = {}) {
|
|
|
47
49
|
service = svc.name;
|
|
48
50
|
let running = runningServices(m.dir).filter((r) => path.resolve(r.checkout) === path.resolve(checkout));
|
|
49
51
|
let mine = running.find((r) => r.name === svc.name);
|
|
50
|
-
if (
|
|
52
|
+
if (mine)
|
|
53
|
+
await knowKeys(m, svc);
|
|
54
|
+
else {
|
|
51
55
|
if (o.world)
|
|
52
56
|
await applyWorld(m, o.world, 'before', checkout, baseVars(m, checkout, running));
|
|
53
57
|
mine = await bringUp(m, svc, checkout, {
|
|
54
58
|
world: o.world,
|
|
55
59
|
real: new Set(),
|
|
56
60
|
vars: baseVars(m, checkout, running),
|
|
61
|
+
roots: rootsOf(await copiesFor(m, {}, checkout, showing.progress ?? (() => undefined))),
|
|
57
62
|
...(showing.progress && { progress: showing.progress }),
|
|
58
63
|
});
|
|
59
64
|
started.push(mine);
|
package/dist/errors.js
CHANGED
|
@@ -15,7 +15,8 @@ export class RehearseError extends Error {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
export const usage = (says) => new RehearseError('usage', says, { exit: 2 });
|
|
18
|
-
|
|
18
|
+
const firstLineOf = (text) => text.split('\n')[0] ?? '';
|
|
19
|
+
export const errorText = (error) => error instanceof Error ? firstLineOf(error.message) || error.name : firstLineOf(String(error));
|
|
19
20
|
export function errorJson(error) {
|
|
20
21
|
if (error instanceof RehearseError)
|
|
21
22
|
return {
|
package/dist/git/checkout.d.ts
CHANGED
|
@@ -10,7 +10,13 @@ export type Tree = {
|
|
|
10
10
|
repo: string;
|
|
11
11
|
commit: string;
|
|
12
12
|
};
|
|
13
|
+
export declare function folderNamed(given: string, cwd?: string): string | null;
|
|
13
14
|
export declare function locate(repo: string, given: string, cwd?: string): Tree;
|
|
15
|
+
export declare function preparedOnce(dir: string, how: {
|
|
16
|
+
prepare: string | null;
|
|
17
|
+
seed: boolean;
|
|
18
|
+
given: string;
|
|
19
|
+
}, progress: (line: string) => void): Promise<void>;
|
|
14
20
|
export declare function checkoutFor(m: Manifest, repo: string, given: string, progress: (line: string) => void, keep?: readonly string[], cwd?: string): Promise<{
|
|
15
21
|
path: string;
|
|
16
22
|
ref: string | null;
|
package/dist/git/checkout.js
CHANGED
|
@@ -33,18 +33,39 @@ export function overlay(m, checkout) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
const NAMED_AS_A_FOLDER = /^(?:\.{1,2}(?:\/|$)|\/|~\/)/;
|
|
36
|
+
export function folderNamed(given, cwd = process.cwd()) {
|
|
37
|
+
if (!NAMED_AS_A_FOLDER.test(given))
|
|
38
|
+
return null;
|
|
39
|
+
const asPath = path.resolve(cwd, given.replace(/^~(?=\/)/, process.env.HOME ?? '~'));
|
|
40
|
+
if (!existsSync(asPath) || !statSync(asPath).isDirectory())
|
|
41
|
+
throw new RehearseError('no_checkout', `There is no checkout at ${asPath}.`);
|
|
42
|
+
return topOf(asPath) ?? asPath;
|
|
43
|
+
}
|
|
36
44
|
export function locate(repo, given, cwd = process.cwd()) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
throw new RehearseError('no_checkout', `There is no checkout at ${asPath}.`);
|
|
41
|
-
return { folder: topOf(asPath) ?? asPath };
|
|
42
|
-
}
|
|
45
|
+
const folder = folderNamed(given, cwd);
|
|
46
|
+
if (folder !== null)
|
|
47
|
+
return { folder };
|
|
43
48
|
const commit = commitOf(repo, given);
|
|
44
49
|
if (!commit)
|
|
45
50
|
throw new RehearseError('git', `${given} isn't a commit Rehearse can find in ${repo}.${existsSync(path.resolve(cwd, given)) ? ` To mean the folder, write ./${given}.` : ''}`);
|
|
46
51
|
return { repo: topOf(repo) ?? repo, commit };
|
|
47
52
|
}
|
|
53
|
+
export async function preparedOnce(dir, how, progress) {
|
|
54
|
+
const { prepare, given } = how;
|
|
55
|
+
if (!prepare || existsSync(preparedMark(dir)))
|
|
56
|
+
return;
|
|
57
|
+
const seeded = how.seed ? seedDependencies(dir, progress) : [];
|
|
58
|
+
progress(`Preparing ${dir}: ${prepare}`);
|
|
59
|
+
let ran = await sh(prepare, { cwd: dir, timeoutMs: 30 * 60_000 });
|
|
60
|
+
if (ran.code !== 0 && seeded.length > 0) {
|
|
61
|
+
progress(`Preparing ${dir} again, without the seeded dependencies: ${prepare}`);
|
|
62
|
+
unseed(seeded);
|
|
63
|
+
ran = await sh(prepare, { cwd: dir, timeoutMs: 30 * 60_000 });
|
|
64
|
+
}
|
|
65
|
+
if (ran.code !== 0)
|
|
66
|
+
throw new RehearseError('prepare', `Preparing ${given} failed (${prepare}): ${tail(ran.stderr || ran.stdout) || `exit ${ran.code}`}`);
|
|
67
|
+
writeFileSync(preparedMark(dir), `${new Date().toISOString()}\n`);
|
|
68
|
+
}
|
|
48
69
|
export async function checkoutFor(m, repo, given, progress, keep = [], cwd = process.cwd()) {
|
|
49
70
|
const found = locate(repo, given, cwd);
|
|
50
71
|
if ('folder' in found)
|
|
@@ -57,19 +78,7 @@ export async function checkoutFor(m, repo, given, progress, keep = [], cwd = pro
|
|
|
57
78
|
gitOr(repo, ['worktree', 'prune']);
|
|
58
79
|
git(repo, ['worktree', 'add', '--detach', dir, commit]);
|
|
59
80
|
}
|
|
60
|
-
|
|
61
|
-
const seeded = m.seed ? seedDependencies(dir, progress) : [];
|
|
62
|
-
progress(`Preparing ${dir}: ${m.prepare}`);
|
|
63
|
-
let ran = await sh(m.prepare, { cwd: dir, timeoutMs: 30 * 60_000 });
|
|
64
|
-
if (ran.code !== 0 && seeded.length > 0) {
|
|
65
|
-
progress(`Preparing ${dir} again, without the seeded dependencies: ${m.prepare}`);
|
|
66
|
-
unseed(seeded);
|
|
67
|
-
ran = await sh(m.prepare, { cwd: dir, timeoutMs: 30 * 60_000 });
|
|
68
|
-
}
|
|
69
|
-
if (ran.code !== 0)
|
|
70
|
-
throw new RehearseError('prepare', `Preparing ${given} failed (${m.prepare}): ${tail(ran.stderr || ran.stdout) || `exit ${ran.code}`}`);
|
|
71
|
-
writeFileSync(preparedMark(dir), `${new Date().toISOString()}\n`);
|
|
72
|
-
}
|
|
81
|
+
await preparedOnce(dir, { prepare: m.prepare, seed: m.seed, given }, progress);
|
|
73
82
|
const now = new Date();
|
|
74
83
|
utimesSync(dir, now, now);
|
|
75
84
|
evict(m, repo, [dir, ...keep]);
|
package/dist/git/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { checkoutFor, evict, infoOf, locate, overlay, type Tree } from './checkout.ts';
|
|
2
|
-
export { commitOf, gitOr, topOf } from './git.ts';
|
|
1
|
+
export { checkoutFor, evict, folderNamed, infoOf, locate, overlay, preparedOnce, type Tree, } from './checkout.ts';
|
|
2
|
+
export { commitOf, git, gitOr, isDirty, topOf } from './git.ts';
|
|
3
|
+
export { preparedMark } from './seed.ts';
|
package/dist/git/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { checkoutFor, evict, infoOf, locate, overlay } from './checkout.js';
|
|
2
|
-
export { commitOf, gitOr, topOf } from './git.js';
|
|
1
|
+
export { checkoutFor, evict, folderNamed, infoOf, locate, overlay, preparedOnce, } from './checkout.js';
|
|
2
|
+
export { commitOf, git, gitOr, isDirty, topOf } from './git.js';
|
|
3
|
+
export { preparedMark } from './seed.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -14,5 +14,6 @@ export { runSide, type SideRun, type SessionOptions } from './run/index.ts';
|
|
|
14
14
|
export type { Observation } from './run/index.ts';
|
|
15
15
|
export { affected } from './affected/index.ts';
|
|
16
16
|
export { bringUp, bringDown } from './services/index.ts';
|
|
17
|
+
export { keepKey, keysOut, withoutKeys, type KeyRow, type KeysOut } from './keys/index.ts';
|
|
17
18
|
export { defineWorld, buildWorld, worldChain, worldMain, type WorldDefinition, type WorldContext, } from './worlds/index.ts';
|
|
18
19
|
export { setAsideJson, setAsideText } from './compare/index.ts';
|
package/dist/index.js
CHANGED
|
@@ -11,5 +11,6 @@ export { compare } from './compare/index.js';
|
|
|
11
11
|
export { runSide } from './run/index.js';
|
|
12
12
|
export { affected } from './affected/index.js';
|
|
13
13
|
export { bringUp, bringDown } from './services/index.js';
|
|
14
|
+
export { keepKey, keysOut, withoutKeys } from './keys/index.js';
|
|
14
15
|
export { defineWorld, buildWorld, worldChain, worldMain, } from './worlds/index.js';
|
|
15
16
|
export { setAsideJson, setAsideText } from './compare/index.js';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { keep, keysFor, knowKeys, lookUp, looksLive, maskKeysIn, SHORTEST_KEY, withoutKeys, withoutKeysDeep, type KeyLook, type KeyState, } from './keys.ts';
|
|
2
|
+
export { KEPT_UNDER, storeOf, type KeyStore } from './store.ts';
|
|
3
|
+
export { keepKey, keyNamed, keysOut, type KeyRow, type KeysOut } from './report.ts';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Key, Manifest, Service } from '../manifest/index.ts';
|
|
2
|
+
export declare const SHORTEST_KEY = 8;
|
|
3
|
+
export declare const looksLive: (text: string) => boolean;
|
|
4
|
+
export declare function withoutKeys(text: string): string;
|
|
5
|
+
export declare function withoutKeysDeep<T>(value: T): T;
|
|
6
|
+
export declare function maskKeysIn(file: string): void;
|
|
7
|
+
export type KeyState = 'found' | 'missing' | 'live' | 'short' | 'unreadable';
|
|
8
|
+
export interface KeyLook {
|
|
9
|
+
state: KeyState;
|
|
10
|
+
says: string;
|
|
11
|
+
value: string | null;
|
|
12
|
+
}
|
|
13
|
+
export declare function lookUp(k: Key): Promise<KeyLook>;
|
|
14
|
+
export declare function knowKeys(m: Manifest, s: Service): Promise<void>;
|
|
15
|
+
export declare function keysFor(m: Manifest, s: Service): Promise<Record<string, string>>;
|
|
16
|
+
export declare function keep(k: Key, value: string): Promise<void>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { closeSync, openSync, readFileSync, writeSync } from 'node:fs';
|
|
2
|
+
import { RehearseError } from '../errors.js';
|
|
3
|
+
import { storeOf } from './store.js';
|
|
4
|
+
import { ask, firstLine } from './tool.js';
|
|
5
|
+
export const SHORTEST_KEY = 8;
|
|
6
|
+
const SAYS_LIVE = /(?:^|[^a-z0-9])(?:live|prod|production)(?:[^a-z0-9]|$)/i;
|
|
7
|
+
export const looksLive = (text) => SAYS_LIVE.test(text);
|
|
8
|
+
const givenHere = new Map();
|
|
9
|
+
export function withoutKeys(text) {
|
|
10
|
+
let out = text;
|
|
11
|
+
for (const [value, name] of [...givenHere].sort(([a], [b]) => b.length - a.length))
|
|
12
|
+
out = out.split(value).join(`{{key:${name}}}`);
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
export function withoutKeysDeep(value) {
|
|
16
|
+
if (givenHere.size === 0)
|
|
17
|
+
return value;
|
|
18
|
+
const walk = (v) => typeof v === 'string'
|
|
19
|
+
? withoutKeys(v)
|
|
20
|
+
: Array.isArray(v)
|
|
21
|
+
? v.map(walk)
|
|
22
|
+
: v && typeof v === 'object'
|
|
23
|
+
? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x)]))
|
|
24
|
+
: v;
|
|
25
|
+
return walk(value);
|
|
26
|
+
}
|
|
27
|
+
export function maskKeysIn(file) {
|
|
28
|
+
if (givenHere.size === 0)
|
|
29
|
+
return;
|
|
30
|
+
let fd;
|
|
31
|
+
try {
|
|
32
|
+
fd = openSync(file, 'r+');
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const text = readFileSync(fd);
|
|
39
|
+
for (const value of givenHere.keys()) {
|
|
40
|
+
const bytes = Buffer.from(value);
|
|
41
|
+
for (let at = text.indexOf(bytes); at !== -1; at = text.indexOf(bytes, at + bytes.length))
|
|
42
|
+
writeSync(fd, Buffer.alloc(bytes.length, '*'), 0, bytes.length, at);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
closeSync(fd);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function fromManager(k) {
|
|
50
|
+
const a = await ask('op', ['read', '--no-newline', k.from]);
|
|
51
|
+
if (a.code === 0)
|
|
52
|
+
return judged(k, a.out);
|
|
53
|
+
return {
|
|
54
|
+
state: 'unreadable',
|
|
55
|
+
value: null,
|
|
56
|
+
says: a.missing
|
|
57
|
+
? `It comes from ${k.from}, read through 1Password's \`op\`, which isn't installed here.`
|
|
58
|
+
: `1Password's \`op\` couldn't read it from ${k.from}: ${firstLine(a.err) || `exit ${a.code}`}.`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function judged(k, value) {
|
|
62
|
+
const add = k.from ? `Change it in ${k.from}` : `Add a test key with \`rehearse keys add ${k.name}\``;
|
|
63
|
+
if (value === '')
|
|
64
|
+
return {
|
|
65
|
+
state: 'missing',
|
|
66
|
+
value: null,
|
|
67
|
+
says: `It's empty ${k.from ? `in ${k.from}` : 'on this computer'}. ${add}.`,
|
|
68
|
+
};
|
|
69
|
+
if (looksLive(value))
|
|
70
|
+
return {
|
|
71
|
+
state: 'live',
|
|
72
|
+
value: null,
|
|
73
|
+
says: `It looks live: it says live or production, and Rehearse gives services test keys only. ${add}.`,
|
|
74
|
+
};
|
|
75
|
+
if (value.length < SHORTEST_KEY)
|
|
76
|
+
return {
|
|
77
|
+
state: 'short',
|
|
78
|
+
value: null,
|
|
79
|
+
says: `It's ${value.length} characters, and a key has at least ${SHORTEST_KEY}, so Rehearse can keep it out of what it prints. ${add}.`,
|
|
80
|
+
};
|
|
81
|
+
return { state: 'found', value, says: "It's there." };
|
|
82
|
+
}
|
|
83
|
+
export async function lookUp(k) {
|
|
84
|
+
if (k.from && looksLive(k.from))
|
|
85
|
+
return {
|
|
86
|
+
state: 'live',
|
|
87
|
+
value: null,
|
|
88
|
+
says: `It comes from ${k.from}, which names a live or production key, and Rehearse gives services test keys only.`,
|
|
89
|
+
};
|
|
90
|
+
if (k.from)
|
|
91
|
+
return fromManager(k);
|
|
92
|
+
try {
|
|
93
|
+
const value = await storeOf().read(k.name);
|
|
94
|
+
if (value === null)
|
|
95
|
+
return {
|
|
96
|
+
state: 'missing',
|
|
97
|
+
value: null,
|
|
98
|
+
says: `It isn't kept on this computer: add it with \`rehearse keys add ${k.name}\`.${k.find ? ` Where to find it: ${k.find}` : ''}`,
|
|
99
|
+
};
|
|
100
|
+
return judged(k, value);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
return { state: 'unreadable', value: null, says: error.message };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const CODES = {
|
|
107
|
+
missing: 'no_key',
|
|
108
|
+
live: 'live_key',
|
|
109
|
+
short: 'key',
|
|
110
|
+
unreadable: 'key',
|
|
111
|
+
};
|
|
112
|
+
export async function knowKeys(m, s) {
|
|
113
|
+
for (const name of new Set(Object.values(s.keys))) {
|
|
114
|
+
const look = await lookUp(m.keys[name]);
|
|
115
|
+
if (look.state === 'found')
|
|
116
|
+
givenHere.set(look.value, name);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export async function keysFor(m, s) {
|
|
120
|
+
const given = {};
|
|
121
|
+
for (const [env, name] of Object.entries(s.keys)) {
|
|
122
|
+
const look = await lookUp(m.keys[name]);
|
|
123
|
+
if (look.state !== 'found')
|
|
124
|
+
throw new RehearseError(CODES[look.state], `${s.name} needs the key ${name}. ${look.says}`);
|
|
125
|
+
givenHere.set(look.value, name);
|
|
126
|
+
given[env] = look.value;
|
|
127
|
+
}
|
|
128
|
+
return given;
|
|
129
|
+
}
|
|
130
|
+
export async function keep(k, value) {
|
|
131
|
+
if (k.from)
|
|
132
|
+
throw new RehearseError('usage', `${k.name} comes from ${k.from}: add or change it there, not on this computer.`, { exit: 2 });
|
|
133
|
+
if (!/^[\x21-\x7e]+$/.test(value))
|
|
134
|
+
throw new RehearseError('key', `Rehearse doesn't keep ${k.name}: a key it keeps is letters, digits and punctuation, with no spaces, control characters or other alphabets.`);
|
|
135
|
+
const look = judged(k, value);
|
|
136
|
+
if (look.state !== 'found')
|
|
137
|
+
throw new RehearseError(CODES[look.state], `Rehearse doesn't keep ${k.name}. ${look.says}`);
|
|
138
|
+
await storeOf().keep(k.name, value);
|
|
139
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Key, Manifest } from '../manifest/index.ts';
|
|
2
|
+
import { type KeyState } from './keys.ts';
|
|
3
|
+
export interface KeyRow {
|
|
4
|
+
name: string;
|
|
5
|
+
from: string | null;
|
|
6
|
+
find: string | null;
|
|
7
|
+
services: {
|
|
8
|
+
service: string;
|
|
9
|
+
env: string;
|
|
10
|
+
}[];
|
|
11
|
+
state?: KeyState;
|
|
12
|
+
says?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface KeysOut {
|
|
15
|
+
v: 1;
|
|
16
|
+
kept: string;
|
|
17
|
+
keys: KeyRow[];
|
|
18
|
+
}
|
|
19
|
+
export declare function keysOut(m: Manifest, o?: {
|
|
20
|
+
check?: boolean;
|
|
21
|
+
}): Promise<KeysOut>;
|
|
22
|
+
export declare function keyNamed(m: Manifest, name: string): Key;
|
|
23
|
+
export declare function keepKey(m: Manifest, name: string, value: string): Promise<KeysOut>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RehearseError } from '../errors.js';
|
|
2
|
+
import { keep, lookUp } from './keys.js';
|
|
3
|
+
import { storeOf } from './store.js';
|
|
4
|
+
function rowOf(m, k, look) {
|
|
5
|
+
return {
|
|
6
|
+
name: k.name,
|
|
7
|
+
from: k.from,
|
|
8
|
+
find: k.find,
|
|
9
|
+
services: Object.values(m.services).flatMap((s) => Object.entries(s.keys)
|
|
10
|
+
.filter(([, name]) => name === k.name)
|
|
11
|
+
.map(([env]) => ({ service: s.name, env }))),
|
|
12
|
+
...(look && { state: look.state, says: look.says }),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export async function keysOut(m, o = {}) {
|
|
16
|
+
const keys = [];
|
|
17
|
+
for (const k of Object.values(m.keys))
|
|
18
|
+
keys.push(rowOf(m, k, o.check ? await lookUp(k) : null));
|
|
19
|
+
return { v: 1, kept: storeOf().called, keys };
|
|
20
|
+
}
|
|
21
|
+
export function keyNamed(m, name) {
|
|
22
|
+
const k = m.keys[name];
|
|
23
|
+
if (!k)
|
|
24
|
+
throw new RehearseError('no_key', `The manifest names no key “${name}”${Object.keys(m.keys).length ? `: it names ${Object.keys(m.keys).join(', ')}` : ''}.`);
|
|
25
|
+
return k;
|
|
26
|
+
}
|
|
27
|
+
export async function keepKey(m, name, value) {
|
|
28
|
+
const k = keyNamed(m, name);
|
|
29
|
+
await keep(k, value);
|
|
30
|
+
return { v: 1, kept: storeOf().called, keys: [rowOf(m, k, await lookUp(k))] };
|
|
31
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const KEPT_UNDER = "rehearse";
|
|
2
|
+
export interface KeyStore {
|
|
3
|
+
called: string;
|
|
4
|
+
read(name: string): Promise<string | null>;
|
|
5
|
+
keep(name: string, value: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare const storeOf: (platform?: NodeJS.Platform) => KeyStore;
|