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

@httpland/chain-handler

v1.1.0

Published

Chainable and immutable HTTP handler for standard Request and Response

Downloads

9

Readme

chain-handler

Chainable and immutable HTTP handler for standard Request and Response.

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

What

Defines an API for chainable HTTP handler.

interface Handler {
  (request: Request): Response | Promise<Response>;
}

The declarative, web-standard compliant HTTP handler API is a powerful employed by deno/std.

We maintain this API and extend it. What we are adding to the Handler is a chaining mechanism.

interface ChainableHandler {
  (request: Request, next: OptionalHandler): Response | Promise<Response>;
}

interface OptionalHandler {
  (request?: Request): Response | Promise<Response>;
}

ChainableHandler is a handler that takes the next handler as the second argument.

ChainableHandler satisfies the following features:

  • It can access to the Request.
  • It can access the next handler.
  • It can call the next handler.
  • It can choose not to call the next handler.
  • It can access the next handler's return value (Response).
  • It can return Response.

OptionalHandler is the next handler itself. It is optional to call it, or to pass a modified Request object.

However, because of the emphasis on immutable, the Request object is propagated only through its arguments.

These features make it the core of middleware.

Packages

The package supports multiple platforms.

  • deno.land/x - https://deno.land/x/chain_handler/mod.ts
  • npm - @httpland/chain-handler

Usage

Chain is a stateful constructor. Add a ChainableHandler from the constructor or from the next function.

Handlers are executed asynchronously and recursively in the order of their declarations. Calling the next handler executes the next handler. await a call to the next handler gives access to the Response of the next handler.

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const chain = new Chain();
chain.next(async (request, next) => {
  // logger
  console.log("start");
  const response = await next();
  console.log("end");
  return response;
}, (request, next) => {
  // request proxy
  request.headers.append("x-proxy", "chain");
  return next(request);
}, async (_, next) => {
  // response proxy
  const response = await next();
  response.headers.append("server", "deno");
  return response;
}, () => {
  // cut off chain
  return new Response("hello");
}, () => {
  // not call because cut off by previous chain.
  return new Response("goodby");
});

const response = await chain.respond(new Request("http://localhost"));
assertEquals(await response.text(), "hello");
assertEquals(response.headers.get("server"), "deno");

In the respond function, apply a ChainableHandler to convert the Request into a Response.

Immutability

To reduce unexpected bugs, Request and Response are NOT shared among handlers. To propagate a change, pass the Request or Response to the next handler.

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const chain = new Chain();

chain.next((request, next) => {
  request.headers.append("x-effect", "no effect");
  return next();
}).next((request, next) => {
  assertEquals(request.headers.get("x-effect"), null);

  return next();
});

In order to propagate changes, a Request or Response must be passed to the next handler.

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const chain = new Chain((request, next) => {
  request.headers.append("x-effect", "effected");

  return next(request);
}, (request) => {
  assertEquals(request.headers.get("x-effect"), "effected");

  return new Response("ok");
});

This ensures that there are no destructive changes to the object.

Utility

Stateless functions are also available.

import {
  chain,
  type ChainableHandler,
} from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const initRequest = new Request("http://localhost");
const initResponse = new Response(null, { status: 404 });
const justThrough: ChainableHandler = (_, next) => next();

const response = await chain(
  initRequest,
  initResponse,
  justThrough,
  justThrough,
  ...new Array(5).fill(justThrough),
);
assertEquals(response.status, 404);

Tips

Detailed specifications are explained below.

Clone is not required

If you make a destructive change, such as reading a body, you do not need to clone it for the next handler.

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";

const chain = new Chain(async (request, next) => {
  // No need request.clone()
  const text = await request.text();
  return next();
}, async (request) => {
  const json = await request.json();
  return new Response("ok");
});

A clone of the argument or return value object is always used.

That is, clone is required in the following cases

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const chain = new Chain(async (request, next) => {
  const response = await next();
  const cloned = response.clone();

  assertEquals(await cloned.text(), "ok");

  return response;
}, async () => new Response("ok"));

Default response

Default Response is new Response(null, { status: 404 }). This can be completely changed.

import { Chain } from "https://deno.land/x/chain_handler@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const response = await new Chain().respond(
  new Request("http://localhost"),
  new Response("ok"),
);

assertEquals(await response.text(), "ok");

License

Copyright © 2023-present httpland.

Released under the MIT license