storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/jsonrpc.js (about) 1 /* 2 * MinIO Cloud Storage (C) 2016 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import SuperAgent from 'superagent-es6-promise'; 18 import url from 'url' 19 import Moment from 'moment' 20 21 export default class JSONrpc { 22 constructor(params) { 23 this.endpoint = params.endpoint 24 this.namespace = params.namespace 25 this.version = '2.0'; 26 const parsedUrl = url.parse(this.endpoint) 27 this.host = parsedUrl.hostname 28 this.path = parsedUrl.path 29 this.port = parsedUrl.port 30 31 switch (parsedUrl.protocol) { 32 case 'http:': { 33 this.scheme = 'http' 34 if (parsedUrl.port === 0) { 35 this.port = 80 36 } 37 break 38 } 39 case 'https:': { 40 this.scheme = 'https' 41 if (parsedUrl.port === 0) { 42 this.port = 443 43 } 44 break 45 } 46 default: { 47 throw new Error('Unknown protocol: ' + parsedUrl.protocol) 48 } 49 } 50 } 51 // call('Get', {id: NN, params: [...]}, function() {}) 52 call(method, options, token) { 53 if (!options) { 54 options = {} 55 } 56 if (!options.id) { 57 options.id = 1; 58 } 59 if (!options.params) { 60 options.params = {}; 61 } 62 const dataObj = { 63 id: options.id, 64 jsonrpc: this.version, 65 params: options.params ? options.params : {}, 66 method: this.namespace ? this.namespace + '.' + method : method 67 } 68 let requestParams = { 69 host: this.host, 70 port: this.port, 71 path: this.path, 72 scheme: this.scheme, 73 method: 'POST', 74 headers: { 75 'Content-Type': 'application/json', 76 'x-amz-date': Moment().utc().format('YYYYMMDDTHHmmss') + 'Z' 77 } 78 } 79 80 if (token) { 81 requestParams.headers.Authorization = 'Bearer ' + token 82 } 83 84 let req = SuperAgent.post(this.endpoint) 85 for (let key in requestParams.headers) { 86 req.set(key, requestParams.headers[key]) 87 } 88 // req.set('Access-Control-Allow-Origin', 'http://localhost:8080') 89 return req.send(JSON.stringify(dataObj)).then(res => res) 90 } 91 }