-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
115 lines (93 loc) · 3.21 KB
/
index.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
"use strict"
var http=require('http')
var debug=require('debug')
var qs=require('querystring')
var uuid=require('uuid4')
const util = require('util')
if (process.argv.length == 2)
var sPort = 80
else
var sPort = process.argv[2]
// Modify these function to do your own custom integration
const cmdTable = {
"ObjectCreated:Put" : putObject,
"ObjectCreated:Post" : unSupportedEvent,
"ObjectCreated:Copy" : unSupportedEvent,
"ObjectCreated:CompleteMultipartUpload" : unSupportedEvent,
"ObjectRemoved:Delete" : delObject,
"ObjectRemoved:DeleteMarkerCreated" : unSupportedEvent,
"ReducedRedundancyLostObject" : unSupportedEvent
}
// Modify these functions for your custom integration
function putObject( snsRecord ) {
// modify to intercept puts
let myEvent = {
eventName : snsRecord.eventName,
sourceIpAddress : snsRecord.requestParameters.sourceIPAddress,
bucketName : snsRecord.s3.bucket.name,
objectKey : snsRecord.s3.object.key
}
console.log(myEvent)
}
function delObject( snsRecord ) {
// modify to intercept deletes
let myEvent = {
eventName : snsRecord.eventName,
sourceIpAddress : snsRecord.requestParameters.sourceIPAddress,
bucketName : snsRecord.s3.bucket.name,
objectKey : snsRecord.s3.object.key
}
console.log(myEvent)
}
// Unsuppported event
function unSupportedEvent( snsRecord ) {
console.log("Received unSupportedEvent")
console.log(snsRecord)
}
var httpServer = http.createServer(function(req,res) {
console.log("Incoming msg:")
let body = []
req.on('data', (chunk) => { body.push(chunk) })
req.on('end', () => {
let resBody = `
<PublishResponse xmlns="http://vishnus-sns-server/">
<PublishResult>
<MessageId>` + uuid() + `</MessageId>
</PublishResult>
<ResponseMetadata>
<RequestId>` + uuid() + `</RequestId>
</ResponseMetadata>
</PublishResponse>\n`
switch(req.method) {
case 'HEAD':
resBody = ""
console.log(req.method)
console.log(req.headers)
break
case 'POST':
case 'PUT':
let myBody = qs.parse(decodeURIComponent(Buffer.concat(body).toString()))
try {
myBody.Message = JSON.parse(myBody.Message)
//console.log(util.inspect(myBody, {showHidden: false, depth: null}))
for (let record of myBody.Message.Records)
(record.eventName in cmdTable) ?
cmdTable[record.eventName](record) : unSupportedEvent (record)
} catch (err) {
//resBody = "Got a non-JSON Message"
console.log("Got a non-JSON Message")
console.log(myBody)
}
break
default:
console.log(req.method)
console.log(req.headers)
}
// framing the response
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(resBody)
console.log("msg handling complete.")
})
})
httpServer.listen({port: sPort})
console.log ("On-Prem-SNS v1.1 ready on port: ", httpServer.address().port)