@tstdl/base 0.91.26 → 0.91.28
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/authentication/client/http-client.middleware.js +2 -2
- package/disposable/async-disposer.d.ts +2 -3
- package/disposable/async-disposer.js +1 -1
- package/package.json +1 -1
- package/utils/cryptography.d.ts +3 -3
- package/utils/cryptography.js +12 -13
- package/utils/value-or-provider.d.ts +8 -0
- package/utils/value-or-provider.js +13 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { firstValueFrom } from 'rxjs';
|
|
2
|
-
import {
|
|
2
|
+
import { cacheValueOrAsyncProvider } from '../../utils/value-or-provider.js';
|
|
3
3
|
import { dontWaitForValidToken } from '../authentication.api.js';
|
|
4
4
|
export function waitForAuthenticationCredentialsMiddleware(authenticationServiceOrProvider) {
|
|
5
|
-
const getAuthenticationService =
|
|
5
|
+
const getAuthenticationService = cacheValueOrAsyncProvider(authenticationServiceOrProvider);
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
7
7
|
async function waitForAuthenticationCredentialsMiddleware({ request }, next) {
|
|
8
8
|
const endpoint = request.context?.endpoint;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
import type { AsyncDisposable, Disposable } from './disposable.js';
|
|
1
|
+
import { type CancellationSignal, CancellationToken } from '../cancellation/token.js';
|
|
2
|
+
import { type AsyncDisposable, type Disposable } from './disposable.js';
|
|
4
3
|
declare const deferrerToken: unique symbol;
|
|
5
4
|
export type AsyncDisposeTaskFunction = () => any;
|
|
6
5
|
export type AsyncDisposeHandler = AsyncDisposeTaskFunction | Disposable | AsyncDisposable;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isDefined, isNullOrUndefined } from '../utils/type-guards.js';
|
|
2
1
|
import { CancellationToken } from '../cancellation/token.js';
|
|
2
|
+
import { isDefined, isNullOrUndefined } from '../utils/type-guards.js';
|
|
3
3
|
import { MultiError } from '../errors/multi.error.js';
|
|
4
4
|
import { dispose, disposeAsync, isAsyncDisposable, isDisposable } from './disposable.js';
|
|
5
5
|
const deferrerToken = Symbol('DeferrerToken');
|
package/package.json
CHANGED
package/utils/cryptography.d.ts
CHANGED
|
@@ -5,9 +5,9 @@ export type EcdsaCurve = 'P-256' | 'P-384' | 'P-521';
|
|
|
5
5
|
export type HashAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
|
|
6
6
|
export type SymmetricAlgorithm = `AES-${AesMode}`;
|
|
7
7
|
export type AsymmetricAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP' | 'ECDSA' | 'ECDH' | 'NODE-DSA' | 'NODE-DH' | 'NODE-ED25519' | 'NODE-ED448';
|
|
8
|
-
export type CryptionAlgorithm = Parameters<typeof crypto.subtle.encrypt>[0];
|
|
9
|
-
export type SignAlgorithm = Parameters<typeof crypto.subtle.sign>[0];
|
|
10
|
-
export type KeyAlgorithm = Parameters<typeof crypto.subtle.generateKey>[0];
|
|
8
|
+
export type CryptionAlgorithm = Parameters<typeof globalThis.crypto.subtle.encrypt>[0];
|
|
9
|
+
export type SignAlgorithm = Parameters<typeof globalThis.crypto.subtle.sign>[0];
|
|
10
|
+
export type KeyAlgorithm = Parameters<typeof globalThis.crypto.subtle.generateKey>[0];
|
|
11
11
|
export type DeriveAlgorithm = Parameters<typeof globalThis.crypto.subtle.deriveBits>['0'];
|
|
12
12
|
export type KeyType = 'raw' | 'pkcs8' | 'spki' | 'jwk';
|
|
13
13
|
export type Key = JsonWebKey | BinaryData;
|
package/utils/cryptography.js
CHANGED
|
@@ -4,7 +4,6 @@ import { decodeText, encodeHex, encodeUtf8 } from './encoding.js';
|
|
|
4
4
|
import { getRandomBytes } from './random.js';
|
|
5
5
|
import { isArray, isDefined, isString } from './type-guards.js';
|
|
6
6
|
import { zBase32Encode } from './z-base32.js';
|
|
7
|
-
const subtle = globalThis.crypto.subtle;
|
|
8
7
|
/**
|
|
9
8
|
* Encrypt data
|
|
10
9
|
* @param algorithm algorithm as supported by Web Crypto API
|
|
@@ -13,7 +12,7 @@ const subtle = globalThis.crypto.subtle;
|
|
|
13
12
|
*/
|
|
14
13
|
export function encrypt(algorithm, key, data) {
|
|
15
14
|
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
16
|
-
const encryptedBuffer = subtle.encrypt(algorithm, key, bytes);
|
|
15
|
+
const encryptedBuffer = globalThis.crypto.subtle.encrypt(algorithm, key, bytes);
|
|
17
16
|
return {
|
|
18
17
|
toBuffer: async () => encryptedBuffer,
|
|
19
18
|
toHex: async () => encodeHex(await encryptedBuffer),
|
|
@@ -29,7 +28,7 @@ export function encrypt(algorithm, key, data) {
|
|
|
29
28
|
* @param data data to decrypt
|
|
30
29
|
*/
|
|
31
30
|
export function decrypt(algorithm, key, bytes) {
|
|
32
|
-
const decryptedBuffer = subtle.decrypt(algorithm, key, bytes);
|
|
31
|
+
const decryptedBuffer = globalThis.crypto.subtle.decrypt(algorithm, key, bytes);
|
|
33
32
|
return {
|
|
34
33
|
toBuffer: async () => decryptedBuffer,
|
|
35
34
|
toHex: async () => encodeHex(await decryptedBuffer),
|
|
@@ -46,7 +45,7 @@ export function decrypt(algorithm, key, bytes) {
|
|
|
46
45
|
*/
|
|
47
46
|
export function digest(algorithm, data) {
|
|
48
47
|
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
49
|
-
const arrayBufferPromise = subtle.digest(algorithm, bytes);
|
|
48
|
+
const arrayBufferPromise = globalThis.crypto.subtle.digest(algorithm, bytes);
|
|
50
49
|
const result = {
|
|
51
50
|
toBuffer: async () => arrayBufferPromise,
|
|
52
51
|
toHex: async () => encodeHex(await arrayBufferPromise),
|
|
@@ -64,7 +63,7 @@ export function digest(algorithm, data) {
|
|
|
64
63
|
*/
|
|
65
64
|
export function sign(algorithm, key, data) {
|
|
66
65
|
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
67
|
-
const arrayBufferPromise = subtle.sign(algorithm, key, bytes);
|
|
66
|
+
const arrayBufferPromise = globalThis.crypto.subtle.sign(algorithm, key, bytes);
|
|
68
67
|
const result = {
|
|
69
68
|
toBuffer: async () => arrayBufferPromise,
|
|
70
69
|
toHex: async () => encodeHex(await arrayBufferPromise),
|
|
@@ -84,7 +83,7 @@ export function sign(algorithm, key, data) {
|
|
|
84
83
|
export async function verify(algorithm, key, signature, data) {
|
|
85
84
|
const signatureBytes = isString(signature) ? encodeUtf8(signature) : signature;
|
|
86
85
|
const dataBytes = isString(data) ? encodeUtf8(data) : data;
|
|
87
|
-
return subtle.verify(algorithm, key, signatureBytes, dataBytes);
|
|
86
|
+
return globalThis.crypto.subtle.verify(algorithm, key, signatureBytes, dataBytes);
|
|
88
87
|
}
|
|
89
88
|
/**
|
|
90
89
|
* Imports a HMAC CryptoKey
|
|
@@ -95,9 +94,9 @@ export async function verify(algorithm, key, signature, data) {
|
|
|
95
94
|
export async function importHmacKey(algorithm, key, extractable = false) {
|
|
96
95
|
const binaryKey = isString(key) ? encodeUtf8(key) : key;
|
|
97
96
|
if (isBinaryKey(binaryKey)) {
|
|
98
|
-
return subtle.importKey('raw', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
97
|
+
return globalThis.crypto.subtle.importKey('raw', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
99
98
|
}
|
|
100
|
-
return subtle.importKey('jwk', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
99
|
+
return globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
101
100
|
}
|
|
102
101
|
/**
|
|
103
102
|
* Imports a CryptoKey for symmetric encryption
|
|
@@ -109,9 +108,9 @@ export async function importHmacKey(algorithm, key, extractable = false) {
|
|
|
109
108
|
export async function importSymmetricKey(algorithm, length, key, extractable = false) {
|
|
110
109
|
const binaryKey = isString(key) ? encodeUtf8(key) : key;
|
|
111
110
|
if (isBinaryKey(binaryKey)) {
|
|
112
|
-
return subtle.importKey('raw', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
|
|
111
|
+
return globalThis.crypto.subtle.importKey('raw', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
|
|
113
112
|
}
|
|
114
|
-
return subtle.importKey('jwk', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
|
|
113
|
+
return globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
|
|
115
114
|
}
|
|
116
115
|
/**
|
|
117
116
|
* Imports an ECDSA CryptoKey
|
|
@@ -122,9 +121,9 @@ export async function importSymmetricKey(algorithm, length, key, extractable = f
|
|
|
122
121
|
export async function importEcdsaKey(curve, key, extractable = false) {
|
|
123
122
|
const binaryKey = isString(key) ? encodeUtf8(key) : key;
|
|
124
123
|
if (isBinaryKey(binaryKey)) {
|
|
125
|
-
return subtle.importKey('spki', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
|
|
124
|
+
return globalThis.crypto.subtle.importKey('spki', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
|
|
126
125
|
}
|
|
127
|
-
return subtle.importKey('jwk', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
|
|
126
|
+
return globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
|
|
128
127
|
}
|
|
129
128
|
/**
|
|
130
129
|
* Import a pbkdf2 CryptoKey
|
|
@@ -142,7 +141,7 @@ export async function importPbkdf2Key(key, extractable = false) {
|
|
|
142
141
|
* @param usages whether to generate a key for signing, verifiying or both. Defaults to both
|
|
143
142
|
*/
|
|
144
143
|
export async function generateEcdsaKey(curve, extractable = false, usages = ['sign', 'verify']) {
|
|
145
|
-
return subtle.generateKey({ name: 'ECDSA', namedCurve: curve }, extractable, usages);
|
|
144
|
+
return globalThis.crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: curve }, extractable, usages);
|
|
146
145
|
}
|
|
147
146
|
/**
|
|
148
147
|
* Generates a pbkdf2 CryptoKey
|
|
@@ -4,5 +4,13 @@ export type ValueOrProvider<T> = T extends (() => any) ? never : (T | Provider<T
|
|
|
4
4
|
export type ValueOrAsyncProvider<T> = T extends (() => any) ? never : (T | Provider<T> | AsyncProvider<T>);
|
|
5
5
|
export type ResolvedValueOrProvider<T extends ValueOrAsyncProvider<any>> = T extends ValueOrAsyncProvider<infer U> ? U : never;
|
|
6
6
|
export declare function resolveValueOrProvider<T>(valueOrProvider: ValueOrProvider<T>): T;
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated use {@link resolveValueOrAsyncProvider}
|
|
9
|
+
*/
|
|
7
10
|
export declare function resolveAsyncValueOrProvider<T>(valueOrProvider: ValueOrAsyncProvider<T>): Promise<T>;
|
|
11
|
+
export declare function resolveValueOrAsyncProvider<T>(valueOrProvider: ValueOrAsyncProvider<T>): Promise<T>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated use {@link cacheValueOrAsyncProvider}
|
|
14
|
+
*/
|
|
8
15
|
export declare function cacheAsyncValueOrProvider<T>(provider: ValueOrAsyncProvider<T>): () => (T | Promise<T>);
|
|
16
|
+
export declare function cacheValueOrAsyncProvider<T>(provider: ValueOrAsyncProvider<T>): () => (T | Promise<T>);
|
|
@@ -5,15 +5,27 @@ export function resolveValueOrProvider(valueOrProvider) {
|
|
|
5
5
|
}
|
|
6
6
|
return valueOrProvider;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated use {@link resolveValueOrAsyncProvider}
|
|
10
|
+
*/
|
|
8
11
|
export async function resolveAsyncValueOrProvider(valueOrProvider) {
|
|
12
|
+
return resolveValueOrAsyncProvider(valueOrProvider);
|
|
13
|
+
}
|
|
14
|
+
export async function resolveValueOrAsyncProvider(valueOrProvider) {
|
|
9
15
|
if (isFunction(valueOrProvider)) {
|
|
10
16
|
return valueOrProvider();
|
|
11
17
|
}
|
|
12
18
|
return valueOrProvider;
|
|
13
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use {@link cacheValueOrAsyncProvider}
|
|
22
|
+
*/
|
|
14
23
|
export function cacheAsyncValueOrProvider(provider) {
|
|
24
|
+
return cacheValueOrAsyncProvider(provider);
|
|
25
|
+
}
|
|
26
|
+
export function cacheValueOrAsyncProvider(provider) {
|
|
15
27
|
let getValue = async () => {
|
|
16
|
-
const valuePromise =
|
|
28
|
+
const valuePromise = resolveValueOrAsyncProvider(provider);
|
|
17
29
|
getValue = async () => valuePromise;
|
|
18
30
|
void valuePromise.then((resolvedValue) => (getValue = () => resolvedValue));
|
|
19
31
|
return valuePromise;
|