Skip to content

Commit fb03788

Browse files
committed
Cleanup
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent c0b9f12 commit fb03788

4 files changed

Lines changed: 125 additions & 149 deletions

File tree

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const {Client} = require('./lib/client')
1+
const express = require('./lib/express')
22

33
module.exports = {
4-
Client,
4+
express,
55
}

lib/client.js

Lines changed: 0 additions & 147 deletions
This file was deleted.

lib/cube/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const got = require('got')
2+
3+
class Cube {
4+
constructor(key, options) {
5+
this._started = false
6+
this.url = 'https://api.labstack.com/cube'
7+
this.key = key
8+
this.batchSize = options.batchSize || 60
9+
this.dispatchInterval = options.dispatchInterval || 60
10+
this.activeRequests = 0
11+
this.requests = []
12+
}
13+
14+
_getRealIP(request) {
15+
let ip = request.headers['x-forwarded-for'] ||
16+
request.headers['x-real-ip'] ||
17+
request.connection.remoteAddress
18+
if (Array.isArray(ip)) {
19+
return ip.split(', ')[0]
20+
} else {
21+
return ip
22+
}
23+
}
24+
25+
_resetRequests() {
26+
this.requests.length = 0
27+
}
28+
29+
_dispatch() {
30+
if (!this.requests.length) {
31+
return
32+
}
33+
34+
// TODO: handler error
35+
got.post(this.url, {
36+
headers: {
37+
'Authorization': `Bearer ${this.key}`
38+
},
39+
body: this.requests,
40+
json: true
41+
}).then(response => {
42+
}).catch(error => {
43+
console.error(error)
44+
// if (error instanceof got.HTTPError) {
45+
// const body = JSON.parse(error.response.body)
46+
// throw new Error(body.code, body.message)
47+
// } else {
48+
// throw new APIError(0, error)
49+
// }
50+
})
51+
52+
// Reset requests
53+
this.requests.length = 0
54+
}
55+
56+
start(request, response) {
57+
this.activeRequests++
58+
59+
// Daemon
60+
if (!this.started) {
61+
setInterval(() => {
62+
this._dispatch()
63+
}, this.dispatchInterval * 1000);
64+
}
65+
66+
const req = {}
67+
req.id = ''
68+
req.time = Date.now()
69+
req.host = request.headers.host
70+
req.path = request.url
71+
req.method = request.method
72+
req.bytes_in = parseInt(request.headers['content-length'])
73+
if (!req.bytes_in) {
74+
req.bytes_in = 0
75+
}
76+
req.remote_ip = this._getRealIP(request)
77+
req.client_id = cr.remote_ip
78+
req.user_agent = request.headers['user-agent']
79+
this.requests.push(req)
80+
return req
81+
}
82+
83+
stop(request, response) {
84+
this.activeRequests--
85+
86+
request.status = response.status
87+
request.bytes_out = response._headers['content-length']
88+
request.latency = Date.now() - request.time
89+
90+
// Dispatch batch
91+
if (this.requests.length >= this.batchSize) {
92+
this._dispatch()
93+
}
94+
}
95+
}
96+
97+
// class CubeError extends Error {
98+
// constructor(code, message) {
99+
// super(message)
100+
// this.code = code
101+
// Error.captureStackTrace(this, APIError)
102+
// this.name = this.constructor.name
103+
// }
104+
// }
105+
106+
module.exports = {
107+
Cube
108+
}
109+

lib/express/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {Cube} = require('../cube')
2+
3+
function cube(key, options) {
4+
return (request, response, next) => {
5+
const cube = new Cube(key, options)
6+
const req = cube.start(request, response)
7+
next()
8+
cube.stop(req, response)
9+
}
10+
}
11+
12+
module.exports = {
13+
cube
14+
}

0 commit comments

Comments
 (0)