| 6 | const Pusher = require('pusher-js'); |
| 7 | |
| 8 | class Registry { |
| 9 | |
| 10 | constructor (host, port, accessToken) { |
| 11 | this.host = host; |
| 12 | this.port = parseInt(port) || 0; |
| 13 | this.accessToken = accessToken; |
| 14 | this.http = this.port === 443 ? https : http; |
| 15 | this.pusher = new Pusher('676bca06a7eb744a334f', { |
| 16 | cluster: 'us3', |
| 17 | forceTLS: true |
| 18 | }); |
| 19 | } |
| 20 | |
| 21 | request (action, params, body, completeCallback, progressCallback) { |
| 22 | |
| 23 | let pusher = this.pusher; |
| 24 | |
| 25 | let completed = false; |
| 26 | let callback = function () { |
| 27 | completed = true; |
| 28 | completeCallback.apply(this, arguments); |
| 29 | }; |
| 30 | |
| 31 | let performAction = function () { |
| 32 | |
| 33 | let uriString = Object.keys(params) |
| 34 | .filter(function (key) { |
| 35 | return params[key] !== undefined && params[key] !== null; |
| 36 | }) |
| 37 | .map(function (key) { |
| 38 | let value = params[key]; |
| 39 | value = typeof value === 'object' |
| 40 | ? JSON.stringify(value) |
| 41 | : value; |
| 42 | return [encodeURIComponent(key), encodeURIComponent(value)].join('='); |
| 43 | }) |
| 44 | .join('&'); |
| 45 | |
| 46 | let req = this.http.request( |
| 47 | { |
| 48 | hostname: this.host, |
| 49 | port: this.port, |
| 50 | path: '/' + action + '?' + uriString, |
| 51 | method: 'POST', |
| 52 | headers: { |
| 53 | 'Authorization': `Bearer ${this.accessToken}`, |
| 54 | 'Content-Length': body ? body.byteLength : 0 |
| 55 | } |
| 56 | }, |
| 57 | res => { |
| 58 | let buffers = []; |
| 59 | res.on('data', chunk => buffers.push(chunk)); |
| 60 | res.on('end', () => { |
| 61 | let buffer = Buffer.concat(buffers); |
| 62 | let text = buffer.toString(); |
| 63 | let json; |
| 64 | let error; |
| 65 | try { |
nothing calls this directly
no outgoing calls
no test coverage detected