franchising 0.6.0 → 0.8.0

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/README.md +13 -72
  2. package/dist/{accounts-TyMPSHM0.cjs → accounts-BscmRiXc.cjs} +26 -1
  3. package/dist/{accounts-TyMPSHM0.cjs.map → accounts-BscmRiXc.cjs.map} +1 -1
  4. package/dist/{accounts-B4bnTXUd.js → accounts-DJRg4gXj.js} +26 -1
  5. package/dist/{accounts-B4bnTXUd.js.map → accounts-DJRg4gXj.js.map} +1 -1
  6. package/dist/cli/template-assets/api/src/main.ts +13 -11
  7. package/dist/cli/template-assets/api/src/modules/health/controller.ts +25 -4
  8. package/dist/cli/template-assets/api/src/shared/http/errors.ts +17 -0
  9. package/dist/cli/template-assets/cloudflare/cache.service.ts +1 -0
  10. package/dist/cli/template-assets/cloudflare/queue.service.ts +1 -0
  11. package/dist/cli/template-assets/cloudflare/worker.ts +4 -0
  12. package/dist/cli/template-assets/github/PULL_REQUEST_TEMPLATE.md +37 -0
  13. package/dist/cli/template-assets/observability/adapters.ts +37 -0
  14. package/dist/cli/template-assets/observability/bootstrap.ts +48 -0
  15. package/dist/cli/template-assets/observability/env.ts +21 -0
  16. package/dist/cli/template-assets/observability/index.ts +10 -0
  17. package/dist/cli/template-assets/observability/lifecycle.ts +97 -0
  18. package/dist/cli/template-assets/observability/local-index.ts +9 -0
  19. package/dist/cli/template-assets/observability/register.ts +12 -0
  20. package/dist/cli/template-assets/packages/jobs/src/index.ts +4 -40
  21. package/dist/cli/template-assets/packages/jobs/src/shared/queue/job-id.ts +9 -0
  22. package/dist/cli/template-assets/packages/jobs/src/shared/queue/options.ts +7 -0
  23. package/dist/cli/template-assets/packages/jobs/src/shared/queue/prefix.ts +18 -0
  24. package/dist/cli/template-assets/packages/queue/src/index.ts +4 -0
  25. package/dist/cli/template-assets/packages/queue/src/shared/errors/failure-reason.ts +7 -0
  26. package/dist/cli/template-assets/packages/{queue-runtime/src/index.ts → queue/src/shared/redis/connection.ts} +2 -15
  27. package/dist/cli/template-assets/packages/queue/src/types/redis/connection.ts +6 -0
  28. package/dist/cli/template-assets/packages/webhooks/src/index.ts +11 -0
  29. package/dist/cli/template-assets/packages/webhooks/src/shared/crypto/secret.ts +81 -0
  30. package/dist/cli/template-assets/packages/webhooks/src/shared/signing/headers.ts +43 -0
  31. package/dist/cli/template-assets/packages/webhooks/src/shared/url/deliverable.ts +58 -0
  32. package/dist/cli/template-assets/packages/webhooks/src/types/webhooks/job.ts +18 -0
  33. package/dist/cli/template-assets/packages/webhooks/test/webhooks.test.ts.tpl +69 -0
  34. package/dist/cli/template-assets/root/dot-dockerignore +88 -8
  35. package/dist/cli/template-assets/root/dot-oxlintrc.json +22 -0
  36. package/dist/cli/template-assets/root/lefthook.yml +32 -8
  37. package/dist/cli/template-assets/web/src/app/(app)/layout.tsx +2 -3
  38. package/dist/cli/template-assets/web/src/app/(auth)/login/page.tsx +1 -1
  39. package/dist/cli/template-assets/web/src/app/page.tsx +1 -0
  40. package/dist/cli/template-assets/web/src/{lib/api-client.ts → shared/api/client.ts} +11 -8
  41. package/dist/cli/template-assets/web/src/{lib/server-auth.ts → shared/auth/server.ts} +1 -2
  42. package/dist/cli/template-assets/web/src/types/api.ts +0 -15
  43. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/controller.ts +81 -0
  44. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/create-endpoint.dto.ts +25 -0
  45. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/endpoint.dto.ts +28 -0
  46. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/module.ts +27 -0
  47. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/repository.ts +147 -0
  48. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/service.ts +119 -0
  49. package/dist/cli/template-assets/webhooks/api/src/types/webhooks/endpoint.ts +26 -0
  50. package/dist/cli/template-assets/webhooks/worker/src/env.ts +18 -0
  51. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/dispatcher.ts +119 -0
  52. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/module.ts +16 -0
  53. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/processor.ts +176 -0
  54. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/repository.ts +187 -0
  55. package/dist/cli/template-assets/webhooks/worker/src/shared/http/deliver.ts +41 -0
  56. package/dist/cli/template-assets/webhooks/worker/src/types/webhooks/dispatch.ts +26 -0
  57. package/dist/cli/template-assets/worker/src/app.module.ts +14 -0
  58. package/dist/cli/template-assets/worker/src/main.ts +24 -0
  59. package/dist/cli/template-assets/worker/src/modules/queue/processor.ts +26 -3
  60. package/dist/cli/template-assets/worker/src/modules/queue/retry.ts +22 -7
  61. package/dist/cli/template-assets/worker/test/retry.test.ts.tpl +13 -1
  62. package/dist/cli/template-assets/worker-messaging/messaging-api-service.ts +6 -6
  63. package/dist/cli/template-assets/worker-messaging/src/env.ts +1 -1
  64. package/dist/cli/template-assets/worker-messaging/src/modules/messaging/service.ts +2 -4
  65. package/dist/cli/template-assets/worker-messaging/src/modules/queue/module.ts +1 -1
  66. package/dist/cli/template-assets/worker-messaging/src/modules/queue/processor.ts +32 -10
  67. package/dist/cli.js +631 -314
  68. package/dist/{index-CNlb6WNr.d.ts → index-Bl4Ka1Wr.d.ts} +2 -2
  69. package/dist/{index-I_Nx5SsQ.d.cts → index-CBue43h0.d.cts} +2 -2
  70. package/dist/{index-B_byXBRW.d.ts → index-cKCKFKLi.d.cts} +181 -5
  71. package/dist/{index-B_byXBRW.d.cts → index-cKCKFKLi.d.ts} +181 -5
  72. package/dist/index.cjs +1 -1
  73. package/dist/index.d.cts +1 -1
  74. package/dist/index.d.ts +1 -1
  75. package/dist/index.js +1 -1
  76. package/dist/messaging/index.cjs +1 -1
  77. package/dist/messaging/index.d.cts +1 -1
  78. package/dist/messaging/index.d.ts +1 -1
  79. package/dist/messaging/index.js +1 -1
  80. package/dist/sso/client.d.cts +2 -2
  81. package/dist/sso/client.d.ts +2 -2
  82. package/dist/sso/index.cjs +72 -1
  83. package/dist/sso/index.cjs.map +1 -1
  84. package/dist/sso/index.d.cts +2 -2
  85. package/dist/sso/index.d.ts +2 -2
  86. package/dist/sso/index.js +72 -1
  87. package/dist/sso/index.js.map +1 -1
  88. package/dist/sso/nestjs.cjs +2 -2
  89. package/dist/sso/nestjs.d.cts +2 -2
  90. package/dist/sso/nestjs.d.ts +2 -2
  91. package/dist/sso/nestjs.js +2 -2
  92. package/dist/sso/resource.cjs +2 -2
  93. package/dist/sso/resource.d.cts +3 -3
  94. package/dist/sso/resource.d.ts +3 -3
  95. package/dist/sso/resource.js +2 -2
  96. package/dist/{verifier-BIBtPCVi.cjs → verifier-BYrFb8tg.cjs} +2 -2
  97. package/dist/{verifier-BIBtPCVi.cjs.map → verifier-BYrFb8tg.cjs.map} +1 -1
  98. package/dist/{verifier-DNdy5Ynw.js → verifier-DSvtdWuY.js} +2 -2
  99. package/dist/{verifier-DNdy5Ynw.js.map → verifier-DSvtdWuY.js.map} +1 -1
  100. package/dist/version-8Zc_uUvG.js +1 -0
  101. package/dist/{version-DDLRsahz.cjs → version-BMXokpna.cjs} +1 -1
  102. package/package.json +1 -1
  103. package/dist/cli/template-assets/api/src/errors/codes.ts +0 -17
  104. package/dist/cli/template-assets/api/src/errors/index.ts +0 -16
  105. package/dist/cli/template-assets/api/src/filters/exception.ts +0 -53
  106. package/dist/cli/template-assets/api/src/shared/exceptions.ts +0 -40
  107. package/dist/cli/template-assets/api/src/shared/response.ts +0 -31
  108. package/dist/cli/template-assets/api/src/telemetry.ts +0 -59
  109. package/dist/version-DHlIo_M1.js +0 -1
  110. /package/dist/cli/template-assets/web/src/{lib/auth-client.ts → shared/auth/client.ts} +0 -0
@@ -0,0 +1,119 @@
1
+ import {
2
+ Injectable,
3
+ Logger,
4
+ type OnModuleDestroy,
5
+ type OnModuleInit,
6
+ } from '@nestjs/common';
7
+ import { Queue } from 'bullmq';
8
+ import {
9
+ __ROCK_WORKER_QUEUE_CONST__,
10
+ DEFAULT_JOB_OPTIONS,
11
+ jobId,
12
+ queuePrefix,
13
+ } from '__ROCK_SCOPE__/jobs';
14
+ import {
15
+ createProducerRedisConnection,
16
+ failureReason,
17
+ type QueueRedisConnection,
18
+ } from '__ROCK_SCOPE__/queue';
19
+ import type { WebhookDeliveryJob } from '__ROCK_SCOPE__/webhooks';
20
+ import { env } from '../../env';
21
+ import type { DispatchedEvent } from '../../types/webhooks/dispatch';
22
+ import { WebhooksRepository } from './repository';
23
+
24
+ /**
25
+ * Leva os eventos do outbox para a fila de entrega.
26
+ *
27
+ * @remarks
28
+ * A API so grava no banco, na transacao do negocio; este loop e quem
29
+ * enfileira. Assim um evento nunca se perde por o Redis estar fora no momento
30
+ * da escrita, e nunca sai um evento de uma transacao que nao foi gravada. Um
31
+ * job por endpoint inscrito: cada destino retenta no proprio ritmo, sem um
32
+ * receptor lento atrasar os outros.
33
+ */
34
+ @Injectable()
35
+ export class WebhooksDispatcher implements OnModuleInit, OnModuleDestroy {
36
+ private readonly logger = new Logger(WebhooksDispatcher.name);
37
+ private connection?: QueueRedisConnection;
38
+ private queue?: Queue<WebhookDeliveryJob>;
39
+ private timer?: NodeJS.Timeout;
40
+ private running?: Promise<void>;
41
+ private stopped = false;
42
+
43
+ constructor(private readonly repository: WebhooksRepository) {}
44
+
45
+ onModuleInit(): void {
46
+ this.connection = createProducerRedisConnection(env.QUEUE_REDIS_URL);
47
+
48
+ this.queue = new Queue<WebhookDeliveryJob>(__ROCK_WORKER_QUEUE_CONST__, {
49
+ connection: this.connection.client,
50
+ prefix: queuePrefix(env.QUEUE_PREFIX, __ROCK_WORKER_QUEUE_CONST__),
51
+ defaultJobOptions: DEFAULT_JOB_OPTIONS,
52
+ });
53
+
54
+ this.schedule(0);
55
+ }
56
+
57
+ async onModuleDestroy(): Promise<void> {
58
+ this.stopped = true;
59
+
60
+ clearTimeout(this.timer);
61
+
62
+ await this.running;
63
+ await this.queue?.close();
64
+ await this.connection?.close();
65
+ }
66
+
67
+ /* `setTimeout` encadeado, e nao `setInterval`: uma rodada lenta nunca se sobrepoe a outra. */
68
+ private schedule(delay: number): void {
69
+ if (this.stopped) return;
70
+
71
+ this.timer = setTimeout(() => {
72
+ this.running = this.tick();
73
+ }, delay);
74
+ }
75
+
76
+ private async tick(): Promise<void> {
77
+ let dispatched = 0;
78
+
79
+ try {
80
+ dispatched = await this.repository.dispatchBatch(
81
+ env.WEBHOOKS_DISPATCH_BATCH_SIZE,
82
+ (events) => this.enqueue(events),
83
+ );
84
+ } catch (error) {
85
+ this.logger.error(
86
+ JSON.stringify({
87
+ event: 'webhooks.dispatch_failed',
88
+ reason: failureReason(error),
89
+ }),
90
+ );
91
+ }
92
+
93
+ /* Lote cheio: ainda ha evento esperando, entao a proxima rodada e ja. */
94
+ this.schedule(
95
+ dispatched >= env.WEBHOOKS_DISPATCH_BATCH_SIZE
96
+ ? 0
97
+ : env.WEBHOOKS_DISPATCH_INTERVAL_MS,
98
+ );
99
+ }
100
+
101
+ private async enqueue(events: DispatchedEvent[]): Promise<void> {
102
+ const jobs = events.flatMap((event) =>
103
+ event.endpointIds.map((endpointId) => ({
104
+ name: event.eventType,
105
+ data: {
106
+ messageId: event.id,
107
+ endpointId,
108
+ eventType: event.eventType,
109
+ payload: event.payload,
110
+ occurredAt: event.createdAt.toISOString(),
111
+ },
112
+ /* Mesmo evento e endpoint, mesmo job: reenfileirar nao duplica a entrega. */
113
+ opts: { jobId: jobId(['webhook', event.id, endpointId]) },
114
+ })),
115
+ );
116
+
117
+ if (jobs.length > 0) await this.queue?.addBulk(jobs);
118
+ }
119
+ }
@@ -0,0 +1,16 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { DatabaseService } from '__ROCK_SCOPE__/database';
3
+ import { WebhooksDispatcher } from './dispatcher';
4
+ import { __ROCK_WORKER_CLASS__Processor } from './processor';
5
+ import { WebhooksRepository } from './repository';
6
+
7
+ @Module({
8
+ providers: [
9
+ /* O service do package nao e um provider Nest: o worker cria o seu. */
10
+ { provide: DatabaseService, useFactory: () => new DatabaseService() },
11
+ WebhooksRepository,
12
+ WebhooksDispatcher,
13
+ __ROCK_WORKER_CLASS__Processor,
14
+ ],
15
+ })
16
+ export class __ROCK_WORKER_CLASS__Module {}
@@ -0,0 +1,176 @@
1
+ import {
2
+ Injectable,
3
+ // #if !telemetry
4
+ Logger,
5
+ // #endif
6
+ type OnModuleDestroy,
7
+ type OnModuleInit,
8
+ } from '@nestjs/common';
9
+ import { type Job, UnrecoverableError, Worker } from 'bullmq';
10
+ // #if telemetry
11
+ import { Telemetry } from 'franchising/telemetry';
12
+ import { observeWorker, runObservedJob } from '__ROCK_SCOPE__/observability';
13
+ // #endif
14
+ import { __ROCK_WORKER_QUEUE_CONST__, queuePrefix } from '__ROCK_SCOPE__/jobs';
15
+ import {
16
+ createWorkerRedisConnection,
17
+ failureReason,
18
+ type QueueRedisConnection,
19
+ } from '__ROCK_SCOPE__/queue';
20
+ import {
21
+ isDeliverableUrl,
22
+ openWebhookSecret,
23
+ signWebhook,
24
+ type WebhookDeliveryJob,
25
+ } from '__ROCK_SCOPE__/webhooks';
26
+ import { env } from '../../env';
27
+ import { deliverWebhook, WebhookDeliveryError } from '../../shared/http/deliver';
28
+ import {
29
+ LOCK_DURATION_MS,
30
+ MAX_STALLED_COUNT,
31
+ STALLED_INTERVAL_MS,
32
+ } from './config';
33
+ import { WebhooksRepository } from './repository';
34
+ import { isRetryable } from './retry';
35
+
36
+ /**
37
+ * Entrega cada webhook ao endpoint inscrito.
38
+ *
39
+ * @remarks
40
+ * O endpoint e lido de novo a cada tentativa: desativado ou com segredo
41
+ * rotacionado no meio do caminho, a proxima tentativa ja respeita. A
42
+ * politica de retentativa e a do `DEFAULT_JOB_OPTIONS`; 4xx (fora 408 e 429)
43
+ * desiste na hora, porque o receptor recusou o conteudo e repetir nao muda
44
+ * isso.
45
+ */
46
+ @Injectable()
47
+ export class __ROCK_WORKER_CLASS__Processor
48
+ implements OnModuleInit, OnModuleDestroy
49
+ {
50
+ // #if !telemetry
51
+ private readonly logger = new Logger(__ROCK_WORKER_CLASS__Processor.name);
52
+ // #endif
53
+ private worker?: Worker<WebhookDeliveryJob>;
54
+ private connection?: QueueRedisConnection;
55
+
56
+ // #if telemetry
57
+ constructor(
58
+ private readonly repository: WebhooksRepository,
59
+ private readonly telemetry: Telemetry,
60
+ ) {}
61
+ // #else
62
+ constructor(private readonly repository: WebhooksRepository) {}
63
+ // #endif
64
+
65
+ onModuleInit(): void {
66
+ this.connection = createWorkerRedisConnection(env.QUEUE_REDIS_URL);
67
+
68
+ this.worker = new Worker<WebhookDeliveryJob>(
69
+ __ROCK_WORKER_QUEUE_CONST__,
70
+ // #if telemetry
71
+ (job) => runObservedJob(this.telemetry, job, () => this.process(job)),
72
+ // #else
73
+ (job) => this.process(job),
74
+ // #endif
75
+ {
76
+ connection: this.connection.client,
77
+ prefix: queuePrefix(env.QUEUE_PREFIX, __ROCK_WORKER_QUEUE_CONST__),
78
+ concurrency: env.__ROCK_WORKER_CONCURRENCY_ENV__,
79
+ lockDuration: LOCK_DURATION_MS,
80
+ stalledInterval: STALLED_INTERVAL_MS,
81
+ maxStalledCount: MAX_STALLED_COUNT,
82
+ },
83
+ );
84
+
85
+ // #if telemetry
86
+ observeWorker(this.worker, this.telemetry);
87
+ // #else
88
+ this.worker.on('failed', (job, error) => {
89
+ this.logger.error(
90
+ JSON.stringify({
91
+ event: '__ROCK_WORKER_QUEUE__.failed',
92
+ jobId: job?.id,
93
+ attempts: job?.attemptsMade,
94
+ reason: failureReason(error),
95
+ }),
96
+ );
97
+ });
98
+
99
+ this.worker.on('error', (error) => {
100
+ this.logger.error(
101
+ JSON.stringify({
102
+ event: '__ROCK_WORKER_QUEUE__.error',
103
+ reason: failureReason(error),
104
+ }),
105
+ );
106
+ });
107
+ // #endif
108
+ }
109
+
110
+ async onModuleDestroy(): Promise<void> {
111
+ await this.worker?.close();
112
+ await this.connection?.close();
113
+ }
114
+
115
+ private async process(job: Job<WebhookDeliveryJob>): Promise<void> {
116
+ const delivery = job.data;
117
+ const endpoint = await this.repository.findEndpoint(delivery.endpointId);
118
+
119
+ /* Endpoint removido ou desativado depois do evento: nada a entregar. */
120
+ if (!endpoint?.enabled) return;
121
+
122
+ const attempt = {
123
+ endpointId: delivery.endpointId,
124
+ messageId: delivery.messageId,
125
+ eventType: delivery.eventType,
126
+ attempt: job.attemptsMade + 1,
127
+ };
128
+
129
+ try {
130
+ /* Checada de novo aqui: a URL pode ter sido gravada antes da regra. */
131
+ if (!isDeliverableUrl(endpoint.url, env.isProduction))
132
+ throw new TypeError('WEBHOOK_URL_NOT_ALLOWED');
133
+
134
+ const { body, headers } = signWebhook({
135
+ secret: openWebhookSecret(endpoint.secret, env.WEBHOOK_SECRETS_KEY),
136
+ messageId: delivery.messageId,
137
+ eventType: delivery.eventType,
138
+ payload: delivery.payload,
139
+ occurredAt: delivery.occurredAt,
140
+ });
141
+
142
+ const status = await deliverWebhook({
143
+ url: endpoint.url,
144
+ body,
145
+ headers,
146
+ timeoutMs: env.WEBHOOKS_TIMEOUT_MS,
147
+ // #if telemetry
148
+ fetch: (url, init) =>
149
+ this.telemetry.fetch(url, init, { dependency: 'webhooks' }),
150
+ // #endif
151
+ });
152
+
153
+ if (status < 200 || status >= 300) throw new WebhookDeliveryError(status);
154
+
155
+ await this.repository.recordDelivery({
156
+ ...attempt,
157
+ status: 'succeeded',
158
+ responseStatus: status,
159
+ error: null,
160
+ });
161
+ } catch (error) {
162
+ await this.repository.recordDelivery({
163
+ ...attempt,
164
+ status: 'failed',
165
+ responseStatus:
166
+ error instanceof WebhookDeliveryError ? error.status : null,
167
+ error: failureReason(error),
168
+ });
169
+
170
+ if (!isRetryable(error))
171
+ throw new UnrecoverableError(failureReason(error));
172
+
173
+ throw error;
174
+ }
175
+ }
176
+ }
@@ -0,0 +1,187 @@
1
+ import { Injectable, type OnModuleDestroy } from '@nestjs/common';
2
+ // #if drizzle
3
+ import { and, arrayContains, asc, eq, inArray, isNull } from 'drizzle-orm';
4
+ import {
5
+ DatabaseService,
6
+ webhookDeliveries,
7
+ webhookEndpoints,
8
+ webhookOutbox,
9
+ } from '__ROCK_SCOPE__/database';
10
+ // #else
11
+ import { DatabaseService } from '__ROCK_SCOPE__/database';
12
+ // #endif
13
+ import type {
14
+ DeliveryAttempt,
15
+ DeliveryEndpoint,
16
+ DispatchedEvent,
17
+ } from '../../types/webhooks/dispatch';
18
+
19
+ // #if prisma
20
+ type OutboxRow = {
21
+ id: string;
22
+ ownerId: string;
23
+ eventType: string;
24
+ payload: unknown;
25
+ createdAt: Date;
26
+ };
27
+
28
+ // #endif
29
+ /**
30
+ * Outbox, endpoints e registro de entregas.
31
+ *
32
+ * @remarks
33
+ * `dispatchBatch` reserva o lote com `FOR UPDATE SKIP LOCKED`: duas replicas
34
+ * do worker pegam lotes diferentes, sem fila extra e sem lock global. O
35
+ * enfileiramento roda dentro da mesma transacao; se o Redis falhar, nada e
36
+ * marcado como despachado e o lote volta para a proxima rodada. O id de job
37
+ * estavel impede que esse retorno duplique entrega.
38
+ */
39
+ @Injectable()
40
+ export class WebhooksRepository implements OnModuleDestroy {
41
+ constructor(private readonly database: DatabaseService) {}
42
+
43
+ // #if drizzle
44
+ dispatchBatch(
45
+ limit: number,
46
+ enqueue: (events: DispatchedEvent[]) => Promise<void>,
47
+ ): Promise<number> {
48
+ return this.database.client.transaction(async (tx) => {
49
+ const events = await tx
50
+ .select()
51
+ .from(webhookOutbox)
52
+ .where(isNull(webhookOutbox.dispatchedAt))
53
+ .orderBy(asc(webhookOutbox.createdAt))
54
+ .limit(limit)
55
+ .for('update', { skipLocked: true });
56
+
57
+ if (events.length === 0) return 0;
58
+
59
+ const dispatched: DispatchedEvent[] = [];
60
+
61
+ for (const event of events) {
62
+ const endpoints = await tx
63
+ .select({ id: webhookEndpoints.id })
64
+ .from(webhookEndpoints)
65
+ .where(
66
+ and(
67
+ eq(webhookEndpoints.ownerId, event.ownerId),
68
+ eq(webhookEndpoints.enabled, true),
69
+ arrayContains(webhookEndpoints.eventTypes, [
70
+ event.eventType,
71
+ ]),
72
+ ),
73
+ );
74
+
75
+ dispatched.push({
76
+ id: event.id,
77
+ eventType: event.eventType,
78
+ payload: event.payload,
79
+ createdAt: event.createdAt,
80
+ endpointIds: endpoints.map((endpoint) => endpoint.id),
81
+ });
82
+ }
83
+
84
+ await enqueue(dispatched);
85
+
86
+ await tx
87
+ .update(webhookOutbox)
88
+ .set({ dispatchedAt: new Date() })
89
+ .where(
90
+ inArray(
91
+ webhookOutbox.id,
92
+ events.map((event) => event.id),
93
+ ),
94
+ );
95
+
96
+ return events.length;
97
+ });
98
+ }
99
+
100
+ async findEndpoint(id: string): Promise<DeliveryEndpoint | undefined> {
101
+ const [endpoint] = await this.database.client
102
+ .select({
103
+ url: webhookEndpoints.url,
104
+ secret: webhookEndpoints.secret,
105
+ enabled: webhookEndpoints.enabled,
106
+ })
107
+ .from(webhookEndpoints)
108
+ .where(eq(webhookEndpoints.id, id));
109
+
110
+ return endpoint;
111
+ }
112
+
113
+ async recordDelivery(attempt: DeliveryAttempt): Promise<void> {
114
+ await this.database.client.insert(webhookDeliveries).values(attempt);
115
+ }
116
+ // #else
117
+ dispatchBatch(
118
+ limit: number,
119
+ enqueue: (events: DispatchedEvent[]) => Promise<void>,
120
+ ): Promise<number> {
121
+ return this.database.client.$transaction(
122
+ async (tx) => {
123
+ const events = await tx.$queryRaw<OutboxRow[]>`
124
+ SELECT id, owner_id AS "ownerId", event_type AS "eventType",
125
+ payload, created_at AS "createdAt"
126
+ FROM webhook_outbox
127
+ WHERE dispatched_at IS NULL
128
+ ORDER BY created_at
129
+ LIMIT ${limit}
130
+ FOR UPDATE SKIP LOCKED
131
+ `;
132
+
133
+ if (events.length === 0) return 0;
134
+
135
+ const dispatched: DispatchedEvent[] = [];
136
+
137
+ for (const event of events) {
138
+ const endpoints = await tx.webhookEndpoint.findMany({
139
+ where: {
140
+ ownerId: event.ownerId,
141
+ enabled: true,
142
+ eventTypes: { has: event.eventType },
143
+ },
144
+ select: { id: true },
145
+ });
146
+
147
+ dispatched.push({
148
+ id: event.id,
149
+ eventType: event.eventType,
150
+ payload: event.payload,
151
+ createdAt: event.createdAt,
152
+ endpointIds: endpoints.map((endpoint) => endpoint.id),
153
+ });
154
+ }
155
+
156
+ await enqueue(dispatched);
157
+
158
+ await tx.webhookOutbox.updateMany({
159
+ where: { id: { in: events.map((event) => event.id) } },
160
+ data: { dispatchedAt: new Date() },
161
+ });
162
+
163
+ return events.length;
164
+ },
165
+ /* O enfileiramento no Redis roda dentro da transacao. */
166
+ { timeout: 30_000 },
167
+ );
168
+ }
169
+
170
+ async findEndpoint(id: string): Promise<DeliveryEndpoint | undefined> {
171
+ const endpoint = await this.database.client.webhookEndpoint.findUnique({
172
+ where: { id },
173
+ select: { url: true, secret: true, enabled: true },
174
+ });
175
+
176
+ return endpoint ?? void 0;
177
+ }
178
+
179
+ async recordDelivery(attempt: DeliveryAttempt): Promise<void> {
180
+ await this.database.client.webhookDelivery.create({ data: attempt });
181
+ }
182
+ // #endif
183
+
184
+ async onModuleDestroy(): Promise<void> {
185
+ await this.database.disconnect();
186
+ }
187
+ }
@@ -0,0 +1,41 @@
1
+ import type { WebhookHeaders } from '__ROCK_SCOPE__/webhooks';
2
+
3
+ /** Resposta fora de 2xx; `status` decide a retentativa em `isRetryable`. */
4
+ export class WebhookDeliveryError extends Error {
5
+ override readonly name = 'WebhookDeliveryError';
6
+
7
+ constructor(readonly status: number) {
8
+ super(`WEBHOOK_RESPONSE_${status}`);
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Faz o POST e devolve o status.
14
+ *
15
+ * @remarks
16
+ * `redirect: 'manual'`: seguir redirecionamento deixaria o receptor mandar o
17
+ * worker para a rede interna, contornando a checagem da URL; um 3xx conta
18
+ * como falha. O corpo da resposta e descartado, porque so o status importa
19
+ * e um receptor lento nao deve segurar a conexao.
20
+ */
21
+ export async function deliverWebhook(input: {
22
+ url: string;
23
+ body: string;
24
+ headers: WebhookHeaders;
25
+ timeoutMs: number;
26
+ fetch?: ((url: string, init: RequestInit) => Promise<Response>) | undefined;
27
+ }): Promise<number> {
28
+ const send = input.fetch ?? fetch;
29
+
30
+ const response = await send(input.url, {
31
+ method: 'POST',
32
+ headers: input.headers,
33
+ body: input.body,
34
+ redirect: 'manual',
35
+ signal: AbortSignal.timeout(input.timeoutMs),
36
+ });
37
+
38
+ await response.body?.cancel();
39
+
40
+ return response.status;
41
+ }
@@ -0,0 +1,26 @@
1
+ /** Evento reservado do outbox, com os endpoints inscritos nele. */
2
+ export type DispatchedEvent = {
3
+ id: string;
4
+ eventType: string;
5
+ payload: unknown;
6
+ createdAt: Date;
7
+ endpointIds: string[];
8
+ };
9
+
10
+ /** O que a entrega precisa do endpoint, lido de novo a cada tentativa. */
11
+ export type DeliveryEndpoint = {
12
+ url: string;
13
+ secret: string;
14
+ enabled: boolean;
15
+ };
16
+
17
+ /** Uma tentativa, gravada em `webhook_deliveries`. */
18
+ export type DeliveryAttempt = {
19
+ endpointId: string;
20
+ messageId: string;
21
+ eventType: string;
22
+ attempt: number;
23
+ status: 'succeeded' | 'failed';
24
+ responseStatus: number | null;
25
+ error: string | null;
26
+ };
@@ -1,5 +1,19 @@
1
1
  import { Module } from '@nestjs/common';
2
+ // #if telemetry
3
+ import { TelemetryModule } from 'franchising/telemetry';
4
+ import { adapters } from '__ROCK_SCOPE__/observability';
5
+ // #endif
2
6
  import { __ROCK_WORKER_CLASS__Module } from './modules/queue/module';
3
7
 
8
+ // #if telemetry
9
+ @Module({
10
+ imports: [
11
+ /* Mesmos adapters da API: o worker manda para o mesmo destino. */
12
+ TelemetryModule.forRoot({ adapters }),
13
+ __ROCK_WORKER_CLASS__Module,
14
+ ],
15
+ })
16
+ // #else
4
17
  @Module({ imports: [__ROCK_WORKER_CLASS__Module] })
18
+ // #endif
5
19
  export class AppModule {}
@@ -1,6 +1,16 @@
1
1
  import 'reflect-metadata';
2
+ // #if telemetry
3
+ /*
4
+ * Antes de qualquer `@nestjs/*`: a auto-instrumentacao precisa envolver o
5
+ * Nest, o Redis e os clients HTTP antes de eles existirem.
6
+ */
7
+ import '__ROCK_SCOPE__/observability/register';
8
+ import { NestFactory } from '@nestjs/core';
9
+ import { bootstrapFailed, observeProcess } from '__ROCK_SCOPE__/observability';
10
+ // #else
2
11
  import { Logger } from '@nestjs/common';
3
12
  import { NestFactory } from '@nestjs/core';
13
+ // #endif
4
14
  import { AppModule } from './app.module';
5
15
 
6
16
  /*
@@ -9,10 +19,22 @@ import { AppModule } from './app.module';
9
19
  * conexoes antes do processo morrer, para um job em andamento nao ficar preso.
10
20
  */
11
21
  async function bootstrap(): Promise<void> {
22
+ // #if telemetry
23
+ const app = await NestFactory.createApplicationContext(AppModule, {
24
+ bufferLogs: true,
25
+ });
26
+
27
+ observeProcess(app);
28
+ // #else
12
29
  const app = await NestFactory.createApplicationContext(AppModule);
30
+ // #endif
31
+
13
32
  app.enableShutdownHooks();
14
33
  }
15
34
 
35
+ // #if telemetry
36
+ void bootstrap().catch(bootstrapFailed('o worker __ROCK_WORKER_NAME__'));
37
+ // #else
16
38
  void bootstrap().catch((error: unknown) => {
17
39
  Logger.error(
18
40
  error instanceof Error
@@ -21,5 +43,7 @@ void bootstrap().catch((error: unknown) => {
21
43
  undefined,
22
44
  'Bootstrap',
23
45
  );
46
+
24
47
  process.exitCode = 1;
25
48
  });
49
+ // #endif