@parse/push-adapter 6.2.0 → 6.4.0

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.
@@ -1,167 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
-
9
- var _parse = require('parse');
10
-
11
- var _parse2 = _interopRequireDefault(_parse);
12
-
13
- var _npmlog = require('npmlog');
14
-
15
- var _npmlog2 = _interopRequireDefault(_npmlog);
16
-
17
- var _APNS = require('./APNS');
18
-
19
- var _APNS2 = _interopRequireDefault(_APNS);
20
-
21
- var _GCM = require('./GCM');
22
-
23
- var _GCM2 = _interopRequireDefault(_GCM);
24
-
25
- var _FCM = require('./FCM');
26
-
27
- var _FCM2 = _interopRequireDefault(_FCM);
28
-
29
- var _WEB = require('./WEB');
30
-
31
- var _WEB2 = _interopRequireDefault(_WEB);
32
-
33
- var _EXPO = require('./EXPO');
34
-
35
- var _EXPO2 = _interopRequireDefault(_EXPO);
36
-
37
- var _PushAdapterUtils = require('./PushAdapterUtils');
38
-
39
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
-
41
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
42
-
43
- var LOG_PREFIX = 'parse-server-push-adapter';
44
-
45
- var ParsePushAdapter = function () {
46
- function ParsePushAdapter() {
47
- var pushConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
48
-
49
- _classCallCheck(this, ParsePushAdapter);
50
-
51
- this.supportsPushTracking = true;
52
-
53
- this.validPushTypes = ['ios', 'osx', 'tvos', 'android', 'fcm', 'web', 'expo'];
54
- this.senderMap = {};
55
- // used in PushController for Dashboard Features
56
- this.feature = {
57
- immediatePush: true
58
- };
59
- var pushTypes = Object.keys(pushConfig);
60
-
61
- var _iteratorNormalCompletion = true;
62
- var _didIteratorError = false;
63
- var _iteratorError = undefined;
64
-
65
- try {
66
- for (var _iterator = pushTypes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
67
- var pushType = _step.value;
68
-
69
- // adapter may be passed as part of the parse-server initialization
70
- if (this.validPushTypes.indexOf(pushType) < 0 && pushType != 'adapter') {
71
- throw new _parse2.default.Error(_parse2.default.Error.PUSH_MISCONFIGURED, 'Push to ' + pushType + ' is not supported');
72
- }
73
- switch (pushType) {
74
- case 'ios':
75
- case 'tvos':
76
- case 'osx':
77
- if (pushConfig[pushType].hasOwnProperty('firebaseServiceAccount')) {
78
- this.senderMap[pushType] = new _FCM2.default(pushConfig[pushType], 'apple');
79
- } else {
80
- this.senderMap[pushType] = new _APNS2.default(pushConfig[pushType]);
81
- }
82
- break;
83
- case 'web':
84
- this.senderMap[pushType] = new _WEB2.default(pushConfig[pushType]);
85
- break;
86
- case 'expo':
87
- this.senderMap[pushType] = new _EXPO2.default(pushConfig[pushType]);
88
- break;
89
- case 'android':
90
- case 'fcm':
91
- if (pushConfig[pushType].hasOwnProperty('firebaseServiceAccount')) {
92
- this.senderMap[pushType] = new _FCM2.default(pushConfig[pushType], 'android');
93
- } else {
94
- this.senderMap[pushType] = new _GCM2.default(pushConfig[pushType]);
95
- }
96
- break;
97
- }
98
- }
99
- } catch (err) {
100
- _didIteratorError = true;
101
- _iteratorError = err;
102
- } finally {
103
- try {
104
- if (!_iteratorNormalCompletion && _iterator.return) {
105
- _iterator.return();
106
- }
107
- } finally {
108
- if (_didIteratorError) {
109
- throw _iteratorError;
110
- }
111
- }
112
- }
113
- }
114
-
115
- _createClass(ParsePushAdapter, [{
116
- key: 'getValidPushTypes',
117
- value: function getValidPushTypes() {
118
- return this.validPushTypes;
119
- }
120
- }, {
121
- key: 'send',
122
- value: function send(data, installations) {
123
- var _this = this;
124
-
125
- var deviceMap = (0, _PushAdapterUtils.classifyInstallations)(installations, this.validPushTypes);
126
- var sendPromises = [];
127
-
128
- var _loop = function _loop(pushType) {
129
- var sender = _this.senderMap[pushType];
130
- var devices = deviceMap[pushType];
131
-
132
- if (Array.isArray(devices) && devices.length > 0) {
133
- if (!sender) {
134
- _npmlog2.default.verbose(LOG_PREFIX, 'Can not find sender for push type ' + pushType + ', ' + data);
135
- var results = devices.map(function (device) {
136
- return Promise.resolve({
137
- device: device,
138
- transmitted: false,
139
- response: { 'error': 'Can not find sender for push type ' + pushType + ', ' + data }
140
- });
141
- });
142
- sendPromises.push(Promise.all(results));
143
- } else {
144
- sendPromises.push(sender.send(data, devices));
145
- }
146
- }
147
- };
148
-
149
- for (var pushType in deviceMap) {
150
- _loop(pushType);
151
- }
152
- return Promise.all(sendPromises).then(function (promises) {
153
- // flatten all
154
- return [].concat.apply([], promises);
155
- });
156
- }
157
- }], [{
158
- key: 'classifyInstallations',
159
- value: function classifyInstallations(installations, validTypes) {
160
- return (0, _PushAdapterUtils.classifyInstallations)(installations, validTypes);
161
- }
162
- }]);
163
-
164
- return ParsePushAdapter;
165
- }();
166
-
167
- exports.default = ParsePushAdapter;
@@ -1,95 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.classifyInstallations = classifyInstallations;
7
- exports.randomString = randomString;
8
-
9
- var _crypto = require('crypto');
10
-
11
- /**g
12
- * Classify the device token of installations based on its device type.
13
- * @param {Object} installations An array of installations
14
- * @param {Array} validPushTypes An array of valid push types(string)
15
- * @returns {Object} A map whose key is device type and value is an array of device
16
- */
17
- function classifyInstallations(installations, validPushTypes) {
18
- // Init deviceTokenMap, create a empty array for each valid pushType
19
- var deviceMap = {};
20
- var _iteratorNormalCompletion = true;
21
- var _didIteratorError = false;
22
- var _iteratorError = undefined;
23
-
24
- try {
25
- for (var _iterator = validPushTypes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
26
- var validPushType = _step.value;
27
-
28
- deviceMap[validPushType] = [];
29
- }
30
- } catch (err) {
31
- _didIteratorError = true;
32
- _iteratorError = err;
33
- } finally {
34
- try {
35
- if (!_iteratorNormalCompletion && _iterator.return) {
36
- _iterator.return();
37
- }
38
- } finally {
39
- if (_didIteratorError) {
40
- throw _iteratorError;
41
- }
42
- }
43
- }
44
-
45
- var _iteratorNormalCompletion2 = true;
46
- var _didIteratorError2 = false;
47
- var _iteratorError2 = undefined;
48
-
49
- try {
50
- for (var _iterator2 = installations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
51
- var installation = _step2.value;
52
-
53
- // No deviceToken, ignore
54
- if (!installation.deviceToken) {
55
- continue;
56
- }
57
- var devices = deviceMap[installation.pushType] || deviceMap[installation.deviceType] || null;
58
- if (Array.isArray(devices)) {
59
- devices.push({
60
- deviceToken: installation.deviceToken,
61
- deviceType: installation.deviceType,
62
- appIdentifier: installation.appIdentifier
63
- });
64
- }
65
- }
66
- } catch (err) {
67
- _didIteratorError2 = true;
68
- _iteratorError2 = err;
69
- } finally {
70
- try {
71
- if (!_iteratorNormalCompletion2 && _iterator2.return) {
72
- _iterator2.return();
73
- }
74
- } finally {
75
- if (_didIteratorError2) {
76
- throw _iteratorError2;
77
- }
78
- }
79
- }
80
-
81
- return deviceMap;
82
- }
83
-
84
- function randomString(size) {
85
- if (size === 0) {
86
- throw new Error('Zero-length randomString is useless.');
87
- }
88
- var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';
89
- var objectId = '';
90
- var bytes = (0, _crypto.randomBytes)(size);
91
- for (var i = 0; i < bytes.length; ++i) {
92
- objectId += chars[bytes.readUInt8(i) % chars.length];
93
- }
94
- return objectId;
95
- }
package/lib/WEB.js DELETED
@@ -1,204 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.WEB = undefined;
7
-
8
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
-
10
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
-
12
- var _parse = require('parse');
13
-
14
- var _parse2 = _interopRequireDefault(_parse);
15
-
16
- var _npmlog = require('npmlog');
17
-
18
- var _npmlog2 = _interopRequireDefault(_npmlog);
19
-
20
- var _webPush = require('web-push');
21
-
22
- var _webPush2 = _interopRequireDefault(_webPush);
23
-
24
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
- function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
27
-
28
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29
-
30
- var LOG_PREFIX = 'parse-server-push-adapter WEB';
31
-
32
- var WEB = exports.WEB = function () {
33
- /**
34
- * Create a new WEB push adapter.
35
- *
36
- * @param {Object} args https://github.com/web-push-libs/web-push#api-reference
37
- */
38
- function WEB(args) {
39
- _classCallCheck(this, WEB);
40
-
41
- if ((typeof args === 'undefined' ? 'undefined' : _typeof(args)) !== 'object' || !args.vapidDetails) {
42
- throw new _parse2.default.Error(_parse2.default.Error.PUSH_MISCONFIGURED, 'WEB Push Configuration is invalid');
43
- }
44
- this.options = args;
45
- }
46
-
47
- /**
48
- * Send web push notification request.
49
- *
50
- * @param {Object} data The data we need to send, the format is the same with api request body
51
- * @param {Array} devices An array of devices
52
- * @returns {Object} A promise which is resolved immediately
53
- */
54
-
55
-
56
- _createClass(WEB, [{
57
- key: 'send',
58
- value: function () {
59
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(data, devices) {
60
- var coreData, devicesMap, deviceTokens, resolvers, promises, length, response, results, sent, failed;
61
- return regeneratorRuntime.wrap(function _callee$(_context) {
62
- while (1) {
63
- switch (_context.prev = _context.next) {
64
- case 0:
65
- coreData = data && data.data;
66
-
67
- if (!(!coreData || !devices || !Array.isArray(devices))) {
68
- _context.next = 4;
69
- break;
70
- }
71
-
72
- _npmlog2.default.warn(LOG_PREFIX, 'invalid push payload');
73
- return _context.abrupt('return');
74
-
75
- case 4:
76
- devicesMap = devices.reduce(function (memo, device) {
77
- memo[device.deviceToken] = device;
78
- return memo;
79
- }, {});
80
- deviceTokens = Object.keys(devicesMap);
81
- resolvers = [];
82
- promises = deviceTokens.map(function () {
83
- return new Promise(function (resolve) {
84
- return resolvers.push(resolve);
85
- });
86
- });
87
- length = deviceTokens.length;
88
-
89
- _npmlog2.default.verbose(LOG_PREFIX, 'sending to ' + length + ' ' + (length > 1 ? 'devices' : 'device'));
90
-
91
- _context.next = 12;
92
- return WEB.sendNotifications(coreData, deviceTokens, this.options);
93
-
94
- case 12:
95
- response = _context.sent;
96
- results = response.results, sent = response.sent, failed = response.failed;
97
-
98
- if (sent) {
99
- _npmlog2.default.verbose(LOG_PREFIX, 'WEB Response: %d out of %d sent successfully', sent, results.length);
100
- }
101
- if (failed) {
102
- _npmlog2.default.error(LOG_PREFIX, 'send errored: %d out of %d failed with error %s', failed, results.length, 'push subscription has unsubscribed or expired.');
103
- }
104
- deviceTokens.forEach(function (token, index) {
105
- var resolve = resolvers[index];
106
- var _results$index = results[index],
107
- result = _results$index.result,
108
- error = _results$index.error;
109
-
110
- var device = devicesMap[token];
111
- device.deviceType = 'web';
112
- var resolution = {
113
- device: device,
114
- response: error || result,
115
- transmitted: !error
116
- };
117
- resolve(resolution);
118
- });
119
- return _context.abrupt('return', Promise.all(promises));
120
-
121
- case 18:
122
- case 'end':
123
- return _context.stop();
124
- }
125
- }
126
- }, _callee, this);
127
- }));
128
-
129
- function send(_x, _x2) {
130
- return _ref.apply(this, arguments);
131
- }
132
-
133
- return send;
134
- }()
135
-
136
- /**
137
- * Send multiple web push notification request.
138
- *
139
- * @param {Object} payload The data we need to send, the format is the same with api request body
140
- * @param {Array} deviceTokens An array of devicesTokens
141
- * @param {Object} options The options for the request
142
- * @returns {Object} A promise which is resolved immediately
143
- */
144
-
145
- }], [{
146
- key: 'sendNotifications',
147
- value: function () {
148
- var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(payload, deviceTokens, options) {
149
- var promises, allResults, response;
150
- return regeneratorRuntime.wrap(function _callee2$(_context2) {
151
- while (1) {
152
- switch (_context2.prev = _context2.next) {
153
- case 0:
154
- promises = deviceTokens.map(function (deviceToken) {
155
- if (typeof deviceToken === 'string') {
156
- deviceToken = JSON.parse(deviceToken);
157
- }
158
- if ((typeof payload === 'undefined' ? 'undefined' : _typeof(payload)) === 'object') {
159
- payload = JSON.stringify(payload);
160
- }
161
- return _webPush2.default.sendNotification(deviceToken, payload, options);
162
- });
163
- _context2.next = 3;
164
- return Promise.allSettled(promises);
165
-
166
- case 3:
167
- allResults = _context2.sent;
168
- response = {
169
- sent: 0,
170
- failed: 0,
171
- results: []
172
- };
173
-
174
- allResults.forEach(function (result) {
175
- if (result.status === 'fulfilled') {
176
- response.sent += 1;
177
- response.results.push({ result: result.value.statusCode });
178
- } else {
179
- response.failed += 1;
180
- response.results.push({ error: result.reason.body });
181
- }
182
- });
183
- return _context2.abrupt('return', response);
184
-
185
- case 7:
186
- case 'end':
187
- return _context2.stop();
188
- }
189
- }
190
- }, _callee2, this);
191
- }));
192
-
193
- function sendNotifications(_x3, _x4, _x5) {
194
- return _ref2.apply(this, arguments);
195
- }
196
-
197
- return sendNotifications;
198
- }()
199
- }]);
200
-
201
- return WEB;
202
- }();
203
-
204
- exports.default = WEB;
package/lib/index.js DELETED
@@ -1,52 +0,0 @@
1
- "use strict";
2
- // ParsePushAdapter is the default implementation of
3
- // PushAdapter, it uses GCM for android push, APNS for ios push.
4
- // WEB for web push.
5
-
6
- Object.defineProperty(exports, "__esModule", {
7
- value: true
8
- });
9
- exports.utils = exports.EXPO = exports.WEB = exports.GCM = exports.APNS = exports.ParsePushAdapter = undefined;
10
-
11
- var _npmlog = require('npmlog');
12
-
13
- var _npmlog2 = _interopRequireDefault(_npmlog);
14
-
15
- var _ParsePushAdapter = require('./ParsePushAdapter');
16
-
17
- var _ParsePushAdapter2 = _interopRequireDefault(_ParsePushAdapter);
18
-
19
- var _GCM = require('./GCM');
20
-
21
- var _GCM2 = _interopRequireDefault(_GCM);
22
-
23
- var _APNS = require('./APNS');
24
-
25
- var _APNS2 = _interopRequireDefault(_APNS);
26
-
27
- var _WEB = require('./WEB');
28
-
29
- var _WEB2 = _interopRequireDefault(_WEB);
30
-
31
- var _EXPO = require('./EXPO');
32
-
33
- var _EXPO2 = _interopRequireDefault(_EXPO);
34
-
35
- var _PushAdapterUtils = require('./PushAdapterUtils');
36
-
37
- var utils = _interopRequireWildcard(_PushAdapterUtils);
38
-
39
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
40
-
41
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
-
43
- /* istanbul ignore if */
44
- if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER_PUSH_ADAPTER) {
45
- _npmlog2.default.level = 'verbose';
46
- }exports.default = _ParsePushAdapter2.default;
47
- exports.ParsePushAdapter = _ParsePushAdapter2.default;
48
- exports.APNS = _APNS2.default;
49
- exports.GCM = _GCM2.default;
50
- exports.WEB = _WEB2.default;
51
- exports.EXPO = _EXPO2.default;
52
- exports.utils = utils;