Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fastify/fastify-plugin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.2.1
Choose a base ref
...
head repository: fastify/fastify-plugin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.3.0
Choose a head ref
  • 6 commits
  • 7 files changed
  • 4 contributors

Commits on Aug 29, 2022

  1. build(deps-dev): bump tsd from 0.22.0 to 0.23.0 (#194)

    Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.22.0 to 0.23.0.
    - [Release notes](https://github.com/SamVerschueren/tsd/releases)
    - [Commits](tsdjs/tsd@v0.22.0...v0.23.0)
    
    ---
    updated-dependencies:
    - dependency-name: tsd
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Aug 29, 2022
    Copy the full SHA
    ce26e8a View commit details

Commits on Aug 30, 2022

  1. Copy the full SHA
    5f6eee0 View commit details

Commits on Sep 19, 2022

  1. build(deps-dev): bump tsd from 0.23.0 to 0.24.1 (#196)

    Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.23.0 to 0.24.1.
    - [Release notes](https://github.com/SamVerschueren/tsd/releases)
    - [Commits](tsdjs/tsd@v0.23.0...v0.24.1)
    
    ---
    updated-dependencies:
    - dependency-name: tsd
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 19, 2022
    Copy the full SHA
    f0d24a2 View commit details

Commits on Sep 21, 2022

  1. Copy the full SHA
    35a6288 View commit details

Commits on Oct 13, 2022

  1. feat: add option to encapsulate (#199)

    * add option to encapsulate
    
    * don't coerce truthy values for `encapsulate`
    
    * update docs
    
    * Apply suggestions from code review
    
    Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
    
    * add test
    
    * test: more use cases
    
    Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
    dwickern and Eomm authored Oct 13, 2022
    Copy the full SHA
    bdbe525 View commit details
  2. Bumped v4.3.0

    Eomm committed Oct 13, 2022
    Copy the full SHA
    f4e9c85 View commit details
Showing with 178 additions and 19 deletions.
  1. +25 −0 README.md
  2. +3 −3 package.json
  3. +2 −1 plugin.d.ts
  4. +2 −5 plugin.js
  5. +4 −2 plugin.test-d.ts
  6. +142 −0 test/test.js
  7. +0 −8 tsconfig.json
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -96,6 +96,31 @@ module.exports = fp(plugin, {
})
```

#### Encapsulate

By default, `fastify-plugin` breaks the [encapsulation](https://github.com/fastify/fastify/blob/HEAD/docs/Reference/Encapsulation.md) but you can optionally keep the plugin encapsulated.
This allows you to set the plugin's name and validate its dependencies without making the plugin accessible.
```js
const fp = require('fastify-plugin')

function plugin (fastify, opts, next) {
// the decorator is not accessible outside this plugin
fastify.decorate('util', function() {})
next()
}

module.exports = fp(plugin, {
name: 'my-encapsulated-plugin',
fastify: '4.x',
decorators: {
fastify: ['plugin1', 'plugin2'],
reply: ['compress']
},
dependencies: ['plugin1-name', 'plugin2-name'],
encapsulate: true
})
```

#### Bundlers and Typescript
`fastify-plugin` adds a `.default` and `[name]` property to the passed in function.
The type definition would have to be updated to leverage this.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-plugin",
"version": "4.2.1",
"version": "4.3.0",
"description": "Plugin helper for Fastify",
"main": "plugin.js",
"types": "plugin.d.ts",
@@ -29,9 +29,9 @@
"@types/node": "^18.0.0",
"fastify": "^4.0.1",
"proxyquire": "^2.1.3",
"semver": "^7.3.7",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.22.0",
"typescript": "^4.0.5"
"tsd": "^0.24.1"
}
}
3 changes: 2 additions & 1 deletion plugin.d.ts
Original file line number Diff line number Diff line change
@@ -65,7 +65,8 @@ export interface PluginMetadata {
request?: (string | symbol)[]
},
/** The plugin dependencies */
dependencies?: string[]
dependencies?: string[],
encapsulate?: boolean
}

// Exporting PluginOptions for backward compatibility after renaming it to PluginMetadata
7 changes: 2 additions & 5 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -18,10 +18,6 @@ function plugin (fn, options = {}) {
)
}

fn[Symbol.for('skip-override')] = true

const pluginName = (options && options.name) || checkName(fn)

if (typeof options === 'string') {
options = {
fastify: options
@@ -42,9 +38,10 @@ function plugin (fn, options = {}) {

if (!options.name) {
autoName = true
options.name = pluginName + '-auto-' + count++
options.name = checkName(fn) + '-auto-' + count++
}

fn[Symbol.for('skip-override')] = options.encapsulate !== true
fn[Symbol.for('fastify.display-name')] = options.name
fn[Symbol.for('plugin-meta')] = options

6 changes: 4 additions & 2 deletions plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -29,7 +29,8 @@ expectAssignable<FastifyPluginCallback>(fp(pluginCallback, {
reply: [ '', testSymbol ],
request: [ '', testSymbol ]
},
dependencies: [ '' ]
dependencies: [ '' ],
encapsulate: true
}))

const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, options, next) => {
@@ -66,7 +67,8 @@ expectAssignable<FastifyPluginAsync>(fp(pluginAsync, {
reply: [ '', testSymbol ],
request: [ '', testSymbol ]
},
dependencies: [ '' ]
dependencies: [ '' ],
encapsulate: true
}))

const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, options) => {
142 changes: 142 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -237,3 +237,145 @@ test('should check fastify dependency graph - decorateReply', t => {
t.equal(err.message, "The decorator 'plugin2' required by 'test' is not present in Reply")
})
})

test('should accept an option to encapsulate', t => {
t.plan(4)
const fastify = Fastify()

fastify.register(fp((fastify, opts, next) => {
fastify.decorate('accessible', true)
next()
}, {
name: 'accessible-plugin'
}))

fastify.register(fp((fastify, opts, next) => {
fastify.decorate('alsoAccessible', true)
next()
}, {
name: 'accessible-plugin2',
encapsulate: false
}))

fastify.register(fp((fastify, opts, next) => {
fastify.decorate('encapsulated', true)
next()
}, {
name: 'encapsulated-plugin',
encapsulate: true
}))

fastify.ready(err => {
t.error(err)
t.ok(fastify.hasDecorator('accessible'))
t.ok(fastify.hasDecorator('alsoAccessible'))
t.notOk(fastify.hasDecorator('encapsulated'))
})
})

test('should check dependencies when encapsulated', t => {
t.plan(1)
const fastify = Fastify()

fastify.register(fp((fastify, opts, next) => next(), {
name: 'test',
dependencies: ['missing-dependency-name'],
encapsulate: true
}))

fastify.ready(err => {
t.equal(err.message, "The dependency 'missing-dependency-name' of plugin 'test' is not registered")
})
})

test('should check version when encapsulated', t => {
t.plan(1)
const fastify = Fastify()

fastify.register(fp((fastify, opts, next) => next(), {
name: 'test',
fastify: '<=2.10.0',
encapsulate: true
}))

fastify.ready(err => {
t.match(err.message, /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d.\d' is installed/)
})
})

test('should check decorators when encapsulated', t => {
t.plan(1)
const fastify = Fastify()

fastify.decorate('plugin1', 'foo')

fastify.register(fp((fastify, opts, next) => next(), {
fastify: '4.x',
name: 'test',
encapsulate: true,
decorators: { fastify: ['plugin1', 'plugin2'] }
}))

fastify.ready(err => {
t.equal(err.message, "The decorator 'plugin2' required by 'test' is not present in Fastify")
})
})

test('plugin name when encapsulated', async t => {
const fastify = Fastify()

fastify.register(function plugin (instance, opts, next) {
next()
})

fastify.register(fp(getFn('hello'), {
fastify: '4.x',
name: 'hello',
encapsulate: true
}))

fastify.register(function plugin (fastify, opts, next) {
fastify.register(fp(getFn('deep'), {
fastify: '4.x',
name: 'deep',
encapsulate: true
}))

fastify.register(fp(function genericPlugin (fastify, opts, next) {
t.equal(fastify.pluginName, 'deep-deep', 'should be deep-deep')

fastify.register(fp(getFn('deep-deep-deep'), {
fastify: '4.x',
name: 'deep-deep-deep',
encapsulate: true
}))

fastify.register(fp(getFn('deep-deep -> not-encapsulated-2'), {
fastify: '4.x',
name: 'not-encapsulated-2'
}))

next()
}, {
fastify: '4.x',
name: 'deep-deep',
encapsulate: true
}))

fastify.register(fp(getFn('plugin -> not-encapsulated'), {
fastify: '4.x',
name: 'not-encapsulated'
}))

next()
})

await fastify.ready()

function getFn (expectedName) {
return function genericPlugin (fastify, opts, next) {
t.equal(fastify.pluginName, expectedName, `should be ${expectedName}`)
next()
}
}
})
8 changes: 0 additions & 8 deletions tsconfig.json

This file was deleted.