{"size": 693, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "poppinlp/fastify-referrer-policy", "max_stars_repo_stars_event_min_datetime": "2018-09-25T09:53:02.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-29T23:32:58.000Z", "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "poppinlp/fastify-referrer-policy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "poppinlp/fastify-referrer-policy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.65625, "max_line_length": 78, "alphanum_fraction": 0.658008658}
const fp = require('fastify-plugin');

const referrerPolicy = (app, opts, next) => {
	const DEFAULT_POLICY = 'no-referrer';
	const ALLOWED_POLICIES = new Set([
		'no-referrer',
		'no-referrer-when-downgrade',
		'same-origin',
		'origin',
		'strict-origin',
		'origin-when-cross-origin',
		'strict-origin-when-cross-origin',
		'unsafe-url',
		''
	]);
	const policy = opts.policy !== undefined && ALLOWED_POLICIES.has(opts.policy)
		? opts.policy
		: DEFAULT_POLICY;

	app.addHook('onSend', (request, reply, payload, next) => {
		reply.header('Referrer-Policy', policy);
		next();
	});

	next();
};

module.exports = fp(referrerPolicy, {
	fastify: '^1.0.0',
	name: 'fastify-referrer-policy'
});
