1 | # Common JS utils
|
2 |
|
3 | ## Migration guide
|
4 | ### 4.4.2
|
5 |
|
6 | Prometheus middleware `getHttpRequestMetricsMiddleware` is now deprecated, you should remove it, same metrics can be aggregated from `getRequestDurationMetricsMiddleware` alone
|
7 |
|
8 | ### 5.2.0
|
9 |
|
10 | Added loggly adapter. To use loggly you have to set `silent:false` and loggly token via `LOGGLY_TOKEN` env or directly in the config.
|
11 |
|
12 | ### 7.1.0
|
13 |
|
14 | - Added default timeouts for getMongoCachedJSONFetcher (2000ms for fetch execution, 20000ms for background request)
|
15 | - parallel call of the fetcher will not cause parallel requests to database nor the URL
|
16 |
|
17 | ### 8.0
|
18 |
|
19 | logger.module returns pure Winston child instance, rename logger calls:
|
20 | log.e -> log.error
|
21 | log.w -> log.warn
|
22 | log.i -> log.info
|
23 |
|
24 | ### 9.0
|
25 |
|
26 | getMongoCacheFetcher is now async
|
27 | mongoCachedFetcher
|
28 | - supports remote ETag
|
29 | - expose ifNoneMatch
|
30 | - returns result object instead of direct file content
|
31 |
|
32 | ### 10.0
|
33 |
|
34 | concurrentTask accepts options object as the second parameter allowing configure: noLaterThan, startAttemptsDelay
|
35 |
|
36 |
|
37 | ## MongoCachedFetcher
|
38 |
|
39 | ### Usage:
|
40 | ```javascript
|
41 | const collection = mongodb.collection('myCachedFiles');
|
42 |
|
43 | const fetcher = await getMongoCachedJSOFetcher(collection, /* optional */ {
|
44 | url: 'https://my.files.com/file1',
|
45 | cacheLifetime: 60 * 1000, // 60 seconds
|
46 | fetchOptions: { headers: { Authorization: 'myToken' } }, // options for remote fetch
|
47 | transform: async (content, key) => content, // allows decorate the fetched content just before its storage
|
48 | ensureIndexes: true, // it allow's more optimal cache manipulation
|
49 | logError: (err) => console.error(err)
|
50 | });
|
51 |
|
52 | /* optional parameters */
|
53 | const parameters = {
|
54 | url: 'https://my.files.com/file2', // url of json content
|
55 | key: 'file2', // key, under which will be the content cached, url is used by default
|
56 | metaOnly: false, // boolean, if truthy the content is not returned, useful for finding cache freshness
|
57 | ifNoneMatch: 'someOldEtag', // saying, we want to get content only if the current etag is not equal to the value
|
58 | };
|
59 |
|
60 | const {
|
61 | content, // file content, null in case of etagMatch=true
|
62 | isCacheFresh, // boolean saying the content is not after its lifetime
|
63 | etag, // entity tag (version). If not null, it can be used in future fetcher calls as ifNoneMatch parameter
|
64 | etagMatch // boolean, truthy if isNoneMatch parameter provided and corresponds with latest cached etag value
|
65 | } = await fetcher(parameters /* optional */ );
|
66 | ```
|
67 |
|
68 | ### etag & ifNoneMatch
|
69 | MongoCachedFetcher automatically stores ```ETag``` of remote resource if it is present in response from remote source.
|
70 | The stored etag is then used for consequent cache-refresh http call to optimise traffic - no data are transferred
|
71 | when the data didn't change. This functionality assumes the remote source of JSON data supports ```If-None-Match``` request header
|
72 | and ```ETag``` response header.
|
73 |
|
74 | On top of that, the fetcher accepts optional ```ifNoneMatch``` parameter. If it is used, and its value matches currently stored (refreshed) etag value,
|
75 | result object will not contain ```content``` and the ```etagMatch``` will be ```true```.
|
76 |
|
\ | No newline at end of file |