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

pusher-js

Package Overview
Dependencies
Maintainers
3
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pusher-js - npm Package Compare versions

Comparing version 4.3.1 to 4.4.0

.npmignore

6

CHANGELOG.md
# Changelog
## 4.4.0 (2019-02-14)
[NEW] Callbacks bound to client events on presence channels will be called with an extra argument containing the `user_id` of the message sender
[NEW] Warn when trying to trigger client-events to a channel that isn't subscribed
## 4.3.1 (2018-09-03)

@@ -4,0 +10,0 @@

2

package.json
{
"name": "pusher-js",
"version": "4.3.1",
"version": "4.4.0",
"description": "Pusher JavaScript library for browsers, React Native, NodeJS and web workers",

@@ -5,0 +5,0 @@ "main": "dist/node/pusher.js",

@@ -27,2 +27,3 @@ # Pusher Javascript Client

* [Subscribing to Channels (Public and Private)](https://github.com/pusher/pusher-js#subscribing-to-channels)
* [Accessing Channels](https://github.com/pusher/pusher-js#accessing-channels)
* [Binding to Events](https://github.com/pusher/pusher-js#binding-to-events)

@@ -64,3 +65,3 @@ * [Default events](https://github.com/pusher/pusher-js#default-events)

```html
<script src="https://js.pusher.com/4.2/pusher.min.js"></script>
<script src="https://js.pusher.com/4.4/pusher.min.js"></script>
```

@@ -100,7 +101,7 @@

### Web Workers
(`pusher-js`'s Web Workers implementation is currently not compatible with Internet Explorer)
You can import the worker script (`pusher.worker.js`, not `pusher.js`) from the CDN:
```javascript
importScripts('https://js.pusher.com/4.2/pusher.worker.min.js');
importScripts('https://js.pusher.com/4.4/pusher.worker.min.js');
```

@@ -372,2 +373,4 @@

## Accessing Channels
It is possible to access channels by name, through the `channel` function:

@@ -421,2 +424,10 @@

For client-events on presence channels, bound callbacks will be called with an additional argument. This argument is an object containing the `user_id` of the user who triggered the event
```
presenceChannel.bind('client-message', function (data, metadata) {
console.log('received data from', metadata.user_id, ':', data);
});
```
Unsubscribe behaviour varies depending on which parameters you provide it with. For example:

@@ -504,13 +515,13 @@

First, make sure you expose all files from the `dist` directory. They need to be in a directory with named after the version number. For example, if you're hosting version 4.2.0 under `http://example.com/pusher-js` (and https for SSL), files should be accessible under following URL's:
First, make sure you expose all files from the `dist` directory. They need to be in a directory with named after the version number. For example, if you're hosting version 4.4.0 under `http://example.com/pusher-js` (and https for SSL), files should be accessible under following URL's:
http://example.com/pusher-js/4.2.0/pusher.js
http://example.com/pusher-js/4.2.0/json2.js
http://example.com/pusher-js/4.2.0/sockjs.js
http://example.com/pusher-js/4.4.0/pusher.js
http://example.com/pusher-js/4.4.0/json2.js
http://example.com/pusher-js/4.4.0/sockjs.js
Minified files should have `.min` in their names, as in the `dist/web` directory:
http://example.com/pusher-js/4.2.0/pusher.min.js
http://example.com/pusher-js/4.2.0/json2.min.js
http://example.com/pusher-js/4.2.0/sockjs.min.js
http://example.com/pusher-js/4.4.0/pusher.min.js
http://example.com/pusher-js/4.4.0/json2.min.js
http://example.com/pusher-js/4.4.0/sockjs.min.js

@@ -517,0 +528,0 @@ ## SockJS compatibility

@@ -38,2 +38,5 @@ var Errors = require('core/errors');

describe("#trigger", function() {
beforeEach(function() {
channel.subscribed = true;
});
it("should raise an exception if the event name does not start with client-", function() {

@@ -64,3 +67,5 @@ expect(function() {

it("should set subscribed to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded"
});
channel.disconnect();

@@ -85,3 +90,5 @@ expect(channel.subscribed).toEqual(false);

channel.handleEvent("pusher_internal:test");
channel.handleEvent({
event: "pusher_internal:test"
});

@@ -96,3 +103,6 @@ expect(callback).not.toHaveBeenCalled();

channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -103,3 +113,6 @@ expect(callback).toHaveBeenCalledWith("123");

it("should set #subscribed to true", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -110,3 +123,6 @@ expect(channel.subscribed).toEqual(true);

it("should set #subscriptionPending to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -123,3 +139,6 @@ expect(channel.subscriptionPending).toEqual(false);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -131,3 +150,6 @@ expect(callback).not.toHaveBeenCalled();

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -139,3 +161,6 @@ expect(channel.subscribed).toEqual(true);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -149,3 +174,6 @@ expect(channel.subscriptionPending).toEqual(false);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -161,5 +189,8 @@ expect(pusher.unsubscribe).toHaveBeenCalledWith(channel.name);

channel.handleEvent("something", 9);
channel.handleEvent({
event: "something",
data: 9
});
expect(callback).toHaveBeenCalledWith(9);
expect(callback).toHaveBeenCalledWith(9, {});
});

@@ -171,5 +202,8 @@

channel.handleEvent("toString", "works");
channel.handleEvent({
event: "toString",
data: "works"
});
expect(callback).toHaveBeenCalledWith("works");
expect(callback).toHaveBeenCalledWith("works", {});
});

@@ -209,3 +243,3 @@ });

"pusher:subscribe",
{ auth: "one", channel_data: "two", channel: "test" }
{ auth: "one", channel_data: "two", channel: "test" },
);

@@ -212,0 +246,0 @@ });

@@ -116,3 +116,5 @@ const Errors = require("core/errors");

it("should set subscribed to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded"
});
channel.disconnect();

@@ -129,3 +131,5 @@ expect(channel.subscribed).toEqual(false);

channel.handleEvent("pusher_internal:test");
channel.handleEvent({
event: "pusher_internal:test"
});

@@ -139,3 +143,6 @@ expect(callback).not.toHaveBeenCalled();

channel.bind("pusher:subscription_succeeded", callback);
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(callback).toHaveBeenCalledWith("123");

@@ -145,3 +152,6 @@ });

it("should set #subscribed to true", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(channel.subscribed).toEqual(true);

@@ -151,3 +161,6 @@ });

it("should set #subscriptionPending to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(channel.subscriptionPending).toEqual(false);

@@ -162,3 +175,6 @@ });

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(callback).not.toHaveBeenCalled();

@@ -169,3 +185,6 @@ });

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(channel.subscribed).toEqual(true);

@@ -176,3 +195,6 @@ });

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(channel.subscriptionPending).toEqual(false);

@@ -184,3 +206,6 @@ });

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});
expect(pusher.unsubscribe).toHaveBeenCalledWith(channel.name);

@@ -209,3 +234,6 @@ });

channel.bind("something", boundCallback);
channel.handleEvent("something", encryptedPayload);
channel.handleEvent({
event: "something",
data: encryptedPayload
});
expect(boundCallback).toHaveBeenCalledWith(payload);

@@ -217,4 +245,7 @@ });

channel.bind("pusher:subscription_error", boundCallback);
channel.handleEvent("pusher:subscription_error", payload);
expect(boundCallback).toHaveBeenCalledWith(payload);
channel.handleEvent({
event: "pusher:subscription_error",
data: payload
});
expect(boundCallback).toHaveBeenCalledWith(payload, {});
});

@@ -247,3 +278,6 @@

channel.bind("something", boundCallback);
channel.handleEvent("something", encryptedPayload);
channel.handleEvent({
event: "something",
data: encryptedPayload
});
authorizer._callback(false, {

@@ -261,3 +295,6 @@ shared_secret: newSecretBase64,

spyOn(Logger, "warn");
channel.handleEvent("something", encryptedPayload);
channel.handleEvent({
event: "something",
data: encryptedPayload
});
authorizer._callback(false, {

@@ -278,3 +315,6 @@ shared_secret: newSecretBase64,

spyOn(Logger, "warn");
channel.handleEvent("something", encryptedPayload);
channel.handleEvent({
event: "something",
data: encryptedPayload
});
authorizer._callback(true, "ERROR");

@@ -281,0 +321,0 @@ expect(Logger.warn).toHaveBeenCalledWith(

@@ -92,2 +92,5 @@ var PresenceChannel = require('core/channels/presence_channel').default;

describe("#trigger", function() {
beforeEach(function() {
channel.subscribed = true;
});
it("should raise an exception if the event name does not start with client-", function() {

@@ -135,3 +138,3 @@ expect(function() {

channel.handleEvent("pusher_internal:test");
channel.handleEvent({event: "pusher_internal:test"});

@@ -146,6 +149,9 @@ expect(callback).not.toHaveBeenCalled();

channel.handleEvent("pusher_internal:subscription_succeeded", {
presence: {
hash: { "U": "me" },
count: 1
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: {
presence: {
hash: { "U": "me" },
count: 1
}
}

@@ -158,6 +164,9 @@ });

it("should set #subscribed to true", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", {
presence: {
hash: { "U": "me" },
count: 1
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: {
presence: {
hash: { "U": "me" },
count: 1
}
}

@@ -170,6 +179,9 @@ });

it("should set #subscriptionPending to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", {
presence: {
hash: { "U": "me" },
count: 1
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: {
presence: {
hash: { "U": "me" },
count: 1
}
}

@@ -188,3 +200,6 @@ });

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -196,3 +211,6 @@ expect(callback).not.toHaveBeenCalled();

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -204,3 +222,6 @@ expect(channel.subscribed).toEqual(true);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -214,3 +235,6 @@ expect(channel.subscriptionPending).toEqual(false);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -226,6 +250,20 @@ expect(pusher.unsubscribe).toHaveBeenCalledWith(channel.name);

channel.handleEvent("something", 9);
channel.handleEvent({
event: "something",
data: 9
});
expect(callback).toHaveBeenCalledWith(9);
expect(callback).toHaveBeenCalledWith(9, {});
});
it("should emit metadata with user_id", function() {
var callback = jasmine.createSpy("callback");
channel.bind("client-something", callback);
channel.handleEvent({
event: "client-something",
data: 9,
user_id: "abc-def"
});
expect(callback).toHaveBeenCalledWith(9, {user_id: "abc-def"});
});
});

@@ -240,10 +278,13 @@ });

channel.bind("pusher:subscription_succeeded", callback);
channel.handleEvent("pusher_internal:subscription_succeeded", {
presence: {
hash: {
"A": "user A",
"B": "user B",
"U": "me"
},
count: 3
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: {
presence: {
hash: {
"A": "user A",
"B": "user B",
"U": "me"
},
count: 3
}
}

@@ -278,5 +319,8 @@ });

it("should add a new member", function() {
channel.handleEvent("pusher_internal:member_added", {
user_id: "C",
user_info: "user C"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "C",
user_info: "user C"
}
});

@@ -288,5 +332,8 @@

it("should increment member count after adding a new member", function() {
channel.handleEvent("pusher_internal:member_added", {
user_id: "C",
user_info: "user C"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "C",
user_info: "user C"
}
});

@@ -301,5 +348,8 @@

channel.handleEvent("pusher_internal:member_added", {
user_id: "C",
user_info: "user C"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "C",
user_info: "user C"
}
});

@@ -311,5 +361,8 @@

it("should update an existing member", function() {
channel.handleEvent("pusher_internal:member_added", {
user_id: "B",
user_info: "updated B"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "B",
user_info: "updated B"
}
});

@@ -321,5 +374,8 @@

it("should not increment member count after updating a member", function() {
channel.handleEvent("pusher_internal:member_added", {
user_id: "B",
user_info: "updated B"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "B",
user_info: "updated B"
}
});

@@ -334,5 +390,8 @@

channel.handleEvent("pusher_internal:member_added", {
user_id: "B",
user_info: "updated B"
channel.handleEvent({
event: "pusher_internal:member_added",
data: {
user_id: "B",
user_info: "updated B"
}
});

@@ -346,4 +405,7 @@

it("should remove an existing member", function() {
channel.handleEvent("pusher_internal:member_removed", {
user_id: "B"
channel.handleEvent({
event: "pusher_internal:member_removed",
data: {
user_id: "B"
}
});

@@ -358,4 +420,7 @@

channel.handleEvent("pusher_internal:member_removed", {
user_id: "B"
channel.handleEvent({
event: "pusher_internal:member_removed",
data: {
user_id: "B"
}
});

@@ -367,4 +432,7 @@

it("should decrement member count after removing a member", function() {
channel.handleEvent("pusher_internal:member_removed", {
user_id: "B"
channel.handleEvent({
event: "pusher_internal:member_removed",
data: {
user_id: "B"
}
});

@@ -379,4 +447,7 @@

channel.handleEvent("pusher_internal:member_removed", {
user_id: "C"
channel.handleEvent({
event: "pusher_internal:member_removed",
data: {
user_id: "C"
},
});

@@ -388,4 +459,7 @@

it("should not decrement member count if member was not removed", function() {
channel.handleEvent("pusher_internal:member_removed", {
user_id: "C"
channel.handleEvent({
event: "pusher_internal:member_removed",
data: {
user_id: "C"
}
});

@@ -392,0 +466,0 @@

@@ -79,2 +79,5 @@ var Authorizer = require('core/auth/pusher_authorizer').default;

describe("#trigger", function() {
beforeEach(function() {
channel.subscribed = true;
})
it("should raise an exception if the event name does not start with client-", function() {

@@ -105,3 +108,5 @@ expect(function() {

it("should set subscribed to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded"
});
channel.disconnect();

@@ -118,3 +123,5 @@ expect(channel.subscribed).toEqual(false);

channel.handleEvent("pusher_internal:test");
channel.handleEvent({
event: "pusher_internal:test"
});

@@ -129,3 +136,6 @@ expect(callback).not.toHaveBeenCalled();

channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -136,3 +146,6 @@ expect(callback).toHaveBeenCalledWith("123");

it("should set #subscribed to true", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -143,3 +156,6 @@ expect(channel.subscribed).toEqual(true);

it("should set #subscriptionPending to false", function() {
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -156,3 +172,6 @@ expect(channel.subscriptionPending).toEqual(false);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -164,3 +183,6 @@ expect(callback).not.toHaveBeenCalled();

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -172,3 +194,6 @@ expect(channel.subscribed).toEqual(true);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -182,3 +207,6 @@ expect(channel.subscriptionPending).toEqual(false);

channel.cancelSubscription();
channel.handleEvent("pusher_internal:subscription_succeeded", "123");
channel.handleEvent({
event: "pusher_internal:subscription_succeeded",
data: "123"
});

@@ -194,5 +222,8 @@ expect(pusher.unsubscribe).toHaveBeenCalledWith(channel.name);

channel.handleEvent("something", 9);
channel.handleEvent({
event: "something",
data: 9
});
expect(callback).toHaveBeenCalledWith(9);
expect(callback).toHaveBeenCalledWith(9, {});
});

@@ -199,0 +230,0 @@ });

@@ -117,3 +117,3 @@ var Connection = require('core/connection/connection').default;

event: "random",
data: { foo: "bar" }
data: { foo: "bar" },
});

@@ -120,0 +120,0 @@ });

@@ -17,3 +17,3 @@ var Protocol = require('core/connection/protocol/protocol');

foo: "bar"
}
},
});

@@ -32,3 +32,3 @@ });

event: "raw",
data: "just a string"
data: "just a string",
});

@@ -50,6 +50,21 @@ });

z: 1
}
},
});
});
it("should parse user_id message with user_id", function() {
var message = {
data: JSON.stringify({
event: "raw",
data: "just a string",
user_id: "abc-def",
})
};
expect(Protocol.decodeMessage(message)).toEqual({
event: "raw",
data: "just a string",
user_id: "abc-def"
});
});
it("should throw an exception if message is malformed", function() {

@@ -56,0 +71,0 @@ var message = {

@@ -309,2 +309,10 @@ var EventsDispatcher = require('core/events/dispatcher').default;

});
it("should call listener with provided metadata", function() {
var callback = jasmine.createSpy("callback");
dispatcher.bind("some-event", callback);
dispatcher.emit("some-event", {data: 1}, {user_id: "123-abc"})
expect(callback).toHaveBeenCalledWith({data: 1}, {user_id: "123-abc"})
})
});

@@ -311,0 +319,0 @@

@@ -379,5 +379,7 @@ var TestEnv = require('testenv');

});
expect(channel.handleEvent).toHaveBeenCalledWith(
"event", { key: "value" }
);
expect(channel.handleEvent).toHaveBeenCalledWith({
channel: "chan",
event: "event",
data: { key: "value" },
});
});

@@ -384,0 +386,0 @@

@@ -542,3 +542,3 @@ var TestEnv = require('testenv');

expect(onClosed).toHaveBeenCalledWith(undefined);
expect(onClosed).toHaveBeenCalled();
expect(onClosed.calls.length).toEqual(1);

@@ -545,0 +545,0 @@ });

@@ -5,2 +5,5 @@ import {default as EventsDispatcher} from '../events/dispatcher';

import Pusher from '../pusher';
import {PusherEvent} from '../connection/protocol/message-types';
import Metadata from './metadata'
import UrlStore from '../utils/url_store';

@@ -50,2 +53,8 @@ /** Provides base public channel interface with an event emitter.

}
if (!this.subscribed) {
var suffix = UrlStore.buildLogSuffix("triggeringClientEvents");
Logger.warn(
`Client event triggered before channel 'subscription_succeeded' event . ${suffix}`
);
}
return this.pusher.send_event(event, data, this.name);

@@ -60,20 +69,24 @@ }

/** Handles an event. For internal use only.
/** Handles a PusherEvent. For internal use only.
*
* @param {String} event
* @param {*} data
* @param {PusherEvent} event
*/
handleEvent(event : string, data : any) {
if (event.indexOf("pusher_internal:") === 0) {
if (event === "pusher_internal:subscription_succeeded") {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
} else {
this.emit("pusher:subscription_succeeded", data);
}
}
handleEvent(event: PusherEvent) {
var eventName = event.event;
var data = event.data;
if (eventName === "pusher_internal:subscription_succeeded") {
this.handleSubscriptionSucceededEvent(event);
} else if (eventName.indexOf("pusher_internal:") !== 0) {
var metadata: Metadata = {}
this.emit(eventName, data, metadata);
}
}
handleSubscriptionSucceededEvent(event: PusherEvent) {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
} else {
this.emit(event, data);
this.emit("pusher:subscription_succeeded", event.data);
}

@@ -89,3 +102,3 @@ }

if (error) {
this.handleEvent('pusher:subscription_error', data);
this.emit('pusher:subscription_error', data)
} else {

@@ -92,0 +105,0 @@ this.pusher.send_event('pusher:subscribe', {

@@ -10,2 +10,3 @@ import PrivateChannel from "./private_channel";

import Dispatcher from "../events/dispatcher";
import {PusherEvent} from '../connection/protocol/message-types';

@@ -52,11 +53,12 @@ /** Extends private channels to provide encrypted channel interface.

*
* @param {String} event
* @param {*} data
* @param {PusherEvent} event
*/
handleEvent(event: string, data: any) {
if (event.indexOf("pusher_internal:") === 0 || event.indexOf("pusher:") === 0) {
super.handleEvent(event, data);
handleEvent(event: PusherEvent) {
var eventName = event.event;
var data = event.data;
if (eventName.indexOf("pusher_internal:") === 0 || eventName.indexOf("pusher:") === 0) {
super.handleEvent(event);
return
}
this.handleEncryptedEvent(event, data)
this.handleEncryptedEvent(eventName, data)
}

@@ -63,0 +65,0 @@

@@ -6,2 +6,4 @@ import PrivateChannel from './private_channel';

import UrlStore from 'core/utils/url_store';
import {PusherEvent} from '../connection/protocol/message-types';
import Metadata from './metadata'

@@ -47,17 +49,24 @@ export default class PresenceChannel extends PrivateChannel {

*
* @param {String} event
* @param {*} data
* @param {PusherEvent} event
*/
handleEvent(event : string, data : any) {
switch (event) {
handleEvent(event: PusherEvent) {
var eventName = event.event;
if (eventName.indexOf("pusher_internal:") === 0) {
this.handleInternalEvent(event)
} else {
var data = event.data;
var metadata: Metadata = {};
if (event.user_id) {
metadata.user_id = event.user_id
}
this.emit(eventName, data, metadata);
}
}
handleInternalEvent(event: PusherEvent) {
var eventName = event.event;
var data = event.data;
switch (eventName) {
case "pusher_internal:subscription_succeeded":
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
} else {
this.members.onSubscription(data);
this.emit("pusher:subscription_succeeded", this.members);
}
break;
this.handleSubscriptionSucceededEvent(event)
break
case "pusher_internal:member_added":

@@ -73,7 +82,16 @@ var addedMember = this.members.addMember(data);

break;
default:
PrivateChannel.prototype.handleEvent.call(this, event, data);
}
}
handleSubscriptionSucceededEvent(event: PusherEvent) {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
} else {
this.members.onSubscription(event.data);
this.emit("pusher:subscription_succeeded", this.members);
}
}
/** Resets the channel state, including members map. For internal use only. */

@@ -80,0 +98,0 @@ disconnect() {

import * as Collections from '../utils/collections';
import {default as EventsDispatcher} from '../events/dispatcher';
import * as Protocol from './protocol/protocol';
import Message from './protocol/message';
import {PusherEvent} from './protocol/message-types';
import Logger from '../logger';

@@ -61,8 +61,8 @@ import TransportConnection from "../transports/transport_connection";

send_event(name : string, data : any, channel?: string) : boolean {
var message : Message = { event: name, data: data };
var event : PusherEvent = { event: name, data: data };
if (channel) {
message.channel = channel;
event.channel = channel;
}
Logger.debug('Event sent', message);
return this.send(Protocol.encodeMessage(message));
Logger.debug('Event sent', event);
return this.send(Protocol.encodeMessage(event));
}

@@ -90,6 +90,6 @@

var listeners = {
message: (m)=> {
var message;
message: (messageEvent: MessageEvent)=> {
var pusherEvent;
try {
message = Protocol.decodeMessage(m);
pusherEvent = Protocol.decodeMessage(messageEvent);
} catch(e) {

@@ -99,12 +99,12 @@ this.emit('error', {

error: e,
data: m.data
data: messageEvent.data
});
}
if (message !== undefined) {
Logger.debug('Event recd', message);
if (pusherEvent !== undefined) {
Logger.debug('Event recd', pusherEvent);
switch (message.event) {
switch (pusherEvent.event) {
case 'pusher:error':
this.emit('error', { type: 'PusherError', data: message.data });
this.emit('error', { type: 'PusherError', data: pusherEvent.data });
break;

@@ -118,3 +118,3 @@ case 'pusher:ping':

}
this.emit('message', message);
this.emit('message', pusherEvent);
}

@@ -121,0 +121,0 @@ },

import Action from './action';
import Message from './message';
import {PusherEvent} from './message-types';
/**

@@ -10,24 +10,34 @@ * Provides functions for handling Pusher protocol-specific messages.

*
* The MessageEvent we receive from the transport should contain a pusher event
* (https://pusher.com/docs/pusher_protocol#events) serialized as JSON in the
* data field
*
* The pusher event may contain a data field too, and it may also be
* serialised as JSON
*
* Throws errors when messages are not parse'able.
*
* @param {Object} message
* @return {Object}
* @param {MessageEvent} messageEvent
* @return {PusherEvent}
*/
export var decodeMessage = function(message : Message) : Message {
export var decodeMessage = function(messageEvent : MessageEvent) : PusherEvent {
try {
var params = JSON.parse(message.data);
if (typeof params.data === 'string') {
var messageData = JSON.parse(messageEvent.data);
var pusherEventData = messageData.data;
if (typeof pusherEventData === 'string') {
try {
params.data = JSON.parse(params.data);
} catch (e) {
if (!(e instanceof SyntaxError)) {
// TODO looks like unreachable code
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse
throw e;
}
}
pusherEventData = JSON.parse(messageData.data)
} catch (e) {}
}
return params;
var pusherEvent: PusherEvent = {
event: messageData.event,
channel: messageData.channel,
data: pusherEventData,
}
if (messageData.user_id) {
pusherEvent.user_id = messageData.user_id
}
return pusherEvent;
} catch (e) {
throw { type: 'MessageParseError', error: e, data: message.data};
throw { type: 'MessageParseError', error: e, data: messageEvent.data};
}

@@ -39,7 +49,7 @@ };

*
* @param {Object} message
* @param {PusherEvent} event
* @return {String}
*/
export var encodeMessage = function(message : Message) : string {
return JSON.stringify(message);
export var encodeMessage = function(event : PusherEvent) : string {
return JSON.stringify(event);
};

@@ -60,4 +70,4 @@

*/
export var processHandshake = function(message : Message) : Action {
message = decodeMessage(message);
export var processHandshake = function(messageEvent : MessageEvent) : Action {
var message = decodeMessage(messageEvent);

@@ -64,0 +74,0 @@ if (message.event === "pusher:connection_established") {

import * as Collections from '../utils/collections';
import Callback from './callback';
import Metadata from '../channels/metadata';
import CallbackRegistry from './callback_registry';

@@ -55,6 +56,4 @@

emit(eventName : string, data?: any) : Dispatcher {
var i;
for (i = 0; i < this.global_callbacks.length; i++) {
emit(eventName : string, data?: any, metadata?: Metadata) : Dispatcher {
for (var i = 0; i < this.global_callbacks.length; i++) {
this.global_callbacks[i](eventName, data);

@@ -64,5 +63,19 @@ }

var callbacks = this.callbacks.get(eventName);
var args = [];
if (metadata) {
// if there's a metadata argument, we need to call the callback with both
// data and metadata regardless of whether data is undefined
args.push(data, metadata)
} else if (data) {
// metadata is undefined, so we only need to call the callback with data
// if data exists
args.push(data)
}
if (callbacks && callbacks.length > 0) {
for (i = 0; i < callbacks.length; i++) {
callbacks[i].fn.call(callbacks[i].context || global, data);
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].fn.apply(callbacks[i].context || global, args);
}

@@ -69,0 +82,0 @@ } else if (this.failThrough) {

@@ -131,8 +131,9 @@ import AbstractRuntime from "runtimes/interface";

});
this.connection.bind('message', (params)=> {
var internal = (params.event.indexOf('pusher_internal:') === 0);
if (params.channel) {
var channel = this.channel(params.channel);
this.connection.bind('message', (event)=> {
var eventName = event.event;
var internal = (eventName.indexOf('pusher_internal:') === 0);
if (event.channel) {
var channel = this.channel(event.channel);
if (channel) {
channel.handleEvent(params.event, params.data);
channel.handleEvent(event);
}

@@ -142,3 +143,3 @@ }

if (!internal) {
this.global_emitter.emit(params.event, params.data);
this.global_emitter.emit(event.event, event.data);
}

@@ -145,0 +146,0 @@ });

@@ -14,2 +14,5 @@ /**

},
triggeringClientEvents: {
path: "/docs/client_api_guide/client_events#trigger-events"
}
}

@@ -16,0 +19,0 @@ }

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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