@twin.org/core 0.0.3-next.8 → 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 (163) 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/randomHelper.js +50 -2
  34. package/dist/es/helpers/randomHelper.js.map +1 -1
  35. package/dist/es/helpers/stringHelper.js +11 -0
  36. package/dist/es/helpers/stringHelper.js.map +1 -1
  37. package/dist/es/index.js +8 -1
  38. package/dist/es/index.js.map +1 -1
  39. package/dist/es/models/IComponent.js +0 -2
  40. package/dist/es/models/IComponent.js.map +1 -1
  41. package/dist/es/models/IError.js.map +1 -1
  42. package/dist/es/models/IHealth.js +2 -0
  43. package/dist/es/models/IHealth.js.map +1 -0
  44. package/dist/es/models/IMutexWorkerMessage.js +2 -0
  45. package/dist/es/models/IMutexWorkerMessage.js.map +1 -0
  46. package/dist/es/models/healthStatus.js +21 -0
  47. package/dist/es/models/healthStatus.js.map +1 -0
  48. package/dist/es/models/mutexMessageTypes.js +13 -0
  49. package/dist/es/models/mutexMessageTypes.js.map +1 -0
  50. package/dist/es/types/objectOrArray.js.map +1 -0
  51. package/dist/es/types/singleOccurrenceArray.js +2 -0
  52. package/dist/es/types/singleOccurrenceArray.js.map +1 -0
  53. package/dist/es/types/singleOccurrenceArrayDepthHelper.js +2 -0
  54. package/dist/es/types/singleOccurrenceArrayDepthHelper.js.map +1 -0
  55. package/dist/es/types/urn.js +1 -2
  56. package/dist/es/types/urn.js.map +1 -1
  57. package/dist/es/utils/asyncCache.js +236 -88
  58. package/dist/es/utils/asyncCache.js.map +1 -1
  59. package/dist/es/utils/guards.js +16 -0
  60. package/dist/es/utils/guards.js.map +1 -1
  61. package/dist/es/utils/i18n.js.map +1 -1
  62. package/dist/es/utils/is.js +16 -0
  63. package/dist/es/utils/is.js.map +1 -1
  64. package/dist/es/utils/mutex.js +185 -0
  65. package/dist/es/utils/mutex.js.map +1 -0
  66. package/dist/types/encoding/base32.d.ts +1 -1
  67. package/dist/types/errors/alreadyExistsError.d.ts +1 -1
  68. package/dist/types/errors/baseError.d.ts +1 -1
  69. package/dist/types/errors/conflictError.d.ts +1 -1
  70. package/dist/types/errors/generalError.d.ts +1 -1
  71. package/dist/types/errors/guardError.d.ts +1 -1
  72. package/dist/types/errors/notFoundError.d.ts +1 -1
  73. package/dist/types/errors/notSupportedError.d.ts +1 -1
  74. package/dist/types/errors/unauthorizedError.d.ts +2 -2
  75. package/dist/types/errors/unprocessableError.d.ts +1 -1
  76. package/dist/types/factories/factory.d.ts +23 -1
  77. package/dist/types/helpers/arrayHelper.d.ts +1 -8
  78. package/dist/types/helpers/errorHelper.d.ts +11 -4
  79. package/dist/types/helpers/objectHelper.d.ts +18 -14
  80. package/dist/types/helpers/randomHelper.d.ts +16 -0
  81. package/dist/types/helpers/stringHelper.d.ts +6 -0
  82. package/dist/types/index.d.ts +8 -1
  83. package/dist/types/models/IComponent.d.ts +12 -0
  84. package/dist/types/models/IError.d.ts +1 -1
  85. package/dist/types/models/IHealth.d.ts +32 -0
  86. package/dist/types/models/IMutexWorkerMessage.d.ts +23 -0
  87. package/dist/types/models/healthStatus.d.ts +21 -0
  88. package/dist/types/models/mutexMessageTypes.d.ts +13 -0
  89. package/dist/types/types/singleOccurrenceArray.d.ts +6 -0
  90. package/dist/types/types/singleOccurrenceArrayDepthHelper.d.ts +4 -0
  91. package/dist/types/utils/asyncCache.d.ts +11 -3
  92. package/dist/types/utils/guards.d.ts +10 -1
  93. package/dist/types/utils/is.d.ts +7 -0
  94. package/dist/types/utils/mutex.d.ts +53 -0
  95. package/docs/changelog.md +945 -216
  96. package/docs/examples.md +308 -1
  97. package/docs/reference/classes/AlreadyExistsError.md +36 -36
  98. package/docs/reference/classes/ArrayHelper.md +10 -44
  99. package/docs/reference/classes/AsyncCache.md +13 -12
  100. package/docs/reference/classes/Base32.md +4 -4
  101. package/docs/reference/classes/Base58.md +3 -3
  102. package/docs/reference/classes/Base64.md +4 -4
  103. package/docs/reference/classes/Base64Url.md +3 -3
  104. package/docs/reference/classes/BaseError.md +35 -35
  105. package/docs/reference/classes/BitString.md +6 -6
  106. package/docs/reference/classes/Coerce.md +11 -11
  107. package/docs/reference/classes/Compression.md +3 -3
  108. package/docs/reference/classes/ConflictError.md +36 -36
  109. package/docs/reference/classes/Converter.md +18 -18
  110. package/docs/reference/classes/EnvHelper.md +1 -1
  111. package/docs/reference/classes/ErrorHelper.md +20 -10
  112. package/docs/reference/classes/Factory.md +112 -18
  113. package/docs/reference/classes/FilenameHelper.md +1 -1
  114. package/docs/reference/classes/GeneralError.md +36 -36
  115. package/docs/reference/classes/GuardError.md +36 -36
  116. package/docs/reference/classes/Guards.md +72 -30
  117. package/docs/reference/classes/HexHelper.md +6 -6
  118. package/docs/reference/classes/I18n.md +14 -14
  119. package/docs/reference/classes/Is.md +67 -39
  120. package/docs/reference/classes/JsonHelper.md +10 -10
  121. package/docs/reference/classes/Mutex.md +128 -0
  122. package/docs/reference/classes/NotFoundError.md +36 -36
  123. package/docs/reference/classes/NotImplementedError.md +35 -35
  124. package/docs/reference/classes/NotSupportedError.md +36 -36
  125. package/docs/reference/classes/NumberHelper.md +2 -2
  126. package/docs/reference/classes/ObjectHelper.md +135 -73
  127. package/docs/reference/classes/RandomHelper.md +53 -1
  128. package/docs/reference/classes/SharedStore.md +3 -3
  129. package/docs/reference/classes/StringHelper.md +45 -23
  130. package/docs/reference/classes/Uint8ArrayHelper.md +1 -1
  131. package/docs/reference/classes/UnauthorizedError.md +37 -37
  132. package/docs/reference/classes/UnprocessableError.md +36 -36
  133. package/docs/reference/classes/Url.md +8 -8
  134. package/docs/reference/classes/Urn.md +24 -24
  135. package/docs/reference/classes/Validation.md +25 -25
  136. package/docs/reference/classes/ValidationError.md +35 -35
  137. package/docs/reference/index.md +9 -0
  138. package/docs/reference/interfaces/IComponent.md +40 -4
  139. package/docs/reference/interfaces/IError.md +11 -11
  140. package/docs/reference/interfaces/IHealth.md +55 -0
  141. package/docs/reference/interfaces/II18nShared.md +4 -4
  142. package/docs/reference/interfaces/IKeyValue.md +2 -2
  143. package/docs/reference/interfaces/ILabelledValue.md +2 -2
  144. package/docs/reference/interfaces/ILocale.md +2 -2
  145. package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
  146. package/docs/reference/interfaces/ILocalesIndex.md +1 -1
  147. package/docs/reference/interfaces/IMutexWorkerMessage.md +35 -0
  148. package/docs/reference/interfaces/IPatchOperation.md +6 -6
  149. package/docs/reference/interfaces/IUrlParts.md +9 -9
  150. package/docs/reference/interfaces/IValidationFailure.md +4 -4
  151. package/docs/reference/type-aliases/HealthStatus.md +5 -0
  152. package/docs/reference/type-aliases/MutexMessageTypes.md +5 -0
  153. package/docs/reference/type-aliases/SingleOccurrenceArray.md +15 -0
  154. package/docs/reference/type-aliases/SingleOccurrenceArrayDepthHelper.md +19 -0
  155. package/docs/reference/variables/CoerceType.md +10 -10
  156. package/docs/reference/variables/CompressionType.md +2 -2
  157. package/docs/reference/variables/HealthStatus.md +25 -0
  158. package/docs/reference/variables/MutexMessageTypes.md +13 -0
  159. package/locales/en.json +11 -2
  160. package/package.json +6 -6
  161. package/dist/es/models/objectOrArray.js.map +0 -1
  162. /package/dist/es/{models → types}/objectOrArray.js +0 -0
  163. /package/dist/types/{models → types}/objectOrArray.d.ts +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutex.js","sourceRoot":"","sources":["../../../src/utils/mutex.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,cAAc,EAEd,YAAY,EACZ,UAAU,EACV,oBAAoB,EACpB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,KAAK;IACjB;;OAEG;IACI,MAAM,CAAU,UAAU,WAA2B;IAE5D;;;OAGG;IACK,MAAM,CAAU,UAAU,GAAG,YAAY,CAAC;IAElD;;;;;;;;;;;;;O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KAAK,CAAC,KAAK,EAAE,CAAC;QACf,CAAC;IACF,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,QAAQ;QACtB,IAAI,KAAK,GAAG,WAAW,CAAC,GAAG,CAAgC,KAAK,CAAC,UAAU,CAAC,CAAC;QAC7E,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,KAAK,GAAG,EAAE,CAAC;YACX,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tMessageChannel,\n\ttype MessagePort,\n\tisMainThread,\n\tparentPort,\n\treceiveMessageOnPort\n} from \"node:worker_threads\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { Guards } from \"./guards.js\";\nimport { Is } from \"./is.js\";\nimport { SharedStore } from \"./sharedStore.js\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport type { IMutexWorkerMessage } from \"../models/IMutexWorkerMessage.js\";\nimport { MutexMessageTypes } from \"../models/mutexMessageTypes.js\";\n\n/**\n * A cross-thread mutex built on Atomics and SharedArrayBuffer.\n *\n * When isMainThread is true (main thread or fork-mode child process) the class acts as\n * the authoritative registry: it creates a SharedArrayBuffer-backed Int32Array for each\n * key on first use and never discards it, because worker threads may hold references to\n * the same underlying memory.\n *\n * When isMainThread is false (a true worker thread) the class synchronously negotiates\n * the shared buffer with the main thread on first use of each key, then caches it locally.\n * The main thread must call Mutex.handleWorkerMessage(msg) from its worker message handler\n * before that worker first calls Mutex.lock().\n *\n * The lock is not re-entrant: a thread that already holds a key and calls lock() again on\n * the same key will block until the timeout elapses.\n */\nexport class Mutex {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Mutex>();\n\n\t/**\n\t * SharedStore key for the per-thread sparse map from lock key strings to Int32Arrays.\n\t * @internal\n\t */\n\tprivate static readonly _LOCKS_KEY = \"mutexLocks\";\n\n\t/**\n\t * 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.\n\t * 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.\n\t *\n\t * WARNING: this method calls Atomics.wait internally. On the main thread this blocks the Node.js event loop for the\n\t * duration of the wait. Do not call from the main thread while a worker thread may simultaneously need to fetch a\n\t * buffer for a new mutex key, as that fetch requires the main thread's message loop to be running and will deadlock.\n\t * @param key The key to lock on.\n\t * @param options Lock options.\n\t * @param options.timeoutMs The maximum time to wait for the lock in milliseconds, default is 5000.\n\t * @param options.throwOnTimeout Whether to throw an error if the lock could not be acquired within the timeout, default is false.\n\t * @returns True if the lock was acquired, false if it timed out and throwOnTimeout is false.\n\t * @throws GeneralError if the key is invalid or if the lock could not be acquired within the timeout and throwOnTimeout is true.\n\t */\n\tpublic static lock(\n\t\tkey: string,\n\t\toptions?: { timeoutMs?: number; throwOnTimeout?: boolean }\n\t): boolean {\n\t\tGuards.stringValue(Mutex.CLASS_NAME, nameof(key), key);\n\n\t\tconst timeoutMs = options?.timeoutMs ?? 5000;\n\t\tconst throwOnTimeout = options?.throwOnTimeout ?? false;\n\t\tconst deadline = Date.now() + timeoutMs;\n\n\t\tconst lock = Mutex.getOrFetchLock(key, deadline);\n\n\t\tfor (;;) {\n\t\t\t// Atomically swap 0 → 1; if the previous value was 0 we acquired the lock.\n\t\t\tconst previous = Atomics.compareExchange(lock, 0, 0, 1);\n\t\t\tif (previous === 0) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise, the lock is held by someone else. Check if we've already timed out before blocking.\n\t\t\tconst remaining = deadline - Date.now();\n\t\t\tif (remaining <= 0) {\n\t\t\t\tif (throwOnTimeout) {\n\t\t\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"lockTimeout\", { key, timeoutMs });\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// Block until the value changes away from 1 (i.e. the holder calls unlock)\n\t\t\t// or until the remaining timeout elapses.\n\t\t\tconst waitResult = Atomics.wait(lock, 0, 1, remaining);\n\t\t\tif (waitResult === \"timed-out\") {\n\t\t\t\tif (throwOnTimeout) {\n\t\t\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"lockTimeout\", { key, timeoutMs });\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Releases the lock for the given key.\n\t * @param key The key to unlock.\n\t * @throws GeneralError if the key is invalid or the lock is not currently held.\n\t */\n\tpublic static unlock(key: string): void {\n\t\tGuards.stringValue(Mutex.CLASS_NAME, nameof(key), key);\n\n\t\tconst locks = Mutex.getLocks();\n\t\tconst lock = locks[key];\n\t\tif (Is.empty(lock)) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"lockNotFound\", { key });\n\t\t}\n\n\t\tconst previous = Atomics.compareExchange(lock, 0, 1, 0);\n\t\tif (previous !== 1) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"lockAlreadyReleased\", { key });\n\t\t}\n\n\t\tAtomics.notify(lock, 0, 1);\n\t}\n\n\t/**\n\t * Inspect a message received from a worker and, if it is a Mutex buffer-fetch request,\n\t * respond to it synchronously. Call from the main thread's worker message handler.\n\t * @param msg The raw message received from the worker.\n\t * @returns True if the message was a Mutex protocol message and was handled, false otherwise.\n\t */\n\tpublic static handleWorkerMessage(msg: unknown): boolean {\n\t\tif (!Is.object<IMutexWorkerMessage>(msg) || msg.type !== MutexMessageTypes.GetBuffer) {\n\t\t\treturn false;\n\t\t}\n\n\t\tGuards.stringValue(Mutex.CLASS_NAME, nameof(msg.key), msg.key);\n\t\tGuards.object<SharedArrayBuffer>(Mutex.CLASS_NAME, nameof(msg.signal), msg.signal);\n\t\tGuards.object<MessagePort>(Mutex.CLASS_NAME, nameof(msg.port), msg.port);\n\n\t\tconst locks = Mutex.getLocks();\n\t\tlocks[msg.key] ??= new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));\n\t\t// Send the buffer to the worker before notifying so that it is guaranteed\n\t\t// to be in port1's receive queue when Atomics.wait returns on the worker side.\n\t\tmsg.port.postMessage({ buffer: locks[msg.key].buffer });\n\t\tAtomics.notify(new Int32Array(msg.signal), 0, 1);\n\t\tmsg.port.close();\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Returns the Int32Array for the given key, fetching it from the main thread if this\n\t * is a worker thread and the key is not yet in the local cache.\n\t * @param key The lock key.\n\t * @returns The Int32Array backed by a SharedArrayBuffer for this key.\n\t * @internal\n\t */\n\tprivate static getOrFetchLock(key: string, deadline: number): Int32Array {\n\t\tconst locks = Mutex.getLocks();\n\t\tif (!Is.empty(locks[key])) {\n\t\t\treturn locks[key];\n\t\t}\n\n\t\tif (isMainThread) {\n\t\t\t// Main thread or fork-mode process: own the registry entry.\n\t\t\tlocks[key] = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));\n\t\t\treturn locks[key];\n\t\t}\n\n\t\t// Worker thread: synchronously request the SharedArrayBuffer from the main thread.\n\t\t// Mutex.handleWorkerMessage(msg) must be called on the main thread's worker message handler.\n\t\tif (Is.empty(parentPort)) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"bufferFetchFailed\", { key });\n\t\t}\n\n\t\tconst { port1, port2 } = new MessageChannel();\n\t\tconst signal = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));\n\n\t\tconst msg: IMutexWorkerMessage = {\n\t\t\ttype: MutexMessageTypes.GetBuffer,\n\t\t\tkey,\n\t\t\tsignal: signal.buffer,\n\t\t\tport: port2\n\t\t};\n\n\t\tparentPort.postMessage(msg, [port2]);\n\n\t\ttry {\n\t\t\t// Block until the main thread posts the response and fires Atomics.notify.\n\t\t\t// The response is guaranteed to be in port1's queue at this point because\n\t\t\t// port.postMessage executes before Atomics.notify on the main thread.\n\t\t\t// Use the lock deadline so the buffer fetch is bounded by the same timeout.\n\t\t\tconst waitResult = Atomics.wait(signal, 0, 0, Math.max(0, deadline - Date.now()));\n\t\t\tif (waitResult === \"timed-out\") {\n\t\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"bufferFetchFailed\", { key });\n\t\t\t}\n\n\t\t\tconst response = receiveMessageOnPort(port1) as {\n\t\t\t\tmessage: { buffer: SharedArrayBuffer };\n\t\t\t} | null;\n\n\t\t\tif (Is.empty(response)) {\n\t\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"bufferFetchFailed\", { key });\n\t\t\t}\n\n\t\t\tlocks[key] = new Int32Array(response.message.buffer);\n\t\t\treturn locks[key];\n\t\t} finally {\n\t\t\tport1.close();\n\t\t}\n\t}\n\n\t/**\n\t * Get the shared locks map, creating it if it does not exist.\n\t * @returns The shared locks map.\n\t * @internal\n\t */\n\tprivate static getLocks(): { [key: string]: Int32Array } {\n\t\tlet locks = SharedStore.get<{ [key: string]: Int32Array }>(Mutex._LOCKS_KEY);\n\t\tif (Is.undefined(locks)) {\n\t\t\tlocks = {};\n\t\t\tSharedStore.set(Mutex._LOCKS_KEY, locks);\n\t\t}\n\t\treturn locks;\n\t}\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Class to help with base63 Encoding/Decoding.
2
+ * Class to help with base32 Encoding/Decoding.
3
3
  */
4
4
  export declare class Base32 {
5
5
  /**
@@ -10,7 +10,7 @@ export declare class AlreadyExistsError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of AlreadyExistsError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param existingId The id for the item.
15
15
  * @param properties Any additional information for the error.
16
16
  * @param cause The cause of the error if we have wrapped another error.
@@ -21,7 +21,7 @@ export declare class BaseError extends Error implements IError {
21
21
  * Create a new instance of BaseError.
22
22
  * @param name The name of the error.
23
23
  * @param source The source of the error.
24
- * @param message The message as a code.
24
+ * @param message The message as an i18n key.
25
25
  * @param properties Any additional information for the error.
26
26
  * @param cause The cause of error if we have wrapped another error.
27
27
  */
@@ -10,7 +10,7 @@ export declare class ConflictError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of ConflictError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param conflictId The id that has conflicts.
15
15
  * @param conflicts The conflicts that occurred.
16
16
  * @param properties Any additional information for the error.
@@ -10,7 +10,7 @@ export declare class GeneralError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of GeneralError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param properties Any additional information for the error.
15
15
  * @param cause The cause of the error if we have wrapped another error.
16
16
  */
@@ -10,7 +10,7 @@ export declare class GuardError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of GuardError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param propertyName The property which triggered the guard error for the item.
15
15
  * @param propertyValue The property value which triggered the guard error for the item.
16
16
  * @param propertyOptions The property options which might be allowed.
@@ -10,7 +10,7 @@ export declare class NotFoundError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of NotFoundError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param notFoundId The id for the item.
15
15
  * @param properties Any additional information for the error.
16
16
  * @param cause The cause of the error if we have wrapped another error.
@@ -10,7 +10,7 @@ export declare class NotSupportedError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of NotSupportedError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param properties Any additional information for the error.
15
15
  * @param cause The cause of the error if we have wrapped another error.
16
16
  */
@@ -1,6 +1,6 @@
1
1
  import { BaseError } from "./baseError.js";
2
2
  /**
3
- * Class to handle errors which are triggered by access not being unauthorized.
3
+ * Class to handle errors which are triggered by access not being authorized.
4
4
  */
5
5
  export declare class UnauthorizedError extends BaseError {
6
6
  /**
@@ -10,7 +10,7 @@ export declare class UnauthorizedError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of UnauthorizedError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param properties Any additional information for the error.
15
15
  * @param cause The cause of the error if we have wrapped another error.
16
16
  */
@@ -10,7 +10,7 @@ export declare class UnprocessableError extends BaseError {
10
10
  /**
11
11
  * Create a new instance of UnprocessableError.
12
12
  * @param source The source of the error.
13
- * @param message The message as a code.
13
+ * @param message The message as an i18n key.
14
14
  * @param properties Any additional information for the error.
15
15
  * @param cause The cause of the error if we have wrapped another error.
16
16
  */
@@ -29,12 +29,17 @@ export declare class Factory<T> {
29
29
  * Clear all the factories, which removes anything registered with the factories.
30
30
  */
31
31
  static clearFactories(): void;
32
+ /**
33
+ * Get the type name of the factory.
34
+ * @returns The type name of the factory.
35
+ */
36
+ typeName(): string;
32
37
  /**
33
38
  * Register a new generator.
34
39
  * @param name The name of the generator.
35
40
  * @param generator The function to create an instance.
36
41
  */
37
- register<U extends T>(name: string, generator: () => U): void;
42
+ register<U extends T>(name: string, generator: (args?: unknown) => U): void;
38
43
  /**
39
44
  * Unregister a generator.
40
45
  * @param name The name of the generator to unregister.
@@ -56,6 +61,23 @@ export declare class Factory<T> {
56
61
  * @returns An instance of the item or undefined if it does not exist.
57
62
  */
58
63
  getIfExists<U extends T>(name?: string): U | undefined;
64
+ /**
65
+ * Create a new instance without caching it.
66
+ * @param name The name of the instance to generate.
67
+ * @param args The arguments to pass to the generator.
68
+ * @returns A new instance of the item.
69
+ * @throws GuardError if the parameters are invalid.
70
+ * @throws GeneralError if no item exists to create.
71
+ */
72
+ create<U extends T>(name: string, args?: unknown): U;
73
+ /**
74
+ * Create a new instance without caching it if it exists.
75
+ * @param name The name of the instance to generate.
76
+ * @param args The arguments to pass to the generator.
77
+ * @returns A new instance of the item if it exists.
78
+ * @throws GuardError if the parameters are invalid.
79
+ */
80
+ createIfExists<U extends T>(name: string, args?: unknown): U | undefined;
59
81
  /**
60
82
  * Remove all the instances and leave the generators intact.
61
83
  */
@@ -1,4 +1,3 @@
1
- import type { ObjectOrArray } from "../models/objectOrArray.js";
2
1
  /**
3
2
  * Class to help with arrays.
4
3
  */
@@ -15,11 +14,5 @@ export declare class ArrayHelper {
15
14
  * @param value The object or array to convert.
16
15
  * @returns The array.
17
16
  */
18
- static fromObjectOrArray<T = unknown>(value: undefined): undefined;
19
- /**
20
- * Convert an object or array to an array.
21
- * @param value The object or array to convert.
22
- * @returns The array.
23
- */
24
- static fromObjectOrArray<T = unknown>(value: ObjectOrArray<T>): T[];
17
+ static fromObjectOrArray<T = unknown>(value: T | T[] | undefined): T extends undefined ? undefined : T[];
25
18
  }
@@ -6,20 +6,27 @@ export declare class ErrorHelper {
6
6
  /**
7
7
  * Format Errors and returns just their messages.
8
8
  * @param error The error to format.
9
- * @param includeDetails Whether to include error details, defaults to false.
9
+ * @param options Options for formatting the error.
10
+ * @param options.includeStack Whether to include the stack trace in the output, defaults to false.
11
+ * @param options.includeAdditional Whether to include additional error information in the output, defaults to false.
10
12
  * @returns The error formatted including any causes errors.
11
13
  */
12
- static formatErrors(error: unknown, includeDetails?: boolean): string[];
14
+ static formatErrors(error: unknown, options?: {
15
+ includeStack?: boolean;
16
+ includeAdditional?: boolean;
17
+ }): string[];
13
18
  /**
14
19
  * Localize the content of an error and any causes.
15
20
  * @param error The error to format.
16
21
  * @returns The localized version of the errors flattened.
17
22
  */
18
- static localizeErrors(error: unknown): IError[];
23
+ static localizeErrors(error: unknown): (IError & {
24
+ additional?: string[];
25
+ })[];
19
26
  /**
20
27
  * Localize the content of an error and any causes.
21
28
  * @param error The error to format.
22
29
  * @returns The localized version of the errors flattened.
23
30
  */
24
- static formatValidationErrors(error: IError): string | undefined;
31
+ static formatValidationErrors(error: IError): string[] | undefined;
25
32
  }
@@ -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.
@@ -2,10 +2,26 @@
2
2
  * Class to help with random generation.
3
3
  */
4
4
  export declare class RandomHelper {
5
+ /**
6
+ * Runtime name for the class.
7
+ */
8
+ static readonly CLASS_NAME: string;
5
9
  /**
6
10
  * Generate a new random array.
7
11
  * @param length The length of buffer to create.
8
12
  * @returns The random array.
9
13
  */
10
14
  static generate(length: number): Uint8Array;
15
+ /**
16
+ * Generate a new UUIDv7.
17
+ * @param format The format of the UUIDv7 string.
18
+ * @returns The UUIDv7 string.
19
+ */
20
+ static generateUuidV7(format?: "standard" | "compact"): string;
21
+ /**
22
+ * Extract the unix timestamp (ms) from a UUIDv7.
23
+ * @param uuid The UUIDv7 string.
24
+ * @returns The unix timestamp in milliseconds.
25
+ */
26
+ static uuidV7ExtractTimestamp(uuid: string): number;
11
27
  }
@@ -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
  */
@@ -221,4 +221,13 @@ export declare class Guards {
221
221
  * @throws GuardError If the value does not match the assertion.
222
222
  */
223
223
  static email(source: string, property: string, value: unknown): asserts value is string;
224
+ /**
225
+ * Is the property a string containing uuidV7.
226
+ * @param source The source of the error.
227
+ * @param property The name of the property.
228
+ * @param value The value to test.
229
+ * @param format The format of the uuidV7, either standard or compact.
230
+ * @throws GuardError If the value does not match the assertion.
231
+ */
232
+ static uuidV7(source: string, property: string, value: unknown, format?: "standard" | "compact"): asserts value is string;
224
233
  }
@@ -220,4 +220,11 @@ export declare class Is {
220
220
  * @returns True if the object is a class, false otherwise.
221
221
  */
222
222
  static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T;
223
+ /**
224
+ * Is the value a uuidV7 string.
225
+ * @param value The value to test.
226
+ * @param format The format of the UUIDv7 string.
227
+ * @returns True if the value is a uuidV7 string.
228
+ */
229
+ static uuidV7(value: unknown, format?: "standard" | "compact"): value is string;
223
230
  }
@@ -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
+ }