@bgord/bun 1.4.15 → 1.4.17

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/bun",
3
- "version": "1.4.15",
3
+ "version": "1.4.17",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": "Bartosz Gordon",
package/readme.md CHANGED
@@ -135,6 +135,7 @@ src/
135
135
  ├── image-resizer.port.ts
136
136
  ├── invariant-error-handler.service.ts
137
137
  ├── invariant.service.ts
138
+ ├── job-handler.service.ts
138
139
  ├── jobs.service.ts
139
140
  ├── json-file-reader-bun-forgiving.adapter.ts
140
141
  ├── json-file-reader-bun.adapter.ts
@@ -1,6 +1,8 @@
1
1
  import type { z } from "zod/v4";
2
2
  import { NodeEnvironment, type NodeEnvironmentEnum } from "../src/node-env.vo";
3
3
 
4
+ export type EnvironmentResultType<Schema> = z.infer<Schema> & { type: NodeEnvironmentEnum };
5
+
4
6
  export class EnvironmentValidator<Schema extends z.ZodObject<any>> {
5
7
  private readonly type: NodeEnvironmentEnum;
6
8
  private readonly schema: Schema;
@@ -10,7 +12,7 @@ export class EnvironmentValidator<Schema extends z.ZodObject<any>> {
10
12
  this.type = NodeEnvironment.parse(config.type);
11
13
  }
12
14
 
13
- load(env: NodeJS.ProcessEnv): z.infer<Schema> & { type: NodeEnvironmentEnum } {
15
+ load(env: NodeJS.ProcessEnv): EnvironmentResultType<Schema> {
14
16
  return { ...this.schema.parse(env), type: this.type };
15
17
  }
16
18
  }
package/src/index.ts CHANGED
@@ -110,6 +110,7 @@ export * from "./image-resizer-noop.adapter";
110
110
  export * from "./image-resizer-sharp.adapter";
111
111
  export * from "./invariant.service";
112
112
  export * from "./invariant-error-handler.service";
113
+ export * from "./job-handler.service";
113
114
  export * from "./jobs.service";
114
115
  export * from "./json-file-reader.port";
115
116
  export * from "./json-file-reader-bun.adapter";
@@ -0,0 +1,54 @@
1
+ import * as tools from "@bgord/tools";
2
+ import type { Cron } from "croner";
3
+ import type { ClockPort } from "./clock.port";
4
+ import { CorrelationStorage } from "./correlation-storage.service";
5
+ import type { IdProviderPort } from "./id-provider.port";
6
+ import type { JobNameType } from "./jobs.service";
7
+ import type { LoggerPort } from "./logger.port";
8
+ import { formatError } from "./logger-format-error.service";
9
+
10
+ type Dependencies = { Logger: LoggerPort; IdProvider: IdProviderPort; Clock: ClockPort };
11
+
12
+ export interface UnitOfWork {
13
+ label: JobNameType;
14
+ process: () => Promise<void>;
15
+ }
16
+
17
+ export class JobHandler {
18
+ private readonly base = { component: "infra", operation: "job_handler" };
19
+
20
+ constructor(private readonly deps: Dependencies) {}
21
+
22
+ handle(uow: UnitOfWork) {
23
+ const correlationId = this.deps.IdProvider.generate();
24
+
25
+ return async () => {
26
+ const stopwatch = new tools.Stopwatch(this.deps.Clock.now());
27
+
28
+ try {
29
+ this.deps.Logger.info({ message: `${uow.label} start`, correlationId, ...this.base });
30
+
31
+ await CorrelationStorage.run(correlationId, uow.process);
32
+
33
+ this.deps.Logger.info({
34
+ message: `${uow.label} success`,
35
+ correlationId,
36
+ metadata: stopwatch.stop(),
37
+ ...this.base,
38
+ });
39
+ } catch (error) {
40
+ this.deps.Logger.error({
41
+ message: `${uow.label} error`,
42
+ correlationId,
43
+ error: formatError(error),
44
+ metadata: { ...stopwatch.stop() },
45
+ ...this.base,
46
+ });
47
+ }
48
+ };
49
+ }
50
+
51
+ protect(cron: Cron) {
52
+ return async () => this.deps.Logger.info({ message: `${cron.name} overrun`, ...this.base });
53
+ }
54
+ }
@@ -1,16 +1,7 @@
1
- import * as tools from "@bgord/tools";
2
1
  import type { Cron } from "croner";
3
- import type { ClockPort } from "./clock.port";
4
- import { CorrelationStorage } from "./correlation-storage.service";
5
- import type { IdProviderPort } from "./id-provider.port";
6
- import type { LoggerPort } from "./logger.port";
7
- import { formatError } from "./logger-format-error.service";
8
2
 
9
3
  export type JobNameType = string;
10
4
  export type MultipleJobsType = Record<JobNameType, Cron>;
11
- export type JobProcessorType = { cron: string; label: JobNameType; process: () => Promise<void> };
12
-
13
- type Dependencies = { Logger: LoggerPort; IdProvider: IdProviderPort; Clock: ClockPort };
14
5
 
15
6
  export class Jobs {
16
7
  static SCHEDULES = { EVERY_MINUTE: "* * * * *", EVERY_HOUR: "0 * * * *" };
@@ -23,48 +14,3 @@ export class Jobs {
23
14
  return Object.values(jobs).every((job) => job.isRunning());
24
15
  }
25
16
  }
26
-
27
- export class JobHandler {
28
- private readonly base = { component: "infra", operation: "job_handler" };
29
-
30
- constructor(private readonly deps: Dependencies) {}
31
-
32
- handle(jobProcessor: JobProcessorType) {
33
- const correlationId = this.deps.IdProvider.generate();
34
-
35
- // biome-ignore lint: lint/complexity/noUselessThisAlias
36
- const that = this;
37
-
38
- return async () => {
39
- const stopwatch = new tools.Stopwatch(this.deps.Clock.now());
40
-
41
- try {
42
- that.deps.Logger.info({ message: `${jobProcessor.label} start`, correlationId, ...this.base });
43
-
44
- await CorrelationStorage.run(correlationId, jobProcessor.process);
45
-
46
- that.deps.Logger.info({
47
- message: `${jobProcessor.label} success`,
48
- correlationId,
49
- metadata: stopwatch.stop(),
50
- ...this.base,
51
- });
52
- } catch (error) {
53
- that.deps.Logger.error({
54
- message: `${jobProcessor.label} error`,
55
- correlationId,
56
- error: formatError(error),
57
- metadata: { ...stopwatch.stop() },
58
- ...this.base,
59
- });
60
- }
61
- };
62
- }
63
-
64
- protect(cron: Cron) {
65
- // biome-ignore lint: lint/complexity/noUselessThisAlias
66
- const that = this;
67
-
68
- return async () => that.deps.Logger.info({ message: `${cron.name} overrun`, ...this.base });
69
- }
70
- }
@@ -8,20 +8,20 @@ export class PrerequisiteJobs implements prereqs.Prerequisite {
8
8
  readonly label: prereqs.PrerequisiteLabelType;
9
9
  readonly enabled?: boolean = true;
10
10
 
11
- private readonly jobs: MultipleJobsType;
11
+ private readonly Jobs: MultipleJobsType;
12
12
 
13
- constructor(config: prereqs.PrerequisiteConfigType & { jobs: MultipleJobsType }) {
13
+ constructor(config: prereqs.PrerequisiteConfigType & { Jobs: MultipleJobsType }) {
14
14
  this.label = config.label;
15
15
  this.enabled = config.enabled === undefined ? true : config.enabled;
16
16
 
17
- this.jobs = config.jobs;
17
+ this.Jobs = config.Jobs;
18
18
  }
19
19
 
20
20
  async verify(clock: ClockPort): Promise<prereqs.VerifyOutcome> {
21
21
  const stopwatch = new tools.Stopwatch(clock.now());
22
22
 
23
23
  if (!this.enabled) return prereqs.Verification.undetermined(stopwatch.stop());
24
- if (Jobs.areAllRunning(this.jobs)) return prereqs.Verification.success(stopwatch.stop());
24
+ if (Jobs.areAllRunning(this.Jobs)) return prereqs.Verification.success(stopwatch.stop());
25
25
  return prereqs.Verification.failure(stopwatch.stop());
26
26
  }
27
27
  }