@twin.org/core 0.0.3-next.9 → 0.0.4-next.1
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/README.md +1 -9
- package/dist/es/encoding/base32.js +1 -1
- package/dist/es/encoding/base32.js.map +1 -1
- package/dist/es/encoding/base64.js +1 -1
- package/dist/es/encoding/base64.js.map +1 -1
- package/dist/es/errors/alreadyExistsError.js +1 -1
- package/dist/es/errors/alreadyExistsError.js.map +1 -1
- package/dist/es/errors/baseError.js +1 -1
- package/dist/es/errors/baseError.js.map +1 -1
- package/dist/es/errors/conflictError.js +1 -1
- package/dist/es/errors/conflictError.js.map +1 -1
- package/dist/es/errors/generalError.js +1 -1
- package/dist/es/errors/generalError.js.map +1 -1
- package/dist/es/errors/guardError.js +1 -1
- package/dist/es/errors/guardError.js.map +1 -1
- package/dist/es/errors/notFoundError.js +1 -1
- package/dist/es/errors/notFoundError.js.map +1 -1
- package/dist/es/errors/notSupportedError.js +1 -1
- package/dist/es/errors/notSupportedError.js.map +1 -1
- package/dist/es/errors/unauthorizedError.js +2 -2
- package/dist/es/errors/unauthorizedError.js.map +1 -1
- package/dist/es/errors/unprocessableError.js +1 -1
- package/dist/es/errors/unprocessableError.js.map +1 -1
- package/dist/es/factories/factory.js +41 -0
- package/dist/es/factories/factory.js.map +1 -1
- package/dist/es/helpers/arrayHelper.js +2 -0
- package/dist/es/helpers/arrayHelper.js.map +1 -1
- package/dist/es/helpers/errorHelper.js +18 -14
- package/dist/es/helpers/errorHelper.js.map +1 -1
- package/dist/es/helpers/jsonHelper.js.map +1 -1
- package/dist/es/helpers/objectHelper.js +12 -36
- package/dist/es/helpers/objectHelper.js.map +1 -1
- package/dist/es/helpers/stringHelper.js +11 -0
- package/dist/es/helpers/stringHelper.js.map +1 -1
- package/dist/es/index.js +8 -1
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IComponent.js +0 -2
- package/dist/es/models/IComponent.js.map +1 -1
- package/dist/es/models/IError.js.map +1 -1
- package/dist/es/models/IHealth.js +2 -0
- package/dist/es/models/IHealth.js.map +1 -0
- package/dist/es/models/IMutexWorkerMessage.js +2 -0
- package/dist/es/models/IMutexWorkerMessage.js.map +1 -0
- package/dist/es/models/healthStatus.js +21 -0
- package/dist/es/models/healthStatus.js.map +1 -0
- package/dist/es/models/mutexMessageTypes.js +13 -0
- package/dist/es/models/mutexMessageTypes.js.map +1 -0
- package/dist/es/types/objectOrArray.js.map +1 -0
- package/dist/es/types/singleOccurrenceArray.js +2 -0
- package/dist/es/types/singleOccurrenceArray.js.map +1 -0
- package/dist/es/types/singleOccurrenceArrayDepthHelper.js +2 -0
- package/dist/es/types/singleOccurrenceArrayDepthHelper.js.map +1 -0
- package/dist/es/types/urn.js +1 -2
- package/dist/es/types/urn.js.map +1 -1
- package/dist/es/utils/asyncCache.js +236 -88
- package/dist/es/utils/asyncCache.js.map +1 -1
- package/dist/es/utils/guards.js.map +1 -1
- package/dist/es/utils/i18n.js.map +1 -1
- package/dist/es/utils/mutex.js +188 -0
- package/dist/es/utils/mutex.js.map +1 -0
- package/dist/types/encoding/base32.d.ts +1 -1
- package/dist/types/errors/alreadyExistsError.d.ts +1 -1
- package/dist/types/errors/baseError.d.ts +1 -1
- package/dist/types/errors/conflictError.d.ts +1 -1
- package/dist/types/errors/generalError.d.ts +1 -1
- package/dist/types/errors/guardError.d.ts +1 -1
- package/dist/types/errors/notFoundError.d.ts +1 -1
- package/dist/types/errors/notSupportedError.d.ts +1 -1
- package/dist/types/errors/unauthorizedError.d.ts +2 -2
- package/dist/types/errors/unprocessableError.d.ts +1 -1
- package/dist/types/factories/factory.d.ts +23 -1
- package/dist/types/helpers/arrayHelper.d.ts +1 -8
- package/dist/types/helpers/errorHelper.d.ts +11 -4
- package/dist/types/helpers/objectHelper.d.ts +18 -14
- package/dist/types/helpers/stringHelper.d.ts +6 -0
- package/dist/types/index.d.ts +8 -1
- package/dist/types/models/IComponent.d.ts +12 -0
- package/dist/types/models/IError.d.ts +1 -1
- package/dist/types/models/IHealth.d.ts +32 -0
- package/dist/types/models/IMutexWorkerMessage.d.ts +23 -0
- package/dist/types/models/healthStatus.d.ts +21 -0
- package/dist/types/models/mutexMessageTypes.d.ts +13 -0
- package/dist/types/types/singleOccurrenceArray.d.ts +6 -0
- package/dist/types/types/singleOccurrenceArrayDepthHelper.d.ts +4 -0
- package/dist/types/utils/asyncCache.d.ts +11 -3
- package/dist/types/utils/guards.d.ts +1 -1
- package/dist/types/utils/mutex.d.ts +53 -0
- package/docs/changelog.md +1011 -218
- package/docs/examples.md +308 -1
- package/docs/reference/classes/AlreadyExistsError.md +36 -36
- package/docs/reference/classes/ArrayHelper.md +10 -44
- package/docs/reference/classes/AsyncCache.md +13 -12
- package/docs/reference/classes/Base32.md +4 -4
- package/docs/reference/classes/Base58.md +3 -3
- package/docs/reference/classes/Base64.md +4 -4
- package/docs/reference/classes/Base64Url.md +3 -3
- package/docs/reference/classes/BaseError.md +35 -35
- package/docs/reference/classes/BitString.md +6 -6
- package/docs/reference/classes/Coerce.md +11 -11
- package/docs/reference/classes/Compression.md +3 -3
- package/docs/reference/classes/ConflictError.md +36 -36
- package/docs/reference/classes/Converter.md +18 -18
- package/docs/reference/classes/EnvHelper.md +1 -1
- package/docs/reference/classes/ErrorHelper.md +20 -10
- package/docs/reference/classes/Factory.md +112 -18
- package/docs/reference/classes/FilenameHelper.md +1 -1
- package/docs/reference/classes/GeneralError.md +36 -36
- package/docs/reference/classes/GuardError.md +36 -36
- package/docs/reference/classes/Guards.md +33 -33
- package/docs/reference/classes/HexHelper.md +6 -6
- package/docs/reference/classes/I18n.md +14 -14
- package/docs/reference/classes/Is.md +42 -42
- package/docs/reference/classes/JsonHelper.md +10 -10
- package/docs/reference/classes/Mutex.md +128 -0
- package/docs/reference/classes/NotFoundError.md +36 -36
- package/docs/reference/classes/NotImplementedError.md +35 -35
- package/docs/reference/classes/NotSupportedError.md +36 -36
- package/docs/reference/classes/NumberHelper.md +2 -2
- package/docs/reference/classes/ObjectHelper.md +135 -73
- package/docs/reference/classes/RandomHelper.md +8 -8
- package/docs/reference/classes/SharedStore.md +3 -3
- package/docs/reference/classes/StringHelper.md +45 -23
- package/docs/reference/classes/Uint8ArrayHelper.md +1 -1
- package/docs/reference/classes/UnauthorizedError.md +37 -37
- package/docs/reference/classes/UnprocessableError.md +36 -36
- package/docs/reference/classes/Url.md +8 -8
- package/docs/reference/classes/Urn.md +24 -24
- package/docs/reference/classes/Validation.md +25 -25
- package/docs/reference/classes/ValidationError.md +35 -35
- package/docs/reference/index.md +9 -0
- package/docs/reference/interfaces/IComponent.md +40 -4
- package/docs/reference/interfaces/IError.md +11 -11
- package/docs/reference/interfaces/IHealth.md +55 -0
- package/docs/reference/interfaces/II18nShared.md +4 -4
- package/docs/reference/interfaces/IKeyValue.md +2 -2
- package/docs/reference/interfaces/ILabelledValue.md +2 -2
- package/docs/reference/interfaces/ILocale.md +2 -2
- package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
- package/docs/reference/interfaces/ILocalesIndex.md +1 -1
- package/docs/reference/interfaces/IMutexWorkerMessage.md +35 -0
- package/docs/reference/interfaces/IPatchOperation.md +6 -6
- package/docs/reference/interfaces/IUrlParts.md +9 -9
- package/docs/reference/interfaces/IValidationFailure.md +4 -4
- package/docs/reference/type-aliases/HealthStatus.md +5 -0
- package/docs/reference/type-aliases/MutexMessageTypes.md +5 -0
- package/docs/reference/type-aliases/SingleOccurrenceArray.md +15 -0
- package/docs/reference/type-aliases/SingleOccurrenceArrayDepthHelper.md +19 -0
- package/docs/reference/variables/CoerceType.md +10 -10
- package/docs/reference/variables/CompressionType.md +2 -2
- package/docs/reference/variables/HealthStatus.md +25 -0
- package/docs/reference/variables/MutexMessageTypes.md +13 -0
- package/locales/en.json +8 -1
- package/package.json +6 -6
- package/dist/es/models/objectOrArray.js.map +0 -1
- /package/dist/es/{models → types}/objectOrArray.js +0 -0
- /package/dist/types/{models → types}/objectOrArray.d.ts +0 -0
|
@@ -58,8 +58,8 @@ export declare class ObjectHelper {
|
|
|
58
58
|
static propertySet(obj: unknown, property: string, value: unknown): void;
|
|
59
59
|
/**
|
|
60
60
|
* Delete the property of an unknown object.
|
|
61
|
-
* @param obj The object to
|
|
62
|
-
* @param property The property to
|
|
61
|
+
* @param obj The object to delete the property from.
|
|
62
|
+
* @param property The property to delete.
|
|
63
63
|
*/
|
|
64
64
|
static propertyDelete(obj: unknown, property: string): void;
|
|
65
65
|
/**
|
|
@@ -74,26 +74,30 @@ export declare class ObjectHelper {
|
|
|
74
74
|
* Pick a subset of properties from an object.
|
|
75
75
|
* @param obj The object to pick the properties from.
|
|
76
76
|
* @param keys The property keys to pick.
|
|
77
|
-
* @returns The
|
|
77
|
+
* @returns The picked object.
|
|
78
78
|
*/
|
|
79
|
-
static pick<T>(obj: T
|
|
79
|
+
static pick<T, K extends keyof T>(obj: T, keys?: K[]): Pick<T, K>;
|
|
80
|
+
/**
|
|
81
|
+
* Pick a subset of properties from an object.
|
|
82
|
+
* @param obj The object to pick the properties from.
|
|
83
|
+
* @param keys The property keys to pick.
|
|
84
|
+
* @returns The picked object, or undefined if the input was undefined.
|
|
85
|
+
*/
|
|
86
|
+
static pick<T, K extends keyof T>(obj: T | undefined, keys?: K[]): Pick<T, K> | undefined;
|
|
80
87
|
/**
|
|
81
88
|
* Omit a subset of properties from an object.
|
|
82
89
|
* @param obj The object to omit the properties from.
|
|
83
90
|
* @param keys The property keys to omit.
|
|
84
|
-
* @returns The
|
|
91
|
+
* @returns The object without the omitted keys.
|
|
85
92
|
*/
|
|
86
|
-
static omit<T>(obj: T
|
|
93
|
+
static omit<T, K extends keyof T>(obj: T, keys?: K[]): Omit<T, K>;
|
|
87
94
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @param obj The object to
|
|
90
|
-
* @param keys The property keys to
|
|
91
|
-
* @returns The
|
|
95
|
+
* Omit a subset of properties from an object.
|
|
96
|
+
* @param obj The object to omit the properties from.
|
|
97
|
+
* @param keys The property keys to omit.
|
|
98
|
+
* @returns The object without the omitted keys, or undefined if the input was undefined.
|
|
92
99
|
*/
|
|
93
|
-
static
|
|
94
|
-
picked: Partial<T> | undefined;
|
|
95
|
-
omitted: Partial<T> | undefined;
|
|
96
|
-
};
|
|
100
|
+
static omit<T, K extends keyof T>(obj: T | undefined, keys?: K[]): Omit<T, K> | undefined;
|
|
97
101
|
/**
|
|
98
102
|
* Converter the non JSON primitives to extended types.
|
|
99
103
|
* @param obj The object to convert.
|
|
@@ -14,6 +14,12 @@ export declare class StringHelper {
|
|
|
14
14
|
* @returns The trimmed string or the original.
|
|
15
15
|
*/
|
|
16
16
|
static trimLeadingSlashes(value: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Trim both leading and trailing slashes from a string.
|
|
19
|
+
* @param value The value to trim.
|
|
20
|
+
* @returns The trimmed string or the original.
|
|
21
|
+
*/
|
|
22
|
+
static trimLeadingAndTrailingSlashes(value: string): string;
|
|
17
23
|
/**
|
|
18
24
|
* Convert the input string to kebab case.
|
|
19
25
|
* @param input The input to convert.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -28,19 +28,25 @@ export * from "./helpers/stringHelper.js";
|
|
|
28
28
|
export * from "./helpers/uint8ArrayHelper.js";
|
|
29
29
|
export * from "./models/coerceType.js";
|
|
30
30
|
export * from "./models/compressionType.js";
|
|
31
|
+
export * from "./models/healthStatus.js";
|
|
31
32
|
export * from "./models/IComponent.js";
|
|
32
33
|
export * from "./models/IError.js";
|
|
34
|
+
export * from "./models/IHealth.js";
|
|
33
35
|
export * from "./models/II18nShared.js";
|
|
34
36
|
export * from "./models/IKeyValue.js";
|
|
35
37
|
export * from "./models/ILabelledValue.js";
|
|
36
38
|
export * from "./models/ILocale.js";
|
|
37
39
|
export * from "./models/ILocaleDictionary.js";
|
|
38
40
|
export * from "./models/ILocalesIndex.js";
|
|
41
|
+
export * from "./models/IMutexWorkerMessage.js";
|
|
39
42
|
export * from "./models/IPatchOperation.js";
|
|
40
43
|
export * from "./models/IUrlParts.js";
|
|
41
44
|
export * from "./models/IValidationFailure.js";
|
|
42
|
-
export * from "./models/
|
|
45
|
+
export * from "./models/mutexMessageTypes.js";
|
|
43
46
|
export * from "./types/bitString.js";
|
|
47
|
+
export * from "./types/objectOrArray.js";
|
|
48
|
+
export * from "./types/singleOccurrenceArray.js";
|
|
49
|
+
export * from "./types/singleOccurrenceArrayDepthHelper.js";
|
|
44
50
|
export * from "./types/url.js";
|
|
45
51
|
export * from "./types/urn.js";
|
|
46
52
|
export * from "./utils/asyncCache.js";
|
|
@@ -50,5 +56,6 @@ export * from "./utils/converter.js";
|
|
|
50
56
|
export * from "./utils/guards.js";
|
|
51
57
|
export * from "./utils/i18n.js";
|
|
52
58
|
export * from "./utils/is.js";
|
|
59
|
+
export * from "./utils/mutex.js";
|
|
53
60
|
export * from "./utils/sharedStore.js";
|
|
54
61
|
export * from "./utils/validation.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IHealth } from "./IHealth.js";
|
|
1
2
|
/**
|
|
2
3
|
* Interface describing a component which can be bootstrapped, started and stopped.
|
|
3
4
|
*/
|
|
@@ -13,6 +14,12 @@ export interface IComponent {
|
|
|
13
14
|
* @returns True if the bootstrapping process was successful.
|
|
14
15
|
*/
|
|
15
16
|
bootstrap?(nodeLoggingComponentType?: string): Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Teardown the component by releasing any resources it holds.
|
|
19
|
+
* @param nodeLoggingComponentType The node logging component type.
|
|
20
|
+
* @returns True if the teardown process was successful.
|
|
21
|
+
*/
|
|
22
|
+
teardown?(nodeLoggingComponentType?: string): Promise<boolean>;
|
|
16
23
|
/**
|
|
17
24
|
* The component needs to be started when the node is initialized.
|
|
18
25
|
* @param nodeLoggingComponentType The node logging component type.
|
|
@@ -25,4 +32,9 @@ export interface IComponent {
|
|
|
25
32
|
* @returns Nothing.
|
|
26
33
|
*/
|
|
27
34
|
stop?(nodeLoggingComponentType?: string): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the health status of the component.
|
|
37
|
+
* @returns The health status of the component, can return multiple entries for elements within the component.
|
|
38
|
+
*/
|
|
39
|
+
health?(): Promise<IHealth[]>;
|
|
28
40
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HealthStatus } from "./healthStatus.js";
|
|
2
|
+
/**
|
|
3
|
+
* Provides health information for a component.
|
|
4
|
+
*/
|
|
5
|
+
export interface IHealth {
|
|
6
|
+
/**
|
|
7
|
+
* The source of the health information.
|
|
8
|
+
*/
|
|
9
|
+
source: string;
|
|
10
|
+
/**
|
|
11
|
+
* The description of the component as an i18n key.
|
|
12
|
+
*/
|
|
13
|
+
description?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The overall status of the component, the entries can also report their own health.
|
|
16
|
+
*/
|
|
17
|
+
status: HealthStatus;
|
|
18
|
+
/**
|
|
19
|
+
* The message for the status if there are further details to provide as an i18n key.
|
|
20
|
+
*/
|
|
21
|
+
message?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Data to substitute in the i18n key for the message.
|
|
24
|
+
*/
|
|
25
|
+
data?: {
|
|
26
|
+
[id: string]: unknown;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The grouped child components, if any.
|
|
30
|
+
*/
|
|
31
|
+
grouped?: IHealth[];
|
|
32
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MessagePort } from "node:worker_threads";
|
|
2
|
+
import type { MutexMessageTypes } from "./mutexMessageTypes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Message sent from a worker thread to the main thread to request a SharedArrayBuffer for a given mutex key.
|
|
5
|
+
*/
|
|
6
|
+
export interface IMutexWorkerMessage {
|
|
7
|
+
/**
|
|
8
|
+
* The message type.
|
|
9
|
+
*/
|
|
10
|
+
type: typeof MutexMessageTypes.GetBuffer;
|
|
11
|
+
/**
|
|
12
|
+
* The mutex key for which the buffer is requested.
|
|
13
|
+
*/
|
|
14
|
+
key: string;
|
|
15
|
+
/**
|
|
16
|
+
* The SharedArrayBuffer for the mutex, sent from the worker to the main thread.
|
|
17
|
+
*/
|
|
18
|
+
signal: SharedArrayBuffer;
|
|
19
|
+
/**
|
|
20
|
+
* The MessagePort for the main thread to respond with the buffer, sent from the worker to the main thread.
|
|
21
|
+
*/
|
|
22
|
+
port: MessagePort;
|
|
23
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The health status of the component.
|
|
3
|
+
*/
|
|
4
|
+
export declare const HealthStatus: {
|
|
5
|
+
/**
|
|
6
|
+
* OK.
|
|
7
|
+
*/
|
|
8
|
+
readonly Ok: "ok";
|
|
9
|
+
/**
|
|
10
|
+
* Warning.
|
|
11
|
+
*/
|
|
12
|
+
readonly Warning: "warning";
|
|
13
|
+
/**
|
|
14
|
+
* Error.
|
|
15
|
+
*/
|
|
16
|
+
readonly Error: "error";
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The health status of the component.
|
|
20
|
+
*/
|
|
21
|
+
export type HealthStatus = (typeof HealthStatus)[keyof typeof HealthStatus];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mutex message types.
|
|
3
|
+
*/
|
|
4
|
+
export declare const MutexMessageTypes: {
|
|
5
|
+
/**
|
|
6
|
+
* Get buffer.
|
|
7
|
+
*/
|
|
8
|
+
readonly GetBuffer: "twin:mutex:getBuffer";
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Mutex message types.
|
|
12
|
+
*/
|
|
13
|
+
export type MutexMessageTypes = (typeof MutexMessageTypes)[keyof typeof MutexMessageTypes];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SingleOccurrenceArrayDepthHelper } from "./singleOccurrenceArrayDepthHelper.js";
|
|
2
|
+
/**
|
|
3
|
+
* Utility type to create a non-empty array with values of type T and exactly one value of type U.
|
|
4
|
+
*/
|
|
5
|
+
export type SingleOccurrenceArray<T = unknown, U = never> = SingleOccurrenceArrayDepthHelper<T, U, [
|
|
6
|
+
]>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper with bounded recursion depth to keep type instantiation tractable.
|
|
3
|
+
*/
|
|
4
|
+
export type SingleOccurrenceArrayDepthHelper<T, U, Depth extends 0[]> = Depth["length"] extends 16 ? [U, ...T[]] : [U, ...T[]] | [T, ...SingleOccurrenceArrayDepthHelper<T, U, [0, ...Depth]>];
|
|
@@ -10,18 +10,19 @@ export declare class AsyncCache {
|
|
|
10
10
|
* @param cacheFailures Cache failure results, defaults to false.
|
|
11
11
|
* @returns The response.
|
|
12
12
|
*/
|
|
13
|
-
static exec<T = unknown>(key: string, ttlMs: number | undefined, requestMethod: () => Promise<T>, cacheFailures?: boolean): Promise<T
|
|
13
|
+
static exec<T = unknown>(key: string, ttlMs: number | undefined, requestMethod: () => Promise<T>, cacheFailures?: boolean): Promise<T>;
|
|
14
14
|
/**
|
|
15
15
|
* Get an entry from the cache.
|
|
16
16
|
* @param key The key to get from the cache.
|
|
17
|
-
* @returns The item from the cache if it exists
|
|
17
|
+
* @returns The item from the cache if it exists, or undefined if the key is missing, expired, or
|
|
18
|
+
* its request is still in-progress. Throws if a cached failure exists for the key.
|
|
18
19
|
*/
|
|
19
20
|
static get<T = unknown>(key: string): Promise<T | undefined>;
|
|
20
21
|
/**
|
|
21
22
|
* Set an entry into the cache.
|
|
22
23
|
* @param key The key to set in the cache.
|
|
23
24
|
* @param value The value to set in the cache.
|
|
24
|
-
* @param ttlMs The TTL of the entry in the cache in
|
|
25
|
+
* @param ttlMs The TTL of the entry in the cache in milliseconds. Defaults to 1000 (1 second).
|
|
25
26
|
* @returns Nothing.
|
|
26
27
|
*/
|
|
27
28
|
static set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void>;
|
|
@@ -39,4 +40,11 @@ export declare class AsyncCache {
|
|
|
39
40
|
* Perform a cleanup of the expired entries in the cache.
|
|
40
41
|
*/
|
|
41
42
|
static cleanupExpired(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Resolve a waiter by re-running its request method safely.
|
|
45
|
+
* @param requestMethod The method to execute.
|
|
46
|
+
* @param resolve The resolver for the waiter.
|
|
47
|
+
* @param reject The rejector for the waiter.
|
|
48
|
+
*/
|
|
49
|
+
private static resolveWaiter;
|
|
42
50
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A cross-thread mutex built on Atomics and SharedArrayBuffer.
|
|
3
|
+
*
|
|
4
|
+
* When isMainThread is true (main thread or fork-mode child process) the class acts as
|
|
5
|
+
* the authoritative registry: it creates a SharedArrayBuffer-backed Int32Array for each
|
|
6
|
+
* key on first use and never discards it, because worker threads may hold references to
|
|
7
|
+
* the same underlying memory.
|
|
8
|
+
*
|
|
9
|
+
* When isMainThread is false (a true worker thread) the class synchronously negotiates
|
|
10
|
+
* the shared buffer with the main thread on first use of each key, then caches it locally.
|
|
11
|
+
* The main thread must call Mutex.handleWorkerMessage(msg) from its worker message handler
|
|
12
|
+
* before that worker first calls Mutex.lock().
|
|
13
|
+
*
|
|
14
|
+
* The lock is not re-entrant: a thread that already holds a key and calls lock() again on
|
|
15
|
+
* the same key will block until the timeout elapses.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Mutex {
|
|
18
|
+
/**
|
|
19
|
+
* Runtime name for the class.
|
|
20
|
+
*/
|
|
21
|
+
static readonly CLASS_NAME: string;
|
|
22
|
+
/**
|
|
23
|
+
* Acquires a lock for the given key without blocking the event loop. If the lock is already
|
|
24
|
+
* held, it suspends the current async task until the lock is released or the timeout is reached.
|
|
25
|
+
* Use this in async single-threaded contexts (e.g. the main thread or a Fastify route handler)
|
|
26
|
+
* where calling the synchronous lock() would freeze the event loop and deadlock.
|
|
27
|
+
* The lock is not re-entrant: if the same context holds the key and calls lockAsync() again on
|
|
28
|
+
* the same key, it will suspend until the timeout elapses.
|
|
29
|
+
* @param key The key to lock on.
|
|
30
|
+
* @param options Lock options.
|
|
31
|
+
* @param options.timeoutMs The maximum time to wait for the lock in milliseconds, default is 5000.
|
|
32
|
+
* @param options.throwOnTimeout Whether to throw an error if the lock could not be acquired within the timeout, default is false.
|
|
33
|
+
* @returns True if the lock was acquired, false if it timed out and throwOnTimeout is false.
|
|
34
|
+
* @throws GeneralError if the key is invalid or if the lock could not be acquired within the timeout and throwOnTimeout is true.
|
|
35
|
+
*/
|
|
36
|
+
static lock(key: string, options?: {
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
throwOnTimeout?: boolean;
|
|
39
|
+
}): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Releases the lock for the given key.
|
|
42
|
+
* @param key The key to unlock.
|
|
43
|
+
* @throws GeneralError if the key is invalid or the lock is not currently held.
|
|
44
|
+
*/
|
|
45
|
+
static unlock(key: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Inspect a message received from a worker and, if it is a Mutex buffer-fetch request,
|
|
48
|
+
* respond to it synchronously. Call from the main thread's worker message handler.
|
|
49
|
+
* @param msg The raw message received from the worker.
|
|
50
|
+
* @returns True if the message was a Mutex protocol message and was handled, false otherwise.
|
|
51
|
+
*/
|
|
52
|
+
static handleWorkerMessage(msg: unknown): boolean;
|
|
53
|
+
}
|