@hkdigital/lib-core 0.4.13 → 0.4.15

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.
@@ -1,6 +1,38 @@
1
1
  /**
2
- * Casts jsonwebtoken library errors to internal error types
3
- * @param {Error} error - The original jsonwebtoken error
4
- * @returns {Error} - The corresponding internal error
2
+ * Create a JSON Web Token (JWT)
3
+ * - Stringifies the claims as JSON object
4
+ * - Encodes the options
5
+ * - Calculates a Message Authentication Code (MAC)
6
+ * (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
7
+ * - Combines the parts into a JWT string
8
+ *
9
+ * @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
10
+ * @param {import('./typedef.js').Secret} secretOrPrivateKey
11
+ * Secret or private key that is used by the MAC calculation algorithm
12
+ *
13
+ * - To generate a secret for a Hash based Authentication Code (HMAC):
14
+ * use a function like `generateSecretKeyForHmacBase58()`.
15
+ *
16
+ * - For algorithms that use asymmetric keys, the secret is the private key
17
+ * of the key pair.
18
+ *
19
+ * @param {import('./typedef.js').SignOptions} [options] - JWT signing options
20
+ *
21
+ * For more options:
22
+ * @see https://github.com/auth0/node-jsonwebtoken
23
+ *
24
+ * @returns {string} JsonWebToken
5
25
  */
6
- export function castJwtError(error: Error): Error;
26
+ export function sign(claims: import("./typedef.js").JwtPayload, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").SignOptions): string;
27
+ /**
28
+ * Decode and verify a JWT token
29
+ * - Forces the use of the algorithm specified in VERIFY_OPTIONS
30
+ *
31
+ * @param {string} token - A JWT token
32
+ * @param {import('./typedef.js').Secret} secretOrPrivateKey
33
+ * The secret of private key to be used for decoding
34
+ * @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
35
+ *
36
+ * @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
37
+ */
38
+ export function verify(token: string, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").VerifyOptions): import("./typedef.js").JwtPayload;
@@ -2,14 +2,26 @@
2
2
  * JWT utility functions
3
3
  *
4
4
  * @description
5
- * This module provides utility functions for JWT operations including error casting.
5
+ * This module provides utility functions for JWT operations including
6
+ * sign, verify and error casting.
6
7
  */
7
8
 
8
- import jwt, {
9
+ import jwt from 'jsonwebtoken';
10
+
11
+ import {
9
12
  TokenExpiredError as JwtTokenExpiredError,
10
13
  JsonWebTokenError as JwtJsonWebTokenError,
11
14
  NotBeforeError as JwtNotBeforeError
12
15
  } from 'jsonwebtoken';
16
+
17
+ import * as expect from '../../util/expect.js';
18
+
19
+ import {
20
+ JWT_DEFAULT_EXPIRES_IN,
21
+ DEFAULT_ALGORITHM,
22
+ VERIFY_OPTIONS
23
+ } from './constants.js';
24
+
13
25
  import {
14
26
  TokenExpiredError,
15
27
  JsonWebTokenError,
@@ -17,12 +29,109 @@ import {
17
29
  NotBeforeError
18
30
  } from './errors.js';
19
31
 
32
+ /**
33
+ * Create a JSON Web Token (JWT)
34
+ * - Stringifies the claims as JSON object
35
+ * - Encodes the options
36
+ * - Calculates a Message Authentication Code (MAC)
37
+ * (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
38
+ * - Combines the parts into a JWT string
39
+ *
40
+ * @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
41
+ * @param {import('./typedef.js').Secret} secretOrPrivateKey
42
+ * Secret or private key that is used by the MAC calculation algorithm
43
+ *
44
+ * - To generate a secret for a Hash based Authentication Code (HMAC):
45
+ * use a function like `generateSecretKeyForHmacBase58()`.
46
+ *
47
+ * - For algorithms that use asymmetric keys, the secret is the private key
48
+ * of the key pair.
49
+ *
50
+ * @param {import('./typedef.js').SignOptions} [options] - JWT signing options
51
+ *
52
+ * For more options:
53
+ * @see https://github.com/auth0/node-jsonwebtoken
54
+ *
55
+ * @returns {string} JsonWebToken
56
+ */
57
+ export function sign(
58
+ claims,
59
+ secretOrPrivateKey,
60
+ options={} )
61
+ {
62
+ expect.object( claims );
63
+ expect.defined( secretOrPrivateKey );
64
+
65
+ if( options )
66
+ {
67
+ expect.object( options );
68
+ }
69
+ else {
70
+ options = {};
71
+ }
72
+
73
+ if( !('algorithm' in options) )
74
+ {
75
+ options.algorithm = DEFAULT_ALGORITHM;
76
+ }
77
+
78
+ if( !('expiresIn' in options) )
79
+ {
80
+ options.expiresIn = JWT_DEFAULT_EXPIRES_IN;
81
+ }
82
+ else if( !options.expiresIn )
83
+ {
84
+ delete options.expiresIn;
85
+ }
86
+
87
+ // @ts-ignore
88
+ return jwt.sign( claims, secretOrPrivateKey, options );
89
+ }
90
+
91
+ /**
92
+ * Decode and verify a JWT token
93
+ * - Forces the use of the algorithm specified in VERIFY_OPTIONS
94
+ *
95
+ * @param {string} token - A JWT token
96
+ * @param {import('./typedef.js').Secret} secretOrPrivateKey
97
+ * The secret of private key to be used for decoding
98
+ * @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
99
+ *
100
+ * @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
101
+ */
102
+ export function verify( token, secretOrPrivateKey, options=VERIFY_OPTIONS )
103
+ {
104
+ expect.notEmptyString( token );
105
+ expect.defined( secretOrPrivateKey );
106
+
107
+ if( !('algorithms' in options) )
108
+ {
109
+ options.algorithms = VERIFY_OPTIONS.algorithms;
110
+ }
111
+
112
+ try {
113
+ // @ts-ignore
114
+ const decoded = jwt.verify( token, secretOrPrivateKey, options );
115
+
116
+ return decoded;
117
+ }
118
+ catch( e )
119
+ {
120
+ //
121
+ // Cast internal jsonwebtoken errors to Error types defined in this lib
122
+ //
123
+ throw castJwtError(e);
124
+ }
125
+ }
126
+
127
+ // Internals
128
+
20
129
  /**
21
130
  * Casts jsonwebtoken library errors to internal error types
22
131
  * @param {Error} error - The original jsonwebtoken error
23
132
  * @returns {Error} - The corresponding internal error
24
133
  */
25
- export function castJwtError(error) {
134
+ function castJwtError(error) {
26
135
  if (error instanceof JwtTokenExpiredError) {
27
136
  return new TokenExpiredError(error.message, error.expiredAt, error);
28
137
  }
@@ -40,4 +149,4 @@ export function castJwtError(error) {
40
149
 
41
150
  // Return original error if not a known JWT error
42
151
  return error;
43
- }
152
+ }
@@ -1,4 +1,4 @@
1
- export * from "./jwt/core.js";
1
+ export * from "./jwt/util.js";
2
2
  export * from "./jwt/generators.js";
3
3
  export * from "./jwt/errors.js";
4
4
  export * from "./jwt/constants.js";
package/dist/auth/jwt.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * verifying tokens, and generating secret keys.
7
7
  */
8
8
 
9
- export * from './jwt/core.js';
9
+ export * from './jwt/util.js';
10
10
  export * from './jwt/generators.js';
11
11
  export * from './jwt/errors.js';
12
12
  export * from './jwt/constants.js';
@@ -0,0 +1 @@
1
+ export * from "./bases/index.js";
@@ -0,0 +1 @@
1
+ export * from './bases/index.js';
@@ -0,0 +1,2 @@
1
+ export * from "./http/headers.js";
2
+ export * from "./http/methods.js";
@@ -0,0 +1,2 @@
1
+ export * from './http/headers.js';
2
+ export * from './http/methods.js';
@@ -0,0 +1,4 @@
1
+ export * from "./mime/application.js";
2
+ export * from "./mime/audio.js";
3
+ export * from "./mime/text.js";
4
+ export * from "./mime/image.js";
@@ -0,0 +1,4 @@
1
+ export * from './mime/application.js';
2
+ export * from './mime/audio.js';
3
+ export * from './mime/text.js';
4
+ export * from './mime/image.js';
@@ -0,0 +1,3 @@
1
+ export * from "./regexp/text.js";
2
+ export * from "./regexp/url.js";
3
+ export * from "./regexp/user.js";
@@ -0,0 +1,3 @@
1
+ export * from './regexp/text.js';
2
+ export * from './regexp/url.js';
3
+ export * from './regexp/user.js';
@@ -1,4 +1,4 @@
1
- export const IDLE: "idle";
1
+ export const DRAG_IDLE: "idle";
2
2
  export const DRAGGING: "dragging";
3
3
  export const DRAG_PREVIEW: "drag-preview";
4
4
  export const DROPPING: "dropping";
@@ -1,5 +1,5 @@
1
1
  // Draggable states
2
- export const IDLE = 'idle'; // Not being dragged
2
+ export const DRAG_IDLE = 'idle'; // Not being dragged
3
3
  export const DRAGGING = 'dragging'; // Currently being dragged
4
4
  export const DRAG_PREVIEW = 'drag-preview'; // Mouse down, before drag
5
5
  export const DROPPING = 'dropping'; // Just dropped, animating
@@ -1,4 +1,4 @@
1
- export const IDLE: "idle";
1
+ export const SUBMIT_IDLE: "idle";
2
2
  export const SUBMITTING: "submitting";
3
3
  export const SUBMIT_OK: "submit-ok";
4
4
  export const SUBMIT_FAILED: "submit-failed";
@@ -1,4 +1,4 @@
1
- export const IDLE = 'idle';
1
+ export const SUBMIT_IDLE = 'idle';
2
2
  export const SUBMITTING = 'submitting';
3
3
  export const SUBMIT_OK = 'submit-ok';
4
4
  export const SUBMIT_FAILED = 'submit-failed';
@@ -0,0 +1,4 @@
1
+ export * from "./states/drag.js";
2
+ export * from "./states/drop.js";
3
+ export * from "./states/input.js";
4
+ export * from "./states/submit.js";
@@ -0,0 +1,4 @@
1
+ export * from './states/drag.js';
2
+ export * from './states/drop.js';
3
+ export * from './states/input.js';
4
+ export * from './states/submit.js';
@@ -0,0 +1 @@
1
+ export * from "./time/index.js";
@@ -0,0 +1 @@
1
+ export * from './time/index.js';
@@ -1,9 +1,9 @@
1
1
  import * as expect from '../../util/expect.js';
2
2
  import { DetailedError } from '../../generic/errors.js';
3
3
 
4
- import { CONTENT_TYPE } from '../../constants/http/index.js';
4
+ import { CONTENT_TYPE } from '../../constants/http.js';
5
5
 
6
- import { APPLICATION_JSON, TEXT_PLAIN } from '../../constants/mime/index.js';
6
+ import { APPLICATION_JSON, TEXT_PLAIN } from '../../constants/mime.js';
7
7
 
8
8
  /**
9
9
  * Try to get error information from the server error response
@@ -17,6 +17,6 @@ export * from './response.js';
17
17
  export * from './http-request.js';
18
18
  export * from './json-request.js';
19
19
 
20
- // import { CONTENT_TYPE, METHOD_GET, METHOD_POST } from '../../constants/http/index.js';
20
+ // import { CONTENT_TYPE, METHOD_GET, METHOD_POST } from '../../constants/http.js';
21
21
 
22
- // import { APPLICATION_JSON } from '../../constants/mime/index.js';
22
+ // import { APPLICATION_JSON } from '../../constants/mime.js';
@@ -1,6 +1,6 @@
1
- import { CONTENT_TYPE, CONTENT_LENGTH } from '../../constants/http/index.js';
1
+ import { CONTENT_TYPE, CONTENT_LENGTH } from '../../constants/http.js';
2
2
 
3
- import { OCTET_STREAM } from '../../constants/mime/index.js';
3
+ import { OCTET_STREAM } from '../../constants/mime.js';
4
4
 
5
5
  /**
6
6
  * Create a response value that can be used by a mocked fetch function
@@ -1,4 +1,4 @@
1
- import { CONTENT_TYPE, CONTENT_LENGTH } from '../../../constants/http/index.js';
1
+ import { CONTENT_TYPE, CONTENT_LENGTH } from '../../../constants/http.js';
2
2
 
3
3
  import { AUDIO_WAV } from '../../../constants/mime/audio.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { CONTENT_TYPE, CONTENT_LENGTH } from '../../../constants/http/index.js';
1
+ import { CONTENT_TYPE, CONTENT_LENGTH } from '../../../constants/http.js';
2
2
 
3
3
  import { IMAGE_PNG } from '../../../constants/mime/image.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { CONTENT_TYPE } from '../../constants/http/index.js';
1
+ import { CONTENT_TYPE } from '../../constants/http.js';
2
2
 
3
3
  import { LoadingStateMachine } from '../../state/classes.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { CONTENT_TYPE, CONTENT_LENGTH } from '../../constants/http/index.js';
1
+ import { CONTENT_TYPE, CONTENT_LENGTH } from '../../constants/http.js';
2
2
 
3
3
  import { OCTET_STREAM } from '../../constants/mime/application.js';
4
4
 
@@ -6,7 +6,7 @@
6
6
  import { DragController } from './DragController.js';
7
7
  import { onDestroy } from 'svelte';
8
8
  import {
9
- IDLE,
9
+ DRAG_IDLE,
10
10
  DRAGGING,
11
11
  DRAG_PREVIEW,
12
12
  DROPPING
@@ -91,7 +91,7 @@
91
91
  let draggableElement;
92
92
 
93
93
  let dragTimeout = null;
94
- let currentState = $state(IDLE);
94
+ let currentState = $state(DRAG_IDLE);
95
95
 
96
96
  // Custom preview follower state
97
97
  let showPreview = $state(false);
@@ -112,7 +112,7 @@
112
112
 
113
113
  // Computed state object for CSS classes
114
114
  let stateObject = $derived({
115
- idle: currentState === IDLE,
115
+ idle: currentState === DRAG_IDLE,
116
116
  dragging: currentState === DRAGGING,
117
117
  'drag-preview': currentState === DRAG_PREVIEW,
118
118
  dropping: currentState === DROPPING,
@@ -295,10 +295,10 @@
295
295
 
296
296
  // Brief dropping state before returning to idle
297
297
  setTimeout(() => {
298
- currentState = IDLE;
298
+ currentState = DRAG_IDLE;
299
299
  }, 100);
300
300
  } else {
301
- currentState = IDLE;
301
+ currentState = DRAG_IDLE;
302
302
  }
303
303
 
304
304
  onDragEnd?.({ event, item, wasDropped });
@@ -321,7 +321,7 @@
321
321
  function handleMouseUp(event) {
322
322
  if (dragTimeout) {
323
323
  clearTimeout(dragTimeout);
324
- currentState = IDLE;
324
+ currentState = DRAG_IDLE;
325
325
  }
326
326
  }
327
327
 
@@ -456,7 +456,7 @@
456
456
 
457
457
  // Clean up
458
458
  touchDragging = false;
459
- currentState = IDLE;
459
+ currentState = DRAG_IDLE;
460
460
  showPreview = false;
461
461
  dragState.end(draggableId);
462
462
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.4.13",
3
+ "version": "0.4.15",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"
@@ -1,38 +0,0 @@
1
- /**
2
- * Create a JSON Web Token (JWT)
3
- * - Stringifies the claims as JSON object
4
- * - Encodes the options
5
- * - Calculates a Message Authentication Code (MAC)
6
- * (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
7
- * - Combines the parts into a JWT string
8
- *
9
- * @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
10
- * @param {import('./typedef.js').Secret} secretOrPrivateKey
11
- * Secret or private key that is used by the MAC calculation algorithm
12
- *
13
- * - To generate a secret for a Hash based Authentication Code (HMAC):
14
- * use a function like `generateSecretKeyForHmacBase58()`.
15
- *
16
- * - For algorithms that use asymmetric keys, the secret is the private key
17
- * of the key pair.
18
- *
19
- * @param {import('./typedef.js').SignOptions} [options] - JWT signing options
20
- *
21
- * For more options:
22
- * @see https://github.com/auth0/node-jsonwebtoken
23
- *
24
- * @returns {string} JsonWebToken
25
- */
26
- export function sign(claims: import("./typedef.js").JwtPayload, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").SignOptions): string;
27
- /**
28
- * Decode and verify a JWT token
29
- * - Forces the use of the algorithm specified in VERIFY_OPTIONS
30
- *
31
- * @param {string} token - A JWT token
32
- * @param {import('./typedef.js').Secret} secretOrPrivateKey
33
- * The secret of private key to be used for decoding
34
- * @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
35
- *
36
- * @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
37
- */
38
- export function verify(token: string, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").VerifyOptions): import("./typedef.js").JwtPayload;
@@ -1,114 +0,0 @@
1
- /**
2
- * Core JWT operations - sign and verify functions
3
- *
4
- * @description
5
- * This module provides the main JWT functionality for signing and verifying tokens.
6
- * It wraps the jsonwebtoken library with consistent error handling and validation.
7
- */
8
-
9
- import jwt from 'jsonwebtoken';
10
-
11
- import * as expect from '../../util/expect.js';
12
-
13
- import {
14
- JWT_DEFAULT_EXPIRES_IN,
15
- DEFAULT_ALGORITHM,
16
- VERIFY_OPTIONS
17
- } from './constants.js';
18
-
19
- import { castJwtError } from './util.js';
20
-
21
- /**
22
- * Create a JSON Web Token (JWT)
23
- * - Stringifies the claims as JSON object
24
- * - Encodes the options
25
- * - Calculates a Message Authentication Code (MAC)
26
- * (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
27
- * - Combines the parts into a JWT string
28
- *
29
- * @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
30
- * @param {import('./typedef.js').Secret} secretOrPrivateKey
31
- * Secret or private key that is used by the MAC calculation algorithm
32
- *
33
- * - To generate a secret for a Hash based Authentication Code (HMAC):
34
- * use a function like `generateSecretKeyForHmacBase58()`.
35
- *
36
- * - For algorithms that use asymmetric keys, the secret is the private key
37
- * of the key pair.
38
- *
39
- * @param {import('./typedef.js').SignOptions} [options] - JWT signing options
40
- *
41
- * For more options:
42
- * @see https://github.com/auth0/node-jsonwebtoken
43
- *
44
- * @returns {string} JsonWebToken
45
- */
46
- export function sign(
47
- claims,
48
- secretOrPrivateKey,
49
- options={} )
50
- {
51
- expect.object( claims );
52
- expect.defined( secretOrPrivateKey );
53
-
54
- if( options )
55
- {
56
- expect.object( options );
57
- }
58
- else {
59
- options = {};
60
- }
61
-
62
- if( !('algorithm' in options) )
63
- {
64
- options.algorithm = DEFAULT_ALGORITHM;
65
- }
66
-
67
- if( !('expiresIn' in options) )
68
- {
69
- options.expiresIn = JWT_DEFAULT_EXPIRES_IN;
70
- }
71
- else if( !options.expiresIn )
72
- {
73
- delete options.expiresIn;
74
- }
75
-
76
- // @ts-ignore
77
- return jwt.sign( claims, secretOrPrivateKey, options );
78
- }
79
-
80
- /**
81
- * Decode and verify a JWT token
82
- * - Forces the use of the algorithm specified in VERIFY_OPTIONS
83
- *
84
- * @param {string} token - A JWT token
85
- * @param {import('./typedef.js').Secret} secretOrPrivateKey
86
- * The secret of private key to be used for decoding
87
- * @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
88
- *
89
- * @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
90
- */
91
- export function verify( token, secretOrPrivateKey, options=VERIFY_OPTIONS )
92
- {
93
- expect.notEmptyString( token );
94
- expect.defined( secretOrPrivateKey );
95
-
96
- if( !('algorithms' in options) )
97
- {
98
- options.algorithms = VERIFY_OPTIONS.algorithms;
99
- }
100
-
101
- try {
102
- // @ts-ignore
103
- const decoded = jwt.verify( token, secretOrPrivateKey, options );
104
-
105
- return decoded;
106
- }
107
- catch( e )
108
- {
109
- //
110
- // Cast internal jsonwebtoken errors to Error types defined in this lib
111
- //
112
- throw castJwtError(e);
113
- }
114
- }
@@ -1,6 +0,0 @@
1
- export * as bases from "./bases/index.js";
2
- export * as http from "./http/index.js";
3
- export * as mime from "./mime/index.js";
4
- export * as regexp from "./regexp/index.js";
5
- export * as states from "./states/index.js";
6
- export * as time from "./time/index.js";
@@ -1,6 +0,0 @@
1
- export * as bases from './bases/index.js';
2
- export * as http from './http/index.js';
3
- export * as mime from './mime/index.js';
4
- export * as regexp from './regexp/index.js';
5
- export * as states from './states/index.js';
6
- export * as time from './time/index.js';