@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
package/README.md
CHANGED
|
@@ -1415,6 +1415,11 @@ subidos por la persona, que necesitan un permiso que la app no tiene, y el callb
|
|
|
1415
1415
|
500. El flujo entero son unas cuarenta líneas de `fetch` y cada campo que se manda se ve en
|
|
1416
1416
|
`oauth-provider.ts`.
|
|
1417
1417
|
|
|
1418
|
+
**Red inyectable (0.26.0, opcional).** Todo proveedor acepta `fetchImpl?: typeof fetch` en sus credenciales
|
|
1419
|
+
(`googleProvider({ fetchImpl })`, etc.): lo usa para el canje del `code` por el token y para leer el perfil
|
|
1420
|
+
(Apple, también para sus llaves públicas). Sirve para pasar por un cliente con tiempo límite, reintentos y
|
|
1421
|
+
alertas. Sin la opción se usa el `fetch` global, igual que antes.
|
|
1422
|
+
|
|
1418
1423
|
Un proveedor es un objeto, no una clase que heredar: vienen ocho —`googleProvider`,
|
|
1419
1424
|
`appleProvider`, `facebookProvider`, `tiktokProvider`, `githubProvider`, `microsoftProvider`,
|
|
1420
1425
|
`discordProvider`, `linkedinProvider`— y uno nuevo es declarar cuatro URLs y una función que mapee su
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For cron/job methods: if they fail, alert (level `error`) and rethrow so the scheduler logs it as
|
|
3
|
+
* before. Put it under `@Cron(...)`. The same job failing repeatedly = one alert per dedupe window.
|
|
4
|
+
*/
|
|
5
|
+
export declare function AlertOnFailure(name?: string): MethodDecorator;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { AlertMessage, AlertSinkPort, WebhookSinkConfig } from './alerts.types.js';
|
|
2
|
+
/** POSTs the alert as JSON. Throws on a non-2xx so the service can log it; the service swallows it. */
|
|
3
|
+
export declare class WebhookAlertSink implements AlertSinkPort {
|
|
4
|
+
private readonly config;
|
|
5
|
+
constructor(config: WebhookSinkConfig);
|
|
6
|
+
send(alert: AlertMessage): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/** Keeps what it receives; for tests and local development. */
|
|
9
|
+
export declare class MemoryAlertSink implements AlertSinkPort {
|
|
10
|
+
readonly sent: AlertMessage[];
|
|
11
|
+
send(alert: AlertMessage): Promise<void>;
|
|
12
|
+
clear(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare function isSinkPort(sink: unknown): sink is AlertSinkPort;
|
|
15
|
+
/**
|
|
16
|
+
* Adapts an {@link AlertSinkPort} to the shape of `resilience`'s `AlertChannelPort`
|
|
17
|
+
* (structural, so `alerts` does not import `resilience`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function alertSinkAsChannel(sink: AlertSinkPort): {
|
|
20
|
+
send(message: {
|
|
21
|
+
title: string;
|
|
22
|
+
text?: string;
|
|
23
|
+
level?: AlertMessage['level'];
|
|
24
|
+
source?: string;
|
|
25
|
+
fields?: AlertMessage['fields'];
|
|
26
|
+
error?: AlertMessage['error'];
|
|
27
|
+
repeated?: number;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { AlertsOptions } from './alerts.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Outbound alerts with dedupe and a per-minute cap.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* AlertsModule.forRoot({ sink: { webhookUrl, bypass }, source: 'api · prod' })
|
|
8
|
+
* // main.ts: app.useLogger(new LoggerWithAlerts(app.get(AlertsService)));
|
|
9
|
+
* // installProcessGuards({ alerts: app.get(AlertsService), onFatal: 'continue' });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare class AlertsModule {
|
|
13
|
+
static forRoot(options?: AlertsOptions): DynamicModule;
|
|
14
|
+
static forRootAsync(config: {
|
|
15
|
+
imports?: any[];
|
|
16
|
+
inject?: any[];
|
|
17
|
+
useFactory: (...args: any[]) => AlertsOptions | Promise<AlertsOptions>;
|
|
18
|
+
}): DynamicModule;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ALERTS_OPTIONS: unique symbol;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AlertPayload, AlertsOptions } from './alerts.types.js';
|
|
2
|
+
/** `Error` (or anything thrown) to the `error` shape of a payload. */
|
|
3
|
+
export declare function errorInfo(e: unknown): {
|
|
4
|
+
name?: string;
|
|
5
|
+
message: string;
|
|
6
|
+
stack?: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Sends alerts without ever throwing or blocking the caller.
|
|
10
|
+
*
|
|
11
|
+
* - `notify()` is fire-and-forget; `notifyAndWait()` resolves (never rejects) with whether it went out.
|
|
12
|
+
* - Own failures go to `console.warn`, never to `Logger.error`: {@link LoggerWithAlerts} calls this,
|
|
13
|
+
* and an error here would loop.
|
|
14
|
+
* - Dedupe per key for `dedupeMs` (the swallowed ones are counted in `repeated` of the next) and a
|
|
15
|
+
* global cap per rolling minute.
|
|
16
|
+
*/
|
|
17
|
+
export declare class AlertsService {
|
|
18
|
+
private readonly options;
|
|
19
|
+
private readonly sink;
|
|
20
|
+
private readonly seen;
|
|
21
|
+
private sent;
|
|
22
|
+
constructor(options: AlertsOptions);
|
|
23
|
+
private now;
|
|
24
|
+
notify(payload: AlertPayload): void;
|
|
25
|
+
/** `true` if the sink accepted it. Never rejects. */
|
|
26
|
+
notifyAndWait(payload: AlertPayload): Promise<boolean>;
|
|
27
|
+
private redact;
|
|
28
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export type AlertLevel = 'info' | 'success' | 'warn' | 'error' | 'critical';
|
|
2
|
+
export type AlertFields = Record<string, string | number | boolean>;
|
|
3
|
+
export interface AlertPayload {
|
|
4
|
+
/** Required; trimmed to 200 chars. */
|
|
5
|
+
title: string;
|
|
6
|
+
/** Trimmed to 3000 chars. */
|
|
7
|
+
text?: string;
|
|
8
|
+
level?: AlertLevel;
|
|
9
|
+
/** Where it comes from ("api · prod"). Falls back to the module's `source`. */
|
|
10
|
+
source?: string;
|
|
11
|
+
/** Context. Goes through `redact` before leaving the process. */
|
|
12
|
+
fields?: AlertFields;
|
|
13
|
+
/** Log/SQL/response, shown monospaced. Trimmed to 3000 chars. */
|
|
14
|
+
code?: string;
|
|
15
|
+
error?: {
|
|
16
|
+
name?: string;
|
|
17
|
+
message: string;
|
|
18
|
+
stack?: string;
|
|
19
|
+
};
|
|
20
|
+
/** At most 4, https only. */
|
|
21
|
+
links?: {
|
|
22
|
+
label: string;
|
|
23
|
+
url: string;
|
|
24
|
+
}[];
|
|
25
|
+
silent?: boolean;
|
|
26
|
+
/** Same key = at most one alert per `dedupeMs`. Defaults to level|source|title. */
|
|
27
|
+
dedupeKey?: string;
|
|
28
|
+
}
|
|
29
|
+
/** What the sink actually receives: already trimmed, redacted and with `repeated` filled in. */
|
|
30
|
+
export interface AlertMessage extends AlertPayload {
|
|
31
|
+
title: string;
|
|
32
|
+
level: AlertLevel;
|
|
33
|
+
/** How many identical alerts were swallowed by the dedupe window since the last one that went out. */
|
|
34
|
+
repeated?: number;
|
|
35
|
+
}
|
|
36
|
+
/** Outbound port (webhook, chat, e-mail…). A sink may throw; the service never lets it escape. */
|
|
37
|
+
export interface AlertSinkPort {
|
|
38
|
+
send(alert: AlertMessage): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export interface AlertsOptions {
|
|
41
|
+
/** A port, or `{ webhookUrl, bypass }` for the built-in webhook sink. Without a sink the module is a silent no-op. */
|
|
42
|
+
sink?: AlertSinkPort | WebhookSinkConfig;
|
|
43
|
+
/** Default `source` of every alert, e.g. "api · prod". */
|
|
44
|
+
source?: string;
|
|
45
|
+
/** Dedupe window. Default 5 minutes. */
|
|
46
|
+
dedupeMs?: number;
|
|
47
|
+
/** Global cap per rolling minute. Default 20. */
|
|
48
|
+
maxPerMinute?: number;
|
|
49
|
+
/** Applied to `fields` (and nothing else) before sending. Must not throw; if it does the fields are dropped. */
|
|
50
|
+
redact?: (fields: AlertFields) => AlertFields;
|
|
51
|
+
/** Levels that go out. Default `['error', 'critical']`. */
|
|
52
|
+
levels?: AlertLevel[];
|
|
53
|
+
/** Turn everything off (tests). Default false. */
|
|
54
|
+
disabled?: boolean;
|
|
55
|
+
/** Clock, for tests. */
|
|
56
|
+
now?: () => number;
|
|
57
|
+
}
|
|
58
|
+
export interface WebhookSinkConfig {
|
|
59
|
+
webhookUrl: string;
|
|
60
|
+
/** Sent as `x-vercel-protection-bypass`. */
|
|
61
|
+
bypass?: string;
|
|
62
|
+
/** Extra headers (e.g. `authorization`). */
|
|
63
|
+
headers?: Record<string, string>;
|
|
64
|
+
timeoutMs?: number;
|
|
65
|
+
fetchImpl?: typeof fetch;
|
|
66
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkLRX7AMSScjs = require('../chunk-LRX7AMSS.cjs');
|
|
5
|
+
|
|
6
|
+
// src/alerts/alerts.module.ts
|
|
7
|
+
var _common = require('@nestjs/common');
|
|
8
|
+
|
|
9
|
+
// src/alerts/alerts.options.ts
|
|
10
|
+
var ALERTS_OPTIONS = /* @__PURE__ */ Symbol("ALERTS_OPTIONS");
|
|
11
|
+
|
|
12
|
+
// src/alerts/alerts.service.ts
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
// src/alerts/alert-sinks.ts
|
|
16
|
+
var WebhookAlertSink = class {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
async send(alert) {
|
|
21
|
+
const { webhookUrl, bypass, headers, timeoutMs = 5e3, fetchImpl = fetch } = this.config;
|
|
22
|
+
const res = await fetchImpl(webhookUrl, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: {
|
|
25
|
+
"content-type": "application/json",
|
|
26
|
+
...bypass ? { "x-vercel-protection-bypass": bypass } : {},
|
|
27
|
+
...headers
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify(alert),
|
|
30
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
31
|
+
});
|
|
32
|
+
if (!res.ok) throw new Error(`alert webhook answered ${res.status}`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var MemoryAlertSink = class {
|
|
36
|
+
constructor() {
|
|
37
|
+
this.sent = [];
|
|
38
|
+
}
|
|
39
|
+
async send(alert) {
|
|
40
|
+
this.sent.push(alert);
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
this.sent.length = 0;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
function isSinkPort(sink) {
|
|
47
|
+
return typeof sink === "object" && sink !== null && typeof sink.send === "function";
|
|
48
|
+
}
|
|
49
|
+
function alertSinkAsChannel(sink) {
|
|
50
|
+
return {
|
|
51
|
+
send: (m) => sink.send({ ...m, level: _nullishCoalesce(m.level, () => ( "error")) })
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/alerts/runtime.ts
|
|
56
|
+
var active = null;
|
|
57
|
+
function setActiveAlerts(service) {
|
|
58
|
+
active = service;
|
|
59
|
+
}
|
|
60
|
+
function getActiveAlerts() {
|
|
61
|
+
return active;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/alerts/alerts.service.ts
|
|
65
|
+
var DEFAULT_LEVELS = ["error", "critical"];
|
|
66
|
+
var cut = (s, n) => s === void 0 ? void 0 : s.length > n ? `${s.slice(0, n - 1)}\u2026` : s;
|
|
67
|
+
function errorInfo(e) {
|
|
68
|
+
if (e instanceof Error) return { name: e.name, message: e.message, stack: _optionalChain([e, 'access', _ => _.stack, 'optionalAccess', _2 => _2.slice, 'call', _3 => _3(0, 2500)]) };
|
|
69
|
+
return { message: String(e) };
|
|
70
|
+
}
|
|
71
|
+
var AlertsService = class {
|
|
72
|
+
constructor(options) {
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.seen = /* @__PURE__ */ new Map();
|
|
75
|
+
this.sent = [];
|
|
76
|
+
const s = options.sink;
|
|
77
|
+
this.sink = !s ? null : isSinkPort(s) ? s : new WebhookAlertSink(s);
|
|
78
|
+
setActiveAlerts(this);
|
|
79
|
+
}
|
|
80
|
+
now() {
|
|
81
|
+
return (_nullishCoalesce(this.options.now, () => ( Date.now)))();
|
|
82
|
+
}
|
|
83
|
+
notify(payload) {
|
|
84
|
+
void this.notifyAndWait(payload);
|
|
85
|
+
}
|
|
86
|
+
/** `true` if the sink accepted it. Never rejects. */
|
|
87
|
+
async notifyAndWait(payload) {
|
|
88
|
+
try {
|
|
89
|
+
if (this.options.disabled || !this.sink) return false;
|
|
90
|
+
const level = _nullishCoalesce(payload.level, () => ( "error"));
|
|
91
|
+
if (!(_nullishCoalesce(this.options.levels, () => ( DEFAULT_LEVELS))).includes(level)) return false;
|
|
92
|
+
const now = this.now();
|
|
93
|
+
const source = _nullishCoalesce(payload.source, () => ( this.options.source));
|
|
94
|
+
const key = _nullishCoalesce(payload.dedupeKey, () => ( `${level}|${_nullishCoalesce(source, () => ( ""))}|${payload.title}`));
|
|
95
|
+
const windowMs = _nullishCoalesce(this.options.dedupeMs, () => ( 3e5));
|
|
96
|
+
const prev = this.seen.get(key);
|
|
97
|
+
if (prev && now - prev.at < windowMs) {
|
|
98
|
+
prev.skipped += 1;
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
this.sent = this.sent.filter((t) => now - t < 6e4);
|
|
102
|
+
if (this.sent.length >= (_nullishCoalesce(this.options.maxPerMinute, () => ( 20)))) return false;
|
|
103
|
+
this.sent.push(now);
|
|
104
|
+
const repeated = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _4 => _4.skipped]), () => ( 0));
|
|
105
|
+
this.seen.set(key, { at: now, skipped: 0 });
|
|
106
|
+
if (this.seen.size > 200) {
|
|
107
|
+
for (const [k, v] of this.seen) if (now - v.at >= windowMs) this.seen.delete(k);
|
|
108
|
+
}
|
|
109
|
+
const message = {
|
|
110
|
+
...payload,
|
|
111
|
+
title: _nullishCoalesce(cut(payload.title, 200), () => ( "Alert")),
|
|
112
|
+
text: cut(payload.text, 3e3),
|
|
113
|
+
code: cut(payload.code, 3e3),
|
|
114
|
+
level,
|
|
115
|
+
source: cut(source, 100),
|
|
116
|
+
links: _optionalChain([payload, 'access', _5 => _5.links, 'optionalAccess', _6 => _6.filter, 'call', _7 => _7((l) => l.url.startsWith("https://")), 'access', _8 => _8.slice, 'call', _9 => _9(0, 4)]),
|
|
117
|
+
fields: this.redact(payload.fields),
|
|
118
|
+
...repeated ? { repeated } : {}
|
|
119
|
+
};
|
|
120
|
+
delete message.dedupeKey;
|
|
121
|
+
await this.sink.send(message);
|
|
122
|
+
return true;
|
|
123
|
+
} catch (e) {
|
|
124
|
+
console.warn(`[alerts] could not send alert: ${String(e)}`);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
redact(fields) {
|
|
129
|
+
if (!fields || !this.options.redact) return fields;
|
|
130
|
+
try {
|
|
131
|
+
return this.options.redact(fields);
|
|
132
|
+
} catch (e2) {
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
AlertsService = exports.AlertsService = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
138
|
+
_common.Injectable.call(void 0, ),
|
|
139
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, ALERTS_OPTIONS))
|
|
140
|
+
], AlertsService);
|
|
141
|
+
|
|
142
|
+
// src/alerts/alerts.module.ts
|
|
143
|
+
var AlertsModule = class {
|
|
144
|
+
static forRoot(options = {}) {
|
|
145
|
+
return {
|
|
146
|
+
module: AlertsModule,
|
|
147
|
+
providers: [{ provide: ALERTS_OPTIONS, useValue: options }, AlertsService],
|
|
148
|
+
exports: [ALERTS_OPTIONS, AlertsService]
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
static forRootAsync(config) {
|
|
152
|
+
return {
|
|
153
|
+
module: AlertsModule,
|
|
154
|
+
imports: _nullishCoalesce(config.imports, () => ( [])),
|
|
155
|
+
providers: [{ provide: ALERTS_OPTIONS, useFactory: config.useFactory, inject: _nullishCoalesce(config.inject, () => ( [])) }, AlertsService],
|
|
156
|
+
exports: [ALERTS_OPTIONS, AlertsService]
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
AlertsModule = exports.AlertsModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
161
|
+
_common.Global.call(void 0, ),
|
|
162
|
+
_common.Module.call(void 0, {})
|
|
163
|
+
], AlertsModule);
|
|
164
|
+
|
|
165
|
+
// src/alerts/logger-with-alerts.ts
|
|
166
|
+
|
|
167
|
+
var LoggerWithAlerts = class extends _common.ConsoleLogger {
|
|
168
|
+
constructor(notifier, context) {
|
|
169
|
+
super(_nullishCoalesce(context, () => ( "")));
|
|
170
|
+
this.notifier = notifier;
|
|
171
|
+
}
|
|
172
|
+
error(message, ...optionalParams) {
|
|
173
|
+
super.error(message, ...optionalParams);
|
|
174
|
+
try {
|
|
175
|
+
this.alert(message, optionalParams);
|
|
176
|
+
} catch (e3) {
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
alert(message, params) {
|
|
180
|
+
const text = typeof message === "string" ? message : String(message);
|
|
181
|
+
const strings = params.filter((p) => typeof p === "string");
|
|
182
|
+
const context = strings.length > 0 ? strings[strings.length - 1] : void 0;
|
|
183
|
+
const stack = strings.length > 1 ? strings[0] : void 0;
|
|
184
|
+
const dedupeKey = `${_nullishCoalesce(context, () => ( ""))}|${text.replace(/[0-9a-f-]{8,}|\d+/gi, "#").slice(0, 80)}`;
|
|
185
|
+
this.notifier.notify({
|
|
186
|
+
title: text.split("\n")[0] || "Error",
|
|
187
|
+
level: "error",
|
|
188
|
+
fields: context ? { context } : void 0,
|
|
189
|
+
error: { message: text, stack },
|
|
190
|
+
dedupeKey
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/alerts/alert-on-failure.decorator.ts
|
|
196
|
+
function AlertOnFailure(name) {
|
|
197
|
+
return (target, key, descriptor) => {
|
|
198
|
+
const label = _nullishCoalesce(name, () => ( `${_nullishCoalesce(_optionalChain([target, 'access', _10 => _10.constructor, 'optionalAccess', _11 => _11.name]), () => ( "job"))}.${String(key)}`));
|
|
199
|
+
const original = descriptor.value;
|
|
200
|
+
descriptor.value = async function(...args) {
|
|
201
|
+
try {
|
|
202
|
+
return await original.apply(this, args);
|
|
203
|
+
} catch (e) {
|
|
204
|
+
const err = errorInfo(e);
|
|
205
|
+
_optionalChain([getActiveAlerts, 'call', _12 => _12(), 'optionalAccess', _13 => _13.notify, 'call', _14 => _14({
|
|
206
|
+
title: `Job "${label}" failed: ${err.message}`.slice(0, 200),
|
|
207
|
+
level: "error",
|
|
208
|
+
source: `job \xB7 ${_nullishCoalesce(process.env["NODE_ENV"], () => ( "development"))}`,
|
|
209
|
+
error: err,
|
|
210
|
+
dedupeKey: `job|${label}`
|
|
211
|
+
})]);
|
|
212
|
+
throw e;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/alerts/process-guards.ts
|
|
219
|
+
var asText = (e) => {
|
|
220
|
+
if (e instanceof Error) return _nullishCoalesce(e.stack, () => ( `${e.name}: ${e.message}`));
|
|
221
|
+
try {
|
|
222
|
+
return typeof e === "string" ? e : JSON.stringify(e);
|
|
223
|
+
} catch (e4) {
|
|
224
|
+
return String(e);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
function installProcessGuards(options = {}) {
|
|
228
|
+
const proc = _nullishCoalesce(options.proc, () => ( process));
|
|
229
|
+
const exit = _nullishCoalesce(options.exit, () => ( ((code) => process.exit(code))));
|
|
230
|
+
const log = _nullishCoalesce(options.log, () => ( ((m) => console.error(m))));
|
|
231
|
+
const alerts = () => _nullishCoalesce(options.alerts, () => ( getActiveAlerts()));
|
|
232
|
+
const base = (title, level, e) => ({
|
|
233
|
+
title,
|
|
234
|
+
level,
|
|
235
|
+
source: `process \xB7 ${_nullishCoalesce(process.env["NODE_ENV"], () => ( "development"))}`,
|
|
236
|
+
code: asText(e).slice(0, 2900),
|
|
237
|
+
error: errorInfo(e)
|
|
238
|
+
});
|
|
239
|
+
proc.on("unhandledRejection", (reason) => {
|
|
240
|
+
try {
|
|
241
|
+
log(`Unhandled rejection: ${asText(reason)}`);
|
|
242
|
+
_optionalChain([alerts, 'call', _15 => _15(), 'optionalAccess', _16 => _16.notify, 'call', _17 => _17(base("Unhandled promise rejection", "error", reason))]);
|
|
243
|
+
} catch (e5) {
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
proc.on("uncaughtException", (e) => {
|
|
247
|
+
const finish = () => {
|
|
248
|
+
if ((_nullishCoalesce(options.onFatal, () => ( "continue"))) === "exit") exit(1);
|
|
249
|
+
};
|
|
250
|
+
try {
|
|
251
|
+
log(`Uncaught exception: ${asText(e)}`);
|
|
252
|
+
const a = alerts();
|
|
253
|
+
const payload = base("Uncaught exception", "critical", e);
|
|
254
|
+
if (options.onFatal === "exit" && _optionalChain([a, 'optionalAccess', _18 => _18.notifyAndWait])) {
|
|
255
|
+
const grace = new Promise((r) => setTimeout(r, _nullishCoalesce(options.exitGraceMs, () => ( 3e3))));
|
|
256
|
+
void Promise.race([a.notifyAndWait(payload).catch(() => false), grace]).then(finish, finish);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
_optionalChain([a, 'optionalAccess', _19 => _19.notify, 'call', _20 => _20(payload)]);
|
|
260
|
+
} catch (e6) {
|
|
261
|
+
}
|
|
262
|
+
finish();
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
exports.ALERTS_OPTIONS = ALERTS_OPTIONS; exports.AlertOnFailure = AlertOnFailure; exports.AlertsModule = AlertsModule; exports.AlertsService = AlertsService; exports.LoggerWithAlerts = LoggerWithAlerts; exports.MemoryAlertSink = MemoryAlertSink; exports.WebhookAlertSink = WebhookAlertSink; exports.alertSinkAsChannel = alertSinkAsChannel; exports.errorInfo = errorInfo; exports.installProcessGuards = installProcessGuards;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { AlertsModule } from './alerts.module.js';
|
|
2
|
+
export { ALERTS_OPTIONS } from './alerts.options.js';
|
|
3
|
+
export { AlertsService, errorInfo } from './alerts.service.js';
|
|
4
|
+
export type { AlertFields, AlertLevel, AlertMessage, AlertPayload, AlertSinkPort, AlertsOptions, WebhookSinkConfig, } from './alerts.types.js';
|
|
5
|
+
export { WebhookAlertSink, MemoryAlertSink, alertSinkAsChannel } from './alert-sinks.js';
|
|
6
|
+
export { LoggerWithAlerts } from './logger-with-alerts.js';
|
|
7
|
+
export type { AlertNotifier } from './logger-with-alerts.js';
|
|
8
|
+
export { AlertOnFailure } from './alert-on-failure.decorator.js';
|
|
9
|
+
export { installProcessGuards } from './process-guards.js';
|
|
10
|
+
export type { ProcessGuardsOptions } from './process-guards.js';
|