@miguelmorales13/nestkit 0.22.0 → 0.25.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.
@@ -0,0 +1,31 @@
1
+ import type { PushChannelPort, PushDevice, PushDeviceStorePort, PushMessage, PushPort, PushResult, ResolvedPushMessage, SendOptions } from './push.port.js';
2
+ /**
3
+ * Routes each device to its channel, translates the message once, and
4
+ * turns dead devices into deletions when a store is there to delete from.
5
+ *
6
+ * Fan-out is per device, concurrently: FCM's v1 API has no multicast, and
7
+ * one slow or dead device must not hold the rest. A device on a channel
8
+ * that is not configured fails with `channel_not_configured` — reported,
9
+ * never thrown.
10
+ */
11
+ export declare class PushService implements PushPort {
12
+ private readonly devices?;
13
+ private readonly concurrency;
14
+ private readonly logger;
15
+ private readonly byKind;
16
+ constructor(channels: PushChannelPort[], devices?: PushDeviceStorePort | undefined, concurrency?: number);
17
+ channels(): {
18
+ fcm: boolean;
19
+ webpush: boolean;
20
+ };
21
+ send(devices: PushDevice | PushDevice[], message: PushMessage, options?: SendOptions): Promise<PushResult>;
22
+ sendToUser(userId: string, message: PushMessage, options?: SendOptions): Promise<PushResult>;
23
+ sendToTopic(topic: string, message: PushMessage, options?: SendOptions): Promise<{
24
+ id: string | null;
25
+ }>;
26
+ subscribeToTopic(tokens: string[], topic: string): Promise<void>;
27
+ unsubscribeFromTopic(tokens: string[], topic: string): Promise<void>;
28
+ private fcm;
29
+ }
30
+ /** Every `I18nMessage` in the message turned into text, once, in `lang`. */
31
+ export declare function resolveMessage(message: PushMessage, lang?: string): ResolvedPushMessage;
@@ -0,0 +1,35 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }require('../chunk-2REOCMUD.cjs');
2
+
3
+ // src/security/signed-state.ts
4
+ var _crypto = require('crypto');
5
+ function signState(payload, options) {
6
+ const body = JSON.stringify({ ts: Date.now(), nonce: _crypto.randomBytes.call(void 0, 8).toString("hex"), ...payload });
7
+ const b64 = Buffer.from(body).toString("base64url");
8
+ const sig = _crypto.createHmac.call(void 0, "sha256", options.secret).update(b64).digest("base64url");
9
+ return `${b64}.${sig}`;
10
+ }
11
+ function readState(state, options) {
12
+ if (!state) return null;
13
+ const parts = state.split(".");
14
+ if (parts.length !== 2) return null;
15
+ const [b64, sig] = parts;
16
+ const expected = _crypto.createHmac.call(void 0, "sha256", options.secret).update(b64).digest("base64url");
17
+ const provided = Buffer.from(sig);
18
+ const wanted = Buffer.from(expected);
19
+ if (provided.length !== wanted.length || !_crypto.timingSafeEqual.call(void 0, provided, wanted)) return null;
20
+ let parsed;
21
+ try {
22
+ parsed = JSON.parse(Buffer.from(b64, "base64url").toString());
23
+ } catch (e) {
24
+ return null;
25
+ }
26
+ const ts = parsed.ts;
27
+ const ttl = _nullishCoalesce(options.ttlMs, () => ( 10 * 6e4));
28
+ if (typeof ts !== "number" || !Number.isFinite(ts) || Date.now() - ts >= ttl) return null;
29
+ const { ts: _ts, nonce: _nonce, ...rest } = parsed;
30
+ return rest;
31
+ }
32
+
33
+
34
+
35
+ exports.readState = readState; exports.signState = signState;
@@ -0,0 +1,2 @@
1
+ export { signState, readState } from './signed-state.js';
2
+ export type { SignedStateOptions } from './signed-state.js';
@@ -0,0 +1,35 @@
1
+ import "../chunk-4MGIQFAJ.js";
2
+
3
+ // src/security/signed-state.ts
4
+ import { createHmac, randomBytes, timingSafeEqual } from "crypto";
5
+ function signState(payload, options) {
6
+ const body = JSON.stringify({ ts: Date.now(), nonce: randomBytes(8).toString("hex"), ...payload });
7
+ const b64 = Buffer.from(body).toString("base64url");
8
+ const sig = createHmac("sha256", options.secret).update(b64).digest("base64url");
9
+ return `${b64}.${sig}`;
10
+ }
11
+ function readState(state, options) {
12
+ if (!state) return null;
13
+ const parts = state.split(".");
14
+ if (parts.length !== 2) return null;
15
+ const [b64, sig] = parts;
16
+ const expected = createHmac("sha256", options.secret).update(b64).digest("base64url");
17
+ const provided = Buffer.from(sig);
18
+ const wanted = Buffer.from(expected);
19
+ if (provided.length !== wanted.length || !timingSafeEqual(provided, wanted)) return null;
20
+ let parsed;
21
+ try {
22
+ parsed = JSON.parse(Buffer.from(b64, "base64url").toString());
23
+ } catch {
24
+ return null;
25
+ }
26
+ const ts = parsed.ts;
27
+ const ttl = options.ttlMs ?? 10 * 6e4;
28
+ if (typeof ts !== "number" || !Number.isFinite(ts) || Date.now() - ts >= ttl) return null;
29
+ const { ts: _ts, nonce: _nonce, ...rest } = parsed;
30
+ return rest;
31
+ }
32
+ export {
33
+ readState,
34
+ signState
35
+ };
@@ -0,0 +1,42 @@
1
+ export interface SignedStateOptions {
2
+ /** HMAC key. Reuse a secret you already trust and rotate (a JWT signing key works well) rather than minting a new one just for this. */
3
+ secret: string;
4
+ /**
5
+ * How long the state stays valid, in milliseconds. Default 10 minutes.
6
+ * Only {@link readState} reads this — the expiry is checked against the
7
+ * timestamp embedded at signing time, not chosen when signing.
8
+ */
9
+ ttlMs?: number;
10
+ }
11
+ /**
12
+ * Signs a stateless CSRF token for an OAuth "connect" redirect — the
13
+ * `state` parameter — with no server-side session and no cookie.
14
+ *
15
+ * The alternative, a cookie set right before the redirect, breaks the
16
+ * moment the provider's consent screen adds a step of its own: a phone
17
+ * verification, an app confirmation, a "this login looks unusual" prompt.
18
+ * By the time the browser comes back minutes later, the cookie can be gone
19
+ * — a third-party-cookie rule, a different tab, a session that timed out —
20
+ * and a completely legitimate connection fails with something that reads
21
+ * like "try again", for a reason that has nothing to do with the
22
+ * connection itself. Signing the payload and carrying it *in* the `state`
23
+ * parameter needs nothing kept server-side, so there is nothing to expire
24
+ * early.
25
+ *
26
+ * The payload is free-form (`Record<string, string>`) on purpose: what
27
+ * belongs in it — a tenant id, an account id, the resource being connected
28
+ * — is specific to what you are connecting, not something this function
29
+ * should know about.
30
+ */
31
+ export declare function signState(payload: Record<string, string>, options: SignedStateOptions): string;
32
+ /**
33
+ * Verifies a `state` produced by {@link signState} and returns the payload
34
+ * it was signed with — or `null` if it is missing, was not signed with this
35
+ * `secret`, or is older than `ttlMs`.
36
+ *
37
+ * Never throws on a bad state: an OAuth callback runs with no session and
38
+ * no signed-in user to explain the failure to, so it always has to fall
39
+ * back to "start over" rather than surface an error — a caller checks for
40
+ * `null` and does exactly that.
41
+ */
42
+ export declare function readState<T extends Record<string, string> = Record<string, string>>(state: string | undefined, options: SignedStateOptions): T | null;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@miguelmorales13/nestkit",
3
- "version": "0.22.0",
3
+ "version": "0.25.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip), and CFDI 4.0 invoicing (Facturapi, Facturama).",
7
+ "description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth and Store/POS provisioning, signed stateless OAuth connect state, and CFDI 4.0 invoicing (Facturapi, Facturama).",
8
8
  "publishConfig": {
9
9
  "access": "public"
10
10
  },
@@ -91,6 +91,11 @@
91
91
  "import": "./dist/auth/oauth/index.js",
92
92
  "require": "./dist/auth/oauth/index.cjs"
93
93
  },
94
+ "./push": {
95
+ "types": "./dist/push/index.d.ts",
96
+ "import": "./dist/push/index.js",
97
+ "require": "./dist/push/index.cjs"
98
+ },
94
99
  "./email": {
95
100
  "types": "./dist/email/index.d.ts",
96
101
  "import": "./dist/email/index.js",
@@ -151,6 +156,11 @@
151
156
  "import": "./dist/payments/index.js",
152
157
  "require": "./dist/payments/index.cjs"
153
158
  },
159
+ "./security": {
160
+ "types": "./dist/security/index.d.ts",
161
+ "import": "./dist/security/index.js",
162
+ "require": "./dist/security/index.cjs"
163
+ },
154
164
  "./invoicing": {
155
165
  "types": "./dist/invoicing/index.d.ts",
156
166
  "import": "./dist/invoicing/index.js",
@@ -192,9 +202,13 @@
192
202
  "@aws-sdk/s3-request-presigner": "^3.0.0",
193
203
  "@aws-sdk/lib-storage": "^3.0.0",
194
204
  "typeorm": "^0.3.0 || ^1.0.0",
195
- "@simplewebauthn/server": "^13.0.0 || ^14.0.0"
205
+ "@simplewebauthn/server": "^13.0.0 || ^14.0.0",
206
+ "web-push": "^3.6.0"
196
207
  },
197
208
  "peerDependenciesMeta": {
209
+ "web-push": {
210
+ "optional": true
211
+ },
198
212
  "@simplewebauthn/server": {
199
213
  "optional": true
200
214
  },
@@ -260,6 +274,7 @@
260
274
  "@types/node": "^22.0.0",
261
275
  "@types/nodemailer": "^8.0.1",
262
276
  "@types/pg": "^8.11.0",
277
+ "@types/web-push": "^3.6.4",
263
278
  "bcryptjs": "^3.0.3",
264
279
  "class-transformer": "^0.5.1",
265
280
  "class-validator": "^0.15.1",
@@ -274,7 +289,8 @@
274
289
  "telegraf": "^4.16.3",
275
290
  "tsup": "^8.5.1",
276
291
  "typeorm": "^1.1.0",
277
- "typescript": "^5.6.0"
292
+ "typescript": "^5.6.0",
293
+ "web-push": "^3.6.7"
278
294
  },
279
295
  "overrides": {
280
296
  "@nestjs/common": "11.2.3",