@fastify/caching 8.0.0 → 8.1.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.
@@ -14,4 +14,5 @@ jobs:
14
14
  test:
15
15
  uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
16
  with:
17
+ license-check: true
17
18
  lint: true
package/.taprc ADDED
@@ -0,0 +1,2 @@
1
+ files:
2
+ - test/**/*.test.js
package/Readme.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
6
6
 
7
7
  *@fastify/caching* is a plugin for the [Fastify](http://fastify.io/) framework
8
- that provides mechanisms for manipulating HTTP cache headers according to
8
+ that provides server-side caching and mechanisms for manipulating HTTP cache headers according to
9
9
  [RFC 2616 §14.9](https://tools.ietf.org/html/rfc2616#section-14.9).
10
10
 
11
11
  Supports Fastify versions ^3.0.0. Version v5.x supports Fastify ^3.0.0 in
@@ -64,7 +64,7 @@ const redis = new IORedis({host: '127.0.0.1'})
64
64
  const abcache = require('abstract-cache')({
65
65
  useAwait: false,
66
66
  driver: {
67
- name: 'abstract-cache-redis', // must be installed via `npm install`
67
+ name: 'abstract-cache-redis', // must be installed via `npm i`
68
68
  options: {client: redis}
69
69
  }
70
70
  })
@@ -53,7 +53,7 @@ function etagOnSend (req, res, payload, next) {
53
53
  )
54
54
  }
55
55
 
56
- function fastifyCachingPlugin (instance, options, next) {
56
+ function fastifyCaching (instance, options, next) {
57
57
  let _options
58
58
  if (Function.prototype.isPrototypeOf(options)) {
59
59
  _options = Object.assign({}, defaultOptions)
@@ -92,10 +92,12 @@ function fastifyCachingPlugin (instance, options, next) {
92
92
  next()
93
93
  }
94
94
 
95
- module.exports = fp(fastifyCachingPlugin, {
96
- fastify: '^4.0.0',
95
+ module.exports = fp(fastifyCaching, {
96
+ fastify: '4.x',
97
97
  name: '@fastify/caching'
98
98
  })
99
+ module.exports.default = fastifyCaching
100
+ module.exports.fastifyCaching = fastifyCaching
99
101
 
100
102
  module.exports.privacy = {
101
103
  NOCACHE: 'no-cache',
package/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "@fastify/caching",
3
- "version": "8.0.0",
3
+ "version": "8.1.0",
4
4
  "description": "A plugin for Fastify to enable management of cache control headers",
5
- "main": "plugin.js",
6
- "types": "plugin.d.ts",
5
+ "main": "index.js",
6
+ "types": "types/index.d.ts",
7
7
  "scripts": {
8
8
  "lint": "standard --verbose | snazzy",
9
- "lint:ci": "standard",
10
- "test": "npm run unit && npm run test:types",
11
- "test:types": "tsd",
12
- "unit": "tap -J \"test/*.test.js\"",
13
- "unit:report": "npm run unit -- --coverage-report=html",
14
- "unit:verbose": "npm run unit -- -Rspec"
9
+ "test": "npm run test:unit && npm run test:typescript",
10
+ "test:typescript": "tsd",
11
+ "test:unit": "tap",
12
+ "test:unit:report": "npm run test:unit -- --coverage-report=html",
13
+ "test:unit:verbose": "npm run test:unit -- -Rspec"
15
14
  },
16
15
  "precommit": [
17
16
  "lint",
@@ -33,23 +32,24 @@
33
32
  },
34
33
  "homepage": "https://github.com/fastify/fastify-caching#readme",
35
34
  "devDependencies": {
36
- "@types/node": "^17.0.21",
37
- "fastify": "^4.0.0-rc.2",
38
- "pre-commit": "^1.2.2",
35
+ "@fastify/pre-commit": "^2.0.2",
36
+ "@types/node": "^18.0.0",
37
+ "fastify": "^4.0.0-rc.3",
39
38
  "snazzy": "^9.0.0",
40
39
  "standard": "^17.0.0",
41
40
  "tap": "^16.0.0",
42
- "tsd": "^0.20.0"
41
+ "tsd": "^0.25.0"
43
42
  },
44
43
  "dependencies": {
45
44
  "abstract-cache": "^1.0.1",
46
- "fastify-plugin": "^3.0.1",
45
+ "fastify-plugin": "^4.0.0",
47
46
  "uid-safe": "^2.1.5"
48
47
  },
49
- "tsd": {
50
- "directory": "test/types"
51
- },
52
48
  "publishConfig": {
53
49
  "access": "public"
54
- }
50
+ },
51
+ "pre-commit": [
52
+ "lint",
53
+ "test"
54
+ ]
55
55
  }
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('tap')
4
- const plugin = require('../plugin')
4
+ const plugin = require('..')
5
5
 
6
6
  const Fastify = require('fastify')
7
7
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  const { test } = require('tap')
4
4
  const Fastify = require('fastify')
5
- const plugin = require('../plugin')
5
+ const plugin = require('..')
6
6
 
7
7
  test('decorators get added', async (t) => {
8
8
  t.plan(1)
@@ -0,0 +1,128 @@
1
+ /// <reference types='node' />
2
+
3
+ import { FastifyPluginCallback } from 'fastify';
4
+
5
+ declare module 'fastify' {
6
+ interface FastifyInstance {
7
+ cache: fastifyCaching.AbstractCacheCompliantObject;
8
+ cacheSegment: string;
9
+ etagMaxLife: number | undefined;
10
+ }
11
+
12
+ interface FastifyReply {
13
+ /**
14
+ * This method allows setting of the `expires` header.
15
+ *
16
+ * @link [reply.expires() documentation](https://github.com/fastify/fastify-caching#replyexpiresdate)
17
+ *
18
+ * @param date A regular `Date` object, or a valid date string according to [RFC 2616 section 14.21](https://datatracker.ietf.org/doc/html/rfc2616#section-14.21).
19
+ */
20
+ expires(date?: Date): this;
21
+
22
+ /**
23
+ * This method allows setting of the `etag` header.
24
+ *
25
+ * @link [reply.etag() documentation](https://github.com/fastify/fastify-caching#replyetagstring-number)
26
+ *
27
+ * @param tag Any arbitrary string that is valid for HTTP headers.
28
+ * @param timeToLive The time must be specified in milliseconds. The default lifetime, when the parameter is not specified, is `3600000`.
29
+ */
30
+ etag(tag?: string, timeToLive?: number): this;
31
+ }
32
+ }
33
+
34
+ type FastifyCaching = FastifyPluginCallback<fastifyCaching.FastifyCachingPluginOptions> & {
35
+ privacy: fastifyCaching.Privacy;
36
+ };
37
+
38
+ declare namespace fastifyCaching {
39
+ /**
40
+ * @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol)
41
+ */
42
+ export interface AbstractCacheCompliantObject {
43
+ get(
44
+ key: string | { id: string; segment: string },
45
+ callback?: (error: unknown, result: unknown) => void
46
+ ): void;
47
+ set(
48
+ key: string | { id: string; segment: string },
49
+ value: unknown,
50
+ timeToLive: number,
51
+ callback?: (error: unknown, result: unknown) => void
52
+ ): void;
53
+ }
54
+
55
+ export interface Privacy {
56
+ NOCACHE: 'no-cache';
57
+ PRIVATE: 'private';
58
+ PUBLIC: 'public';
59
+ }
60
+
61
+ /**
62
+ * @link [`fastify-caching` options documentation](https://github.com/fastify/fastify-caching#options)
63
+ */
64
+ export interface FastifyCachingPluginOptions {
65
+ /**
66
+ * An [abstract-cache](https://www.npmjs.com/package/abstract-cache) protocol compliant cache object.
67
+ * Note: the plugin requires a cache instance to properly support the ETag mechanism.
68
+ * Therefore, if a falsy value is supplied the default will be used.
69
+ *
70
+ * - Default value: `abstract-cache.memclient`
71
+ */
72
+ cache?: AbstractCacheCompliantObject;
73
+
74
+ /**
75
+ * The segment identifier to use when communicating with the cache.
76
+ *
77
+ * - Default value: `fastify-caching`
78
+ */
79
+ cacheSegment?: string;
80
+
81
+ etagMaxLife?: number;
82
+
83
+ /**
84
+ * A value, in seconds, for the max-age the resource may be cached.
85
+ * When this is set, and privacy is not set to no-cache, then ', max-age=<value>'
86
+ * will be appended to the cache-control header.
87
+ *
88
+ * - Default value: `undefined`
89
+ */
90
+ expiresIn?: number;
91
+
92
+ /**
93
+ * It can be set to any string that is valid for a cache-response-directive as
94
+ * defined by [RFC 2616](https://datatracker.ietf.org/doc/html/rfc2616#section-14.9).
95
+ *
96
+ * - Default value: `undefined`
97
+ *
98
+ * @link [MDN Cache-Control - Response Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives)
99
+ *
100
+ * @example
101
+ * const fastifyCaching = require('fastify-caching');
102
+ *
103
+ * // Disabling client side caching of all routes.
104
+ * fastify.register(fastifyCaching, { privacy: fastifyCaching.privacy.NOCACHE });
105
+ */
106
+ privacy?: string;
107
+
108
+ /**
109
+ * A value, in seconds, for the length of time the resource is fresh and may be
110
+ * held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when
111
+ * this is specified, though browsers will continue to use max-age. Should be
112
+ * used with expiresIn, not in place of it. When this is set, and privacy is set
113
+ * to public, then ', s-maxage=<value>' will be appended to the cache-control header.
114
+ *
115
+ * - Default value: `undefined`
116
+ */
117
+ serverExpiresIn?: number;
118
+ }
119
+
120
+ export const privacy: {
121
+ privacy: Privacy;
122
+ };
123
+ export const fastifyCaching: FastifyCaching;
124
+ export { fastifyCaching as default };
125
+ }
126
+
127
+ declare function fastifyCaching(...params: Parameters<FastifyCaching>): ReturnType<FastifyCaching>
128
+ export = fastifyCaching
@@ -3,7 +3,7 @@ import { expectAssignable, expectError, expectType } from 'tsd';
3
3
  import fastifyCaching, {
4
4
  AbstractCacheCompliantObject,
5
5
  FastifyCachingPluginOptions,
6
- } from '../..';
6
+ } from '..';
7
7
 
8
8
  const fastify = Fastify({ logger: true });
9
9
 
package/plugin.d.ts DELETED
@@ -1,124 +0,0 @@
1
- /// <reference types='node' />
2
-
3
- import { FastifyPluginCallback } from 'fastify';
4
-
5
- declare module 'fastify' {
6
- interface FastifyInstance {
7
- cache: AbstractCacheCompliantObject;
8
- cacheSegment: string;
9
- etagMaxLife: number | undefined;
10
- }
11
-
12
- interface FastifyReply {
13
- /**
14
- * This method allows setting of the `expires` header.
15
- *
16
- * @link [reply.expires() documentation](https://github.com/fastify/fastify-caching#replyexpiresdate)
17
- *
18
- * @param date A regular `Date` object, or a valid date string according to [RFC 2616 section 14.21](https://datatracker.ietf.org/doc/html/rfc2616#section-14.21).
19
- */
20
- expires(date?: Date): this;
21
-
22
- /**
23
- * This method allows setting of the `etag` header.
24
- *
25
- * @link [reply.etag() documentation](https://github.com/fastify/fastify-caching#replyetagstring-number)
26
- *
27
- * @param tag Any arbitrary string that is valid for HTTP headers.
28
- * @param timeToLive The time must be specified in milliseconds. The default lifetime, when the parameter is not specified, is `3600000`.
29
- */
30
- etag(tag?: string, timeToLive?: number): this;
31
- }
32
- }
33
-
34
- /**
35
- * @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol)
36
- */
37
- export interface AbstractCacheCompliantObject {
38
- get(
39
- key: string | { id: string; segment: string },
40
- callback?: (error: unknown, result: unknown) => void
41
- ): void;
42
- set(
43
- key: string | { id: string; segment: string },
44
- value: unknown,
45
- timeToLive: number,
46
- callback?: (error: unknown, result: unknown) => void
47
- ): void;
48
- }
49
-
50
- export interface Privacy {
51
- NOCACHE: 'no-cache';
52
- PRIVATE: 'private';
53
- PUBLIC: 'public';
54
- }
55
-
56
- /**
57
- * @link [`fastify-caching` options documentation](https://github.com/fastify/fastify-caching#options)
58
- */
59
- export interface FastifyCachingPluginOptions {
60
- /**
61
- * An [abstract-cache](https://www.npmjs.com/package/abstract-cache) protocol compliant cache object.
62
- * Note: the plugin requires a cache instance to properly support the ETag mechanism.
63
- * Therefore, if a falsy value is supplied the default will be used.
64
- *
65
- * - Default value: `abstract-cache.memclient`
66
- */
67
- cache?: AbstractCacheCompliantObject;
68
-
69
- /**
70
- * The segment identifier to use when communicating with the cache.
71
- *
72
- * - Default value: `fastify-caching`
73
- */
74
- cacheSegment?: string;
75
-
76
- etagMaxLife?: number;
77
-
78
- /**
79
- * A value, in seconds, for the max-age the resource may be cached.
80
- * When this is set, and privacy is not set to no-cache, then ', max-age=<value>'
81
- * will be appended to the cache-control header.
82
- *
83
- * - Default value: `undefined`
84
- */
85
- expiresIn?: number;
86
-
87
- /**
88
- * It can be set to any string that is valid for a cache-response-directive as
89
- * defined by [RFC 2616](https://datatracker.ietf.org/doc/html/rfc2616#section-14.9).
90
- *
91
- * - Default value: `undefined`
92
- *
93
- * @link [MDN Cache-Control - Response Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives)
94
- *
95
- * @example
96
- * const fastifyCaching = require('fastify-caching');
97
- *
98
- * // Disabling client side caching of all routes.
99
- * fastify.register(fastifyCaching, { privacy: fastifyCaching.privacy.NOCACHE });
100
- */
101
- privacy?: string;
102
-
103
- /**
104
- * A value, in seconds, for the length of time the resource is fresh and may be
105
- * held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when
106
- * this is specified, though browsers will continue to use max-age. Should be
107
- * used with expiresIn, not in place of it. When this is set, and privacy is set
108
- * to public, then ', s-maxage=<value>' will be appended to the cache-control header.
109
- *
110
- * - Default value: `undefined`
111
- */
112
- serverExpiresIn?: number;
113
- }
114
-
115
- declare const fastifyCaching: FastifyPluginCallback<FastifyCachingPluginOptions> & {
116
- privacy: Privacy;
117
- };
118
-
119
- declare const privacy: {
120
- privacy: Privacy;
121
- };
122
-
123
- export default fastifyCaching;
124
- export { fastifyCaching, privacy };