@twin.org/core 0.0.4-next.9 → 0.9.0

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 (46) hide show
  1. package/dist/es/index.js +3 -0
  2. package/dist/es/index.js.map +1 -1
  3. package/dist/es/models/IDuration.js +4 -0
  4. package/dist/es/models/IDuration.js.map +1 -0
  5. package/dist/es/models/coerceType.js +5 -1
  6. package/dist/es/models/coerceType.js.map +1 -1
  7. package/dist/es/types/duration.js +184 -0
  8. package/dist/es/types/duration.js.map +1 -0
  9. package/dist/es/types/durationRegExp.js +4 -0
  10. package/dist/es/types/durationRegExp.js.map +1 -0
  11. package/dist/es/types/url.js +1 -0
  12. package/dist/es/types/url.js.map +1 -1
  13. package/dist/es/utils/coerce.js +24 -0
  14. package/dist/es/utils/coerce.js.map +1 -1
  15. package/dist/es/utils/guards.js +12 -0
  16. package/dist/es/utils/guards.js.map +1 -1
  17. package/dist/es/utils/is.js +56 -7
  18. package/dist/es/utils/is.js.map +1 -1
  19. package/dist/es/utils/mutex.js +49 -8
  20. package/dist/es/utils/mutex.js.map +1 -1
  21. package/dist/es/utils/sharedObjectBuffer.js +4 -1
  22. package/dist/es/utils/sharedObjectBuffer.js.map +1 -1
  23. package/dist/types/index.d.ts +3 -0
  24. package/dist/types/models/IDuration.d.ts +45 -0
  25. package/dist/types/models/coerceType.d.ts +4 -0
  26. package/dist/types/types/duration.d.ts +29 -0
  27. package/dist/types/types/durationRegExp.d.ts +1 -0
  28. package/dist/types/types/url.d.ts +1 -0
  29. package/dist/types/utils/coerce.d.ts +9 -0
  30. package/dist/types/utils/guards.d.ts +9 -0
  31. package/dist/types/utils/is.d.ts +7 -0
  32. package/dist/types/utils/mutex.d.ts +12 -1
  33. package/docs/changelog.md +271 -0
  34. package/docs/reference/classes/Coerce.md +24 -0
  35. package/docs/reference/classes/Duration.md +88 -0
  36. package/docs/reference/classes/Guards.md +36 -0
  37. package/docs/reference/classes/Is.md +22 -0
  38. package/docs/reference/classes/Mutex.md +39 -1
  39. package/docs/reference/classes/ObjectHelper.md +8 -0
  40. package/docs/reference/classes/Url.md +4 -0
  41. package/docs/reference/index.md +3 -0
  42. package/docs/reference/interfaces/IDuration.md +83 -0
  43. package/docs/reference/variables/CoerceType.md +6 -0
  44. package/docs/reference/variables/DURATION_REG_EXP.md +3 -0
  45. package/locales/en.json +4 -2
  46. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;;;OAMG;IACI,MAAM,CAAC,OAAO,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACrE,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAClE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACpE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,WAAW,CACxB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,MAAM,CAA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C,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,kBAAkB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACnE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACH,8DAA8D;IACvD,MAAM,CAAC,QAAQ,CACrB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAI,KAAK,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACnE,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,MAAc,EACd,QAAgB,EAChB,KAAc,EACd,MAA+B;QAE/B,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;YAC/B,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBAC1B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,qBAAqB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;YACtE,CAAC;YACD,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { GuardError } from \"../errors/guardError.js\";\nimport { ArrayHelper } from \"../helpers/arrayHelper.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { ObjectOrArray } from \"../types/objectOrArray.js\";\n\n/**\n * Class to handle guard operations for parameters.\n */\nexport class Guards {\n\t/**\n\t * Is the property defined.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static defined(source: string, property: string, value: unknown): asserts value {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.undefined\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static string(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringValue(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.stringEmpty\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a JSON value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static json(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.json(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringJson\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 url string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64Url(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64Url(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64Url\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base58 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase58(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase58(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase58\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHex(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringValue(source, property, value);\n\t\tif (!HexHelper.isHex(value, allowPrefix)) {\n\t\t\tthrow new GuardError(source, \"guard.stringHex\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value with fixed length.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param length The length of the string to match.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHexLength(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringHex(source, property, value, allowPrefix);\n\t\tif (HexHelper.stripPrefix(value).length !== length) {\n\t\t\tthrow new GuardError(\n\t\t\t\tsource,\n\t\t\t\t\"guard.stringHexLength\",\n\t\t\t\tproperty,\n\t\t\t\tvalue.length,\n\t\t\t\tlength.toString()\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a number.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static number(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.number(value)) {\n\t\t\tthrow new GuardError(source, \"guard.number\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an integer.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static integer(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\tthrow new GuardError(source, \"guard.integer\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a bigint.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static bigint(source: string, property: string, value: unknown): asserts value is bigint {\n\t\tif (!Is.bigint(value)) {\n\t\t\tthrow new GuardError(source, \"guard.bigint\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a boolean.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static boolean(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is boolean {\n\t\tif (!Is.boolean(value)) {\n\t\t\tthrow new GuardError(source, \"guard.boolean\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static date(source: string, property: string, value: unknown): asserts value is Date {\n\t\tif (!Is.date(value)) {\n\t\t\tthrow new GuardError(source, \"guard.date\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-only string (ISO 8601 date, no time component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-time string (ISO 8601 with T separator).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateTimeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateTimeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateTimeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a time-only string (ISO 8601 time, no date component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.timeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in milliseconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampMilliseconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampMilliseconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampMilliseconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in seconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampSeconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampSeconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampSeconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an object with at least one property.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t\tif (Object.keys(value || {}).length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.objectValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static array<T>(source: string, property: string, value: unknown): asserts value is T[] {\n\t\tif (!Is.array<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array with at least one item.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayValue<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T[] {\n\t\tif (!Is.array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.arrayValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property one of a list of items.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayOneOf<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: T,\n\t\toptions: T[]\n\t): asserts value is T {\n\t\tif (!Is.array<T>(options)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (!options.includes(value)) {\n\t\t\tthrow new GuardError(source, \"guard.arrayOneOf\", property, value, options.join(\", \"));\n\t\t}\n\t}\n\n\t/**\n\t * Does the array start with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param startValues The values that must start the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayStartsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tstartValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst startValuesArray = ArrayHelper.fromObjectOrArray(startValues);\n\n\t\tif (!Is.arrayValue<T>(startValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, startValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < startValuesArray.length; i++) {\n\t\t\tif (value[i] !== startValuesArray[i]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayStartsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tstartValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the array end with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param endValues The values that must end the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayEndsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tendValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst endValuesArray = ArrayHelper.fromObjectOrArray(endValues);\n\n\t\tif (!Is.arrayValue<T>(endValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, endValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < endValuesArray.length; i++) {\n\t\t\tif (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayEndsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tendValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a Uint8Array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uint8Array(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is Uint8Array {\n\t\tif (!Is.uint8Array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.uint8Array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (!Is.function<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.function\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string formatted as an email address.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static email(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.email(value)) {\n\t\t\tthrow new GuardError(source, \"guard.email\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string containing uuidV7.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param format The format of the uuidV7, either standard or compact.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uuidV7(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tformat?: \"standard\" | \"compact\"\n\t): asserts value is string {\n\t\tif (!Is.uuidV7(value, format)) {\n\t\t\tif (format === \"compact\") {\n\t\t\t\tthrow new GuardError(source, \"guard.uuidV7Compact\", property, value);\n\t\t\t}\n\t\t\tthrow new GuardError(source, \"guard.uuidV7\", property, value);\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/utils/guards.ts"],"names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AI,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACnE,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,MAAc,EACd,QAAgB,EAChB,KAAc,EACd,MAA+B;QAE/B,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;YAC/B,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBAC1B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,qBAAqB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;YACtE,CAAC;YACD,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,QAAQ,CACrB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { GuardError } from \"../errors/guardError.js\";\nimport { ArrayHelper } from \"../helpers/arrayHelper.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport type { ObjectOrArray } from \"../types/objectOrArray.js\";\n\n/**\n * Class to handle guard operations for parameters.\n */\nexport class Guards {\n\t/**\n\t * Is the property defined.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static defined(source: string, property: string, value: unknown): asserts value {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.undefined\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static string(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringValue(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.stringEmpty\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a JSON value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static json(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.json(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringJson\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 url string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64Url(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64Url(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64Url\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base58 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase58(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase58(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase58\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHex(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringValue(source, property, value);\n\t\tif (!HexHelper.isHex(value, allowPrefix)) {\n\t\t\tthrow new GuardError(source, \"guard.stringHex\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value with fixed length.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param length The length of the string to match.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHexLength(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringHex(source, property, value, allowPrefix);\n\t\tif (HexHelper.stripPrefix(value).length !== length) {\n\t\t\tthrow new GuardError(\n\t\t\t\tsource,\n\t\t\t\t\"guard.stringHexLength\",\n\t\t\t\tproperty,\n\t\t\t\tvalue.length,\n\t\t\t\tlength.toString()\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a number.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static number(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.number(value)) {\n\t\t\tthrow new GuardError(source, \"guard.number\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an integer.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static integer(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\tthrow new GuardError(source, \"guard.integer\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a bigint.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static bigint(source: string, property: string, value: unknown): asserts value is bigint {\n\t\tif (!Is.bigint(value)) {\n\t\t\tthrow new GuardError(source, \"guard.bigint\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a boolean.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static boolean(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is boolean {\n\t\tif (!Is.boolean(value)) {\n\t\t\tthrow new GuardError(source, \"guard.boolean\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static date(source: string, property: string, value: unknown): asserts value is Date {\n\t\tif (!Is.date(value)) {\n\t\t\tthrow new GuardError(source, \"guard.date\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-only string (ISO 8601 date, no time component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-time string (ISO 8601 with T separator).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateTimeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateTimeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateTimeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a time-only string (ISO 8601 time, no date component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.timeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in milliseconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampMilliseconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampMilliseconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampMilliseconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in seconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampSeconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampSeconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampSeconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an object with at least one property.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t\tif (Object.keys(value || {}).length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.objectValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static array<T>(source: string, property: string, value: unknown): asserts value is T[] {\n\t\tif (!Is.array<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array with at least one item.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayValue<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T[] {\n\t\tif (!Is.array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.arrayValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property one of a list of items.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayOneOf<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: T,\n\t\toptions: T[]\n\t): asserts value is T {\n\t\tif (!Is.array<T>(options)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (!options.includes(value)) {\n\t\t\tthrow new GuardError(source, \"guard.arrayOneOf\", property, value, options.join(\", \"));\n\t\t}\n\t}\n\n\t/**\n\t * Does the array start with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param startValues The values that must start the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayStartsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tstartValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst startValuesArray = ArrayHelper.fromObjectOrArray(startValues);\n\n\t\tif (!Is.arrayValue<T>(startValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, startValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < startValuesArray.length; i++) {\n\t\t\tif (value[i] !== startValuesArray[i]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayStartsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tstartValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the array end with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param endValues The values that must end the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayEndsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tendValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst endValuesArray = ArrayHelper.fromObjectOrArray(endValues);\n\n\t\tif (!Is.arrayValue<T>(endValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, endValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < endValuesArray.length; i++) {\n\t\t\tif (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayEndsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tendValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a Uint8Array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uint8Array(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is Uint8Array {\n\t\tif (!Is.uint8Array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.uint8Array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (!Is.function<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.function\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string formatted as an email address.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static email(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.email(value)) {\n\t\t\tthrow new GuardError(source, \"guard.email\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string containing uuidV7.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param format The format of the uuidV7, either standard or compact.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uuidV7(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tformat?: \"standard\" | \"compact\"\n\t): asserts value is string {\n\t\tif (!Is.uuidV7(value, format)) {\n\t\t\tif (format === \"compact\") {\n\t\t\t\tthrow new GuardError(source, \"guard.uuidV7Compact\", property, value);\n\t\t\t}\n\t\t\tthrow new GuardError(source, \"guard.uuidV7\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a valid ISO 8601 duration string or IDuration object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static duration(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string | IDuration {\n\t\tif (!Is.duration(value)) {\n\t\t\tthrow new GuardError(source, \"guard.duration\", property, value);\n\t\t}\n\t}\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  // Copyright 2024 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  import { HexHelper } from "../helpers/hexHelper.js";
4
+ import { DURATION_REG_EXP } from "../types/durationRegExp.js";
4
5
  /**
5
6
  * Class to check types of objects.
6
7
  */
@@ -82,7 +83,6 @@ export class Is {
82
83
  */
83
84
  static stringBase64(value) {
84
85
  return (Is.stringValue(value) &&
85
- // eslint-disable-next-line unicorn/better-regex
86
86
  /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value));
87
87
  }
88
88
  /**
@@ -91,9 +91,7 @@ export class Is {
91
91
  * @returns True if the value is a base64 string.
92
92
  */
93
93
  static stringBase64Url(value) {
94
- return (Is.stringValue(value) &&
95
- // eslint-disable-next-line unicorn/better-regex
96
- /^([A-Za-z0-9-_])*$/.test(value));
94
+ return Is.stringValue(value) && /^([A-Za-z0-9-_])*$/.test(value);
97
95
  }
98
96
  /**
99
97
  * Is the value a base58 string.
@@ -101,9 +99,7 @@ export class Is {
101
99
  * @returns True if the value is a base58 string.
102
100
  */
103
101
  static stringBase58(value) {
104
- return (Is.stringValue(value) &&
105
- // eslint-disable-next-line unicorn/better-regex
106
- /^[A-HJ-NP-Za-km-z1-9]*$/.test(value));
102
+ return Is.stringValue(value) && /^[A-HJ-NP-Za-km-z1-9]*$/.test(value);
107
103
  }
108
104
  /**
109
105
  * Is the value a hex string.
@@ -363,5 +359,58 @@ export class Is {
363
359
  return (Is.stringValue(value) &&
364
360
  /^[\da-f]{8}-[\da-f]{4}-7[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i.test(value));
365
361
  }
362
+ /**
363
+ * Is the value a valid ISO 8601 duration string or an IDuration object.
364
+ * @param value The value to test.
365
+ * @returns True if the value is a valid ISO 8601 duration string or an IDuration object.
366
+ */
367
+ static duration(value) {
368
+ if (Is.object(value)) {
369
+ if (!Is.number(value.years) ||
370
+ !Is.number(value.months) ||
371
+ !Is.number(value.weeks) ||
372
+ !Is.number(value.days) ||
373
+ !Is.number(value.hours) ||
374
+ !Is.number(value.minutes) ||
375
+ !Is.number(value.seconds) ||
376
+ (!Is.undefined(value.milliseconds) &&
377
+ (!Is.integer(value.milliseconds) || Math.abs(value.milliseconds) > 999)) ||
378
+ (!Is.undefined(value.microseconds) &&
379
+ (!Is.integer(value.microseconds) || Math.abs(value.microseconds) > 999)) ||
380
+ (!Is.undefined(value.nanoseconds) &&
381
+ (!Is.integer(value.nanoseconds) || Math.abs(value.nanoseconds) > 999))) {
382
+ return false;
383
+ }
384
+ let sign = 0;
385
+ for (const component of [
386
+ value.years,
387
+ value.months,
388
+ value.weeks,
389
+ value.days,
390
+ value.hours,
391
+ value.minutes,
392
+ value.seconds,
393
+ value.milliseconds ?? 0,
394
+ value.microseconds ?? 0,
395
+ value.nanoseconds ?? 0
396
+ ]) {
397
+ if (component !== 0) {
398
+ const componentSign = component < 0 ? -1 : 1;
399
+ if (sign === 0) {
400
+ sign = componentSign;
401
+ }
402
+ else if (sign !== componentSign) {
403
+ return false;
404
+ }
405
+ }
406
+ }
407
+ return true;
408
+ }
409
+ if (!Is.stringValue(value)) {
410
+ return false;
411
+ }
412
+ const match = DURATION_REG_EXP.exec(value);
413
+ return Boolean(match?.slice(2).some(Boolean));
414
+ }
366
415
  }
367
416
  //# sourceMappingURL=is.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"is.js","sourceRoot":"","sources":["../../../src/utils/is.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD;;GAEG;AACH,MA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CACN,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC;YACrB,wHAAwH,CAAC,IAAI,CAC5H,KAAK,CACL,CACD,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,OAAO,CAAc,KAAc;QAChD,yDAAyD;QACzD,gDAAgD;QAChD,OAAO,KAAK,YAAY,OAAO,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc;QAClC,wDAAwD;QACxD,gDAAgD;QAChD,OAAO,KAAK,YAAY,MAAM,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACH,8DAA8D;IACvD,MAAM,CAAC,KAAK,CAAc,GAAY;QAC5C,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;YAC/B,OAAO,KAAK,CAAC;QACd,CAAC;QACD,MAAM,GAAG,GAAG,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClD,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc,EAAE,MAA+B;QACnE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YAC1B,uBAAuB;YACvB,kDAAkD;YAClD,2DAA2D;YAC3D,OAAO,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,4CAA4C,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1F,CAAC;QAED,OAAO,CACN,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC;YACrB,mEAAmE,CAAC,IAAI,CAAC,KAAK,CAAC,CAC/E,CAAC;IACH,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is undefined.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is null.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is null or undefined.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is not null or undefined.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a non-empty string.\n\t * @param value The value to test.\n\t * @returns True if the value is a non-empty string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^([A-Za-z0-9-_])*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^[A-HJ-NP-Za-km-z1-9]*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in seconds.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in milliseconds.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n}\n"]}
1
+ {"version":3,"file":"is.js","sourceRoot":"","sources":["../../../src/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ACvB,KAAK,CAAC,KAAK;gBACX,KAAK,CAAC,MAAM;gBACZ,KAAK,CAAC,KAAK;gBACX,KAAK,CAAC,IAAI;gBACV,KAAK,CAAC,KAAK;gBACX,KAAK,CAAC,OAAO;gBACb,KAAK,CAAC,OAAO;gBACb,KAAK,CAAC,YAAY,IAAI,CAAC;gBACvB,KAAK,CAAC,YAAY,IAAI,CAAC;gBACvB,KAAK,CAAC,WAAW,IAAI,CAAC;aACtB,EAAE,CAAC;gBACH,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC;oBACrB,MAAM,aAAa,GAAG,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC7C,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;wBAChB,IAAI,GAAG,aAAa,CAAC;oBACtB,CAAC;yBAAM,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;wBACnC,OAAO,KAAK,CAAC;oBACd,CAAC;gBACF,CAAC;YACF,CAAC;YACD,OAAO,IAAI,CAAC;QACb,CAAC;QACD,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO,KAAK,CAAC;QACd,CAAC;QACD,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport { DURATION_REG_EXP } from \"../types/durationRegExp.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is undefined.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is null.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is null or undefined.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is not null or undefined.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a non-empty string.\n\t * @param value The value to test.\n\t * @returns True if the value is a non-empty string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn Is.stringValue(value) && /^([A-Za-z0-9-_])*$/.test(value);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn Is.stringValue(value) && /^[A-HJ-NP-Za-km-z1-9]*$/.test(value);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in seconds.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in milliseconds.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a valid ISO 8601 duration string or an IDuration object.\n\t * @param value The value to test.\n\t * @returns True if the value is a valid ISO 8601 duration string or an IDuration object.\n\t */\n\tpublic static duration(value: unknown): value is string | IDuration {\n\t\tif (Is.object<IDuration>(value)) {\n\t\t\tif (\n\t\t\t\t!Is.number(value.years) ||\n\t\t\t\t!Is.number(value.months) ||\n\t\t\t\t!Is.number(value.weeks) ||\n\t\t\t\t!Is.number(value.days) ||\n\t\t\t\t!Is.number(value.hours) ||\n\t\t\t\t!Is.number(value.minutes) ||\n\t\t\t\t!Is.number(value.seconds) ||\n\t\t\t\t(!Is.undefined(value.milliseconds) &&\n\t\t\t\t\t(!Is.integer(value.milliseconds) || Math.abs(value.milliseconds) > 999)) ||\n\t\t\t\t(!Is.undefined(value.microseconds) &&\n\t\t\t\t\t(!Is.integer(value.microseconds) || Math.abs(value.microseconds) > 999)) ||\n\t\t\t\t(!Is.undefined(value.nanoseconds) &&\n\t\t\t\t\t(!Is.integer(value.nanoseconds) || Math.abs(value.nanoseconds) > 999))\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tlet sign = 0;\n\t\t\tfor (const component of [\n\t\t\t\tvalue.years,\n\t\t\t\tvalue.months,\n\t\t\t\tvalue.weeks,\n\t\t\t\tvalue.days,\n\t\t\t\tvalue.hours,\n\t\t\t\tvalue.minutes,\n\t\t\t\tvalue.seconds,\n\t\t\t\tvalue.milliseconds ?? 0,\n\t\t\t\tvalue.microseconds ?? 0,\n\t\t\t\tvalue.nanoseconds ?? 0\n\t\t\t]) {\n\t\t\t\tif (component !== 0) {\n\t\t\t\t\tconst componentSign = component < 0 ? -1 : 1;\n\t\t\t\t\tif (sign === 0) {\n\t\t\t\t\t\tsign = componentSign;\n\t\t\t\t\t} else if (sign !== componentSign) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\t\tconst match = DURATION_REG_EXP.exec(value);\n\t\treturn Boolean(match?.slice(2).some(Boolean));\n\t}\n}\n"]}
@@ -29,6 +29,11 @@ export class Mutex {
29
29
  * @internal
30
30
  */
31
31
  static _LOCKS_KEY = "mutexLocks";
32
+ /**
33
+ * SharedStore key for the default timeout in milliseconds.
34
+ * @internal
35
+ */
36
+ static _DEFAULT_TIMEOUT_KEY = "mutexDefaultTimeoutMs";
32
37
  /**
33
38
  * Cached reference to the node:worker_threads module, null if unavailable (browser).
34
39
  * @internal
@@ -36,6 +41,25 @@ export class Mutex {
36
41
  // false positive: this is a type not an actual import
37
42
  // eslint-disable-next-line @typescript-eslint/consistent-type-imports
38
43
  static _workerThreadsModule;
44
+ /**
45
+ * Gets the default timeout in milliseconds for lock acquisition.
46
+ * @returns The default timeout in milliseconds.
47
+ */
48
+ static getDefaultTimeoutMs() {
49
+ return SharedStore.get(Mutex._DEFAULT_TIMEOUT_KEY) ?? 5000;
50
+ }
51
+ /**
52
+ * Sets the default timeout in milliseconds for lock acquisition.
53
+ * @param timeoutMs The default timeout in milliseconds.
54
+ * @throws GeneralError if timeoutMs is not a non-negative integer.
55
+ */
56
+ static setDefaultTimeoutMs(timeoutMs) {
57
+ Guards.integer(Mutex.CLASS_NAME, "timeoutMs", timeoutMs);
58
+ if (timeoutMs < 0) {
59
+ throw new GeneralError(Mutex.CLASS_NAME, "invalidTimeout", { timeoutMs });
60
+ }
61
+ SharedStore.set(Mutex._DEFAULT_TIMEOUT_KEY, timeoutMs);
62
+ }
39
63
  /**
40
64
  * Acquires a lock for the given key without blocking the event loop. If the lock is already
41
65
  * held, it suspends the current async task until the lock is released or the timeout is reached.
@@ -45,14 +69,22 @@ export class Mutex {
45
69
  * the same key, it will suspend until the timeout elapses.
46
70
  * @param key The key to lock on.
47
71
  * @param options Lock options.
48
- * @param options.timeoutMs The maximum time to wait for the lock in milliseconds, default is 5000.
72
+ * @param options.timeoutMs The maximum time to wait for the lock in milliseconds, defaults to getDefaultTimeoutMs().
49
73
  * @param options.throwOnTimeout Whether to throw an error if the lock could not be acquired within the timeout, default is false.
50
74
  * @returns True if the lock was acquired, false if it timed out and throwOnTimeout is false.
51
75
  * @throws GeneralError if the key is invalid or if the lock could not be acquired within the timeout and throwOnTimeout is true.
52
76
  */
53
77
  static async lock(key, options) {
54
78
  Guards.stringValue(Mutex.CLASS_NAME, "key", key);
55
- const timeoutMs = options?.timeoutMs ?? 5000;
79
+ if (!Is.empty(options?.timeoutMs)) {
80
+ Guards.integer(Mutex.CLASS_NAME, "options.timeoutMs", options.timeoutMs);
81
+ if (options.timeoutMs < 0) {
82
+ throw new GeneralError(Mutex.CLASS_NAME, "invalidTimeout", {
83
+ timeoutMs: options.timeoutMs
84
+ });
85
+ }
86
+ }
87
+ const timeoutMs = options?.timeoutMs ?? Mutex.getDefaultTimeoutMs();
56
88
  const throwOnTimeout = options?.throwOnTimeout ?? false;
57
89
  const deadline = Date.now() + timeoutMs;
58
90
  // getOrFetchLock may block once per key on worker threads to negotiate the
@@ -117,10 +149,17 @@ export class Mutex {
117
149
  Guards.object(Mutex.CLASS_NAME, "msg.port", msg.port);
118
150
  const locks = Mutex.getLocks();
119
151
  locks[msg.key] ??= new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
120
- // Send the buffer to the worker before notifying so that it is guaranteed
121
- // to be in port1's receive queue when Atomics.wait returns on the worker side.
152
+ // Send the buffer before updating the signal so it is guaranteed to be in
153
+ // port1's receive queue when Atomics.wait returns on the worker side.
122
154
  msg.port.postMessage({ buffer: locks[msg.key].buffer });
123
- Atomics.notify(new Int32Array(msg.signal), 0, 1);
155
+ // Set signal[0] = 1 before notifying. If the OS scheduled the main thread
156
+ // to process this request before the worker reached Atomics.wait, the notify
157
+ // would fire with no waiters (lost wakeup). Setting the value first means
158
+ // Atomics.wait(signal, 0, 0) sees a non-zero value and returns "not-equal"
159
+ // immediately instead of blocking indefinitely.
160
+ const signalArr = new Int32Array(msg.signal);
161
+ Atomics.store(signalArr, 0, 1);
162
+ Atomics.notify(signalArr, 0, 1);
124
163
  msg.port.close();
125
164
  return true;
126
165
  }
@@ -163,9 +202,11 @@ export class Mutex {
163
202
  };
164
203
  wt.parentPort.postMessage(msg, [port2]);
165
204
  try {
166
- // Block until the main thread posts the response and fires Atomics.notify.
167
- // The response is guaranteed to be in port1's queue at this point because
168
- // port.postMessage executes before Atomics.notify on the main thread.
205
+ // Block until the main thread signals readiness. The main thread sets
206
+ // signal[0] = 1 before calling notify, so if the notify fired before this
207
+ // wait call (lost-wakeup scenario with concurrent workers), Atomics.wait
208
+ // sees a non-zero value and returns "not-equal" immediately.
209
+ // Either way the port message is already in port1's receive queue.
169
210
  // Use the lock deadline so the buffer fetch is bounded by the same timeout.
170
211
  const waitResult = Atomics.wait(signal, 0, 0, Math.max(0, deadline - Date.now()));
171
212
  if (waitResult === "timed-out") {
@@ -1 +1 @@
1
- {"version":3,"file":"mutex.js","sourceRoot":"","sources":["../../../src/utils/mutex.ts"],"names":[],"mappings":"AAIA,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;;;OAGG;IACH,sDAAsD;IACtD,sEAAsE;IAC9D,MAAM,CAAC,oBAAoB,CAA0D;IAE7F;;;;;;;;;;;;;OAaG;IACI,MAAM,CAAC,KAAK,CAAC,IAAI,CACvB,GAAW,EACX,OAA0D;QAE1D,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QAEvD,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,IAAI,CAAC;QAC7C,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,IAAI,KAAK,CAAC;QACxD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EA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AE,EAAE,GAAG,EAAE,CAAC,CAAC;YACxE,CAAC;YAED,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACrD,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;gBAAS,CAAC;YACV,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;IAED;;;;OAIG;IACH,sDAAsD;IACtD,sEAAsE;IAC9D,MAAM,CAAC,KAAK,CAAC,iBAAiB;QACrC,IAAI,KAAK,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;YAC9C,IAAI,CAAC;gBACJ,KAAK,CAAC,oBAAoB,GAAG,MAAM,MAAM,CAAC,qBAAqB,CAAC,CAAC;YAClE,CAAC;YAAC,MAAM,CAAC;gBACR,KAAK,CAAC,oBAAoB,GAAG,IAAI,CAAC;YACnC,CAAC;QACF,CAAC;QACD,OAAO,KAAK,CAAC,oBAAoB,CAAC;IACnC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { MessagePort } 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 * Cached reference to the node:worker_threads module, null if unavailable (browser).\n\t * @internal\n\t */\n\t// false positive: this is a type not an actual import\n\t// eslint-disable-next-line @typescript-eslint/consistent-type-imports\n\tprivate static _workerThreadsModule: typeof import(\"node:worker_threads\") | null | undefined;\n\n\t/**\n\t * Acquires a lock for the given key without blocking the event loop. If the lock is already\n\t * held, it suspends the current async task until the lock is released or the timeout is reached.\n\t * Use this in async single-threaded contexts (e.g. the main thread or a Fastify route handler)\n\t * where calling the synchronous lock() would freeze the event loop and deadlock.\n\t * The lock is not re-entrant: if the same context holds the key and calls lockAsync() again on\n\t * the same key, it will suspend until the timeout elapses.\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 async lock(\n\t\tkey: string,\n\t\toptions?: { timeoutMs?: number; throwOnTimeout?: boolean }\n\t): Promise<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\t// getOrFetchLock may block once per key on worker threads to negotiate the\n\t\t// shared buffer with the main thread; that one-time fetch is acceptable here.\n\t\tconst lock = await 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.\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// Suspend without blocking the event loop so the lock holder's async\n\t\t\t// continuations can run and eventually call unlock().\n\t\t\tconst waitResult = Atomics.waitAsync(lock, 0, 1, remaining);\n\t\t\tconst outcome = waitResult.async ? await waitResult.value : waitResult.value;\n\t\t\tif (outcome === \"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 * @param deadline The deadline to use while waiting for the main thread to provide the lock.\n\t * @returns The Int32Array backed by a SharedArrayBuffer for this key.\n\t * @internal\n\t */\n\tprivate static async getOrFetchLock(key: string, deadline: number): Promise<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\tconst wt = await Mutex.loadWorkerThreads();\n\n\t\t// Re-check after the await: another coroutine that was also waiting on\n\t\t// loadWorkerThreads() may have allocated the buffer while we yielded.\n\t\tif (!Is.empty(locks[key])) {\n\t\t\treturn locks[key];\n\t\t}\n\n\t\tif (Is.empty(wt) || wt.isMainThread) {\n\t\t\t// Main thread, fork-mode process, or browser: 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(wt.parentPort)) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"bufferFetchFailed\", { key });\n\t\t}\n\n\t\tconst { port1, port2 } = new wt.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\twt.parentPort.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 = wt.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\t/**\n\t * Lazily loads node:worker_threads, returning null in environments where it is unavailable.\n\t * @returns The worker_threads module or null.\n\t * @internal\n\t */\n\t// false positive: this is a type not an actual import\n\t// eslint-disable-next-line @typescript-eslint/consistent-type-imports\n\tprivate static async loadWorkerThreads(): Promise<typeof import(\"node:worker_threads\") | null> {\n\t\tif (Mutex._workerThreadsModule === undefined) {\n\t\t\ttry {\n\t\t\t\tMutex._workerThreadsModule = await import(\"node:worker_threads\");\n\t\t\t} catch {\n\t\t\t\tMutex._workerThreadsModule = null;\n\t\t\t}\n\t\t}\n\t\treturn Mutex._workerThreadsModule;\n\t}\n}\n"]}
1
+ {"version":3,"file":"mutex.js","sourceRoot":"","sources":["../../../src/utils/mutex.ts"],"names":[],"mappings":"AAIA,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;AAEz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,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;YAC9C,IAAI,CAAC;gBACJ,KAAK,CAAC,oBAAoB,GAAG,MAAM,MAAM,CAAC,qBAAqB,CAAC,CAAC;YAClE,CAAC;YAAC,MAAM,CAAC;gBACR,KAAK,CAAC,oBAAoB,GAAG,IAAI,CAAC;YACnC,CAAC;QACF,CAAC;QACD,OAAO,KAAK,CAAC,oBAAoB,CAAC;IACnC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { MessagePort } 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 * SharedStore key for the default timeout in milliseconds.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_TIMEOUT_KEY = \"mutexDefaultTimeoutMs\";\n\n\t/**\n\t * Cached reference to the node:worker_threads module, null if unavailable (browser).\n\t * @internal\n\t */\n\t// false positive: this is a type not an actual import\n\t// eslint-disable-next-line @typescript-eslint/consistent-type-imports\n\tprivate static _workerThreadsModule: typeof import(\"node:worker_threads\") | null | undefined;\n\n\t/**\n\t * Gets the default timeout in milliseconds for lock acquisition.\n\t * @returns The default timeout in milliseconds.\n\t */\n\tpublic static getDefaultTimeoutMs(): number {\n\t\treturn SharedStore.get<number>(Mutex._DEFAULT_TIMEOUT_KEY) ?? 5000;\n\t}\n\n\t/**\n\t * Sets the default timeout in milliseconds for lock acquisition.\n\t * @param timeoutMs The default timeout in milliseconds.\n\t * @throws GeneralError if timeoutMs is not a non-negative integer.\n\t */\n\tpublic static setDefaultTimeoutMs(timeoutMs: number): void {\n\t\tGuards.integer(Mutex.CLASS_NAME, nameof(timeoutMs), timeoutMs);\n\t\tif (timeoutMs < 0) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"invalidTimeout\", { timeoutMs });\n\t\t}\n\t\tSharedStore.set(Mutex._DEFAULT_TIMEOUT_KEY, timeoutMs);\n\t}\n\n\t/**\n\t * Acquires a lock for the given key without blocking the event loop. If the lock is already\n\t * held, it suspends the current async task until the lock is released or the timeout is reached.\n\t * Use this in async single-threaded contexts (e.g. the main thread or a Fastify route handler)\n\t * where calling the synchronous lock() would freeze the event loop and deadlock.\n\t * The lock is not re-entrant: if the same context holds the key and calls lockAsync() again on\n\t * the same key, it will suspend until the timeout elapses.\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, defaults to getDefaultTimeoutMs().\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 async lock(\n\t\tkey: string,\n\t\toptions?: { timeoutMs?: number; throwOnTimeout?: boolean }\n\t): Promise<boolean> {\n\t\tGuards.stringValue(Mutex.CLASS_NAME, nameof(key), key);\n\t\tif (!Is.empty(options?.timeoutMs)) {\n\t\t\tGuards.integer(Mutex.CLASS_NAME, nameof(options.timeoutMs), options.timeoutMs);\n\t\t\tif (options.timeoutMs < 0) {\n\t\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"invalidTimeout\", {\n\t\t\t\t\ttimeoutMs: options.timeoutMs\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst timeoutMs = options?.timeoutMs ?? Mutex.getDefaultTimeoutMs();\n\t\tconst throwOnTimeout = options?.throwOnTimeout ?? false;\n\t\tconst deadline = Date.now() + timeoutMs;\n\n\t\t// getOrFetchLock may block once per key on worker threads to negotiate the\n\t\t// shared buffer with the main thread; that one-time fetch is acceptable here.\n\t\tconst lock = await 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.\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// Suspend without blocking the event loop so the lock holder's async\n\t\t\t// continuations can run and eventually call unlock().\n\t\t\tconst waitResult = Atomics.waitAsync(lock, 0, 1, remaining);\n\t\t\tconst outcome = waitResult.async ? await waitResult.value : waitResult.value;\n\t\t\tif (outcome === \"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 before updating the signal so it is guaranteed to be in\n\t\t// port1's receive queue when Atomics.wait returns on the worker side.\n\t\tmsg.port.postMessage({ buffer: locks[msg.key].buffer });\n\t\t// Set signal[0] = 1 before notifying. If the OS scheduled the main thread\n\t\t// to process this request before the worker reached Atomics.wait, the notify\n\t\t// would fire with no waiters (lost wakeup). Setting the value first means\n\t\t// Atomics.wait(signal, 0, 0) sees a non-zero value and returns \"not-equal\"\n\t\t// immediately instead of blocking indefinitely.\n\t\tconst signalArr = new Int32Array(msg.signal);\n\t\tAtomics.store(signalArr, 0, 1);\n\t\tAtomics.notify(signalArr, 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 * @param deadline The deadline to use while waiting for the main thread to provide the lock.\n\t * @returns The Int32Array backed by a SharedArrayBuffer for this key.\n\t * @internal\n\t */\n\tprivate static async getOrFetchLock(key: string, deadline: number): Promise<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\tconst wt = await Mutex.loadWorkerThreads();\n\n\t\t// Re-check after the await: another coroutine that was also waiting on\n\t\t// loadWorkerThreads() may have allocated the buffer while we yielded.\n\t\tif (!Is.empty(locks[key])) {\n\t\t\treturn locks[key];\n\t\t}\n\n\t\tif (Is.empty(wt) || wt.isMainThread) {\n\t\t\t// Main thread, fork-mode process, or browser: 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(wt.parentPort)) {\n\t\t\tthrow new GeneralError(Mutex.CLASS_NAME, \"bufferFetchFailed\", { key });\n\t\t}\n\n\t\tconst { port1, port2 } = new wt.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\twt.parentPort.postMessage(msg, [port2]);\n\n\t\ttry {\n\t\t\t// Block until the main thread signals readiness. The main thread sets\n\t\t\t// signal[0] = 1 before calling notify, so if the notify fired before this\n\t\t\t// wait call (lost-wakeup scenario with concurrent workers), Atomics.wait\n\t\t\t// sees a non-zero value and returns \"not-equal\" immediately.\n\t\t\t// Either way the port message is already in port1's receive queue.\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 = wt.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\t/**\n\t * Lazily loads node:worker_threads, returning null in environments where it is unavailable.\n\t * @returns The worker_threads module or null.\n\t * @internal\n\t */\n\t// false positive: this is a type not an actual import\n\t// eslint-disable-next-line @typescript-eslint/consistent-type-imports\n\tprivate static async loadWorkerThreads(): Promise<typeof import(\"node:worker_threads\") | null> {\n\t\tif (Mutex._workerThreadsModule === undefined) {\n\t\t\ttry {\n\t\t\t\tMutex._workerThreadsModule = await import(\"node:worker_threads\");\n\t\t\t} catch {\n\t\t\t\tMutex._workerThreadsModule = null;\n\t\t\t}\n\t\t}\n\t\treturn Mutex._workerThreadsModule;\n\t}\n}\n"]}
@@ -1,4 +1,7 @@
1
- import { GeneralError, Is, ObjectHelper, SharedStore } from "@twin.org/core";
1
+ import { Is } from "./is.js";
2
+ import { SharedStore } from "./sharedStore.js";
3
+ import { GeneralError } from "../errors/generalError.js";
4
+ import { ObjectHelper } from "../helpers/objectHelper.js";
2
5
  import { SharedObjectBufferMessageTypes } from "../models/sharedObjectBufferMessageTypes.js";
3
6
  /**
4
7
  * Manages per-object SharedArrayBuffers that store objects as UTF-8 JSON.