@authup/server-kit 1.0.0-beta.20 → 1.0.0-beta.22

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.
package/dist/index.cjs CHANGED
@@ -7,10 +7,11 @@ var path = require('node:path');
7
7
  var fs = require('node:fs');
8
8
  var jsonwebtoken = require('@node-rs/jsonwebtoken');
9
9
  var smob = require('smob');
10
+ var redisExtension = require('redis-extension');
11
+ var singa = require('singa');
12
+ var TTLCache = require('@isaacs/ttlcache');
10
13
  var process$1 = require('node:process');
11
14
  var winston = require('winston');
12
- var singa = require('singa');
13
- var redisExtension = require('redis-extension');
14
15
  var vault = require('@hapic/vault');
15
16
  var redisEmitter = require('@socket.io/redis-emitter');
16
17
 
@@ -46,11 +47,11 @@ async function hash(str, rounds = 10) {
46
47
  * Author Peter Placzek (tada5hi)
47
48
  * For the full copyright and license information,
48
49
  * view the LICENSE file that was distributed with this source code.
49
- */ exports.KeyPairKind = void 0;
50
- (function(KeyPairKind) {
50
+ */ var KeyPairKind = /*#__PURE__*/ function(KeyPairKind) {
51
51
  KeyPairKind["PRIVATE"] = "private";
52
52
  KeyPairKind["PUBLIC"] = "public";
53
- })(exports.KeyPairKind || (exports.KeyPairKind = {}));
53
+ return KeyPairKind;
54
+ }({});
54
55
 
55
56
  function isKeyPair(data) {
56
57
  return kit.isObject(data) && typeof data.privateKey !== 'undefined' && typeof data.publicKey !== 'undefined';
@@ -90,7 +91,7 @@ function buildKeyFileName(type, context) {
90
91
  const options = extendKeyPairOptions(context);
91
92
  const parts = [];
92
93
  switch(type){
93
- case exports.KeyPairKind.PRIVATE:
94
+ case KeyPairKind.PRIVATE:
94
95
  {
95
96
  if (options.privateName) {
96
97
  parts.push(options.privateName);
@@ -107,7 +108,7 @@ function buildKeyFileName(type, context) {
107
108
  }
108
109
  break;
109
110
  }
110
- case exports.KeyPairKind.PUBLIC:
111
+ case KeyPairKind.PUBLIC:
111
112
  {
112
113
  if (options.publicName) {
113
114
  parts.push(options.publicName);
@@ -187,11 +188,11 @@ async function saveKeyPair(keyPair, context) {
187
188
  });
188
189
  await Promise.all([
189
190
  {
190
- path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, context)),
191
+ path: path.resolve(context.directory, buildKeyFileName(KeyPairKind.PRIVATE, context)),
191
192
  content: keyPair.privateKey
192
193
  },
193
194
  {
194
- path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, context)),
195
+ path: path.resolve(context.directory, buildKeyFileName(KeyPairKind.PUBLIC, context)),
195
196
  content: keyPair.publicKey
196
197
  }
197
198
  ].map((file)=>fs.promises.writeFile(file.path, file.content)));
@@ -234,8 +235,8 @@ async function createKeyPair(context) {
234
235
 
235
236
  async function deleteKeyPair(context) {
236
237
  const options = extendKeyPairOptions(context);
237
- const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
238
- const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
238
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(KeyPairKind.PRIVATE, options));
239
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(KeyPairKind.PUBLIC, options));
239
240
  try {
240
241
  await Promise.all([
241
242
  privateKeyPath,
@@ -252,7 +253,7 @@ async function deleteKeyPair(context) {
252
253
 
253
254
  async function loadKeyPair(context) {
254
255
  const options = extendKeyPairOptions(context);
255
- const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
256
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(KeyPairKind.PRIVATE, options));
256
257
  try {
257
258
  await fs.promises.stat(privateKeyPath);
258
259
  } catch (e) {
@@ -263,7 +264,7 @@ async function loadKeyPair(context) {
263
264
  if (options.passphrase || options.privateKeyEncoding.passphrase) {
264
265
  privateKey = decryptRSAPrivateKey(options, privateKey);
265
266
  }
266
- const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
267
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(KeyPairKind.PUBLIC, options));
267
268
  let publicKey;
268
269
  try {
269
270
  await fs.promises.stat(publicKeyPath);
@@ -559,6 +560,152 @@ async function signToken(claims, context) {
559
560
  return output;
560
561
  }
561
562
 
563
+ const instance$3 = singa.singa({
564
+ name: 'redis'
565
+ });
566
+ function setRedisFactory(factory) {
567
+ instance$3.setFactory(factory);
568
+ }
569
+ function isRedisClientUsable() {
570
+ return instance$3.has() || instance$3.hasFactory();
571
+ }
572
+ function setRedisClient(input) {
573
+ instance$3.set(input);
574
+ }
575
+ function useRedisClient() {
576
+ return instance$3.use();
577
+ }
578
+
579
+ class MemoryCacheAdapter {
580
+ async get(key) {
581
+ return this.instance.get(key);
582
+ }
583
+ async set(key, value, options) {
584
+ this.instance.set(key, value, {
585
+ ttl: options.ttl
586
+ });
587
+ }
588
+ async drop(key) {
589
+ this.instance.delete(key);
590
+ }
591
+ async dropMany(keys) {
592
+ for(let i = 0; i < keys.length; i++){
593
+ this.instance.delete(keys[i]);
594
+ }
595
+ }
596
+ async clear(options = {}) {
597
+ if (options.prefix) {
598
+ const keys = this.instance.keys();
599
+ let iterator = keys.next();
600
+ while(!iterator.done){
601
+ if (iterator.value.startsWith(options.prefix)) {
602
+ this.instance.delete(iterator.value);
603
+ }
604
+ iterator = keys.next();
605
+ }
606
+ return;
607
+ }
608
+ this.instance.clear();
609
+ }
610
+ constructor(options = {}){
611
+ this.instance = new TTLCache({
612
+ checkAgeOnGet: true,
613
+ ttl: Infinity,
614
+ ...options || {}
615
+ });
616
+ }
617
+ }
618
+
619
+ class RedisCacheAdapter {
620
+ async get(key) {
621
+ return this.instance.get(key);
622
+ }
623
+ async set(key, value, options) {
624
+ await this.instance.set(key, value, {
625
+ milliseconds: options.ttl
626
+ });
627
+ }
628
+ async drop(key) {
629
+ await this.instance.drop(key);
630
+ }
631
+ async dropMany(keys) {
632
+ const pipeline = this.client.pipeline();
633
+ for(let i = 0; i < keys.length; i++){
634
+ pipeline.del(keys[i]);
635
+ }
636
+ await pipeline.exec();
637
+ }
638
+ async clear(options = {}) {
639
+ if (options.prefix) {
640
+ const pipeline = this.client.pipeline();
641
+ const keys = await this.client.keys(`${options.prefix}*`);
642
+ for(let i = 0; i < keys.length; i++){
643
+ pipeline.del(keys[i]);
644
+ }
645
+ await pipeline.exec();
646
+ return;
647
+ }
648
+ await this.client.flushdb();
649
+ }
650
+ constructor(){
651
+ this.client = useRedisClient();
652
+ this.instance = new redisExtension.JsonAdapter(this.client);
653
+ }
654
+ }
655
+
656
+ function createCacheAdapter() {
657
+ if (isRedisClientUsable()) {
658
+ return new RedisCacheAdapter();
659
+ }
660
+ return new MemoryCacheAdapter();
661
+ }
662
+
663
+ function buildCacheKey(options) {
664
+ return redisExtension.buildKeyPath(options);
665
+ }
666
+
667
+ /*
668
+ * Copyright (c) 2024.
669
+ * Author Peter Placzek (tada5hi)
670
+ * For the full copyright and license information,
671
+ * view the LICENSE file that was distributed with this source code.
672
+ */ class Cache {
673
+ async set(key, value, options = {}) {
674
+ await this.adapter.set(key, value, options);
675
+ }
676
+ async get(key) {
677
+ return this.adapter.get(key);
678
+ }
679
+ async drop(key) {
680
+ return this.adapter.drop(key);
681
+ }
682
+ async dropMany(keys) {
683
+ return this.adapter.dropMany(keys);
684
+ }
685
+ async clear(options = {}) {
686
+ return this.adapter.clear(options);
687
+ }
688
+ constructor(adapter){
689
+ this.adapter = adapter;
690
+ }
691
+ }
692
+
693
+ const instance$2 = singa.singa({
694
+ name: 'cache',
695
+ factory: ()=>{
696
+ let adapter;
697
+ if (isRedisClientUsable()) {
698
+ adapter = new RedisCacheAdapter();
699
+ } else {
700
+ adapter = new MemoryCacheAdapter();
701
+ }
702
+ return new Cache(adapter);
703
+ }
704
+ });
705
+ function useCache() {
706
+ return instance$2.use();
707
+ }
708
+
562
709
  function createLogger(context) {
563
710
  let items;
564
711
  const cwd = context.directory || process__namespace.cwd();
@@ -595,35 +742,19 @@ function createLogger(context) {
595
742
  });
596
743
  }
597
744
 
598
- const instance$2 = singa.singa({
745
+ const instance$1 = singa.singa({
599
746
  name: 'logger'
600
747
  });
601
748
  function setLoggerFactory(factory) {
602
- instance$2.setFactory(factory);
603
- }
604
- function isLoggerUsable() {
605
- return instance$2.has() || instance$2.hasFactory();
606
- }
607
- function setLogger(input) {
608
- instance$2.set(input);
609
- }
610
- function useLogger() {
611
- return instance$2.use();
612
- }
613
-
614
- const instance$1 = singa.singa({
615
- name: 'redis'
616
- });
617
- function setRedisFactory(factory) {
618
749
  instance$1.setFactory(factory);
619
750
  }
620
- function isRedisClientUsable() {
751
+ function isLoggerUsable() {
621
752
  return instance$1.has() || instance$1.hasFactory();
622
753
  }
623
- function setRedisClient(input) {
754
+ function setLogger(input) {
624
755
  instance$1.set(input);
625
756
  }
626
- function useRedisClient() {
757
+ function useLogger() {
627
758
  return instance$1.use();
628
759
  }
629
760
 
@@ -745,10 +876,6 @@ class DomainEventPublisher {
745
876
  return Object.prototype.hasOwnProperty.call(obj, prop);
746
877
  }
747
878
 
748
- Object.defineProperty(exports, "Logger", {
749
- enumerable: true,
750
- get: function () { return winston.Logger; }
751
- });
752
879
  Object.defineProperty(exports, "RedisClient", {
753
880
  enumerable: true,
754
881
  get: function () { return redisExtension.Client; }
@@ -781,6 +908,10 @@ Object.defineProperty(exports, "parseRedisKeyPath", {
781
908
  enumerable: true,
782
909
  get: function () { return redisExtension.parseKeyPath; }
783
910
  });
911
+ Object.defineProperty(exports, "Logger", {
912
+ enumerable: true,
913
+ get: function () { return winston.Logger; }
914
+ });
784
915
  Object.defineProperty(exports, "VaultClient", {
785
916
  enumerable: true,
786
917
  get: function () { return vault.VaultClient; }
@@ -789,11 +920,17 @@ Object.defineProperty(exports, "createVaultClient", {
789
920
  enumerable: true,
790
921
  get: function () { return vault.createClient; }
791
922
  });
923
+ exports.Cache = Cache;
792
924
  exports.DomainEventPublisher = DomainEventPublisher;
793
925
  exports.DomainEventRedisPublisher = DomainEventRedisPublisher;
794
926
  exports.DomainEventSocketPublisher = DomainEventSocketPublisher;
927
+ exports.KeyPairKind = KeyPairKind;
928
+ exports.MemoryCacheAdapter = MemoryCacheAdapter;
929
+ exports.RedisCacheAdapter = RedisCacheAdapter;
930
+ exports.buildCacheKey = buildCacheKey;
795
931
  exports.buildKeyFileName = buildKeyFileName;
796
932
  exports.compare = compare;
933
+ exports.createCacheAdapter = createCacheAdapter;
797
934
  exports.createKeyPair = createKeyPair;
798
935
  exports.createLogger = createLogger;
799
936
  exports.decryptRSAPrivateKey = decryptRSAPrivateKey;
@@ -818,6 +955,7 @@ exports.setVaultFactory = setVaultFactory;
818
955
  exports.signToken = signToken;
819
956
  exports.unwrapPrivateKeyPem = unwrapPrivateKeyPem;
820
957
  exports.unwrapPublicKeyPem = unwrapPublicKeyPem;
958
+ exports.useCache = useCache;
821
959
  exports.useKeyPair = useKeyPair;
822
960
  exports.useLogger = useLogger;
823
961
  exports.useRedisClient = useRedisClient;