@open-mercato/cache 0.11.1-develop.8589.1.db1a76ea60 → 0.11.1-develop.8665.1.2455b2503c
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.
- package/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +27 -21
- package/README.md +30 -0
- package/dist/codec.js +1 -74
- package/dist/codec.js.map +2 -2
- package/dist/defaults.js +1 -6
- package/dist/defaults.js.map +2 -2
- package/dist/errors.js +1 -12
- package/dist/errors.js.map +2 -2
- package/dist/index.js.map +1 -1
- package/dist/patterns.js +1 -35
- package/dist/patterns.js.map +2 -2
- package/dist/provider.js +440 -0
- package/dist/provider.js.map +7 -0
- package/dist/redisConnection.js +2 -0
- package/dist/redisConnection.js.map +7 -0
- package/dist/service.js +320 -80
- package/dist/service.js.map +3 -3
- package/dist/strategies/jsonfile.js +1 -186
- package/dist/strategies/jsonfile.js.map +3 -3
- package/dist/strategies/memory.js +1 -196
- package/dist/strategies/memory.js.map +3 -3
- package/dist/strategies/redis.js +1 -443
- package/dist/strategies/redis.js.map +3 -3
- package/dist/strategies/sqlite.js +1 -196
- package/dist/strategies/sqlite.js.map +3 -3
- package/dist/testing/conformance.js +448 -0
- package/dist/testing/conformance.js.map +7 -0
- package/dist/testing/index.js +12 -0
- package/dist/testing/index.js.map +7 -0
- package/dist/testing/remote.js +182 -0
- package/dist/testing/remote.js.map +7 -0
- package/fixtures/build-conformance-host.mjs +58 -0
- package/fixtures/cache-provider-fixture/index.d.ts +18 -0
- package/fixtures/cache-provider-fixture/index.js +307 -0
- package/fixtures/cache-provider-fixture/package.json +25 -0
- package/fixtures/conformance-child.mjs +41 -0
- package/jest.config.cjs +3 -1
- package/package.json +10 -2
- package/src/__tests__/cache-di-contract-docs.test.ts +2 -2
- package/src/__tests__/conformance.memory-provider.test.ts +77 -0
- package/src/__tests__/provider-fixture.packed.test.ts +178 -0
- package/src/__tests__/provider.test.ts +469 -0
- package/src/__tests__/service.provider.test.ts +513 -0
- package/src/__tests__/service.test.ts +94 -3
- package/src/codec.ts +1 -94
- package/src/defaults.ts +1 -2
- package/src/errors.ts +1 -14
- package/src/index.ts +4 -0
- package/src/patterns.ts +1 -40
- package/src/provider.ts +641 -0
- package/src/redisConnection.ts +1 -0
- package/src/service.ts +396 -89
- package/src/strategies/jsonfile.ts +1 -242
- package/src/strategies/memory.ts +1 -275
- package/src/strategies/redis.ts +1 -615
- package/src/strategies/sqlite.ts +1 -292
- package/src/testing/conformance.ts +569 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/remote.ts +240 -0
- package/src/types.ts +8 -106
package/src/codec.ts
CHANGED
|
@@ -1,94 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type CacheCompression = 'off' | 'zstd' | 'gzip'
|
|
4
|
-
|
|
5
|
-
export type CacheCodecConfig = {
|
|
6
|
-
compression: CacheCompression
|
|
7
|
-
minBytes: number
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type CacheCodecOptions = {
|
|
11
|
-
compression?: CacheCompression
|
|
12
|
-
compressionMinBytes?: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const DEFAULT_COMPRESSION_MIN_BYTES = 1024
|
|
16
|
-
|
|
17
|
-
/** Upper bound for one decompressed entry; larger JSON is never compressed, so no legitimate entry can hit it. */
|
|
18
|
-
export const MAX_DECOMPRESSED_BYTES = 64 * 1024 * 1024
|
|
19
|
-
|
|
20
|
-
const ZSTD_LEVEL = 1
|
|
21
|
-
const COMPRESSIONS: CacheCompression[] = ['off', 'zstd', 'gzip']
|
|
22
|
-
|
|
23
|
-
const warnedMessages = new Set<string>()
|
|
24
|
-
|
|
25
|
-
function warnOnce(message: string): void {
|
|
26
|
-
if (warnedMessages.has(message)) return
|
|
27
|
-
warnedMessages.add(message)
|
|
28
|
-
console.warn(message)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function isZstdAvailable(): boolean {
|
|
32
|
-
return typeof zlib.zstdCompressSync === 'function' && typeof zlib.zstdDecompressSync === 'function'
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function parseCacheCompression(raw: string | undefined): CacheCompression {
|
|
36
|
-
const value = raw?.trim().toLowerCase()
|
|
37
|
-
if (!value) return 'off'
|
|
38
|
-
if (COMPRESSIONS.includes(value as CacheCompression)) return value as CacheCompression
|
|
39
|
-
warnOnce(`[cache] Unknown CACHE_COMPRESSION value "${raw}". Compression stays off.`)
|
|
40
|
-
return 'off'
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function parseCacheCompressionMinBytes(raw: string | undefined): number | undefined {
|
|
44
|
-
if (!raw) return undefined
|
|
45
|
-
const parsed = Number.parseInt(raw, 10)
|
|
46
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function resolveCacheCodecConfig(options?: CacheCodecOptions): CacheCodecConfig {
|
|
50
|
-
const minBytes = options?.compressionMinBytes && options.compressionMinBytes > 0
|
|
51
|
-
? options.compressionMinBytes
|
|
52
|
-
: DEFAULT_COMPRESSION_MIN_BYTES
|
|
53
|
-
let compression = options?.compression ?? 'off'
|
|
54
|
-
if (compression === 'zstd' && !isZstdAvailable()) {
|
|
55
|
-
warnOnce('[cache] zstd is not available in this Node.js runtime. Falling back to gzip cache compression.')
|
|
56
|
-
compression = 'gzip'
|
|
57
|
-
}
|
|
58
|
-
return { compression, minBytes }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function compress(input: Buffer, compression: Exclude<CacheCompression, 'off'>): Buffer {
|
|
62
|
-
if (compression === 'zstd') {
|
|
63
|
-
return zlib.zstdCompressSync(input, { params: { [zlib.constants.ZSTD_c_compressionLevel]: ZSTD_LEVEL } })
|
|
64
|
-
}
|
|
65
|
-
return zlib.gzipSync(input)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function encodeCacheText(json: string, config: CacheCodecConfig): string | Buffer {
|
|
69
|
-
if (config.compression === 'off') return json
|
|
70
|
-
const size = Buffer.byteLength(json)
|
|
71
|
-
if (size < config.minBytes || size > MAX_DECOMPRESSED_BYTES) return json
|
|
72
|
-
const compressed = compress(Buffer.from(json, 'utf8'), config.compression)
|
|
73
|
-
return compressed.length < size ? compressed : json
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function isZstdFrame(raw: Buffer): boolean {
|
|
77
|
-
return raw.length >= 4 && raw[0] === 0x28 && raw[1] === 0xb5 && raw[2] === 0x2f && raw[3] === 0xfd
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function isGzipStream(raw: Buffer): boolean {
|
|
81
|
-
return raw.length >= 2 && raw[0] === 0x1f && raw[1] === 0x8b
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Detects the format from the stored bytes, so entries stay readable whatever CACHE_COMPRESSION is set to now. */
|
|
85
|
-
export function decodeCacheText(raw: string | Buffer): string {
|
|
86
|
-
if (typeof raw === 'string') return raw
|
|
87
|
-
if (isZstdFrame(raw)) {
|
|
88
|
-
return zlib.zstdDecompressSync(raw, { maxOutputLength: MAX_DECOMPRESSED_BYTES }).toString('utf8')
|
|
89
|
-
}
|
|
90
|
-
if (isGzipStream(raw)) {
|
|
91
|
-
return zlib.gunzipSync(raw, { maxOutputLength: MAX_DECOMPRESSED_BYTES }).toString('utf8')
|
|
92
|
-
}
|
|
93
|
-
return raw.toString('utf8')
|
|
94
|
-
}
|
|
1
|
+
export * from '@open-mercato/cache-provider-kit/codec'
|
package/src/defaults.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export const DEFAULT_JSON_FILE_CACHE_PATH = '.mercato/cache/cache.json'
|
|
1
|
+
export * from '@open-mercato/cache-provider-kit/defaults'
|
package/src/errors.ts
CHANGED
|
@@ -1,14 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
public readonly strategy: string
|
|
3
|
-
public readonly dependency: string
|
|
4
|
-
public readonly originalError?: unknown
|
|
5
|
-
|
|
6
|
-
constructor(strategy: string, dependency: string, originalError?: unknown) {
|
|
7
|
-
super(`Cache strategy "${strategy}" requires dependency "${dependency}" which is not available`)
|
|
8
|
-
this.name = 'CacheDependencyUnavailableError'
|
|
9
|
-
this.strategy = strategy
|
|
10
|
-
this.dependency = dependency
|
|
11
|
-
this.originalError = originalError
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
1
|
+
export * from '@open-mercato/cache-provider-kit/errors'
|
package/src/index.ts
CHANGED
|
@@ -6,3 +6,7 @@ export { createRedisStrategy } from './strategies/redis'
|
|
|
6
6
|
export { createSqliteStrategy } from './strategies/sqlite'
|
|
7
7
|
export { createJsonFileStrategy } from './strategies/jsonfile'
|
|
8
8
|
export { runWithCacheTenant, getCurrentCacheTenant } from './tenantContext'
|
|
9
|
+
export type {
|
|
10
|
+
CacheProviderPlacement,
|
|
11
|
+
ResolvedCacheProvider,
|
|
12
|
+
} from './provider'
|
package/src/patterns.ts
CHANGED
|
@@ -1,40 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
let patternIndex = 0
|
|
3
|
-
let keyIndex = 0
|
|
4
|
-
let starIndex = -1
|
|
5
|
-
let keyRetryIndex = 0
|
|
6
|
-
|
|
7
|
-
while (keyIndex < key.length) {
|
|
8
|
-
const patternChar = pattern[patternIndex]
|
|
9
|
-
const keyChar = key[keyIndex]
|
|
10
|
-
|
|
11
|
-
if (patternChar === '*') {
|
|
12
|
-
starIndex = patternIndex
|
|
13
|
-
keyRetryIndex = keyIndex
|
|
14
|
-
patternIndex += 1
|
|
15
|
-
continue
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (patternChar === '?' || patternChar === keyChar) {
|
|
19
|
-
patternIndex += 1
|
|
20
|
-
keyIndex += 1
|
|
21
|
-
continue
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (starIndex !== -1) {
|
|
25
|
-
patternIndex = starIndex + 1
|
|
26
|
-
keyRetryIndex += 1
|
|
27
|
-
keyIndex = keyRetryIndex
|
|
28
|
-
continue
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return false
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
while (pattern[patternIndex] === '*') {
|
|
35
|
-
patternIndex += 1
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return patternIndex === pattern.length
|
|
39
|
-
}
|
|
40
|
-
|
|
1
|
+
export * from '@open-mercato/cache-provider-kit/patterns'
|