@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,49 @@
|
|
|
1
|
+
import { type BeforeApplicationShutdown, type OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { type EventsOptions } from './events.options.js';
|
|
4
|
+
import { type EventSignal, type PublishInput, type StreamMessage } from './events.types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Signals per scope with replay, and optional fan-out between instances.
|
|
7
|
+
*
|
|
8
|
+
* Publish AFTER the transaction commits. A signal is small and carries nothing sensitive: the client re-reads
|
|
9
|
+
* through the normal (authorized) path. Ids are microseconds since 1970 (monotonic here, comparable across
|
|
10
|
+
* instances), so a reconnection on another instance replays what it missed, or gets `resync` if that instance
|
|
11
|
+
* cannot cover it (it started later, or already pruned).
|
|
12
|
+
*/
|
|
13
|
+
export declare class EventsService implements OnModuleInit, BeforeApplicationShutdown {
|
|
14
|
+
private readonly options;
|
|
15
|
+
private readonly log;
|
|
16
|
+
private readonly instance;
|
|
17
|
+
private readonly buffers;
|
|
18
|
+
private readonly prunedUpTo;
|
|
19
|
+
private readonly live;
|
|
20
|
+
private readonly observers;
|
|
21
|
+
private lastId;
|
|
22
|
+
private listeningSince;
|
|
23
|
+
private timer;
|
|
24
|
+
constructor(options: EventsOptions);
|
|
25
|
+
onModuleInit(): Promise<void>;
|
|
26
|
+
beforeApplicationShutdown(): Promise<void>;
|
|
27
|
+
/** Publish a signal to a scope. Throws on an unknown `module` or an empty scope (programming errors). */
|
|
28
|
+
publish(scope: string, input: PublishInput): EventSignal;
|
|
29
|
+
/** Tell everybody in the scope to re-read everything (permissions changed, bulk import…). */
|
|
30
|
+
resync(scope: string): EventSignal;
|
|
31
|
+
/** React to what is published on THIS instance (not to what arrives over the bus). Returns the unsubscribe. */
|
|
32
|
+
observe(fn: (signal: EventSignal) => void): () => void;
|
|
33
|
+
/**
|
|
34
|
+
* Observable for SSE. Emits `ready` (first connection) or the missed signals / `resync` (reconnection with
|
|
35
|
+
* `lastEventId`), then live signals. Unsubscribing disconnects.
|
|
36
|
+
*/
|
|
37
|
+
stream(scope: string, lastEventId?: number | string | null, filter?: (s: EventSignal) => boolean): Observable<StreamMessage>;
|
|
38
|
+
/** Signals after `lastId`, or `null` if this instance cannot guarantee it has all of them. */
|
|
39
|
+
replay(scope: string, lastId: number): EventSignal[] | null;
|
|
40
|
+
connections(): number;
|
|
41
|
+
/** Drop signals beyond the window or older than the max age. Also runs every minute and on replay. */
|
|
42
|
+
prune(): void;
|
|
43
|
+
private now;
|
|
44
|
+
/** Microseconds since 1970, always increasing on this instance. */
|
|
45
|
+
private nextId;
|
|
46
|
+
private deliver;
|
|
47
|
+
private broadcast;
|
|
48
|
+
private receive;
|
|
49
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** A small signal: it says "something changed", never carries sensitive data. Clients re-read through the normal path. */
|
|
2
|
+
export interface EventSignal {
|
|
3
|
+
/**
|
|
4
|
+
* Microseconds since 1970, strictly increasing per instance and comparable across instances
|
|
5
|
+
* (so a reconnection that lands on another instance can still replay with its `Last-Event-ID`).
|
|
6
|
+
*/
|
|
7
|
+
id: number;
|
|
8
|
+
scope: string;
|
|
9
|
+
module: string;
|
|
10
|
+
/** What happened, lowercase: `created`, `accepted`, `paid`… */
|
|
11
|
+
type: string;
|
|
12
|
+
/** Identifier of what changed (to refresh only that). */
|
|
13
|
+
ref?: string;
|
|
14
|
+
/** Minimal, NON sensitive data. */
|
|
15
|
+
extra?: Record<string, string | number | boolean | null>;
|
|
16
|
+
/** When it happened (ISO). */
|
|
17
|
+
at: string;
|
|
18
|
+
}
|
|
19
|
+
export interface PublishInput {
|
|
20
|
+
module: string;
|
|
21
|
+
type: string;
|
|
22
|
+
ref?: string;
|
|
23
|
+
extra?: EventSignal['extra'];
|
|
24
|
+
}
|
|
25
|
+
/** What `stream()` emits. */
|
|
26
|
+
export type StreamMessage = {
|
|
27
|
+
kind: 'ready';
|
|
28
|
+
}
|
|
29
|
+
/** The replay window does not cover the client's `Last-Event-ID`: it must re-read everything. */
|
|
30
|
+
| {
|
|
31
|
+
kind: 'resync';
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'signal';
|
|
34
|
+
signal: EventSignal;
|
|
35
|
+
};
|
|
36
|
+
/** Module of the signal that tells everybody in a scope to re-read (also used when a payload is too large). */
|
|
37
|
+
export declare const RESYNC_MODULE = "channel";
|
|
38
|
+
export declare const RESYNC_TYPE = "resync";
|
|
39
|
+
/** Fan-out between instances. Receives its own messages back; the service discards them. */
|
|
40
|
+
export interface EventsBus {
|
|
41
|
+
readonly kind: string;
|
|
42
|
+
publish(message: string): Promise<void>;
|
|
43
|
+
listen(onMessage: (message: string) => void): Promise<void>;
|
|
44
|
+
close(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
/** Structural `pg` client (a dedicated, DIRECT connection: LISTEN does not survive a transaction-mode pooler). */
|
|
47
|
+
export interface PgNotifyClient {
|
|
48
|
+
query(sql: string, params?: unknown[]): Promise<unknown>;
|
|
49
|
+
on(event: 'notification', cb: (n: {
|
|
50
|
+
channel: string;
|
|
51
|
+
payload?: string | undefined;
|
|
52
|
+
}) => void): unknown;
|
|
53
|
+
on(event: 'error' | 'end', cb: (e?: Error) => void): unknown;
|
|
54
|
+
end?(): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
/** Structural `ioredis` client. */
|
|
57
|
+
export interface RedisPubSubClient {
|
|
58
|
+
publish(channel: string, message: string): Promise<unknown>;
|
|
59
|
+
subscribe(channel: string): Promise<unknown>;
|
|
60
|
+
unsubscribe?(channel: string): Promise<unknown>;
|
|
61
|
+
on(event: 'message', cb: (channel: string, message: string) => void): unknown;
|
|
62
|
+
/** ioredis: a subscribed connection cannot publish, so the bus subscribes on a duplicate. */
|
|
63
|
+
duplicate?(): RedisPubSubClient;
|
|
64
|
+
quit?(): Promise<unknown>;
|
|
65
|
+
disconnect?(): void;
|
|
66
|
+
}
|
|
67
|
+
export interface TicketPayload {
|
|
68
|
+
/** Whatever the host needs to know who it is; must be JSON-serializable for shared stores. */
|
|
69
|
+
user: unknown;
|
|
70
|
+
scope: string;
|
|
71
|
+
}
|
|
72
|
+
/** One-use tickets so `EventSource` (no headers) never puts the access token in the URL. */
|
|
73
|
+
export interface TicketStorePort {
|
|
74
|
+
put(id: string, payload: TicketPayload, ttlSec: number): Promise<void>;
|
|
75
|
+
/** Atomic: returns the payload and deletes it. A second call returns `null`. */
|
|
76
|
+
take(id: string): Promise<TicketPayload | null>;
|
|
77
|
+
}
|
|
@@ -0,0 +1,557 @@
|
|
|
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; }require('../chunk-7SOM7EZP.cjs');
|
|
2
|
+
require('../chunk-JJKGQNGD.cjs');
|
|
3
|
+
require('../chunk-AXMW5JF7.cjs');
|
|
4
|
+
require('../chunk-Y72D5L6I.cjs');
|
|
5
|
+
require('../chunk-SDWHKPXI.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkHDFPSA2Vcjs = require('../chunk-HDFPSA2V.cjs');
|
|
11
|
+
require('../chunk-Q5Y33L7B.cjs');
|
|
12
|
+
require('../chunk-PNK5WMPP.cjs');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
var _chunkLRX7AMSScjs = require('../chunk-LRX7AMSS.cjs');
|
|
17
|
+
|
|
18
|
+
// src/events/events.module.ts
|
|
19
|
+
var _common = require('@nestjs/common');
|
|
20
|
+
|
|
21
|
+
// src/events/events.controller.ts
|
|
22
|
+
var _crypto = require('crypto');
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
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
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
var _rxjs = require('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 (0, _common.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 = _nullishCoalesce(options.instanceId, () => ( _crypto.randomUUID.call(void 0, )));
|
|
68
|
+
this.listeningSince = this.nextId();
|
|
69
|
+
}
|
|
70
|
+
async onModuleInit() {
|
|
71
|
+
this.timer = setInterval(() => this.prune(), 6e4);
|
|
72
|
+
_optionalChain([this, 'access', _ => _.timer, 'access', _2 => _2.unref, 'optionalCall', _3 => _3()]);
|
|
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 _optionalChain([this, 'access', _4 => _4.options, 'access', _5 => _5.bus, 'optionalAccess', _6 => _6.close, 'call', _7 => _7(), 'access', _8 => _8.catch, 'call', _9 => _9(() => 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 (0, _rxjs.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 = _nullishCoalesce(this.buffers.get(scope), () => ( []));
|
|
161
|
+
const covered = Math.max(this.listeningSince, _nullishCoalesce(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() - (_nullishCoalesce(this.options.replayMaxAgeMs, () => ( DEFAULT_REPLAY_MAX_AGE_MS)));
|
|
171
|
+
const max = _nullishCoalesce(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(_nullishCoalesce(this.prunedUpTo.get(scope), () => ( 0)), dropped.id));
|
|
176
|
+
}
|
|
177
|
+
if (list.length === 0) this.buffers.delete(scope);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
now() {
|
|
181
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _10 => _10.options, 'access', _11 => _11.now, 'optionalCall', _12 => _12()]), () => ( 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 = _nullishCoalesce(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 = _nullishCoalesce(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 (e2) {
|
|
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 = exports.EventsService = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
242
|
+
_common.Injectable.call(void 0, ),
|
|
243
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, 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 (0, _chunkHDFPSA2Vcjs.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 = _optionalChain([this, 'access', _13 => _13.options, 'access', _14 => _14.scopeOf, 'optionalCall', _15 => _15(req)]);
|
|
274
|
+
}
|
|
275
|
+
if (user === void 0 || user === null) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)();
|
|
276
|
+
if (!scope) throw new (0, _chunkHDFPSA2Vcjs.ForbiddenAppException)("No scope", void 0, "EVENTS_NO_SCOPE");
|
|
277
|
+
if (!await this.allowed(user, scope)) throw new (0, _chunkHDFPSA2Vcjs.ForbiddenAppException)(void 0, void 0, "EVENTS_FORBIDDEN");
|
|
278
|
+
const cfg = this.options.controller || {};
|
|
279
|
+
const heartbeatMs = _nullishCoalesce(cfg.heartbeatMs, () => ( DEFAULT_HEARTBEAT_MS));
|
|
280
|
+
const maxBacklog = _nullishCoalesce(cfg.maxBacklogBytes, () => ( DEFAULT_MAX_BACKLOG_BYTES));
|
|
281
|
+
const canRead = this.options.canRead;
|
|
282
|
+
const write = (text) => {
|
|
283
|
+
res.write(text);
|
|
284
|
+
_optionalChain([res, 'access', _16 => _16.flush, 'optionalCall', _17 => _17()]);
|
|
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
|
+
_optionalChain([res, 'access', _18 => _18.flushHeaders, 'optionalCall', _19 => _19()]);
|
|
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
|
+
_optionalChain([subscription, 'optionalAccess', _20 => _20.unsubscribe, 'call', _21 => _21()]);
|
|
303
|
+
res.end();
|
|
304
|
+
};
|
|
305
|
+
const slow = () => (_nullishCoalesce(res.writableLength, () => ( 0))) > maxBacklog;
|
|
306
|
+
subscription = this.events.stream(scope, _nullishCoalesce(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
|
+
_optionalChain([beat, 'access', _22 => _22.unref, 'optionalCall', _23 => _23()]);
|
|
318
|
+
_optionalChain([req, 'access', _24 => _24.on, 'optionalCall', _25 => _25("close", close)]);
|
|
319
|
+
}
|
|
320
|
+
async ticket(req) {
|
|
321
|
+
const cfg = this.options.ticket;
|
|
322
|
+
if (cfg === false) throw new (0, _chunkHDFPSA2Vcjs.NotFoundAppException)();
|
|
323
|
+
const user = this.userOf(req);
|
|
324
|
+
if (user === void 0 || user === null) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)();
|
|
325
|
+
const scope = _optionalChain([this, 'access', _26 => _26.options, 'access', _27 => _27.scopeOf, 'optionalCall', _28 => _28(req)]);
|
|
326
|
+
if (!scope) throw new (0, _chunkHDFPSA2Vcjs.ForbiddenAppException)("No scope", void 0, "EVENTS_NO_SCOPE");
|
|
327
|
+
if (!await this.allowed(user, scope)) throw new (0, _chunkHDFPSA2Vcjs.ForbiddenAppException)(void 0, void 0, "EVENTS_FORBIDDEN");
|
|
328
|
+
const ttlSec = _nullishCoalesce(_optionalChain([cfg, 'optionalAccess', _29 => _29.ttlSec]), () => ( DEFAULT_TICKET_TTL_SEC));
|
|
329
|
+
const id = _crypto.randomBytes.call(void 0, 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) : _optionalChain([req, 'optionalAccess', _30 => _30.user]);
|
|
335
|
+
}
|
|
336
|
+
async allowed(user, scope) {
|
|
337
|
+
return this.options.authorize ? await this.options.authorize(user, scope) : true;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
_chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
341
|
+
_common.Get.call(void 0, ),
|
|
342
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Req.call(void 0, )),
|
|
343
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 1, _common.Res.call(void 0, )),
|
|
344
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 2, _common.Headers.call(void 0, "last-event-id")),
|
|
345
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 3, _common.Query.call(void 0, "desde")),
|
|
346
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 4, _common.Query.call(void 0, "ticket"))
|
|
347
|
+
], EventsController.prototype, "stream", 1);
|
|
348
|
+
_chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
349
|
+
_common.Post.call(void 0, "ticket"),
|
|
350
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Req.call(void 0, ))
|
|
351
|
+
], EventsController.prototype, "ticket", 1);
|
|
352
|
+
EventsController = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
353
|
+
_common.Controller.call(void 0, path),
|
|
354
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, EventsService)),
|
|
355
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 1, _common.Inject.call(void 0, EVENTS_OPTIONS)),
|
|
356
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 2, _common.Inject.call(void 0, EVENTS_TICKET_STORE))
|
|
357
|
+
], EventsController);
|
|
358
|
+
return EventsController;
|
|
359
|
+
}
|
|
360
|
+
function decorateRoute(controller, method, decorators) {
|
|
361
|
+
if (!_optionalChain([decorators, 'optionalAccess', _31 => _31.length])) return;
|
|
362
|
+
const descriptor = Object.getOwnPropertyDescriptor(controller.prototype, method);
|
|
363
|
+
_common.applyDecorators.call(void 0, ...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 = _nullishCoalesce(c, () => ( {}));
|
|
389
|
+
const ctl = createEventsController(_nullishCoalesce(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 && _optionalChain([o, 'access', _32 => _32.ticket, 'optionalAccess', _33 => _33.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: _nullishCoalesce(config.inject, () => ( [])) },
|
|
420
|
+
storeProvider(),
|
|
421
|
+
EventsService
|
|
422
|
+
];
|
|
423
|
+
return {
|
|
424
|
+
module: EventsModule,
|
|
425
|
+
imports: _nullishCoalesce(config.imports, () => ( [])),
|
|
426
|
+
controllers: controllersFor(config.controller),
|
|
427
|
+
providers,
|
|
428
|
+
exports: [EVENTS_OPTIONS, EVENTS_TICKET_STORE, EventsService]
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
EventsModule = exports.EventsModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
433
|
+
_common.Global.call(void 0, ),
|
|
434
|
+
_common.Module.call(void 0, {})
|
|
435
|
+
], EventsModule);
|
|
436
|
+
|
|
437
|
+
// src/events/buses.ts
|
|
438
|
+
|
|
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 (0, _common.Logger)("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 _optionalChain([c, 'access', _34 => _34.end, 'optionalCall', _35 => _35(), 'access', _36 => _36.catch, 'call', _37 => _37(() => void 0)]);
|
|
454
|
+
if (factory && !closed) {
|
|
455
|
+
timer = setTimeout(() => void connect(), _nullishCoalesce(options.reconnectMs, () => ( 5e3)));
|
|
456
|
+
_optionalChain([timer, 'access', _38 => _38.unref, 'optionalCall', _39 => _39()]);
|
|
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) _optionalChain([onMessage, 'optionalCall', _40 => _40(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), _nullishCoalesce(options.reconnectMs, () => ( 5e3)));
|
|
484
|
+
_optionalChain([timer, 'access', _41 => _41.unref, 'optionalCall', _42 => _42()]);
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
async close() {
|
|
488
|
+
closed = true;
|
|
489
|
+
if (timer) clearTimeout(timer);
|
|
490
|
+
const c = client;
|
|
491
|
+
client = null;
|
|
492
|
+
await _optionalChain([c, 'optionalAccess', _43 => _43.end, 'optionalCall', _44 => _44(), 'access', _45 => _45.catch, 'call', _46 => _46(() => void 0)]);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
function RedisBus(options) {
|
|
497
|
+
const channel = _nullishCoalesce(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 = _nullishCoalesce(_nullishCoalesce(options.subscriber, () => ( _optionalChain([options, 'access', _47 => _47.client, 'access', _48 => _48.duplicate, 'optionalCall', _49 => _49()]))), () => ( 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 _optionalChain([s, 'access', _50 => _50.unsubscribe, 'optionalCall', _51 => _51(channel), 'access', _52 => _52.catch, 'call', _53 => _53(() => void 0)]);
|
|
517
|
+
if (s.quit) await s.quit().catch(() => void 0);
|
|
518
|
+
else _optionalChain([s, 'access', _54 => _54.disconnect, 'optionalCall', _55 => _55()]);
|
|
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
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
exports.EVENTS_OPTIONS = EVENTS_OPTIONS; exports.EVENTS_TICKET_STORE = EVENTS_TICKET_STORE; exports.EventsModule = EventsModule; exports.EventsService = EventsService; exports.MemoryTicketStore = MemoryTicketStore; exports.PgNotifyBus = PgNotifyBus; exports.RESYNC_MODULE = RESYNC_MODULE; exports.RESYNC_TYPE = RESYNC_TYPE; exports.RedisBus = RedisBus; exports.createEventsController = createEventsController; exports.createMemoryBusHub = createMemoryBusHub; exports.formatSignal = formatSignal;
|