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

git-webhook-handler

v1.0.6

Published

Web handler / middleware for processing GitHub, Gitee, Gitlab Webhooks

Downloads

14

Readme

git-webhook-handler

Build Status

NPM

Fork form github-webhook-handler, add support for gitee , gitlab, gitea, gogs. English Document

网页开发中的网络钩子(Webhook)是一种通过自定义回调函数来增加或更改网页表现的方法。这些回调可被可能与原始网站或应用相关的第三方用户及开发者保存、修改与管理。术语“网络钩子”由杰夫·林德塞(Jeff Lindsay)于2007年通过给计算机编程术语“钩子”(Hook)加上前缀得来。网络钩子

基于 nodejs 实现对 webhook 处理的,支持:

Git 服务器的仓库都提供了 Webhooks 功能。每当代码仓库中有事件发生时,比如 push 代码,提 issue,提交 pull request,都可以往你配置的 Webhook 地址发送一个带有操作和仓库详细信息的请求。根据请求的信息,我们可以运行特定操作,自动更新代码等。Github Webhooks 文档

该库是Node.js Web服务器的小型处理程序,包含处理 Git 服务器发送的 Webhook 请求的所有逻辑。

注意

在 Git 仓库的 Webhooks 设置里面, 需要设置 Content-Typeapplication/json

例子🌰

以 Github 为栗。

const http = require('http')
const createHandler = require('git-webhook-handler')
const handler = createHandler({ path: '/webhook', secret: 'myhashsecret' })

http.createServer(function (req, res) {
  handler(req, res, function (err) {
    res.statusCode = 404
    res.end('no such location')
  })
}).listen(7777)

handler.on('error', function (err) {
  console.error('Error:', err.message)
})

handler.on('push', function (event) {
  console.log('Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref)
})

handler.on('issues', function (event) {
  console.log('Received an issue event for %s action=%s: #%d %s',
    event.payload.repository.name,
    event.payload.action,
    event.payload.issue.number,
    event.payload.issue.title)
})

部署和启动服务后,在 git 仓库进行设置:

webhook-setting

API 介绍

git-webhook-handler 会导出一个方法,通过这个方法来创建 webhook 的处理函数 handler。你需要提供一个 options 来确定一些参数:

  • "path": ${服务器地址/域名}:${端口号}${options.path} 就是最后的请求地址,填写在 git 仓库里面的。
  • "secret": 可以是一串随机字符串、hash。用来验证请求的,有的 Git 服务器会加密后返回,有的直接返回。比如 Github 是 HMAC SHA-1加密后放在请求头的 x-hub-signature 里面 Payloads,我们拿到这个 signature ,对比验证后,如果通过就可以执行定义好的对应事件的后续操作了。没有通过的话,会抛出 error 事件。
  • "events": 一个事件数组/字符串(事件参考: events.json),可选。会验证请求携带的事件参数是否在数组里面。比如 Github 是在请求的 x-github-event 。如果不存在也会抛出 error

options 也可以是一个数组:

const handler = createHandler([
  { path: '/webhook1', secret: 'myhashsecret1' },
  { path: '/webhook2', secret: 'myhashsecret2' }
])

返回的 handler 函数接受三个参数:request, response, callback。如果验证失败则执行 callback 回调。

handler 函数继承自 EventEmitter。所以可以在上面注册对应事件来处理 Git 服务器发来的具体事件类型。

可以通过下面的代码,查看 events.json 里面预先定义了一些事件。

var events = require('git-webhook-handler/events')
Object.keys(events).forEach(function (event) {
  console.log(event, '=', events[event])
})

也可以使用通配符 * 来监听所有事件,在调试的时候非常有用。

handler.on('*', function (event) {
  console.log(event.event);
});

License

MIT

git-webhook-handler is Copyright (c) 2020 Rem486.具体查看 LICENSE.md