Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump from upstream #214

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Update dependancies again (#4)
  • Loading branch information
dchymko authored Jan 11, 2024
commit 0c0a19f7d3eda388bc07ca75faa15e73e1237599
6 changes: 2 additions & 4 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = {
DEFAULT_SETTINGS: {
gcm: {
id: null // PUT YOUR GCM SERVER API KEY,

},
apn: {
// See options at https://github.com/node-apn/node-apn/blob/master/doc/provider.markdown
Expand Down Expand Up @@ -49,7 +48,6 @@ module.exports = {
client_id: null,
// PUT YOUR ADM CLIENT ID,
client_secret: null // PUT YOUR ADM CLIENT SECRET,

},
wns: {
client_id: null,
Expand All @@ -65,11 +63,11 @@ module.exports = {
subject: "< 'mailto' Address or URL >",
publicKey: '< URL Safe Base64 Encoded Public Key >',
privateKey: '< URL Safe Base64 Encoded Private Key >'
} // gcmAPIKey: '< GCM API Key >',
}
// gcmAPIKey: '< GCM API Key >',
// TTL: 2419200
// headers: { }
// contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >'

},
isAlwaysUseFCM: false
}
Expand Down
68 changes: 20 additions & 48 deletions lib/push-notifications.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
"use strict";

var _sendGCM = _interopRequireDefault(require("./sendGCM"));

var _sendAPN = _interopRequireDefault(require("./sendAPN"));

var _sendADM = _interopRequireDefault(require("./sendADM"));

var _sendWNS = _interopRequireDefault(require("./sendWNS"));

var _sendWeb = _interopRequireDefault(require("./sendWeb"));

var _constants = require("./constants");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /* eslint-disable import/no-import-module-exports */
class PN {
constructor(options) {
this.setOptions(options);
}

setOptions(opts) {
this.settings = _objectSpread(_objectSpread({}, _constants.DEFAULT_SETTINGS), opts);

if (this.apn) {
this.apn.shutdown();
}

this.apn = new _sendAPN.default(this.settings.apn);
}

sendWith(method, regIds, data, cb) {
return method(regIds, data, this.settings).then(results => {
(cb || (noop => noop))(null, results);
Expand All @@ -44,65 +30,57 @@ class PN {
return Promise.reject(error);
});
}

getPushMethodByRegId(regId) {
if (typeof regId === 'object' && (!regId.type || !regId.id)) {
return {
regId,
pushMethod: _constants.WEB_METHOD
};
}

if (typeof regId === 'object' && regId.id && regId.type) {
return {
regId: regId.id,
pushMethod: this.settings.isAlwaysUseFCM ? _constants.GCM_METHOD : regId.type
};
} // TODO: deprecated, remove of all cases below in v3.0
// and review test cases

}

// TODO: deprecated, remove of all cases below in v3.0
// and review test cases
if (this.settings.isAlwaysUseFCM) {
return {
regId,
pushMethod: _constants.GCM_METHOD
};
}

if (regId.substring(0, 4) === 'http') {
return {
regId,
pushMethod: _constants.WNS_METHOD
};
}

if (/^(amzn[0-9]*.adm)/i.test(regId)) {
return {
regId,
pushMethod: _constants.ADM_METHOD
};
}

if ((regId.length === 64 || regId.length === 160) && /^[a-fA-F0-9]+$/.test(regId)) {
return {
regId,
pushMethod: _constants.APN_METHOD
};
}

if (regId.length > 64) {
return {
regId,
pushMethod: _constants.GCM_METHOD
};
}

return {
regId,
pushMethod: _constants.UNKNOWN_METHOD
};
}

send(_regIds, data, callback) {
const promises = [];
const regIdsGCM = [];
Expand All @@ -111,13 +89,13 @@ class PN {
const regIdsADM = [];
const regIdsWebPush = [];
const regIdsUnk = [];
const regIds = Array.isArray(_regIds || []) ? _regIds || [] : [_regIds]; // Classify each pushId for corresponding device
const regIds = Array.isArray(_regIds || []) ? _regIds || [] : [_regIds];

// Classify each pushId for corresponding device
regIds.forEach(regIdOriginal => {
const _this$getPushMethodBy = this.getPushMethodByRegId(regIdOriginal),
regId = _this$getPushMethodBy.regId,
pushMethod = _this$getPushMethodBy.pushMethod;

regId = _this$getPushMethodBy.regId,
pushMethod = _this$getPushMethodBy.pushMethod;
if (pushMethod === _constants.WEB_METHOD) {
regIdsWebPush.push(regId);
} else if (pushMethod === _constants.GCM_METHOD) {
Expand All @@ -132,37 +110,36 @@ class PN {
regIdsUnk.push(regId);
}
});

try {
// Android GCM
if (regIdsGCM.length > 0) {
promises.push(this.sendWith(_sendGCM.default, regIdsGCM, data));
} // iOS APN

}

// iOS APN
if (regIdsAPN.length > 0) {
promises.push(this.sendWith(this.apn.sendAPN.bind(this.apn), regIdsAPN, data));
} // Microsoft WNS

}

// Microsoft WNS
if (regIdsWNS.length > 0) {
promises.push(this.sendWith(_sendWNS.default, regIdsWNS, data));
} // Amazon ADM

}

// Amazon ADM
if (regIdsADM.length > 0) {
promises.push(this.sendWith(_sendADM.default, regIdsADM, data));
} // Web Push

}

// Web Push
if (regIdsWebPush.length > 0) {
promises.push(this.sendWith(_sendWeb.default, regIdsWebPush, data));
}
} catch (err) {
promises.push(Promise.reject(err));
} // Unknown

}

// Unknown
if (regIdsUnk.length > 0) {
const results = {
method: 'unknown',
Expand All @@ -177,9 +154,9 @@ class PN {
});
});
promises.push(Promise.resolve(results));
} // No regIds detected

}

// No regIds detected
if (promises.length === 0) {
promises.push(Promise.resolve({
method: 'none',
Expand All @@ -188,22 +165,17 @@ class PN {
message: []
}));
}

return Promise.all(promises).then(results => {
const cb = callback || (noop => noop);

cb(null, results);
return results;
}).catch(err => {
const cb = callback || (noop => noop);

cb(err);
return Promise.reject(err);
});
}

}

module.exports = PN;
module.exports.WEB = _constants.WEB_METHOD;
module.exports.WNS = _constants.WNS_METHOD;
Expand Down
16 changes: 4 additions & 12 deletions lib/sendADM.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
"use strict";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const adm = require('node-adm');

const _require = require('./constants'),
ADM_METHOD = _require.ADM_METHOD;

ADM_METHOD = _require.ADM_METHOD;
const sendADM = (regIds, _data, settings) => {
const resumed = {
method: ADM_METHOD,
Expand All @@ -20,13 +15,11 @@ const sendADM = (regIds, _data, settings) => {
};
const promises = [];
const admSender = new adm.Sender(settings.adm);

const data = _objectSpread({}, _data);

const consolidationKey = data.consolidationKey,
expiry = data.expiry,
timeToLive = data.timeToLive,
custom = data.custom;
expiry = data.expiry,
timeToLive = data.timeToLive,
custom = data.custom;
delete data.consolidationKey;
delete data.expiry;
delete data.timeToLive;
Expand Down Expand Up @@ -54,5 +47,4 @@ const sendADM = (regIds, _data, settings) => {
});
return Promise.all(promises).then(() => resumed);
};

module.exports = sendADM;
22 changes: 2 additions & 20 deletions lib/sendAPN.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
"use strict";

const apn = require('@parse/node-apn');

const R = require('ramda');

const _require = require('./constants'),
DEFAULT_TTL = _require.DEFAULT_TTL,
APN_METHOD = _require.APN_METHOD;

DEFAULT_TTL = _require.DEFAULT_TTL,
APN_METHOD = _require.APN_METHOD;
const expiryFromTtl = ttl => ttl + Math.floor(Date.now() / 1000);

const extractExpiry = R.cond([[R.propIs(Number, 'expiry'), R.prop('expiry')], [R.propIs(Number, 'timeToLive'), ({
timeToLive
}) => expiryFromTtl(timeToLive)], [R.T, () => expiryFromTtl(DEFAULT_TTL)]]);

const getPropValueOrUndefinedIfIsSilent = (propName, data) => R.ifElse(R.propEq('silent', true), R.always(undefined), R.prop(propName))(data);

const toJSONorUndefined = R.tryCatch(JSON.parse, R.always(undefined));
const alertLocArgsToJSON = R.evolve({
alert: {
'title-loc-args': toJSONorUndefined,
'loc-args': toJSONorUndefined
}
});

const getDefaultAlert = data => ({
title: data.title,
body: data.body,
Expand All @@ -35,13 +28,9 @@ const getDefaultAlert = data => ({
'launch-image': data.launchImage,
action: data.action
});

const alertOrDefault = data => R.when(R.propSatisfies(R.isNil, 'alert'), R.assoc('alert', getDefaultAlert(data)));

const getParsedAlertOrDefault = data => R.pipe(alertOrDefault(data), alertLocArgsToJSON)(data);

const getDeviceTokenOrSelf = R.ifElse(R.has('device'), R.prop('device'), R.identity);

class APN {
constructor(settings) {
try {
Expand All @@ -51,13 +40,11 @@ class APN {
this.connection = null;
}
}

shutdown() {
if (this.connection) {
this.connection.shutdown();
}
}

sendAPN(regIds, data) {
const message = new apn.Notification({
retryLimit: data.retries || -1,
Expand All @@ -80,11 +67,9 @@ class APN {
pushType: data.pushType,
interruptionLevel: data.interruptionLevel
});

if (!this.connection) {
return Promise.reject(this.connectionError || new Error('Unknown error: APN connection not configured properly'));
}

return this.connection.send(message, regIds).then(response => {
const resumed = {
method: APN_METHOD,
Expand All @@ -102,7 +87,6 @@ class APN {
(response.failed || []).forEach(failure => {
// See https://github.com/node-apn/node-apn/blob/master/doc/provider.markdown#failed
resumed.failure += 1;

if (failure.error) {
// A transport-level error occurred (e.g. network problem)
resumed.message.push({
Expand All @@ -123,7 +107,5 @@ class APN {
return resumed;
});
}

}

module.exports = APN;
Loading