@abyss-project/runtime 2.0.1 → 3.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 (45) hide show
  1. package/README.md +27 -0
  2. package/dist/cost/execution-cost.d.ts +0 -2
  3. package/dist/cost/execution-cost.js +2 -6
  4. package/dist/cost/execution-cost.js.map +1 -1
  5. package/dist/discord/discord.types.d.ts +123 -0
  6. package/dist/discord/discord.types.js +53 -0
  7. package/dist/discord/discord.types.js.map +1 -0
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.js +1 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/runner-protocol/execution-protocol.types.d.ts +0 -1
  12. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  13. package/package.json +1 -1
  14. package/scripts/build-engine.cjs +70 -70
  15. package/src/__tests__/engine-bundle.test.ts +121 -121
  16. package/src/cost/__tests__/execution-cost.test.ts +196 -200
  17. package/src/cost/execution-cost.ts +186 -192
  18. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  19. package/src/definition/definition.limits.ts +9 -9
  20. package/src/definition/definition.schema.ts +172 -172
  21. package/src/definition/definition.types.ts +171 -171
  22. package/src/discord/discord.types.ts +155 -0
  23. package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
  24. package/src/engine/engine-entry.ts +3 -3
  25. package/src/engine/engine-global.ts +2 -2
  26. package/src/engine/run-in-isolate.ts +404 -404
  27. package/src/errors/runtime-error-code.enum.ts +37 -37
  28. package/src/index.ts +31 -30
  29. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  30. package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -223
  31. package/src/interpreter/interpreter.ts +461 -461
  32. package/src/interpreter/interpreter.types.ts +192 -192
  33. package/src/interpreter/test-host.ts +85 -85
  34. package/src/runner-protocol/evaluation.types.ts +21 -21
  35. package/src/runner-protocol/execution-protocol.types.ts +231 -221
  36. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  37. package/src/server/code-runner-error.ts +12 -12
  38. package/src/server/code-runner-session.ts +310 -310
  39. package/src/server/index.ts +11 -11
  40. package/src/steps/__tests__/global-references.test.ts +133 -113
  41. package/src/steps/execute-step.ts +186 -186
  42. package/src/steps/global-references.ts +48 -48
  43. package/src/steps/step-access.ts +56 -56
  44. package/src/steps/step-fields.ts +87 -87
  45. package/src/steps/value-source.ts +69 -69
@@ -1,9 +1,9 @@
1
- // Bounds of the definition format, without Joi: the interpreter and the isolate engine bundle
2
- // read them, and neither may pull Joi in (see `engine/run-in-isolate.ts`).
3
-
4
- export const MAX_HTTP_TIMEOUT_MS = 30_000;
5
- export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
6
-
7
- /** HTTP status range a RESPOND step may send. */
8
- export const MIN_RESPONSE_STATUS = 200;
9
- export const MAX_RESPONSE_STATUS = 599;
1
+ // Bounds of the definition format, without Joi: the interpreter and the isolate engine bundle
2
+ // read them, and neither may pull Joi in (see `engine/run-in-isolate.ts`).
3
+
4
+ export const MAX_HTTP_TIMEOUT_MS = 30_000;
5
+ export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
6
+
7
+ /** HTTP status range a RESPOND step may send. */
8
+ export const MIN_RESPONSE_STATUS = 200;
9
+ export const MAX_RESPONSE_STATUS = 599;
@@ -1,172 +1,172 @@
1
- import Joi from 'joi';
2
- import { AccessKind } from '../access/access.types';
3
- import {
4
- MAX_HTTP_TIMEOUT_MS,
5
- MAX_RESPONSE_STATUS,
6
- MAX_WAIT_DURATION_MS,
7
- MIN_RESPONSE_STATUS,
8
- } from './definition.limits';
9
- import {
10
- CURRENT_FORMAT_VERSION,
11
- HttpMethod,
12
- LogLevel,
13
- StepType,
14
- StopStatus,
15
- } from './definition.types';
16
-
17
- export { MAX_HTTP_TIMEOUT_MS, MAX_WAIT_DURATION_MS };
18
-
19
- /** Names that would hit `Object.prototype` when used as a key of `outputs` or `vars`. */
20
- const RESERVED_NAMES = ['__proto__', 'constructor', 'prototype'];
21
-
22
- const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
23
- const HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
24
-
25
- let valueSourceCount = 0;
26
-
27
- /**
28
- * Literal JSON, `{ $expression }` (that key alone), arrays and objects of value sources. Joi
29
- * links resolve by id and ids must be unique inside one schema tree, hence a fresh id per use.
30
- */
31
- const valueSource = (): Joi.AlternativesSchema => {
32
- const id = `valueSource${valueSourceCount++}`;
33
- return Joi.alternatives()
34
- .try(
35
- Joi.string().allow(''),
36
- Joi.number(),
37
- Joi.boolean(),
38
- Joi.valid(null),
39
- Joi.array().items(Joi.link(`#${id}`)),
40
- Joi.object({ $expression: Joi.string().required() }),
41
- Joi.object().pattern(Joi.string().invalid('$expression'), Joi.link(`#${id}`)),
42
- )
43
- .id(id);
44
- };
45
-
46
- const valueMap = () => Joi.object().pattern(Joi.string(), valueSource());
47
-
48
- /** A number is bounded here; anything else (template, `$expression`) is checked at run time. */
49
- const durationSource = (): Joi.AlternativesSchema =>
50
- Joi.alternatives().try(
51
- Joi.number().integer().min(0).max(MAX_WAIT_DURATION_MS),
52
- Joi.string().allow(''),
53
- Joi.object({ $expression: Joi.string().required() }),
54
- );
55
-
56
- /** A literal must be a date; a template or `$expression` is checked at run time. */
57
- const untilSource = (): Joi.AlternativesSchema =>
58
- Joi.alternatives().try(
59
- Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
60
- Joi.string().isoDate(),
61
- Joi.object({ $expression: Joi.string().required() }),
62
- );
63
-
64
- /** A literal must be an HTTP status; a template or `$expression` is checked at run time. */
65
- const statusSource = (): Joi.AlternativesSchema =>
66
- Joi.alternatives().try(
67
- Joi.number().integer().min(MIN_RESPONSE_STATUS).max(MAX_RESPONSE_STATUS),
68
- Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
69
- Joi.object({ $expression: Joi.string().required() }),
70
- );
71
-
72
- const stepBase = {
73
- id: Joi.string()
74
- .min(1)
75
- .invalid(...RESERVED_NAMES)
76
- .required(),
77
- name: Joi.string().allow(''),
78
- };
79
-
80
- // Children (`then`, `else`, `steps`, `default`) are only checked to be arrays here: the
81
- // validator walks them to give each nested step its own path.
82
- export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
83
- [StepType.CONDITION]: Joi.object({
84
- ...stepBase,
85
- type: Joi.valid(StepType.CONDITION).required(),
86
- if: valueSource().required(),
87
- then: Joi.array().required(),
88
- else: Joi.array(),
89
- }),
90
- [StepType.SWITCH]: Joi.object({
91
- ...stepBase,
92
- type: Joi.valid(StepType.SWITCH).required(),
93
- value: valueSource().required(),
94
- cases: Joi.array()
95
- .min(1)
96
- .items(Joi.object({ match: valueSource().required(), steps: Joi.array().required() }))
97
- .required(),
98
- default: Joi.array(),
99
- }),
100
- [StepType.WAIT]: Joi.object({
101
- ...stepBase,
102
- type: Joi.valid(StepType.WAIT).required(),
103
- durationMs: durationSource(),
104
- until: untilSource(),
105
- }).xor('durationMs', 'until'),
106
- [StepType.SET]: Joi.object({
107
- ...stepBase,
108
- type: Joi.valid(StepType.SET).required(),
109
- variable: Joi.string()
110
- .pattern(IDENTIFIER)
111
- .invalid(...RESERVED_NAMES)
112
- .required(),
113
- value: valueSource().required(),
114
- }),
115
- [StepType.CODE]: Joi.object({
116
- ...stepBase,
117
- type: Joi.valid(StepType.CODE).required(),
118
- source: Joi.string().min(1).required(),
119
- }),
120
- [StepType.HTTP]: Joi.object({
121
- ...stepBase,
122
- type: Joi.valid(StepType.HTTP).required(),
123
- method: Joi.valid(...Object.values(HttpMethod)).required(),
124
- url: valueSource().required(),
125
- headers: valueMap(),
126
- body: valueSource(),
127
- timeoutMs: Joi.number().integer().min(1).max(MAX_HTTP_TIMEOUT_MS),
128
- }),
129
- [StepType.HOST]: Joi.object({
130
- ...stepBase,
131
- type: Joi.valid(StepType.HOST).required(),
132
- action: Joi.string().min(1).required(),
133
- input: valueMap().required(),
134
- }),
135
- [StepType.LOG]: Joi.object({
136
- ...stepBase,
137
- type: Joi.valid(StepType.LOG).required(),
138
- level: Joi.valid(...Object.values(LogLevel)).required(),
139
- message: valueSource().required(),
140
- data: valueSource(),
141
- }),
142
- [StepType.RESPOND]: Joi.object({
143
- ...stepBase,
144
- type: Joi.valid(StepType.RESPOND).required(),
145
- status: statusSource().required(),
146
- headers: valueMap(),
147
- body: valueSource(),
148
- }),
149
- [StepType.STOP]: Joi.object({
150
- ...stepBase,
151
- type: Joi.valid(StepType.STOP).required(),
152
- status: Joi.valid(...Object.values(StopStatus)).required(),
153
- output: valueSource(),
154
- }),
155
- };
156
-
157
- export const DEFINITION_SCHEMA = Joi.object({
158
- formatVersion: Joi.valid(CURRENT_FORMAT_VERSION).required(),
159
- accesses: Joi.array().required(),
160
- steps: Joi.array().required(),
161
- });
162
-
163
- export const ACCESS_SCHEMAS: Record<AccessKind, Joi.ObjectSchema> = {
164
- [AccessKind.HTTP]: Joi.object({
165
- kind: Joi.valid(AccessKind.HTTP).required(),
166
- hosts: Joi.array().items(Joi.string().pattern(HOST_PATTERN)).min(1).required(),
167
- }),
168
- [AccessKind.HOST_ACTION]: Joi.object({
169
- kind: Joi.valid(AccessKind.HOST_ACTION).required(),
170
- action: Joi.string().min(1).required(),
171
- }),
172
- };
1
+ import Joi from 'joi';
2
+ import { AccessKind } from '../access/access.types';
3
+ import {
4
+ MAX_HTTP_TIMEOUT_MS,
5
+ MAX_RESPONSE_STATUS,
6
+ MAX_WAIT_DURATION_MS,
7
+ MIN_RESPONSE_STATUS,
8
+ } from './definition.limits';
9
+ import {
10
+ CURRENT_FORMAT_VERSION,
11
+ HttpMethod,
12
+ LogLevel,
13
+ StepType,
14
+ StopStatus,
15
+ } from './definition.types';
16
+
17
+ export { MAX_HTTP_TIMEOUT_MS, MAX_WAIT_DURATION_MS };
18
+
19
+ /** Names that would hit `Object.prototype` when used as a key of `outputs` or `vars`. */
20
+ const RESERVED_NAMES = ['__proto__', 'constructor', 'prototype'];
21
+
22
+ const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
23
+ const HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
24
+
25
+ let valueSourceCount = 0;
26
+
27
+ /**
28
+ * Literal JSON, `{ $expression }` (that key alone), arrays and objects of value sources. Joi
29
+ * links resolve by id and ids must be unique inside one schema tree, hence a fresh id per use.
30
+ */
31
+ const valueSource = (): Joi.AlternativesSchema => {
32
+ const id = `valueSource${valueSourceCount++}`;
33
+ return Joi.alternatives()
34
+ .try(
35
+ Joi.string().allow(''),
36
+ Joi.number(),
37
+ Joi.boolean(),
38
+ Joi.valid(null),
39
+ Joi.array().items(Joi.link(`#${id}`)),
40
+ Joi.object({ $expression: Joi.string().required() }),
41
+ Joi.object().pattern(Joi.string().invalid('$expression'), Joi.link(`#${id}`)),
42
+ )
43
+ .id(id);
44
+ };
45
+
46
+ const valueMap = () => Joi.object().pattern(Joi.string(), valueSource());
47
+
48
+ /** A number is bounded here; anything else (template, `$expression`) is checked at run time. */
49
+ const durationSource = (): Joi.AlternativesSchema =>
50
+ Joi.alternatives().try(
51
+ Joi.number().integer().min(0).max(MAX_WAIT_DURATION_MS),
52
+ Joi.string().allow(''),
53
+ Joi.object({ $expression: Joi.string().required() }),
54
+ );
55
+
56
+ /** A literal must be a date; a template or `$expression` is checked at run time. */
57
+ const untilSource = (): Joi.AlternativesSchema =>
58
+ Joi.alternatives().try(
59
+ Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
60
+ Joi.string().isoDate(),
61
+ Joi.object({ $expression: Joi.string().required() }),
62
+ );
63
+
64
+ /** A literal must be an HTTP status; a template or `$expression` is checked at run time. */
65
+ const statusSource = (): Joi.AlternativesSchema =>
66
+ Joi.alternatives().try(
67
+ Joi.number().integer().min(MIN_RESPONSE_STATUS).max(MAX_RESPONSE_STATUS),
68
+ Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
69
+ Joi.object({ $expression: Joi.string().required() }),
70
+ );
71
+
72
+ const stepBase = {
73
+ id: Joi.string()
74
+ .min(1)
75
+ .invalid(...RESERVED_NAMES)
76
+ .required(),
77
+ name: Joi.string().allow(''),
78
+ };
79
+
80
+ // Children (`then`, `else`, `steps`, `default`) are only checked to be arrays here: the
81
+ // validator walks them to give each nested step its own path.
82
+ export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
83
+ [StepType.CONDITION]: Joi.object({
84
+ ...stepBase,
85
+ type: Joi.valid(StepType.CONDITION).required(),
86
+ if: valueSource().required(),
87
+ then: Joi.array().required(),
88
+ else: Joi.array(),
89
+ }),
90
+ [StepType.SWITCH]: Joi.object({
91
+ ...stepBase,
92
+ type: Joi.valid(StepType.SWITCH).required(),
93
+ value: valueSource().required(),
94
+ cases: Joi.array()
95
+ .min(1)
96
+ .items(Joi.object({ match: valueSource().required(), steps: Joi.array().required() }))
97
+ .required(),
98
+ default: Joi.array(),
99
+ }),
100
+ [StepType.WAIT]: Joi.object({
101
+ ...stepBase,
102
+ type: Joi.valid(StepType.WAIT).required(),
103
+ durationMs: durationSource(),
104
+ until: untilSource(),
105
+ }).xor('durationMs', 'until'),
106
+ [StepType.SET]: Joi.object({
107
+ ...stepBase,
108
+ type: Joi.valid(StepType.SET).required(),
109
+ variable: Joi.string()
110
+ .pattern(IDENTIFIER)
111
+ .invalid(...RESERVED_NAMES)
112
+ .required(),
113
+ value: valueSource().required(),
114
+ }),
115
+ [StepType.CODE]: Joi.object({
116
+ ...stepBase,
117
+ type: Joi.valid(StepType.CODE).required(),
118
+ source: Joi.string().min(1).required(),
119
+ }),
120
+ [StepType.HTTP]: Joi.object({
121
+ ...stepBase,
122
+ type: Joi.valid(StepType.HTTP).required(),
123
+ method: Joi.valid(...Object.values(HttpMethod)).required(),
124
+ url: valueSource().required(),
125
+ headers: valueMap(),
126
+ body: valueSource(),
127
+ timeoutMs: Joi.number().integer().min(1).max(MAX_HTTP_TIMEOUT_MS),
128
+ }),
129
+ [StepType.HOST]: Joi.object({
130
+ ...stepBase,
131
+ type: Joi.valid(StepType.HOST).required(),
132
+ action: Joi.string().min(1).required(),
133
+ input: valueMap().required(),
134
+ }),
135
+ [StepType.LOG]: Joi.object({
136
+ ...stepBase,
137
+ type: Joi.valid(StepType.LOG).required(),
138
+ level: Joi.valid(...Object.values(LogLevel)).required(),
139
+ message: valueSource().required(),
140
+ data: valueSource(),
141
+ }),
142
+ [StepType.RESPOND]: Joi.object({
143
+ ...stepBase,
144
+ type: Joi.valid(StepType.RESPOND).required(),
145
+ status: statusSource().required(),
146
+ headers: valueMap(),
147
+ body: valueSource(),
148
+ }),
149
+ [StepType.STOP]: Joi.object({
150
+ ...stepBase,
151
+ type: Joi.valid(StepType.STOP).required(),
152
+ status: Joi.valid(...Object.values(StopStatus)).required(),
153
+ output: valueSource(),
154
+ }),
155
+ };
156
+
157
+ export const DEFINITION_SCHEMA = Joi.object({
158
+ formatVersion: Joi.valid(CURRENT_FORMAT_VERSION).required(),
159
+ accesses: Joi.array().required(),
160
+ steps: Joi.array().required(),
161
+ });
162
+
163
+ export const ACCESS_SCHEMAS: Record<AccessKind, Joi.ObjectSchema> = {
164
+ [AccessKind.HTTP]: Joi.object({
165
+ kind: Joi.valid(AccessKind.HTTP).required(),
166
+ hosts: Joi.array().items(Joi.string().pattern(HOST_PATTERN)).min(1).required(),
167
+ }),
168
+ [AccessKind.HOST_ACTION]: Joi.object({
169
+ kind: Joi.valid(AccessKind.HOST_ACTION).required(),
170
+ action: Joi.string().min(1).required(),
171
+ }),
172
+ };