@authup/server-kit 1.0.0-beta.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 (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +23 -0
  3. package/dist/crypto/hash/compare.d.ts +2 -0
  4. package/dist/crypto/hash/compare.d.ts.map +1 -0
  5. package/dist/crypto/hash/hash.d.ts +2 -0
  6. package/dist/crypto/hash/hash.d.ts.map +1 -0
  7. package/dist/crypto/hash/index.d.ts +3 -0
  8. package/dist/crypto/hash/index.d.ts.map +1 -0
  9. package/dist/crypto/index.d.ts +4 -0
  10. package/dist/crypto/index.d.ts.map +1 -0
  11. package/dist/crypto/json-web-token/decode.d.ts +15 -0
  12. package/dist/crypto/json-web-token/decode.d.ts.map +1 -0
  13. package/dist/crypto/json-web-token/index.d.ts +5 -0
  14. package/dist/crypto/json-web-token/index.d.ts.map +1 -0
  15. package/dist/crypto/json-web-token/sign.d.ts +4 -0
  16. package/dist/crypto/json-web-token/sign.d.ts.map +1 -0
  17. package/dist/crypto/json-web-token/type.d.ts +34 -0
  18. package/dist/crypto/json-web-token/type.d.ts.map +1 -0
  19. package/dist/crypto/json-web-token/utils.d.ts +3 -0
  20. package/dist/crypto/json-web-token/utils.d.ts.map +1 -0
  21. package/dist/crypto/json-web-token/verify.d.ts +15 -0
  22. package/dist/crypto/json-web-token/verify.d.ts.map +1 -0
  23. package/dist/crypto/key-pair/constants.d.ts +5 -0
  24. package/dist/crypto/key-pair/constants.d.ts.map +1 -0
  25. package/dist/crypto/key-pair/create.d.ts +3 -0
  26. package/dist/crypto/key-pair/create.d.ts.map +1 -0
  27. package/dist/crypto/key-pair/delete.d.ts +3 -0
  28. package/dist/crypto/key-pair/delete.d.ts.map +1 -0
  29. package/dist/crypto/key-pair/index.d.ts +9 -0
  30. package/dist/crypto/key-pair/index.d.ts.map +1 -0
  31. package/dist/crypto/key-pair/load.d.ts +3 -0
  32. package/dist/crypto/key-pair/load.d.ts.map +1 -0
  33. package/dist/crypto/key-pair/module.d.ts +3 -0
  34. package/dist/crypto/key-pair/module.d.ts.map +1 -0
  35. package/dist/crypto/key-pair/save.d.ts +3 -0
  36. package/dist/crypto/key-pair/save.d.ts.map +1 -0
  37. package/dist/crypto/key-pair/type.d.ts +65 -0
  38. package/dist/crypto/key-pair/type.d.ts.map +1 -0
  39. package/dist/crypto/key-pair/utils/check.d.ts +4 -0
  40. package/dist/crypto/key-pair/utils/check.d.ts.map +1 -0
  41. package/dist/crypto/key-pair/utils/file-name.d.ts +4 -0
  42. package/dist/crypto/key-pair/utils/file-name.d.ts.map +1 -0
  43. package/dist/crypto/key-pair/utils/index.d.ts +5 -0
  44. package/dist/crypto/key-pair/utils/index.d.ts.map +1 -0
  45. package/dist/crypto/key-pair/utils/options.d.ts +3 -0
  46. package/dist/crypto/key-pair/utils/options.d.ts.map +1 -0
  47. package/dist/crypto/key-pair/utils/private-key.d.ts +4 -0
  48. package/dist/crypto/key-pair/utils/private-key.d.ts.map +1 -0
  49. package/dist/domain-event/index.d.ts +5 -0
  50. package/dist/domain-event/index.d.ts.map +1 -0
  51. package/dist/domain-event/publish.d.ts +4 -0
  52. package/dist/domain-event/publish.d.ts.map +1 -0
  53. package/dist/domain-event/redis/index.d.ts +2 -0
  54. package/dist/domain-event/redis/index.d.ts.map +1 -0
  55. package/dist/domain-event/redis/publish.d.ts +4 -0
  56. package/dist/domain-event/redis/publish.d.ts.map +1 -0
  57. package/dist/domain-event/socket/index.d.ts +3 -0
  58. package/dist/domain-event/socket/index.d.ts.map +1 -0
  59. package/dist/domain-event/socket/publish.d.ts +4 -0
  60. package/dist/domain-event/socket/publish.d.ts.map +1 -0
  61. package/dist/domain-event/socket/singleton.d.ts +4 -0
  62. package/dist/domain-event/socket/singleton.d.ts.map +1 -0
  63. package/dist/domain-event/socket/type.d.ts +5 -0
  64. package/dist/domain-event/socket/type.d.ts.map +1 -0
  65. package/dist/domain-event/type.d.ts +7 -0
  66. package/dist/domain-event/type.d.ts.map +1 -0
  67. package/dist/domain-event/utils.d.ts +4 -0
  68. package/dist/domain-event/utils.d.ts.map +1 -0
  69. package/dist/index.cjs +715 -0
  70. package/dist/index.cjs.map +1 -0
  71. package/dist/index.d.ts +7 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.mjs +684 -0
  74. package/dist/index.mjs.map +1 -0
  75. package/dist/logger/index.d.ts +4 -0
  76. package/dist/logger/index.d.ts.map +1 -0
  77. package/dist/logger/module.d.ts +4 -0
  78. package/dist/logger/module.d.ts.map +1 -0
  79. package/dist/logger/presets/index.d.ts +2 -0
  80. package/dist/logger/presets/index.d.ts.map +1 -0
  81. package/dist/logger/presets/void.d.ts +16 -0
  82. package/dist/logger/presets/void.d.ts.map +1 -0
  83. package/dist/logger/type.d.ts +14 -0
  84. package/dist/logger/type.d.ts.map +1 -0
  85. package/dist/proxy/index.d.ts +3 -0
  86. package/dist/proxy/index.d.ts.map +1 -0
  87. package/dist/proxy/module.d.ts +24 -0
  88. package/dist/proxy/module.d.ts.map +1 -0
  89. package/dist/proxy/utils.d.ts +8 -0
  90. package/dist/proxy/utils.d.ts.map +1 -0
  91. package/dist/smtp/config/index.d.ts +2 -0
  92. package/dist/smtp/config/index.d.ts.map +1 -0
  93. package/dist/smtp/config/singleton.d.ts +5 -0
  94. package/dist/smtp/config/singleton.d.ts.map +1 -0
  95. package/dist/smtp/index.d.ts +5 -0
  96. package/dist/smtp/index.d.ts.map +1 -0
  97. package/dist/smtp/module.d.ts +4 -0
  98. package/dist/smtp/module.d.ts.map +1 -0
  99. package/dist/smtp/singleton.d.ts +3 -0
  100. package/dist/smtp/singleton.d.ts.map +1 -0
  101. package/dist/smtp/type.d.ts +14 -0
  102. package/dist/smtp/type.d.ts.map +1 -0
  103. package/dist/utils/has-property.d.ts +2 -0
  104. package/dist/utils/has-property.d.ts.map +1 -0
  105. package/dist/utils/index.d.ts +2 -0
  106. package/dist/utils/index.d.ts.map +1 -0
  107. package/package.json +77 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,715 @@
1
+ 'use strict';
2
+
3
+ var bcrypt = require('bcrypt');
4
+ var node_crypto = require('node:crypto');
5
+ var core = require('@authup/core');
6
+ var path = require('node:path');
7
+ var fs = require('node:fs');
8
+ var jsonwebtoken = require('jsonwebtoken');
9
+ var smob = require('smob');
10
+ var redisExtension = require('redis-extension');
11
+ var redisEmitter = require('@socket.io/redis-emitter');
12
+ var http = require('node:http');
13
+ var https = require('node:https');
14
+ var proxyFromEnv = require('proxy-from-env');
15
+ var nodemailer = require('nodemailer');
16
+
17
+ async function compare(value, hashedValue) {
18
+ return bcrypt.compare(value, hashedValue);
19
+ }
20
+
21
+ async function hash(str, saltOrRounds = 10) {
22
+ return bcrypt.hash(str, saltOrRounds);
23
+ }
24
+
25
+ /*
26
+ * Copyright (c) 2022.
27
+ * Author Peter Placzek (tada5hi)
28
+ * For the full copyright and license information,
29
+ * view the LICENSE file that was distributed with this source code.
30
+ */ exports.KeyPairKind = void 0;
31
+ (function(KeyPairKind) {
32
+ KeyPairKind["PRIVATE"] = "private";
33
+ KeyPairKind["PUBLIC"] = "public";
34
+ })(exports.KeyPairKind || (exports.KeyPairKind = {}));
35
+
36
+ function isKeyPair(data) {
37
+ return core.isObject(data) && typeof data.privateKey !== 'undefined' && typeof data.publicKey !== 'undefined';
38
+ }
39
+ function isKeyPairWithPublicKey(data) {
40
+ return core.isObject(data) && typeof data.publicKey !== 'undefined';
41
+ }
42
+
43
+ function extendKeyPairOptions(options) {
44
+ var _options;
45
+ options = options ?? {};
46
+ options.directory = options.directory || process.cwd();
47
+ options.directory = path.isAbsolute(options.directory) ? options.directory : path.resolve(process.cwd(), options.directory);
48
+ (_options = options).type ?? (_options.type = 'rsa');
49
+ if (options.type === 'rsa' || options.type === 'rsa-pss' || options.type === 'dsa') {
50
+ options.modulusLength = 2048;
51
+ }
52
+ if (!options.privateKeyEncoding) {
53
+ options.privateKeyEncoding = {
54
+ type: 'pkcs8',
55
+ format: 'pem'
56
+ };
57
+ }
58
+ if (!options.publicKeyEncoding) {
59
+ options.publicKeyEncoding = {
60
+ type: 'spki',
61
+ format: 'pem'
62
+ };
63
+ }
64
+ if (options.privateKeyEncoding.passphrase && !options.privateKeyEncoding.cipher) {
65
+ options.privateKeyEncoding.cipher = 'aes-256-cbc';
66
+ }
67
+ return options;
68
+ }
69
+
70
+ function buildKeyFileName(type, context) {
71
+ const options = extendKeyPairOptions(context);
72
+ const parts = [];
73
+ switch(type){
74
+ case exports.KeyPairKind.PRIVATE:
75
+ {
76
+ if (options.privateName) {
77
+ parts.push(options.privateName);
78
+ } else {
79
+ parts.push(type);
80
+ }
81
+ if (options.privateExtension) {
82
+ if (options.privateExtension.startsWith('.')) {
83
+ options.privateExtension = options.privateExtension.slice(1);
84
+ }
85
+ parts.push(options.privateExtension);
86
+ } else {
87
+ parts.push('pem');
88
+ }
89
+ break;
90
+ }
91
+ case exports.KeyPairKind.PUBLIC:
92
+ {
93
+ if (options.publicName) {
94
+ parts.push(options.publicName);
95
+ } else {
96
+ parts.push(type);
97
+ }
98
+ if (options.publicExtension) {
99
+ if (options.publicExtension.startsWith('.')) {
100
+ options.publicExtension = options.publicExtension.slice(1);
101
+ }
102
+ parts.push(options.publicExtension);
103
+ } else {
104
+ parts.push('pem');
105
+ }
106
+ break;
107
+ }
108
+ }
109
+ return parts.join('.');
110
+ }
111
+
112
+ function decryptRSAPrivateKey(context, key) {
113
+ const privateKey = node_crypto.createPrivateKey({
114
+ type: context.privateKeyEncoding.type,
115
+ format: context.privateKeyEncoding.format,
116
+ key,
117
+ passphrase: context.privateKeyEncoding.passphrase || context.passphrase
118
+ });
119
+ let content = privateKey.export({
120
+ type: context.privateKeyEncoding.type,
121
+ format: context.privateKeyEncoding.format
122
+ });
123
+ if (typeof content !== 'string') {
124
+ content = Buffer.from(content).toString('utf-8');
125
+ }
126
+ return content;
127
+ }
128
+
129
+ async function saveKeyPair(keyPair, context) {
130
+ context = extendKeyPairOptions(context);
131
+ await fs.promises.mkdir(context.directory, {
132
+ recursive: true
133
+ });
134
+ await Promise.all([
135
+ {
136
+ path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, context)),
137
+ content: keyPair.privateKey
138
+ },
139
+ {
140
+ path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, context)),
141
+ content: keyPair.publicKey
142
+ }
143
+ ].map((file)=>fs.promises.writeFile(file.path, file.content)));
144
+ return keyPair;
145
+ }
146
+
147
+ async function createKeyPair(context) {
148
+ const options = extendKeyPairOptions(context);
149
+ const keyPair = await new Promise((resolve, reject)=>{
150
+ const callback = (err, publicKey, privateKey)=>{
151
+ if (err) reject(err);
152
+ resolve({
153
+ privateKey,
154
+ publicKey
155
+ });
156
+ };
157
+ switch(options.type){
158
+ case 'dsa':
159
+ node_crypto.generateKeyPair(options.type, options, callback);
160
+ break;
161
+ case 'ec':
162
+ node_crypto.generateKeyPair(options.type, options, callback);
163
+ break;
164
+ case 'rsa':
165
+ node_crypto.generateKeyPair(options.type, options, callback);
166
+ break;
167
+ case 'rsa-pss':
168
+ node_crypto.generateKeyPair(options.type, options, callback);
169
+ break;
170
+ }
171
+ });
172
+ if (options.save) {
173
+ await saveKeyPair(keyPair, options);
174
+ }
175
+ if (options.passphrase || options.privateKeyEncoding.passphrase) {
176
+ keyPair.privateKey = decryptRSAPrivateKey(options, keyPair.privateKey);
177
+ }
178
+ return keyPair;
179
+ }
180
+
181
+ async function deleteKeyPair(context) {
182
+ const options = extendKeyPairOptions(context);
183
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
184
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
185
+ try {
186
+ await Promise.all([
187
+ privateKeyPath,
188
+ publicKeyPath
189
+ ].map((filePath)=>fs.promises.stat(filePath)));
190
+ } catch (e) {
191
+ return;
192
+ }
193
+ await Promise.all([
194
+ privateKeyPath,
195
+ publicKeyPath
196
+ ].map((filePath)=>fs.promises.rm(filePath)));
197
+ }
198
+
199
+ async function loadKeyPair(context) {
200
+ const options = extendKeyPairOptions(context);
201
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
202
+ try {
203
+ await fs.promises.stat(privateKeyPath);
204
+ } catch (e) {
205
+ return undefined;
206
+ }
207
+ const privateKeyBuffer = await fs.promises.readFile(privateKeyPath);
208
+ let privateKey = privateKeyBuffer.toString();
209
+ if (options.passphrase || options.privateKeyEncoding.passphrase) {
210
+ privateKey = decryptRSAPrivateKey(options, privateKey);
211
+ }
212
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
213
+ let publicKey;
214
+ try {
215
+ await fs.promises.stat(publicKeyPath);
216
+ const publicKeyBuffer = await fs.promises.readFile(publicKeyPath);
217
+ publicKey = publicKeyBuffer.toString();
218
+ } catch (e) {
219
+ const publicKeyObject = node_crypto.createPublicKey({
220
+ key: privateKey,
221
+ format: options.privateKeyEncoding.format,
222
+ type: options.publicKeyEncoding.type
223
+ });
224
+ const stringOrBuffer = publicKeyObject.export({
225
+ format: options.publicKeyEncoding.format,
226
+ type: options.publicKeyEncoding.type
227
+ });
228
+ if (typeof stringOrBuffer !== 'string') {
229
+ publicKey = stringOrBuffer.toString();
230
+ } else {
231
+ publicKey = stringOrBuffer;
232
+ }
233
+ if (options.save) {
234
+ await saveKeyPair({
235
+ privateKey,
236
+ publicKey
237
+ }, options);
238
+ }
239
+ }
240
+ return {
241
+ privateKey,
242
+ publicKey
243
+ };
244
+ }
245
+
246
+ const keyPairCache = {};
247
+ async function useKeyPair(value) {
248
+ let options;
249
+ if (typeof value === 'string') {
250
+ options = extendKeyPairOptions({
251
+ privateName: value
252
+ });
253
+ } else {
254
+ options = extendKeyPairOptions(value || {});
255
+ }
256
+ if (Object.prototype.hasOwnProperty.call(keyPairCache, options.privateName)) {
257
+ return keyPairCache[options.privateName];
258
+ }
259
+ let keyPair = await loadKeyPair(options);
260
+ if (typeof keyPair === 'undefined') {
261
+ keyPair = await createKeyPair(options);
262
+ }
263
+ keyPairCache[options.privateName] = keyPair;
264
+ return keyPair;
265
+ }
266
+
267
+ function createErrorForJWTError(e) {
268
+ if (smob.isObject(e) && typeof e.name === 'string') {
269
+ switch(e.name){
270
+ case 'TokenExpiredError':
271
+ {
272
+ return core.TokenError.expired();
273
+ }
274
+ case 'NotBeforeError':
275
+ {
276
+ if (typeof e.date === 'string' || e.date instanceof Date) {
277
+ return core.TokenError.notActiveBefore(e.date);
278
+ }
279
+ break;
280
+ }
281
+ case 'JsonWebTokenError':
282
+ {
283
+ if (typeof e.message === 'string') {
284
+ return core.TokenError.payloadInvalid(e.message);
285
+ }
286
+ break;
287
+ }
288
+ }
289
+ }
290
+ return new core.TokenError({
291
+ cause: e,
292
+ logMessage: true,
293
+ message: 'The JWT error could not be determined.'
294
+ });
295
+ }
296
+
297
+ function decodeToken(token, options) {
298
+ options ?? (options = {});
299
+ let output;
300
+ try {
301
+ output = jsonwebtoken.decode(token, {
302
+ ...options
303
+ });
304
+ } catch (e) {
305
+ throw createErrorForJWTError(e);
306
+ }
307
+ if (output === null) {
308
+ throw core.TokenError.payloadInvalid('The token could not be decoded.');
309
+ }
310
+ return output;
311
+ }
312
+
313
+ async function signToken(payload, context) {
314
+ context.expiresIn = context.expiresIn || 3600;
315
+ switch(context.type){
316
+ case core.KeyType.RSA:
317
+ case core.KeyType.EC:
318
+ {
319
+ const { type, keyPair, ...options } = context;
320
+ const { privateKey } = isKeyPair(keyPair) ? keyPair : await useKeyPair(keyPair);
321
+ if (type === core.KeyType.RSA) {
322
+ options.algorithm = options.algorithm || 'RS256';
323
+ } else {
324
+ options.algorithm = options.algorithm || 'ES256';
325
+ }
326
+ return jsonwebtoken.sign(payload, privateKey, options);
327
+ }
328
+ case core.KeyType.OCT:
329
+ {
330
+ const { type, secret, ...options } = context;
331
+ options.algorithm = options.algorithm || 'HS256';
332
+ return jsonwebtoken.sign(payload, secret, options);
333
+ }
334
+ }
335
+ throw new core.TokenError();
336
+ }
337
+
338
+ async function verifyToken(token, context) {
339
+ let promise;
340
+ let output;
341
+ try {
342
+ switch(context.type){
343
+ case core.KeyType.RSA:
344
+ case core.KeyType.EC:
345
+ {
346
+ const { type, keyPair, ...options } = context;
347
+ const { publicKey } = isKeyPairWithPublicKey(keyPair) ? keyPair : await useKeyPair(keyPair);
348
+ if (type === core.KeyType.RSA) {
349
+ options.algorithms = options.algorithms || [
350
+ 'RS256',
351
+ 'RS384',
352
+ 'RS512',
353
+ 'PS256',
354
+ 'PS384',
355
+ 'PS512'
356
+ ];
357
+ } else {
358
+ options.algorithms = options.algorithms || [
359
+ 'ES256',
360
+ 'ES384',
361
+ 'ES512'
362
+ ];
363
+ }
364
+ promise = new Promise((resolve, reject)=>{
365
+ jsonwebtoken.verify(token, publicKey, options, (err, decoded)=>{
366
+ if (err) {
367
+ reject(err);
368
+ return;
369
+ }
370
+ resolve(decoded);
371
+ });
372
+ });
373
+ break;
374
+ }
375
+ case core.KeyType.OCT:
376
+ {
377
+ const { type, secret, ...options } = context;
378
+ options.algorithms = options.algorithms || [
379
+ 'HS256',
380
+ 'HS384',
381
+ 'HS512'
382
+ ];
383
+ promise = new Promise((resolve, reject)=>{
384
+ jsonwebtoken.verify(token, secret, options, (err, decoded)=>{
385
+ if (err) {
386
+ reject(err);
387
+ return;
388
+ }
389
+ resolve(decoded);
390
+ });
391
+ });
392
+ }
393
+ }
394
+ output = await promise;
395
+ } catch (e) {
396
+ throw createErrorForJWTError(e);
397
+ }
398
+ if (typeof output === 'undefined') {
399
+ throw new core.TokenError({
400
+ message: 'Invalid type.'
401
+ });
402
+ }
403
+ return output;
404
+ }
405
+
406
+ function transformDomainEventData(input) {
407
+ if (core.isObject(input)) {
408
+ const keys = Object.keys(input);
409
+ for(let i = 0; i < keys.length; i++){
410
+ const value = input[keys[i]];
411
+ if (value instanceof Date) {
412
+ input[keys[i]] = value.toISOString();
413
+ }
414
+ }
415
+ }
416
+ return input;
417
+ }
418
+ function buildDomainEventChannelName(input, id) {
419
+ if (typeof input === 'string') {
420
+ return input;
421
+ }
422
+ return input(id);
423
+ }
424
+
425
+ async function publishDomainRedisEvent(context, destinations) {
426
+ if (!redisExtension.hasClient() && !redisExtension.hasConfig()) {
427
+ return Promise.resolve();
428
+ }
429
+ context = transformDomainEventData(context);
430
+ const json = JSON.stringify(context);
431
+ const client = redisExtension.useClient();
432
+ const pipeline = client.pipeline();
433
+ for(let i = 0; i < destinations.length; i++){
434
+ const { namespace } = destinations[i];
435
+ const keyPrefix = namespace ? `${namespace}:` : '';
436
+ let key = keyPrefix + buildDomainEventChannelName(destinations[i].channel);
437
+ pipeline.publish(key, json);
438
+ if (context.event !== core.DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
439
+ key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
440
+ pipeline.publish(key, json);
441
+ }
442
+ }
443
+ return pipeline.exec();
444
+ }
445
+
446
+ let instance$3;
447
+ function useSocketEmitter() {
448
+ if (typeof instance$3 !== 'undefined') {
449
+ return instance$3;
450
+ }
451
+ instance$3 = new redisEmitter.Emitter(redisExtension.useClient());
452
+ return instance$3;
453
+ }
454
+
455
+ function publishDomainSocketEvent(context, destinations) {
456
+ if (!redisExtension.hasClient() && !redisExtension.hasConfig()) {
457
+ return;
458
+ }
459
+ context = transformDomainEventData(context);
460
+ for(let i = 0; i < destinations.length; i++){
461
+ let emitter = useSocketEmitter();
462
+ if (destinations[i].namespace) {
463
+ emitter = emitter.of(destinations[i].namespace);
464
+ }
465
+ let roomName = buildDomainEventChannelName(destinations[i].channel);
466
+ const fullEventName = core.buildDomainEventFullName(context.type, context.event);
467
+ emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
468
+ // @ts-ignore
469
+ .emit(fullEventName, {
470
+ ...context,
471
+ meta: {
472
+ roomName
473
+ }
474
+ });
475
+ if (context.event !== core.DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
476
+ roomName = buildDomainEventChannelName(destinations[i].channel, context.data.id);
477
+ emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
478
+ // @ts-ignore
479
+ .emit(fullEventName, {
480
+ ...context,
481
+ meta: {
482
+ roomName,
483
+ roomId: context.data.id
484
+ }
485
+ });
486
+ }
487
+ }
488
+ }
489
+
490
+ async function publishDomainEvent(context, destinations) {
491
+ await publishDomainRedisEvent(context, destinations);
492
+ publishDomainSocketEvent(context, destinations);
493
+ }
494
+
495
+ /*
496
+ * Copyright (c) 2022.
497
+ * Author Peter Placzek (tada5hi)
498
+ * For the full copyright and license information,
499
+ * view the LICENSE file that was distributed with this source code.
500
+ */ class VoidLogger {
501
+ error() {
502
+ return this;
503
+ }
504
+ warn() {
505
+ return this;
506
+ }
507
+ info() {
508
+ return this;
509
+ }
510
+ http() {
511
+ return this;
512
+ }
513
+ verbose() {
514
+ return this;
515
+ }
516
+ debug() {
517
+ return this;
518
+ }
519
+ }
520
+
521
+ let instance$2;
522
+ function useLogger() {
523
+ if (typeof instance$2 !== 'undefined') {
524
+ return instance$2;
525
+ }
526
+ instance$2 = new VoidLogger();
527
+ return instance$2;
528
+ }
529
+ function setLogger(logger) {
530
+ instance$2 = logger;
531
+ }
532
+
533
+ class ProxyClient {
534
+ /**
535
+ * Create a http agent for an url.
536
+ *
537
+ * @param input
538
+ */ async createAgent(input) {
539
+ return new Promise((resolve, reject)=>{
540
+ const headers = {};
541
+ if (this.options.user && this.options.password) {
542
+ headers['Proxy-Authorization'] = `Basic ${Buffer.from(`${this.options.user}:${this.options.password}`).toString('base64')}`;
543
+ }
544
+ const urlParsed = new URL(input);
545
+ const request = http.request({
546
+ host: this.options.host,
547
+ port: this.options.port,
548
+ method: 'CONNECT',
549
+ path: `${urlParsed.hostname}:443`,
550
+ headers
551
+ });
552
+ request.on('connect', (res, socket)=>{
553
+ if (res.statusCode >= 200 && res.statusCode < 300) {
554
+ resolve(new https.Agent({
555
+ socket,
556
+ ...this.agentOptions
557
+ }));
558
+ } else {
559
+ reject(new Error('Could not connect to proxy!'));
560
+ }
561
+ });
562
+ request.on('error', (err)=>{
563
+ reject(err);
564
+ });
565
+ request.on('timeout', (err)=>{
566
+ reject(err);
567
+ });
568
+ request.end();
569
+ });
570
+ }
571
+ constructor(options, agentOptions){
572
+ this.options = options;
573
+ this.agentOptions = smob.merge(agentOptions || {}, {
574
+ keepAlive: false
575
+ });
576
+ }
577
+ }
578
+
579
+ async function buildHTTPClientConfigForProxy(url) {
580
+ const connectionString = proxyFromEnv.getProxyForUrl(url);
581
+ if (connectionString) {
582
+ const connectionDetails = core.parseProxyConnectionString(connectionString);
583
+ const proxyClient = new ProxyClient({
584
+ host: connectionDetails.host,
585
+ port: connectionDetails.port || 3128,
586
+ ...connectionDetails.auth.username ? {
587
+ user: connectionDetails.auth.username
588
+ } : {},
589
+ ...connectionDetails.auth.password ? {
590
+ password: connectionDetails.auth.password
591
+ } : {}
592
+ });
593
+ const agent = await proxyClient.createAgent(url);
594
+ return {
595
+ agent
596
+ };
597
+ }
598
+ return {};
599
+ }
600
+
601
+ /*
602
+ * Copyright (c) 2022.
603
+ * Author Peter Placzek (tada5hi)
604
+ * For the full copyright and license information,
605
+ * view the LICENSE file that was distributed with this source code.
606
+ */ let instance$1;
607
+ function hasSmtpConfig() {
608
+ return !!instance$1;
609
+ }
610
+ function setSmtpConfig(value) {
611
+ instance$1 = value;
612
+ }
613
+ function useSmtpConfig() {
614
+ if (typeof instance$1 !== 'undefined') {
615
+ return instance$1;
616
+ }
617
+ instance$1 = {};
618
+ return instance$1;
619
+ }
620
+
621
+ function createSmtpClient(options) {
622
+ let transport;
623
+ options = options || {};
624
+ if (typeof options === 'string') {
625
+ transport = nodemailer.createTransport(options);
626
+ } else if (options.connectionString) {
627
+ transport = nodemailer.createTransport(options.connectionString);
628
+ } else {
629
+ let auth;
630
+ if (options.user && options.password) {
631
+ auth = {
632
+ type: 'login',
633
+ user: options.user,
634
+ pass: options.password
635
+ };
636
+ }
637
+ transport = nodemailer.createTransport({
638
+ host: options.host,
639
+ port: options.port,
640
+ auth,
641
+ secure: options.ssl,
642
+ opportunisticTLS: options.starttls,
643
+ tls: {
644
+ rejectUnauthorized: false
645
+ }
646
+ });
647
+ }
648
+ transport.on('error', (e)=>{
649
+ useLogger().error(e.message);
650
+ });
651
+ return transport;
652
+ }
653
+
654
+ let instance;
655
+ async function useSMTPClient() {
656
+ if (typeof instance !== 'undefined') {
657
+ return instance;
658
+ }
659
+ let options;
660
+ if (process.env.NODE_ENV === 'test') {
661
+ const testAccount = await nodemailer.createTestAccount();
662
+ options = {
663
+ host: 'smtp.ethereal.email',
664
+ port: 587,
665
+ ssl: false,
666
+ user: testAccount.user,
667
+ password: testAccount.pass
668
+ };
669
+ } else {
670
+ options = useSmtpConfig();
671
+ }
672
+ instance = createSmtpClient(options);
673
+ return instance;
674
+ }
675
+
676
+ /*
677
+ * Copyright (c) 2022.
678
+ * Author Peter Placzek (tada5hi)
679
+ * For the full copyright and license information,
680
+ * view the LICENSE file that was distributed with this source code.
681
+ */ function hasOwnProperty(obj, prop) {
682
+ return Object.prototype.hasOwnProperty.call(obj, prop);
683
+ }
684
+
685
+ exports.ProxyClient = ProxyClient;
686
+ exports.VoidLogger = VoidLogger;
687
+ exports.buildHTTPClientConfigForProxy = buildHTTPClientConfigForProxy;
688
+ exports.buildKeyFileName = buildKeyFileName;
689
+ exports.compare = compare;
690
+ exports.createKeyPair = createKeyPair;
691
+ exports.createSmtpClient = createSmtpClient;
692
+ exports.decodeToken = decodeToken;
693
+ exports.decryptRSAPrivateKey = decryptRSAPrivateKey;
694
+ exports.deleteKeyPair = deleteKeyPair;
695
+ exports.extendKeyPairOptions = extendKeyPairOptions;
696
+ exports.hasOwnProperty = hasOwnProperty;
697
+ exports.hasSmtpConfig = hasSmtpConfig;
698
+ exports.hash = hash;
699
+ exports.isKeyPair = isKeyPair;
700
+ exports.isKeyPairWithPublicKey = isKeyPairWithPublicKey;
701
+ exports.loadKeyPair = loadKeyPair;
702
+ exports.publishDomainEvent = publishDomainEvent;
703
+ exports.publishDomainRedisEvent = publishDomainRedisEvent;
704
+ exports.publishDomainSocketEvent = publishDomainSocketEvent;
705
+ exports.saveKeyPair = saveKeyPair;
706
+ exports.setLogger = setLogger;
707
+ exports.setSmtpConfig = setSmtpConfig;
708
+ exports.signToken = signToken;
709
+ exports.useKeyPair = useKeyPair;
710
+ exports.useLogger = useLogger;
711
+ exports.useSMTPClient = useSMTPClient;
712
+ exports.useSmtpConfig = useSmtpConfig;
713
+ exports.useSocketEmitter = useSocketEmitter;
714
+ exports.verifyToken = verifyToken;
715
+ //# sourceMappingURL=index.cjs.map