@serve.zone/coremail 1.1.0 → 1.3.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 (57) hide show
  1. package/changelog.md +43 -0
  2. package/dist_ts/00_commitinfo_data.js +1 -1
  3. package/dist_ts/classes.auth.d.ts +19 -0
  4. package/dist_ts/classes.auth.js +58 -1
  5. package/dist_ts/classes.coremail.d.ts +3 -0
  6. package/dist_ts/classes.coremail.js +11 -4
  7. package/dist_ts/classes.gateway.d.ts +11 -1
  8. package/dist_ts/classes.gateway.js +101 -63
  9. package/dist_ts/classes.inbound.d.ts +10 -1
  10. package/dist_ts/classes.inbound.js +45 -7
  11. package/dist_ts/classes.maintenance.js +16 -1
  12. package/dist_ts/classes.models.d.ts +24 -2
  13. package/dist_ts/classes.models.js +72 -2
  14. package/dist_ts/classes.server.d.ts +6 -2
  15. package/dist_ts/classes.server.js +18 -68
  16. package/dist_ts/classes.smtpsubmission.d.ts +85 -0
  17. package/dist_ts/classes.smtpsubmission.js +379 -0
  18. package/dist_ts/classes.storage.d.ts +9 -0
  19. package/dist_ts/classes.storage.js +44 -1
  20. package/dist_ts/classes.submissions.d.ts +27 -1
  21. package/dist_ts/classes.submissions.js +164 -14
  22. package/dist_ts/coremail.errors.d.ts +18 -0
  23. package/dist_ts/coremail.errors.js +84 -0
  24. package/dist_ts/coremail.log.d.ts +1 -1
  25. package/dist_ts/coremail.log.js +1 -1
  26. package/dist_ts/coremail.mime.d.ts +15 -0
  27. package/dist_ts/coremail.mime.js +76 -1
  28. package/dist_ts/coremail.persistence.d.ts +38 -1
  29. package/dist_ts/coremail.persistence.js +124 -40
  30. package/dist_ts/coremail.stats.d.ts +72 -0
  31. package/dist_ts/coremail.stats.js +242 -0
  32. package/dist_ts/coremail.validation.d.ts +15 -2
  33. package/dist_ts/coremail.validation.js +26 -78
  34. package/dist_ts/interfaces.d.ts +18 -0
  35. package/dist_ts/plugins.d.ts +1 -0
  36. package/dist_ts/plugins.js +2 -1
  37. package/package.json +12 -11
  38. package/readme.md +51 -2
  39. package/ts/00_commitinfo_data.ts +1 -1
  40. package/ts/classes.auth.ts +77 -0
  41. package/ts/classes.coremail.ts +20 -0
  42. package/ts/classes.gateway.ts +121 -66
  43. package/ts/classes.inbound.ts +71 -6
  44. package/ts/classes.maintenance.ts +14 -0
  45. package/ts/classes.models.ts +67 -1
  46. package/ts/classes.server.ts +23 -80
  47. package/ts/classes.smtpsubmission.ts +466 -0
  48. package/ts/classes.storage.ts +54 -0
  49. package/ts/classes.submissions.ts +247 -12
  50. package/ts/coremail.errors.ts +111 -0
  51. package/ts/coremail.log.ts +1 -0
  52. package/ts/coremail.mime.ts +89 -0
  53. package/ts/coremail.persistence.ts +196 -48
  54. package/ts/coremail.stats.ts +342 -0
  55. package/ts/coremail.validation.ts +31 -103
  56. package/ts/interfaces.ts +20 -0
  57. package/ts/plugins.ts +1 -0
@@ -1,11 +1,16 @@
1
1
  import * as plugins from './plugins.js';
2
- import type { ISubmissionPartRecord, TTransferGrantPurpose } from './interfaces.js';
2
+ import type {
3
+ ISubmissionPartRecord,
4
+ TCoreMailSubmissionSource,
5
+ TTransferGrantPurpose,
6
+ } from './interfaces.js';
3
7
  import {
4
8
  assertCoreMailDesiredSnapshotRecord,
5
9
  assertCoreMailInboundDeliveryRecord,
6
10
  assertCoreMailInboundHandoffRecord,
7
11
  assertCoreMailQuotaCounterRecord,
8
12
  assertCoreMailRecipientHandleRecord,
13
+ assertCoreMailStatCounterRecord,
9
14
  assertCoreMailStateRecord,
10
15
  assertCoreMailSubmissionRecord,
11
16
  assertCoreMailTransferGrantRecord,
@@ -14,6 +19,7 @@ import {
14
19
  type ICoreMailInboundHandoffRecord,
15
20
  type ICoreMailQuotaCounterRecord,
16
21
  type ICoreMailRecipientHandleRecord,
22
+ type ICoreMailStatCounterRecord,
17
23
  type ICoreMailStateRecord,
18
24
  type ICoreMailSubmissionRecord,
19
25
  type ICoreMailTransferGrantRecord,
@@ -111,6 +117,33 @@ export class CoreMailTransferGrantDoc extends plugins.smartdata.SmartDataDbDoc<
111
117
  public consumedAt?: number;
112
118
  }
113
119
 
120
+ @plugins.smartdata.exactPersistence({
121
+ assertDocument: assertCoreMailStatCounterRecord,
122
+ reconcileBy: 'statId',
123
+ })
124
+ export class CoreMailStatCounterDoc extends plugins.smartdata.SmartDataDbDoc<
125
+ CoreMailStatCounterDoc,
126
+ ICoreMailStatCounterRecord
127
+ > implements ICoreMailStatCounterRecord {
128
+ declare public static exact: plugins.smartdata.TExact<CoreMailStatCounterDoc>;
129
+ public statId!: TCoreMailSha256;
130
+ public tenantId!: string;
131
+ public serviceId!: string;
132
+ public bindingId!: string;
133
+ public dayUtc!: number;
134
+ public submittedApi!: number;
135
+ public submittedSmtp!: number;
136
+ public delivered!: number;
137
+ public deferred!: number;
138
+ public failed!: number;
139
+ public deadLettered!: number;
140
+ public received!: number;
141
+ public acknowledgedProcessed!: number;
142
+ public acknowledgedDiscarded!: number;
143
+ public createdAt!: number;
144
+ public updatedAt!: number;
145
+ }
146
+
114
147
  @plugins.smartdata.exactPersistence({
115
148
  assertDocument: assertCoreMailSubmissionRecord,
116
149
  reconcileBy: 'submissionId',
@@ -129,6 +162,8 @@ export class CoreMailSubmissionDoc extends plugins.smartdata.SmartDataDbDoc<
129
162
  public idempotencyKey!: string;
130
163
  public submissionDigest!: TCoreMailSha256;
131
164
  public state!: plugins.serveZoneInterfaces.data.TCoreMailSubmissionState;
165
+ public source?: TCoreMailSubmissionSource;
166
+ public statsSubmittedCountedAt?: number;
132
167
  public message!: plugins.serveZoneInterfaces.data.ICoreMailOutboundMessageDescriptor;
133
168
  public parts!: ISubmissionPartRecord[];
134
169
  public mimeObjectKey?: string;
@@ -237,6 +272,7 @@ export interface ICoreMailModels {
237
272
  State: typeof CoreMailStateDoc;
238
273
  DesiredSnapshot: typeof CoreMailDesiredSnapshotDoc;
239
274
  QuotaCounter: typeof CoreMailQuotaCounterDoc;
275
+ StatCounter: typeof CoreMailStatCounterDoc;
240
276
  TransferGrant: typeof CoreMailTransferGrantDoc;
241
277
  Submission: typeof CoreMailSubmissionDoc;
242
278
  InboundDelivery: typeof CoreMailInboundDeliveryDoc;
@@ -310,6 +346,34 @@ export const createCoreMailModels = (
310
346
  key: { state: 1, updatedAt: 1 },
311
347
  },
312
348
  ]) as typeof CoreMailDesiredSnapshotDoc,
349
+ StatCounter: bind(CoreMailStatCounterDoc, 'CoreMailStatCounter', [
350
+ 'statId',
351
+ 'tenantId',
352
+ 'serviceId',
353
+ 'bindingId',
354
+ 'dayUtc',
355
+ 'submittedApi',
356
+ 'submittedSmtp',
357
+ 'delivered',
358
+ 'deferred',
359
+ 'failed',
360
+ 'deadLettered',
361
+ 'received',
362
+ 'acknowledgedProcessed',
363
+ 'acknowledgedDiscarded',
364
+ 'createdAt',
365
+ 'updatedAt',
366
+ ], 'statId', [
367
+ {
368
+ name: 'authority_day_unique',
369
+ key: { tenantId: 1, serviceId: 1, bindingId: 1, dayUtc: 1 },
370
+ options: { unique: true },
371
+ },
372
+ {
373
+ name: 'day_retention',
374
+ key: { dayUtc: 1, statId: 1 },
375
+ },
376
+ ]) as typeof CoreMailStatCounterDoc,
313
377
  QuotaCounter: bind(CoreMailQuotaCounterDoc, 'CoreMailQuotaCounter', [
314
378
  'counterId',
315
379
  'tenantId',
@@ -375,6 +439,8 @@ export const createCoreMailModels = (
375
439
  'idempotencyKey',
376
440
  'submissionDigest',
377
441
  'state',
442
+ 'source',
443
+ 'statsSubmittedCountedAt',
378
444
  'message',
379
445
  'parts',
380
446
  'mimeObjectKey',
@@ -10,6 +10,8 @@ import {
10
10
  import type { CoreMailDesiredStateService } from './classes.desiredstate.js';
11
11
  import type { CoreMailGateway } from './classes.gateway.js';
12
12
  import type { CoreMailInboundService } from './classes.inbound.js';
13
+ import type { ICoreMailModels } from './classes.models.js';
14
+ import type { CoreMailSmtpSubmissionServer } from './classes.smtpsubmission.js';
13
15
  import type { CoreMailStorage } from './classes.storage.js';
14
16
  import type { CoreMailSubmissionService } from './classes.submissions.js';
15
17
  import type { CoreMailTransferService } from './classes.transfer.js';
@@ -19,72 +21,13 @@ import type {
19
21
  ICoreMailReadiness,
20
22
  ICoreMailWorkloadSession,
21
23
  } from './interfaces.js';
22
- import { CoreMailQuotaExceededError } from './coremail.quota.js';
23
- import { logCoreMailFailure } from './coremail.log.js';
24
-
25
- type TErrorCode = plugins.serveZoneInterfaces.data.TCoreMailErrorCode;
24
+ import { addPrivacySafeTypedHandler } from './coremail.errors.js';
25
+ import { listCoreMailServiceMailStatistics } from './coremail.stats.js';
26
26
 
27
27
  interface ICoreMailTypedTools {
28
28
  localData: Record<string, unknown>;
29
29
  }
30
30
 
31
- export const privacySafeError = (
32
- errorArg: unknown,
33
- ): plugins.typedrequest.TypedResponseError => {
34
- let code: TErrorCode = 'INVALID_REQUEST';
35
- let retryable = false;
36
- if (errorArg instanceof CoreMailAuthenticationError) {
37
- code = 'AUTHENTICATION_FAILED';
38
- } else if (errorArg instanceof CoreMailCapabilityDeniedError) {
39
- code = 'CAPABILITY_DENIED';
40
- } else if (errorArg instanceof CoreMailAuthorizationError) {
41
- code = 'AUTHORITY_REVOKED';
42
- } else if (errorArg instanceof CoreMailQuotaExceededError) {
43
- return new plugins.typedrequest.TypedResponseError('CoreMail request rejected.', {
44
- code: 'QUOTA_EXCEEDED',
45
- retryable: true,
46
- ...(errorArg.retryAfterMs === undefined
47
- ? {}
48
- : { retryAfterMs: errorArg.retryAfterMs }),
49
- } satisfies plugins.serveZoneInterfaces.data.ICoreMailErrorData);
50
- } else if (
51
- errorArg instanceof plugins.serveZoneInterfaces.data.CoreMailContractError
52
- ) {
53
- code = errorArg.code;
54
- } else if (
55
- errorArg instanceof Error
56
- && errorArg.message.includes('gateway')
57
- && errorArg.message.includes('unavailable')
58
- ) {
59
- code = 'GATEWAY_UNAVAILABLE';
60
- retryable = true;
61
- } else if (
62
- errorArg instanceof Error
63
- && errorArg.message.includes('idempotency')
64
- ) {
65
- code = 'IDEMPOTENCY_CONFLICT';
66
- } else if (
67
- errorArg instanceof Error
68
- && errorArg.message.includes('fence')
69
- ) {
70
- code = 'STATE_CONFLICT';
71
- retryable = true;
72
- } else if (
73
- errorArg instanceof Error
74
- && (
75
- errorArg.message.includes('byte budget')
76
- || errorArg.message.includes('content budget')
77
- || errorArg.message.includes('exceeds')
78
- )
79
- ) {
80
- code = 'PAYLOAD_LIMIT_EXCEEDED';
81
- }
82
- return new plugins.typedrequest.TypedResponseError('CoreMail request rejected.', {
83
- code,
84
- retryable,
85
- } satisfies plugins.serveZoneInterfaces.data.ICoreMailErrorData);
86
- };
87
-
88
31
  const asPeer = (
89
32
  toolsArg: ICoreMailTypedTools | undefined,
90
33
  ): ICoreMailPeer => {
@@ -146,6 +89,7 @@ export class CoreMailServer {
146
89
  public constructor(
147
90
  private readonly config: ICoreMailConfig,
148
91
  private readonly database: plugins.smartdata.SmartdataDb,
92
+ private readonly models: ICoreMailModels,
149
93
  private readonly storage: CoreMailStorage,
150
94
  private readonly transfers: CoreMailTransferService,
151
95
  private readonly desiredState: CoreMailDesiredStateService,
@@ -153,6 +97,7 @@ export class CoreMailServer {
153
97
  private readonly submissions: CoreMailSubmissionService,
154
98
  private readonly inbound: CoreMailInboundService,
155
99
  private readonly gateway: CoreMailGateway,
100
+ private readonly smtpSubmission: CoreMailSmtpSubmissionServer,
156
101
  private readonly now: () => number = Date.now,
157
102
  ) {
158
103
  this.registerHandlers();
@@ -295,6 +240,10 @@ export class CoreMailServer {
295
240
  exactPurge: objectStorage,
296
241
  controlBootstrap: true,
297
242
  gateway: Boolean(desiredState) && this.gateway.isReady(),
243
+ // An absent or disabled listener is ready by omission; one the desired
244
+ // state asks for is ready only while it is actually accepting.
245
+ smtpSubmission: !desiredState?.smtp?.enabled
246
+ || this.smtpSubmission.isReady(),
298
247
  };
299
248
  }
300
249
 
@@ -343,6 +292,12 @@ export class CoreMailServer {
343
292
  this.auth.requireControlSession(asPeer(toolsArg));
344
293
  return { status: await this.desiredState.getStatus(requestArg) };
345
294
  });
295
+ this.addHandler<
296
+ plugins.serveZoneInterfaces.requests.coremail.IReq_CoreMailGetServiceMailStatistics
297
+ >('coreMailGetServiceMailStatistics', async (requestArg, toolsArg) => {
298
+ this.auth.requireControlSession(asPeer(toolsArg));
299
+ return await listCoreMailServiceMailStatistics(this.models, requestArg);
300
+ });
346
301
  this.addHandler<
347
302
  plugins.serveZoneInterfaces.requests.coremail.IReq_CoreMailPrepareOutboundSubmission
348
303
  >('coreMailPrepareOutboundSubmission', async (requestArg, toolsArg) => {
@@ -426,30 +381,18 @@ export class CoreMailServer {
426
381
  });
427
382
  }
428
383
 
384
+ /** Register one typed workload handler with uniform failure redaction. */
429
385
  private addHandler<
430
386
  TRequest extends plugins.typedrequestInterfaces.ITypedRequest,
431
387
  >(
432
388
  methodArg: TRequest['method'],
433
- handlerArg: (
434
- requestArg: TRequest['request'],
435
- toolsArg: ICoreMailTypedTools | undefined,
436
- ) => Promise<TRequest['response']>,
389
+ handlerArg: plugins.typedrequest.THandlerFunction<TRequest>,
437
390
  ): void {
438
- this.router.addTypedHandler(new plugins.typedrequest.TypedHandler<TRequest>(
391
+ addPrivacySafeTypedHandler<TRequest>(
392
+ this.router,
393
+ 'server',
439
394
  methodArg,
440
- async (requestArg, toolsArg) => {
441
- try {
442
- return await handlerArg(requestArg, toolsArg);
443
- } catch (error) {
444
- const safeError = privacySafeError(error);
445
- if (safeError.errorData?.code === 'INVALID_REQUEST') {
446
- await logCoreMailFailure('server', 'UNEXPECTED_HANDLER_FAILURE', error, {
447
- method: methodArg,
448
- }).catch(() => undefined);
449
- }
450
- throw safeError;
451
- }
452
- },
453
- ));
395
+ handlerArg,
396
+ );
454
397
  }
455
398
  }