nmtjs 0.15.0-beta.2 → 0.15.0-beta.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 (254) hide show
  1. package/dist/cli.d.ts +2 -0
  2. package/dist/cli.js +3 -2
  3. package/dist/cli.js.map +1 -0
  4. package/dist/config.d.ts +51 -0
  5. package/dist/config.js +1 -0
  6. package/dist/config.js.map +1 -0
  7. package/dist/entrypoints/cli.d.ts +1 -0
  8. package/dist/entrypoints/cli.js +1 -0
  9. package/dist/entrypoints/cli.js.map +1 -0
  10. package/dist/entrypoints/main.d.ts +5 -0
  11. package/dist/entrypoints/main.js +83 -15
  12. package/dist/entrypoints/main.js.map +1 -0
  13. package/dist/entrypoints/thread.d.ts +14 -0
  14. package/dist/entrypoints/thread.js +130 -24
  15. package/dist/entrypoints/thread.js.map +1 -0
  16. package/dist/entrypoints/worker.d.ts +3 -0
  17. package/dist/entrypoints/worker.js +4 -3
  18. package/dist/entrypoints/worker.js.map +1 -0
  19. package/dist/index.d.ts +69 -0
  20. package/dist/{_exports/index.js → index.js} +9 -5
  21. package/dist/index.js.map +1 -0
  22. package/dist/resolver.d.ts +2 -0
  23. package/dist/resolver.js +1 -0
  24. package/dist/resolver.js.map +1 -0
  25. package/dist/runtime/application/api/api.d.ts +49 -0
  26. package/dist/runtime/application/api/api.js +193 -0
  27. package/dist/runtime/application/api/api.js.map +1 -0
  28. package/dist/runtime/application/api/constants.d.ts +14 -0
  29. package/dist/runtime/application/api/constants.js +8 -0
  30. package/dist/runtime/application/api/constants.js.map +1 -0
  31. package/dist/runtime/application/api/filters.d.ts +14 -0
  32. package/dist/runtime/application/api/filters.js +11 -0
  33. package/dist/runtime/application/api/filters.js.map +1 -0
  34. package/dist/runtime/application/api/guards.d.ts +13 -0
  35. package/dist/runtime/application/api/guards.js +8 -0
  36. package/dist/runtime/application/api/guards.js.map +1 -0
  37. package/dist/runtime/application/api/index.d.ts +8 -0
  38. package/dist/runtime/application/api/index.js +9 -0
  39. package/dist/runtime/application/api/index.js.map +1 -0
  40. package/dist/runtime/application/api/middlewares.d.ts +14 -0
  41. package/dist/runtime/application/api/middlewares.js +12 -0
  42. package/dist/runtime/application/api/middlewares.js.map +1 -0
  43. package/dist/runtime/application/api/procedure.d.ts +67 -0
  44. package/dist/runtime/application/api/procedure.js +50 -0
  45. package/dist/runtime/application/api/procedure.js.map +1 -0
  46. package/dist/runtime/application/api/router.d.ts +71 -0
  47. package/dist/runtime/application/api/router.js +51 -0
  48. package/dist/runtime/application/api/router.js.map +1 -0
  49. package/dist/runtime/application/api/types.d.ts +32 -0
  50. package/dist/runtime/application/api/types.js +2 -0
  51. package/dist/runtime/application/api/types.js.map +1 -0
  52. package/dist/runtime/application/config.d.ts +26 -0
  53. package/dist/runtime/application/config.js +21 -0
  54. package/dist/runtime/application/config.js.map +1 -0
  55. package/dist/runtime/application/constants.d.ts +2 -0
  56. package/dist/runtime/application/constants.js +2 -0
  57. package/dist/runtime/application/constants.js.map +1 -0
  58. package/dist/runtime/application/hook.d.ts +19 -0
  59. package/dist/runtime/application/hook.js +11 -0
  60. package/dist/runtime/application/hook.js.map +1 -0
  61. package/dist/runtime/application/hooks.d.ts +3 -0
  62. package/dist/runtime/application/hooks.js +4 -0
  63. package/dist/runtime/application/hooks.js.map +1 -0
  64. package/dist/runtime/application/index.d.ts +5 -0
  65. package/dist/runtime/application/index.js +6 -0
  66. package/dist/runtime/application/index.js.map +1 -0
  67. package/dist/runtime/constants.d.ts +8 -0
  68. package/dist/runtime/constants.js +5 -0
  69. package/dist/runtime/constants.js.map +1 -0
  70. package/dist/runtime/core/hooks.d.ts +4 -0
  71. package/dist/runtime/core/hooks.js +4 -0
  72. package/dist/runtime/core/hooks.js.map +1 -0
  73. package/dist/runtime/core/plugin.d.ts +8 -0
  74. package/dist/runtime/core/plugin.js +4 -0
  75. package/dist/runtime/core/plugin.js.map +1 -0
  76. package/dist/runtime/core/runtime.d.ts +27 -0
  77. package/dist/runtime/core/runtime.js +81 -0
  78. package/dist/runtime/core/runtime.js.map +1 -0
  79. package/dist/runtime/enums.d.ts +21 -0
  80. package/dist/runtime/enums.js +26 -0
  81. package/dist/runtime/enums.js.map +1 -0
  82. package/dist/runtime/index.d.ts +21 -0
  83. package/dist/runtime/index.js +22 -0
  84. package/dist/runtime/index.js.map +1 -0
  85. package/dist/runtime/injectables.d.ts +23 -0
  86. package/dist/runtime/injectables.js +20 -0
  87. package/dist/runtime/injectables.js.map +1 -0
  88. package/dist/runtime/jobs/job.d.ts +132 -0
  89. package/dist/runtime/jobs/job.js +68 -0
  90. package/dist/runtime/jobs/job.js.map +1 -0
  91. package/dist/runtime/jobs/manager.d.ts +113 -0
  92. package/dist/runtime/jobs/manager.js +210 -0
  93. package/dist/runtime/jobs/manager.js.map +1 -0
  94. package/dist/runtime/jobs/router.d.ts +266 -0
  95. package/dist/runtime/jobs/router.js +432 -0
  96. package/dist/runtime/jobs/router.js.map +1 -0
  97. package/dist/runtime/jobs/runner.d.ts +64 -0
  98. package/dist/runtime/jobs/runner.js +256 -0
  99. package/dist/runtime/jobs/runner.js.map +1 -0
  100. package/dist/runtime/jobs/step.d.ts +23 -0
  101. package/dist/runtime/jobs/step.js +18 -0
  102. package/dist/runtime/jobs/step.js.map +1 -0
  103. package/dist/runtime/jobs/ui.d.ts +3 -0
  104. package/dist/runtime/jobs/ui.js +17 -0
  105. package/dist/runtime/jobs/ui.js.map +1 -0
  106. package/dist/runtime/pubsub/manager.d.ts +48 -0
  107. package/dist/runtime/pubsub/manager.js +119 -0
  108. package/dist/runtime/pubsub/manager.js.map +1 -0
  109. package/dist/runtime/pubsub/redis.d.ts +16 -0
  110. package/dist/runtime/pubsub/redis.js +98 -0
  111. package/dist/runtime/pubsub/redis.js.map +1 -0
  112. package/dist/runtime/scheduler/index.d.ts +22 -0
  113. package/dist/runtime/scheduler/index.js +20 -0
  114. package/dist/runtime/scheduler/index.js.map +1 -0
  115. package/dist/runtime/server/applications.d.ts +52 -0
  116. package/dist/runtime/server/applications.js +133 -0
  117. package/dist/runtime/server/applications.js.map +1 -0
  118. package/dist/runtime/server/config.d.ts +121 -0
  119. package/dist/runtime/server/config.js +33 -0
  120. package/dist/runtime/server/config.js.map +1 -0
  121. package/dist/runtime/server/jobs.d.ts +41 -0
  122. package/dist/runtime/server/jobs.js +181 -0
  123. package/dist/runtime/server/jobs.js.map +1 -0
  124. package/dist/runtime/server/pool.d.ts +54 -0
  125. package/dist/runtime/server/pool.js +194 -0
  126. package/dist/runtime/server/pool.js.map +1 -0
  127. package/dist/runtime/server/proxy.d.ts +21 -0
  128. package/dist/runtime/server/proxy.js +79 -0
  129. package/dist/runtime/server/proxy.js.map +1 -0
  130. package/dist/runtime/server/server.d.ts +53 -0
  131. package/dist/runtime/server/server.js +90 -0
  132. package/dist/runtime/server/server.js.map +1 -0
  133. package/dist/runtime/store/index.d.ts +3 -0
  134. package/dist/runtime/store/index.js +23 -0
  135. package/dist/runtime/store/index.js.map +1 -0
  136. package/dist/runtime/types.d.ts +103 -0
  137. package/dist/runtime/types.js +2 -0
  138. package/dist/runtime/types.js.map +1 -0
  139. package/dist/runtime/workers/application.d.ts +47 -0
  140. package/dist/runtime/workers/application.js +162 -0
  141. package/dist/runtime/workers/application.js.map +1 -0
  142. package/dist/runtime/workers/base.d.ts +16 -0
  143. package/dist/runtime/workers/base.js +46 -0
  144. package/dist/runtime/workers/base.js.map +1 -0
  145. package/dist/runtime/workers/cli.d.ts +1 -0
  146. package/dist/runtime/workers/cli.js +2 -0
  147. package/dist/runtime/workers/cli.js.map +1 -0
  148. package/dist/runtime/workers/job.d.ts +20 -0
  149. package/dist/runtime/workers/job.js +172 -0
  150. package/dist/runtime/workers/job.js.map +1 -0
  151. package/dist/typings.d.ts +5 -0
  152. package/dist/typings.js +4 -3
  153. package/dist/typings.js.map +1 -0
  154. package/dist/vite/builder.d.ts +5 -0
  155. package/dist/vite/builder.js +5 -1
  156. package/dist/vite/builder.js.map +1 -0
  157. package/dist/vite/config.d.ts +28 -0
  158. package/dist/vite/config.js +1 -0
  159. package/dist/vite/config.js.map +1 -0
  160. package/dist/vite/plugins.d.ts +2 -0
  161. package/dist/vite/plugins.js +1 -0
  162. package/dist/vite/plugins.js.map +1 -0
  163. package/dist/vite/runners/worker.d.ts +4 -0
  164. package/dist/vite/runners/worker.js +1 -0
  165. package/dist/vite/runners/worker.js.map +1 -0
  166. package/dist/vite/server.d.ts +3 -0
  167. package/dist/vite/server.js +6 -1
  168. package/dist/vite/server.js.map +1 -0
  169. package/dist/vite/servers/main.d.ts +8 -0
  170. package/dist/vite/servers/main.js +1 -0
  171. package/dist/vite/servers/main.js.map +1 -0
  172. package/dist/vite/servers/worker.d.ts +11 -0
  173. package/dist/vite/servers/worker.js +28 -0
  174. package/dist/vite/servers/worker.js.map +1 -0
  175. package/package.json +31 -18
  176. package/src/cli.ts +144 -0
  177. package/src/config.ts +64 -0
  178. package/src/entrypoints/cli.ts +13 -0
  179. package/src/entrypoints/main.ts +200 -0
  180. package/src/entrypoints/thread.ts +184 -0
  181. package/src/entrypoints/worker.ts +48 -0
  182. package/src/index.ts +82 -0
  183. package/src/resolver.ts +16 -0
  184. package/src/runtime/application/api/api.ts +265 -0
  185. package/src/runtime/application/api/constants.ts +22 -0
  186. package/src/runtime/application/api/filters.ts +39 -0
  187. package/src/runtime/application/api/guards.ts +29 -0
  188. package/src/runtime/application/api/index.ts +8 -0
  189. package/src/runtime/application/api/middlewares.ts +37 -0
  190. package/src/runtime/application/api/procedure.ts +229 -0
  191. package/src/runtime/application/api/router.ts +193 -0
  192. package/src/runtime/application/api/types.ts +124 -0
  193. package/src/runtime/application/config.ts +69 -0
  194. package/src/runtime/application/constants.ts +4 -0
  195. package/src/runtime/application/hook.ts +51 -0
  196. package/src/runtime/application/hooks.ts +3 -0
  197. package/src/runtime/application/index.ts +5 -0
  198. package/src/runtime/constants.ts +13 -0
  199. package/src/runtime/core/hooks.ts +5 -0
  200. package/src/runtime/core/plugin.ts +13 -0
  201. package/src/runtime/core/runtime.ts +109 -0
  202. package/src/runtime/enums.ts +24 -0
  203. package/src/runtime/index.ts +21 -0
  204. package/src/runtime/injectables.ts +61 -0
  205. package/src/runtime/jobs/job.ts +370 -0
  206. package/src/runtime/jobs/manager.ts +348 -0
  207. package/src/runtime/jobs/router.ts +896 -0
  208. package/src/runtime/jobs/runner.ts +320 -0
  209. package/src/runtime/jobs/step.ts +66 -0
  210. package/src/runtime/jobs/ui.ts +21 -0
  211. package/src/runtime/pubsub/manager.ts +211 -0
  212. package/src/runtime/pubsub/redis.ts +108 -0
  213. package/src/runtime/scheduler/index.ts +39 -0
  214. package/src/runtime/server/applications.ts +210 -0
  215. package/src/runtime/server/config.ts +158 -0
  216. package/src/runtime/server/jobs.ts +250 -0
  217. package/src/runtime/server/pool.ts +260 -0
  218. package/src/runtime/server/proxy.ts +118 -0
  219. package/src/runtime/server/server.ts +155 -0
  220. package/src/runtime/store/index.ts +30 -0
  221. package/src/runtime/types.ts +93 -0
  222. package/src/runtime/workers/application.ts +209 -0
  223. package/src/runtime/workers/base.ts +68 -0
  224. package/src/runtime/workers/cli.ts +0 -0
  225. package/src/runtime/workers/job.ts +153 -0
  226. package/src/typings.ts +30 -0
  227. package/src/vite/builder.ts +122 -0
  228. package/src/vite/config.ts +45 -0
  229. package/src/vite/plugins.ts +26 -0
  230. package/src/vite/runners/worker.ts +57 -0
  231. package/src/vite/server.ts +39 -0
  232. package/src/vite/servers/main.ts +34 -0
  233. package/src/vite/servers/worker.ts +143 -0
  234. package/dist/_exports/application.js +0 -1
  235. package/dist/_exports/common.js +0 -1
  236. package/dist/_exports/contract.js +0 -2
  237. package/dist/_exports/core.js +0 -1
  238. package/dist/_exports/gateway.js +0 -1
  239. package/dist/_exports/http-transport/bun.js +0 -1
  240. package/dist/_exports/http-transport/deno.js +0 -1
  241. package/dist/_exports/http-transport/node.js +0 -1
  242. package/dist/_exports/http-transport.js +0 -1
  243. package/dist/_exports/json-format.js +0 -1
  244. package/dist/_exports/protocol/client.js +0 -1
  245. package/dist/_exports/protocol/server.js +0 -1
  246. package/dist/_exports/protocol.js +0 -1
  247. package/dist/_exports/runtime/types.js +0 -1
  248. package/dist/_exports/runtime.js +0 -1
  249. package/dist/_exports/type.js +0 -2
  250. package/dist/_exports/ws-transport/bun.js +0 -1
  251. package/dist/_exports/ws-transport/deno.js +0 -1
  252. package/dist/_exports/ws-transport/node.js +0 -1
  253. package/dist/_exports/ws-transport.js +0 -1
  254. package/dist/command.js +0 -30
@@ -0,0 +1,13 @@
1
+ import type { Injection } from '@nmtjs/core'
2
+
3
+ import type { LifecycleHooks } from './hooks.ts'
4
+
5
+ export interface RuntimePlugin {
6
+ name: string
7
+ hooks?: LifecycleHooks['_']['config']
8
+ injections?: Injection[]
9
+ }
10
+
11
+ export function createPlugin<T extends RuntimePlugin>(plugin: T): T {
12
+ return plugin
13
+ }
@@ -0,0 +1,109 @@
1
+ import type {
2
+ AnyInjectable,
3
+ Dependant,
4
+ Logger,
5
+ LoggingOptions,
6
+ } from '@nmtjs/core'
7
+ import {
8
+ Container,
9
+ createLogger,
10
+ getDepedencencyInjectable,
11
+ Scope,
12
+ } from '@nmtjs/core'
13
+
14
+ import type { RuntimePlugin } from './plugin.ts'
15
+ import { LifecycleHook } from '../enums.ts'
16
+ import { LifecycleHooks } from './hooks.ts'
17
+
18
+ export type BaseRuntimeOptions = {
19
+ logger?: LoggingOptions
20
+ container?: Container
21
+ plugins?: RuntimePlugin[]
22
+ name?: string
23
+ }
24
+
25
+ export abstract class BaseRuntime {
26
+ logger: Logger
27
+ container: Container
28
+ lifecycleHooks: LifecycleHooks
29
+ plugins: RuntimePlugin[]
30
+
31
+ constructor(options: BaseRuntimeOptions = {}) {
32
+ this.logger = createLogger(options.logger, options.name || 'Runtime')
33
+ this.container = options.container
34
+ ? options.container.fork(Scope.Global)
35
+ : new Container({ logger: this.logger })
36
+ this.lifecycleHooks = new LifecycleHooks()
37
+ this.plugins = options.plugins || []
38
+ }
39
+
40
+ protected abstract _initialize(): Promise<void>
41
+ protected abstract _dispose(): Promise<void>
42
+ protected abstract _dependents(): Generator<Dependant>
43
+
44
+ async reload(...args: any[]): Promise<void> {
45
+ await this._dispose()
46
+ await this._initialize()
47
+ }
48
+
49
+ async initialize() {
50
+ this.logger.debug('Initializing a runtime...')
51
+ await this._initializePlugins()
52
+ await this._initializeContainer()
53
+ await this.lifecycleHooks.callHook(LifecycleHook.BeforeInitialize, this)
54
+ await this._initialize()
55
+ await this.lifecycleHooks.callHook(LifecycleHook.AfterInitialize, this)
56
+ this.logger.debug('Runtime initialized')
57
+ }
58
+
59
+ async dispose() {
60
+ this.logger.debug('Disposing a runtime...')
61
+ await this.lifecycleHooks.callHook(LifecycleHook.BeforeDispose, this)
62
+ await this._dispose()
63
+ await this.lifecycleHooks.callHook(LifecycleHook.AfterDispose, this)
64
+ await this._disposeContainer()
65
+ await this._disposePlugins()
66
+ this.logger.debug('Runtime disposed')
67
+ }
68
+
69
+ protected async _initializePlugins() {
70
+ if (!this.plugins?.length) return
71
+ for (const { name, hooks, injections } of this.plugins) {
72
+ this.logger.debug(`Initializing plugin [${name}]...`)
73
+ if (injections) {
74
+ for (const injection of injections) {
75
+ await this.container.provide(injection.token, injection.value)
76
+ }
77
+ }
78
+ if (hooks) this.lifecycleHooks.addHooks(hooks)
79
+ }
80
+ }
81
+
82
+ protected async _disposePlugins() {
83
+ if (!this.plugins?.length) return
84
+ for (const { name, hooks, injections } of this.plugins) {
85
+ this.logger.debug(`Disposing plugin [${name}]...`)
86
+ if (hooks) this.lifecycleHooks.removeHooks(hooks)
87
+ if (injections) {
88
+ for (const injection of injections) {
89
+ await this.container.disposeInjectableInstances(injection.token)
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+ protected async _initializeContainer() {
96
+ const dependents = this._dependents()
97
+ const dependencies = new Set<AnyInjectable>()
98
+ for (const dependant of dependents) {
99
+ for (const dependency of Object.values(dependant.dependencies)) {
100
+ dependencies.add(getDepedencencyInjectable(dependency))
101
+ }
102
+ }
103
+ await this.container.initialize(dependencies)
104
+ }
105
+
106
+ protected async _disposeContainer() {
107
+ await this.container.dispose()
108
+ }
109
+ }
@@ -0,0 +1,24 @@
1
+ export enum StoreType {
2
+ Redis = 'Redis',
3
+ Valkey = 'Valkey',
4
+ }
5
+
6
+ export enum JobWorkerPool {
7
+ Io = 'Io',
8
+ Compute = 'Compute',
9
+ }
10
+
11
+ export enum LifecycleHook {
12
+ BeforeInitialize = 'lifecycle:beforeInitialize',
13
+ AfterInitialize = 'lifecycle:afterInitialize',
14
+ BeforeDispose = 'lifecycle:beforeDispose',
15
+ AfterDispose = 'lifecycle:afterDispose',
16
+ Stop = 'lifecycle:stop',
17
+ Start = 'lifecycle:start',
18
+ }
19
+
20
+ export enum WorkerType {
21
+ Application = 'Application',
22
+ Job = 'Job',
23
+ Command = 'Command',
24
+ }
@@ -0,0 +1,21 @@
1
+ export * from './application/index.ts'
2
+ export * from './constants.ts'
3
+ export * from './core/hooks.ts'
4
+ export * from './core/plugin.ts'
5
+ export * from './core/runtime.ts'
6
+ export * from './enums.ts'
7
+ export * from './injectables.ts'
8
+ export * from './jobs/job.ts'
9
+ export * from './jobs/manager.ts'
10
+ export * from './jobs/router.ts'
11
+ export * from './jobs/step.ts'
12
+ export * from './pubsub/manager.ts'
13
+ export * from './pubsub/redis.ts'
14
+ export * from './scheduler/index.ts'
15
+ export * from './server/config.ts'
16
+ export * from './server/server.ts'
17
+ export * from './store/index.ts'
18
+ export * from './types.ts'
19
+ export * from './workers/application.ts'
20
+ export * from './workers/base.ts'
21
+ export * from './workers/job.ts'
@@ -0,0 +1,61 @@
1
+ import { createLazyInjectable, Scope } from '@nmtjs/core'
2
+
3
+ import type { JobWorkerPool, WorkerType } from './enums.ts'
4
+ import type { JobManagerInstance } from './jobs/manager.ts'
5
+ import type {
6
+ PubSubAdapterType,
7
+ PubSubPublish,
8
+ PubSubSubscribe,
9
+ } from './pubsub/manager.ts'
10
+ import type { ServerStoreConfig } from './server/config.ts'
11
+
12
+ export const pubSubAdapter = createLazyInjectable<PubSubAdapterType>(
13
+ Scope.Global,
14
+ 'PubSubAdapter',
15
+ )
16
+
17
+ export const pubSubPublish = createLazyInjectable<PubSubPublish>(
18
+ Scope.Global,
19
+ 'PubSubPublish',
20
+ )
21
+
22
+ export const pubSubSubscribe = createLazyInjectable<PubSubSubscribe>(
23
+ Scope.Global,
24
+ 'PubSubSubscribe',
25
+ )
26
+
27
+ export const jobManager = createLazyInjectable<JobManagerInstance>(
28
+ Scope.Global,
29
+ 'JobManager',
30
+ )
31
+
32
+ export const storeConfig = createLazyInjectable<ServerStoreConfig>(
33
+ Scope.Global,
34
+ 'StoreConfig',
35
+ )
36
+
37
+ export const workerType = createLazyInjectable<WorkerType>(
38
+ Scope.Global,
39
+ 'WorkerType',
40
+ )
41
+
42
+ export const jobWorkerPool = createLazyInjectable<JobWorkerPool>(
43
+ Scope.Global,
44
+ 'JobWorkerPool',
45
+ )
46
+
47
+ export const jobAbortSignal = createLazyInjectable<AbortSignal>(
48
+ Scope.Global,
49
+ 'JobAbortSignal',
50
+ )
51
+
52
+ export const RuntimeInjectables = {
53
+ pubSubAdapter,
54
+ pubSubPublish,
55
+ pubSubSubscribe,
56
+ jobManager,
57
+ storeConfig,
58
+ workerType,
59
+ jobWorkerPool,
60
+ jobAbortSignal,
61
+ }
@@ -0,0 +1,370 @@
1
+ import type { MaybePromise, TSError } from '@nmtjs/common'
2
+ import type { Dependencies, DependencyContext } from '@nmtjs/core'
3
+ import type { t } from '@nmtjs/type'
4
+ import type { AnyObjectLikeType, ObjectType } from '@nmtjs/type/object'
5
+ import { tryCaptureStackTrace } from '@nmtjs/common'
6
+
7
+ import type { JobWorkerPool } from '../enums.ts'
8
+ import type { AnyJobStep, JobStep } from './step.ts'
9
+ import { kJobKey } from '../constants.ts'
10
+ import { isJobStep } from './step.ts'
11
+
12
+ type DefaultObjectType = ObjectType<{}>
13
+
14
+ type DefaultResultType = Record<string, any>
15
+
16
+ export type AnyJobOptions = JobOptions<string, any, any, any, any, any>
17
+
18
+ export interface AnyJob {
19
+ _: { data: any; result: any; input: any; output: any; progress: any }
20
+ [kJobKey]: true
21
+ options: AnyJobOptions
22
+ steps: readonly AnyJobStep[]
23
+ conditions: Map<number, JobCondition<any, any, any, any>>
24
+ name: string
25
+ dependencies: Dependencies
26
+ input: AnyObjectLikeType
27
+ output: AnyObjectLikeType
28
+ progress: AnyObjectLikeType
29
+ afterEachHandler?: JobAfterEachHandler<any, any, any, any, any>
30
+ beforeEachHandler?: JobBeforeEachHandler<any, any, any, any, any>
31
+ onErrorHandler?: JobOnErrorHandler<any, any, any, any, any>
32
+ returnHandler?: JobReturnHandler<any, any, any, any, any, any>
33
+ }
34
+
35
+ export type JobBackoffOptions = {
36
+ type: 'fixed' | 'exponential'
37
+ delay: number
38
+ jitter?: number
39
+ }
40
+
41
+ export type JobCondition<
42
+ Deps extends Dependencies = {},
43
+ Result extends DefaultResultType = {},
44
+ Data = any,
45
+ Input extends AnyObjectLikeType = AnyObjectLikeType,
46
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
47
+ > = (params: {
48
+ context: DependencyContext<Deps>
49
+ data: Data
50
+ input: t.infer.decode.output<Input>
51
+ result: Result
52
+ progress: t.infer.decode.output<Progress>
53
+ }) => MaybePromise<boolean>
54
+
55
+ export type JobReturnHandler<
56
+ Deps extends Dependencies,
57
+ Result extends DefaultResultType,
58
+ Input extends AnyObjectLikeType,
59
+ Output extends AnyObjectLikeType,
60
+ Data,
61
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
62
+ > = (params: {
63
+ context: DependencyContext<Deps>
64
+ data: Data
65
+ input: t.infer.decode.output<Input>
66
+ result: Result
67
+ progress: t.infer.decode.output<Progress>
68
+ }) => MaybePromise<t.infer.encode.input<Output>>
69
+
70
+ export type JobDataHandler<
71
+ Deps extends Dependencies,
72
+ Input extends AnyObjectLikeType,
73
+ Data,
74
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
75
+ > = (
76
+ ctx: DependencyContext<Deps>,
77
+ input: t.infer.decode.output<Input>,
78
+ progress: t.infer.decode.output<Progress>,
79
+ ) => MaybePromise<Data>
80
+
81
+ export type JobAfterEachHandler<
82
+ Deps extends Dependencies,
83
+ Result extends DefaultResultType,
84
+ Input extends AnyObjectLikeType,
85
+ Data,
86
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
87
+ > = (params: {
88
+ context: DependencyContext<Deps>
89
+ data: Data
90
+ input: t.infer.decode.output<Input>
91
+ result: Result
92
+ progress: t.infer.decode.output<Progress>
93
+ step: AnyJobStep
94
+ stepIndex: number
95
+ }) => MaybePromise<void>
96
+
97
+ export type JobBeforeEachHandler<
98
+ Deps extends Dependencies,
99
+ Result extends DefaultResultType,
100
+ Input extends AnyObjectLikeType,
101
+ Data,
102
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
103
+ > = (params: {
104
+ context: DependencyContext<Deps>
105
+ data: Data
106
+ input: t.infer.decode.output<Input>
107
+ result: Result
108
+ progress: t.infer.decode.output<Progress>
109
+ step: AnyJobStep
110
+ stepIndex: number
111
+ }) => MaybePromise<void>
112
+
113
+ export type JobOnErrorHandler<
114
+ Deps extends Dependencies,
115
+ Result extends DefaultResultType,
116
+ Input extends AnyObjectLikeType,
117
+ Data,
118
+ Progress extends AnyObjectLikeType = AnyObjectLikeType,
119
+ > = (params: {
120
+ context: DependencyContext<Deps>
121
+ data: Data
122
+ input: t.infer.decode.output<Input>
123
+ result: Result
124
+ progress: t.infer.decode.output<Progress>
125
+ step: AnyJobStep
126
+ stepIndex: number
127
+ error: unknown
128
+ }) => MaybePromise<
129
+ | boolean
130
+ // biome-ignore lint/suspicious/noConfusingVoidType: its ok
131
+ | void
132
+ >
133
+
134
+ export interface JobOptions<
135
+ Name extends string = string,
136
+ Input extends AnyObjectLikeType = AnyObjectLikeType,
137
+ Output extends AnyObjectLikeType = AnyObjectLikeType,
138
+ Progress extends AnyObjectLikeType = DefaultObjectType,
139
+ Deps extends Dependencies = {},
140
+ Data = any,
141
+ > {
142
+ name: Name
143
+ pool: JobWorkerPool
144
+ input: Input
145
+ output: Output
146
+ progress?: Progress
147
+ concurrency?: number
148
+ timeout?: number
149
+ dependencies?: Deps
150
+ data?: JobDataHandler<Deps, Input, Data, Progress>
151
+ attempts?: number
152
+ backoff?: JobBackoffOptions
153
+ oneoff?: boolean
154
+ }
155
+
156
+ export class Job<
157
+ in out Name extends string = string,
158
+ in out Deps extends Dependencies = {},
159
+ in out Data = any,
160
+ in out Input extends AnyObjectLikeType = DefaultObjectType,
161
+ in out Output extends AnyObjectLikeType = DefaultObjectType,
162
+ in out Progress extends AnyObjectLikeType = DefaultObjectType,
163
+ out Steps extends [...AnyJobStep[]] = [],
164
+ in out Result extends DefaultResultType = {},
165
+ out Return extends boolean = false,
166
+ > implements AnyJob
167
+ {
168
+ _!: {
169
+ data: Data
170
+ result: Result & t.infer.decode.output<Input>
171
+ input: t.infer.decode.output<Input>
172
+ output: t.infer.decode.output<Output>
173
+ progress: t.infer.decode.output<Progress>
174
+ };
175
+ [kJobKey] = true as const
176
+ steps: Steps = [] as unknown as Steps
177
+ conditions: Map<number, JobCondition<any, any, any, any, any>> = new Map()
178
+ returnHandler?: JobReturnHandler<
179
+ Deps,
180
+ this['_']['result'],
181
+ Input,
182
+ Output,
183
+ Data,
184
+ Progress
185
+ >
186
+ afterEachHandler?: JobAfterEachHandler<
187
+ Deps,
188
+ this['_']['result'],
189
+ Input,
190
+ Data,
191
+ Progress
192
+ >
193
+ beforeEachHandler?: JobBeforeEachHandler<
194
+ Deps,
195
+ this['_']['result'],
196
+ Input,
197
+ Data,
198
+ Progress
199
+ >
200
+ onErrorHandler?: JobOnErrorHandler<
201
+ Deps,
202
+ this['_']['result'],
203
+ Input,
204
+ Data,
205
+ Progress
206
+ >
207
+
208
+ constructor(
209
+ public options: JobOptions<Name, Input, Output, Progress, Deps, Data>,
210
+ public stack?: string,
211
+ ) {}
212
+
213
+ get name(): Name {
214
+ return this.options.name
215
+ }
216
+
217
+ get dependencies(): Deps {
218
+ return this.options.dependencies ?? ({} as Deps)
219
+ }
220
+
221
+ get input(): Input {
222
+ return this.options.input as Input
223
+ }
224
+
225
+ get output(): Output {
226
+ return this.options.output as Output
227
+ }
228
+
229
+ get progress(): Progress {
230
+ return this.options.progress as Progress
231
+ }
232
+
233
+ step<
234
+ StepInput extends AnyObjectLikeType,
235
+ StepOutput extends AnyObjectLikeType,
236
+ StepDeps extends Dependencies,
237
+ StepResult,
238
+ Condition extends
239
+ | JobCondition<Deps, Result, this['_']['data'], Input>
240
+ | undefined,
241
+ >(
242
+ ...[step, condition]: Return extends false
243
+ ? [
244
+ step: JobStep<
245
+ StepInput,
246
+ StepOutput,
247
+ StepDeps,
248
+ this['_']['result'] extends t.infer.decode.output<StepInput>
249
+ ? t.infer.decode.output<StepOutput>
250
+ : TSError<
251
+ 'Accumulated job result does not satisfy current step input:',
252
+ this['_']['result']
253
+ >,
254
+ this['_']['data']
255
+ >,
256
+ condition?: Condition,
257
+ ]
258
+ : [TSError<'Job has already has return statement'>]
259
+ ) {
260
+ if (!isJobStep(step)) throw new Error('Invalid job step object')
261
+
262
+ const length = this.steps.push(step)
263
+ if (condition) this.conditions.set(length - 1, condition)
264
+
265
+ return this as unknown as Job<
266
+ Name,
267
+ Deps,
268
+ Data,
269
+ Input,
270
+ Output,
271
+ Progress,
272
+ [...Steps, JobStep<StepInput, StepOutput, StepDeps, StepResult, any>],
273
+ Result &
274
+ (undefined extends Condition
275
+ ? t.infer.decode.output<
276
+ StepOutput extends undefined ? DefaultObjectType : StepOutput
277
+ >
278
+ : Partial<
279
+ t.infer.decode.output<
280
+ StepOutput extends undefined ? DefaultObjectType : StepOutput
281
+ >
282
+ >)
283
+ >
284
+ }
285
+
286
+ return(
287
+ ...[handler]: Return extends false
288
+ ? Result extends t.infer.encode.input<Output>
289
+ ? [
290
+ JobReturnHandler<
291
+ Deps,
292
+ this['_']['result'],
293
+ Input,
294
+ Output,
295
+ Data,
296
+ Progress
297
+ >?,
298
+ ]
299
+ : [
300
+ JobReturnHandler<
301
+ Deps,
302
+ this['_']['result'],
303
+ Input,
304
+ Output,
305
+ Data,
306
+ Progress
307
+ >,
308
+ ]
309
+ : [TSError<'Job already has a return statement'>]
310
+ ) {
311
+ if (typeof handler === 'function') {
312
+ this.returnHandler = handler
313
+ } else {
314
+ this.returnHandler = (result) => result as any
315
+ }
316
+ return this as unknown as Job<
317
+ Name,
318
+ Deps,
319
+ Data,
320
+ Input,
321
+ Output,
322
+ Progress,
323
+ Steps,
324
+ Result,
325
+ true
326
+ >
327
+ }
328
+
329
+ afterEach(
330
+ handler: Return extends true
331
+ ? JobAfterEachHandler<Deps, this['_']['result'], Input, Data, Progress>
332
+ : TSError<'Job must have a return statement to use afterEach'>,
333
+ ) {
334
+ this.afterEachHandler = handler as any
335
+ return this
336
+ }
337
+
338
+ beforeEach(
339
+ handler: Return extends true
340
+ ? JobBeforeEachHandler<Deps, this['_']['result'], Input, Data, Progress>
341
+ : TSError<'Job must have a return statement to use beforeEach'>,
342
+ ) {
343
+ this.beforeEachHandler = handler as any
344
+ return this
345
+ }
346
+
347
+ onError(
348
+ handler: Return extends true
349
+ ? JobOnErrorHandler<Deps, this['_']['result'], Input, Data, Progress>
350
+ : TSError<'Job must have a return statement to use onError'>,
351
+ ) {
352
+ this.onErrorHandler = handler as any
353
+ return this
354
+ }
355
+ }
356
+
357
+ export function createJob<
358
+ Name extends string,
359
+ Input extends AnyObjectLikeType,
360
+ Output extends AnyObjectLikeType,
361
+ Deps extends Dependencies = {},
362
+ Data = any,
363
+ Progress extends AnyObjectLikeType = DefaultObjectType,
364
+ >(options: JobOptions<Name, Input, Output, Progress, Deps, Data>) {
365
+ const stack = tryCaptureStackTrace()
366
+ return new Job<Name, Deps, Data, Input, Output, Progress>(
367
+ { dependencies: {} as Deps, ...options },
368
+ stack,
369
+ )
370
+ }