@twin.org/core 0.0.3-next.9 → 0.0.3

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.
Files changed (156) hide show
  1. package/README.md +1 -9
  2. package/dist/es/encoding/base32.js +1 -1
  3. package/dist/es/encoding/base32.js.map +1 -1
  4. package/dist/es/encoding/base64.js +1 -1
  5. package/dist/es/encoding/base64.js.map +1 -1
  6. package/dist/es/errors/alreadyExistsError.js +1 -1
  7. package/dist/es/errors/alreadyExistsError.js.map +1 -1
  8. package/dist/es/errors/baseError.js +1 -1
  9. package/dist/es/errors/baseError.js.map +1 -1
  10. package/dist/es/errors/conflictError.js +1 -1
  11. package/dist/es/errors/conflictError.js.map +1 -1
  12. package/dist/es/errors/generalError.js +1 -1
  13. package/dist/es/errors/generalError.js.map +1 -1
  14. package/dist/es/errors/guardError.js +1 -1
  15. package/dist/es/errors/guardError.js.map +1 -1
  16. package/dist/es/errors/notFoundError.js +1 -1
  17. package/dist/es/errors/notFoundError.js.map +1 -1
  18. package/dist/es/errors/notSupportedError.js +1 -1
  19. package/dist/es/errors/notSupportedError.js.map +1 -1
  20. package/dist/es/errors/unauthorizedError.js +2 -2
  21. package/dist/es/errors/unauthorizedError.js.map +1 -1
  22. package/dist/es/errors/unprocessableError.js +1 -1
  23. package/dist/es/errors/unprocessableError.js.map +1 -1
  24. package/dist/es/factories/factory.js +41 -0
  25. package/dist/es/factories/factory.js.map +1 -1
  26. package/dist/es/helpers/arrayHelper.js +2 -0
  27. package/dist/es/helpers/arrayHelper.js.map +1 -1
  28. package/dist/es/helpers/errorHelper.js +18 -14
  29. package/dist/es/helpers/errorHelper.js.map +1 -1
  30. package/dist/es/helpers/jsonHelper.js.map +1 -1
  31. package/dist/es/helpers/objectHelper.js +12 -36
  32. package/dist/es/helpers/objectHelper.js.map +1 -1
  33. package/dist/es/helpers/stringHelper.js +11 -0
  34. package/dist/es/helpers/stringHelper.js.map +1 -1
  35. package/dist/es/index.js +8 -1
  36. package/dist/es/index.js.map +1 -1
  37. package/dist/es/models/IComponent.js +0 -2
  38. package/dist/es/models/IComponent.js.map +1 -1
  39. package/dist/es/models/IError.js.map +1 -1
  40. package/dist/es/models/IHealth.js +2 -0
  41. package/dist/es/models/IHealth.js.map +1 -0
  42. package/dist/es/models/IMutexWorkerMessage.js +2 -0
  43. package/dist/es/models/IMutexWorkerMessage.js.map +1 -0
  44. package/dist/es/models/healthStatus.js +21 -0
  45. package/dist/es/models/healthStatus.js.map +1 -0
  46. package/dist/es/models/mutexMessageTypes.js +13 -0
  47. package/dist/es/models/mutexMessageTypes.js.map +1 -0
  48. package/dist/es/types/objectOrArray.js.map +1 -0
  49. package/dist/es/types/singleOccurrenceArray.js +2 -0
  50. package/dist/es/types/singleOccurrenceArray.js.map +1 -0
  51. package/dist/es/types/singleOccurrenceArrayDepthHelper.js +2 -0
  52. package/dist/es/types/singleOccurrenceArrayDepthHelper.js.map +1 -0
  53. package/dist/es/types/urn.js +1 -2
  54. package/dist/es/types/urn.js.map +1 -1
  55. package/dist/es/utils/asyncCache.js +236 -88
  56. package/dist/es/utils/asyncCache.js.map +1 -1
  57. package/dist/es/utils/guards.js.map +1 -1
  58. package/dist/es/utils/i18n.js.map +1 -1
  59. package/dist/es/utils/mutex.js +185 -0
  60. package/dist/es/utils/mutex.js.map +1 -0
  61. package/dist/types/encoding/base32.d.ts +1 -1
  62. package/dist/types/errors/alreadyExistsError.d.ts +1 -1
  63. package/dist/types/errors/baseError.d.ts +1 -1
  64. package/dist/types/errors/conflictError.d.ts +1 -1
  65. package/dist/types/errors/generalError.d.ts +1 -1
  66. package/dist/types/errors/guardError.d.ts +1 -1
  67. package/dist/types/errors/notFoundError.d.ts +1 -1
  68. package/dist/types/errors/notSupportedError.d.ts +1 -1
  69. package/dist/types/errors/unauthorizedError.d.ts +2 -2
  70. package/dist/types/errors/unprocessableError.d.ts +1 -1
  71. package/dist/types/factories/factory.d.ts +23 -1
  72. package/dist/types/helpers/arrayHelper.d.ts +1 -8
  73. package/dist/types/helpers/errorHelper.d.ts +11 -4
  74. package/dist/types/helpers/objectHelper.d.ts +18 -14
  75. package/dist/types/helpers/stringHelper.d.ts +6 -0
  76. package/dist/types/index.d.ts +8 -1
  77. package/dist/types/models/IComponent.d.ts +12 -0
  78. package/dist/types/models/IError.d.ts +1 -1
  79. package/dist/types/models/IHealth.d.ts +32 -0
  80. package/dist/types/models/IMutexWorkerMessage.d.ts +23 -0
  81. package/dist/types/models/healthStatus.d.ts +21 -0
  82. package/dist/types/models/mutexMessageTypes.d.ts +13 -0
  83. package/dist/types/types/singleOccurrenceArray.d.ts +6 -0
  84. package/dist/types/types/singleOccurrenceArrayDepthHelper.d.ts +4 -0
  85. package/dist/types/utils/asyncCache.d.ts +11 -3
  86. package/dist/types/utils/guards.d.ts +1 -1
  87. package/dist/types/utils/mutex.d.ts +53 -0
  88. package/docs/changelog.md +930 -218
  89. package/docs/examples.md +308 -1
  90. package/docs/reference/classes/AlreadyExistsError.md +36 -36
  91. package/docs/reference/classes/ArrayHelper.md +10 -44
  92. package/docs/reference/classes/AsyncCache.md +13 -12
  93. package/docs/reference/classes/Base32.md +4 -4
  94. package/docs/reference/classes/Base58.md +3 -3
  95. package/docs/reference/classes/Base64.md +4 -4
  96. package/docs/reference/classes/Base64Url.md +3 -3
  97. package/docs/reference/classes/BaseError.md +35 -35
  98. package/docs/reference/classes/BitString.md +6 -6
  99. package/docs/reference/classes/Coerce.md +11 -11
  100. package/docs/reference/classes/Compression.md +3 -3
  101. package/docs/reference/classes/ConflictError.md +36 -36
  102. package/docs/reference/classes/Converter.md +18 -18
  103. package/docs/reference/classes/EnvHelper.md +1 -1
  104. package/docs/reference/classes/ErrorHelper.md +20 -10
  105. package/docs/reference/classes/Factory.md +112 -18
  106. package/docs/reference/classes/FilenameHelper.md +1 -1
  107. package/docs/reference/classes/GeneralError.md +36 -36
  108. package/docs/reference/classes/GuardError.md +36 -36
  109. package/docs/reference/classes/Guards.md +33 -33
  110. package/docs/reference/classes/HexHelper.md +6 -6
  111. package/docs/reference/classes/I18n.md +14 -14
  112. package/docs/reference/classes/Is.md +42 -42
  113. package/docs/reference/classes/JsonHelper.md +10 -10
  114. package/docs/reference/classes/Mutex.md +128 -0
  115. package/docs/reference/classes/NotFoundError.md +36 -36
  116. package/docs/reference/classes/NotImplementedError.md +35 -35
  117. package/docs/reference/classes/NotSupportedError.md +36 -36
  118. package/docs/reference/classes/NumberHelper.md +2 -2
  119. package/docs/reference/classes/ObjectHelper.md +135 -73
  120. package/docs/reference/classes/RandomHelper.md +8 -8
  121. package/docs/reference/classes/SharedStore.md +3 -3
  122. package/docs/reference/classes/StringHelper.md +45 -23
  123. package/docs/reference/classes/Uint8ArrayHelper.md +1 -1
  124. package/docs/reference/classes/UnauthorizedError.md +37 -37
  125. package/docs/reference/classes/UnprocessableError.md +36 -36
  126. package/docs/reference/classes/Url.md +8 -8
  127. package/docs/reference/classes/Urn.md +24 -24
  128. package/docs/reference/classes/Validation.md +25 -25
  129. package/docs/reference/classes/ValidationError.md +35 -35
  130. package/docs/reference/index.md +9 -0
  131. package/docs/reference/interfaces/IComponent.md +40 -4
  132. package/docs/reference/interfaces/IError.md +11 -11
  133. package/docs/reference/interfaces/IHealth.md +55 -0
  134. package/docs/reference/interfaces/II18nShared.md +4 -4
  135. package/docs/reference/interfaces/IKeyValue.md +2 -2
  136. package/docs/reference/interfaces/ILabelledValue.md +2 -2
  137. package/docs/reference/interfaces/ILocale.md +2 -2
  138. package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
  139. package/docs/reference/interfaces/ILocalesIndex.md +1 -1
  140. package/docs/reference/interfaces/IMutexWorkerMessage.md +35 -0
  141. package/docs/reference/interfaces/IPatchOperation.md +6 -6
  142. package/docs/reference/interfaces/IUrlParts.md +9 -9
  143. package/docs/reference/interfaces/IValidationFailure.md +4 -4
  144. package/docs/reference/type-aliases/HealthStatus.md +5 -0
  145. package/docs/reference/type-aliases/MutexMessageTypes.md +5 -0
  146. package/docs/reference/type-aliases/SingleOccurrenceArray.md +15 -0
  147. package/docs/reference/type-aliases/SingleOccurrenceArrayDepthHelper.md +19 -0
  148. package/docs/reference/variables/CoerceType.md +10 -10
  149. package/docs/reference/variables/CompressionType.md +2 -2
  150. package/docs/reference/variables/HealthStatus.md +25 -0
  151. package/docs/reference/variables/MutexMessageTypes.md +13 -0
  152. package/locales/en.json +8 -1
  153. package/package.json +6 -6
  154. package/dist/es/models/objectOrArray.js.map +0 -1
  155. /package/dist/es/{models → types}/objectOrArray.js +0 -0
  156. /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 set the property from.
62
- * @param property The property to set
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 partial object.
77
+ * @returns The picked object.
78
78
  */
79
- static pick<T>(obj: T | undefined, keys?: (keyof T)[]): Partial<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 partial object.
91
+ * @returns The object without the omitted keys.
85
92
  */
86
- static omit<T>(obj: T | undefined, keys?: (keyof T)[]): Partial<T>;
93
+ static omit<T, K extends keyof T>(obj: T, keys?: K[]): Omit<T, K>;
87
94
  /**
88
- * Split an object into two with the specified keys.
89
- * @param obj The object to split.
90
- * @param keys The property keys to split.
91
- * @returns The two partial objects.
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 split<T>(obj: T | undefined, keys?: (keyof T)[]): {
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.
@@ -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/objectOrArray.js";
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
  }
@@ -7,7 +7,7 @@ export interface IError {
7
7
  */
8
8
  name: string;
9
9
  /**
10
- * The message for the error.
10
+ * The message for the error as an i18n key.
11
11
  */
12
12
  message: string;
13
13
  /**
@@ -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> | undefined;
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 ms, defaults to 1s.
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
  }
@@ -1,4 +1,4 @@
1
- import type { ObjectOrArray } from "../models/objectOrArray.js";
1
+ import type { ObjectOrArray } from "../types/objectOrArray.js";
2
2
  /**
3
3
  * Class to handle guard operations for parameters.
4
4
  */
@@ -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. If the lock is already held, it will wait until it is released or until the timeout is reached.
24
+ * The lock is not re-entrant: if the same thread tries to acquire the same lock again, it will deadlock until the timeout is reached.
25
+ *
26
+ * WARNING: this method calls Atomics.wait internally. On the main thread this blocks the Node.js event loop for the
27
+ * duration of the wait. Do not call from the main thread while a worker thread may simultaneously need to fetch a
28
+ * buffer for a new mutex key, as that fetch requires the main thread's message loop to be running and will deadlock.
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
+ }): 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
+ }