@axiumine/koa-utils 7.2.0 → 8.0.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.
Files changed (64) hide show
  1. package/README.md +38 -13
  2. package/dist/dataSources/MariaDB.mjs +3 -3
  3. package/dist/dataSources/MongoDB.mjs +2 -2
  4. package/dist/dataSources/PostgreSQL.mjs +3 -3
  5. package/dist/dataSources/Redis.mjs +3 -3
  6. package/dist/email/ISocketLabsClient.d.mts +8 -0
  7. package/dist/email/ISocketLabsClient.mjs +2 -0
  8. package/dist/email/ISocketLabsMessage.d.mts +20 -0
  9. package/dist/email/ISocketLabsMessage.mjs +4 -0
  10. package/dist/email/ISocketLabsSendResult.d.mts +12 -0
  11. package/dist/email/ISocketLabsSendResult.mjs +2 -0
  12. package/dist/email/ISocketLabsSenderOptions.d.mts +30 -0
  13. package/dist/email/ISocketLabsSenderOptions.mjs +3 -0
  14. package/dist/email/SocketLabsLib.d.mts +1 -0
  15. package/dist/email/SocketLabsLib.mjs +30 -21
  16. package/dist/email/createSocketLabsSender.d.mts +20 -0
  17. package/dist/email/createSocketLabsSender.mjs +66 -0
  18. package/dist/files/scanVirus.mjs +1 -1
  19. package/dist/files/storeUploadAsTemp.mjs +2 -2
  20. package/dist/graphQL/schema/mutations/login4Ever.mjs +1 -1
  21. package/dist/graphQL/schema/mutations/loginAdmin.mjs +1 -1
  22. package/dist/graphQL/schema/mutations/loginRememberme.mjs +1 -1
  23. package/dist/graphQL/schema/mutations/logout.mjs +3 -3
  24. package/dist/graphQL/schema/mutations/refresh.mjs +3 -3
  25. package/dist/graphQL/schema/mutations/resetPwd.mjs +1 -1
  26. package/dist/graphQL/schema/mutations/updatePassword.mjs +1 -1
  27. package/dist/koa/IKoaErrorHandlerContext.d.mts +11 -0
  28. package/dist/koa/IKoaErrorHandlerContext.mjs +2 -0
  29. package/dist/koa/IKoaErrorHandlerOptions.d.mts +28 -0
  30. package/dist/koa/IKoaErrorHandlerOptions.mjs +3 -0
  31. package/dist/koa/createKoaErrorHandler.d.mts +11 -0
  32. package/dist/koa/createKoaErrorHandler.mjs +37 -0
  33. package/dist/koa/middleware/authenticatedAuthorizationHandler/index.mjs +2 -2
  34. package/dist/koa/middleware/authenticatedLogoutHandler/index.mjs +2 -2
  35. package/dist/koa/middleware/authenticatedResourceHandler/index.mjs +2 -2
  36. package/dist/koa/tdwKoaErrorHandler.mjs +15 -23
  37. package/dist/lib/Constants.d.mts +0 -2
  38. package/dist/lib/Constants.mjs +0 -2
  39. package/dist/lib/MongoDB/throwMongoErrors.mjs +1 -1
  40. package/dist/lib/OBJECTID_0_OBJ.d.mts +6 -0
  41. package/dist/lib/OBJECTID_0_OBJ.mjs +8 -0
  42. package/dist/lib/tokenOptions.mjs +2 -2
  43. package/dist/lib/tokens.mjs +3 -2
  44. package/dist/lib/tryCatchRethrow.mjs +1 -1
  45. package/dist/private/email/orFalse.mjs +1 -1
  46. package/dist/private/files/reEncode.mjs +1 -1
  47. package/dist/private/graphQL/schema/mutations/setLastLoginSQL.mjs +1 -1
  48. package/dist/private/graphQL/schema/mutations/setRedisLoginSession.mjs +3 -3
  49. package/dist/private/lib/ISentryClient.d.mts +10 -0
  50. package/dist/private/lib/ISentryClient.mjs +2 -0
  51. package/dist/private/lib/access/assertResetAllowed.mjs +1 -1
  52. package/dist/private/lib/access/db/deleteUserByEmail.mjs +1 -1
  53. package/dist/private/lib/access/db/enableEmailAccess.mjs +1 -1
  54. package/dist/private/lib/access/db/userData4VerifyEmail.mjs +1 -1
  55. package/dist/private/lib/access/handleBadDB.mjs +1 -1
  56. package/dist/private/lib/createSentryClient.d.mts +7 -0
  57. package/dist/private/lib/createSentryClient.mjs +20 -0
  58. package/dist/private/lib/importOptional.d.mts +7 -0
  59. package/dist/private/lib/importOptional.mjs +27 -0
  60. package/dist/private/lib/loadEnv.d.mts +12 -0
  61. package/dist/private/lib/loadEnv.mjs +14 -0
  62. package/dist/private/lib/sentry.d.mts +3 -0
  63. package/dist/private/lib/sentry.mjs +12 -0
  64. package/package.json +63 -7
@@ -0,0 +1,37 @@
1
+ /** Statuses that must not carry a body (RFC 9110). Copied into a `Set` per handler, never mutated. */
2
+ const NO_BODY_STATUSES = [100, 101, 102, 204, 205, 304];
3
+ // `||`, not `??`: status 0 and NaN must give 500 too
4
+ const defaultStatusOf = (err) => err?.status || 500;
5
+ const defaultBodyOf = (err) => ({ message: err?.message });
6
+ /**
7
+ * Builds a Koa error middleware: `await next()` in a `try`, and on a throw sets `ctx.status`, sets `ctx.body`, calls
8
+ * `report`, then emits `'error'` on the app, all in one synchronous `catch` block so `emit` listeners see the final
9
+ * context. It never rethrows what `next()` threw.
10
+ *
11
+ * Importing it loads no environment file, no Sentry, no `graphql` and no `koa`: it has no peer dependency.
12
+ * `tdwKoaErrorHandler` is this factory configured for GraphQL errors, Sentry and dotenv.
13
+ */
14
+ export function createKoaErrorHandler(options) {
15
+ const statusOf = options?.statusOf ?? defaultStatusOf;
16
+ const bodyOf = options?.bodyOf ?? defaultBodyOf;
17
+ const report = options?.report;
18
+ const noBodyStatuses = new Set(options?.noBodyStatuses ?? NO_BODY_STATUSES);
19
+ return async function koaErrorHandler(ctx, next) {
20
+ try {
21
+ await next();
22
+ }
23
+ catch (err) {
24
+ ctx.status = statusOf(err, ctx);
25
+ // Read back, not the value just assigned: Koa's setter can store a different number (it drops the body of 204/205/304)
26
+ if (!noBodyStatuses.has(ctx.status)) {
27
+ const body = bodyOf(err, ctx);
28
+ if (body !== undefined) {
29
+ ctx.body = body;
30
+ }
31
+ }
32
+ report?.(err, ctx);
33
+ ctx.app.emit('error', err, ctx);
34
+ }
35
+ };
36
+ }
37
+ //# sourceMappingURL=createKoaErrorHandler.mjs.map
@@ -1,12 +1,12 @@
1
1
  import { redisClient } from "../../../dataSources/Redis.mjs";
2
2
  import { isSessionBlocked } from "../../../lib/isSessionBlocked.mjs";
3
+ import { loadEnv } from "../../../private/lib/loadEnv.mjs";
3
4
  import { verifyIntrospectionCode } from "../../../private/lib/verifyIntrospectionCode.mjs";
4
5
  import { throwForbiddenError } from "../../../graphQL/throw/throwForbiddenError.mjs";
5
6
  import { throwRefreshTokenExpiredOrDeleted } from "../../../graphQL/throw/throwRefreshTokenExpiredOrDeleted.mjs";
6
- import * as dotenv from 'dotenv';
7
7
  import { Types } from 'mongoose';
8
8
  import { verifySignedRefreshToken } from './verifySignedRefreshToken.mjs';
9
- dotenv.config();
9
+ loadEnv();
10
10
  export const authenticatedAuthorizationHandler = (keys) => async (ctx, next) => {
11
11
  /***************************
12
12
  * CLIENT:
@@ -1,12 +1,12 @@
1
1
  import { redisClient } from "../../../dataSources/Redis.mjs";
2
2
  import { isValidUuidV4 } from "../../../lib/isValidUuidV4.mjs";
3
+ import { loadEnv } from "../../../private/lib/loadEnv.mjs";
3
4
  import { verifyIntrospectionCode } from "../../../private/lib/verifyIntrospectionCode.mjs";
4
5
  import { throwAlreadyDone } from "../../../graphQL/throw/throwAlreadyDone.mjs";
5
6
  import { throwPreconditionFailedNoAuthCookie } from "../../../graphQL/throw/throwPreconditionFailedNoAuthCookie.mjs";
6
7
  import { throwPreconditionFailedNoAuthHeader } from "../../../graphQL/throw/throwPreconditionFailedNoAuthHeader.mjs";
7
- import * as dotenv from 'dotenv';
8
8
  import { verifySignedRefreshToken } from '../authenticatedAuthorizationHandler/verifySignedRefreshToken.mjs';
9
- dotenv.config();
9
+ loadEnv();
10
10
  const requireIntrospectionOrThrow = (header, throwOnFail) => {
11
11
  if (typeof header !== 'undefined' && verifyIntrospectionCode(header['x-introspectioncode'])) {
12
12
  return true;
@@ -1,15 +1,15 @@
1
1
  import { redisClient } from "../../../dataSources/Redis.mjs";
2
2
  import { isSessionBlocked } from "../../../lib/isSessionBlocked.mjs";
3
3
  import { isValidUuidV4 } from "../../../lib/isValidUuidV4.mjs";
4
+ import { loadEnv } from "../../../private/lib/loadEnv.mjs";
4
5
  import { verifyIntrospectionCode } from "../../../private/lib/verifyIntrospectionCode.mjs";
5
6
  import { throwAccessTokenExpiredOrDeleted } from "../../../graphQL/throw/throwAccessTokenExpiredOrDeleted.mjs";
6
7
  import { throwAccessTokenRequired } from "../../../graphQL/throw/throwAccessTokenRequired.mjs";
7
8
  import { throwForbiddenError } from "../../../graphQL/throw/throwForbiddenError.mjs";
8
9
  import { throwMissingMalformedInvalidToken } from "../../../graphQL/throw/throwMissingMalformedInvalidToken.mjs";
9
10
  import { throwPreconditionFailedNoAuthHeader } from "../../../graphQL/throw/throwPreconditionFailedNoAuthHeader.mjs";
10
- import * as dotenv from 'dotenv';
11
11
  import { Types } from 'mongoose';
12
- dotenv.config();
12
+ loadEnv();
13
13
  /**
14
14
  * Find out access token in Bearer header
15
15
  */
@@ -1,7 +1,8 @@
1
- import * as Sentry from '@sentry/node';
2
- import * as dotenv from 'dotenv';
1
+ import { loadEnv } from "../private/lib/loadEnv.mjs";
2
+ import * as Sentry from "../private/lib/sentry.mjs";
3
3
  import { GraphQLError } from 'graphql';
4
- dotenv.config();
4
+ import { createKoaErrorHandler } from './createKoaErrorHandler.mjs';
5
+ loadEnv();
5
6
  // check status
6
7
  function resolveErrorStatus(errKoa, isIstanceOfGQL) {
7
8
  if (isIstanceOfGQL) {
@@ -10,13 +11,9 @@ function resolveErrorStatus(errKoa, isIstanceOfGQL) {
10
11
  return errKoa.status || 500;
11
12
  }
12
13
  /**
13
- * do not set body for status codes that do not allow it
14
+ * the factory skips this for status codes that do not allow a body
14
15
  */
15
- function buildErrorBody(errKoa, isIstanceOfGQL, status) {
16
- const allowBody = ![100, 101, 102, 204, 205, 304].includes(status);
17
- if (!allowBody) {
18
- return undefined;
19
- }
16
+ function buildErrorBody(errKoa, isIstanceOfGQL) {
20
17
  const body = {
21
18
  message: errKoa.message
22
19
  };
@@ -33,20 +30,15 @@ function maybeCaptureSentryError(errKoa, isIstanceOfGQL) {
33
30
  }
34
31
  }
35
32
  }
36
- export async function tdwKoaErrorHandler(ctx, next) {
37
- try {
38
- await next();
39
- }
40
- catch (err) {
41
- const errKoa = err;
42
- const isIstanceOfGQL = err instanceof GraphQLError;
43
- ctx.status = resolveErrorStatus(errKoa, isIstanceOfGQL);
44
- const body = buildErrorBody(errKoa, isIstanceOfGQL, ctx.status);
45
- if (body) {
46
- ctx.body = body;
47
- }
48
- maybeCaptureSentryError(errKoa, isIstanceOfGQL);
49
- ctx.app.emit('error', errKoa, ctx);
33
+ const handle = createKoaErrorHandler({
34
+ statusOf: (err) => resolveErrorStatus(err, err instanceof GraphQLError),
35
+ bodyOf: (err) => buildErrorBody(err, err instanceof GraphQLError),
36
+ report: (err) => {
37
+ maybeCaptureSentryError(err, err instanceof GraphQLError);
50
38
  }
39
+ });
40
+ // Not async: returns the factory's promise as is, so there is no extra microtask tick. Name and length (2) stay as before.
41
+ export function tdwKoaErrorHandler(ctx, next) {
42
+ return handle(ctx, next);
51
43
  }
52
44
  //# sourceMappingURL=tdwKoaErrorHandler.mjs.map
@@ -1,6 +1,4 @@
1
- import { Types } from 'mongoose';
2
1
  export declare const OBJECTID_0_STR = "000000000000000000000000";
3
- export declare const OBJECTID_0_OBJ: Types.ObjectId;
4
2
  export declare const EMAIL_MAX_LEN = 255;
5
3
  export declare const MIN_PWD_LENGTH = 10;
6
4
  export declare const MAX_PWD_LENGTH = 72;
@@ -1,6 +1,4 @@
1
- import { Types } from 'mongoose';
2
1
  export const OBJECTID_0_STR = '000000000000000000000000';
3
- export const OBJECTID_0_OBJ = new Types.ObjectId(OBJECTID_0_STR);
4
2
  export const EMAIL_MAX_LEN = 255;
5
3
  export const MIN_PWD_LENGTH = 10;
6
4
  export const MAX_PWD_LENGTH = 72;
@@ -1,5 +1,5 @@
1
1
  import { throwIfMongoErr } from "./throwIfMongoErr.mjs";
2
- import * as Sentry from '@sentry/node';
2
+ import * as Sentry from "../../private/lib/sentry.mjs";
3
3
  import { throwInternalError } from "../../graphQL/throw/throwInternalError.mjs";
4
4
  export const throwMongoDBErrors = (e) => {
5
5
  throwIfMongoErr(e);
@@ -0,0 +1,6 @@
1
+ import { Types } from 'mongoose';
2
+ /**
3
+ * The all-zero sentinel id as a Mongoose `Types.ObjectId`. It has its own subpath so that `lib/Constants` and the
4
+ * helpers that import it do not load `mongoose`.
5
+ */
6
+ export declare const OBJECTID_0_OBJ: Types.ObjectId;
@@ -0,0 +1,8 @@
1
+ import { OBJECTID_0_STR } from "./Constants.mjs";
2
+ import { Types } from 'mongoose';
3
+ /**
4
+ * The all-zero sentinel id as a Mongoose `Types.ObjectId`. It has its own subpath so that `lib/Constants` and the
5
+ * helpers that import it do not load `mongoose`.
6
+ */
7
+ export const OBJECTID_0_OBJ = new Types.ObjectId(OBJECTID_0_STR);
8
+ //# sourceMappingURL=OBJECTID_0_OBJ.mjs.map
@@ -1,5 +1,5 @@
1
- import * as dotenv from 'dotenv';
2
- dotenv.config();
1
+ import { loadEnv } from "../private/lib/loadEnv.mjs";
2
+ loadEnv();
3
3
  const baseOptions = {
4
4
  httpOnly: true,
5
5
  sameSite: 'Strict',
@@ -1,7 +1,8 @@
1
- import { v4 as uuidv4 } from 'uuid';
1
+ import { randomUUID } from 'node:crypto';
2
2
  export const REFRESH_TOKEN_EXPIRY = 90 * 24 * 60 * 60; // 90 days
3
+ // typed `string`, as uuid v4() was: randomUUID() returns a template literal type that would narrow every caller
3
4
  function generateToken() {
4
- return uuidv4();
5
+ return randomUUID();
5
6
  }
6
7
  export function generateAccessToken() {
7
8
  return generateToken();
@@ -1,4 +1,4 @@
1
- import * as Sentry from '@sentry/node';
1
+ import * as Sentry from "../private/lib/sentry.mjs";
2
2
  import { throwGraphQLError } from "../graphQL/throw/throwGraphQLError.mjs";
3
3
  import { throwInternalError } from "../graphQL/throw/throwInternalError.mjs";
4
4
  import { GraphQLError } from 'graphql';
@@ -1,4 +1,4 @@
1
- import * as Sentry from '@sentry/node';
1
+ import * as Sentry from "../lib/sentry.mjs";
2
2
  /** Run a send; a rejection or a throw becomes a Sentry report and `false`, so the caller never sees it. */
3
3
  export async function orFalse(send) {
4
4
  try {
@@ -1,4 +1,4 @@
1
- import * as Sentry from '@sentry/node';
1
+ import * as Sentry from "../lib/sentry.mjs";
2
2
  import { throwInternalError } from "../../graphQL/throw/throwInternalError.mjs";
3
3
  import { promises as fs } from 'fs';
4
4
  import sharp from 'sharp';
@@ -1,5 +1,5 @@
1
1
  import { sequelize } from "../../../../dataSources/MariaDB.mjs";
2
- import * as Sentry from '@sentry/node';
2
+ import * as Sentry from "../../../lib/sentry.mjs";
3
3
  import { QueryTypes } from 'sequelize';
4
4
  export async function setLastLoginSQL(id) {
5
5
  // Get the current time in milliseconds since the Unix Epoch
@@ -1,9 +1,9 @@
1
1
  import { redisClient } from "../../../../dataSources/Redis.mjs";
2
2
  import { REFRESH_TOKEN_EXPIRY } from "../../../../lib/tokens.mjs";
3
- import * as Sentry from '@sentry/node';
3
+ import { loadEnv } from "../../../lib/loadEnv.mjs";
4
+ import * as Sentry from "../../../lib/sentry.mjs";
4
5
  import { throwInternalError } from "../../../../graphQL/throw/throwInternalError.mjs";
5
- import * as dotenv from 'dotenv';
6
- dotenv.config();
6
+ loadEnv();
7
7
  export async function setRedisLoginSession(id, accessToken, accTokenExp, refreshToken) {
8
8
  const keyDataAccess = { id: id.toString() };
9
9
  const keyDataRefresh = { ...keyDataAccess, access: accessToken };
@@ -0,0 +1,10 @@
1
+ import type { captureException, captureMessage, Scope } from '@sentry/node';
2
+ /**
3
+ * The three Sentry calls this package makes, with the signatures of `@sentry/node`. The real module satisfies it as is
4
+ * (its `withScope` has a second overload this package never uses); the no-op client in `createSentryClient` too.
5
+ */
6
+ export interface ISentryClient {
7
+ captureException: typeof captureException;
8
+ captureMessage: typeof captureMessage;
9
+ withScope: <T>(callback: (scope: Scope) => T) => T;
10
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ISentryClient.mjs.map
@@ -1,5 +1,5 @@
1
1
  import { DateLib } from "../../../lib/DateLib.mjs";
2
- import * as Sentry from '@sentry/node';
2
+ import * as Sentry from "../sentry.mjs";
3
3
  import { throwForbiddenError } from "../../../graphQL/throw/throwForbiddenError.mjs";
4
4
  import { safeEqualSecret } from './safeEqualSecret.mjs';
5
5
  /** Minutes a reset link stays valid after the request was made. */
@@ -1,6 +1,6 @@
1
1
  import { DEFAULT_VERIFY_EMAIL_PATHS } from "../../../../lib/access/accessPaths.mjs";
2
2
  import { UserBase } from "../../../../graphQL/models/MongoDB/UserBase.mjs";
3
- import * as Sentry from '@sentry/node';
3
+ import * as Sentry from "../../sentry.mjs";
4
4
  /**
5
5
  * Hard delete of an abandoned pending registration, keyed by the login-email path.
6
6
  *
@@ -2,7 +2,7 @@ import { DEFAULT_VERIFY_EMAIL_PATHS } from "../../../../lib/access/accessPaths.m
2
2
  import { defaultVerifyEmailMailer } from "../../../../lib/access/verifyEmailMailer.mjs";
3
3
  import { UserBase } from "../../../../graphQL/models/MongoDB/UserBase.mjs";
4
4
  import { buildUnset } from "../pathTools.mjs";
5
- import * as Sentry from '@sentry/node';
5
+ import * as Sentry from "../../sentry.mjs";
6
6
  /**
7
7
  * Mark the address verified, clear the pending-verification fields, welcome the user.
8
8
  *
@@ -2,7 +2,7 @@ import { DEFAULT_VERIFY_EMAIL_PATHS } from "../../../../lib/access/accessPaths.m
2
2
  import { UserBase } from "../../../../graphQL/models/MongoDB/UserBase.mjs";
3
3
  import { EMAIL_CHECK_LINK } from "../Constants.mjs";
4
4
  import { buildProjection } from "../pathTools.mjs";
5
- import * as Sentry from '@sentry/node';
5
+ import * as Sentry from "../../sentry.mjs";
6
6
  export const createUserData4VerifyEmail = (model, paths) => async function userData4VerifyEmail(uEmail) {
7
7
  // Every field the guard chain reads must be listed. This is a .lean() read, so a field left out
8
8
  // of the projection is simply absent on the returned object, with no error — which is how a
@@ -1,4 +1,4 @@
1
- import * as Sentry from '@sentry/node';
1
+ import * as Sentry from "../sentry.mjs";
2
2
  import { EMAIL_CHECK_LINK } from './Constants.mjs';
3
3
  /**
4
4
  * A stored hash without its bookkeeping fields is our bug, not the caller's.
@@ -0,0 +1,7 @@
1
+ import type { ISentryClient } from "./ISentryClient.mjs";
2
+ /**
3
+ * The Sentry client of this package: the SDK itself when it is installed, no-ops when it is not.
4
+ *
5
+ * @param sdk the `@sentry/node` module, or `null` when it is not installed
6
+ */
7
+ export declare function createSentryClient(sdk: ISentryClient | null): ISentryClient;
@@ -0,0 +1,20 @@
1
+ // A scope whose every method is a chainable no-op, like the real setters (`scope.setTag(...).setLevel(...)`).
2
+ // `then` is left undefined: a callback that returns the scope from an async function would otherwise treat it as a
3
+ // thenable and wait forever.
4
+ const noopSetter = () => NOOP_SCOPE;
5
+ const NOOP_SCOPE = new Proxy({}, { get: (_target, property) => (property === 'then' ? undefined : noopSetter) });
6
+ // What the package reports to when `@sentry/node` is not installed: nothing. The event id is '' as no event exists.
7
+ const NOOP_CLIENT = {
8
+ captureException: () => '',
9
+ captureMessage: () => '',
10
+ withScope: (callback) => callback(NOOP_SCOPE)
11
+ };
12
+ /**
13
+ * The Sentry client of this package: the SDK itself when it is installed, no-ops when it is not.
14
+ *
15
+ * @param sdk the `@sentry/node` module, or `null` when it is not installed
16
+ */
17
+ export function createSentryClient(sdk) {
18
+ return sdk ?? NOOP_CLIENT;
19
+ }
20
+ //# sourceMappingURL=createSentryClient.mjs.map
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Load an optional peer dependency. Resolves `null` when `specifier` is not installed, rethrows every other failure.
3
+ *
4
+ * The caller passes its own `() => import('pkg')`, so the specifier stays a string literal at the call site where
5
+ * `tsc` types it and bundlers can see it. `specifier` repeats that name for the error match.
6
+ */
7
+ export declare function importOptional<T>(specifier: string, load: () => Promise<T>): Promise<T | null>;
@@ -0,0 +1,27 @@
1
+ // Node raises ERR_MODULE_NOT_FOUND for a missing ESM package; a CommonJS resolver (or a tool that wraps one) uses MODULE_NOT_FOUND.
2
+ const MISSING_CODES = new Set(['ERR_MODULE_NOT_FOUND', 'MODULE_NOT_FOUND']);
3
+ /**
4
+ * True only when `error` says that `specifier` itself is not installed. An installed package that fails to load one of
5
+ * its own dependencies raises the same code for a different name; that one must surface, so the name is matched too.
6
+ */
7
+ function isMissingPackage(error, specifier) {
8
+ const { code, message } = (error ?? {});
9
+ return MISSING_CODES.has(code) && typeof message === 'string' && message.includes(`'${specifier}'`);
10
+ }
11
+ /**
12
+ * Load an optional peer dependency. Resolves `null` when `specifier` is not installed, rethrows every other failure.
13
+ *
14
+ * The caller passes its own `() => import('pkg')`, so the specifier stays a string literal at the call site where
15
+ * `tsc` types it and bundlers can see it. `specifier` repeats that name for the error match.
16
+ */
17
+ export async function importOptional(specifier, load) {
18
+ try {
19
+ return await load();
20
+ }
21
+ catch (error) {
22
+ if (isMissingPackage(error, specifier))
23
+ return null;
24
+ throw error;
25
+ }
26
+ }
27
+ //# sourceMappingURL=importOptional.mjs.map
@@ -0,0 +1,12 @@
1
+ /** The one method of `dotenv` this package calls. */
2
+ interface IEnvLoader {
3
+ config(): unknown;
4
+ }
5
+ /**
6
+ * Load the environment file of the current directory into `process.env` when `dotenv` is installed; do nothing when
7
+ * it is not. Call it once at module scope, after the imports, where `dotenv.config()` used to run.
8
+ *
9
+ * @param loader replaces `dotenv`; `null` stands for "not installed"
10
+ */
11
+ export declare function loadEnv(loader?: IEnvLoader | null): void;
12
+ export {};
@@ -0,0 +1,14 @@
1
+ import { importOptional } from "./importOptional.mjs";
2
+ // dotenv is an optional peer. Top-level await is safe: every `exports` entry has only `import` and `types` conditions,
3
+ // so no CommonJS consumer can load this package. One ESM instance, so call sites stay synchronous.
4
+ const dotenv = await importOptional('dotenv', () => import('dotenv'));
5
+ /**
6
+ * Load the environment file of the current directory into `process.env` when `dotenv` is installed; do nothing when
7
+ * it is not. Call it once at module scope, after the imports, where `dotenv.config()` used to run.
8
+ *
9
+ * @param loader replaces `dotenv`; `null` stands for "not installed"
10
+ */
11
+ export function loadEnv(loader = dotenv) {
12
+ loader?.config();
13
+ }
14
+ //# sourceMappingURL=loadEnv.mjs.map
@@ -0,0 +1,3 @@
1
+ export declare const captureException: typeof import("@sentry/node").captureException;
2
+ export declare const captureMessage: typeof import("@sentry/node").captureMessage;
3
+ export declare const withScope: <T>(callback: (scope: import("@sentry/node").Scope) => T) => T;
@@ -0,0 +1,12 @@
1
+ import { createSentryClient } from "./createSentryClient.mjs";
2
+ import { importOptional } from "./importOptional.mjs";
3
+ // @sentry/node is an optional peer. Top-level await is safe: every `exports` entry has only `import` and `types`
4
+ // conditions, so no CommonJS consumer can load this package. One ESM instance, so call sites stay synchronous and run
5
+ // in the same order as with a static import (see `tryCatchRethrow`, `scanVirus`).
6
+ const client = createSentryClient(await importOptional('@sentry/node', () => import('@sentry/node')));
7
+ // Same names and signatures as the SDK: a module swaps `import * as Sentry from '@sentry/node'` for this file and its
8
+ // `Sentry.captureException(...)` calls stay as they are.
9
+ export const captureException = client.captureException;
10
+ export const captureMessage = client.captureMessage;
11
+ export const withScope = client.withScope;
12
+ //# sourceMappingURL=sentry.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiumine/koa-utils",
3
- "version": "7.2.0",
3
+ "version": "8.0.0",
4
4
  "description": "Koa Utils",
5
5
  "type": "module",
6
6
  "repository": {
@@ -19,7 +19,10 @@
19
19
  },
20
20
  "scripts": {
21
21
  "prepare": "yarn run hooks:install && rm -rf dist && yarn run build",
22
- "hooks:install": "git rev-parse --git-dir > /dev/null 2>&1 && git config core.hooksPath .githooks && ./scripts/lockfile-registry-filter.sh install || true",
22
+ "hooks:install": "git rev-parse --git-dir > /dev/null 2>&1 && git config core.hooksPath .githooks || true",
23
+ "registry:mirror": "node scripts/registry-rewrite.mjs mirror",
24
+ "registry:canonical": "node scripts/registry-rewrite.mjs canonical",
25
+ "registry:check": "node scripts/registry-rewrite.mjs check",
23
26
  "build": "yarn clean && yarn run build:esm",
24
27
  "prepare:all": "echo 'read REAMDE' && rm -rf dist && yarn run build:all",
25
28
  "build:all": "yarn run build:cjs && yarn run build:esm",
@@ -58,7 +61,7 @@
58
61
  "scc": "scc . --exclude-dir=node_modules,dist,.git -w --sort --directory-walker-job-workers=8 --count-as=mts:TypeScript"
59
62
  },
60
63
  "engines": {
61
- "node": "^24.14.0"
64
+ "node": "^24.14.0 || ^26.13.0"
62
65
  },
63
66
  "_moduleAliases": {
64
67
  "@lib": "dist/lib",
@@ -235,10 +238,30 @@
235
238
  "import": "./dist/dataSources/Redis.mjs",
236
239
  "types": "./dist/dataSources/Redis.d.mts"
237
240
  },
241
+ "./email/ISocketLabsClient": {
242
+ "import": "./dist/email/ISocketLabsClient.mjs",
243
+ "types": "./dist/email/ISocketLabsClient.d.mts"
244
+ },
245
+ "./email/ISocketLabsMessage": {
246
+ "import": "./dist/email/ISocketLabsMessage.mjs",
247
+ "types": "./dist/email/ISocketLabsMessage.d.mts"
248
+ },
249
+ "./email/ISocketLabsSendResult": {
250
+ "import": "./dist/email/ISocketLabsSendResult.mjs",
251
+ "types": "./dist/email/ISocketLabsSendResult.d.mts"
252
+ },
253
+ "./email/ISocketLabsSenderOptions": {
254
+ "import": "./dist/email/ISocketLabsSenderOptions.mjs",
255
+ "types": "./dist/email/ISocketLabsSenderOptions.d.mts"
256
+ },
238
257
  "./email/SocketLabsLib": {
239
258
  "import": "./dist/email/SocketLabsLib.mjs",
240
259
  "types": "./dist/email/SocketLabsLib.d.mts"
241
260
  },
261
+ "./email/createSocketLabsSender": {
262
+ "import": "./dist/email/createSocketLabsSender.mjs",
263
+ "types": "./dist/email/createSocketLabsSender.d.mts"
264
+ },
242
265
  "./graphQL/models/MongoDB/log/DevStatsGraphQLCalls": {
243
266
  "import": "./dist/graphQL/models/MongoDB/log/DevStatsGraphQLCalls.mjs",
244
267
  "types": "./dist/graphQL/models/MongoDB/log/DevStatsGraphQLCalls.d.mts"
@@ -315,6 +338,14 @@
315
338
  "import": "./dist/koa/IKoaError.mjs",
316
339
  "types": "./dist/koa/IKoaError.d.mts"
317
340
  },
341
+ "./koa/IKoaErrorHandlerContext": {
342
+ "import": "./dist/koa/IKoaErrorHandlerContext.mjs",
343
+ "types": "./dist/koa/IKoaErrorHandlerContext.d.mts"
344
+ },
345
+ "./koa/IKoaErrorHandlerOptions": {
346
+ "import": "./dist/koa/IKoaErrorHandlerOptions.mjs",
347
+ "types": "./dist/koa/IKoaErrorHandlerOptions.d.mts"
348
+ },
318
349
  "./files/uploadTempImage": {
319
350
  "import": "./dist/files/uploadTempImage.mjs",
320
351
  "types": "./dist/files/uploadTempImage.d.mts"
@@ -539,6 +570,10 @@
539
570
  "import": "./dist/koa/router/verifyEmail.mjs",
540
571
  "types": "./dist/koa/router/verifyEmail.d.mts"
541
572
  },
573
+ "./koa/createKoaErrorHandler": {
574
+ "import": "./dist/koa/createKoaErrorHandler.mjs",
575
+ "types": "./dist/koa/createKoaErrorHandler.d.mts"
576
+ },
542
577
  "./koa/customFormatErrorFn": {
543
578
  "import": "./dist/koa/customFormatErrorFn.mjs",
544
579
  "types": "./dist/koa/customFormatErrorFn.d.mts"
@@ -651,6 +686,10 @@
651
686
  "import": "./dist/lib/NumLib.mjs",
652
687
  "types": "./dist/lib/NumLib.d.mts"
653
688
  },
689
+ "./lib/OBJECTID_0_OBJ": {
690
+ "import": "./dist/lib/OBJECTID_0_OBJ.mjs",
691
+ "types": "./dist/lib/OBJECTID_0_OBJ.d.mts"
692
+ },
654
693
  "./lib/setLoginCookies": {
655
694
  "import": "./dist/lib/setLoginCookies.mjs",
656
695
  "types": "./dist/lib/setLoginCookies.d.mts"
@@ -696,22 +735,33 @@
696
735
  "reflect-metadata": "<0.3",
697
736
  "sequelize": "<7",
698
737
  "sequelize-typescript": "<3",
699
- "sharp": "<1",
700
- "uuid": "<15"
738
+ "sharp": "<1"
701
739
  },
702
740
  "peerDependenciesMeta": {
741
+ "@node-rs/bcrypt": {
742
+ "optional": true
743
+ },
744
+ "@sentry/node": {
745
+ "optional": true
746
+ },
703
747
  "@socketlabs/email": {
704
748
  "optional": true
705
749
  },
706
750
  "clamscan": {
707
751
  "optional": true
708
752
  },
753
+ "dotenv": {
754
+ "optional": true
755
+ },
709
756
  "file-type": {
710
757
  "optional": true
711
758
  },
712
759
  "fs-extra": {
713
760
  "optional": true
714
761
  },
762
+ "graphql": {
763
+ "optional": true
764
+ },
715
765
  "keygrip": {
716
766
  "optional": true
717
767
  },
@@ -721,9 +771,15 @@
721
771
  "mariadb": {
722
772
  "optional": true
723
773
  },
774
+ "mongoose": {
775
+ "optional": true
776
+ },
724
777
  "pg": {
725
778
  "optional": true
726
779
  },
780
+ "redis": {
781
+ "optional": true
782
+ },
727
783
  "reflect-metadata": {
728
784
  "optional": true
729
785
  },
@@ -753,7 +809,7 @@
753
809
  "@types/fs-extra": "^11.0.4",
754
810
  "@types/koa": "^3.0.3",
755
811
  "@types/mocha": "^10.0.10",
756
- "@types/node": "^26.6.5",
812
+ "@types/node": "^26.6.6",
757
813
  "@types/pg": "^8.23.1",
758
814
  "@types/sinon": "^22.0.2",
759
815
  "@typescript-eslint/eslint-plugin": "^8.72.1",
@@ -784,7 +840,7 @@
784
840
  "patch-package": "8.0.1",
785
841
  "pg": "^8.23.1",
786
842
  "prettier": "^3.9.10",
787
- "redis": "^6.3.0",
843
+ "redis": "^6.3.1",
788
844
  "reflect-metadata": "^0.2.2",
789
845
  "sequelize": "^6.37.8",
790
846
  "sequelize-typescript": "^2.1.6",