@geekmidas/constructs 10.0.0-alpha.4 → 10.0.0-alpha.6

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 (110) hide show
  1. package/dist/{Cron-B2OFsfDM.d.cts → Cron-DmQ8vQ4b.d.cts} +10 -2
  2. package/dist/Cron-DmQ8vQ4b.d.cts.map +1 -0
  3. package/dist/{Cron-ByxdOfpE.d.mts → Cron-dh4rUgib.d.mts} +10 -2
  4. package/dist/Cron-dh4rUgib.d.mts.map +1 -0
  5. package/dist/{CronBuilder-LSJMfqnq.d.cts → CronBuilder-BquXDbDI.d.cts} +3 -3
  6. package/dist/{CronBuilder-BL0AP6fs.d.mts.map → CronBuilder-BquXDbDI.d.cts.map} +1 -1
  7. package/dist/{CronBuilder-BL0AP6fs.d.mts → CronBuilder-CGgY2080.d.mts} +3 -3
  8. package/dist/{CronBuilder-LSJMfqnq.d.cts.map → CronBuilder-CGgY2080.d.mts.map} +1 -1
  9. package/dist/{CronBuilder-CdibiHNB.mjs → CronBuilder-CYKT5RiO.mjs} +11 -2
  10. package/dist/CronBuilder-CYKT5RiO.mjs.map +1 -0
  11. package/dist/{CronBuilder-DyUr6nYz.cjs → CronBuilder-Da0C0edR.cjs} +11 -2
  12. package/dist/CronBuilder-Da0C0edR.cjs.map +1 -0
  13. package/dist/{Endpoint-2Wg7T7Cq.d.mts → Endpoint-BjWKinFA.d.mts} +2 -2
  14. package/dist/{Endpoint-2Wg7T7Cq.d.mts.map → Endpoint-BjWKinFA.d.mts.map} +1 -1
  15. package/dist/{Endpoint-Dij_wOcc.mjs → Endpoint-DCQPMdUX.mjs} +2 -2
  16. package/dist/{Endpoint-Dij_wOcc.mjs.map → Endpoint-DCQPMdUX.mjs.map} +1 -1
  17. package/dist/{Endpoint-DODn1RhN.cjs → Endpoint-DcWXE-SZ.cjs} +2 -2
  18. package/dist/{Endpoint-DODn1RhN.cjs.map → Endpoint-DcWXE-SZ.cjs.map} +1 -1
  19. package/dist/{Endpoint-DrD8a1yH.d.cts → Endpoint-dav89W6-.d.cts} +2 -2
  20. package/dist/Endpoint-dav89W6-.d.cts.map +1 -0
  21. package/dist/{EndpointFactory-BHFMmFkV.cjs → EndpointFactory-CdWB7wPw.cjs} +3 -3
  22. package/dist/{EndpointFactory-BHFMmFkV.cjs.map → EndpointFactory-CdWB7wPw.cjs.map} +1 -1
  23. package/dist/{EndpointFactory-b4ituq-s.mjs → EndpointFactory-CjV8F_lJ.mjs} +3 -3
  24. package/dist/{EndpointFactory-b4ituq-s.mjs.map → EndpointFactory-CjV8F_lJ.mjs.map} +1 -1
  25. package/dist/{HonoEndpointAdaptor-De6IlBZe.cjs → HonoEndpointAdaptor-DtL9j19R.cjs} +2 -2
  26. package/dist/{HonoEndpointAdaptor-De6IlBZe.cjs.map → HonoEndpointAdaptor-DtL9j19R.cjs.map} +1 -1
  27. package/dist/{HonoEndpointAdaptor-BM2jbWht.mjs → HonoEndpointAdaptor-P1ahhZHu.mjs} +2 -2
  28. package/dist/{HonoEndpointAdaptor-BM2jbWht.mjs.map → HonoEndpointAdaptor-P1ahhZHu.mjs.map} +1 -1
  29. package/dist/adaptors/aws.cjs +2 -2
  30. package/dist/adaptors/aws.d.cts +3 -3
  31. package/dist/adaptors/aws.d.mts +3 -3
  32. package/dist/adaptors/aws.mjs +2 -2
  33. package/dist/adaptors/hono.cjs +3 -3
  34. package/dist/adaptors/hono.d.cts +4 -4
  35. package/dist/adaptors/hono.d.mts +4 -4
  36. package/dist/adaptors/hono.mjs +3 -3
  37. package/dist/adaptors/testing.cjs +3 -3
  38. package/dist/adaptors/testing.d.cts +2 -2
  39. package/dist/adaptors/testing.d.mts +2 -2
  40. package/dist/adaptors/testing.mjs +3 -3
  41. package/dist/crons/index.cjs +66 -5
  42. package/dist/crons/index.cjs.map +1 -1
  43. package/dist/crons/index.d.cts +34 -10
  44. package/dist/crons/index.d.cts.map +1 -1
  45. package/dist/crons/index.d.mts +34 -10
  46. package/dist/crons/index.d.mts.map +1 -1
  47. package/dist/crons/index.mjs +65 -5
  48. package/dist/crons/index.mjs.map +1 -1
  49. package/dist/endpoints/index.cjs +3 -3
  50. package/dist/endpoints/index.d.cts +3 -3
  51. package/dist/endpoints/index.d.mts +3 -3
  52. package/dist/endpoints/index.mjs +3 -3
  53. package/dist/functions/index.cjs +2 -3
  54. package/dist/functions/index.d.cts +2 -2
  55. package/dist/functions/index.d.mts +2 -2
  56. package/dist/functions/index.mjs +2 -2
  57. package/dist/{functions-Ck21Uyq0.mjs → functions-14lpEFL4.mjs} +10 -6
  58. package/dist/functions-14lpEFL4.mjs.map +1 -0
  59. package/dist/{functions-DJJA7VGc.cjs → functions-i_la-N5r.cjs} +9 -11
  60. package/dist/functions-i_la-N5r.cjs.map +1 -0
  61. package/dist/{index-B8g8POkr.d.cts → index-Bd5FqCoe.d.cts} +3 -3
  62. package/dist/{index-BxHDdy6a.d.mts.map → index-Bd5FqCoe.d.cts.map} +1 -1
  63. package/dist/index-BknhM6Ux.d.mts +31 -0
  64. package/dist/index-BknhM6Ux.d.mts.map +1 -0
  65. package/dist/index-CBSfrowv.d.cts +31 -0
  66. package/dist/index-CBSfrowv.d.cts.map +1 -0
  67. package/dist/{index-AIfVzkhi.d.cts → index-D24Xm79M.d.cts} +10 -9
  68. package/dist/index-D24Xm79M.d.cts.map +1 -0
  69. package/dist/{index-CubPzHd9.d.mts → index-_ajboYxK.d.mts} +10 -9
  70. package/dist/index-_ajboYxK.d.mts.map +1 -0
  71. package/dist/{index-BxHDdy6a.d.mts → index-lAO9egTb.d.mts} +3 -3
  72. package/dist/{index-B8g8POkr.d.cts.map → index-lAO9egTb.d.mts.map} +1 -1
  73. package/dist/queue/index.d.cts +2 -2
  74. package/dist/queue/index.d.cts.map +1 -1
  75. package/dist/queue/index.d.mts +2 -2
  76. package/dist/rest-api.cjs +3 -3
  77. package/dist/rest-api.d.cts +3 -3
  78. package/dist/rest-api.d.mts +9 -9
  79. package/dist/rest-api.d.mts.map +1 -1
  80. package/dist/rest-api.mjs +3 -3
  81. package/dist/subscribers/index.cjs +1 -7
  82. package/dist/subscribers/index.d.cts +1 -9
  83. package/dist/subscribers/index.d.mts +1 -9
  84. package/dist/subscribers/index.mjs +1 -6
  85. package/dist/topic/index.d.cts +2 -30
  86. package/dist/topic/index.d.mts +2 -30
  87. package/dist/worker.cjs +98 -23
  88. package/dist/worker.cjs.map +1 -1
  89. package/dist/worker.d.cts +83 -32
  90. package/dist/worker.d.cts.map +1 -1
  91. package/dist/worker.d.mts +82 -32
  92. package/dist/worker.d.mts.map +1 -1
  93. package/dist/worker.mjs +98 -23
  94. package/dist/worker.mjs.map +1 -1
  95. package/package.json +30 -30
  96. package/dist/Cron-B2OFsfDM.d.cts.map +0 -1
  97. package/dist/Cron-ByxdOfpE.d.mts.map +0 -1
  98. package/dist/CronBuilder-CdibiHNB.mjs.map +0 -1
  99. package/dist/CronBuilder-DyUr6nYz.cjs.map +0 -1
  100. package/dist/Endpoint-DrD8a1yH.d.cts.map +0 -1
  101. package/dist/functions-Ck21Uyq0.mjs.map +0 -1
  102. package/dist/functions-DJJA7VGc.cjs.map +0 -1
  103. package/dist/index-AIfVzkhi.d.cts.map +0 -1
  104. package/dist/index-CubPzHd9.d.mts.map +0 -1
  105. package/dist/subscribers/index.cjs.map +0 -1
  106. package/dist/subscribers/index.d.cts.map +0 -1
  107. package/dist/subscribers/index.d.mts.map +0 -1
  108. package/dist/subscribers/index.mjs.map +0 -1
  109. package/dist/topic/index.d.cts.map +0 -1
  110. package/dist/topic/index.d.mts.map +0 -1
package/dist/worker.cjs CHANGED
@@ -1,10 +1,10 @@
1
1
  const require_chunk = require('./chunk-DWy1uDak.cjs');
2
2
  const require_construct_interface = require('./construct-interface-BfjcOD_E.cjs');
3
3
  require('./Construct-CyS3RpTL.cjs');
4
- const require_functions = require('./functions-DJJA7VGc.cjs');
4
+ const require_functions = require('./functions-i_la-N5r.cjs');
5
5
  require('./clone-Dy0cR2SZ.cjs');
6
6
  const require_surfaceEnv = require('./surfaceEnv-BFXEms2f.cjs');
7
- const require_CronBuilder = require('./CronBuilder-DyUr6nYz.cjs');
7
+ const require_CronBuilder = require('./CronBuilder-Da0C0edR.cjs');
8
8
  const require_SubscriberBuilder = require('./SubscriberBuilder-4OPehf2R.cjs');
9
9
  const __geekmidas_manifest = require_chunk.__toESM(require("@geekmidas/manifest"));
10
10
  const __geekmidas_logger_console = require_chunk.__toESM(require("@geekmidas/logger/console"));
@@ -16,7 +16,12 @@ var Worker = class Worker {
16
16
  logger;
17
17
  /** Always defined, and normally the default. */
18
18
  envParser;
19
- constructor(id, config = {}, dependencies = []) {
19
+ /**
20
+ * Where this worker's schedules live, when it has crons and a server runs
21
+ * them. `undefined` until `.database(…)` says.
22
+ */
23
+ scheduleStore;
24
+ constructor(id, config = {}, dependencies = [], scheduleStore) {
20
25
  this.config = config;
21
26
  this.dependencies = dependencies;
22
27
  const canonical = (0, __geekmidas_manifest.canonicalId)(id);
@@ -26,37 +31,107 @@ var Worker = class Worker {
26
31
  id: canonical,
27
32
  envParser: config.envParser
28
33
  } : void 0);
34
+ this.scheduleStore = scheduleStore;
29
35
  }
30
36
  /**
31
- * This worker's cron factory, carrying its logger.
37
+ * The database this worker keeps its schedules in.
38
+ *
39
+ * Only a server target uses it. On AWS a cron is an EventBridge rule
40
+ * invoking a Lambda and nothing here is consulted — but a server has nothing
41
+ * firing its crons except itself, and a process that schedules in memory
42
+ * fires every job once per replica while reporting that all is well.
43
+ *
44
+ * So the store is declared rather than discovered. It could have been
45
+ * inferred — from a database the app happens to declare, or from the events
46
+ * backend when that backend is pg-boss — and both would work until an app
47
+ * had two databases, or moved its events to SNS, at which point the
48
+ * schedules would quietly move or stop. Naming it is one line and cannot
49
+ * drift.
32
50
  *
33
- * `export const c = crons.logger(logger)` written once and hung off the
34
- * thing that runs it, so a cron file opens with the schedule rather than
35
- * with an import of a logger it has to know the path to.
51
+ * @example
52
+ * ```ts
53
+ * export const jobs = new Worker('Jobs', { logger }).database(database);
54
+ * ```
36
55
  */
37
- get crons() {
38
- return this.own(new require_CronBuilder.CronBuilder().logger(this.logger));
56
+ database(source) {
57
+ return new Worker(this.id, this.config, [...this.dependencies, require_construct_interface.edgeTo(source)], source);
39
58
  }
40
59
  /**
41
- * This worker's subscriber factory, carrying its logger.
60
+ * A scheduled runnable.
42
61
  *
43
- * Chain `.publisher(…)` before `.subscribe([…])`: the event names are typed
44
- * from the publisher's message union, so there is nothing to spell twice.
62
+ * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
45
63
  */
46
- get subscribers() {
47
- return this.own(new require_SubscriberBuilder.SubscriberBuilder().logger(this.logger));
64
+ cron(schedule) {
65
+ return this.own(new require_CronBuilder.CronBuilder().logger(this.logger)).schedule(schedule);
48
66
  }
49
- /** This worker's function factory, carrying its logger. */
50
- get functions() {
51
- return this.own(new require_functions.FunctionBuilder().logger(this.logger));
67
+ /**
68
+ * A runnable that consumes a topic.
69
+ *
70
+ * Binding is not publishing: a consumer is handed the topic's event types
71
+ * and no connection string it could publish with. Chain `.publishes(…)` when
72
+ * the handler emits follow-up events, which is the only reason it would need
73
+ * one.
74
+ *
75
+ * @example `worker.topic(users).subscribe(['user.created']).handle(…)`
76
+ */
77
+ topic(topic) {
78
+ return this.own(new require_SubscriberBuilder.SubscriberBuilder().logger(this.logger)).topic(topic);
52
79
  }
53
80
  /**
54
- * Sugar for the common case, the way `api.get()` is for `api.endpoints`.
81
+ * A runnable that consumes events typed from a publisher service.
55
82
  *
56
- * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
83
+ * The older of the two ways to bind a subscriber, kept for a project whose
84
+ * events come from a hand-written publisher rather than a `Topic`
85
+ * construct. `topic()` is the one to reach for: it types the events the same
86
+ * way and hands the consumer no connection string it could publish with.
57
87
  */
58
- cron(schedule) {
59
- return this.crons.schedule(schedule);
88
+ publisher(service) {
89
+ return this.own(new require_SubscriberBuilder.SubscriberBuilder().logger(this.logger)).publisher(service);
90
+ }
91
+ /**
92
+ * Everything below builds a function — the runnable with no schedule and no
93
+ * topic, invoked by something else.
94
+ *
95
+ * These sit on the worker rather than behind a `functions` namespace because
96
+ * the namespace was a hop that said nothing: `worker.functions.input(…)`
97
+ * names a collection to reach one member of it. What kind of runnable is
98
+ * being built is decided by which of these is called first — a schedule
99
+ * makes a cron, a topic makes a subscriber, and anything else makes a
100
+ * function.
101
+ */
102
+ input(schema) {
103
+ return this.functions.input(schema);
104
+ }
105
+ output(schema) {
106
+ return this.functions.output(schema);
107
+ }
108
+ timeout(ms) {
109
+ return this.functions.timeout(ms);
110
+ }
111
+ memorySize(mb) {
112
+ return this.functions.memorySize(mb);
113
+ }
114
+ dependsOn(constructs) {
115
+ return this.functions.dependsOn(constructs);
116
+ }
117
+ services(services) {
118
+ return this.functions.services(services);
119
+ }
120
+ /**
121
+ * A function with no input schema — the shortest runnable there is.
122
+ *
123
+ * @example `worker.handle(async ({ logger }) => { … })`
124
+ */
125
+ handle(...args) {
126
+ return this.schemalessFunction().handle(...args);
127
+ }
128
+ /** The builder `handle` delegates to, named so its type can be referred to. */
129
+ schemalessFunction() {
130
+ return this.functions;
131
+ }
132
+ /** The function builder the delegating methods above are built on. */
133
+ get functions() {
134
+ return this.own(new require_functions.FunctionBuilder().logger(this.logger));
60
135
  }
61
136
  /**
62
137
  * Resources and surfaces this worker calls.
@@ -65,7 +140,7 @@ var Worker = class Worker {
65
140
  * environment and its access, the same distinction `RestApi.calls()` draws.
66
141
  */
67
142
  calls(constructs) {
68
- return new Worker(this.id, this.config, [...this.dependencies, ...constructs.map(require_construct_interface.edgeTo)]);
143
+ return new Worker(this.id, this.config, [...this.dependencies, ...constructs.map(require_construct_interface.edgeTo)], this.scheduleStore);
69
144
  }
70
145
  /**
71
146
  * Stamps this worker's id onto a builder, so everything built from it says
@@ -73,13 +148,13 @@ var Worker = class Worker {
73
148
  */
74
149
  own(builder) {
75
150
  builder._owner = this.id;
151
+ builder._scheduleStore = this.scheduleStore;
76
152
  return builder;
77
153
  }
78
154
  declare() {
79
155
  return [{
80
156
  kind: "worker",
81
157
  id: this.id,
82
- ...this.config.app ? { app: this.config.app } : {},
83
158
  ...this.dependencies.length ? { dependencies: this.dependencies } : {},
84
159
  provides: []
85
160
  }];
@@ -1 +1 @@
1
- {"version":3,"file":"worker.cjs","names":["id: ConstructName<TName>","config: WorkerConfig","dependencies: readonly Dependency[]","DEFAULT_LOGGER","CronBuilder","SubscriberBuilder","FunctionBuilder","schedule: ScheduleExpression","constructs: readonly Declarable[]","edgeTo","builder: T"],"sources":["../src/worker.ts"],"sourcesContent":["/**\n * A process with no port.\n *\n * `RestApi` is the process that answers HTTP. This is the one that does not —\n * the thing that runs a schedule, drains a queue, consumes a topic. Until it\n * existed, those had nowhere to say they belonged: a cron declared a schedule\n * and nothing else, and which container ran it was decided by the directory the\n * file sat in. A glob owned it.\n *\n * The cost of that shows up in the scaffolds. `gkm init --template worker` —\n * a project that is background jobs and nothing else — declared\n * `new RestApi('Api', { defaultAuthorizer: 'none', logger })`: an HTTP surface\n * with no HTTP on it, written because it was the only construct that made an\n * app exist and the only way to get a logger into the generated runtime.\n *\n * It takes no authorizer, and that is the tell that these are two constructs\n * rather than one with a flag: a `RestApi` needs one because an HTTP surface\n * can ship open by omission, and nothing reaches a worker from outside.\n *\n * @example\n * ```ts\n * // constructs/worker.ts\n * import { Worker } from '@geekmidas/constructs/worker';\n * import { logger } from './logger.js';\n *\n * export const worker = new Worker('Worker', { logger }).calls([database]);\n *\n * // crons/cleanup.ts\n * import { worker } from '@acme/constructs/worker.js';\n *\n * export const cleanup = worker\n * .cron('rate(1 day)')\n * .handle(async ({ logger }) => { … });\n * ```\n */\n\nimport type { EnvironmentParser } from '@geekmidas/envkit';\nimport type { Logger } from '@geekmidas/logger';\nimport { DEFAULT_LOGGER } from '@geekmidas/logger/console';\nimport {\n\ttype AppSpec,\n\ttype ConstructName,\n\tcanonicalId,\n\ttype Declaration,\n\ttype Dependency,\n} from '@geekmidas/manifest';\nimport { type Declarable, edgeTo } from './construct-interface';\nimport type { ScheduleExpression } from './crons/Cron';\nimport { CronBuilder } from './crons/CronBuilder';\nimport { envParserFor } from './endpoints/surfaceEnv';\nimport { FunctionBuilder } from './functions/FunctionBuilder';\nimport { SubscriberBuilder } from './subscribers/SubscriberBuilder';\n\nexport interface WorkerConfig {\n\t/**\n\t * Where its source lives and how it is run.\n\t *\n\t * Normally omitted, like every other app's: `Worker` means `apps/worker`\n\t * where that directory exists and the project root otherwise. Write one only\n\t * for a layout that genuinely differs.\n\t */\n\tapp?: AppSpec;\n\t/**\n\t * The logger everything built from this worker runs with.\n\t *\n\t * The actual logger, not a path to one — the same reason `RestApi` takes\n\t * one. Every cron and subscriber in a scaffolded project used to open with\n\t * `import { logger } from '…/constructs/logger.js'`, which is the line this\n\t * removes.\n\t */\n\tlogger?: Logger;\n\t/**\n\t * The environment parser everything built from this worker runs with.\n\t *\n\t * Defaults to `process.env` merged with the credentials `gkm dev` injected,\n\t * which is what an application's own `config/env.ts` always was.\n\t */\n\tenvParser?: EnvironmentParser<{}>;\n}\n\nexport class Worker<TName extends string = string>\n\timplements Declarable<TName>\n{\n\treadonly id: TName;\n\n\t/** Always defined — the worker's own, or the console logger. */\n\treadonly logger: Logger;\n\n\t/** Always defined, and normally the default. */\n\treadonly envParser: EnvironmentParser<{}>;\n\n\tconstructor(\n\t\tid: ConstructName<TName>,\n\t\tprivate readonly config: WorkerConfig = {},\n\t\t/** Internal: how `.calls()` carries edges into the copy it returns. */\n\t\tprivate readonly dependencies: readonly Dependency[] = [],\n\t) {\n\t\tconst canonical = canonicalId(id as string);\n\n\t\tthis.id = canonical as TName;\n\t\tthis.logger = config.logger ?? DEFAULT_LOGGER;\n\t\tthis.envParser = envParserFor(\n\t\t\tconfig.envParser\n\t\t\t\t? { id: canonical, envParser: config.envParser }\n\t\t\t\t: undefined,\n\t\t);\n\t}\n\n\t/**\n\t * This worker's cron factory, carrying its logger.\n\t *\n\t * `export const c = crons.logger(logger)` written once and hung off the\n\t * thing that runs it, so a cron file opens with the schedule rather than\n\t * with an import of a logger it has to know the path to.\n\t */\n\tget crons() {\n\t\treturn this.own(new CronBuilder().logger(this.logger));\n\t}\n\n\t/**\n\t * This worker's subscriber factory, carrying its logger.\n\t *\n\t * Chain `.publisher(…)` before `.subscribe([…])`: the event names are typed\n\t * from the publisher's message union, so there is nothing to spell twice.\n\t */\n\tget subscribers() {\n\t\treturn this.own(new SubscriberBuilder().logger(this.logger));\n\t}\n\n\t/** This worker's function factory, carrying its logger. */\n\tget functions() {\n\t\treturn this.own(new FunctionBuilder().logger(this.logger));\n\t}\n\n\t/**\n\t * Sugar for the common case, the way `api.get()` is for `api.endpoints`.\n\t *\n\t * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`\n\t */\n\tcron(schedule: ScheduleExpression) {\n\t\treturn this.crons.schedule(schedule);\n\t}\n\n\t/**\n\t * Resources and surfaces this worker calls.\n\t *\n\t * Not `.dependsOn()`: this records the edge that derives the worker's\n\t * environment and its access, the same distinction `RestApi.calls()` draws.\n\t */\n\tcalls(constructs: readonly Declarable[]): Worker<TName> {\n\t\treturn new Worker<TName>(this.id as ConstructName<TName>, this.config, [\n\t\t\t...this.dependencies,\n\t\t\t...constructs.map(edgeTo),\n\t\t]);\n\t}\n\n\t/**\n\t * Stamps this worker's id onto a builder, so everything built from it says\n\t * which process runs it.\n\t */\n\tprivate own<T extends { _owner?: string }>(builder: T): T {\n\t\tbuilder._owner = this.id;\n\n\t\treturn builder;\n\t}\n\n\tdeclare(): Declaration[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tkind: 'worker',\n\t\t\t\tid: this.id,\n\t\t\t\t...(this.config.app ? { app: this.config.app } : {}),\n\t\t\t\t...(this.dependencies.length\n\t\t\t\t\t? { dependencies: this.dependencies }\n\t\t\t\t\t: {}),\n\t\t\t\t// Nothing calls a worker, so it publishes no address.\n\t\t\t\tprovides: [],\n\t\t\t},\n\t\t];\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAgFA,IAAa,SAAb,MAAa,OAEb;CACC,AAAS;;CAGT,AAAS;;CAGT,AAAS;CAET,YACCA,IACiBC,SAAuB,CAAE,GAEzBC,eAAsC,CAAE,GACxD;EAHgB;EAEA;EAEjB,MAAM,YAAY,sCAAY,GAAa;AAE3C,OAAK,KAAK;AACV,OAAK,SAAS,OAAO,UAAUC;AAC/B,OAAK,YAAY,gCAChB,OAAO,YACJ;GAAE,IAAI;GAAW,WAAW,OAAO;EAAW,WAEjD;CACD;;;;;;;;CASD,IAAI,QAAQ;AACX,SAAO,KAAK,IAAI,IAAIC,kCAAc,OAAO,KAAK,OAAO,CAAC;CACtD;;;;;;;CAQD,IAAI,cAAc;AACjB,SAAO,KAAK,IAAI,IAAIC,8CAAoB,OAAO,KAAK,OAAO,CAAC;CAC5D;;CAGD,IAAI,YAAY;AACf,SAAO,KAAK,IAAI,IAAIC,oCAAkB,OAAO,KAAK,OAAO,CAAC;CAC1D;;;;;;CAOD,KAAKC,UAA8B;AAClC,SAAO,KAAK,MAAM,SAAS,SAAS;CACpC;;;;;;;CAQD,MAAMC,YAAkD;AACvD,SAAO,IAAI,OAAc,KAAK,IAA4B,KAAK,QAAQ,CACtE,GAAG,KAAK,cACR,GAAG,WAAW,IAAIC,mCAAO,AACzB;CACD;;;;;CAMD,AAAQ,IAAmCC,SAAe;AACzD,UAAQ,SAAS,KAAK;AAEtB,SAAO;CACP;CAED,UAAyB;AACxB,SAAO,CACN;GACC,MAAM;GACN,IAAI,KAAK;GACT,GAAI,KAAK,OAAO,MAAM,EAAE,KAAK,KAAK,OAAO,IAAK,IAAG,CAAE;GACnD,GAAI,KAAK,aAAa,SACnB,EAAE,cAAc,KAAK,aAAc,IACnC,CAAE;GAEL,UAAU,CAAE;EACZ,CACD;CACD;AACD"}
1
+ {"version":3,"file":"worker.cjs","names":["id: ConstructName<TName>","config: WorkerConfig","dependencies: readonly Dependency[]","scheduleStore?: Consumable<string, unknown>","DEFAULT_LOGGER","source: Consumable<TDbName, T>","schedule: ScheduleExpression","CronBuilder","topic: Topic<TTopicName, TEvents>","SubscriberBuilder","service: Service<TPubName, T> | Consumable<TPubName, T>","schema: T","ms: number","mb: number","constructs: T","services: T","FunctionBuilder","constructs: readonly Declarable[]","edgeTo","builder: T"],"sources":["../src/worker.ts"],"sourcesContent":["/**\n * A process with no port.\n *\n * `RestApi` is the process that answers HTTP. This is the one that does not —\n * the thing that runs a schedule, drains a queue, consumes a topic. Until it\n * existed, those had nowhere to say they belonged: a cron declared a schedule\n * and nothing else, and which container ran it was decided by the directory the\n * file sat in. A glob owned it.\n *\n * The cost of that shows up in the scaffolds. `gkm init --template worker` —\n * a project that is background jobs and nothing else — declared\n * `new RestApi('Api', { defaultAuthorizer: 'none', logger })`: an HTTP surface\n * with no HTTP on it, written because it was the only construct that made an\n * app exist and the only way to get a logger into the generated runtime.\n *\n * It takes no authorizer, and that is the tell that these are two constructs\n * rather than one with a flag: a `RestApi` needs one because an HTTP surface\n * can ship open by omission, and nothing reaches a worker from outside.\n *\n * ## Crons on a server target need a database\n *\n * A cron becomes an EventBridge rule on AWS and nothing here is consulted. A\n * server has nothing firing its crons but itself, and a process that schedules\n * in memory fires every job once per replica while reporting success — so the\n * schedule is kept in Postgres, and the worker says which one with\n * `.database(db)`.\n *\n * A worker with crons and no `.database(…)` schedules nothing on a server\n * target and says so at startup. That is a limitation of workers today rather\n * than a design: the store could be a cache, or something the target\n * provisions, and Postgres is what exists.\n *\n * It is not an app. A worker names the process that runs its crons and\n * subscribers, and that process is the app's server — the same one the\n * endpoints run in, minus the HTTP surface. Giving a worker a container of its\n * own made a second thing to build, deploy and keep alive for work that was\n * always going to run somewhere already. Declare as many as the shape of the\n * work wants: they are groupings, not deployments.\n *\n * @example\n * ```ts\n * // constructs/worker.ts\n * import { Worker } from '@geekmidas/constructs/worker';\n * import { logger } from './logger.js';\n *\n * export const worker = new Worker('Worker', { logger }).calls([database]);\n *\n * // crons/cleanup.ts\n * import { worker } from '@acme/constructs/worker.js';\n *\n * export const cleanup = worker\n * .cron('rate(1 day)')\n * .handle(async ({ logger }) => { … });\n * ```\n */\n\nimport type { EnvironmentParser } from '@geekmidas/envkit';\nimport type { EventPublisher } from '@geekmidas/events';\nimport type { Logger } from '@geekmidas/logger';\nimport { DEFAULT_LOGGER } from '@geekmidas/logger/console';\nimport {\n\ttype ConstructName,\n\tcanonicalId,\n\ttype Declaration,\n\ttype Dependency,\n} from '@geekmidas/manifest';\nimport type { ComposableStandardSchema } from '@geekmidas/schema';\nimport type { Service } from '@geekmidas/services';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\nimport {\n\ttype Consumable,\n\ttype Declarable,\n\tedgeTo,\n} from './construct-interface';\nimport type { ScheduleExpression } from './crons/Cron';\nimport { CronBuilder } from './crons/CronBuilder';\nimport { envParserFor } from './endpoints/surfaceEnv';\nimport { FunctionBuilder } from './functions/FunctionBuilder';\nimport { SubscriberBuilder } from './subscribers/SubscriberBuilder';\nimport type { Topic, TopicEvents } from './topic/Topic';\n\nexport interface WorkerConfig {\n\t/**\n\t * The logger everything built from this worker runs with.\n\t *\n\t * The actual logger, not a path to one — the same reason `RestApi` takes\n\t * one. Every cron and subscriber in a scaffolded project used to open with\n\t * `import { logger } from '…/constructs/logger.js'`, which is the line this\n\t * removes.\n\t */\n\tlogger?: Logger;\n\t/**\n\t * The environment parser everything built from this worker runs with.\n\t *\n\t * Defaults to `process.env` merged with the credentials `gkm dev` injected,\n\t * which is what an application's own `config/env.ts` always was.\n\t */\n\tenvParser?: EnvironmentParser<{}>;\n}\n\nexport class Worker<TName extends string = string>\n\timplements Declarable<TName>\n{\n\treadonly id: TName;\n\n\t/** Always defined — the worker's own, or the console logger. */\n\treadonly logger: Logger;\n\n\t/** Always defined, and normally the default. */\n\treadonly envParser: EnvironmentParser<{}>;\n\n\t/**\n\t * Where this worker's schedules live, when it has crons and a server runs\n\t * them. `undefined` until `.database(…)` says.\n\t */\n\treadonly scheduleStore?: Consumable<string, unknown>;\n\n\tconstructor(\n\t\tid: ConstructName<TName>,\n\t\tprivate readonly config: WorkerConfig = {},\n\t\t/** Internal: how `.calls()` carries edges into the copy it returns. */\n\t\tprivate readonly dependencies: readonly Dependency[] = [],\n\t\tscheduleStore?: Consumable<string, unknown>,\n\t) {\n\t\tconst canonical = canonicalId(id as string);\n\n\t\tthis.id = canonical as TName;\n\t\tthis.logger = config.logger ?? DEFAULT_LOGGER;\n\t\tthis.envParser = envParserFor(\n\t\t\tconfig.envParser\n\t\t\t\t? { id: canonical, envParser: config.envParser }\n\t\t\t\t: undefined,\n\t\t);\n\t\tthis.scheduleStore = scheduleStore;\n\t}\n\n\t/**\n\t * The database this worker keeps its schedules in.\n\t *\n\t * Only a server target uses it. On AWS a cron is an EventBridge rule\n\t * invoking a Lambda and nothing here is consulted — but a server has nothing\n\t * firing its crons except itself, and a process that schedules in memory\n\t * fires every job once per replica while reporting that all is well.\n\t *\n\t * So the store is declared rather than discovered. It could have been\n\t * inferred — from a database the app happens to declare, or from the events\n\t * backend when that backend is pg-boss — and both would work until an app\n\t * had two databases, or moved its events to SNS, at which point the\n\t * schedules would quietly move or stop. Naming it is one line and cannot\n\t * drift.\n\t *\n\t * @example\n\t * ```ts\n\t * export const jobs = new Worker('Jobs', { logger }).database(database);\n\t * ```\n\t */\n\tdatabase<T, TDbName extends string>(\n\t\tsource: Consumable<TDbName, T>,\n\t): Worker<TName> {\n\t\treturn new Worker<TName>(\n\t\t\tthis.id as ConstructName<TName>,\n\t\t\tthis.config,\n\t\t\t[...this.dependencies, edgeTo(source)],\n\t\t\tsource as unknown as Consumable<string, unknown>,\n\t\t);\n\t}\n\n\t/**\n\t * A scheduled runnable.\n\t *\n\t * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`\n\t */\n\tcron(schedule: ScheduleExpression) {\n\t\treturn this.own(new CronBuilder().logger(this.logger)).schedule(schedule);\n\t}\n\n\t/**\n\t * A runnable that consumes a topic.\n\t *\n\t * Binding is not publishing: a consumer is handed the topic's event types\n\t * and no connection string it could publish with. Chain `.publishes(…)` when\n\t * the handler emits follow-up events, which is the only reason it would need\n\t * one.\n\t *\n\t * @example `worker.topic(users).subscribe(['user.created']).handle(…)`\n\t */\n\ttopic<TTopicName extends string, TEvents extends TopicEvents>(\n\t\ttopic: Topic<TTopicName, TEvents>,\n\t) {\n\t\treturn this.own(new SubscriberBuilder().logger(this.logger)).topic(topic);\n\t}\n\n\t/**\n\t * A runnable that consumes events typed from a publisher service.\n\t *\n\t * The older of the two ways to bind a subscriber, kept for a project whose\n\t * events come from a hand-written publisher rather than a `Topic`\n\t * construct. `topic()` is the one to reach for: it types the events the same\n\t * way and hands the consumer no connection string it could publish with.\n\t */\n\tpublisher<T extends EventPublisher<any>, TPubName extends string>(\n\t\tservice: Service<TPubName, T> | Consumable<TPubName, T>,\n\t) {\n\t\treturn this.own(new SubscriberBuilder().logger(this.logger)).publisher(\n\t\t\tservice as never,\n\t\t);\n\t}\n\n\t/**\n\t * Everything below builds a function — the runnable with no schedule and no\n\t * topic, invoked by something else.\n\t *\n\t * These sit on the worker rather than behind a `functions` namespace because\n\t * the namespace was a hop that said nothing: `worker.functions.input(…)`\n\t * names a collection to reach one member of it. What kind of runnable is\n\t * being built is decided by which of these is called first — a schedule\n\t * makes a cron, a topic makes a subscriber, and anything else makes a\n\t * function.\n\t */\n\tinput<T extends ComposableStandardSchema>(schema: T) {\n\t\treturn this.functions.input(schema);\n\t}\n\n\toutput<T extends StandardSchemaV1>(schema: T) {\n\t\treturn this.functions.output(schema);\n\t}\n\n\ttimeout(ms: number) {\n\t\treturn this.functions.timeout(ms);\n\t}\n\n\tmemorySize(mb: number) {\n\t\treturn this.functions.memorySize(mb);\n\t}\n\n\tdependsOn<const T extends readonly Consumable[]>(constructs: T) {\n\t\treturn this.functions.dependsOn(constructs);\n\t}\n\n\tservices<T extends Service[]>(services: T) {\n\t\treturn this.functions.services(services);\n\t}\n\n\t/**\n\t * A function with no input schema — the shortest runnable there is.\n\t *\n\t * @example `worker.handle(async ({ logger }) => { … })`\n\t */\n\thandle(\n\t\t...args: Parameters<\n\t\t\tReturnType<Worker<TName>['schemalessFunction']>['handle']\n\t\t>\n\t) {\n\t\treturn this.schemalessFunction().handle(...args);\n\t}\n\n\t/** The builder `handle` delegates to, named so its type can be referred to. */\n\tprivate schemalessFunction() {\n\t\treturn this.functions;\n\t}\n\n\t/** The function builder the delegating methods above are built on. */\n\tprivate get functions() {\n\t\treturn this.own(new FunctionBuilder().logger(this.logger));\n\t}\n\n\t/**\n\t * Resources and surfaces this worker calls.\n\t *\n\t * Not `.dependsOn()`: this records the edge that derives the worker's\n\t * environment and its access, the same distinction `RestApi.calls()` draws.\n\t */\n\tcalls(constructs: readonly Declarable[]): Worker<TName> {\n\t\treturn new Worker<TName>(\n\t\t\tthis.id as ConstructName<TName>,\n\t\t\tthis.config,\n\t\t\t[...this.dependencies, ...constructs.map(edgeTo)],\n\t\t\tthis.scheduleStore,\n\t\t);\n\t}\n\n\t/**\n\t * Stamps this worker's id onto a builder, so everything built from it says\n\t * which process runs it.\n\t */\n\tprivate own<T extends { _owner?: string; _scheduleStore?: unknown }>(\n\t\tbuilder: T,\n\t): T {\n\t\tbuilder._owner = this.id;\n\t\tbuilder._scheduleStore = this.scheduleStore;\n\n\t\treturn builder;\n\t}\n\n\tdeclare(): Declaration[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tkind: 'worker',\n\t\t\t\tid: this.id,\n\t\t\t\t...(this.dependencies.length\n\t\t\t\t\t? { dependencies: this.dependencies }\n\t\t\t\t\t: {}),\n\t\t\t\t// Nothing calls a worker, so it publishes no address.\n\t\t\t\tprovides: [],\n\t\t\t},\n\t\t];\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAoGA,IAAa,SAAb,MAAa,OAEb;CACC,AAAS;;CAGT,AAAS;;CAGT,AAAS;;;;;CAMT,AAAS;CAET,YACCA,IACiBC,SAAuB,CAAE,GAEzBC,eAAsC,CAAE,GACzDC,eACC;EAJgB;EAEA;EAGjB,MAAM,YAAY,sCAAY,GAAa;AAE3C,OAAK,KAAK;AACV,OAAK,SAAS,OAAO,UAAUC;AAC/B,OAAK,YAAY,gCAChB,OAAO,YACJ;GAAE,IAAI;GAAW,WAAW,OAAO;EAAW,WAEjD;AACD,OAAK,gBAAgB;CACrB;;;;;;;;;;;;;;;;;;;;;CAsBD,SACCC,QACgB;AAChB,SAAO,IAAI,OACV,KAAK,IACL,KAAK,QACL,CAAC,GAAG,KAAK,cAAc,mCAAO,OAAO,AAAC,GACtC;CAED;;;;;;CAOD,KAAKC,UAA8B;AAClC,SAAO,KAAK,IAAI,IAAIC,kCAAc,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,SAAS;CACzE;;;;;;;;;;;CAYD,MACCC,OACC;AACD,SAAO,KAAK,IAAI,IAAIC,8CAAoB,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,MAAM;CACzE;;;;;;;;;CAUD,UACCC,SACC;AACD,SAAO,KAAK,IAAI,IAAID,8CAAoB,OAAO,KAAK,OAAO,CAAC,CAAC,UAC5D,QACA;CACD;;;;;;;;;;;;CAaD,MAA0CE,QAAW;AACpD,SAAO,KAAK,UAAU,MAAM,OAAO;CACnC;CAED,OAAmCA,QAAW;AAC7C,SAAO,KAAK,UAAU,OAAO,OAAO;CACpC;CAED,QAAQC,IAAY;AACnB,SAAO,KAAK,UAAU,QAAQ,GAAG;CACjC;CAED,WAAWC,IAAY;AACtB,SAAO,KAAK,UAAU,WAAW,GAAG;CACpC;CAED,UAAiDC,YAAe;AAC/D,SAAO,KAAK,UAAU,UAAU,WAAW;CAC3C;CAED,SAA8BC,UAAa;AAC1C,SAAO,KAAK,UAAU,SAAS,SAAS;CACxC;;;;;;CAOD,OACC,GAAG,MAGF;AACD,SAAO,KAAK,oBAAoB,CAAC,OAAO,GAAG,KAAK;CAChD;;CAGD,AAAQ,qBAAqB;AAC5B,SAAO,KAAK;CACZ;;CAGD,IAAY,YAAY;AACvB,SAAO,KAAK,IAAI,IAAIC,oCAAkB,OAAO,KAAK,OAAO,CAAC;CAC1D;;;;;;;CAQD,MAAMC,YAAkD;AACvD,SAAO,IAAI,OACV,KAAK,IACL,KAAK,QACL,CAAC,GAAG,KAAK,cAAc,GAAG,WAAW,IAAIC,mCAAO,AAAC,GACjD,KAAK;CAEN;;;;;CAMD,AAAQ,IACPC,SACI;AACJ,UAAQ,SAAS,KAAK;AACtB,UAAQ,iBAAiB,KAAK;AAE9B,SAAO;CACP;CAED,UAAyB;AACxB,SAAO,CACN;GACC,MAAM;GACN,IAAI,KAAK;GACT,GAAI,KAAK,aAAa,SACnB,EAAE,cAAc,KAAK,aAAc,IACnC,CAAE;GAEL,UAAU,CAAE;EACZ,CACD;CACD;AACD"}
package/dist/worker.d.cts CHANGED
@@ -1,29 +1,24 @@
1
1
  import "./Construct-C0ijr2qp.cjs";
2
- import { FunctionBuilder } from "./index-AIfVzkhi.cjs";
3
- import { Declarable } from "./construct-interface-D9d4Gm4p.cjs";
4
- import { ScheduleExpression } from "./Cron-B2OFsfDM.cjs";
2
+ import { Function, FunctionBuilder, FunctionHandler } from "./index-D24Xm79M.cjs";
3
+ import { Consumable, Declarable, ServicesOf } from "./construct-interface-D9d4Gm4p.cjs";
4
+ import { ScheduleExpression } from "./Cron-DmQ8vQ4b.cjs";
5
5
  import "./Subscriber-2mEi0ImH.cjs";
6
- import { CronBuilder } from "./CronBuilder-LSJMfqnq.cjs";
7
- import "./Topic-19QeHBIR.cjs";
6
+ import { CronBuilder } from "./CronBuilder-BquXDbDI.cjs";
7
+ import { Topic, TopicEvents, TopicMessage } from "./Topic-19QeHBIR.cjs";
8
8
  import { SubscriberBuilder } from "./SubscriberBuilder-waNhzjSM.cjs";
9
+ import "./index-CBSfrowv.cjs";
9
10
  import { EnvironmentParser } from "@geekmidas/envkit";
11
+ import { EventPublisher } from "@geekmidas/events";
10
12
  import { Logger } from "@geekmidas/logger";
11
- import * as _geekmidas_schema21 from "@geekmidas/schema";
12
- import * as _geekmidas_services22 from "@geekmidas/services";
13
- import * as _geekmidas_audit24 from "@geekmidas/audit";
14
- import { AppSpec, ConstructName, Declaration, Dependency } from "@geekmidas/manifest";
13
+ import { ComposableStandardSchema } from "@geekmidas/schema";
14
+ import { Service } from "@geekmidas/services";
15
+ import { StandardSchemaV1 } from "@standard-schema/spec";
16
+ import * as _geekmidas_audit13 from "@geekmidas/audit";
17
+ import { ConstructName, Declaration, Dependency } from "@geekmidas/manifest";
15
18
 
16
19
  //#region src/worker.d.ts
17
20
 
18
21
  interface WorkerConfig {
19
- /**
20
- * Where its source lives and how it is run.
21
- *
22
- * Normally omitted, like every other app's: `Worker` means `apps/worker`
23
- * where that directory exists and the project root otherwise. Write one only
24
- * for a layout that genuinely differs.
25
- */
26
- app?: AppSpec;
27
22
  /**
28
23
  * The logger everything built from this worker runs with.
29
24
  *
@@ -50,31 +45,87 @@ declare class Worker<TName extends string = string> implements Declarable<TName>
50
45
  readonly logger: Logger;
51
46
  /** Always defined, and normally the default. */
52
47
  readonly envParser: EnvironmentParser<{}>;
48
+ /**
49
+ * Where this worker's schedules live, when it has crons and a server runs
50
+ * them. `undefined` until `.database(…)` says.
51
+ */
52
+ readonly scheduleStore?: Consumable<string, unknown>;
53
53
  constructor(id: ConstructName<TName>, config?: WorkerConfig, /** Internal: how `.calls()` carries edges into the copy it returns. */
54
- dependencies?: readonly Dependency[]);
54
+ dependencies?: readonly Dependency[], scheduleStore?: Consumable<string, unknown>);
55
55
  /**
56
- * This worker's cron factory, carrying its logger.
56
+ * The database this worker keeps its schedules in.
57
57
  *
58
- * `export const c = crons.logger(logger)` written once and hung off the
59
- * thing that runs it, so a cron file opens with the schedule rather than
60
- * with an import of a logger it has to know the path to.
58
+ * Only a server target uses it. On AWS a cron is an EventBridge rule
59
+ * invoking a Lambda and nothing here is consulted — but a server has nothing
60
+ * firing its crons except itself, and a process that schedules in memory
61
+ * fires every job once per replica while reporting that all is well.
62
+ *
63
+ * So the store is declared rather than discovered. It could have been
64
+ * inferred — from a database the app happens to declare, or from the events
65
+ * backend when that backend is pg-boss — and both would work until an app
66
+ * had two databases, or moved its events to SNS, at which point the
67
+ * schedules would quietly move or stop. Naming it is one line and cannot
68
+ * drift.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * export const jobs = new Worker('Jobs', { logger }).database(database);
73
+ * ```
61
74
  */
62
- get crons(): CronBuilder<_geekmidas_schema21.ComposableStandardSchema, _geekmidas_services22.Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
75
+ database<T, TDbName extends string>(source: Consumable<TDbName, T>): Worker<TName>;
63
76
  /**
64
- * This worker's subscriber factory, carrying its logger.
77
+ * A scheduled runnable.
65
78
  *
66
- * Chain `.publisher(…)` before `.subscribe([…])`: the event names are typed
67
- * from the publisher's message union, so there is nothing to spell twice.
79
+ * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
68
80
  */
69
- get subscribers(): SubscriberBuilder<[], Logger, undefined, undefined, string, []>;
70
- /** This worker's function factory, carrying its logger. */
71
- get functions(): FunctionBuilder<_geekmidas_schema21.ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit24.AuditableAction<string, unknown>>;
81
+ cron(schedule: ScheduleExpression): CronBuilder<ComposableStandardSchema, Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
72
82
  /**
73
- * Sugar for the common case, the way `api.get()` is for `api.endpoints`.
83
+ * A runnable that consumes a topic.
74
84
  *
75
- * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
85
+ * Binding is not publishing: a consumer is handed the topic's event types
86
+ * and no connection string it could publish with. Chain `.publishes(…)` when
87
+ * the handler emits follow-up events, which is the only reason it would need
88
+ * one.
89
+ *
90
+ * @example `worker.topic(users).subscribe(['user.created']).handle(…)`
91
+ */
92
+ topic<TTopicName extends string, TEvents extends TopicEvents>(topic: Topic<TTopicName, TEvents>): SubscriberBuilder<[], Logger, undefined, EventPublisher<TopicMessage<TEvents>>, string, []>;
93
+ /**
94
+ * A runnable that consumes events typed from a publisher service.
95
+ *
96
+ * The older of the two ways to bind a subscriber, kept for a project whose
97
+ * events come from a hand-written publisher rather than a `Topic`
98
+ * construct. `topic()` is the one to reach for: it types the events the same
99
+ * way and hands the consumer no connection string it could publish with.
100
+ */
101
+ publisher<T extends EventPublisher<any>, TPubName extends string>(service: Service<TPubName, T> | Consumable<TPubName, T>): SubscriberBuilder<[], Logger, undefined, EventPublisher<any>, string, []>;
102
+ /**
103
+ * Everything below builds a function — the runnable with no schedule and no
104
+ * topic, invoked by something else.
105
+ *
106
+ * These sit on the worker rather than behind a `functions` namespace because
107
+ * the namespace was a hop that said nothing: `worker.functions.input(…)`
108
+ * names a collection to reach one member of it. What kind of runnable is
109
+ * being built is decided by which of these is called first — a schedule
110
+ * makes a cron, a topic makes a subscriber, and anything else makes a
111
+ * function.
112
+ */
113
+ input<T extends ComposableStandardSchema>(schema: T): FunctionBuilder<T, undefined, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
114
+ output<T extends StandardSchemaV1>(schema: T): FunctionBuilder<ComposableStandardSchema, T, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
115
+ timeout(ms: number): FunctionBuilder<ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
116
+ memorySize(mb: number): FunctionBuilder<ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
117
+ dependsOn<const T extends readonly Consumable[]>(constructs: T): FunctionBuilder<ComposableStandardSchema, undefined, [...ServicesOf<T>], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
118
+ services<T extends Service[]>(services: T): FunctionBuilder<ComposableStandardSchema, undefined, [...T], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>>;
119
+ /**
120
+ * A function with no input schema — the shortest runnable there is.
121
+ *
122
+ * @example `worker.handle(async ({ logger }) => { … })`
76
123
  */
77
- cron(schedule: ScheduleExpression): CronBuilder<_geekmidas_schema21.ComposableStandardSchema, _geekmidas_services22.Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
124
+ handle(...args: Parameters<ReturnType<Worker<TName>['schemalessFunction']>['handle']>): Function<ComposableStandardSchema, [], Logger, undefined, undefined, string, undefined, string, undefined, string, _geekmidas_audit13.AuditableAction<string, unknown>, FunctionHandler<ComposableStandardSchema, [], Logger, undefined, undefined, undefined, _geekmidas_audit13.AuditableAction<string, unknown>>>;
125
+ /** The builder `handle` delegates to, named so its type can be referred to. */
126
+ private schemalessFunction;
127
+ /** The function builder the delegating methods above are built on. */
128
+ private get functions();
78
129
  /**
79
130
  * Resources and surfaces this worker calls.
80
131
  *
@@ -1 +1 @@
1
- {"version":3,"file":"worker.d.cts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;AA2IkC,UAtFjB,YAAA,CAsFiB;EAAA;;;;;;;EA2BX,GArFX,CAAA,EApBL,OAoBK;EAAU;;;;;;;;WAXZ;;;;;;;cAOG;;cAGA,iDACD,WAAW;;;;eAET;;mBAGI;;sBAGG;kBAGf,cAAc,iBACO;0BAEe;;;;;;;;eAoBhC,YApB0C,mBAAA,CAoB1C,wBAAA,EAAA,qBAAA,CAAA,4BAAA;;;;;;;qBAUM,sBAAA;;mBAKF,gBALE,mBAAA,CAKF,wBAAA,iBAAA,iEAAA,kBAAA,CAAA;;;;;;iBASE,qBAAkB,YAAA,mBAAA,CAAA,wBAAA,EAAA,qBAAA,CAAA,4BAAA;;;;;;;6BAUN,eAAe,OAAO;;;;;;aAiBtC"}
1
+ {"version":3,"file":"worker.d.cts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;;;;AA2NoD,UA1InC,YAAA,CA0ImC;EAAA;;;;;;;;EAIP,MAAA,CAAA,EArInC,MAqImC;EAAA;;;;;;EAQvB,SAAA,CAAA,EAtIT,iBAsIS,CAAA,CAAA,CAAA,CAAA;;AAIc,cAvIvB,MAuIuB,CAAA,cAAA,MAAA,GAAA,MAAA,CAAA,YAtIxB,UAsIwB,CAtIb,KAsIa,CAAA,CAAA;EAAU,iBAAgB,MAAA;EAAC;EAAA,iBAAA,YAAA;EAAA,SAAA,EAAA,EApIjD,KAoIiD;EAAA;EAAA,SAAA,MAAA,EAjI7C,MAiI6C;EAAA;EAAA,SAI3C,SAAA,EAlIC,iBAkID,CAAA,CAAA,CAAA,CAAA;EAAO;;;;EAAe,SAAA,aAAA,CAAA,EA5HhB,UA4HgB,CAAA,MAAA,EAAA,OAAA,CAAA;EAAA,WAAA,CAAA,EAAA,EAzHpC,aAyHoC,CAzHtB,KAyHsB,CAAA,EAAA,MAAA,CAAA,EAxHf,YAwHe,EAAA;EAWhB,YAAZ,CAAA,EAAA,SAjI4B,UAiI5B,EAAA,EAAA,aAAA,CAAA,EAhII,UAgIJ,CAAA,MAAA,EAAA,OAAA,CAAA;EAAM;;;;;;;;;;;;;;;AArJE;;;;;8CAwDZ,WAAW,SAAS,KAC1B,OAAO;;;;;;iBAcK,qBAAkB,YAAA,0BAAA,4BAAA;;;;;;;;;;;mDAcgB,oBACzC,MAAM,YAAY,WAAQ,sBAAA,mBAAA,eAAA,aAAA;;;;;;;;;sBAad,uDACV,QAAQ,UAAU,KAAK,WAAW,UAAU,KAAE,sBAAA,mBAAA;;;;;;;;;;;;kBAkBxC,kCAAkC,IAAC,gBAAA,kBAAA,iEAAA,kBAAA,CAAA;mBAIlC,0BAA0B,IAAC,gBAAA,0BAAA,OAAA,iEAAA,kBAAA,CAAA;uBAI1B,gBAAA,yCAAA,iEAAA,kBAAA,CAAA;0BAIG,gBAAA,yCAAA,iEAAA,kBAAA,CAAA;qCAIc,0BAA0B,IAAC,gBAAA,yCAAA,WAAA,KAAA,iEAAA,kBAAA,CAAA;qBAI3C,qBAAqB,IAAC,gBAAA,yCAAA,IAAA,iEAAA,kBAAA,CAAA;;;;;;kBAU/B,WACR,WAAW,OAAO,2CAClB,SAAA,8BAAA,4EAAA,kBAAA,CAAA,kCAAA,gBAAA,8BAAA,yCAAA,kBAAA,CAAA;;;;;;;;;;;6BAqByB,eAAe,OAAO;;;;;;aAsBtC"}
package/dist/worker.d.mts CHANGED
@@ -1,29 +1,23 @@
1
1
  import "./Construct-Ct8nalEi.mjs";
2
- import { FunctionBuilder } from "./index-CubPzHd9.mjs";
3
- import { Declarable } from "./construct-interface-g5GM5pwR.mjs";
4
- import { ScheduleExpression } from "./Cron-ByxdOfpE.mjs";
2
+ import { Function, FunctionBuilder, FunctionHandler } from "./index-_ajboYxK.mjs";
3
+ import { Consumable, Declarable, ServicesOf } from "./construct-interface-g5GM5pwR.mjs";
4
+ import { ScheduleExpression } from "./Cron-dh4rUgib.mjs";
5
5
  import "./Subscriber-xTJ9ayPN.mjs";
6
- import { CronBuilder } from "./CronBuilder-BL0AP6fs.mjs";
7
- import "./Topic-DuFeqkBh.mjs";
6
+ import { CronBuilder } from "./CronBuilder-CGgY2080.mjs";
7
+ import { Topic, TopicEvents, TopicMessage } from "./Topic-DuFeqkBh.mjs";
8
8
  import { SubscriberBuilder } from "./SubscriberBuilder-Dptmo-1A.mjs";
9
- import { AppSpec, ConstructName, Declaration, Dependency } from "@geekmidas/manifest";
9
+ import "./index-BknhM6Ux.mjs";
10
+ import { ConstructName, Declaration, Dependency } from "@geekmidas/manifest";
10
11
  import { EnvironmentParser } from "@geekmidas/envkit";
11
- import * as _geekmidas_services16 from "@geekmidas/services";
12
- import * as _geekmidas_audit18 from "@geekmidas/audit";
12
+ import { Service } from "@geekmidas/services";
13
+ import { EventPublisher } from "@geekmidas/events";
13
14
  import { Logger } from "@geekmidas/logger";
14
- import * as _geekmidas_schema15 from "@geekmidas/schema";
15
+ import { ComposableStandardSchema } from "@geekmidas/schema";
16
+ import { StandardSchemaV1 } from "@standard-schema/spec";
15
17
 
16
18
  //#region src/worker.d.ts
17
19
 
18
20
  interface WorkerConfig {
19
- /**
20
- * Where its source lives and how it is run.
21
- *
22
- * Normally omitted, like every other app's: `Worker` means `apps/worker`
23
- * where that directory exists and the project root otherwise. Write one only
24
- * for a layout that genuinely differs.
25
- */
26
- app?: AppSpec;
27
21
  /**
28
22
  * The logger everything built from this worker runs with.
29
23
  *
@@ -50,31 +44,87 @@ declare class Worker<TName extends string = string> implements Declarable<TName>
50
44
  readonly logger: Logger;
51
45
  /** Always defined, and normally the default. */
52
46
  readonly envParser: EnvironmentParser<{}>;
47
+ /**
48
+ * Where this worker's schedules live, when it has crons and a server runs
49
+ * them. `undefined` until `.database(…)` says.
50
+ */
51
+ readonly scheduleStore?: Consumable<string, unknown>;
53
52
  constructor(id: ConstructName<TName>, config?: WorkerConfig, /** Internal: how `.calls()` carries edges into the copy it returns. */
54
- dependencies?: readonly Dependency[]);
53
+ dependencies?: readonly Dependency[], scheduleStore?: Consumable<string, unknown>);
55
54
  /**
56
- * This worker's cron factory, carrying its logger.
55
+ * The database this worker keeps its schedules in.
57
56
  *
58
- * `export const c = crons.logger(logger)` written once and hung off the
59
- * thing that runs it, so a cron file opens with the schedule rather than
60
- * with an import of a logger it has to know the path to.
57
+ * Only a server target uses it. On AWS a cron is an EventBridge rule
58
+ * invoking a Lambda and nothing here is consulted — but a server has nothing
59
+ * firing its crons except itself, and a process that schedules in memory
60
+ * fires every job once per replica while reporting that all is well.
61
+ *
62
+ * So the store is declared rather than discovered. It could have been
63
+ * inferred — from a database the app happens to declare, or from the events
64
+ * backend when that backend is pg-boss — and both would work until an app
65
+ * had two databases, or moved its events to SNS, at which point the
66
+ * schedules would quietly move or stop. Naming it is one line and cannot
67
+ * drift.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * export const jobs = new Worker('Jobs', { logger }).database(database);
72
+ * ```
61
73
  */
62
- get crons(): CronBuilder<_geekmidas_schema15.ComposableStandardSchema, _geekmidas_services16.Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
74
+ database<T, TDbName extends string>(source: Consumable<TDbName, T>): Worker<TName>;
63
75
  /**
64
- * This worker's subscriber factory, carrying its logger.
76
+ * A scheduled runnable.
65
77
  *
66
- * Chain `.publisher(…)` before `.subscribe([…])`: the event names are typed
67
- * from the publisher's message union, so there is nothing to spell twice.
78
+ * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
68
79
  */
69
- get subscribers(): SubscriberBuilder<[], Logger, undefined, undefined, string, []>;
70
- /** This worker's function factory, carrying its logger. */
71
- get functions(): FunctionBuilder<_geekmidas_schema15.ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit18.AuditableAction<string, unknown>>;
80
+ cron(schedule: ScheduleExpression): CronBuilder<ComposableStandardSchema, Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
72
81
  /**
73
- * Sugar for the common case, the way `api.get()` is for `api.endpoints`.
82
+ * A runnable that consumes a topic.
74
83
  *
75
- * @example `worker.cron('rate(1 day)').handle(async ({ logger }) => { … })`
84
+ * Binding is not publishing: a consumer is handed the topic's event types
85
+ * and no connection string it could publish with. Chain `.publishes(…)` when
86
+ * the handler emits follow-up events, which is the only reason it would need
87
+ * one.
88
+ *
89
+ * @example `worker.topic(users).subscribe(['user.created']).handle(…)`
90
+ */
91
+ topic<TTopicName extends string, TEvents extends TopicEvents>(topic: Topic<TTopicName, TEvents>): SubscriberBuilder<[], Logger, undefined, EventPublisher<TopicMessage<TEvents_1>>, string, []>;
92
+ /**
93
+ * A runnable that consumes events typed from a publisher service.
94
+ *
95
+ * The older of the two ways to bind a subscriber, kept for a project whose
96
+ * events come from a hand-written publisher rather than a `Topic`
97
+ * construct. `topic()` is the one to reach for: it types the events the same
98
+ * way and hands the consumer no connection string it could publish with.
99
+ */
100
+ publisher<T extends EventPublisher<any>, TPubName extends string>(service: Service<TPubName, T> | Consumable<TPubName, T>): SubscriberBuilder<[], Logger, undefined, EventPublisher<any>, string, []>;
101
+ /**
102
+ * Everything below builds a function — the runnable with no schedule and no
103
+ * topic, invoked by something else.
104
+ *
105
+ * These sit on the worker rather than behind a `functions` namespace because
106
+ * the namespace was a hop that said nothing: `worker.functions.input(…)`
107
+ * names a collection to reach one member of it. What kind of runnable is
108
+ * being built is decided by which of these is called first — a schedule
109
+ * makes a cron, a topic makes a subscriber, and anything else makes a
110
+ * function.
111
+ */
112
+ input<T extends ComposableStandardSchema>(schema: T): FunctionBuilder<T, undefined, [], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
113
+ output<T extends StandardSchemaV1>(schema: T): FunctionBuilder<ComposableStandardSchema, T, [], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
114
+ timeout(ms: number): FunctionBuilder<ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
115
+ memorySize(mb: number): FunctionBuilder<ComposableStandardSchema, undefined, [], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
116
+ dependsOn<const T extends readonly Consumable[]>(constructs: T): FunctionBuilder<ComposableStandardSchema, undefined, [...ServicesOf<T>], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
117
+ services<T extends Service[]>(services: T): FunctionBuilder<ComposableStandardSchema, undefined, [...T], Logger, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>>;
118
+ /**
119
+ * A function with no input schema — the shortest runnable there is.
120
+ *
121
+ * @example `worker.handle(async ({ logger }) => { … })`
76
122
  */
77
- cron(schedule: ScheduleExpression): CronBuilder<_geekmidas_schema15.ComposableStandardSchema, _geekmidas_services16.Service<string, unknown>[], Logger, undefined, undefined, string, undefined, string>;
123
+ handle(...args: Parameters<ReturnType<Worker<TName>['schemalessFunction']>['handle']>): Function<ComposableStandardSchema, [], Logger, undefined, undefined, string, undefined, string, undefined, string, AuditableAction<string, unknown>, FunctionHandler<ComposableStandardSchema, [], Logger, undefined, undefined, undefined, AuditableAction<string, unknown>>>;
124
+ /** The builder `handle` delegates to, named so its type can be referred to. */
125
+ private schemalessFunction;
126
+ /** The function builder the delegating methods above are built on. */
127
+ private get functions();
78
128
  /**
79
129
  * Resources and surfaces this worker calls.
80
130
  *
@@ -1 +1 @@
1
- {"version":3,"file":"worker.d.mts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;AA2IkC,UAtFjB,YAAA,CAsFiB;EAAA;;;;;;;EA2BX,GArFX,CAAA,EApBL,OAoBK;EAAU;;;;;;;;WAXZ;;;;;;;cAOG;;cAGA,iDACD,WAAW;;;;eAET;;mBAGI;;sBAGG;kBAGf,cAAc,iBACO;0BAEe;;;;;;;;eAoBhC,YApB0C,mBAAA,CAoB1C,wBAAA,EAAA,qBAAA,CAAA,4BAAA;;;;;;;qBAUM,sBAAA;;mBAKF,gBALE,mBAAA,CAKF,wBAAA,iBAAA,iEAAA,kBAAA,CAAA;;;;;;iBASE,qBAAkB,YAAA,mBAAA,CAAA,wBAAA,EAAA,qBAAA,CAAA,4BAAA;;;;;;;6BAUN,eAAe,OAAO;;;;;;aAiBtC"}
1
+ {"version":3,"file":"worker.d.mts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;;;AA2NoD,UA1InC,YAAA,CA0ImC;EAAA;;;;;;;;EAIP,MAI1B,CAAA,EAzIT,MAyIS;EAAA;;;;;;EAIG,SAAA,CAAA,EAtIT,iBAsIS,CAAA,CAAA,CAAA,CAAA;;AAIwC,cAvIjD,MAuIiD,CAAA,cAAA,MAAA,GAAA,MAAA,CAAA,YAtIlD,UAsIkD,CAtIvC,KAsIuC,CAAA,CAAA;EAAC,iBAAA,MAAA;EAAA;EAAA,iBAAA,YAAA;EAAA,SAAA,EAAA,EApIjD,KAoIiD;EAAA;EAAA,SAAA,MAAA,EAjI7C,MAiI6C;EAAA;EAIpC,SAAc,SAAA,EAlIpB,iBAkIoB,CAAA,CAAA,CAAA,CAAA;EAAC;;;;EAAA,SAAA,aAAA,CAAA,EA5HhB,UA4HgB,CAAA,MAAA,EAAA,OAAA,CAAA;EAAA,WAWrB,CAAA,EAAA,EApIf,aAoIe,CApID,KAoIC,CAAA,EAAA,MAAA,CAAA,EAnIM,YAmIN,EAAK;EAAN,YAAjB,CAAA,EAAA,SAjIuC,UAiIvC,EAAA,EAAA,aAAA,CAAA,EAhIe,UAgIf,CAAA,MAAA,EAAA,OAAA,CAAA;EAAU;;;;;;;;;;;;;;AArJS;;;;;;8CAwDZ,WAAW,SAAS,KAC1B,OAAO;;;;;;iBAcK,qBAAkB,YAAA,0BAAA,4BAAA;;;;;;;;;;;mDAcgB,oBACzC,MAAM,YAAY,WAAQ,sBAAA,mBAAA,eAAA,aAAA;;;;;;;;;sBAad,uDACV,QAAQ,UAAU,KAAK,WAAW,UAAU,KAAE,sBAAA,mBAAA;;;;;;;;;;;;kBAkBxC,kCAAkC,IAAC,gBAAA,kBAAA,iEAAA;mBAIlC,0BAA0B,IAAC,gBAAA,0BAAA,OAAA,iEAAA;uBAI1B,gBAAA,yCAAA,iEAAA;0BAIG,gBAAA,yCAAA,iEAAA;qCAIc,0BAA0B,IAAC,gBAAA,yCAAA,WAAA,KAAA,iEAAA;qBAI3C,qBAAqB,IAAC,gBAAA,yCAAA,IAAA,iEAAA;;;;;;kBAU/B,WACR,WAAW,OAAO,2CAClB,SAAA,8BAAA,4EAAA,kCAAA,gBAAA,8BAAA,yCAAA;;;;;;;;;;;6BAqByB,eAAe,OAAO;;;;;;aAsBtC"}