pusher 4.0.2 → 5.1.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/stale.yml +2 -2
- package/.github/workflows/test.yml +36 -0
- package/CHANGELOG.md +19 -0
- package/README.md +111 -14
- package/examples/typescript/main.ts +1 -2
- package/index.d.ts +8 -1
- package/lib/auth.js +10 -0
- package/lib/config.js +2 -5
- package/lib/events.js +3 -7
- package/lib/pusher.js +82 -9
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/tests/integration/pusher/authenticate.js +163 -36
- package/tests/integration/pusher/constructor.js +5 -5
- package/tests/integration/pusher/terminate.js +55 -0
- package/tests/integration/pusher/trigger.js +78 -11
- package/.travis.yml +0 -20
package/.github/stale.yml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Configuration for probot-stale - https://github.com/probot/stale
|
|
2
2
|
|
|
3
3
|
# Number of days of inactivity before an Issue or Pull Request becomes stale
|
|
4
|
-
daysUntilStale:
|
|
4
|
+
daysUntilStale: 90
|
|
5
5
|
|
|
6
6
|
# Number of days of inactivity before an Issue or Pull Request with the stale label is closed.
|
|
7
7
|
# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale.
|
|
@@ -23,4 +23,4 @@ markComment: >
|
|
|
23
23
|
This issue has been automatically marked as stale because it has not had
|
|
24
24
|
recent activity. It will be closed if no further activity occurs. If you'd
|
|
25
25
|
like this issue to stay open please leave a comment indicating how this issue
|
|
26
|
-
is affecting you.
|
|
26
|
+
is affecting you. Thank you.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches: [master, main]
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-20.04
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
node: [10.x, 10.x, 12.x, 14.x]
|
|
15
|
+
|
|
16
|
+
name: Node.js ${{ matrix.node }} Test
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Checkout
|
|
20
|
+
uses: actions/checkout@v2
|
|
21
|
+
|
|
22
|
+
- name: Setup Node.js
|
|
23
|
+
uses: actions/setup-node@v2
|
|
24
|
+
with:
|
|
25
|
+
node-version: ${{ matrix.node }}
|
|
26
|
+
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: npm ci
|
|
29
|
+
|
|
30
|
+
- name: Run linter
|
|
31
|
+
run: npm run lint
|
|
32
|
+
|
|
33
|
+
- name: Run test suite
|
|
34
|
+
run: npm test
|
|
35
|
+
env:
|
|
36
|
+
PUSHER_URL: ${{ secrets.INTEGRATION_TESTS_PUSHER_URL }}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
## 5.1.0-beta (2022-04-22)
|
|
2
|
+
|
|
3
|
+
[ADDED] Support for terminating user connections based on user id
|
|
4
|
+
[ADDED] Support for sending messages to users based on user id
|
|
5
|
+
[ADDED] Support for implementing user authentication endpoints
|
|
6
|
+
[DEPRECATED] authenticate function is deprecated. The same functionality (and interface) is now provided by authorizeChannel
|
|
7
|
+
|
|
8
|
+
## 5.0.1 (2022-01-24)
|
|
9
|
+
|
|
10
|
+
[FIXED] Incorrect `require` on `version.js` was causing a compilation error in Webpack
|
|
11
|
+
[FIXED] Inconsistent encoding for shared secret between other SDKs
|
|
12
|
+
|
|
13
|
+
## 5.0.0 (2021-02-18)
|
|
14
|
+
|
|
15
|
+
[BREAKING CHANGE] `trigger` now accepts a `params` _object_ instead of a `socket_id` as the third parameter.
|
|
16
|
+
|
|
17
|
+
[ADDED] Support for requesting channel attributes as part of a `trigger` and `triggerBatch` request via an `info` parameter.
|
|
18
|
+
|
|
1
19
|
## 4.0.2 (2020-11-30)
|
|
2
20
|
|
|
3
21
|
[FIXED] Workaround for a [Webpack bug](https://github.com/webpack/webpack/issues/4742) (thanks @JJ-Kidd)
|
|
@@ -46,6 +64,7 @@ const pusher = new Pusher.forURL(process.env.PUSHER_URL, {
|
|
|
46
64
|
[UPGRADED] development dependencies
|
|
47
65
|
|
|
48
66
|
[ADDED] encryptionMasterKeyBase64 constructor parameter to make it easier to use the full range of 32 byte binary values in encryption key
|
|
67
|
+
|
|
49
68
|
[DEPRECATED] encryptionMasterKey constructor parameter - use encryptionMasterKeyBase64
|
|
50
69
|
|
|
51
70
|
## 3.0.0 (2019-09-26)
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://github.com/pusher/pusher-http-node/actions?query=workflow%3ATests+branch%3Amaster)
|
|
2
2
|
[](https://badge.fury.io/js/pusher)
|
|
3
3
|
|
|
4
4
|
# Pusher Channels Node.js REST library
|
|
@@ -7,15 +7,15 @@ In order to use this library, you need to have an account on <https://pusher.com
|
|
|
7
7
|
|
|
8
8
|
## Supported platforms
|
|
9
9
|
|
|
10
|
-
This SDK supports **Node.js** version
|
|
10
|
+
This SDK supports **Node.js** version 10+.
|
|
11
11
|
|
|
12
|
-
We test the library against a selection of Node.js versions which we update over time. Please refer to [
|
|
12
|
+
We test the library against a selection of Node.js versions which we update over time. Please refer to [test.yml](https://github.com/pusher/pusher-http-node/blob/master/.github/workflows/test.yml) for the set of versions that are currently tested with CI.
|
|
13
13
|
|
|
14
14
|
If you find any compatibility issues, please [raise an issue](https://github.com/pusher/pusher-http-node/issues/new) in the repository or contact support at [support@pusher.com](support@pusher.com). We will happily investigate reported problems ❤️.
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
18
|
-
You need to be running at least Node.js
|
|
18
|
+
You need to be running at least Node.js 10 to use this library.
|
|
19
19
|
|
|
20
20
|
```
|
|
21
21
|
$ npm install pusher
|
|
@@ -160,18 +160,88 @@ You can trigger a batch of up to 10 events.
|
|
|
160
160
|
|
|
161
161
|
### Excluding event recipients
|
|
162
162
|
|
|
163
|
-
In order to avoid the client that triggered the event from also receiving it,
|
|
163
|
+
In order to avoid the client that triggered the event from also receiving it, a `socket_id` parameter can be added to the `params` object. For more information see: <http://pusher.com/docs/publisher_api_guide/publisher_excluding_recipients>.
|
|
164
164
|
|
|
165
165
|
```javascript
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
pusher.trigger(channel, event, data, { socket_id: "1302.1081607" })
|
|
167
|
+
|
|
168
|
+
pusher.triggerBatch([
|
|
169
|
+
{ channel: channel, name: name, data: data, socket_id: "1302.1081607" },
|
|
170
|
+
])
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Fetch subscriber and user counts at the time of publish [[EXPERIMENTAL](https://pusher.com/docs/lab#experimental-program)]
|
|
174
|
+
|
|
175
|
+
For the channels that were published to, you can request for the number of subscribers or user to be returned in the response body.
|
|
176
|
+
|
|
177
|
+
#### Regular triggering
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
pusher
|
|
181
|
+
.trigger("presence-my-channel", "event", "test", { info: "user_count,subscription_count" })
|
|
182
|
+
.then(response => {
|
|
183
|
+
if (response.status !== 200) {
|
|
184
|
+
throw Error("unexpected status")
|
|
185
|
+
}
|
|
186
|
+
// Parse the response body as JSON
|
|
187
|
+
return response.json()
|
|
188
|
+
)
|
|
189
|
+
.then(body => {
|
|
190
|
+
const channelsInfo = body.channels
|
|
191
|
+
// Do something with channelsInfo
|
|
192
|
+
})
|
|
193
|
+
.catch(error => {
|
|
194
|
+
// Handle error
|
|
195
|
+
})
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Batch triggering
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
const batch = [
|
|
202
|
+
{
|
|
203
|
+
channel: "my-channel",
|
|
204
|
+
name: "event",
|
|
205
|
+
data: "test1",
|
|
206
|
+
info: "subscription_count",
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
channel: "presence-my-channel",
|
|
210
|
+
name: "event",
|
|
211
|
+
data: "test2",
|
|
212
|
+
info: "user_count,subscription_count",
|
|
213
|
+
},
|
|
214
|
+
]
|
|
215
|
+
pusher
|
|
216
|
+
.triggerBatch(batch)
|
|
217
|
+
.then((response) => {
|
|
218
|
+
if (response.status !== 200) {
|
|
219
|
+
throw Error("unexpected status")
|
|
220
|
+
}
|
|
221
|
+
// Parse the response body as JSON
|
|
222
|
+
return response.json()
|
|
223
|
+
})
|
|
224
|
+
.then((body) => {
|
|
225
|
+
body.batch.forEach((attributes, i) => {
|
|
226
|
+
process.stdout.write(
|
|
227
|
+
`channel: ${batch[i].channel}, name: ${batch[i].name}, subscription_count: ${attributes.subscription_count}`
|
|
228
|
+
)
|
|
229
|
+
if ("user_count" in attributes) {
|
|
230
|
+
process.stdout.write(`, user_count: ${attributes.user_count}`)
|
|
231
|
+
}
|
|
232
|
+
process.stdout.write("\n")
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
.catch((error) => {
|
|
236
|
+
console.error(error)
|
|
237
|
+
})
|
|
168
238
|
```
|
|
169
239
|
|
|
170
|
-
### End-to-end encryption
|
|
240
|
+
### End-to-end encryption
|
|
171
241
|
|
|
172
242
|
This library supports end-to-end encryption of your private channels. This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them. You can enable this feature by following these steps:
|
|
173
243
|
|
|
174
|
-
1. You should first set up Private channels. This involves [creating an
|
|
244
|
+
1. You should first set up Private channels. This involves [creating an authorization endpoint on your server](https://pusher.com/docs/authenticating_users).
|
|
175
245
|
|
|
176
246
|
2. Next, generate your 32 byte master encryption key, encode it as base64 and pass it to the Pusher constructor.
|
|
177
247
|
|
|
@@ -208,17 +278,44 @@ pusher.trigger(["channel-1", "private-encrypted-channel-2"], "test_event", {
|
|
|
208
278
|
|
|
209
279
|
Rationale: the methods in this library map directly to individual Channels HTTP API requests. If we allowed triggering a single event on multiple channels (some encrypted, some unencrypted), then it would require two API requests: one where the event is encrypted to the encrypted channels, and one where the event is unencrypted for unencrypted channels.
|
|
210
280
|
|
|
211
|
-
### Authenticating
|
|
281
|
+
### Authenticating users
|
|
282
|
+
|
|
283
|
+
To authenticate users during sign in, you can use the `authenticateUser` function:
|
|
284
|
+
|
|
285
|
+
```javascript
|
|
286
|
+
const userData = {
|
|
287
|
+
id: "unique_user_id",
|
|
288
|
+
name: "John Doe",
|
|
289
|
+
image: "https://...",
|
|
290
|
+
}
|
|
291
|
+
const auth = pusher.authenticateUser(socketId, userData)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
The `userData` parameter must contain an `id` property with a non empty string. For more information see: <http://pusher.com/docs/authenticating_users>
|
|
295
|
+
|
|
296
|
+
### Terminating user connections
|
|
297
|
+
|
|
298
|
+
In order to terminate a user's connections, the user must have been authenticated. Check the [Server user authentication docs](http://pusher.com/docs/authenticating_users) for the information on how to create a user authentication endpoint.
|
|
299
|
+
|
|
300
|
+
To terminate all connections established by a given user, you can use the `terminateUserConnections` function:
|
|
301
|
+
|
|
302
|
+
```javascript
|
|
303
|
+
pusher.terminateUserConnections(userId)
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Please note, that it only terminates the user's active connections. This means, if nothing else is done, the user will be able to reconnect. For more information see: [Terminating user connections docs](https://pusher.com/docs/channels/server_api/terminating-user-connections/).
|
|
307
|
+
|
|
308
|
+
### Private channel authorisation
|
|
212
309
|
|
|
213
|
-
To authorise your users to access private channels on Pusher Channels, you can use the `
|
|
310
|
+
To authorise your users to access private channels on Pusher Channels, you can use the `authorizeChannel` function:
|
|
214
311
|
|
|
215
312
|
```javascript
|
|
216
|
-
const auth = pusher.
|
|
313
|
+
const auth = pusher.authorizeChannel(socketId, channel)
|
|
217
314
|
```
|
|
218
315
|
|
|
219
316
|
For more information see: <http://pusher.com/docs/authenticating_users>
|
|
220
317
|
|
|
221
|
-
###
|
|
318
|
+
### Presence channel authorisation
|
|
222
319
|
|
|
223
320
|
Using presence channels is similar to private channels, but you can specify extra data to identify that particular user:
|
|
224
321
|
|
|
@@ -230,7 +327,7 @@ const channelData = {
|
|
|
230
327
|
twitter_id: '@leggetter'
|
|
231
328
|
}
|
|
232
329
|
};
|
|
233
|
-
const auth = pusher.
|
|
330
|
+
const auth = pusher.authorizeChannel(socketId, channel, channelData);
|
|
234
331
|
```
|
|
235
332
|
|
|
236
333
|
The `auth` is then returned to the caller as JSON.
|
|
@@ -4,8 +4,7 @@ import * as Pusher from "pusher"
|
|
|
4
4
|
|
|
5
5
|
const pusher = Pusher.forURL(process.env.PUSHER_URL, {
|
|
6
6
|
encryptionMasterKeyBase64: Buffer.from(
|
|
7
|
-
"01234567890123456789012345678901"
|
|
8
|
-
"binary"
|
|
7
|
+
"01234567890123456789012345678901"
|
|
9
8
|
).toString("base64"),
|
|
10
9
|
agent: new Agent({ keepAlive: true }),
|
|
11
10
|
})
|
package/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ declare class Pusher {
|
|
|
10
10
|
channel: string | Array<string>,
|
|
11
11
|
event: string,
|
|
12
12
|
data: any,
|
|
13
|
-
|
|
13
|
+
params?: Pusher.TriggerParams
|
|
14
14
|
): Promise<Response>
|
|
15
15
|
|
|
16
16
|
trigger(
|
|
@@ -61,10 +61,17 @@ declare namespace Pusher {
|
|
|
61
61
|
|
|
62
62
|
export type Options = ClusterOptions | HostOptions
|
|
63
63
|
|
|
64
|
+
export interface TriggerParams {
|
|
65
|
+
socket_id?: string
|
|
66
|
+
info?: string
|
|
67
|
+
}
|
|
68
|
+
|
|
64
69
|
export interface BatchEvent {
|
|
65
70
|
channel: string
|
|
66
71
|
name: string
|
|
67
72
|
data: any
|
|
73
|
+
socket_id?: string
|
|
74
|
+
info?: string
|
|
68
75
|
}
|
|
69
76
|
|
|
70
77
|
type ReservedParams =
|
package/lib/auth.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
const util = require("./util")
|
|
2
2
|
|
|
3
|
+
function getSocketSignatureForUser(token, socketId, userData) {
|
|
4
|
+
const serializedUserData = JSON.stringify(userData)
|
|
5
|
+
const signature = token.sign(`${socketId}::user::${serializedUserData}`)
|
|
6
|
+
return {
|
|
7
|
+
auth: `${token.key}:${signature}`,
|
|
8
|
+
user_data: serializedUserData,
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
function getSocketSignature(pusher, token, channel, socketID, data) {
|
|
4
13
|
const result = {}
|
|
5
14
|
|
|
@@ -26,4 +35,5 @@ function getSocketSignature(pusher, token, channel, socketID, data) {
|
|
|
26
35
|
return result
|
|
27
36
|
}
|
|
28
37
|
|
|
38
|
+
exports.getSocketSignatureForUser = getSocketSignatureForUser
|
|
29
39
|
exports.getSocketSignature = getSocketSignature
|
package/lib/config.js
CHANGED
|
@@ -49,7 +49,7 @@ function Config(options) {
|
|
|
49
49
|
)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
this.encryptionMasterKey = options.encryptionMasterKey
|
|
52
|
+
this.encryptionMasterKey = Buffer.from(options.encryptionMasterKey)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// Handle base64 encoded 32 byte key to encourage use of the full range of byte values
|
|
@@ -61,10 +61,7 @@ function Config(options) {
|
|
|
61
61
|
throw new Error("encryptionMasterKeyBase64 must be valid base64")
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const decodedKey = Buffer.from(
|
|
65
|
-
options.encryptionMasterKeyBase64,
|
|
66
|
-
"base64"
|
|
67
|
-
).toString("binary")
|
|
64
|
+
const decodedKey = Buffer.from(options.encryptionMasterKeyBase64, "base64")
|
|
68
65
|
if (decodedKey.length !== 32) {
|
|
69
66
|
throw new Error(
|
|
70
67
|
"encryptionMasterKeyBase64 must decode to 32 bytes, but the string " +
|
package/lib/events.js
CHANGED
|
@@ -23,16 +23,14 @@ function encrypt(pusher, channel, data) {
|
|
|
23
23
|
})
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
exports.trigger = function (pusher, channels, eventName, data,
|
|
26
|
+
exports.trigger = function (pusher, channels, eventName, data, params) {
|
|
27
27
|
if (channels.length === 1 && util.isEncryptedChannel(channels[0])) {
|
|
28
28
|
const channel = channels[0]
|
|
29
29
|
const event = {
|
|
30
30
|
name: eventName,
|
|
31
31
|
data: encrypt(pusher, channel, data),
|
|
32
32
|
channels: [channel],
|
|
33
|
-
|
|
34
|
-
if (socketId) {
|
|
35
|
-
event.socket_id = socketId
|
|
33
|
+
...params,
|
|
36
34
|
}
|
|
37
35
|
return pusher.post({ path: "/events", body: event })
|
|
38
36
|
} else {
|
|
@@ -49,9 +47,7 @@ exports.trigger = function (pusher, channels, eventName, data, socketId) {
|
|
|
49
47
|
name: eventName,
|
|
50
48
|
data: ensureJSON(data),
|
|
51
49
|
channels: channels,
|
|
52
|
-
|
|
53
|
-
if (socketId) {
|
|
54
|
-
event.socket_id = socketId
|
|
50
|
+
...params,
|
|
55
51
|
}
|
|
56
52
|
return pusher.post({ path: "/events", body: event })
|
|
57
53
|
}
|
package/lib/pusher.js
CHANGED
|
@@ -34,6 +34,19 @@ const validateSocketId = function (socketId) {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
const validateUserId = function (userId) {
|
|
38
|
+
if (typeof userId !== "string" || userId === "") {
|
|
39
|
+
throw new Error("Invalid user id: '" + userId + "'")
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const validateUserData = function (userData) {
|
|
44
|
+
if (userData == null || typeof userData !== "object") {
|
|
45
|
+
throw new Error("Invalid user data: '" + userData + "'")
|
|
46
|
+
}
|
|
47
|
+
validateUserId(userData.id)
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
/** Provides access to Pusher's REST API, WebHooks and authentication.
|
|
38
51
|
*
|
|
39
52
|
* @constructor
|
|
@@ -103,9 +116,9 @@ Pusher.forCluster = function (cluster, options) {
|
|
|
103
116
|
* @param {String} socketId socket id
|
|
104
117
|
* @param {String} channel channel name
|
|
105
118
|
* @param {Object} [data] additional socket data
|
|
106
|
-
* @returns {String}
|
|
119
|
+
* @returns {String} authorization signature
|
|
107
120
|
*/
|
|
108
|
-
Pusher.prototype.
|
|
121
|
+
Pusher.prototype.authorizeChannel = function (socketId, channel, data) {
|
|
109
122
|
validateSocketId(socketId)
|
|
110
123
|
validateChannel(channel)
|
|
111
124
|
|
|
@@ -118,6 +131,60 @@ Pusher.prototype.authenticate = function (socketId, channel, data) {
|
|
|
118
131
|
)
|
|
119
132
|
}
|
|
120
133
|
|
|
134
|
+
/** Returns a signature for given socket id, channel and socket data.
|
|
135
|
+
*
|
|
136
|
+
* DEPRECATED. Use authorizeChannel.
|
|
137
|
+
*
|
|
138
|
+
* @param {String} socketId socket id
|
|
139
|
+
* @param {String} channel channel name
|
|
140
|
+
* @param {Object} [data] additional socket data
|
|
141
|
+
* @returns {String} authorization signature
|
|
142
|
+
*/
|
|
143
|
+
Pusher.prototype.authenticate = Pusher.prototype.authorizeChannel
|
|
144
|
+
|
|
145
|
+
/** Returns a signature for given socket id and user data.
|
|
146
|
+
*
|
|
147
|
+
* @param {String} socketId socket id
|
|
148
|
+
* @param {Object} userData user data
|
|
149
|
+
* @returns {String} authentication signature
|
|
150
|
+
*/
|
|
151
|
+
Pusher.prototype.authenticateUser = function (socketId, userData) {
|
|
152
|
+
validateSocketId(socketId)
|
|
153
|
+
validateUserData(userData)
|
|
154
|
+
|
|
155
|
+
return auth.getSocketSignatureForUser(this.config.token, socketId, userData)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Sends an event to a user.
|
|
159
|
+
*
|
|
160
|
+
* Event name can be at most 200 characters long.
|
|
161
|
+
*
|
|
162
|
+
* @param {String} userId user id
|
|
163
|
+
* @param {String} event event name
|
|
164
|
+
* @param data event data, objects are JSON-encoded
|
|
165
|
+
* @returns {Promise} a promise resolving to a response, or rejecting to a RequestError.
|
|
166
|
+
* @see RequestError
|
|
167
|
+
*/
|
|
168
|
+
Pusher.prototype.sendToUser = function (userId, event, data) {
|
|
169
|
+
if (event.length > 200) {
|
|
170
|
+
throw new Error("Too long event name: '" + event + "'")
|
|
171
|
+
}
|
|
172
|
+
validateUserId(userId)
|
|
173
|
+
return events.trigger(this, [`#server-to-user-${userId}`], event, data)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Terminate users's connections.
|
|
177
|
+
*
|
|
178
|
+
*
|
|
179
|
+
* @param {String} userId user id
|
|
180
|
+
* @returns {Promise} a promise resolving to a response, or rejecting to a RequestError.
|
|
181
|
+
* @see RequestError
|
|
182
|
+
*/
|
|
183
|
+
Pusher.prototype.terminateUserConnections = function (userId) {
|
|
184
|
+
validateUserId(userId)
|
|
185
|
+
return this.post({ path: `/users/${userId}/terminate_connections`, body: {} })
|
|
186
|
+
}
|
|
187
|
+
|
|
121
188
|
/** Triggers an event.
|
|
122
189
|
*
|
|
123
190
|
* Channel names can contain only characters which are alphanumeric, '_' or '-'
|
|
@@ -130,12 +197,14 @@ Pusher.prototype.authenticate = function (socketId, channel, data) {
|
|
|
130
197
|
* @param {String|String[]} channel list of at most 100 channels
|
|
131
198
|
* @param {String} event event name
|
|
132
199
|
* @param data event data, objects are JSON-encoded
|
|
133
|
-
* @param {
|
|
200
|
+
* @param {Object} [params] additional optional request body parameters
|
|
201
|
+
* @param {String} [params.socket_id] id of a socket that should not receive the event
|
|
202
|
+
* @param {String} [params.info] a comma separate list of attributes to be returned in the response. Experimental, see https://pusher.com/docs/lab#experimental-program
|
|
134
203
|
* @see RequestError
|
|
135
204
|
*/
|
|
136
|
-
Pusher.prototype.trigger = function (channels, event, data,
|
|
137
|
-
if (
|
|
138
|
-
validateSocketId(
|
|
205
|
+
Pusher.prototype.trigger = function (channels, event, data, params) {
|
|
206
|
+
if (params && params.socket_id) {
|
|
207
|
+
validateSocketId(params.socket_id)
|
|
139
208
|
}
|
|
140
209
|
if (!(channels instanceof Array)) {
|
|
141
210
|
// add single channel to array for multi trigger compatibility
|
|
@@ -150,7 +219,7 @@ Pusher.prototype.trigger = function (channels, event, data, socketId) {
|
|
|
150
219
|
for (let i = 0; i < channels.length; i++) {
|
|
151
220
|
validateChannel(channels[i])
|
|
152
221
|
}
|
|
153
|
-
return events.trigger(this, channels, event, data,
|
|
222
|
+
return events.trigger(this, channels, event, data, params)
|
|
154
223
|
}
|
|
155
224
|
|
|
156
225
|
/* Triggers a batch of events
|
|
@@ -159,7 +228,9 @@ Pusher.prototype.trigger = function (channels, event, data, socketId) {
|
|
|
159
228
|
* {
|
|
160
229
|
* name: string,
|
|
161
230
|
* channel: string,
|
|
162
|
-
* data: any JSON-encodable data
|
|
231
|
+
* data: any JSON-encodable data,
|
|
232
|
+
* socket_id: [optional] string,
|
|
233
|
+
* info: [optional] string experimental, see https://pusher.com/docs/lab#experimental-program
|
|
163
234
|
* }
|
|
164
235
|
*/
|
|
165
236
|
Pusher.prototype.triggerBatch = function (batch) {
|
|
@@ -230,7 +301,9 @@ Pusher.prototype.createSignedQueryString = function (options) {
|
|
|
230
301
|
Pusher.prototype.channelSharedSecret = function (channel) {
|
|
231
302
|
return crypto
|
|
232
303
|
.createHash("sha256")
|
|
233
|
-
.update(
|
|
304
|
+
.update(
|
|
305
|
+
Buffer.concat([Buffer.from(channel), this.config.encryptionMasterKey])
|
|
306
|
+
)
|
|
234
307
|
.digest()
|
|
235
308
|
}
|
|
236
309
|
|
package/lib/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require("../package").version
|
|
1
|
+
module.exports = require("../package.json").version
|
package/package.json
CHANGED
|
@@ -9,38 +9,163 @@ describe("Pusher", function () {
|
|
|
9
9
|
pusher = new Pusher({ appId: 10000, key: "aaaa", secret: "tofu" })
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
describe("#
|
|
12
|
+
describe("#authenticateUser", function () {
|
|
13
13
|
it("should prefix the signature with the app key", function () {
|
|
14
14
|
let pusher = new Pusher({ appId: 10000, key: "1234", secret: "tofu" })
|
|
15
|
-
expect(pusher.
|
|
15
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
16
|
+
auth:
|
|
17
|
+
"1234:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
18
|
+
user_data: '{"id":"45678"}',
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
pusher = new Pusher({ appId: 10000, key: "abcdef", secret: "tofu" })
|
|
22
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
23
|
+
auth:
|
|
24
|
+
"abcdef:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
25
|
+
user_data: '{"id":"45678"}',
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("should return correct authentication signatures for different user data", function () {
|
|
30
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
31
|
+
auth:
|
|
32
|
+
"aaaa:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
33
|
+
user_data: '{"id":"45678"}',
|
|
34
|
+
})
|
|
35
|
+
expect(
|
|
36
|
+
pusher.authenticateUser("123.456", { id: "55555", user_name: "test" })
|
|
37
|
+
).to.eql({
|
|
38
|
+
auth:
|
|
39
|
+
"aaaa:b8a9f173455903792ae2b788add0c4c78ad7372b3ae7fb5769479276a1993743",
|
|
40
|
+
user_data: JSON.stringify({ id: "55555", user_name: "test" }),
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("should return correct authentication signatures for different secrets", function () {
|
|
45
|
+
let pusher = new Pusher({ appId: 10000, key: "11111", secret: "1" })
|
|
46
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
47
|
+
auth:
|
|
48
|
+
"11111:79bddf29fe8e2153dd5d8d569b3f45e5aeb26ae2eb4758879d844791b466cfa2",
|
|
49
|
+
user_data: '{"id":"45678"}',
|
|
50
|
+
})
|
|
51
|
+
pusher = new Pusher({ appId: 10000, key: "11111", secret: "2" })
|
|
52
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
53
|
+
auth:
|
|
54
|
+
"11111:a542498ffa6faf6de7c17a8106b923c042319bd73acfd1d274df32e269b55d1f",
|
|
55
|
+
user_data: '{"id":"45678"}',
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("should return correct authentication signature with utf-8 in user data", function () {
|
|
60
|
+
expect(pusher.authenticateUser("1.1", { id: "ą§¶™€łü€ß£" })).to.eql({
|
|
61
|
+
auth:
|
|
62
|
+
"aaaa:620494cee53d6c568b49598313194088afda37218f0d059af03c0c898ed61ff4",
|
|
63
|
+
user_data: '{"id":"ą§¶™€łü€ß£"}',
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it("should raise an exception if socket id is not a string", function () {
|
|
68
|
+
expect(function () {
|
|
69
|
+
pusher.authenticateUser(undefined, { id: "123" })
|
|
70
|
+
}).to.throwException(/^Invalid socket id: 'undefined'$/)
|
|
71
|
+
expect(function () {
|
|
72
|
+
pusher.authenticateUser(null, { id: "123" })
|
|
73
|
+
}).to.throwException(/^Invalid socket id: 'null'$/)
|
|
74
|
+
expect(function () {
|
|
75
|
+
pusher.authenticateUser(111, { id: "123" })
|
|
76
|
+
}).to.throwException(/^Invalid socket id: '111'$/)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("should raise an exception if socket id is an empty string", function () {
|
|
80
|
+
expect(function () {
|
|
81
|
+
pusher.authenticateUser("", { id: "123" })
|
|
82
|
+
}).to.throwException(/^Invalid socket id: ''$/)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("should raise an exception if socket id is invalid", function () {
|
|
86
|
+
expect(function () {
|
|
87
|
+
pusher.authenticateUser("1.1:", { id: "123" })
|
|
88
|
+
}).to.throwException(/^Invalid socket id/)
|
|
89
|
+
expect(function () {
|
|
90
|
+
pusher.authenticateUser(":1.1", { id: "123" })
|
|
91
|
+
}).to.throwException(/^Invalid socket id/)
|
|
92
|
+
expect(function () {
|
|
93
|
+
pusher.authenticateUser(":\n1.1", { id: "123" })
|
|
94
|
+
}).to.throwException(/^Invalid socket id/)
|
|
95
|
+
expect(function () {
|
|
96
|
+
pusher.authenticateUser("1.1\n:", { id: "123" })
|
|
97
|
+
}).to.throwException(/^Invalid socket id/)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it("should raise an exception if user data is not a non-null object", function () {
|
|
101
|
+
expect(function () {
|
|
102
|
+
pusher.authenticateUser("111.222", undefined)
|
|
103
|
+
}).to.throwException(/^Invalid user data: 'undefined'$/)
|
|
104
|
+
expect(function () {
|
|
105
|
+
pusher.authenticateUser("111.222", null)
|
|
106
|
+
}).to.throwException(/^Invalid user data: 'null'$/)
|
|
107
|
+
expect(function () {
|
|
108
|
+
pusher.authenticateUser("111.222", 111)
|
|
109
|
+
}).to.throwException(/^Invalid user data: '111'$/)
|
|
110
|
+
expect(function () {
|
|
111
|
+
pusher.authenticateUser("111.222", "")
|
|
112
|
+
}).to.throwException(/^Invalid user data: ''$/)
|
|
113
|
+
expect(function () {
|
|
114
|
+
pusher.authenticateUser("111.222", "abc")
|
|
115
|
+
}).to.throwException(/^Invalid user data: 'abc'$/)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("should raise an exception if user data doesn't have a valid id field", function () {
|
|
119
|
+
expect(function () {
|
|
120
|
+
pusher.authenticateUser("111.222", {})
|
|
121
|
+
}).to.throwException(/^Invalid user id: 'undefined'$/)
|
|
122
|
+
expect(function () {
|
|
123
|
+
pusher.authenticateUser("111.222", { id: "" })
|
|
124
|
+
}).to.throwException(/^Invalid user id: ''$/)
|
|
125
|
+
expect(function () {
|
|
126
|
+
pusher.authenticateUser("111.222", { id: 123 })
|
|
127
|
+
}).to.throwException(/^Invalid user id: '123'$/)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe("#authenticate", function () {
|
|
132
|
+
it("should be the exactly the same as authorizeChannel", function () {
|
|
133
|
+
expect(pusher.authenticate).to.eql(pusher.authorizeChannel)
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
describe("#authorizeChannel", function () {
|
|
138
|
+
it("should prefix the signature with the app key", function () {
|
|
139
|
+
let pusher = new Pusher({ appId: 10000, key: "1234", secret: "tofu" })
|
|
140
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
16
141
|
auth:
|
|
17
142
|
"1234:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
18
143
|
})
|
|
19
144
|
|
|
20
145
|
pusher = new Pusher({ appId: 10000, key: "abcdef", secret: "tofu" })
|
|
21
|
-
expect(pusher.
|
|
146
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
22
147
|
auth:
|
|
23
148
|
"abcdef:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
24
149
|
})
|
|
25
150
|
})
|
|
26
151
|
|
|
27
152
|
it("should return correct authentication signatures for different socket ids", function () {
|
|
28
|
-
expect(pusher.
|
|
153
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
29
154
|
auth:
|
|
30
155
|
"aaaa:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
31
156
|
})
|
|
32
|
-
expect(pusher.
|
|
157
|
+
expect(pusher.authorizeChannel("321.654", "test")).to.eql({
|
|
33
158
|
auth:
|
|
34
159
|
"aaaa:f6ecb0a17d3e4f68aca28f1673197a7608587c09deb0208faa4b5519aee0a777",
|
|
35
160
|
})
|
|
36
161
|
})
|
|
37
162
|
|
|
38
163
|
it("should return correct authentication signatures for different channels", function () {
|
|
39
|
-
expect(pusher.
|
|
164
|
+
expect(pusher.authorizeChannel("123.456", "test1")).to.eql({
|
|
40
165
|
auth:
|
|
41
166
|
"aaaa:d5ab857f805433cb50562da96afa41688d7742a3c3a021ed15a4d991a4d8cf94",
|
|
42
167
|
})
|
|
43
|
-
expect(pusher.
|
|
168
|
+
expect(pusher.authorizeChannel("123.456", "test2")).to.eql({
|
|
44
169
|
auth:
|
|
45
170
|
"aaaa:43affa6a09af1fb9ce1cadf176171346beaf7366673ec1e5920f68b3e97a466d",
|
|
46
171
|
})
|
|
@@ -48,12 +173,12 @@ describe("Pusher", function () {
|
|
|
48
173
|
|
|
49
174
|
it("should return correct authentication signatures for different secrets", function () {
|
|
50
175
|
let pusher = new Pusher({ appId: 10000, key: "11111", secret: "1" })
|
|
51
|
-
expect(pusher.
|
|
176
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
52
177
|
auth:
|
|
53
178
|
"11111:584828bd6e80b2d177d2b28fde07b8e170abf87ccb5a791a50c933711fb8eb28",
|
|
54
179
|
})
|
|
55
180
|
pusher = new Pusher({ appId: 10000, key: "11111", secret: "2" })
|
|
56
|
-
expect(pusher.
|
|
181
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
57
182
|
auth:
|
|
58
183
|
"11111:269bbf3f7625db4e0d0525b617efa5915c3ae667fd222dc8e4cb94bc531f26f2",
|
|
59
184
|
})
|
|
@@ -61,24 +186,26 @@ describe("Pusher", function () {
|
|
|
61
186
|
|
|
62
187
|
it("should return the channel data", function () {
|
|
63
188
|
expect(
|
|
64
|
-
pusher.
|
|
189
|
+
pusher.authorizeChannel("123.456", "test", { foo: "bar" }).channel_data
|
|
65
190
|
).to.eql('{"foo":"bar"}')
|
|
66
191
|
})
|
|
67
192
|
|
|
68
193
|
it("should return correct authentication signatures with and without the channel data", function () {
|
|
69
|
-
expect(pusher.
|
|
194
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
70
195
|
auth:
|
|
71
196
|
"aaaa:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
72
197
|
})
|
|
73
|
-
expect(pusher.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
198
|
+
expect(pusher.authorizeChannel("123.456", "test", { foo: "bar" })).to.eql(
|
|
199
|
+
{
|
|
200
|
+
auth:
|
|
201
|
+
"aaaa:f41faf9ead2ea76772cc6b1168363057459f02499ae4d92e88229dc7f4efa2d4",
|
|
202
|
+
channel_data: '{"foo":"bar"}',
|
|
203
|
+
}
|
|
204
|
+
)
|
|
78
205
|
})
|
|
79
206
|
|
|
80
207
|
it("should return correct authentication signature with utf-8 in channel data", function () {
|
|
81
|
-
expect(pusher.
|
|
208
|
+
expect(pusher.authorizeChannel("1.1", "test", "ą§¶™€łü€ß£")).to.eql({
|
|
82
209
|
auth:
|
|
83
210
|
"aaaa:2a229263e89d9c50524fd80c2e88be2843379f6931e28995e2cc214282c9db0c",
|
|
84
211
|
channel_data: '"ą§¶™€łü€ß£"',
|
|
@@ -87,58 +214,58 @@ describe("Pusher", function () {
|
|
|
87
214
|
|
|
88
215
|
it("should raise an exception if socket id is not a string", function () {
|
|
89
216
|
expect(function () {
|
|
90
|
-
pusher.
|
|
217
|
+
pusher.authorizeChannel(undefined, "test")
|
|
91
218
|
}).to.throwException(/^Invalid socket id: 'undefined'$/)
|
|
92
219
|
expect(function () {
|
|
93
|
-
pusher.
|
|
220
|
+
pusher.authorizeChannel(null, "test")
|
|
94
221
|
}).to.throwException(/^Invalid socket id: 'null'$/)
|
|
95
222
|
expect(function () {
|
|
96
|
-
pusher.
|
|
223
|
+
pusher.authorizeChannel(111, "test")
|
|
97
224
|
}).to.throwException(/^Invalid socket id: '111'$/)
|
|
98
225
|
})
|
|
99
226
|
|
|
100
227
|
it("should raise an exception if socket id is an empty string", function () {
|
|
101
228
|
expect(function () {
|
|
102
|
-
pusher.
|
|
229
|
+
pusher.authorizeChannel("", "test")
|
|
103
230
|
}).to.throwException(/^Invalid socket id: ''$/)
|
|
104
231
|
})
|
|
105
232
|
|
|
106
233
|
it("should raise an exception if socket id is invalid", function () {
|
|
107
234
|
expect(function () {
|
|
108
|
-
pusher.
|
|
235
|
+
pusher.authorizeChannel("1.1:", "test")
|
|
109
236
|
}).to.throwException(/^Invalid socket id/)
|
|
110
237
|
expect(function () {
|
|
111
|
-
pusher.
|
|
238
|
+
pusher.authorizeChannel(":1.1", "test")
|
|
112
239
|
}).to.throwException(/^Invalid socket id/)
|
|
113
240
|
expect(function () {
|
|
114
|
-
pusher.
|
|
241
|
+
pusher.authorizeChannel(":\n1.1", "test")
|
|
115
242
|
}).to.throwException(/^Invalid socket id/)
|
|
116
243
|
expect(function () {
|
|
117
|
-
pusher.
|
|
244
|
+
pusher.authorizeChannel("1.1\n:", "test")
|
|
118
245
|
}).to.throwException(/^Invalid socket id/)
|
|
119
246
|
})
|
|
120
247
|
|
|
121
248
|
it("should raise an exception if channel name is not a string", function () {
|
|
122
249
|
expect(function () {
|
|
123
|
-
pusher.
|
|
250
|
+
pusher.authorizeChannel("111.222", undefined)
|
|
124
251
|
}).to.throwException(/^Invalid channel name: 'undefined'$/)
|
|
125
252
|
expect(function () {
|
|
126
|
-
pusher.
|
|
253
|
+
pusher.authorizeChannel("111.222", null)
|
|
127
254
|
}).to.throwException(/^Invalid channel name: 'null'$/)
|
|
128
255
|
expect(function () {
|
|
129
|
-
pusher.
|
|
256
|
+
pusher.authorizeChannel("111.222", 111)
|
|
130
257
|
}).to.throwException(/^Invalid channel name: '111'$/)
|
|
131
258
|
})
|
|
132
259
|
|
|
133
260
|
it("should raise an exception if channel name is an empty string", function () {
|
|
134
261
|
expect(function () {
|
|
135
|
-
pusher.
|
|
262
|
+
pusher.authorizeChannel("111.222", "")
|
|
136
263
|
}).to.throwException(/^Invalid channel name: ''$/)
|
|
137
264
|
})
|
|
138
265
|
|
|
139
266
|
it("should throw an error for private-encrypted- channels", function () {
|
|
140
267
|
expect(function () {
|
|
141
|
-
pusher.
|
|
268
|
+
pusher.authorizeChannel("123.456", "private-encrypted-bla", "foo")
|
|
142
269
|
}).to.throwException(
|
|
143
270
|
"Cannot generate shared_secret because encryptionMasterKey is not set"
|
|
144
271
|
)
|
|
@@ -149,30 +276,30 @@ describe("Pusher", function () {
|
|
|
149
276
|
describe("Pusher with encryptionMasterKey", function () {
|
|
150
277
|
let pusher
|
|
151
278
|
|
|
152
|
-
const
|
|
279
|
+
const testMasterKeyBase64 = "zyrm8pvV2C9fJcBfhyXzvxbJVN/H7QLmbe0xJi1GhPU="
|
|
153
280
|
|
|
154
281
|
beforeEach(function () {
|
|
155
282
|
pusher = new Pusher({
|
|
156
283
|
appId: 1234,
|
|
157
284
|
key: "f00d",
|
|
158
285
|
secret: "tofu",
|
|
159
|
-
|
|
286
|
+
encryptionMasterKeyBase64: testMasterKeyBase64,
|
|
160
287
|
})
|
|
161
288
|
})
|
|
162
289
|
|
|
163
|
-
describe("#
|
|
290
|
+
describe("#authorizeChannel", function () {
|
|
164
291
|
it("should return a shared_secret for private-encrypted- channels", function () {
|
|
165
292
|
expect(
|
|
166
|
-
pusher.
|
|
293
|
+
pusher.authorizeChannel("123.456", "private-encrypted-bla", "foo")
|
|
167
294
|
).to.eql({
|
|
168
295
|
auth:
|
|
169
296
|
"f00d:962c48b78bf93d98ff4c92ee7dff04865821455b7b401e9d60a9e0a90af2c105",
|
|
170
297
|
channel_data: '"foo"',
|
|
171
|
-
shared_secret: "
|
|
298
|
+
shared_secret: "nlr49ISQHz91yS3cy/yWmW8wFMNeTnNL5tNHnbPJcLQ=",
|
|
172
299
|
})
|
|
173
300
|
})
|
|
174
301
|
it("should not return a shared_secret for non-encrypted channels", function () {
|
|
175
|
-
expect(pusher.
|
|
302
|
+
expect(pusher.authorizeChannel("123.456", "bla", "foo")).to.eql({
|
|
176
303
|
auth:
|
|
177
304
|
"f00d:013ad3da0d88e0df6ae0a8184bef50b9c3933f2344499e6e3d1ad67fad799e20",
|
|
178
305
|
channel_data: '"foo"',
|
|
@@ -102,7 +102,7 @@ describe("Pusher", function () {
|
|
|
102
102
|
it("should support `encryptionMasterKey` of 32 bytes", function () {
|
|
103
103
|
const key = "01234567890123456789012345678901"
|
|
104
104
|
const pusher = new Pusher({ encryptionMasterKey: key })
|
|
105
|
-
expect(pusher.config.encryptionMasterKey).to.equal(key)
|
|
105
|
+
expect(pusher.config.encryptionMasterKey.toString()).to.equal(key)
|
|
106
106
|
})
|
|
107
107
|
|
|
108
108
|
it("should reject `encryptionMasterKey` of 31 bytes", function () {
|
|
@@ -121,14 +121,14 @@ describe("Pusher", function () {
|
|
|
121
121
|
|
|
122
122
|
it("should support `encryptionMasterKeyBase64` which decodes to 32 bytes", function () {
|
|
123
123
|
const key = "01234567890123456789012345678901"
|
|
124
|
-
const keyBase64 = Buffer.from(key
|
|
124
|
+
const keyBase64 = Buffer.from(key).toString("base64")
|
|
125
125
|
const pusher = new Pusher({ encryptionMasterKeyBase64: keyBase64 })
|
|
126
|
-
expect(pusher.config.encryptionMasterKey).to.equal(key)
|
|
126
|
+
expect(pusher.config.encryptionMasterKey.toString()).to.equal(key)
|
|
127
127
|
})
|
|
128
128
|
|
|
129
129
|
it("should reject `encryptionMasterKeyBase64` which decodes to 31 bytes", function () {
|
|
130
130
|
const key = "0123456789012345678901234567890"
|
|
131
|
-
const keyBase64 = Buffer.from(key
|
|
131
|
+
const keyBase64 = Buffer.from(key).toString("base64")
|
|
132
132
|
expect(function () {
|
|
133
133
|
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
|
|
134
134
|
}).to.throwException(/31 bytes/)
|
|
@@ -136,7 +136,7 @@ describe("Pusher", function () {
|
|
|
136
136
|
|
|
137
137
|
it("should reject `encryptionMasterKeyBase64` which decodes to 33 bytes", function () {
|
|
138
138
|
const key = "012345678901234567890123456789012"
|
|
139
|
-
const keyBase64 = Buffer.from(key
|
|
139
|
+
const keyBase64 = Buffer.from(key).toString("base64")
|
|
140
140
|
expect(function () {
|
|
141
141
|
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
|
|
142
142
|
}).to.throwException(/33 bytes/)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const expect = require("expect.js")
|
|
2
|
+
const nock = require("nock")
|
|
3
|
+
|
|
4
|
+
const Pusher = require("../../../lib/pusher")
|
|
5
|
+
const sinon = require("sinon")
|
|
6
|
+
|
|
7
|
+
describe("Pusher", function () {
|
|
8
|
+
let pusher
|
|
9
|
+
|
|
10
|
+
beforeEach(function () {
|
|
11
|
+
pusher = new Pusher({ appId: 1234, key: "f00d", secret: "tofu" })
|
|
12
|
+
nock.disableNetConnect()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
afterEach(function () {
|
|
16
|
+
nock.cleanAll()
|
|
17
|
+
nock.enableNetConnect()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe("#terminateUserConnections", function () {
|
|
21
|
+
it("should throw an error if user id is empty", function () {
|
|
22
|
+
expect(function () {
|
|
23
|
+
pusher.terminateUserConnections("")
|
|
24
|
+
}).to.throwError(function (e) {
|
|
25
|
+
expect(e).to.be.an(Error)
|
|
26
|
+
expect(e.message).to.equal("Invalid user id: ''")
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("should throw an error if user id is not a string", function () {
|
|
31
|
+
expect(function () {
|
|
32
|
+
pusher.terminateUserConnections(123)
|
|
33
|
+
}).to.throwError(function (e) {
|
|
34
|
+
expect(e).to.be.an(Error)
|
|
35
|
+
expect(e.message).to.equal("Invalid user id: '123'")
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("should call /terminate_connections endpoint", function (done) {
|
|
41
|
+
sinon.stub(pusher, "post")
|
|
42
|
+
pusher.appId = 1234
|
|
43
|
+
const userId = "testUserId"
|
|
44
|
+
|
|
45
|
+
pusher.terminateUserConnections(userId)
|
|
46
|
+
|
|
47
|
+
expect(pusher.post.called).to.be(true)
|
|
48
|
+
expect(pusher.post.getCall(0).args[0]).eql({
|
|
49
|
+
path: `/users/${userId}/terminate_connections`,
|
|
50
|
+
body: {},
|
|
51
|
+
})
|
|
52
|
+
pusher.post.restore()
|
|
53
|
+
done()
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -2,8 +2,10 @@ const expect = require("expect.js")
|
|
|
2
2
|
const nock = require("nock")
|
|
3
3
|
const nacl = require("tweetnacl")
|
|
4
4
|
const naclUtil = require("tweetnacl-util")
|
|
5
|
+
const sinon = require("sinon")
|
|
5
6
|
|
|
6
7
|
const Pusher = require("../../../lib/pusher")
|
|
8
|
+
const events = require("../../../lib/events")
|
|
7
9
|
|
|
8
10
|
describe("Pusher", function () {
|
|
9
11
|
let pusher
|
|
@@ -99,7 +101,7 @@ describe("Pusher", function () {
|
|
|
99
101
|
.catch(done)
|
|
100
102
|
})
|
|
101
103
|
|
|
102
|
-
it("should add
|
|
104
|
+
it("should add params to the request body", function (done) {
|
|
103
105
|
nock("http://api.pusherapp.com")
|
|
104
106
|
.filteringPath(function (path) {
|
|
105
107
|
return path
|
|
@@ -107,18 +109,23 @@ describe("Pusher", function () {
|
|
|
107
109
|
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y")
|
|
108
110
|
})
|
|
109
111
|
.post(
|
|
110
|
-
"/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=
|
|
112
|
+
"/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=2e4f053f1c325dedbe21abd8f1852b53&auth_signature=Y",
|
|
111
113
|
{
|
|
112
114
|
name: "my_event",
|
|
113
115
|
data: '{"some":"data "}',
|
|
114
116
|
channels: ["test_channel"],
|
|
115
117
|
socket_id: "123.567",
|
|
118
|
+
info: "user_count,subscription_count",
|
|
116
119
|
}
|
|
117
120
|
)
|
|
118
121
|
.reply(200, "{}")
|
|
119
122
|
|
|
123
|
+
const params = {
|
|
124
|
+
socket_id: "123.567",
|
|
125
|
+
info: "user_count,subscription_count",
|
|
126
|
+
}
|
|
120
127
|
pusher
|
|
121
|
-
.trigger("test_channel", "my_event", { some: "data " },
|
|
128
|
+
.trigger("test_channel", "my_event", { some: "data " }, params)
|
|
122
129
|
.then(() => done())
|
|
123
130
|
.catch(done)
|
|
124
131
|
})
|
|
@@ -131,20 +138,31 @@ describe("Pusher", function () {
|
|
|
131
138
|
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y")
|
|
132
139
|
})
|
|
133
140
|
.post(
|
|
134
|
-
"/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=
|
|
141
|
+
"/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=d3a47b3241328a6432adf60c8e91b6fb&auth_signature=Y",
|
|
135
142
|
{
|
|
136
143
|
name: "my_event",
|
|
137
144
|
data: '{"some":"data "}',
|
|
138
145
|
channels: ["test_channel"],
|
|
146
|
+
info: "subscription_count",
|
|
139
147
|
}
|
|
140
148
|
)
|
|
141
|
-
.reply(200, "
|
|
149
|
+
.reply(200, '{"channels":{"test_channel":{"subscription_count":123}}}')
|
|
142
150
|
|
|
143
151
|
pusher
|
|
144
|
-
.trigger(
|
|
152
|
+
.trigger(
|
|
153
|
+
"test_channel",
|
|
154
|
+
"my_event",
|
|
155
|
+
{ some: "data " },
|
|
156
|
+
{ info: "subscription_count" }
|
|
157
|
+
)
|
|
145
158
|
.then((response) => {
|
|
146
159
|
expect(response.status).to.equal(200)
|
|
147
|
-
|
|
160
|
+
return response.text().then((body) => {
|
|
161
|
+
expect(body).to.equal(
|
|
162
|
+
'{"channels":{"test_channel":{"subscription_count":123}}}'
|
|
163
|
+
)
|
|
164
|
+
done()
|
|
165
|
+
})
|
|
148
166
|
})
|
|
149
167
|
.catch(done)
|
|
150
168
|
})
|
|
@@ -286,7 +304,12 @@ describe("Pusher", function () {
|
|
|
286
304
|
.reply(200, "{}")
|
|
287
305
|
|
|
288
306
|
pusher
|
|
289
|
-
.trigger(
|
|
307
|
+
.trigger(
|
|
308
|
+
"test_channel",
|
|
309
|
+
"my_event",
|
|
310
|
+
{ some: "data " },
|
|
311
|
+
{ socket_id: "123.567" }
|
|
312
|
+
)
|
|
290
313
|
.then(() => done())
|
|
291
314
|
.catch(done)
|
|
292
315
|
})
|
|
@@ -317,7 +340,12 @@ describe("Pusher", function () {
|
|
|
317
340
|
.reply(200)
|
|
318
341
|
|
|
319
342
|
pusher
|
|
320
|
-
.trigger(
|
|
343
|
+
.trigger(
|
|
344
|
+
"test_channel",
|
|
345
|
+
"my_event",
|
|
346
|
+
{ some: "data " },
|
|
347
|
+
{ socket_id: "123.567" }
|
|
348
|
+
)
|
|
321
349
|
.catch((error) => {
|
|
322
350
|
expect(error).to.be.a(Pusher.RequestError)
|
|
323
351
|
expect(error.message).to.equal("Request failed with an error")
|
|
@@ -415,14 +443,53 @@ describe("Pusher", function () {
|
|
|
415
443
|
.catch(done)
|
|
416
444
|
})
|
|
417
445
|
})
|
|
446
|
+
|
|
447
|
+
describe("#sendToUser", function () {
|
|
448
|
+
it("should trigger an event on #server-to-user-{userId}", function () {
|
|
449
|
+
sinon.stub(events, "trigger")
|
|
450
|
+
pusher.sendToUser("abc123", "halo", { foo: "bar" })
|
|
451
|
+
expect(events.trigger.called).to.be(true)
|
|
452
|
+
expect(events.trigger.getCall(0).args[1]).eql(["#server-to-user-abc123"])
|
|
453
|
+
expect(events.trigger.getCall(0).args[2]).equal("halo")
|
|
454
|
+
expect(events.trigger.getCall(0).args[3]).eql({ foo: "bar" })
|
|
455
|
+
events.trigger.restore()
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
it("should throw an error if user id is empty", function () {
|
|
459
|
+
expect(function () {
|
|
460
|
+
pusher.sendToUser("", "halo", { foo: "bar" })
|
|
461
|
+
}).to.throwError(function (e) {
|
|
462
|
+
expect(e).to.be.an(Error)
|
|
463
|
+
expect(e.message).to.equal("Invalid user id: ''")
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
|
|
467
|
+
it("should throw an error if user id is not a string", function () {
|
|
468
|
+
expect(function () {
|
|
469
|
+
pusher.sendToUser(123, "halo", { foo: "bar" })
|
|
470
|
+
}).to.throwError(function (e) {
|
|
471
|
+
expect(e).to.be.an(Error)
|
|
472
|
+
expect(e.message).to.equal("Invalid user id: '123'")
|
|
473
|
+
})
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
it("should throw an error if event name is longer than 200 characters", function () {
|
|
477
|
+
const event = new Array(202).join("x") // 201 characters
|
|
478
|
+
expect(function () {
|
|
479
|
+
pusher.sendToUser("abc123", event, { foo: "bar" })
|
|
480
|
+
}).to.throwError(function (e) {
|
|
481
|
+
expect(e).to.be.an(Error)
|
|
482
|
+
expect(e.message).to.equal("Too long event name: '" + event + "'")
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
})
|
|
418
486
|
})
|
|
419
487
|
|
|
420
488
|
describe("Pusher with encryptionMasterKey", function () {
|
|
421
489
|
let pusher
|
|
422
490
|
|
|
423
491
|
const testMasterKey = Buffer.from(
|
|
424
|
-
"01234567890123456789012345678901"
|
|
425
|
-
"binary"
|
|
492
|
+
"01234567890123456789012345678901"
|
|
426
493
|
).toString("base64")
|
|
427
494
|
|
|
428
495
|
beforeEach(function () {
|
package/.travis.yml
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
language: node_js
|
|
2
|
-
jobs:
|
|
3
|
-
include:
|
|
4
|
-
- node_js: 14
|
|
5
|
-
script:
|
|
6
|
-
- npm run lint
|
|
7
|
-
- npm test
|
|
8
|
-
- node_js: 12
|
|
9
|
-
script:
|
|
10
|
-
- npm run lint
|
|
11
|
-
- npm test
|
|
12
|
-
- node_js: 10
|
|
13
|
-
script:
|
|
14
|
-
- npm run lint
|
|
15
|
-
- npm test
|
|
16
|
-
- node_js: 8
|
|
17
|
-
script:
|
|
18
|
-
# eslint doesn't support node 8, but it's sufficient to check that the
|
|
19
|
-
# tests pass.
|
|
20
|
-
- npm test
|