Refactor: replace typeson w/ devalue

We don't need to stringify promise, so devalue is good
This commit is contained in:
SukkaW 2024-10-30 03:40:55 +08:00
parent 4f9c2a5b83
commit 8e96c36ea0
3 changed files with 34 additions and 72 deletions

View File

@ -5,18 +5,9 @@ import { isCI } from 'ci-info';
import { xxhash64 } from 'hash-wasm'; import { xxhash64 } from 'hash-wasm';
import { Typeson, set, map, typedArrays, undef, infinity } from 'typeson-registry';
import picocolors from 'picocolors'; import picocolors from 'picocolors';
import { identity } from './misc'; import { identity } from './misc';
const typeson = new Typeson().register([
typedArrays,
set,
map,
undef,
infinity
]);
const fsMemoCache = new Cache({ cachePath: path.resolve(__dirname, '../../.cache'), tableName: 'fs_memo_cache' }); const fsMemoCache = new Cache({ cachePath: path.resolve(__dirname, '../../.cache'), tableName: 'fs_memo_cache' });
const TTL = isCI const TTL = isCI
@ -25,39 +16,48 @@ const TTL = isCI
// We run locally less frequently, so we need to persist the cache for longer, 7 days // We run locally less frequently, so we need to persist the cache for longer, 7 days
: 7 * 86400 * 1000; : 7 * 86400 * 1000;
type TypesonValue = type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
| string
// https://github.com/Rich-Harris/devalue/blob/f3fd2aa93d79f21746555671f955a897335edb1b/src/stringify.js#L77
type Devalue =
| number | number
| string
| boolean | boolean
| bigint
| Date
| RegExp
| Set<Devalue>
| Devalue[]
| null | null
| undefined | undefined
| Set<any> | Map<Devalue, Devalue>
| Map<any, any> | DevalueObject
| TypesonObject | TypedArray
| TypesonArray; | ArrayBuffer;
interface TypesonObject { // Has to use an interface to avoid circular reference
[key: string]: TypesonValue interface DevalueObject {
[key: string]: Devalue
} }
interface TypesonArray extends Array<TypesonValue> { }
export type FsMemoCacheOptions<T> = CacheApplyOption<T, string> & { export type FsMemoCacheOptions<T> = CacheApplyOption<T, string> & {
ttl?: undefined | never ttl?: undefined | never
}; };
export function cache<Args extends TypesonValue[], T>( export function cache<Args extends Devalue[], T>(
fn: (...args: Args) => Promise<T>, fn: (...args: Args) => Promise<T>,
opt: FsMemoCacheOptions<T> opt: FsMemoCacheOptions<T>
): (...args: Args) => Promise<T> { ): (...args: Args) => Promise<T> {
const fixedKey = fn.toString(); const fixedKey = fn.toString();
return async function cachedCb(...args: Args) { return async function cachedCb(...args: Args) {
const { stringify: devalueStringify } = await import('devalue');
// Construct the complete cache key for this function invocation // Construct the complete cache key for this function invocation
// typeson.stringify is still limited. For now we uses typescript to guard the args. // typeson.stringify is still limited. For now we uses typescript to guard the args.
const cacheKey = (await Promise.all([ const cacheKey = (await Promise.all([
xxhash64(fixedKey), xxhash64(fixedKey),
xxhash64(typeson.stringifySync(args)) xxhash64(devalueStringify(args))
])).join('|'); ])).join('|');
const cacheName = fn.name || fixedKey; const cacheName = fn.name || fixedKey;
@ -87,18 +87,20 @@ export function cache<Args extends TypesonValue[], T>(
}; };
} }
export function cachedOnlyFail<Args extends TypesonValue[], T>( export function cachedOnlyFail<Args extends Devalue[], T>(
fn: (...args: Args) => Promise<T>, fn: (...args: Args) => Promise<T>,
opt: FsMemoCacheOptions<T> opt: FsMemoCacheOptions<T>
): (...args: Args) => Promise<T> { ): (...args: Args) => Promise<T> {
const fixedKey = fn.toString(); const fixedKey = fn.toString();
return async function cachedCb(...args: Args) { return async function cachedCb(...args: Args) {
const { stringify: devalueStringify } = await import('devalue');
// Construct the complete cache key for this function invocation // Construct the complete cache key for this function invocation
// typeson.stringify is still limited. For now we uses typescript to guard the args. // typeson.stringify is still limited. For now we uses typescript to guard the args.
const cacheKey = (await Promise.all([ const cacheKey = (await Promise.all([
xxhash64(fixedKey), xxhash64(fixedKey),
xxhash64(typeson.stringifySync(args)) xxhash64(devalueStringify(args))
])).join('|'); ])).join('|');
const cacheName = fn.name || fixedKey; const cacheName = fn.name || fixedKey;

View File

@ -29,6 +29,7 @@
"ci-info": "^4.0.0", "ci-info": "^4.0.0",
"cli-table3": "^0.6.5", "cli-table3": "^0.6.5",
"csv-parse": "^5.5.6", "csv-parse": "^5.5.6",
"devalue": "^5.1.1",
"fast-cidr-tools": "^0.3.1", "fast-cidr-tools": "^0.3.1",
"fdir": "^6.4.2", "fdir": "^6.4.2",
"foxact": "^0.2.39", "foxact": "^0.2.39",
@ -41,7 +42,6 @@
"tar-fs": "^3.0.6", "tar-fs": "^3.0.6",
"tldts": "^6.1.52", "tldts": "^6.1.52",
"tldts-experimental": "^6.1.52", "tldts-experimental": "^6.1.52",
"typeson-registry": "^11.1.1",
"undici": "6.20.1", "undici": "6.20.1",
"why-is-node-running": "^3.2.0", "why-is-node-running": "^3.2.0",
"yaml": "^2.6.0" "yaml": "^2.6.0"

56
pnpm-lock.yaml generated
View File

@ -43,6 +43,9 @@ importers:
csv-parse: csv-parse:
specifier: ^5.5.6 specifier: ^5.5.6
version: 5.5.6 version: 5.5.6
devalue:
specifier: ^5.1.1
version: 5.1.1
fast-cidr-tools: fast-cidr-tools:
specifier: ^0.3.1 specifier: ^0.3.1
version: 0.3.1 version: 0.3.1
@ -79,9 +82,6 @@ importers:
tldts-experimental: tldts-experimental:
specifier: ^6.1.52 specifier: ^6.1.52
version: 6.1.52 version: 6.1.52
typeson-registry:
specifier: ^11.1.1
version: 11.1.1
undici: undici:
specifier: 6.20.1 specifier: 6.20.1
version: 6.20.1(patch_hash=yuj5uy4vvwj67xoliq5togiyme) version: 6.20.1(patch_hash=yuj5uy4vvwj67xoliq5togiyme)
@ -706,10 +706,6 @@ packages:
bare-stream@2.2.1: bare-stream@2.2.1:
resolution: {integrity: sha512-YTB47kHwBW9zSG8LD77MIBAAQXjU2WjAkMHeeb7hUplVs6+IoM5I7uEVQNPMB7lj9r8I76UMdoMkGnCodHOLqg==} resolution: {integrity: sha512-YTB47kHwBW9zSG8LD77MIBAAQXjU2WjAkMHeeb7hUplVs6+IoM5I7uEVQNPMB7lj9r8I76UMdoMkGnCodHOLqg==}
base64-arraybuffer-es6@3.1.0:
resolution: {integrity: sha512-QKKtftiSrKjilihGNLXxnrb9LJj7rnEdB1cYAqVpekFy0tisDklAf1RAgvpm0HsGYx9sv7FUbgpsrfwTyCPVLg==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
base64-js@1.5.1: base64-js@1.5.1:
resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
@ -878,6 +874,9 @@ packages:
resolution: {integrity: sha512-bwy0MGW55bG41VqxxypOsdSdGqLwXPI/focwgTYCFMbdUiBAxLg9CFzG08sz2aqzknwiX7Hkl0bQENjg8iLByw==} resolution: {integrity: sha512-bwy0MGW55bG41VqxxypOsdSdGqLwXPI/focwgTYCFMbdUiBAxLg9CFzG08sz2aqzknwiX7Hkl0bQENjg8iLByw==}
engines: {node: '>=8'} engines: {node: '>=8'}
devalue@5.1.1:
resolution: {integrity: sha512-maua5KUiapvEwiEAe+XnlZ3Rh0GD+qI1J/nb9vrJc3muPXvcF/8gXYTWF76+5DAqHyDUtOIImEuo0YKE9mshVw==}
diff-sequences@29.6.3: diff-sequences@29.6.3:
resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==} resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==}
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
@ -1812,10 +1811,6 @@ packages:
resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==}
engines: {node: '>=8.0'} engines: {node: '>=8.0'}
tr46@5.0.0:
resolution: {integrity: sha512-tk2G5R2KRwBd+ZN0zaEXpmzdKyOYksXwywulIX95MBODjSzMIuQnQ3m8JxgbhnL1LeVo7lqQKsYa1O3Htl7K5g==}
engines: {node: '>=18'}
ts-api-utils@1.3.0: ts-api-utils@1.3.0:
resolution: {integrity: sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==} resolution: {integrity: sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==}
engines: {node: '>=16'} engines: {node: '>=16'}
@ -1853,14 +1848,6 @@ packages:
engines: {node: '>=14.17'} engines: {node: '>=14.17'}
hasBin: true hasBin: true
typeson-registry@11.1.1:
resolution: {integrity: sha512-WF8meNtURfGXXY2RitEMgZrMEclHU8IdxxN4YTcnXTQUHvisPYsKrxxPFhc4DKTCAc6sVvWVtKelOXFDtw/OQQ==}
engines: {node: '>=18.14.0'}
typeson@9.0.3:
resolution: {integrity: sha512-vsOn+VPbIYI3jgj96vwIhAO0BNmlBxEgBKVmuzl1dwXzQe+3XjRi/gK2zYgAansWYlrtvluy4lvWcL8LF01P3Q==}
engines: {node: '>=16.0.0'}
undici-types@6.19.8: undici-types@6.19.8:
resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==} resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==}
@ -1882,14 +1869,6 @@ packages:
util-deprecate@1.0.2: util-deprecate@1.0.2:
resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}
webidl-conversions@7.0.0:
resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==}
engines: {node: '>=12'}
whatwg-url@14.0.0:
resolution: {integrity: sha512-1lfMEm2IEr7RIV+f4lUNPOqfFL+pO+Xw3fJSqmjX9AbXcXcYOkCe1P6+9VBZB6n94af16NfZf+sSk0JCBZC9aw==}
engines: {node: '>=18'}
which@2.0.2: which@2.0.2:
resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==} resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==}
engines: {node: '>= 8'} engines: {node: '>= 8'}
@ -2555,8 +2534,6 @@ snapshots:
streamx: 2.20.0 streamx: 2.20.0
optional: true optional: true
base64-arraybuffer-es6@3.1.0: {}
base64-js@1.5.1: {} base64-js@1.5.1: {}
better-sqlite3@11.4.0: better-sqlite3@11.4.0:
@ -2722,6 +2699,8 @@ snapshots:
detect-libc@2.0.3: {} detect-libc@2.0.3: {}
devalue@5.1.1: {}
diff-sequences@29.6.3: {} diff-sequences@29.6.3: {}
diff@5.2.0: {} diff@5.2.0: {}
@ -3763,10 +3742,6 @@ snapshots:
dependencies: dependencies:
is-number: 7.0.0 is-number: 7.0.0
tr46@5.0.0:
dependencies:
punycode: 2.3.1
ts-api-utils@1.3.0(typescript@5.6.3): ts-api-utils@1.3.0(typescript@5.6.3):
dependencies: dependencies:
typescript: 5.6.3 typescript: 5.6.3
@ -3798,14 +3773,6 @@ snapshots:
typescript@5.6.3: {} typescript@5.6.3: {}
typeson-registry@11.1.1:
dependencies:
base64-arraybuffer-es6: 3.1.0
typeson: 9.0.3
whatwg-url: 14.0.0
typeson@9.0.3: {}
undici-types@6.19.8: {} undici-types@6.19.8: {}
undici@6.20.1(patch_hash=yuj5uy4vvwj67xoliq5togiyme): {} undici@6.20.1(patch_hash=yuj5uy4vvwj67xoliq5togiyme): {}
@ -3824,13 +3791,6 @@ snapshots:
util-deprecate@1.0.2: {} util-deprecate@1.0.2: {}
webidl-conversions@7.0.0: {}
whatwg-url@14.0.0:
dependencies:
tr46: 5.0.0
webidl-conversions: 7.0.0
which@2.0.2: which@2.0.2:
dependencies: dependencies:
isexe: 2.0.0 isexe: 2.0.0