@axiumine/koa-utils 5.7.2 → 5.9.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.
@@ -15,7 +15,28 @@ export declare class SocketLabsLib {
15
15
  private readonly emailHtmlHeader2;
16
16
  private readonly emailHtmlFooter;
17
17
  constructor(htmlHeader1?: string, htmlHeader2?: string, htmlFooter?: string);
18
- sendEmailVerify(emailTo: string, hash: string, name?: string): Promise<boolean>;
18
+ /**
19
+ * Send the registration confirmation link.
20
+ *
21
+ * `linkBase` and `linkPath` exist because one process can serve more than one audience, and the
22
+ * confirmation link of each has to land on that audience's own domain. Every other link in this class
23
+ * is built from `APP_DOMAIN`, read once in the constructor — which is correct while a deployment has a
24
+ * single front-end, and wrong the moment a second one (a customer site next to an operator panel, say)
25
+ * registers accounts through the same backend. Passing the base per call is the smallest thing that
26
+ * makes both links expressible without a second process or a second env read.
27
+ *
28
+ * Both are optional and default to the previous behaviour exactly, so every existing caller is
29
+ * unaffected. A trailing slash on `linkBase` and a missing leading slash on `linkPath` are both
30
+ * normalised away — the caller supplies configuration, not a pre-joined URL, and `//check` or
31
+ * `example.comcheck` are not errors worth surfacing to a user mid-registration.
32
+ *
33
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
34
+ * @param hash the activation hash stored on the account
35
+ * @param name optional greeting name
36
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
37
+ * @param linkPath route prefix the receiving service listens on; defaults to `/check/verify-email`
38
+ */
39
+ sendEmailVerify(emailTo: string, hash: string, name?: string, linkBase?: string, linkPath?: string): Promise<boolean>;
19
40
  /**
20
41
  * @fixme handle retry
21
42
  *
@@ -79,7 +100,26 @@ SendResponse {
79
100
  sendSubscriptionEmail(emailTo: string, otp: string): Promise<boolean>;
80
101
  sendResetPwdConfirmation(emailTo: string, name?: string): Promise<boolean>;
81
102
  sendResetPwdConfirmationHash(email: string, name: string, hash: string): Promise<boolean>;
82
- sendEmailReset(emailTo: string, hash: string, name?: string): Promise<boolean>;
103
+ /**
104
+ * Send the password-reset link.
105
+ *
106
+ * `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
107
+ * pair has to move together: an account that confirmed its address on one front end and then resets its
108
+ * password on another has been sent to two different sites for two halves of the same login. Both
109
+ * default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
110
+ *
111
+ * ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
112
+ * router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
113
+ * therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
114
+ * router rather than with a service mount point.
115
+ *
116
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
117
+ * @param hash the reset hash stored on the account
118
+ * @param name optional greeting name
119
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
120
+ * @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
121
+ */
122
+ sendEmailReset(emailTo: string, hash: string, name?: string, linkBase?: string, linkPath?: string): Promise<boolean>;
83
123
  /**
84
124
  * fallback footer
85
125
  */
@@ -26,11 +26,34 @@ export class SocketLabsLib {
26
26
  this.emailHtmlHeader2 = htmlHeader2 === '' ? this.htmlHeader2() : htmlHeader2;
27
27
  this.emailHtmlFooter = htmlFooter === '' ? this.htmlFooter() : htmlFooter;
28
28
  }
29
- async sendEmailVerify(emailTo, hash, name = '') {
29
+ /**
30
+ * Send the registration confirmation link.
31
+ *
32
+ * `linkBase` and `linkPath` exist because one process can serve more than one audience, and the
33
+ * confirmation link of each has to land on that audience's own domain. Every other link in this class
34
+ * is built from `APP_DOMAIN`, read once in the constructor — which is correct while a deployment has a
35
+ * single front-end, and wrong the moment a second one (a customer site next to an operator panel, say)
36
+ * registers accounts through the same backend. Passing the base per call is the smallest thing that
37
+ * makes both links expressible without a second process or a second env read.
38
+ *
39
+ * Both are optional and default to the previous behaviour exactly, so every existing caller is
40
+ * unaffected. A trailing slash on `linkBase` and a missing leading slash on `linkPath` are both
41
+ * normalised away — the caller supplies configuration, not a pre-joined URL, and `//check` or
42
+ * `example.comcheck` are not errors worth surfacing to a user mid-registration.
43
+ *
44
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
45
+ * @param hash the activation hash stored on the account
46
+ * @param name optional greeting name
47
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
48
+ * @param linkPath route prefix the receiving service listens on; defaults to `/check/verify-email`
49
+ */
50
+ async sendEmailVerify(emailTo, hash, name = '', linkBase = this.linkBase, linkPath = '/check/verify-email') {
30
51
  const subject = `Confirm your email for ${this.platformName}`;
31
52
  const encodedEmail = encodeURI(emailTo);
32
53
  const nameFixed = this.fixName(name);
33
- const link = `${this.linkBase}/check/verify-email/${encodedEmail}/${hash}`;
54
+ const base = linkBase.replace(/\/+$/, '');
55
+ const path = `/${linkPath.replace(/^\/+/, '')}`;
56
+ const link = `${base}${path}/${encodedEmail}/${hash}`;
34
57
  const linkHtml = this.StringObj.makeLink(link);
35
58
  const messageTxt = `Hi${nameFixed}, you or someone else is registering you on ${this.platformName}.
36
59
  You can confirm the registration by copying this link ${link} into your browser. The Team`;
@@ -233,10 +256,31 @@ SendResponse {
233
256
  return false;
234
257
  }
235
258
  }
236
- async sendEmailReset(emailTo, hash, name = '') {
259
+ /**
260
+ * Send the password-reset link.
261
+ *
262
+ * `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
263
+ * pair has to move together: an account that confirmed its address on one front end and then resets its
264
+ * password on another has been sent to two different sites for two halves of the same login. Both
265
+ * default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
266
+ *
267
+ * ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
268
+ * router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
269
+ * therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
270
+ * router rather than with a service mount point.
271
+ *
272
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
273
+ * @param hash the reset hash stored on the account
274
+ * @param name optional greeting name
275
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
276
+ * @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
277
+ */
278
+ async sendEmailReset(emailTo, hash, name = '', linkBase = this.linkBase, linkPath = '/x/reset') {
237
279
  const subject = `Password reset for ${this.platformName}`;
238
280
  const encodedEmail = encodeURI(emailTo);
239
- const link = `${this.linkBase}/x/reset/${encodedEmail}/${hash}`;
281
+ const base = linkBase.replace(/\/+$/, '');
282
+ const path = `/${linkPath.replace(/^\/+/, '')}`;
283
+ const link = `${base}${path}/${encodedEmail}/${hash}`;
240
284
  const linkHtml = this.StringObj.makeLink(link);
241
285
  const nameFixed = this.fixName(name);
242
286
  const textBody = `Hi${nameFixed}, you or someone else has requested a password reset. You can change your login password
@@ -1,3 +1,4 @@
1
+ import { IResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
1
2
  import { TGetResetPwd } from "../../../private/lib/access/db/getResetPwd.mjs";
2
3
  import { TSaveResetReq } from "../../../private/lib/access/db/saveResetReq.mjs";
3
4
  import { GraphQLNonNull } from 'graphql';
@@ -9,6 +10,15 @@ export interface IResetPwdArgs {
9
10
  export interface IResetPwdDeps {
10
11
  getResetPwd: TGetResetPwd;
11
12
  saveResetReq: TSaveResetReq;
13
+ /**
14
+ * Where the link in the mail points. Defaults to SocketLabs on `APP_DOMAIN`, which is what every
15
+ * caller got before the option existed.
16
+ *
17
+ * Supply one when the process serves more than one front end: the link host is the only part of this
18
+ * flow that differs per audience, and it is not derivable from the account — two collections behind
19
+ * one service look identical by the time the resolver has an email and a hash.
20
+ */
21
+ mailer?: IResetPwdMailer;
12
22
  }
13
23
  /**
14
24
  * Take the user email and send an email with a link for change the password.
@@ -1,4 +1,4 @@
1
- import { SocketLabsLib } from "../../../email/SocketLabsLib.mjs";
1
+ import { socketLabsResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
2
2
  import { checkEmailLen } from "../../../lib/checkEmailLen.mjs";
3
3
  import { EMAIL_HASH_LEN } from "../../../lib/Constants.mjs";
4
4
  import { DateLib } from "../../../lib/DateLib.mjs";
@@ -93,8 +93,8 @@ export const createResetPwdMutation = (deps) => ({
93
93
  const mail = pendingMail;
94
94
  if (mail !== null) {
95
95
  try {
96
- const SocketLabsObj = new SocketLabsLib();
97
- void SocketLabsObj.sendEmailReset(uEmail, mail.hash, mail.name).catch((e) => {
96
+ const mailer = deps.mailer ?? socketLabsResetPwdMailer;
97
+ void mailer.sendEmailReset(uEmail, mail.hash, mail.name).catch((e) => {
98
98
  Sentry.captureException(e);
99
99
  });
100
100
  }
@@ -1,10 +1,19 @@
1
1
  import { TResetPwdMutation } from '../../graphQL/schema/mutations/resetPwd.mjs';
2
2
  import { TUpdatePasswordMutation } from '../../graphQL/schema/mutations/updatePassword.mjs';
3
3
  import { IResetPwdPaths, TAccessModel } from './accessPaths.mjs';
4
+ import { IResetPwdMailer } from './resetPwdMailer.mjs';
4
5
  /** What the factory needs: the account model, plus any path that differs from the default layout. */
5
6
  export interface ICreateResetPwdFlowArgs {
6
7
  model: TAccessModel;
7
8
  paths?: Partial<IResetPwdPaths>;
9
+ /**
10
+ * Where the reset link points. Omitted means SocketLabs on `APP_DOMAIN`, the pre-existing behaviour.
11
+ *
12
+ * Pass `createResetPwdMailer(base, path)` when this process mails more than one audience. The flow is
13
+ * already per-model, so per-flow is the right granularity: the collection and the front end that
14
+ * serves it are the same choice made twice.
15
+ */
16
+ mailer?: IResetPwdMailer;
8
17
  }
9
18
  /** The two mutations the flow exposes, bound to the model and paths passed in. */
10
19
  export interface IResetPwdFlow {
@@ -30,4 +39,4 @@ export interface IResetPwdFlow {
30
39
  * })
31
40
  * ```
32
41
  */
33
- export declare const createResetPwdFlow: ({ model, paths }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
42
+ export declare const createResetPwdFlow: ({ model, paths, mailer }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
@@ -24,13 +24,14 @@ import { resolveResetPwdPaths } from './accessPaths.mjs';
24
24
  * })
25
25
  * ```
26
26
  */
27
- export const createResetPwdFlow = ({ model, paths }) => {
27
+ export const createResetPwdFlow = ({ model, paths, mailer }) => {
28
28
  const resolved = resolveResetPwdPaths(paths);
29
29
  const getResetPwd = createGetResetPwd(model, resolved);
30
30
  return {
31
31
  resetPwd: createResetPwdMutation({
32
32
  getResetPwd,
33
- saveResetReq: createSaveResetReq(model, resolved)
33
+ saveResetReq: createSaveResetReq(model, resolved),
34
+ mailer
34
35
  }),
35
36
  updatePassword: createUpdatePasswordMutation({
36
37
  getResetPwd,
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The one message the password-reset flow sends.
3
+ *
4
+ * A single-method interface looks like ceremony next to `IVerifyEmailMailer`'s six, and it is not: the
5
+ * link in this mail carries the host the account will finish the reset on, and the resolver that sends it
6
+ * has no way to know which of a deployment's front ends the account belongs to. Before this seam existed
7
+ * the host was `APP_DOMAIN`, read once in `SocketLabsLib`'s constructor — correct for a deployment with
8
+ * one front end, and wrong the moment a second one authenticates against the same backend, because every
9
+ * customer's reset link then points at the operator panel.
10
+ *
11
+ * Structural, so `SocketLabsLib` itself satisfies it and a test double is an object literal.
12
+ *
13
+ * The resolver never awaits the returned promise (see `createResetPwdMutation` for the three reasons), so
14
+ * an implementation must not rely on its rejection being observed by the caller — the flow attaches its
15
+ * own Sentry handler and nothing else looks at it.
16
+ */
17
+ export interface IResetPwdMailer {
18
+ sendEmailReset(email: string, hash: string, name: string): Promise<unknown>;
19
+ }
20
+ /**
21
+ * SocketLabs on the default link base, constructed per call.
22
+ *
23
+ * Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
24
+ * constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
25
+ * the module was first resolved.
26
+ */
27
+ export declare const socketLabsResetPwdMailer: IResetPwdMailer;
28
+ /**
29
+ * A mailer that points the reset link at a specific host and route.
30
+ *
31
+ * The intended use is one call per audience: a deployment serving a customer site next to an operator
32
+ * panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
33
+ * arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
34
+ * on the base and a missing leading slash on the path.
35
+ *
36
+ * Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
37
+ * arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
38
+ * Normalising that to `''` at the call site instead would produce a link with no host at all.
39
+ *
40
+ * ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
41
+ * the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
42
+ * loudly if it does not, because the link is only ever followed by a person.
43
+ */
44
+ export declare const createResetPwdMailer: (linkBase?: string, linkPath?: string) => IResetPwdMailer;
@@ -0,0 +1,31 @@
1
+ import { SocketLabsLib } from "../../email/SocketLabsLib.mjs";
2
+ /**
3
+ * SocketLabs on the default link base, constructed per call.
4
+ *
5
+ * Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
6
+ * constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
7
+ * the module was first resolved.
8
+ */
9
+ export const socketLabsResetPwdMailer = {
10
+ sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name)
11
+ };
12
+ /**
13
+ * A mailer that points the reset link at a specific host and route.
14
+ *
15
+ * The intended use is one call per audience: a deployment serving a customer site next to an operator
16
+ * panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
17
+ * arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
18
+ * on the base and a missing leading slash on the path.
19
+ *
20
+ * Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
21
+ * arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
22
+ * Normalising that to `''` at the call site instead would produce a link with no host at all.
23
+ *
24
+ * ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
25
+ * the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
26
+ * loudly if it does not, because the link is only ever followed by a person.
27
+ */
28
+ export const createResetPwdMailer = (linkBase, linkPath) => ({
29
+ sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name, linkBase, linkPath)
30
+ });
31
+ //# sourceMappingURL=resetPwdMailer.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiumine/koa-utils",
3
- "version": "5.7.2",
3
+ "version": "5.9.0",
4
4
  "description": "Koa Utils",
5
5
  "type": "module",
6
6
  "repository": {
@@ -27,7 +27,7 @@
27
27
  "test": "yarn build && yarn build:tests && mocha",
28
28
  "test:watch": "mocha --watch",
29
29
  "test:coverage": "yarn build && yarn build:tests && c8 mocha",
30
- "qodana": "yarn test:coverage && mkdir -p .qodana/results && docker run --rm --env-file .env -v \"$PWD\":/data/project -v \"$PWD\"/.qodana/results:/data/results jetbrains/qodana-js:2026.1 --show-report",
30
+ "qodana": "yarn test:coverage && mkdir -p .qodana/results && docker run --rm --env-file .env -v \"$PWD\":/data/project -v \"$PWD\"/.qodana/results:/data/results jetbrains/qodana-js:2026.2 --show-report",
31
31
  "qodana:cli": "yarn test:coverage && set -a && . ./.env && set +a && qodana scan --show-report",
32
32
  "reindex": "node .gitnexus/run.cjs analyze --no-stats",
33
33
  "semgrep": "./scripts/semgrep.sh",
@@ -72,6 +72,10 @@
72
72
  "import": "./dist/lib/access/verifyEmailMailer.mjs",
73
73
  "types": "./dist/lib/access/verifyEmailMailer.d.mts"
74
74
  },
75
+ "./lib/access/resetPwdMailer": {
76
+ "import": "./dist/lib/access/resetPwdMailer.mjs",
77
+ "types": "./dist/lib/access/resetPwdMailer.d.mts"
78
+ },
75
79
  "./lib/MongoDB/IOnboarding": {
76
80
  "import": "./dist/lib/MongoDB/IOnboarding.mjs",
77
81
  "types": "./dist/lib/MongoDB/IOnboarding.d.mts"
@@ -711,60 +715,53 @@
711
715
  }
712
716
  },
713
717
  "devDependencies": {
714
- "@axiumine/eslint-config-be": "^2.0.1",
715
- "@eslint/eslintrc": "^3.3.5",
718
+ "@axiumine/eslint-config-be": "^2.0.2",
719
+ "@eslint/eslintrc": "^3.3.6",
716
720
  "@eslint/js": "^10.0.1",
717
- "@node-rs/bcrypt": "^1.10.4",
718
- "@sentry/node": "^10.42.0",
721
+ "@node-rs/bcrypt": "^1.10.7",
722
+ "@sentry/node": "^10.69.0",
719
723
  "@socketlabs/email": "^1.4.4",
720
- "@types/bcrypt": "^6.0.0",
721
724
  "@types/chai": "^5.2.3",
722
- "@types/clamscan": "^2.4.0",
725
+ "@types/clamscan": "^2.4.1",
723
726
  "@types/fs-extra": "^11.0.4",
724
- "@types/koa": "^3.0.1",
727
+ "@types/koa": "^3.0.3",
725
728
  "@types/mocha": "^10.0.10",
726
- "@types/node": "^25.4.0",
727
- "@types/pg": "^8.18.0",
729
+ "@types/node": "^26.1.2",
730
+ "@types/pg": "^8.20.3",
728
731
  "@types/sinon": "^21.0.1",
729
- "@types/uuid": "^11.0.0",
730
- "@typescript-eslint/eslint-plugin": "^8.57.0",
731
- "@typescript-eslint/parser": "^8.57.0",
732
+ "@typescript-eslint/eslint-plugin": "^8.65.0",
733
+ "@typescript-eslint/parser": "^8.65.0",
732
734
  "c8": "^11.0.0",
733
735
  "chai": "^6.2.2",
734
736
  "clamscan": "^2.4.0",
735
- "dotenv": "^17.2.3",
736
- "eslint": "^10.4.0",
737
+ "dotenv": "^17.4.2",
738
+ "eslint": "^10.8.0",
737
739
  "eslint-config-prettier": "^10.1.8",
738
- "eslint-config-standard": "^17.1.0",
739
- "eslint-import-resolver-typescript": "^4.4.4",
740
- "eslint-plugin-import": "^2.29.1",
741
- "eslint-plugin-jest": "^29.12.1",
742
- "eslint-plugin-n": "^18.0.1",
743
- "eslint-plugin-promise": "^7.2.1",
740
+ "eslint-import-resolver-typescript": "^4.4.5",
741
+ "eslint-plugin-n": "^18.2.2",
742
+ "eslint-plugin-promise": "^7.3.0",
744
743
  "eslint-plugin-simple-import-sort": "^13.0.0",
745
744
  "file-type": "^22.0.1",
746
- "fs-extra": "^11.3.4",
747
- "graphql": "^16.13.1",
748
- "koa": "^3.1.1",
749
- "lint-staged": "^17.0.5",
750
- "mariadb": "^3.5.2",
745
+ "fs-extra": "^11.4.0",
746
+ "graphql": "^16.14.2",
747
+ "koa": "^3.2.1",
748
+ "mariadb": "^3.5.3",
751
749
  "mocha": "^11.7.6",
752
- "module-alias": "^2.2.3",
753
- "mongodb-memory-server": "^11.1.0",
754
- "mongoose": "^9.2.4",
755
- "pg": "^8.20.0",
756
- "prettier": "^3.8.1",
757
- "redis": "^5.10.0",
750
+ "mongodb-memory-server": "^11.2.0",
751
+ "mongoose": "^9.9.1",
752
+ "pg": "^8.22.0",
753
+ "prettier": "^3.9.6",
754
+ "redis": "^6.2.0",
758
755
  "reflect-metadata": "^0.2.2",
759
756
  "sequelize": "^6.37.8",
760
757
  "sequelize-typescript": "^2.1.6",
761
- "sharp": "^0.34.5",
762
- "sinon": "^22.0.0",
758
+ "sharp": "^0.35.3",
759
+ "sinon": "^22.1.0",
763
760
  "ts-node": "^10.9.2",
764
761
  "ts-patch": "^4.0.1",
765
- "tsx": "^4.22.3",
762
+ "tsx": "^4.23.1",
766
763
  "typescript": "^6.0.3",
767
764
  "typescript-transform-paths": "^4.0.0",
768
- "uuid": "^14.0.0"
765
+ "uuid": "^14.0.1"
769
766
  }
770
767
  }