Programmable Custom Protocol Handlers

An api that allows a WebExtension to register itself as the handler for new protocols, and can return responses to the browser.

Motivation

Users of a decentralized web want to get content from a network of peers rather than a specific server.

Right now, only possible to register custom protocols handlers statically, from the manifest.json, where a uri is mapped to a url template, and a separate, centralised service must be maintained to handler the mapping. The custom protocol handler can only redirect the user to an (http) url, it cannot return content directly to the browser. The redirect is visible in the browsers url bar, and the original uri is no longer visible to the user.

Usage Documentation

Using protocol.registerStreamProtocol in electron as a starting point

browser.protocol.registerStreamProtocol('ipfs', (request, callback) => {
  const {cid, contentType} = extractInfo(request)  // an exercise for the reader
  const stream = ipfs.files.catReadableStream(cid)
  callback({
    statusCode: 200,
    headers: {
      'Content-Type': contentType
    }
    data: stream
  })
}, (error) => {
  if (error) console.error('Failed to register protocol')
})

This example shows how you would add a handler for the ipfs protocol and have it return a file retrieved via the IPFS network as a stream to the browser.

Notes