@plitzi/sdk-server 0.38.19 → 0.38.21

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 (242) hide show
  1. package/CHANGELOG.md +178 -0
  2. package/README.md +2 -2
  3. package/dist/actions.d.ts +6 -0
  4. package/dist/actions.js +2 -1
  5. package/dist/core/auth/createAuth.d.ts +1 -1
  6. package/dist/core/auth/csrf.js +13 -8
  7. package/dist/core/auth/tokens.d.ts +1 -1
  8. package/dist/core/configSeam.js +2 -2
  9. package/dist/core/services/functionRoutes.js +8 -2
  10. package/dist/core/services/realtime.js +5 -2
  11. package/dist/functions-api.d.ts +228 -0
  12. package/dist/functions.d.ts +4 -2
  13. package/dist/functions.js +2 -1
  14. package/dist/helpers/serverLog.d.ts +1 -1
  15. package/dist/helpers/serverLog.js +1 -1
  16. package/dist/hosts/project/plugins.js +20 -3
  17. package/dist/modules/actions/index.js +16 -3
  18. package/dist/modules/actions/jobs/index.d.ts +4 -3
  19. package/dist/modules/actions/jobs/index.js +2 -1
  20. package/dist/modules/actions/jobs/later.d.ts +2 -2
  21. package/dist/modules/actions/jobs/later.js +5 -2
  22. package/dist/modules/actions/jobs/memoryQueue.d.ts +4 -4
  23. package/dist/modules/actions/jobs/memoryQueue.js +69 -25
  24. package/dist/modules/actions/jobs/scheduler.d.ts +2 -2
  25. package/dist/modules/actions/jobs/scheduler.js +5 -2
  26. package/dist/modules/actions/jobs/worker.d.ts +6 -3
  27. package/dist/modules/actions/jobs/worker.js +104 -10
  28. package/dist/modules/actions/runtime/runAction.d.ts +5 -2
  29. package/dist/modules/actions/runtime/runAction.js +5 -3
  30. package/dist/modules/actions/runtime/signing.d.ts +7 -0
  31. package/dist/modules/actions/runtime/signing.js +23 -1
  32. package/dist/modules/actions/runtime/taskContext.d.ts +12 -3
  33. package/dist/modules/actions/runtime/taskContext.js +8 -1
  34. package/dist/modules/actions/transport/callHandler.js +3 -0
  35. package/dist/modules/actions/types.d.ts +29 -4
  36. package/dist/modules/functions/capabilities.js +107 -0
  37. package/dist/modules/functions/config.d.ts +9 -0
  38. package/dist/modules/functions/context.d.ts +2 -1
  39. package/dist/modules/functions/context.js +21 -3
  40. package/dist/modules/functions/contract.d.ts +129 -1
  41. package/dist/modules/functions/driver.js +128 -2
  42. package/dist/modules/functions/jobControl.d.ts +21 -0
  43. package/dist/modules/functions/jobControl.js +43 -0
  44. package/dist/modules/functions/jobs.d.ts +98 -0
  45. package/dist/modules/functions/jobs.js +438 -0
  46. package/dist/modules/functions/manifest.js +117 -2
  47. package/dist/modules/functions/native.js +2 -1
  48. package/dist/modules/functions/oauth.d.ts +31 -0
  49. package/dist/modules/functions/oauth.js +326 -0
  50. package/dist/modules/functions/protocol.d.ts +88 -3
  51. package/dist/modules/functions/protocol.js +2 -1
  52. package/dist/modules/functions/routes.d.ts +11 -1
  53. package/dist/modules/functions/routes.js +71 -30
  54. package/dist/modules/functions/runner/messages.d.ts +0 -1
  55. package/dist/modules/functions/runner/messages.js +40 -2
  56. package/dist/modules/functions/runner/remote.js +58 -3
  57. package/dist/modules/functions/runner/service.js +53 -7
  58. package/dist/modules/functions/sandbox/isolate.js +5 -3
  59. package/dist/modules/functions/space.js +4 -4
  60. package/dist/modules/mongo/jobQueue.d.ts +3 -3
  61. package/dist/modules/mongo/jobQueue.js +137 -26
  62. package/dist/modules/mysql/jobs/jobQueue.d.ts +3 -3
  63. package/dist/modules/mysql/jobs/jobQueue.js +97 -33
  64. package/dist/modules/mysql/jobs/schema.js +38 -11
  65. package/dist/modules/realtime/handlers.d.ts +5 -2
  66. package/dist/modules/realtime/handlers.js +15 -4
  67. package/dist/modules/realtime/index.d.ts +11 -1
  68. package/dist/modules/realtime/index.js +8 -1
  69. package/dist/modules/realtime/jobs.d.ts +20 -0
  70. package/dist/modules/realtime/jobs.js +41 -0
  71. package/dist/modules/realtime/retained.d.ts +3 -2
  72. package/dist/modules/realtime/retained.js +2 -2
  73. package/dist/modules/realtime/socket.d.ts +4 -1
  74. package/dist/modules/realtime/socket.js +2 -2
  75. package/dist/modules/rsc/resolveRscData.js +2 -1
  76. package/dist/modules/runtime/host.js +1 -1
  77. package/dist/modules/runtime/runner.d.ts +1 -1
  78. package/dist/modules/runtime/runner.js +12 -2
  79. package/dist/modules/runtime/stages.js +1 -1
  80. package/dist/modules/ssr/applySSRResult.d.ts +5 -0
  81. package/dist/modules/ssr/applySSRResult.js +8 -1
  82. package/dist/modules/ssr/buildBody.js +2 -1
  83. package/dist/modules/ssr/prepareRender.d.ts +3 -0
  84. package/dist/modules/ssr/prepareRender.js +17 -5
  85. package/dist/modules/ssr/spaceDocument.d.ts +2 -1
  86. package/dist/modules/ssr/spaceDocument.js +14 -5
  87. package/dist/modules/ssr/streamBody.js +2 -1
  88. package/package.json +6 -3
  89. package/skills/plitzi-authoring/reference/auth.md +6 -0
  90. package/skills/plitzi-authoring/reference/authoring-errors.md +1 -0
  91. package/skills/plitzi-authoring/reference/plugin-reach.md +3 -0
  92. package/skills/plitzi-authoring/reference/realtime.md +4 -0
  93. package/dist/adapters/cloudAdapters.test.d.ts +0 -1
  94. package/dist/adapters/jsonAdapters.test.d.ts +0 -1
  95. package/dist/adapters/space/space.test.d.ts +0 -1
  96. package/dist/core/auth/api.test.d.ts +0 -1
  97. package/dist/core/auth/authorize.test.d.ts +0 -1
  98. package/dist/core/auth/createAuth.test.d.ts +0 -1
  99. package/dist/core/auth/csrf.test.d.ts +0 -1
  100. package/dist/core/auth/domains.test.d.ts +0 -1
  101. package/dist/core/auth/identity.test.d.ts +0 -1
  102. package/dist/core/auth/lifecycle.test.d.ts +0 -1
  103. package/dist/core/auth/oauth/oauth.test.d.ts +0 -1
  104. package/dist/core/auth/redirects.test.d.ts +0 -1
  105. package/dist/core/auth/routes.test.d.ts +0 -1
  106. package/dist/core/auth/session.test.d.ts +0 -1
  107. package/dist/core/auth/spaceTokens.test.d.ts +0 -1
  108. package/dist/core/auth/throttle.test.d.ts +0 -1
  109. package/dist/core/auth/tokens.test.d.ts +0 -1
  110. package/dist/core/auth/totp.test.d.ts +0 -1
  111. package/dist/core/configSeam.test.d.ts +0 -1
  112. package/dist/core/createServer.actions.test.d.ts +0 -1
  113. package/dist/core/createServer.functions.test.d.ts +0 -1
  114. package/dist/core/createServer.livePages.test.d.ts +0 -1
  115. package/dist/core/createServer.notFound.test.d.ts +0 -1
  116. package/dist/core/createServer.publicData.test.d.ts +0 -1
  117. package/dist/core/createServer.test.d.ts +0 -1
  118. package/dist/core/fontStore.test.d.ts +0 -1
  119. package/dist/core/freePort.test.d.ts +0 -1
  120. package/dist/core/handlers/csrfMiddleware.test.d.ts +0 -1
  121. package/dist/core/handlers/handlers.test.d.ts +0 -1
  122. package/dist/core/handlers/socialRouteHandlers.test.d.ts +0 -1
  123. package/dist/core/http/dispatcher.test.d.ts +0 -1
  124. package/dist/core/http/stages/crossSiteWrites.test.d.ts +0 -1
  125. package/dist/core/http/stages/exchangeRoute.test.d.ts +0 -1
  126. package/dist/core/http/stages/images.test.d.ts +0 -1
  127. package/dist/core/http/stages/middlewares.test.d.ts +0 -1
  128. package/dist/core/http/stages/sessionRenewal.test.d.ts +0 -1
  129. package/dist/core/http/stages/signInRoute.test.d.ts +0 -1
  130. package/dist/core/oauth/authorize.test.d.ts +0 -1
  131. package/dist/core/oauth/register.test.d.ts +0 -1
  132. package/dist/core/oauth/token.test.d.ts +0 -1
  133. package/dist/core/requestParser.test.d.ts +0 -1
  134. package/dist/core/server/baseServer.test.d.ts +0 -1
  135. package/dist/core/server/closeOnSignals.test.d.ts +0 -1
  136. package/dist/core/server/drain.test.d.ts +0 -1
  137. package/dist/core/server/fleet/channel.test.d.ts +0 -1
  138. package/dist/core/server/fleet/fixtures/fleetServer.d.ts +0 -1
  139. package/dist/core/server/fleet/fleet.integration.test.d.ts +0 -1
  140. package/dist/core/server/fleet/relay.test.d.ts +0 -1
  141. package/dist/core/server/fleet/role.test.d.ts +0 -1
  142. package/dist/core/server/fleet/supervisor.test.d.ts +0 -1
  143. package/dist/core/services/resolve.test.d.ts +0 -1
  144. package/dist/core/staticFiles.test.d.ts +0 -1
  145. package/dist/env.test.d.ts +0 -1
  146. package/dist/helpers/atomicFile.test.d.ts +0 -1
  147. package/dist/helpers/buildResponseHelpers.test.d.ts +0 -1
  148. package/dist/helpers/buildServerInfo.test.d.ts +0 -1
  149. package/dist/helpers/cache/CompressedFileCache.test.d.ts +0 -1
  150. package/dist/helpers/cache/cacheManager.test.d.ts +0 -1
  151. package/dist/helpers/cache/keys.test.d.ts +0 -1
  152. package/dist/helpers/compress.test.d.ts +0 -1
  153. package/dist/helpers/hydrationPayload.test.d.ts +0 -1
  154. package/dist/helpers/offlineDataLoader.test.d.ts +0 -1
  155. package/dist/helpers/onAbort.test.d.ts +0 -1
  156. package/dist/helpers/outboundGuard.test.d.ts +0 -1
  157. package/dist/helpers/reportReactBuild.test.d.ts +0 -1
  158. package/dist/helpers/resolveDebugMode.test.d.ts +0 -1
  159. package/dist/helpers/serverLog.test.d.ts +0 -1
  160. package/dist/hosts/authoringHost/index.test.d.ts +0 -1
  161. package/dist/hosts/project/serveProject.test.d.ts +0 -1
  162. package/dist/layers.test.d.ts +0 -1
  163. package/dist/middlewares/spaceDeployment.test.d.ts +0 -1
  164. package/dist/modules/actions/connectorWrite.test.d.ts +0 -1
  165. package/dist/modules/actions/jobs/failureStreak.test.d.ts +0 -1
  166. package/dist/modules/actions/jobs/later.test.d.ts +0 -1
  167. package/dist/modules/actions/jobs/memoryStores.test.d.ts +0 -1
  168. package/dist/modules/actions/jobs/scheduler.test.d.ts +0 -1
  169. package/dist/modules/actions/jobs/testing/jobQueueContract.d.ts +0 -25
  170. package/dist/modules/actions/jobs/worker.test.d.ts +0 -1
  171. package/dist/modules/actions/moduleFor.test.d.ts +0 -1
  172. package/dist/modules/actions/runtime/check.test.d.ts +0 -1
  173. package/dist/modules/actions/runtime/credentials.test.d.ts +0 -1
  174. package/dist/modules/actions/runtime/email.test.d.ts +0 -1
  175. package/dist/modules/actions/runtime/fileKv.test.d.ts +0 -1
  176. package/dist/modules/actions/runtime/flags.test.d.ts +0 -1
  177. package/dist/modules/actions/runtime/guards.test.d.ts +0 -1
  178. package/dist/modules/actions/runtime/kvStore.test.d.ts +0 -1
  179. package/dist/modules/actions/runtime/namespaceKv.test.d.ts +0 -1
  180. package/dist/modules/actions/runtime/redisKv.integration.test.d.ts +0 -1
  181. package/dist/modules/actions/runtime/renderResolver.test.d.ts +0 -1
  182. package/dist/modules/actions/runtime/renderShare.test.d.ts +0 -1
  183. package/dist/modules/actions/runtime/runAction.test.d.ts +0 -1
  184. package/dist/modules/actions/runtime/runLogger.test.d.ts +0 -1
  185. package/dist/modules/actions/runtime/signing.test.d.ts +0 -1
  186. package/dist/modules/actions/taskCatalog.test.d.ts +0 -1
  187. package/dist/modules/actions/tasks/db.test.d.ts +0 -1
  188. package/dist/modules/actions/tasks/email.test.d.ts +0 -1
  189. package/dist/modules/actions/tasks/http.test.d.ts +0 -1
  190. package/dist/modules/actions/tasks/kv.test.d.ts +0 -1
  191. package/dist/modules/actions/transport/callHandler.test.d.ts +0 -1
  192. package/dist/modules/actions/transport/webhookHandler.test.d.ts +0 -1
  193. package/dist/modules/connectors/engine.test.d.ts +0 -1
  194. package/dist/modules/connectors/projection.test.d.ts +0 -1
  195. package/dist/modules/connectors/resolver.test.d.ts +0 -1
  196. package/dist/modules/functions/build.test.d.ts +0 -1
  197. package/dist/modules/functions/load.test.d.ts +0 -1
  198. package/dist/modules/functions/local.test.d.ts +0 -1
  199. package/dist/modules/functions/manifest.test.d.ts +0 -1
  200. package/dist/modules/functions/native.test.d.ts +0 -1
  201. package/dist/modules/functions/plugins.test.d.ts +0 -1
  202. package/dist/modules/functions/routes.test.d.ts +0 -1
  203. package/dist/modules/functions/runner/remote.test.d.ts +0 -1
  204. package/dist/modules/functions/sandbox/isolate.test.d.ts +0 -1
  205. package/dist/modules/functions/testing/tasksOf.d.ts +0 -4
  206. package/dist/modules/images/imageProxy.test.d.ts +0 -1
  207. package/dist/modules/images/transform.test.d.ts +0 -1
  208. package/dist/modules/mongo/stores.integration.test.d.ts +0 -1
  209. package/dist/modules/mysql/accounts.test.d.ts +0 -1
  210. package/dist/modules/mysql/jobs/stores.integration.test.d.ts +0 -1
  211. package/dist/modules/mysql/schema.test.d.ts +0 -1
  212. package/dist/modules/mysql/store.integration.test.d.ts +0 -1
  213. package/dist/modules/realtime/realtime.test.d.ts +0 -1
  214. package/dist/modules/realtime/redisPubSub.test.d.ts +0 -1
  215. package/dist/modules/realtime/retained.test.d.ts +0 -1
  216. package/dist/modules/realtime/socket.test.d.ts +0 -1
  217. package/dist/modules/rsc/connectorRscData.test.d.ts +0 -1
  218. package/dist/modules/rsc/handler.test.d.ts +0 -1
  219. package/dist/modules/rsc/publicFileResolver.test.d.ts +0 -1
  220. package/dist/modules/rsc/resolveRscData.test.d.ts +0 -1
  221. package/dist/modules/runtime/elsewhere.test.d.ts +0 -1
  222. package/dist/modules/runtime/entry.test.d.ts +0 -1
  223. package/dist/modules/runtime/inside.test.d.ts +0 -1
  224. package/dist/modules/runtime/ownServer.test.d.ts +0 -1
  225. package/dist/modules/runtime/runtime.test.d.ts +0 -1
  226. package/dist/modules/sqlite/kv.test.d.ts +0 -1
  227. package/dist/modules/ssr/draftSession.test.d.ts +0 -1
  228. package/dist/modules/ssr/loadPluginComponents.test.d.ts +0 -1
  229. package/dist/modules/ssr/metering.test.d.ts +0 -1
  230. package/dist/modules/ssr/prepareRender.test.d.ts +0 -1
  231. package/dist/modules/ssr/redirect.test.d.ts +0 -1
  232. package/dist/modules/ssr/registerExternalPlugins.test.d.ts +0 -1
  233. package/dist/modules/ssr/reportMissingPlugins.test.d.ts +0 -1
  234. package/dist/modules/ssr/resolvePageSeo.test.d.ts +0 -1
  235. package/dist/modules/ssr/spaceDocument.test.d.ts +0 -1
  236. package/dist/modules/ssr/template.test.d.ts +0 -1
  237. package/dist/modules/ssr/testing/offlineData.d.ts +0 -5
  238. package/dist/modules/ssr/vendor.test.d.ts +0 -1
  239. package/dist/plugins/compile.test.d.ts +0 -1
  240. package/dist/plugins/copy.test.d.ts +0 -1
  241. package/dist/plugins/manager.test.d.ts +0 -1
  242. package/dist/plugins/validate.test.d.ts +0 -1
@@ -18,7 +18,12 @@ var describeFunctions = (definition) => {
18
18
  limits: task.limits
19
19
  } : {}),
20
20
  routes: isRecord(found.routes) ? Object.keys(found.routes) : [],
21
- limits: found.limits
21
+ limits: found.limits,
22
+ oauth: found.oauth,
23
+ jobs: isRecord(found.jobs) ? Object.fromEntries(Object.entries(found.jobs).map(([name, job]) => [name, isRecord(job) ? {
24
+ watch: job.watch,
25
+ maxSeconds: job.maxSeconds
26
+ } : job])) : found.jobs
22
27
  };
23
28
  };
24
29
  /**
@@ -129,6 +134,74 @@ var createFunctionsDriver = (environment) => {
129
134
  op: "data",
130
135
  file
131
136
  }),
137
+ jobs: {
138
+ start: (name, input = {}, options = {}) => call({
139
+ op: "jobs",
140
+ method: "start",
141
+ name,
142
+ input,
143
+ key: options.key
144
+ }),
145
+ get: (id) => call({
146
+ op: "jobs",
147
+ method: "get",
148
+ id
149
+ }).then((found) => found ?? void 0),
150
+ pause: (id) => call({
151
+ op: "jobs",
152
+ method: "pause",
153
+ id
154
+ }),
155
+ resume: (id) => call({
156
+ op: "jobs",
157
+ method: "resume",
158
+ id
159
+ }),
160
+ cancel: (id) => call({
161
+ op: "jobs",
162
+ method: "cancel",
163
+ id
164
+ })
165
+ },
166
+ oauth: {
167
+ connect: (connection, options = {}) => call({
168
+ op: "oauth",
169
+ method: "connect",
170
+ connection,
171
+ returnTo: options.returnTo,
172
+ owner: options.owner
173
+ }),
174
+ status: (connection, options = {}) => call({
175
+ op: "oauth",
176
+ method: "status",
177
+ connection,
178
+ owner: options.owner
179
+ }),
180
+ disconnect: (connection, options = {}) => call({
181
+ op: "oauth",
182
+ method: "disconnect",
183
+ connection,
184
+ owner: options.owner
185
+ }),
186
+ token: (connection, options = {}) => call({
187
+ op: "oauth",
188
+ method: "token",
189
+ connection,
190
+ owner: options.owner
191
+ }),
192
+ fetch: async (connection, url, init = {}) => {
193
+ const { owner, ...rest } = init;
194
+ const answer = await call({
195
+ op: "oauth",
196
+ method: "fetch",
197
+ connection,
198
+ url: String(url),
199
+ init: rest,
200
+ owner
201
+ });
202
+ return responseOf(isRecord(answer) ? answer : {});
203
+ }
204
+ },
132
205
  log,
133
206
  emit: (chunk) => {
134
207
  call({
@@ -139,10 +212,63 @@ var createFunctionsDriver = (environment) => {
139
212
  signal
140
213
  };
141
214
  };
142
- /** One invocation: what a task returned (`null` for nothing), or the wire response a route answered. */
215
+ const STOPS = [
216
+ "pause",
217
+ "cancel",
218
+ "limit",
219
+ "shutdown"
220
+ ];
221
+ /**
222
+ * Why the slice is asked to end, once it is: the reason the signal was aborted with — said as itself in a runtime, and
223
+ * as the message of the `AbortError` the sandbox raises — and a cancel when it said none of them.
224
+ */
225
+ const stoppingOf = () => {
226
+ const current = signal;
227
+ if (typeof current !== "object" || current === null || !("aborted" in current) || current.aborted !== true) return;
228
+ const reason = "reason" in current ? current.reason : void 0;
229
+ const said = typeof reason === "string" ? reason : typeof reason === "object" && reason !== null && "message" in reason && typeof reason.message === "string" ? reason.message : void 0;
230
+ return said !== void 0 && STOPS.includes(said) ? said : "cancel";
231
+ };
232
+ /** A job's `run` and what it is handed: the slice, how it saves and reports, and why it is asked to end. */
233
+ const runJob = async (found, invocation, ctx) => {
234
+ const jobs = isRecord(found.jobs) ? found.jobs : {};
235
+ const name = String(invocation.name);
236
+ const job = Object.hasOwn(jobs, name) ? jobs[name] : void 0;
237
+ if (!isRecord(job) || !isCallable(job.run)) throw new Error(`This space's functions have no job "${name}"`);
238
+ const slice = isRecord(invocation.job) ? invocation.job : {};
239
+ const handle = {
240
+ id: slice.id,
241
+ name,
242
+ state: slice.state,
243
+ attempt: slice.attempt,
244
+ save: async (state) => {
245
+ await call({
246
+ op: "job",
247
+ method: "save",
248
+ state
249
+ });
250
+ },
251
+ report: (fraction, step) => {
252
+ call({
253
+ op: "job",
254
+ method: "report",
255
+ fraction,
256
+ step
257
+ }).catch(() => void 0);
258
+ },
259
+ get stopping() {
260
+ return stoppingOf();
261
+ },
262
+ signal
263
+ };
264
+ const value = await job.run(isRecord(invocation.input) ? invocation.input : {}, handle, ctx);
265
+ return value === void 0 ? null : value;
266
+ };
267
+ /** One invocation: what a task or a job's slice returned (`null` for nothing), or the wire response a route answered. */
143
268
  const invoke = async (definition, invocation) => {
144
269
  const found = isRecord(definition) ? definition : {};
145
270
  const ctx = contextOf(invocation);
271
+ if (invocation.kind === "job") return runJob(found, invocation, ctx);
146
272
  if (invocation.kind === "task") {
147
273
  const task = listOf(found.tasks).find((entry) => isRecord(entry) && `${String(entry.namespace)}.${String(entry.action)}` === invocation.name);
148
274
  if (!isRecord(task) || !isCallable(task.run)) throw new Error(`This space's functions have no task "${String(invocation.name)}"`);
@@ -0,0 +1,21 @@
1
+ import { ActionKvAdapter } from '../actions/types';
2
+ import { JobQueue, PubSubAdapter } from '@plitzi/sdk-shared';
3
+ export type JobControlOptions = {
4
+ queue: JobQueue;
5
+ /**
6
+ * The page server's realtime — the pub/sub its replicas listen on and the `kv` its retained messages are kept in — so
7
+ * whoever watches a job hears what an operator did to it. Absent, the queue changes and nobody is told until the job
8
+ * moves again.
9
+ */
10
+ realtime?: {
11
+ pubsub: PubSubAdapter;
12
+ kv: ActionKvAdapter;
13
+ };
14
+ };
15
+ export type JobControl = Pick<JobQueue, 'pause' | 'resume' | 'cancel' | 'requeue'>;
16
+ /**
17
+ * An operator's hold on the jobs, from a process that serves no pages — a deployment's API behind its Queue panel: the
18
+ * queue's own `pause`, `resume`, `cancel` and `requeue`, each said on `$job:<id>` when it changed a job of the spaces'
19
+ * functions, as the page server says it when its code does the same.
20
+ */
21
+ export declare const createJobControl: ({ queue, realtime }: JobControlOptions) => JobControl;
@@ -0,0 +1,43 @@
1
+ import { serverLog } from "../../helpers/serverLog.js";
2
+ import { jobViewOf } from "./jobs.js";
3
+ import { createRealtimeHub } from "../realtime/hub.js";
4
+ import { publishJobView } from "../realtime/jobs.js";
5
+ import { createRealtimeRetained } from "../realtime/retained.js";
6
+ //#region src/modules/functions/jobControl.ts
7
+ /**
8
+ * An operator's hold on the jobs, from a process that serves no pages — a deployment's API behind its Queue panel: the
9
+ * queue's own `pause`, `resume`, `cancel` and `requeue`, each said on `$job:<id>` when it changed a job of the spaces'
10
+ * functions, as the page server says it when its code does the same.
11
+ */
12
+ var createJobControl = ({ queue, realtime }) => {
13
+ const said = realtime ? {
14
+ retained: createRealtimeRetained(realtime.kv),
15
+ pubsub: realtime.pubsub
16
+ } : void 0;
17
+ const hub = said ? createRealtimeHub(said.pubsub, said.retained) : void 0;
18
+ const announce = async (spaceIds, jobId) => {
19
+ if (!hub || !said) return;
20
+ try {
21
+ const job = await queue.getJob(spaceIds, jobId);
22
+ if (job?.target.kind === "function") await publishJobView(hub, said.retained, {
23
+ spaceId: job.spaceId,
24
+ environment: job.environment
25
+ }, jobId, jobViewOf(job));
26
+ } catch (error) {
27
+ serverLog.warn("Actions", `job ${jobId}: its watchers were not told what an operator did`, error);
28
+ }
29
+ };
30
+ const told = (change) => async (spaceIds, jobId, ...rest) => {
31
+ const changed = await change(spaceIds, jobId, ...rest);
32
+ if (changed) await announce(spaceIds, jobId);
33
+ return changed;
34
+ };
35
+ return {
36
+ pause: told((spaceIds, jobId) => queue.pause(spaceIds, jobId)),
37
+ resume: told((spaceIds, jobId) => queue.resume(spaceIds, jobId)),
38
+ cancel: told((spaceIds, jobId) => queue.cancel(spaceIds, jobId)),
39
+ requeue: told((spaceIds, jobId, options) => queue.requeue(spaceIds, jobId, options))
40
+ };
41
+ };
42
+ //#endregion
43
+ export { createJobControl };
@@ -0,0 +1,98 @@
1
+ import { FunctionsConfig } from './config';
2
+ import { FunctionJobs, FunctionJobStop, FunctionsDefinition } from './contract';
3
+ import { SpaceFunctions } from './protocol';
4
+ import { TaskContextSource } from '../actions/runtime/runAction';
5
+ import { Job, JobQueue, Environment, FunctionJobManifest, FunctionJobView, RunUser, SpaceRevision } from '@plitzi/sdk-shared';
6
+ /**
7
+ * A space's jobs (`defineFunctions({ jobs })`): work longer than a request, kept in the actions' durable queue and run
8
+ * slice by slice — in the sandbox, in a space's runtime, or natively in a self-hosted server.
9
+ *
10
+ * A slice ends when the code returns (the job is done), throws (an attempt failed: the next starts from the last save),
11
+ * or is asked to stop: paused, cancelled, its time in one go up, the replica going away. `job.save` is the one
12
+ * checkpoint, and the rule that keeps a job from looping forever is that a slice cut by its time goes on only if it saved
13
+ * since it began.
14
+ */
15
+ /** What a server lets a space's jobs spend: a deployment sets it per space (its plan), each unset one the default's. */
16
+ export type FunctionJobLimits = {
17
+ /** Jobs running at once, across `pool`. */
18
+ running: number;
19
+ /** The spaces whose jobs `running` counts together — an account's. Default: the space alone. */
20
+ pool?: number[];
21
+ /** Jobs waiting (`pending` or `paused`) in one space. */
22
+ waiting: number;
23
+ /** How long one job may run, every slice together, in seconds. */
24
+ maxSeconds: number;
25
+ /** How long one slice runs in the sandbox before it is asked to save and go on in the next. */
26
+ sliceMs: number;
27
+ /** CPU one slice in the sandbox may use. */
28
+ sliceCpuMs: number;
29
+ /** Calls to `ctx` one slice in the sandbox may make. */
30
+ sliceCalls: number;
31
+ /** The most a job keeps of what it saves — and of its input — as JSON, in bytes. */
32
+ stateBytes: number;
33
+ };
34
+ export declare const DEFAULT_FUNCTION_JOB_LIMITS: Omit<FunctionJobLimits, 'pool'>;
35
+ /** How long a slice asked to end has to save and return before it is cut. */
36
+ export declare const JOB_GRACE_MS = 5000;
37
+ /** How a slice ended, for the worker to settle the job by. */
38
+ export type JobSliceOutcome = {
39
+ kind: 'done';
40
+ result: unknown;
41
+ spentMs: number;
42
+ } | {
43
+ kind: 'stopped';
44
+ stop: FunctionJobStop;
45
+ saved: boolean;
46
+ spentMs: number;
47
+ }
48
+ /** `final`: no attempt would change it — a refusal, a job no code declares, a job out of its time. */
49
+ | {
50
+ kind: 'failed';
51
+ error: string;
52
+ final: boolean;
53
+ refused: boolean;
54
+ spentMs: number;
55
+ }
56
+ /** Not started: the account runs as many jobs as it may, or may not run code right now. */
57
+ | {
58
+ kind: 'wait';
59
+ delayMs: number;
60
+ why: string;
61
+ };
62
+ /** Who and where a run of a space's code is, as `ctx.jobs` starts a job from it. */
63
+ export type JobRunScope = {
64
+ spaceId: number;
65
+ environment: Environment;
66
+ at?: SpaceRevision;
67
+ user?: RunUser;
68
+ };
69
+ export type FunctionJobsService = {
70
+ /** `ctx.jobs` for one run of a space's code, over the jobs that code declares. */
71
+ forRun: (run: JobRunScope, declared: Readonly<Record<string, FunctionJobManifest>>) => FunctionJobs;
72
+ /** Runs one slice of `job`, `signal` aborting with why (`pause`, `cancel`, `shutdown`) when its worker is told to. */
73
+ slice: (job: Job, signal: AbortSignal, workerId: string) => Promise<JobSliceOutcome>;
74
+ /** Says how the job is now to whoever watches it (`$job:<id>`). Never fails: a watcher missing a beat is not a fault. */
75
+ announce: (spaceId: number, jobId: string) => Promise<void>;
76
+ };
77
+ export type FunctionJobsDeps = {
78
+ /** Read when asked: the queue is made after the runner, and a server may run none. */
79
+ queue: () => JobQueue | undefined;
80
+ config: FunctionsConfig;
81
+ getFunctions?: (spaceId: number, at?: SpaceRevision) => Promise<SpaceFunctions | undefined>;
82
+ /** Read when a slice runs: the runner that makes it is made after this. */
83
+ taskContext: () => TaskContextSource;
84
+ /** Says the job as it is on `$job:<id>` — absent when the server has no realtime channels. */
85
+ publish?: (space: {
86
+ spaceId: number;
87
+ environment: string;
88
+ }, jobId: string, view: FunctionJobView) => Promise<void>;
89
+ maxAttempts: number;
90
+ };
91
+ /** What a job's declaration says besides its code, as a manifest holds it. */
92
+ export declare const jobsManifestOf: (definition: FunctionsDefinition) => Record<string, FunctionJobManifest>;
93
+ /**
94
+ * A job as whoever may watch it sees it: never its input or what it saved, and of a failure only the code's own refusal
95
+ * — anything else it threw can carry what a visitor must not read, and stays in the Queue for an operator.
96
+ */
97
+ export declare const jobViewOf: (job: Job) => FunctionJobView;
98
+ export declare const createFunctionJobs: ({ queue, config, getFunctions, taskContext, publish, maxAttempts }: FunctionJobsDeps) => FunctionJobsService;