@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,58 @@
|
|
|
1
|
+
import { type DevicesOptions } from './devices.options.js';
|
|
2
|
+
import { type CreatedDevice, type DeviceRecord, type DeviceStorePort, type PairedDevice, type VerifiedDevice } from './devices.port.js';
|
|
3
|
+
/** Throws at boot if the code scheme is weak or inconsistent. */
|
|
4
|
+
export declare function validateCodeScheme(alphabet: string, length: number): void;
|
|
5
|
+
/** Uniform draw (no modulo bias): `randomInt` rejects internally. */
|
|
6
|
+
export declare function generateCode(alphabet?: string, length?: number, groupSize?: number): string;
|
|
7
|
+
/** What the tablet typed -> what is compared: upper case, only alphabet characters. */
|
|
8
|
+
export declare function normalizeCode(input: string, alphabet?: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Pairing of shared devices (a tablet at the counter, a kitchen screen): an
|
|
11
|
+
* admin creates the device and gets a short code, the tablet redeems it for a
|
|
12
|
+
* long-lived token tied to a mode and a site. Only hashes are stored.
|
|
13
|
+
*
|
|
14
|
+
* Throttle the redeem endpoint in the host (it takes a guessable-by-brute-force
|
|
15
|
+
* code of ~40 bits, short-lived, but still).
|
|
16
|
+
*/
|
|
17
|
+
export declare class DevicesService {
|
|
18
|
+
private readonly options;
|
|
19
|
+
private readonly store;
|
|
20
|
+
private readonly alphabet;
|
|
21
|
+
private readonly length;
|
|
22
|
+
private readonly codes;
|
|
23
|
+
private readonly cache;
|
|
24
|
+
constructor(options: DevicesOptions, store: DeviceStorePort);
|
|
25
|
+
private now;
|
|
26
|
+
/** Active devices that share the limit with `device`, in the order they started working. */
|
|
27
|
+
private peers;
|
|
28
|
+
private limitFor;
|
|
29
|
+
private pausedError;
|
|
30
|
+
/** The admin creates a device and gets the code to type on the tablet. */
|
|
31
|
+
create(input: {
|
|
32
|
+
tenant: string;
|
|
33
|
+
site?: string | null;
|
|
34
|
+
name: string;
|
|
35
|
+
mode: string;
|
|
36
|
+
createdBy?: string | null;
|
|
37
|
+
}): Promise<CreatedDevice>;
|
|
38
|
+
/**
|
|
39
|
+
* The tablet types the code. Returns the token ONCE. A device the plan would
|
|
40
|
+
* pause is refused BEFORE redeeming, so the code is not spent for nothing.
|
|
41
|
+
*/
|
|
42
|
+
redeem(code: string): Promise<PairedDevice>;
|
|
43
|
+
private invalidCode;
|
|
44
|
+
/** Looks a token up. `null` = unknown, revoked or pending. Cached `cacheMs` (revoke clears it here). */
|
|
45
|
+
verify(token: string): Promise<VerifiedDevice | null>;
|
|
46
|
+
/** Cuts a device off. Throws 404 if it is not the tenant's. */
|
|
47
|
+
revoke(tenant: string, deviceId: string): Promise<void>;
|
|
48
|
+
/** The device lets itself go (the tablet "unlinks"). No-op if the token is unknown. */
|
|
49
|
+
release(token: string): Promise<void>;
|
|
50
|
+
/** The panel's list, with the derived `paused` flag. Never includes hashes. */
|
|
51
|
+
list(tenant: string): Promise<Array<Omit<DeviceRecord, 'codeHash' | 'tokenHash'> & {
|
|
52
|
+
paused: boolean;
|
|
53
|
+
}>>;
|
|
54
|
+
/** Error for the interceptor when the token is not good. */
|
|
55
|
+
notAuthorized(): Error;
|
|
56
|
+
/** Error for the interceptor when it is paused. */
|
|
57
|
+
paused(verdict: VerifiedDevice): Error;
|
|
58
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await 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
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkHDFPSA2Vcjs = require('../chunk-HDFPSA2V.cjs');
|
|
7
|
+
require('../chunk-Q5Y33L7B.cjs');
|
|
8
|
+
require('../chunk-PNK5WMPP.cjs');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _chunkLRX7AMSScjs = require('../chunk-LRX7AMSS.cjs');
|
|
13
|
+
|
|
14
|
+
// src/devices/devices.module.ts
|
|
15
|
+
var _common = require('@nestjs/common');
|
|
16
|
+
|
|
17
|
+
// src/devices/device-check.interceptor.ts
|
|
18
|
+
|
|
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
|
+
var _crypto = require('crypto');
|
|
39
|
+
|
|
40
|
+
var sha256 = (s) => _crypto.createHash.call(void 0, "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[_crypto.randomInt.call(void 0, 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(_nullishCoalesce(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 = _nullishCoalesce(options.codeAlphabet, () => ( DEFAULT_CODE_ALPHABET));
|
|
66
|
+
this.length = _nullishCoalesce(options.codeLength, () => ( 8));
|
|
67
|
+
validateCodeScheme(this.alphabet, this.length);
|
|
68
|
+
if (!_optionalChain([options, 'access', _ => _.modes, 'optionalAccess', _2 => _2.length])) throw new Error("DevicesModule: `modes` cannot be empty.");
|
|
69
|
+
this.codes = { ...DEFAULT_DEVICE_ERROR_CODES, ...options.errorCodes };
|
|
70
|
+
}
|
|
71
|
+
now() {
|
|
72
|
+
return (_nullishCoalesce(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) => (_nullishCoalesce(a.activatedAt, () => ( 0))) - (_nullishCoalesce(b.activatedAt, () => ( 0))) || a.id.localeCompare(b.id));
|
|
78
|
+
}
|
|
79
|
+
async limitFor(device) {
|
|
80
|
+
return await _asyncNullishCoalesce(await _optionalChain([this, 'access', _3 => _3.options, 'access', _4 => _4.limit, 'optionalCall', _5 => _5(device.tenant, device.site)]), async () => ( null));
|
|
81
|
+
}
|
|
82
|
+
pausedError(device, limit) {
|
|
83
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _6 => _6.options, 'access', _7 => _7.onPaused, 'optionalCall', _8 => _8(device, limit)]), () => ( new (0, _chunkHDFPSA2Vcjs.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 (0, _chunkHDFPSA2Vcjs.ValidationAppException)(`Mode "${input.mode}" is not valid.`, { modes: this.options.modes }, this.codes.invalidMode);
|
|
89
|
+
}
|
|
90
|
+
const site = _nullishCoalesce(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 (0, _chunkHDFPSA2Vcjs.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, _nullishCoalesce(this.options.codeGroupSize, () => ( this.length / 2)));
|
|
104
|
+
const expiresAt = now + (_nullishCoalesce(this.options.codeTtlMs, () => ( DEFAULT_CODE_TTL_MS)));
|
|
105
|
+
const device = {
|
|
106
|
+
id: _crypto.randomUUID.call(void 0, ),
|
|
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: _nullishCoalesce(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 = _crypto.randomBytes.call(void 0, 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 (0, _chunkHDFPSA2Vcjs.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 = _nullishCoalesce(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 (0, _chunkHDFPSA2Vcjs.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" ? _nullishCoalesce(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 (0, _chunkHDFPSA2Vcjs.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 = exports.DevicesService = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
206
|
+
_common.Injectable.call(void 0, ),
|
|
207
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, DEVICES_OPTIONS)),
|
|
208
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 1, _common.Inject.call(void 0, 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 = (_nullishCoalesce(this.options.header, () => ( DEFAULT_DEVICE_HEADER))).toLowerCase();
|
|
220
|
+
const raw = _optionalChain([req, 'optionalAccess', _9 => _9.headers, 'optionalAccess', _10 => _10[header]]);
|
|
221
|
+
const token = Array.isArray(raw) ? raw[0] : raw;
|
|
222
|
+
if (typeof token !== "string" || !token) return next.handle();
|
|
223
|
+
const path = _nullishCoalesce(req.path, () => ( String(_nullishCoalesce(req.url, () => ( ""))).split("?")[0]));
|
|
224
|
+
if ((_nullishCoalesce(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) : _optionalChain([req, 'optionalAccess', _11 => _11.user, 'optionalAccess', _12 => _12.tenantId]);
|
|
228
|
+
if (tenant && tenant !== verdict.device.tenant) throw this.devices.notAuthorized();
|
|
229
|
+
if (verdict.paused && !(_nullishCoalesce(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 = exports.DeviceCheckInterceptor = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
237
|
+
_common.Injectable.call(void 0, ),
|
|
238
|
+
_chunkLRX7AMSScjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, 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: _nullishCoalesce(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: _nullishCoalesce(config.inject, () => ( [])) },
|
|
299
|
+
{
|
|
300
|
+
provide: DEVICE_STORE,
|
|
301
|
+
useFactory: (o) => _nullishCoalesce(o.store, () => ( new MemoryDeviceStore())),
|
|
302
|
+
inject: [DEVICES_OPTIONS]
|
|
303
|
+
},
|
|
304
|
+
DevicesService,
|
|
305
|
+
DeviceCheckInterceptor
|
|
306
|
+
];
|
|
307
|
+
return { module: DevicesModule, imports: _nullishCoalesce(config.imports, () => ( [])), providers, exports: exportsList };
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
DevicesModule = exports.DevicesModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
311
|
+
_common.Global.call(void 0, ),
|
|
312
|
+
_common.Module.call(void 0, {})
|
|
313
|
+
], DevicesModule);
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
exports.DEFAULT_CODE_ALPHABET = DEFAULT_CODE_ALPHABET; exports.DEFAULT_DEVICE_ERROR_CODES = DEFAULT_DEVICE_ERROR_CODES; exports.DEFAULT_DEVICE_HEADER = DEFAULT_DEVICE_HEADER; exports.DEVICES_OPTIONS = DEVICES_OPTIONS; exports.DEVICE_STORE = DEVICE_STORE; exports.DeviceCheckInterceptor = DeviceCheckInterceptor; exports.DevicesModule = DevicesModule; exports.DevicesService = DevicesService; exports.MemoryDeviceStore = MemoryDeviceStore; exports.generateCode = generateCode; exports.normalizeCode = normalizeCode; exports.validateCodeScheme = validateCodeScheme;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { DevicesModule } from './devices.module.js';
|
|
2
|
+
export { DevicesService, generateCode, normalizeCode, validateCodeScheme } from './devices.service.js';
|
|
3
|
+
export { DeviceCheckInterceptor } from './device-check.interceptor.js';
|
|
4
|
+
export { MemoryDeviceStore } from './memory.store.js';
|
|
5
|
+
export { DEVICES_OPTIONS, DEVICE_STORE } from './devices.port.js';
|
|
6
|
+
export type { DeviceRecord, DeviceStatus, DeviceStorePort, PairedDevice, CreatedDevice, VerifiedDevice, } from './devices.port.js';
|
|
7
|
+
export { DEFAULT_CODE_ALPHABET, DEFAULT_DEVICE_ERROR_CODES, DEFAULT_DEVICE_HEADER, } from './devices.options.js';
|
|
8
|
+
export type { DevicesOptions, DeviceErrorCodes } from './devices.options.js';
|