npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-protocol-handler

v3.0.1

Published

Resolve custom protocols using registered handlers

Downloads

57

Readme

custom-protocol-handler

build status install size npm package version github license js semistandard style

Resolve custom protocols using registered handlers.

Instalation

$ npm install custom-protocol-handler

Usage

This module can be used either in standalone mode or as Express middleware.

const protocolHandler = require('custom-protocol-handler')();
protocolHandler.protocol('s3://', url => 'https://example.com');

// Standalone usage
protocolHandler.resolve('s3://test').then(url => console.log(url));
//=> https://example.com

// Using as Express middleware
const port = 3000;
const app = require('express')();
app.get('/resolve', protocolHandler.middleware());
app.listen(port, () => console.log('listening on port: %i!', port));
$ ./example.sh

# resolve registered protocol: `s3:`

GET /resolve?url=s3://test HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:3000
User-Agent: HTTPie/1.0.2



HTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 41
Content-Type: text/plain; charset=utf-8
Date: Sat, 12 Jan 2019 16:55:26 GMT
Location: https://example.com
Vary: Accept
X-Powered-By: Express

Found. Redirecting to https://example.com

# resolve standard protocol: `https:`

GET /resolve?url=https://google.com HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:3000
User-Agent: HTTPie/1.0.2



HTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 40
Content-Type: text/plain; charset=utf-8
Date: Sat, 12 Jan 2019 16:55:26 GMT
Location: https://google.com
Vary: Accept
X-Powered-By: Express

Found. Redirecting to https://google.com

# resolve unknown protocol: `gdrive:`

GET /resolve?url=gdrive://test HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:3000
User-Agent: HTTPie/1.0.2



HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 83
Content-Type: application/json; charset=utf-8
Date: Sat, 12 Jan 2019 16:55:27 GMT
ETag: W/"53-Z2BGf/llR30GzNCkJLqNslE8IJ4"
X-Powered-By: Express

{
    "error": {
        "code": 1,
        "message": "Unknown protocol: `gdrive:`",
        "name": "ProtocolError"
    }
}

API

Table of Contents

ProtocolError

Extends Error

Custom error indicating invalid, unknown or blacklisted protocol

Parameters

ProtocolError.code

Type: Object

Properties

ProtocolHandler

Create protocol handler

Parameters

  • options ProtocolHandlerOptions protocol handler options (optional, default {})
    • options.blacklist (optional, default [])

protocol

Registers protocol handler

Parameters
Examples
// register multiple handlers
const handler = new ProtocolHandler();
handler
  .protocol('s3://', resolve)
  .protocol('gdrive://', resolve);
  • Throws ProtocolError throws if protocol scheme is invalid or blacklisted

Returns ProtocolHandler instance to allow chaining

protocols

Properties
Examples
// check if protocol is registered
const handler = new ProtocolHandler();
handler.protocol('s3://', resolve);
console.log(handler.protocols.has('s3:'));
//=> true

resolve

Asynchronously resolves url with registered protocol handler

Parameters
Examples
// create handler
const handler = new ProtocolHandler();
handler.protocol('s3://', url => 'https://example.com');
// resolve url
handler.resolve('s3://test').then(url => console.log(url));
//=> https://example.com
handler.resolve('file:///local/file.txt').then(url => console.log(url));
//=> file:///local/file.txt
handler.resolve('dummy://unknown/protocol');
//=> throws ProtocolError
  • Throws ProtocolError throws if url contains invalid or unknown protocol

Returns Promise<String> resolved url, redirect location

middleware

Returns Express middleware

Parameters
  • param String name of query param containing target url (optional, default 'url')
  • cb ProtocolErrorCallback? custom error handling callback
Examples
// create handler
const handler = new ProtocolHandler();
handler.protocol('s3://', resolve);
// attach to express app
app.use(handler.middleware());

module.exports

Create new ProtocolHandler instance

Parameters

Examples

const handler = require('custom-protocol-handler')();

Returns ProtocolHandler instance

ProtocolHandlerOptions

Type: Object

Properties

ProtocolCallback

Resolver function for specific protocol

Type: Function

Parameters

Examples

// Resolve gdrive urls
const { fetchInfo } = require('gdrive-file-info');

async function resolve(url) {
  const itemId = new URL(url).pathname;
  const fileInfo = await fetchInfo(itemId);
  return fileInfo.downloadUrl;
}

Returns (String | Promise<String>) resolved url redirect location

ProtocolErrorCallback

Custom error calback for Express middleware

Type: Function

Parameters

Examples

const handler = new ProtocolHandler();
handler.protocol('s3://', resolve);
// Redirect ONLY registered protocols
app.use(handler.middleware('url', (err, url, res) => {
  if (!err) res.redirect(url);
  return res.sendStatus(400);
}));