@miguelmorales13/nestkit 0.26.0 → 0.29.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 +69 -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 +101 -101
- package/dist/auth/oauth/index.js +3 -3
- package/dist/bootstrap/apply-defaults.d.ts +9 -0
- package/dist/bootstrap/index.cjs +7 -6
- package/dist/bootstrap/index.js +6 -5
- package/dist/chunk-33KFVAUZ.cjs +151 -0
- 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-2DUEQKOI.js → chunk-5LVGCTJ3.js} +4 -4
- 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-LSDY6ETG.cjs → chunk-JJKGQNGD.cjs} +5 -5
- package/dist/{chunk-ALIKVFOV.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-LU3HO7B3.js +151 -0
- 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-PFHHLB7B.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 +33 -32
- 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/connect/credentials-cipher.d.ts +14 -0
- package/dist/payments/connect/mercadopago-refund.d.ts +118 -0
- package/dist/payments/connect/mercadopago-signature.d.ts +6 -0
- package/dist/payments/connect/online-checkout.d.ts +69 -0
- package/dist/payments/connect/rejection.d.ts +7 -0
- package/dist/payments/connect/status.d.ts +20 -0
- package/dist/payments/connect/terminal-connections.d.ts +291 -0
- package/dist/payments/index.cjs +908 -10
- package/dist/payments/index.d.ts +10 -0
- package/dist/payments/index.js +900 -2
- 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 +27 -35
- package/dist/security/index.d.ts +5 -0
- package/dist/security/index.js +24 -32
- 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 +108 -3
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type HttpRequestLike } from './http.types.js';
|
|
2
|
+
export interface TrustedIpOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Headers, in priority order, that a trusted edge (CDN / proxy) writes itself with the real client IP.
|
|
5
|
+
* Only list headers that clients cannot forge: pair them with `originSecret` so nobody reaches the app
|
|
6
|
+
* bypassing the edge. Default: none (falls back to `req.ips` / `req.ip`, i.e. Express `trust proxy`).
|
|
7
|
+
*/
|
|
8
|
+
trustedHeaders?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The client IP, preferring headers set by a trusted edge over `X-Forwarded-For` (which a caller can
|
|
12
|
+
* send and rotate on every request). `req.ips[0]` / `req.ip` are the fallback for dev and tests.
|
|
13
|
+
*/
|
|
14
|
+
export declare function trustedIp(req: HttpRequestLike, options?: TrustedIpOptions): string;
|
|
15
|
+
/** Express `trust proxy` value: hop count, boolean, CIDR list/keyword string. */
|
|
16
|
+
export type TrustProxyValue = number | boolean | string | string[];
|
|
17
|
+
/** Sets Express `trust proxy` on the underlying adapter instance (no-op if the adapter has no `set`). */
|
|
18
|
+
export declare function applyTrustProxy(app: {
|
|
19
|
+
getHttpAdapter(): {
|
|
20
|
+
getInstance(): unknown;
|
|
21
|
+
};
|
|
22
|
+
}, value: TrustProxyValue): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type HealthOptions } from './health.options.js';
|
|
2
|
+
export interface CheckResult {
|
|
3
|
+
status: 'up' | 'down';
|
|
4
|
+
message?: string;
|
|
5
|
+
ms: number;
|
|
6
|
+
}
|
|
7
|
+
export declare class HealthService {
|
|
8
|
+
private readonly options;
|
|
9
|
+
constructor(options: HealthOptions);
|
|
10
|
+
live(): {
|
|
11
|
+
status: string;
|
|
12
|
+
uptime: number;
|
|
13
|
+
};
|
|
14
|
+
ready(): Promise<{
|
|
15
|
+
status: 'ok' | 'error';
|
|
16
|
+
checks: Record<string, CheckResult>;
|
|
17
|
+
}>;
|
|
18
|
+
private run;
|
|
19
|
+
}
|
|
20
|
+
/** `GET /health` (alive) and `GET /ready` (checks; 503 when any is down). Outside the response envelope on purpose: monitors expect a flat body. */
|
|
21
|
+
export declare class HealthController {
|
|
22
|
+
private readonly health;
|
|
23
|
+
constructor(health: HealthService);
|
|
24
|
+
live(): {
|
|
25
|
+
status: string;
|
|
26
|
+
uptime: number;
|
|
27
|
+
};
|
|
28
|
+
ready(): Promise<{
|
|
29
|
+
status: "ok" | "error";
|
|
30
|
+
checks: Record<string, CheckResult>;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { type HealthOptions } from './health.options.js';
|
|
3
|
+
/**
|
|
4
|
+
* ```ts
|
|
5
|
+
* HealthModule.forRoot({
|
|
6
|
+
* checks: [{ name: 'postgres', check: () => dataSource.query('SELECT 1') }],
|
|
7
|
+
* decorators: [Public()],
|
|
8
|
+
* })
|
|
9
|
+
* // main.ts: app.setGlobalPrefix('api', { exclude: healthPrefixExclude() })
|
|
10
|
+
* ```
|
|
11
|
+
* No @nestjs/terminus: a check is just a function that throws when the dependency is down.
|
|
12
|
+
*/
|
|
13
|
+
export declare class HealthModule {
|
|
14
|
+
static forRoot(options?: HealthOptions): DynamicModule;
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface HealthCheckDef {
|
|
2
|
+
name: string;
|
|
3
|
+
/** Resolves when healthy; throws (or rejects) when not. */
|
|
4
|
+
check: () => Promise<unknown> | unknown;
|
|
5
|
+
/** Per-check time limit in ms (default: `HealthOptions.timeoutMs`). */
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface HealthOptions {
|
|
9
|
+
/** Readiness checks run in parallel by `/ready`. */
|
|
10
|
+
checks?: HealthCheckDef[];
|
|
11
|
+
/**
|
|
12
|
+
* Route names to keep out of the global prefix (default `['health', 'ready']`). Nest cannot read this
|
|
13
|
+
* by itself: pass `healthPrefixExclude(options)` to `app.setGlobalPrefix('api', { exclude })`.
|
|
14
|
+
*/
|
|
15
|
+
prefixExclude?: string[];
|
|
16
|
+
/** Default time limit per check (default 3000 ms). */
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
/** Extra fields for the `/health` body (e.g. `() => ({ env: process.env.APP_ENV })`). */
|
|
19
|
+
info?: () => Record<string, unknown>;
|
|
20
|
+
/** Decorators applied to the controller class, e.g. your `@Public()` so the auth guard lets probes through. */
|
|
21
|
+
decorators?: ClassDecorator[];
|
|
22
|
+
}
|
|
23
|
+
export declare const HEALTH_OPTIONS: unique symbol;
|
|
24
|
+
export declare function healthPrefixExclude(options?: Pick<HealthOptions, 'prefixExclude'>): string[];
|
|
@@ -0,0 +1,103 @@
|
|
|
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/health/health.module.ts
|
|
7
|
+
var _common = require('@nestjs/common');
|
|
8
|
+
|
|
9
|
+
// src/health/health.controller.ts
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// src/health/health.options.ts
|
|
13
|
+
var HEALTH_OPTIONS = /* @__PURE__ */ Symbol("NESTKIT_HEALTH_OPTIONS");
|
|
14
|
+
function healthPrefixExclude(options = {}) {
|
|
15
|
+
return _nullishCoalesce(options.prefixExclude, () => ( ["health", "ready"]));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/health/health.controller.ts
|
|
19
|
+
var HealthService = class {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
}
|
|
23
|
+
live() {
|
|
24
|
+
return { status: "ok", uptime: Math.round(process.uptime()), ..._nullishCoalesce(_optionalChain([this, 'access', _2 => _2.options, 'access', _3 => _3.info, 'optionalCall', _4 => _4()]), () => ( {})) };
|
|
25
|
+
}
|
|
26
|
+
async ready() {
|
|
27
|
+
const defs = _nullishCoalesce(this.options.checks, () => ( []));
|
|
28
|
+
const results = await Promise.all(defs.map((d) => this.run(d)));
|
|
29
|
+
const checks = {};
|
|
30
|
+
defs.forEach((d, i) => checks[d.name] = results[i]);
|
|
31
|
+
const ok = results.every((r) => r.status === "up");
|
|
32
|
+
return { status: ok ? "ok" : "error", checks };
|
|
33
|
+
}
|
|
34
|
+
async run(def) {
|
|
35
|
+
const started = Date.now();
|
|
36
|
+
const limit = _nullishCoalesce(_nullishCoalesce(def.timeoutMs, () => ( this.options.timeoutMs)), () => ( 3e3));
|
|
37
|
+
let timer;
|
|
38
|
+
try {
|
|
39
|
+
await Promise.race([
|
|
40
|
+
Promise.resolve().then(() => def.check()),
|
|
41
|
+
new Promise((_, reject) => {
|
|
42
|
+
timer = setTimeout(() => reject(new Error(`timeout after ${limit} ms`)), limit);
|
|
43
|
+
})
|
|
44
|
+
]);
|
|
45
|
+
return { status: "up", ms: Date.now() - started };
|
|
46
|
+
} catch (err) {
|
|
47
|
+
return { status: "down", message: _nullishCoalesce(_optionalChain([err, 'optionalAccess', _5 => _5.message]), () => ( String(err))), ms: Date.now() - started };
|
|
48
|
+
} finally {
|
|
49
|
+
if (timer) clearTimeout(timer);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
HealthService = exports.HealthService = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
54
|
+
_common.Injectable.call(void 0, ),
|
|
55
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, HEALTH_OPTIONS))
|
|
56
|
+
], HealthService);
|
|
57
|
+
var HealthController = class {
|
|
58
|
+
constructor(health) {
|
|
59
|
+
this.health = health;
|
|
60
|
+
}
|
|
61
|
+
live() {
|
|
62
|
+
return this.health.live();
|
|
63
|
+
}
|
|
64
|
+
async ready() {
|
|
65
|
+
const result = await this.health.ready();
|
|
66
|
+
if (result.status !== "ok") throw new (0, _common.HttpException)(result, _common.HttpStatus.SERVICE_UNAVAILABLE);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
_chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
71
|
+
_common.Get.call(void 0, "health")
|
|
72
|
+
], HealthController.prototype, "live", 1);
|
|
73
|
+
_chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
74
|
+
_common.Get.call(void 0, "ready")
|
|
75
|
+
], HealthController.prototype, "ready", 1);
|
|
76
|
+
HealthController = exports.HealthController = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
77
|
+
_common.Controller.call(void 0, )
|
|
78
|
+
], HealthController);
|
|
79
|
+
|
|
80
|
+
// src/health/health.module.ts
|
|
81
|
+
var HealthModule = class {
|
|
82
|
+
static forRoot(options = {}) {
|
|
83
|
+
class Controller2 extends HealthController {
|
|
84
|
+
}
|
|
85
|
+
for (const d of _nullishCoalesce(options.decorators, () => ( []))) d(Controller2);
|
|
86
|
+
return {
|
|
87
|
+
module: HealthModule,
|
|
88
|
+
controllers: [Controller2],
|
|
89
|
+
providers: [{ provide: HEALTH_OPTIONS, useValue: options }, HealthService],
|
|
90
|
+
exports: [HealthService]
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
HealthModule = exports.HealthModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
95
|
+
_common.Module.call(void 0, {})
|
|
96
|
+
], HealthModule);
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
exports.HEALTH_OPTIONS = HEALTH_OPTIONS; exports.HealthController = HealthController; exports.HealthModule = HealthModule; exports.HealthService = HealthService; exports.healthPrefixExclude = healthPrefixExclude;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { HealthModule } from './health.module.js';
|
|
2
|
+
export { HealthController, HealthService } from './health.controller.js';
|
|
3
|
+
export type { CheckResult } from './health.controller.js';
|
|
4
|
+
export { HEALTH_OPTIONS, healthPrefixExclude } from './health.options.js';
|
|
5
|
+
export type { HealthOptions, HealthCheckDef } from './health.options.js';
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass,
|
|
3
|
+
__decorateParam
|
|
4
|
+
} from "../chunk-D3DDJWUD.js";
|
|
5
|
+
|
|
6
|
+
// src/health/health.module.ts
|
|
7
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
|
|
9
|
+
// src/health/health.controller.ts
|
|
10
|
+
import { Controller, Get, HttpException, HttpStatus, Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
|
|
12
|
+
// src/health/health.options.ts
|
|
13
|
+
var HEALTH_OPTIONS = /* @__PURE__ */ Symbol("NESTKIT_HEALTH_OPTIONS");
|
|
14
|
+
function healthPrefixExclude(options = {}) {
|
|
15
|
+
return options.prefixExclude ?? ["health", "ready"];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/health/health.controller.ts
|
|
19
|
+
var HealthService = class {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
}
|
|
23
|
+
live() {
|
|
24
|
+
return { status: "ok", uptime: Math.round(process.uptime()), ...this.options.info?.() ?? {} };
|
|
25
|
+
}
|
|
26
|
+
async ready() {
|
|
27
|
+
const defs = this.options.checks ?? [];
|
|
28
|
+
const results = await Promise.all(defs.map((d) => this.run(d)));
|
|
29
|
+
const checks = {};
|
|
30
|
+
defs.forEach((d, i) => checks[d.name] = results[i]);
|
|
31
|
+
const ok = results.every((r) => r.status === "up");
|
|
32
|
+
return { status: ok ? "ok" : "error", checks };
|
|
33
|
+
}
|
|
34
|
+
async run(def) {
|
|
35
|
+
const started = Date.now();
|
|
36
|
+
const limit = def.timeoutMs ?? this.options.timeoutMs ?? 3e3;
|
|
37
|
+
let timer;
|
|
38
|
+
try {
|
|
39
|
+
await Promise.race([
|
|
40
|
+
Promise.resolve().then(() => def.check()),
|
|
41
|
+
new Promise((_, reject) => {
|
|
42
|
+
timer = setTimeout(() => reject(new Error(`timeout after ${limit} ms`)), limit);
|
|
43
|
+
})
|
|
44
|
+
]);
|
|
45
|
+
return { status: "up", ms: Date.now() - started };
|
|
46
|
+
} catch (err) {
|
|
47
|
+
return { status: "down", message: err?.message ?? String(err), ms: Date.now() - started };
|
|
48
|
+
} finally {
|
|
49
|
+
if (timer) clearTimeout(timer);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
HealthService = __decorateClass([
|
|
54
|
+
Injectable(),
|
|
55
|
+
__decorateParam(0, Inject(HEALTH_OPTIONS))
|
|
56
|
+
], HealthService);
|
|
57
|
+
var HealthController = class {
|
|
58
|
+
constructor(health) {
|
|
59
|
+
this.health = health;
|
|
60
|
+
}
|
|
61
|
+
live() {
|
|
62
|
+
return this.health.live();
|
|
63
|
+
}
|
|
64
|
+
async ready() {
|
|
65
|
+
const result = await this.health.ready();
|
|
66
|
+
if (result.status !== "ok") throw new HttpException(result, HttpStatus.SERVICE_UNAVAILABLE);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
__decorateClass([
|
|
71
|
+
Get("health")
|
|
72
|
+
], HealthController.prototype, "live", 1);
|
|
73
|
+
__decorateClass([
|
|
74
|
+
Get("ready")
|
|
75
|
+
], HealthController.prototype, "ready", 1);
|
|
76
|
+
HealthController = __decorateClass([
|
|
77
|
+
Controller()
|
|
78
|
+
], HealthController);
|
|
79
|
+
|
|
80
|
+
// src/health/health.module.ts
|
|
81
|
+
var HealthModule = class {
|
|
82
|
+
static forRoot(options = {}) {
|
|
83
|
+
class Controller2 extends HealthController {
|
|
84
|
+
}
|
|
85
|
+
for (const d of options.decorators ?? []) d(Controller2);
|
|
86
|
+
return {
|
|
87
|
+
module: HealthModule,
|
|
88
|
+
controllers: [Controller2],
|
|
89
|
+
providers: [{ provide: HEALTH_OPTIONS, useValue: options }, HealthService],
|
|
90
|
+
exports: [HealthService]
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
HealthModule = __decorateClass([
|
|
95
|
+
Module({})
|
|
96
|
+
], HealthModule);
|
|
97
|
+
export {
|
|
98
|
+
HEALTH_OPTIONS,
|
|
99
|
+
HealthController,
|
|
100
|
+
HealthModule,
|
|
101
|
+
HealthService,
|
|
102
|
+
healthPrefixExclude
|
|
103
|
+
};
|
package/dist/i18n/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunkFDZQCLLKcjs = require('../chunk-FDZQCLLK.cjs');
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
@@ -17,7 +17,7 @@ require('../chunk-SDWHKPXI.cjs');
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
var _chunkPNK5WMPPcjs = require('../chunk-PNK5WMPP.cjs');
|
|
20
|
-
require('../chunk-
|
|
20
|
+
require('../chunk-LRX7AMSS.cjs');
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
|
|
@@ -30,4 +30,4 @@ require('../chunk-2REOCMUD.cjs');
|
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
exports.I18nInterceptor =
|
|
33
|
+
exports.I18nInterceptor = _chunkFDZQCLLKcjs.I18nInterceptor; exports.I18nMessage = _chunkPNK5WMPPcjs.I18nMessage; exports.I18nModule = _chunkFDZQCLLKcjs.I18nModule; exports.NestI18nTranslator = _chunkFDZQCLLKcjs.NestI18nTranslator; exports.currentLanguage = _chunkAXMW5JF7cjs.currentLanguage; exports.isI18nMessage = _chunkPNK5WMPPcjs.isI18nMessage; exports.matchLanguage = _chunkFDZQCLLKcjs.matchLanguage; exports.t = _chunkPNK5WMPPcjs.t; exports.translate = _chunkAXMW5JF7cjs.translate; exports.translateDeep = _chunkAXMW5JF7cjs.translateDeep; exports.translateOr = _chunkFDZQCLLKcjs.translateOr;
|
package/dist/i18n/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
NestI18nTranslator,
|
|
5
5
|
matchLanguage,
|
|
6
6
|
translateOr
|
|
7
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-OTE77ONL.js";
|
|
8
8
|
import {
|
|
9
9
|
currentLanguage,
|
|
10
10
|
translate,
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
isI18nMessage,
|
|
18
18
|
t
|
|
19
19
|
} from "../chunk-VVMRZEIW.js";
|
|
20
|
-
import "../chunk-
|
|
20
|
+
import "../chunk-D3DDJWUD.js";
|
|
21
21
|
export {
|
|
22
22
|
I18nInterceptor,
|
|
23
23
|
I18nMessage,
|
|
@@ -31,5 +31,6 @@ export declare class IdempotencyInterceptor implements NestInterceptor {
|
|
|
31
31
|
private readonly options;
|
|
32
32
|
constructor(store: IdempotencyStorePort, options?: IdempotencyOptions);
|
|
33
33
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
34
|
+
private isExcluded;
|
|
34
35
|
private mismatch;
|
|
35
36
|
}
|
|
@@ -24,6 +24,21 @@ export interface IdempotencyOptions {
|
|
|
24
24
|
* are global.
|
|
25
25
|
*/
|
|
26
26
|
scope?: (request: unknown) => string | undefined;
|
|
27
|
+
/** Alias of `scope` (the name the analysis used). If both are set, `scopeBy` wins. */
|
|
28
|
+
scopeBy?: (request: unknown) => string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Routes (matched against the path without the query string) that are never
|
|
31
|
+
* made idempotent, even when the client sends a key. Use it for responses
|
|
32
|
+
* that ARE a credential — `/auth/*`, device pairing — which would otherwise
|
|
33
|
+
* sit in the store, in clear, for the whole TTL. Per-handler: `@NoIdempotency()`.
|
|
34
|
+
*/
|
|
35
|
+
excludeRoutes?: RegExp[];
|
|
36
|
+
/**
|
|
37
|
+
* Largest answer worth keeping, in bytes of its JSON. A bigger one is not
|
|
38
|
+
* stored: the key is released and a retry runs the handler again, so the
|
|
39
|
+
* store cannot be filled by one huge response. Unlimited by default.
|
|
40
|
+
*/
|
|
41
|
+
maxResponseBytes?: number;
|
|
27
42
|
/** Where keys live. Defaults to an in-process store — single instance only. */
|
|
28
43
|
store?: IdempotencyStorePort;
|
|
29
44
|
}
|
|
@@ -7,7 +7,7 @@ require('../chunk-PNK5WMPP.cjs');
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunkLRX7AMSScjs = require('../chunk-LRX7AMSS.cjs');
|
|
11
11
|
|
|
12
12
|
// src/idempotency/idempotency.port.ts
|
|
13
13
|
var IDEMPOTENCY_STORE = /* @__PURE__ */ Symbol("IDEMPOTENCY_STORE");
|
|
@@ -60,6 +60,13 @@ var _crypto = require('crypto');
|
|
|
60
60
|
var _common = require('@nestjs/common');
|
|
61
61
|
var _rxjs = require('rxjs');
|
|
62
62
|
var _operators = require('rxjs/operators');
|
|
63
|
+
|
|
64
|
+
// src/idempotency/no-idempotency.decorator.ts
|
|
65
|
+
|
|
66
|
+
var NO_IDEMPOTENCY_KEY = "nestkit:no-idempotency";
|
|
67
|
+
var NoIdempotency = () => _common.SetMetadata.call(void 0, NO_IDEMPOTENCY_KEY, true);
|
|
68
|
+
|
|
69
|
+
// src/idempotency/idempotency.interceptor.ts
|
|
63
70
|
var DEFAULT_METHODS = ["POST", "PUT", "PATCH", "DELETE"];
|
|
64
71
|
var DEFAULT_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
65
72
|
function headerOf(request, name) {
|
|
@@ -85,6 +92,7 @@ var IdempotencyInterceptor = class {
|
|
|
85
92
|
const http = context.switchToHttp();
|
|
86
93
|
const request = http.getRequest();
|
|
87
94
|
const response = http.getResponse();
|
|
95
|
+
if (this.isExcluded(context, request)) return next.handle();
|
|
88
96
|
const methods = _nullishCoalesce(this.options.methods, () => ( DEFAULT_METHODS));
|
|
89
97
|
const method = String(_nullishCoalesce(_optionalChain([request, 'optionalAccess', _7 => _7.method]), () => ( "GET"))).toUpperCase();
|
|
90
98
|
if (!methods.includes(method)) return next.handle();
|
|
@@ -101,7 +109,8 @@ var IdempotencyInterceptor = class {
|
|
|
101
109
|
return next.handle();
|
|
102
110
|
}
|
|
103
111
|
const ttlMs = _nullishCoalesce(this.options.ttlMs, () => ( DEFAULT_TTL_MS));
|
|
104
|
-
const
|
|
112
|
+
const scopeFn = _nullishCoalesce(this.options.scopeBy, () => ( this.options.scope));
|
|
113
|
+
const scope = scopeFn ? scopeFn(request) : _optionalChain([request, 'optionalAccess', _8 => _8.user, 'optionalAccess', _9 => _9.sub]);
|
|
105
114
|
const storeKey = `${_nullishCoalesce(scope, () => ( "anonymous"))}:${key}`;
|
|
106
115
|
const path = _nullishCoalesce(_nullishCoalesce(_optionalChain([request, 'optionalAccess', _10 => _10.originalUrl]), () => ( _optionalChain([request, 'optionalAccess', _11 => _11.url]))), () => ( ""));
|
|
107
116
|
const fingerprint = _crypto.createHash.call(void 0, "sha256").update(`${method} ${path} ${stableStringify(_nullishCoalesce(_optionalChain([request, 'optionalAccess', _12 => _12.body]), () => ( null)))}`).digest("hex");
|
|
@@ -126,6 +135,11 @@ var IdempotencyInterceptor = class {
|
|
|
126
135
|
// yet is not replayable, and the retry this protects against is
|
|
127
136
|
// exactly the one that arrives immediately.
|
|
128
137
|
_operators.concatMap.call(void 0, async (body) => {
|
|
138
|
+
const max = this.options.maxResponseBytes;
|
|
139
|
+
if (max !== void 0 && Buffer.byteLength(JSON.stringify(_nullishCoalesce(body, () => ( null))), "utf8") > max) {
|
|
140
|
+
await this.store.release(storeKey);
|
|
141
|
+
return body;
|
|
142
|
+
}
|
|
129
143
|
await this.store.complete(
|
|
130
144
|
storeKey,
|
|
131
145
|
{ status: Number(_nullishCoalesce(_optionalChain([response, 'optionalAccess', _14 => _14.statusCode]), () => ( 200))), body, fingerprint, completedAt: /* @__PURE__ */ new Date() },
|
|
@@ -144,6 +158,19 @@ var IdempotencyInterceptor = class {
|
|
|
144
158
|
})
|
|
145
159
|
);
|
|
146
160
|
}
|
|
161
|
+
isExcluded(context, request) {
|
|
162
|
+
const handler = typeof context.getHandler === "function" ? context.getHandler() : void 0;
|
|
163
|
+
const klass = typeof context.getClass === "function" ? context.getClass() : void 0;
|
|
164
|
+
if (handler && Reflect.getMetadata(NO_IDEMPOTENCY_KEY, handler)) return true;
|
|
165
|
+
if (klass && Reflect.getMetadata(NO_IDEMPOTENCY_KEY, klass)) return true;
|
|
166
|
+
const patterns = this.options.excludeRoutes;
|
|
167
|
+
if (!_optionalChain([patterns, 'optionalAccess', _15 => _15.length])) return false;
|
|
168
|
+
const path = String(_nullishCoalesce(_nullishCoalesce(_optionalChain([request, 'optionalAccess', _16 => _16.originalUrl]), () => ( _optionalChain([request, 'optionalAccess', _17 => _17.url]))), () => ( ""))).split("?")[0];
|
|
169
|
+
return patterns.some((re) => {
|
|
170
|
+
re.lastIndex = 0;
|
|
171
|
+
return re.test(path);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
147
174
|
mismatch(headerName) {
|
|
148
175
|
return new (0, _chunkHDFPSA2Vcjs.ValidationAppException)(
|
|
149
176
|
`This ${headerName} was already used for a different request`,
|
|
@@ -152,10 +179,10 @@ var IdempotencyInterceptor = class {
|
|
|
152
179
|
);
|
|
153
180
|
}
|
|
154
181
|
};
|
|
155
|
-
IdempotencyInterceptor = exports.IdempotencyInterceptor =
|
|
182
|
+
IdempotencyInterceptor = exports.IdempotencyInterceptor = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
156
183
|
_common.Injectable.call(void 0, ),
|
|
157
|
-
|
|
158
|
-
|
|
184
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, IDEMPOTENCY_STORE)),
|
|
185
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 1, _common.Inject.call(void 0, IDEMPOTENCY_OPTIONS))
|
|
159
186
|
], IdempotencyInterceptor);
|
|
160
187
|
|
|
161
188
|
// src/idempotency/idempotency.module.ts
|
|
@@ -194,14 +221,194 @@ var IdempotencyModule = class {
|
|
|
194
221
|
};
|
|
195
222
|
}
|
|
196
223
|
};
|
|
197
|
-
IdempotencyModule = exports.IdempotencyModule =
|
|
224
|
+
IdempotencyModule = exports.IdempotencyModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
198
225
|
_common.Global.call(void 0, ),
|
|
199
226
|
_common.Module.call(void 0, {})
|
|
200
227
|
], IdempotencyModule);
|
|
201
228
|
|
|
229
|
+
// src/idempotency/postgres.store.ts
|
|
230
|
+
var IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?$/;
|
|
231
|
+
function checkTable(table) {
|
|
232
|
+
if (!IDENTIFIER.test(table)) throw new Error(`PostgresIdempotencyStore: invalid table name "${table}"`);
|
|
233
|
+
return table;
|
|
234
|
+
}
|
|
235
|
+
function rowsOf(result) {
|
|
236
|
+
if (Array.isArray(result)) return result.length === 2 && Array.isArray(result[0]) ? result[0] : result;
|
|
237
|
+
const rows = _optionalChain([result, 'optionalAccess', _18 => _18.rows]);
|
|
238
|
+
return Array.isArray(rows) ? rows : [];
|
|
239
|
+
}
|
|
240
|
+
var PostgresIdempotencyStore = class _PostgresIdempotencyStore {
|
|
241
|
+
constructor(options) {
|
|
242
|
+
this.calls = 0;
|
|
243
|
+
this.table = checkTable(_nullishCoalesce(options.table, () => ( "idempotency_key")));
|
|
244
|
+
const { pool } = options;
|
|
245
|
+
if (options.query) this.query = options.query;
|
|
246
|
+
else if (pool) this.query = (sql, params) => pool.query(sql, params);
|
|
247
|
+
else throw new Error("PostgresIdempotencyStore: pass `pool` or `query`");
|
|
248
|
+
this.maxBytes = options.maxBytes;
|
|
249
|
+
this.cleanupEvery = _nullishCoalesce(options.cleanupEvery, () => ( 100));
|
|
250
|
+
}
|
|
251
|
+
static createTableSql(table = "idempotency_key") {
|
|
252
|
+
const t = checkTable(table);
|
|
253
|
+
const idx = `${t.replace(".", "_")}_expires_idx`;
|
|
254
|
+
return `CREATE TABLE IF NOT EXISTS ${t} (
|
|
255
|
+
key text PRIMARY KEY,
|
|
256
|
+
fingerprint text NOT NULL,
|
|
257
|
+
status integer,
|
|
258
|
+
body jsonb,
|
|
259
|
+
completed_at timestamptz,
|
|
260
|
+
expires_at timestamptz NOT NULL
|
|
261
|
+
);
|
|
262
|
+
CREATE INDEX IF NOT EXISTS ${idx} ON ${t} (expires_at)`;
|
|
263
|
+
}
|
|
264
|
+
async ensureTable() {
|
|
265
|
+
for (const statement of _PostgresIdempotencyStore.createTableSql(this.table).split(";\n")) {
|
|
266
|
+
await this.query(statement, []);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async begin(key, fingerprint, ttlMs) {
|
|
270
|
+
await this.maybeCleanup();
|
|
271
|
+
const claimed = rowsOf(
|
|
272
|
+
await this.query(
|
|
273
|
+
`INSERT INTO ${this.table} (key, fingerprint, expires_at)
|
|
274
|
+
VALUES ($1, $2, now() + make_interval(secs => $3::double precision / 1000))
|
|
275
|
+
ON CONFLICT (key) DO UPDATE
|
|
276
|
+
SET fingerprint = EXCLUDED.fingerprint, status = NULL, body = NULL,
|
|
277
|
+
completed_at = NULL, expires_at = EXCLUDED.expires_at
|
|
278
|
+
WHERE ${this.table}.expires_at <= now()
|
|
279
|
+
RETURNING key`,
|
|
280
|
+
[key, fingerprint, ttlMs]
|
|
281
|
+
)
|
|
282
|
+
);
|
|
283
|
+
if (claimed.length > 0) return { state: "claimed" };
|
|
284
|
+
const found = rowsOf(
|
|
285
|
+
await this.query(
|
|
286
|
+
`SELECT fingerprint, status, body, completed_at FROM ${this.table} WHERE key = $1`,
|
|
287
|
+
[key]
|
|
288
|
+
)
|
|
289
|
+
)[0];
|
|
290
|
+
if (!found) return this.begin(key, fingerprint, ttlMs);
|
|
291
|
+
if (found.status === null || found.status === void 0) {
|
|
292
|
+
return { state: "in_progress", fingerprint: found.fingerprint };
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
state: "completed",
|
|
296
|
+
record: {
|
|
297
|
+
status: Number(found.status),
|
|
298
|
+
body: found.body,
|
|
299
|
+
fingerprint: found.fingerprint,
|
|
300
|
+
completedAt: new Date(found.completed_at)
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
async complete(key, record, ttlMs) {
|
|
305
|
+
const body = JSON.stringify(_nullishCoalesce(record.body, () => ( null)));
|
|
306
|
+
if (this.maxBytes !== void 0 && Buffer.byteLength(body, "utf8") > this.maxBytes) {
|
|
307
|
+
await this.release(key);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
await this.query(
|
|
311
|
+
`UPDATE ${this.table}
|
|
312
|
+
SET status = $2, body = $3::jsonb, fingerprint = $4, completed_at = $5,
|
|
313
|
+
expires_at = now() + make_interval(secs => $6::double precision / 1000)
|
|
314
|
+
WHERE key = $1`,
|
|
315
|
+
[key, record.status, body, record.fingerprint, record.completedAt, ttlMs]
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
async release(key) {
|
|
319
|
+
await this.query(`DELETE FROM ${this.table} WHERE key = $1`, [key]);
|
|
320
|
+
}
|
|
321
|
+
/** Deletes every expired row now. Returns nothing; also runs on its own every `cleanupEvery` calls. */
|
|
322
|
+
async purgeExpired() {
|
|
323
|
+
await this.query(`DELETE FROM ${this.table} WHERE expires_at <= now()`, []);
|
|
324
|
+
}
|
|
325
|
+
async maybeCleanup() {
|
|
326
|
+
if (this.cleanupEvery <= 0) return;
|
|
327
|
+
this.calls += 1;
|
|
328
|
+
if (this.calls % this.cleanupEvery !== 0) return;
|
|
329
|
+
try {
|
|
330
|
+
await this.purgeExpired();
|
|
331
|
+
} catch (e) {
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
// src/idempotency/redis.store.ts
|
|
337
|
+
var REDIS_IDEMPOTENCY_DEFAULT_MAX_BYTES = 256 * 1024;
|
|
338
|
+
var RedisIdempotencyStore = class {
|
|
339
|
+
constructor(options) {
|
|
340
|
+
this.inFallback = /* @__PURE__ */ new Set();
|
|
341
|
+
this.client = options.client;
|
|
342
|
+
this.prefix = _nullishCoalesce(options.prefix, () => ( "idem:"));
|
|
343
|
+
this.maxBytes = _nullishCoalesce(options.maxBytes, () => ( REDIS_IDEMPOTENCY_DEFAULT_MAX_BYTES));
|
|
344
|
+
this.onError = options.onError;
|
|
345
|
+
this.fallback = options.fallback;
|
|
346
|
+
}
|
|
347
|
+
async begin(key, fingerprint, ttlMs) {
|
|
348
|
+
try {
|
|
349
|
+
const raw = await this.client.reclamarOLeer(
|
|
350
|
+
this.prefix + key,
|
|
351
|
+
JSON.stringify({ f: fingerprint }),
|
|
352
|
+
ttlMs
|
|
353
|
+
);
|
|
354
|
+
if (raw === null || raw === void 0) return { state: "claimed" };
|
|
355
|
+
const entry = JSON.parse(raw);
|
|
356
|
+
if (entry.r) {
|
|
357
|
+
return {
|
|
358
|
+
state: "completed",
|
|
359
|
+
record: {
|
|
360
|
+
status: entry.r.status,
|
|
361
|
+
body: entry.r.body,
|
|
362
|
+
fingerprint: entry.f,
|
|
363
|
+
completedAt: new Date(entry.r.completedAt)
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
return { state: "in_progress", fingerprint: entry.f };
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (!this.fallback) throw error;
|
|
370
|
+
_optionalChain([this, 'access', _19 => _19.onError, 'optionalCall', _20 => _20(error)]);
|
|
371
|
+
const result = await this.fallback.begin(key, fingerprint, ttlMs);
|
|
372
|
+
if (result.state === "claimed") this.inFallback.add(key);
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
async complete(key, record, ttlMs) {
|
|
377
|
+
if (this.fallback && this.inFallback.delete(key)) return this.fallback.complete(key, record, ttlMs);
|
|
378
|
+
const text = JSON.stringify({
|
|
379
|
+
f: record.fingerprint,
|
|
380
|
+
r: { status: record.status, body: record.body, completedAt: record.completedAt.toISOString() }
|
|
381
|
+
});
|
|
382
|
+
try {
|
|
383
|
+
if (Buffer.byteLength(text, "utf8") > this.maxBytes) {
|
|
384
|
+
await this.client.borrar(this.prefix + key);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
await this.client.fijar(this.prefix + key, text, ttlMs);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
if (!this.fallback) throw error;
|
|
390
|
+
_optionalChain([this, 'access', _21 => _21.onError, 'optionalCall', _22 => _22(error)]);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async release(key) {
|
|
394
|
+
if (this.fallback && this.inFallback.delete(key)) return this.fallback.release(key);
|
|
395
|
+
try {
|
|
396
|
+
await this.client.borrar(this.prefix + key);
|
|
397
|
+
} catch (error) {
|
|
398
|
+
if (!this.fallback) throw error;
|
|
399
|
+
_optionalChain([this, 'access', _23 => _23.onError, 'optionalCall', _24 => _24(error)]);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
202
409
|
|
|
203
410
|
|
|
204
411
|
|
|
205
412
|
|
|
206
413
|
|
|
207
|
-
exports.IDEMPOTENCY_OPTIONS = IDEMPOTENCY_OPTIONS; exports.IDEMPOTENCY_STORE = IDEMPOTENCY_STORE; exports.IdempotencyInterceptor = IdempotencyInterceptor; exports.IdempotencyModule = IdempotencyModule; exports.MemoryIdempotencyStore = MemoryIdempotencyStore;
|
|
414
|
+
exports.IDEMPOTENCY_OPTIONS = IDEMPOTENCY_OPTIONS; exports.IDEMPOTENCY_STORE = IDEMPOTENCY_STORE; exports.IdempotencyInterceptor = IdempotencyInterceptor; exports.IdempotencyModule = IdempotencyModule; exports.MemoryIdempotencyStore = MemoryIdempotencyStore; exports.NO_IDEMPOTENCY_KEY = NO_IDEMPOTENCY_KEY; exports.NoIdempotency = NoIdempotency; exports.PostgresIdempotencyStore = PostgresIdempotencyStore; exports.REDIS_IDEMPOTENCY_DEFAULT_MAX_BYTES = REDIS_IDEMPOTENCY_DEFAULT_MAX_BYTES; exports.RedisIdempotencyStore = RedisIdempotencyStore;
|
|
@@ -5,3 +5,8 @@ export type { IdempotencyOptions } from './idempotency.options.js';
|
|
|
5
5
|
export { MemoryIdempotencyStore } from './memory.store.js';
|
|
6
6
|
export { IdempotencyInterceptor } from './idempotency.interceptor.js';
|
|
7
7
|
export { IdempotencyModule } from './idempotency.module.js';
|
|
8
|
+
export { NoIdempotency, NO_IDEMPOTENCY_KEY } from './no-idempotency.decorator.js';
|
|
9
|
+
export { PostgresIdempotencyStore } from './postgres.store.js';
|
|
10
|
+
export type { IdempotencyPgPool, IdempotencyQuery, PostgresIdempotencyStoreOptions, } from './postgres.store.js';
|
|
11
|
+
export { RedisIdempotencyStore, REDIS_IDEMPOTENCY_DEFAULT_MAX_BYTES } from './redis.store.js';
|
|
12
|
+
export type { RedisIdempotencyClient, RedisIdempotencyStoreOptions } from './redis.store.js';
|