ecwt 0.2.1-beta.1 → 0.2.1-beta.2

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/main.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -78,21 +79,21 @@ var Ecwt = class {
78
79
  ts_expired;
79
80
  /**
80
81
  * Data stored in token.
81
- * @type {{ [key: string]: any }}
82
+ * @type {D}
82
83
  * @readonly
83
84
  */
84
85
  data;
85
86
  /** @type {EcwtFactory} */
86
87
  #ecwtFactory;
87
- /** @type {number} */
88
+ /** @type {number | null} */
88
89
  #ttl_initial;
89
90
  /**
90
91
  * @param {EcwtFactory} ecwtFactory -
91
92
  * @param {object} options -
92
93
  * @param {string} options.token String representation of token.
93
94
  * @param {Snowflake} options.snowflake -
94
- * @param {number?} options.ttl_initial Time to live in seconds at the moment of token creation.
95
- * @param {object} options.data Data stored in token.
95
+ * @param {number | null} options.ttl_initial Time to live in seconds at the moment of token creation.
96
+ * @param {D} options.data Data stored in token.
96
97
  */
97
98
  constructor(ecwtFactory, {
98
99
  token,
@@ -197,9 +198,8 @@ var EcwtFactory = class {
197
198
  /** @type {CborEncoder | null} */
198
199
  #cborEncoder = null;
199
200
  /**
200
- *
201
201
  * @param {object} param0 -
202
- * @param {import('redis').RedisClientType} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
202
+ * @param {import('redis').RedisClientType<import('redis').RedisModules, import('redis').RedisFunctions, import('redis').RedisScripts>} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
203
203
  * @param {LRUCache<string, CacheValue>} [param0.lruCache] LRUCache instance. If not provided, tokens will be decrypted every time they are verified.
204
204
  * @param {SnowflakeFactory} param0.snowflakeFactory SnowflakeFactory instance.
205
205
  * @param {object} param0.options -
@@ -255,7 +255,7 @@ var EcwtFactory = class {
255
255
  /**
256
256
  * Creates new token.
257
257
  * @async
258
- * @param {object} data Data to be stored in token.
258
+ * @param {D} data Data to be stored in token.
259
259
  * @param {object} [options] -
260
260
  * @param {number | null} [options.ttl] Time to live in seconds. By default, token will never expire.
261
261
  * @returns {Promise<Ecwt>} -
@@ -308,7 +308,7 @@ var EcwtFactory = class {
308
308
  this.#lruCache?.set(
309
309
  token,
310
310
  cache_value,
311
- {
311
+ cache_value.ttl_initial === null ? void 0 : {
312
312
  ttl: cache_value.ttl_initial * 1e3
313
313
  }
314
314
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecwt",
3
- "version": "0.2.1-beta.1",
3
+ "version": "0.2.1-beta.2",
4
4
  "description": "Encrypted CBOR-encoded Web Token",
5
5
  "type": "module",
6
6
  "main": "src/main.js",
package/src/factory.js CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * @typedef {object} CacheValue
7
7
  * @property {Snowflake} snowflake -
8
- * @property {number} ttl_initial -
8
+ * @property {number | null} ttl_initial -
9
9
  * @property {Record<string, any>} data -
10
10
  */
11
11
 
@@ -47,6 +47,9 @@ const redisClient = createClient();
47
47
  const redis_client_constructor_name = redisClient.constructor.name;
48
48
  const redis_client_keys = getAllKeysList(redisClient);
49
49
 
50
+ /**
51
+ * @template {Record<string, any>} [D=Record<string, any>]
52
+ */
50
53
  export class EcwtFactory {
51
54
  #redisClient;
52
55
  #lruCache;
@@ -60,9 +63,8 @@ export class EcwtFactory {
60
63
  #cborEncoder = null;
61
64
 
62
65
  /**
63
- *
64
66
  * @param {object} param0 -
65
- * @param {import('redis').RedisClientType} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
67
+ * @param {import('redis').RedisClientType<import('redis').RedisModules, import('redis').RedisFunctions, import('redis').RedisScripts>} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
66
68
  * @param {LRUCache<string, CacheValue>} [param0.lruCache] LRUCache instance. If not provided, tokens will be decrypted every time they are verified.
67
69
  * @param {SnowflakeFactory} param0.snowflakeFactory SnowflakeFactory instance.
68
70
  * @param {object} param0.options -
@@ -134,7 +136,7 @@ export class EcwtFactory {
134
136
  /**
135
137
  * Creates new token.
136
138
  * @async
137
- * @param {object} data Data to be stored in token.
139
+ * @param {D} data Data to be stored in token.
138
140
  * @param {object} [options] -
139
141
  * @param {number | null} [options.ttl] Time to live in seconds. By default, token will never expire.
140
142
  * @returns {Promise<Ecwt>} -
@@ -204,9 +206,11 @@ export class EcwtFactory {
204
206
  this.#lruCache?.set(
205
207
  token,
206
208
  cache_value,
207
- {
208
- ttl: cache_value.ttl_initial * 1000,
209
- },
209
+ cache_value.ttl_initial === null
210
+ ? undefined
211
+ : {
212
+ ttl: cache_value.ttl_initial * 1000,
213
+ },
210
214
  );
211
215
  }
212
216
 
package/src/token.js CHANGED
@@ -6,6 +6,9 @@ import { toSeconds } from './utils/time.js';
6
6
  * @typedef {import('./factory.js').EcwtFactory} EcwtFactory
7
7
  */
8
8
 
9
+ /**
10
+ * @template {Record<string, any>} [D=Record<string, any>]
11
+ */
9
12
  export class Ecwt {
10
13
  /**
11
14
  * Token string representation.
@@ -33,14 +36,14 @@ export class Ecwt {
33
36
  ts_expired;
34
37
  /**
35
38
  * Data stored in token.
36
- * @type {{ [key: string]: any }}
39
+ * @type {D}
37
40
  * @readonly
38
41
  */
39
42
  data;
40
43
 
41
44
  /** @type {EcwtFactory} */
42
45
  #ecwtFactory;
43
- /** @type {number} */
46
+ /** @type {number | null} */
44
47
  #ttl_initial;
45
48
 
46
49
  /**
@@ -48,8 +51,8 @@ export class Ecwt {
48
51
  * @param {object} options -
49
52
  * @param {string} options.token String representation of token.
50
53
  * @param {Snowflake} options.snowflake -
51
- * @param {number?} options.ttl_initial Time to live in seconds at the moment of token creation.
52
- * @param {object} options.data Data stored in token.
54
+ * @param {number | null} options.ttl_initial Time to live in seconds at the moment of token creation.
55
+ * @param {D} options.data Data stored in token.
53
56
  */
54
57
  constructor(
55
58
  ecwtFactory,
package/tsconfig.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "outDir": "./dist/",
3
4
  "target": "esnext",
4
5
  "module": "esnext",
5
- "noImplicitAny": true,
6
6
  "checkJs": true,
7
7
  "allowJs": true,
8
8
  "moduleResolution": "node",
9
- "allowSyntheticDefaultImports": true
9
+ "allowSyntheticDefaultImports": true,
10
+ "strict": true
10
11
  },
11
12
  "include": [
12
13
  "src/**/*"
13
14
  ],
14
15
  "exclude": [
15
16
  "node_modules/",
16
- "dist/",
17
- "**/*.test.js"
17
+ "dist/"
18
18
  ]
19
19
  }
@@ -1,8 +1,10 @@
1
- export class EcwtFactory {
1
+ /**
2
+ * @template {Record<string, any>} [D=Record<string, any>]
3
+ */
4
+ export class EcwtFactory<D extends Record<string, any> = Record<string, any>> {
2
5
  /**
3
- *
4
6
  * @param {object} param0 -
5
- * @param {import('redis').RedisClientType} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
7
+ * @param {import('redis').RedisClientType<import('redis').RedisModules, import('redis').RedisFunctions, import('redis').RedisScripts>} [param0.redisClient] RedisClient instance. If not provided, tokens will not be revoked and cannot be checked for revocation.
6
8
  * @param {LRUCache<string, CacheValue>} [param0.lruCache] LRUCache instance. If not provided, tokens will be decrypted every time they are verified.
7
9
  * @param {SnowflakeFactory} param0.snowflakeFactory SnowflakeFactory instance.
8
10
  * @param {object} param0.options -
@@ -12,27 +14,27 @@ export class EcwtFactory {
12
14
  * @param {Record<string, number>} [param0.options.senml_key_map] Payload object keys mapped for their SenML keys.
13
15
  */
14
16
  constructor({ redisClient, lruCache, snowflakeFactory, options: { namespace, key, validator, senml_key_map, }, }: {
15
- redisClient?: import("redis").RedisClientType;
16
- lruCache?: LRUCache<string, CacheValue>;
17
+ redisClient?: import("redis").RedisClientType<import("redis").RedisModules, import("redis").RedisFunctions, import("redis").RedisScripts> | undefined;
18
+ lruCache?: LRUCache<string, CacheValue, any> | undefined;
17
19
  snowflakeFactory: SnowflakeFactory;
18
20
  options: {
19
- namespace?: string;
21
+ namespace?: string | undefined;
20
22
  key: Buffer;
21
- validator?: (value: any) => any;
22
- senml_key_map?: Record<string, number>;
23
+ validator?: ((value: any) => any) | undefined;
24
+ senml_key_map?: Record<string, number> | undefined;
23
25
  };
24
26
  });
25
27
  /**
26
28
  * Creates new token.
27
29
  * @async
28
- * @param {object} data Data to be stored in token.
30
+ * @param {D} data Data to be stored in token.
29
31
  * @param {object} [options] -
30
32
  * @param {number | null} [options.ttl] Time to live in seconds. By default, token will never expire.
31
33
  * @returns {Promise<Ecwt>} -
32
34
  */
33
- create(data: object, { ttl, }?: {
34
- ttl?: number | null;
35
- }): Promise<Ecwt>;
35
+ create(data: D, { ttl, }?: {
36
+ ttl?: number | null | undefined;
37
+ } | undefined): Promise<Ecwt>;
36
38
  /**
37
39
  * Parses token.
38
40
  * @async
@@ -84,7 +86,7 @@ export type CacheValue = {
84
86
  /**
85
87
  * -
86
88
  */
87
- ttl_initial: number;
89
+ ttl_initial: number | null;
88
90
  /**
89
91
  * -
90
92
  */
@@ -0,0 +1 @@
1
+ export {};
package/types/token.d.ts CHANGED
@@ -2,20 +2,23 @@
2
2
  * @typedef {import('@kirick/snowflake').Snowflake} Snowflake
3
3
  * @typedef {import('./factory.js').EcwtFactory} EcwtFactory
4
4
  */
5
- export class Ecwt {
5
+ /**
6
+ * @template {Record<string, any>} [D=Record<string, any>]
7
+ */
8
+ export class Ecwt<D extends Record<string, any> = Record<string, any>> {
6
9
  /**
7
10
  * @param {EcwtFactory} ecwtFactory -
8
11
  * @param {object} options -
9
12
  * @param {string} options.token String representation of token.
10
13
  * @param {Snowflake} options.snowflake -
11
- * @param {number?} options.ttl_initial Time to live in seconds at the moment of token creation.
12
- * @param {object} options.data Data stored in token.
14
+ * @param {number | null} options.ttl_initial Time to live in seconds at the moment of token creation.
15
+ * @param {D} options.data Data stored in token.
13
16
  */
14
17
  constructor(ecwtFactory: EcwtFactory, { token, snowflake, ttl_initial, data, }: {
15
18
  token: string;
16
19
  snowflake: Snowflake;
17
20
  ttl_initial: number | null;
18
- data: object;
21
+ data: D;
19
22
  });
20
23
  /**
21
24
  * Token string representation.
@@ -43,12 +46,10 @@ export class Ecwt {
43
46
  readonly ts_expired: number | null;
44
47
  /**
45
48
  * Data stored in token.
46
- * @type {{ [key: string]: any }}
49
+ * @type {D}
47
50
  * @readonly
48
51
  */
49
- readonly data: {
50
- [key: string]: any;
51
- };
52
+ readonly data: D;
52
53
  /**
53
54
  * Actual time to live in seconds.
54
55
  * @returns {number | null} -
@@ -23,7 +23,7 @@ export class EcwtInvalidError extends Error {
23
23
  * @param {Ecwt} ecwt -
24
24
  */
25
25
  constructor(ecwt: Ecwt);
26
- ecwt: import("../token.js").Ecwt;
26
+ ecwt: import("../token.js").Ecwt<Record<string, any>>;
27
27
  }
28
28
  /**
29
29
  * Error thrown when parsed Ecwt is expired.