-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
130 lines (99 loc) · 4.54 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// const createHookReceiver = require('npm-hook-receiver')
// const kafka = require('./kafka')
// const producer = kafka.producer()
// const main = async () => {
// await producer.connect()
// const server = createHookReceiver({
// // Secret created when registering the webhook with NPM.
// // Used to validate the payload.
// secret: process.env.HOOK_SECRET,
// // Path for the handler to be mounted on.
// mount: '/hook'
// })
// server.on('package:publish', async event => {
// try {
// const responses = await producer.send({
// topic: process.env.TOPIC,
// messages: [{
// // Name of the published package as key, to make sure that we process events in order
// key: event.name,
// // The message value is just bytes to Kafka, so we need to serialize our JavaScript
// // object to a JSON string. Other serialization methods like Avro are available.
// value: JSON.stringify({
// package: event.name,
// version: event.version
// })
// }]
// })
// console.log('Published message', { responses })
// } catch (error) {
// console.error('Error publishing message', error)
// }
// })
// server.listen(process.env.PORT || 3000, () => {
// console.log(`Server listening on port ${process.env.PORT || 3000}`)
// })
// }
// main().catch(error => {
// console.error(error)
// process.exit(1)
// })
// working way to start server
// HOOK_SECRET="super-secret-string" KAFKA_BOOTSTRAP_SERVER="localhost:9092" TOPIC="npm-package-published" KAFKA_USERNAME="SJ335XIPE5Q5ZPYS" KAFKA_PASSWORD="r4kpR3eQXiEEsH6CVvTfILuDaO3MIDRPgiXSM9fWLd3dVqmWDT4qafdCc9W9aKbF" KAFKA_BOOTSTRAP_SERVER="pkc-ep9mm.us-east-2.aws.confluent.cloud:9092" node server.js
// HOOK_SECRET="super-secret-string" KAFKA_BOOTSTRAP_SERVER="localhost:9092" TOPIC="npm-package-published" node server.js
// KAFKA_BOOTSTRAP_SERVER=pkc-ep9mm.us-east-2.aws.confluent.cloud:9092
// API_KEY=SJ335XIPE5Q5ZPYS
// API_PASSWORD=r4kpR3eQXiEEsH6CVvTfILuDaO3MIDRPgiXSM9fWLd3dVqmWDT4qafdCc9W9aKbF
// const crypto = require('crypto')
// const hmac = crypto.createHmac('sha256', 'YOUR_SECRET').update('REQUEST_BODY').digest('hex')
// const signature = `sha256=${hmac}`
// console.log(signature);
const createHookReceiver = require('npm-hook-receiver')
const kafka = require('./kafka')
const producer = kafka.producer()
const main = async () => {
await producer.connect()
const server = createHookReceiver({
// Secret created when registering the webhook with NPM.
// Used to validate the payload.
secret: process.env.HOOK_SECRET,
// Path for the handler to be mounted on.
mount: '/hook'
})
server.on('package:publish', async event => {
try {
const responses = await producer.send({
topic: process.env.TOPIC,
messages: [{
// Name of the published package as key, to make sure that we process events in order
key: event.name,
// The message value is just bytes to Kafka, so we need to serialize our JavaScript
// object to a JSON string. Other serialization methods like Avro are available.
value: JSON.stringify({
package: event.name,
version: event.version
})
}]
})
console.log('Published message', { responses })
} catch (error) {
console.error('Error publishing message', error)
}
})
server.listen(process.env.PORT || 3000, () => {
console.log(`Server listening on port ${process.env.PORT || 3000}`)
})
}
main().catch(error => {
console.error(error)
process.exit(1)
})
// curl -XPOST \
// -H "Content-Type: application/json" \
// -H "x-npm-signature: sha256=af9688ba91280abf35e4720bf9ae6c9662c55acef800cfcfc40a778bd0cd5c08" \
// -d '{"event":"package:publish","name":"@kafkajs/zstd","version":"1.0.0","hookOwner":{"username":"nevon"},"payload":{"name":"@kafkajs/zstd"},"change":{"version":"1.0.0"},"time":1603444214995}' \
// http://localhost:3000/hook
// const crypto = require('crypto')
// const hmac = crypto.createHmac('sha256', 'YOUR_SECRET').update('REQUEST_BODY').digest('hex')
// const signature = `sha256=${hmac}`
// curl -XPOST -H "Content-Type: application/json" -H "x-npm-signature: sha256=af9688ba91280abf35e4720bf9ae6c9662c55acef800cfcfc40a778bd0cd5c08" -d '{"event":"package:publish","name":"@kafkajs/zstd","version":"1.0.0","hookOwner":{"username":"nevon"},"payload":{"name":"@kafkajs/zstd"},"change":{"version":"1.0.0"},"time":1603444214995}' http://localhost:3000/hook