@miguelmorales13/nestkit 0.26.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/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-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-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 +19 -18
- package/dist/index.js +32 -31
- 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,327 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ForbiddenAppException,
|
|
3
|
+
NotFoundAppException,
|
|
4
|
+
UnauthorizedAppException,
|
|
5
|
+
ValidationAppException
|
|
6
|
+
} from "../chunk-ZFIDAX7K.js";
|
|
7
|
+
import "../chunk-M3GQDRWR.js";
|
|
8
|
+
import "../chunk-VVMRZEIW.js";
|
|
9
|
+
import {
|
|
10
|
+
__decorateClass,
|
|
11
|
+
__decorateParam
|
|
12
|
+
} from "../chunk-D3DDJWUD.js";
|
|
13
|
+
|
|
14
|
+
// src/devices/devices.module.ts
|
|
15
|
+
import { Global, Module } from "@nestjs/common";
|
|
16
|
+
|
|
17
|
+
// src/devices/device-check.interceptor.ts
|
|
18
|
+
import { Inject as Inject2, Injectable as Injectable2 } from "@nestjs/common";
|
|
19
|
+
|
|
20
|
+
// src/devices/devices.options.ts
|
|
21
|
+
var DEFAULT_DEVICE_ERROR_CODES = {
|
|
22
|
+
invalidCode: "INVALID_PAIRING_CODE",
|
|
23
|
+
limitReached: "DEVICE_LIMIT_REACHED",
|
|
24
|
+
notAuthorized: "DEVICE_NOT_AUTHORIZED",
|
|
25
|
+
paused: "DEVICE_PAUSED",
|
|
26
|
+
invalidMode: "INVALID_DEVICE_MODE"
|
|
27
|
+
};
|
|
28
|
+
var DEFAULT_CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
29
|
+
var DEFAULT_CODE_TTL_MS = 15 * 6e4;
|
|
30
|
+
var DEFAULT_DEVICE_CACHE_MS = 5e3;
|
|
31
|
+
var DEFAULT_DEVICE_HEADER = "x-device-token";
|
|
32
|
+
|
|
33
|
+
// src/devices/devices.port.ts
|
|
34
|
+
var DEVICES_OPTIONS = /* @__PURE__ */ Symbol("DEVICES_OPTIONS");
|
|
35
|
+
var DEVICE_STORE = /* @__PURE__ */ Symbol("DEVICE_STORE");
|
|
36
|
+
|
|
37
|
+
// src/devices/devices.service.ts
|
|
38
|
+
import { createHash, randomBytes, randomInt, randomUUID } from "crypto";
|
|
39
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
40
|
+
var sha256 = (s) => createHash("sha256").update(s).digest("hex");
|
|
41
|
+
function validateCodeScheme(alphabet, length) {
|
|
42
|
+
if (new Set(alphabet).size !== alphabet.length) throw new Error("DevicesModule: codeAlphabet has repeated characters.");
|
|
43
|
+
if (alphabet.length < 16) throw new Error("DevicesModule: codeAlphabet needs at least 16 characters.");
|
|
44
|
+
if (!Number.isInteger(length) || length < 6) throw new Error("DevicesModule: codeLength must be an integer >= 6.");
|
|
45
|
+
}
|
|
46
|
+
function generateCode(alphabet = DEFAULT_CODE_ALPHABET, length = 8, groupSize = length / 2) {
|
|
47
|
+
let code = "";
|
|
48
|
+
for (let i = 0; i < length; i += 1) code += alphabet[randomInt(alphabet.length)];
|
|
49
|
+
if (!groupSize || groupSize >= length) return code;
|
|
50
|
+
const parts = [];
|
|
51
|
+
for (let i = 0; i < code.length; i += groupSize) parts.push(code.slice(i, i + groupSize));
|
|
52
|
+
return parts.join("-");
|
|
53
|
+
}
|
|
54
|
+
function normalizeCode(input, alphabet = DEFAULT_CODE_ALPHABET) {
|
|
55
|
+
const up = String(input ?? "").toUpperCase();
|
|
56
|
+
let out = "";
|
|
57
|
+
for (const ch of up) if (alphabet.includes(ch)) out += ch;
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
var DevicesService = class {
|
|
61
|
+
constructor(options, store) {
|
|
62
|
+
this.options = options;
|
|
63
|
+
this.store = store;
|
|
64
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
65
|
+
this.alphabet = options.codeAlphabet ?? DEFAULT_CODE_ALPHABET;
|
|
66
|
+
this.length = options.codeLength ?? 8;
|
|
67
|
+
validateCodeScheme(this.alphabet, this.length);
|
|
68
|
+
if (!options.modes?.length) throw new Error("DevicesModule: `modes` cannot be empty.");
|
|
69
|
+
this.codes = { ...DEFAULT_DEVICE_ERROR_CODES, ...options.errorCodes };
|
|
70
|
+
}
|
|
71
|
+
now() {
|
|
72
|
+
return (this.options.now ?? Date.now)();
|
|
73
|
+
}
|
|
74
|
+
/** Active devices that share the limit with `device`, in the order they started working. */
|
|
75
|
+
async peers(device) {
|
|
76
|
+
const all = await this.store.listByTenant(device.tenant);
|
|
77
|
+
return all.filter((d) => d.status === "active" && (this.options.limitScope !== "site" || d.site === device.site)).sort((a, b) => (a.activatedAt ?? 0) - (b.activatedAt ?? 0) || a.id.localeCompare(b.id));
|
|
78
|
+
}
|
|
79
|
+
async limitFor(device) {
|
|
80
|
+
return await this.options.limit?.(device.tenant, device.site) ?? null;
|
|
81
|
+
}
|
|
82
|
+
pausedError(device, limit) {
|
|
83
|
+
return this.options.onPaused?.(device, limit) ?? new ForbiddenAppException("This device is paused: the plan allows fewer devices.", { limit }, this.codes.paused);
|
|
84
|
+
}
|
|
85
|
+
/** The admin creates a device and gets the code to type on the tablet. */
|
|
86
|
+
async create(input) {
|
|
87
|
+
if (!this.options.modes.includes(input.mode)) {
|
|
88
|
+
throw new ValidationAppException(`Mode "${input.mode}" is not valid.`, { modes: this.options.modes }, this.codes.invalidMode);
|
|
89
|
+
}
|
|
90
|
+
const site = input.site ?? null;
|
|
91
|
+
const limit = await this.limitFor({ tenant: input.tenant, site });
|
|
92
|
+
if (limit !== null) {
|
|
93
|
+
const active = (await this.peers({ tenant: input.tenant, site })).length;
|
|
94
|
+
if (active >= limit) {
|
|
95
|
+
throw new ForbiddenAppException(
|
|
96
|
+
`The plan allows ${limit} device${limit === 1 ? "" : "s"}.`,
|
|
97
|
+
{ limit, active },
|
|
98
|
+
this.codes.limitReached
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const now = this.now();
|
|
103
|
+
const code = generateCode(this.alphabet, this.length, this.options.codeGroupSize ?? this.length / 2);
|
|
104
|
+
const expiresAt = now + (this.options.codeTtlMs ?? DEFAULT_CODE_TTL_MS);
|
|
105
|
+
const device = {
|
|
106
|
+
id: randomUUID(),
|
|
107
|
+
tenant: input.tenant,
|
|
108
|
+
site,
|
|
109
|
+
name: input.name,
|
|
110
|
+
mode: input.mode,
|
|
111
|
+
status: "pending",
|
|
112
|
+
codeHash: sha256(normalizeCode(code, this.alphabet)),
|
|
113
|
+
codeExpiresAt: expiresAt,
|
|
114
|
+
tokenHash: null,
|
|
115
|
+
createdAt: now,
|
|
116
|
+
activatedAt: null,
|
|
117
|
+
revokedAt: null,
|
|
118
|
+
createdBy: input.createdBy ?? null
|
|
119
|
+
};
|
|
120
|
+
await this.store.insert(device);
|
|
121
|
+
return { deviceId: device.id, code, expiresAt };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The tablet types the code. Returns the token ONCE. A device the plan would
|
|
125
|
+
* pause is refused BEFORE redeeming, so the code is not spent for nothing.
|
|
126
|
+
*/
|
|
127
|
+
async redeem(code) {
|
|
128
|
+
const codeHash = sha256(normalizeCode(code, this.alphabet));
|
|
129
|
+
const pending = await this.store.findByCodeHash(codeHash);
|
|
130
|
+
if (!pending || pending.status !== "pending" || pending.codeExpiresAt <= this.now()) throw this.invalidCode();
|
|
131
|
+
const limit = await this.limitFor(pending);
|
|
132
|
+
if (limit !== null && (await this.peers(pending)).length >= limit) throw this.pausedError(pending, limit);
|
|
133
|
+
const token = randomBytes(32).toString("hex");
|
|
134
|
+
const device = await this.store.redeem(codeHash, sha256(token), this.now());
|
|
135
|
+
if (!device) throw this.invalidCode();
|
|
136
|
+
return { deviceId: device.id, token, tenant: device.tenant, site: device.site, mode: device.mode, name: device.name };
|
|
137
|
+
}
|
|
138
|
+
invalidCode() {
|
|
139
|
+
return new NotFoundAppException("Invalid or expired code. Ask for a new one.", void 0, this.codes.invalidCode);
|
|
140
|
+
}
|
|
141
|
+
/** Looks a token up. `null` = unknown, revoked or pending. Cached `cacheMs` (revoke clears it here). */
|
|
142
|
+
async verify(token) {
|
|
143
|
+
const key = sha256(token);
|
|
144
|
+
const ttl = this.options.cacheMs ?? DEFAULT_DEVICE_CACHE_MS;
|
|
145
|
+
const now = this.now();
|
|
146
|
+
const hit = this.cache.get(key);
|
|
147
|
+
if (hit && hit.until > now) return hit.verdict;
|
|
148
|
+
const device = await this.store.findByTokenHash(key);
|
|
149
|
+
let verdict = null;
|
|
150
|
+
if (device && device.status === "active") {
|
|
151
|
+
const limit = await this.limitFor(device);
|
|
152
|
+
const position = limit === null ? 0 : (await this.peers(device)).findIndex((d) => d.id === device.id) + 1;
|
|
153
|
+
verdict = { device, limit, paused: limit !== null && position > limit };
|
|
154
|
+
}
|
|
155
|
+
if (ttl > 0) {
|
|
156
|
+
this.cache.set(key, { verdict, until: now + ttl });
|
|
157
|
+
if (this.cache.size > 500) {
|
|
158
|
+
for (const [k, v] of this.cache) if (v.until <= now) this.cache.delete(k);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return verdict;
|
|
162
|
+
}
|
|
163
|
+
/** Cuts a device off. Throws 404 if it is not the tenant's. */
|
|
164
|
+
async revoke(tenant, deviceId) {
|
|
165
|
+
const device = await this.store.get(deviceId);
|
|
166
|
+
if (!device || device.tenant !== tenant) throw new NotFoundAppException("Device not found", void 0, "DEVICE_NOT_FOUND");
|
|
167
|
+
await this.store.revoke(deviceId, this.now());
|
|
168
|
+
this.cache.clear();
|
|
169
|
+
}
|
|
170
|
+
/** The device lets itself go (the tablet "unlinks"). No-op if the token is unknown. */
|
|
171
|
+
async release(token) {
|
|
172
|
+
const device = await this.store.findByTokenHash(sha256(token));
|
|
173
|
+
if (device) await this.store.revoke(device.id, this.now());
|
|
174
|
+
this.cache.clear();
|
|
175
|
+
}
|
|
176
|
+
/** The panel's list, with the derived `paused` flag. Never includes hashes. */
|
|
177
|
+
async list(tenant) {
|
|
178
|
+
const all = await this.store.listByTenant(tenant);
|
|
179
|
+
const pausedIds = /* @__PURE__ */ new Set();
|
|
180
|
+
const seen = /* @__PURE__ */ new Set();
|
|
181
|
+
for (const d of all) {
|
|
182
|
+
if (d.status !== "active") continue;
|
|
183
|
+
const group = this.options.limitScope === "site" ? d.site ?? "" : "";
|
|
184
|
+
if (seen.has(group)) continue;
|
|
185
|
+
seen.add(group);
|
|
186
|
+
const limit = await this.limitFor(d);
|
|
187
|
+
if (limit === null) continue;
|
|
188
|
+
(await this.peers(d)).slice(limit).forEach((p) => pausedIds.add(p.id));
|
|
189
|
+
}
|
|
190
|
+
return all.sort((a, b) => a.createdAt - b.createdAt).map(({ codeHash: _c, tokenHash: _t, ...rest }) => ({ ...rest, paused: pausedIds.has(rest.id) }));
|
|
191
|
+
}
|
|
192
|
+
/** Error for the interceptor when the token is not good. */
|
|
193
|
+
notAuthorized() {
|
|
194
|
+
return new UnauthorizedAppException(
|
|
195
|
+
"This device is no longer authorized. Ask a manager to pair it again.",
|
|
196
|
+
void 0,
|
|
197
|
+
this.codes.notAuthorized
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
/** Error for the interceptor when it is paused. */
|
|
201
|
+
paused(verdict) {
|
|
202
|
+
return this.pausedError(verdict.device, verdict.limit);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
DevicesService = __decorateClass([
|
|
206
|
+
Injectable(),
|
|
207
|
+
__decorateParam(0, Inject(DEVICES_OPTIONS)),
|
|
208
|
+
__decorateParam(1, Inject(DEVICE_STORE))
|
|
209
|
+
], DevicesService);
|
|
210
|
+
|
|
211
|
+
// src/devices/device-check.interceptor.ts
|
|
212
|
+
var DeviceCheckInterceptor = class {
|
|
213
|
+
constructor(options, devices) {
|
|
214
|
+
this.options = options;
|
|
215
|
+
this.devices = devices;
|
|
216
|
+
}
|
|
217
|
+
async intercept(ctx, next) {
|
|
218
|
+
const req = ctx.switchToHttp().getRequest();
|
|
219
|
+
const header = (this.options.header ?? DEFAULT_DEVICE_HEADER).toLowerCase();
|
|
220
|
+
const raw = req?.headers?.[header];
|
|
221
|
+
const token = Array.isArray(raw) ? raw[0] : raw;
|
|
222
|
+
if (typeof token !== "string" || !token) return next.handle();
|
|
223
|
+
const path = req.path ?? String(req.url ?? "").split("?")[0];
|
|
224
|
+
if ((this.options.skip ?? []).some((r) => r.test(path))) return next.handle();
|
|
225
|
+
const verdict = await this.devices.verify(token);
|
|
226
|
+
if (!verdict) throw this.devices.notAuthorized();
|
|
227
|
+
const tenant = this.options.getTenant ? this.options.getTenant(req) : req?.user?.tenantId;
|
|
228
|
+
if (tenant && tenant !== verdict.device.tenant) throw this.devices.notAuthorized();
|
|
229
|
+
if (verdict.paused && !(this.options.pausedAllowed ?? []).some((r) => r.test(path))) {
|
|
230
|
+
throw this.devices.paused(verdict);
|
|
231
|
+
}
|
|
232
|
+
req.device = verdict.device;
|
|
233
|
+
return next.handle();
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
DeviceCheckInterceptor = __decorateClass([
|
|
237
|
+
Injectable2(),
|
|
238
|
+
__decorateParam(0, Inject2(DEVICES_OPTIONS))
|
|
239
|
+
], DeviceCheckInterceptor);
|
|
240
|
+
|
|
241
|
+
// src/devices/memory.store.ts
|
|
242
|
+
var MemoryDeviceStore = class {
|
|
243
|
+
constructor() {
|
|
244
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
245
|
+
}
|
|
246
|
+
async insert(device) {
|
|
247
|
+
this.rows.set(device.id, { ...device });
|
|
248
|
+
}
|
|
249
|
+
async get(id) {
|
|
250
|
+
const r = this.rows.get(id);
|
|
251
|
+
return r ? { ...r } : null;
|
|
252
|
+
}
|
|
253
|
+
async findByCodeHash(codeHash) {
|
|
254
|
+
for (const r of this.rows.values()) if (r.codeHash === codeHash) return { ...r };
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
async findByTokenHash(tokenHash) {
|
|
258
|
+
for (const r of this.rows.values()) if (r.tokenHash === tokenHash) return { ...r };
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
async listByTenant(tenant) {
|
|
262
|
+
return [...this.rows.values()].filter((r) => r.tenant === tenant).map((r) => ({ ...r }));
|
|
263
|
+
}
|
|
264
|
+
async redeem(codeHash, tokenHash, now) {
|
|
265
|
+
for (const r of this.rows.values()) {
|
|
266
|
+
if (r.codeHash === codeHash && r.status === "pending" && r.codeExpiresAt > now) {
|
|
267
|
+
r.status = "active";
|
|
268
|
+
r.tokenHash = tokenHash;
|
|
269
|
+
r.activatedAt = now;
|
|
270
|
+
return { ...r };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
async revoke(id, now) {
|
|
276
|
+
const r = this.rows.get(id);
|
|
277
|
+
if (!r || r.status === "revoked") return false;
|
|
278
|
+
r.status = "revoked";
|
|
279
|
+
r.revokedAt = now;
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/devices/devices.module.ts
|
|
285
|
+
var exportsList = [DEVICES_OPTIONS, DEVICE_STORE, DevicesService, DeviceCheckInterceptor];
|
|
286
|
+
var DevicesModule = class {
|
|
287
|
+
static forRoot(options) {
|
|
288
|
+
const providers = [
|
|
289
|
+
{ provide: DEVICES_OPTIONS, useValue: options },
|
|
290
|
+
{ provide: DEVICE_STORE, useValue: options.store ?? new MemoryDeviceStore() },
|
|
291
|
+
DevicesService,
|
|
292
|
+
DeviceCheckInterceptor
|
|
293
|
+
];
|
|
294
|
+
return { module: DevicesModule, providers, exports: exportsList };
|
|
295
|
+
}
|
|
296
|
+
static forRootAsync(config) {
|
|
297
|
+
const providers = [
|
|
298
|
+
{ provide: DEVICES_OPTIONS, useFactory: config.useFactory, inject: config.inject ?? [] },
|
|
299
|
+
{
|
|
300
|
+
provide: DEVICE_STORE,
|
|
301
|
+
useFactory: (o) => o.store ?? new MemoryDeviceStore(),
|
|
302
|
+
inject: [DEVICES_OPTIONS]
|
|
303
|
+
},
|
|
304
|
+
DevicesService,
|
|
305
|
+
DeviceCheckInterceptor
|
|
306
|
+
];
|
|
307
|
+
return { module: DevicesModule, imports: config.imports ?? [], providers, exports: exportsList };
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
DevicesModule = __decorateClass([
|
|
311
|
+
Global(),
|
|
312
|
+
Module({})
|
|
313
|
+
], DevicesModule);
|
|
314
|
+
export {
|
|
315
|
+
DEFAULT_CODE_ALPHABET,
|
|
316
|
+
DEFAULT_DEVICE_ERROR_CODES,
|
|
317
|
+
DEFAULT_DEVICE_HEADER,
|
|
318
|
+
DEVICES_OPTIONS,
|
|
319
|
+
DEVICE_STORE,
|
|
320
|
+
DeviceCheckInterceptor,
|
|
321
|
+
DevicesModule,
|
|
322
|
+
DevicesService,
|
|
323
|
+
MemoryDeviceStore,
|
|
324
|
+
generateCode,
|
|
325
|
+
normalizeCode,
|
|
326
|
+
validateCodeScheme
|
|
327
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DeviceRecord, DeviceStorePort } from './devices.port.js';
|
|
2
|
+
/** In-process store: tests and a single instance. `redeem` is atomic by being synchronous. */
|
|
3
|
+
export declare class MemoryDeviceStore implements DeviceStorePort {
|
|
4
|
+
private readonly rows;
|
|
5
|
+
insert(device: DeviceRecord): Promise<void>;
|
|
6
|
+
get(id: string): Promise<DeviceRecord | null>;
|
|
7
|
+
findByCodeHash(codeHash: string): Promise<DeviceRecord | null>;
|
|
8
|
+
findByTokenHash(tokenHash: string): Promise<DeviceRecord | null>;
|
|
9
|
+
listByTenant(tenant: string): Promise<DeviceRecord[]>;
|
|
10
|
+
redeem(codeHash: string, tokenHash: string, now: number): Promise<DeviceRecord | null>;
|
|
11
|
+
revoke(id: string, now: number): Promise<boolean>;
|
|
12
|
+
}
|
package/dist/email/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@ var _chunkQ5Y33L7Bcjs = require('../chunk-Q5Y33L7B.cjs');
|
|
|
4
4
|
require('../chunk-PNK5WMPP.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunkLRX7AMSScjs = require('../chunk-LRX7AMSS.cjs');
|
|
8
8
|
|
|
9
9
|
// src/email/email.port.ts
|
|
10
10
|
var _common = require('@nestjs/common');
|
|
@@ -373,7 +373,7 @@ var EmailModule = class {
|
|
|
373
373
|
return { module: EmailModule, imports: _nullishCoalesce(config.imports, () => ( [])), providers, exports: [EMAIL, EMAIL_OPTIONS] };
|
|
374
374
|
}
|
|
375
375
|
};
|
|
376
|
-
EmailModule = exports.EmailModule =
|
|
376
|
+
EmailModule = exports.EmailModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
377
377
|
_common.Global.call(void 0, ),
|
|
378
378
|
_common.Module.call(void 0, {})
|
|
379
379
|
], EmailModule);
|
package/dist/email/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkLRX7AMSScjs = require('../../chunk-LRX7AMSS.cjs');
|
|
4
4
|
|
|
5
5
|
// src/email/nodemailer/nodemailer.module.ts
|
|
6
6
|
var _common = require('@nestjs/common');
|
|
@@ -15,7 +15,7 @@ function requireEnv(name) {
|
|
|
15
15
|
}
|
|
16
16
|
var NodemailerModule = class {
|
|
17
17
|
};
|
|
18
|
-
NodemailerModule = exports.NodemailerModule =
|
|
18
|
+
NodemailerModule = exports.NodemailerModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
19
19
|
_common.Module.call(void 0, {
|
|
20
20
|
providers: [
|
|
21
21
|
{
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkLRX7AMSScjs = require('../../chunk-LRX7AMSS.cjs');
|
|
4
4
|
|
|
5
5
|
// src/email/resend/resend.module.ts
|
|
6
6
|
var _common = require('@nestjs/common');
|
|
@@ -8,7 +8,7 @@ var _resend = require('resend');
|
|
|
8
8
|
var RESEND_CLIENT = /* @__PURE__ */ Symbol("RESEND_CLIENT");
|
|
9
9
|
var ResendModule = class {
|
|
10
10
|
};
|
|
11
|
-
ResendModule = exports.ResendModule =
|
|
11
|
+
ResendModule = exports.ResendModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
12
12
|
_common.Module.call(void 0, {
|
|
13
13
|
providers: [
|
|
14
14
|
{
|
package/dist/entities/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
var _chunkQS2W5XCQcjs = require('../chunk-QS2W5XCQ.cjs');
|
|
4
|
-
require('../chunk-
|
|
4
|
+
require('../chunk-LRX7AMSS.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
exports.BaseResponseDto = _chunkQS2W5XCQcjs.BaseResponseDto;
|
package/dist/entities/index.js
CHANGED
package/dist/errors/index.cjs
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-7SOM7EZP.cjs');
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
var _chunkJJKGQNGDcjs = require('../chunk-JJKGQNGD.cjs');
|
|
5
|
+
require('../chunk-AXMW5JF7.cjs');
|
|
6
|
+
require('../chunk-Y72D5L6I.cjs');
|
|
7
|
+
require('../chunk-SDWHKPXI.cjs');
|
|
4
8
|
|
|
5
9
|
|
|
6
10
|
|
|
7
11
|
|
|
8
|
-
var _chunkHDFPSA2Vcjs = require('../chunk-HDFPSA2V.cjs');
|
|
9
12
|
|
|
10
13
|
|
|
11
|
-
var
|
|
14
|
+
var _chunkHDFPSA2Vcjs = require('../chunk-HDFPSA2V.cjs');
|
|
12
15
|
|
|
13
16
|
|
|
14
17
|
var _chunkQ5Y33L7Bcjs = require('../chunk-Q5Y33L7B.cjs');
|
|
15
|
-
require('../chunk-AXMW5JF7.cjs');
|
|
16
|
-
require('../chunk-Y72D5L6I.cjs');
|
|
17
|
-
require('../chunk-SDWHKPXI.cjs');
|
|
18
18
|
require('../chunk-PNK5WMPP.cjs');
|
|
19
|
-
require('../chunk-
|
|
19
|
+
require('../chunk-LRX7AMSS.cjs');
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
|
|
@@ -25,4 +25,4 @@ require('../chunk-2REOCMUD.cjs');
|
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
exports.AppException = _chunkQ5Y33L7Bcjs.AppException; exports.ConflictAppException = _chunkHDFPSA2Vcjs.ConflictAppException; exports.ForbiddenAppException = _chunkHDFPSA2Vcjs.ForbiddenAppException; exports.GlobalExceptionFilter =
|
|
28
|
+
exports.AppException = _chunkQ5Y33L7Bcjs.AppException; exports.ConflictAppException = _chunkHDFPSA2Vcjs.ConflictAppException; exports.ForbiddenAppException = _chunkHDFPSA2Vcjs.ForbiddenAppException; exports.GlobalExceptionFilter = _chunkJJKGQNGDcjs.GlobalExceptionFilter; exports.NotFoundAppException = _chunkHDFPSA2Vcjs.NotFoundAppException; exports.UnauthorizedAppException = _chunkHDFPSA2Vcjs.UnauthorizedAppException; exports.ValidationAppException = _chunkHDFPSA2Vcjs.ValidationAppException;
|
package/dist/errors/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import "../chunk-DQYAIQQ5.js";
|
|
2
|
+
import {
|
|
3
|
+
GlobalExceptionFilter
|
|
4
|
+
} from "../chunk-5LVGCTJ3.js";
|
|
5
|
+
import "../chunk-X4PQJZ4G.js";
|
|
6
|
+
import "../chunk-TCAMLNUZ.js";
|
|
7
|
+
import "../chunk-ETRUVZTJ.js";
|
|
2
8
|
import {
|
|
3
9
|
ConflictAppException,
|
|
4
10
|
ForbiddenAppException,
|
|
@@ -6,17 +12,11 @@ import {
|
|
|
6
12
|
UnauthorizedAppException,
|
|
7
13
|
ValidationAppException
|
|
8
14
|
} from "../chunk-ZFIDAX7K.js";
|
|
9
|
-
import {
|
|
10
|
-
GlobalExceptionFilter
|
|
11
|
-
} from "../chunk-2DUEQKOI.js";
|
|
12
15
|
import {
|
|
13
16
|
AppException
|
|
14
17
|
} from "../chunk-M3GQDRWR.js";
|
|
15
|
-
import "../chunk-X4PQJZ4G.js";
|
|
16
|
-
import "../chunk-TCAMLNUZ.js";
|
|
17
|
-
import "../chunk-ETRUVZTJ.js";
|
|
18
18
|
import "../chunk-VVMRZEIW.js";
|
|
19
|
-
import "../chunk-
|
|
19
|
+
import "../chunk-D3DDJWUD.js";
|
|
20
20
|
export {
|
|
21
21
|
AppException,
|
|
22
22
|
ConflictAppException,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EventsBus, PgNotifyClient, RedisPubSubClient } from './events.types.js';
|
|
2
|
+
export interface PgNotifyBusOptions {
|
|
3
|
+
/** Channel name (an identifier: it is interpolated in `LISTEN`). */
|
|
4
|
+
channel: string;
|
|
5
|
+
/** A connected dedicated client, or a factory (enables reconnection). */
|
|
6
|
+
client: PgNotifyClient | (() => Promise<PgNotifyClient>);
|
|
7
|
+
/** Reconnection delay when the client is a factory. Default 5000. */
|
|
8
|
+
reconnectMs?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Postgres `LISTEN/NOTIFY`. The payload limit (~8000 bytes) is why `maxPayloadBytes` defaults to 7000. */
|
|
11
|
+
export declare function PgNotifyBus(options: PgNotifyBusOptions): EventsBus;
|
|
12
|
+
export interface RedisBusOptions {
|
|
13
|
+
client: RedisPubSubClient;
|
|
14
|
+
/** Redis channel. Default `events`. */
|
|
15
|
+
channel?: string;
|
|
16
|
+
/** Subscriber connection; default `client.duplicate()` (ioredis cannot publish while subscribed). */
|
|
17
|
+
subscriber?: RedisPubSubClient;
|
|
18
|
+
}
|
|
19
|
+
/** Redis pub/sub. */
|
|
20
|
+
export declare function RedisBus(options: RedisBusOptions): EventsBus;
|
|
21
|
+
/**
|
|
22
|
+
* In-process bus shared by several `connect()`ions: simulates N instances in tests (and nothing else — it does not
|
|
23
|
+
* cross processes). Delivers asynchronously, like a real bus, and echoes to the sender.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createMemoryBusHub(): {
|
|
26
|
+
connect(): EventsBus;
|
|
27
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Type } from '@nestjs/common';
|
|
2
|
+
import type { EventSignal } from './events.types.js';
|
|
3
|
+
export declare const DEFAULT_HEARTBEAT_MS = 25000;
|
|
4
|
+
export declare const DEFAULT_MAX_BACKLOG_BYTES: number;
|
|
5
|
+
/** SSE wire format of one signal. */
|
|
6
|
+
export declare function formatSignal(s: EventSignal): string;
|
|
7
|
+
/**
|
|
8
|
+
* Builds the SSE controller for a route prefix (dynamic path ⇒ a factory).
|
|
9
|
+
*
|
|
10
|
+
* `GET <path>?ticket=…` (or the host's own auth) streams; `POST <path>/ticket` issues a one-use ticket.
|
|
11
|
+
* Authorization runs BEFORE the stream opens (an error thrown after `flushHeaders` would travel as data in a 200).
|
|
12
|
+
*/
|
|
13
|
+
export declare function createEventsController(path?: string): Type<unknown>;
|
|
14
|
+
/** Applies the host's decorators (auth, "public"…) to a controller method. */
|
|
15
|
+
export declare function decorateRoute(controller: Type<unknown>, method: string, decorators: Array<ClassDecorator | MethodDecorator | PropertyDecorator> | undefined): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { type EventsControllerOptions, type EventsOptions } from './events.options.js';
|
|
3
|
+
/**
|
|
4
|
+
* Signals per scope over SSE, with replay and optional fan-out between instances.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* EventsModule.forRoot({
|
|
8
|
+
* bus: PgNotifyBus({ channel: 'app_events', client: () => connectDirectPg() }),
|
|
9
|
+
* scopeOf: (req) => req.user?.tenantId,
|
|
10
|
+
* kinds: ['orders', 'stock'],
|
|
11
|
+
* authorize: (user, scope) => user.tenantId === scope,
|
|
12
|
+
* controller: { path: 'events', streamDecorators: [Public()], ticketDecorators: [UseGuards(AuthGuard)] },
|
|
13
|
+
* })
|
|
14
|
+
* // after commit: events.publish(tenantId, { module: 'orders', type: 'created', ref: orderId })
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The controller is optional (`controller: false`): `EventsService.stream()` returns an Observable you can serve
|
|
18
|
+
* with Nest's `@Sse()` or your own transport. For several instances use a shared `ticket.store`.
|
|
19
|
+
*/
|
|
20
|
+
export declare class EventsModule {
|
|
21
|
+
static forRoot(options?: EventsOptions): DynamicModule;
|
|
22
|
+
/** `controller` is repeated here because routes must be known before the factory runs. */
|
|
23
|
+
static forRootAsync(config: {
|
|
24
|
+
imports?: any[];
|
|
25
|
+
inject?: any[];
|
|
26
|
+
useFactory: (...args: any[]) => EventsOptions | Promise<EventsOptions>;
|
|
27
|
+
controller?: false | EventsControllerOptions;
|
|
28
|
+
}): DynamicModule;
|
|
29
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { EventSignal, EventsBus, TicketStorePort } from './events.types.js';
|
|
2
|
+
export declare const EVENTS_OPTIONS: unique symbol;
|
|
3
|
+
export declare const EVENTS_TICKET_STORE: unique symbol;
|
|
4
|
+
export interface EventsControllerOptions {
|
|
5
|
+
/** Route prefix. Default `events` (`GET /events`, `POST /events/ticket`). */
|
|
6
|
+
path?: string;
|
|
7
|
+
/** Decorators for `POST ticket` (typically the host's authentication). */
|
|
8
|
+
ticketDecorators?: Array<ClassDecorator | MethodDecorator | PropertyDecorator>;
|
|
9
|
+
/** Decorators for `GET` (typically the host's "public" marker, because the ticket authenticates it). */
|
|
10
|
+
streamDecorators?: Array<ClassDecorator | MethodDecorator | PropertyDecorator>;
|
|
11
|
+
/** Heartbeat comment interval, ms. Default 25 000. */
|
|
12
|
+
heartbeatMs?: number;
|
|
13
|
+
/** Close the connection when this many bytes are queued unread. Default 65 536. */
|
|
14
|
+
maxBacklogBytes?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface EventsOptions {
|
|
17
|
+
/** Fan-out between instances. Omit for in-memory (single instance). */
|
|
18
|
+
bus?: EventsBus;
|
|
19
|
+
/** Scope of the request (tenant, branch…). `undefined` → rejected. Used by the controller. */
|
|
20
|
+
scopeOf?: (request: any) => string | undefined;
|
|
21
|
+
/** Who is asking when no ticket is used. Default `req.user`. */
|
|
22
|
+
getUser?: (request: any) => unknown;
|
|
23
|
+
/** Allowed `module` values. Omit to accept any. */
|
|
24
|
+
kinds?: readonly string[];
|
|
25
|
+
/** Max signals kept per scope for replay. Default 500. */
|
|
26
|
+
replayWindow?: number;
|
|
27
|
+
/** Max age of a replayable signal, ms. Default 300 000. */
|
|
28
|
+
replayMaxAgeMs?: number;
|
|
29
|
+
/** Max bytes of a signal sent over the bus. Larger ones degrade to `channel.resync`. Default 7000. */
|
|
30
|
+
maxPayloadBytes?: number;
|
|
31
|
+
/** One-use tickets for `EventSource`. `false` disables the ticket route. */
|
|
32
|
+
ticket?: false | {
|
|
33
|
+
ttlSec?: number;
|
|
34
|
+
store?: TicketStorePort;
|
|
35
|
+
};
|
|
36
|
+
/** May this user listen to this scope? Default: any identified user. */
|
|
37
|
+
authorize?: (user: any, scope: string) => boolean | Promise<boolean>;
|
|
38
|
+
/** Per-signal filter (e.g. only modules the user can read). Default: all. */
|
|
39
|
+
canRead?: (user: any, signal: EventSignal) => boolean;
|
|
40
|
+
/** Mount the SSE controller. `false` = service only. */
|
|
41
|
+
controller?: false | EventsControllerOptions;
|
|
42
|
+
/** Clock, for tests (ms). */
|
|
43
|
+
now?: () => number;
|
|
44
|
+
/** Identifier of this instance. Default random. */
|
|
45
|
+
instanceId?: string;
|
|
46
|
+
}
|
|
47
|
+
export declare const DEFAULT_REPLAY_WINDOW = 500;
|
|
48
|
+
export declare const DEFAULT_REPLAY_MAX_AGE_MS = 300000;
|
|
49
|
+
export declare const DEFAULT_MAX_PAYLOAD_BYTES = 7000;
|
|
50
|
+
export declare const DEFAULT_TICKET_TTL_SEC = 30;
|