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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@epiijs/httply

v1.1.0

Published

A toolkit for handling HTTP requests and responses.

Readme

@epiijs/httply

English

处理 HTTP 请求与响应的工具包。

它把 Node 的一对收发对象整理成结构化消息,交给消费它的 handler。

安装

npm i --save @epiijs/httply

用法

import http from 'node:http';
import { IncomingMessage, OutgoingMessage } from '@epiijs/httply';

http.createServer(async (request, response) => {
  // 由原始请求构造结构化入站消息
  const incoming = new IncomingMessage(request);

  // 读取请求体(惰性、缓存、任意 method 都可读)
  // 要在发出响应之前读:响应 finish 之后,Node 会丢弃残余的请求体
  const body = await incoming.body;

  // 由多种内容类型构造结构化出站消息
  const outgoing = OutgoingMessage.from('Hello, world!');
  // 或:OutgoingMessage.from(Buffer.from('...'))
  // 或:new OutgoingMessage({ status: 201, headers: {...}, content: '...' })

  // 写出响应
  const { completed } = await outgoing.applyToResponse(response);
  // completed 为 false 表示响应没有完整交入内核(通常是客户端提前断开),这不是错误
}).listen(8080);

IncomingMessage

new IncomingMessage(req) 把 Node 的 IncomingMessage 包装成:

| 字段 | 类型 | 说明 | |-----------|-----------------------------|--------------------------| | url | string | 请求 URL | | method | HttpMethod | GET、POST、PUT 等 | | headers | IncomingHttpHeaders | 原始请求头 | | query | Record<string, string | string[]> | 惰性解析的查询参数 | | body | Promise<Buffer> | 惰性读取的请求体(缓存) |

incoming.body 读不到完整数据就 reject,不返回半份或空数据。reject 的错误的 code 有这些情况:

  • HTTPLY_BODY_ABORTED:请求流被截断。客户端发到一半断开、请求帧解析失败,或服务端调用了 req.destroy()。
  • HTTPLY_BODY_DROPPED:首次读取发生在响应 finish 之后,此时 Node 已丢弃残余请求体。

HTTPLY_BODY_DROPPED 在读取当场给出,HTTPLY_BODY_ABORTED 在流终止时给出。

try {
  const body = await incoming.body;
} catch (error) {
  if (error.code === 'HTTPLY_BODY_DROPPED') {
    // 顺序错了:要在写出响应之前读 body
  }
}

IncomingMessage.body 是全量 Promise<Buffer>,没有提供流式读取。 所以 httply 适合收到请求体后立刻消费,不适合代理转发:转发需要 await 请求体后再转发,性能有瓶颈。

httply 不提供读取请求体的超时控制,如果需要超时控制,可以自己实现或使用 Node API 。

OutgoingMessage

OutgoingMessage.from(message) 接受以下任一种输入:

  • string:按 text/plain 响应
  • Buffer / Readable:按 application/octet-stream 响应
  • { status?, headers?, content? }:逐项指定
  • null / undefined:按 204 No Content 响应

结构化构造用 new OutgoingMessage({ status?, headers?, content? })。

message.applyToResponse(response) 把消息写入 ServerResponse,本次写入流程终止时 resolve { completed: boolean }:

  • completed: true:响应已交入内核(finish 已触发),不代表客户端已收到。
  • completed: false:写入在 finish 之前终止,通常是连接已经断了。这不是错误,Promise 照样 resolve。
  • 只有内容流自身出错才 reject。

finish 之后,请求体未读完的连接会由 httply 回收,不读 body 的分支不需要自己 Connection: close。