@mondaydotcomorg/atp-server 0.17.14

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 (307) hide show
  1. package/README.md +489 -0
  2. package/dist/aggregator/index.d.ts +59 -0
  3. package/dist/aggregator/index.d.ts.map +1 -0
  4. package/dist/aggregator/index.js +171 -0
  5. package/dist/aggregator/index.js.map +1 -0
  6. package/dist/callback/index.d.ts +98 -0
  7. package/dist/callback/index.d.ts.map +1 -0
  8. package/dist/callback/index.js +136 -0
  9. package/dist/callback/index.js.map +1 -0
  10. package/dist/client-sessions.d.ts +82 -0
  11. package/dist/client-sessions.d.ts.map +1 -0
  12. package/dist/client-sessions.js +174 -0
  13. package/dist/client-sessions.js.map +1 -0
  14. package/dist/controllers/definitions.controller.d.ts +4 -0
  15. package/dist/controllers/definitions.controller.d.ts.map +1 -0
  16. package/dist/controllers/definitions.controller.js +11 -0
  17. package/dist/controllers/definitions.controller.js.map +1 -0
  18. package/dist/controllers/execute.controller.d.ts +18 -0
  19. package/dist/controllers/execute.controller.d.ts.map +1 -0
  20. package/dist/controllers/execute.controller.js +122 -0
  21. package/dist/controllers/execute.controller.js.map +1 -0
  22. package/dist/controllers/info.controller.d.ts +3 -0
  23. package/dist/controllers/info.controller.d.ts.map +1 -0
  24. package/dist/controllers/info.controller.js +13 -0
  25. package/dist/controllers/info.controller.js.map +1 -0
  26. package/dist/controllers/resume.controller.d.ts +11 -0
  27. package/dist/controllers/resume.controller.d.ts.map +1 -0
  28. package/dist/controllers/resume.controller.js +61 -0
  29. package/dist/controllers/resume.controller.js.map +1 -0
  30. package/dist/controllers/search.controller.d.ts +4 -0
  31. package/dist/controllers/search.controller.d.ts.map +1 -0
  32. package/dist/controllers/search.controller.js +7 -0
  33. package/dist/controllers/search.controller.js.map +1 -0
  34. package/dist/controllers/stream.controller.d.ts +19 -0
  35. package/dist/controllers/stream.controller.d.ts.map +1 -0
  36. package/dist/controllers/stream.controller.js +141 -0
  37. package/dist/controllers/stream.controller.js.map +1 -0
  38. package/dist/core/config.d.ts +161 -0
  39. package/dist/core/config.d.ts.map +1 -0
  40. package/dist/core/config.js +7 -0
  41. package/dist/core/config.js.map +1 -0
  42. package/dist/core/http.d.ts +4 -0
  43. package/dist/core/http.d.ts.map +1 -0
  44. package/dist/core/http.js +17 -0
  45. package/dist/core/http.js.map +1 -0
  46. package/dist/create-server.d.ts +120 -0
  47. package/dist/create-server.d.ts.map +1 -0
  48. package/dist/create-server.js +423 -0
  49. package/dist/create-server.js.map +1 -0
  50. package/dist/execution-state/index.d.ts +95 -0
  51. package/dist/execution-state/index.d.ts.map +1 -0
  52. package/dist/execution-state/index.js +128 -0
  53. package/dist/execution-state/index.js.map +1 -0
  54. package/dist/executor/ast-provenance-bridge.d.ts +12 -0
  55. package/dist/executor/ast-provenance-bridge.d.ts.map +1 -0
  56. package/dist/executor/ast-provenance-bridge.js +66 -0
  57. package/dist/executor/ast-provenance-bridge.js.map +1 -0
  58. package/dist/executor/ast-tracking-runtime.d.ts +7 -0
  59. package/dist/executor/ast-tracking-runtime.d.ts.map +1 -0
  60. package/dist/executor/ast-tracking-runtime.js +559 -0
  61. package/dist/executor/ast-tracking-runtime.js.map +1 -0
  62. package/dist/executor/bootstrap-generated.d.ts +32 -0
  63. package/dist/executor/bootstrap-generated.d.ts.map +1 -0
  64. package/dist/executor/bootstrap-generated.js +90 -0
  65. package/dist/executor/bootstrap-generated.js.map +1 -0
  66. package/dist/executor/compiler-config.d.ts +32 -0
  67. package/dist/executor/compiler-config.d.ts.map +1 -0
  68. package/dist/executor/compiler-config.js +99 -0
  69. package/dist/executor/compiler-config.js.map +1 -0
  70. package/dist/executor/constants.d.ts +4 -0
  71. package/dist/executor/constants.d.ts.map +1 -0
  72. package/dist/executor/constants.js +4 -0
  73. package/dist/executor/constants.js.map +1 -0
  74. package/dist/executor/error-handler.d.ts +9 -0
  75. package/dist/executor/error-handler.d.ts.map +1 -0
  76. package/dist/executor/error-handler.js +95 -0
  77. package/dist/executor/error-handler.js.map +1 -0
  78. package/dist/executor/execution-error-handler.d.ts +7 -0
  79. package/dist/executor/execution-error-handler.d.ts.map +1 -0
  80. package/dist/executor/execution-error-handler.js +136 -0
  81. package/dist/executor/execution-error-handler.js.map +1 -0
  82. package/dist/executor/executor.d.ts +20 -0
  83. package/dist/executor/executor.d.ts.map +1 -0
  84. package/dist/executor/executor.js +452 -0
  85. package/dist/executor/executor.js.map +1 -0
  86. package/dist/executor/index.d.ts +4 -0
  87. package/dist/executor/index.d.ts.map +1 -0
  88. package/dist/executor/index.js +3 -0
  89. package/dist/executor/index.js.map +1 -0
  90. package/dist/executor/resume-handler.d.ts +9 -0
  91. package/dist/executor/resume-handler.d.ts.map +1 -0
  92. package/dist/executor/resume-handler.js +22 -0
  93. package/dist/executor/resume-handler.js.map +1 -0
  94. package/dist/executor/sandbox-builder.d.ts +29 -0
  95. package/dist/executor/sandbox-builder.d.ts.map +1 -0
  96. package/dist/executor/sandbox-builder.js +538 -0
  97. package/dist/executor/sandbox-builder.js.map +1 -0
  98. package/dist/executor/sandbox-injector.d.ts +7 -0
  99. package/dist/executor/sandbox-injector.d.ts.map +1 -0
  100. package/dist/executor/sandbox-injector.js +293 -0
  101. package/dist/executor/sandbox-injector.js.map +1 -0
  102. package/dist/executor/types.d.ts +21 -0
  103. package/dist/executor/types.d.ts.map +1 -0
  104. package/dist/executor/types.js +2 -0
  105. package/dist/executor/types.js.map +1 -0
  106. package/dist/explorer/index.d.ts +69 -0
  107. package/dist/explorer/index.d.ts.map +1 -0
  108. package/dist/explorer/index.js +228 -0
  109. package/dist/explorer/index.js.map +1 -0
  110. package/dist/handlers/definitions.handler.d.ts +3 -0
  111. package/dist/handlers/definitions.handler.d.ts.map +1 -0
  112. package/dist/handlers/definitions.handler.js +11 -0
  113. package/dist/handlers/definitions.handler.js.map +1 -0
  114. package/dist/handlers/execute.handler.d.ts +7 -0
  115. package/dist/handlers/execute.handler.d.ts.map +1 -0
  116. package/dist/handlers/execute.handler.js +225 -0
  117. package/dist/handlers/execute.handler.js.map +1 -0
  118. package/dist/handlers/explorer.handler.d.ts +4 -0
  119. package/dist/handlers/explorer.handler.d.ts.map +1 -0
  120. package/dist/handlers/explorer.handler.js +10 -0
  121. package/dist/handlers/explorer.handler.js.map +1 -0
  122. package/dist/handlers/init.handler.d.ts +5 -0
  123. package/dist/handlers/init.handler.d.ts.map +1 -0
  124. package/dist/handlers/init.handler.js +41 -0
  125. package/dist/handlers/init.handler.js.map +1 -0
  126. package/dist/handlers/resume.handler.d.ts +6 -0
  127. package/dist/handlers/resume.handler.d.ts.map +1 -0
  128. package/dist/handlers/resume.handler.js +256 -0
  129. package/dist/handlers/resume.handler.js.map +1 -0
  130. package/dist/handlers/search.handler.d.ts +5 -0
  131. package/dist/handlers/search.handler.d.ts.map +1 -0
  132. package/dist/handlers/search.handler.js +11 -0
  133. package/dist/handlers/search.handler.js.map +1 -0
  134. package/dist/http/request-handler.d.ts +15 -0
  135. package/dist/http/request-handler.d.ts.map +1 -0
  136. package/dist/http/request-handler.js +94 -0
  137. package/dist/http/request-handler.js.map +1 -0
  138. package/dist/http/router.d.ts +4 -0
  139. package/dist/http/router.d.ts.map +1 -0
  140. package/dist/http/router.js +32 -0
  141. package/dist/http/router.js.map +1 -0
  142. package/dist/index.d.ts +10 -0
  143. package/dist/index.d.ts.map +1 -0
  144. package/dist/index.js +8 -0
  145. package/dist/index.js.map +1 -0
  146. package/dist/instrumentation/index.d.ts +5 -0
  147. package/dist/instrumentation/index.d.ts.map +1 -0
  148. package/dist/instrumentation/index.js +5 -0
  149. package/dist/instrumentation/index.js.map +1 -0
  150. package/dist/instrumentation/serializer.d.ts +61 -0
  151. package/dist/instrumentation/serializer.d.ts.map +1 -0
  152. package/dist/instrumentation/serializer.js +334 -0
  153. package/dist/instrumentation/serializer.js.map +1 -0
  154. package/dist/instrumentation/state-manager.d.ts +61 -0
  155. package/dist/instrumentation/state-manager.d.ts.map +1 -0
  156. package/dist/instrumentation/state-manager.js +205 -0
  157. package/dist/instrumentation/state-manager.js.map +1 -0
  158. package/dist/instrumentation/transformer.d.ts +9 -0
  159. package/dist/instrumentation/transformer.d.ts.map +1 -0
  160. package/dist/instrumentation/transformer.js +70 -0
  161. package/dist/instrumentation/transformer.js.map +1 -0
  162. package/dist/instrumentation/types.d.ts +59 -0
  163. package/dist/instrumentation/types.d.ts.map +1 -0
  164. package/dist/instrumentation/types.js +5 -0
  165. package/dist/instrumentation/types.js.map +1 -0
  166. package/dist/middleware/audit.d.ts +18 -0
  167. package/dist/middleware/audit.d.ts.map +1 -0
  168. package/dist/middleware/audit.js +76 -0
  169. package/dist/middleware/audit.js.map +1 -0
  170. package/dist/openapi/index.d.ts +133 -0
  171. package/dist/openapi/index.d.ts.map +1 -0
  172. package/dist/openapi/index.js +235 -0
  173. package/dist/openapi/index.js.map +1 -0
  174. package/dist/openapi-loader.d.ts +87 -0
  175. package/dist/openapi-loader.d.ts.map +1 -0
  176. package/dist/openapi-loader.js +491 -0
  177. package/dist/openapi-loader.js.map +1 -0
  178. package/dist/routes/index.d.ts +21 -0
  179. package/dist/routes/index.d.ts.map +1 -0
  180. package/dist/routes/index.js +47 -0
  181. package/dist/routes/index.js.map +1 -0
  182. package/dist/search/index.d.ts +48 -0
  183. package/dist/search/index.d.ts.map +1 -0
  184. package/dist/search/index.js +156 -0
  185. package/dist/search/index.js.map +1 -0
  186. package/dist/security/index.d.ts +2 -0
  187. package/dist/security/index.d.ts.map +1 -0
  188. package/dist/security/index.js +2 -0
  189. package/dist/security/index.js.map +1 -0
  190. package/dist/shutdown.d.ts +19 -0
  191. package/dist/shutdown.d.ts.map +1 -0
  192. package/dist/shutdown.js +87 -0
  193. package/dist/shutdown.js.map +1 -0
  194. package/dist/utils/banner.d.ts +12 -0
  195. package/dist/utils/banner.d.ts.map +1 -0
  196. package/dist/utils/banner.js +18 -0
  197. package/dist/utils/banner.js.map +1 -0
  198. package/dist/utils/context.d.ts +16 -0
  199. package/dist/utils/context.d.ts.map +1 -0
  200. package/dist/utils/context.js +44 -0
  201. package/dist/utils/context.js.map +1 -0
  202. package/dist/utils/error.d.ts +8 -0
  203. package/dist/utils/error.d.ts.map +1 -0
  204. package/dist/utils/error.js +17 -0
  205. package/dist/utils/error.js.map +1 -0
  206. package/dist/utils/hint-based-instrumentation.d.ts +14 -0
  207. package/dist/utils/hint-based-instrumentation.d.ts.map +1 -0
  208. package/dist/utils/hint-based-instrumentation.js +84 -0
  209. package/dist/utils/hint-based-instrumentation.js.map +1 -0
  210. package/dist/utils/index.d.ts +8 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/index.js +8 -0
  213. package/dist/utils/index.js.map +1 -0
  214. package/dist/utils/info.d.ts +20 -0
  215. package/dist/utils/info.d.ts.map +1 -0
  216. package/dist/utils/info.js +15 -0
  217. package/dist/utils/info.js.map +1 -0
  218. package/dist/utils/provenance-reattachment.d.ts +32 -0
  219. package/dist/utils/provenance-reattachment.d.ts.map +1 -0
  220. package/dist/utils/provenance-reattachment.js +115 -0
  221. package/dist/utils/provenance-reattachment.js.map +1 -0
  222. package/dist/utils/request.d.ts +21 -0
  223. package/dist/utils/request.d.ts.map +1 -0
  224. package/dist/utils/request.js +44 -0
  225. package/dist/utils/request.js.map +1 -0
  226. package/dist/utils/response.d.ts +30 -0
  227. package/dist/utils/response.d.ts.map +1 -0
  228. package/dist/utils/response.js +53 -0
  229. package/dist/utils/response.js.map +1 -0
  230. package/dist/utils/runtime-types.d.ts +6 -0
  231. package/dist/utils/runtime-types.d.ts.map +1 -0
  232. package/dist/utils/runtime-types.js +14 -0
  233. package/dist/utils/runtime-types.js.map +1 -0
  234. package/dist/utils/schema.d.ts +9 -0
  235. package/dist/utils/schema.d.ts.map +1 -0
  236. package/dist/utils/schema.js +13 -0
  237. package/dist/utils/schema.js.map +1 -0
  238. package/dist/utils/token-emitter.d.ts +21 -0
  239. package/dist/utils/token-emitter.d.ts.map +1 -0
  240. package/dist/utils/token-emitter.js +129 -0
  241. package/dist/utils/token-emitter.js.map +1 -0
  242. package/dist/validator/index.d.ts +36 -0
  243. package/dist/validator/index.d.ts.map +1 -0
  244. package/dist/validator/index.js +224 -0
  245. package/dist/validator/index.js.map +1 -0
  246. package/package.json +68 -0
  247. package/src/aggregator/index.ts +207 -0
  248. package/src/callback/index.ts +191 -0
  249. package/src/client-sessions.ts +234 -0
  250. package/src/controllers/definitions.controller.ts +19 -0
  251. package/src/controllers/execute.controller.ts +166 -0
  252. package/src/controllers/info.controller.ts +14 -0
  253. package/src/controllers/resume.controller.ts +92 -0
  254. package/src/controllers/search.controller.ts +16 -0
  255. package/src/controllers/stream.controller.ts +190 -0
  256. package/src/core/config.ts +180 -0
  257. package/src/core/http.ts +21 -0
  258. package/src/create-server.ts +536 -0
  259. package/src/execution-state/index.ts +204 -0
  260. package/src/executor/ast-provenance-bridge.ts +80 -0
  261. package/src/executor/ast-tracking-runtime.ts +558 -0
  262. package/src/executor/bootstrap-generated.ts +90 -0
  263. package/src/executor/compiler-config.ts +146 -0
  264. package/src/executor/constants.ts +5 -0
  265. package/src/executor/error-handler.ts +118 -0
  266. package/src/executor/execution-error-handler.ts +178 -0
  267. package/src/executor/executor.ts +631 -0
  268. package/src/executor/index.ts +3 -0
  269. package/src/executor/resume-handler.ts +39 -0
  270. package/src/executor/sandbox-builder.ts +684 -0
  271. package/src/executor/sandbox-injector.ts +345 -0
  272. package/src/executor/types.ts +22 -0
  273. package/src/explorer/index.ts +297 -0
  274. package/src/handlers/definitions.handler.ts +13 -0
  275. package/src/handlers/execute.handler.ts +286 -0
  276. package/src/handlers/explorer.handler.ts +18 -0
  277. package/src/handlers/init.handler.ts +53 -0
  278. package/src/handlers/resume.handler.ts +316 -0
  279. package/src/handlers/search.handler.ts +32 -0
  280. package/src/http/request-handler.ts +117 -0
  281. package/src/http/router.ts +29 -0
  282. package/src/index.ts +60 -0
  283. package/src/instrumentation/index.ts +4 -0
  284. package/src/instrumentation/serializer.ts +421 -0
  285. package/src/instrumentation/state-manager.ts +237 -0
  286. package/src/instrumentation/transformer.ts +84 -0
  287. package/src/instrumentation/types.ts +76 -0
  288. package/src/middleware/audit.ts +101 -0
  289. package/src/openapi/index.ts +378 -0
  290. package/src/openapi-loader.ts +744 -0
  291. package/src/routes/index.ts +93 -0
  292. package/src/search/index.ts +216 -0
  293. package/src/security/index.ts +1 -0
  294. package/src/shutdown.ts +108 -0
  295. package/src/utils/banner.ts +25 -0
  296. package/src/utils/context.ts +58 -0
  297. package/src/utils/error.ts +25 -0
  298. package/src/utils/hint-based-instrumentation.ts +99 -0
  299. package/src/utils/index.ts +15 -0
  300. package/src/utils/info.ts +31 -0
  301. package/src/utils/provenance-reattachment.ts +144 -0
  302. package/src/utils/request.ts +53 -0
  303. package/src/utils/response.ts +69 -0
  304. package/src/utils/runtime-types.ts +14 -0
  305. package/src/utils/schema.ts +18 -0
  306. package/src/utils/token-emitter.ts +182 -0
  307. package/src/validator/index.ts +253 -0
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Provenance Re-attachment Utility
3
+ *
4
+ * Re-attaches provenance to values based on verified hints
5
+ */
6
+ import {
7
+ getProvenance,
8
+ getProvenanceForPrimitive,
9
+ createProvenanceProxy,
10
+ markPrimitiveTainted,
11
+ computeDigest,
12
+ type ProvenanceMetadata,
13
+ } from '@mondaydotcomorg/atp-provenance';
14
+
15
+ /**
16
+ * Global registry of hint maps per execution
17
+ * Key: executionId, Value: Map<digest, metadata>
18
+ */
19
+ const executionHintMaps = new Map<string, Map<string, ProvenanceMetadata>>();
20
+
21
+ /**
22
+ * Global registry of hint values per execution
23
+ * Key: executionId, Value: Map<value, metadata> for substring checking
24
+ */
25
+ const executionHintValues = new Map<string, Map<string, ProvenanceMetadata>>();
26
+
27
+ /**
28
+ * Store hint map for an execution
29
+ */
30
+ export function storeHintMap(executionId: string, hintMap: Map<string, ProvenanceMetadata>): void {
31
+ executionHintMaps.set(executionId, hintMap);
32
+ }
33
+
34
+ /**
35
+ * Store a hint value for substring matching
36
+ */
37
+ export function storeHintValue(
38
+ executionId: string,
39
+ value: string,
40
+ metadata: ProvenanceMetadata
41
+ ): void {
42
+ let valueMap = executionHintValues.get(executionId);
43
+ if (!valueMap) {
44
+ valueMap = new Map();
45
+ executionHintValues.set(executionId, valueMap);
46
+ }
47
+ valueMap.set(value, metadata);
48
+ }
49
+
50
+ /**
51
+ * Get hint map for an execution
52
+ */
53
+ export function getHintMap(executionId: string): Map<string, ProvenanceMetadata> | undefined {
54
+ return executionHintMaps.get(executionId);
55
+ }
56
+
57
+ /**
58
+ * Get hint values for an execution (for substring matching)
59
+ */
60
+ export function getHintValues(executionId: string): Map<string, ProvenanceMetadata> | undefined {
61
+ return executionHintValues.get(executionId);
62
+ }
63
+
64
+ /**
65
+ * Clear hint map for an execution (cleanup)
66
+ */
67
+ export function clearHintMap(executionId: string): void {
68
+ executionHintMaps.delete(executionId);
69
+ executionHintValues.delete(executionId);
70
+ }
71
+
72
+ /**
73
+ * Re-attach provenance from hints to tool arguments
74
+ * Scans arguments recursively and attaches provenance based on value digests
75
+ */
76
+ export function reattachProvenanceFromHints(
77
+ args: Record<string, unknown>,
78
+ hintMap: Map<string, ProvenanceMetadata>
79
+ ): void {
80
+ if (!hintMap || hintMap.size === 0) {
81
+ return;
82
+ }
83
+
84
+ const visited = new WeakSet<object>();
85
+
86
+ function processValue(value: unknown): void {
87
+ if (value === null || value === undefined) {
88
+ return;
89
+ }
90
+
91
+ // Handle primitives (string/number)
92
+ if (typeof value === 'string' || typeof value === 'number') {
93
+ // Skip if already has provenance
94
+ if (getProvenanceForPrimitive(value)) {
95
+ return;
96
+ }
97
+
98
+ // Compute digest and check hint map
99
+ const digest = computeDigest(value);
100
+ if (digest && hintMap.has(digest)) {
101
+ const metadata = hintMap.get(digest)!;
102
+ markPrimitiveTainted(value, metadata);
103
+ }
104
+ return;
105
+ }
106
+
107
+ // Handle objects/arrays
108
+ if (typeof value === 'object') {
109
+ // Prevent circular reference processing
110
+ if (visited.has(value as object)) {
111
+ return;
112
+ }
113
+ visited.add(value as object);
114
+
115
+ // Skip if already has provenance
116
+ if (getProvenance(value)) {
117
+ return;
118
+ }
119
+
120
+ // Check if object itself has provenance in hints
121
+ const digest = computeDigest(value);
122
+ if (digest && hintMap.has(digest)) {
123
+ const metadata = hintMap.get(digest)!;
124
+ // Note: We can't modify the object in place, but we mark primitives inside
125
+ }
126
+
127
+ // Process children
128
+ if (Array.isArray(value)) {
129
+ for (const item of value) {
130
+ processValue(item);
131
+ }
132
+ } else {
133
+ for (const childValue of Object.values(value as Record<string, unknown>)) {
134
+ processValue(childValue);
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ // Process all argument values
141
+ for (const value of Object.values(args)) {
142
+ processValue(value);
143
+ }
144
+ }
@@ -0,0 +1,53 @@
1
+ import { IncomingMessage } from 'node:http';
2
+
3
+ /**
4
+ * Default maximum request body size (10MB)
5
+ */
6
+ export const DEFAULT_MAX_BODY_SIZE = 10 * 1024 * 1024;
7
+
8
+ /**
9
+ * Reads the full request body as a string
10
+ * @param req - The HTTP request
11
+ * @param maxSize - Maximum allowed body size in bytes (default: 10MB)
12
+ * @returns Promise resolving to the complete body string
13
+ * @throws Error if body exceeds maxSize
14
+ */
15
+ export function readBody(req: IncomingMessage, maxSize = DEFAULT_MAX_BODY_SIZE): Promise<string> {
16
+ return new Promise((resolve, reject) => {
17
+ let body = '';
18
+ let size = 0;
19
+
20
+ req.on('data', (chunk) => {
21
+ size += chunk.length;
22
+
23
+ if (size > maxSize) {
24
+ req.destroy();
25
+ reject(new Error(`Request body too large (max ${maxSize} bytes)`));
26
+ return;
27
+ }
28
+
29
+ body += chunk.toString();
30
+ });
31
+
32
+ req.on('end', () => resolve(body));
33
+ req.on('error', reject);
34
+ });
35
+ }
36
+
37
+ /**
38
+ * Reads and parses request body as JSON
39
+ * @param req - The HTTP request
40
+ * @param maxSize - Maximum allowed body size in bytes
41
+ * @returns Promise resolving to the parsed JSON object
42
+ */
43
+ export async function readJsonBody<T = any>(
44
+ req: IncomingMessage,
45
+ maxSize = DEFAULT_MAX_BODY_SIZE
46
+ ): Promise<T> {
47
+ const body = await readBody(req, maxSize);
48
+ try {
49
+ return body ? JSON.parse(body) : (null as T);
50
+ } catch (error) {
51
+ throw new Error('Invalid JSON');
52
+ }
53
+ }
@@ -0,0 +1,69 @@
1
+ import { ServerResponse } from 'node:http';
2
+
3
+ /**
4
+ * Sends a JSON response
5
+ */
6
+ export function sendJson(res: ServerResponse, data: unknown, status = 200): void {
7
+ res.writeHead(status, { 'Content-Type': 'application/json' });
8
+ res.end(JSON.stringify(data));
9
+ }
10
+
11
+ /**
12
+ * Sends an error response
13
+ */
14
+ export function sendError(
15
+ res: ServerResponse,
16
+ error: string | Error,
17
+ status = 500,
18
+ requestId?: string
19
+ ): void {
20
+ const message = error instanceof Error ? error.message : error;
21
+ sendJson(
22
+ res,
23
+ {
24
+ error: message,
25
+ ...(requestId && { requestId }),
26
+ },
27
+ status
28
+ );
29
+ }
30
+
31
+ /**
32
+ * Sends a 404 Not Found response
33
+ */
34
+ export function send404(res: ServerResponse): void {
35
+ sendJson(res, { error: 'Not found' }, 404);
36
+ }
37
+
38
+ /**
39
+ * Sends a 400 Bad Request response
40
+ */
41
+ export function sendBadRequest(res: ServerResponse, message: string): void {
42
+ sendJson(res, { error: message }, 400);
43
+ }
44
+
45
+ /**
46
+ * Sends a 503 Service Unavailable response
47
+ */
48
+ export function sendServiceUnavailable(res: ServerResponse, message: string): void {
49
+ sendJson(res, { error: message }, 503);
50
+ }
51
+
52
+ /**
53
+ * Sets CORS headers on a response
54
+ */
55
+ export function setCorsHeaders(res: ServerResponse, origin = '*'): void {
56
+ res.setHeader('Access-Control-Allow-Origin', origin);
57
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
58
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Client-ID');
59
+ res.setHeader('Access-Control-Max-Age', '86400');
60
+ }
61
+
62
+ /**
63
+ * Handles OPTIONS preflight requests
64
+ */
65
+ export function handleOptions(res: ServerResponse): void {
66
+ setCorsHeaders(res);
67
+ res.writeHead(204);
68
+ res.end();
69
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Runtime SDK Type Generator
3
+ *
4
+ * Generates TypeScript definitions from the runtime API registry.
5
+ */
6
+ import { GENERATED_METADATA, generateRuntimeTypes as generate } from '@mondaydotcomorg/atp-runtime';
7
+
8
+ /**
9
+ * Generates TypeScript definitions for the runtime SDK
10
+ * Delegates to the runtime package's own type generator
11
+ */
12
+ export function generateRuntimeTypes(): string {
13
+ return generate(GENERATED_METADATA);
14
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Converts a simple type map to a JSON Schema object
3
+ */
4
+ export function toJSONSchema(types: Record<string, string>): {
5
+ type: 'object';
6
+ properties: Record<string, unknown>;
7
+ required: string[];
8
+ } {
9
+ const properties: Record<string, unknown> = {};
10
+ const required: string[] = [];
11
+
12
+ for (const [key, type] of Object.entries(types)) {
13
+ properties[key] = { type };
14
+ required.push(key);
15
+ }
16
+
17
+ return { type: 'object', properties, required };
18
+ }
@@ -0,0 +1,182 @@
1
+ import {
2
+ issueProvenanceToken,
3
+ type ProvenanceMetadata,
4
+ type ProvenanceMode,
5
+ ProvenanceMode as PM,
6
+ } from '@mondaydotcomorg/atp-provenance';
7
+ import type { CacheProvider } from '@mondaydotcomorg/atp-protocol';
8
+ import type { log } from '@mondaydotcomorg/atp-runtime';
9
+
10
+ type Logger = ReturnType<typeof log.child>;
11
+
12
+ interface TokenEmission {
13
+ path: string;
14
+ token: string;
15
+ }
16
+
17
+ /**
18
+ * Emits provenance tokens for all values in the result that have provenance in the snapshot.
19
+ * This works by:
20
+ * 1. Traversing the actual serialized result object
21
+ * 2. For each value, checking if it matches provenance in the snapshot
22
+ * 3. Emitting tokens with the ACTUAL value for correct digest matching
23
+ */
24
+ export async function emitProvenanceTokens(
25
+ result: unknown,
26
+ clientId: string,
27
+ executionId: string,
28
+ provenanceMode: ProvenanceMode,
29
+ cacheProvider: CacheProvider,
30
+ logger: Logger,
31
+ maxTokens: number = 5000,
32
+ tokenTTL: number = 3600,
33
+ provenanceSnapshot?: {
34
+ registry: Array<[string, ProvenanceMetadata]>;
35
+ primitives: Array<[string, ProvenanceMetadata]>;
36
+ }
37
+ ): Promise<TokenEmission[]> {
38
+ if (provenanceMode === PM.NONE || !result || !provenanceSnapshot) {
39
+ logger.debug('Skipping token emission', {
40
+ hasResult: !!result,
41
+ hasSnapshot: !!provenanceSnapshot,
42
+ mode: provenanceMode,
43
+ });
44
+ return [];
45
+ }
46
+
47
+ logger.info('Token emission starting from snapshot', {
48
+ executionId,
49
+ registrySize: provenanceSnapshot.registry.length,
50
+ primitiveMapSize: provenanceSnapshot.primitives.length,
51
+ resultType: typeof result,
52
+ });
53
+
54
+ const tokens: TokenEmission[] = [];
55
+ const visited = new WeakSet<object>();
56
+ const primitiveMap = new Map<string, ProvenanceMetadata>(provenanceSnapshot.primitives);
57
+
58
+ const taintedValues = new Set<string>();
59
+ for (const [key] of provenanceSnapshot.primitives) {
60
+ if (key.startsWith('tainted:')) {
61
+ taintedValues.add(key.slice('tainted:'.length));
62
+ }
63
+ }
64
+
65
+ const queue: Array<{ value: unknown; path: string }> = [{ value: result, path: '' }];
66
+
67
+ while (queue.length > 0 && tokens.length < maxTokens) {
68
+ const { value, path } = queue.shift()!;
69
+
70
+ if (value === null || value === undefined) {
71
+ continue;
72
+ }
73
+
74
+ if (typeof value === 'string' || typeof value === 'number') {
75
+ const valueStr = String(value);
76
+
77
+ const taintedKey = `tainted:${valueStr}`;
78
+ let meta = primitiveMap.get(taintedKey);
79
+
80
+ if (!meta) {
81
+ for (const [key, metadata] of primitiveMap.entries()) {
82
+ if (!key.startsWith('tainted:')) {
83
+ const parts = key.split(':');
84
+ if (parts.length >= 3) {
85
+ const derivedValue = parts.slice(2).join(':');
86
+ if (derivedValue === valueStr) {
87
+ meta = metadata;
88
+ logger.debug('Found property-derived primitive match', {
89
+ path,
90
+ key,
91
+ valuePreview: valueStr.substring(0, 30),
92
+ });
93
+ break;
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+ if (meta) {
101
+ try {
102
+ const token = await issueProvenanceToken(
103
+ meta,
104
+ value,
105
+ clientId,
106
+ executionId,
107
+ cacheProvider,
108
+ tokenTTL
109
+ );
110
+ if (token) {
111
+ tokens.push({ path, token });
112
+ logger.debug('Emitted token for primitive', {
113
+ path,
114
+ valuePreview: typeof value === 'string' ? value.substring(0, 30) : value,
115
+ tokenPrefix: token.substring(0, 10),
116
+ });
117
+ }
118
+ } catch (error) {
119
+ logger.warn('Failed to issue token for primitive', { path, error });
120
+ }
121
+ }
122
+ continue;
123
+ }
124
+
125
+ if (typeof value === 'object') {
126
+ if (visited.has(value as object)) {
127
+ continue;
128
+ }
129
+ visited.add(value as object);
130
+
131
+ // For objects, we need to check if ANY of the registry metadata applies
132
+ // Since we can't match by identity, we emit tokens for ALL registry entries
133
+ // and let the client match by digest
134
+ if (provenanceSnapshot.registry.length > 0 && path === '') {
135
+ for (const [id, meta] of provenanceSnapshot.registry) {
136
+ if (tokens.length >= maxTokens) break;
137
+
138
+ try {
139
+ const token = await issueProvenanceToken(
140
+ meta,
141
+ value,
142
+ clientId,
143
+ executionId,
144
+ cacheProvider,
145
+ tokenTTL
146
+ );
147
+ if (token) {
148
+ tokens.push({ path, token });
149
+ logger.debug('Emitted token for object', {
150
+ path,
151
+ id,
152
+ tokenPrefix: token.substring(0, 10),
153
+ });
154
+ }
155
+ } catch (error) {
156
+ logger.warn('Failed to issue token for object', { path, id, error });
157
+ }
158
+ }
159
+ }
160
+
161
+ if (Array.isArray(value)) {
162
+ for (let i = 0; i < value.length; i++) {
163
+ queue.push({ value: value[i], path: `${path}/${i}` });
164
+ }
165
+ } else {
166
+ for (const key in value) {
167
+ if (Object.prototype.hasOwnProperty.call(value, key)) {
168
+ const escapedKey = key.replace(/~/g, '~0').replace(/\//g, '~1');
169
+ queue.push({ value: (value as any)[key], path: `${path}/${escapedKey}` });
170
+ }
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ if (tokens.length >= maxTokens) {
177
+ logger.warn('Max provenance tokens reached', { executionId, maxTokens });
178
+ }
179
+
180
+ logger.info('Token emission completed', { executionId, tokenCount: tokens.length });
181
+ return tokens;
182
+ }