ecwt 0.2.1-beta.2 → 0.2.1-beta.4
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/bun.lockb +0 -0
- package/dist/main.cjs +8 -26
- package/{types → dist/types}/factory.d.ts +2 -2
- package/package.json +10 -17
- package/src/factory.js +11 -41
- package/src/token.js +0 -1
- package/src/utils/errors.js +5 -1
- package/tsconfig.json +0 -19
- package/types/factory.test.d.ts +0 -1
- /package/{types → dist/types}/main.d.ts +0 -0
- /package/{types → dist/types}/token.d.ts +0 -0
- /package/{types → dist/types}/utils/base62.d.ts +0 -0
- /package/{types → dist/types}/utils/errors.d.ts +0 -0
- /package/{types → dist/types}/utils/time.d.ts +0 -0
package/bun.lockb
ADDED
|
Binary file
|
package/dist/main.cjs
CHANGED
|
@@ -44,7 +44,6 @@ var import_snowflake = require("@kirick/snowflake");
|
|
|
44
44
|
var import_cbor_x = require("cbor-x");
|
|
45
45
|
var import_evilcrypt = require("evilcrypt");
|
|
46
46
|
var import_lru_cache = require("lru-cache");
|
|
47
|
-
var import_redis = require("redis");
|
|
48
47
|
|
|
49
48
|
// src/utils/time.js
|
|
50
49
|
function toSeconds(value) {
|
|
@@ -151,7 +150,9 @@ var InvalidPackageInstanceError = class extends TypeError {
|
|
|
151
150
|
* @param {string} package_name -
|
|
152
151
|
*/
|
|
153
152
|
constructor(property, class_name, package_name) {
|
|
154
|
-
super(
|
|
153
|
+
super(
|
|
154
|
+
`Value ${property} must be an instance of ${class_name} from package "${package_name}". That error is probably caused by two separate installations of "${package_name}". Please, make sure that "${package_name}" in your project is matches "peerDependencies" of "ecwt" package.`
|
|
155
|
+
);
|
|
155
156
|
}
|
|
156
157
|
};
|
|
157
158
|
var EcwtParseError = class extends Error {
|
|
@@ -178,16 +179,6 @@ var EcwtRevokedError = class extends EcwtInvalidError {
|
|
|
178
179
|
|
|
179
180
|
// src/factory.js
|
|
180
181
|
var REDIS_PREFIX = "@ecwt:";
|
|
181
|
-
function getAllKeysList(value) {
|
|
182
|
-
const keys = [];
|
|
183
|
-
for (const key in value) {
|
|
184
|
-
keys.push(key);
|
|
185
|
-
}
|
|
186
|
-
return keys.sort().join(",");
|
|
187
|
-
}
|
|
188
|
-
var redisClient = (0, import_redis.createClient)();
|
|
189
|
-
var redis_client_constructor_name = redisClient.constructor.name;
|
|
190
|
-
var redis_client_keys = getAllKeysList(redisClient);
|
|
191
182
|
var EcwtFactory = class {
|
|
192
183
|
#redisClient;
|
|
193
184
|
#lruCache;
|
|
@@ -205,11 +196,11 @@ var EcwtFactory = class {
|
|
|
205
196
|
* @param {object} param0.options -
|
|
206
197
|
* @param {string} [param0.options.namespace] Namespace for Redis keys.
|
|
207
198
|
* @param {Buffer} param0.options.key Encryption key, 64 bytes
|
|
208
|
-
* @param {(value:
|
|
199
|
+
* @param {(value: unknown) => D} [param0.options.validator] Validator for token data. Should return validated value or throw an error.
|
|
209
200
|
* @param {Record<string, number>} [param0.options.senml_key_map] Payload object keys mapped for their SenML keys.
|
|
210
201
|
*/
|
|
211
202
|
constructor({
|
|
212
|
-
redisClient
|
|
203
|
+
redisClient,
|
|
213
204
|
lruCache,
|
|
214
205
|
snowflakeFactory,
|
|
215
206
|
options: {
|
|
@@ -219,14 +210,7 @@ var EcwtFactory = class {
|
|
|
219
210
|
senml_key_map
|
|
220
211
|
}
|
|
221
212
|
}) {
|
|
222
|
-
|
|
223
|
-
throw new InvalidPackageInstanceError(
|
|
224
|
-
"redisClient",
|
|
225
|
-
"Commander extends RedisClient",
|
|
226
|
-
"redis"
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
this.#redisClient = redisClient2;
|
|
213
|
+
this.#redisClient = redisClient;
|
|
230
214
|
if (lruCache !== void 0 && lruCache instanceof import_lru_cache.LRUCache !== true) {
|
|
231
215
|
throw new InvalidPackageInstanceError(
|
|
232
216
|
"lruCache",
|
|
@@ -341,9 +325,7 @@ var EcwtFactory = class {
|
|
|
341
325
|
throw new EcwtParseError();
|
|
342
326
|
}
|
|
343
327
|
const payload = this.#cborEncoder ? this.#cborEncoder.decode(token_raw) : (0, import_cbor_x.decode)(token_raw);
|
|
344
|
-
const [
|
|
345
|
-
snowflake_buffer
|
|
346
|
-
] = payload;
|
|
328
|
+
const [snowflake_buffer] = payload;
|
|
347
329
|
[
|
|
348
330
|
,
|
|
349
331
|
ttl_initial,
|
|
@@ -440,7 +422,7 @@ var EcwtFactory = class {
|
|
|
440
422
|
ttl_initial
|
|
441
423
|
}) {
|
|
442
424
|
if (this.#redisClient) {
|
|
443
|
-
ttl_initial
|
|
425
|
+
ttl_initial ??= Number.MAX_SAFE_INTEGER;
|
|
444
426
|
const ts_ms_expired = ts_ms_created + ttl_initial * 1e3;
|
|
445
427
|
if (ts_ms_expired > Date.now()) {
|
|
446
428
|
await this.#redisClient.MULTI().addCommand([
|
|
@@ -10,7 +10,7 @@ export class EcwtFactory<D extends Record<string, any> = Record<string, any>> {
|
|
|
10
10
|
* @param {object} param0.options -
|
|
11
11
|
* @param {string} [param0.options.namespace] Namespace for Redis keys.
|
|
12
12
|
* @param {Buffer} param0.options.key Encryption key, 64 bytes
|
|
13
|
-
* @param {(value:
|
|
13
|
+
* @param {(value: unknown) => D} [param0.options.validator] Validator for token data. Should return validated value or throw an error.
|
|
14
14
|
* @param {Record<string, number>} [param0.options.senml_key_map] Payload object keys mapped for their SenML keys.
|
|
15
15
|
*/
|
|
16
16
|
constructor({ redisClient, lruCache, snowflakeFactory, options: { namespace, key, validator, senml_key_map, }, }: {
|
|
@@ -20,7 +20,7 @@ export class EcwtFactory<D extends Record<string, any> = Record<string, any>> {
|
|
|
20
20
|
options: {
|
|
21
21
|
namespace?: string | undefined;
|
|
22
22
|
key: Buffer;
|
|
23
|
-
validator?: ((value:
|
|
23
|
+
validator?: ((value: unknown) => D) | undefined;
|
|
24
24
|
senml_key_map?: Record<string, number> | undefined;
|
|
25
25
|
};
|
|
26
26
|
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ecwt",
|
|
3
|
-
"version": "0.2.1-beta.
|
|
3
|
+
"version": "0.2.1-beta.4",
|
|
4
4
|
"description": "Encrypted CBOR-encoded Web Token",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/main.js",
|
|
7
|
-
"types": "types/main.d.ts",
|
|
7
|
+
"types": "dist/types/main.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"import": "./src/main.js",
|
|
@@ -25,28 +25,21 @@
|
|
|
25
25
|
"redis": "^4"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@types/node": "^
|
|
30
|
-
"eslint": "8.
|
|
31
|
-
"
|
|
32
|
-
"eslint-plugin-import": "2.27.5",
|
|
33
|
-
"eslint-plugin-jsdoc": "46.5.0",
|
|
34
|
-
"eslint-plugin-node": "11.1.0",
|
|
35
|
-
"eslint-plugin-promise": "6.1.1",
|
|
36
|
-
"eslint-plugin-unicorn": "47.0.0",
|
|
37
|
-
"valibot": "^0.33",
|
|
28
|
+
"@kirick/eslint-config": "^0.1.21",
|
|
29
|
+
"@types/node": "^22.2",
|
|
30
|
+
"eslint": "9.8.0",
|
|
31
|
+
"valibot": "^0.37",
|
|
38
32
|
"vitest": "^1.6"
|
|
39
33
|
},
|
|
40
34
|
"scripts": {
|
|
41
|
-
"test": "export REDIS_PORT=16274 ; bun run redis:up && npm run test:vitest && bun test --coverage ; bun run redis:down",
|
|
42
|
-
"test:vitest": "vitest run --no-file-parallelism",
|
|
43
35
|
"build": "bun run build:types && bun run build:cjs",
|
|
44
36
|
"build:cjs": "bunx esbuild --bundle --platform=node --format=cjs --packages=external --outfile=dist/main.cjs src/main.js",
|
|
45
|
-
"build:types": "bunx tsc --
|
|
37
|
+
"build:types": "rm -rf dist/esm dist/types ; bunx tsc --emitDeclarationOnly && rm dist/**/*.test.d.ts",
|
|
46
38
|
"lint": "eslint . && bunx tsc --skipLibCheck --noemit",
|
|
47
39
|
"publish:npm": "bun run build && bun run lint && bun run test && npm publish",
|
|
48
|
-
"redis:up": "docker ps | grep test-
|
|
49
|
-
"
|
|
40
|
+
"redis:up": "docker ps | grep test-redis >/dev/null || docker run --rm -d -p 16379:6379 --name test-redis redis:7-alpine",
|
|
41
|
+
"test": "bun run redis:up && npm run test:vitest && bun test --coverage",
|
|
42
|
+
"test:vitest": "vitest run --no-file-parallelism"
|
|
50
43
|
},
|
|
51
44
|
"repository": {
|
|
52
45
|
"type": "git",
|
package/src/factory.js
CHANGED
|
@@ -13,12 +13,13 @@ import { SnowflakeFactory } from '@kirick/snowflake';
|
|
|
13
13
|
import {
|
|
14
14
|
Encoder as CborEncoder,
|
|
15
15
|
encode as cborEncode,
|
|
16
|
-
decode as cborDecode
|
|
16
|
+
decode as cborDecode,
|
|
17
|
+
} from 'cbor-x';
|
|
17
18
|
import {
|
|
18
19
|
decrypt as evilcryptDecrypt,
|
|
19
|
-
v2 as evilcryptV2
|
|
20
|
+
v2 as evilcryptV2,
|
|
21
|
+
} from 'evilcrypt';
|
|
20
22
|
import { LRUCache } from 'lru-cache';
|
|
21
|
-
import { createClient } from 'redis';
|
|
22
23
|
import { Ecwt } from './token.js';
|
|
23
24
|
import { base62 } from './utils/base62.js';
|
|
24
25
|
import {
|
|
@@ -26,27 +27,11 @@ import {
|
|
|
26
27
|
EcwtInvalidError,
|
|
27
28
|
EcwtExpiredError,
|
|
28
29
|
EcwtRevokedError,
|
|
29
|
-
EcwtParseError
|
|
30
|
+
EcwtParseError,
|
|
31
|
+
} from './utils/errors.js';
|
|
30
32
|
|
|
31
33
|
const REDIS_PREFIX = '@ecwt:';
|
|
32
34
|
|
|
33
|
-
/**
|
|
34
|
-
* @param {object} value -
|
|
35
|
-
* @returns {string} -
|
|
36
|
-
*/
|
|
37
|
-
function getAllKeysList(value) {
|
|
38
|
-
const keys = [];
|
|
39
|
-
// eslint-disable-next-line guard-for-in
|
|
40
|
-
for (const key in value) {
|
|
41
|
-
keys.push(key);
|
|
42
|
-
}
|
|
43
|
-
return keys.sort().join(',');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const redisClient = createClient();
|
|
47
|
-
const redis_client_constructor_name = redisClient.constructor.name;
|
|
48
|
-
const redis_client_keys = getAllKeysList(redisClient);
|
|
49
|
-
|
|
50
35
|
/**
|
|
51
36
|
* @template {Record<string, any>} [D=Record<string, any>]
|
|
52
37
|
*/
|
|
@@ -54,10 +39,8 @@ export class EcwtFactory {
|
|
|
54
39
|
#redisClient;
|
|
55
40
|
#lruCache;
|
|
56
41
|
#snowflakeFactory;
|
|
57
|
-
|
|
58
42
|
#redis_key_revoked;
|
|
59
43
|
#encryption_key;
|
|
60
|
-
|
|
61
44
|
#validator;
|
|
62
45
|
/** @type {CborEncoder | null} */
|
|
63
46
|
#cborEncoder = null;
|
|
@@ -70,7 +53,7 @@ export class EcwtFactory {
|
|
|
70
53
|
* @param {object} param0.options -
|
|
71
54
|
* @param {string} [param0.options.namespace] Namespace for Redis keys.
|
|
72
55
|
* @param {Buffer} param0.options.key Encryption key, 64 bytes
|
|
73
|
-
* @param {(value:
|
|
56
|
+
* @param {(value: unknown) => D} [param0.options.validator] Validator for token data. Should return validated value or throw an error.
|
|
74
57
|
* @param {Record<string, number>} [param0.options.senml_key_map] Payload object keys mapped for their SenML keys.
|
|
75
58
|
*/
|
|
76
59
|
constructor({
|
|
@@ -84,19 +67,6 @@ export class EcwtFactory {
|
|
|
84
67
|
senml_key_map,
|
|
85
68
|
},
|
|
86
69
|
}) {
|
|
87
|
-
if (
|
|
88
|
-
redisClient !== undefined
|
|
89
|
-
&& (
|
|
90
|
-
redisClient.constructor.name !== redis_client_constructor_name
|
|
91
|
-
|| getAllKeysList(redisClient) !== redis_client_keys
|
|
92
|
-
)
|
|
93
|
-
) {
|
|
94
|
-
throw new InvalidPackageInstanceError(
|
|
95
|
-
'redisClient',
|
|
96
|
-
'Commander extends RedisClient',
|
|
97
|
-
'redis',
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
70
|
this.#redisClient = redisClient;
|
|
101
71
|
|
|
102
72
|
if (
|
|
@@ -109,6 +79,7 @@ export class EcwtFactory {
|
|
|
109
79
|
'lru-cache',
|
|
110
80
|
);
|
|
111
81
|
}
|
|
82
|
+
|
|
112
83
|
this.#lruCache = lruCache;
|
|
113
84
|
|
|
114
85
|
if (snowflakeFactory instanceof SnowflakeFactory !== true) {
|
|
@@ -118,6 +89,7 @@ export class EcwtFactory {
|
|
|
118
89
|
'@kirick/snowflake',
|
|
119
90
|
);
|
|
120
91
|
}
|
|
92
|
+
|
|
121
93
|
this.#snowflakeFactory = snowflakeFactory;
|
|
122
94
|
|
|
123
95
|
this.#redis_key_revoked = `${REDIS_PREFIX}${namespace}:revoked`;
|
|
@@ -251,9 +223,7 @@ export class EcwtFactory {
|
|
|
251
223
|
? this.#cborEncoder.decode(token_raw)
|
|
252
224
|
: cborDecode(token_raw);
|
|
253
225
|
|
|
254
|
-
const [
|
|
255
|
-
snowflake_buffer,
|
|
256
|
-
] = payload;
|
|
226
|
+
const [ snowflake_buffer ] = payload;
|
|
257
227
|
[
|
|
258
228
|
,
|
|
259
229
|
ttl_initial,
|
|
@@ -373,7 +343,7 @@ export class EcwtFactory {
|
|
|
373
343
|
ttl_initial,
|
|
374
344
|
}) {
|
|
375
345
|
if (this.#redisClient) {
|
|
376
|
-
ttl_initial
|
|
346
|
+
ttl_initial ??= Number.MAX_SAFE_INTEGER;
|
|
377
347
|
|
|
378
348
|
const ts_ms_expired = ts_ms_created + (ttl_initial * 1000);
|
|
379
349
|
if (ts_ms_expired > Date.now()) {
|
package/src/token.js
CHANGED
package/src/utils/errors.js
CHANGED
|
@@ -10,7 +10,11 @@ export class InvalidPackageInstanceError extends TypeError {
|
|
|
10
10
|
* @param {string} package_name -
|
|
11
11
|
*/
|
|
12
12
|
constructor(property, class_name, package_name) {
|
|
13
|
-
super(
|
|
13
|
+
super(
|
|
14
|
+
`Value ${property} must be an instance of ${class_name} from package "${package_name}".`
|
|
15
|
+
+ ` That error is probably caused by two separate installations of "${package_name}".`
|
|
16
|
+
+ ` Please, make sure that "${package_name}" in your project is matches "peerDependencies" of "ecwt" package.`,
|
|
17
|
+
);
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
20
|
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "./dist/",
|
|
4
|
-
"target": "esnext",
|
|
5
|
-
"module": "esnext",
|
|
6
|
-
"checkJs": true,
|
|
7
|
-
"allowJs": true,
|
|
8
|
-
"moduleResolution": "node",
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
|
-
"strict": true
|
|
11
|
-
},
|
|
12
|
-
"include": [
|
|
13
|
-
"src/**/*"
|
|
14
|
-
],
|
|
15
|
-
"exclude": [
|
|
16
|
-
"node_modules/",
|
|
17
|
-
"dist/"
|
|
18
|
-
]
|
|
19
|
-
}
|
package/types/factory.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|