@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/cache",
|
|
3
|
-
"version": "0.11.1-develop.
|
|
3
|
+
"version": "0.11.1-develop.8665.1.2455b2503c",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Multi-strategy cache service with tag-based invalidation support",
|
|
6
6
|
"type": "module",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"types": "./src/types.ts",
|
|
21
21
|
"default": "./dist/types.js"
|
|
22
22
|
},
|
|
23
|
+
"./testing": {
|
|
24
|
+
"types": "./src/testing/index.ts",
|
|
25
|
+
"default": "./dist/testing/index.js"
|
|
26
|
+
},
|
|
23
27
|
"./*": {
|
|
24
28
|
"types": [
|
|
25
29
|
"./src/*.ts"
|
|
@@ -55,7 +59,11 @@
|
|
|
55
59
|
}
|
|
56
60
|
},
|
|
57
61
|
"dependencies": {
|
|
58
|
-
"@
|
|
62
|
+
"@open-mercato/cache-provider-jsonfile": "0.11.1-develop.8665.1.2455b2503c",
|
|
63
|
+
"@open-mercato/cache-provider-kit": "0.11.1-develop.8665.1.2455b2503c",
|
|
64
|
+
"@open-mercato/cache-provider-memory": "0.11.1-develop.8665.1.2455b2503c",
|
|
65
|
+
"@open-mercato/cache-provider-redis-legacy": "0.11.1-develop.8665.1.2455b2503c",
|
|
66
|
+
"@open-mercato/cache-provider-sqlite": "0.11.1-develop.8665.1.2455b2503c"
|
|
59
67
|
},
|
|
60
68
|
"devDependencies": {
|
|
61
69
|
"@types/jest": "^30.0.0",
|
|
@@ -21,7 +21,7 @@ import { join } from 'node:path'
|
|
|
21
21
|
|
|
22
22
|
const repoRoot = join(__dirname, '..', '..', '..', '..')
|
|
23
23
|
const bootstrapPath = join(repoRoot, 'packages', 'core', 'src', 'bootstrap.ts')
|
|
24
|
-
const cacheTypesPath = join(repoRoot, 'packages', 'cache', 'src', '
|
|
24
|
+
const cacheTypesPath = join(repoRoot, 'packages', 'cache-provider-kit', 'src', 'index.ts')
|
|
25
25
|
const cacheAgentsPath = join(repoRoot, 'packages', 'cache', 'AGENTS.md')
|
|
26
26
|
const reviewChecklistPath = join(repoRoot, '.ai', 'review-checklist.md')
|
|
27
27
|
|
|
@@ -46,7 +46,7 @@ function registeredCacheToken(): string {
|
|
|
46
46
|
function cacheStrategyBody(): string {
|
|
47
47
|
const source = readSource(cacheTypesPath)
|
|
48
48
|
const start = source.indexOf('export type CacheStrategy = {')
|
|
49
|
-
if (start === -1) throw new Error('[internal] CacheStrategy type not found in packages/cache/src/
|
|
49
|
+
if (start === -1) throw new Error('[internal] CacheStrategy type not found in packages/cache-provider-kit/src/index.ts')
|
|
50
50
|
const rest = source.slice(start)
|
|
51
51
|
const endOffset = rest.search(/^\}/m)
|
|
52
52
|
if (endOffset === -1) throw new Error('[internal] CacheStrategy type body is not terminated')
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { defineCacheProvider, type ResolvedCacheProvider } from '../provider'
|
|
2
|
+
import { createCacheService } from '../service'
|
|
3
|
+
import { createMemoryStrategy } from '../strategies/memory'
|
|
4
|
+
import { defineCacheProviderConformance, randomNamespace, withConformanceNamespace } from '../testing/conformance'
|
|
5
|
+
import type { CacheStrategy } from '../types'
|
|
6
|
+
|
|
7
|
+
const stores = new Map<string, CacheStrategy>()
|
|
8
|
+
|
|
9
|
+
const definition = defineCacheProvider<Record<string, never>>({
|
|
10
|
+
id: 'acme/shared-memory',
|
|
11
|
+
settings: { safeParse: () => ({ success: true, data: {} }) },
|
|
12
|
+
capability: () => ({ placement: 'local' }),
|
|
13
|
+
create: (_config, context) => {
|
|
14
|
+
let store = stores.get(context.namespace)
|
|
15
|
+
if (!store) {
|
|
16
|
+
store = createMemoryStrategy({ defaultTtl: context.defaultTtl, maxEntries: 0 })
|
|
17
|
+
stores.set(context.namespace, store)
|
|
18
|
+
}
|
|
19
|
+
const base = store
|
|
20
|
+
return {
|
|
21
|
+
get: (key, options) => base.get(key, options),
|
|
22
|
+
set: (key, value, options) => base.set(key, value, options),
|
|
23
|
+
has: (key) => base.has(key),
|
|
24
|
+
delete: (key) => base.delete(key),
|
|
25
|
+
deleteByTags: (tags) => base.deleteByTags(tags),
|
|
26
|
+
clear: () => base.clear(),
|
|
27
|
+
keys: (pattern) => base.keys(pattern),
|
|
28
|
+
stats: () => base.stats(),
|
|
29
|
+
cleanup: () => base.cleanup!(),
|
|
30
|
+
healthcheck: async () => {},
|
|
31
|
+
close: async () => {},
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
function resolved(namespace: string): ResolvedCacheProvider {
|
|
37
|
+
return {
|
|
38
|
+
id: definition.id,
|
|
39
|
+
definition: definition as ResolvedCacheProvider['definition'],
|
|
40
|
+
config: {},
|
|
41
|
+
placement: 'local',
|
|
42
|
+
namespace,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('conformance fixture lifecycle helpers', () => {
|
|
47
|
+
it('generates valid unique namespaces', () => {
|
|
48
|
+
const first = randomNamespace('kit')
|
|
49
|
+
const second = randomNamespace('kit')
|
|
50
|
+
expect(first).toMatch(/^kit-[0-9a-f]{12}$/)
|
|
51
|
+
expect(first).not.toBe(second)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('clears tracked tenants and closes services even when the body throws', async () => {
|
|
55
|
+
let namespace = ''
|
|
56
|
+
await expect(withConformanceNamespace(
|
|
57
|
+
(target, options) => createCacheService({ provider: resolved(target), ...(options ?? {}) }),
|
|
58
|
+
async (ctx) => {
|
|
59
|
+
namespace = ctx.namespace
|
|
60
|
+
const service = await ctx.open()
|
|
61
|
+
await service.set('leftover', 1)
|
|
62
|
+
throw new Error('body failed')
|
|
63
|
+
},
|
|
64
|
+
)).rejects.toThrow('body failed')
|
|
65
|
+
expect(await stores.get(namespace)?.keys()).toEqual([])
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
defineCacheProviderConformance({
|
|
70
|
+
name: 'in-process shared memory provider',
|
|
71
|
+
createService: (namespace, options) => createCacheService({
|
|
72
|
+
provider: resolved(namespace),
|
|
73
|
+
...(options?.defaultTtl !== undefined ? { defaultTtl: options.defaultTtl } : {}),
|
|
74
|
+
}),
|
|
75
|
+
supportsReturnExpired: true,
|
|
76
|
+
batchSize: 300,
|
|
77
|
+
})
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { promises as fs } from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import {
|
|
7
|
+
CacheProviderError,
|
|
8
|
+
defineCacheProviderRegistration,
|
|
9
|
+
resolveCacheProvider,
|
|
10
|
+
type CacheProviderDefinition,
|
|
11
|
+
type CacheProviderSettingsSchema,
|
|
12
|
+
type ResolvedCacheProvider,
|
|
13
|
+
} from '../provider'
|
|
14
|
+
import { createCacheService } from '../service'
|
|
15
|
+
import { defineCacheProviderConformance } from '../testing/conformance'
|
|
16
|
+
import { spawnConformanceRemoteProcess } from '../testing/remote'
|
|
17
|
+
|
|
18
|
+
type FixtureConfig = { directory: string; maxBatch: number }
|
|
19
|
+
|
|
20
|
+
type FixtureModule = {
|
|
21
|
+
provider: CacheProviderDefinition<FixtureConfig>
|
|
22
|
+
settings: CacheProviderSettingsSchema<FixtureConfig>
|
|
23
|
+
fixtureEntryPath(directory: string, namespace: string, physicalKey: string): string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const PROVIDER_ID = 'acme/fixture-fs'
|
|
27
|
+
const FIXTURES_DIR = path.resolve(__dirname, '../../fixtures')
|
|
28
|
+
const FIXTURE_PACKAGE_DIR = path.join(FIXTURES_DIR, 'cache-provider-fixture')
|
|
29
|
+
const CHILD_RUNNER = path.join(FIXTURES_DIR, 'conformance-child.mjs')
|
|
30
|
+
const HOST_BUILDER = path.join(FIXTURES_DIR, 'build-conformance-host.mjs')
|
|
31
|
+
const SETUP_TIMEOUT_MS = 180_000
|
|
32
|
+
|
|
33
|
+
jest.setTimeout(120_000)
|
|
34
|
+
|
|
35
|
+
let workDir = ''
|
|
36
|
+
let dataDir = ''
|
|
37
|
+
let hostModule = ''
|
|
38
|
+
let providerEntry = ''
|
|
39
|
+
let installMode: 'npm' | 'tar' = 'tar'
|
|
40
|
+
|
|
41
|
+
function installTarball(tarball: string, appDir: string): 'npm' | 'tar' {
|
|
42
|
+
try {
|
|
43
|
+
execFileSync('npm', [
|
|
44
|
+
'install',
|
|
45
|
+
tarball,
|
|
46
|
+
'--no-save',
|
|
47
|
+
'--no-package-lock',
|
|
48
|
+
'--ignore-scripts',
|
|
49
|
+
'--no-audit',
|
|
50
|
+
'--no-fund',
|
|
51
|
+
'--offline',
|
|
52
|
+
'--legacy-peer-deps',
|
|
53
|
+
], { cwd: appDir, stdio: 'pipe', timeout: 60_000 })
|
|
54
|
+
if (existsSync(path.join(appDir, 'node_modules', '@acme', 'cache-provider-fixture', 'package.json'))) return 'npm'
|
|
55
|
+
} catch {
|
|
56
|
+
// offline installs may be refused; fall back to extracting the tarball
|
|
57
|
+
}
|
|
58
|
+
const target = path.join(appDir, 'node_modules', '@acme', 'cache-provider-fixture')
|
|
59
|
+
rmSync(target, { recursive: true, force: true })
|
|
60
|
+
mkdirSync(target, { recursive: true })
|
|
61
|
+
execFileSync('tar', ['-xzf', tarball, '-C', target, '--strip-components=1'], { stdio: 'pipe' })
|
|
62
|
+
return 'tar'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function loadFixture(): Promise<FixtureModule> {
|
|
66
|
+
return (await import(providerEntry)) as FixtureModule
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function fixtureRegistration(settings: () => unknown = () => ({ directory: dataDir, maxBatch: 128 })) {
|
|
70
|
+
return defineCacheProviderRegistration<FixtureConfig>({
|
|
71
|
+
id: PROVIDER_ID,
|
|
72
|
+
load: () => loadFixture().then((module) => module.provider),
|
|
73
|
+
settings,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function resolveFixture(namespace: string): Promise<ResolvedCacheProvider> {
|
|
78
|
+
const resolved = await resolveCacheProvider([fixtureRegistration()], {
|
|
79
|
+
env: { CACHE_PROVIDER: PROVIDER_ID, CACHE_PROVIDER_NAMESPACE: namespace },
|
|
80
|
+
})
|
|
81
|
+
if (!resolved) throw new Error('[internal] fixture provider was not selected')
|
|
82
|
+
return resolved
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
beforeAll(() => {
|
|
86
|
+
workDir = mkdtempSync(path.join(os.tmpdir(), 'om-cache-provider-fixture-'))
|
|
87
|
+
dataDir = path.join(workDir, 'data')
|
|
88
|
+
mkdirSync(dataDir, { recursive: true })
|
|
89
|
+
|
|
90
|
+
const packDir = path.join(workDir, 'pack')
|
|
91
|
+
mkdirSync(packDir, { recursive: true })
|
|
92
|
+
execFileSync('npm', ['pack', '--pack-destination', packDir, '--ignore-scripts', '--silent'], {
|
|
93
|
+
cwd: FIXTURE_PACKAGE_DIR,
|
|
94
|
+
stdio: 'pipe',
|
|
95
|
+
timeout: 60_000,
|
|
96
|
+
})
|
|
97
|
+
const tarball = readdirSync(packDir).find((name) => name.endsWith('.tgz'))
|
|
98
|
+
if (!tarball) throw new Error('[internal] npm pack produced no tarball')
|
|
99
|
+
|
|
100
|
+
const appDir = path.join(workDir, 'app')
|
|
101
|
+
mkdirSync(appDir, { recursive: true })
|
|
102
|
+
writeFileSync(path.join(appDir, 'package.json'), JSON.stringify({ name: 'conformance-app', private: true, version: '0.0.0' }))
|
|
103
|
+
installMode = installTarball(path.join(packDir, tarball), appDir)
|
|
104
|
+
providerEntry = path.join(appDir, 'node_modules', '@acme', 'cache-provider-fixture', 'index.js')
|
|
105
|
+
|
|
106
|
+
hostModule = path.join(workDir, 'host', 'conformance-host.mjs')
|
|
107
|
+
execFileSync(process.execPath, [HOST_BUILDER, hostModule], { stdio: 'pipe', timeout: 120_000 })
|
|
108
|
+
}, SETUP_TIMEOUT_MS)
|
|
109
|
+
|
|
110
|
+
afterAll(() => {
|
|
111
|
+
if (workDir) rmSync(workDir, { recursive: true, force: true })
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('packed fixture provider installation', () => {
|
|
115
|
+
it('installs only the packed files outside the workspace', () => {
|
|
116
|
+
const installedDir = path.dirname(providerEntry)
|
|
117
|
+
expect(['npm', 'tar']).toContain(installMode)
|
|
118
|
+
expect(installedDir.startsWith(workDir)).toBe(true)
|
|
119
|
+
expect(readdirSync(installedDir).sort()).toEqual(['index.d.ts', 'index.js', 'package.json'])
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('resolves through a registration with shared placement and validated settings', async () => {
|
|
123
|
+
const resolved = await resolveFixture('fixture-ns')
|
|
124
|
+
expect(resolved.id).toBe(PROVIDER_ID)
|
|
125
|
+
expect(resolved.placement).toBe('shared')
|
|
126
|
+
expect(resolved.namespace).toBe('fixture-ns')
|
|
127
|
+
expect(resolved.config).toEqual({ directory: dataDir, maxBatch: 128 })
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('rejects invalid settings with field paths from the hand-written schema', async () => {
|
|
131
|
+
const registration = fixtureRegistration(() => ({ directory: 'relative/path', maxBatch: 0, extra: true }))
|
|
132
|
+
await expect(resolveCacheProvider([registration], {
|
|
133
|
+
env: { CACHE_PROVIDER: PROVIDER_ID, CACHE_PROVIDER_NAMESPACE: 'fixture-ns' },
|
|
134
|
+
})).rejects.toMatchObject({
|
|
135
|
+
code: 'invalid_settings',
|
|
136
|
+
fieldPaths: expect.arrayContaining(['directory', 'maxBatch', 'extra']),
|
|
137
|
+
})
|
|
138
|
+
await expect(resolveCacheProvider([fixtureRegistration(() => ({}))], {
|
|
139
|
+
env: { CACHE_PROVIDER: PROVIDER_ID, CACHE_PROVIDER_NAMESPACE: 'fixture-ns' },
|
|
140
|
+
})).rejects.toBeInstanceOf(CacheProviderError)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('stores a logical entry in one namespaced file', async () => {
|
|
144
|
+
const service = createCacheService({ provider: await resolveFixture('layout-ns') })
|
|
145
|
+
try {
|
|
146
|
+
await service.set('layout-key', { ok: true })
|
|
147
|
+
const files = (await fs.readdir(path.join(dataDir, 'ns-layout-ns'))).filter((name) => name.endsWith('.json'))
|
|
148
|
+
expect(files).toHaveLength(1)
|
|
149
|
+
} finally {
|
|
150
|
+
await service.clear()
|
|
151
|
+
await service.close?.()
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
defineCacheProviderConformance({
|
|
157
|
+
name: `${PROVIDER_ID} (packed tarball)`,
|
|
158
|
+
createService: async (namespace, options) => createCacheService({
|
|
159
|
+
provider: await resolveFixture(namespace),
|
|
160
|
+
...(options?.defaultTtl !== undefined ? { defaultTtl: options.defaultTtl } : {}),
|
|
161
|
+
}),
|
|
162
|
+
createIndependentProcess: (namespace) => spawnConformanceRemoteProcess({
|
|
163
|
+
command: process.execPath,
|
|
164
|
+
args: [CHILD_RUNNER],
|
|
165
|
+
env: {
|
|
166
|
+
CACHE_CONFORMANCE_HOST: hostModule,
|
|
167
|
+
CACHE_CONFORMANCE_PROVIDER_ENTRY: providerEntry,
|
|
168
|
+
CACHE_CONFORMANCE_DIRECTORY: dataDir,
|
|
169
|
+
CACHE_PROVIDER: PROVIDER_ID,
|
|
170
|
+
CACHE_PROVIDER_NAMESPACE: namespace,
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
corrupt: async ({ namespace, physicalKey }) => {
|
|
174
|
+
const fixture = await loadFixture()
|
|
175
|
+
await fs.writeFile(fixture.fixtureEntryPath(dataDir, namespace, physicalKey), '{"v":1,"key":', 'utf8')
|
|
176
|
+
},
|
|
177
|
+
supportsReturnExpired: true,
|
|
178
|
+
})
|