@prezly/theme-kit-core 10.10.0 → 10.11.0

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.
@@ -0,0 +1,219 @@
1
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
2
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
3
+ import { createStackedCache } from "./stacked.mjs";
4
+ /** A layer that exposes `lookup`, with a spy on `set`. */
5
+ function layer(name) {
6
+ var entries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
7
+ var store = new Map(Object.entries(entries));
8
+ var set = jest.fn(/*#__PURE__*/function () {
9
+ var _ref = _asyncToGenerator(function* (key, value, version, options) {
10
+ store.set(key, {
11
+ value,
12
+ version,
13
+ ttl: options === null || options === void 0 ? void 0 : options.ttl
14
+ });
15
+ });
16
+ return function (_x, _x2, _x3, _x4) {
17
+ return _ref.apply(this, arguments);
18
+ };
19
+ }());
20
+ var cache = {
21
+ get: (key, latestVersion) => {
22
+ var entry = store.get(key);
23
+ return entry && entry.version >= latestVersion ? entry.value : undefined;
24
+ },
25
+ lookup: (key, latestVersion) => {
26
+ var entry = store.get(key);
27
+ if (!entry || entry.version < latestVersion) return undefined;
28
+ return {
29
+ value: entry.value,
30
+ version: entry.version,
31
+ ttl: entry.ttl,
32
+ layer: name
33
+ };
34
+ },
35
+ set,
36
+ namespace() {
37
+ return cache;
38
+ }
39
+ };
40
+ return {
41
+ cache,
42
+ set,
43
+ store
44
+ };
45
+ }
46
+
47
+ /** A layer without `lookup`, as a third-party cache would be. */
48
+ function legacyLayer() {
49
+ var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
50
+ var store = new Map(Object.entries(entries));
51
+ var set = jest.fn(/*#__PURE__*/_asyncToGenerator(function* () {}));
52
+ var cache = {
53
+ get: (key, latestVersion, onSource) => {
54
+ var entry = store.get(key);
55
+ if (!entry || entry.version < latestVersion) return undefined;
56
+ onSource === null || onSource === void 0 || onSource('custom');
57
+ return entry.value;
58
+ },
59
+ set,
60
+ namespace() {
61
+ return cache;
62
+ }
63
+ };
64
+ return {
65
+ cache,
66
+ set
67
+ };
68
+ }
69
+ var flush = () => new Promise(resolve => setImmediate(resolve));
70
+ it('refills every earlier layer from a later hit with the entry version and retention', /*#__PURE__*/_asyncToGenerator(function* () {
71
+ var memory = layer('memory');
72
+ var redis = layer('redis', {
73
+ key: {
74
+ value: {
75
+ name: 'room'
76
+ },
77
+ version: 5
78
+ }
79
+ });
80
+ var stacked = createStackedCache([memory.cache, redis.cache]);
81
+ var sources = [];
82
+ expect(yield stacked.get('key', 3, source => sources.push(source))).toEqual({
83
+ name: 'room'
84
+ });
85
+ expect(sources).toEqual(['redis']);
86
+ yield flush();
87
+ expect(memory.set).toHaveBeenCalledWith('key', {
88
+ name: 'room'
89
+ }, 5, undefined);
90
+ expect(redis.set).not.toHaveBeenCalled();
91
+ sources.length = 0;
92
+ expect(yield stacked.get('key', 5, source => sources.push(source))).toEqual({
93
+ name: 'room'
94
+ });
95
+ expect(sources).toEqual(['memory']);
96
+ }));
97
+ it('carries a short retention into the refilled layer', /*#__PURE__*/_asyncToGenerator(function* () {
98
+ var memory = layer('memory');
99
+ var redis = layer('redis', {
100
+ missing: {
101
+ value: null,
102
+ version: 2,
103
+ ttl: 45
104
+ }
105
+ });
106
+ var stacked = createStackedCache([memory.cache, redis.cache]);
107
+ expect(yield stacked.get('missing', 2)).toBeNull();
108
+ yield flush();
109
+ expect(memory.set).toHaveBeenCalledWith('missing', null, 2, {
110
+ ttl: 45
111
+ });
112
+ }));
113
+ it('never refills from a stale entry or on a miss', /*#__PURE__*/_asyncToGenerator(function* () {
114
+ var memory = layer('memory');
115
+ var redis = layer('redis', {
116
+ key: {
117
+ value: 'old',
118
+ version: 1
119
+ }
120
+ });
121
+ var stacked = createStackedCache([memory.cache, redis.cache]);
122
+ expect(yield stacked.get('key', 2)).toBeUndefined();
123
+ expect(yield stacked.get('absent', 1)).toBeUndefined();
124
+ yield flush();
125
+ expect(memory.set).not.toHaveBeenCalled();
126
+ }));
127
+ it('treats an entry holding undefined as a miss and reads the next layer', /*#__PURE__*/_asyncToGenerator(function* () {
128
+ var memory = layer('memory', {
129
+ key: {
130
+ value: undefined,
131
+ version: 1
132
+ }
133
+ });
134
+ var redis = layer('redis', {
135
+ key: {
136
+ value: 'value',
137
+ version: 1
138
+ }
139
+ });
140
+ var stacked = createStackedCache([memory.cache, redis.cache]);
141
+ var sources = [];
142
+ expect(yield stacked.get('key', 1, source => sources.push(source))).toBe('value');
143
+ expect(sources).toEqual(['redis']);
144
+ }));
145
+ it('does not refill from a layer without lookup', /*#__PURE__*/_asyncToGenerator(function* () {
146
+ var memory = layer('memory');
147
+ var custom = legacyLayer({
148
+ key: {
149
+ value: 'custom value',
150
+ version: 1
151
+ }
152
+ });
153
+ var stacked = createStackedCache([memory.cache, custom.cache]);
154
+ var sources = [];
155
+ expect(yield stacked.get('key', 1, source => sources.push(source))).toBe('custom value');
156
+ expect(sources).toEqual(['custom']);
157
+ yield flush();
158
+ expect(memory.set).not.toHaveBeenCalled();
159
+ }));
160
+ it('withholds lookup when a layer cannot report hits, so nested stacks keep those hits', /*#__PURE__*/_asyncToGenerator(function* () {
161
+ var memory = layer('memory');
162
+ var custom = legacyLayer({
163
+ key: {
164
+ value: 'custom value',
165
+ version: 1
166
+ }
167
+ });
168
+ var inner = createStackedCache([memory.cache, custom.cache]);
169
+ expect(inner.lookup).toBeUndefined();
170
+ var outerMemory = layer('memory');
171
+ var outer = createStackedCache([outerMemory.cache, inner]);
172
+ var sources = [];
173
+ expect(yield outer.get('key', 1, source => sources.push(source))).toBe('custom value');
174
+ expect(sources).toEqual(['custom']);
175
+ yield flush();
176
+ expect(outerMemory.set).not.toHaveBeenCalled();
177
+ expect(createStackedCache([memory.cache, layer('redis').cache]).lookup).toBeDefined();
178
+ }));
179
+ it('returns the hit even when a refill write fails', /*#__PURE__*/_asyncToGenerator(function* () {
180
+ var memory = layer('memory');
181
+ memory.set.mockRejectedValue(new Error('memory full'));
182
+ var redis = layer('redis', {
183
+ key: {
184
+ value: 'value',
185
+ version: 1
186
+ }
187
+ });
188
+ var stacked = createStackedCache([memory.cache, redis.cache]);
189
+ yield expect(stacked.get('key', 1)).resolves.toBe('value');
190
+ yield flush();
191
+ expect(memory.set).toHaveBeenCalled();
192
+ }));
193
+ it('exposes lookup itself so stacks can nest, and writes through to all layers', /*#__PURE__*/_asyncToGenerator(function* () {
194
+ var _stacked$lookup;
195
+ var memory = layer('memory');
196
+ var redis = layer('redis', {
197
+ key: {
198
+ value: 'value',
199
+ version: 9,
200
+ ttl: 10
201
+ }
202
+ });
203
+ var stacked = createStackedCache([memory.cache, redis.cache]);
204
+ expect(yield (_stacked$lookup = stacked.lookup) === null || _stacked$lookup === void 0 ? void 0 : _stacked$lookup.call(stacked, 'key', 9)).toEqual({
205
+ value: 'value',
206
+ version: 9,
207
+ ttl: 10,
208
+ layer: 'redis'
209
+ });
210
+ yield stacked.set('other', 'written', 3, {
211
+ ttl: 5
212
+ });
213
+ expect(memory.set).toHaveBeenCalledWith('other', 'written', 3, {
214
+ ttl: 5
215
+ });
216
+ expect(redis.set).toHaveBeenCalledWith('other', 'written', 3, {
217
+ ttl: 5
218
+ });
219
+ }));
@@ -8,12 +8,28 @@ export interface SetOptions {
8
8
  */
9
9
  ttl?: Seconds;
10
10
  }
11
+ /**
12
+ * A validated entry with the metadata another layer needs to store it as is.
13
+ */
14
+ export interface Lookup<T> {
15
+ value: T;
16
+ version: UnixTimestampInSeconds;
17
+ /** Remaining or original short retention, when the entry has one. */
18
+ ttl?: Seconds;
19
+ layer: CacheLayer;
20
+ }
11
21
  export interface Cache {
12
22
  /**
13
23
  * Resolves `undefined` only when no entry exists (or it is stale).
14
24
  * A stored `null`, `false` or `0` is a valid hit and must be returned as is.
15
25
  */
16
26
  get<T>(key: string, latestVersion: UnixTimestampInSeconds, onSource?: (layer: CacheLayer) => void): Awaitable<T | undefined>;
27
+ /**
28
+ * Like `get`, but returns the entry's version and retention as well, so a
29
+ * stacked cache can refill an earlier layer without guessing. Layers that
30
+ * do not implement it are read with `get` and never used as a refill source.
31
+ */
32
+ lookup?<T>(key: string, latestVersion: UnixTimestampInSeconds): Awaitable<Lookup<T> | undefined>;
17
33
  set<T>(key: string, value: T, version: UnixTimestampInSeconds, options?: SetOptions): Awaitable<void>;
18
34
  namespace(namespace: string): Cache;
19
35
  }
package/package.json CHANGED
@@ -1,74 +1,73 @@
1
1
  {
2
- "name": "@prezly/theme-kit-core",
3
- "version": "10.10.0",
4
- "description": "Data layer and utility library for developing Prezly themes with JavaScript",
5
- "main": "build/index.mjs",
6
- "type": "module",
7
- "types": "build/index.d.ts",
8
- "files": [
9
- "build",
10
- "README.md"
11
- ],
12
- "exports": {
13
- ".": {
14
- "types": "./build/index.d.ts",
15
- "require": "./build/index.cjs",
16
- "default": "./build/index.mjs"
17
- }
18
- },
19
- "scripts": {
20
- "prepack": "pnpm test:build",
21
- "prebuild": "rimraf --glob build/* *.tsbuildinfo",
22
- "build": "pnpm build:types && pnpm build:esm && pnpm build:cjs",
23
- "build:types": "tsc --project .",
24
- "build:esm": "babel --config-file=../../babel.esm.json src/ --extensions=.ts --out-dir=build/ --out-file-extension=.mjs",
25
- "build:cjs": "babel --config-file=../../babel.cjs.json src/ --extensions=.ts --out-dir=build/ --out-file-extension=.cjs",
26
- "dev": "tsc --watch --preserveWatchOutput --project .",
27
- "typecheck": "tsc --noEmit --emitDeclarationOnly false --incremental --project .",
28
- "lint": "biome lint src",
29
- "lint:fix": "biome lint --write src",
30
- "format": "biome format src",
31
- "format:fix": "biome format --write src",
32
- "test": "pnpm test:unit && pnpm test:build",
33
- "test:unit": "jest",
34
- "test:build": "pnpm test:build:cjs && pnpm test:build:esm && pnpm test:build:types",
35
- "test:build:types": "tsc --esModuleInterop --noEmit --target esnext --moduleResolution node build/index.d.ts",
36
- "test:build:esm": "node tests/test-build.mjs",
37
- "test:build:cjs": "node tests/test-build.cjs"
38
- },
39
- "peerDependencies": {
40
- "@prezly/sdk": "21.12.0 || ^23.x"
41
- },
42
- "dependencies": {
43
- "@prezly/theme-kit-intl": "workspace:^",
44
- "@prezly/uploadcare": "2.4.4",
45
- "@technically/is-not-undefined": "^1.0.0",
46
- "@technically/omit-undefined": "^1.0.4",
47
- "parse-data-url": "^6.0.0",
48
- "url-pattern": "^1.0.3"
49
- },
50
- "devDependencies": {
51
- "@types/parse-data-url": "3.0.2"
52
- },
53
- "engines": {
54
- "node": ">= 16.x",
55
- "npm": ">= 8.x"
56
- },
57
- "keywords": [
58
- "prezly"
59
- ],
60
- "author": "Prezly Team",
61
- "license": "MIT",
62
- "bugs": {
63
- "url": "https://github.com/prezly/theme-kit-js/issues"
64
- },
65
- "homepage": "https://github.com/prezly/theme-kit-js/tree/main/packages/core#readme",
66
- "repository": {
67
- "type": "git",
68
- "url": "git+https://github.com/prezly/theme-kit-js.git",
69
- "directory": "packages/core"
70
- },
71
- "publishConfig": {
72
- "access": "public"
2
+ "name": "@prezly/theme-kit-core",
3
+ "version": "10.11.0",
4
+ "description": "Data layer and utility library for developing Prezly themes with JavaScript",
5
+ "main": "build/index.mjs",
6
+ "type": "module",
7
+ "types": "build/index.d.ts",
8
+ "files": [
9
+ "build",
10
+ "README.md"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./build/index.d.ts",
15
+ "require": "./build/index.cjs",
16
+ "default": "./build/index.mjs"
73
17
  }
74
- }
18
+ },
19
+ "peerDependencies": {
20
+ "@prezly/sdk": "21.12.0 || ^23.x"
21
+ },
22
+ "dependencies": {
23
+ "@prezly/uploadcare": "2.4.4",
24
+ "@technically/is-not-undefined": "^1.0.0",
25
+ "@technically/omit-undefined": "^1.0.4",
26
+ "parse-data-url": "^6.0.0",
27
+ "url-pattern": "^1.0.3",
28
+ "@prezly/theme-kit-intl": "^10.9.1"
29
+ },
30
+ "devDependencies": {
31
+ "@types/parse-data-url": "3.0.2"
32
+ },
33
+ "engines": {
34
+ "node": ">= 16.x",
35
+ "npm": ">= 8.x"
36
+ },
37
+ "keywords": [
38
+ "prezly"
39
+ ],
40
+ "author": "Prezly Team",
41
+ "license": "MIT",
42
+ "bugs": {
43
+ "url": "https://github.com/prezly/theme-kit-js/issues"
44
+ },
45
+ "homepage": "https://github.com/prezly/theme-kit-js/tree/main/packages/core#readme",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/prezly/theme-kit-js.git",
49
+ "directory": "packages/core"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "scripts": {
55
+ "prebuild": "rimraf --glob build/* *.tsbuildinfo",
56
+ "build": "pnpm build:types && pnpm build:esm && pnpm build:cjs",
57
+ "build:types": "tsc --project .",
58
+ "build:esm": "babel --config-file=../../babel.esm.json src/ --extensions=.ts --out-dir=build/ --out-file-extension=.mjs",
59
+ "build:cjs": "babel --config-file=../../babel.cjs.json src/ --extensions=.ts --out-dir=build/ --out-file-extension=.cjs",
60
+ "dev": "tsc --watch --preserveWatchOutput --project .",
61
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false --incremental --project .",
62
+ "lint": "biome lint src",
63
+ "lint:fix": "biome lint --write src",
64
+ "format": "biome format src",
65
+ "format:fix": "biome format --write src",
66
+ "test": "pnpm test:unit && pnpm test:build",
67
+ "test:unit": "jest",
68
+ "test:build": "pnpm test:build:cjs && pnpm test:build:esm && pnpm test:build:types",
69
+ "test:build:types": "tsc --esModuleInterop --noEmit --target esnext --moduleResolution node build/index.d.ts",
70
+ "test:build:esm": "node tests/test-build.mjs",
71
+ "test:build:cjs": "node tests/test-build.cjs"
72
+ }
73
+ }