@cyberskill/shared 3.17.0 → 3.18.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.
@@ -1 +1 @@
1
- {"version":3,"file":"string.util.js","names":[],"sources":["../../../src/util/string/string.util.ts"],"sourcesContent":["import type { T_Object } from '#typescript/index.js';\n\nimport type { I_SlugifyOptions } from './string.type.js';\n\nimport { removeAccent } from '../common/common.util.js';\n\nconst RE_NON_ALNUM = /[^a-z0-9\\s-]/gi;\nconst RE_MULTI_SPACE_DASH = /[\\s-]+/g;\nconst RE_LEADING_TRAILING_DASH = /^-+|-+$/g;\nconst RE_HYPHEN = /-/g;\nconst RE_QUERY_FRAGMENT = /[?#]/;\nconst RE_HAS_LOWER = /[a-z]/;\nconst RE_HAS_UPPER = /[A-Z]/;\nconst RE_HAS_DIGIT = /\\d/;\nconst RE_HAS_SPECIAL = /[!@#$%^&*()_+[\\]{}|;:,.<>?]/;\n\n/**\n * Generates a slug from a string.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens.\n *\n * @param input - The string to be slugified.\n * @param options - Options for slugification.\n * @returns The slugified string.\n */\nfunction slugify(input: string, options?: I_SlugifyOptions): string {\n let slug = input.trim();\n\n // 1. Remove accents\n slug = removeAccent(slug);\n\n // 2. To lower case if requested (default true)\n if (options?.lower !== false) {\n slug = slug.toLowerCase();\n }\n\n // 3. Replace invalid characters with space (keeping alphanumeric, hyphens, and spaces)\n slug = slug.replace(RE_NON_ALNUM, ' ');\n\n // 4. Replace multiple spaces or hyphens with a single hyphen\n slug = slug.replace(RE_MULTI_SPACE_DASH, '-');\n\n // 5. Remove leading/trailing hyphens\n slug = slug.replace(RE_LEADING_TRAILING_DASH, '');\n\n return slug;\n}\n\n/**\n * Generates a slug from a string or an object containing strings.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens. This function can handle both single strings\n * and objects with string values.\n *\n * @param input - The string or object to be slugified.\n * @param options - Options for slugification including replacement character, case sensitivity, locale, etc.\n * @returns The slugified string or object with the same structure as the input.\n */\nexport function generateSlug<T = string>(\n input: T,\n options?: I_SlugifyOptions,\n): T {\n const slugifyWithOptions = (value: string) =>\n slugify(value ?? '', options);\n\n if (typeof input === 'object' && input !== null) {\n const result: T_Object = {};\n\n for (const [key, value] of Object.entries(input)) {\n result[key] = slugifyWithOptions(value as string);\n }\n\n return result as T;\n }\n\n return slugifyWithOptions(input as string) as T;\n}\n\n/**\n * Generates a short ID from a UUID.\n * The ID is a substring of the UUID, providing a shorter identifier.\n * Note: This is NOT cryptographically secure and collisions are possible,\n * but suitable for display purposes where uniqueness is handled elsewhere.\n *\n * @param uuid - The UUID to be converted to a short ID.\n * @param length - The desired length of the short ID (default: 4 characters).\n * @returns A short ID string of the specified length derived from the UUID.\n */\nexport function generateShortId(uuid: string, length = 4): string {\n // Simple hash function (FNV-1a variant) to generate a hex string from the UUID\n let hash = 0x811C9DC5;\n for (let i = 0; i < uuid.length; i++) {\n hash ^= uuid.charCodeAt(i);\n hash = Math.imul(hash, 0x01000193);\n }\n // Convert to unsigned 32-bit integer hex string\n const hex = (hash >>> 0).toString(16).padStart(8, '0');\n\n // If we need more than 8 chars, we can just append part of the original UUID (stripped of dashes)\n // or use a different strategy. For short IDs (usually < 8), the hash is fine.\n // If length > 8, we fallback to just slicing the clean UUID.\n if (length > 8) {\n return uuid.replace(RE_HYPHEN, '').slice(0, length);\n }\n\n return hex.slice(0, length);\n}\n\n/**\n * Internal helper that fills `length` characters from `charset` using\n * rejection-sampling over `crypto.getRandomValues` to avoid modulo bias.\n * Random values are requested in bounded chunks to keep allocations small.\n *\n * @param length - Number of characters to generate.\n * @param charset - The pool of characters to draw from.\n * @returns A randomly generated string of the requested length.\n */\nfunction generateRandomFromCharset(length: number, charset: string): string {\n const limit = Math.floor(2 ** 32 / charset.length) * charset.length;\n const result: string[] = [];\n const MAX_UINT32_VALUES_PER_CALL = 16384;\n\n while (result.length < length) {\n const remaining = length - result.length;\n const chunkSize = remaining > MAX_UINT32_VALUES_PER_CALL ? MAX_UINT32_VALUES_PER_CALL : remaining;\n const values = new Uint32Array(chunkSize);\n crypto.getRandomValues(values);\n\n for (const value of values) {\n if (result.length >= length) {\n break;\n }\n if (value < limit) {\n result.push(charset[value % charset.length] as string);\n }\n }\n }\n\n return result.join('');\n}\n\n/**\n * Generates a random password of a given length.\n * The password contains a mix of letters (both cases), numbers, and special characters\n * to ensure complexity and security.\n *\n * @param length - The desired length of the password (default: 8 characters).\n * @returns A randomly generated password string with the specified length.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n */\nexport function generateRandomPassword(length = 8): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n const lower = 'abcdefghijklmnopqrstuvwxyz';\n const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n const digits = '0123456789';\n const special = '!@#$%^&*()_+[]{}|;:,.<>?';\n const charset = lower + upper + digits + special;\n\n const password = generateRandomFromCharset(length, charset);\n\n // Ensure at least one char from each class when length >= 4\n if (length >= 4) {\n const hasLower = RE_HAS_LOWER.test(password);\n const hasUpper = RE_HAS_UPPER.test(password);\n const hasDigit = RE_HAS_DIGIT.test(password);\n const hasSpecial = RE_HAS_SPECIAL.test(password);\n\n if (hasLower && hasUpper && hasDigit && hasSpecial) {\n return password;\n }\n\n // Replace random positions with missing classes to avoid predictable placement\n const chars = [...password];\n const missing: { test: boolean; pool: string }[] = [\n { test: hasLower, pool: lower },\n { test: hasUpper, pool: upper },\n { test: hasDigit, pool: digits },\n { test: hasSpecial, pool: special },\n ];\n\n // Collect random, unique indices via Fisher-Yates partial shuffle\n const indices = Array.from({ length }, (_, i) => i);\n for (let i = indices.length - 1; i > 0; i--) {\n const buf = new Uint32Array(1);\n crypto.getRandomValues(buf);\n const j = buf[0]! % (i + 1);\n [indices[i], indices[j]] = [indices[j]!, indices[i]!];\n }\n\n let pos = 0;\n for (const { test, pool } of missing) {\n if (!test) {\n chars[indices[pos]!] = generateRandomFromCharset(1, pool);\n pos++;\n }\n }\n\n return chars.join('');\n }\n\n return password;\n}\n\n/**\n * Generates a random string of a given length using a secure random number generator.\n * This function is a cryptographically secure alternative to Math.random().toString(36).\n *\n * @param length - The desired length of the string (default: 8 characters).\n * @param charset - The characters to use (default: lowercase alphanumeric).\n * @returns A randomly generated string.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n * @throws {RangeError} If `charset` is empty or exceeds 2^32 characters.\n */\nexport function generateRandomString(\n length = 8,\n charset = 'abcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n if (charset.length === 0 || charset.length > 2 ** 32) {\n throw new RangeError('charset.length must be between 1 and 2^32');\n }\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Get the file name from a URL.\n * This function extracts the file name from a URL, optionally including or excluding\n * the file extension. It handles URLs with query parameters and fragments.\n *\n * @param url - The URL to extract the file name from (default: empty string).\n * @param getExtension - Whether to include the file extension in the result (default: false).\n * @returns The file name extracted from the URL, with or without the extension.\n */\nexport function getFileName(url = '', getExtension = false): string {\n const withoutQuery = url.split(RE_QUERY_FRAGMENT)[0] || '';\n const fileName = withoutQuery.substring(withoutQuery.lastIndexOf('/') + 1);\n\n if (getExtension) {\n return fileName;\n }\n\n const dotIndex = fileName.lastIndexOf('.');\n\n return dotIndex > 0 ? fileName.slice(0, dotIndex) : fileName;\n}\n\n/**\n * Extracts a substring between two strings.\n * This function finds the first occurrence of the starting string, then finds the first\n * occurrence of the ending string after the starting string, and returns the content between them.\n *\n * @param s - The original string to search within.\n * @param a - The starting string that marks the beginning of the desired substring.\n * @param b - The ending string that marks the end of the desired substring.\n * @returns The substring between the two specified strings, or an empty string if either string is not found.\n */\nexport function substringBetween(s: string, a: string, b: string): string {\n const start = s.indexOf(a);\n\n if (start === -1) {\n return '';\n }\n\n const from = start + a.length;\n const end = s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ACV,OAAK,IAAM,EAAE,SAAM,aAAU,EACzB,CAAK,MACD,EAAM,EAAQ,MAAS,EAA0B,GAAG,EAAK,EACzD;AAIR,SAAO,EAAM,KAAK,GAAG;;AAGzB,QAAO;;AAaX,SAAgB,EACZ,IAAS,GACT,IAAU,wCACJ;AACN,KAAI,CAAC,OAAO,cAAc,EAAO,IAAI,IAAS,EAC1C,OAAU,WAAW,6CAA6C;AAGtE,KAAI,EAAQ,WAAW,KAAK,EAAQ,SAAS,KAAK,GAC9C,OAAU,WAAW,4CAA4C;AAGrE,QAAO,EAA0B,GAAQ,EAAQ;;AAYrD,SAAgB,EAAY,IAAM,IAAI,IAAe,IAAe;CAChE,IAAM,IAAe,EAAI,MAAM,EAAkB,CAAC,MAAM,IAClD,IAAW,EAAa,UAAU,EAAa,YAAY,IAAI,GAAG,EAAE;AAE1E,KAAI,EACA,QAAO;CAGX,IAAM,IAAW,EAAS,YAAY,IAAI;AAE1C,QAAO,IAAW,IAAI,EAAS,MAAM,GAAG,EAAS,GAAG;;AAaxD,SAAgB,EAAiB,GAAW,GAAW,GAAmB;CACtE,IAAM,IAAQ,EAAE,QAAQ,EAAE;AAE1B,KAAI,MAAU,GACV,QAAO;CAGX,IAAM,IAAO,IAAQ,EAAE,QACjB,IAAM,EAAE,QAAQ,GAAG,EAAK;AAM9B,QAJI,MAAQ,KACD,KAGJ,EAAE,MAAM,GAAM,EAAI"}
1
+ {"version":3,"file":"string.util.js","names":[],"sources":["../../../src/util/string/string.util.ts"],"sourcesContent":["import type { T_Object } from '#typescript/index.js';\n\nimport type { I_SlugifyOptions } from './string.type.js';\n\nimport { removeAccent } from '../common/common.util.js';\n\nconst RE_NON_ALNUM = /[^a-z0-9\\s-]/gi;\nconst RE_MULTI_SPACE_DASH = /[\\s-]+/g;\nconst RE_LEADING_TRAILING_DASH = /^-+|-+$/g;\nconst RE_HYPHEN = /-/g;\nconst RE_QUERY_FRAGMENT = /[?#]/;\nconst RE_HAS_LOWER = /[a-z]/;\nconst RE_HAS_UPPER = /[A-Z]/;\nconst RE_HAS_DIGIT = /\\d/;\nconst RE_HAS_SPECIAL = /[!@#$%^&*()_+[\\]{}|;:,.<>?]/;\n\n/**\n * Generates a slug from a string.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens.\n *\n * @param input - The string to be slugified.\n * @param options - Options for slugification.\n * @returns The slugified string.\n */\nfunction slugify(input: string, options?: I_SlugifyOptions): string {\n let slug = input.trim();\n\n // 1. Remove accents\n slug = removeAccent(slug);\n\n // 2. To lower case if requested (default true)\n if (options?.lower !== false) {\n slug = slug.toLowerCase();\n }\n\n // 3. Replace invalid characters with space (keeping alphanumeric, hyphens, and spaces)\n slug = slug.replace(RE_NON_ALNUM, ' ');\n\n // 4. Replace multiple spaces or hyphens with a single hyphen\n slug = slug.replace(RE_MULTI_SPACE_DASH, '-');\n\n // 5. Remove leading/trailing hyphens\n slug = slug.replace(RE_LEADING_TRAILING_DASH, '');\n\n return slug;\n}\n\n/**\n * Generates a slug from a string or an object containing strings.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens. This function can handle both single strings\n * and objects with string values.\n *\n * @param input - The string or object to be slugified.\n * @param options - Options for slugification including replacement character, case sensitivity, locale, etc.\n * @returns The slugified string or object with the same structure as the input.\n */\nexport function generateSlug<T = string>(\n input: T,\n options?: I_SlugifyOptions,\n): T {\n const slugifyWithOptions = (value: string) =>\n slugify(value ?? '', options);\n\n if (typeof input === 'object' && input !== null) {\n const result: T_Object = {};\n\n for (const [key, value] of Object.entries(input)) {\n result[key] = slugifyWithOptions(value as string);\n }\n\n return result as T;\n }\n\n return slugifyWithOptions(input as string) as T;\n}\n\n/**\n * Generates a short ID from a UUID.\n * The ID is a substring of the UUID, providing a shorter identifier.\n * Note: This is NOT cryptographically secure and collisions are possible,\n * but suitable for display purposes where uniqueness is handled elsewhere.\n *\n * @param uuid - The UUID to be converted to a short ID.\n * @param length - The desired length of the short ID (default: 4 characters).\n * @returns A short ID string of the specified length derived from the UUID.\n */\nexport function generateShortId(uuid: string, length = 4): string {\n // Simple hash function (FNV-1a variant) to generate a hex string from the UUID\n let hash = 0x811C9DC5;\n for (let i = 0; i < uuid.length; i++) {\n hash ^= uuid.charCodeAt(i);\n hash = Math.imul(hash, 0x01000193);\n }\n // Convert to unsigned 32-bit integer hex string\n const hex = (hash >>> 0).toString(16).padStart(8, '0');\n\n // If we need more than 8 chars, we can just append part of the original UUID (stripped of dashes)\n // or use a different strategy. For short IDs (usually < 8), the hash is fine.\n // If length > 8, we fallback to just slicing the clean UUID.\n if (length > 8) {\n return uuid.replace(RE_HYPHEN, '').slice(0, length);\n }\n\n return hex.slice(0, length);\n}\n\n/**\n * Internal helper that fills `length` characters from `charset` using\n * rejection-sampling over `crypto.getRandomValues` to avoid modulo bias.\n * Random values are requested in bounded chunks to keep allocations small.\n *\n * @param length - Number of characters to generate.\n * @param charset - The pool of characters to draw from.\n * @returns A randomly generated string of the requested length.\n */\nfunction generateRandomFromCharset(length: number, charset: string): string {\n const limit = Math.floor(2 ** 32 / charset.length) * charset.length;\n const result: string[] = [];\n const MAX_UINT32_VALUES_PER_CALL = 16384;\n\n while (result.length < length) {\n const remaining = length - result.length;\n const chunkSize = remaining > MAX_UINT32_VALUES_PER_CALL ? MAX_UINT32_VALUES_PER_CALL : remaining;\n const values = new Uint32Array(chunkSize);\n crypto.getRandomValues(values);\n\n for (const value of values) {\n if (result.length >= length) {\n break;\n }\n if (value < limit) {\n result.push(charset[value % charset.length] as string);\n }\n }\n }\n\n return result.join('');\n}\n\n/**\n * Generates a random password of a given length.\n * The password contains a mix of letters (both cases), numbers, and special characters\n * to ensure complexity and security.\n *\n * @param length - The desired length of the password (default: 8 characters).\n * @returns A randomly generated password string with the specified length.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n */\nexport function generateRandomPassword(length = 8): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n const lower = 'abcdefghijklmnopqrstuvwxyz';\n const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n const digits = '0123456789';\n const special = '!@#$%^&*()_+[]{}|;:,.<>?';\n const charset = lower + upper + digits + special;\n\n const password = generateRandomFromCharset(length, charset);\n\n // Ensure at least one char from each class when length >= 4\n if (length >= 4) {\n const hasLower = RE_HAS_LOWER.test(password);\n const hasUpper = RE_HAS_UPPER.test(password);\n const hasDigit = RE_HAS_DIGIT.test(password);\n const hasSpecial = RE_HAS_SPECIAL.test(password);\n\n if (hasLower && hasUpper && hasDigit && hasSpecial) {\n return password;\n }\n\n // If the pure random string lacks required classes, construct a guaranteed-valid one\n const chars = [\n generateRandomFromCharset(1, lower),\n generateRandomFromCharset(1, upper),\n generateRandomFromCharset(1, digits),\n generateRandomFromCharset(1, special),\n ];\n\n // Fill the rest with random characters\n if (length > 4) {\n const rest = generateRandomFromCharset(length - 4, charset);\n for (const c of rest) {\n chars.push(c);\n }\n }\n\n // Fisher-Yates shuffle using rejection sampling to avoid modulo bias\n for (let i = chars.length - 1; i > 0; i--) {\n const range = i + 1;\n // 0x1_0000_0000 = 2^32, the exclusive upper bound of Uint32Array values\n const maxUnbiased = Math.floor(0x1_0000_0000 / range) * range;\n const buf = new Uint32Array(1);\n let randomValue: number;\n\n do {\n crypto.getRandomValues(buf);\n randomValue = buf[0]!;\n } while (randomValue >= maxUnbiased);\n\n const j = randomValue % range;\n [chars[i], chars[j]] = [chars[j]!, chars[i]!];\n }\n\n return chars.join('');\n }\n\n return password;\n}\n\n/**\n * Generates a random string of a given length using a secure random number generator.\n * This function is a cryptographically secure alternative to Math.random().toString(36).\n *\n * @param length - The desired length of the string (default: 8 characters).\n * @param charset - The characters to use (default: lowercase alphanumeric).\n * @returns A randomly generated string.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n * @throws {RangeError} If `charset` is empty or exceeds 2^32 characters.\n */\nexport function generateRandomString(\n length = 8,\n charset = 'abcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n if (charset.length === 0 || charset.length > 2 ** 32) {\n throw new RangeError('charset.length must be between 1 and 2^32');\n }\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Get the file name from a URL.\n * This function extracts the file name from a URL, optionally including or excluding\n * the file extension. It handles URLs with query parameters and fragments.\n *\n * @param url - The URL to extract the file name from (default: empty string).\n * @param getExtension - Whether to include the file extension in the result (default: false).\n * @returns The file name extracted from the URL, with or without the extension.\n */\nexport function getFileName(url = '', getExtension = false): string {\n const withoutQuery = url.split(RE_QUERY_FRAGMENT)[0] || '';\n const fileName = withoutQuery.substring(withoutQuery.lastIndexOf('/') + 1);\n\n if (getExtension) {\n return fileName;\n }\n\n const dotIndex = fileName.lastIndexOf('.');\n\n return dotIndex > 0 ? fileName.slice(0, dotIndex) : fileName;\n}\n\n/**\n * Extracts a substring between two strings.\n * This function finds the first occurrence of the starting string, then finds the first\n * occurrence of the ending string after the starting string, and returns the content between them.\n *\n * @param s - The original string to search within.\n * @param a - The starting string that marks the beginning of the desired substring.\n * @param b - The ending string that marks the end of the desired substring.\n * @returns The substring between the two specified strings, or an empty string if either string is not found.\n */\nexport function substringBetween(s: string, a: string, b: string): string {\n const start = s.indexOf(a);\n\n if (start === -1) {\n return '';\n }\n\n const from = start + a.length;\n const end = s.indexOf(b, from);\n\n if (end === -1) {\n return '';\n }\n\n return s.slice(from, end);\n}\n"],"mappings":";;AAMA,IAAM,IAAe,kBACf,IAAsB,WACtB,IAA2B,YAC3B,IAAY,MACZ,IAAoB,QACpB,IAAe,SACf,IAAe,SACf,IAAe,MACf,IAAiB;AAWvB,SAAS,EAAQ,GAAe,GAAoC;CAChE,IAAI,IAAO,EAAM,MAAM;AAmBvB,QAhBA,IAAO,EAAa,EAAK,EAGrB,GAAS,UAAU,OACnB,IAAO,EAAK,aAAa,GAI7B,IAAO,EAAK,QAAQ,GAAc,IAAI,EAGtC,I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I,IAAe,IAAe;CAChE,IAAM,IAAe,EAAI,MAAM,EAAkB,CAAC,MAAM,IAClD,IAAW,EAAa,UAAU,EAAa,YAAY,IAAI,GAAG,EAAE;AAE1E,KAAI,EACA,QAAO;CAGX,IAAM,IAAW,EAAS,YAAY,IAAI;AAE1C,QAAO,IAAW,IAAI,EAAS,MAAM,GAAG,EAAS,GAAG;;AAaxD,SAAgB,EAAiB,GAAW,GAAW,GAAmB;CACtE,IAAM,IAAQ,EAAE,QAAQ,EAAE;AAE1B,KAAI,MAAU,GACV,QAAO;CAGX,IAAM,IAAO,IAAQ,EAAE,QACjB,IAAM,EAAE,QAAQ,GAAG,EAAK;AAM9B,QAJI,MAAQ,KACD,KAGJ,EAAE,MAAM,GAAM,EAAI"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cyberskill/shared",
3
3
  "type": "module",
4
- "version": "3.17.0",
4
+ "version": "3.18.0",
5
5
  "description": "CyberSkill Shared",
6
6
  "author": "Stephen Cheng",
7
7
  "license": "MIT",
@@ -255,8 +255,8 @@
255
255
  "@eslint-react/eslint-plugin": "3.0.0",
256
256
  "@graphql-codegen/cli": "6.2.1",
257
257
  "@graphql-codegen/client-preset": "5.2.4",
258
- "@nestjs/common": "11.1.17",
259
- "@nestjs/core": "11.1.17",
258
+ "@nestjs/common": "11.1.18",
259
+ "@nestjs/core": "11.1.18",
260
260
  "@userback/widget": "0.3.12",
261
261
  "@vitejs/plugin-react": "6.0.1",
262
262
  "body-parser": "2.2.2",
@@ -324,7 +324,7 @@
324
324
  "@types/fs-extra": "11.0.4",
325
325
  "@types/graphql-upload": "17.0.0",
326
326
  "@types/migrate-mongo": "10.0.6",
327
- "@types/node": "25.5.0",
327
+ "@types/node": "25.5.2",
328
328
  "@types/react": "19.2.14",
329
329
  "@types/react-dom": "19.2.3",
330
330
  "@types/ws": "8.18.1",
@@ -339,7 +339,7 @@
339
339
  "lint-staged": "16.4.0",
340
340
  "node-modules-inspector": "1.4.2",
341
341
  "npm-run-all2": "8.0.4",
342
- "sass": "1.98.0",
342
+ "sass": "1.99.0",
343
343
  "semantic-release": "25.0.3",
344
344
  "simple-git-hooks": "2.13.1",
345
345
  "tsx": "4.21.0",