@operad-io/engine 0.0.0-stage → 0.0.1

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/config.schema.json +188 -0
  3. package/keys.schema.json +12 -0
  4. package/package.json +22 -4
  5. package/src/config/config.ts +627 -0
  6. package/src/config/index.ts +1 -0
  7. package/src/flow/define-flow.ts +57 -0
  8. package/src/flow/index.ts +5 -0
  9. package/src/flow/load.ts +41 -0
  10. package/src/flow/types.ts +107 -0
  11. package/src/flow/validate.ts +296 -0
  12. package/src/http/address.ts +142 -0
  13. package/src/http/index.ts +25 -0
  14. package/src/http/link.ts +20 -0
  15. package/src/http/server.ts +920 -0
  16. package/src/http/token.ts +85 -0
  17. package/src/index.ts +16 -0
  18. package/src/kernel/decide.ts +689 -0
  19. package/src/kernel/index.ts +3 -0
  20. package/src/kernel/state.ts +275 -0
  21. package/src/kernel/types.ts +366 -0
  22. package/src/log/errors.ts +77 -0
  23. package/src/log/facts.ts +50 -0
  24. package/src/log/index.ts +6 -0
  25. package/src/log/json-value.ts +67 -0
  26. package/src/log/open-log.ts +17 -0
  27. package/src/log/schema.ts +5 -0
  28. package/src/log/secret.ts +30 -0
  29. package/src/log/sqlite.ts +1011 -0
  30. package/src/log/types.ts +230 -0
  31. package/src/loop/code-watch.ts +486 -0
  32. package/src/loop/index.ts +3 -0
  33. package/src/loop/older-code.ts +58 -0
  34. package/src/loop/serve.ts +483 -0
  35. package/src/markdown.ts +451 -0
  36. package/src/notify/index.ts +10 -0
  37. package/src/notify/notify.ts +256 -0
  38. package/src/platform/file-lock.ts +385 -0
  39. package/src/platform/index.ts +13 -0
  40. package/src/platform/load.ts +18 -0
  41. package/src/platform/process-group.ts +635 -0
  42. package/src/platform/process-watch.ts +152 -0
  43. package/src/platform/sqlite-open.ts +104 -0
  44. package/src/platform/step-reap.ts +668 -0
  45. package/src/platform/watch.ts +158 -0
  46. package/src/platform/worker-reports.ts +34 -0
  47. package/src/plugin/action-schema.ts +550 -0
  48. package/src/plugin/declared-flows.ts +74 -0
  49. package/src/plugin/define-plugin.ts +12 -0
  50. package/src/plugin/errors.ts +73 -0
  51. package/src/plugin/filters.ts +46 -0
  52. package/src/plugin/harness.ts +50 -0
  53. package/src/plugin/home.ts +13 -0
  54. package/src/plugin/index.ts +65 -0
  55. package/src/plugin/install.ts +268 -0
  56. package/src/plugin/keys.ts +63 -0
  57. package/src/plugin/load.ts +256 -0
  58. package/src/plugin/logger.ts +30 -0
  59. package/src/plugin/object-facts.ts +132 -0
  60. package/src/plugin/open-store.ts +32 -0
  61. package/src/plugin/paging.ts +133 -0
  62. package/src/plugin/registry.ts +1783 -0
  63. package/src/plugin/secrets.ts +141 -0
  64. package/src/plugin/semver.ts +110 -0
  65. package/src/plugin/settings.ts +64 -0
  66. package/src/plugin/sqlite-store.ts +756 -0
  67. package/src/plugin/store.ts +318 -0
  68. package/src/plugin/types.ts +1236 -0
  69. package/src/plugin/usage.ts +157 -0
  70. package/src/plugin/validate.ts +1028 -0
  71. package/src/runner/answer.ts +156 -0
  72. package/src/runner/ask.ts +483 -0
  73. package/src/runner/cancel.ts +51 -0
  74. package/src/runner/errors.ts +20 -0
  75. package/src/runner/grants.ts +92 -0
  76. package/src/runner/index.ts +33 -0
  77. package/src/runner/limits.ts +403 -0
  78. package/src/runner/lock.ts +140 -0
  79. package/src/runner/runner.ts +1175 -0
  80. package/src/runner/service.ts +184 -0
  81. package/src/runner/step-worker.ts +303 -0
  82. package/src/testing/clock.ts +67 -0
  83. package/src/testing/index.ts +90 -0
  84. package/src/testing/types.ts +116 -0
  85. package/src/testing/world.ts +369 -0
  86. package/src/trigger/cron.ts +193 -0
  87. package/src/trigger/index.ts +32 -0
  88. package/src/trigger/kernel.ts +1212 -0
  89. package/src/trigger/rules.ts +300 -0
  90. package/src/trigger/store.ts +233 -0
  91. package/src/trigger/types.ts +156 -0
  92. package/src/util.ts +50 -0
  93. package/src/views/history.ts +634 -0
  94. package/src/views/index.ts +7 -0
  95. package/src/views/pending.ts +209 -0
  96. package/src/views/presentation.ts +2551 -0
  97. package/src/views/running.ts +255 -0
  98. package/src/views/settings.ts +452 -0
  99. package/src/views/types.ts +72 -0
  100. package/src/views/uploads.ts +228 -0
  101. package/triggers.schema.json +48 -0
  102. package/README.md +0 -3
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The one bearer token `serve` checks. It lives beside the plugins' secrets in
3
+ * `$OPERAD_HOME/secrets/http.json` (0600 in a 0700 directory), never in the log.
4
+ */
5
+
6
+ import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
7
+ import { FileSecretStore } from "../plugin/secrets.ts";
8
+
9
+ const PLUGIN = "http";
10
+ const TOKEN = "token";
11
+ const LABEL = "label";
12
+
13
+ /** The actor id used when a token has no label of its own. */
14
+ export const DEFAULT_HTTP_TOKEN_LABEL = "operad app";
15
+
16
+ /** The token and the name an action records it by. */
17
+ export interface HttpToken {
18
+ readonly token: string;
19
+ readonly label: string;
20
+ }
21
+
22
+ function tokenStore(home: string): FileSecretStore {
23
+ return new FileSecretStore({ home, plugin: PLUGIN });
24
+ }
25
+
26
+ /** 32 random bytes, base64url, as the protocol asks. */
27
+ export function generateHttpToken(): string {
28
+ return randomBytes(32).toString("base64url");
29
+ }
30
+
31
+ /** Reads the token, or undefined before the first enabled `serve`. */
32
+ export async function readHttpToken(
33
+ home: string,
34
+ ): Promise<HttpToken | undefined> {
35
+ const store = tokenStore(home);
36
+ const token = await store.get(TOKEN);
37
+ if (token === undefined) return undefined;
38
+ const label = await store.get(LABEL);
39
+ return {
40
+ token: token.reveal(),
41
+ label: label?.reveal() ?? DEFAULT_HTTP_TOKEN_LABEL,
42
+ };
43
+ }
44
+
45
+ /** Creates the token on the first enabled `serve`; a later call keeps it. */
46
+ export async function ensureHttpToken(home: string): Promise<HttpToken> {
47
+ const store = tokenStore(home);
48
+ const existing = await store.get(TOKEN);
49
+ let token: string;
50
+ if (existing === undefined) {
51
+ token = generateHttpToken();
52
+ await store.set(TOKEN, token);
53
+ } else {
54
+ token = existing.reveal();
55
+ }
56
+ let label = await store.get(LABEL);
57
+ if (label === undefined) {
58
+ await store.set(LABEL, DEFAULT_HTTP_TOKEN_LABEL);
59
+ label = await store.get(LABEL);
60
+ }
61
+ return { token, label: label?.reveal() ?? DEFAULT_HTTP_TOKEN_LABEL };
62
+ }
63
+
64
+ /**
65
+ * Replaces the token. A running `serve` reads the file for each request, so the
66
+ * old token is refused from the moment this returns.
67
+ */
68
+ export async function rotateHttpToken(home: string): Promise<HttpToken> {
69
+ const store = tokenStore(home);
70
+ const token = generateHttpToken();
71
+ await store.set(TOKEN, token);
72
+ let label = await store.get(LABEL);
73
+ if (label === undefined) {
74
+ await store.set(LABEL, DEFAULT_HTTP_TOKEN_LABEL);
75
+ label = await store.get(LABEL);
76
+ }
77
+ return { token, label: label?.reveal() ?? DEFAULT_HTTP_TOKEN_LABEL };
78
+ }
79
+
80
+ /** Compares two tokens without an early exit. */
81
+ export function tokensEqual(expected: string, provided: string): boolean {
82
+ const left = createHash("sha256").update(expected, "utf8").digest();
83
+ const right = createHash("sha256").update(provided, "utf8").digest();
84
+ return timingSafeEqual(left, right);
85
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ /** The operad graph engine. */
2
+ export const ENGINE_VERSION = "0.0.1";
3
+
4
+ export * from "./flow/index.ts";
5
+ export * from "./kernel/index.ts";
6
+ export * from "./log/index.ts";
7
+ export * from "./plugin/index.ts";
8
+ export * from "./config/index.ts";
9
+ export * from "./http/index.ts";
10
+ export * from "./runner/index.ts";
11
+ export * from "./views/index.ts";
12
+ export * from "./loop/index.ts";
13
+ export * from "./trigger/index.ts";
14
+ export * from "./notify/index.ts";
15
+ export * from "./markdown.ts";
16
+ export * from "./util.ts";