|
@@ -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 =
|
|
|
|
|
- | string
|
|
|
|
|
|
|
+type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
|
|
|
+
|
|
|
|
|
+// 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<any, any>
|
|
|
|
|
- | TypesonObject
|
|
|
|
|
- | TypesonArray;
|
|
|
|
|
-
|
|
|
|
|
-interface TypesonObject {
|
|
|
|
|
- [key: string]: TypesonValue
|
|
|
|
|
|
|
+ | Map<Devalue, Devalue>
|
|
|
|
|
+ | DevalueObject
|
|
|
|
|
+ | TypedArray
|
|
|
|
|
+ | ArrayBuffer;
|
|
|
|
|
+
|
|
|
|
|
+// Has to use an interface to avoid circular reference
|
|
|
|
|
+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;
|