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

node-proxymock

v0.4.3

Published

proxy and mock

Downloads

74

Readme

proxymock

类似fiddler的编程式代理mock工具

启用了3个端口, 代理服务器转发到http server和https server, 最终在express中集中处理

实际上只需要2个端口就可以实现同样的功能, 但3个端口可以做的事情更多, 比如http Server和https Server可以和proxy Server部署在不同的机器上, 实现Proxy Server向不同机器转发请求

如何使用

  • yarn add node-proxymocknpm install node-proxymock --save

  • 请参考test

  const { proxyMock } = require('node-proxymock');

  proxyMock({
    rules: {
      'GET http://e.iguzhi.com/lib/c.json': async(req, res, rawData) => {
        return {
          a: 5,
          b: 6
        }
      },
      'GET http://f.iguzhi.com/api/d.json': async(req, res, rawData) => {
        await new Promise((resolve) => setTimeout(() => resolve(), 3000))
        res.json([{a:23},'ff', [24.4,22]])
      },
      'GET http://g.iguzhi.com/api/ss.json': {
        a: 33,
        b: 44
      },
      'POST https://test.abc.com/api/mini/go.do': async(req, res, rawData) => {
        return req.body;
      },
      '^ GET http://g.iguzhi.com/api/s.json': { // 以 ^ 开头的规则不会发送真实请求而直接返回这里的mock数据
        a: 1,
        b: 2
      },
      'GET https://www.jb51.net/skin/2019/css/base.css': '123',
      'GET https://www.jb51.net/article/1353101.htm': (req, res, rawData) => {
        return `
          <html>
            <title>Hello ProxyMock</title>
            <body>类似fiddler的编程式代理mock工具</body>
          </html>
        `
      },
      // '^ POST https://a.iguzhi.com/pmp.gif': { b: 55556666 },
      'GET https://i.baidu.com/map.json': (req, res, rawData) => {
        return rawData;
      },
      'GET https://www.jb51.net/jslib/syntaxhighlighter/scripts/shCore.js': (req, res, rawData) => {
        return `alert('hello proxymock')`
      },
      '^ GET /iguzhi\.com\/regexp\/test\.json/i': 'test regexp match' // 支持正则
    },
    setSystemProxy: true, // 是否设置系统代理, 默认值 false
    logLevel: 'info', // 日志级别, 默认值级别 info
    disableCache: true // 禁用缓存, 默认值 true
  });

匹配规则优先级

  • 通过 req.methodreq.protocol + '://' + req.hostname + req.path 与 配置的规则进行比对

  • 优先 === 比对, 比对失败再进行正则比对

  • 正则比对时, req.method依然是===比对, req.protocol + '://' + req.hostname + req.path部分使用正则比对

  • 进入到正则比对时, 如果有多个正则规则, 那么遇到第一个匹配成功的规则后终止后续规则比对

测试

运行 yarn test 或者 npm run test

response header 附加字段说明

Prxoy-Engine: ProxyMock 表示当前资源是经过ProxyMock转发而来

Prxoy-Phase-Flag: ^ 表示当前资源由命中规则的数据直接返回, 没有向远程服务器发送真实请求; 若命中规则是函数则该函数没有第三个参数. 请注意与下一条描述的差异点

Prxoy-Phase-Flag: $ 表示当前资源虽然由命中规则返回, 但是在向远程服务器发送真实请求返回真实数据后再返回命中规则的返回的数据; 如果命中规则是个函数, 那么该函数的第三个参数是真实请求返回的数据

日志

日志打印在操作系统的用户目录下 .proxymock/logs

参考以下分享、感谢作者

CDN的基本工作过程

浅谈一个网页打开的全过程(涉及DNS、CDN、Nginx负载均衡等)

使用程序修改系统(IE)代理设置

Fiddler工作原理

HTTP协议 (一) HTTP协议详解

HTTP协议 (二) 基本认证

HTTP协议 (三) 压缩

HTTP协议 (四) 缓存

HTTP协议 (五) 代理

HTTP协议 (六) 状态码详解

HTTP协议 (七) Cookie

利用nodejs搭建 https 代理服务器并实现中间人攻击

编写了一个HTTP高匿代理

用c#编写socks代理服务器,大白话细述协议的最重要部分。

加密的TCP通讯全过程

c#做端口转发程序支持正向连接和反向链接