I’ve been having a look at a few node AP projects and how they handle webfinger. I think there’s subtle differences but it might make sense to create a connect library for responding to webfinger requests, which can be used with express, koa, and node’s native http server without much pain.
The library could handle parsing the query, check the hostname, 404 responses, setting the correct headers, writing the response, etc etc. An example API might look like this:
const webfingerMiddleware = webfinger(
// The first argument looks up the user and returns the links. Can return the user's url for a shortcut.
async username => {
const user = await myApi.lookupUser(username);
if(!user) { return null; } // Triggers the 404 case
return user.href;
},
{
// Options object allows for more control, like using a custom regex
matcher: /^acct:[@~]?([^@]+)@(.+)$/
}
);
@datatitian so far I think your library is the only one I’ve found that uses webfinger with express. What do you think of this idea? I notice your library splits webfinger requests into three - request parsing, 404 handling, and response generation. I think this connect middleware could handle all of these for most use cases without any extra configuration than I’ve shown above.
The only reservation I have here is that a webfinger specific library might be too small/specific to make it a library of its own. But the payoff of new AP implementors not having to read up on webfinger could be worth it?
Any thoughts?