@aomex/core
Advanced tools
Comparing version 0.0.26 to 0.0.27
# @aomex/core | ||
## 0.0.27 | ||
### Patch Changes | ||
- [`00bafbb`](https://github.com/aomex/aomex/commit/00bafbbac2d32205b63a6bf561fb0a69c38a54bb) Thanks [@geekact](https://github.com/geekact)! - refactor(core): 缓存迁移到新的包@aomex/internal-cache | ||
- [`8becf8e`](https://github.com/aomex/aomex/commit/8becf8ee5ef86a5909783d0654e536db7be9bf5b) Thanks [@geekact](https://github.com/geekact)! - refactor(core): 工具移动到新的包@aomex/internal-tools | ||
- [`0776719`](https://github.com/aomex/aomex/commit/077671963401f1dafb5b03722899452d45df13fc) Thanks [@geekact](https://github.com/geekact)! - refactor: 文件搜索转移到新的包@aomex/internal-file-import | ||
- [`f996bf7`](https://github.com/aomex/aomex/commit/f996bf7e529e7751a5e858c579feed33f5f02d65) Thanks [@geekact](https://github.com/geekact)! - fix(core): enum规则类型提示不精确 | ||
## 0.0.26 | ||
@@ -4,0 +16,0 @@ |
@@ -1,17 +0,5 @@ | ||
export { default as chalk } from 'chalk'; | ||
export { default as bytes } from 'bytes'; | ||
import { NonReadonly } from '@aomex/internal-tools'; | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
export { OpenAPIV3 as OpenAPI } from 'openapi-types'; | ||
import { LRUCache } from 'lru-cache'; | ||
declare function toArray<T>(data: T | readonly T[], unique?: boolean): T[]; | ||
declare function toArray<T>(data: T | T[], unique?: boolean): T[]; | ||
declare const sleep: (ms: number) => Promise<void>; | ||
type NonReadonly<T extends object> = { | ||
-readonly [K in keyof T]: T[K]; | ||
}; | ||
type Union2Intersection<T> = (T extends any ? (arg: T) => void : never) extends (arg: infer P) => void ? P : never; | ||
interface _PureFn { | ||
@@ -735,4 +723,4 @@ <Props extends object = object>(fn: (ctx: NonReadonly<Props>, next: Next) => any): PureMiddleware<Props>; | ||
email(): EmailValidator<string>; | ||
enum<T extends string | number | boolean>(ranges: T[]): EnumValidator<T>; | ||
enum<T extends readonly (string | number | boolean)[]>(ranges: T): EnumValidator<T[number]>; | ||
enum<const T extends string | number | boolean>(ranges: T[]): EnumValidator<T>; | ||
enum<const T extends readonly (string | number | boolean)[]>(ranges: T): EnumValidator<T[number]>; | ||
hash(algorithm: HashValidator.Algorithm): HashValidator<string>; | ||
@@ -825,17 +813,4 @@ /** | ||
interface CacheOptions { | ||
interface Caching { | ||
/** | ||
* key前缀,避免与其他存储内容混淆 | ||
*/ | ||
keyPrefix?: string; | ||
/** | ||
* 删除过期缓存防止占用空间,加快读取速度。概率取值范围:`1-100`。默认值:`10` | ||
*/ | ||
gcProbability?: number; | ||
} | ||
declare abstract class Cache { | ||
protected readonly keyPrefix: string; | ||
protected readonly gcProbability: number; | ||
constructor(config: CacheOptions); | ||
/** | ||
* 查看缓存是否存在 | ||
@@ -848,4 +823,4 @@ */ | ||
* ```typescript | ||
* await cache.get('non-exist'); // null | ||
* await cache.get('non-exist', 'my-value'); // my-value | ||
* await cache.get('key'); // null | ||
* await cache.get('key', 'my-value'); // my-value | ||
* ``` | ||
@@ -903,56 +878,7 @@ */ | ||
/** | ||
* 删除所有缓存 | ||
* 删除所有缓存。若指定了keyPrefix,则只删除有该前缀的缓存 | ||
*/ | ||
deleteAll(): Promise<boolean>; | ||
protected buildKey(key: string): string; | ||
protected addValue(key: string, value: string, duration?: number): Promise<boolean>; | ||
protected abstract existsKey(key: string): Promise<boolean>; | ||
protected abstract getValue(key: string): Promise<string | null>; | ||
protected abstract setValue(key: string, value: string, duration?: number): Promise<boolean>; | ||
protected abstract deleteValue(key: string): Promise<boolean>; | ||
protected abstract deleteAllValues(): Promise<boolean>; | ||
protected parseValue(value: string | null, defaultValue?: string | number | object | boolean): any; | ||
protected applyGC(): Promise<void>; | ||
protected gc(): Promise<void>; | ||
} | ||
interface MemoryCacheOptions extends CacheOptions { | ||
/** | ||
* 最大的缓存数量。默认值:`1000` | ||
*/ | ||
maxItems?: number; | ||
} | ||
declare class MemoryCache extends Cache { | ||
readonly lru: LRUCache<string, string>; | ||
constructor(options?: MemoryCacheOptions); | ||
getAndDelete<T>(key: string, defaultValue: T): Promise<T>; | ||
getAndDelete<T extends string | number | object | boolean>(key: string): Promise<T | null>; | ||
protected existsKey(key: string): Promise<boolean>; | ||
protected getValue(key: string): Promise<string | null>; | ||
protected syncGetValue(key: string): string | null; | ||
protected setValue(key: string, value: string, duration?: number): Promise<boolean>; | ||
protected deleteValue(key: string): Promise<boolean>; | ||
protected syncDeleteValue(key: string): boolean; | ||
protected deleteAllValues(): Promise<boolean>; | ||
protected gc(): Promise<void>; | ||
} | ||
interface Options { | ||
pattern: string[]; | ||
ignore?: string[]; | ||
dot?: boolean; | ||
/** | ||
* 忽略 **node_modules** 文件夹。默认值:`true` | ||
*/ | ||
ignoreNodeModules?: boolean; | ||
/** | ||
* 忽略所有 **.d.{ts,mts,cts}** 文件。默认值:`true` | ||
*/ | ||
ignoreDTS?: boolean; | ||
} | ||
type PathToFileOptions = string | string[] | Options | Options[]; | ||
declare const pathToFiles: (paths: PathToFileOptions) => Promise<string[]>; | ||
declare const fileToModules: <T extends unknown>(files: string[], filter?: ((item?: T | undefined) => boolean) | undefined) => Promise<T[]>; | ||
export { AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, Cache, CacheOptions, Chain, ChainPlatform, CompatibleValidator, Compose, DataTransferObject, DateTimeValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, LengthRange, MemoryCache, MemoryCacheOptions, MiddleWareToken, Middleware, MiddlewarePlatform, MixinLength, Next, NonReadonly, NumberValidator, ObjectValidator, OneOfValidator, PathToFileOptions, PureChain, PureMiddleware, PureMiddlewareToken, Rule, StringValidator, TransformedValidator, UlidValidator, Union2Intersection, UrlValidator, UuidValidator, ValidateOptions, Validator, ValidatorError, _PureFn, chain, compose, createDTO, fileToModules, forceToValidator, magistrate, middleware, mixinLength, pathToFiles, rule, sleep, toArray, validate }; | ||
export { AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, Caching, Chain, ChainPlatform, CompatibleValidator, Compose, DataTransferObject, DateTimeValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, LengthRange, MiddleWareToken, Middleware, MiddlewarePlatform, MixinLength, Next, NumberValidator, ObjectValidator, OneOfValidator, PureChain, PureMiddleware, PureMiddlewareToken, Rule, StringValidator, TransformedValidator, UlidValidator, UrlValidator, UuidValidator, ValidateOptions, Validator, ValidatorError, _PureFn, chain, compose, createDTO, forceToValidator, magistrate, middleware, mixinLength, rule, validate }; |
@@ -1,16 +0,3 @@ | ||
// src/utility/to-array.ts | ||
function toArray(data, unique = false) { | ||
return Array.isArray(data) ? unique ? [...new Set(data)] : data : [data]; | ||
} | ||
// src/utility/sleep.ts | ||
var sleep = (ms) => ms <= 0 ? Promise.resolve(void 0) : new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
// src/utility/index.ts | ||
import { default as default2 } from "chalk"; | ||
import { default as default3 } from "bytes"; | ||
// src/middleware/chain.ts | ||
import { toArray } from "@aomex/internal-tools"; | ||
var Chain = class _Chain { | ||
@@ -1230,2 +1217,3 @@ constructor(middlewareList = []) { | ||
// src/validator/api/rule.ts | ||
import { toArray as toArray2 } from "@aomex/internal-tools"; | ||
var Rule = class { | ||
@@ -1278,3 +1266,3 @@ static register(name, SubValidator) { | ||
enum(ranges) { | ||
return new EnumValidator(toArray(ranges, true)); | ||
return new EnumValidator(toArray2(ranges, true)); | ||
} | ||
@@ -1301,3 +1289,3 @@ hash(algorithm) { | ||
ip(version) { | ||
return new IpValidator(toArray(version, true)); | ||
return new IpValidator(toArray2(version, true)); | ||
} | ||
@@ -1345,3 +1333,3 @@ /** | ||
uuid(versions2) { | ||
return new UuidValidator(toArray(versions2, true)); | ||
return new UuidValidator(toArray2(versions2, true)); | ||
} | ||
@@ -1365,230 +1353,2 @@ }; | ||
import { OpenAPIV3 } from "openapi-types"; | ||
// src/caching/cache.ts | ||
import { createHash } from "node:crypto"; | ||
var Cache = class { | ||
keyPrefix; | ||
gcProbability; | ||
constructor(config) { | ||
this.keyPrefix = config.keyPrefix ?? ""; | ||
this.gcProbability = (config.gcProbability ?? 10) / 100; | ||
} | ||
/** | ||
* 查看缓存是否存在 | ||
*/ | ||
async exists(key) { | ||
return this.existsKey(this.buildKey(key)); | ||
} | ||
async get(key, defaultValue) { | ||
const hashKey = this.buildKey(key); | ||
const result = await this.getValue(hashKey); | ||
return this.parseValue(result, defaultValue); | ||
} | ||
async getAndDelete(key, defaultValue) { | ||
const result = await this.get(key, defaultValue); | ||
if (result !== null) { | ||
await this.delete(key); | ||
} | ||
return result; | ||
} | ||
async getOrSet(key, orSet, durationMs) { | ||
let value = await this.get(key); | ||
if (value !== null) | ||
return value; | ||
await this.set(key, await orSet(), durationMs); | ||
return await this.get(key); | ||
} | ||
/** | ||
* 设置缓存。可以指定过期时间(毫秒) | ||
* | ||
* ```typescript | ||
* await cache.set('key', 'value'); | ||
* await cache.set('key', 'value', 3600); | ||
* ``` | ||
*/ | ||
async set(key, value, durationMs) { | ||
await this.applyGC(); | ||
const hashKey = this.buildKey(key); | ||
return this.setValue(hashKey, JSON.stringify(value), durationMs); | ||
} | ||
/** | ||
* 设置缓存。如果缓存已经存在,则设置失败,返回`false` | ||
* | ||
* ```typescript | ||
* await cache.exists('key'); // false | ||
* await cache.add('key', 'value'); // true | ||
* | ||
* await cache.exists('key'); // true | ||
* await cache.add('key', 'value'); // false | ||
* ``` | ||
*/ | ||
async add(key, value, durationMs) { | ||
await this.applyGC(); | ||
const hashKey = this.buildKey(key); | ||
return this.addValue(hashKey, JSON.stringify(value), durationMs); | ||
} | ||
/** | ||
* 删除指定缓存 | ||
*/ | ||
async delete(key) { | ||
const hashKey = this.buildKey(key); | ||
return this.deleteValue(hashKey); | ||
} | ||
/** | ||
* 删除所有缓存 | ||
*/ | ||
async deleteAll() { | ||
return this.deleteAllValues(); | ||
} | ||
buildKey(key) { | ||
const hashKey = key.length <= 32 ? key : createHash("md5").update(key).digest("hex"); | ||
return this.keyPrefix + hashKey; | ||
} | ||
async addValue(key, value, duration) { | ||
if (await this.existsKey(key)) | ||
return false; | ||
if (!await this.setValue(key, value, duration)) | ||
return false; | ||
return await this.getValue(key) === value; | ||
} | ||
parseValue(value, defaultValue) { | ||
if (value === null) { | ||
return defaultValue === void 0 ? null : defaultValue; | ||
} | ||
try { | ||
return JSON.parse(value); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
async applyGC() { | ||
if (Math.random() > this.gcProbability) | ||
return; | ||
await this.gc(); | ||
} | ||
async gc() { | ||
} | ||
}; | ||
// src/caching/memory-cache.ts | ||
import { LRUCache } from "lru-cache"; | ||
var MemoryCache = class extends Cache { | ||
lru; | ||
constructor(options2 = {}) { | ||
super(options2); | ||
this.lru = new LRUCache({ | ||
max: options2.maxItems || 1e3 | ||
}); | ||
} | ||
async getAndDelete(key, defaultValue) { | ||
const hashKey = this.buildKey(key); | ||
const result = this.syncGetValue(hashKey); | ||
result !== null && this.syncDeleteValue(hashKey); | ||
return this.parseValue(result, defaultValue); | ||
} | ||
async existsKey(key) { | ||
return this.lru.has(key); | ||
} | ||
async getValue(key) { | ||
return this.syncGetValue(key); | ||
} | ||
syncGetValue(key) { | ||
const data = this.lru.get(key); | ||
return data === void 0 ? null : data; | ||
} | ||
async setValue(key, value, duration) { | ||
this.gc(); | ||
this.lru.set(key, value, { | ||
ttl: duration | ||
}); | ||
return true; | ||
} | ||
async deleteValue(key) { | ||
return this.syncDeleteValue(key); | ||
} | ||
syncDeleteValue(key) { | ||
return this.lru.delete(key); | ||
} | ||
async deleteAllValues() { | ||
this.lru.clear(); | ||
return true; | ||
} | ||
async gc() { | ||
this.lru.purgeStale(); | ||
} | ||
}; | ||
// src/file-parser/path-to-files.ts | ||
import path from "node:path"; | ||
import { stat } from "node:fs/promises"; | ||
import { glob, hasMagic } from "glob"; | ||
var pathToFiles = async (paths) => { | ||
const opts = normalizeSearchPath(paths); | ||
const files = await Promise.all( | ||
opts.map((opt) => { | ||
const { | ||
dot, | ||
pattern: patterns, | ||
ignoreNodeModules = true, | ||
ignoreDTS = true | ||
} = opt; | ||
const ignore = (opt.ignore || []).slice(); | ||
ignoreDTS && ignore.push("**/*.d.{ts,mts,cts}"); | ||
ignoreNodeModules && ignore.push("**/node_modules/**"); | ||
const options2 = { | ||
nodir: true, | ||
dot, | ||
ignore, | ||
withFileTypes: false | ||
}; | ||
return Promise.all( | ||
patterns.map(async (pattern2) => { | ||
if (!path.isAbsolute(pattern2)) { | ||
pattern2 = path.posix.resolve(pattern2); | ||
} | ||
pattern2 = pattern2.replace(/\\/g, path.posix.sep); | ||
if (!hasMagic(pattern2, { magicalBraces: true })) { | ||
const stats = await stat(pattern2); | ||
if (!stats.isFile()) { | ||
pattern2 = path.posix.join( | ||
pattern2, | ||
"**", | ||
"*.{ts,js,mts,mjs,cts,cjs}" | ||
); | ||
} | ||
} | ||
return glob(pattern2, options2); | ||
}) | ||
); | ||
}) | ||
); | ||
return [...new Set(files.flat(2))].sort(); | ||
}; | ||
var normalizeSearchPath = (paths) => { | ||
if (typeof paths === "string") { | ||
return [{ pattern: [paths] }]; | ||
} | ||
if (Array.isArray(paths)) { | ||
if (!paths.length) | ||
return []; | ||
return isStringArray(paths) ? [{ pattern: paths }] : paths; | ||
} | ||
return [paths]; | ||
}; | ||
var isStringArray = (data) => { | ||
return typeof data[0] === "string"; | ||
}; | ||
// src/file-parser/file-to-modules.ts | ||
import { pathToFileURL } from "node:url"; | ||
var fileToModules = async (files, filter) => { | ||
const result = await Promise.all( | ||
files.map(async (file) => { | ||
const modules2 = await import(pathToFileURL(file).toString()); | ||
return typeof modules2 === "object" ? Object.values(modules2) : []; | ||
}) | ||
); | ||
const modules = [...new Set(result.flat())]; | ||
return filter ? modules.filter(filter) : modules; | ||
}; | ||
export { | ||
@@ -1602,3 +1362,2 @@ AnyValidator, | ||
BufferValidator, | ||
Cache, | ||
Chain, | ||
@@ -1612,3 +1371,2 @@ DataTransferObject, | ||
IpValidator, | ||
MemoryCache, | ||
Middleware, | ||
@@ -1628,8 +1386,5 @@ NumberValidator, | ||
ValidatorError, | ||
default3 as bytes, | ||
chain, | ||
default2 as chalk, | ||
compose, | ||
createDTO, | ||
fileToModules, | ||
forceToValidator, | ||
@@ -1639,8 +1394,5 @@ magistrate, | ||
mixinLength, | ||
pathToFiles, | ||
rule, | ||
sleep, | ||
toArray, | ||
validate | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@aomex/core", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"description": "", | ||
@@ -36,10 +36,6 @@ "type": "module", | ||
"dependencies": { | ||
"@types/bytes": "^3.1.4", | ||
"bytes": "^3.1.2", | ||
"chalk": "^5.3.0", | ||
"email-validator": "^2.0.4", | ||
"glob": "^10.3.10", | ||
"ip-regex": "^5.0.0", | ||
"lru-cache": "^10.0.2", | ||
"openapi-types": "^12.1.3" | ||
"openapi-types": "^12.1.3", | ||
"@aomex/internal-tools": "^0.0.27" | ||
}, | ||
@@ -46,0 +42,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6
174059
2202
+ Added@aomex/internal-tools@0.0.27(transitive)
- Removed@types/bytes@^3.1.4
- Removedbytes@^3.1.2
- Removedchalk@^5.3.0
- Removedglob@^10.3.10
- Removedlru-cache@^10.0.2
- Removed@isaacs/cliui@8.0.2(transitive)
- Removed@pkgjs/parseargs@0.11.0(transitive)
- Removedansi-regex@5.0.16.1.0(transitive)
- Removedansi-styles@4.3.06.2.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@2.0.1(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedcross-spawn@7.0.5(transitive)
- Removedeastasianwidth@0.2.0(transitive)
- Removedemoji-regex@8.0.09.2.2(transitive)
- Removedforeground-child@3.3.0(transitive)
- Removedglob@10.4.5(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjackspeak@3.4.3(transitive)
- Removedlru-cache@10.4.3(transitive)
- Removedminimatch@9.0.5(transitive)
- Removedminipass@7.1.2(transitive)
- Removedpackage-json-from-dist@1.0.1(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedpath-scurry@1.11.1(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedsignal-exit@4.1.0(transitive)
- Removedstring-width@4.2.35.1.2(transitive)
- Removedstrip-ansi@6.0.17.1.0(transitive)
- Removedwhich@2.0.2(transitive)
- Removedwrap-ansi@7.0.08.1.0(transitive)