@merkur/plugin-http-cache 0.38.0 → 0.39.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
2
  <a href="https://merkur.js.org/docs/getting-started" title="Getting started">
3
- <img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
3
+ <img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-logo.png" width="100px" height="100px" alt="Merkur illustration"/>
4
4
  </a>
5
5
  </p>
6
6
 
package/lib/index.cjs CHANGED
@@ -48,21 +48,21 @@ class CacheEntry {
48
48
  serialize() {
49
49
  return {
50
50
  value: this.value,
51
- ttl: this._ttl === Infinity ? 'Infinity' : this._ttl,
51
+ ttl: this._ttl === Infinity ? 'Infinity' : this._ttl
52
52
  };
53
53
  }
54
54
  }
55
55
 
56
56
  const DEV = 'development';
57
- const ENV =
58
- typeof process !== 'undefined' && process && process.env
59
- ? process.env.NODE_ENV
60
- : DEV;
61
-
62
- function getCacheKey({ method, url, body, query }) {
57
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
58
+ function getCacheKey({
59
+ method,
60
+ url,
61
+ body,
62
+ query
63
+ }) {
63
64
  const data = ~['GET', 'HEAD'].indexOf(method) ? query : body;
64
65
  let dataQuery = '';
65
-
66
66
  if (data) {
67
67
  try {
68
68
  dataQuery = JSON.stringify(data).replace(/<\/script/gi, '<\\/script');
@@ -72,133 +72,121 @@ function getCacheKey({ method, url, body, query }) {
72
72
  }
73
73
  return `${method}:${url}?${dataQuery}`;
74
74
  }
75
-
76
75
  function httpCachePlugin() {
77
76
  return {
78
77
  async setup(widget) {
79
78
  if (ENV === DEV && !widget.$in.httpClient) {
80
- throw new Error(
81
- 'You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache',
82
- );
79
+ throw new Error('You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache');
83
80
  }
84
-
85
81
  core.assignMissingKeys(widget, httpCacheAPI());
86
-
87
82
  widget.$in.httpClient.cache = new Map();
88
-
89
83
  return widget;
90
84
  },
91
85
  async create(widget) {
92
- const { useCache, transferServerCache, ttl } =
93
- widget.$in.httpClient.defaultConfig;
94
-
86
+ const {
87
+ useCache,
88
+ transferServerCache,
89
+ ttl
90
+ } = widget.$in.httpClient.defaultConfig;
95
91
  core.bindWidgetToFunctions(widget, widget.httpCache);
96
-
97
92
  pluginHttpClient.setDefaultConfig(widget, {
98
93
  useCache: useCache ?? true,
99
94
  transferServerCache: transferServerCache ?? false,
100
- ttl: ttl ?? 60000,
95
+ ttl: ttl ?? 60000
101
96
  });
102
-
103
97
  if (transferServerCache) {
104
98
  core.hookMethod(widget, 'info', infoHook);
105
99
  if (widget.$httpCache) {
106
100
  widget.httpCache.deserialize(widget.$httpCache);
107
101
  }
108
102
  }
109
-
110
103
  return widget;
111
- },
104
+ }
112
105
  };
113
106
  }
114
-
115
107
  function cacheInTransformer() {
116
108
  return {
117
109
  async transformResponse(widget, request, response) {
118
110
  if (request.useCache && !response.cached) {
119
- const { cache } = widget.$in.httpClient;
111
+ const {
112
+ cache
113
+ } = widget.$in.httpClient;
120
114
  const cacheEntry = new CacheEntry(pluginHttpClient.copyResponse(response), request.ttl);
121
115
  cache.set(getCacheKey(request), cacheEntry);
122
116
  }
123
-
124
117
  return [request, response];
125
- },
118
+ }
126
119
  };
127
120
  }
128
-
129
121
  function cacheOutTransformer() {
130
122
  return {
131
123
  async transformRequest(widget, request, response) {
132
124
  if (request.useCache) {
133
125
  const cachedResponse = widget.httpCache.get(getCacheKey(request));
134
-
135
126
  if (cachedResponse) {
136
- return [request, { ...cachedResponse, cached: true }];
127
+ return [request, {
128
+ ...cachedResponse,
129
+ cached: true
130
+ }];
137
131
  }
138
132
  }
139
-
140
133
  return [request, response];
141
- },
134
+ }
142
135
  };
143
136
  }
144
-
145
137
  function httpCacheAPI() {
146
138
  return {
147
139
  httpCache: {
148
140
  get(widget, key) {
149
- const { cache } = widget.$in.httpClient;
141
+ const {
142
+ cache
143
+ } = widget.$in.httpClient;
150
144
  const cacheEntry = cache.get(key);
151
-
152
- return cacheEntry && !cacheEntry.isExpired()
153
- ? pluginHttpClient.copyResponse(cacheEntry.value)
154
- : null;
145
+ return cacheEntry && !cacheEntry.isExpired() ? pluginHttpClient.copyResponse(cacheEntry.value) : null;
155
146
  },
156
147
  serialize(widget) {
157
- const { cache } = widget.$in.httpClient;
148
+ const {
149
+ cache
150
+ } = widget.$in.httpClient;
158
151
  const serializedData = {};
159
-
160
152
  for (const key of cache.keys()) {
161
153
  const entry = cache.get(key);
162
-
163
154
  if (entry instanceof CacheEntry) {
164
155
  serializedData[key] = entry.serialize();
165
156
  }
166
157
  }
167
-
168
- return JSON.stringify(serializedData).replace(
169
- /<\/script/gi,
170
- '<\\/script',
171
- );
158
+ return JSON.stringify(serializedData).replace(/<\/script/gi, '<\\/script');
172
159
  },
173
-
174
160
  deserialize(widget, serializedData) {
175
- const { cache } = widget.$in.httpClient;
161
+ const {
162
+ cache
163
+ } = widget.$in.httpClient;
176
164
  let parsedData = {};
177
-
178
165
  try {
179
166
  parsedData = JSON.parse(serializedData);
180
167
  } catch (error) {
181
168
  console.warn('Failed to parse http cache data', error);
182
169
  }
183
-
184
170
  for (const key of Object.keys(parsedData)) {
185
- let { value, ttl } = parsedData[key];
186
-
171
+ let {
172
+ value,
173
+ ttl
174
+ } = parsedData[key];
187
175
  if (ttl === 'Infinity') {
188
176
  ttl = Infinity;
189
177
  }
190
-
191
178
  cache.set(key, new CacheEntry(value, ttl));
192
179
  }
193
- },
194
- },
180
+ }
181
+ }
195
182
  };
196
183
  }
197
-
198
184
  async function infoHook(widget, originalInfoFn, ...rest) {
199
185
  const originalInfo = await originalInfoFn(...rest);
200
-
201
- return { ...originalInfo, $httpCache: widget.httpCache.serialize() };
186
+ return {
187
+ ...originalInfo,
188
+ $httpCache: widget.httpCache.serialize()
189
+ };
202
190
  }
203
191
 
204
192
  exports.cacheInTransformer = cacheInTransformer;
package/lib/index.js CHANGED
@@ -48,21 +48,21 @@ class CacheEntry {
48
48
  serialize() {
49
49
  return {
50
50
  value: this.value,
51
- ttl: this._ttl === Infinity ? 'Infinity' : this._ttl,
51
+ ttl: this._ttl === Infinity ? 'Infinity' : this._ttl
52
52
  };
53
53
  }
54
54
  }
55
55
 
56
56
  const DEV = 'development';
57
- const ENV =
58
- typeof process !== 'undefined' && process && process.env
59
- ? process.env.NODE_ENV
60
- : DEV;
61
-
62
- function getCacheKey({ method, url, body, query }) {
57
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
58
+ function getCacheKey({
59
+ method,
60
+ url,
61
+ body,
62
+ query
63
+ }) {
63
64
  const data = ~['GET', 'HEAD'].indexOf(method) ? query : body;
64
65
  let dataQuery = '';
65
-
66
66
  if (data) {
67
67
  try {
68
68
  dataQuery = JSON.stringify(data).replace(/<\/script/gi, '<\\/script');
@@ -72,133 +72,121 @@ function getCacheKey({ method, url, body, query }) {
72
72
  }
73
73
  return `${method}:${url}?${dataQuery}`;
74
74
  }
75
-
76
75
  function httpCachePlugin() {
77
76
  return {
78
77
  async setup(widget) {
79
78
  if (ENV === DEV && !widget.$in.httpClient) {
80
- throw new Error(
81
- 'You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache',
82
- );
79
+ throw new Error('You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache');
83
80
  }
84
-
85
81
  core.assignMissingKeys(widget, httpCacheAPI());
86
-
87
82
  widget.$in.httpClient.cache = new Map();
88
-
89
83
  return widget;
90
84
  },
91
85
  async create(widget) {
92
- const { useCache, transferServerCache, ttl } =
93
- widget.$in.httpClient.defaultConfig;
94
-
86
+ const {
87
+ useCache,
88
+ transferServerCache,
89
+ ttl
90
+ } = widget.$in.httpClient.defaultConfig;
95
91
  core.bindWidgetToFunctions(widget, widget.httpCache);
96
-
97
92
  pluginHttpClient.setDefaultConfig(widget, {
98
93
  useCache: useCache ?? true,
99
94
  transferServerCache: transferServerCache ?? false,
100
- ttl: ttl ?? 60000,
95
+ ttl: ttl ?? 60000
101
96
  });
102
-
103
97
  if (transferServerCache) {
104
98
  core.hookMethod(widget, 'info', infoHook);
105
99
  if (widget.$httpCache) {
106
100
  widget.httpCache.deserialize(widget.$httpCache);
107
101
  }
108
102
  }
109
-
110
103
  return widget;
111
- },
104
+ }
112
105
  };
113
106
  }
114
-
115
107
  function cacheInTransformer() {
116
108
  return {
117
109
  async transformResponse(widget, request, response) {
118
110
  if (request.useCache && !response.cached) {
119
- const { cache } = widget.$in.httpClient;
111
+ const {
112
+ cache
113
+ } = widget.$in.httpClient;
120
114
  const cacheEntry = new CacheEntry(pluginHttpClient.copyResponse(response), request.ttl);
121
115
  cache.set(getCacheKey(request), cacheEntry);
122
116
  }
123
-
124
117
  return [request, response];
125
- },
118
+ }
126
119
  };
127
120
  }
128
-
129
121
  function cacheOutTransformer() {
130
122
  return {
131
123
  async transformRequest(widget, request, response) {
132
124
  if (request.useCache) {
133
125
  const cachedResponse = widget.httpCache.get(getCacheKey(request));
134
-
135
126
  if (cachedResponse) {
136
- return [request, { ...cachedResponse, cached: true }];
127
+ return [request, {
128
+ ...cachedResponse,
129
+ cached: true
130
+ }];
137
131
  }
138
132
  }
139
-
140
133
  return [request, response];
141
- },
134
+ }
142
135
  };
143
136
  }
144
-
145
137
  function httpCacheAPI() {
146
138
  return {
147
139
  httpCache: {
148
140
  get(widget, key) {
149
- const { cache } = widget.$in.httpClient;
141
+ const {
142
+ cache
143
+ } = widget.$in.httpClient;
150
144
  const cacheEntry = cache.get(key);
151
-
152
- return cacheEntry && !cacheEntry.isExpired()
153
- ? pluginHttpClient.copyResponse(cacheEntry.value)
154
- : null;
145
+ return cacheEntry && !cacheEntry.isExpired() ? pluginHttpClient.copyResponse(cacheEntry.value) : null;
155
146
  },
156
147
  serialize(widget) {
157
- const { cache } = widget.$in.httpClient;
148
+ const {
149
+ cache
150
+ } = widget.$in.httpClient;
158
151
  const serializedData = {};
159
-
160
152
  for (const key of cache.keys()) {
161
153
  const entry = cache.get(key);
162
-
163
154
  if (entry instanceof CacheEntry) {
164
155
  serializedData[key] = entry.serialize();
165
156
  }
166
157
  }
167
-
168
- return JSON.stringify(serializedData).replace(
169
- /<\/script/gi,
170
- '<\\/script',
171
- );
158
+ return JSON.stringify(serializedData).replace(/<\/script/gi, '<\\/script');
172
159
  },
173
-
174
160
  deserialize(widget, serializedData) {
175
- const { cache } = widget.$in.httpClient;
161
+ const {
162
+ cache
163
+ } = widget.$in.httpClient;
176
164
  let parsedData = {};
177
-
178
165
  try {
179
166
  parsedData = JSON.parse(serializedData);
180
167
  } catch (error) {
181
168
  console.warn('Failed to parse http cache data', error);
182
169
  }
183
-
184
170
  for (const key of Object.keys(parsedData)) {
185
- let { value, ttl } = parsedData[key];
186
-
171
+ let {
172
+ value,
173
+ ttl
174
+ } = parsedData[key];
187
175
  if (ttl === 'Infinity') {
188
176
  ttl = Infinity;
189
177
  }
190
-
191
178
  cache.set(key, new CacheEntry(value, ttl));
192
179
  }
193
- },
194
- },
180
+ }
181
+ }
195
182
  };
196
183
  }
197
-
198
184
  async function infoHook(widget, originalInfoFn, ...rest) {
199
185
  const originalInfo = await originalInfoFn(...rest);
200
-
201
- return { ...originalInfo, $httpCache: widget.httpCache.serialize() };
186
+ return {
187
+ ...originalInfo,
188
+ $httpCache: widget.httpCache.serialize()
189
+ };
202
190
  }
203
191
 
204
192
  exports.cacheInTransformer = cacheInTransformer;
package/lib/index.mjs CHANGED
@@ -46,21 +46,21 @@ class CacheEntry {
46
46
  serialize() {
47
47
  return {
48
48
  value: this.value,
49
- ttl: this._ttl === Infinity ? 'Infinity' : this._ttl,
49
+ ttl: this._ttl === Infinity ? 'Infinity' : this._ttl
50
50
  };
51
51
  }
52
52
  }
53
53
 
54
54
  const DEV = 'development';
55
- const ENV =
56
- typeof process !== 'undefined' && process && process.env
57
- ? process.env.NODE_ENV
58
- : DEV;
59
-
60
- function getCacheKey({ method, url, body, query }) {
55
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
56
+ function getCacheKey({
57
+ method,
58
+ url,
59
+ body,
60
+ query
61
+ }) {
61
62
  const data = ~['GET', 'HEAD'].indexOf(method) ? query : body;
62
63
  let dataQuery = '';
63
-
64
64
  if (data) {
65
65
  try {
66
66
  dataQuery = JSON.stringify(data).replace(/<\/script/gi, '<\\/script');
@@ -70,133 +70,121 @@ function getCacheKey({ method, url, body, query }) {
70
70
  }
71
71
  return `${method}:${url}?${dataQuery}`;
72
72
  }
73
-
74
73
  function httpCachePlugin() {
75
74
  return {
76
75
  async setup(widget) {
77
76
  if (ENV === DEV && !widget.$in.httpClient) {
78
- throw new Error(
79
- 'You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache',
80
- );
77
+ throw new Error('You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache');
81
78
  }
82
-
83
79
  assignMissingKeys(widget, httpCacheAPI());
84
-
85
80
  widget.$in.httpClient.cache = new Map();
86
-
87
81
  return widget;
88
82
  },
89
83
  async create(widget) {
90
- const { useCache, transferServerCache, ttl } =
91
- widget.$in.httpClient.defaultConfig;
92
-
84
+ const {
85
+ useCache,
86
+ transferServerCache,
87
+ ttl
88
+ } = widget.$in.httpClient.defaultConfig;
93
89
  bindWidgetToFunctions(widget, widget.httpCache);
94
-
95
90
  setDefaultConfig(widget, {
96
91
  useCache: useCache ?? true,
97
92
  transferServerCache: transferServerCache ?? false,
98
- ttl: ttl ?? 60000,
93
+ ttl: ttl ?? 60000
99
94
  });
100
-
101
95
  if (transferServerCache) {
102
96
  hookMethod(widget, 'info', infoHook);
103
97
  if (widget.$httpCache) {
104
98
  widget.httpCache.deserialize(widget.$httpCache);
105
99
  }
106
100
  }
107
-
108
101
  return widget;
109
- },
102
+ }
110
103
  };
111
104
  }
112
-
113
105
  function cacheInTransformer() {
114
106
  return {
115
107
  async transformResponse(widget, request, response) {
116
108
  if (request.useCache && !response.cached) {
117
- const { cache } = widget.$in.httpClient;
109
+ const {
110
+ cache
111
+ } = widget.$in.httpClient;
118
112
  const cacheEntry = new CacheEntry(copyResponse(response), request.ttl);
119
113
  cache.set(getCacheKey(request), cacheEntry);
120
114
  }
121
-
122
115
  return [request, response];
123
- },
116
+ }
124
117
  };
125
118
  }
126
-
127
119
  function cacheOutTransformer() {
128
120
  return {
129
121
  async transformRequest(widget, request, response) {
130
122
  if (request.useCache) {
131
123
  const cachedResponse = widget.httpCache.get(getCacheKey(request));
132
-
133
124
  if (cachedResponse) {
134
- return [request, { ...cachedResponse, cached: true }];
125
+ return [request, {
126
+ ...cachedResponse,
127
+ cached: true
128
+ }];
135
129
  }
136
130
  }
137
-
138
131
  return [request, response];
139
- },
132
+ }
140
133
  };
141
134
  }
142
-
143
135
  function httpCacheAPI() {
144
136
  return {
145
137
  httpCache: {
146
138
  get(widget, key) {
147
- const { cache } = widget.$in.httpClient;
139
+ const {
140
+ cache
141
+ } = widget.$in.httpClient;
148
142
  const cacheEntry = cache.get(key);
149
-
150
- return cacheEntry && !cacheEntry.isExpired()
151
- ? copyResponse(cacheEntry.value)
152
- : null;
143
+ return cacheEntry && !cacheEntry.isExpired() ? copyResponse(cacheEntry.value) : null;
153
144
  },
154
145
  serialize(widget) {
155
- const { cache } = widget.$in.httpClient;
146
+ const {
147
+ cache
148
+ } = widget.$in.httpClient;
156
149
  const serializedData = {};
157
-
158
150
  for (const key of cache.keys()) {
159
151
  const entry = cache.get(key);
160
-
161
152
  if (entry instanceof CacheEntry) {
162
153
  serializedData[key] = entry.serialize();
163
154
  }
164
155
  }
165
-
166
- return JSON.stringify(serializedData).replace(
167
- /<\/script/gi,
168
- '<\\/script',
169
- );
156
+ return JSON.stringify(serializedData).replace(/<\/script/gi, '<\\/script');
170
157
  },
171
-
172
158
  deserialize(widget, serializedData) {
173
- const { cache } = widget.$in.httpClient;
159
+ const {
160
+ cache
161
+ } = widget.$in.httpClient;
174
162
  let parsedData = {};
175
-
176
163
  try {
177
164
  parsedData = JSON.parse(serializedData);
178
165
  } catch (error) {
179
166
  console.warn('Failed to parse http cache data', error);
180
167
  }
181
-
182
168
  for (const key of Object.keys(parsedData)) {
183
- let { value, ttl } = parsedData[key];
184
-
169
+ let {
170
+ value,
171
+ ttl
172
+ } = parsedData[key];
185
173
  if (ttl === 'Infinity') {
186
174
  ttl = Infinity;
187
175
  }
188
-
189
176
  cache.set(key, new CacheEntry(value, ttl));
190
177
  }
191
- },
192
- },
178
+ }
179
+ }
193
180
  };
194
181
  }
195
-
196
182
  async function infoHook(widget, originalInfoFn, ...rest) {
197
183
  const originalInfo = await originalInfoFn(...rest);
198
-
199
- return { ...originalInfo, $httpCache: widget.httpCache.serialize() };
184
+ return {
185
+ ...originalInfo,
186
+ $httpCache: widget.httpCache.serialize()
187
+ };
200
188
  }
201
189
 
202
190
  export { cacheInTransformer, cacheOutTransformer, getCacheKey, httpCachePlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merkur/plugin-http-cache",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Merkur plugin for maintaining http requests cache.",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -24,8 +24,7 @@
24
24
  "preversion": "npm test",
25
25
  "test": "jest --no-watchman -c ./jest.config.js",
26
26
  "test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
27
- "build": "rollup -c rollup.config.mjs",
28
- "prepare": "npm run build"
27
+ "build": "rollup -c rollup.config.mjs"
29
28
  },
30
29
  "repository": {
31
30
  "type": "git",
@@ -48,13 +47,13 @@
48
47
  },
49
48
  "homepage": "https://merkur.js.org/",
50
49
  "devDependencies": {
51
- "@merkur/core": "^0.38.0",
52
- "@merkur/plugin-component": "^0.38.0",
53
- "@merkur/plugin-http-client": "^0.38.0"
50
+ "@merkur/core": "^0.39.0",
51
+ "@merkur/plugin-component": "^0.39.0",
52
+ "@merkur/plugin-http-client": "^0.39.0"
54
53
  },
55
54
  "peerDependencies": {
56
55
  "@merkur/core": "*",
57
56
  "@merkur/plugin-http-client": "*"
58
57
  },
59
- "gitHead": "a6e379c0cb887898e34465dc3db9231feb68e6a5"
58
+ "gitHead": "8ad2c8b26246850ce6289502a8b05e882f80ce31"
60
59
  }