@serve.zone/platformclient 2.3.0 → 3.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.
@@ -3,6 +3,10 @@ import { SzSmsConnector } from './email/classes.smsconnector.js';
3
3
  import { SzPushNotificationConnector } from './email/classes.pushnotificationconnector.js';
4
4
  import { SzLetterConnector } from './email/classes.letterconnector.js';
5
5
  import { SzWebPushConnector } from './webpush/classes.webpushconnector.js';
6
+ import {
7
+ SzCoreMailConnector,
8
+ type ISzCoreMailConnectorOptions,
9
+ } from './coremail/classes.coremailconnector.js';
6
10
  import * as plugins from './plugins.js';
7
11
 
8
12
  export interface ISzPlatformClientOptions {
@@ -13,6 +17,7 @@ export interface ISzPlatformClientOptions {
13
17
  platformUrl?: string;
14
18
  binding?: plugins.servezoneInterfaces.platform.IPlatformBinding;
15
19
  bindings?: plugins.servezoneInterfaces.platform.IPlatformBinding[];
20
+ coreMail?: ISzCoreMailConnectorOptions;
16
21
  }
17
22
 
18
23
  export class SzPlatformClient {
@@ -23,19 +28,37 @@ export class SzPlatformClient {
23
28
  public typedrouter: plugins.typedrequest.TypedRouter = new plugins.typedrequest.TypedRouter();
24
29
  public typedsocket!: plugins.typedsocket.TypedSocket;
25
30
  private qenvInstance = new plugins.qenv.Qenv();
31
+ private stopped = false;
32
+ private initPromise?: Promise<void>;
33
+ private stopPromise?: Promise<void>;
34
+ private readonly platformSocketAbortController = new AbortController();
26
35
 
27
36
  public emailConnector = new SzEmailConnector(this);
28
37
  public smsConnector = new SzSmsConnector(this);
29
38
  public pushNotificationConnector = new SzPushNotificationConnector(this);
30
39
  public webPushConnector = new SzWebPushConnector(this);
31
40
  public letterConnector = new SzLetterConnector(this);
41
+ public coreMailConnector = new SzCoreMailConnector();
32
42
 
33
43
  constructor(authorizationStringArg?: string | ISzPlatformClientOptions) {
34
44
  this.applyOptions(authorizationStringArg);
35
45
  }
36
46
 
37
- public async init(authorizationStringArg?: string | ISzPlatformClientOptions) {
47
+ public init(
48
+ authorizationStringArg?: string | ISzPlatformClientOptions,
49
+ ): Promise<void> {
50
+ if (this.stopped) {
51
+ return Promise.reject(this.createAbortError());
52
+ }
53
+ if (this.initPromise) {
54
+ return this.initPromise;
55
+ }
38
56
  this.applyOptions(authorizationStringArg);
57
+ this.initPromise = this.initInner();
58
+ return this.initPromise;
59
+ }
60
+
61
+ private async initInner(): Promise<void> {
39
62
  if (!this.authorizationString) {
40
63
  this.authorizationString = await this.getConfiguredAuthorizationString();
41
64
  }
@@ -48,19 +71,33 @@ export class SzPlatformClient {
48
71
  this.platformBindings,
49
72
  await this.discoverPlatformBindings()
50
73
  );
51
- if (!this.authorizationString) {
52
- this.authorizationString = this.getAuthorizationStringFromBindings();
53
- }
54
- if (!this.authorizationString) throw new Error('authorizationString is required');
55
- if (this.authorizationString === 'test') {
56
- this.activateDebugMode();
74
+ if (!this.authorizationString && this.coreMailConnector.isConfigured) {
75
+ await this.coreMailConnector.init();
76
+ this.assertRunning();
57
77
  return;
58
78
  }
59
- this.typedsocket = await plugins.typedsocket.TypedSocket.createClient(
79
+ if (!this.authorizationString) throw new Error('authorizationString is required');
80
+ const socket = await plugins.typedsocket.TypedSocket.createClient(
60
81
  this.typedrouter as any,
61
- await this.getConnectionAddress()
82
+ await this.getConnectionAddress(),
83
+ { abortSignal: this.platformSocketAbortController.signal },
62
84
  );
63
- await this.setAuthorizationTag();
85
+ if (this.stopped) {
86
+ await socket.stop().catch(() => undefined);
87
+ throw this.createAbortError();
88
+ }
89
+ this.typedsocket = socket;
90
+ try {
91
+ await this.setAuthorizationTag();
92
+ this.assertRunning();
93
+ if (this.coreMailConnector.isConfigured) {
94
+ await this.coreMailConnector.init();
95
+ this.assertRunning();
96
+ }
97
+ } catch (error) {
98
+ await socket.stop().catch(() => undefined);
99
+ throw error;
100
+ }
64
101
  }
65
102
 
66
103
  private activateDebugMode() {
@@ -101,6 +138,9 @@ export class SzPlatformClient {
101
138
  Boolean
102
139
  ) as plugins.servezoneInterfaces.platform.IPlatformBinding[]
103
140
  );
141
+ if (optionsArg.coreMail) {
142
+ this.coreMailConnector.configure(optionsArg.coreMail);
143
+ }
104
144
  }
105
145
 
106
146
  private async getConnectionAddress() {
@@ -143,26 +183,6 @@ export class SzPlatformClient {
143
183
  );
144
184
  }
145
185
 
146
- private getAuthorizationStringFromBindings() {
147
- const authEnvNames = [
148
- 'SERVEZONE_PLATFORM_AUTHORIZATION',
149
- 'SERVEZONE_PLATFORM_TOKEN',
150
- 'SERVEZONE_API_TOKEN',
151
- 'AUTHORIZATION',
152
- 'TOKEN',
153
- ];
154
- for (const binding of this.platformBindings) {
155
- for (const credential of binding.credentials || []) {
156
- for (const envName of authEnvNames) {
157
- const value = credential.env?.[envName];
158
- if (value) {
159
- return value;
160
- }
161
- }
162
- }
163
- }
164
- }
165
-
166
186
  private async setAuthorizationTag() {
167
187
  if (!this.authorizationString) {
168
188
  return;
@@ -176,16 +196,39 @@ export class SzPlatformClient {
176
196
  }
177
197
  }
178
198
 
179
- public async stop(): Promise<void> {
180
- await Promise.all([
199
+ public stop(): Promise<void> {
200
+ this.stopPromise ??= this.stopInner();
201
+ return this.stopPromise;
202
+ }
203
+
204
+ private async stopInner(): Promise<void> {
205
+ this.stopped = true;
206
+ this.platformSocketAbortController.abort(this.createAbortError());
207
+ const socket = this.typedsocket;
208
+ await Promise.allSettled([
181
209
  this.emailConnector.stop(),
182
210
  this.webPushConnector.stop(),
211
+ this.coreMailConnector.stop(),
212
+ socket?.stop(),
213
+ this.initPromise,
183
214
  ]);
184
- if (this.typedsocket) {
185
- await this.typedsocket.stop();
215
+ if (this.typedsocket && this.typedsocket !== socket) {
216
+ await this.typedsocket.stop().catch(() => undefined);
186
217
  }
187
218
  }
188
219
 
220
+ private assertRunning(): void {
221
+ if (this.stopped) {
222
+ throw this.createAbortError();
223
+ }
224
+ }
225
+
226
+ private createAbortError(): Error {
227
+ const error = new Error('Platform client operation aborted.');
228
+ error.name = 'AbortError';
229
+ return error;
230
+ }
231
+
189
232
  private async discoverPlatformBindings() {
190
233
  const bindings: plugins.servezoneInterfaces.platform.IPlatformBinding[] = [];
191
234
  const envBindings = await this.getConfiguredEnvVar('SERVEZONE_PLATFORM_BINDINGS');