Skip to content

Commit

Permalink
have fp use meta name option instead of new symbol (#44)
Browse files Browse the repository at this point in the history
* have fp use meta name option instead of new symbol

* add back fastify.display-name support

* always set fastify.display-name

* simplify code
  • Loading branch information
Ethan-Arrowood authored and mcollina committed Jul 1, 2018
1 parent 2f0cf76 commit 60832a3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 14 deletions.
15 changes: 8 additions & 7 deletions index.js
Expand Up @@ -3,30 +3,31 @@
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)
}

fn[Symbol.for('fastify.display-name')] = options.name

if (options.fastify) {
checkVersion(options.fastify)
delete options.fastify
Expand Down
3 changes: 2 additions & 1 deletion test/composite.test.js
Expand Up @@ -5,11 +5,12 @@ const test = t.test
const fp = require('./../')

test('anonymous function should be named composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
})
3 changes: 2 additions & 1 deletion test/composite.test.ts
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named composite.test', (t: any) => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
})
3 changes: 2 additions & 1 deletion test/composite.ts
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named composite.test', (t: any) => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite')
t.is(fn[Symbol.for('fastify.display-name')], 'composite')
})
3 changes: 2 additions & 1 deletion test/mu1tip1e.composite.test.js
Expand Up @@ -5,11 +5,12 @@ const test = t.test
const fp = require('./../')

test('anonymous function should be named mu1tip1e.composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
})
3 changes: 2 additions & 1 deletion test/mu1tip1e.composite.test.ts
Expand Up @@ -5,11 +5,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named mu1tip1e.composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
})
16 changes: 15 additions & 1 deletion test/test.js
Expand Up @@ -172,11 +172,25 @@ test('should throw if the fastify version does not satisfies the plugin requeste
})

test('should set anonymous function name to file it was called from', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

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

test('should set display-name to meta name', t => {
t.plan(2)

const functionName = 'superDuperSpecialFunction'

const fn = fp((fastify, opts, next) => next(), {
name: functionName
})

t.is(fn[Symbol.for('plugin-meta')].name, functionName)
t.is(fn[Symbol.for('fastify.display-name')], functionName)
})
3 changes: 2 additions & 1 deletion test/test.ts
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('should set anonymous function name to file it was called from', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

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

0 comments on commit 60832a3

Please sign in to comment.