@prezly/theme-kit-core 10.10.1 → 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.
@@ -1,4 +1,4 @@
1
- import { createSharedMemoryCache, RECORDS_LIMIT } from "./memory.mjs";
1
+ import { clearSharedMemoryCache, configureSharedMemoryCache, createSharedMemoryCache, inspectSharedMemoryCache } from "./memory.mjs";
2
2
  describe('createSharedMemoryCache', () => {
3
3
  afterEach(() => jest.useRealTimers());
4
4
  it.each([null, false, 0, ''])('returns a stored %p as a hit', value => {
@@ -53,30 +53,112 @@ describe('createSharedMemoryCache', () => {
53
53
  expect(a.get('hello', 0)).toBe('world');
54
54
  expect(b.get('hello', 0)).toBe('universe');
55
55
  });
56
- it('should garbage-collect old cache records when threshold is reached (with a chance of 1/100)', () => {
57
- var cache = createSharedMemoryCache();
58
- for (var i = 0; i < RECORDS_LIMIT; i += 1) {
59
- cache.set("record-".concat(i), "value-".concat(i), 0);
60
- }
61
- for (var _i = 0; _i < RECORDS_LIMIT; _i += 1) {
62
- expect(cache.get("record-".concat(_i), 0)).toBe("value-".concat(_i));
63
- }
64
-
65
- // Write 10% more records, 10 times to trigger CG
66
- for (var repeat = 0; repeat < 100; repeat += 1) {
67
- for (var _i2 = RECORDS_LIMIT; _i2 < RECORDS_LIMIT * 1.1; _i2 += 1) {
68
- cache.set("record-".concat(_i2), "value-".concat(_i2), 0);
69
- }
56
+ it('evicts the least recently used entries once the record bound is reached', () => {
57
+ clearSharedMemoryCache();
58
+ configureSharedMemoryCache({
59
+ maxRecords: 3
60
+ });
61
+ try {
62
+ var cache = createSharedMemoryCache('lru:');
63
+ cache.set('a', 1, 0);
64
+ cache.set('b', 2, 0);
65
+ cache.set('c', 3, 0);
66
+ expect(cache.get('a', 0)).toBe(1); // a becomes most recently used
67
+ cache.set('d', 4, 0); // evicts b, the least recently used
68
+ expect(cache.get('b', 0)).toBeUndefined();
69
+ expect(cache.get('a', 0)).toBe(1);
70
+ expect(cache.get('c', 0)).toBe(3);
71
+ expect(cache.get('d', 0)).toBe(4);
72
+ expect(inspectSharedMemoryCache().records).toBe(3);
73
+ } finally {
74
+ configureSharedMemoryCache();
75
+ clearSharedMemoryCache();
70
76
  }
71
-
72
- // the oldest part of the cache should be removed already
73
- for (var _i3 = 0; _i3 < RECORDS_LIMIT * 0.09; _i3 += 1) {
74
- expect(cache.get("record-".concat(_i3), 0)).toBeUndefined();
77
+ });
78
+ it('evicts by estimated bytes and accounts for overwrites and removals', () => {
79
+ clearSharedMemoryCache();
80
+ configureSharedMemoryCache({
81
+ maxBytes: 200
82
+ });
83
+ try {
84
+ var cache = createSharedMemoryCache('bytes:');
85
+ var big = 'x'.repeat(120);
86
+ cache.set('one', big, 0);
87
+ expect(inspectSharedMemoryCache().bytes).toBeGreaterThan(120);
88
+ cache.set('two', big, 0); // two entries exceed 200 bytes: one is evicted
89
+ expect(cache.get('one', 0)).toBeUndefined();
90
+ expect(cache.get('two', 0)).toBe(big);
91
+ cache.set('two', 'tiny', 0); // overwrite releases the old size
92
+ expect(inspectSharedMemoryCache().bytes).toBeLessThan(40);
93
+ expect(cache.get('two', 1)).toBeUndefined(); // version removal releases the rest
94
+ expect(inspectSharedMemoryCache().bytes).toBe(0);
95
+ } finally {
96
+ configureSharedMemoryCache();
97
+ clearSharedMemoryCache();
75
98
  }
76
-
77
- // and the rest kept
78
- for (var _i4 = RECORDS_LIMIT * 0.11; _i4 < RECORDS_LIMIT * 1.1; _i4 += 1) {
79
- expect(cache.get("record-".concat(_i4), 0)).toBe("value-".concat(_i4));
99
+ });
100
+ it('keeps a newer entry when an older version is written late', () => {
101
+ var cache = createSharedMemoryCache('late:');
102
+ cache.set('key', 'new', 2);
103
+ cache.set('key', 'old', 1); // e.g. a refill that raced with an origin fetch
104
+ expect(cache.get('key', 2)).toBe('new');
105
+ cache.set('key', 'newer', 3);
106
+ expect(cache.get('key', 3)).toBe('newer');
107
+ });
108
+ it('skips an entry larger than the whole store instead of flushing it', () => {
109
+ clearSharedMemoryCache();
110
+ configureSharedMemoryCache({
111
+ maxBytes: 100
112
+ });
113
+ try {
114
+ var cache = createSharedMemoryCache('huge:');
115
+ cache.set('a', 'x'.repeat(30), 0);
116
+ cache.set('b', 'x'.repeat(30), 0);
117
+ cache.set('c', 'x'.repeat(500), 0);
118
+ expect(cache.get('c', 0)).toBeUndefined();
119
+ expect(cache.get('a', 0)).toBe('x'.repeat(30));
120
+ expect(cache.get('b', 0)).toBe('x'.repeat(30));
121
+ expect(inspectSharedMemoryCache().records).toBe(2);
122
+ } finally {
123
+ configureSharedMemoryCache();
124
+ clearSharedMemoryCache();
80
125
  }
81
126
  });
127
+ it('rejects invalid bounds', () => {
128
+ expect(() => configureSharedMemoryCache({
129
+ maxRecords: 0
130
+ })).toThrow(RangeError);
131
+ expect(() => configureSharedMemoryCache({
132
+ maxBytes: 1.5
133
+ })).toThrow(RangeError);
134
+ configureSharedMemoryCache();
135
+ });
136
+ it('exposes version and remaining retention through lookup', () => {
137
+ var _cache$lookup, _cache$lookup2, _cache$lookup3, _cache$lookup4;
138
+ jest.useFakeTimers();
139
+ var cache = createSharedMemoryCache('lookup:');
140
+ cache.set('content', {
141
+ a: 1
142
+ }, 7);
143
+ cache.set('missing', null, 7, {
144
+ ttl: 30
145
+ });
146
+ expect((_cache$lookup = cache.lookup) === null || _cache$lookup === void 0 ? void 0 : _cache$lookup.call(cache, 'content', 7)).toEqual({
147
+ value: {
148
+ a: 1
149
+ },
150
+ version: 7,
151
+ ttl: undefined,
152
+ layer: 'memory'
153
+ });
154
+ jest.advanceTimersByTime(10000);
155
+ expect((_cache$lookup2 = cache.lookup) === null || _cache$lookup2 === void 0 ? void 0 : _cache$lookup2.call(cache, 'missing', 7)).toEqual({
156
+ value: null,
157
+ version: 7,
158
+ ttl: 20,
159
+ layer: 'memory'
160
+ });
161
+ expect((_cache$lookup3 = cache.lookup) === null || _cache$lookup3 === void 0 ? void 0 : _cache$lookup3.call(cache, 'content', 8)).toBeUndefined();
162
+ expect((_cache$lookup4 = cache.lookup) === null || _cache$lookup4 === void 0 ? void 0 : _cache$lookup4.call(cache, 'absent', 7)).toBeUndefined();
163
+ });
82
164
  });
@@ -7,30 +7,107 @@ exports.createStackedCache = createStackedCache;
7
7
  var _telemetry = require("../telemetry.cjs");
8
8
  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); }
9
9
  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); }); }; }
10
+ /**
11
+ * Reads layers in order and refills every earlier layer with a hit found in a
12
+ * later one, carrying the entry's own version and retention, so the next
13
+ * request on this runtime is served from memory instead of Redis. Only layers
14
+ * that expose `lookup` are refill sources; a hit from a `get`-only layer is
15
+ * returned as before and not copied.
16
+ */
10
17
  function createStackedCache(caches) {
11
- return {
12
- get(key, latestVersion, onSource) {
13
- return _asyncToGenerator(function* () {
14
- var _loop = function* _loop() {
15
- var source = 'custom';
16
- var value = onSource ? yield caches[i].get(key, latestVersion, layer => {
17
- source = layer;
18
- }) : yield caches[i].get(key, latestVersion);
19
- if (value !== undefined) {
20
- if (onSource) (0, _telemetry.notify)(onSource, source);
18
+ function find(_x, _x2, _x3) {
19
+ return _find.apply(this, arguments);
20
+ }
21
+ function _find() {
22
+ _find = _asyncToGenerator(function* (key, latestVersion, onSource) {
23
+ var _loop2 = function* _loop2() {
24
+ var layer = caches[i];
25
+ if (layer.lookup) {
26
+ var found = yield layer.lookup(key, latestVersion);
27
+ // An entry holding `undefined` is a miss by the `get` contract.
28
+ if (found !== undefined && found.value !== undefined) {
29
+ if (onSource) (0, _telemetry.notify)(onSource, found.layer);
21
30
  return {
22
- v: value
31
+ v: {
32
+ index: i,
33
+ found,
34
+ value: found.value
35
+ }
23
36
  };
24
37
  }
25
- },
26
- _ret;
27
- for (var i = 0; i < caches.length; i += 1) {
28
- _ret = yield* _loop();
29
- if (_ret) return _ret.v;
30
- }
31
- return undefined;
38
+ return 0; // continue
39
+ }
40
+ var source = 'custom';
41
+ var value = onSource ? yield layer.get(key, latestVersion, reported => {
42
+ source = reported;
43
+ }) : yield layer.get(key, latestVersion);
44
+ if (value !== undefined) {
45
+ if (onSource) (0, _telemetry.notify)(onSource, source);
46
+ return {
47
+ v: {
48
+ index: i,
49
+ found: undefined,
50
+ value
51
+ }
52
+ };
53
+ }
54
+ },
55
+ _ret;
56
+ for (var i = 0; i < caches.length; i += 1) {
57
+ _ret = yield* _loop2();
58
+ if (_ret === 0) continue;
59
+ if (_ret) return _ret.v;
60
+ }
61
+ return {
62
+ index: -1,
63
+ found: undefined,
64
+ value: undefined
65
+ };
66
+ });
67
+ return _find.apply(this, arguments);
68
+ }
69
+ function refill(key, index, found) {
70
+ var _loop = function _loop() {
71
+ var layer = caches[j];
72
+ // Best effort: the response never waits on a refill, and a failing
73
+ // layer must not turn a hit into an error.
74
+ void Promise.resolve().then(() => layer.set(key, found.value, found.version, found.ttl === undefined ? undefined : {
75
+ ttl: found.ttl
76
+ })).catch(() => undefined);
77
+ };
78
+ for (var j = 0; j < index; j += 1) {
79
+ _loop();
80
+ }
81
+ }
82
+
83
+ // A hit from a get-only layer has no version or retention to report, so a
84
+ // stack containing one cannot represent every hit as a `Lookup`. Such a
85
+ // stack is read with `get` by an outer stack and never used as a refill
86
+ // source, instead of turning those hits into misses.
87
+ var lookup = caches.every(cache => cache.lookup) ? (/*#__PURE__*/function () {
88
+ var _ref = _asyncToGenerator(function* (key, latestVersion) {
89
+ var _yield$find = yield find(key, latestVersion),
90
+ index = _yield$find.index,
91
+ found = _yield$find.found;
92
+ if (found && index > 0) refill(key, index, found);
93
+ return found;
94
+ });
95
+ return function (_x4, _x5) {
96
+ return _ref.apply(this, arguments);
97
+ };
98
+ }()) : undefined;
99
+ return {
100
+ get(key, latestVersion, onSource) {
101
+ return _asyncToGenerator(function* () {
102
+ var _yield$find2 = yield find(key, latestVersion, onSource),
103
+ index = _yield$find2.index,
104
+ found = _yield$find2.found,
105
+ value = _yield$find2.value;
106
+ if (found && index > 0) refill(key, index, found);
107
+ return value;
32
108
  })();
33
109
  },
110
+ lookup,
34
111
  set(key, value, version, options) {
35
112
  return _asyncToGenerator(function* () {
36
113
  yield Promise.all(caches.map(cache => cache.set(key, value, version, options)));
@@ -1,2 +1,9 @@
1
1
  import type { Cache } from './type';
2
+ /**
3
+ * Reads layers in order and refills every earlier layer with a hit found in a
4
+ * later one, carrying the entry's own version and retention, so the next
5
+ * request on this runtime is served from memory instead of Redis. Only layers
6
+ * that expose `lookup` are refill sources; a hit from a `get`-only layer is
7
+ * returned as before and not copied.
8
+ */
2
9
  export declare function createStackedCache(caches: Cache[]): Cache;
@@ -1,30 +1,107 @@
1
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
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
3
  import { notify } from "../telemetry.mjs";
4
+ /**
5
+ * Reads layers in order and refills every earlier layer with a hit found in a
6
+ * later one, carrying the entry's own version and retention, so the next
7
+ * request on this runtime is served from memory instead of Redis. Only layers
8
+ * that expose `lookup` are refill sources; a hit from a `get`-only layer is
9
+ * returned as before and not copied.
10
+ */
4
11
  export function createStackedCache(caches) {
5
- return {
6
- get(key, latestVersion, onSource) {
7
- return _asyncToGenerator(function* () {
8
- var _loop = function* _loop() {
9
- var source = 'custom';
10
- var value = onSource ? yield caches[i].get(key, latestVersion, layer => {
11
- source = layer;
12
- }) : yield caches[i].get(key, latestVersion);
13
- if (value !== undefined) {
14
- if (onSource) notify(onSource, source);
12
+ function find(_x, _x2, _x3) {
13
+ return _find.apply(this, arguments);
14
+ }
15
+ function _find() {
16
+ _find = _asyncToGenerator(function* (key, latestVersion, onSource) {
17
+ var _loop2 = function* _loop2() {
18
+ var layer = caches[i];
19
+ if (layer.lookup) {
20
+ var found = yield layer.lookup(key, latestVersion);
21
+ // An entry holding `undefined` is a miss by the `get` contract.
22
+ if (found !== undefined && found.value !== undefined) {
23
+ if (onSource) notify(onSource, found.layer);
15
24
  return {
16
- v: value
25
+ v: {
26
+ index: i,
27
+ found,
28
+ value: found.value
29
+ }
17
30
  };
18
31
  }
19
- },
20
- _ret;
21
- for (var i = 0; i < caches.length; i += 1) {
22
- _ret = yield* _loop();
23
- if (_ret) return _ret.v;
24
- }
25
- return undefined;
32
+ return 0; // continue
33
+ }
34
+ var source = 'custom';
35
+ var value = onSource ? yield layer.get(key, latestVersion, reported => {
36
+ source = reported;
37
+ }) : yield layer.get(key, latestVersion);
38
+ if (value !== undefined) {
39
+ if (onSource) notify(onSource, source);
40
+ return {
41
+ v: {
42
+ index: i,
43
+ found: undefined,
44
+ value
45
+ }
46
+ };
47
+ }
48
+ },
49
+ _ret;
50
+ for (var i = 0; i < caches.length; i += 1) {
51
+ _ret = yield* _loop2();
52
+ if (_ret === 0) continue;
53
+ if (_ret) return _ret.v;
54
+ }
55
+ return {
56
+ index: -1,
57
+ found: undefined,
58
+ value: undefined
59
+ };
60
+ });
61
+ return _find.apply(this, arguments);
62
+ }
63
+ function refill(key, index, found) {
64
+ var _loop = function _loop() {
65
+ var layer = caches[j];
66
+ // Best effort: the response never waits on a refill, and a failing
67
+ // layer must not turn a hit into an error.
68
+ void Promise.resolve().then(() => layer.set(key, found.value, found.version, found.ttl === undefined ? undefined : {
69
+ ttl: found.ttl
70
+ })).catch(() => undefined);
71
+ };
72
+ for (var j = 0; j < index; j += 1) {
73
+ _loop();
74
+ }
75
+ }
76
+
77
+ // A hit from a get-only layer has no version or retention to report, so a
78
+ // stack containing one cannot represent every hit as a `Lookup`. Such a
79
+ // stack is read with `get` by an outer stack and never used as a refill
80
+ // source, instead of turning those hits into misses.
81
+ var lookup = caches.every(cache => cache.lookup) ? (/*#__PURE__*/function () {
82
+ var _ref = _asyncToGenerator(function* (key, latestVersion) {
83
+ var _yield$find = yield find(key, latestVersion),
84
+ index = _yield$find.index,
85
+ found = _yield$find.found;
86
+ if (found && index > 0) refill(key, index, found);
87
+ return found;
88
+ });
89
+ return function (_x4, _x5) {
90
+ return _ref.apply(this, arguments);
91
+ };
92
+ }()) : undefined;
93
+ return {
94
+ get(key, latestVersion, onSource) {
95
+ return _asyncToGenerator(function* () {
96
+ var _yield$find2 = yield find(key, latestVersion, onSource),
97
+ index = _yield$find2.index,
98
+ found = _yield$find2.found,
99
+ value = _yield$find2.value;
100
+ if (found && index > 0) refill(key, index, found);
101
+ return value;
26
102
  })();
27
103
  },
104
+ lookup,
28
105
  set(key, value, version, options) {
29
106
  return _asyncToGenerator(function* () {
30
107
  yield Promise.all(caches.map(cache => cache.set(key, value, version, options)));
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+
3
+ var _stacked = require("./stacked.cjs");
4
+ 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); }
5
+ 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); }); }; }
6
+ /** A layer that exposes `lookup`, with a spy on `set`. */
7
+ function layer(name) {
8
+ var entries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
9
+ var store = new Map(Object.entries(entries));
10
+ var set = jest.fn(/*#__PURE__*/function () {
11
+ var _ref = _asyncToGenerator(function* (key, value, version, options) {
12
+ store.set(key, {
13
+ value,
14
+ version,
15
+ ttl: options === null || options === void 0 ? void 0 : options.ttl
16
+ });
17
+ });
18
+ return function (_x, _x2, _x3, _x4) {
19
+ return _ref.apply(this, arguments);
20
+ };
21
+ }());
22
+ var cache = {
23
+ get: (key, latestVersion) => {
24
+ var entry = store.get(key);
25
+ return entry && entry.version >= latestVersion ? entry.value : undefined;
26
+ },
27
+ lookup: (key, latestVersion) => {
28
+ var entry = store.get(key);
29
+ if (!entry || entry.version < latestVersion) return undefined;
30
+ return {
31
+ value: entry.value,
32
+ version: entry.version,
33
+ ttl: entry.ttl,
34
+ layer: name
35
+ };
36
+ },
37
+ set,
38
+ namespace() {
39
+ return cache;
40
+ }
41
+ };
42
+ return {
43
+ cache,
44
+ set,
45
+ store
46
+ };
47
+ }
48
+
49
+ /** A layer without `lookup`, as a third-party cache would be. */
50
+ function legacyLayer() {
51
+ var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
52
+ var store = new Map(Object.entries(entries));
53
+ var set = jest.fn(/*#__PURE__*/_asyncToGenerator(function* () {}));
54
+ var cache = {
55
+ get: (key, latestVersion, onSource) => {
56
+ var entry = store.get(key);
57
+ if (!entry || entry.version < latestVersion) return undefined;
58
+ onSource === null || onSource === void 0 || onSource('custom');
59
+ return entry.value;
60
+ },
61
+ set,
62
+ namespace() {
63
+ return cache;
64
+ }
65
+ };
66
+ return {
67
+ cache,
68
+ set
69
+ };
70
+ }
71
+ var flush = () => new Promise(resolve => setImmediate(resolve));
72
+ it('refills every earlier layer from a later hit with the entry version and retention', /*#__PURE__*/_asyncToGenerator(function* () {
73
+ var memory = layer('memory');
74
+ var redis = layer('redis', {
75
+ key: {
76
+ value: {
77
+ name: 'room'
78
+ },
79
+ version: 5
80
+ }
81
+ });
82
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
83
+ var sources = [];
84
+ expect(yield stacked.get('key', 3, source => sources.push(source))).toEqual({
85
+ name: 'room'
86
+ });
87
+ expect(sources).toEqual(['redis']);
88
+ yield flush();
89
+ expect(memory.set).toHaveBeenCalledWith('key', {
90
+ name: 'room'
91
+ }, 5, undefined);
92
+ expect(redis.set).not.toHaveBeenCalled();
93
+ sources.length = 0;
94
+ expect(yield stacked.get('key', 5, source => sources.push(source))).toEqual({
95
+ name: 'room'
96
+ });
97
+ expect(sources).toEqual(['memory']);
98
+ }));
99
+ it('carries a short retention into the refilled layer', /*#__PURE__*/_asyncToGenerator(function* () {
100
+ var memory = layer('memory');
101
+ var redis = layer('redis', {
102
+ missing: {
103
+ value: null,
104
+ version: 2,
105
+ ttl: 45
106
+ }
107
+ });
108
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
109
+ expect(yield stacked.get('missing', 2)).toBeNull();
110
+ yield flush();
111
+ expect(memory.set).toHaveBeenCalledWith('missing', null, 2, {
112
+ ttl: 45
113
+ });
114
+ }));
115
+ it('never refills from a stale entry or on a miss', /*#__PURE__*/_asyncToGenerator(function* () {
116
+ var memory = layer('memory');
117
+ var redis = layer('redis', {
118
+ key: {
119
+ value: 'old',
120
+ version: 1
121
+ }
122
+ });
123
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
124
+ expect(yield stacked.get('key', 2)).toBeUndefined();
125
+ expect(yield stacked.get('absent', 1)).toBeUndefined();
126
+ yield flush();
127
+ expect(memory.set).not.toHaveBeenCalled();
128
+ }));
129
+ it('treats an entry holding undefined as a miss and reads the next layer', /*#__PURE__*/_asyncToGenerator(function* () {
130
+ var memory = layer('memory', {
131
+ key: {
132
+ value: undefined,
133
+ version: 1
134
+ }
135
+ });
136
+ var redis = layer('redis', {
137
+ key: {
138
+ value: 'value',
139
+ version: 1
140
+ }
141
+ });
142
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
143
+ var sources = [];
144
+ expect(yield stacked.get('key', 1, source => sources.push(source))).toBe('value');
145
+ expect(sources).toEqual(['redis']);
146
+ }));
147
+ it('does not refill from a layer without lookup', /*#__PURE__*/_asyncToGenerator(function* () {
148
+ var memory = layer('memory');
149
+ var custom = legacyLayer({
150
+ key: {
151
+ value: 'custom value',
152
+ version: 1
153
+ }
154
+ });
155
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, custom.cache]);
156
+ var sources = [];
157
+ expect(yield stacked.get('key', 1, source => sources.push(source))).toBe('custom value');
158
+ expect(sources).toEqual(['custom']);
159
+ yield flush();
160
+ expect(memory.set).not.toHaveBeenCalled();
161
+ }));
162
+ it('withholds lookup when a layer cannot report hits, so nested stacks keep those hits', /*#__PURE__*/_asyncToGenerator(function* () {
163
+ var memory = layer('memory');
164
+ var custom = legacyLayer({
165
+ key: {
166
+ value: 'custom value',
167
+ version: 1
168
+ }
169
+ });
170
+ var inner = (0, _stacked.createStackedCache)([memory.cache, custom.cache]);
171
+ expect(inner.lookup).toBeUndefined();
172
+ var outerMemory = layer('memory');
173
+ var outer = (0, _stacked.createStackedCache)([outerMemory.cache, inner]);
174
+ var sources = [];
175
+ expect(yield outer.get('key', 1, source => sources.push(source))).toBe('custom value');
176
+ expect(sources).toEqual(['custom']);
177
+ yield flush();
178
+ expect(outerMemory.set).not.toHaveBeenCalled();
179
+ expect((0, _stacked.createStackedCache)([memory.cache, layer('redis').cache]).lookup).toBeDefined();
180
+ }));
181
+ it('returns the hit even when a refill write fails', /*#__PURE__*/_asyncToGenerator(function* () {
182
+ var memory = layer('memory');
183
+ memory.set.mockRejectedValue(new Error('memory full'));
184
+ var redis = layer('redis', {
185
+ key: {
186
+ value: 'value',
187
+ version: 1
188
+ }
189
+ });
190
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
191
+ yield expect(stacked.get('key', 1)).resolves.toBe('value');
192
+ yield flush();
193
+ expect(memory.set).toHaveBeenCalled();
194
+ }));
195
+ it('exposes lookup itself so stacks can nest, and writes through to all layers', /*#__PURE__*/_asyncToGenerator(function* () {
196
+ var _stacked$lookup;
197
+ var memory = layer('memory');
198
+ var redis = layer('redis', {
199
+ key: {
200
+ value: 'value',
201
+ version: 9,
202
+ ttl: 10
203
+ }
204
+ });
205
+ var stacked = (0, _stacked.createStackedCache)([memory.cache, redis.cache]);
206
+ expect(yield (_stacked$lookup = stacked.lookup) === null || _stacked$lookup === void 0 ? void 0 : _stacked$lookup.call(stacked, 'key', 9)).toEqual({
207
+ value: 'value',
208
+ version: 9,
209
+ ttl: 10,
210
+ layer: 'redis'
211
+ });
212
+ yield stacked.set('other', 'written', 3, {
213
+ ttl: 5
214
+ });
215
+ expect(memory.set).toHaveBeenCalledWith('other', 'written', 3, {
216
+ ttl: 5
217
+ });
218
+ expect(redis.set).toHaveBeenCalledWith('other', 'written', 3, {
219
+ ttl: 5
220
+ });
221
+ }));