Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have fp use meta name option instead of new symbol #44

Merged
merged 4 commits into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
const semver = require('semver')
const console = require('console')

const DISPLAY_NAME_SYMBOL = Symbol.for('fastify.display-name')
const fpStackTracePattern = new RegExp('at\\s{1}(?:.*\\.)?plugin\\s{1}.*\\n\\s*(.*)')
const fileNamePattern = new RegExp('(?:\\/|\\\\)(\\w*(\\.\\w*)*)\\..*')

function plugin (fn, options) {
function plugin (fn, options = {}) {
if (typeof fn !== 'function') {
throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof fn}'`)
}

fn[DISPLAY_NAME_SYMBOL] = checkName(fn)

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

if (options === undefined) return fn

if (typeof options === 'string') {
checkVersion(options)
return fn
options = {}
}

if (typeof options !== 'object' || Array.isArray(options) || options === null) {
throw new TypeError('The options object should be an object')
}

if (!options.name) {
options.name = checkName(fn)
}

if (options.fastify) {
checkVersion(options.fastify)
delete options.fastify
Expand Down
2 changes: 1 addition & 1 deletion test/composite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ test('anonymous function should be named composite.test', t => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep fastify.display-name as well? Otherwise it will be a breaking change, and I would not bump the major for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll keep it in even though it is redundant. Maybe we can make deprecation note for whenever fastify-plugin v2?

t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this fastify.plugin-meta?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the symbol is changed, this line in the fastify repo would also need to change, so changing the symbol could be considered a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be considered a breaking change as I'd have to update fasitfy code as well? I'll push a commit asap 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, forget about it!

})
2 changes: 1 addition & 1 deletion test/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test('anonymous function should be named composite.test', (t: any) => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
})
2 changes: 1 addition & 1 deletion test/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test('anonymous function should be named composite.test', (t: any) => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'composite')
t.is(fn[Symbol.for('plugin-meta')].name, 'composite')
})
2 changes: 1 addition & 1 deletion test/mu1tip1e.composite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ test('anonymous function should be named mu1tip1e.composite.test', t => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
})
2 changes: 1 addition & 1 deletion test/mu1tip1e.composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ test('anonymous function should be named mu1tip1e.composite.test', t => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
})
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@ test('should set anonymous function name to file it was called from', t => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'test')
t.is(fn[Symbol.for('plugin-meta')].name, 'test')
})
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test('should set anonymous function name to file it was called from', t => {
next()
})

t.is(fn[Symbol.for('fastify.display-name')], 'test')
t.is(fn[Symbol.for('plugin-meta')].name, 'test')
})