Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@uppy/companion-client

Package Overview
Dependencies
Maintainers
5
Versions
89
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uppy/companion-client - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

27

lib/AuthError.js
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : 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); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
var AuthError = function (_Error) {
_inherits(AuthError, _Error);
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var AuthError =
/*#__PURE__*/
function (_Error) {
_inheritsLoose(AuthError, _Error);
function AuthError() {
_classCallCheck(this, AuthError);
var _this;
var _this = _possibleConstructorReturn(this, _Error.call(this, 'Authorization required'));
_this = _Error.call(this, 'Authorization required') || this;
_this.name = 'AuthError';

@@ -23,4 +32,4 @@ _this.isAuthError = true;

return AuthError;
}(Error);
}(_wrapNativeSuper(Error));
module.exports = AuthError;

@@ -7,3 +7,5 @@ 'use-strict';

var RequestClient = require('./RequestClient');
var Provider = require('./Provider');
var Socket = require('./Socket');

@@ -10,0 +12,0 @@

'use strict';
var _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; };
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
var RequestClient = require('./RequestClient');
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var RequestClient = require('./RequestClient');
var tokenStorage = require('./tokenStorage');

@@ -20,10 +17,11 @@

module.exports = function (_RequestClient) {
_inherits(Provider, _RequestClient);
module.exports =
/*#__PURE__*/
function (_RequestClient) {
_inheritsLoose(Provider, _RequestClient);
function Provider(uppy, opts) {
_classCallCheck(this, Provider);
var _this;
var _this = _possibleConstructorReturn(this, _RequestClient.call(this, uppy, opts));
_this = _RequestClient.call(this, uppy, opts) || this;
_this.provider = opts.provider;

@@ -34,7 +32,9 @@ _this.id = _this.provider;

_this.pluginId = _this.opts.pluginId;
_this.tokenKey = 'companion-' + _this.pluginId + '-auth-token';
_this.tokenKey = "companion-" + _this.pluginId + "-auth-token";
return _this;
}
Provider.prototype.headers = function headers() {
var _proto = Provider.prototype;
_proto.headers = function headers() {
var _this2 = this;

@@ -45,3 +45,5 @@

_this2.getAuthToken().then(function (token) {
resolve(_extends({}, headers, { 'uppy-auth-token': token }));
resolve(_extends({}, headers, {
'uppy-auth-token': token
}));
});

@@ -52,39 +54,41 @@ }).catch(reject);

Provider.prototype.onReceiveResponse = function onReceiveResponse(response) {
_proto.onReceiveResponse = function onReceiveResponse(response) {
response = _RequestClient.prototype.onReceiveResponse.call(this, response);
var authenticated = response.status !== 401;
this.uppy.getPlugin(this.pluginId).setPluginState({ authenticated: authenticated });
this.uppy.getPlugin(this.pluginId).setPluginState({
authenticated: authenticated
});
return response;
};
} // @todo(i.olarewaju) consider whether or not this method should be exposed
;
// @todo(i.olarewaju) consider whether or not this method should be exposed
Provider.prototype.setAuthToken = function setAuthToken(token) {
_proto.setAuthToken = function setAuthToken(token) {
return this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token);
};
Provider.prototype.getAuthToken = function getAuthToken() {
_proto.getAuthToken = function getAuthToken() {
return this.uppy.getPlugin(this.pluginId).storage.getItem(this.tokenKey);
};
Provider.prototype.authUrl = function authUrl() {
return this.hostname + '/' + this.id + '/connect';
_proto.authUrl = function authUrl() {
return this.hostname + "/" + this.id + "/connect";
};
Provider.prototype.fileUrl = function fileUrl(id) {
return this.hostname + '/' + this.id + '/get/' + id;
_proto.fileUrl = function fileUrl(id) {
return this.hostname + "/" + this.id + "/get/" + id;
};
Provider.prototype.list = function list(directory) {
return this.get(this.id + '/list/' + (directory || ''));
_proto.list = function list(directory) {
return this.get(this.id + "/list/" + (directory || ''));
};
Provider.prototype.logout = function logout() {
_proto.logout = function logout(redirect) {
var _this3 = this;
var redirect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : location.href;
if (redirect === void 0) {
redirect = location.href;
}
return new Promise(function (resolve, reject) {
_this3.get(_this3.id + '/logout?redirect=' + redirect).then(function (res) {
_this3.get(_this3.id + "/logout?redirect=" + redirect).then(function (res) {
_this3.uppy.getPlugin(_this3.pluginId).storage.removeItem(_this3.tokenKey).then(function () {

@@ -100,2 +104,3 @@ return resolve(res);

plugin.files = [];
if (defaultOpts) {

@@ -110,7 +115,8 @@ plugin.opts = _extends({}, defaultOpts, opts);

if (opts.companionAllowedHosts) {
var pattern = opts.companionAllowedHosts;
// validate companionAllowedHosts param
var pattern = opts.companionAllowedHosts; // validate companionAllowedHosts param
if (typeof pattern !== 'string' && !Array.isArray(pattern) && !(pattern instanceof RegExp)) {
throw new TypeError(plugin.id + ': the option "companionAllowedHosts" must be one of string, Array, RegExp');
throw new TypeError(plugin.id + ": the option \"companionAllowedHosts\" must be one of string, Array, RegExp");
}
plugin.opts.companionAllowedHosts = pattern;

@@ -120,3 +126,3 @@ } else {

if (/^(?!https?:\/\/).*$/i.test(opts.companionUrl)) {
plugin.opts.companionAllowedHosts = 'https://' + opts.companionUrl.replace(/^\/\//, '');
plugin.opts.companionAllowedHosts = "https://" + opts.companionUrl.replace(/^\/\//, '');
} else {

@@ -123,0 +129,0 @@ plugin.opts.companionAllowedHosts = opts.companionUrl;

'use strict';
var _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; };
var _class, _temp;
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
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 _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); } }
var AuthError = require('./AuthError');
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// Remove the trailing slash so we can always safely append /xyz.
var AuthError = require('./AuthError'); // Remove the trailing slash so we can always safely append /xyz.
function stripSlash(url) {

@@ -16,6 +18,6 @@ return url.replace(/\/$/, '');

module.exports = function () {
module.exports = (_temp = _class =
/*#__PURE__*/
function () {
function RequestClient(uppy, opts) {
_classCallCheck(this, RequestClient);
this.uppy = uppy;

@@ -26,7 +28,9 @@ this.opts = opts;

RequestClient.prototype.headers = function headers() {
var _proto = RequestClient.prototype;
_proto.headers = function headers() {
return Promise.resolve(_extends({}, this.defaultHeaders, this.opts.serverHeaders || {}));
};
RequestClient.prototype._getPostResponseFunc = function _getPostResponseFunc(skip) {
_proto._getPostResponseFunc = function _getPostResponseFunc(skip) {
var _this = this;

@@ -43,8 +47,8 @@

RequestClient.prototype.onReceiveResponse = function onReceiveResponse(response) {
_proto.onReceiveResponse = function onReceiveResponse(response) {
var state = this.uppy.getState();
var companion = state.companion || {};
var host = this.opts.companionUrl;
var headers = response.headers;
// Store the self-identified domain name for the Companion instance we just hit.
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]) {

@@ -57,13 +61,15 @@ var _extends2;

}
return response;
};
RequestClient.prototype._getUrl = function _getUrl(url) {
_proto._getUrl = function _getUrl(url) {
if (/^(https?:|)\/\//.test(url)) {
return url;
}
return this.hostname + '/' + url;
return this.hostname + "/" + url;
};
RequestClient.prototype._json = function _json(res) {
_proto._json = function _json(res) {
if (res.status === 401) {

@@ -74,8 +80,9 @@ throw new AuthError();

if (res.status < 200 || res.status > 300) {
throw new Error('Failed request to ' + res.url + '. ' + res.statusText);
throw new Error("Failed request to " + res.url + ". " + res.statusText);
}
return res.json();
};
RequestClient.prototype.get = function get(path, skipPostResponse) {
_proto.get = function get(path, skipPostResponse) {
var _this2 = this;

@@ -92,3 +99,3 @@

}).catch(function (err) {
err = err.isAuthError ? err : new Error('Could not get ' + _this2._getUrl(path) + '. ' + err);
err = err.isAuthError ? err : new Error("Could not get " + _this2._getUrl(path) + ". " + err);
reject(err);

@@ -100,3 +107,3 @@ });

RequestClient.prototype.post = function post(path, data, skipPostResponse) {
_proto.post = function post(path, data, skipPostResponse) {
var _this3 = this;

@@ -114,3 +121,3 @@

}).catch(function (err) {
err = err.isAuthError ? err : new Error('Could not post ' + _this3._getUrl(path) + '. ' + err);
err = err.isAuthError ? err : new Error("Could not post " + _this3._getUrl(path) + ". " + err);
reject(err);

@@ -122,3 +129,3 @@ });

RequestClient.prototype.delete = function _delete(path, data, skipPostResponse) {
_proto.delete = function _delete(path, data, skipPostResponse) {
var _this4 = this;

@@ -128,3 +135,3 @@

_this4.headers().then(function (headers) {
fetch(_this4.hostname + '/' + path, {
fetch(_this4.hostname + "/" + path, {
method: 'delete',

@@ -137,3 +144,3 @@ headers: headers,

}).catch(function (err) {
err = err.isAuthError ? err : new Error('Could not delete ' + _this4._getUrl(path) + '. ' + err);
err = err.isAuthError ? err : new Error("Could not delete " + _this4._getUrl(path) + ". " + err);
reject(err);

@@ -146,6 +153,6 @@ });

_createClass(RequestClient, [{
key: 'hostname',
key: "hostname",
get: function get() {
var _uppy$getState = this.uppy.getState(),
companion = _uppy$getState.companion;
var _this$uppy$getState = this.uppy.getState(),
companion = _this$uppy$getState.companion;

@@ -156,3 +163,3 @@ var host = this.opts.companionUrl;

}, {
key: 'defaultHeaders',
key: "defaultHeaders",
get: function get() {

@@ -167,2 +174,2 @@ return {

return RequestClient;
}();
}(), _class.VERSION = "1.1.0", _temp);

@@ -1,11 +0,9 @@

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ee = require('namespace-emitter');
module.exports = function () {
module.exports =
/*#__PURE__*/
function () {
function UppySocket(opts) {
var _this = this;
_classCallCheck(this, UppySocket);
this.queued = [];

@@ -21,3 +19,5 @@ this.isOpen = false;

var first = _this.queued[0];
_this.send(first.action, first.payload);
_this.queued = _this.queued.slice(1);

@@ -32,5 +32,3 @@ }

this._handleMessage = this._handleMessage.bind(this);
this.socket.onmessage = this._handleMessage;
this.close = this.close.bind(this);

@@ -43,11 +41,15 @@ this.emit = this.emit.bind(this);

UppySocket.prototype.close = function close() {
var _proto = UppySocket.prototype;
_proto.close = function close() {
return this.socket.close();
};
UppySocket.prototype.send = function send(action, payload) {
_proto.send = function send(action, payload) {
// attach uuid
if (!this.isOpen) {
this.queued.push({ action: action, payload: payload });
this.queued.push({
action: action,
payload: payload
});
return;

@@ -62,15 +64,15 @@ }

UppySocket.prototype.on = function on(action, handler) {
_proto.on = function on(action, handler) {
this.emitter.on(action, handler);
};
UppySocket.prototype.emit = function emit(action, payload) {
_proto.emit = function emit(action, payload) {
this.emitter.emit(action, payload);
};
UppySocket.prototype.once = function once(action, handler) {
_proto.once = function once(action, handler) {
this.emitter.once(action, handler);
};
UppySocket.prototype._handleMessage = function _handleMessage(e) {
_proto._handleMessage = function _handleMessage(e) {
try {

@@ -77,0 +79,0 @@ var message = JSON.parse(e.data);

{
"name": "@uppy/companion-client",
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",

@@ -26,3 +26,3 @@ "main": "lib/index.js",

},
"gitHead": "1ad322497ff694f06f328c3cccbe5cf0922c5bed"
"gitHead": "28d235fe2fb57d87a399c20883fd6590aa49f4f4"
}

@@ -11,2 +11,4 @@ 'use strict'

module.exports = class RequestClient {
static VERSION = require('../package.json').version
constructor (uppy, opts) {

@@ -13,0 +15,0 @@ this.uppy = uppy

@@ -43,3 +43,3 @@ const ee = require('namespace-emitter')

if (!this.isOpen) {
this.queued.push({action, payload})
this.queued.push({ action, payload })
return

@@ -46,0 +46,0 @@ }

@@ -1,3 +0,1 @@

import Uppy = require('@uppy/core');
export interface RequestClientOptions {

@@ -9,3 +7,3 @@ companionUrl: string;

export class RequestClient {
constructor (uppy: Uppy.Uppy, opts: RequestClientOptions);
constructor (uppy: any, opts: RequestClientOptions);
get (path: string): Promise<any>;

@@ -23,3 +21,3 @@ post (path: string, data: object): Promise<any>;

export class Provider extends RequestClient {
constructor (uppy: Uppy.Uppy, opts: ProviderOptions);
constructor (uppy: any, opts: ProviderOptions);
checkAuth (): Promise<boolean>;

@@ -30,3 +28,3 @@ authUrl (): string;

logout (redirect?: string): Promise<any>;
static initPlugin(plugin: Uppy.Plugin, opts: object, defaultOpts?: object): void;
static initPlugin(plugin: any, opts: object, defaultOpts?: object): void;
}

@@ -33,0 +31,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc