@operad-io/engine 0.0.0-stage → 0.1.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.
Files changed (102) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -2
  3. package/config.schema.json +188 -0
  4. package/keys.schema.json +12 -0
  5. package/package.json +22 -4
  6. package/src/config/config.ts +627 -0
  7. package/src/config/index.ts +1 -0
  8. package/src/flow/define-flow.ts +57 -0
  9. package/src/flow/index.ts +5 -0
  10. package/src/flow/load.ts +41 -0
  11. package/src/flow/types.ts +107 -0
  12. package/src/flow/validate.ts +296 -0
  13. package/src/http/address.ts +142 -0
  14. package/src/http/index.ts +25 -0
  15. package/src/http/link.ts +20 -0
  16. package/src/http/server.ts +920 -0
  17. package/src/http/token.ts +90 -0
  18. package/src/index.ts +18 -0
  19. package/src/kernel/decide.ts +689 -0
  20. package/src/kernel/index.ts +3 -0
  21. package/src/kernel/state.ts +275 -0
  22. package/src/kernel/types.ts +366 -0
  23. package/src/log/errors.ts +77 -0
  24. package/src/log/facts.ts +50 -0
  25. package/src/log/index.ts +6 -0
  26. package/src/log/json-value.ts +67 -0
  27. package/src/log/open-log.ts +17 -0
  28. package/src/log/schema.ts +5 -0
  29. package/src/log/secret.ts +30 -0
  30. package/src/log/sqlite.ts +1011 -0
  31. package/src/log/types.ts +230 -0
  32. package/src/loop/code-watch.ts +486 -0
  33. package/src/loop/index.ts +3 -0
  34. package/src/loop/older-code.ts +58 -0
  35. package/src/loop/serve.ts +483 -0
  36. package/src/markdown.ts +451 -0
  37. package/src/notify/index.ts +10 -0
  38. package/src/notify/notify.ts +256 -0
  39. package/src/platform/file-lock.ts +385 -0
  40. package/src/platform/index.ts +13 -0
  41. package/src/platform/load.ts +18 -0
  42. package/src/platform/process-group.ts +675 -0
  43. package/src/platform/process-watch.ts +152 -0
  44. package/src/platform/sqlite-open.ts +104 -0
  45. package/src/platform/step-reap.ts +668 -0
  46. package/src/platform/watch.ts +158 -0
  47. package/src/platform/worker-reports.ts +34 -0
  48. package/src/plugin/action-schema.ts +550 -0
  49. package/src/plugin/declared-flows.ts +74 -0
  50. package/src/plugin/define-plugin.ts +12 -0
  51. package/src/plugin/errors.ts +73 -0
  52. package/src/plugin/filters.ts +46 -0
  53. package/src/plugin/harness.ts +50 -0
  54. package/src/plugin/home.ts +13 -0
  55. package/src/plugin/index.ts +65 -0
  56. package/src/plugin/install.ts +268 -0
  57. package/src/plugin/keys.ts +63 -0
  58. package/src/plugin/load.ts +256 -0
  59. package/src/plugin/logger.ts +30 -0
  60. package/src/plugin/object-facts.ts +132 -0
  61. package/src/plugin/open-store.ts +32 -0
  62. package/src/plugin/paging.ts +133 -0
  63. package/src/plugin/registry.ts +1786 -0
  64. package/src/plugin/secrets.ts +141 -0
  65. package/src/plugin/semver.ts +110 -0
  66. package/src/plugin/settings.ts +64 -0
  67. package/src/plugin/sqlite-store.ts +756 -0
  68. package/src/plugin/store.ts +318 -0
  69. package/src/plugin/types.ts +1243 -0
  70. package/src/plugin/usage.ts +157 -0
  71. package/src/plugin/validate.ts +1028 -0
  72. package/src/runner/answer.ts +156 -0
  73. package/src/runner/ask.ts +483 -0
  74. package/src/runner/cancel.ts +51 -0
  75. package/src/runner/errors.ts +20 -0
  76. package/src/runner/grants.ts +92 -0
  77. package/src/runner/index.ts +33 -0
  78. package/src/runner/limits.ts +403 -0
  79. package/src/runner/lock.ts +140 -0
  80. package/src/runner/runner.ts +1175 -0
  81. package/src/runner/service.ts +184 -0
  82. package/src/runner/step-worker.ts +303 -0
  83. package/src/testing/clock.ts +67 -0
  84. package/src/testing/index.ts +90 -0
  85. package/src/testing/types.ts +116 -0
  86. package/src/testing/world.ts +369 -0
  87. package/src/trigger/cron.ts +193 -0
  88. package/src/trigger/index.ts +32 -0
  89. package/src/trigger/kernel.ts +1221 -0
  90. package/src/trigger/rules.ts +300 -0
  91. package/src/trigger/store.ts +233 -0
  92. package/src/trigger/types.ts +156 -0
  93. package/src/util.ts +50 -0
  94. package/src/views/history.ts +634 -0
  95. package/src/views/index.ts +7 -0
  96. package/src/views/pending.ts +209 -0
  97. package/src/views/presentation.ts +2609 -0
  98. package/src/views/running.ts +255 -0
  99. package/src/views/settings.ts +452 -0
  100. package/src/views/types.ts +72 -0
  101. package/src/views/uploads.ts +228 -0
  102. package/triggers.schema.json +48 -0
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Secrets live outside the fact log: one 0600 JSON file per plugin in a 0700
3
+ * directory under the operad home. A value only leaves as a `Secret`, so the
4
+ * log's existing refusal still protects it.
5
+ */
6
+
7
+ import { randomUUID } from "node:crypto";
8
+ import { chmod, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
9
+ import { join } from "node:path";
10
+ import { Secret } from "../log/secret.ts";
11
+ import type { SecretStore } from "./types.ts";
12
+ import { isRecord } from "../util.ts";
13
+
14
+ const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
15
+
16
+ /** The directory that holds every plugin's secret file. */
17
+ export function secretsDirectory(home: string): string {
18
+ return join(home, "secrets");
19
+ }
20
+
21
+ /** The file one plugin's secrets live in. */
22
+ export function secretsPathFor(home: string, plugin: string): string {
23
+ return join(secretsDirectory(home), `${plugin}.json`);
24
+ }
25
+
26
+ function assertName(value: string, kind: string): string {
27
+ if (!NAME.test(value)) {
28
+ throw new Error(`${kind} "${value}": must be letters, digits, dot, dash or underscore`);
29
+ }
30
+ return value;
31
+ }
32
+
33
+ /** One plugin's secrets. A missing file means the plugin has none. */
34
+ export class FileSecretStore implements SecretStore {
35
+ readonly #home: string;
36
+ readonly #plugin: string;
37
+
38
+ constructor(options: { readonly home: string; readonly plugin: string }) {
39
+ this.#home = options.home;
40
+ this.#plugin = assertName(options.plugin, "plugin");
41
+ }
42
+
43
+ async get(name: string): Promise<Secret<string> | undefined> {
44
+ assertName(name, "secret");
45
+ const values = await this.#read();
46
+ const value = values[name];
47
+ return typeof value === "string" ? new Secret(value) : undefined;
48
+ }
49
+
50
+ async set(name: string, value: string | Secret<string>): Promise<void> {
51
+ assertName(name, "secret");
52
+ const text = value instanceof Secret ? value.reveal() : value;
53
+ if (typeof text !== "string") {
54
+ throw new TypeError(`secret "${name}": value must be a string`);
55
+ }
56
+ const values = await this.#read();
57
+ values[name] = text;
58
+ await this.#write(values);
59
+ }
60
+
61
+ async delete(name: string): Promise<void> {
62
+ assertName(name, "secret");
63
+ const values = await this.#read();
64
+ if (!(name in values)) return;
65
+ delete values[name];
66
+ await this.#write(values);
67
+ }
68
+
69
+ async names(): Promise<readonly string[]> {
70
+ return Object.keys(await this.#read()).sort();
71
+ }
72
+
73
+ async #read(): Promise<Record<string, string>> {
74
+ const path = secretsPathFor(this.#home, this.#plugin);
75
+ let text: string;
76
+ try {
77
+ text = await readFile(path, "utf8");
78
+ } catch (error) {
79
+ if (
80
+ typeof error === "object" &&
81
+ error !== null &&
82
+ "code" in error &&
83
+ error.code === "ENOENT"
84
+ ) {
85
+ return {};
86
+ }
87
+ throw new Error(`secrets for plugin "${this.#plugin}": cannot read: ${String(error)}`);
88
+ }
89
+ let parsed: unknown;
90
+ try {
91
+ parsed = JSON.parse(text);
92
+ } catch {
93
+ throw new Error(`secrets for plugin "${this.#plugin}": the file is not valid JSON`);
94
+ }
95
+ if (!isRecord(parsed)) {
96
+ throw new Error(`secrets for plugin "${this.#plugin}": the file must hold an object`);
97
+ }
98
+ const values: Record<string, string> = {};
99
+ for (const [name, value] of Object.entries(parsed)) {
100
+ if (typeof value === "string") values[name] = value;
101
+ }
102
+ return values;
103
+ }
104
+
105
+ async #write(values: Record<string, string>): Promise<void> {
106
+ const directory = secretsDirectory(this.#home);
107
+ await mkdir(directory, { recursive: true, mode: 0o700 });
108
+ await chmod(directory, 0o700);
109
+ const path = secretsPathFor(this.#home, this.#plugin);
110
+ if (Object.keys(values).length === 0) {
111
+ try {
112
+ await unlink(path);
113
+ } catch (error) {
114
+ if (
115
+ !(
116
+ typeof error === "object" &&
117
+ error !== null &&
118
+ "code" in error &&
119
+ error.code === "ENOENT"
120
+ )
121
+ ) {
122
+ throw error;
123
+ }
124
+ }
125
+ return;
126
+ }
127
+ const temporary = `${path}.${randomUUID()}.tmp`;
128
+ try {
129
+ await writeFile(temporary, `${JSON.stringify(values, null, 2)}\n`, {
130
+ mode: 0o600,
131
+ });
132
+ await chmod(temporary, 0o600);
133
+ await rename(temporary, path);
134
+ } catch (error) {
135
+ await unlink(temporary).catch(() => undefined);
136
+ throw new Error(
137
+ `secrets for plugin "${this.#plugin}": cannot write: ${String(error)}`,
138
+ );
139
+ }
140
+ }
141
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The small semver range check a plugin declaration needs: `^`, `~`, `>=`
3
+ * and an exact version. No dependency, no prerelease or `||` support.
4
+ */
5
+
6
+ interface Version {
7
+ readonly major: number;
8
+ readonly minor: number;
9
+ readonly patch: number;
10
+ }
11
+
12
+ function parseVersion(value: string): Version | undefined {
13
+ const match = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?$/.exec(value.trim());
14
+ if (match === null) return undefined;
15
+ return {
16
+ major: Number(match[1]),
17
+ minor: Number(match[2] ?? 0),
18
+ patch: Number(match[3] ?? 0),
19
+ };
20
+ }
21
+
22
+ function compare(left: Version, right: Version): number {
23
+ for (const key of ["major", "minor", "patch"] as const) {
24
+ if (left[key] !== right[key]) return left[key] < right[key] ? -1 : 1;
25
+ }
26
+ return 0;
27
+ }
28
+
29
+ function atLeast(value: Version, base: Version): boolean {
30
+ return compare(value, base) >= 0;
31
+ }
32
+
33
+ function below(value: Version, base: Version): boolean {
34
+ return compare(value, base) < 0;
35
+ }
36
+
37
+ /** The upper bound of a caret range, following the usual zero-major rule. */
38
+ function caretUpper(base: Version): Version {
39
+ if (base.major > 0) return { major: base.major + 1, minor: 0, patch: 0 };
40
+ if (base.minor > 0) return { major: 0, minor: base.minor + 1, patch: 0 };
41
+ return { major: 0, minor: 0, patch: base.patch + 1 };
42
+ }
43
+
44
+ /** The upper bound of a tilde range: the next minor, or the next major for `~1`. */
45
+ function tildeUpper(text: string, base: Version): Version {
46
+ const parts = text.trim().split(".");
47
+ if (parts.length <= 1) return { major: base.major + 1, minor: 0, patch: 0 };
48
+ return { major: base.major, minor: base.minor + 1, patch: 0 };
49
+ }
50
+
51
+ function matchesOne(version: Version, range: string): boolean {
52
+ const text = range.trim();
53
+ if (text.length === 0) return false;
54
+
55
+ if (text.startsWith("^")) {
56
+ const base = parseVersion(text.slice(1));
57
+ if (base === undefined) return false;
58
+ return atLeast(version, base) && below(version, caretUpper(base));
59
+ }
60
+ if (text.startsWith("~")) {
61
+ const baseText = text.slice(1);
62
+ const base = parseVersion(baseText);
63
+ if (base === undefined) return false;
64
+ return atLeast(version, base) && below(version, tildeUpper(baseText, base));
65
+ }
66
+ if (text.startsWith(">=")) {
67
+ const base = parseVersion(text.slice(2));
68
+ return base === undefined ? false : atLeast(version, base);
69
+ }
70
+ if (text.startsWith("=")) {
71
+ const exact = parseVersion(text.slice(1));
72
+ return exact === undefined ? false : compare(version, exact) === 0;
73
+ }
74
+ const exact = parseVersion(text);
75
+ return exact === undefined ? false : compare(version, exact) === 0;
76
+ }
77
+
78
+ /** Whether `version` satisfies `range`, or throws when the range is malformed. */
79
+ export function satisfiesRange(version: string, range: string): boolean {
80
+ const parsed = parseVersion(version);
81
+ if (parsed === undefined) {
82
+ throw new Error(`version "${version}" is not a version`);
83
+ }
84
+ const parts = range.split("||").map((part) => part.trim());
85
+ if (parts.length === 0 || parts.some((part) => part.length === 0)) {
86
+ throw new Error(`range "${range}" is empty`);
87
+ }
88
+ for (const part of parts) {
89
+ if (!isRangeSyntax(part)) {
90
+ throw new Error(`range "${range}" is not supported; use ^, ~, >= or an exact version`);
91
+ }
92
+ }
93
+ return parts.some((part) => matchesOne(parsed, part));
94
+ }
95
+
96
+ /** The shape check `satisfiesRange` uses, so a bad declaration is named early. */
97
+ export function isRangeSyntax(range: string): boolean {
98
+ const parts = range.split("||").map((part) => part.trim());
99
+ if (parts.length === 0 || parts.some((part) => part.length === 0)) return false;
100
+ return parts.every((part) => {
101
+ const text = part.startsWith("^") || part.startsWith("~")
102
+ ? part.slice(1)
103
+ : part.startsWith(">=")
104
+ ? part.slice(2)
105
+ : part.startsWith("=")
106
+ ? part.slice(1)
107
+ : part;
108
+ return parseVersion(text) !== undefined;
109
+ });
110
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * A plugin's settings: the JSON Schema it declares, the section of
3
+ * `config.json` the kernel validates, and the default values it fills.
4
+ */
5
+
6
+ import type { JsonValue } from "../log/types.ts";
7
+ import { ActionArgsError, validateSchemaValue } from "./action-schema.ts";
8
+ import type { ActionArgSchema, ActionArgsSchema } from "./types.ts";
9
+ import { isRecord } from "../util.ts";
10
+
11
+ function cloneJson(value: unknown): unknown {
12
+ if (Array.isArray(value)) return value.map((item) => cloneJson(item));
13
+ if (isRecord(value)) {
14
+ const copy: Record<string, unknown> = {};
15
+ for (const [key, item] of Object.entries(value)) copy[key] = cloneJson(item);
16
+ return copy;
17
+ }
18
+ return value;
19
+ }
20
+
21
+ /** Fills every declared default into an absent field, recursively. */
22
+ function applyDefaults(schema: ActionArgSchema, value: unknown): unknown {
23
+ if (Array.isArray(value) && schema.items !== undefined) {
24
+ return value.map((item) => applyDefaults(schema.items as ActionArgSchema, item));
25
+ }
26
+ if (!isRecord(value) || schema.properties === undefined) return value;
27
+
28
+ const result: Record<string, unknown> = { ...value };
29
+ for (const [name, property] of Object.entries(schema.properties)) {
30
+ if (result[name] === undefined) {
31
+ if (property.default !== undefined) result[name] = cloneJson(property.default);
32
+ continue;
33
+ }
34
+ result[name] = applyDefaults(property, result[name]);
35
+ }
36
+ return result;
37
+ }
38
+
39
+ function deepFreeze<T>(value: T): T {
40
+ if (typeof value !== "object" || value === null) return value;
41
+ Object.freeze(value);
42
+ for (const item of Object.values(value as Record<string, unknown>)) {
43
+ deepFreeze(item);
44
+ }
45
+ return value;
46
+ }
47
+
48
+ /**
49
+ * Validates one plugin's settings section and fills the defaults its schema
50
+ * declares. The message names the plugin and the field, never a secret.
51
+ */
52
+ export function prepareSettings(
53
+ schema: ActionArgsSchema,
54
+ section: unknown,
55
+ plugin: string,
56
+ ): Readonly<Record<string, JsonValue>> {
57
+ if (section === undefined) section = {};
58
+ if (!isRecord(section)) {
59
+ throw new ActionArgsError(`plugin "${plugin}": settings: must be an object`);
60
+ }
61
+ const filled = applyDefaults(schema, section) as Record<string, unknown>;
62
+ validateSchemaValue(schema, filled, `plugin "${plugin}"`, "settings");
63
+ return deepFreeze(filled) as Readonly<Record<string, JsonValue>>;
64
+ }