@filerobot/companion-client
Advanced tools
Comparing version 1.0.21 to 1.0.22
'use strict'; | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
@@ -18,8 +28,12 @@ | ||
var AuthError = /*#__PURE__*/function (_Error) { | ||
_inheritsLoose(AuthError, _Error); | ||
_inherits(AuthError, _Error); | ||
var _super = _createSuper(AuthError); | ||
function AuthError() { | ||
var _this; | ||
_this = _Error.call(this, 'Authorization required') || this; | ||
_classCallCheck(this, AuthError); | ||
_this = _super.call(this, 'Authorization required'); | ||
_this.name = 'AuthError'; | ||
@@ -26,0 +40,0 @@ _this.isAuthError = true; |
@@ -0,0 +0,0 @@ 'use strict'; |
'use strict'; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } | ||
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
import RequestClient from './RequestClient'; | ||
@@ -19,12 +41,14 @@ import * as tokenStorage from './tokenStorage'; | ||
var Provider = /*#__PURE__*/function (_RequestClient) { | ||
_inheritsLoose(Provider, _RequestClient); | ||
_inherits(Provider, _RequestClient); | ||
function Provider(filerobot, opts) { | ||
var _super = _createSuper(Provider); | ||
function Provider(filerobot) { | ||
var _this; | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_this = _RequestClient.call(this, filerobot, opts) || this; | ||
_classCallCheck(this, Provider); | ||
_this = _super.call(this, filerobot, opts); | ||
_this.provider = opts.provider; | ||
@@ -34,77 +58,86 @@ _this.id = _this.provider; | ||
_this.pluginId = _this.opts.pluginId; | ||
_this.tokenKey = "companion-" + _this.pluginId + "-auth-token"; | ||
_this.tokenKey = "companion-".concat(_this.pluginId, "-auth-token"); | ||
return _this; | ||
} | ||
var _proto = Provider.prototype; | ||
_createClass(Provider, [{ | ||
key: "headers", | ||
value: function headers() { | ||
var _this2 = this; | ||
_proto.headers = function headers() { | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
_get(_getPrototypeOf(Provider.prototype), "headers", _this2).call(_this2).then(function (headers) { | ||
_this2.getAuthToken().then(function (token) { | ||
// TODO: change uppy-auth-token & all uppyTokens to filerobot-auth-token & filerobotTokens when BE do it. | ||
resolve(_extends({}, headers, { | ||
'uppy-auth-token': token | ||
})); | ||
}); | ||
}).catch(reject); | ||
}); | ||
} | ||
}, { | ||
key: "onReceiveResponse", | ||
value: function onReceiveResponse(response) { | ||
response = _get(_getPrototypeOf(Provider.prototype), "onReceiveResponse", this).call(this, response); | ||
var plugin = this.filerobot.getPlugin(this.pluginId); | ||
var oldAuthenticated = plugin.getPluginState().authenticated; | ||
var authenticated = oldAuthenticated ? response.status !== 401 : response.status < 400; | ||
plugin.setPluginState({ | ||
authenticated: authenticated | ||
}); | ||
return response; | ||
} | ||
}, { | ||
key: "setAuthToken", | ||
value: function setAuthToken(token) { | ||
return this.filerobot.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token); | ||
} | ||
}, { | ||
key: "getAuthToken", | ||
value: function getAuthToken() { | ||
return this.filerobot.getPlugin(this.pluginId).storage.getItem(this.tokenKey); | ||
} | ||
}, { | ||
key: "authUrl", | ||
value: function authUrl() { | ||
return "".concat(this.hostname, "/").concat(this.id, "/connect"); | ||
} | ||
}, { | ||
key: "fileUrl", | ||
value: function fileUrl(id) { | ||
return "".concat(this.hostname, "/").concat(this.id, "/get/").concat(id); | ||
} | ||
}, { | ||
key: "list", | ||
value: function list(directory) { | ||
return this.get("".concat(this.id, "/list/").concat(directory || '')); | ||
} | ||
}, { | ||
key: "logout", | ||
value: function logout() { | ||
var _this3 = this; | ||
return new Promise(function (resolve, reject) { | ||
_RequestClient.prototype.headers.call(_this2).then(function (headers) { | ||
_this2.getAuthToken().then(function (token) { | ||
// TODO: change uppy-auth-token & all uppyTokens to filerobot-auth-token & filerobotTokens when BE do it. | ||
resolve(_extends({}, headers, { | ||
'uppy-auth-token': token | ||
})); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}; | ||
_proto.onReceiveResponse = function onReceiveResponse(response) { | ||
response = _RequestClient.prototype.onReceiveResponse.call(this, response); | ||
var plugin = this.filerobot.getPlugin(this.pluginId); | ||
var oldAuthenticated = plugin.getPluginState().authenticated; | ||
var authenticated = oldAuthenticated ? response.status !== 401 : response.status < 400; | ||
plugin.setPluginState({ | ||
authenticated: authenticated | ||
}); | ||
return response; | ||
}; | ||
_proto.setAuthToken = function setAuthToken(token) { | ||
return this.filerobot.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token); | ||
}; | ||
_proto.getAuthToken = function getAuthToken() { | ||
return this.filerobot.getPlugin(this.pluginId).storage.getItem(this.tokenKey); | ||
}; | ||
_proto.authUrl = function authUrl() { | ||
return this.hostname + "/" + this.id + "/connect"; | ||
}; | ||
_proto.fileUrl = function fileUrl(id) { | ||
return this.hostname + "/" + this.id + "/get/" + id; | ||
}; | ||
_proto.list = function list(directory) { | ||
return this.get(this.id + "/list/" + (directory || '')); | ||
}; | ||
_proto.logout = function logout() { | ||
var _this3 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this3.get(_this3.id + "/logout").then(function (res) { | ||
_this3.filerobot.getPlugin(_this3.pluginId).storage.removeItem(_this3.tokenKey).then(function () { | ||
return resolve(res); | ||
return new Promise(function (resolve, reject) { | ||
_this3.get("".concat(_this3.id, "/logout")).then(function (res) { | ||
_this3.filerobot.getPlugin(_this3.pluginId).storage.removeItem(_this3.tokenKey).then(function () { | ||
return resolve(res); | ||
}).catch(reject); | ||
}).catch(reject); | ||
}).catch(reject); | ||
}); | ||
}; | ||
}); | ||
} | ||
}], [{ | ||
key: "initPlugin", | ||
value: function initPlugin(plugin, opts, defaultOpts) { | ||
plugin.type = 'acquirer'; | ||
plugin.files = []; | ||
Provider.initPlugin = function initPlugin(plugin, opts, defaultOpts) { | ||
plugin.type = 'acquirer'; | ||
plugin.files = []; | ||
if (defaultOpts) { | ||
plugin.opts = _extends({}, defaultOpts, opts); | ||
} | ||
if (defaultOpts) { | ||
plugin.opts = _extends({}, defaultOpts, opts); | ||
plugin.storage = plugin.opts.storage || tokenStorage; | ||
} | ||
}]); | ||
plugin.storage = plugin.opts.storage || tokenStorage; | ||
}; | ||
return Provider; | ||
@@ -111,0 +144,0 @@ }(RequestClient); |
'use strict'; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
@@ -9,2 +27,4 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import { COMPANION_URL } from '@filerobot/utils/lib/constants'; | ||
@@ -19,7 +39,7 @@ import fetchWithNetworkError from '@filerobot/utils/lib/fetchWithNetworkError'; | ||
var RequestClient = /*#__PURE__*/function () { | ||
function RequestClient(filerobot, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
function RequestClient(filerobot) { | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, RequestClient); | ||
this.filerobot = filerobot; | ||
@@ -40,223 +60,236 @@ this.companionUrl = COMPANION_URL; | ||
var _proto = RequestClient.prototype; | ||
_createClass(RequestClient, [{ | ||
key: "hostname", | ||
get: function get() { | ||
var _this$filerobot$getSt = this.filerobot.getState(), | ||
companion = _this$filerobot$getSt.companion; | ||
_proto.headers = function headers() { | ||
var userHeaders = this.opts.companionHeaders || {}; | ||
return Promise.resolve(_extends({}, this.defaultHeaders, userHeaders)); | ||
}; | ||
var host = COMPANION_URL; | ||
return stripSlash(companion && companion[host] ? companion[host] : host); | ||
} | ||
}, { | ||
key: "defaultHeaders", | ||
get: function get() { | ||
return { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'Filerobot-Versions': "@filerobot/companion-client=".concat(RequestClient.VERSION) | ||
}; | ||
} | ||
}, { | ||
key: "headers", | ||
value: function headers() { | ||
var userHeaders = this.opts.companionHeaders || {}; | ||
return Promise.resolve(_objectSpread(_objectSpread({}, this.defaultHeaders), userHeaders)); | ||
} | ||
}, { | ||
key: "_getPostResponseFunc", | ||
value: function _getPostResponseFunc(skip) { | ||
var _this = this; | ||
_proto._getPostResponseFunc = function _getPostResponseFunc(skip) { | ||
var _this = this; | ||
return function (response) { | ||
if (!skip) { | ||
return _this.onReceiveResponse(response); | ||
} | ||
return function (response) { | ||
if (!skip) { | ||
return _this.onReceiveResponse(response); | ||
return response; | ||
}; | ||
} | ||
}, { | ||
key: "onReceiveResponse", | ||
value: function onReceiveResponse(response) { | ||
var state = this.filerobot.getState(); | ||
var companion = state.companion || {}; | ||
var host = COMPANION_URL; | ||
var headers = response.headers; // Store the self-identified domain name for the Companion instance we just hit. | ||
if (headers.has('i-am') && headers.get('i-am') !== companion[host]) { | ||
this.filerobot.setState({ | ||
companion: _extends({}, companion, _defineProperty({}, host, headers.get('i-am'))) | ||
}); | ||
} | ||
return response; | ||
}; | ||
}; | ||
_proto.onReceiveResponse = function onReceiveResponse(response) { | ||
var state = this.filerobot.getState(); | ||
var companion = state.companion || {}; | ||
var host = COMPANION_URL; | ||
var headers = response.headers; // Store the self-identified domain name for the Companion instance we just hit. | ||
if (headers.has('i-am') && headers.get('i-am') !== companion[host]) { | ||
var _extends2; | ||
this.filerobot.setState({ | ||
companion: _extends({}, companion, (_extends2 = {}, _extends2[host] = headers.get('i-am'), _extends2)) | ||
}); | ||
} | ||
}, { | ||
key: "_getUrl", | ||
value: function _getUrl(url) { | ||
if (/^(https?:|)\/\//.test(url)) { | ||
return url; | ||
} | ||
return response; | ||
}; | ||
_proto._getUrl = function _getUrl(url) { | ||
if (/^(https?:|)\/\//.test(url)) { | ||
return url; | ||
return "".concat(this.hostname, "/").concat(url); | ||
} | ||
}, { | ||
key: "_json", | ||
value: function _json(res) { | ||
if (res.status === 401) { | ||
throw new AuthError(); | ||
} | ||
return this.hostname + "/" + url; | ||
}; | ||
if (res.status < 200 || res.status > 300) { | ||
var errMsg = "Failed request with status: ".concat(res.status, ". ").concat(res.statusText); | ||
return res.json().then(function (errData) { | ||
errMsg = errData.message ? "".concat(errMsg, " message: ").concat(errData.message) : errMsg; | ||
errMsg = errData.requestId ? "".concat(errMsg, " request-Id: ").concat(errData.requestId) : errMsg; | ||
throw new Error(errMsg); | ||
}).catch(function () { | ||
throw new Error(errMsg); | ||
}); | ||
} | ||
_proto._json = function _json(res) { | ||
if (res.status === 401) { | ||
throw new AuthError(); | ||
return res.json(); | ||
} | ||
if (res.status < 200 || res.status > 300) { | ||
var errMsg = "Failed request with status: " + res.status + ". " + res.statusText; | ||
return res.json().then(function (errData) { | ||
errMsg = errData.message ? errMsg + " message: " + errData.message : errMsg; | ||
errMsg = errData.requestId ? errMsg + " request-Id: " + errData.requestId : errMsg; | ||
throw new Error(errMsg); | ||
}).catch(function () { | ||
throw new Error(errMsg); | ||
}); | ||
}, { | ||
key: "_increaseRequestsNumber", | ||
value: function _increaseRequestsNumber() { | ||
this._requestsNumber++; | ||
} | ||
}, { | ||
key: "_decreaseRequestsNumber", | ||
value: function _decreaseRequestsNumber() { | ||
this._requestsNumber--; | ||
} | ||
}, { | ||
key: "_addCancelAbility", | ||
value: function _addCancelAbility() { | ||
this._controller = new AbortController(); | ||
this._signal = this._controller.signal; | ||
this._requestsNumber = 0; | ||
} | ||
}, { | ||
key: "checkIfTherePendingRequests", | ||
value: function checkIfTherePendingRequests() { | ||
return this._requestsNumber > 0; | ||
} | ||
}, { | ||
key: "preflight", | ||
value: function preflight(path) { | ||
var _this2 = this; | ||
return res.json(); | ||
}; | ||
_proto._increaseRequestsNumber = function _increaseRequestsNumber() { | ||
this._requestsNumber++; | ||
}; | ||
_proto._decreaseRequestsNumber = function _decreaseRequestsNumber() { | ||
this._requestsNumber--; | ||
}; | ||
_proto._addCancelAbility = function _addCancelAbility() { | ||
this._controller = new AbortController(); | ||
this._signal = this._controller.signal; | ||
this._requestsNumber = 0; | ||
}; | ||
_proto.checkIfTherePendingRequests = function checkIfTherePendingRequests() { | ||
return this._requestsNumber > 0; | ||
}; | ||
_proto.preflight = function preflight(path) { | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
if (_this2.preflightDone) { | ||
return resolve(_this2.allowedHeaders.slice()); | ||
} | ||
_this2._increaseRequestsNumber(); | ||
fetch(_this2._getUrl(path), { | ||
method: 'OPTIONS', | ||
signal: _this2._signal | ||
}).then(function (response) { | ||
if (response.headers.has('access-control-allow-headers')) { | ||
_this2.allowedHeaders = response.headers.get('access-control-allow-headers').split(',').map(function (headerName) { | ||
return headerName.trim().toLowerCase(); | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
if (_this2.preflightDone) { | ||
return resolve(_this2.allowedHeaders.slice()); | ||
} | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}).catch(function (err) { | ||
_this2.filerobot.log("[CompanionClient] unable to make preflight request " + err, 'warning'); | ||
_this2._increaseRequestsNumber(); | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}).finally(_this2._decreaseRequestsNumber); | ||
}); | ||
}; | ||
fetch(_this2._getUrl(path), { | ||
method: 'OPTIONS', | ||
signal: _this2._signal | ||
}).then(function (response) { | ||
if (response.headers.has('access-control-allow-headers')) { | ||
_this2.allowedHeaders = response.headers.get('access-control-allow-headers').split(',').map(function (headerName) { | ||
return headerName.trim().toLowerCase(); | ||
}); | ||
} | ||
_proto.preflightAndHeaders = function preflightAndHeaders(path) { | ||
var _this3 = this; | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}).catch(function (err) { | ||
_this2.filerobot.log("[CompanionClient] unable to make preflight request ".concat(err), 'warning'); | ||
return Promise.all([this.preflight(path), this.headers()]).then(function (_ref) { | ||
var allowedHeaders = _ref[0], | ||
headers = _ref[1]; | ||
// filter to keep only allowed Headers | ||
Object.keys(headers).forEach(function (header) { | ||
if (allowedHeaders.indexOf(header.toLowerCase()) === -1) { | ||
_this3.filerobot.log("[CompanionClient] excluding unallowed header " + header); | ||
delete headers[header]; | ||
} | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}).finally(_this2._decreaseRequestsNumber); | ||
}); | ||
return headers; | ||
}); | ||
}; | ||
} | ||
}, { | ||
key: "preflightAndHeaders", | ||
value: function preflightAndHeaders(path) { | ||
var _this3 = this; | ||
_proto.get = function get(path, skipPostResponse) { | ||
var _this4 = this; | ||
return Promise.all([this.preflight(path), this.headers()]).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
allowedHeaders = _ref2[0], | ||
headers = _ref2[1]; | ||
return new Promise(function (resolve, reject) { | ||
_this4.preflightAndHeaders(path).then(function (headers) { | ||
_this4._increaseRequestsNumber(); | ||
// filter to keep only allowed Headers | ||
Object.keys(headers).forEach(function (header) { | ||
if (allowedHeaders.indexOf(header.toLowerCase()) === -1) { | ||
_this3.filerobot.log("[CompanionClient] excluding unallowed header ".concat(header)); | ||
fetchWithNetworkError(_this4._getUrl(path), { | ||
method: 'get', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
signal: _this4._signal | ||
}).then(_this4._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this4._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not get " + _this4._getUrl(path) + ". " + err); | ||
reject(err); | ||
}).finally(_this4._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
}; | ||
delete headers[header]; | ||
} | ||
}); | ||
return headers; | ||
}); | ||
} | ||
}, { | ||
key: "get", | ||
value: function get(path, skipPostResponse) { | ||
var _this4 = this; | ||
_proto.post = function post(path, data, skipPostResponse) { | ||
var _this5 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this4.preflightAndHeaders(path).then(function (headers) { | ||
_this4._increaseRequestsNumber(); | ||
return new Promise(function (resolve, reject) { | ||
_this5.preflightAndHeaders(path).then(function (headers) { | ||
_this5._increaseRequestsNumber(); | ||
fetchWithNetworkError(_this4._getUrl(path), { | ||
method: 'get', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
signal: _this4._signal | ||
}).then(_this4._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this4._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not get ".concat(_this4._getUrl(path), ". ").concat(err)); | ||
reject(err); | ||
}).finally(_this4._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
} | ||
}, { | ||
key: "post", | ||
value: function post(path, data, skipPostResponse) { | ||
var _this5 = this; | ||
fetchWithNetworkError(_this5._getUrl(path), { | ||
method: 'post', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data), | ||
signal: _this5._signal | ||
}).then(_this5._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this5._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not post " + _this5._getUrl(path) + ". " + err); | ||
reject(err); | ||
}).finally(_this5._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
_this5.preflightAndHeaders(path).then(function (headers) { | ||
_this5._increaseRequestsNumber(); | ||
_proto.delete = function _delete(path, data, skipPostResponse) { | ||
var _this6 = this; | ||
fetchWithNetworkError(_this5._getUrl(path), { | ||
method: 'post', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data), | ||
signal: _this5._signal | ||
}).then(_this5._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this5._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not post ".concat(_this5._getUrl(path), ". ").concat(err)); | ||
reject(err); | ||
}).finally(_this5._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
} | ||
}, { | ||
key: "delete", | ||
value: function _delete(path, data, skipPostResponse) { | ||
var _this6 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this6.preflightAndHeaders(path).then(function (headers) { | ||
_this6._increaseRequestsNumber(); | ||
return new Promise(function (resolve, reject) { | ||
_this6.preflightAndHeaders(path).then(function (headers) { | ||
_this6._increaseRequestsNumber(); | ||
fetchWithNetworkError(_this6.hostname + "/" + path, { | ||
method: 'delete', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null, | ||
signal: _this6._signal | ||
}).then(_this6._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this6._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not delete " + _this6._getUrl(path) + ". " + err); | ||
reject(err); | ||
}).finally(_this6._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
}; | ||
_proto.cancel = function cancel() { | ||
this._controller.abort(); | ||
this._addCancelAbility(); | ||
}; | ||
_createClass(RequestClient, [{ | ||
key: "hostname", | ||
get: function get() { | ||
var _this$filerobot$getSt = this.filerobot.getState(), | ||
companion = _this$filerobot$getSt.companion; | ||
var host = COMPANION_URL; | ||
return stripSlash(companion && companion[host] ? companion[host] : host); | ||
fetchWithNetworkError("".concat(_this6.hostname, "/").concat(path), { | ||
method: 'delete', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null, | ||
signal: _this6._signal | ||
}).then(_this6._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this6._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not delete ".concat(_this6._getUrl(path), ". ").concat(err)); | ||
reject(err); | ||
}).finally(_this6._decreaseRequestsNumber); | ||
}).catch(reject); | ||
}); | ||
} | ||
}, { | ||
key: "defaultHeaders", | ||
get: function get() { | ||
return { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'Filerobot-Versions': "@filerobot/companion-client=" + RequestClient.VERSION | ||
}; | ||
key: "cancel", | ||
value: function cancel() { | ||
this._controller.abort(); | ||
this._addCancelAbility(); | ||
} | ||
@@ -268,3 +301,4 @@ }]); | ||
RequestClient.VERSION = require('../package.json').version; | ||
_defineProperty(RequestClient, "VERSION", require('../package.json').version); | ||
export default RequestClient; |
@@ -0,1 +1,7 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
import ee from 'namespace-emitter'; | ||
@@ -5,2 +11,4 @@ | ||
function FilerobotSocket(opts) { | ||
_classCallCheck(this, FilerobotSocket); | ||
this.opts = opts; | ||
@@ -22,71 +30,78 @@ this._queued = []; | ||
var _proto = FilerobotSocket.prototype; | ||
_createClass(FilerobotSocket, [{ | ||
key: "open", | ||
value: function open() { | ||
var _this = this; | ||
_proto.open = function open() { | ||
var _this = this; | ||
this.socket = new WebSocket(this.opts.target); | ||
this.socket = new WebSocket(this.opts.target); | ||
this.socket.onopen = function (e) { | ||
_this.isOpen = true; | ||
this.socket.onopen = function (e) { | ||
_this.isOpen = true; | ||
while (_this._queued.length > 0 && _this.isOpen) { | ||
var first = _this._queued[0]; | ||
while (_this._queued.length > 0 && _this.isOpen) { | ||
var first = _this._queued[0]; | ||
_this.send(first.action, first.payload); | ||
_this.send(first.action, first.payload); | ||
_this._queued = _this._queued.slice(1); | ||
} | ||
}; | ||
_this._queued = _this._queued.slice(1); | ||
this.socket.onclose = function (e) { | ||
_this.isOpen = false; | ||
}; | ||
this.socket.onmessage = this._handleMessage; | ||
} | ||
}, { | ||
key: "close", | ||
value: function close() { | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
}; | ||
} | ||
}, { | ||
key: "send", | ||
value: function send(action, payload) { | ||
// attach uuid | ||
if (!this.isOpen) { | ||
this._queued.push({ | ||
action: action, | ||
payload: payload | ||
}); | ||
this.socket.onclose = function (e) { | ||
_this.isOpen = false; | ||
}; | ||
return; | ||
} | ||
this.socket.onmessage = this._handleMessage; | ||
}; | ||
_proto.close = function close() { | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
}; | ||
_proto.send = function send(action, payload) { | ||
// attach uuid | ||
if (!this.isOpen) { | ||
this._queued.push({ | ||
this.socket.send(JSON.stringify({ | ||
action: action, | ||
payload: payload | ||
}); | ||
return; | ||
})); | ||
} | ||
this.socket.send(JSON.stringify({ | ||
action: action, | ||
payload: payload | ||
})); | ||
}; | ||
_proto.on = function on(action, handler) { | ||
this.emitter.on(action, handler); | ||
}; | ||
_proto.emit = function emit(action, payload) { | ||
this.emitter.emit(action, payload); | ||
}; | ||
_proto.once = function once(action, handler) { | ||
this.emitter.once(action, handler); | ||
}; | ||
_proto._handleMessage = function _handleMessage(e) { | ||
try { | ||
var message = JSON.parse(e.data); | ||
this.emit(message.action, message.payload); | ||
} catch (err) { | ||
console.log(err); | ||
}, { | ||
key: "on", | ||
value: function on(action, handler) { | ||
this.emitter.on(action, handler); | ||
} | ||
}; | ||
}, { | ||
key: "emit", | ||
value: function emit(action, payload) { | ||
this.emitter.emit(action, payload); | ||
} | ||
}, { | ||
key: "once", | ||
value: function once(action, handler) { | ||
this.emitter.once(action, handler); | ||
} | ||
}, { | ||
key: "_handleMessage", | ||
value: function _handleMessage(e) { | ||
try { | ||
var message = JSON.parse(e.data); | ||
this.emit(message.action, message.payload); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
}]); | ||
@@ -93,0 +108,0 @@ return FilerobotSocket; |
@@ -0,0 +0,0 @@ 'use strict'; |
{ | ||
"name": "@filerobot/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Filerobot plugins.", | ||
"version": "1.0.21", | ||
"version": "1.0.22", | ||
"license": "MIT", | ||
@@ -17,5 +17,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@filerobot/utils": "1.0.21", | ||
"@filerobot/utils": "1.0.22", | ||
"namespace-emitter": "^2.0.1" | ||
} | ||
} |
# @filerobot/companion-client | ||
in hardcoded process yet. | ||
The package used internally in Filerobot Widget while communicating with companion of backend for handling 3rd parties uploading. |
@@ -0,0 +0,0 @@ /** |
import { RequestClient, Provider, Socket } from '../' | ||
// TODO tests |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31774
592
4
+ Added@filerobot/icons@1.0.22(transitive)
+ Added@filerobot/utils@1.0.22(transitive)
- Removed@filerobot/icons@1.0.21(transitive)
- Removed@filerobot/utils@1.0.21(transitive)
Updated@filerobot/utils@1.0.22