@uppy/companion-client
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -83,11 +83,7 @@ 'use strict'; | ||
_proto.logout = function logout(redirect) { | ||
_proto.logout = function logout() { | ||
var _this3 = this; | ||
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").then(function (res) { | ||
_this3.uppy.getPlugin(_this3.pluginId).storage.removeItem(_this3.tokenKey).then(function () { | ||
@@ -94,0 +90,0 @@ return resolve(res); |
@@ -32,3 +32,4 @@ 'use strict'; | ||
_proto.headers = function headers() { | ||
return Promise.resolve(_extends({}, this.defaultHeaders, this.opts.serverHeaders || {})); | ||
var userHeaders = this.opts.companionHeaders || this.opts.serverHeaders || {}; | ||
return Promise.resolve(_extends({}, this.defaultHeaders, {}, userHeaders)); | ||
}; | ||
@@ -218,2 +219,2 @@ | ||
return RequestClient; | ||
}(), _class.VERSION = "1.3.0", _temp); | ||
}(), _class.VERSION = "1.4.0", _temp); |
@@ -7,18 +7,34 @@ var ee = require('namespace-emitter'); | ||
function UppySocket(opts) { | ||
var _this = this; | ||
this.queued = []; | ||
this.opts = opts; | ||
this._queued = []; | ||
this.isOpen = false; | ||
this.socket = new WebSocket(opts.target); | ||
this.emitter = ee(); | ||
this._handleMessage = this._handleMessage.bind(this); | ||
this.close = this.close.bind(this); | ||
this.emit = this.emit.bind(this); | ||
this.on = this.on.bind(this); | ||
this.once = this.once.bind(this); | ||
this.send = this.send.bind(this); | ||
if (!opts || opts.autoOpen !== false) { | ||
this.open(); | ||
} | ||
} | ||
var _proto = UppySocket.prototype; | ||
_proto.open = function open() { | ||
var _this = this; | ||
this.socket = new WebSocket(this.opts.target); | ||
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.queued = _this.queued.slice(1); | ||
_this._queued = _this._queued.slice(1); | ||
} | ||
@@ -31,15 +47,9 @@ }; | ||
this._handleMessage = this._handleMessage.bind(this); | ||
this.socket.onmessage = this._handleMessage; | ||
this.close = this.close.bind(this); | ||
this.emit = this.emit.bind(this); | ||
this.on = this.on.bind(this); | ||
this.once = this.once.bind(this); | ||
this.send = this.send.bind(this); | ||
} | ||
}; | ||
var _proto = UppySocket.prototype; | ||
_proto.close = function close() { | ||
return this.socket.close(); | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
}; | ||
@@ -50,6 +60,7 @@ | ||
if (!this.isOpen) { | ||
this.queued.push({ | ||
this._queued.push({ | ||
action: action, | ||
payload: payload | ||
}); | ||
return; | ||
@@ -56,0 +67,0 @@ } |
{ | ||
"name": "@uppy/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "056a7114a15fc7480a4014342d7f2c19305dc96c" | ||
"gitHead": "4e32e61d7c7821ca5d8641a3df741487ea27f0bb" | ||
} |
@@ -61,5 +61,5 @@ 'use strict' | ||
logout (redirect = location.href) { | ||
logout () { | ||
return new Promise((resolve, reject) => { | ||
this.get(`${this.id}/logout?redirect=${redirect}`) | ||
this.get(`${this.id}/logout`) | ||
.then((res) => { | ||
@@ -66,0 +66,0 @@ this.uppy.getPlugin(this.pluginId).storage.removeItem(this.tokenKey) |
@@ -36,5 +36,7 @@ 'use strict' | ||
headers () { | ||
return Promise.resolve( | ||
Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {}) | ||
) | ||
const userHeaders = this.opts.companionHeaders || this.opts.serverHeaders || {} | ||
return Promise.resolve({ | ||
...this.defaultHeaders, | ||
...userHeaders | ||
}) | ||
} | ||
@@ -41,0 +43,0 @@ |
@@ -5,14 +5,30 @@ const ee = require('namespace-emitter') | ||
constructor (opts) { | ||
this.queued = [] | ||
this.opts = opts | ||
this._queued = [] | ||
this.isOpen = false | ||
this.socket = new WebSocket(opts.target) | ||
this.emitter = ee() | ||
this._handleMessage = this._handleMessage.bind(this) | ||
this.close = this.close.bind(this) | ||
this.emit = this.emit.bind(this) | ||
this.on = this.on.bind(this) | ||
this.once = this.once.bind(this) | ||
this.send = this.send.bind(this) | ||
if (!opts || opts.autoOpen !== false) { | ||
this.open() | ||
} | ||
} | ||
open () { | ||
this.socket = new WebSocket(this.opts.target) | ||
this.socket.onopen = (e) => { | ||
this.isOpen = true | ||
while (this.queued.length > 0 && this.isOpen) { | ||
const first = this.queued[0] | ||
while (this._queued.length > 0 && this.isOpen) { | ||
const first = this._queued[0] | ||
this.send(first.action, first.payload) | ||
this.queued = this.queued.slice(1) | ||
this._queued = this._queued.slice(1) | ||
} | ||
@@ -25,15 +41,9 @@ } | ||
this._handleMessage = this._handleMessage.bind(this) | ||
this.socket.onmessage = this._handleMessage | ||
this.close = this.close.bind(this) | ||
this.emit = this.emit.bind(this) | ||
this.on = this.on.bind(this) | ||
this.once = this.once.bind(this) | ||
this.send = this.send.bind(this) | ||
} | ||
close () { | ||
return this.socket.close() | ||
if (this.socket) { | ||
this.socket.close() | ||
} | ||
} | ||
@@ -45,3 +55,3 @@ | ||
if (!this.isOpen) { | ||
this.queued.push({ action, payload }) | ||
this._queued.push({ action, payload }) | ||
return | ||
@@ -48,0 +58,0 @@ } |
@@ -69,3 +69,3 @@ const UppySocket = require('./Socket') | ||
uppySocket.send('bar', 'boo') | ||
expect(uppySocket.queued).toEqual([{ action: 'bar', payload: 'boo' }]) | ||
expect(uppySocket._queued).toEqual([{ action: 'bar', payload: 'boo' }]) | ||
expect(webSocketSendSpy.mock.calls.length).toEqual(0) | ||
@@ -80,3 +80,3 @@ }) | ||
uppySocket.send('moo', 'baa') | ||
expect(uppySocket.queued).toEqual([ | ||
expect(uppySocket._queued).toEqual([ | ||
{ action: 'bar', payload: 'boo' }, | ||
@@ -89,3 +89,3 @@ { action: 'moo', payload: 'baa' } | ||
expect(uppySocket.queued).toEqual([]) | ||
expect(uppySocket._queued).toEqual([]) | ||
expect(webSocketSendSpy.mock.calls.length).toEqual(2) | ||
@@ -105,7 +105,7 @@ expect(webSocketSendSpy.mock.calls[0]).toEqual([ | ||
uppySocket.send('bar', 'boo') | ||
expect(uppySocket.queued).toEqual([]) | ||
expect(uppySocket._queued).toEqual([]) | ||
webSocketInstance.triggerClose() | ||
uppySocket.send('bar', 'boo') | ||
expect(uppySocket.queued).toEqual([{ action: 'bar', payload: 'boo' }]) | ||
expect(uppySocket._queued).toEqual([{ action: 'bar', payload: 'boo' }]) | ||
}) | ||
@@ -112,0 +112,0 @@ |
export interface RequestClientOptions { | ||
companionUrl: string; | ||
companionHeaders?: object; | ||
/** | ||
* Deprecated, use `companionHeaders` instead. | ||
*/ | ||
serverHeaders?: object; | ||
@@ -31,6 +35,10 @@ } | ||
target: string; | ||
autoOpen?: boolean; | ||
} | ||
export class Socket { | ||
isOpen: boolean; | ||
constructor (opts: SocketOptions); | ||
open (): void; | ||
close (): void; | ||
@@ -37,0 +45,0 @@ send (action: string, payload: any): void; |
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
39270
961