@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.
Files changed (61) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/AGENTS.md +27 -21
  3. package/README.md +30 -0
  4. package/dist/codec.js +1 -74
  5. package/dist/codec.js.map +2 -2
  6. package/dist/defaults.js +1 -6
  7. package/dist/defaults.js.map +2 -2
  8. package/dist/errors.js +1 -12
  9. package/dist/errors.js.map +2 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/patterns.js +1 -35
  12. package/dist/patterns.js.map +2 -2
  13. package/dist/provider.js +440 -0
  14. package/dist/provider.js.map +7 -0
  15. package/dist/redisConnection.js +2 -0
  16. package/dist/redisConnection.js.map +7 -0
  17. package/dist/service.js +320 -80
  18. package/dist/service.js.map +3 -3
  19. package/dist/strategies/jsonfile.js +1 -186
  20. package/dist/strategies/jsonfile.js.map +3 -3
  21. package/dist/strategies/memory.js +1 -196
  22. package/dist/strategies/memory.js.map +3 -3
  23. package/dist/strategies/redis.js +1 -443
  24. package/dist/strategies/redis.js.map +3 -3
  25. package/dist/strategies/sqlite.js +1 -196
  26. package/dist/strategies/sqlite.js.map +3 -3
  27. package/dist/testing/conformance.js +448 -0
  28. package/dist/testing/conformance.js.map +7 -0
  29. package/dist/testing/index.js +12 -0
  30. package/dist/testing/index.js.map +7 -0
  31. package/dist/testing/remote.js +182 -0
  32. package/dist/testing/remote.js.map +7 -0
  33. package/fixtures/build-conformance-host.mjs +58 -0
  34. package/fixtures/cache-provider-fixture/index.d.ts +18 -0
  35. package/fixtures/cache-provider-fixture/index.js +307 -0
  36. package/fixtures/cache-provider-fixture/package.json +25 -0
  37. package/fixtures/conformance-child.mjs +41 -0
  38. package/jest.config.cjs +3 -1
  39. package/package.json +10 -2
  40. package/src/__tests__/cache-di-contract-docs.test.ts +2 -2
  41. package/src/__tests__/conformance.memory-provider.test.ts +77 -0
  42. package/src/__tests__/provider-fixture.packed.test.ts +178 -0
  43. package/src/__tests__/provider.test.ts +469 -0
  44. package/src/__tests__/service.provider.test.ts +513 -0
  45. package/src/__tests__/service.test.ts +94 -3
  46. package/src/codec.ts +1 -94
  47. package/src/defaults.ts +1 -2
  48. package/src/errors.ts +1 -14
  49. package/src/index.ts +4 -0
  50. package/src/patterns.ts +1 -40
  51. package/src/provider.ts +641 -0
  52. package/src/redisConnection.ts +1 -0
  53. package/src/service.ts +396 -89
  54. package/src/strategies/jsonfile.ts +1 -242
  55. package/src/strategies/memory.ts +1 -275
  56. package/src/strategies/redis.ts +1 -615
  57. package/src/strategies/sqlite.ts +1 -292
  58. package/src/testing/conformance.ts +569 -0
  59. package/src/testing/index.ts +11 -0
  60. package/src/testing/remote.ts +240 -0
  61. package/src/types.ts +8 -106
package/src/codec.ts CHANGED
@@ -1,94 +1 @@
1
- import zlib from 'node:zlib'
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 const DEFAULT_SQLITE_CACHE_PATH = '.mercato/cache/cache.db'
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 class CacheDependencyUnavailableError extends Error {
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 function matchCacheKeyPattern(key: string, pattern: string): boolean {
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'