|
| 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 | + |
0 commit comments