@operad-io/engine 0.1.1 → 0.2.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/config.schema.json +2 -1
- package/package.json +1 -1
- package/src/config/config.ts +74 -45
- package/src/flow/types.ts +4 -0
- package/src/flow/validate.ts +5 -0
- package/src/http/index.ts +2 -0
- package/src/http/plugins.ts +66 -0
- package/src/http/server.ts +342 -48
- package/src/kernel/decide.ts +115 -30
- package/src/kernel/state.ts +9 -0
- package/src/kernel/types.ts +25 -40
- package/src/log/open-log.ts +2 -5
- package/src/log/sqlite.ts +127 -85
- package/src/log/types.ts +35 -9
- package/src/loop/code-watch.ts +108 -2
- package/src/loop/older-code.ts +3 -4
- package/src/loop/serve.ts +122 -95
- package/src/notify/notify.ts +2 -6
- package/src/platform/atomic-write.ts +163 -0
- package/src/platform/file-lock.ts +43 -21
- package/src/platform/index.ts +2 -0
- package/src/platform/kept-workers.ts +290 -0
- package/src/platform/process-group.ts +11 -3
- package/src/platform/sqlite-open.ts +20 -6
- package/src/platform/sqlite-schema.ts +30 -0
- package/src/platform/step-reap.ts +33 -18
- package/src/platform/watch.ts +1 -11
- package/src/plugin/action-schema.ts +59 -1
- package/src/plugin/choice-info.ts +133 -0
- package/src/plugin/context.ts +279 -0
- package/src/plugin/declared-flows.ts +3 -10
- package/src/plugin/errors.ts +9 -0
- package/src/plugin/freeze.ts +12 -0
- package/src/plugin/harness.ts +28 -0
- package/src/plugin/index.ts +8 -7
- package/src/plugin/install.ts +265 -17
- package/src/plugin/keys.ts +2 -12
- package/src/plugin/load.ts +47 -23
- package/src/plugin/logger.ts +14 -0
- package/src/plugin/open-store.ts +0 -6
- package/src/plugin/question-form.ts +323 -0
- package/src/plugin/registry.ts +512 -76
- package/src/plugin/secrets.ts +14 -24
- package/src/plugin/semver.ts +52 -0
- package/src/plugin/settings.ts +1 -9
- package/src/plugin/sqlite-store.ts +94 -64
- package/src/plugin/store.ts +5 -0
- package/src/plugin/types.ts +566 -21
- package/src/plugin/validate.ts +396 -26
- package/src/runner/answer.ts +26 -68
- package/src/runner/ask.ts +227 -28
- package/src/runner/cancel.ts +10 -7
- package/src/runner/grants.ts +2 -2
- package/src/runner/index.ts +1 -3
- package/src/runner/limits.ts +2 -2
- package/src/runner/runner.ts +15 -14
- package/src/runner/service.ts +52 -19
- package/src/runner/step-worker.ts +9 -0
- package/src/testing/answer.ts +69 -0
- package/src/testing/clock-manual.ts +152 -0
- package/src/testing/config.ts +16 -0
- package/src/testing/fs.ts +172 -0
- package/src/testing/home.ts +43 -0
- package/src/testing/index.ts +19 -0
- package/src/testing/types.ts +2 -0
- package/src/testing/world.ts +9 -2
- package/src/trigger/kernel.ts +13 -46
- package/src/trigger/rules.ts +22 -12
- package/src/trigger/types.ts +2 -6
- package/src/util.ts +99 -0
- package/src/views/event-line.ts +22 -0
- package/src/views/history.ts +36 -19
- package/src/views/live-logs.ts +313 -0
- package/src/views/pending.ts +23 -5
- package/src/views/presentation.ts +629 -81
- package/src/views/running.ts +5 -5
- package/src/views/settings.ts +205 -66
- package/src/views/types.ts +3 -2
- package/src/views/uploads.ts +15 -7
- package/triggers.schema.json +4 -0
package/src/views/running.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { evolve, initialState } from "../kernel/state.ts";
|
|
|
6
6
|
import type { KernelState } from "../kernel/types.ts";
|
|
7
7
|
import { queuedSteps } from "../runner/limits.ts";
|
|
8
8
|
import { parseObjectKey } from "../runner/service.ts";
|
|
9
|
+
import { isRecord } from "../util.ts";
|
|
9
10
|
import type { Views } from "./presentation.ts";
|
|
10
11
|
|
|
11
12
|
export interface RunningItem {
|
|
@@ -60,10 +61,7 @@ async function readRun(
|
|
|
60
61
|
queues.clear();
|
|
61
62
|
}
|
|
62
63
|
const payload = fact.payload;
|
|
63
|
-
const effectId =
|
|
64
|
-
typeof payload === "object" && payload !== null && !Array.isArray(payload)
|
|
65
|
-
? payload.effectId
|
|
66
|
-
: undefined;
|
|
64
|
+
const effectId = isRecord(payload) ? payload.effectId : undefined;
|
|
67
65
|
if (typeof effectId !== "string") continue;
|
|
68
66
|
if (fact.kind === "step_queued") {
|
|
69
67
|
if (!queues.has(effectId)) queues.set(effectId, fact.time);
|
|
@@ -115,7 +113,9 @@ export async function runTimes(
|
|
|
115
113
|
/** The log's runs, when the caller already read them for another run. */
|
|
116
114
|
known?: Promise<Awaited<ReturnType<LogBackend["runs"]>>>,
|
|
117
115
|
): Promise<RunTimes | undefined> {
|
|
118
|
-
const info =
|
|
116
|
+
const info = known === undefined
|
|
117
|
+
? await log.run(runId)
|
|
118
|
+
: (await known).find((run) => run.runId === runId);
|
|
119
119
|
if (info === undefined) return undefined;
|
|
120
120
|
let state = initialState();
|
|
121
121
|
let startedAt = info.startedAt;
|
package/src/views/settings.ts
CHANGED
|
@@ -4,20 +4,25 @@
|
|
|
4
4
|
* keep under a registered repository's `.operad/` (project).
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { basename, dirname, join } from "node:path";
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import { join } from "node:path";
|
|
10
9
|
import { ActionArgsError, validateSchemaValue } from "../plugin/action-schema.ts";
|
|
11
10
|
import { configPath } from "../config/config.ts";
|
|
12
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
withLockFile,
|
|
13
|
+
writeFileAtomic,
|
|
14
|
+
type AsyncAtomicWriteOptions,
|
|
15
|
+
} from "../platform/index.ts";
|
|
13
16
|
import type { JsonValue } from "../log/types.ts";
|
|
14
17
|
import type { PluginRegistry } from "../plugin/registry.ts";
|
|
15
18
|
import type {
|
|
16
19
|
ActionArgSchema,
|
|
17
20
|
ActionArgsSchema,
|
|
18
21
|
ObjectStore,
|
|
22
|
+
ProjectSettingsFile,
|
|
23
|
+
SettingsRepository,
|
|
19
24
|
} from "../plugin/types.ts";
|
|
20
|
-
import { isRecord } from "../util.ts";
|
|
25
|
+
import { isMissingFile, isRecord } from "../util.ts";
|
|
21
26
|
|
|
22
27
|
/** The scope of a setting in `config.json`. */
|
|
23
28
|
export const GLOBAL_SCOPE = "global";
|
|
@@ -33,6 +38,8 @@ export interface SettingInfo {
|
|
|
33
38
|
readonly scope: string;
|
|
34
39
|
readonly value?: JsonValue | undefined;
|
|
35
40
|
readonly default?: JsonValue | undefined;
|
|
41
|
+
/** The short name the schema gives the setting. */
|
|
42
|
+
readonly title?: string | undefined;
|
|
36
43
|
readonly description?: string | undefined;
|
|
37
44
|
/** The schema's type and allowed values, so a client can offer a choice list. */
|
|
38
45
|
readonly type?: string | undefined;
|
|
@@ -46,7 +53,9 @@ export type SettingsErrorKind =
|
|
|
46
53
|
| "args"
|
|
47
54
|
| "not-available"
|
|
48
55
|
| "confirm"
|
|
49
|
-
| "ambiguous"
|
|
56
|
+
| "ambiguous"
|
|
57
|
+
/** The plugin's own check refused the change; the message is its reason. */
|
|
58
|
+
| "refused";
|
|
50
59
|
|
|
51
60
|
/** A settings call refused; nothing was written. */
|
|
52
61
|
export class SettingsError extends Error {
|
|
@@ -66,54 +75,7 @@ export interface SettingsOptions {
|
|
|
66
75
|
}
|
|
67
76
|
|
|
68
77
|
/** Hooks a test uses to stop a write between its two steps. */
|
|
69
|
-
export
|
|
70
|
-
/** Called once the temporary file is complete, before it replaces the target. */
|
|
71
|
-
readonly beforeRename?: (() => Promise<void> | void) | undefined;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function isMissing(error: unknown): boolean {
|
|
75
|
-
return (
|
|
76
|
-
typeof error === "object" &&
|
|
77
|
-
error !== null &&
|
|
78
|
-
"code" in error &&
|
|
79
|
-
error.code === "ENOENT"
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Writes a file by writing a temporary file in the same directory and renaming
|
|
85
|
-
* it over the target, so a crash leaves the old file whole.
|
|
86
|
-
*/
|
|
87
|
-
export async function writeFileAtomic(
|
|
88
|
-
path: string,
|
|
89
|
-
text: string,
|
|
90
|
-
hooks: AtomicWriteHooks = {},
|
|
91
|
-
): Promise<void> {
|
|
92
|
-
await mkdir(dirname(path), { recursive: true });
|
|
93
|
-
const temp = join(
|
|
94
|
-
dirname(path),
|
|
95
|
-
`.${basename(path)}.${process.pid}.${randomUUID()}.tmp`,
|
|
96
|
-
);
|
|
97
|
-
const mode = await stat(path).then(
|
|
98
|
-
(info) => info.mode & 0o777,
|
|
99
|
-
() => undefined,
|
|
100
|
-
);
|
|
101
|
-
try {
|
|
102
|
-
const handle = await open(temp, "w");
|
|
103
|
-
try {
|
|
104
|
-
await handle.writeFile(text);
|
|
105
|
-
await handle.sync();
|
|
106
|
-
} finally {
|
|
107
|
-
await handle.close();
|
|
108
|
-
}
|
|
109
|
-
if (mode !== undefined) await chmod(temp, mode);
|
|
110
|
-
await hooks.beforeRename?.();
|
|
111
|
-
await rename(temp, path);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
await rm(temp, { force: true }).catch(() => undefined);
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
78
|
+
export type AtomicWriteHooks = AsyncAtomicWriteOptions;
|
|
117
79
|
|
|
118
80
|
interface Leaf {
|
|
119
81
|
/** The dotted path inside the file's JSON. */
|
|
@@ -179,7 +141,7 @@ async function readJsonFile(path: string, absent: () => Record<string, unknown>)
|
|
|
179
141
|
try {
|
|
180
142
|
text = await readFile(path, "utf8");
|
|
181
143
|
} catch (error) {
|
|
182
|
-
if (
|
|
144
|
+
if (isMissingFile(error)) return { value: absent(), indent: " ", newline: true };
|
|
183
145
|
throw error;
|
|
184
146
|
}
|
|
185
147
|
let parsed: unknown;
|
|
@@ -213,16 +175,51 @@ interface Place {
|
|
|
213
175
|
readonly file: string;
|
|
214
176
|
readonly inside: readonly string[];
|
|
215
177
|
readonly fresh: () => Record<string, unknown>;
|
|
178
|
+
/** A project file's repository and the plugin's own hooks for it. */
|
|
179
|
+
readonly project?: {
|
|
180
|
+
readonly repository: SettingsRepository;
|
|
181
|
+
readonly file: ProjectSettingsFile;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The plugin's own check of the file as it would be written; refuses with its reason. */
|
|
186
|
+
async function runCheck(place: Place, name: string, value: unknown): Promise<void> {
|
|
187
|
+
const project = place.project;
|
|
188
|
+
const check = project?.file.check;
|
|
189
|
+
if (project === undefined || check === undefined || !isRecord(value)) return;
|
|
190
|
+
const reason = await check({ repository: project.repository, value, name });
|
|
191
|
+
if (reason !== undefined) throw new SettingsError("refused", reason);
|
|
216
192
|
}
|
|
217
193
|
|
|
218
194
|
async function readPlace(place: Place): Promise<unknown> {
|
|
195
|
+
const project = place.project;
|
|
196
|
+
const derived = project?.file.derived;
|
|
197
|
+
let file: JsonFile | undefined;
|
|
219
198
|
try {
|
|
220
|
-
|
|
221
|
-
return valueAt(file.value, place.inside);
|
|
199
|
+
file = await readJsonFile(place.file, place.fresh);
|
|
222
200
|
} catch (error) {
|
|
223
|
-
if (error instanceof SettingsError)
|
|
224
|
-
|
|
201
|
+
if (!(error instanceof SettingsError)) throw error;
|
|
202
|
+
}
|
|
203
|
+
if (file === undefined) {
|
|
204
|
+
if (project === undefined || derived === undefined) return undefined;
|
|
205
|
+
// A derived value may still explain why the file cannot be read (dev's
|
|
206
|
+
// flow list names the broken dev.json); with no hook there is nothing.
|
|
207
|
+
try {
|
|
208
|
+
return await derived({ repository: project.repository, value: {}, name: "" });
|
|
209
|
+
} catch {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const stored = valueAt(file.value, place.inside);
|
|
214
|
+
if (project !== undefined && derived !== undefined) {
|
|
215
|
+
// A stored value wins; the derived value fills only a property the file
|
|
216
|
+
// does not store.
|
|
217
|
+
return {
|
|
218
|
+
...(await derived({ repository: project.repository, value: file.value, name: "" })),
|
|
219
|
+
...(isRecord(stored) ? stored : {}),
|
|
220
|
+
};
|
|
225
221
|
}
|
|
222
|
+
return stored;
|
|
226
223
|
}
|
|
227
224
|
|
|
228
225
|
async function places(
|
|
@@ -270,6 +267,7 @@ async function places(
|
|
|
270
267
|
file: join(repository.path, ".operad", file.path),
|
|
271
268
|
inside: [],
|
|
272
269
|
fresh: () => ({}),
|
|
270
|
+
project: { repository, file },
|
|
273
271
|
});
|
|
274
272
|
}
|
|
275
273
|
}
|
|
@@ -328,6 +326,7 @@ function infoOf(place: Place, leaf: Leaf, section: unknown): SettingInfo {
|
|
|
328
326
|
...(leaf.schema.default === undefined
|
|
329
327
|
? {}
|
|
330
328
|
: { default: leaf.schema.default as JsonValue }),
|
|
329
|
+
...(leaf.schema.title === undefined ? {} : { title: leaf.schema.title }),
|
|
331
330
|
...(leaf.schema.description === undefined
|
|
332
331
|
? {}
|
|
333
332
|
: { description: leaf.schema.description }),
|
|
@@ -368,17 +367,41 @@ export async function setSetting(
|
|
|
368
367
|
): Promise<SettingInfo> {
|
|
369
368
|
const scope = set.scope ?? GLOBAL_SCOPE;
|
|
370
369
|
let match: { readonly place: Place; readonly leaf: Leaf } | undefined;
|
|
370
|
+
// A name deeper than an editable setting that holds a map or an object
|
|
371
|
+
// changes one entry of it: `bot.bots.<name>` or `bot.bots.<name>.<field>`.
|
|
372
|
+
let deeper: { readonly place: Place; readonly leaf: Leaf; readonly sub: string[] } | undefined;
|
|
373
|
+
// A deeper name under a setting that is not editable is read-only, not unknown.
|
|
374
|
+
let blocked: { readonly place: Place; readonly leaf: Leaf; readonly sub: string[] } | undefined;
|
|
371
375
|
for (const place of await places(options, scope)) {
|
|
372
376
|
const prefix = `${place.prefix}.`;
|
|
373
377
|
if (!name.startsWith(prefix)) continue;
|
|
374
378
|
const path = name.slice(prefix.length);
|
|
375
|
-
const
|
|
379
|
+
const found = leaves(place.schema);
|
|
380
|
+
const leaf = found.find((item) => item.path.join(".") === path);
|
|
376
381
|
if (leaf !== undefined) {
|
|
377
382
|
match = { place, leaf };
|
|
378
383
|
break;
|
|
379
384
|
}
|
|
385
|
+
if (deeper === undefined && blocked === undefined) {
|
|
386
|
+
const parent = found.find((item) => path.startsWith(`${item.path.join(".")}.`));
|
|
387
|
+
if (parent !== undefined) {
|
|
388
|
+
const entry = {
|
|
389
|
+
place,
|
|
390
|
+
leaf: parent,
|
|
391
|
+
sub: path.slice(parent.path.join(".").length + 1).split("."),
|
|
392
|
+
};
|
|
393
|
+
if (parent.schema.editable === true) deeper = entry;
|
|
394
|
+
else blocked = entry;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (match === undefined && deeper !== undefined) {
|
|
399
|
+
return setEntry(deeper.place, deeper.leaf, deeper.sub, name, value, scope, set);
|
|
380
400
|
}
|
|
381
401
|
if (match === undefined) {
|
|
402
|
+
if (blocked !== undefined) {
|
|
403
|
+
throw new SettingsError("not-available", `setting "${name}" cannot be changed here`);
|
|
404
|
+
}
|
|
382
405
|
throw new SettingsError(
|
|
383
406
|
"not-found",
|
|
384
407
|
scope === GLOBAL_SCOPE
|
|
@@ -391,6 +414,14 @@ export async function setSetting(
|
|
|
391
414
|
if (leaf.schema.editable !== true) {
|
|
392
415
|
throw new SettingsError("not-available", `setting "${name}" cannot be changed here`);
|
|
393
416
|
}
|
|
417
|
+
// Confirmation comes before the value check at both depths, so a value that
|
|
418
|
+
// is both invalid and unconfirmed answers the same way everywhere.
|
|
419
|
+
if (leaf.schema.confirm === true && set.confirmed !== true) {
|
|
420
|
+
throw new SettingsError(
|
|
421
|
+
"confirm",
|
|
422
|
+
`setting "${name}" needs confirmation`,
|
|
423
|
+
);
|
|
424
|
+
}
|
|
394
425
|
try {
|
|
395
426
|
validateSchemaValue(leaf.schema, value, name, "value");
|
|
396
427
|
} catch (error) {
|
|
@@ -401,12 +432,6 @@ export async function setSetting(
|
|
|
401
432
|
}
|
|
402
433
|
throw error;
|
|
403
434
|
}
|
|
404
|
-
if (leaf.schema.confirm === true && set.confirmed !== true) {
|
|
405
|
-
throw new SettingsError(
|
|
406
|
-
"confirm",
|
|
407
|
-
`setting "${name}" needs confirmation`,
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
435
|
|
|
411
436
|
return withLockFile(`${place.file}.lock`, async () => {
|
|
412
437
|
const file = await readJsonFile(place.file, place.fresh);
|
|
@@ -445,8 +470,122 @@ export async function setSetting(
|
|
|
445
470
|
}
|
|
446
471
|
throw error;
|
|
447
472
|
}
|
|
473
|
+
await runCheck(place, name, valueAt(file.value, place.inside));
|
|
448
474
|
await writeFileAtomic(place.file, render(file), set.hooks);
|
|
449
475
|
|
|
450
476
|
return infoOf(place, leaf, valueAt(file.value, place.inside));
|
|
451
477
|
});
|
|
452
478
|
}
|
|
479
|
+
|
|
480
|
+
const UNSAFE_KEYS: ReadonlySet<string> = new Set(["__proto__", "constructor", "prototype"]);
|
|
481
|
+
|
|
482
|
+
/** The schema of the entry `sub` names inside a setting, when the schema declares one. */
|
|
483
|
+
function entrySchema(
|
|
484
|
+
schema: ActionArgSchema,
|
|
485
|
+
sub: readonly string[],
|
|
486
|
+
): ActionArgSchema | undefined {
|
|
487
|
+
let node: ActionArgSchema | undefined = schema;
|
|
488
|
+
for (const key of sub) {
|
|
489
|
+
// Never a key that names something every object inherits.
|
|
490
|
+
if (key.length === 0 || UNSAFE_KEYS.has(key) || node === undefined) return undefined;
|
|
491
|
+
// Only a property the schema declares itself; an inherited name such as
|
|
492
|
+
// `toString` is not an entry schema.
|
|
493
|
+
const properties: Record<string, ActionArgSchema> | undefined = node.properties;
|
|
494
|
+
const next: ActionArgSchema | undefined =
|
|
495
|
+
properties !== undefined && Object.hasOwn(properties, key)
|
|
496
|
+
? properties[key]
|
|
497
|
+
: undefined;
|
|
498
|
+
node =
|
|
499
|
+
next ??
|
|
500
|
+
(typeof node.additionalProperties === "object" ? node.additionalProperties : undefined);
|
|
501
|
+
}
|
|
502
|
+
return node;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function refuseArgs(error: unknown): never {
|
|
506
|
+
if (error instanceof ActionArgsError) {
|
|
507
|
+
throw new SettingsError("args", error.message.replace(": args.", ": "), {
|
|
508
|
+
cause: error,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
throw error;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Changes one entry inside an editable setting, under the same rules as the
|
|
516
|
+
* setting (editable, confirm, schema). The file is read and written under its
|
|
517
|
+
* lock, so another entry changed meanwhile is kept. `null` removes the entry.
|
|
518
|
+
*/
|
|
519
|
+
async function setEntry(
|
|
520
|
+
place: Place,
|
|
521
|
+
parent: Leaf,
|
|
522
|
+
sub: readonly string[],
|
|
523
|
+
name: string,
|
|
524
|
+
value: unknown,
|
|
525
|
+
scope: string,
|
|
526
|
+
set: SetOptions,
|
|
527
|
+
): Promise<SettingInfo> {
|
|
528
|
+
const schema = entrySchema(parent.schema, sub);
|
|
529
|
+
if (schema === undefined) {
|
|
530
|
+
throw new SettingsError(
|
|
531
|
+
"not-found",
|
|
532
|
+
scope === GLOBAL_SCOPE
|
|
533
|
+
? `no setting "${name}"`
|
|
534
|
+
: `no setting "${name}" for repository "${scope}"`,
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
const remove = value === null;
|
|
538
|
+
// The same order as a setting itself: confirmation, then the value.
|
|
539
|
+
if (parent.schema.confirm === true && set.confirmed !== true) {
|
|
540
|
+
throw new SettingsError("confirm", `setting "${name}" needs confirmation`);
|
|
541
|
+
}
|
|
542
|
+
if (!remove) {
|
|
543
|
+
try {
|
|
544
|
+
validateSchemaValue(schema, value, name, "value");
|
|
545
|
+
} catch (error) {
|
|
546
|
+
refuseArgs(error);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const path = [...parent.path, ...sub];
|
|
550
|
+
const leaf: Leaf = {
|
|
551
|
+
path,
|
|
552
|
+
schema: { ...schema, editable: true, confirm: parent.schema.confirm === true },
|
|
553
|
+
};
|
|
554
|
+
return withLockFile(`${place.file}.lock`, async () => {
|
|
555
|
+
const file = await readJsonFile(place.file, place.fresh);
|
|
556
|
+
if (place.scope === GLOBAL_SCOPE) {
|
|
557
|
+
const plugins = file.value.plugins;
|
|
558
|
+
const plugin = place.inside[1] ?? "";
|
|
559
|
+
if (!isRecord(plugins)) {
|
|
560
|
+
throw new SettingsError(
|
|
561
|
+
"args",
|
|
562
|
+
`${place.file}: plugins must be an object keyed by plugin name to change a setting`,
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
const entry = plugins[plugin];
|
|
566
|
+
if (typeof entry === "string") {
|
|
567
|
+
plugins[plugin] = { module: entry, enabled: true };
|
|
568
|
+
} else if (!isRecord(entry)) {
|
|
569
|
+
throw new SettingsError(
|
|
570
|
+
"not-found",
|
|
571
|
+
`${place.file}: plugin "${plugin}" is not listed`,
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (remove) {
|
|
576
|
+
const holder = valueAt(valueAt(file.value, place.inside), path.slice(0, -1));
|
|
577
|
+
const last = path[path.length - 1];
|
|
578
|
+
if (isRecord(holder) && last !== undefined) delete holder[last];
|
|
579
|
+
} else {
|
|
580
|
+
setAt(file.value, [...place.inside, ...path], value);
|
|
581
|
+
}
|
|
582
|
+
try {
|
|
583
|
+
validateSchemaValue(place.schema, valueAt(file.value, place.inside), name, "settings");
|
|
584
|
+
} catch (error) {
|
|
585
|
+
refuseArgs(error);
|
|
586
|
+
}
|
|
587
|
+
await runCheck(place, name, valueAt(file.value, place.inside));
|
|
588
|
+
await writeFileAtomic(place.file, render(file), set.hooks);
|
|
589
|
+
return infoOf(place, leaf, valueAt(file.value, place.inside));
|
|
590
|
+
});
|
|
591
|
+
}
|
package/src/views/types.ts
CHANGED
|
@@ -53,8 +53,9 @@ export function listTypes(registry: PluginRegistry): readonly ViewTypeInfo[] {
|
|
|
53
53
|
...new Set(
|
|
54
54
|
registry
|
|
55
55
|
.actions()
|
|
56
|
-
.filter((action) => action.creates === entry.type)
|
|
57
|
-
.map((action) => action.on)
|
|
56
|
+
.filter((action) => action.creates === entry.type && action.on !== undefined)
|
|
57
|
+
.map((action) => action.on)
|
|
58
|
+
.filter((on): on is string => on !== undefined),
|
|
58
59
|
),
|
|
59
60
|
];
|
|
60
61
|
types.push({
|
package/src/views/uploads.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { randomUUID } from "node:crypto";
|
|
9
9
|
import { readFileSync } from "node:fs";
|
|
10
|
-
import { mkdir, open, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
10
|
+
import { mkdir, open, readdir, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
11
11
|
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
12
12
|
import { isRecord } from "../util.ts";
|
|
13
13
|
|
|
@@ -17,9 +17,6 @@ export const MAX_UPLOAD_BYTES = 20 * 1024 * 1024;
|
|
|
17
17
|
/** How long an upload nobody used stays, in milliseconds. */
|
|
18
18
|
export const UPLOAD_TTL_MS = 60 * 60 * 1000;
|
|
19
19
|
|
|
20
|
-
/** The file name an action's `format` names: an argument that is a file. */
|
|
21
|
-
export const FILE_FORMAT = "file";
|
|
22
|
-
|
|
23
20
|
/** The upload is larger than {@link MAX_UPLOAD_BYTES}. */
|
|
24
21
|
export class UploadTooLargeError extends Error {
|
|
25
22
|
readonly limit: number;
|
|
@@ -90,7 +87,15 @@ export function attachmentUrl(home: string, path: string): string | undefined {
|
|
|
90
87
|
return `/v1/attachments/${rel.split(sep).map(encodeURIComponent).join("/")}`;
|
|
91
88
|
}
|
|
92
89
|
|
|
93
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* The stored file a `/v1/attachments/<rel>` request names, with its type;
|
|
92
|
+
* undefined when there is none. Both the root and the file are resolved, so a
|
|
93
|
+
* symbolic link whose target is outside the attachment directories is not
|
|
94
|
+
* served; a home that is itself reached through a link still works. The type
|
|
95
|
+
* comes from the `.meta.json` beside the name the request asked for, not from
|
|
96
|
+
* the directory a link points into, so the name, the type and the checks that
|
|
97
|
+
* keep `.meta.json` itself out all describe the same file.
|
|
98
|
+
*/
|
|
94
99
|
export async function findAttachment(
|
|
95
100
|
home: string,
|
|
96
101
|
rel: readonly string[],
|
|
@@ -102,11 +107,14 @@ export async function findAttachment(
|
|
|
102
107
|
const path = join(root, ...rel);
|
|
103
108
|
if (!path.startsWith(`${root}${sep}`) || basename(path) === STORED_META) return undefined;
|
|
104
109
|
try {
|
|
105
|
-
|
|
110
|
+
const realRoot = await realpath(root);
|
|
111
|
+
const realPath = await realpath(path);
|
|
112
|
+
if (!realPath.startsWith(`${realRoot}${sep}`) || basename(realPath) === STORED_META) return undefined;
|
|
113
|
+
if (!(await stat(realPath)).isFile()) return undefined;
|
|
114
|
+
return { path: realPath, type: storedType(path) ?? "application/octet-stream", name: basename(path) };
|
|
106
115
|
} catch {
|
|
107
116
|
return undefined;
|
|
108
117
|
}
|
|
109
|
-
return { path, type: storedType(path) ?? "application/octet-stream", name: basename(path) };
|
|
110
118
|
}
|
|
111
119
|
|
|
112
120
|
export interface UploadsOptions {
|
package/triggers.schema.json
CHANGED
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"type": "string",
|
|
41
41
|
"minLength": 1,
|
|
42
42
|
"description": "A five-field cron expression in the local time zone."
|
|
43
|
+
},
|
|
44
|
+
"paused": {
|
|
45
|
+
"type": "boolean",
|
|
46
|
+
"description": "A paused cron rule fires no tick until it is resumed."
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
"required": ["source", "type", "start", "object", "mode"],
|