Comparing version 4.0.4 to 5.0.0
@@ -12,9 +12,5 @@ module.exports = { | ||
}, | ||
globals: { | ||
Promise: false | ||
}, | ||
rules: { | ||
'arrow-parens': ['error', 'as-needed'], | ||
'comma-dangle': ['error', 'never'], | ||
'no-restricted-globals': ['error', 'Promise'], | ||
semi: ['error', 'never'], | ||
@@ -21,0 +17,0 @@ strict: ['error', 'never'] |
@@ -8,2 +8,6 @@ Change Log | ||
## [5.0.0](https://github.com/AndrewBarba/apns2/releases/tag/5.0.0) | ||
1. Update code to use async/await | ||
## [4.0.4](https://github.com/AndrewBarba/apns2/releases/tag/4.0.4) | ||
@@ -10,0 +14,0 @@ |
@@ -6,3 +6,3 @@ import { EventEmitter } from "events" | ||
send(notification: Notification): Promise<Notification> | ||
send(notifications: Notification[]): Promise<Notification[]> | ||
sendMany(notifications: Notification[]): Promise<Notification[]> | ||
} | ||
@@ -9,0 +9,0 @@ |
105
lib/apns.js
@@ -1,2 +0,1 @@ | ||
const _ = require('lodash') | ||
const Promise = require('bluebird') | ||
@@ -85,55 +84,32 @@ const { EventEmitter } = require('events') | ||
/** | ||
* @private | ||
* @method _createClientPool | ||
* @param {String} host | ||
* @param {Number} port | ||
* @return {Pool} | ||
*/ | ||
_createClientPool({ host, port, connections }) { | ||
return new Pool({ | ||
create: cb => new Http2Client(host, port).connect().asCallback(cb), | ||
validate: client => client.ready, | ||
destroy: client => client.destroy(), | ||
min: 0, | ||
max: connections | ||
}) | ||
} | ||
/** | ||
* @private | ||
* @method _acquireClient | ||
* @method send | ||
* @param {Array<Notification>|Notification} notifications | ||
* @return {Promise} | ||
*/ | ||
async _acquireClient() { | ||
return this._clients.acquire().promise | ||
async send(notifications) { | ||
if (Array.isArray(notifications)) { | ||
console.warn('#send(Array<Notification>) is deprecated. Please use #sendMany()') // eslint-disable-line no-console | ||
return this.sendMany(notifications) | ||
} else { | ||
return this._sendOne(notifications) | ||
} | ||
} | ||
/** | ||
* @private | ||
* @method _acquireClient | ||
* @method sendMany | ||
* @param {Array<Notification>} notifications | ||
* @return {Promise} | ||
*/ | ||
_releaseClient(client) { | ||
return this._clients.release(client) | ||
} | ||
/** | ||
* @method send | ||
* @param {Array<Notification>|Notification} notifications | ||
* @return {Promise} | ||
*/ | ||
async send(notifications) { | ||
if (!_.isArray(notifications)) { | ||
return this._send(notifications) | ||
} | ||
return Promise.map(notifications, async notification => { | ||
async sendMany(notifications) { | ||
let sender = async notification => { | ||
try { | ||
return await this._send(notification) | ||
} catch(error) { | ||
return await this._sendOne(notification) | ||
} catch (error) { | ||
return { error } | ||
} | ||
}, { | ||
} | ||
let options = { | ||
concurrency: this._concurrency | ||
}) | ||
} | ||
return Promise.map(notifications, sender, options) | ||
} | ||
@@ -143,7 +119,7 @@ | ||
* @private | ||
* @method _send | ||
* @method _sendOne | ||
* @param {Notification} notification | ||
* @return {Promise} | ||
*/ | ||
async _send(notification) { | ||
async _sendOne(notification) { | ||
let options = { | ||
@@ -166,6 +142,6 @@ path: `/${API_VERSION}/device/${encodeURIComponent(notification.deviceToken)}`, | ||
let body = JSON.stringify(notification.APNSOptions()) | ||
let client = await this._acquireClient() | ||
let client = await this._acquireClient() | ||
try { | ||
let body = JSON.stringify(notification.APNSOptions()) | ||
let res = await client.post(options, body) | ||
@@ -182,2 +158,37 @@ this._releaseClient(client) | ||
* @private | ||
* @method _createClientPool | ||
* @param {String} host | ||
* @param {Number} port | ||
* @return {Pool} | ||
*/ | ||
_createClientPool({ host, port, connections }) { | ||
return new Pool({ | ||
create: () => new Http2Client(host, port).connect(), | ||
validate: client => client.ready, | ||
destroy: client => client.destroy(), | ||
min: 0, | ||
max: connections | ||
}) | ||
} | ||
/** | ||
* @private | ||
* @method _acquireClient | ||
* @return {Promise} | ||
*/ | ||
async _acquireClient() { | ||
return this._clients.acquire().promise | ||
} | ||
/** | ||
* @private | ||
* @method _acquireClient | ||
* @return {Promise} | ||
*/ | ||
_releaseClient(client) { | ||
return this._clients.release(client) | ||
} | ||
/** | ||
* @private | ||
* @method _handleServerResponse | ||
@@ -184,0 +195,0 @@ * @param {ServerResponse} res |
const { URL } = require('url') | ||
const Promise = require('bluebird') | ||
const http2 = require('http2') | ||
@@ -50,3 +49,3 @@ | ||
*/ | ||
connect() { | ||
async connect() { | ||
return new Promise((resolve, reject) => { | ||
@@ -77,3 +76,3 @@ let session = http2.connect(this._url) | ||
*/ | ||
get(options) { | ||
async get(options) { | ||
options.method = `GET` | ||
@@ -86,3 +85,3 @@ return this.request(options) | ||
*/ | ||
post(options, body) { | ||
async post(options, body) { | ||
options.method = `POST` | ||
@@ -95,3 +94,3 @@ return this.request(options, body) | ||
*/ | ||
put(options, body) { | ||
async put(options, body) { | ||
options.method = `PUT` | ||
@@ -104,3 +103,3 @@ return this.request(options, body) | ||
*/ | ||
delete(options) { | ||
async delete(options) { | ||
options.method = `DELETE` | ||
@@ -118,3 +117,3 @@ return this.request(options) | ||
*/ | ||
request({ method, path, headers={} }, body=null) { | ||
async request({ method, path, headers={} }, body=null) { | ||
if (!method) throw new Error('method is required') | ||
@@ -121,0 +120,0 @@ if (!path) throw new Error('path is required') |
{ | ||
"name": "apns2", | ||
"version": "4.0.4", | ||
"version": "5.0.0", | ||
"description": "Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.", | ||
@@ -5,0 +5,0 @@ "author": "Andrew Barba <abarba.77@gmail.com>", |
APNS2 | ||
===== | ||
[![wercker status](https://app.wercker.com/status/0e705662e5c35d51a971764fe3e27814/s/master "wercker status")](https://app.wercker.com/project/byKey/0e705662e5c35d51a971764fe3e27814) | ||
[![npm version](https://badge.fury.io/js/apns2.svg)](https://badge.fury.io/js/apns2) | ||
@@ -39,7 +40,7 @@ [![Twitter](https://img.shields.io/badge/twitter-@andrew_barba-blue.svg?style=flat)](http://twitter.com/andrew_barba) | ||
client.send(bn).then(() => { | ||
// sent successfully | ||
}).catch(err => { | ||
try { | ||
await client.send(bn) | ||
} catch(err) { | ||
console.error(err.reason) | ||
}) | ||
} | ||
``` | ||
@@ -59,7 +60,7 @@ | ||
client.send(bn).then(() => { | ||
// sent successfully | ||
}).catch(err => { | ||
try { | ||
await client.send(bn) | ||
} catch(err) { | ||
console.error(err.reason) | ||
}) | ||
} | ||
``` | ||
@@ -76,7 +77,7 @@ | ||
client.send(sn).then(() => { | ||
// sent successfully | ||
}).catch(err => { | ||
try { | ||
await client.send(sn) | ||
} catch(err) { | ||
console.error(err.reason) | ||
}) | ||
} | ||
``` | ||
@@ -86,2 +87,21 @@ | ||
#### Many | ||
Send multiple notifications concurrently: | ||
```javascript | ||
const { BasicNotification } = require('apns2') | ||
let notifications = [ | ||
new BasicNotification(deviceToken1, 'Hello, World'), | ||
new BasicNotification(deviceToken2, 'Hello, World') | ||
] | ||
try { | ||
await client.sendMany(notifications) | ||
} catch(err) { | ||
console.error(err.reason) | ||
} | ||
``` | ||
#### Advanced | ||
@@ -98,7 +118,7 @@ | ||
client.send(notification).then(() => { | ||
// sent successfully | ||
}).catch(err => { | ||
try { | ||
await client.send(notification) | ||
} catch(err) { | ||
console.error(err.reason) | ||
}) | ||
} | ||
``` | ||
@@ -105,0 +125,0 @@ |
111
test/test.js
@@ -19,3 +19,3 @@ const fs = require('fs') | ||
before(() => { | ||
before(async () => { | ||
client = new HTTP2Client('www.google.com', 443) | ||
@@ -25,16 +25,10 @@ return client.connect() | ||
it('should make a get request', () => { | ||
return client.get({ | ||
path: '/' | ||
}).then(res => { | ||
res.statusCode.should.equal(200) | ||
}) | ||
it('should make a get request', async () => { | ||
let res = await client.get({ path: '/' }) | ||
res.statusCode.should.equal(200) | ||
}) | ||
it('should make a post request', () => { | ||
return client.post({ | ||
path: '/' | ||
}).then(res => { | ||
res.statusCode.should.equal(405) | ||
}) | ||
it('should make a post request', async () => { | ||
let res = await client.post({ path: '/' }) | ||
res.statusCode.should.equal(405) | ||
}) | ||
@@ -50,8 +44,9 @@ }) | ||
it('should not connect', () => { | ||
return client.connect() | ||
.then(() => { | ||
throw new Error('Failed') | ||
}) | ||
.catch(() => {}) | ||
it('should not connect', async () => { | ||
try { | ||
await client.connect() | ||
throw new Error('Should not have worked') | ||
} catch(err) { | ||
should.exist(err) | ||
} | ||
}) | ||
@@ -73,3 +68,6 @@ }) | ||
keyId: `7U6GT5Q49J`, | ||
signingKey: process.env.SK || fs.readFileSync(`${__dirname}/certs/token.p8`), | ||
signingKey: | ||
process.env.APNS_SIGNING_KEY ? | ||
process.env.APNS_SIGNING_KEY.replace(/\\n/gi, '\n') : | ||
fs.readFileSync(`${__dirname}/certs/token.p8`, 'utf8'), | ||
defaultTopic: `com.tablelist.Tablelist` | ||
@@ -79,3 +77,3 @@ }) | ||
it('should send a basic notification', () => { | ||
it('should send a basic notification', async () => { | ||
let basicNotification = new BasicNotification(deviceToken, `Hello, Basic`) | ||
@@ -85,3 +83,3 @@ return apns.send(basicNotification) | ||
it('should send a basic notification with options', () => { | ||
it('should send a basic notification with options', async () => { | ||
let basicNotification = new BasicNotification(deviceToken, `Hello, 1`, { | ||
@@ -93,3 +91,3 @@ badge: 1 | ||
it('should send a basic notification with additional data', () => { | ||
it('should send a basic notification with additional data', async () => { | ||
let basicNotification = new BasicNotification(deviceToken, `Hello, ICON`, { | ||
@@ -104,3 +102,3 @@ badge: 0, | ||
it('should send a silent notification', () => { | ||
it('should send a silent notification', async () => { | ||
let silentNotification = new SilentNotification(deviceToken) | ||
@@ -110,3 +108,3 @@ return apns.send(silentNotification) | ||
it('should send a notification', () => { | ||
it('should send a notification', async () => { | ||
let notification = new Notification(deviceToken, { | ||
@@ -122,50 +120,32 @@ aps: { | ||
it('should send both notifications', () => { | ||
it('should send both notifications', async () => { | ||
let basicNotification = new BasicNotification(deviceToken, `Hello, Multiple`) | ||
let silentNotification = new SilentNotification(deviceToken) | ||
return apns.send([basicNotification, silentNotification]).then(result => { | ||
should.exist(result) | ||
result.length.should.equal(2) | ||
}) | ||
let results = await apns.sendMany([basicNotification, silentNotification]) | ||
should.exist(results) | ||
results.length.should.equal(2) | ||
}) | ||
it('should send a lot of notifications', () => { | ||
let notifications = [ | ||
new BasicNotification(deviceToken, 'Hello 1'), | ||
new BasicNotification(deviceToken, 'Hello 2'), | ||
new BasicNotification(deviceToken, 'Hello 3'), | ||
new BasicNotification(deviceToken, 'Hello 4'), | ||
new BasicNotification(deviceToken, 'Hello 5'), | ||
new BasicNotification(deviceToken, 'Hello 6'), | ||
new BasicNotification(deviceToken, 'Hello 7'), | ||
new BasicNotification(deviceToken, 'Hello 8'), | ||
new BasicNotification(deviceToken, 'Hello 9'), | ||
new BasicNotification(deviceToken, 'Hello 10'), | ||
new BasicNotification(deviceToken, 'Hello 11'), | ||
new BasicNotification(deviceToken, 'Hello 12'), | ||
new BasicNotification(deviceToken, 'Hello 13'), | ||
new BasicNotification(deviceToken, 'Hello 14'), | ||
new BasicNotification(deviceToken, 'Hello 15'), | ||
new BasicNotification(deviceToken, 'Hello 16'), | ||
new BasicNotification(deviceToken, 'Hello 17'), | ||
new BasicNotification(deviceToken, 'Hello 18'), | ||
new BasicNotification(deviceToken, 'Hello 19'), | ||
new BasicNotification(deviceToken, 'Hello 20') | ||
] | ||
return apns.send(notifications).then(result => { | ||
should.exist(result) | ||
result.length.should.equal(notifications.length) | ||
}) | ||
it('should send a lot of notifications', async () => { | ||
let notifications = [] | ||
for (let i = 0; i < 500; i++) { | ||
notifications.push(new BasicNotification(deviceToken, `Hello #${i}`)) | ||
} | ||
let results = await apns.sendMany(notifications) | ||
should.exist(results) | ||
results.length.should.equal(notifications.length) | ||
}) | ||
it('should fail to send a notification', () => { | ||
it('should fail to send a notification', async () => { | ||
let noti = new BasicNotification(`fakedevicetoken`, `Hello, bad token`) | ||
return apns.send(noti).catch(err => { | ||
try { | ||
await apns.send(noti) | ||
throw new Error('Should not have sent notification') | ||
} catch(err) { | ||
should.exist(err) | ||
err.reason.should.equal(Errors.badDeviceToken) | ||
}) | ||
} | ||
}) | ||
it('should fail to send a notification and emit an error', done => { | ||
apns.once(Errors.error, err => { | ||
@@ -178,9 +158,6 @@ should.exist(err) | ||
let noti = new BasicNotification(`fakedevicetoken`, `Hello, bad token`) | ||
apns.send(noti).catch(err => { | ||
should.exist(err) | ||
}) | ||
apns.send(noti).catch(should.exist) | ||
}) | ||
it('should fail to send a notification and emit an error', done => { | ||
apns.once(Errors.badDeviceToken, err => { | ||
@@ -193,7 +170,5 @@ should.exist(err) | ||
let noti = new BasicNotification(`fakedevicetoken`, `Hello, bad token`) | ||
apns.send(noti).catch(err => { | ||
should.exist(err) | ||
}) | ||
apns.send(noti).catch(should.exist) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
169
62159
781
4