@miguelmorales13/nestkit 0.25.0 → 0.28.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.
- package/README.md +5 -0
- package/dist/alerts/alert-on-failure.decorator.d.ts +5 -0
- package/dist/alerts/alert-sinks.d.ts +29 -0
- package/dist/alerts/alerts.module.d.ts +19 -0
- package/dist/alerts/alerts.options.d.ts +1 -0
- package/dist/alerts/alerts.service.d.ts +28 -0
- package/dist/alerts/alerts.types.d.ts +66 -0
- package/dist/alerts/index.cjs +276 -0
- package/dist/alerts/index.d.ts +10 -0
- package/dist/alerts/index.js +276 -0
- package/dist/alerts/logger-with-alerts.d.ts +18 -0
- package/dist/alerts/process-guards.d.ts +21 -0
- package/dist/alerts/runtime.d.ts +4 -0
- package/dist/audit/index.cjs +13 -13
- package/dist/audit/index.js +2 -2
- package/dist/auth/index.cjs +21 -21
- package/dist/auth/index.js +3 -3
- package/dist/auth/oauth/index.cjs +123 -115
- package/dist/auth/oauth/index.js +25 -17
- package/dist/auth/oauth/oauth-provider.d.ts +9 -0
- package/dist/auth/oauth/providers/apple.d.ts +3 -1
- package/dist/bootstrap/apply-defaults.d.ts +9 -0
- package/dist/bootstrap/index.cjs +6 -5
- package/dist/bootstrap/index.js +5 -4
- package/dist/chunk-3TUMVLPJ.js +14 -0
- package/dist/{chunk-PA24P76K.js → chunk-45SW7FBE.js} +1 -1
- package/dist/{chunk-RF75KC63.cjs → chunk-47IYXZHW.cjs} +2 -2
- package/dist/{chunk-TUNDBBQF.js → chunk-4WWOKZIO.js} +4 -4
- package/dist/{chunk-A2DGK4ZL.js → chunk-5LVGCTJ3.js} +1 -1
- package/dist/chunk-A6YGIZCQ.cjs +14 -0
- package/dist/chunk-A7FI6HXV.js +40 -0
- package/dist/chunk-CU4FUIVJ.cjs +8 -0
- package/dist/{chunk-FH5UNWWW.cjs → chunk-D35TBVRO.cjs} +4 -4
- package/dist/{chunk-4MGIQFAJ.js → chunk-D3DDJWUD.js} +5 -0
- package/dist/chunk-DVGPEBUL.js +8 -0
- package/dist/{chunk-VKOPDDCC.js → chunk-ENZ7LQRE.js} +1 -1
- package/dist/{chunk-3AUOOEQO.cjs → chunk-F77X4QUN.cjs} +2 -2
- package/dist/chunk-FBNV5X6C.js +61 -0
- package/dist/{chunk-APUFKA4K.cjs → chunk-FDZQCLLK.cjs} +6 -6
- package/dist/{chunk-DYHSOULW.js → chunk-IGSNB5BX.js} +1 -1
- package/dist/chunk-IVWPA6QH.cjs +90 -0
- package/dist/{chunk-MSTXS3MU.cjs → chunk-JJKGQNGD.cjs} +2 -2
- package/dist/{chunk-FNUZGYUF.cjs → chunk-JRFE7NSO.cjs} +12 -4
- package/dist/{chunk-HVHYZXBB.cjs → chunk-KNU5RN3O.cjs} +3 -3
- package/dist/{chunk-52IOOHNX.cjs → chunk-KQPJCVN3.cjs} +26 -26
- package/dist/chunk-LGFHLAY3.cjs +61 -0
- package/dist/{chunk-2REOCMUD.cjs → chunk-LRX7AMSS.cjs} +6 -1
- package/dist/{chunk-4PFAKLT3.js → chunk-MUWHGBIB.js} +1 -1
- package/dist/{chunk-ZJMLEP5H.js → chunk-NJYLQDB3.js} +1 -1
- package/dist/{chunk-IFNYRZP2.js → chunk-OSRH6KYZ.js} +4 -4
- package/dist/{chunk-6RAPVLJM.js → chunk-OTE77ONL.js} +1 -1
- package/dist/chunk-UDACUMLO.cjs +40 -0
- package/dist/{chunk-UNEVXCFW.js → chunk-UGAAJRPO.js} +10 -2
- package/dist/{chunk-SPTDXUED.cjs → chunk-URLDTS2Q.cjs} +2 -2
- package/dist/{chunk-W2T2VFLC.cjs → chunk-VALDHJOO.cjs} +5 -5
- package/dist/chunk-WFCQDOSN.js +90 -0
- package/dist/consent/consent.interceptor.d.ts +20 -0
- package/dist/consent/consent.module.d.ts +25 -0
- package/dist/consent/consent.options.d.ts +30 -0
- package/dist/consent/consent.port.d.ts +17 -0
- package/dist/consent/consent.service.d.ts +44 -0
- package/dist/consent/guard/index.cjs +13 -0
- package/dist/consent/guard/index.d.ts +31 -0
- package/dist/consent/guard/index.js +13 -0
- package/dist/consent/index.cjs +271 -0
- package/dist/consent/index.d.ts +13 -0
- package/dist/consent/index.js +271 -0
- package/dist/consent/memory.store.d.ts +7 -0
- package/dist/consent/shared/index.cjs +11 -0
- package/dist/consent/shared/index.d.ts +19 -0
- package/dist/consent/shared/index.js +11 -0
- package/dist/consent/typeorm.store.d.ts +22 -0
- package/dist/crud/index.cjs +4 -4
- package/dist/crud/index.js +3 -3
- package/dist/database/postgres/index.cjs +3 -3
- package/dist/database/postgres/index.js +2 -2
- package/dist/database/supabase/index.cjs +3 -3
- package/dist/database/supabase/index.js +2 -2
- package/dist/database/typeorm/index.cjs +2 -2
- package/dist/database/typeorm/index.js +1 -1
- package/dist/devices/device-check.interceptor.d.ts +20 -0
- package/dist/devices/devices.module.d.ts +28 -0
- package/dist/devices/devices.options.d.ts +53 -0
- package/dist/devices/devices.port.d.ts +62 -0
- package/dist/devices/devices.service.d.ts +58 -0
- package/dist/devices/index.cjs +327 -0
- package/dist/devices/index.d.ts +8 -0
- package/dist/devices/index.js +327 -0
- package/dist/devices/memory.store.d.ts +12 -0
- package/dist/email/index.cjs +2 -2
- package/dist/email/index.js +1 -1
- package/dist/email/nodemailer/index.cjs +2 -2
- package/dist/email/nodemailer/index.js +1 -1
- package/dist/email/resend/index.cjs +2 -2
- package/dist/email/resend/index.js +1 -1
- package/dist/entities/index.cjs +1 -1
- package/dist/entities/index.js +1 -1
- package/dist/errors/index.cjs +7 -7
- package/dist/errors/index.js +7 -7
- package/dist/events/buses.d.ts +27 -0
- package/dist/events/events.controller.d.ts +15 -0
- package/dist/events/events.module.d.ts +29 -0
- package/dist/events/events.options.d.ts +50 -0
- package/dist/events/events.service.d.ts +49 -0
- package/dist/events/events.types.d.ts +77 -0
- package/dist/events/index.cjs +557 -0
- package/dist/events/index.d.ts +10 -0
- package/dist/events/index.js +557 -0
- package/dist/events/memory-ticket.store.d.ts +9 -0
- package/dist/google-business/google-business-http.d.ts +41 -0
- package/dist/google-business/google-business.errors.d.ts +75 -0
- package/dist/google-business/google-business.mappers.d.ts +52 -0
- package/dist/google-business/google-business.module.d.ts +26 -0
- package/dist/google-business/google-business.options.d.ts +40 -0
- package/dist/google-business/google-business.ports.d.ts +18 -0
- package/dist/google-business/google-business.service.d.ts +120 -0
- package/dist/google-business/google-business.types.d.ts +155 -0
- package/dist/google-business/index.cjs +1025 -0
- package/dist/google-business/index.d.ts +15 -0
- package/dist/google-business/index.js +1025 -0
- package/dist/google-business/testing/google-simulator.d.ts +61 -0
- package/dist/hardening/body-limits.d.ts +24 -0
- package/dist/hardening/http.types.d.ts +26 -0
- package/dist/hardening/index.cjs +179 -0
- package/dist/hardening/index.d.ts +19 -0
- package/dist/hardening/index.js +179 -0
- package/dist/hardening/memory-rate-limit.store.d.ts +10 -0
- package/dist/hardening/origin-secret.d.ts +29 -0
- package/dist/hardening/rate-limit.decorator.d.ts +8 -0
- package/dist/hardening/rate-limit.guard.d.ts +18 -0
- package/dist/hardening/rate-limit.module.d.ts +23 -0
- package/dist/hardening/rate-limit.options.d.ts +35 -0
- package/dist/hardening/rate-limit.port.d.ts +11 -0
- package/dist/hardening/redis-rate-limit.store.d.ts +23 -0
- package/dist/hardening/trust-proxy.d.ts +22 -0
- package/dist/health/health.controller.d.ts +32 -0
- package/dist/health/health.module.d.ts +15 -0
- package/dist/health/health.options.d.ts +24 -0
- package/dist/health/index.cjs +103 -0
- package/dist/health/index.d.ts +5 -0
- package/dist/health/index.js +103 -0
- package/dist/i18n/index.cjs +3 -3
- package/dist/i18n/index.js +2 -2
- package/dist/idempotency/idempotency.interceptor.d.ts +1 -0
- package/dist/idempotency/idempotency.options.d.ts +15 -0
- package/dist/idempotency/index.cjs +214 -7
- package/dist/idempotency/index.d.ts +5 -0
- package/dist/idempotency/index.js +210 -3
- package/dist/idempotency/no-idempotency.decorator.d.ts +8 -0
- package/dist/idempotency/postgres.store.d.ts +49 -0
- package/dist/idempotency/redis.store.d.ts +59 -0
- package/dist/index.cjs +21 -20
- package/dist/index.js +31 -30
- package/dist/invoicing/index.cjs +2 -2
- package/dist/invoicing/index.js +1 -1
- package/dist/jobs/defer-job.d.ts +10 -0
- package/dist/jobs/index.cjs +318 -0
- package/dist/jobs/index.d.ts +8 -0
- package/dist/jobs/index.js +318 -0
- package/dist/jobs/jobs-admin.controller.d.ts +18 -0
- package/dist/jobs/jobs.module.d.ts +33 -0
- package/dist/jobs/jobs.options.d.ts +62 -0
- package/dist/jobs/jobs.service.d.ts +53 -0
- package/dist/jobs/jobs.sql.d.ts +11 -0
- package/dist/media/index.cjs +14 -14
- package/dist/media/index.js +1 -1
- package/dist/meta/index.cjs +3 -3
- package/dist/meta/index.js +1 -1
- package/dist/notifications/index.cjs +375 -0
- package/dist/notifications/index.d.ts +8 -0
- package/dist/notifications/index.js +375 -0
- package/dist/notifications/memory.stores.d.ts +23 -0
- package/dist/notifications/notifications.controller.d.ts +11 -0
- package/dist/notifications/notifications.module.d.ts +31 -0
- package/dist/notifications/notifications.options.d.ts +33 -0
- package/dist/notifications/notifications.port.d.ts +97 -0
- package/dist/notifications/notifier.service.d.ts +31 -0
- package/dist/payments/index.cjs +7 -7
- package/dist/payments/index.js +1 -1
- package/dist/pinterest/index.cjs +3 -3
- package/dist/pinterest/index.js +1 -1
- package/dist/privacy/anonymizer.d.ts +14 -0
- package/dist/privacy/index.cjs +768 -0
- package/dist/privacy/index.d.ts +13 -0
- package/dist/privacy/index.js +768 -0
- package/dist/privacy/memory.store.d.ts +15 -0
- package/dist/privacy/privacy.module.d.ts +30 -0
- package/dist/privacy/privacy.options.d.ts +43 -0
- package/dist/privacy/privacy.port.d.ts +25 -0
- package/dist/privacy/privacy.service.d.ts +69 -0
- package/dist/privacy/privacy.types.d.ts +125 -0
- package/dist/privacy/sql.d.ts +7 -0
- package/dist/privacy/sql.store.d.ts +21 -0
- package/dist/privacy/zip.d.ts +23 -0
- package/dist/push/index.cjs +13 -13
- package/dist/push/index.js +5 -5
- package/dist/resilience/alert-channels.d.ts +39 -0
- package/dist/resilience/alert-errors.interceptor.d.ts +10 -0
- package/dist/resilience/alert-on-failure.decorator.d.ts +10 -0
- package/dist/resilience/alert.service.d.ts +77 -0
- package/dist/resilience/index.cjs +863 -0
- package/dist/resilience/index.d.ts +18 -0
- package/dist/resilience/index.js +863 -0
- package/dist/resilience/memory-alert-store.d.ts +26 -0
- package/dist/resilience/provider-health.controller.d.ts +14 -0
- package/dist/resilience/provider-unavailable.exception.d.ts +11 -0
- package/dist/resilience/resilience.module.d.ts +34 -0
- package/dist/resilience/resilience.options.d.ts +28 -0
- package/dist/resilience/resilience.types.d.ts +167 -0
- package/dist/resilience/resilient-fetch.d.ts +38 -0
- package/dist/resilience/runtime.d.ts +24 -0
- package/dist/resilience/typeorm/alert.entity.d.ts +21 -0
- package/dist/resilience/typeorm/index.cjs +102 -0
- package/dist/resilience/typeorm/index.d.ts +3 -0
- package/dist/resilience/typeorm/index.js +102 -0
- package/dist/resilience/typeorm/typeorm-alert-store.d.ts +39 -0
- package/dist/resilience/validate-destination.d.ts +63 -0
- package/dist/response/index.cjs +3 -3
- package/dist/response/index.js +2 -2
- package/dist/scheduling/cron-lock.service.d.ts +36 -0
- package/dist/scheduling/cron-unico.decorator.d.ts +27 -0
- package/dist/scheduling/fallback.store.d.ts +34 -0
- package/dist/scheduling/index.cjs +392 -0
- package/dist/scheduling/index.d.ts +18 -0
- package/dist/scheduling/index.js +392 -0
- package/dist/scheduling/lock-store.port.d.ts +19 -0
- package/dist/scheduling/memory.store.d.ts +15 -0
- package/dist/scheduling/redis.module.d.ts +31 -0
- package/dist/scheduling/redis.store.d.ts +24 -0
- package/dist/scheduling/scheduling.module.d.ts +24 -0
- package/dist/scheduling/scheduling.options.d.ts +27 -0
- package/dist/scheduling/sql.store.d.ts +26 -0
- package/dist/secrets/index.cjs +274 -0
- package/dist/secrets/index.d.ts +8 -0
- package/dist/secrets/index.js +274 -0
- package/dist/secrets/secrets.errors.d.ts +6 -0
- package/dist/secrets/secrets.module.d.ts +21 -0
- package/dist/secrets/secrets.options.d.ts +32 -0
- package/dist/secrets/secrets.service.d.ts +54 -0
- package/dist/secrets/secrets.transformer.d.ts +33 -0
- package/dist/security/hash.d.ts +8 -0
- package/dist/security/index.cjs +119 -2
- package/dist/security/index.d.ts +5 -0
- package/dist/security/index.js +119 -2
- package/dist/security/mask.d.ts +22 -0
- package/dist/security/signed-grant.d.ts +30 -0
- package/dist/storage/index.cjs +2 -2
- package/dist/storage/index.js +1 -1
- package/dist/stripe/index.cjs +7 -7
- package/dist/stripe/index.js +1 -1
- package/dist/telegram/index.cjs +2 -2
- package/dist/telegram/index.js +1 -1
- package/dist/tenancy/app-role.d.ts +24 -0
- package/dist/tenancy/db-helpers.d.ts +12 -0
- package/dist/tenancy/index.cjs +205 -0
- package/dist/tenancy/index.d.ts +14 -0
- package/dist/tenancy/index.js +205 -0
- package/dist/tenancy/pg/index.cjs +34 -0
- package/dist/tenancy/pg/index.d.ts +2 -0
- package/dist/tenancy/pg/index.js +34 -0
- package/dist/tenancy/pg/with-tenant.d.ts +23 -0
- package/dist/tenancy/request-context.d.ts +18 -0
- package/dist/tenancy/rls-guardrail.d.ts +38 -0
- package/dist/tenancy/scope.d.ts +5 -0
- package/dist/tenancy/tenancy.errors.d.ts +5 -0
- package/dist/tenancy/tenancy.middleware.d.ts +8 -0
- package/dist/tenancy/tenancy.module.d.ts +37 -0
- package/dist/tenancy/tenancy.options.d.ts +34 -0
- package/dist/tenancy/typeorm/index.cjs +41 -0
- package/dist/tenancy/typeorm/index.d.ts +2 -0
- package/dist/tenancy/typeorm/index.js +41 -0
- package/dist/tenancy/typeorm/with-tenant.d.ts +23 -0
- package/dist/tiktok/index.cjs +3 -3
- package/dist/tiktok/index.js +1 -1
- package/dist/tracking/index.cjs +3 -3
- package/dist/tracking/index.js +2 -2
- package/dist/umami/index.cjs +4 -4
- package/dist/umami/index.js +1 -1
- package/dist/whatsapp/index.cjs +2 -2
- package/dist/whatsapp/index.js +1 -1
- package/package.json +107 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { EventsModule } from './events.module.js';
|
|
2
|
+
export { EventsService } from './events.service.js';
|
|
3
|
+
export { createEventsController, formatSignal } from './events.controller.js';
|
|
4
|
+
export { EVENTS_OPTIONS, EVENTS_TICKET_STORE } from './events.options.js';
|
|
5
|
+
export type { EventsOptions, EventsControllerOptions } from './events.options.js';
|
|
6
|
+
export { PgNotifyBus, RedisBus, createMemoryBusHub } from './buses.js';
|
|
7
|
+
export type { PgNotifyBusOptions, RedisBusOptions } from './buses.js';
|
|
8
|
+
export { MemoryTicketStore } from './memory-ticket.store.js';
|
|
9
|
+
export { RESYNC_MODULE, RESYNC_TYPE } from './events.types.js';
|
|
10
|
+
export type { EventSignal, PublishInput, StreamMessage, EventsBus, PgNotifyClient, RedisPubSubClient, TicketPayload, TicketStorePort, } from './events.types.js';
|
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import "../chunk-DQYAIQQ5.js";
|
|
2
|
+
import "../chunk-5LVGCTJ3.js";
|
|
3
|
+
import "../chunk-X4PQJZ4G.js";
|
|
4
|
+
import "../chunk-TCAMLNUZ.js";
|
|
5
|
+
import "../chunk-ETRUVZTJ.js";
|
|
6
|
+
import {
|
|
7
|
+
ForbiddenAppException,
|
|
8
|
+
NotFoundAppException,
|
|
9
|
+
UnauthorizedAppException
|
|
10
|
+
} from "../chunk-ZFIDAX7K.js";
|
|
11
|
+
import "../chunk-M3GQDRWR.js";
|
|
12
|
+
import "../chunk-VVMRZEIW.js";
|
|
13
|
+
import {
|
|
14
|
+
__decorateClass,
|
|
15
|
+
__decorateParam
|
|
16
|
+
} from "../chunk-D3DDJWUD.js";
|
|
17
|
+
|
|
18
|
+
// src/events/events.module.ts
|
|
19
|
+
import { Global, Module } from "@nestjs/common";
|
|
20
|
+
|
|
21
|
+
// src/events/events.controller.ts
|
|
22
|
+
import { randomBytes } from "crypto";
|
|
23
|
+
import {
|
|
24
|
+
applyDecorators,
|
|
25
|
+
Controller,
|
|
26
|
+
Get,
|
|
27
|
+
Headers,
|
|
28
|
+
Inject as Inject2,
|
|
29
|
+
Post,
|
|
30
|
+
Query,
|
|
31
|
+
Req,
|
|
32
|
+
Res
|
|
33
|
+
} from "@nestjs/common";
|
|
34
|
+
|
|
35
|
+
// src/events/events.options.ts
|
|
36
|
+
var EVENTS_OPTIONS = /* @__PURE__ */ Symbol("EVENTS_OPTIONS");
|
|
37
|
+
var EVENTS_TICKET_STORE = /* @__PURE__ */ Symbol("EVENTS_TICKET_STORE");
|
|
38
|
+
var DEFAULT_REPLAY_WINDOW = 500;
|
|
39
|
+
var DEFAULT_REPLAY_MAX_AGE_MS = 3e5;
|
|
40
|
+
var DEFAULT_MAX_PAYLOAD_BYTES = 7e3;
|
|
41
|
+
var DEFAULT_TICKET_TTL_SEC = 30;
|
|
42
|
+
|
|
43
|
+
// src/events/events.service.ts
|
|
44
|
+
import { randomUUID } from "crypto";
|
|
45
|
+
import {
|
|
46
|
+
Inject,
|
|
47
|
+
Injectable,
|
|
48
|
+
Logger
|
|
49
|
+
} from "@nestjs/common";
|
|
50
|
+
import { Observable } from "rxjs";
|
|
51
|
+
|
|
52
|
+
// src/events/events.types.ts
|
|
53
|
+
var RESYNC_MODULE = "channel";
|
|
54
|
+
var RESYNC_TYPE = "resync";
|
|
55
|
+
|
|
56
|
+
// src/events/events.service.ts
|
|
57
|
+
var EventsService = class {
|
|
58
|
+
constructor(options) {
|
|
59
|
+
this.options = options;
|
|
60
|
+
this.log = new Logger("Events");
|
|
61
|
+
this.buffers = /* @__PURE__ */ new Map();
|
|
62
|
+
this.prunedUpTo = /* @__PURE__ */ new Map();
|
|
63
|
+
this.live = /* @__PURE__ */ new Set();
|
|
64
|
+
this.observers = /* @__PURE__ */ new Set();
|
|
65
|
+
this.lastId = 0;
|
|
66
|
+
this.timer = null;
|
|
67
|
+
this.instance = options.instanceId ?? randomUUID();
|
|
68
|
+
this.listeningSince = this.nextId();
|
|
69
|
+
}
|
|
70
|
+
async onModuleInit() {
|
|
71
|
+
this.timer = setInterval(() => this.prune(), 6e4);
|
|
72
|
+
this.timer.unref?.();
|
|
73
|
+
const bus = this.options.bus;
|
|
74
|
+
if (!bus) return;
|
|
75
|
+
try {
|
|
76
|
+
await bus.listen((m) => this.receive(m));
|
|
77
|
+
this.listeningSince = this.nextId();
|
|
78
|
+
this.log.log(`events between instances over ${bus.kind}`);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
this.log.error(`could not listen to the ${bus.kind} bus: ${e.message}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async beforeApplicationShutdown() {
|
|
84
|
+
if (this.timer) clearInterval(this.timer);
|
|
85
|
+
await this.options.bus?.close().catch(() => void 0);
|
|
86
|
+
for (const l of [...this.live]) this.live.delete(l);
|
|
87
|
+
}
|
|
88
|
+
/** Publish a signal to a scope. Throws on an unknown `module` or an empty scope (programming errors). */
|
|
89
|
+
publish(scope, input) {
|
|
90
|
+
if (!scope) throw new Error("events.publish: empty scope");
|
|
91
|
+
const kinds = this.options.kinds;
|
|
92
|
+
if (kinds && input.module !== RESYNC_MODULE && !kinds.includes(input.module)) {
|
|
93
|
+
throw new Error(`events.publish: unknown module "${input.module}"`);
|
|
94
|
+
}
|
|
95
|
+
const signal = {
|
|
96
|
+
id: this.nextId(),
|
|
97
|
+
scope,
|
|
98
|
+
module: input.module,
|
|
99
|
+
type: input.type,
|
|
100
|
+
...input.ref !== void 0 ? { ref: input.ref } : {},
|
|
101
|
+
...input.extra !== void 0 ? { extra: input.extra } : {},
|
|
102
|
+
at: new Date(this.now()).toISOString()
|
|
103
|
+
};
|
|
104
|
+
this.deliver(signal);
|
|
105
|
+
for (const o of this.observers) {
|
|
106
|
+
try {
|
|
107
|
+
o(signal);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
this.log.warn(`events observer: ${e.message}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.broadcast(signal);
|
|
113
|
+
return signal;
|
|
114
|
+
}
|
|
115
|
+
/** Tell everybody in the scope to re-read everything (permissions changed, bulk import…). */
|
|
116
|
+
resync(scope) {
|
|
117
|
+
return this.publish(scope, { module: RESYNC_MODULE, type: RESYNC_TYPE });
|
|
118
|
+
}
|
|
119
|
+
/** React to what is published on THIS instance (not to what arrives over the bus). Returns the unsubscribe. */
|
|
120
|
+
observe(fn) {
|
|
121
|
+
this.observers.add(fn);
|
|
122
|
+
return () => this.observers.delete(fn);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Observable for SSE. Emits `ready` (first connection) or the missed signals / `resync` (reconnection with
|
|
126
|
+
* `lastEventId`), then live signals. Unsubscribing disconnects.
|
|
127
|
+
*/
|
|
128
|
+
stream(scope, lastEventId, filter) {
|
|
129
|
+
return new Observable((sub) => {
|
|
130
|
+
const ok = (s) => !filter || filter(s);
|
|
131
|
+
const from = Number(lastEventId);
|
|
132
|
+
let seen = 0;
|
|
133
|
+
if (Number.isFinite(from) && from > 0) {
|
|
134
|
+
const missed = this.replay(scope, from);
|
|
135
|
+
if (missed === null) sub.next({ kind: "resync" });
|
|
136
|
+
else
|
|
137
|
+
for (const s of missed) {
|
|
138
|
+
seen = s.id;
|
|
139
|
+
if (ok(s)) sub.next({ kind: "signal", signal: s });
|
|
140
|
+
}
|
|
141
|
+
seen = Math.max(seen, from);
|
|
142
|
+
} else {
|
|
143
|
+
sub.next({ kind: "ready" });
|
|
144
|
+
}
|
|
145
|
+
const entry = {
|
|
146
|
+
scope,
|
|
147
|
+
push: (s) => {
|
|
148
|
+
if (s.id <= seen || !ok(s)) return;
|
|
149
|
+
seen = s.id;
|
|
150
|
+
sub.next({ kind: "signal", signal: s });
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
this.live.add(entry);
|
|
154
|
+
return () => void this.live.delete(entry);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/** Signals after `lastId`, or `null` if this instance cannot guarantee it has all of them. */
|
|
158
|
+
replay(scope, lastId) {
|
|
159
|
+
this.prune();
|
|
160
|
+
const list = this.buffers.get(scope) ?? [];
|
|
161
|
+
const covered = Math.max(this.listeningSince, this.prunedUpTo.get(scope) ?? 0);
|
|
162
|
+
if (lastId < covered) return null;
|
|
163
|
+
return list.filter((s) => s.id > lastId);
|
|
164
|
+
}
|
|
165
|
+
connections() {
|
|
166
|
+
return this.live.size;
|
|
167
|
+
}
|
|
168
|
+
/** Drop signals beyond the window or older than the max age. Also runs every minute and on replay. */
|
|
169
|
+
prune() {
|
|
170
|
+
const cut = this.now() - (this.options.replayMaxAgeMs ?? DEFAULT_REPLAY_MAX_AGE_MS);
|
|
171
|
+
const max = this.options.replayWindow ?? DEFAULT_REPLAY_WINDOW;
|
|
172
|
+
for (const [scope, list] of this.buffers) {
|
|
173
|
+
while (list.length > max || list[0] && Date.parse(list[0].at) < cut) {
|
|
174
|
+
const dropped = list.shift();
|
|
175
|
+
this.prunedUpTo.set(scope, Math.max(this.prunedUpTo.get(scope) ?? 0, dropped.id));
|
|
176
|
+
}
|
|
177
|
+
if (list.length === 0) this.buffers.delete(scope);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
now() {
|
|
181
|
+
return this.options.now?.() ?? Date.now();
|
|
182
|
+
}
|
|
183
|
+
/** Microseconds since 1970, always increasing on this instance. */
|
|
184
|
+
nextId() {
|
|
185
|
+
this.lastId = Math.max(this.lastId + 1, this.now() * 1e3);
|
|
186
|
+
return this.lastId;
|
|
187
|
+
}
|
|
188
|
+
deliver(signal) {
|
|
189
|
+
this.lastId = Math.max(this.lastId, signal.id);
|
|
190
|
+
const list = this.buffers.get(signal.scope) ?? [];
|
|
191
|
+
let i = list.length;
|
|
192
|
+
while (i > 0 && list[i - 1].id > signal.id) i--;
|
|
193
|
+
list.splice(i, 0, signal);
|
|
194
|
+
this.buffers.set(signal.scope, list);
|
|
195
|
+
this.prune();
|
|
196
|
+
for (const l of [...this.live]) {
|
|
197
|
+
if (l.scope !== signal.scope) continue;
|
|
198
|
+
try {
|
|
199
|
+
l.push(signal);
|
|
200
|
+
} catch (e) {
|
|
201
|
+
this.log.warn(`delivery to a connection failed: ${e.message}`);
|
|
202
|
+
this.live.delete(l);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
broadcast(signal) {
|
|
207
|
+
const bus = this.options.bus;
|
|
208
|
+
if (!bus) return;
|
|
209
|
+
const max = this.options.maxPayloadBytes ?? DEFAULT_MAX_PAYLOAD_BYTES;
|
|
210
|
+
const fits = (s) => Buffer.byteLength(JSON.stringify({ o: this.instance, s })) <= max;
|
|
211
|
+
let out = signal;
|
|
212
|
+
if (!fits(out)) {
|
|
213
|
+
out = { id: signal.id, scope: signal.scope, module: RESYNC_MODULE, type: RESYNC_TYPE, at: signal.at };
|
|
214
|
+
this.log.warn(`signal ${signal.module}.${signal.type} exceeds ${max} bytes; sent as ${RESYNC_MODULE}.${RESYNC_TYPE}`);
|
|
215
|
+
if (!fits(out)) return;
|
|
216
|
+
}
|
|
217
|
+
void bus.publish(JSON.stringify({ o: this.instance, s: out })).catch((e) => this.log.warn(`events bus: ${e.message}`));
|
|
218
|
+
}
|
|
219
|
+
receive(message) {
|
|
220
|
+
let m;
|
|
221
|
+
try {
|
|
222
|
+
m = JSON.parse(message);
|
|
223
|
+
} catch {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const s = m.s;
|
|
227
|
+
if (m.o === this.instance || !s) return;
|
|
228
|
+
if (typeof s.id !== "number" || typeof s.scope !== "string" || typeof s.module !== "string" || typeof s.type !== "string")
|
|
229
|
+
return;
|
|
230
|
+
this.deliver({
|
|
231
|
+
id: s.id,
|
|
232
|
+
scope: s.scope,
|
|
233
|
+
module: s.module,
|
|
234
|
+
type: s.type,
|
|
235
|
+
...s.ref !== void 0 ? { ref: s.ref } : {},
|
|
236
|
+
...s.extra !== void 0 ? { extra: s.extra } : {},
|
|
237
|
+
at: typeof s.at === "string" ? s.at : new Date(this.now()).toISOString()
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
EventsService = __decorateClass([
|
|
242
|
+
Injectable(),
|
|
243
|
+
__decorateParam(0, Inject(EVENTS_OPTIONS))
|
|
244
|
+
], EventsService);
|
|
245
|
+
|
|
246
|
+
// src/events/events.controller.ts
|
|
247
|
+
var DEFAULT_HEARTBEAT_MS = 25e3;
|
|
248
|
+
var DEFAULT_MAX_BACKLOG_BYTES = 64 * 1024;
|
|
249
|
+
function formatSignal(s) {
|
|
250
|
+
return `id: ${s.id}
|
|
251
|
+
event: signal
|
|
252
|
+
data: ${JSON.stringify(s)}
|
|
253
|
+
|
|
254
|
+
`;
|
|
255
|
+
}
|
|
256
|
+
function createEventsController(path = "events") {
|
|
257
|
+
let EventsController = class {
|
|
258
|
+
constructor(events, options, tickets) {
|
|
259
|
+
this.events = events;
|
|
260
|
+
this.options = options;
|
|
261
|
+
this.tickets = tickets;
|
|
262
|
+
}
|
|
263
|
+
async stream(req, res, header, since, ticket) {
|
|
264
|
+
let user;
|
|
265
|
+
let scope;
|
|
266
|
+
if (ticket) {
|
|
267
|
+
const payload = await this.tickets.take(ticket);
|
|
268
|
+
if (!payload) throw new UnauthorizedAppException("Invalid or expired ticket", void 0, "EVENTS_TICKET_INVALID");
|
|
269
|
+
user = payload.user;
|
|
270
|
+
scope = payload.scope;
|
|
271
|
+
} else {
|
|
272
|
+
user = this.userOf(req);
|
|
273
|
+
scope = this.options.scopeOf?.(req);
|
|
274
|
+
}
|
|
275
|
+
if (user === void 0 || user === null) throw new UnauthorizedAppException();
|
|
276
|
+
if (!scope) throw new ForbiddenAppException("No scope", void 0, "EVENTS_NO_SCOPE");
|
|
277
|
+
if (!await this.allowed(user, scope)) throw new ForbiddenAppException(void 0, void 0, "EVENTS_FORBIDDEN");
|
|
278
|
+
const cfg = this.options.controller || {};
|
|
279
|
+
const heartbeatMs = cfg.heartbeatMs ?? DEFAULT_HEARTBEAT_MS;
|
|
280
|
+
const maxBacklog = cfg.maxBacklogBytes ?? DEFAULT_MAX_BACKLOG_BYTES;
|
|
281
|
+
const canRead = this.options.canRead;
|
|
282
|
+
const write = (text) => {
|
|
283
|
+
res.write(text);
|
|
284
|
+
res.flush?.();
|
|
285
|
+
};
|
|
286
|
+
res.status(200).set({
|
|
287
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
288
|
+
"Cache-Control": "no-cache, no-transform",
|
|
289
|
+
Connection: "keep-alive",
|
|
290
|
+
"X-Accel-Buffering": "no",
|
|
291
|
+
"Content-Encoding": "identity"
|
|
292
|
+
});
|
|
293
|
+
res.flushHeaders?.();
|
|
294
|
+
write("retry: 3000\n\n");
|
|
295
|
+
let closed = false;
|
|
296
|
+
let subscription;
|
|
297
|
+
let beat;
|
|
298
|
+
const close = () => {
|
|
299
|
+
if (closed) return;
|
|
300
|
+
closed = true;
|
|
301
|
+
if (beat) clearInterval(beat);
|
|
302
|
+
subscription?.unsubscribe();
|
|
303
|
+
res.end();
|
|
304
|
+
};
|
|
305
|
+
const slow = () => (res.writableLength ?? 0) > maxBacklog;
|
|
306
|
+
subscription = this.events.stream(scope, header ?? since, canRead ? (s) => canRead(user, s) : void 0).subscribe((m) => {
|
|
307
|
+
if (closed) return;
|
|
308
|
+
if (slow()) return close();
|
|
309
|
+
if (m.kind === "signal") write(formatSignal(m.signal));
|
|
310
|
+
else write(m.kind === "ready" ? "event: ready\ndata: {}\n\n" : "event: resync\ndata: {}\n\n");
|
|
311
|
+
});
|
|
312
|
+
if (closed) {
|
|
313
|
+
subscription.unsubscribe();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
beat = setInterval(() => slow() ? close() : write(": heartbeat\n\n"), heartbeatMs);
|
|
317
|
+
beat.unref?.();
|
|
318
|
+
req.on?.("close", close);
|
|
319
|
+
}
|
|
320
|
+
async ticket(req) {
|
|
321
|
+
const cfg = this.options.ticket;
|
|
322
|
+
if (cfg === false) throw new NotFoundAppException();
|
|
323
|
+
const user = this.userOf(req);
|
|
324
|
+
if (user === void 0 || user === null) throw new UnauthorizedAppException();
|
|
325
|
+
const scope = this.options.scopeOf?.(req);
|
|
326
|
+
if (!scope) throw new ForbiddenAppException("No scope", void 0, "EVENTS_NO_SCOPE");
|
|
327
|
+
if (!await this.allowed(user, scope)) throw new ForbiddenAppException(void 0, void 0, "EVENTS_FORBIDDEN");
|
|
328
|
+
const ttlSec = cfg?.ttlSec ?? DEFAULT_TICKET_TTL_SEC;
|
|
329
|
+
const id = randomBytes(24).toString("base64url");
|
|
330
|
+
await this.tickets.put(id, { user, scope }, ttlSec);
|
|
331
|
+
return { ticket: id, expiresInSec: ttlSec };
|
|
332
|
+
}
|
|
333
|
+
userOf(req) {
|
|
334
|
+
return this.options.getUser ? this.options.getUser(req) : req?.user;
|
|
335
|
+
}
|
|
336
|
+
async allowed(user, scope) {
|
|
337
|
+
return this.options.authorize ? await this.options.authorize(user, scope) : true;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
__decorateClass([
|
|
341
|
+
Get(),
|
|
342
|
+
__decorateParam(0, Req()),
|
|
343
|
+
__decorateParam(1, Res()),
|
|
344
|
+
__decorateParam(2, Headers("last-event-id")),
|
|
345
|
+
__decorateParam(3, Query("desde")),
|
|
346
|
+
__decorateParam(4, Query("ticket"))
|
|
347
|
+
], EventsController.prototype, "stream", 1);
|
|
348
|
+
__decorateClass([
|
|
349
|
+
Post("ticket"),
|
|
350
|
+
__decorateParam(0, Req())
|
|
351
|
+
], EventsController.prototype, "ticket", 1);
|
|
352
|
+
EventsController = __decorateClass([
|
|
353
|
+
Controller(path),
|
|
354
|
+
__decorateParam(0, Inject2(EventsService)),
|
|
355
|
+
__decorateParam(1, Inject2(EVENTS_OPTIONS)),
|
|
356
|
+
__decorateParam(2, Inject2(EVENTS_TICKET_STORE))
|
|
357
|
+
], EventsController);
|
|
358
|
+
return EventsController;
|
|
359
|
+
}
|
|
360
|
+
function decorateRoute(controller, method, decorators) {
|
|
361
|
+
if (!decorators?.length) return;
|
|
362
|
+
const descriptor = Object.getOwnPropertyDescriptor(controller.prototype, method);
|
|
363
|
+
applyDecorators(...decorators)(controller.prototype, method, descriptor);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/events/memory-ticket.store.ts
|
|
367
|
+
var MemoryTicketStore = class {
|
|
368
|
+
constructor(now = Date.now) {
|
|
369
|
+
this.now = now;
|
|
370
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
371
|
+
}
|
|
372
|
+
async put(id, payload, ttlSec) {
|
|
373
|
+
const t = this.now();
|
|
374
|
+
for (const [k, v] of this.rows) if (v.exp <= t) this.rows.delete(k);
|
|
375
|
+
this.rows.set(id, { payload, exp: t + ttlSec * 1e3 });
|
|
376
|
+
}
|
|
377
|
+
async take(id) {
|
|
378
|
+
const row = this.rows.get(id);
|
|
379
|
+
if (!row) return null;
|
|
380
|
+
this.rows.delete(id);
|
|
381
|
+
return row.exp > this.now() ? row.payload : null;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// src/events/events.module.ts
|
|
386
|
+
function controllersFor(c) {
|
|
387
|
+
if (c === false) return [];
|
|
388
|
+
const cfg = c ?? {};
|
|
389
|
+
const ctl = createEventsController(cfg.path ?? "events");
|
|
390
|
+
decorateRoute(ctl, "ticket", cfg.ticketDecorators);
|
|
391
|
+
decorateRoute(ctl, "stream", cfg.streamDecorators);
|
|
392
|
+
return [ctl];
|
|
393
|
+
}
|
|
394
|
+
var storeProvider = () => ({
|
|
395
|
+
provide: EVENTS_TICKET_STORE,
|
|
396
|
+
useFactory: (...args) => {
|
|
397
|
+
const o = args[0];
|
|
398
|
+
return o.ticket !== false && o.ticket?.store || new MemoryTicketStore(o.now);
|
|
399
|
+
},
|
|
400
|
+
inject: [EVENTS_OPTIONS]
|
|
401
|
+
});
|
|
402
|
+
var EventsModule = class {
|
|
403
|
+
static forRoot(options = {}) {
|
|
404
|
+
const providers = [
|
|
405
|
+
{ provide: EVENTS_OPTIONS, useValue: options },
|
|
406
|
+
storeProvider(),
|
|
407
|
+
EventsService
|
|
408
|
+
];
|
|
409
|
+
return {
|
|
410
|
+
module: EventsModule,
|
|
411
|
+
controllers: controllersFor(options.controller),
|
|
412
|
+
providers,
|
|
413
|
+
exports: [EVENTS_OPTIONS, EVENTS_TICKET_STORE, EventsService]
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/** `controller` is repeated here because routes must be known before the factory runs. */
|
|
417
|
+
static forRootAsync(config) {
|
|
418
|
+
const providers = [
|
|
419
|
+
{ provide: EVENTS_OPTIONS, useFactory: config.useFactory, inject: config.inject ?? [] },
|
|
420
|
+
storeProvider(),
|
|
421
|
+
EventsService
|
|
422
|
+
];
|
|
423
|
+
return {
|
|
424
|
+
module: EventsModule,
|
|
425
|
+
imports: config.imports ?? [],
|
|
426
|
+
controllers: controllersFor(config.controller),
|
|
427
|
+
providers,
|
|
428
|
+
exports: [EVENTS_OPTIONS, EVENTS_TICKET_STORE, EventsService]
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
EventsModule = __decorateClass([
|
|
433
|
+
Global(),
|
|
434
|
+
Module({})
|
|
435
|
+
], EventsModule);
|
|
436
|
+
|
|
437
|
+
// src/events/buses.ts
|
|
438
|
+
import { Logger as Logger2 } from "@nestjs/common";
|
|
439
|
+
var IDENT = /^[a-z_][a-z0-9_]{0,62}$/i;
|
|
440
|
+
function PgNotifyBus(options) {
|
|
441
|
+
if (!IDENT.test(options.channel)) throw new Error(`PgNotifyBus: invalid channel "${options.channel}"`);
|
|
442
|
+
const log = new Logger2("EventsPgBus");
|
|
443
|
+
const { channel } = options;
|
|
444
|
+
let client = null;
|
|
445
|
+
let onMessage = null;
|
|
446
|
+
let closed = false;
|
|
447
|
+
let timer = null;
|
|
448
|
+
const factory = typeof options.client === "function" ? options.client : null;
|
|
449
|
+
const drop = (c, e) => {
|
|
450
|
+
if (client !== c) return;
|
|
451
|
+
client = null;
|
|
452
|
+
if (e) log.warn(`LISTEN connection dropped: ${e.message}`);
|
|
453
|
+
void c.end?.().catch(() => void 0);
|
|
454
|
+
if (factory && !closed) {
|
|
455
|
+
timer = setTimeout(() => void connect(), options.reconnectMs ?? 5e3);
|
|
456
|
+
timer.unref?.();
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
const connect = async () => {
|
|
460
|
+
if (closed) return;
|
|
461
|
+
const c = factory ? await factory() : options.client;
|
|
462
|
+
c.on("notification", (n) => {
|
|
463
|
+
if (n.channel === channel && n.payload) onMessage?.(n.payload);
|
|
464
|
+
});
|
|
465
|
+
c.on("error", (e) => drop(c, e));
|
|
466
|
+
c.on("end", () => drop(c));
|
|
467
|
+
await c.query(`LISTEN ${channel}`);
|
|
468
|
+
client = c;
|
|
469
|
+
};
|
|
470
|
+
return {
|
|
471
|
+
kind: "postgres",
|
|
472
|
+
async publish(message) {
|
|
473
|
+
if (!client) throw new Error("no LISTEN/NOTIFY connection");
|
|
474
|
+
await client.query("SELECT pg_notify($1, $2)", [channel, message]);
|
|
475
|
+
},
|
|
476
|
+
async listen(cb) {
|
|
477
|
+
onMessage = cb;
|
|
478
|
+
try {
|
|
479
|
+
await connect();
|
|
480
|
+
} catch (e) {
|
|
481
|
+
if (!factory) throw e;
|
|
482
|
+
log.warn(`first LISTEN failed: ${e.message}; retrying`);
|
|
483
|
+
timer = setTimeout(() => void connect().catch(() => void 0), options.reconnectMs ?? 5e3);
|
|
484
|
+
timer.unref?.();
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
async close() {
|
|
488
|
+
closed = true;
|
|
489
|
+
if (timer) clearTimeout(timer);
|
|
490
|
+
const c = client;
|
|
491
|
+
client = null;
|
|
492
|
+
await c?.end?.().catch(() => void 0);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
function RedisBus(options) {
|
|
497
|
+
const channel = options.channel ?? "events";
|
|
498
|
+
let sub = null;
|
|
499
|
+
return {
|
|
500
|
+
kind: "redis",
|
|
501
|
+
async publish(message) {
|
|
502
|
+
await options.client.publish(channel, message);
|
|
503
|
+
},
|
|
504
|
+
async listen(cb) {
|
|
505
|
+
sub = options.subscriber ?? options.client.duplicate?.() ?? null;
|
|
506
|
+
if (!sub) throw new Error("RedisBus: pass `subscriber` or a client with duplicate()");
|
|
507
|
+
sub.on("message", (ch, m) => {
|
|
508
|
+
if (ch === channel) cb(m);
|
|
509
|
+
});
|
|
510
|
+
await sub.subscribe(channel);
|
|
511
|
+
},
|
|
512
|
+
async close() {
|
|
513
|
+
const s = sub;
|
|
514
|
+
sub = null;
|
|
515
|
+
if (!s) return;
|
|
516
|
+
await s.unsubscribe?.(channel).catch(() => void 0);
|
|
517
|
+
if (s.quit) await s.quit().catch(() => void 0);
|
|
518
|
+
else s.disconnect?.();
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function createMemoryBusHub() {
|
|
523
|
+
const members = /* @__PURE__ */ new Set();
|
|
524
|
+
return {
|
|
525
|
+
connect() {
|
|
526
|
+
let cb = null;
|
|
527
|
+
return {
|
|
528
|
+
kind: "memory",
|
|
529
|
+
async publish(message) {
|
|
530
|
+
for (const m of [...members]) queueMicrotask(() => m(message));
|
|
531
|
+
},
|
|
532
|
+
async listen(onMessage) {
|
|
533
|
+
cb = onMessage;
|
|
534
|
+
members.add(onMessage);
|
|
535
|
+
},
|
|
536
|
+
async close() {
|
|
537
|
+
if (cb) members.delete(cb);
|
|
538
|
+
cb = null;
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
export {
|
|
545
|
+
EVENTS_OPTIONS,
|
|
546
|
+
EVENTS_TICKET_STORE,
|
|
547
|
+
EventsModule,
|
|
548
|
+
EventsService,
|
|
549
|
+
MemoryTicketStore,
|
|
550
|
+
PgNotifyBus,
|
|
551
|
+
RESYNC_MODULE,
|
|
552
|
+
RESYNC_TYPE,
|
|
553
|
+
RedisBus,
|
|
554
|
+
createEventsController,
|
|
555
|
+
createMemoryBusHub,
|
|
556
|
+
formatSignal
|
|
557
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TicketPayload, TicketStorePort } from './events.types.js';
|
|
2
|
+
/** In-process ticket store: single instance only (use a shared one with several). */
|
|
3
|
+
export declare class MemoryTicketStore implements TicketStorePort {
|
|
4
|
+
private readonly now;
|
|
5
|
+
private readonly rows;
|
|
6
|
+
constructor(now?: () => number);
|
|
7
|
+
put(id: string, payload: TicketPayload, ttlSec: number): Promise<void>;
|
|
8
|
+
take(id: string): Promise<TicketPayload | null>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { GoogleBusinessException } from './google-business.errors.js';
|
|
2
|
+
import type { FetchLike } from './google-business.options.js';
|
|
3
|
+
export type HttpContext = 'oauth' | 'api' | 'verificacion' | 'pin';
|
|
4
|
+
export interface HttpRequest {
|
|
5
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
6
|
+
url: string;
|
|
7
|
+
accessToken?: string;
|
|
8
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
9
|
+
json?: unknown;
|
|
10
|
+
form?: Record<string, string>;
|
|
11
|
+
/** Force-allow retries on a POST that is idempotent by construction (e.g. carries a `requestId`). */
|
|
12
|
+
idempotent?: boolean;
|
|
13
|
+
context?: HttpContext;
|
|
14
|
+
}
|
|
15
|
+
export interface HttpConfig {
|
|
16
|
+
fetchImpl: FetchLike;
|
|
17
|
+
maxRetries: number;
|
|
18
|
+
retryDelayMs: number;
|
|
19
|
+
}
|
|
20
|
+
interface GoogleErrorBody {
|
|
21
|
+
error?: string | {
|
|
22
|
+
code?: number;
|
|
23
|
+
message?: string;
|
|
24
|
+
status?: string;
|
|
25
|
+
errors?: Array<{
|
|
26
|
+
reason?: string;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
error_description?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Maps a failed response to a stable exception. Exported for the specs. */
|
|
32
|
+
export declare function classifyError(status: number, body: GoogleErrorBody | null, context: HttpContext): GoogleBusinessException;
|
|
33
|
+
/** Thin JSON client: bearer auth, query/form/json bodies, safe retries, error mapping. */
|
|
34
|
+
export declare class GoogleBusinessHttp {
|
|
35
|
+
private readonly config;
|
|
36
|
+
constructor(config: HttpConfig);
|
|
37
|
+
request<T>(req: HttpRequest): Promise<T>;
|
|
38
|
+
private buildUrl;
|
|
39
|
+
private buildInit;
|
|
40
|
+
}
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { HttpStatus } from '@nestjs/common';
|
|
2
|
+
import { AppException } from '../errors/app.exception.js';
|
|
3
|
+
/**
|
|
4
|
+
* Stable, machine-readable codes. The host branches on `code`, never on the
|
|
5
|
+
* text; the messages are Spanish defaults that `GlobalExceptionFilter` can
|
|
6
|
+
* replace by looking the code up under its i18n namespace.
|
|
7
|
+
*/
|
|
8
|
+
export declare const GOOGLE_BUSINESS_ERRORS: {
|
|
9
|
+
/** The owner never connected a Google account (no tokens stored). */
|
|
10
|
+
readonly NO_CONECTADO: {
|
|
11
|
+
readonly status: HttpStatus.UNAUTHORIZED;
|
|
12
|
+
readonly message: "Esta cuenta no tiene Google conectado.";
|
|
13
|
+
};
|
|
14
|
+
/** The user revoked access, or the refresh token no longer works: reconnect. */
|
|
15
|
+
readonly TOKEN_REVOCADO: {
|
|
16
|
+
readonly status: HttpStatus.UNAUTHORIZED;
|
|
17
|
+
readonly message: "Google ya no autoriza esta conexión. Vuelve a conectar tu cuenta de Google.";
|
|
18
|
+
};
|
|
19
|
+
/** The OAuth `state` is forged, malformed or expired. */
|
|
20
|
+
readonly ESTADO_INVALIDO: {
|
|
21
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
22
|
+
readonly message: "La solicitud de conexión con Google no es válida o caducó.";
|
|
23
|
+
};
|
|
24
|
+
/** Google rejected the authorization code (used twice, expired, wrong redirect). */
|
|
25
|
+
readonly CODIGO_INVALIDO: {
|
|
26
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
27
|
+
readonly message: "Google rechazó el código de autorización. Inténtalo de nuevo.";
|
|
28
|
+
};
|
|
29
|
+
/** Quota or rate limit hit. */
|
|
30
|
+
readonly CUOTA: {
|
|
31
|
+
readonly status: HttpStatus.TOO_MANY_REQUESTS;
|
|
32
|
+
readonly message: "Google limitó las solicitudes por ahora. Espera un momento e inténtalo de nuevo.";
|
|
33
|
+
};
|
|
34
|
+
/** The business (or its address/phone) already exists in Google. */
|
|
35
|
+
readonly DUPLICADO: {
|
|
36
|
+
readonly status: HttpStatus.CONFLICT;
|
|
37
|
+
readonly message: "Este negocio ya existe en Google. Vincúlalo en lugar de crearlo.";
|
|
38
|
+
};
|
|
39
|
+
/** The location cannot be verified (or the chosen method is not offered). */
|
|
40
|
+
readonly NO_VERIFICABLE: {
|
|
41
|
+
readonly status: HttpStatus.UNPROCESSABLE_ENTITY;
|
|
42
|
+
readonly message: "Google no permite verificar este negocio con ese método.";
|
|
43
|
+
};
|
|
44
|
+
/** Wrong or expired verification PIN. */
|
|
45
|
+
readonly PIN_INVALIDO: {
|
|
46
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
47
|
+
readonly message: "El código de verificación es incorrecto o caducó.";
|
|
48
|
+
};
|
|
49
|
+
/** The Google account lacks rights over that account/location. */
|
|
50
|
+
readonly SIN_PERMISO: {
|
|
51
|
+
readonly status: HttpStatus.FORBIDDEN;
|
|
52
|
+
readonly message: "Tu cuenta de Google no tiene permiso sobre este perfil de negocio.";
|
|
53
|
+
};
|
|
54
|
+
readonly NO_ENCONTRADO: {
|
|
55
|
+
readonly status: HttpStatus.NOT_FOUND;
|
|
56
|
+
readonly message: "Google no encontró ese recurso.";
|
|
57
|
+
};
|
|
58
|
+
/** Google found the payload invalid (`details` carries its message). */
|
|
59
|
+
readonly PETICION_INVALIDA: {
|
|
60
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
61
|
+
readonly message: "Google rechazó los datos enviados.";
|
|
62
|
+
};
|
|
63
|
+
/** Network failure or 5xx after retries. */
|
|
64
|
+
readonly NO_DISPONIBLE: {
|
|
65
|
+
readonly status: HttpStatus.BAD_GATEWAY;
|
|
66
|
+
readonly message: "No pudimos comunicarnos con Google. Inténtalo más tarde.";
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export type GoogleBusinessErrorKey = keyof typeof GOOGLE_BUSINESS_ERRORS;
|
|
70
|
+
export type GoogleBusinessErrorCode = `GOOGLE_BUSINESS_${GoogleBusinessErrorKey}`;
|
|
71
|
+
export declare class GoogleBusinessException extends AppException {
|
|
72
|
+
readonly code: GoogleBusinessErrorCode;
|
|
73
|
+
constructor(key: GoogleBusinessErrorKey, details?: unknown, message?: string);
|
|
74
|
+
}
|
|
75
|
+
export declare function isGoogleBusinessException(e: unknown, key?: GoogleBusinessErrorKey): e is GoogleBusinessException;
|