franchising 0.5.1 → 0.7.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 (97) hide show
  1. package/dist/cli/template-assets/api/src/main.ts +13 -11
  2. package/dist/cli/template-assets/api/src/modules/health/controller.ts +25 -4
  3. package/dist/cli/template-assets/api/src/shared/http/errors.ts +17 -0
  4. package/dist/cli/template-assets/cloudflare/cache.service.ts +1 -0
  5. package/dist/cli/template-assets/cloudflare/queue.service.ts +1 -0
  6. package/dist/cli/template-assets/cloudflare/worker.ts +4 -0
  7. package/dist/cli/template-assets/github/PULL_REQUEST_TEMPLATE.md +37 -0
  8. package/dist/cli/template-assets/observability/adapters.ts +37 -0
  9. package/dist/cli/template-assets/observability/bootstrap.ts +48 -0
  10. package/dist/cli/template-assets/observability/env.ts +21 -0
  11. package/dist/cli/template-assets/observability/index.ts +10 -0
  12. package/dist/cli/template-assets/observability/lifecycle.ts +97 -0
  13. package/dist/cli/template-assets/observability/local-index.ts +9 -0
  14. package/dist/cli/template-assets/observability/register.ts +12 -0
  15. package/dist/cli/template-assets/packages/jobs/src/index.ts +4 -40
  16. package/dist/cli/template-assets/packages/jobs/src/shared/queue/job-id.ts +9 -0
  17. package/dist/cli/template-assets/packages/jobs/src/shared/queue/options.ts +7 -0
  18. package/dist/cli/template-assets/packages/jobs/src/shared/queue/prefix.ts +18 -0
  19. package/dist/cli/template-assets/packages/queue/src/index.ts +4 -0
  20. package/dist/cli/template-assets/packages/queue/src/shared/errors/failure-reason.ts +7 -0
  21. package/dist/cli/template-assets/packages/{queue-runtime/src/index.ts → queue/src/shared/redis/connection.ts} +2 -15
  22. package/dist/cli/template-assets/packages/queue/src/types/redis/connection.ts +6 -0
  23. package/dist/cli/template-assets/packages/webhooks/src/index.ts +11 -0
  24. package/dist/cli/template-assets/packages/webhooks/src/shared/crypto/secret.ts +81 -0
  25. package/dist/cli/template-assets/packages/webhooks/src/shared/signing/headers.ts +43 -0
  26. package/dist/cli/template-assets/packages/webhooks/src/shared/url/deliverable.ts +58 -0
  27. package/dist/cli/template-assets/packages/webhooks/src/types/webhooks/job.ts +18 -0
  28. package/dist/cli/template-assets/packages/webhooks/test/webhooks.test.ts.tpl +69 -0
  29. package/dist/cli/template-assets/root/dot-dockerignore +88 -8
  30. package/dist/cli/template-assets/root/dot-oxlintrc.json +22 -0
  31. package/dist/cli/template-assets/root/lefthook.yml +32 -8
  32. package/dist/cli/template-assets/web/src/app/(app)/layout.tsx +2 -3
  33. package/dist/cli/template-assets/web/src/app/(auth)/login/page.tsx +1 -1
  34. package/dist/cli/template-assets/web/src/app/page.tsx +1 -0
  35. package/dist/cli/template-assets/web/src/{lib/api-client.ts → shared/api/client.ts} +11 -8
  36. package/dist/cli/template-assets/web/src/{lib/server-auth.ts → shared/auth/server.ts} +1 -2
  37. package/dist/cli/template-assets/web/src/types/api.ts +0 -15
  38. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/controller.ts +81 -0
  39. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/create-endpoint.dto.ts +25 -0
  40. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/endpoint.dto.ts +28 -0
  41. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/module.ts +27 -0
  42. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/repository.ts +147 -0
  43. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/service.ts +119 -0
  44. package/dist/cli/template-assets/webhooks/api/src/types/webhooks/endpoint.ts +26 -0
  45. package/dist/cli/template-assets/webhooks/worker/src/env.ts +18 -0
  46. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/dispatcher.ts +119 -0
  47. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/module.ts +16 -0
  48. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/processor.ts +176 -0
  49. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/repository.ts +187 -0
  50. package/dist/cli/template-assets/webhooks/worker/src/shared/http/deliver.ts +41 -0
  51. package/dist/cli/template-assets/webhooks/worker/src/types/webhooks/dispatch.ts +26 -0
  52. package/dist/cli/template-assets/worker/src/app.module.ts +14 -0
  53. package/dist/cli/template-assets/worker/src/main.ts +24 -0
  54. package/dist/cli/template-assets/worker/src/modules/queue/processor.ts +26 -3
  55. package/dist/cli/template-assets/worker/src/modules/queue/retry.ts +22 -7
  56. package/dist/cli/template-assets/worker/test/retry.test.ts.tpl +13 -1
  57. package/dist/cli/template-assets/worker-messaging/messaging-api-service.ts +6 -6
  58. package/dist/cli/template-assets/worker-messaging/src/env.ts +1 -1
  59. package/dist/cli/template-assets/worker-messaging/src/modules/messaging/service.ts +2 -4
  60. package/dist/cli/template-assets/worker-messaging/src/modules/queue/module.ts +1 -1
  61. package/dist/cli/template-assets/worker-messaging/src/modules/queue/processor.ts +32 -10
  62. package/dist/cli.js +631 -314
  63. package/dist/{index-BDYdEWl2.d.ts → index-B_byXBRW.d.cts} +10 -2
  64. package/dist/{index-BDYdEWl2.d.cts → index-B_byXBRW.d.ts} +10 -2
  65. package/dist/{index-u3ArUXO0.d.ts → index-CNlb6WNr.d.ts} +2 -2
  66. package/dist/{index--EJQTnHy.d.cts → index-I_Nx5SsQ.d.cts} +2 -2
  67. package/dist/index.cjs +1 -1
  68. package/dist/index.d.cts +1 -1
  69. package/dist/index.d.ts +1 -1
  70. package/dist/index.js +1 -1
  71. package/dist/messaging/index.cjs +1 -1
  72. package/dist/messaging/index.d.cts +1 -1
  73. package/dist/messaging/index.d.ts +1 -1
  74. package/dist/messaging/index.js +1 -1
  75. package/dist/sso/client.d.cts +7 -2
  76. package/dist/sso/client.d.ts +7 -2
  77. package/dist/sso/index.cjs +3 -1
  78. package/dist/sso/index.cjs.map +1 -1
  79. package/dist/sso/index.d.cts +1 -1
  80. package/dist/sso/index.d.ts +1 -1
  81. package/dist/sso/index.js +3 -1
  82. package/dist/sso/index.js.map +1 -1
  83. package/dist/sso/nestjs.d.cts +2 -2
  84. package/dist/sso/nestjs.d.ts +2 -2
  85. package/dist/sso/resource.d.cts +2 -2
  86. package/dist/sso/resource.d.ts +2 -2
  87. package/dist/version-Cm1EJ-YK.js +1 -0
  88. package/dist/{version-CrBclf1K.cjs → version-iERj9NJ6.cjs} +1 -1
  89. package/package.json +1 -1
  90. package/dist/cli/template-assets/api/src/errors/codes.ts +0 -17
  91. package/dist/cli/template-assets/api/src/errors/index.ts +0 -16
  92. package/dist/cli/template-assets/api/src/filters/exception.ts +0 -53
  93. package/dist/cli/template-assets/api/src/shared/exceptions.ts +0 -40
  94. package/dist/cli/template-assets/api/src/shared/response.ts +0 -31
  95. package/dist/cli/template-assets/api/src/telemetry.ts +0 -59
  96. package/dist/version-BhZpPjUh.js +0 -1
  97. /package/dist/cli/template-assets/web/src/{lib/auth-client.ts → shared/auth/client.ts} +0 -0
@@ -0,0 +1,28 @@
1
+ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
2
+
3
+ export class WebhookEndpointDto {
4
+ @ApiProperty({ format: 'uuid' })
5
+ id!: string;
6
+
7
+ @ApiProperty({ example: 'https://exemplo.com/webhooks' })
8
+ url!: string;
9
+
10
+ @ApiProperty({ example: ['user.created'], type: [String] })
11
+ eventTypes!: string[];
12
+
13
+ @ApiProperty({ example: true })
14
+ enabled!: boolean;
15
+
16
+ @ApiProperty({ type: String, format: 'date-time' })
17
+ createdAt!: Date;
18
+
19
+ @ApiProperty({ type: String, format: 'date-time' })
20
+ updatedAt!: Date;
21
+
22
+ @ApiPropertyOptional({
23
+ example: 'whsec_...',
24
+ description:
25
+ 'Segredo de assinatura (Standard Webhooks). Vem so na criacao e na rotacao: guarde na hora.',
26
+ })
27
+ secret?: string;
28
+ }
@@ -0,0 +1,27 @@
1
+ import { Module } from '@nestjs/common';
2
+ // #if routes
3
+ import { WebhooksController } from './controller';
4
+ // #endif
5
+ import { WebhooksRepository } from './repository';
6
+ import { WebhooksService } from './service';
7
+
8
+ /**
9
+ * Webhooks de saida.
10
+ *
11
+ * @remarks
12
+ * Exporta o `WebhooksService` para os outros modulos publicarem eventos com
13
+ * `publish`. A entrega roda no worker `apps/workers/webhooks`.
14
+ */
15
+ // #if routes
16
+ @Module({
17
+ controllers: [WebhooksController],
18
+ providers: [WebhooksRepository, WebhooksService],
19
+ exports: [WebhooksService],
20
+ })
21
+ // #else
22
+ @Module({
23
+ providers: [WebhooksRepository, WebhooksService],
24
+ exports: [WebhooksService],
25
+ })
26
+ // #endif
27
+ export class WebhooksModule {}
@@ -0,0 +1,147 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ // #if drizzle
3
+ import { and, desc, eq } from 'drizzle-orm';
4
+ import {
5
+ type DatabaseTransaction,
6
+ webhookEndpoints,
7
+ webhookOutbox,
8
+ } from '__ROCK_SCOPE__/database';
9
+ // #else
10
+ import type { DatabaseTransaction } from '__ROCK_SCOPE__/database';
11
+ // #endif
12
+ import type {
13
+ NewWebhookEndpoint,
14
+ WebhookEndpointChanges,
15
+ WebhookEndpointRecord,
16
+ } from '../../types/webhooks/endpoint';
17
+ import { DatabaseService } from '../database/database.service';
18
+
19
+ /**
20
+ * Acesso a `webhook_endpoints` e `webhook_outbox`.
21
+ *
22
+ * @remarks
23
+ * Toda leitura e escrita de endpoint filtra por `ownerId`: o id do endpoint
24
+ * sozinho nao autoriza nada, entao um usuario nunca alcanca o endpoint de
25
+ * outro, nem adivinhando o id.
26
+ */
27
+ @Injectable()
28
+ export class WebhooksRepository {
29
+ constructor(private readonly database: DatabaseService) {}
30
+
31
+ // #if drizzle
32
+ async create(input: NewWebhookEndpoint): Promise<WebhookEndpointRecord> {
33
+ const [row] = await this.database.client
34
+ .insert(webhookEndpoints)
35
+ .values(input)
36
+ .returning();
37
+
38
+ if (!row) throw new Error('WEBHOOK_ENDPOINT_NOT_CREATED');
39
+
40
+ return row;
41
+ }
42
+
43
+ listByOwner(ownerId: string): Promise<WebhookEndpointRecord[]> {
44
+ return this.database.client
45
+ .select()
46
+ .from(webhookEndpoints)
47
+ .where(eq(webhookEndpoints.ownerId, ownerId))
48
+ .orderBy(desc(webhookEndpoints.createdAt));
49
+ }
50
+
51
+ async update(
52
+ ownerId: string,
53
+ id: string,
54
+ changes: WebhookEndpointChanges,
55
+ ): Promise<WebhookEndpointRecord | undefined> {
56
+ const [row] = await this.database.client
57
+ .update(webhookEndpoints)
58
+ .set(changes)
59
+ .where(
60
+ and(
61
+ eq(webhookEndpoints.id, id),
62
+ eq(webhookEndpoints.ownerId, ownerId),
63
+ ),
64
+ )
65
+ .returning();
66
+
67
+ return row;
68
+ }
69
+
70
+ /** Grava o evento no outbox, dentro da transacao de quem publica. */
71
+ async enqueue(
72
+ tx: DatabaseTransaction,
73
+ event: { ownerId: string; eventType: string; payload: unknown },
74
+ ): Promise<string> {
75
+ const [row] = await tx
76
+ .insert(webhookOutbox)
77
+ .values(event)
78
+ .returning({ id: webhookOutbox.id });
79
+
80
+ if (!row) throw new Error('WEBHOOK_EVENT_NOT_CREATED');
81
+
82
+ return row.id;
83
+ }
84
+
85
+ /** Abre uma transacao so para o evento, quando quem publica nao tem uma. */
86
+ transaction<T>(fn: (tx: DatabaseTransaction) => Promise<T>): Promise<T> {
87
+ return this.database.client.transaction(fn);
88
+ }
89
+ // #else
90
+ create(input: NewWebhookEndpoint): Promise<WebhookEndpointRecord> {
91
+ return this.database.client.webhookEndpoint.create({ data: input });
92
+ }
93
+
94
+ listByOwner(ownerId: string): Promise<WebhookEndpointRecord[]> {
95
+ return this.database.client.webhookEndpoint.findMany({
96
+ where: { ownerId },
97
+ orderBy: { createdAt: 'desc' },
98
+ });
99
+ }
100
+
101
+ async update(
102
+ ownerId: string,
103
+ id: string,
104
+ changes: WebhookEndpointChanges,
105
+ ): Promise<WebhookEndpointRecord | undefined> {
106
+ const { count } = await this.database.client.webhookEndpoint.updateMany({
107
+ where: { id, ownerId },
108
+ data: changes,
109
+ });
110
+
111
+ if (count === 0) return void 0;
112
+
113
+ return (
114
+ (await this.database.client.webhookEndpoint.findUnique({
115
+ where: { id },
116
+ })) ?? void 0
117
+ );
118
+ }
119
+
120
+ /** Grava o evento no outbox, dentro da transacao de quem publica. */
121
+ async enqueue(
122
+ tx: DatabaseTransaction,
123
+ event: { ownerId: string; eventType: string; payload: unknown },
124
+ ): Promise<string> {
125
+ const row = await tx.webhookOutbox.create({
126
+ data: {
127
+ ownerId: event.ownerId,
128
+ eventType: event.eventType,
129
+ /*
130
+ * Ida e volta pelo JSON: garante que o payload gravado e
131
+ * exatamente o que o receptor vai ler (sem `Date`, `undefined`
132
+ * ou classe), e satisfaz o tipo de coluna JSON do Prisma.
133
+ */
134
+ payload: JSON.parse(JSON.stringify(event.payload ?? null)),
135
+ },
136
+ select: { id: true },
137
+ });
138
+
139
+ return row.id;
140
+ }
141
+
142
+ /** Abre uma transacao so para o evento, quando quem publica nao tem uma. */
143
+ transaction<T>(fn: (tx: DatabaseTransaction) => Promise<T>): Promise<T> {
144
+ return this.database.client.$transaction(fn);
145
+ }
146
+ // #endif
147
+ }
@@ -0,0 +1,119 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import { badRequest, notFound } from 'franchising/http';
3
+ import type { DatabaseTransaction } from '__ROCK_SCOPE__/database';
4
+ import {
5
+ generateWebhookSecret,
6
+ isDeliverableUrl,
7
+ sealWebhookSecret,
8
+ } from '__ROCK_SCOPE__/webhooks';
9
+ import { env } from '../../env';
10
+ import { Errors } from '../../shared/http/errors';
11
+ import type {
12
+ WebhookEndpointRecord,
13
+ WebhookEndpointView,
14
+ WebhookEndpointWithSecret,
15
+ } from '../../types/webhooks/endpoint';
16
+ import { WebhooksRepository } from './repository';
17
+
18
+ function toView({
19
+ secret: _secret,
20
+ ownerId: _ownerId,
21
+ ...view
22
+ }: WebhookEndpointRecord): WebhookEndpointView {
23
+ return view;
24
+ }
25
+
26
+ /**
27
+ * Endpoints de webhook de cada usuario e publicacao de eventos.
28
+ *
29
+ * @remarks
30
+ * O segredo sai em texto so na criacao e na rotacao; no banco ele fica
31
+ * cifrado com `WEBHOOK_SECRETS_KEY`, e quem le o banco nao consegue assinar
32
+ * em nome da API.
33
+ */
34
+ @Injectable()
35
+ export class WebhooksService {
36
+ constructor(private readonly repository: WebhooksRepository) {}
37
+
38
+ async create(
39
+ ownerId: string,
40
+ input: { url: string; eventTypes: string[] },
41
+ ): Promise<WebhookEndpointWithSecret> {
42
+ if (!isDeliverableUrl(input.url, env.isProduction))
43
+ throw badRequest(Errors.WebhookUrlNotAllowed);
44
+
45
+ const secret = generateWebhookSecret();
46
+
47
+ const endpoint = await this.repository.create({
48
+ ownerId,
49
+ url: input.url,
50
+ eventTypes: [...new Set(input.eventTypes)],
51
+ secret: sealWebhookSecret(secret, env.WEBHOOK_SECRETS_KEY),
52
+ });
53
+
54
+ return { ...toView(endpoint), secret };
55
+ }
56
+
57
+ async list(ownerId: string): Promise<WebhookEndpointView[]> {
58
+ const endpoints = await this.repository.listByOwner(ownerId);
59
+
60
+ return endpoints.map((endpoint) => toView(endpoint));
61
+ }
62
+
63
+ /**
64
+ * Troca o segredo na hora.
65
+ *
66
+ * @remarks
67
+ * Entregas ja enfileiradas assinam com o segredo novo, porque o worker le
68
+ * o endpoint a cada tentativa: o receptor precisa trocar o dele junto.
69
+ */
70
+ async rotateSecret(
71
+ ownerId: string,
72
+ id: string,
73
+ ): Promise<WebhookEndpointWithSecret> {
74
+ const secret = generateWebhookSecret();
75
+
76
+ const endpoint = await this.repository.update(ownerId, id, {
77
+ secret: sealWebhookSecret(secret, env.WEBHOOK_SECRETS_KEY),
78
+ });
79
+
80
+ if (!endpoint) throw notFound(Errors.WebhookEndpointNotFound);
81
+
82
+ return { ...toView(endpoint), secret };
83
+ }
84
+
85
+ /** Para de entregar; as tentativas em andamento desistem na proxima. */
86
+ async disable(ownerId: string, id: string): Promise<WebhookEndpointView> {
87
+ const endpoint = await this.repository.update(ownerId, id, {
88
+ enabled: false,
89
+ });
90
+
91
+ if (!endpoint) throw notFound(Errors.WebhookEndpointNotFound);
92
+
93
+ return toView(endpoint);
94
+ }
95
+
96
+ /**
97
+ * Publica um evento para os endpoints do dono.
98
+ *
99
+ * @remarks
100
+ * So grava no outbox. Passe a transacao da mudanca de negocio em `tx`: o
101
+ * evento existe se e somente se a mudanca foi gravada, e o worker
102
+ * `webhooks` entrega depois, mesmo que o Redis esteja fora agora. Devolve
103
+ * o id do evento, que vira o `webhook-id` de todas as entregas dele.
104
+ */
105
+ publish(
106
+ eventType: string,
107
+ payload: unknown,
108
+ ownerId: string,
109
+ tx?: DatabaseTransaction,
110
+ ): Promise<string> {
111
+ const event = { ownerId, eventType, payload };
112
+
113
+ if (tx) return this.repository.enqueue(tx, event);
114
+
115
+ return this.repository.transaction((transaction) =>
116
+ this.repository.enqueue(transaction, event),
117
+ );
118
+ }
119
+ }
@@ -0,0 +1,26 @@
1
+ /** Linha de `webhook_endpoints`, com o segredo ainda cifrado. */
2
+ export type WebhookEndpointRecord = {
3
+ id: string;
4
+ ownerId: string;
5
+ url: string;
6
+ secret: string;
7
+ eventTypes: string[];
8
+ enabled: boolean;
9
+ createdAt: Date;
10
+ updatedAt: Date;
11
+ };
12
+
13
+ /** O que a API devolve: nunca o segredo, nem cifrado. */
14
+ export type WebhookEndpointView = Omit<WebhookEndpointRecord, 'secret' | 'ownerId'>;
15
+
16
+ /** Resposta de criacao e de rotacao: a unica vez que o segredo aparece. */
17
+ export type WebhookEndpointWithSecret = WebhookEndpointView & { secret: string };
18
+
19
+ export type NewWebhookEndpoint = Pick<
20
+ WebhookEndpointRecord,
21
+ 'ownerId' | 'url' | 'secret' | 'eventTypes'
22
+ >;
23
+
24
+ export type WebhookEndpointChanges = Partial<
25
+ Pick<WebhookEndpointRecord, 'secret' | 'enabled'>
26
+ >;
@@ -0,0 +1,18 @@
1
+ import 'dotenv/config';
2
+ import { cleanEnv, num, str } from 'envalid';
3
+
4
+ export const env = cleanEnv(process.env, {
5
+ NODE_ENV: str({
6
+ choices: ['development', 'test', 'production'],
7
+ default: 'development',
8
+ }),
9
+ QUEUE_REDIS_URL: str({ default: 'redis://localhost:6379' }),
10
+ QUEUE_PREFIX: str({ default: '__ROCK_PROJECT_SLUG__' }),
11
+ __ROCK_WORKER_CONCURRENCY_ENV__: num({ default: 10 }),
12
+ WEBHOOK_SECRETS_KEY: str({
13
+ desc: 'Chave AES-256 dos segredos (openssl rand -base64 32), a mesma da API.',
14
+ }),
15
+ WEBHOOKS_DISPATCH_INTERVAL_MS: num({ default: 1000 }),
16
+ WEBHOOKS_DISPATCH_BATCH_SIZE: num({ default: 100 }),
17
+ WEBHOOKS_TIMEOUT_MS: num({ default: 10_000 }),
18
+ });
@@ -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
+ }