webfinger-handler
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Webfinger Handler

A library that generates a handler for webfinger requests. The created handler works with Node JS HTTP request and response objects, and is otherwise framework agnostic.

import WebfingerHandler from 'webfinger-handler';

// Handle requests for `[username]@my-website.example`
// This example is synchronous, but you can make it async if you need to e.g. access a database
const webfinger = new WebfingerHandler(resource => {
    if(resource.host !== 'my-website.example') {
        // Return null if there are no links for this resource
        return null;
    }

    // Either return the whole json-rd response object or just the links array
    return [
        {
            rel: 'http://webfinger.net/rel/profile-page',
            href: `https://my-website.example/profile/${resource.user}`
        },
    ]
});

// This is the standard httpServer.listen callback
export default async function onRequest(req, res) {
    if(await webfinger.handle(req, res)) {
        // The handler will return true if it has handled the request,
        // and false if not (i.e. the req.url path is not '/.well-known/webfinger')
        return;
    }

    // Not a webfinger request - run whatever site code you need to
    res.end('Welcome to my website!');
}

Express usage

For use with Express or Connect, you'll need to wrap your function like so:

// Assuming app is your Express instance
app.use(async function webfingerExpress(req, res, next) {
	try {
		if(!await webfinger.handle(req, res)) {
			next();
		}
	} catch(e) {
		next(e);
	}
});

Package Sidebar

Install

npm i webfinger-handler

Weekly Downloads

1

Version

2.0.1

License

ISC

Unpacked Size

20.2 kB

Total Files

7

Last publish

Collaborators

  • paulkiddle