-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (130 loc) · 3.74 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const http = require('http')
const httpProxy = require('http-proxy')
const request = require('request')
const crypto = require('crypto')
const _ = require('underscore')
class TransloaditNotifyUrlProxy {
constructor (secret, notifyUrl) {
this._server = null
this._proxy = null
this._secret = secret | ''
this._notifyUrl = notifyUrl || 'http://127.0.0.1:3000/transloadit'
this._defaults = {
target: 'https://api2.transloadit.com/assemblies/',
port: 8888,
pollInterval: 2000,
}
this._settings = {}
}
run (opts) {
if (opts === undefined) {
opts = {}
}
this._settings = _.extend(this._defaults, opts)
this._createProxy()
this._createServer()
}
close () {
if (this._server !== null) {
this._server.close()
this._server = null
}
if (this._proxy !== null) {
this._proxy.close()
this._proxy = null
}
}
_createProxy () {
this._proxy = httpProxy.createProxyServer({
target: this._settings.target
})
}
_createServer () {
this._server = http.createServer((req, res) => {
this._proxy.web(req, res)
this._proxy.on('proxyRes', (res) => {
let body = ''
res.on('data', (chunk) => {
body += chunk
})
res.on('end', () => {
const assemblyUrl = JSON.parse(body).assembly_url
this._out("Received proxy response, polling assemblyUrl: %s", assemblyUrl)
this._pollAssembly(assemblyUrl)
})
})
}).listen(this._settings.port)
this._out("Listening on http://localhost:%d, forwarding to %s, notifying %s",
this._settings.port,
this._settings.target,
this._notifyUrl
)
}
_pollAssembly (assemblyUrl) {
const opts = {
retries: 10,
minTimeout: this._settings.pollInterval,
maxTimeout: this._settings.pollInterval,
}
const operation = retry.operation(opts)
operation.attempt((currentAttempt) => {
this._checkAssembly(assemblyUrl, (err, response) => {
if (!err && response) {
console.debug("%s valid response, notifying.", assemblyUrl)
return this._notify(response)
}
console.debug("%s not completed, checking again.", assemblyUrl)
if (operation.retry(err)) {
return
}
this._out("No attempts left, giving up on checking assemblyUrl: %s", assemblyUrl)
})
})
}
_checkAssembly (assemblyUrl, cb) {
request.get(assemblyUrl, (err, res, body) => {
let response = JSON.parse(body)
let err = null
let msg = ''
if (!response || !response.ok) {
err = new Error('No ok field found in Assembly response.')
return cb(err)
}
if (response.ok == 'ASSEMBLY_COMPLETED') {
this._out('%s completed.', assemblyUrl)
return cb(null, response)
}
if (response.ok == 'ASSEMBLY_UPLOADING'){
msg = `${assemblyUrl} is still uploading.`
this._out(msg)
return cb(new Error(msg))
}
if (response.ok == 'ASSEMBLY_EXECUTING'){
msg = `${assemblyUrl} is still executing.`
this._out(msg)
return cb(new Error(msg))
}
cb(new Error(`${assemblyUrl} - unknown Assembly state found.`))
})
}
_notify (response) {
const stringified = JSON.stringify(response)
const signature = this._getSignature(stringified)
request.post(this._notifyUrl, {
form: {
transloadit: stringified,
signatture: signature,
}
})
}
_getSignature (toSign) {
return crypto
.createHmac('sha1', this._secret)
.update(Buffer.from(toSign, 'utf-8'))
.digest('hex')
}
_out (msg, ...args) {
console.log(msg, ...args)
}
}
module.exports = TransloaditNotifyUrlProxy