@unchainedshop/utils 4.8.21 → 4.8.23

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,12 +1,3 @@
1
- import Hashids from 'hashids';
2
- const hashids = new Hashids('unchained', 6, 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789');
3
- const RANGE = 999999998;
4
- const REJECTION_LIMIT = Math.floor(0x100000000 / RANGE) * RANGE;
5
- export default () => {
6
- let value = crypto.getRandomValues(new Uint32Array(1))[0];
7
- while (value >= REJECTION_LIMIT) {
8
- value = crypto.getRandomValues(new Uint32Array(1))[0];
9
- }
10
- const randomNumber = (value % RANGE) + 1;
11
- return hashids.encode(randomNumber);
12
- };
1
+ import { randomInt } from 'node:crypto';
2
+ const ALPHABET = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
3
+ export default () => Array.from({ length: 6 }, () => ALPHABET[randomInt(ALPHABET.length)]).join('');
@@ -1,8 +1,9 @@
1
- type AsyncFunction = (...args: any[]) => Promise<any>;
2
- interface MemoizeOptions {
3
- cacheKey?: (args: any[]) => string;
4
- }
5
- export declare function memoizeWithTTL<T extends AsyncFunction>(fn: T, ttlMs: number, options?: MemoizeOptions): T & {
1
+ export interface MemoizedWithTTL<A extends unknown[], R> {
2
+ (...args: A): Promise<R>;
6
3
  clear: () => void;
7
- };
8
- export {};
4
+ delete: (key: unknown) => boolean;
5
+ }
6
+ export default function memoizeWithTTL<A extends unknown[], R>(fn: (...args: A) => Promise<R>, { ttl, cacheKey }: {
7
+ ttl: number;
8
+ cacheKey?: (args: A) => unknown;
9
+ }): MemoizedWithTTL<A, R>;
@@ -1,35 +1,27 @@
1
- function createTTLCache(ttlMs) {
2
- const store = new Map();
3
- return {
4
- get(key) {
5
- const entry = store.get(key);
6
- if (!entry)
7
- return undefined;
8
- if (Date.now() > entry.expiry) {
9
- store.delete(key);
10
- return undefined;
11
- }
12
- return entry.value;
13
- },
14
- set(key, value) {
15
- store.set(key, { value, expiry: Date.now() + ttlMs });
16
- },
17
- clear() {
18
- store.clear();
19
- },
20
- };
21
- }
22
- export function memoizeWithTTL(fn, ttlMs, options) {
23
- const cache = createTTLCache(ttlMs);
24
- const memoized = async function (...args) {
25
- const key = options?.cacheKey ? options.cacheKey(args) : JSON.stringify(args);
26
- const cached = cache.get(key);
27
- if (cached !== undefined)
28
- return cached;
29
- const result = await fn(...args);
30
- cache.set(key, result);
31
- return result;
1
+ export default function memoizeWithTTL(fn, { ttl, cacheKey = (args) => args[0] }) {
2
+ const cache = new Map();
3
+ const memoized = (...args) => {
4
+ const key = cacheKey(args);
5
+ const now = Date.now();
6
+ const hit = cache.get(key);
7
+ if (hit && (!hit.settled || hit.expires > now))
8
+ return hit.promise;
9
+ for (const [expiredKey, entry] of cache) {
10
+ if (entry.settled && entry.expires <= now)
11
+ cache.delete(expiredKey);
12
+ }
13
+ const entry = { promise: fn(...args), settled: false, expires: Infinity };
14
+ cache.set(key, entry);
15
+ entry.promise.then(() => {
16
+ entry.settled = true;
17
+ entry.expires = Date.now() + ttl;
18
+ }, () => {
19
+ if (cache.get(key) === entry)
20
+ cache.delete(key);
21
+ });
22
+ return entry.promise;
32
23
  };
33
24
  memoized.clear = () => cache.clear();
25
+ memoized.delete = (key) => cache.delete(key);
34
26
  return memoized;
35
27
  }
@@ -5,6 +5,7 @@ export { default as objectInvert } from './object-invert.ts';
5
5
  export { default as findUnusedSlug } from './find-unused-slug.ts';
6
6
  export { default as slugify } from './slugify.ts';
7
7
  export { default as generateRandomHash } from './generate-random-hash.ts';
8
+ export { default as memoizeWithTTL } from './memoize-with-ttl.ts';
8
9
  export { default as buildObfuscatedFieldsFilter } from './build-obfuscated-fields-filter.ts';
9
10
  export { default as sha256 } from './sha256.ts';
10
11
  export { default as sha1 } from './sha1.ts';
@@ -5,6 +5,7 @@ export { default as objectInvert } from "./object-invert.js";
5
5
  export { default as findUnusedSlug } from "./find-unused-slug.js";
6
6
  export { default as slugify } from "./slugify.js";
7
7
  export { default as generateRandomHash } from "./generate-random-hash.js";
8
+ export { default as memoizeWithTTL } from "./memoize-with-ttl.js";
8
9
  export { default as buildObfuscatedFieldsFilter } from "./build-obfuscated-fields-filter.js";
9
10
  export { default as sha256 } from "./sha256.js";
10
11
  export { default as sha1 } from "./sha1.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unchainedshop/utils",
3
3
  "description": "Common utility functions and base classes for the Unchained Engine",
4
- "version": "4.8.21",
4
+ "version": "4.8.23",
5
5
  "main": "lib/utils-index.js",
6
6
  "types": "lib/utils-index.d.ts",
7
7
  "type": "module",
@@ -30,7 +30,6 @@
30
30
  "homepage": "https://github.com/unchainedshop/unchained#readme",
31
31
  "dependencies": {
32
32
  "@unchainedshop/logger": "^4.8.12",
33
- "hashids": "^2.3.0",
34
33
  "libphonenumber-js": "^1.13.11",
35
34
  "resolve-accept-language": "^3.1.11"
36
35
  },